blob: 216e64b682bf2a74d0a46ba080851437a0ff94b2 [file] [log] [blame]
Linus Walleijb26b4e92016-06-30 03:48:51 +02001/*
2 * SPI interface for the BMP280 driver
3 *
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>
10
11#include "bmp280.h"
12
13static int bmp280_regmap_spi_write(void *context, const void *data,
14 size_t count)
15{
16 struct device *dev = context;
17 struct spi_device *spi = to_spi_device(dev);
18 u8 buf[2];
19
20 memcpy(buf, data, 2);
21 /*
22 * The SPI register address (= full register address without bit 7) and
23 * the write command (bit7 = RW = '0')
24 */
25 buf[0] &= ~0x80;
26
27 return spi_write_then_read(spi, buf, 2, NULL, 0);
28}
29
30static int bmp280_regmap_spi_read(void *context, const void *reg,
31 size_t reg_size, void *val, size_t val_size)
32{
33 struct device *dev = context;
34 struct spi_device *spi = to_spi_device(dev);
35
36 return spi_write_then_read(spi, reg, reg_size, val, val_size);
37}
38
39static 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,
44};
45
46static int bmp280_spi_probe(struct spi_device *spi)
47{
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;
52
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;
58 }
59
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;
70 }
71
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);
79 }
80
81 return bmp280_common_probe(&spi->dev,
82 regmap,
83 id->driver_data,
84 id->name);
85}
86
87static int bmp280_spi_remove(struct spi_device *spi)
88{
89 return bmp280_common_remove(&spi->dev);
90}
91
92static const struct of_device_id bmp280_of_spi_match[] = {
93 { .compatible = "bosch,bmp085", },
94 { .compatible = "bosch,bmp180", },
95 { .compatible = "bosch,bmp181", },
96 { .compatible = "bosch,bmp280", },
97 { .compatible = "bosch,bme280", },
98 { },
99};
100MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
101
102static const struct spi_device_id bmp280_spi_id[] = {
103 { "bmp180", BMP180_CHIP_ID },
104 { "bmp181", BMP180_CHIP_ID },
105 { "bmp280", BMP280_CHIP_ID },
106 { "bme280", BME280_CHIP_ID },
107 { }
108};
109MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
110
111static struct spi_driver bmp280_spi_driver = {
112 .driver = {
113 .name = "bmp280",
114 .of_match_table = bmp280_of_spi_match,
115 },
116 .id_table = bmp280_spi_id,
117 .probe = bmp280_spi_probe,
118 .remove = bmp280_spi_remove,
119};
120module_spi_driver(bmp280_spi_driver);
121
122MODULE_DESCRIPTION("BMP280 SPI bus driver");
123MODULE_LICENSE("GPL");