bmx280.c revision 1.3 1 1.3 thorpej /* $NetBSD: bmx280.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: bmx280.c,v 1.3 2025/09/13 15:55:45 thorpej Exp $");
21 1.1 brad
22 1.1 brad /*
23 1.1 brad * Common driver for the Bosch BMP280/BME280 temperature, humidity (sometimes) and
24 1.1 brad * (usually barometric) pressure sensor. Calls out to specific frontends to
25 1.1 brad * the move bits around.
26 1.1 brad */
27 1.1 brad
28 1.1 brad #include <sys/param.h>
29 1.1 brad #include <sys/systm.h>
30 1.1 brad #include <sys/kernel.h>
31 1.1 brad #include <sys/device.h>
32 1.1 brad #include <sys/module.h>
33 1.1 brad #include <sys/sysctl.h>
34 1.1 brad #include <sys/mutex.h>
35 1.1 brad #include <sys/proc.h>
36 1.1 brad
37 1.1 brad #include <dev/sysmon/sysmonvar.h>
38 1.1 brad #include <dev/spi/spivar.h>
39 1.1 brad #include <dev/i2c/i2cvar.h>
40 1.1 brad #include <dev/ic/bmx280reg.h>
41 1.1 brad #include <dev/ic/bmx280var.h>
42 1.1 brad
43 1.1 brad
44 1.1 brad static void bmx280_store_raw_blob_tp(struct bmx280_sc *, uint8_t *);
45 1.1 brad static void bmx280_store_raw_blob_h(struct bmx280_sc *, uint8_t *);
46 1.1 brad void bmx280_attach(struct bmx280_sc *);
47 1.1 brad static void bmx280_refresh(struct sysmon_envsys *, envsys_data_t *);
48 1.1 brad static int bmx280_verify_sysctl(SYSCTLFN_ARGS);
49 1.1 brad static int bmx280_verify_sysctl_osrs(SYSCTLFN_ARGS);
50 1.1 brad static int bmx280_verify_sysctl_irr(SYSCTLFN_ARGS);
51 1.1 brad
52 1.1 brad #define BMX280_DEBUG
53 1.1 brad #ifdef BMX280_DEBUG
54 1.1 brad #define DPRINTF(s, l, x) \
55 1.1 brad do { \
56 1.1 brad if (l <= s->sc_bmx280debug) \
57 1.1 brad printf x; \
58 1.1 brad } while (/*CONSTCOND*/0)
59 1.1 brad #else
60 1.1 brad #define DPRINTF(s, l, x)
61 1.1 brad #endif
62 1.1 brad
63 1.1 brad static struct bmx280_sensor bmx280_sensors[] = {
64 1.1 brad {
65 1.1 brad .desc = "temperature",
66 1.1 brad .type = ENVSYS_STEMP,
67 1.1 brad },
68 1.1 brad {
69 1.1 brad .desc = "pressure",
70 1.1 brad .type = ENVSYS_PRESSURE,
71 1.1 brad },
72 1.1 brad {
73 1.1 brad .desc = "humidity",
74 1.1 brad .type = ENVSYS_SRELHUMIDITY,
75 1.1 brad }
76 1.1 brad };
77 1.1 brad
78 1.1 brad static struct bmx280_osrs_list bmx280_osrs[] = {
79 1.1 brad {
80 1.1 brad .text = 1,
81 1.1 brad .mask = BMX280_OSRS_TP_VALUE_X1,
82 1.1 brad },
83 1.1 brad {
84 1.1 brad .text = 2,
85 1.1 brad .mask = BMX280_OSRS_TP_VALUE_X2,
86 1.1 brad },
87 1.1 brad {
88 1.1 brad .text = 4,
89 1.1 brad .mask = BMX280_OSRS_TP_VALUE_X4,
90 1.1 brad },
91 1.1 brad {
92 1.1 brad .text = 8,
93 1.1 brad .mask = BMX280_OSRS_TP_VALUE_X8,
94 1.1 brad },
95 1.1 brad {
96 1.1 brad .text = 16,
97 1.1 brad .mask = BMX280_OSRS_TP_VALUE_X16,
98 1.1 brad }
99 1.1 brad };
100 1.1 brad
101 1.1 brad static struct bmx280_irr_list bmx280_irr[] = {
102 1.1 brad {
103 1.1 brad .text = 1,
104 1.1 brad .mask = BMX280_FILTER_VALUE_OFF,
105 1.1 brad },
106 1.1 brad {
107 1.1 brad .text = 2,
108 1.1 brad .mask = BMX280_FILTER_VALUE_2,
109 1.1 brad },
110 1.1 brad {
111 1.1 brad .text = 5,
112 1.1 brad .mask = BMX280_FILTER_VALUE_5,
113 1.1 brad },
114 1.1 brad {
115 1.1 brad .text = 11,
116 1.1 brad .mask = BMX280_FILTER_VALUE_11,
117 1.1 brad },
118 1.1 brad {
119 1.1 brad .text = 22,
120 1.1 brad .mask = BMX280_FILTER_VALUE_22,
121 1.1 brad }
122 1.1 brad };
123 1.1 brad
124 1.1 brad static uint8_t
125 1.1 brad bmx280_osrs_text_to_mask(int t)
126 1.1 brad {
127 1.1 brad int i;
128 1.1 brad uint8_t m = 0;
129 1.1 brad
130 1.1 brad for (i = 0; i < __arraycount(bmx280_osrs); i++) {
131 1.1 brad if (t == bmx280_osrs[i].text) {
132 1.1 brad m = bmx280_osrs[i].mask;
133 1.1 brad break;
134 1.1 brad }
135 1.1 brad }
136 1.1 brad
137 1.1 brad return m;
138 1.1 brad }
139 1.1 brad
140 1.1 brad static uint8_t
141 1.1 brad bmx280_irr_text_to_mask(int t)
142 1.1 brad {
143 1.1 brad int i;
144 1.1 brad uint8_t m = 0;
145 1.1 brad
146 1.1 brad for (i = 0; i < __arraycount(bmx280_irr); i++) {
147 1.1 brad if (t == bmx280_irr[i].text) {
148 1.1 brad m = bmx280_irr[i].mask;
149 1.1 brad break;
150 1.1 brad }
151 1.1 brad }
152 1.1 brad
153 1.1 brad return m;
154 1.1 brad }
155 1.1 brad
156 1.1 brad int
157 1.1 brad bmx280_verify_sysctl(SYSCTLFN_ARGS)
158 1.1 brad {
159 1.1 brad int error, t;
160 1.1 brad struct sysctlnode node;
161 1.1 brad
162 1.1 brad node = *rnode;
163 1.1 brad t = *(int *)rnode->sysctl_data;
164 1.1 brad node.sysctl_data = &t;
165 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
166 1.1 brad if (error || newp == NULL)
167 1.1 brad return error;
168 1.1 brad
169 1.1 brad if (t < 0)
170 1.1 brad return EINVAL;
171 1.1 brad
172 1.1 brad *(int *)rnode->sysctl_data = t;
173 1.1 brad
174 1.1 brad return 0;
175 1.1 brad }
176 1.1 brad
177 1.1 brad int
178 1.1 brad bmx280_verify_sysctl_osrs(SYSCTLFN_ARGS)
179 1.1 brad {
180 1.1 brad struct sysctlnode node;
181 1.1 brad int error = 0, t;
182 1.1 brad size_t i;
183 1.1 brad
184 1.1 brad node = *rnode;
185 1.1 brad t = *(int *)rnode->sysctl_data;
186 1.1 brad node.sysctl_data = &t;
187 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
188 1.1 brad if (error || newp == NULL)
189 1.1 brad return error;
190 1.1 brad
191 1.1 brad for (i = 0; i < __arraycount(bmx280_osrs); i++) {
192 1.1 brad if (t == bmx280_osrs[i].text) {
193 1.1 brad break;
194 1.1 brad }
195 1.1 brad }
196 1.1 brad
197 1.1 brad if (i == __arraycount(bmx280_osrs))
198 1.1 brad return EINVAL;
199 1.1 brad
200 1.1 brad *(int *)rnode->sysctl_data = t;
201 1.1 brad
202 1.1 brad return error;
203 1.1 brad }
204 1.1 brad
205 1.1 brad int
206 1.1 brad bmx280_verify_sysctl_irr(SYSCTLFN_ARGS)
207 1.1 brad {
208 1.1 brad struct sysctlnode node;
209 1.1 brad int error = 0, t;
210 1.1 brad size_t i;
211 1.1 brad
212 1.1 brad node = *rnode;
213 1.1 brad t = *(int *)rnode->sysctl_data;
214 1.1 brad node.sysctl_data = &t;
215 1.1 brad error = sysctl_lookup(SYSCTLFN_CALL(&node));
216 1.1 brad if (error || newp == NULL)
217 1.1 brad return error;
218 1.1 brad
219 1.1 brad for (i = 0; i < __arraycount(bmx280_irr); i++) {
220 1.1 brad if (t == bmx280_irr[i].text) {
221 1.1 brad break;
222 1.1 brad }
223 1.1 brad }
224 1.1 brad
225 1.1 brad if (i == __arraycount(bmx280_irr))
226 1.1 brad return EINVAL;
227 1.1 brad
228 1.1 brad *(int *)rnode->sysctl_data = t;
229 1.1 brad
230 1.1 brad return error;
231 1.1 brad }
232 1.1 brad
233 1.1 brad /* The datasheet was pretty vague as to the byte order...
234 1.1 brad * in fact, down right deceptive...
235 1.1 brad */
236 1.1 brad
237 1.1 brad static void
238 1.1 brad bmx280_store_raw_blob_tp(struct bmx280_sc *sc, uint8_t *b) {
239 1.1 brad sc->sc_cal_blob.dig_T1 = (uint16_t)b[1] << 8;
240 1.1 brad sc->sc_cal_blob.dig_T1 = sc->sc_cal_blob.dig_T1 | (uint16_t)b[0];
241 1.1 brad sc->sc_cal_blob.dig_T2 = (int16_t)b[3] << 8;
242 1.1 brad sc->sc_cal_blob.dig_T2 = sc->sc_cal_blob.dig_T2 | (int16_t)b[2];
243 1.1 brad sc->sc_cal_blob.dig_T3 = (int16_t)b[5] << 8;
244 1.1 brad sc->sc_cal_blob.dig_T3 = sc->sc_cal_blob.dig_T3 | (int16_t)b[4];
245 1.1 brad
246 1.1 brad sc->sc_cal_blob.dig_P1 = (uint16_t)b[7] << 8;
247 1.1 brad sc->sc_cal_blob.dig_P1 = sc->sc_cal_blob.dig_P1 | (uint16_t)b[6];
248 1.1 brad sc->sc_cal_blob.dig_P2 = (int16_t)b[9] << 8;
249 1.1 brad sc->sc_cal_blob.dig_P2 = sc->sc_cal_blob.dig_P2 | (int16_t)b[8];
250 1.1 brad sc->sc_cal_blob.dig_P3 = (int16_t)b[11] << 8;
251 1.1 brad sc->sc_cal_blob.dig_P3 = sc->sc_cal_blob.dig_P3 | (int16_t)b[10];
252 1.1 brad sc->sc_cal_blob.dig_P4 = (int16_t)b[13] << 8;
253 1.1 brad sc->sc_cal_blob.dig_P4 = sc->sc_cal_blob.dig_P4 | (int16_t)b[12];
254 1.1 brad sc->sc_cal_blob.dig_P5 = (int16_t)b[15] << 8;
255 1.1 brad sc->sc_cal_blob.dig_P5 = sc->sc_cal_blob.dig_P5 | (int16_t)b[14];
256 1.1 brad sc->sc_cal_blob.dig_P6 = (int16_t)b[17] << 8;
257 1.1 brad sc->sc_cal_blob.dig_P6 = sc->sc_cal_blob.dig_P6 | (int16_t)b[16];
258 1.1 brad sc->sc_cal_blob.dig_P7 = (int16_t)b[19] << 8;
259 1.1 brad sc->sc_cal_blob.dig_P7 = sc->sc_cal_blob.dig_P7 | (int16_t)b[18];
260 1.1 brad sc->sc_cal_blob.dig_P8 = (int16_t)b[21] << 8;
261 1.1 brad sc->sc_cal_blob.dig_P8 = sc->sc_cal_blob.dig_P8 | (int16_t)b[20];
262 1.1 brad sc->sc_cal_blob.dig_P9 = (int16_t)b[23] << 8;
263 1.1 brad sc->sc_cal_blob.dig_P9 = sc->sc_cal_blob.dig_P9 | (int16_t)b[22];
264 1.1 brad }
265 1.1 brad
266 1.1 brad static void
267 1.1 brad bmx280_store_raw_blob_h(struct bmx280_sc *sc, uint8_t *b) {
268 1.1 brad sc->sc_cal_blob.dig_H1 = (uint8_t)b[0];
269 1.1 brad sc->sc_cal_blob.dig_H2 = (int16_t)b[2] << 8;
270 1.1 brad sc->sc_cal_blob.dig_H2 = sc->sc_cal_blob.dig_H2 | (int16_t)b[1];
271 1.1 brad sc->sc_cal_blob.dig_H3 = (uint8_t)b[3];
272 1.1 brad sc->sc_cal_blob.dig_H4 = ((int16_t)((b[4] << 4) | (b[5] & 0x0F)));
273 1.1 brad sc->sc_cal_blob.dig_H5 = (int16_t)b[6] << 4;
274 1.1 brad sc->sc_cal_blob.dig_H5 = sc->sc_cal_blob.dig_H5 | (((int16_t)b[5] & 0x00f0) >> 4);
275 1.1 brad sc->sc_cal_blob.dig_H6 = (int8_t)b[7];
276 1.1 brad }
277 1.1 brad
278 1.1 brad static int
279 1.1 brad bmx280_sysctl_init(struct bmx280_sc *sc)
280 1.1 brad {
281 1.1 brad int error;
282 1.1 brad const struct sysctlnode *cnode;
283 1.1 brad int sysctlroot_num, sysctlwait_num;
284 1.1 brad
285 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
286 1.1 brad 0, CTLTYPE_NODE, device_xname(sc->sc_dev),
287 1.1 brad SYSCTL_DESCR("bmx280 controls"), NULL, 0, NULL, 0, CTL_HW,
288 1.1 brad CTL_CREATE, CTL_EOL)) != 0)
289 1.1 brad return error;
290 1.1 brad
291 1.1 brad sysctlroot_num = cnode->sysctl_num;
292 1.1 brad
293 1.1 brad #ifdef BMX280_DEBUG
294 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
295 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "debug",
296 1.1 brad SYSCTL_DESCR("Debug level"), bmx280_verify_sysctl, 0,
297 1.1 brad &sc->sc_bmx280debug, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
298 1.1 brad CTL_EOL)) != 0)
299 1.1 brad return error;
300 1.1 brad
301 1.1 brad /* It would be nice to have a CTLTYPE_SHORT */
302 1.1 brad
303 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
304 1.1 brad CTLFLAG_READWRITE, CTLTYPE_BOOL, "dump_calibration",
305 1.1 brad SYSCTL_DESCR("Dumps the calibration values to the console"),
306 1.1 brad bmx280_verify_sysctl, 0,
307 1.1 brad &sc->sc_bmx280dump, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
308 1.1 brad CTL_EOL)) != 0)
309 1.1 brad return error;
310 1.1 brad #endif
311 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
312 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "readattempts",
313 1.1 brad SYSCTL_DESCR("Read attempts"), bmx280_verify_sysctl, 0,
314 1.1 brad &sc->sc_readattempts, 0, CTL_HW, sysctlroot_num, CTL_CREATE,
315 1.1 brad CTL_EOL)) != 0)
316 1.1 brad return error;
317 1.1 brad
318 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
319 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "osrs_t",
320 1.1 brad SYSCTL_DESCR("Temperature oversample"),
321 1.1 brad bmx280_verify_sysctl_osrs, 0, &sc->sc_osrs_t,
322 1.1 brad 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
323 1.1 brad return error;
324 1.1 brad
325 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
326 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "osrs_p",
327 1.1 brad SYSCTL_DESCR("Pressure oversample"),
328 1.1 brad bmx280_verify_sysctl_osrs, 0, &sc->sc_osrs_p,
329 1.1 brad 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
330 1.1 brad return error;
331 1.1 brad
332 1.1 brad if (sc->sc_has_humidity) {
333 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
334 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "osrs_h",
335 1.1 brad SYSCTL_DESCR("Humidity oversample"),
336 1.1 brad bmx280_verify_sysctl_osrs, 0, &sc->sc_osrs_h,
337 1.1 brad 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
338 1.1 brad return error;
339 1.1 brad }
340 1.1 brad
341 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
342 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "irr_samples",
343 1.1 brad SYSCTL_DESCR("IRR samples"),
344 1.1 brad bmx280_verify_sysctl_irr, 0, &sc->sc_irr_samples,
345 1.1 brad 0, CTL_HW, sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
346 1.1 brad return error;
347 1.1 brad
348 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
349 1.1 brad 0, CTLTYPE_NODE, "waitfactor",
350 1.1 brad SYSCTL_DESCR("bmx280 wait factors"), NULL, 0, NULL, 0, CTL_HW,
351 1.1 brad sysctlroot_num, CTL_CREATE, CTL_EOL)) != 0)
352 1.1 brad return error;
353 1.1 brad sysctlwait_num = cnode->sysctl_num;
354 1.1 brad
355 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
356 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "t",
357 1.1 brad SYSCTL_DESCR("Temperature wait multiplier"),
358 1.1 brad bmx280_verify_sysctl, 0, &sc->sc_waitfactor_t,
359 1.1 brad 0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
360 1.1 brad return error;
361 1.1 brad
362 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
363 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "p",
364 1.1 brad SYSCTL_DESCR("Pressure wait multiplier"),
365 1.1 brad bmx280_verify_sysctl, 0, &sc->sc_waitfactor_p,
366 1.1 brad 0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
367 1.1 brad return error;
368 1.1 brad
369 1.1 brad if (sc->sc_has_humidity) {
370 1.1 brad if ((error = sysctl_createv(&sc->sc_bmx280log, 0, NULL, &cnode,
371 1.1 brad CTLFLAG_READWRITE, CTLTYPE_INT, "h",
372 1.1 brad SYSCTL_DESCR("Humidity wait multiplier"),
373 1.1 brad bmx280_verify_sysctl, 0, &sc->sc_waitfactor_h,
374 1.1 brad 0, CTL_HW, sysctlroot_num, sysctlwait_num, CTL_CREATE, CTL_EOL)) != 0)
375 1.1 brad return error;
376 1.1 brad }
377 1.1 brad
378 1.1 brad return 0;
379 1.1 brad }
380 1.1 brad void
381 1.1 brad bmx280_attach(struct bmx280_sc *sc)
382 1.1 brad {
383 1.1 brad int error, i;
384 1.1 brad uint8_t reg, chip_id;
385 1.1 brad uint8_t buf[2];
386 1.1 brad
387 1.1 brad sc->sc_bmx280dump = false;
388 1.1 brad sc->sc_has_humidity = false;
389 1.1 brad sc->sc_readattempts = 25;
390 1.1 brad sc->sc_osrs_t = 1;
391 1.1 brad sc->sc_osrs_p = 4;
392 1.1 brad sc->sc_osrs_h = 1;
393 1.1 brad sc->sc_irr_samples = 1;
394 1.1 brad sc->sc_previous_irr = 0xff;
395 1.1 brad sc->sc_waitfactor_t = 6;
396 1.1 brad sc->sc_waitfactor_p = 2;
397 1.1 brad sc->sc_waitfactor_h = 2;
398 1.1 brad sc->sc_sme = NULL;
399 1.1 brad
400 1.1 brad aprint_normal("\n");
401 1.1 brad
402 1.1 brad mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_NONE);
403 1.1 brad sc->sc_numsensors = __arraycount(bmx280_sensors);
404 1.1 brad
405 1.1 brad if ((sc->sc_sme = sysmon_envsys_create()) == NULL) {
406 1.1 brad aprint_error_dev(sc->sc_dev,
407 1.1 brad "Unable to create sysmon structure\n");
408 1.1 brad sc->sc_sme = NULL;
409 1.1 brad return;
410 1.1 brad }
411 1.1 brad
412 1.3 thorpej error = sc->sc_funcs->acquire_bus(sc);
413 1.1 brad if (error) {
414 1.1 brad aprint_error_dev(sc->sc_dev, "Could not acquire the bus: %d\n",
415 1.1 brad error);
416 1.1 brad goto out;
417 1.1 brad }
418 1.1 brad
419 1.1 brad buf[0] = BMX280_REGISTER_RESET;
420 1.1 brad buf[1] = BMX280_TRIGGER_RESET;
421 1.3 thorpej error = sc->sc_funcs->write_reg(sc, buf, 2);
422 1.1 brad if (error) {
423 1.1 brad aprint_error_dev(sc->sc_dev, "Failed to reset chip: %d\n",
424 1.1 brad error);
425 1.1 brad }
426 1.1 brad
427 1.1 brad delay(30000);
428 1.1 brad
429 1.1 brad reg = BMX280_REGISTER_ID;
430 1.3 thorpej error = sc->sc_funcs->read_reg(sc, reg, &chip_id, 1);
431 1.1 brad if (error) {
432 1.1 brad aprint_error_dev(sc->sc_dev, "Failed to read ID: %d\n",
433 1.1 brad error);
434 1.1 brad }
435 1.1 brad
436 1.1 brad delay(1000);
437 1.1 brad
438 1.1 brad DPRINTF(sc, 2, ("%s: read ID value: %02x\n",
439 1.1 brad device_xname(sc->sc_dev), chip_id));
440 1.1 brad
441 1.1 brad if (chip_id == BMX280_ID_BME280) {
442 1.1 brad sc->sc_has_humidity = true;
443 1.1 brad }
444 1.1 brad
445 1.1 brad uint8_t raw_blob_tp[24];
446 1.1 brad reg = BMX280_REGISTER_DIG_T1;
447 1.3 thorpej error = sc->sc_funcs->read_reg(sc, reg, raw_blob_tp, 24);
448 1.1 brad if (error) {
449 1.1 brad aprint_error_dev(sc->sc_dev, "Failed to read the calibration registers for tp: %d\n",
450 1.1 brad error);
451 1.1 brad }
452 1.1 brad
453 1.1 brad if (sc->sc_bmx280debug > 0) {
454 1.1 brad for(int _d = 0;_d < 24;_d++) {
455 1.1 brad DPRINTF(sc, 0, ("%s: %d %02x\n",
456 1.1 brad device_xname(sc->sc_dev), _d, raw_blob_tp[_d]));
457 1.1 brad }
458 1.1 brad }
459 1.1 brad
460 1.1 brad bmx280_store_raw_blob_tp(sc,raw_blob_tp);
461 1.1 brad
462 1.1 brad if (sc->sc_has_humidity) {
463 1.1 brad uint8_t raw_blob_h[8];
464 1.1 brad
465 1.1 brad reg = BMX280_REGISTER_DIG_H1;
466 1.3 thorpej error = sc->sc_funcs->read_reg(sc, reg, raw_blob_h, 1);
467 1.1 brad if (error) {
468 1.1 brad aprint_error_dev(sc->sc_dev, "Failed to read the calibration registers for h1: %d\n",
469 1.1 brad error);
470 1.1 brad }
471 1.1 brad
472 1.1 brad reg = BMX280_REGISTER_DIG_H2;
473 1.3 thorpej error = sc->sc_funcs->read_reg(sc, reg, &raw_blob_h[1], 7);
474 1.1 brad if (error) {
475 1.1 brad aprint_error_dev(sc->sc_dev, "Failed to read the calibration registers for h2 - h6: %d\n",
476 1.1 brad error);
477 1.1 brad }
478 1.1 brad
479 1.1 brad if (sc->sc_bmx280debug > 0) {
480 1.1 brad for(int _d = 0;_d < 8;_d++) {
481 1.1 brad DPRINTF(sc, 0, ("%s: %d %02x\n",
482 1.1 brad device_xname(sc->sc_dev), _d, raw_blob_h[_d]));
483 1.1 brad }
484 1.1 brad }
485 1.1 brad
486 1.1 brad bmx280_store_raw_blob_h(sc,raw_blob_h);
487 1.1 brad }
488 1.1 brad
489 1.3 thorpej sc->sc_funcs->release_bus(sc);
490 1.1 brad
491 1.1 brad if (error != 0) {
492 1.1 brad aprint_error_dev(sc->sc_dev, "Unable to setup device\n");
493 1.1 brad goto out;
494 1.1 brad }
495 1.1 brad
496 1.2 brad if ((error = bmx280_sysctl_init(sc)) != 0) {
497 1.2 brad aprint_error_dev(sc->sc_dev, "Can't setup sysctl tree (%d)\n", error);
498 1.2 brad goto out;
499 1.2 brad }
500 1.2 brad
501 1.1 brad for (i = 0; i < sc->sc_numsensors; i++) {
502 1.1 brad if (sc->sc_has_humidity == false &&
503 1.1 brad bmx280_sensors[i].type == ENVSYS_SRELHUMIDITY) {
504 1.1 brad break;
505 1.1 brad }
506 1.1 brad
507 1.1 brad strlcpy(sc->sc_sensors[i].desc, bmx280_sensors[i].desc,
508 1.1 brad sizeof(sc->sc_sensors[i].desc));
509 1.1 brad
510 1.1 brad sc->sc_sensors[i].units = bmx280_sensors[i].type;
511 1.1 brad sc->sc_sensors[i].state = ENVSYS_SINVALID;
512 1.1 brad
513 1.1 brad DPRINTF(sc, 2, ("%s: registering sensor %d (%s)\n", __func__, i,
514 1.1 brad sc->sc_sensors[i].desc));
515 1.1 brad
516 1.1 brad error = sysmon_envsys_sensor_attach(sc->sc_sme,
517 1.1 brad &sc->sc_sensors[i]);
518 1.1 brad if (error) {
519 1.1 brad aprint_error_dev(sc->sc_dev,
520 1.1 brad "Unable to attach sensor %d: %d\n", i, error);
521 1.1 brad goto out;
522 1.1 brad }
523 1.1 brad }
524 1.1 brad
525 1.1 brad sc->sc_sme->sme_name = device_xname(sc->sc_dev);
526 1.1 brad sc->sc_sme->sme_cookie = sc;
527 1.1 brad sc->sc_sme->sme_refresh = bmx280_refresh;
528 1.1 brad
529 1.1 brad DPRINTF(sc, 2, ("bmx280_attach: registering with envsys\n"));
530 1.1 brad
531 1.1 brad if (sysmon_envsys_register(sc->sc_sme)) {
532 1.1 brad aprint_error_dev(sc->sc_dev,
533 1.1 brad "unable to register with sysmon\n");
534 1.1 brad sysmon_envsys_destroy(sc->sc_sme);
535 1.1 brad sc->sc_sme = NULL;
536 1.1 brad return;
537 1.1 brad }
538 1.1 brad
539 1.1 brad aprint_normal_dev(sc->sc_dev, "Bosch Sensortec %s, Chip ID: 0x%02x\n",
540 1.1 brad (chip_id == BMX280_ID_BMP280) ? "BMP280" : (chip_id == BMX280_ID_BME280) ? "BME280" : "Unknown chip",
541 1.1 brad chip_id);
542 1.1 brad
543 1.1 brad return;
544 1.1 brad out:
545 1.1 brad sysmon_envsys_destroy(sc->sc_sme);
546 1.1 brad sc->sc_sme = NULL;
547 1.1 brad }
548 1.1 brad
549 1.1 brad /* The conversion algorithms are taken from the BMP280 datasheet. The
550 1.1 brad * same algorithms are used with the BME280.
551 1.1 brad *
552 1.1 brad * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
553 1.1 brad *
554 1.1 brad * Section 3.11.3, page 21
555 1.1 brad *
556 1.1 brad */
557 1.1 brad
558 1.1 brad static int32_t
559 1.1 brad bmx280_compensate_T_int32(struct bmx280_calibration_blob *b,
560 1.1 brad int32_t adc_T,
561 1.1 brad int32_t *t_fine)
562 1.1 brad {
563 1.1 brad int32_t var1, var2, T;
564 1.1 brad var1 = ((((adc_T>>3) - ((int32_t)b->dig_T1<<1))) * ((int32_t)b->dig_T2)) >> 11;
565 1.1 brad var2 = (((((adc_T>>4) - ((int32_t)b->dig_T1)) * ((adc_T>>4) - ((int32_t)b->dig_T1))) >> 12) *
566 1.1 brad ((int32_t)b->dig_T3)) >> 14;
567 1.1 brad *t_fine = var1 + var2;
568 1.1 brad T = (*t_fine * 5 + 128) >> 8;
569 1.1 brad return T;
570 1.1 brad }
571 1.1 brad
572 1.1 brad /* Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 integer bits and 8 fractional bits).
573 1.1 brad * Output value of 24674867 represents 24674867/256 = 96386.2 Pa = 963.862 hPa
574 1.1 brad */
575 1.1 brad static uint32_t
576 1.1 brad bmx280_compensate_P_int64(struct bmx280_calibration_blob *b,
577 1.1 brad int32_t adc_P,
578 1.1 brad int32_t t_fine)
579 1.1 brad {
580 1.1 brad int64_t var1, var2, p;
581 1.1 brad var1 = ((int64_t)t_fine) - 128000;
582 1.1 brad var2 = var1 * var1 * (int64_t)b->dig_P6;
583 1.1 brad var2 = var2 + ((var1*(int64_t)b->dig_P5)<<17);
584 1.1 brad var2 = var2 + (((int64_t)b->dig_P4)<<35);
585 1.1 brad var1 = ((var1 * var1 * (int64_t)b->dig_P3)>>8) + ((var1 * (int64_t)b->dig_P2)<<12);
586 1.1 brad var1 = (((((int64_t)1)<<47)+var1))*((int64_t)b->dig_P1)>>33;
587 1.1 brad if (var1 == 0) {
588 1.1 brad return 0; /* avoid exception caused by division by zero */
589 1.1 brad }
590 1.1 brad p = 1048576-adc_P;
591 1.1 brad p = (((p<<31)-var2)*3125)/var1;
592 1.1 brad var1 = (((int64_t)b->dig_P9) * (p>>13) * (p>>13)) >> 25;
593 1.1 brad var2 = (((int64_t)b->dig_P8) * p) >> 19;
594 1.1 brad p = ((p + var1 + var2) >> 8) + (((int64_t)b->dig_P7)<<4);
595 1.1 brad return (uint32_t)p;
596 1.1 brad }
597 1.1 brad
598 1.1 brad /* Returns humidity in %RH as unsigned 32 bit integer in Q22.10 format (22 integer and 10 fractional bits).
599 1.1 brad *
600 1.1 brad * Output value of 47445 represents 47445/1024 = 46.333 %RH
601 1.1 brad */
602 1.1 brad static uint32_t
603 1.1 brad bmx280_compensate_H_int32(struct bmx280_calibration_blob *b,
604 1.1 brad int32_t adc_H,
605 1.1 brad int32_t t_fine)
606 1.1 brad {
607 1.1 brad int32_t v_x1_u32r;
608 1.1 brad v_x1_u32r = (t_fine - ((int32_t)76800));
609 1.1 brad v_x1_u32r = (((((adc_H << 14) - (((int32_t)b->dig_H4) << 20) - (((int32_t)b->dig_H5) *
610 1.1 brad v_x1_u32r)) + ((int32_t)16384)) >> 15) * (((((((v_x1_u32r *
611 1.1 brad ((int32_t)b->dig_H6)) >> 10) * (((v_x1_u32r * ((int32_t)b->dig_H3)) >> 11) +
612 1.1 brad ((int32_t)32768))) >> 10) + ((int32_t)2097152)) * ((int32_t)b->dig_H2) +
613 1.1 brad 8192) >> 14));
614 1.1 brad v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
615 1.1 brad ((int32_t)b->dig_H1)) >> 4));
616 1.1 brad v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
617 1.1 brad v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
618 1.1 brad return (uint32_t)(v_x1_u32r>>12);
619 1.1 brad }
620 1.1 brad
621 1.1 brad
622 1.1 brad static int
623 1.1 brad bmx280_set_control_and_trigger(struct bmx280_sc *sc,
624 1.1 brad uint8_t osrs_t_mask,
625 1.1 brad uint8_t osrs_p_mask,
626 1.1 brad uint8_t osrs_h_mask,
627 1.1 brad uint8_t filter_mask)
628 1.1 brad {
629 1.1 brad uint8_t cr[6];
630 1.1 brad int error;
631 1.1 brad int s = 0;
632 1.1 brad
633 1.1 brad cr[0] = cr[1] = cr[2] = cr[3] = cr[4] = cr[5] = 0;
634 1.1 brad
635 1.1 brad if (filter_mask != sc->sc_previous_irr) {
636 1.1 brad cr[s] = BMX280_REGISTER_CONFIG;
637 1.1 brad s++;
638 1.1 brad cr[s] = filter_mask << BMX280_CONFIG_FILTER_SHIFT;
639 1.1 brad s++;
640 1.1 brad sc->sc_previous_irr = filter_mask;
641 1.1 brad }
642 1.1 brad if (sc->sc_has_humidity) {
643 1.1 brad cr[s] = BMX280_REGISTER_CTRL_HUM;
644 1.1 brad s++;
645 1.1 brad cr[s] = osrs_h_mask;
646 1.1 brad s++;
647 1.1 brad }
648 1.1 brad cr[s] = BMX280_REGISTER_CTRL_MEAS;
649 1.1 brad s++;
650 1.1 brad cr[s] = osrs_t_mask << BMX280_CTRL_OSRS_T_SHIFT;
651 1.1 brad cr[s] = cr[s] | osrs_p_mask << BMX280_CTRL_OSRS_P_SHIFT;
652 1.1 brad cr[s] = cr[s] | BMX280_MODE_FORCED;
653 1.1 brad s++;
654 1.1 brad DPRINTF(sc, 2, ("%s: control register set up: num: %d ; %02x %02x ; %02x %02x ; %02x %02x\n",
655 1.1 brad device_xname(sc->sc_dev), s, cr[0], cr[1], cr[2], cr[3], cr[4], cr[5]));
656 1.3 thorpej error = sc->sc_funcs->write_reg(sc, cr, s);
657 1.1 brad if (error) {
658 1.1 brad DPRINTF(sc, 2, ("%s: write control registers: %d\n",
659 1.1 brad device_xname(sc->sc_dev), error));
660 1.1 brad error = EINVAL;
661 1.1 brad }
662 1.1 brad
663 1.1 brad /* The wait needed is not well documented, so this is somewhat of a guess.
664 1.1 brad * There is an attempt with this to only wait as long as needed.
665 1.1 brad */
666 1.1 brad
667 1.1 brad int p1, p2;
668 1.1 brad
669 1.1 brad p1 = (osrs_t_mask * sc->sc_waitfactor_t) + (osrs_p_mask * sc->sc_waitfactor_p);
670 1.1 brad if (sc->sc_has_humidity) {
671 1.1 brad p1 = p1 + (osrs_h_mask * sc->sc_waitfactor_h);
672 1.1 brad }
673 1.1 brad p2 = mstohz(p1);
674 1.1 brad if (p2 == 0) {
675 1.1 brad p2 = 1;
676 1.1 brad }
677 1.1 brad /* Be careful with this... the print itself will cause extra delay */
678 1.1 brad DPRINTF(sc, 2, ("%s: p1: %d ; %d\n",
679 1.1 brad device_xname(sc->sc_dev), p1, p2));
680 1.1 brad kpause("b280mea",false,p2,NULL);
681 1.1 brad
682 1.1 brad return error;
683 1.1 brad }
684 1.1 brad
685 1.1 brad static int
686 1.1 brad bmx280_wait_for_data(struct bmx280_sc *sc)
687 1.1 brad {
688 1.1 brad uint8_t reg;
689 1.1 brad uint8_t running = 99;
690 1.1 brad int c = sc->sc_readattempts;
691 1.1 brad int error = 0, ierror;
692 1.1 brad
693 1.1 brad reg = BMX280_REGISTER_STATUS;
694 1.1 brad do {
695 1.1 brad delay(1000);
696 1.3 thorpej ierror = sc->sc_funcs->read_reg(sc, reg, &running, 1);
697 1.1 brad if (ierror) {
698 1.1 brad DPRINTF(sc, 2, ("%s: Refresh failed to read back status: %d\n",
699 1.1 brad device_xname(sc->sc_dev), ierror));
700 1.1 brad error = EINVAL;
701 1.1 brad break;
702 1.1 brad }
703 1.1 brad
704 1.1 brad DPRINTF(sc, 2, ("%s: Refresh status read back: %02x\n",
705 1.1 brad device_xname(sc->sc_dev), running));
706 1.1 brad
707 1.1 brad c--;
708 1.1 brad } while (c > 0 && (running & BMX280_STATUS_MEASURING_MASK));
709 1.1 brad
710 1.1 brad return error;
711 1.1 brad }
712 1.1 brad
713 1.1 brad static int
714 1.1 brad bmx280_read_data(struct bmx280_sc *sc,
715 1.1 brad int32_t *temp,
716 1.1 brad int32_t *press,
717 1.1 brad int32_t *hum,
718 1.1 brad bool justtemp)
719 1.1 brad {
720 1.1 brad int error = 0, ierror;
721 1.1 brad int rlen, rtstart, rpstart, rhstart;
722 1.1 brad int x_temp, x_press, x_hum;
723 1.1 brad uint8_t raw_press_temp_hum[8], reg;
724 1.1 brad
725 1.1 brad raw_press_temp_hum[0] = raw_press_temp_hum[1] =
726 1.1 brad raw_press_temp_hum[2] = raw_press_temp_hum[3] =
727 1.1 brad raw_press_temp_hum[4] = raw_press_temp_hum[5] =
728 1.1 brad raw_press_temp_hum[6] = raw_press_temp_hum[7] = 0;
729 1.1 brad
730 1.1 brad if (justtemp) {
731 1.1 brad reg = BMX280_REGISTER_TEMP_MSB;
732 1.1 brad rlen = 3;
733 1.1 brad rtstart = 0;
734 1.1 brad rpstart = 0;
735 1.1 brad rhstart = 0;
736 1.1 brad } else {
737 1.1 brad reg = BMX280_REGISTER_PRESS_MSB;
738 1.1 brad if (sc->sc_has_humidity == false) {
739 1.1 brad rlen = 6;
740 1.1 brad } else {
741 1.1 brad rlen = 8;
742 1.1 brad }
743 1.1 brad rtstart = 3;
744 1.1 brad rpstart = 0;
745 1.1 brad rhstart = 6;
746 1.1 brad }
747 1.1 brad
748 1.1 brad DPRINTF(sc, 2, ("%s: read data: reg: %02x ; len: %d ; tstart: %d ; pstart: %d\n",
749 1.1 brad device_xname(sc->sc_dev), reg, rlen, rtstart, rpstart));
750 1.1 brad
751 1.3 thorpej ierror = sc->sc_funcs->read_reg(sc, reg, raw_press_temp_hum, rlen);
752 1.1 brad if (ierror) {
753 1.1 brad DPRINTF(sc, 2, ("%s: failed to read pressure and temp registers: %d\n",
754 1.1 brad device_xname(sc->sc_dev), ierror));
755 1.1 brad error = EINVAL;
756 1.1 brad goto out;
757 1.1 brad }
758 1.1 brad
759 1.1 brad DPRINTF(sc, 2, ("%s: raw pressure, temp and hum: %02x %02x %02x - %02x %02x %02x - %02x %02x\n",
760 1.1 brad device_xname(sc->sc_dev),
761 1.1 brad raw_press_temp_hum[0], raw_press_temp_hum[1], raw_press_temp_hum[2],
762 1.1 brad raw_press_temp_hum[3], raw_press_temp_hum[4], raw_press_temp_hum[5],
763 1.1 brad raw_press_temp_hum[6],raw_press_temp_hum[7]));
764 1.1 brad
765 1.1 brad x_temp = raw_press_temp_hum[rtstart] << 12;
766 1.1 brad x_temp = x_temp | (raw_press_temp_hum[rtstart + 1] << 4);
767 1.1 brad x_temp = x_temp | (raw_press_temp_hum[rtstart + 2] >> 4);
768 1.1 brad
769 1.1 brad DPRINTF(sc, 1, ("%s: intermediate temp: %d (%04x)\n",
770 1.1 brad device_xname(sc->sc_dev), x_temp, x_temp));
771 1.1 brad
772 1.1 brad *temp = x_temp;
773 1.1 brad
774 1.1 brad *hum = 0;
775 1.1 brad *press = 0;
776 1.1 brad
777 1.1 brad if (justtemp == false) {
778 1.1 brad x_press = raw_press_temp_hum[rpstart] << 12;
779 1.1 brad x_press = x_press | (raw_press_temp_hum[rpstart + 1] << 4);
780 1.1 brad x_press = x_press | (raw_press_temp_hum[rpstart + 2] >> 4);
781 1.1 brad
782 1.1 brad DPRINTF(sc, 1, ("%s: intermediate pressure: %d (%04x)\n",
783 1.1 brad device_xname(sc->sc_dev), x_press, x_press));
784 1.1 brad *press = x_press;
785 1.1 brad }
786 1.1 brad if (sc->sc_has_humidity) {
787 1.1 brad x_hum = raw_press_temp_hum[rhstart] << 8;
788 1.1 brad x_hum = x_hum | raw_press_temp_hum[rhstart + 1];
789 1.1 brad
790 1.1 brad DPRINTF(sc, 1, ("%s: intermediate humidity: %d (%02x)\n",
791 1.1 brad device_xname(sc->sc_dev), x_hum, x_hum));
792 1.1 brad *hum = x_hum;
793 1.1 brad }
794 1.1 brad
795 1.1 brad out:
796 1.1 brad return error;
797 1.1 brad }
798 1.1 brad
799 1.1 brad static void
800 1.1 brad bmx280_refresh(struct sysmon_envsys * sme, envsys_data_t * edata)
801 1.1 brad {
802 1.1 brad struct bmx280_sc *sc;
803 1.1 brad sc = sme->sme_cookie;
804 1.1 brad int error = 0;
805 1.1 brad int32_t t_fine;
806 1.1 brad int32_t m_temp, m_press, m_hum;
807 1.1 brad int32_t comp_temp;
808 1.1 brad uint32_t comp_press;
809 1.1 brad uint32_t comp_hum;
810 1.1 brad edata->state = ENVSYS_SINVALID;
811 1.1 brad
812 1.1 brad /* Ya... just do this on a refresh... */
813 1.1 brad
814 1.1 brad if (sc->sc_bmx280dump) {
815 1.1 brad DPRINTF(sc, 1, ("%s: dig_T1: %d %04x\n",__func__,sc->sc_cal_blob.dig_T1,sc->sc_cal_blob.dig_T1));
816 1.1 brad DPRINTF(sc, 1, ("%s: dig_T2: %d %04x\n",__func__,sc->sc_cal_blob.dig_T2,sc->sc_cal_blob.dig_T2));
817 1.1 brad DPRINTF(sc, 1, ("%s: dig_T3: %d %04x\n",__func__,sc->sc_cal_blob.dig_T3,sc->sc_cal_blob.dig_T3));
818 1.1 brad DPRINTF(sc, 1, ("%s: dig_P1: %d %04x\n",__func__,sc->sc_cal_blob.dig_P1,sc->sc_cal_blob.dig_P1));
819 1.1 brad DPRINTF(sc, 1, ("%s: dig_P2: %d %04x\n",__func__,sc->sc_cal_blob.dig_P2,sc->sc_cal_blob.dig_P2));
820 1.1 brad DPRINTF(sc, 1, ("%s: dig_P3: %d %04x\n",__func__,sc->sc_cal_blob.dig_P3,sc->sc_cal_blob.dig_P3));
821 1.1 brad DPRINTF(sc, 1, ("%s: dig_P4: %d %04x\n",__func__,sc->sc_cal_blob.dig_P4,sc->sc_cal_blob.dig_P4));
822 1.1 brad DPRINTF(sc, 1, ("%s: dig_P5: %d %04x\n",__func__,sc->sc_cal_blob.dig_P5,sc->sc_cal_blob.dig_P5));
823 1.1 brad DPRINTF(sc, 1, ("%s: dig_P6: %d %04x\n",__func__,sc->sc_cal_blob.dig_P6,sc->sc_cal_blob.dig_P6));
824 1.1 brad DPRINTF(sc, 1, ("%s: dig_P7: %d %04x\n",__func__,sc->sc_cal_blob.dig_P7,sc->sc_cal_blob.dig_P7));
825 1.1 brad DPRINTF(sc, 1, ("%s: dig_P8: %d %04x\n",__func__,sc->sc_cal_blob.dig_P8,sc->sc_cal_blob.dig_P8));
826 1.1 brad DPRINTF(sc, 1, ("%s: dig_P9: %d %04x\n",__func__,sc->sc_cal_blob.dig_P9,sc->sc_cal_blob.dig_P9));
827 1.1 brad
828 1.1 brad if (sc->sc_has_humidity) {
829 1.1 brad DPRINTF(sc, 1, ("%s: dig_H1: %d %02x\n",__func__,sc->sc_cal_blob.dig_H1,sc->sc_cal_blob.dig_H1));
830 1.1 brad DPRINTF(sc, 1, ("%s: dig_H2: %d %04x\n",__func__,sc->sc_cal_blob.dig_H2,sc->sc_cal_blob.dig_H2));
831 1.1 brad DPRINTF(sc, 1, ("%s: dig_H3: %d %02x\n",__func__,sc->sc_cal_blob.dig_H3,sc->sc_cal_blob.dig_H3));
832 1.1 brad DPRINTF(sc, 1, ("%s: dig_H4: %d %04x\n",__func__,sc->sc_cal_blob.dig_H4,sc->sc_cal_blob.dig_H4));
833 1.1 brad DPRINTF(sc, 1, ("%s: dig_H5: %d %04x\n",__func__,sc->sc_cal_blob.dig_H5,sc->sc_cal_blob.dig_H5));
834 1.1 brad DPRINTF(sc, 1, ("%s: dig_H6: %d %02x\n",__func__,sc->sc_cal_blob.dig_H6,sc->sc_cal_blob.dig_H6));
835 1.1 brad }
836 1.1 brad
837 1.1 brad sc->sc_bmx280dump = false;
838 1.1 brad }
839 1.1 brad
840 1.1 brad mutex_enter(&sc->sc_mutex);
841 1.3 thorpej error = sc->sc_funcs->acquire_bus(sc);
842 1.1 brad if (error) {
843 1.1 brad DPRINTF(sc, 2, ("%s: Could not acquire i2c bus: %x\n",
844 1.1 brad device_xname(sc->sc_dev), error));
845 1.1 brad goto out;
846 1.1 brad }
847 1.1 brad
848 1.1 brad if (error == 0) {
849 1.1 brad switch (edata->sensor) {
850 1.1 brad case BMX280_TEMP_SENSOR:
851 1.1 brad /* A temperature reading does not need pressure */
852 1.1 brad
853 1.1 brad error = bmx280_set_control_and_trigger(sc,
854 1.1 brad bmx280_osrs_text_to_mask(sc->sc_osrs_t),
855 1.1 brad 0,
856 1.1 brad 0,
857 1.1 brad bmx280_irr_text_to_mask(sc->sc_irr_samples));
858 1.1 brad
859 1.1 brad if (error == 0) {
860 1.1 brad error = bmx280_wait_for_data(sc);
861 1.1 brad
862 1.1 brad if (error == 0) {
863 1.1 brad error = bmx280_read_data(sc, &m_temp, &m_press, &m_hum, true);
864 1.1 brad
865 1.1 brad if (error == 0) {
866 1.1 brad comp_temp = bmx280_compensate_T_int32(&sc->sc_cal_blob, m_temp, &t_fine);
867 1.1 brad
868 1.1 brad DPRINTF(sc, 1, ("%s: Refresh compensated temp: %d - t_fine: %d\n",
869 1.1 brad device_xname(sc->sc_dev), comp_temp, t_fine));
870 1.1 brad
871 1.1 brad /* comp_temp is in Celcius * 100. This converts it to microkelvin */
872 1.1 brad
873 1.1 brad uint32_t q;
874 1.1 brad
875 1.1 brad q = (uint32_t)comp_temp;
876 1.1 brad q = q + 27315;
877 1.1 brad q = q * 10000;
878 1.1 brad
879 1.1 brad DPRINTF(sc, 1, ("%s: Refresh Q: %d\n", __func__, q));
880 1.1 brad
881 1.1 brad edata->value_cur = q;
882 1.1 brad edata->state = ENVSYS_SVALID;
883 1.1 brad }
884 1.1 brad }
885 1.1 brad }
886 1.1 brad break;
887 1.1 brad case BMX280_PRESSURE_SENSOR:
888 1.1 brad
889 1.1 brad /* Pressure needs the temp too */
890 1.1 brad error = bmx280_set_control_and_trigger(sc,
891 1.1 brad bmx280_osrs_text_to_mask(sc->sc_osrs_t),
892 1.1 brad bmx280_osrs_text_to_mask(sc->sc_osrs_p),
893 1.1 brad 0,
894 1.1 brad bmx280_irr_text_to_mask(sc->sc_irr_samples));
895 1.1 brad
896 1.1 brad if (error == 0) {
897 1.1 brad error = bmx280_wait_for_data(sc);
898 1.1 brad
899 1.1 brad if (error == 0) {
900 1.1 brad error = bmx280_read_data(sc, &m_temp, &m_press, &m_hum, false);
901 1.1 brad
902 1.1 brad if (error == 0) {
903 1.1 brad comp_temp = bmx280_compensate_T_int32(&sc->sc_cal_blob, m_temp, &t_fine);
904 1.1 brad
905 1.1 brad DPRINTF(sc, 1, ("%s: Refresh compensated temp for pressure: %d - t_fine: %d\n",
906 1.1 brad device_xname(sc->sc_dev), comp_temp, t_fine));
907 1.1 brad
908 1.1 brad comp_press = bmx280_compensate_P_int64(&sc->sc_cal_blob, m_press, t_fine);
909 1.1 brad
910 1.1 brad DPRINTF(sc, 1, ("%s: Refresh compensated pressure: %d\n",
911 1.1 brad device_xname(sc->sc_dev), comp_press));
912 1.1 brad
913 1.1 brad uint32_t q;
914 1.1 brad
915 1.1 brad q = comp_press;
916 1.1 brad q = q / 256;
917 1.1 brad q = q * 100;
918 1.1 brad
919 1.1 brad DPRINTF(sc, 1, ("%s: Refresh pressure Q: %d\n", __func__, q));
920 1.1 brad
921 1.1 brad edata->value_cur = q;
922 1.1 brad edata->state = ENVSYS_SVALID;
923 1.1 brad }
924 1.1 brad }
925 1.1 brad }
926 1.1 brad break;
927 1.1 brad
928 1.1 brad case BMX280_HUMIDITY_SENSOR:
929 1.1 brad
930 1.1 brad /* Humidity wants temperature */
931 1.1 brad
932 1.1 brad error = bmx280_set_control_and_trigger(sc,
933 1.1 brad bmx280_osrs_text_to_mask(sc->sc_osrs_t),
934 1.1 brad 0,
935 1.1 brad bmx280_osrs_text_to_mask(sc->sc_osrs_h),
936 1.1 brad bmx280_irr_text_to_mask(sc->sc_irr_samples));
937 1.1 brad
938 1.1 brad if (error == 0) {
939 1.1 brad error = bmx280_wait_for_data(sc);
940 1.1 brad
941 1.1 brad if (error == 0) {
942 1.1 brad error = bmx280_read_data(sc, &m_temp, &m_press, &m_hum, false);
943 1.1 brad
944 1.1 brad if (error == 0) {
945 1.1 brad comp_temp = bmx280_compensate_T_int32(&sc->sc_cal_blob, m_temp, &t_fine);
946 1.1 brad
947 1.1 brad DPRINTF(sc, 1, ("%s: Refresh compensated temp for humidity: %d - t_fine: %d\n",
948 1.1 brad device_xname(sc->sc_dev), comp_temp, t_fine));
949 1.1 brad
950 1.1 brad comp_hum = bmx280_compensate_H_int32(&sc->sc_cal_blob, m_hum, t_fine);
951 1.1 brad
952 1.1 brad DPRINTF(sc, 2, ("%s: Refresh compensated humidity: %d\n",
953 1.1 brad device_xname(sc->sc_dev), comp_hum));
954 1.1 brad
955 1.1 brad uint64_t q;
956 1.1 brad
957 1.1 brad q = (uint64_t)comp_hum * 1000000;
958 1.1 brad DPRINTF(sc, 1, ("%s: Refresh humidity Q 1: %jd\n", __func__, (uintmax_t)q));
959 1.1 brad q = q / 1024;
960 1.1 brad
961 1.1 brad DPRINTF(sc, 1, ("%s: Refresh humidity Q 2: %jd\n", __func__, (uintmax_t)q));
962 1.1 brad
963 1.1 brad edata->value_cur = (uint32_t) q;
964 1.1 brad edata->state = ENVSYS_SVALID;
965 1.1 brad }
966 1.1 brad }
967 1.1 brad }
968 1.1 brad break;
969 1.1 brad }
970 1.1 brad }
971 1.1 brad
972 1.1 brad if (error) {
973 1.1 brad DPRINTF(sc, 2, ("%s: Failed to get new status in refresh %d\n",
974 1.1 brad device_xname(sc->sc_dev), error));
975 1.1 brad }
976 1.1 brad
977 1.3 thorpej sc->sc_funcs->release_bus(sc);
978 1.1 brad out:
979 1.1 brad mutex_exit(&sc->sc_mutex);
980 1.1 brad }
981 1.1 brad
982 1.1 brad MODULE(MODULE_CLASS_DRIVER, bmx280thp, NULL);
983 1.1 brad
984 1.1 brad #ifdef _MODULE
985 1.1 brad CFDRIVER_DECL(bmx280thp, DV_DULL, NULL);
986 1.1 brad #include "ioconf.c"
987 1.1 brad #endif
988 1.1 brad
989 1.1 brad static int
990 1.1 brad bmx280thp_modcmd(modcmd_t cmd, void *opaque)
991 1.1 brad {
992 1.1 brad
993 1.1 brad switch (cmd) {
994 1.1 brad case MODULE_CMD_INIT:
995 1.1 brad #ifdef _MODULE
996 1.1 brad return config_init_component(cfdriver_ioconf_bmx280thp,
997 1.1 brad cfattach_ioconf_bmx280thp, cfdata_ioconf_bmx280thp);
998 1.1 brad #else
999 1.1 brad return 0;
1000 1.1 brad #endif
1001 1.1 brad case MODULE_CMD_FINI:
1002 1.1 brad #ifdef _MODULE
1003 1.1 brad return config_fini_component(cfdriver_ioconf_bmx280thp,
1004 1.1 brad cfattach_ioconf_bmx280thp, cfdata_ioconf_bmx280thp);
1005 1.1 brad #else
1006 1.1 brad return 0;
1007 1.1 brad #endif
1008 1.1 brad default:
1009 1.1 brad return ENOTTY;
1010 1.1 brad }
1011 1.1 brad }
1012