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/module.h>
8 #include <linux/spi/spi.h>
10 #include <linux/regmap.h>
14 static int bmp280_regmap_spi_write(void *context, const void *data,
17 struct spi_device *spi = to_spi_device(context);
22 * The SPI register address (= full register address without bit 7) and
23 * the write command (bit7 = RW = '0')
27 return spi_write_then_read(spi, buf, 2, NULL, 0);
30 static int bmp280_regmap_spi_read(void *context, const void *reg,
31 size_t reg_size, void *val, size_t val_size)
33 struct spi_device *spi = to_spi_device(context);
35 return spi_write_then_read(spi, reg, reg_size, val, val_size);
38 static struct regmap_bus bmp280_regmap_bus = {
39 .write = bmp280_regmap_spi_write,
40 .read = bmp280_regmap_spi_read,
41 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
42 .val_format_endian_default = REGMAP_ENDIAN_BIG,
45 static int bmp280_spi_probe(struct spi_device *spi)
47 const struct spi_device_id *id = spi_get_device_id(spi);
48 const struct bmp280_chip_info *chip_info;
49 struct regmap *regmap;
52 spi->bits_per_word = 8;
55 dev_err(&spi->dev, "spi_setup failed!\n");
59 chip_info = spi_get_device_match_data(spi);
61 regmap = devm_regmap_init(&spi->dev,
64 chip_info->regmap_config);
66 dev_err(&spi->dev, "failed to allocate register map\n");
67 return PTR_ERR(regmap);
70 return bmp280_common_probe(&spi->dev,
77 static const struct of_device_id bmp280_of_spi_match[] = {
78 { .compatible = "bosch,bmp085", .data = &bmp180_chip_info },
79 { .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
80 { .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
81 { .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
82 { .compatible = "bosch,bme280", .data = &bmp280_chip_info },
83 { .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
84 { .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
87 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
89 static const struct spi_device_id bmp280_spi_id[] = {
90 { "bmp180", (kernel_ulong_t)&bmp180_chip_info },
91 { "bmp181", (kernel_ulong_t)&bmp180_chip_info },
92 { "bmp280", (kernel_ulong_t)&bmp280_chip_info },
93 { "bme280", (kernel_ulong_t)&bmp280_chip_info },
94 { "bmp380", (kernel_ulong_t)&bmp380_chip_info },
95 { "bmp580", (kernel_ulong_t)&bmp580_chip_info },
98 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
100 static struct spi_driver bmp280_spi_driver = {
103 .of_match_table = bmp280_of_spi_match,
104 .pm = pm_ptr(&bmp280_dev_pm_ops),
106 .id_table = bmp280_spi_id,
107 .probe = bmp280_spi_probe,
109 module_spi_driver(bmp280_spi_driver);
111 MODULE_DESCRIPTION("BMP280 SPI bus driver");
112 MODULE_LICENSE("GPL");
113 MODULE_IMPORT_NS(IIO_BMP280);