Linux 4.16.11
[linux/fpc-iii.git] / drivers / iio / pressure / bmp280-spi.c
blob17bc95586f9e23ab7a3afb9f912553625435380a
1 /*
2 * SPI interface for the BMP280 driver
4 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
5 */
6 #include <linux/module.h>
7 #include <linux/spi/spi.h>
8 #include <linux/err.h>
9 #include <linux/regmap.h>
11 #include "bmp280.h"
13 static int bmp280_regmap_spi_write(void *context, const void *data,
14 size_t count)
16 struct device *dev = context;
17 struct spi_device *spi = to_spi_device(dev);
18 u8 buf[2];
20 memcpy(buf, data, 2);
22 * The SPI register address (= full register address without bit 7) and
23 * the write command (bit7 = RW = '0')
25 buf[0] &= ~0x80;
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 device *dev = context;
34 struct spi_device *spi = to_spi_device(dev);
36 return spi_write_then_read(spi, reg, reg_size, val, val_size);
39 static struct regmap_bus bmp280_regmap_bus = {
40 .write = bmp280_regmap_spi_write,
41 .read = bmp280_regmap_spi_read,
42 .reg_format_endian_default = REGMAP_ENDIAN_BIG,
43 .val_format_endian_default = REGMAP_ENDIAN_BIG,
46 static int bmp280_spi_probe(struct spi_device *spi)
48 const struct spi_device_id *id = spi_get_device_id(spi);
49 struct regmap *regmap;
50 const struct regmap_config *regmap_config;
51 int ret;
53 spi->bits_per_word = 8;
54 ret = spi_setup(spi);
55 if (ret < 0) {
56 dev_err(&spi->dev, "spi_setup failed!\n");
57 return ret;
60 switch (id->driver_data) {
61 case BMP180_CHIP_ID:
62 regmap_config = &bmp180_regmap_config;
63 break;
64 case BMP280_CHIP_ID:
65 case BME280_CHIP_ID:
66 regmap_config = &bmp280_regmap_config;
67 break;
68 default:
69 return -EINVAL;
72 regmap = devm_regmap_init(&spi->dev,
73 &bmp280_regmap_bus,
74 &spi->dev,
75 regmap_config);
76 if (IS_ERR(regmap)) {
77 dev_err(&spi->dev, "failed to allocate register map\n");
78 return PTR_ERR(regmap);
81 return bmp280_common_probe(&spi->dev,
82 regmap,
83 id->driver_data,
84 id->name,
85 spi->irq);
88 static int bmp280_spi_remove(struct spi_device *spi)
90 return bmp280_common_remove(&spi->dev);
93 static const struct of_device_id bmp280_of_spi_match[] = {
94 { .compatible = "bosch,bmp085", },
95 { .compatible = "bosch,bmp180", },
96 { .compatible = "bosch,bmp181", },
97 { .compatible = "bosch,bmp280", },
98 { .compatible = "bosch,bme280", },
99 { },
101 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
103 static const struct spi_device_id bmp280_spi_id[] = {
104 { "bmp180", BMP180_CHIP_ID },
105 { "bmp181", BMP180_CHIP_ID },
106 { "bmp280", BMP280_CHIP_ID },
107 { "bme280", BME280_CHIP_ID },
110 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
112 static struct spi_driver bmp280_spi_driver = {
113 .driver = {
114 .name = "bmp280",
115 .of_match_table = bmp280_of_spi_match,
116 .pm = &bmp280_dev_pm_ops,
118 .id_table = bmp280_spi_id,
119 .probe = bmp280_spi_probe,
120 .remove = bmp280_spi_remove,
122 module_spi_driver(bmp280_spi_driver);
124 MODULE_DESCRIPTION("BMP280 SPI bus driver");
125 MODULE_LICENSE("GPL");