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