2 * SPI interface for the BMP280 driver
4 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
6 #include <linux/module.h>
7 #include <linux/spi/spi.h>
9 #include <linux/regmap.h>
13 static int bmp280_regmap_spi_write(void *context
, const void *data
,
16 struct device
*dev
= context
;
17 struct spi_device
*spi
= to_spi_device(dev
);
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 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
;
53 spi
->bits_per_word
= 8;
56 dev_err(&spi
->dev
, "spi_setup failed!\n");
60 switch (id
->driver_data
) {
62 regmap_config
= &bmp180_regmap_config
;
66 regmap_config
= &bmp280_regmap_config
;
72 regmap
= devm_regmap_init(&spi
->dev
,
77 dev_err(&spi
->dev
, "failed to allocate register map\n");
78 return PTR_ERR(regmap
);
81 return bmp280_common_probe(&spi
->dev
,
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", },
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
= {
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");