bmx280thpi2c.c revision 1.2 1 /* $NetBSD: bmx280thpi2c.c,v 1.2 2025/09/12 02:22:25 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 2022 Brad Spencer <brad (at) anduin.eldar.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/cdefs.h>
20 __KERNEL_RCSID(0, "$NetBSD: bmx280thpi2c.c,v 1.2 2025/09/12 02:22:25 thorpej Exp $");
21
22 /*
23 * I2C driver for the Bosch BMP280 / BME280 sensor.
24 * Uses the common bmx280thp driver to do the real work.
25 */
26
27 #include <sys/param.h>
28 #include <sys/systm.h>
29 #include <sys/kernel.h>
30 #include <sys/device.h>
31 #include <sys/module.h>
32 #include <sys/conf.h>
33 #include <sys/sysctl.h>
34 #include <sys/mutex.h>
35 #include <sys/condvar.h>
36 #include <sys/pool.h>
37 #include <sys/kmem.h>
38
39 #include <dev/sysmon/sysmonvar.h>
40 #include <dev/i2c/i2cvar.h>
41 #include <dev/spi/spivar.h>
42 #include <dev/ic/bmx280reg.h>
43 #include <dev/ic/bmx280var.h>
44
45 /*
46 * XXX Should add configuration information for variants ...
47 * XXX E devices have humidity sensors, P devices do not.
48 */
49 static const struct device_compatible_entry compat_data[] = {
50 { .compat = "bosch,bmp280" },
51 { .compat = "bosch,bme280" },
52 #if 0
53 /*
54 * XXX Should also add support for:
55 * bosch,bmp085
56 * bosch,bmp180
57 * bosch,bmp380
58 * bosch,bmp580
59 */
60 #endif
61 DEVICE_COMPAT_EOL
62 };
63
64 extern void bmx280_attach(struct bmx280_sc *);
65
66 static int bmx280thpi2c_poke(i2c_tag_t, i2c_addr_t, bool);
67 static int bmx280thpi2c_match(device_t, cfdata_t, void *);
68 static void bmx280thpi2c_attach(device_t, device_t, void *);
69 static int bmx280thpi2c_detach(device_t, int);
70
71 #define BMX280_DEBUG
72 #ifdef BMX280_DEBUG
73 #define DPRINTF(s, l, x) \
74 do { \
75 if (l <= s->sc_bmx280debug) \
76 printf x; \
77 } while (/*CONSTCOND*/0)
78 #else
79 #define DPRINTF(s, l, x)
80 #endif
81
82 CFATTACH_DECL_NEW(bmx280thpi2c, sizeof(struct bmx280_sc),
83 bmx280thpi2c_match, bmx280thpi2c_attach, bmx280thpi2c_detach, NULL);
84
85 /* For the BMX280, a read consists of writing on the I2C bus
86 * a I2C START, I2C SLAVE address, then the starting register.
87 * If that works, then following will be another I2C START,
88 * I2C SLAVE address, followed by as many I2C reads that is
89 * desired and then a I2C STOP
90 */
91
92 static int
93 bmx280thpi2c_read_register_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
94 uint8_t *buf, size_t blen)
95 {
96 int error;
97
98 error = iic_exec(tag,I2C_OP_WRITE,addr,®,1,NULL,0,0);
99
100 if (error == 0) {
101 error = iic_exec(tag,I2C_OP_READ_WITH_STOP,addr,NULL,0,
102 buf,blen,0);
103 }
104
105 return error;
106 }
107
108 static int
109 bmx280thpi2c_read_register(struct bmx280_sc *sc, uint8_t reg,
110 uint8_t *buf, size_t blen)
111 {
112 int error;
113
114 KASSERT(blen > 0);
115
116 error = bmx280thpi2c_read_register_direct(sc->sc_tag, sc->sc_addr, reg,
117 buf, blen);
118
119 return error;
120 }
121
122 /* For the BMX280, a write consists of sending a I2C START, I2C SLAVE
123 * address and then pairs of registers and data until a I2C STOP is
124 * sent.
125 */
126
127 static int
128 bmx280thpi2c_write_register(struct bmx280_sc *sc,
129 uint8_t *buf, size_t blen)
130 {
131 int error;
132
133 KASSERT(blen > 0);
134 /* XXX - there should be a KASSERT for blen at least
135 being an even number */
136
137 error = iic_exec(sc->sc_tag,I2C_OP_WRITE_WITH_STOP,sc->sc_addr,NULL,0,
138 buf,blen,0);
139
140 return error;
141 }
142
143 static int
144 bmx280thpi2c_acquire_bus(struct bmx280_sc *sc)
145 {
146 return(iic_acquire_bus(sc->sc_tag, 0));
147 }
148
149 static void
150 bmx280thpi2c_release_bus(struct bmx280_sc *sc)
151 {
152 iic_release_bus(sc->sc_tag, 0);
153 }
154
155 static int
156 bmx280thpi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
157 {
158 uint8_t reg = BMX280_REGISTER_ID;
159 uint8_t buf[1];
160 int error;
161
162 error = bmx280thpi2c_read_register_direct(tag, addr, reg, buf, 1);
163 if (matchdebug) {
164 printf("poke X 1: %d\n", error);
165 }
166 return error;
167 }
168
169 static int
170 bmx280thpi2c_match(device_t parent, cfdata_t cf, void *aux)
171 {
172 struct i2c_attach_args *ia = aux;
173 int error, match_result;
174 const bool matchdebug = false;
175
176 if (iic_use_direct_match(ia, cf, compat_data, &match_result)) {
177 return match_result;
178 }
179
180 /* indirect config - check for configured address */
181 if (ia->ia_addr != BMX280_TYPICAL_ADDR_1 &&
182 ia->ia_addr != BMX280_TYPICAL_ADDR_2)
183 return 0;
184
185 /*
186 * Check to see if something is really at this i2c address. This will
187 * keep phantom devices from appearing
188 */
189 if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
190 if (matchdebug)
191 printf("in match acquire bus failed\n");
192 return 0;
193 }
194
195 error = bmx280thpi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
196 iic_release_bus(ia->ia_tag, 0);
197
198 return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
199 }
200
201 static void
202 bmx280thpi2c_attach(device_t parent, device_t self, void *aux)
203 {
204 struct bmx280_sc *sc;
205 struct i2c_attach_args *ia;
206
207 ia = aux;
208 sc = device_private(self);
209
210 sc->sc_dev = self;
211 sc->sc_tag = ia->ia_tag;
212 sc->sc_addr = ia->ia_addr;
213 sc->sc_bmx280debug = 0;
214 sc->sc_func_acquire_bus = &bmx280thpi2c_acquire_bus;
215 sc->sc_func_release_bus = &bmx280thpi2c_release_bus;
216 sc->sc_func_read_register = &bmx280thpi2c_read_register;
217 sc->sc_func_write_register = &bmx280thpi2c_write_register;
218
219 mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
220
221 /*
222 * XXX Need to get this data from the device tree:
223 *
224 * vddd-supply (a regulator)
225 * vdda-supply (a regulator)
226 * reset-gpios (a gpio, active-low reset)
227 */
228
229 bmx280_attach(sc);
230
231 return;
232 }
233
234 static int
235 bmx280thpi2c_detach(device_t self, int flags)
236 {
237 struct bmx280_sc *sc;
238
239 sc = device_private(self);
240
241 mutex_enter(&sc->sc_mutex);
242
243 /* Remove the sensors */
244 if (sc->sc_sme != NULL) {
245 sysmon_envsys_unregister(sc->sc_sme);
246 sc->sc_sme = NULL;
247 }
248 mutex_exit(&sc->sc_mutex);
249
250 /* Remove the sysctl tree */
251 sysctl_teardown(&sc->sc_bmx280log);
252
253 /* Remove the mutex */
254 mutex_destroy(&sc->sc_mutex);
255
256 return 0;
257 }
258
259 MODULE(MODULE_CLASS_DRIVER, bmx280thpi2c, "iic,bmx280thp");
260
261 #ifdef _MODULE
262 /* Like other drivers, we do this because the bmx280 common
263 * driver has the definitions already.
264 */
265 #undef CFDRIVER_DECL
266 #define CFDRIVER_DECL(name, class, attr)
267 #include "ioconf.c"
268 #endif
269
270 static int
271 bmx280thpi2c_modcmd(modcmd_t cmd, void *opaque)
272 {
273
274 switch (cmd) {
275 case MODULE_CMD_INIT:
276 #ifdef _MODULE
277 return config_init_component(cfdriver_ioconf_bmx280thpi2c,
278 cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
279 #else
280 return 0;
281 #endif
282 case MODULE_CMD_FINI:
283 #ifdef _MODULE
284 return config_fini_component(cfdriver_ioconf_bmx280thpi2c,
285 cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
286 #else
287 return 0;
288 #endif
289 default:
290 return ENOTTY;
291 }
292 }
293