1 // SPDX-License-Identifier: GPL-2.0
3 * Bosch BME680 - Temperature, Pressure, Humidity & Gas Sensor
5 * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
6 * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
9 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf
11 #include <linux/bitfield.h>
12 #include <linux/cleanup.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/log2.h>
16 #include <linux/module.h>
17 #include <linux/regmap.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/sysfs.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
25 #include <linux/unaligned.h>
29 /* 1st set of calibration data */
31 /* Temperature calib indexes */
34 /* Pressure calib indexes */
47 /* 2nd set of calibration data */
49 /* Humidity calib indexes */
57 /* Stray T1 calib index */
59 /* Gas heater calib indexes */
65 /* 3rd set of calibration data */
101 /* values of CTRL_MEAS register */
102 enum bme680_op_mode
{
103 BME680_MODE_SLEEP
= 0,
104 BME680_MODE_FORCED
= 1,
115 struct regmap
*regmap
;
116 struct bme680_calib bme680
;
117 struct mutex lock
; /* Protect multiple serial R/W ops to device. */
118 u8 oversampling_temp
;
119 u8 oversampling_press
;
120 u8 oversampling_humid
;
131 u8 buf
[BME680_NUM_BULK_READ_REGS
];
134 u8 bme680_cal_buf_1
[BME680_CALIB_RANGE_1_LEN
];
135 u8 bme680_cal_buf_2
[BME680_CALIB_RANGE_2_LEN
];
136 u8 bme680_cal_buf_3
[BME680_CALIB_RANGE_3_LEN
];
140 static const struct regmap_range bme680_volatile_ranges
[] = {
141 regmap_reg_range(BME680_REG_MEAS_STAT_0
, BME680_REG_GAS_R_LSB
),
142 regmap_reg_range(BME680_REG_STATUS
, BME680_REG_STATUS
),
143 regmap_reg_range(BME680_T2_LSB_REG
, BME680_GH3_REG
),
146 static const struct regmap_access_table bme680_volatile_table
= {
147 .yes_ranges
= bme680_volatile_ranges
,
148 .n_yes_ranges
= ARRAY_SIZE(bme680_volatile_ranges
),
151 const struct regmap_config bme680_regmap_config
= {
154 .max_register
= 0xef,
155 .volatile_table
= &bme680_volatile_table
,
156 .cache_type
= REGCACHE_RBTREE
,
158 EXPORT_SYMBOL_NS(bme680_regmap_config
, IIO_BME680
);
160 static const struct iio_chan_spec bme680_channels
[] = {
163 /* PROCESSED maintained for ABI backwards compatibility */
164 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
165 BIT(IIO_CHAN_INFO_RAW
) |
166 BIT(IIO_CHAN_INFO_SCALE
) |
167 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
173 .endianness
= IIO_CPU
,
177 .type
= IIO_PRESSURE
,
178 /* PROCESSED maintained for ABI backwards compatibility */
179 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
180 BIT(IIO_CHAN_INFO_RAW
) |
181 BIT(IIO_CHAN_INFO_SCALE
) |
182 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
188 .endianness
= IIO_CPU
,
192 .type
= IIO_HUMIDITYRELATIVE
,
193 /* PROCESSED maintained for ABI backwards compatibility */
194 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
195 BIT(IIO_CHAN_INFO_RAW
) |
196 BIT(IIO_CHAN_INFO_SCALE
) |
197 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
203 .endianness
= IIO_CPU
,
207 .type
= IIO_RESISTANCE
,
208 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
214 .endianness
= IIO_CPU
,
217 IIO_CHAN_SOFT_TIMESTAMP(4),
220 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
226 static int bme680_read_calib(struct bme680_data
*data
,
227 struct bme680_calib
*calib
)
229 struct device
*dev
= regmap_get_device(data
->regmap
);
230 unsigned int tmp_msb
, tmp_lsb
;
233 ret
= regmap_bulk_read(data
->regmap
, BME680_T2_LSB_REG
,
234 data
->bme680_cal_buf_1
,
235 sizeof(data
->bme680_cal_buf_1
));
237 dev_err(dev
, "failed to read 1st set of calib data;\n");
241 calib
->par_t2
= get_unaligned_le16(&data
->bme680_cal_buf_1
[T2_LSB
]);
242 calib
->par_t3
= data
->bme680_cal_buf_1
[T3
];
243 calib
->par_p1
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P1_LSB
]);
244 calib
->par_p2
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P2_LSB
]);
245 calib
->par_p3
= data
->bme680_cal_buf_1
[P3
];
246 calib
->par_p4
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P4_LSB
]);
247 calib
->par_p5
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P5_LSB
]);
248 calib
->par_p7
= data
->bme680_cal_buf_1
[P7
];
249 calib
->par_p6
= data
->bme680_cal_buf_1
[P6
];
250 calib
->par_p8
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P8_LSB
]);
251 calib
->par_p9
= get_unaligned_le16(&data
->bme680_cal_buf_1
[P9_LSB
]);
252 calib
->par_p10
= data
->bme680_cal_buf_1
[P10
];
254 ret
= regmap_bulk_read(data
->regmap
, BME680_H2_MSB_REG
,
255 data
->bme680_cal_buf_2
,
256 sizeof(data
->bme680_cal_buf_2
));
258 dev_err(dev
, "failed to read 2nd set of calib data;\n");
262 tmp_lsb
= data
->bme680_cal_buf_2
[H1_LSB
];
263 tmp_msb
= data
->bme680_cal_buf_2
[H1_LSB
+ 1];
264 calib
->par_h1
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
265 (tmp_lsb
& BME680_BIT_H1_DATA_MASK
);
267 tmp_msb
= data
->bme680_cal_buf_2
[H2_MSB
];
268 tmp_lsb
= data
->bme680_cal_buf_2
[H2_MSB
+ 1];
269 calib
->par_h2
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
270 (tmp_lsb
>> BME680_HUM_REG_SHIFT_VAL
);
272 calib
->par_h3
= data
->bme680_cal_buf_2
[H3
];
273 calib
->par_h4
= data
->bme680_cal_buf_2
[H4
];
274 calib
->par_h5
= data
->bme680_cal_buf_2
[H5
];
275 calib
->par_h6
= data
->bme680_cal_buf_2
[H6
];
276 calib
->par_h7
= data
->bme680_cal_buf_2
[H7
];
277 calib
->par_t1
= get_unaligned_le16(&data
->bme680_cal_buf_2
[T1_LSB
]);
278 calib
->par_gh2
= get_unaligned_le16(&data
->bme680_cal_buf_2
[GH2_LSB
]);
279 calib
->par_gh1
= data
->bme680_cal_buf_2
[GH1
];
280 calib
->par_gh3
= data
->bme680_cal_buf_2
[GH3
];
282 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_RES_HEAT_VAL
,
283 data
->bme680_cal_buf_3
,
284 sizeof(data
->bme680_cal_buf_3
));
286 dev_err(dev
, "failed to read 3rd set of calib data;\n");
290 calib
->res_heat_val
= data
->bme680_cal_buf_3
[RES_HEAT_VAL
];
292 calib
->res_heat_range
= FIELD_GET(BME680_RHRANGE_MASK
,
293 data
->bme680_cal_buf_3
[RES_HEAT_RANGE
]);
295 calib
->range_sw_err
= FIELD_GET(BME680_RSERROR_MASK
,
296 data
->bme680_cal_buf_3
[RANGE_SW_ERR
]);
301 static int bme680_read_temp_adc(struct bme680_data
*data
, u32
*adc_temp
)
303 struct device
*dev
= regmap_get_device(data
->regmap
);
307 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_TEMP_MSB
,
308 data
->buf
, BME680_TEMP_NUM_BYTES
);
310 dev_err(dev
, "failed to read temperature\n");
314 value_temp
= FIELD_GET(BME680_MEAS_TRIM_MASK
,
315 get_unaligned_be24(data
->buf
));
316 if (value_temp
== BME680_MEAS_SKIPPED
) {
317 /* reading was skipped */
318 dev_err(dev
, "reading temperature skipped\n");
321 *adc_temp
= value_temp
;
327 * Taken from Bosch BME680 API:
328 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876
330 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore,
331 * output value of "3233" represents 32.33 DegC.
333 static s32
bme680_calc_t_fine(struct bme680_data
*data
, u32 adc_temp
)
335 struct bme680_calib
*calib
= &data
->bme680
;
336 s64 var1
, var2
, var3
;
338 /* If the calibration is invalid, attempt to reload it */
340 bme680_read_calib(data
, calib
);
342 var1
= ((s32
)adc_temp
>> 3) - ((s32
)calib
->par_t1
<< 1);
343 var2
= (var1
* calib
->par_t2
) >> 11;
344 var3
= ((var1
>> 1) * (var1
>> 1)) >> 12;
345 var3
= (var3
* ((s32
)calib
->par_t3
<< 4)) >> 14;
346 return var2
+ var3
; /* t_fine = var2 + var3 */
349 static int bme680_get_t_fine(struct bme680_data
*data
, s32
*t_fine
)
354 ret
= bme680_read_temp_adc(data
, &adc_temp
);
358 *t_fine
= bme680_calc_t_fine(data
, adc_temp
);
363 static s16
bme680_compensate_temp(struct bme680_data
*data
,
366 return (bme680_calc_t_fine(data
, adc_temp
) * 5 + 128) / 256;
369 static int bme680_read_press_adc(struct bme680_data
*data
, u32
*adc_press
)
371 struct device
*dev
= regmap_get_device(data
->regmap
);
375 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_PRESS_MSB
,
376 data
->buf
, BME680_PRESS_NUM_BYTES
);
378 dev_err(dev
, "failed to read pressure\n");
382 value_press
= FIELD_GET(BME680_MEAS_TRIM_MASK
,
383 get_unaligned_be24(data
->buf
));
384 if (value_press
== BME680_MEAS_SKIPPED
) {
385 /* reading was skipped */
386 dev_err(dev
, "reading pressure skipped\n");
389 *adc_press
= value_press
;
395 * Taken from Bosch BME680 API:
396 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896
398 * Returns pressure measurement in Pa. Output value of "97356" represents
399 * 97356 Pa = 973.56 hPa.
401 static u32
bme680_compensate_press(struct bme680_data
*data
,
402 u32 adc_press
, s32 t_fine
)
404 struct bme680_calib
*calib
= &data
->bme680
;
405 s32 var1
, var2
, var3
, press_comp
;
407 var1
= (t_fine
>> 1) - 64000;
408 var2
= ((((var1
>> 2) * (var1
>> 2)) >> 11) * calib
->par_p6
) >> 2;
409 var2
= var2
+ (var1
* calib
->par_p5
<< 1);
410 var2
= (var2
>> 2) + ((s32
)calib
->par_p4
<< 16);
411 var1
= (((((var1
>> 2) * (var1
>> 2)) >> 13) *
412 ((s32
)calib
->par_p3
<< 5)) >> 3) +
413 ((calib
->par_p2
* var1
) >> 1);
415 var1
= ((32768 + var1
) * calib
->par_p1
) >> 15;
416 press_comp
= 1048576 - adc_press
;
417 press_comp
= ((press_comp
- (var2
>> 12)) * 3125);
419 if (press_comp
>= BME680_MAX_OVERFLOW_VAL
)
420 press_comp
= ((press_comp
/ (u32
)var1
) << 1);
422 press_comp
= ((press_comp
<< 1) / (u32
)var1
);
424 var1
= (calib
->par_p9
* (((press_comp
>> 3) *
425 (press_comp
>> 3)) >> 13)) >> 12;
426 var2
= ((press_comp
>> 2) * calib
->par_p8
) >> 13;
427 var3
= ((press_comp
>> 8) * (press_comp
>> 8) *
428 (press_comp
>> 8) * calib
->par_p10
) >> 17;
430 press_comp
+= (var1
+ var2
+ var3
+ ((s32
)calib
->par_p7
<< 7)) >> 4;
435 static int bme680_read_humid_adc(struct bme680_data
*data
, u32
*adc_humidity
)
437 struct device
*dev
= regmap_get_device(data
->regmap
);
441 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_HUMIDITY_MSB
,
442 &data
->be16
, BME680_HUMID_NUM_BYTES
);
444 dev_err(dev
, "failed to read humidity\n");
448 value_humidity
= be16_to_cpu(data
->be16
);
449 if (value_humidity
== BME680_MEAS_SKIPPED
) {
450 /* reading was skipped */
451 dev_err(dev
, "reading humidity skipped\n");
454 *adc_humidity
= value_humidity
;
460 * Taken from Bosch BME680 API:
461 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937
463 * Returns humidity measurement in percent, resolution is 0.001 percent. Output
464 * value of "43215" represents 43.215 %rH.
466 static u32
bme680_compensate_humid(struct bme680_data
*data
,
467 u16 adc_humid
, s32 t_fine
)
469 struct bme680_calib
*calib
= &data
->bme680
;
470 s32 var1
, var2
, var3
, var4
, var5
, var6
, temp_scaled
, calc_hum
;
472 temp_scaled
= (t_fine
* 5 + 128) >> 8;
473 var1
= (adc_humid
- (((s32
)calib
->par_h1
* 16))) -
474 (((temp_scaled
* calib
->par_h3
) / 100) >> 1);
475 var2
= (calib
->par_h2
*
476 (((temp_scaled
* calib
->par_h4
) / 100) +
477 (((temp_scaled
* ((temp_scaled
* calib
->par_h5
) / 100))
478 >> 6) / 100) + (1 << 14))) >> 10;
480 var4
= (s32
)calib
->par_h6
<< 7;
481 var4
= (var4
+ ((temp_scaled
* calib
->par_h7
) / 100)) >> 4;
482 var5
= ((var3
>> 14) * (var3
>> 14)) >> 10;
483 var6
= (var4
* var5
) >> 1;
484 calc_hum
= (((var3
+ var6
) >> 10) * 1000) >> 12;
486 calc_hum
= clamp(calc_hum
, 0, 100000); /* clamp between 0-100 %rH */
492 * Taken from Bosch BME680 API:
493 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973
495 * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms.
497 static u32
bme680_compensate_gas(struct bme680_data
*data
, u16 gas_res_adc
,
500 struct bme680_calib
*calib
= &data
->bme680
;
506 /* Look up table for the possible gas range values */
507 static const u32 lookup_table
[16] = {
508 2147483647u, 2147483647u, 2147483647u, 2147483647u,
509 2147483647u, 2126008810u, 2147483647u, 2130303777u,
510 2147483647u, 2147483647u, 2143188679u, 2136746228u,
511 2147483647u, 2126008810u, 2147483647u, 2147483647u
514 var1
= ((1340LL + (5 * calib
->range_sw_err
)) *
515 (lookup_table
[gas_range
])) >> 16;
516 var2
= ((gas_res_adc
<< 15) - 16777216) + var1
;
517 var3
= ((125000 << (15 - gas_range
)) * var1
) >> 9;
519 calc_gas_res
= div64_s64(var3
, (s64
)var2
);
525 * Taken from Bosch BME680 API:
526 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002
528 static u8
bme680_calc_heater_res(struct bme680_data
*data
, u16 temp
)
530 struct bme680_calib
*calib
= &data
->bme680
;
531 s32 var1
, var2
, var3
, var4
, var5
, heatr_res_x100
;
534 if (temp
> 400) /* Cap temperature */
537 var1
= (((s32
)BME680_AMB_TEMP
* calib
->par_gh3
) / 1000) * 256;
538 var2
= (calib
->par_gh1
+ 784) * (((((calib
->par_gh2
+ 154009) *
541 var3
= var1
+ (var2
/ 2);
542 var4
= (var3
/ (calib
->res_heat_range
+ 4));
543 var5
= 131 * calib
->res_heat_val
+ 65536;
544 heatr_res_x100
= ((var4
/ var5
) - 250) * 34;
545 heatr_res
= DIV_ROUND_CLOSEST(heatr_res_x100
, 100);
551 * Taken from Bosch BME680 API:
552 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188
554 static u8
bme680_calc_heater_dur(u16 dur
)
556 u8 durval
, factor
= 0;
559 durval
= 0xff; /* Max duration */
565 durval
= dur
+ (factor
* 64);
571 /* Taken from datasheet, section 5.3.3 */
572 static u8
bme680_calc_heater_preheat_current(u8 curr
)
577 static int bme680_set_mode(struct bme680_data
*data
, enum bme680_op_mode mode
)
579 struct device
*dev
= regmap_get_device(data
->regmap
);
582 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
583 BME680_MODE_MASK
, mode
);
585 dev_err(dev
, "failed to set ctrl_meas register\n");
592 static u8
bme680_oversampling_to_reg(u8 val
)
594 return ilog2(val
) + 1;
598 * Taken from Bosch BME680 API:
599 * https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L490
601 static int bme680_wait_for_eoc(struct bme680_data
*data
)
603 struct device
*dev
= regmap_get_device(data
->regmap
);
606 * (Sum of oversampling ratios * time per oversampling) +
607 * TPH measurement + gas measurement + wait transition from forced mode
610 int wait_eoc_us
= ((data
->oversampling_temp
+ data
->oversampling_press
+
611 data
->oversampling_humid
) * 1936) + (477 * 4) +
612 (477 * 5) + 1000 + (data
->heater_dur
* 1000);
616 ret
= regmap_read(data
->regmap
, BME680_REG_MEAS_STAT_0
, &data
->check
);
618 dev_err(dev
, "failed to read measurement status register.\n");
621 if (data
->check
& BME680_MEAS_BIT
) {
622 dev_err(dev
, "Device measurement cycle incomplete.\n");
625 if (!(data
->check
& BME680_NEW_DATA_BIT
)) {
626 dev_err(dev
, "No new data available from the device.\n");
633 static int bme680_chip_config(struct bme680_data
*data
)
635 struct device
*dev
= regmap_get_device(data
->regmap
);
639 osrs
= FIELD_PREP(BME680_OSRS_HUMIDITY_MASK
,
640 bme680_oversampling_to_reg(data
->oversampling_humid
));
642 * Highly recommended to set oversampling of humidity before
643 * temperature/pressure oversampling.
645 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_HUMIDITY
,
646 BME680_OSRS_HUMIDITY_MASK
, osrs
);
648 dev_err(dev
, "failed to write ctrl_hum register\n");
652 /* IIR filter settings */
653 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CONFIG
,
654 BME680_FILTER_MASK
, BME680_FILTER_COEFF_VAL
);
656 dev_err(dev
, "failed to write config register\n");
660 osrs
= FIELD_PREP(BME680_OSRS_TEMP_MASK
,
661 bme680_oversampling_to_reg(data
->oversampling_temp
)) |
662 FIELD_PREP(BME680_OSRS_PRESS_MASK
,
663 bme680_oversampling_to_reg(data
->oversampling_press
));
664 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
665 BME680_OSRS_TEMP_MASK
| BME680_OSRS_PRESS_MASK
,
668 dev_err(dev
, "failed to write ctrl_meas register\n");
675 static int bme680_preheat_curr_config(struct bme680_data
*data
, u8 val
)
677 struct device
*dev
= regmap_get_device(data
->regmap
);
681 heatr_curr
= bme680_calc_heater_preheat_current(val
);
682 ret
= regmap_write(data
->regmap
, BME680_REG_IDAC_HEAT_0
, heatr_curr
);
684 dev_err(dev
, "failed to write idac_heat_0 register\n");
689 static int bme680_gas_config(struct bme680_data
*data
)
691 struct device
*dev
= regmap_get_device(data
->regmap
);
693 u8 heatr_res
, heatr_dur
;
695 ret
= bme680_set_mode(data
, BME680_MODE_SLEEP
);
699 heatr_res
= bme680_calc_heater_res(data
, data
->heater_temp
);
701 /* set target heater temperature */
702 ret
= regmap_write(data
->regmap
, BME680_REG_RES_HEAT_0
, heatr_res
);
704 dev_err(dev
, "failed to write res_heat_0 register\n");
708 heatr_dur
= bme680_calc_heater_dur(data
->heater_dur
);
710 /* set target heating duration */
711 ret
= regmap_write(data
->regmap
, BME680_REG_GAS_WAIT_0
, heatr_dur
);
713 dev_err(dev
, "failed to write gas_wait_0 register\n");
717 ret
= bme680_preheat_curr_config(data
, data
->preheat_curr_mA
);
721 /* Enable the gas sensor and select heater profile set-point 0 */
722 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_GAS_1
,
723 BME680_RUN_GAS_MASK
| BME680_NB_CONV_MASK
,
724 FIELD_PREP(BME680_RUN_GAS_MASK
, 1) |
725 FIELD_PREP(BME680_NB_CONV_MASK
, 0));
727 dev_err(dev
, "failed to write ctrl_gas_1 register\n");
732 static int bme680_read_temp(struct bme680_data
*data
, s16
*comp_temp
)
737 ret
= bme680_read_temp_adc(data
, &adc_temp
);
741 *comp_temp
= bme680_compensate_temp(data
, adc_temp
);
745 static int bme680_read_press(struct bme680_data
*data
, u32
*comp_press
)
751 ret
= bme680_get_t_fine(data
, &t_fine
);
755 ret
= bme680_read_press_adc(data
, &adc_press
);
759 *comp_press
= bme680_compensate_press(data
, adc_press
, t_fine
);
763 static int bme680_read_humid(struct bme680_data
*data
, u32
*comp_humidity
)
769 ret
= bme680_get_t_fine(data
, &t_fine
);
773 ret
= bme680_read_humid_adc(data
, &adc_humidity
);
777 *comp_humidity
= bme680_compensate_humid(data
, adc_humidity
, t_fine
);
781 static int bme680_read_gas(struct bme680_data
*data
, int *comp_gas_res
)
783 struct device
*dev
= regmap_get_device(data
->regmap
);
785 u16 adc_gas_res
, gas_regs_val
;
788 ret
= regmap_read(data
->regmap
, BME680_REG_MEAS_STAT_0
, &data
->check
);
789 if (data
->check
& BME680_GAS_MEAS_BIT
) {
790 dev_err(dev
, "gas measurement incomplete\n");
794 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_GAS_MSB
,
795 &data
->be16
, BME680_GAS_NUM_BYTES
);
797 dev_err(dev
, "failed to read gas resistance\n");
801 gas_regs_val
= be16_to_cpu(data
->be16
);
802 adc_gas_res
= FIELD_GET(BME680_ADC_GAS_RES
, gas_regs_val
);
805 * occurs if either the gas heating duration was insuffient
806 * to reach the target heater temperature or the target
807 * heater temperature was too high for the heater sink to
810 if ((gas_regs_val
& BME680_GAS_STAB_BIT
) == 0) {
811 dev_err(dev
, "heater failed to reach the target temperature\n");
815 gas_range
= FIELD_GET(BME680_GAS_RANGE_MASK
, gas_regs_val
);
816 *comp_gas_res
= bme680_compensate_gas(data
, adc_gas_res
, gas_range
);
820 static int bme680_read_raw(struct iio_dev
*indio_dev
,
821 struct iio_chan_spec
const *chan
,
822 int *val
, int *val2
, long mask
)
824 struct bme680_data
*data
= iio_priv(indio_dev
);
828 guard(mutex
)(&data
->lock
);
830 ret
= bme680_set_mode(data
, BME680_MODE_FORCED
);
834 ret
= bme680_wait_for_eoc(data
);
839 case IIO_CHAN_INFO_PROCESSED
:
840 switch (chan
->type
) {
842 ret
= bme680_read_temp(data
, &temp_chan_val
);
846 *val
= temp_chan_val
* 10;
849 ret
= bme680_read_press(data
, &chan_val
);
855 return IIO_VAL_FRACTIONAL
;
856 case IIO_HUMIDITYRELATIVE
:
857 ret
= bme680_read_humid(data
, &chan_val
);
863 return IIO_VAL_FRACTIONAL
;
865 ret
= bme680_read_gas(data
, &chan_val
);
874 case IIO_CHAN_INFO_RAW
:
875 switch (chan
->type
) {
877 ret
= bme680_read_temp(data
, (s16
*)&chan_val
);
884 ret
= bme680_read_press(data
, &chan_val
);
890 case IIO_HUMIDITYRELATIVE
:
891 ret
= bme680_read_humid(data
, &chan_val
);
900 case IIO_CHAN_INFO_SCALE
:
901 switch (chan
->type
) {
908 return IIO_VAL_FRACTIONAL
;
909 case IIO_HUMIDITYRELATIVE
:
912 return IIO_VAL_FRACTIONAL
;
916 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
917 switch (chan
->type
) {
919 *val
= data
->oversampling_temp
;
922 *val
= data
->oversampling_press
;
924 case IIO_HUMIDITYRELATIVE
:
925 *val
= data
->oversampling_humid
;
935 static bool bme680_is_valid_oversampling(int rate
)
937 return (rate
> 0 && rate
<= 16 && is_power_of_2(rate
));
940 static int bme680_write_raw(struct iio_dev
*indio_dev
,
941 struct iio_chan_spec
const *chan
,
942 int val
, int val2
, long mask
)
944 struct bme680_data
*data
= iio_priv(indio_dev
);
946 guard(mutex
)(&data
->lock
);
952 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
954 if (!bme680_is_valid_oversampling(val
))
957 switch (chan
->type
) {
959 data
->oversampling_temp
= val
;
962 data
->oversampling_press
= val
;
964 case IIO_HUMIDITYRELATIVE
:
965 data
->oversampling_humid
= val
;
971 return bme680_chip_config(data
);
973 case IIO_CHAN_INFO_PROCESSED
:
975 switch (chan
->type
) {
977 return bme680_preheat_curr_config(data
, (u8
)val
);
987 static const char bme680_oversampling_ratio_show
[] = "1 2 4 8 16";
989 static IIO_CONST_ATTR(oversampling_ratio_available
,
990 bme680_oversampling_ratio_show
);
992 static struct attribute
*bme680_attributes
[] = {
993 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
997 static const struct attribute_group bme680_attribute_group
= {
998 .attrs
= bme680_attributes
,
1001 static const struct iio_info bme680_info
= {
1002 .read_raw
= &bme680_read_raw
,
1003 .write_raw
= &bme680_write_raw
,
1004 .attrs
= &bme680_attribute_group
,
1007 static const unsigned long bme680_avail_scan_masks
[] = {
1008 BIT(BME680_GAS
) | BIT(BME680_HUMID
) | BIT(BME680_PRESS
) | BIT(BME680_TEMP
),
1012 static irqreturn_t
bme680_trigger_handler(int irq
, void *p
)
1014 struct iio_poll_func
*pf
= p
;
1015 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1016 struct bme680_data
*data
= iio_priv(indio_dev
);
1017 struct device
*dev
= regmap_get_device(data
->regmap
);
1018 u32 adc_temp
, adc_press
, adc_humid
;
1019 u16 adc_gas_res
, gas_regs_val
;
1024 guard(mutex
)(&data
->lock
);
1026 ret
= bme680_set_mode(data
, BME680_MODE_FORCED
);
1030 ret
= bme680_wait_for_eoc(data
);
1034 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_MEAS_STAT_0
,
1035 data
->buf
, sizeof(data
->buf
));
1037 dev_err(dev
, "failed to burst read sensor data\n");
1040 if (data
->buf
[0] & BME680_GAS_MEAS_BIT
) {
1041 dev_err(dev
, "gas measurement incomplete\n");
1045 /* Temperature calculations */
1046 adc_temp
= FIELD_GET(BME680_MEAS_TRIM_MASK
, get_unaligned_be24(&data
->buf
[5]));
1047 if (adc_temp
== BME680_MEAS_SKIPPED
) {
1048 dev_err(dev
, "reading temperature skipped\n");
1051 data
->scan
.chan
[0] = bme680_compensate_temp(data
, adc_temp
);
1052 t_fine
= bme680_calc_t_fine(data
, adc_temp
);
1054 /* Pressure calculations */
1055 adc_press
= FIELD_GET(BME680_MEAS_TRIM_MASK
, get_unaligned_be24(&data
->buf
[2]));
1056 if (adc_press
== BME680_MEAS_SKIPPED
) {
1057 dev_err(dev
, "reading pressure skipped\n");
1060 data
->scan
.chan
[1] = bme680_compensate_press(data
, adc_press
, t_fine
);
1062 /* Humidity calculations */
1063 adc_humid
= get_unaligned_be16(&data
->buf
[8]);
1064 if (adc_humid
== BME680_MEAS_SKIPPED
) {
1065 dev_err(dev
, "reading humidity skipped\n");
1068 data
->scan
.chan
[2] = bme680_compensate_humid(data
, adc_humid
, t_fine
);
1070 /* Gas calculations */
1071 gas_regs_val
= get_unaligned_be16(&data
->buf
[13]);
1072 adc_gas_res
= FIELD_GET(BME680_ADC_GAS_RES
, gas_regs_val
);
1073 if ((gas_regs_val
& BME680_GAS_STAB_BIT
) == 0) {
1074 dev_err(dev
, "heater failed to reach the target temperature\n");
1077 gas_range
= FIELD_GET(BME680_GAS_RANGE_MASK
, gas_regs_val
);
1078 data
->scan
.chan
[3] = bme680_compensate_gas(data
, adc_gas_res
, gas_range
);
1080 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->scan
,
1081 iio_get_time_ns(indio_dev
));
1083 iio_trigger_notify_done(indio_dev
->trig
);
1087 int bme680_core_probe(struct device
*dev
, struct regmap
*regmap
,
1090 struct iio_dev
*indio_dev
;
1091 struct bme680_data
*data
;
1094 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1098 data
= iio_priv(indio_dev
);
1099 mutex_init(&data
->lock
);
1100 dev_set_drvdata(dev
, indio_dev
);
1101 data
->regmap
= regmap
;
1102 indio_dev
->name
= name
;
1103 indio_dev
->channels
= bme680_channels
;
1104 indio_dev
->num_channels
= ARRAY_SIZE(bme680_channels
);
1105 indio_dev
->available_scan_masks
= bme680_avail_scan_masks
;
1106 indio_dev
->info
= &bme680_info
;
1107 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1109 /* default values for the sensor */
1110 data
->oversampling_humid
= 2; /* 2X oversampling rate */
1111 data
->oversampling_press
= 4; /* 4X oversampling rate */
1112 data
->oversampling_temp
= 8; /* 8X oversampling rate */
1113 data
->heater_temp
= 320; /* degree Celsius */
1114 data
->heater_dur
= 150; /* milliseconds */
1115 data
->preheat_curr_mA
= 0;
1117 ret
= regmap_write(regmap
, BME680_REG_SOFT_RESET
, BME680_CMD_SOFTRESET
);
1119 return dev_err_probe(dev
, ret
, "Failed to reset chip\n");
1121 fsleep(BME680_STARTUP_TIME_US
);
1123 ret
= regmap_read(regmap
, BME680_REG_CHIP_ID
, &data
->check
);
1125 return dev_err_probe(dev
, ret
, "Error reading chip ID\n");
1127 if (data
->check
!= BME680_CHIP_ID_VAL
) {
1128 dev_err(dev
, "Wrong chip ID, got %x expected %x\n",
1129 data
->check
, BME680_CHIP_ID_VAL
);
1133 ret
= bme680_read_calib(data
, &data
->bme680
);
1135 return dev_err_probe(dev
, ret
,
1136 "failed to read calibration coefficients at probe\n");
1139 ret
= bme680_chip_config(data
);
1141 return dev_err_probe(dev
, ret
,
1142 "failed to set chip_config data\n");
1144 ret
= bme680_gas_config(data
);
1146 return dev_err_probe(dev
, ret
,
1147 "failed to set gas config data\n");
1149 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
1150 iio_pollfunc_store_time
,
1151 bme680_trigger_handler
,
1154 return dev_err_probe(dev
, ret
,
1155 "iio triggered buffer setup failed\n");
1157 return devm_iio_device_register(dev
, indio_dev
);
1159 EXPORT_SYMBOL_NS_GPL(bme680_core_probe
, IIO_BME680
);
1161 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
1162 MODULE_DESCRIPTION("Bosch BME680 Driver");
1163 MODULE_LICENSE("GPL v2");