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 device
*dev
= context
;
18 struct spi_device
*spi
= to_spi_device(dev
);
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 device
*dev
= context
;
35 struct spi_device
*spi
= to_spi_device(dev
);
37 return spi_write_then_read(spi
, reg
, reg_size
, val
, val_size
);
40 static struct regmap_bus bmp280_regmap_bus
= {
41 .write
= bmp280_regmap_spi_write
,
42 .read
= bmp280_regmap_spi_read
,
43 .reg_format_endian_default
= REGMAP_ENDIAN_BIG
,
44 .val_format_endian_default
= REGMAP_ENDIAN_BIG
,
47 static int bmp280_spi_probe(struct spi_device
*spi
)
49 const struct spi_device_id
*id
= spi_get_device_id(spi
);
50 struct regmap
*regmap
;
51 const struct regmap_config
*regmap_config
;
54 spi
->bits_per_word
= 8;
57 dev_err(&spi
->dev
, "spi_setup failed!\n");
61 switch (id
->driver_data
) {
63 regmap_config
= &bmp180_regmap_config
;
67 regmap_config
= &bmp280_regmap_config
;
73 regmap
= devm_regmap_init(&spi
->dev
,
78 dev_err(&spi
->dev
, "failed to allocate register map\n");
79 return PTR_ERR(regmap
);
82 return bmp280_common_probe(&spi
->dev
,
89 static int bmp280_spi_remove(struct spi_device
*spi
)
91 return bmp280_common_remove(&spi
->dev
);
94 static const struct of_device_id bmp280_of_spi_match
[] = {
95 { .compatible
= "bosch,bmp085", },
96 { .compatible
= "bosch,bmp180", },
97 { .compatible
= "bosch,bmp181", },
98 { .compatible
= "bosch,bmp280", },
99 { .compatible
= "bosch,bme280", },
102 MODULE_DEVICE_TABLE(of
, bmp280_of_spi_match
);
104 static const struct spi_device_id bmp280_spi_id
[] = {
105 { "bmp180", BMP180_CHIP_ID
},
106 { "bmp181", BMP180_CHIP_ID
},
107 { "bmp280", BMP280_CHIP_ID
},
108 { "bme280", BME280_CHIP_ID
},
111 MODULE_DEVICE_TABLE(spi
, bmp280_spi_id
);
113 static struct spi_driver bmp280_spi_driver
= {
116 .of_match_table
= bmp280_of_spi_match
,
117 .pm
= &bmp280_dev_pm_ops
,
119 .id_table
= bmp280_spi_id
,
120 .probe
= bmp280_spi_probe
,
121 .remove
= bmp280_spi_remove
,
123 module_spi_driver(bmp280_spi_driver
);
125 MODULE_DESCRIPTION("BMP280 SPI bus driver");
126 MODULE_LICENSE("GPL");