Skip to content

Commit a9dd9ba

Browse files
vamoiridjic23
authored andcommitted
iio: pressure: Fixes BMP38x and BMP390 SPI support
According to the datasheet of BMP38x and BMP390 devices, for an SPI read operation the first byte that is returned needs to be dropped, and the rest of the bytes are the actual data returned from the sensor. Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Fixes: 8d32930 ("iio: pressure: bmp280: Add support for BMP380 sensor family") Signed-off-by: Vasileios Amoiridis <vassilisamir@gmail.com> Acked-by: Angel Iglesias <ang.iglesiasg@gmail.com> Link: https://lore.kernel.org/r/20240219191359.18367-1-vassilisamir@gmail.com Cc: <Stable@vger.kernel.org> Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent 60caa8b commit a9dd9ba

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

drivers/iio/pressure/bmp280-spi.c

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*
55
* Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
66
*/
7+
#include <linux/bits.h>
78
#include <linux/module.h>
89
#include <linux/spi/spi.h>
910
#include <linux/err.h>
@@ -35,17 +36,54 @@ static int bmp280_regmap_spi_read(void *context, const void *reg,
3536
return spi_write_then_read(spi, reg, reg_size, val, val_size);
3637
}
3738

39+
static int bmp380_regmap_spi_read(void *context, const void *reg,
40+
size_t reg_size, void *val, size_t val_size)
41+
{
42+
struct spi_device *spi = to_spi_device(context);
43+
u8 rx_buf[4];
44+
ssize_t status;
45+
46+
/*
47+
* Maximum number of consecutive bytes read for a temperature or
48+
* pressure measurement is 3.
49+
*/
50+
if (val_size > 3)
51+
return -EINVAL;
52+
53+
/*
54+
* According to the BMP3xx datasheets, for a basic SPI read opertion,
55+
* the first byte needs to be dropped and the rest are the requested
56+
* data.
57+
*/
58+
status = spi_write_then_read(spi, reg, 1, rx_buf, val_size + 1);
59+
if (status)
60+
return status;
61+
62+
memcpy(val, rx_buf + 1, val_size);
63+
64+
return 0;
65+
}
66+
3867
static struct regmap_bus bmp280_regmap_bus = {
3968
.write = bmp280_regmap_spi_write,
4069
.read = bmp280_regmap_spi_read,
4170
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
4271
.val_format_endian_default = REGMAP_ENDIAN_BIG,
4372
};
4473

74+
static struct regmap_bus bmp380_regmap_bus = {
75+
.write = bmp280_regmap_spi_write,
76+
.read = bmp380_regmap_spi_read,
77+
.read_flag_mask = BIT(7),
78+
.reg_format_endian_default = REGMAP_ENDIAN_BIG,
79+
.val_format_endian_default = REGMAP_ENDIAN_BIG,
80+
};
81+
4582
static int bmp280_spi_probe(struct spi_device *spi)
4683
{
4784
const struct spi_device_id *id = spi_get_device_id(spi);
4885
const struct bmp280_chip_info *chip_info;
86+
struct regmap_bus *bmp_regmap_bus;
4987
struct regmap *regmap;
5088
int ret;
5189

@@ -58,8 +96,18 @@ static int bmp280_spi_probe(struct spi_device *spi)
5896

5997
chip_info = spi_get_device_match_data(spi);
6098

99+
switch (chip_info->chip_id[0]) {
100+
case BMP380_CHIP_ID:
101+
case BMP390_CHIP_ID:
102+
bmp_regmap_bus = &bmp380_regmap_bus;
103+
break;
104+
default:
105+
bmp_regmap_bus = &bmp280_regmap_bus;
106+
break;
107+
}
108+
61109
regmap = devm_regmap_init(&spi->dev,
62-
&bmp280_regmap_bus,
110+
bmp_regmap_bus,
63111
&spi->dev,
64112
chip_info->regmap_config);
65113
if (IS_ERR(regmap)) {

0 commit comments

Comments
 (0)