1 /* Bosch BMP388 pressure sensor
2 *
3 * Copyright (c) 2020 Facebook, Inc. and its affiliates
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Datasheet:
8 * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf
9 */
10
11 #include <zephyr/logging/log.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/pm/device.h>
14
15 #include "bmp388.h"
16
17 LOG_MODULE_REGISTER(BMP388, CONFIG_SENSOR_LOG_LEVEL);
18
19 enum chipset_id {
20 BMP388_ID = 0x50,
21 BMP390_ID = 0x60,
22 };
23
24 #if defined(CONFIG_BMP388_ODR_RUNTIME)
25 static const struct {
26 uint16_t freq_int;
27 uint16_t freq_milli;
28 } bmp388_odr_map[] = {
29 { 0, 3 }, /* 25/8192 - 327.68s */
30 { 0, 6 }, /* 25/4096 - 163.84s */
31 { 0, 12 }, /* 25/2048 - 81.92s */
32 { 0, 24 }, /* 25/1024 - 40.96s */
33 { 0, 49 }, /* 25/512 - 20.48s */
34 { 0, 98 }, /* 25/256 - 10.24s */
35 { 0, 195 }, /* 25/128 - 5.12s */
36 { 0, 391 }, /* 25/64 - 2.56s */
37 { 0, 781 }, /* 25/32 - 1.28s */
38 { 1, 563 }, /* 25/16 - 640ms */
39 { 3, 125 }, /* 25/8 - 320ms */
40 { 6, 250 }, /* 25/4 - 160ms */
41 { 12, 500 }, /* 25/2 - 80ms */
42 { 25, 0 }, /* 25 - 40ms */
43 { 50, 0 }, /* 50 - 20ms */
44 { 100, 0 }, /* 100 - 10ms */
45 { 200, 0 }, /* 200 - 5ms */
46 };
47 #endif
48
bmp388_bus_check(const struct device * dev)49 static inline int bmp388_bus_check(const struct device *dev)
50 {
51 const struct bmp388_config *cfg = dev->config;
52
53 return cfg->bus_io->check(&cfg->bus);
54 }
55
bmp388_reg_read(const struct device * dev,uint8_t start,uint8_t * buf,int size)56 static inline int bmp388_reg_read(const struct device *dev,
57 uint8_t start, uint8_t *buf, int size)
58 {
59 const struct bmp388_config *cfg = dev->config;
60
61 return cfg->bus_io->read(&cfg->bus, start, buf, size);
62 }
63
bmp388_reg_write(const struct device * dev,uint8_t reg,uint8_t val)64 static inline int bmp388_reg_write(const struct device *dev, uint8_t reg,
65 uint8_t val)
66 {
67 const struct bmp388_config *cfg = dev->config;
68
69 return cfg->bus_io->write(&cfg->bus, reg, val);
70 }
71
bmp388_reg_field_update(const struct device * dev,uint8_t reg,uint8_t mask,uint8_t val)72 int bmp388_reg_field_update(const struct device *dev,
73 uint8_t reg,
74 uint8_t mask,
75 uint8_t val)
76 {
77 int rc = 0;
78 uint8_t old_value, new_value;
79 const struct bmp388_config *cfg = dev->config;
80
81 rc = cfg->bus_io->read(&cfg->bus, reg, &old_value, 1);
82 if (rc != 0) {
83 return rc;
84 }
85
86 new_value = (old_value & ~mask) | (val & mask);
87 if (new_value == old_value) {
88 return 0;
89 }
90
91 return cfg->bus_io->write(&cfg->bus, reg, new_value);
92 }
93
94 #ifdef CONFIG_BMP388_ODR_RUNTIME
bmp388_freq_to_odr_val(uint16_t freq_int,uint16_t freq_milli)95 static int bmp388_freq_to_odr_val(uint16_t freq_int, uint16_t freq_milli)
96 {
97 size_t i;
98
99 /* An ODR of 0 Hz is not allowed */
100 if (freq_int == 0U && freq_milli == 0U) {
101 return -EINVAL;
102 }
103
104 for (i = 0; i < ARRAY_SIZE(bmp388_odr_map); i++) {
105 if (freq_int < bmp388_odr_map[i].freq_int ||
106 (freq_int == bmp388_odr_map[i].freq_int &&
107 freq_milli <= bmp388_odr_map[i].freq_milli)) {
108 return (ARRAY_SIZE(bmp388_odr_map) - 1) - i;
109 }
110 }
111
112 return -EINVAL;
113 }
114
bmp388_attr_set_odr(const struct device * dev,uint16_t freq_int,uint16_t freq_milli)115 static int bmp388_attr_set_odr(const struct device *dev,
116 uint16_t freq_int,
117 uint16_t freq_milli)
118 {
119 int err;
120 struct bmp388_data *data = dev->data;
121 int odr = bmp388_freq_to_odr_val(freq_int, freq_milli);
122
123 if (odr < 0) {
124 return odr;
125 }
126
127 err = bmp388_reg_field_update(dev,
128 BMP388_REG_ODR,
129 BMP388_ODR_MASK,
130 (uint8_t)odr);
131 if (err == 0) {
132 data->odr = odr;
133 }
134
135 return err;
136 }
137 #endif
138
139 #ifdef CONFIG_BMP388_OSR_RUNTIME
bmp388_attr_set_oversampling(const struct device * dev,enum sensor_channel chan,uint16_t val)140 static int bmp388_attr_set_oversampling(const struct device *dev,
141 enum sensor_channel chan,
142 uint16_t val)
143 {
144 uint8_t reg_val = 0;
145 uint32_t pos, mask;
146 int err;
147
148 struct bmp388_data *data = dev->data;
149
150 /* Value must be a positive power of 2 <= 32. */
151 if ((val <= 0) || (val > 32) || ((val & (val - 1)) != 0)) {
152 return -EINVAL;
153 }
154
155 if (chan == SENSOR_CHAN_PRESS) {
156 pos = BMP388_OSR_PRESSURE_POS;
157 mask = BMP388_OSR_PRESSURE_MASK;
158 } else if ((chan == SENSOR_CHAN_AMBIENT_TEMP) ||
159 (chan == SENSOR_CHAN_DIE_TEMP)) {
160 pos = BMP388_OSR_TEMP_POS;
161 mask = BMP388_OSR_TEMP_MASK;
162 } else {
163 return -EINVAL;
164 }
165
166 /* Determine exponent: this corresponds to register setting. */
167 while ((val % 2) == 0) {
168 val >>= 1;
169 ++reg_val;
170 }
171
172 err = bmp388_reg_field_update(dev,
173 BMP388_REG_OSR,
174 mask,
175 reg_val << pos);
176 if (err < 0) {
177 return err;
178 }
179
180 /* Store for future use in converting RAW values. */
181 if (chan == SENSOR_CHAN_PRESS) {
182 data->osr_pressure = reg_val;
183 } else {
184 data->osr_temp = reg_val;
185 }
186
187 return err;
188 }
189 #endif
190
bmp388_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)191 static int bmp388_attr_set(const struct device *dev,
192 enum sensor_channel chan,
193 enum sensor_attribute attr,
194 const struct sensor_value *val)
195 {
196 int ret;
197
198 #ifdef CONFIG_PM_DEVICE
199 enum pm_device_state state;
200
201 (void)pm_device_state_get(dev, &state);
202 if (state != PM_DEVICE_STATE_ACTIVE) {
203 return -EBUSY;
204 }
205 #endif
206
207 switch (attr) {
208 #ifdef CONFIG_BMP388_ODR_RUNTIME
209 case SENSOR_ATTR_SAMPLING_FREQUENCY:
210 ret = bmp388_attr_set_odr(dev, val->val1, val->val2 / 1000);
211 break;
212 #endif
213
214 #ifdef CONFIG_BMP388_OSR_RUNTIME
215 case SENSOR_ATTR_OVERSAMPLING:
216 ret = bmp388_attr_set_oversampling(dev, chan, val->val1);
217 break;
218 #endif
219
220 default:
221 ret = -EINVAL;
222 }
223
224 return ret;
225 }
226
bmp388_sample_fetch(const struct device * dev,enum sensor_channel chan)227 static int bmp388_sample_fetch(const struct device *dev,
228 enum sensor_channel chan)
229 {
230 struct bmp388_data *bmp3xx = dev->data;
231 uint8_t raw[BMP388_SAMPLE_BUFFER_SIZE];
232 int ret = 0;
233
234 __ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
235
236 #ifdef CONFIG_PM_DEVICE
237 enum pm_device_state state;
238
239 (void)pm_device_state_get(dev, &state);
240 if (state != PM_DEVICE_STATE_ACTIVE) {
241 return -EBUSY;
242 }
243 #endif
244
245 pm_device_busy_set(dev);
246
247 /* Wait for status to indicate that data is ready. */
248 raw[0] = 0U;
249 while ((raw[0] & BMP388_STATUS_DRDY_PRESS) == 0U) {
250 ret = bmp388_reg_read(dev, BMP388_REG_STATUS, raw, 1);
251 if (ret < 0) {
252 goto error;
253 }
254 }
255
256 ret = bmp388_reg_read(dev,
257 BMP388_REG_DATA0,
258 raw,
259 BMP388_SAMPLE_BUFFER_SIZE);
260 if (ret < 0) {
261 goto error;
262 }
263
264 /* convert samples to 32bit values */
265 bmp3xx->sample.press = sys_get_le24(&raw[0]);
266 bmp3xx->sample.raw_temp = sys_get_le24(&raw[3]);
267 bmp3xx->sample.comp_temp = 0;
268
269 error:
270 pm_device_busy_clear(dev);
271 return ret;
272 }
273
bmp388_compensate_temp(struct bmp388_data * data)274 static void bmp388_compensate_temp(struct bmp388_data *data)
275 {
276 /* Adapted from:
277 * https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c
278 */
279
280 int64_t partial_data1;
281 int64_t partial_data2;
282 int64_t partial_data3;
283 int64_t partial_data4;
284 int64_t partial_data5;
285
286 struct bmp388_cal_data *cal = &data->cal;
287
288 partial_data1 = ((int64_t)data->sample.raw_temp - (256 * cal->t1));
289 partial_data2 = cal->t2 * partial_data1;
290 partial_data3 = (partial_data1 * partial_data1);
291 partial_data4 = (int64_t)partial_data3 * cal->t3;
292 partial_data5 = ((int64_t)(partial_data2 * 262144) + partial_data4);
293
294 /* Store for pressure calculation */
295 data->sample.comp_temp = partial_data5 / 4294967296;
296 }
297
bmp388_temp_channel_get(const struct device * dev,struct sensor_value * val)298 static int bmp388_temp_channel_get(const struct device *dev,
299 struct sensor_value *val)
300 {
301 struct bmp388_data *data = dev->data;
302
303 if (data->sample.comp_temp == 0) {
304 bmp388_compensate_temp(data);
305 }
306
307 int64_t tmp = (data->sample.comp_temp * 250000) / 16384;
308
309 val->val1 = tmp / 1000000;
310 val->val2 = tmp % 1000000;
311
312 return 0;
313 }
314
bmp388_compensate_press(struct bmp388_data * data)315 static uint64_t bmp388_compensate_press(struct bmp388_data *data)
316 {
317 /* Adapted from:
318 * https://github.com/BoschSensortec/BMP3-Sensor-API/blob/master/bmp3.c
319 */
320
321 int64_t partial_data1;
322 int64_t partial_data2;
323 int64_t partial_data3;
324 int64_t partial_data4;
325 int64_t partial_data5;
326 int64_t partial_data6;
327 int64_t offset;
328 int64_t sensitivity;
329 uint64_t comp_press;
330
331 struct bmp388_cal_data *cal = &data->cal;
332
333 int64_t t_lin = data->sample.comp_temp;
334 uint32_t raw_pressure = data->sample.press;
335
336 partial_data1 = t_lin * t_lin;
337 partial_data2 = partial_data1 / 64;
338 partial_data3 = (partial_data2 * t_lin) / 256;
339 partial_data4 = (cal->p8 * partial_data3) / 32;
340 partial_data5 = (cal->p7 * partial_data1) * 16;
341 partial_data6 = (cal->p6 * t_lin) * 4194304;
342 offset = (cal->p5 * 140737488355328) + partial_data4 + partial_data5 +
343 partial_data6;
344 partial_data2 = (cal->p4 * partial_data3) / 32;
345 partial_data4 = (cal->p3 * partial_data1) * 4;
346 partial_data5 = (cal->p2 - 16384) * t_lin * 2097152;
347 sensitivity = ((cal->p1 - 16384) * 70368744177664) + partial_data2 +
348 partial_data4 + partial_data5;
349 partial_data1 = (sensitivity / 16777216) * raw_pressure;
350 partial_data2 = cal->p10 * t_lin;
351 partial_data3 = partial_data2 + (65536 * cal->p9);
352 partial_data4 = (partial_data3 * raw_pressure) / 8192;
353 /* Dividing by 10 followed by multiplying by 10 to avoid overflow caused
354 * (raw_pressure * partial_data4)
355 */
356 partial_data5 = (raw_pressure * (partial_data4 / 10)) / 512;
357 partial_data5 = partial_data5 * 10;
358 partial_data6 = ((int64_t)raw_pressure * (int64_t)raw_pressure);
359 partial_data2 = (cal->p11 * partial_data6) / 65536;
360 partial_data3 = (partial_data2 * raw_pressure) / 128;
361 partial_data4 = (offset / 4) + partial_data1 + partial_data5 +
362 partial_data3;
363
364 comp_press = (((uint64_t)partial_data4 * 25) / (uint64_t)1099511627776);
365
366 /* returned value is in hundredths of Pa. */
367 return comp_press;
368 }
369
bmp388_press_channel_get(const struct device * dev,struct sensor_value * val)370 static int bmp388_press_channel_get(const struct device *dev,
371 struct sensor_value *val)
372 {
373 struct bmp388_data *data = dev->data;
374
375 if (data->sample.comp_temp == 0) {
376 bmp388_compensate_temp(data);
377 }
378
379 uint64_t tmp = bmp388_compensate_press(data);
380
381 /* tmp is in hundredths of Pa. Convert to kPa as specified in sensor
382 * interface.
383 */
384 val->val1 = tmp / 100000;
385 val->val2 = (tmp % 100000) * 10;
386
387 return 0;
388 }
389
bmp388_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)390 static int bmp388_channel_get(const struct device *dev,
391 enum sensor_channel chan,
392 struct sensor_value *val)
393 {
394 switch (chan) {
395 case SENSOR_CHAN_PRESS:
396 bmp388_press_channel_get(dev, val);
397 break;
398
399 case SENSOR_CHAN_DIE_TEMP:
400 case SENSOR_CHAN_AMBIENT_TEMP:
401 bmp388_temp_channel_get(dev, val);
402 break;
403
404 default:
405 LOG_DBG("Channel not supported.");
406 return -ENOTSUP;
407 }
408
409 return 0;
410 }
411
bmp388_get_calibration_data(const struct device * dev)412 static int bmp388_get_calibration_data(const struct device *dev)
413 {
414 struct bmp388_data *data = dev->data;
415 struct bmp388_cal_data *cal = &data->cal;
416
417 if (bmp388_reg_read(dev, BMP388_REG_CALIB0, (uint8_t *)cal, sizeof(*cal)) < 0) {
418 return -EIO;
419 }
420
421 cal->t1 = sys_le16_to_cpu(cal->t1);
422 cal->t2 = sys_le16_to_cpu(cal->t2);
423 cal->p1 = (int16_t)sys_le16_to_cpu(cal->p1);
424 cal->p2 = (int16_t)sys_le16_to_cpu(cal->p2);
425 cal->p5 = sys_le16_to_cpu(cal->p5);
426 cal->p6 = sys_le16_to_cpu(cal->p6);
427 cal->p9 = (int16_t)sys_le16_to_cpu(cal->p9);
428
429 return 0;
430 }
431
432 #ifdef CONFIG_PM_DEVICE
bmp388_pm_action(const struct device * dev,enum pm_device_action action)433 static int bmp388_pm_action(const struct device *dev,
434 enum pm_device_action action)
435 {
436 uint8_t reg_val;
437
438 switch (action) {
439 case PM_DEVICE_ACTION_RESUME:
440 reg_val = BMP388_PWR_CTRL_MODE_NORMAL;
441 break;
442 case PM_DEVICE_ACTION_SUSPEND:
443 reg_val = BMP388_PWR_CTRL_MODE_SLEEP;
444 break;
445 default:
446 return -ENOTSUP;
447 }
448
449 if (bmp388_reg_field_update(dev,
450 BMP388_REG_PWR_CTRL,
451 BMP388_PWR_CTRL_MODE_MASK,
452 reg_val) < 0) {
453 LOG_DBG("Failed to set power mode.");
454 return -EIO;
455 }
456
457 return 0;
458 }
459 #endif /* CONFIG_PM_DEVICE */
460
461 static DEVICE_API(sensor, bmp388_api) = {
462 .attr_set = bmp388_attr_set,
463 #ifdef CONFIG_BMP388_TRIGGER
464 .trigger_set = bmp388_trigger_set,
465 #endif
466 .sample_fetch = bmp388_sample_fetch,
467 .channel_get = bmp388_channel_get,
468 };
469
bmp388_init(const struct device * dev)470 static int bmp388_init(const struct device *dev)
471 {
472 struct bmp388_data *bmp3xx = dev->data;
473 const struct bmp388_config *cfg = dev->config;
474 uint8_t val = 0U;
475
476 if (bmp388_bus_check(dev) < 0) {
477 LOG_DBG("bus check failed");
478 return -ENODEV;
479 }
480
481 /* reboot the chip */
482 if (bmp388_reg_write(dev, BMP388_REG_CMD, BMP388_CMD_SOFT_RESET) < 0) {
483 LOG_ERR("Cannot reboot chip.");
484 return -EIO;
485 }
486
487 k_busy_wait(2000);
488
489 if (bmp388_reg_read(dev, BMP388_REG_CHIPID, &val, 1) < 0) {
490 LOG_ERR("Failed to read chip id.");
491 return -EIO;
492 }
493
494 if (val != bmp3xx->chip_id) {
495 LOG_ERR("Unsupported chip detected (0x%x)!", val);
496 return -ENODEV;
497 }
498
499 /* Read calibration data */
500 if (bmp388_get_calibration_data(dev) < 0) {
501 LOG_ERR("Failed to read calibration data.");
502 return -EIO;
503 }
504
505 /* Set ODR */
506 if (bmp388_reg_field_update(dev, BMP388_REG_ODR, BMP388_ODR_MASK, bmp3xx->odr) < 0) {
507 LOG_ERR("Failed to set ODR.");
508 return -EIO;
509 }
510
511 /* Set OSR */
512 val = (bmp3xx->osr_pressure << BMP388_OSR_PRESSURE_POS);
513 val |= (bmp3xx->osr_temp << BMP388_OSR_TEMP_POS);
514 if (bmp388_reg_write(dev, BMP388_REG_OSR, val) < 0) {
515 LOG_ERR("Failed to set OSR.");
516 return -EIO;
517 }
518
519 /* Set IIR filter coefficient */
520 val = (cfg->iir_filter << BMP388_IIR_FILTER_POS) & BMP388_IIR_FILTER_MASK;
521 if (bmp388_reg_write(dev, BMP388_REG_CONFIG, val) < 0) {
522 LOG_ERR("Failed to set IIR coefficient.");
523 return -EIO;
524 }
525
526 /* Enable sensors and normal mode*/
527 if (bmp388_reg_write(dev,
528 BMP388_REG_PWR_CTRL,
529 BMP388_PWR_CTRL_ON) < 0) {
530 LOG_ERR("Failed to enable sensors.");
531 return -EIO;
532 }
533
534 /* Read error register */
535 if (bmp388_reg_read(dev, BMP388_REG_ERR_REG, &val, 1) < 0) {
536 LOG_ERR("Failed get sensors error register.");
537 return -EIO;
538 }
539
540 /* OSR and ODR config not proper */
541 if (val & BMP388_STATUS_CONF_ERR) {
542 LOG_ERR("OSR and ODR configuration is not proper");
543 return -EINVAL;
544 }
545
546 #ifdef CONFIG_BMP388_TRIGGER
547 if (cfg->gpio_int.port != NULL && bmp388_trigger_mode_init(dev) < 0) {
548 LOG_ERR("Cannot set up trigger mode.");
549 return -EINVAL;
550 }
551 #endif
552
553 return 0;
554 }
555
556 /* Initializes a struct bmp388_config for an instance on a SPI bus. */
557 #define BMP388_CONFIG_SPI(inst) \
558 .bus.spi = SPI_DT_SPEC_INST_GET(inst, BMP388_SPI_OPERATION, 0), \
559 .bus_io = &bmp388_bus_io_spi,
560
561 /* Initializes a struct bmp388_config for an instance on an I2C bus. */
562 #define BMP388_CONFIG_I2C(inst) \
563 .bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
564 .bus_io = &bmp388_bus_io_i2c,
565
566 #define BMP388_BUS_CFG(inst) \
567 COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
568 (BMP388_CONFIG_I2C(inst)), \
569 (BMP388_CONFIG_SPI(inst)))
570
571 #if defined(CONFIG_BMP388_TRIGGER)
572 #define BMP388_INT_CFG(inst) \
573 .gpio_int = GPIO_DT_SPEC_INST_GET_OR(inst, int_gpios, {0}),
574 #else
575 #define BMP388_INT_CFG(inst)
576 #endif
577
578 #define BMP3XX_INST(inst, chipid)\
579 static struct bmp388_data bmp388_data_##inst##chipid = {\
580 .odr = DT_INST_ENUM_IDX(inst, odr),\
581 .osr_pressure = DT_INST_ENUM_IDX(inst, osr_press),\
582 .osr_temp = DT_INST_ENUM_IDX(inst, osr_temp),\
583 .chip_id = chipid,\
584 };\
585 static const struct bmp388_config bmp388_config_##inst##chipid = {\
586 BMP388_BUS_CFG(inst)\
587 BMP388_INT_CFG(inst)\
588 .iir_filter = DT_INST_ENUM_IDX(inst, iir_filter),\
589 };\
590 PM_DEVICE_DT_INST_DEFINE(inst, bmp388_pm_action);\
591 SENSOR_DEVICE_DT_INST_DEFINE(inst, bmp388_init, PM_DEVICE_DT_INST_GET(inst),\
592 &bmp388_data_##inst##chipid, &bmp388_config_##inst##chipid,\
593 POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &bmp388_api);
594
595 #define DT_DRV_COMPAT bosch_bmp388
596 DT_INST_FOREACH_STATUS_OKAY_VARGS(BMP3XX_INST, BMP388_ID)
597 #undef DT_DRV_COMPAT
598
599 #define DT_DRV_COMPAT bosch_bmp390
600 DT_INST_FOREACH_STATUS_OKAY_VARGS(BMP3XX_INST, BMP390_ID)
601 #undef DT_DRV_COMPAT
602