1 // SPDX-License-Identifier: GPL-2.0-only
3 * SPI interface for the BMP280 driver
5 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
7 #include <linux/bits.h>
8 #include <linux/module.h>
9 #include <linux/spi/spi.h>
10 #include <linux/err.h>
11 #include <linux/regmap.h>
15 static int bmp280_regmap_spi_write(void *context, const void *data,
18 struct spi_device *spi = to_spi_device(context);
23 * The SPI register address (= full register address without bit 7) and
24 * the write command (bit7 = RW = '0')
28 return spi_write_then_read(spi, buf, 2, NULL, 0);
31 static int bmp280_regmap_spi_read(void *context, const void *reg,
32 size_t reg_size, void *val, size_t val_size)
34 struct spi_device *spi = to_spi_device(context);
36 return spi_write_then_read(spi, reg, reg_size, val, val_size);
39 static int bmp380_regmap_spi_read(void *context, const void *reg,
40 size_t reg_size, void *val, size_t val_size)
42 struct spi_device *spi = to_spi_device(context);
47 * Maximum number of consecutive bytes read for a temperature or
48 * pressure measurement is 3.
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
58 status = spi_write_then_read(spi, reg, 1, rx_buf, val_size + 1);
62 memcpy(val, rx_buf + 1, val_size);
67 static struct regmap_bus bmp280_regmap_bus = {
68 .write = bmp280_regmap_spi_write,
69 .read = bmp280_regmap_spi_read,
70 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
71 .val_format_endian_default = REGMAP_ENDIAN_BIG,
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,
82 static int bmp280_spi_probe(struct spi_device *spi)
84 const struct spi_device_id *id = spi_get_device_id(spi);
85 const struct bmp280_chip_info *chip_info;
86 struct regmap_bus *bmp_regmap_bus;
87 struct regmap *regmap;
90 spi->bits_per_word = 8;
93 dev_err(&spi->dev, "spi_setup failed!\n");
97 chip_info = spi_get_device_match_data(spi);
99 if (chip_info->spi_read_extra_byte)
100 bmp_regmap_bus = &bmp380_regmap_bus;
102 bmp_regmap_bus = &bmp280_regmap_bus;
104 regmap = devm_regmap_init(&spi->dev,
107 chip_info->regmap_config);
108 if (IS_ERR(regmap)) {
109 dev_err(&spi->dev, "failed to allocate register map\n");
110 return PTR_ERR(regmap);
113 return bmp280_common_probe(&spi->dev,
120 static const struct of_device_id bmp280_of_spi_match[] = {
121 { .compatible = "bosch,bmp085", .data = &bmp180_chip_info },
122 { .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
123 { .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
124 { .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
125 { .compatible = "bosch,bme280", .data = &bme280_chip_info },
126 { .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
127 { .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
130 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
132 static const struct spi_device_id bmp280_spi_id[] = {
133 { "bmp085", (kernel_ulong_t)&bmp180_chip_info },
134 { "bmp180", (kernel_ulong_t)&bmp180_chip_info },
135 { "bmp181", (kernel_ulong_t)&bmp180_chip_info },
136 { "bmp280", (kernel_ulong_t)&bmp280_chip_info },
137 { "bme280", (kernel_ulong_t)&bme280_chip_info },
138 { "bmp380", (kernel_ulong_t)&bmp380_chip_info },
139 { "bmp580", (kernel_ulong_t)&bmp580_chip_info },
142 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
144 static struct spi_driver bmp280_spi_driver = {
147 .of_match_table = bmp280_of_spi_match,
148 .pm = pm_ptr(&bmp280_dev_pm_ops),
150 .id_table = bmp280_spi_id,
151 .probe = bmp280_spi_probe,
153 module_spi_driver(bmp280_spi_driver);
155 MODULE_DESCRIPTION("BMP280 SPI bus driver");
156 MODULE_LICENSE("GPL");
157 MODULE_IMPORT_NS(IIO_BMP280);