xref: /linux/drivers/iio/pressure/bmp280-spi.c (revision e7d759f31ca295d589f7420719c311870bb3166f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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 
14 static int bmp280_regmap_spi_write(void *context, const void *data,
15                                    size_t count)
16 {
17 	struct spi_device *spi = to_spi_device(context);
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 
30 static int bmp280_regmap_spi_read(void *context, const void *reg,
31                                   size_t reg_size, void *val, size_t val_size)
32 {
33 	struct spi_device *spi = to_spi_device(context);
34 
35 	return spi_write_then_read(spi, reg, reg_size, val, val_size);
36 }
37 
38 static struct regmap_bus bmp280_regmap_bus = {
39 	.write = bmp280_regmap_spi_write,
40 	.read = bmp280_regmap_spi_read,
41 	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
42 	.val_format_endian_default = REGMAP_ENDIAN_BIG,
43 };
44 
45 static int bmp280_spi_probe(struct spi_device *spi)
46 {
47 	const struct spi_device_id *id = spi_get_device_id(spi);
48 	const struct bmp280_chip_info *chip_info;
49 	struct regmap *regmap;
50 	int ret;
51 
52 	spi->bits_per_word = 8;
53 	ret = spi_setup(spi);
54 	if (ret < 0) {
55 		dev_err(&spi->dev, "spi_setup failed!\n");
56 		return ret;
57 	}
58 
59 	chip_info = spi_get_device_match_data(spi);
60 
61 	regmap = devm_regmap_init(&spi->dev,
62 				  &bmp280_regmap_bus,
63 				  &spi->dev,
64 				  chip_info->regmap_config);
65 	if (IS_ERR(regmap)) {
66 		dev_err(&spi->dev, "failed to allocate register map\n");
67 		return PTR_ERR(regmap);
68 	}
69 
70 	return bmp280_common_probe(&spi->dev,
71 				   regmap,
72 				   chip_info,
73 				   id->name,
74 				   spi->irq);
75 }
76 
77 static const struct of_device_id bmp280_of_spi_match[] = {
78 	{ .compatible = "bosch,bmp085", .data = &bmp180_chip_info },
79 	{ .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
80 	{ .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
81 	{ .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
82 	{ .compatible = "bosch,bme280", .data = &bmp280_chip_info },
83 	{ .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
84 	{ .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
85 	{ },
86 };
87 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
88 
89 static const struct spi_device_id bmp280_spi_id[] = {
90 	{ "bmp180", (kernel_ulong_t)&bmp180_chip_info },
91 	{ "bmp181", (kernel_ulong_t)&bmp180_chip_info },
92 	{ "bmp280", (kernel_ulong_t)&bmp280_chip_info },
93 	{ "bme280", (kernel_ulong_t)&bmp280_chip_info },
94 	{ "bmp380", (kernel_ulong_t)&bmp380_chip_info },
95 	{ "bmp580", (kernel_ulong_t)&bmp580_chip_info },
96 	{ }
97 };
98 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
99 
100 static struct spi_driver bmp280_spi_driver = {
101 	.driver = {
102 		.name = "bmp280",
103 		.of_match_table = bmp280_of_spi_match,
104 		.pm = pm_ptr(&bmp280_dev_pm_ops),
105 	},
106 	.id_table = bmp280_spi_id,
107 	.probe = bmp280_spi_probe,
108 };
109 module_spi_driver(bmp280_spi_driver);
110 
111 MODULE_DESCRIPTION("BMP280 SPI bus driver");
112 MODULE_LICENSE("GPL");
113 MODULE_IMPORT_NS(IIO_BMP280);
114