xref: /linux/drivers/iio/pressure/bmp280-spi.c (revision cb787f4ac0c2e439ea8d7e6387b925f74576bdf8)
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/bits.h>
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/regmap.h>
11 #include <linux/spi/spi.h>
12 
13 #include "bmp280.h"
14 
15 static int bmp280_regmap_spi_write(void *context, const void *data,
16 				   size_t count)
17 {
18 	struct spi_device *spi = to_spi_device(context);
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 
31 static int bmp280_regmap_spi_read(void *context, const void *reg,
32 				  size_t reg_size, void *val, size_t val_size)
33 {
34 	struct spi_device *spi = to_spi_device(context);
35 
36 	return spi_write_then_read(spi, reg, reg_size, val, val_size);
37 }
38 
39 static int bmp380_regmap_spi_read(void *context, const void *reg,
40 				  size_t reg_size, void *val, size_t val_size)
41 {
42 	struct spi_device *spi = to_spi_device(context);
43 	u8 rx_buf[BME280_BURST_READ_BYTES + 1];
44 	ssize_t status;
45 
46 	if (val_size > BME280_BURST_READ_BYTES)
47 		return -EINVAL;
48 
49 	/*
50 	 * According to the BMP3xx datasheets, for a basic SPI read opertion,
51 	 * the first byte needs to be dropped and the rest are the requested
52 	 * data.
53 	 */
54 	status = spi_write_then_read(spi, reg, 1, rx_buf, val_size + 1);
55 	if (status)
56 		return status;
57 
58 	memcpy(val, rx_buf + 1, val_size);
59 
60 	return 0;
61 }
62 
63 static const struct regmap_bus bmp280_regmap_bus = {
64 	.write = bmp280_regmap_spi_write,
65 	.read = bmp280_regmap_spi_read,
66 	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
67 	.val_format_endian_default = REGMAP_ENDIAN_BIG,
68 };
69 
70 static const struct regmap_bus bmp380_regmap_bus = {
71 	.write = bmp280_regmap_spi_write,
72 	.read = bmp380_regmap_spi_read,
73 	.read_flag_mask = BIT(7),
74 	.reg_format_endian_default = REGMAP_ENDIAN_BIG,
75 	.val_format_endian_default = REGMAP_ENDIAN_BIG,
76 };
77 
78 static int bmp280_spi_probe(struct spi_device *spi)
79 {
80 	const struct spi_device_id *id = spi_get_device_id(spi);
81 	const struct bmp280_chip_info *chip_info;
82 	struct regmap_bus const *bmp_regmap_bus;
83 	struct regmap *regmap;
84 	int ret;
85 
86 	spi->bits_per_word = 8;
87 	ret = spi_setup(spi);
88 	if (ret < 0) {
89 		dev_err(&spi->dev, "spi_setup failed!\n");
90 		return ret;
91 	}
92 
93 	chip_info = spi_get_device_match_data(spi);
94 
95 	if (chip_info->spi_read_extra_byte)
96 		bmp_regmap_bus = &bmp380_regmap_bus;
97 	else
98 		bmp_regmap_bus = &bmp280_regmap_bus;
99 
100 	regmap = devm_regmap_init(&spi->dev,
101 				  bmp_regmap_bus,
102 				  &spi->dev,
103 				  chip_info->regmap_config);
104 	if (IS_ERR(regmap)) {
105 		dev_err(&spi->dev, "failed to allocate register map\n");
106 		return PTR_ERR(regmap);
107 	}
108 
109 	return bmp280_common_probe(&spi->dev,
110 				   regmap,
111 				   chip_info,
112 				   id->name,
113 				   spi->irq);
114 }
115 
116 static const struct of_device_id bmp280_of_spi_match[] = {
117 	{ .compatible = "bosch,bmp085", .data = &bmp180_chip_info },
118 	{ .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
119 	{ .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
120 	{ .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
121 	{ .compatible = "bosch,bme280", .data = &bme280_chip_info },
122 	{ .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
123 	{ .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
124 	{ },
125 };
126 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
127 
128 static const struct spi_device_id bmp280_spi_id[] = {
129 	{ "bmp085", (kernel_ulong_t)&bmp180_chip_info },
130 	{ "bmp180", (kernel_ulong_t)&bmp180_chip_info },
131 	{ "bmp181", (kernel_ulong_t)&bmp180_chip_info },
132 	{ "bmp280", (kernel_ulong_t)&bmp280_chip_info },
133 	{ "bme280", (kernel_ulong_t)&bme280_chip_info },
134 	{ "bmp380", (kernel_ulong_t)&bmp380_chip_info },
135 	{ "bmp580", (kernel_ulong_t)&bmp580_chip_info },
136 	{ }
137 };
138 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
139 
140 static struct spi_driver bmp280_spi_driver = {
141 	.driver = {
142 		.name = "bmp280",
143 		.of_match_table = bmp280_of_spi_match,
144 		.pm = pm_ptr(&bmp280_dev_pm_ops),
145 	},
146 	.id_table = bmp280_spi_id,
147 	.probe = bmp280_spi_probe,
148 };
149 module_spi_driver(bmp280_spi_driver);
150 
151 MODULE_DESCRIPTION("BMP280 SPI bus driver");
152 MODULE_LICENSE("GPL");
153 MODULE_IMPORT_NS(IIO_BMP280);
154