bmx280thpi2c.c revision 1.2 1 1.2 thorpej /* $NetBSD: bmx280thpi2c.c,v 1.2 2025/09/12 02:22:25 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: bmx280thpi2c.c,v 1.2 2025/09/12 02:22:25 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.2 thorpej /*
46 1.2 thorpej * XXX Should add configuration information for variants ...
47 1.2 thorpej * XXX E devices have humidity sensors, P devices do not.
48 1.2 thorpej */
49 1.2 thorpej static const struct device_compatible_entry compat_data[] = {
50 1.2 thorpej { .compat = "bosch,bmp280" },
51 1.2 thorpej { .compat = "bosch,bme280" },
52 1.2 thorpej #if 0
53 1.2 thorpej /*
54 1.2 thorpej * XXX Should also add support for:
55 1.2 thorpej * bosch,bmp085
56 1.2 thorpej * bosch,bmp180
57 1.2 thorpej * bosch,bmp380
58 1.2 thorpej * bosch,bmp580
59 1.2 thorpej */
60 1.2 thorpej #endif
61 1.2 thorpej DEVICE_COMPAT_EOL
62 1.2 thorpej };
63 1.2 thorpej
64 1.1 brad extern void bmx280_attach(struct bmx280_sc *);
65 1.1 brad
66 1.1 brad static int bmx280thpi2c_poke(i2c_tag_t, i2c_addr_t, bool);
67 1.1 brad static int bmx280thpi2c_match(device_t, cfdata_t, void *);
68 1.1 brad static void bmx280thpi2c_attach(device_t, device_t, void *);
69 1.1 brad static int bmx280thpi2c_detach(device_t, int);
70 1.1 brad
71 1.1 brad #define BMX280_DEBUG
72 1.1 brad #ifdef BMX280_DEBUG
73 1.1 brad #define DPRINTF(s, l, x) \
74 1.1 brad do { \
75 1.1 brad if (l <= s->sc_bmx280debug) \
76 1.1 brad printf x; \
77 1.1 brad } while (/*CONSTCOND*/0)
78 1.1 brad #else
79 1.1 brad #define DPRINTF(s, l, x)
80 1.1 brad #endif
81 1.1 brad
82 1.1 brad CFATTACH_DECL_NEW(bmx280thpi2c, sizeof(struct bmx280_sc),
83 1.1 brad bmx280thpi2c_match, bmx280thpi2c_attach, bmx280thpi2c_detach, NULL);
84 1.1 brad
85 1.1 brad /* For the BMX280, a read consists of writing on the I2C bus
86 1.1 brad * a I2C START, I2C SLAVE address, then the starting register.
87 1.1 brad * If that works, then following will be another I2C START,
88 1.1 brad * I2C SLAVE address, followed by as many I2C reads that is
89 1.1 brad * desired and then a I2C STOP
90 1.1 brad */
91 1.1 brad
92 1.1 brad static int
93 1.1 brad bmx280thpi2c_read_register_direct(i2c_tag_t tag, i2c_addr_t addr, uint8_t reg,
94 1.1 brad uint8_t *buf, size_t blen)
95 1.1 brad {
96 1.1 brad int error;
97 1.1 brad
98 1.1 brad error = iic_exec(tag,I2C_OP_WRITE,addr,®,1,NULL,0,0);
99 1.1 brad
100 1.1 brad if (error == 0) {
101 1.1 brad error = iic_exec(tag,I2C_OP_READ_WITH_STOP,addr,NULL,0,
102 1.1 brad buf,blen,0);
103 1.1 brad }
104 1.1 brad
105 1.1 brad return error;
106 1.1 brad }
107 1.1 brad
108 1.1 brad static int
109 1.1 brad bmx280thpi2c_read_register(struct bmx280_sc *sc, uint8_t reg,
110 1.1 brad uint8_t *buf, size_t blen)
111 1.1 brad {
112 1.1 brad int error;
113 1.1 brad
114 1.1 brad KASSERT(blen > 0);
115 1.1 brad
116 1.1 brad error = bmx280thpi2c_read_register_direct(sc->sc_tag, sc->sc_addr, reg,
117 1.1 brad buf, blen);
118 1.1 brad
119 1.1 brad return error;
120 1.1 brad }
121 1.1 brad
122 1.1 brad /* For the BMX280, a write consists of sending a I2C START, I2C SLAVE
123 1.1 brad * address and then pairs of registers and data until a I2C STOP is
124 1.1 brad * sent.
125 1.1 brad */
126 1.1 brad
127 1.1 brad static int
128 1.1 brad bmx280thpi2c_write_register(struct bmx280_sc *sc,
129 1.1 brad uint8_t *buf, size_t blen)
130 1.1 brad {
131 1.1 brad int error;
132 1.1 brad
133 1.1 brad KASSERT(blen > 0);
134 1.1 brad /* XXX - there should be a KASSERT for blen at least
135 1.1 brad being an even number */
136 1.1 brad
137 1.1 brad error = iic_exec(sc->sc_tag,I2C_OP_WRITE_WITH_STOP,sc->sc_addr,NULL,0,
138 1.1 brad buf,blen,0);
139 1.1 brad
140 1.1 brad return error;
141 1.1 brad }
142 1.1 brad
143 1.1 brad static int
144 1.1 brad bmx280thpi2c_acquire_bus(struct bmx280_sc *sc)
145 1.1 brad {
146 1.1 brad return(iic_acquire_bus(sc->sc_tag, 0));
147 1.1 brad }
148 1.1 brad
149 1.1 brad static void
150 1.1 brad bmx280thpi2c_release_bus(struct bmx280_sc *sc)
151 1.1 brad {
152 1.1 brad iic_release_bus(sc->sc_tag, 0);
153 1.1 brad }
154 1.1 brad
155 1.1 brad static int
156 1.1 brad bmx280thpi2c_poke(i2c_tag_t tag, i2c_addr_t addr, bool matchdebug)
157 1.1 brad {
158 1.1 brad uint8_t reg = BMX280_REGISTER_ID;
159 1.1 brad uint8_t buf[1];
160 1.1 brad int error;
161 1.1 brad
162 1.1 brad error = bmx280thpi2c_read_register_direct(tag, addr, reg, buf, 1);
163 1.1 brad if (matchdebug) {
164 1.1 brad printf("poke X 1: %d\n", error);
165 1.1 brad }
166 1.1 brad return error;
167 1.1 brad }
168 1.1 brad
169 1.1 brad static int
170 1.2 thorpej bmx280thpi2c_match(device_t parent, cfdata_t cf, void *aux)
171 1.1 brad {
172 1.1 brad struct i2c_attach_args *ia = aux;
173 1.1 brad int error, match_result;
174 1.1 brad const bool matchdebug = false;
175 1.1 brad
176 1.2 thorpej if (iic_use_direct_match(ia, cf, compat_data, &match_result)) {
177 1.1 brad return match_result;
178 1.2 thorpej }
179 1.1 brad
180 1.1 brad /* indirect config - check for configured address */
181 1.1 brad if (ia->ia_addr != BMX280_TYPICAL_ADDR_1 &&
182 1.1 brad ia->ia_addr != BMX280_TYPICAL_ADDR_2)
183 1.1 brad return 0;
184 1.1 brad
185 1.1 brad /*
186 1.1 brad * Check to see if something is really at this i2c address. This will
187 1.1 brad * keep phantom devices from appearing
188 1.1 brad */
189 1.1 brad if (iic_acquire_bus(ia->ia_tag, 0) != 0) {
190 1.1 brad if (matchdebug)
191 1.1 brad printf("in match acquire bus failed\n");
192 1.1 brad return 0;
193 1.1 brad }
194 1.1 brad
195 1.1 brad error = bmx280thpi2c_poke(ia->ia_tag, ia->ia_addr, matchdebug);
196 1.1 brad iic_release_bus(ia->ia_tag, 0);
197 1.1 brad
198 1.1 brad return error == 0 ? I2C_MATCH_ADDRESS_AND_PROBE : 0;
199 1.1 brad }
200 1.1 brad
201 1.1 brad static void
202 1.1 brad bmx280thpi2c_attach(device_t parent, device_t self, void *aux)
203 1.1 brad {
204 1.1 brad struct bmx280_sc *sc;
205 1.1 brad struct i2c_attach_args *ia;
206 1.1 brad
207 1.1 brad ia = aux;
208 1.1 brad sc = device_private(self);
209 1.1 brad
210 1.1 brad sc->sc_dev = self;
211 1.1 brad sc->sc_tag = ia->ia_tag;
212 1.1 brad sc->sc_addr = ia->ia_addr;
213 1.1 brad sc->sc_bmx280debug = 0;
214 1.1 brad sc->sc_func_acquire_bus = &bmx280thpi2c_acquire_bus;
215 1.1 brad sc->sc_func_release_bus = &bmx280thpi2c_release_bus;
216 1.1 brad sc->sc_func_read_register = &bmx280thpi2c_read_register;
217 1.1 brad sc->sc_func_write_register = &bmx280thpi2c_write_register;
218 1.1 brad
219 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
220 1.1 brad
221 1.2 thorpej /*
222 1.2 thorpej * XXX Need to get this data from the device tree:
223 1.2 thorpej *
224 1.2 thorpej * vddd-supply (a regulator)
225 1.2 thorpej * vdda-supply (a regulator)
226 1.2 thorpej * reset-gpios (a gpio, active-low reset)
227 1.2 thorpej */
228 1.2 thorpej
229 1.1 brad bmx280_attach(sc);
230 1.1 brad
231 1.1 brad return;
232 1.1 brad }
233 1.1 brad
234 1.1 brad static int
235 1.1 brad bmx280thpi2c_detach(device_t self, int flags)
236 1.1 brad {
237 1.1 brad struct bmx280_sc *sc;
238 1.1 brad
239 1.1 brad sc = device_private(self);
240 1.1 brad
241 1.1 brad mutex_enter(&sc->sc_mutex);
242 1.1 brad
243 1.1 brad /* Remove the sensors */
244 1.1 brad if (sc->sc_sme != NULL) {
245 1.1 brad sysmon_envsys_unregister(sc->sc_sme);
246 1.1 brad sc->sc_sme = NULL;
247 1.1 brad }
248 1.1 brad mutex_exit(&sc->sc_mutex);
249 1.1 brad
250 1.1 brad /* Remove the sysctl tree */
251 1.1 brad sysctl_teardown(&sc->sc_bmx280log);
252 1.1 brad
253 1.1 brad /* Remove the mutex */
254 1.1 brad mutex_destroy(&sc->sc_mutex);
255 1.1 brad
256 1.1 brad return 0;
257 1.1 brad }
258 1.1 brad
259 1.1 brad MODULE(MODULE_CLASS_DRIVER, bmx280thpi2c, "iic,bmx280thp");
260 1.1 brad
261 1.1 brad #ifdef _MODULE
262 1.1 brad /* Like other drivers, we do this because the bmx280 common
263 1.1 brad * driver has the definitions already.
264 1.1 brad */
265 1.1 brad #undef CFDRIVER_DECL
266 1.1 brad #define CFDRIVER_DECL(name, class, attr)
267 1.1 brad #include "ioconf.c"
268 1.1 brad #endif
269 1.1 brad
270 1.1 brad static int
271 1.1 brad bmx280thpi2c_modcmd(modcmd_t cmd, void *opaque)
272 1.1 brad {
273 1.1 brad
274 1.1 brad switch (cmd) {
275 1.1 brad case MODULE_CMD_INIT:
276 1.1 brad #ifdef _MODULE
277 1.1 brad return config_init_component(cfdriver_ioconf_bmx280thpi2c,
278 1.1 brad cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
279 1.1 brad #else
280 1.1 brad return 0;
281 1.1 brad #endif
282 1.1 brad case MODULE_CMD_FINI:
283 1.1 brad #ifdef _MODULE
284 1.1 brad return config_fini_component(cfdriver_ioconf_bmx280thpi2c,
285 1.1 brad cfattach_ioconf_bmx280thpi2c, cfdata_ioconf_bmx280thpi2c);
286 1.1 brad #else
287 1.1 brad return 0;
288 1.1 brad #endif
289 1.1 brad default:
290 1.1 brad return ENOTTY;
291 1.1 brad }
292 1.1 brad }
293