blob: 625b86878ad88db05e79cb8211718f3e84cb06c1 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleijb26b4e92016-06-30 03:48:51 +02002/*
3 * SPI interface for the BMP280 driver
4 *
5 * Inspired by the older BMP085 driver drivers/misc/bmp085-spi.c
6 */
7#include <linux/module.h>
8#include <linux/spi/spi.h>
9#include <linux/err.h>
10#include <linux/regmap.h>
11
12#include "bmp280.h"
13
14static int bmp280_regmap_spi_write(void *context, const void *data,
15 size_t count)
16{
17 struct device *dev = context;
18 struct spi_device *spi = to_spi_device(dev);
19 u8 buf[2];
20
21 memcpy(buf, data, 2);
22 /*
23 * The SPI register address (= full register address without bit 7) and
24 * the write command (bit7 = RW = '0')
25 */
26 buf[0] &= ~0x80;
27
28 return spi_write_then_read(spi, buf, 2, NULL, 0);
29}
30
31static int bmp280_regmap_spi_read(void *context, const void *reg,
32 size_t reg_size, void *val, size_t val_size)
33{
34 struct device *dev = context;
35 struct spi_device *spi = to_spi_device(dev);
36
37 return spi_write_then_read(spi, reg, reg_size, val, val_size);
38}
39
40static 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,
45};
46
47static int bmp280_spi_probe(struct spi_device *spi)
48{
49 const struct spi_device_id *id = spi_get_device_id(spi);
50 struct regmap *regmap;
51 const struct regmap_config *regmap_config;
52 int ret;
53
54 spi->bits_per_word = 8;
55 ret = spi_setup(spi);
56 if (ret < 0) {
57 dev_err(&spi->dev, "spi_setup failed!\n");
58 return ret;
59 }
60
61 switch (id->driver_data) {
62 case BMP180_CHIP_ID:
63 regmap_config = &bmp180_regmap_config;
64 break;
65 case BMP280_CHIP_ID:
66 case BME280_CHIP_ID:
67 regmap_config = &bmp280_regmap_config;
68 break;
69 default:
70 return -EINVAL;
71 }
72
73 regmap = devm_regmap_init(&spi->dev,
74 &bmp280_regmap_bus,
75 &spi->dev,
76 regmap_config);
77 if (IS_ERR(regmap)) {
78 dev_err(&spi->dev, "failed to allocate register map\n");
79 return PTR_ERR(regmap);
80 }
81
82 return bmp280_common_probe(&spi->dev,
83 regmap,
84 id->driver_data,
Linus Walleijaae95392016-06-30 03:48:52 +020085 id->name,
86 spi->irq);
Linus Walleijb26b4e92016-06-30 03:48:51 +020087}
88
Linus Walleijb26b4e92016-06-30 03:48:51 +020089static const struct of_device_id bmp280_of_spi_match[] = {
90 { .compatible = "bosch,bmp085", },
91 { .compatible = "bosch,bmp180", },
92 { .compatible = "bosch,bmp181", },
93 { .compatible = "bosch,bmp280", },
94 { .compatible = "bosch,bme280", },
95 { },
96};
97MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
98
99static const struct spi_device_id bmp280_spi_id[] = {
100 { "bmp180", BMP180_CHIP_ID },
101 { "bmp181", BMP180_CHIP_ID },
102 { "bmp280", BMP280_CHIP_ID },
103 { "bme280", BME280_CHIP_ID },
104 { }
105};
106MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
107
108static struct spi_driver bmp280_spi_driver = {
109 .driver = {
110 .name = "bmp280",
111 .of_match_table = bmp280_of_spi_match,
Linus Walleij3d838112016-06-30 03:48:53 +0200112 .pm = &bmp280_dev_pm_ops,
Linus Walleijb26b4e92016-06-30 03:48:51 +0200113 },
114 .id_table = bmp280_spi_id,
115 .probe = bmp280_spi_probe,
Linus Walleijb26b4e92016-06-30 03:48:51 +0200116};
117module_spi_driver(bmp280_spi_driver);
118
119MODULE_DESCRIPTION("BMP280 SPI bus driver");
120MODULE_LICENSE("GPL");