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 const struct regmap_config bme680_regmap_config
= {
70 EXPORT_SYMBOL(bme680_regmap_config
);
72 static const struct iio_chan_spec bme680_channels
[] = {
75 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
76 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
80 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
81 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
84 .type
= IIO_HUMIDITYRELATIVE
,
85 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
86 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
89 .type
= IIO_RESISTANCE
,
90 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
94 static const int bme680_oversampling_avail
[] = { 1, 2, 4, 8, 16 };
96 static int bme680_read_calib(struct bme680_data
*data
,
97 struct bme680_calib
*calib
)
99 struct device
*dev
= regmap_get_device(data
->regmap
);
100 unsigned int tmp
, tmp_msb
, tmp_lsb
;
104 /* Temperature related coefficients */
105 ret
= regmap_bulk_read(data
->regmap
, BME680_T1_LSB_REG
,
108 dev_err(dev
, "failed to read BME680_T1_LSB_REG\n");
111 calib
->par_t1
= le16_to_cpu(buf
);
113 ret
= regmap_bulk_read(data
->regmap
, BME680_T2_LSB_REG
,
116 dev_err(dev
, "failed to read BME680_T2_LSB_REG\n");
119 calib
->par_t2
= le16_to_cpu(buf
);
121 ret
= regmap_read(data
->regmap
, BME680_T3_REG
, &tmp
);
123 dev_err(dev
, "failed to read BME680_T3_REG\n");
128 /* Pressure related coefficients */
129 ret
= regmap_bulk_read(data
->regmap
, BME680_P1_LSB_REG
,
132 dev_err(dev
, "failed to read BME680_P1_LSB_REG\n");
135 calib
->par_p1
= le16_to_cpu(buf
);
137 ret
= regmap_bulk_read(data
->regmap
, BME680_P2_LSB_REG
,
140 dev_err(dev
, "failed to read BME680_P2_LSB_REG\n");
143 calib
->par_p2
= le16_to_cpu(buf
);
145 ret
= regmap_read(data
->regmap
, BME680_P3_REG
, &tmp
);
147 dev_err(dev
, "failed to read BME680_P3_REG\n");
152 ret
= regmap_bulk_read(data
->regmap
, BME680_P4_LSB_REG
,
155 dev_err(dev
, "failed to read BME680_P4_LSB_REG\n");
158 calib
->par_p4
= le16_to_cpu(buf
);
160 ret
= regmap_bulk_read(data
->regmap
, BME680_P5_LSB_REG
,
163 dev_err(dev
, "failed to read BME680_P5_LSB_REG\n");
166 calib
->par_p5
= le16_to_cpu(buf
);
168 ret
= regmap_read(data
->regmap
, BME680_P6_REG
, &tmp
);
170 dev_err(dev
, "failed to read BME680_P6_REG\n");
175 ret
= regmap_read(data
->regmap
, BME680_P7_REG
, &tmp
);
177 dev_err(dev
, "failed to read BME680_P7_REG\n");
182 ret
= regmap_bulk_read(data
->regmap
, BME680_P8_LSB_REG
,
185 dev_err(dev
, "failed to read BME680_P8_LSB_REG\n");
188 calib
->par_p8
= le16_to_cpu(buf
);
190 ret
= regmap_bulk_read(data
->regmap
, BME680_P9_LSB_REG
,
193 dev_err(dev
, "failed to read BME680_P9_LSB_REG\n");
196 calib
->par_p9
= le16_to_cpu(buf
);
198 ret
= regmap_read(data
->regmap
, BME680_P10_REG
, &tmp
);
200 dev_err(dev
, "failed to read BME680_P10_REG\n");
203 calib
->par_p10
= tmp
;
205 /* Humidity related coefficients */
206 ret
= regmap_read(data
->regmap
, BME680_H1_MSB_REG
, &tmp_msb
);
208 dev_err(dev
, "failed to read BME680_H1_MSB_REG\n");
212 ret
= regmap_read(data
->regmap
, BME680_H1_LSB_REG
, &tmp_lsb
);
214 dev_err(dev
, "failed to read BME680_H1_LSB_REG\n");
218 calib
->par_h1
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
219 (tmp_lsb
& BME680_BIT_H1_DATA_MSK
);
221 ret
= regmap_read(data
->regmap
, BME680_H2_MSB_REG
, &tmp_msb
);
223 dev_err(dev
, "failed to read BME680_H2_MSB_REG\n");
227 ret
= regmap_read(data
->regmap
, BME680_H2_LSB_REG
, &tmp_lsb
);
229 dev_err(dev
, "failed to read BME680_H2_LSB_REG\n");
233 calib
->par_h2
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
234 (tmp_lsb
>> BME680_HUM_REG_SHIFT_VAL
);
236 ret
= regmap_read(data
->regmap
, BME680_H3_REG
, &tmp
);
238 dev_err(dev
, "failed to read BME680_H3_REG\n");
243 ret
= regmap_read(data
->regmap
, BME680_H4_REG
, &tmp
);
245 dev_err(dev
, "failed to read BME680_H4_REG\n");
250 ret
= regmap_read(data
->regmap
, BME680_H5_REG
, &tmp
);
252 dev_err(dev
, "failed to read BME680_H5_REG\n");
257 ret
= regmap_read(data
->regmap
, BME680_H6_REG
, &tmp
);
259 dev_err(dev
, "failed to read BME680_H6_REG\n");
264 ret
= regmap_read(data
->regmap
, BME680_H7_REG
, &tmp
);
266 dev_err(dev
, "failed to read BME680_H7_REG\n");
271 /* Gas heater related coefficients */
272 ret
= regmap_read(data
->regmap
, BME680_GH1_REG
, &tmp
);
274 dev_err(dev
, "failed to read BME680_GH1_REG\n");
277 calib
->par_gh1
= tmp
;
279 ret
= regmap_bulk_read(data
->regmap
, BME680_GH2_LSB_REG
,
282 dev_err(dev
, "failed to read BME680_GH2_LSB_REG\n");
285 calib
->par_gh2
= le16_to_cpu(buf
);
287 ret
= regmap_read(data
->regmap
, BME680_GH3_REG
, &tmp
);
289 dev_err(dev
, "failed to read BME680_GH3_REG\n");
292 calib
->par_gh3
= tmp
;
294 /* Other coefficients */
295 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_RANGE
, &tmp
);
297 dev_err(dev
, "failed to read resistance heat range\n");
300 calib
->res_heat_range
= (tmp
& BME680_RHRANGE_MSK
) / 16;
302 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_VAL
, &tmp
);
304 dev_err(dev
, "failed to read resistance heat value\n");
307 calib
->res_heat_val
= tmp
;
309 ret
= regmap_read(data
->regmap
, BME680_REG_RANGE_SW_ERR
, &tmp
);
311 dev_err(dev
, "failed to read range software error\n");
314 calib
->range_sw_err
= (tmp
& BME680_RSERROR_MSK
) / 16;
320 * Taken from Bosch BME680 API:
321 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876
323 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore,
324 * output value of "3233" represents 32.33 DegC.
326 static s16
bme680_compensate_temp(struct bme680_data
*data
,
329 struct bme680_calib
*calib
= &data
->bme680
;
330 s64 var1
, var2
, var3
;
333 var1
= (adc_temp
>> 3) - (calib
->par_t1
<< 1);
334 var2
= (var1
* calib
->par_t2
) >> 11;
335 var3
= ((var1
>> 1) * (var1
>> 1)) >> 12;
336 var3
= (var3
* (calib
->par_t3
<< 4)) >> 14;
337 data
->t_fine
= var2
+ var3
;
338 calc_temp
= (data
->t_fine
* 5 + 128) >> 8;
344 * Taken from Bosch BME680 API:
345 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896
347 * Returns pressure measurement in Pa. Output value of "97356" represents
348 * 97356 Pa = 973.56 hPa.
350 static u32
bme680_compensate_press(struct bme680_data
*data
,
353 struct bme680_calib
*calib
= &data
->bme680
;
354 s32 var1
, var2
, var3
, press_comp
;
356 var1
= (data
->t_fine
>> 1) - 64000;
357 var2
= ((((var1
>> 2) * (var1
>> 2)) >> 11) * calib
->par_p6
) >> 2;
358 var2
= var2
+ (var1
* calib
->par_p5
<< 1);
359 var2
= (var2
>> 2) + (calib
->par_p4
<< 16);
360 var1
= (((((var1
>> 2) * (var1
>> 2)) >> 13) *
361 (calib
->par_p3
<< 5)) >> 3) +
362 ((calib
->par_p2
* var1
) >> 1);
364 var1
= ((32768 + var1
) * calib
->par_p1
) >> 15;
365 press_comp
= 1048576 - adc_press
;
366 press_comp
= ((press_comp
- (var2
>> 12)) * 3125);
368 if (press_comp
>= BME680_MAX_OVERFLOW_VAL
)
369 press_comp
= ((press_comp
/ (u32
)var1
) << 1);
371 press_comp
= ((press_comp
<< 1) / (u32
)var1
);
373 var1
= (calib
->par_p9
* (((press_comp
>> 3) *
374 (press_comp
>> 3)) >> 13)) >> 12;
375 var2
= ((press_comp
>> 2) * calib
->par_p8
) >> 13;
376 var3
= ((press_comp
>> 8) * (press_comp
>> 8) *
377 (press_comp
>> 8) * calib
->par_p10
) >> 17;
379 press_comp
+= (var1
+ var2
+ var3
+ (calib
->par_p7
<< 7)) >> 4;
385 * Taken from Bosch BME680 API:
386 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937
388 * Returns humidity measurement in percent, resolution is 0.001 percent. Output
389 * value of "43215" represents 43.215 %rH.
391 static u32
bme680_compensate_humid(struct bme680_data
*data
,
394 struct bme680_calib
*calib
= &data
->bme680
;
395 s32 var1
, var2
, var3
, var4
, var5
, var6
, temp_scaled
, calc_hum
;
397 temp_scaled
= (data
->t_fine
* 5 + 128) >> 8;
398 var1
= (adc_humid
- ((s32
) ((s32
) calib
->par_h1
* 16))) -
399 (((temp_scaled
* (s32
) calib
->par_h3
) / 100) >> 1);
400 var2
= ((s32
) calib
->par_h2
*
401 (((temp_scaled
* calib
->par_h4
) / 100) +
402 (((temp_scaled
* ((temp_scaled
* calib
->par_h5
) / 100))
403 >> 6) / 100) + (1 << 14))) >> 10;
405 var4
= calib
->par_h6
<< 7;
406 var4
= (var4
+ ((temp_scaled
* calib
->par_h7
) / 100)) >> 4;
407 var5
= ((var3
>> 14) * (var3
>> 14)) >> 10;
408 var6
= (var4
* var5
) >> 1;
409 calc_hum
= (((var3
+ var6
) >> 10) * 1000) >> 12;
411 if (calc_hum
> 100000) /* Cap at 100%rH */
413 else if (calc_hum
< 0)
420 * Taken from Bosch BME680 API:
421 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973
423 * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms.
425 static u32
bme680_compensate_gas(struct bme680_data
*data
, u16 gas_res_adc
,
428 struct bme680_calib
*calib
= &data
->bme680
;
434 /* Look up table for the possible gas range values */
435 const u32 lookupTable
[16] = {2147483647u, 2147483647u,
436 2147483647u, 2147483647u, 2147483647u,
437 2126008810u, 2147483647u, 2130303777u,
438 2147483647u, 2147483647u, 2143188679u,
439 2136746228u, 2147483647u, 2126008810u,
440 2147483647u, 2147483647u};
442 var1
= ((1340 + (5 * (s64
) calib
->range_sw_err
)) *
443 ((s64
) lookupTable
[gas_range
])) >> 16;
444 var2
= ((gas_res_adc
<< 15) - 16777216) + var1
;
445 var3
= ((125000 << (15 - gas_range
)) * var1
) >> 9;
447 calc_gas_res
= div64_s64(var3
, (s64
) var2
);
453 * Taken from Bosch BME680 API:
454 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002
456 static u8
bme680_calc_heater_res(struct bme680_data
*data
, u16 temp
)
458 struct bme680_calib
*calib
= &data
->bme680
;
459 s32 var1
, var2
, var3
, var4
, var5
, heatr_res_x100
;
462 if (temp
> 400) /* Cap temperature */
465 var1
= (((s32
) BME680_AMB_TEMP
* calib
->par_gh3
) / 1000) * 256;
466 var2
= (calib
->par_gh1
+ 784) * (((((calib
->par_gh2
+ 154009) *
469 var3
= var1
+ (var2
/ 2);
470 var4
= (var3
/ (calib
->res_heat_range
+ 4));
471 var5
= 131 * calib
->res_heat_val
+ 65536;
472 heatr_res_x100
= ((var4
/ var5
) - 250) * 34;
473 heatr_res
= (heatr_res_x100
+ 50) / 100;
479 * Taken from Bosch BME680 API:
480 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188
482 static u8
bme680_calc_heater_dur(u16 dur
)
484 u8 durval
, factor
= 0;
487 durval
= 0xff; /* Max duration */
493 durval
= dur
+ (factor
* 64);
499 static int bme680_set_mode(struct bme680_data
*data
, bool mode
)
501 struct device
*dev
= regmap_get_device(data
->regmap
);
505 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
506 BME680_MODE_MASK
, BME680_MODE_FORCED
);
508 dev_err(dev
, "failed to set forced mode\n");
511 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
512 BME680_MODE_MASK
, BME680_MODE_SLEEP
);
514 dev_err(dev
, "failed to set sleep mode\n");
521 static int bme680_chip_config(struct bme680_data
*data
)
523 struct device
*dev
= regmap_get_device(data
->regmap
);
525 u8 osrs
= FIELD_PREP(BME680_OSRS_HUMIDITY_MASK
,
526 data
->oversampling_humid
+ 1);
528 * Highly recommended to set oversampling of humidity before
529 * temperature/pressure oversampling.
531 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_HUMIDITY
,
532 BME680_OSRS_HUMIDITY_MASK
, osrs
);
534 dev_err(dev
, "failed to write ctrl_hum register\n");
538 /* IIR filter settings */
539 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CONFIG
,
541 BME680_FILTER_COEFF_VAL
);
543 dev_err(dev
, "failed to write config register\n");
547 osrs
= FIELD_PREP(BME680_OSRS_TEMP_MASK
, data
->oversampling_temp
+ 1) |
548 FIELD_PREP(BME680_OSRS_PRESS_MASK
, data
->oversampling_press
+ 1);
550 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
551 BME680_OSRS_TEMP_MASK
|
552 BME680_OSRS_PRESS_MASK
,
555 dev_err(dev
, "failed to write ctrl_meas register\n");
560 static int bme680_gas_config(struct bme680_data
*data
)
562 struct device
*dev
= regmap_get_device(data
->regmap
);
564 u8 heatr_res
, heatr_dur
;
566 heatr_res
= bme680_calc_heater_res(data
, data
->heater_temp
);
568 /* set target heater temperature */
569 ret
= regmap_write(data
->regmap
, BME680_REG_RES_HEAT_0
, heatr_res
);
571 dev_err(dev
, "failed to write res_heat_0 register\n");
575 heatr_dur
= bme680_calc_heater_dur(data
->heater_dur
);
577 /* set target heating duration */
578 ret
= regmap_write(data
->regmap
, BME680_REG_GAS_WAIT_0
, heatr_dur
);
580 dev_err(dev
, "failted to write gas_wait_0 register\n");
584 /* Selecting the runGas and NB conversion settings for the sensor */
585 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_GAS_1
,
586 BME680_RUN_GAS_MASK
| BME680_NB_CONV_MASK
,
587 BME680_RUN_GAS_EN_BIT
| BME680_NB_CONV_0_VAL
);
589 dev_err(dev
, "failed to write ctrl_gas_1 register\n");
594 static int bme680_read_temp(struct bme680_data
*data
,
597 struct device
*dev
= regmap_get_device(data
->regmap
);
603 /* set forced mode to trigger measurement */
604 ret
= bme680_set_mode(data
, true);
608 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_TEMP_MSB
,
611 dev_err(dev
, "failed to read temperature\n");
615 adc_temp
= be32_to_cpu(tmp
) >> 12;
616 if (adc_temp
== BME680_MEAS_SKIPPED
) {
617 /* reading was skipped */
618 dev_err(dev
, "reading temperature skipped\n");
621 comp_temp
= bme680_compensate_temp(data
, adc_temp
);
623 * val might be NULL if we're called by the read_press/read_humid
624 * routine which is callled to get t_fine value used in
625 * compensate_press/compensate_humid to get compensated
626 * pressure/humidity readings.
631 return IIO_VAL_FRACTIONAL
;
637 static int bme680_read_press(struct bme680_data
*data
,
640 struct device
*dev
= regmap_get_device(data
->regmap
);
645 /* Read and compensate temperature to get a reading of t_fine */
646 ret
= bme680_read_temp(data
, NULL
, NULL
);
650 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_PRESS_MSB
,
653 dev_err(dev
, "failed to read pressure\n");
657 adc_press
= be32_to_cpu(tmp
) >> 12;
658 if (adc_press
== BME680_MEAS_SKIPPED
) {
659 /* reading was skipped */
660 dev_err(dev
, "reading pressure skipped\n");
664 *val
= bme680_compensate_press(data
, adc_press
);
666 return IIO_VAL_FRACTIONAL
;
669 static int bme680_read_humid(struct bme680_data
*data
,
672 struct device
*dev
= regmap_get_device(data
->regmap
);
678 /* Read and compensate temperature to get a reading of t_fine */
679 ret
= bme680_read_temp(data
, NULL
, NULL
);
683 ret
= regmap_bulk_read(data
->regmap
, BM6880_REG_HUMIDITY_MSB
,
686 dev_err(dev
, "failed to read humidity\n");
690 adc_humidity
= be16_to_cpu(tmp
);
691 if (adc_humidity
== BME680_MEAS_SKIPPED
) {
692 /* reading was skipped */
693 dev_err(dev
, "reading humidity skipped\n");
696 comp_humidity
= bme680_compensate_humid(data
, adc_humidity
);
698 *val
= comp_humidity
;
700 return IIO_VAL_FRACTIONAL
;
703 static int bme680_read_gas(struct bme680_data
*data
,
706 struct device
*dev
= regmap_get_device(data
->regmap
);
713 /* Set heater settings */
714 ret
= bme680_gas_config(data
);
716 dev_err(dev
, "failed to set gas config\n");
720 /* set forced mode to trigger measurement */
721 ret
= bme680_set_mode(data
, true);
725 ret
= regmap_read(data
->regmap
, BME680_REG_MEAS_STAT_0
, &check
);
726 if (check
& BME680_GAS_MEAS_BIT
) {
727 dev_err(dev
, "gas measurement incomplete\n");
731 ret
= regmap_read(data
->regmap
, BME680_REG_GAS_R_LSB
, &check
);
733 dev_err(dev
, "failed to read gas_r_lsb register\n");
738 * occurs if either the gas heating duration was insuffient
739 * to reach the target heater temperature or the target
740 * heater temperature was too high for the heater sink to
743 if ((check
& BME680_GAS_STAB_BIT
) == 0) {
744 dev_err(dev
, "heater failed to reach the target temperature\n");
748 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_GAS_MSB
,
751 dev_err(dev
, "failed to read gas resistance\n");
755 gas_range
= check
& BME680_GAS_RANGE_MASK
;
756 adc_gas_res
= be16_to_cpu(tmp
) >> BME680_ADC_GAS_RES_SHIFT
;
758 *val
= bme680_compensate_gas(data
, adc_gas_res
, gas_range
);
762 static int bme680_read_raw(struct iio_dev
*indio_dev
,
763 struct iio_chan_spec
const *chan
,
764 int *val
, int *val2
, long mask
)
766 struct bme680_data
*data
= iio_priv(indio_dev
);
769 case IIO_CHAN_INFO_PROCESSED
:
770 switch (chan
->type
) {
772 return bme680_read_temp(data
, val
, val2
);
774 return bme680_read_press(data
, val
, val2
);
775 case IIO_HUMIDITYRELATIVE
:
776 return bme680_read_humid(data
, val
, val2
);
778 return bme680_read_gas(data
, val
);
782 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
783 switch (chan
->type
) {
785 *val
= 1 << data
->oversampling_temp
;
788 *val
= 1 << data
->oversampling_press
;
790 case IIO_HUMIDITYRELATIVE
:
791 *val
= 1 << data
->oversampling_humid
;
801 static int bme680_write_oversampling_ratio_temp(struct bme680_data
*data
,
806 for (i
= 0; i
< ARRAY_SIZE(bme680_oversampling_avail
); i
++) {
807 if (bme680_oversampling_avail
[i
] == val
) {
808 data
->oversampling_temp
= ilog2(val
);
810 return bme680_chip_config(data
);
817 static int bme680_write_oversampling_ratio_press(struct bme680_data
*data
,
822 for (i
= 0; i
< ARRAY_SIZE(bme680_oversampling_avail
); i
++) {
823 if (bme680_oversampling_avail
[i
] == val
) {
824 data
->oversampling_press
= ilog2(val
);
826 return bme680_chip_config(data
);
833 static int bme680_write_oversampling_ratio_humid(struct bme680_data
*data
,
838 for (i
= 0; i
< ARRAY_SIZE(bme680_oversampling_avail
); i
++) {
839 if (bme680_oversampling_avail
[i
] == val
) {
840 data
->oversampling_humid
= ilog2(val
);
842 return bme680_chip_config(data
);
849 static int bme680_write_raw(struct iio_dev
*indio_dev
,
850 struct iio_chan_spec
const *chan
,
851 int val
, int val2
, long mask
)
853 struct bme680_data
*data
= iio_priv(indio_dev
);
856 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
857 switch (chan
->type
) {
859 return bme680_write_oversampling_ratio_temp(data
, val
);
861 return bme680_write_oversampling_ratio_press(data
, val
);
862 case IIO_HUMIDITYRELATIVE
:
863 return bme680_write_oversampling_ratio_humid(data
, val
);
872 static const char bme680_oversampling_ratio_show
[] = "1 2 4 8 16";
874 static IIO_CONST_ATTR(oversampling_ratio_available
,
875 bme680_oversampling_ratio_show
);
877 static struct attribute
*bme680_attributes
[] = {
878 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
882 static const struct attribute_group bme680_attribute_group
= {
883 .attrs
= bme680_attributes
,
886 static const struct iio_info bme680_info
= {
887 .read_raw
= &bme680_read_raw
,
888 .write_raw
= &bme680_write_raw
,
889 .attrs
= &bme680_attribute_group
,
892 static const char *bme680_match_acpi_device(struct device
*dev
)
894 const struct acpi_device_id
*id
;
896 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
900 return dev_name(dev
);
903 int bme680_core_probe(struct device
*dev
, struct regmap
*regmap
,
906 struct iio_dev
*indio_dev
;
907 struct bme680_data
*data
;
910 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
914 if (!name
&& ACPI_HANDLE(dev
))
915 name
= bme680_match_acpi_device(dev
);
917 data
= iio_priv(indio_dev
);
918 dev_set_drvdata(dev
, indio_dev
);
919 data
->regmap
= regmap
;
920 indio_dev
->dev
.parent
= dev
;
921 indio_dev
->name
= name
;
922 indio_dev
->channels
= bme680_channels
;
923 indio_dev
->num_channels
= ARRAY_SIZE(bme680_channels
);
924 indio_dev
->info
= &bme680_info
;
925 indio_dev
->modes
= INDIO_DIRECT_MODE
;
927 /* default values for the sensor */
928 data
->oversampling_humid
= ilog2(2); /* 2X oversampling rate */
929 data
->oversampling_press
= ilog2(4); /* 4X oversampling rate */
930 data
->oversampling_temp
= ilog2(8); /* 8X oversampling rate */
931 data
->heater_temp
= 320; /* degree Celsius */
932 data
->heater_dur
= 150; /* milliseconds */
934 ret
= bme680_chip_config(data
);
936 dev_err(dev
, "failed to set chip_config data\n");
940 ret
= bme680_gas_config(data
);
942 dev_err(dev
, "failed to set gas config data\n");
946 ret
= bme680_read_calib(data
, &data
->bme680
);
949 "failed to read calibration coefficients at probe\n");
953 return devm_iio_device_register(dev
, indio_dev
);
955 EXPORT_SYMBOL_GPL(bme680_core_probe
);
957 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
958 MODULE_DESCRIPTION("Bosch BME680 Driver");
959 MODULE_LICENSE("GPL v2");