aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/iio
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2016-06-29 21:48:51 -0400
committerJonathan Cameron <jic23@kernel.org>2016-07-03 06:27:15 -0400
commitb26b4e91700ff45d033eeaac91597d6d479378a4 (patch)
tree3d71bd25de6154b155409b5b7637293e564769b2 /drivers/iio
parent17118843a563c681aaafb29621befba02f28c592 (diff)
iio: pressure: bmp280: add SPI interface driver
This patch mimics the SPI functionality found in the misc driver in drivers/misc/bh085-spi.c to make it possible to reuse the existing BMP280/BMP180/BMP085 driver with all clients of the other driver. The adoption is straight-forward since like the other driver, it is a simple matter of using regmap. This driver is also so obviously inspired/copied from the old misc driver in drivers/misc/bmp085.c that I just took the liberty to add in the authors of the other drivers + self in the core driver file. The MISC driver also supports a variant named "BMP181" so include that here to be complete in comparison to the old driver. The bus mapping code for SPI was written by Akinobu Mita. Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com> Tested-by: Akinobu Mita <akinobu.mita@gmail.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Jonathan Cameron <jic23@kernel.org>
Diffstat (limited to 'drivers/iio')
-rw-r--r--drivers/iio/pressure/Kconfig15
-rw-r--r--drivers/iio/pressure/Makefile1
-rw-r--r--drivers/iio/pressure/bmp280-core.c4
-rw-r--r--drivers/iio/pressure/bmp280-spi.c123
4 files changed, 140 insertions, 3 deletions
diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig
index 3d0d311acb1d..d130cdc78f43 100644
--- a/drivers/iio/pressure/Kconfig
+++ b/drivers/iio/pressure/Kconfig
@@ -7,17 +7,20 @@ menu "Pressure sensors"
7 7
8config BMP280 8config BMP280
9 tristate "Bosch Sensortec BMP180/BMP280 pressure sensor I2C driver" 9 tristate "Bosch Sensortec BMP180/BMP280 pressure sensor I2C driver"
10 depends on I2C 10 depends on (I2C || SPI_MASTER)
11 depends on !(BMP085_I2C=y || BMP085_I2C=m) 11 depends on !(BMP085_I2C=y || BMP085_I2C=m)
12 depends on !(BMP085_SPI=y || BMP085_SPI=m)
12 select REGMAP 13 select REGMAP
13 select BMP280_I2C if (I2C) 14 select BMP280_I2C if (I2C)
15 select BMP280_SPI if (SPI_MASTER)
14 help 16 help
15 Say yes here to build support for Bosch Sensortec BMP180 and BMP280 17 Say yes here to build support for Bosch Sensortec BMP180 and BMP280
16 pressure and temperature sensors. Also supports the BE280 with 18 pressure and temperature sensors. Also supports the BE280 with
17 an additional humidity sensor channel. 19 an additional humidity sensor channel.
18 20
19 To compile this driver as a module, choose M here: the modules 21 To compile this driver as a module, choose M here: the core module
20 will be called bmp280-i2c and bmp280. 22 will be called bmp280 and you will also get bmp280-i2c for I2C
23 and/or bmp280-spi for SPI support.
21 24
22config BMP280_I2C 25config BMP280_I2C
23 tristate 26 tristate
@@ -25,6 +28,12 @@ config BMP280_I2C
25 depends on I2C 28 depends on I2C
26 select REGMAP_I2C 29 select REGMAP_I2C
27 30
31config BMP280_SPI
32 tristate
33 depends on BMP280
34 depends on SPI_MASTER
35 select REGMAP
36
28config HID_SENSOR_PRESS 37config HID_SENSOR_PRESS
29 depends on HID_SENSOR_HUB 38 depends on HID_SENSOR_HUB
30 select IIO_BUFFER 39 select IIO_BUFFER
diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile
index 736f4305fe46..7f395bed5e88 100644
--- a/drivers/iio/pressure/Makefile
+++ b/drivers/iio/pressure/Makefile
@@ -6,6 +6,7 @@
6obj-$(CONFIG_BMP280) += bmp280.o 6obj-$(CONFIG_BMP280) += bmp280.o
7bmp280-objs := bmp280-core.o bmp280-regmap.o 7bmp280-objs := bmp280-core.o bmp280-regmap.o
8obj-$(CONFIG_BMP280_I2C) += bmp280-i2c.o 8obj-$(CONFIG_BMP280_I2C) += bmp280-i2c.o
9obj-$(CONFIG_BMP280_SPI) += bmp280-spi.o
9obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o 10obj-$(CONFIG_HID_SENSOR_PRESS) += hid-sensor-press.o
10obj-$(CONFIG_HP03) += hp03.o 11obj-$(CONFIG_HP03) += hp03.o
11obj-$(CONFIG_MPL115) += mpl115.o 12obj-$(CONFIG_MPL115) += mpl115.o
diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index f1b5f8c0ac8c..43bd0b07619c 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -1,5 +1,9 @@
1/* 1/*
2 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
3 * Copyright (c) 2012 Bosch Sensortec GmbH
4 * Copyright (c) 2012 Unixphere AB
2 * Copyright (c) 2014 Intel Corporation 5 * Copyright (c) 2014 Intel Corporation
6 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
3 * 7 *
4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor. 8 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
5 * 9 *
diff --git a/drivers/iio/pressure/bmp280-spi.c b/drivers/iio/pressure/bmp280-spi.c
new file mode 100644
index 000000000000..216e64b682bf
--- /dev/null
+++ b/drivers/iio/pressure/bmp280-spi.c
@@ -0,0 +1,123 @@
1/*
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");