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/acpi.h>
12 #include <linux/bitfield.h>
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/log2.h>
16 #include <linux/regmap.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
52 struct regmap
*regmap
;
53 struct bme680_calib bme680
;
55 u8 oversampling_press
;
56 u8 oversampling_humid
;
60 * Carryover value from temperature conversion, used in pressure
61 * and humidity compensation calculations.
66 static const struct regmap_range bme680_volatile_ranges
[] = {
67 regmap_reg_range(BME680_REG_MEAS_STAT_0
, BME680_REG_GAS_R_LSB
),
68 regmap_reg_range(BME680_REG_STATUS
, BME680_REG_STATUS
),
69 regmap_reg_range(BME680_T2_LSB_REG
, BME680_GH3_REG
),
72 static const struct regmap_access_table bme680_volatile_table
= {
73 .yes_ranges
= bme680_volatile_ranges
,
74 .n_yes_ranges
= ARRAY_SIZE(bme680_volatile_ranges
),
77 const struct regmap_config bme680_regmap_config
= {
81 .volatile_table
= &bme680_volatile_table
,
82 .cache_type
= REGCACHE_RBTREE
,
84 EXPORT_SYMBOL(bme680_regmap_config
);
86 static const struct iio_chan_spec bme680_channels
[] = {
89 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
90 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
94 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
95 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
98 .type
= IIO_HUMIDITYRELATIVE
,
99 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
100 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
103 .type
= IIO_RESISTANCE
,
104 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
108 static int bme680_read_calib(struct bme680_data
*data
,
109 struct bme680_calib
*calib
)
111 struct device
*dev
= regmap_get_device(data
->regmap
);
112 unsigned int tmp
, tmp_msb
, tmp_lsb
;
116 /* Temperature related coefficients */
117 ret
= regmap_bulk_read(data
->regmap
, BME680_T1_LSB_REG
,
120 dev_err(dev
, "failed to read BME680_T1_LSB_REG\n");
123 calib
->par_t1
= le16_to_cpu(buf
);
125 ret
= regmap_bulk_read(data
->regmap
, BME680_T2_LSB_REG
,
128 dev_err(dev
, "failed to read BME680_T2_LSB_REG\n");
131 calib
->par_t2
= le16_to_cpu(buf
);
133 ret
= regmap_read(data
->regmap
, BME680_T3_REG
, &tmp
);
135 dev_err(dev
, "failed to read BME680_T3_REG\n");
140 /* Pressure related coefficients */
141 ret
= regmap_bulk_read(data
->regmap
, BME680_P1_LSB_REG
,
144 dev_err(dev
, "failed to read BME680_P1_LSB_REG\n");
147 calib
->par_p1
= le16_to_cpu(buf
);
149 ret
= regmap_bulk_read(data
->regmap
, BME680_P2_LSB_REG
,
152 dev_err(dev
, "failed to read BME680_P2_LSB_REG\n");
155 calib
->par_p2
= le16_to_cpu(buf
);
157 ret
= regmap_read(data
->regmap
, BME680_P3_REG
, &tmp
);
159 dev_err(dev
, "failed to read BME680_P3_REG\n");
164 ret
= regmap_bulk_read(data
->regmap
, BME680_P4_LSB_REG
,
167 dev_err(dev
, "failed to read BME680_P4_LSB_REG\n");
170 calib
->par_p4
= le16_to_cpu(buf
);
172 ret
= regmap_bulk_read(data
->regmap
, BME680_P5_LSB_REG
,
175 dev_err(dev
, "failed to read BME680_P5_LSB_REG\n");
178 calib
->par_p5
= le16_to_cpu(buf
);
180 ret
= regmap_read(data
->regmap
, BME680_P6_REG
, &tmp
);
182 dev_err(dev
, "failed to read BME680_P6_REG\n");
187 ret
= regmap_read(data
->regmap
, BME680_P7_REG
, &tmp
);
189 dev_err(dev
, "failed to read BME680_P7_REG\n");
194 ret
= regmap_bulk_read(data
->regmap
, BME680_P8_LSB_REG
,
197 dev_err(dev
, "failed to read BME680_P8_LSB_REG\n");
200 calib
->par_p8
= le16_to_cpu(buf
);
202 ret
= regmap_bulk_read(data
->regmap
, BME680_P9_LSB_REG
,
205 dev_err(dev
, "failed to read BME680_P9_LSB_REG\n");
208 calib
->par_p9
= le16_to_cpu(buf
);
210 ret
= regmap_read(data
->regmap
, BME680_P10_REG
, &tmp
);
212 dev_err(dev
, "failed to read BME680_P10_REG\n");
215 calib
->par_p10
= tmp
;
217 /* Humidity related coefficients */
218 ret
= regmap_read(data
->regmap
, BME680_H1_MSB_REG
, &tmp_msb
);
220 dev_err(dev
, "failed to read BME680_H1_MSB_REG\n");
223 ret
= regmap_read(data
->regmap
, BME680_H1_LSB_REG
, &tmp_lsb
);
225 dev_err(dev
, "failed to read BME680_H1_LSB_REG\n");
228 calib
->par_h1
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
229 (tmp_lsb
& BME680_BIT_H1_DATA_MASK
);
231 ret
= regmap_read(data
->regmap
, BME680_H2_MSB_REG
, &tmp_msb
);
233 dev_err(dev
, "failed to read BME680_H2_MSB_REG\n");
236 ret
= regmap_read(data
->regmap
, BME680_H2_LSB_REG
, &tmp_lsb
);
238 dev_err(dev
, "failed to read BME680_H2_LSB_REG\n");
241 calib
->par_h2
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
242 (tmp_lsb
>> BME680_HUM_REG_SHIFT_VAL
);
244 ret
= regmap_read(data
->regmap
, BME680_H3_REG
, &tmp
);
246 dev_err(dev
, "failed to read BME680_H3_REG\n");
251 ret
= regmap_read(data
->regmap
, BME680_H4_REG
, &tmp
);
253 dev_err(dev
, "failed to read BME680_H4_REG\n");
258 ret
= regmap_read(data
->regmap
, BME680_H5_REG
, &tmp
);
260 dev_err(dev
, "failed to read BME680_H5_REG\n");
265 ret
= regmap_read(data
->regmap
, BME680_H6_REG
, &tmp
);
267 dev_err(dev
, "failed to read BME680_H6_REG\n");
272 ret
= regmap_read(data
->regmap
, BME680_H7_REG
, &tmp
);
274 dev_err(dev
, "failed to read BME680_H7_REG\n");
279 /* Gas heater related coefficients */
280 ret
= regmap_read(data
->regmap
, BME680_GH1_REG
, &tmp
);
282 dev_err(dev
, "failed to read BME680_GH1_REG\n");
285 calib
->par_gh1
= tmp
;
287 ret
= regmap_bulk_read(data
->regmap
, BME680_GH2_LSB_REG
,
290 dev_err(dev
, "failed to read BME680_GH2_LSB_REG\n");
293 calib
->par_gh2
= le16_to_cpu(buf
);
295 ret
= regmap_read(data
->regmap
, BME680_GH3_REG
, &tmp
);
297 dev_err(dev
, "failed to read BME680_GH3_REG\n");
300 calib
->par_gh3
= tmp
;
302 /* Other coefficients */
303 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_RANGE
, &tmp
);
305 dev_err(dev
, "failed to read resistance heat range\n");
308 calib
->res_heat_range
= FIELD_GET(BME680_RHRANGE_MASK
, tmp
);
310 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_VAL
, &tmp
);
312 dev_err(dev
, "failed to read resistance heat value\n");
315 calib
->res_heat_val
= tmp
;
317 ret
= regmap_read(data
->regmap
, BME680_REG_RANGE_SW_ERR
, &tmp
);
319 dev_err(dev
, "failed to read range software error\n");
322 calib
->range_sw_err
= FIELD_GET(BME680_RSERROR_MASK
, tmp
);
328 * Taken from Bosch BME680 API:
329 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876
331 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore,
332 * output value of "3233" represents 32.33 DegC.
334 static s16
bme680_compensate_temp(struct bme680_data
*data
,
337 struct bme680_calib
*calib
= &data
->bme680
;
338 s64 var1
, var2
, var3
;
341 /* If the calibration is invalid, attempt to reload it */
343 bme680_read_calib(data
, calib
);
345 var1
= (adc_temp
>> 3) - (calib
->par_t1
<< 1);
346 var2
= (var1
* calib
->par_t2
) >> 11;
347 var3
= ((var1
>> 1) * (var1
>> 1)) >> 12;
348 var3
= (var3
* (calib
->par_t3
<< 4)) >> 14;
349 data
->t_fine
= var2
+ var3
;
350 calc_temp
= (data
->t_fine
* 5 + 128) >> 8;
356 * Taken from Bosch BME680 API:
357 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896
359 * Returns pressure measurement in Pa. Output value of "97356" represents
360 * 97356 Pa = 973.56 hPa.
362 static u32
bme680_compensate_press(struct bme680_data
*data
,
365 struct bme680_calib
*calib
= &data
->bme680
;
366 s32 var1
, var2
, var3
, press_comp
;
368 var1
= (data
->t_fine
>> 1) - 64000;
369 var2
= ((((var1
>> 2) * (var1
>> 2)) >> 11) * calib
->par_p6
) >> 2;
370 var2
= var2
+ (var1
* calib
->par_p5
<< 1);
371 var2
= (var2
>> 2) + (calib
->par_p4
<< 16);
372 var1
= (((((var1
>> 2) * (var1
>> 2)) >> 13) *
373 (calib
->par_p3
<< 5)) >> 3) +
374 ((calib
->par_p2
* var1
) >> 1);
376 var1
= ((32768 + var1
) * calib
->par_p1
) >> 15;
377 press_comp
= 1048576 - adc_press
;
378 press_comp
= ((press_comp
- (var2
>> 12)) * 3125);
380 if (press_comp
>= BME680_MAX_OVERFLOW_VAL
)
381 press_comp
= ((press_comp
/ (u32
)var1
) << 1);
383 press_comp
= ((press_comp
<< 1) / (u32
)var1
);
385 var1
= (calib
->par_p9
* (((press_comp
>> 3) *
386 (press_comp
>> 3)) >> 13)) >> 12;
387 var2
= ((press_comp
>> 2) * calib
->par_p8
) >> 13;
388 var3
= ((press_comp
>> 8) * (press_comp
>> 8) *
389 (press_comp
>> 8) * calib
->par_p10
) >> 17;
391 press_comp
+= (var1
+ var2
+ var3
+ (calib
->par_p7
<< 7)) >> 4;
397 * Taken from Bosch BME680 API:
398 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937
400 * Returns humidity measurement in percent, resolution is 0.001 percent. Output
401 * value of "43215" represents 43.215 %rH.
403 static u32
bme680_compensate_humid(struct bme680_data
*data
,
406 struct bme680_calib
*calib
= &data
->bme680
;
407 s32 var1
, var2
, var3
, var4
, var5
, var6
, temp_scaled
, calc_hum
;
409 temp_scaled
= (data
->t_fine
* 5 + 128) >> 8;
410 var1
= (adc_humid
- ((s32
) ((s32
) calib
->par_h1
* 16))) -
411 (((temp_scaled
* (s32
) calib
->par_h3
) / 100) >> 1);
412 var2
= ((s32
) calib
->par_h2
*
413 (((temp_scaled
* calib
->par_h4
) / 100) +
414 (((temp_scaled
* ((temp_scaled
* calib
->par_h5
) / 100))
415 >> 6) / 100) + (1 << 14))) >> 10;
417 var4
= calib
->par_h6
<< 7;
418 var4
= (var4
+ ((temp_scaled
* calib
->par_h7
) / 100)) >> 4;
419 var5
= ((var3
>> 14) * (var3
>> 14)) >> 10;
420 var6
= (var4
* var5
) >> 1;
421 calc_hum
= (((var3
+ var6
) >> 10) * 1000) >> 12;
423 calc_hum
= clamp(calc_hum
, 0, 100000); /* clamp between 0-100 %rH */
429 * Taken from Bosch BME680 API:
430 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973
432 * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms.
434 static u32
bme680_compensate_gas(struct bme680_data
*data
, u16 gas_res_adc
,
437 struct bme680_calib
*calib
= &data
->bme680
;
443 /* Look up table for the possible gas range values */
444 const u32 lookupTable
[16] = {2147483647u, 2147483647u,
445 2147483647u, 2147483647u, 2147483647u,
446 2126008810u, 2147483647u, 2130303777u,
447 2147483647u, 2147483647u, 2143188679u,
448 2136746228u, 2147483647u, 2126008810u,
449 2147483647u, 2147483647u};
451 var1
= ((1340 + (5 * (s64
) calib
->range_sw_err
)) *
452 ((s64
) lookupTable
[gas_range
])) >> 16;
453 var2
= ((gas_res_adc
<< 15) - 16777216) + var1
;
454 var3
= ((125000 << (15 - gas_range
)) * var1
) >> 9;
456 calc_gas_res
= div64_s64(var3
, (s64
) var2
);
462 * Taken from Bosch BME680 API:
463 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002
465 static u8
bme680_calc_heater_res(struct bme680_data
*data
, u16 temp
)
467 struct bme680_calib
*calib
= &data
->bme680
;
468 s32 var1
, var2
, var3
, var4
, var5
, heatr_res_x100
;
471 if (temp
> 400) /* Cap temperature */
474 var1
= (((s32
) BME680_AMB_TEMP
* calib
->par_gh3
) / 1000) * 256;
475 var2
= (calib
->par_gh1
+ 784) * (((((calib
->par_gh2
+ 154009) *
478 var3
= var1
+ (var2
/ 2);
479 var4
= (var3
/ (calib
->res_heat_range
+ 4));
480 var5
= 131 * calib
->res_heat_val
+ 65536;
481 heatr_res_x100
= ((var4
/ var5
) - 250) * 34;
482 heatr_res
= (heatr_res_x100
+ 50) / 100;
488 * Taken from Bosch BME680 API:
489 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188
491 static u8
bme680_calc_heater_dur(u16 dur
)
493 u8 durval
, factor
= 0;
496 durval
= 0xff; /* Max duration */
502 durval
= dur
+ (factor
* 64);
508 static int bme680_set_mode(struct bme680_data
*data
, bool mode
)
510 struct device
*dev
= regmap_get_device(data
->regmap
);
514 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
515 BME680_MODE_MASK
, BME680_MODE_FORCED
);
517 dev_err(dev
, "failed to set forced mode\n");
520 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
521 BME680_MODE_MASK
, BME680_MODE_SLEEP
);
523 dev_err(dev
, "failed to set sleep mode\n");
530 static u8
bme680_oversampling_to_reg(u8 val
)
532 return ilog2(val
) + 1;
535 static int bme680_chip_config(struct bme680_data
*data
)
537 struct device
*dev
= regmap_get_device(data
->regmap
);
542 BME680_OSRS_HUMIDITY_MASK
,
543 bme680_oversampling_to_reg(data
->oversampling_humid
));
545 * Highly recommended to set oversampling of humidity before
546 * temperature/pressure oversampling.
548 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_HUMIDITY
,
549 BME680_OSRS_HUMIDITY_MASK
, osrs
);
551 dev_err(dev
, "failed to write ctrl_hum register\n");
555 /* IIR filter settings */
556 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CONFIG
,
558 BME680_FILTER_COEFF_VAL
);
560 dev_err(dev
, "failed to write config register\n");
564 osrs
= FIELD_PREP(BME680_OSRS_TEMP_MASK
,
565 bme680_oversampling_to_reg(data
->oversampling_temp
)) |
566 FIELD_PREP(BME680_OSRS_PRESS_MASK
,
567 bme680_oversampling_to_reg(data
->oversampling_press
));
568 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
569 BME680_OSRS_TEMP_MASK
| BME680_OSRS_PRESS_MASK
,
572 dev_err(dev
, "failed to write ctrl_meas register\n");
577 static int bme680_gas_config(struct bme680_data
*data
)
579 struct device
*dev
= regmap_get_device(data
->regmap
);
581 u8 heatr_res
, heatr_dur
;
583 heatr_res
= bme680_calc_heater_res(data
, data
->heater_temp
);
585 /* set target heater temperature */
586 ret
= regmap_write(data
->regmap
, BME680_REG_RES_HEAT_0
, heatr_res
);
588 dev_err(dev
, "failed to write res_heat_0 register\n");
592 heatr_dur
= bme680_calc_heater_dur(data
->heater_dur
);
594 /* set target heating duration */
595 ret
= regmap_write(data
->regmap
, BME680_REG_GAS_WAIT_0
, heatr_dur
);
597 dev_err(dev
, "failed to write gas_wait_0 register\n");
601 /* Enable the gas sensor and select heater profile set-point 0 */
602 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_GAS_1
,
603 BME680_RUN_GAS_MASK
| BME680_NB_CONV_MASK
,
604 FIELD_PREP(BME680_RUN_GAS_MASK
, 1) |
605 FIELD_PREP(BME680_NB_CONV_MASK
, 0));
607 dev_err(dev
, "failed to write ctrl_gas_1 register\n");
612 static int bme680_read_temp(struct bme680_data
*data
, int *val
)
614 struct device
*dev
= regmap_get_device(data
->regmap
);
620 /* set forced mode to trigger measurement */
621 ret
= bme680_set_mode(data
, true);
625 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_TEMP_MSB
,
628 dev_err(dev
, "failed to read temperature\n");
632 adc_temp
= be32_to_cpu(tmp
) >> 12;
633 if (adc_temp
== BME680_MEAS_SKIPPED
) {
634 /* reading was skipped */
635 dev_err(dev
, "reading temperature skipped\n");
638 comp_temp
= bme680_compensate_temp(data
, adc_temp
);
640 * val might be NULL if we're called by the read_press/read_humid
641 * routine which is callled to get t_fine value used in
642 * compensate_press/compensate_humid to get compensated
643 * pressure/humidity readings.
646 *val
= comp_temp
* 10; /* Centidegrees to millidegrees */
653 static int bme680_read_press(struct bme680_data
*data
,
656 struct device
*dev
= regmap_get_device(data
->regmap
);
661 /* Read and compensate temperature to get a reading of t_fine */
662 ret
= bme680_read_temp(data
, NULL
);
666 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_PRESS_MSB
,
669 dev_err(dev
, "failed to read pressure\n");
673 adc_press
= be32_to_cpu(tmp
) >> 12;
674 if (adc_press
== BME680_MEAS_SKIPPED
) {
675 /* reading was skipped */
676 dev_err(dev
, "reading pressure skipped\n");
680 *val
= bme680_compensate_press(data
, adc_press
);
682 return IIO_VAL_FRACTIONAL
;
685 static int bme680_read_humid(struct bme680_data
*data
,
688 struct device
*dev
= regmap_get_device(data
->regmap
);
694 /* Read and compensate temperature to get a reading of t_fine */
695 ret
= bme680_read_temp(data
, NULL
);
699 ret
= regmap_bulk_read(data
->regmap
, BM6880_REG_HUMIDITY_MSB
,
702 dev_err(dev
, "failed to read humidity\n");
706 adc_humidity
= be16_to_cpu(tmp
);
707 if (adc_humidity
== BME680_MEAS_SKIPPED
) {
708 /* reading was skipped */
709 dev_err(dev
, "reading humidity skipped\n");
712 comp_humidity
= bme680_compensate_humid(data
, adc_humidity
);
714 *val
= comp_humidity
;
716 return IIO_VAL_FRACTIONAL
;
719 static int bme680_read_gas(struct bme680_data
*data
,
722 struct device
*dev
= regmap_get_device(data
->regmap
);
729 /* Set heater settings */
730 ret
= bme680_gas_config(data
);
732 dev_err(dev
, "failed to set gas config\n");
736 /* set forced mode to trigger measurement */
737 ret
= bme680_set_mode(data
, true);
741 ret
= regmap_read(data
->regmap
, BME680_REG_MEAS_STAT_0
, &check
);
742 if (check
& BME680_GAS_MEAS_BIT
) {
743 dev_err(dev
, "gas measurement incomplete\n");
747 ret
= regmap_read(data
->regmap
, BME680_REG_GAS_R_LSB
, &check
);
749 dev_err(dev
, "failed to read gas_r_lsb register\n");
754 * occurs if either the gas heating duration was insuffient
755 * to reach the target heater temperature or the target
756 * heater temperature was too high for the heater sink to
759 if ((check
& BME680_GAS_STAB_BIT
) == 0) {
760 dev_err(dev
, "heater failed to reach the target temperature\n");
764 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_GAS_MSB
,
767 dev_err(dev
, "failed to read gas resistance\n");
771 gas_range
= check
& BME680_GAS_RANGE_MASK
;
772 adc_gas_res
= be16_to_cpu(tmp
) >> BME680_ADC_GAS_RES_SHIFT
;
774 *val
= bme680_compensate_gas(data
, adc_gas_res
, gas_range
);
778 static int bme680_read_raw(struct iio_dev
*indio_dev
,
779 struct iio_chan_spec
const *chan
,
780 int *val
, int *val2
, long mask
)
782 struct bme680_data
*data
= iio_priv(indio_dev
);
785 case IIO_CHAN_INFO_PROCESSED
:
786 switch (chan
->type
) {
788 return bme680_read_temp(data
, val
);
790 return bme680_read_press(data
, val
, val2
);
791 case IIO_HUMIDITYRELATIVE
:
792 return bme680_read_humid(data
, val
, val2
);
794 return bme680_read_gas(data
, val
);
798 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
799 switch (chan
->type
) {
801 *val
= data
->oversampling_temp
;
804 *val
= data
->oversampling_press
;
806 case IIO_HUMIDITYRELATIVE
:
807 *val
= data
->oversampling_humid
;
817 static bool bme680_is_valid_oversampling(int rate
)
819 return (rate
> 0 && rate
<= 16 && is_power_of_2(rate
));
822 static int bme680_write_raw(struct iio_dev
*indio_dev
,
823 struct iio_chan_spec
const *chan
,
824 int val
, int val2
, long mask
)
826 struct bme680_data
*data
= iio_priv(indio_dev
);
832 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
834 if (!bme680_is_valid_oversampling(val
))
837 switch (chan
->type
) {
839 data
->oversampling_temp
= val
;
842 data
->oversampling_press
= val
;
844 case IIO_HUMIDITYRELATIVE
:
845 data
->oversampling_humid
= val
;
851 return bme680_chip_config(data
);
858 static const char bme680_oversampling_ratio_show
[] = "1 2 4 8 16";
860 static IIO_CONST_ATTR(oversampling_ratio_available
,
861 bme680_oversampling_ratio_show
);
863 static struct attribute
*bme680_attributes
[] = {
864 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
868 static const struct attribute_group bme680_attribute_group
= {
869 .attrs
= bme680_attributes
,
872 static const struct iio_info bme680_info
= {
873 .read_raw
= &bme680_read_raw
,
874 .write_raw
= &bme680_write_raw
,
875 .attrs
= &bme680_attribute_group
,
878 static const char *bme680_match_acpi_device(struct device
*dev
)
880 const struct acpi_device_id
*id
;
882 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
886 return dev_name(dev
);
889 int bme680_core_probe(struct device
*dev
, struct regmap
*regmap
,
892 struct iio_dev
*indio_dev
;
893 struct bme680_data
*data
;
897 ret
= regmap_write(regmap
, BME680_REG_SOFT_RESET
,
898 BME680_CMD_SOFTRESET
);
900 dev_err(dev
, "Failed to reset chip\n");
904 ret
= regmap_read(regmap
, BME680_REG_CHIP_ID
, &val
);
906 dev_err(dev
, "Error reading chip ID\n");
910 if (val
!= BME680_CHIP_ID_VAL
) {
911 dev_err(dev
, "Wrong chip ID, got %x expected %x\n",
912 val
, BME680_CHIP_ID_VAL
);
916 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
920 if (!name
&& ACPI_HANDLE(dev
))
921 name
= bme680_match_acpi_device(dev
);
923 data
= iio_priv(indio_dev
);
924 dev_set_drvdata(dev
, indio_dev
);
925 data
->regmap
= regmap
;
926 indio_dev
->name
= name
;
927 indio_dev
->channels
= bme680_channels
;
928 indio_dev
->num_channels
= ARRAY_SIZE(bme680_channels
);
929 indio_dev
->info
= &bme680_info
;
930 indio_dev
->modes
= INDIO_DIRECT_MODE
;
932 /* default values for the sensor */
933 data
->oversampling_humid
= 2; /* 2X oversampling rate */
934 data
->oversampling_press
= 4; /* 4X oversampling rate */
935 data
->oversampling_temp
= 8; /* 8X oversampling rate */
936 data
->heater_temp
= 320; /* degree Celsius */
937 data
->heater_dur
= 150; /* milliseconds */
939 ret
= bme680_chip_config(data
);
941 dev_err(dev
, "failed to set chip_config data\n");
945 ret
= bme680_gas_config(data
);
947 dev_err(dev
, "failed to set gas config data\n");
951 ret
= bme680_read_calib(data
, &data
->bme680
);
954 "failed to read calibration coefficients at probe\n");
958 return devm_iio_device_register(dev
, indio_dev
);
960 EXPORT_SYMBOL_GPL(bme680_core_probe
);
962 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
963 MODULE_DESCRIPTION("Bosch BME680 Driver");
964 MODULE_LICENSE("GPL v2");