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 
85 	chip_info = spi_get_device_match_data(spi);
86 
87 	if (chip_info->spi_read_extra_byte)
88 		bmp_regmap_bus = &bmp380_regmap_bus;
89 	else
90 		bmp_regmap_bus = &bmp280_regmap_bus;
91 
92 	regmap = devm_regmap_init(&spi->dev,
93 				  bmp_regmap_bus,
94 				  &spi->dev,
95 				  chip_info->regmap_config);
96 	if (IS_ERR(regmap)) {
97 		dev_err(&spi->dev, "failed to allocate register map\n");
98 		return PTR_ERR(regmap);
99 	}
100 
101 	return bmp280_common_probe(&spi->dev,
102 				   regmap,
103 				   chip_info,
104 				   id->name,
105 				   spi->irq);
106 }
107 
108 static const struct of_device_id bmp280_of_spi_match[] = {
109 	{ .compatible = "bosch,bmp085", .data = &bmp085_chip_info },
110 	{ .compatible = "bosch,bmp180", .data = &bmp180_chip_info },
111 	{ .compatible = "bosch,bmp181", .data = &bmp180_chip_info },
112 	{ .compatible = "bosch,bmp280", .data = &bmp280_chip_info },
113 	{ .compatible = "bosch,bme280", .data = &bme280_chip_info },
114 	{ .compatible = "bosch,bmp380", .data = &bmp380_chip_info },
115 	{ .compatible = "bosch,bmp580", .data = &bmp580_chip_info },
116 	{ }
117 };
118 MODULE_DEVICE_TABLE(of, bmp280_of_spi_match);
119 
120 static const struct spi_device_id bmp280_spi_id[] = {
121 	{ "bmp085", (kernel_ulong_t)&bmp085_chip_info },
122 	{ "bmp180", (kernel_ulong_t)&bmp180_chip_info },
123 	{ "bmp181", (kernel_ulong_t)&bmp180_chip_info },
124 	{ "bmp280", (kernel_ulong_t)&bmp280_chip_info },
125 	{ "bme280", (kernel_ulong_t)&bme280_chip_info },
126 	{ "bmp380", (kernel_ulong_t)&bmp380_chip_info },
127 	{ "bmp580", (kernel_ulong_t)&bmp580_chip_info },
128 	{ }
129 };
130 MODULE_DEVICE_TABLE(spi, bmp280_spi_id);
131 
132 static struct spi_driver bmp280_spi_driver = {
133 	.driver = {
134 		.name = "bmp280",
135 		.of_match_table = bmp280_of_spi_match,
136 		.pm = pm_ptr(&bmp280_dev_pm_ops),
137 	},
138 	.id_table = bmp280_spi_id,
139 	.probe = bmp280_spi_probe,
140 };
141 module_spi_driver(bmp280_spi_driver);
142 
143 MODULE_DESCRIPTION("BMP280 SPI bus driver");
144 MODULE_LICENSE("GPL");
145 MODULE_IMPORT_NS("IIO_BMP280");
146