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 int bme680_read_calib(struct bme680_data
*data
,
95 struct bme680_calib
*calib
)
97 struct device
*dev
= regmap_get_device(data
->regmap
);
98 unsigned int tmp
, tmp_msb
, tmp_lsb
;
102 /* Temperature related coefficients */
103 ret
= regmap_bulk_read(data
->regmap
, BME680_T1_LSB_REG
, (u8
*) &buf
, 2);
105 dev_err(dev
, "failed to read BME680_T1_LSB_REG\n");
108 calib
->par_t1
= le16_to_cpu(buf
);
110 ret
= regmap_bulk_read(data
->regmap
, BME680_T2_LSB_REG
, (u8
*) &buf
, 2);
112 dev_err(dev
, "failed to read BME680_T2_LSB_REG\n");
115 calib
->par_t2
= le16_to_cpu(buf
);
117 ret
= regmap_read(data
->regmap
, BME680_T3_REG
, &tmp
);
119 dev_err(dev
, "failed to read BME680_T3_REG\n");
124 /* Pressure related coefficients */
125 ret
= regmap_bulk_read(data
->regmap
, BME680_P1_LSB_REG
, (u8
*) &buf
, 2);
127 dev_err(dev
, "failed to read BME680_P1_LSB_REG\n");
130 calib
->par_p1
= le16_to_cpu(buf
);
132 ret
= regmap_bulk_read(data
->regmap
, BME680_P2_LSB_REG
, (u8
*) &buf
, 2);
134 dev_err(dev
, "failed to read BME680_P2_LSB_REG\n");
137 calib
->par_p2
= le16_to_cpu(buf
);
139 ret
= regmap_read(data
->regmap
, BME680_P3_REG
, &tmp
);
141 dev_err(dev
, "failed to read BME680_P3_REG\n");
146 ret
= regmap_bulk_read(data
->regmap
, BME680_P4_LSB_REG
, (u8
*) &buf
, 2);
148 dev_err(dev
, "failed to read BME680_P4_LSB_REG\n");
151 calib
->par_p4
= le16_to_cpu(buf
);
153 ret
= regmap_bulk_read(data
->regmap
, BME680_P5_LSB_REG
, (u8
*) &buf
, 2);
155 dev_err(dev
, "failed to read BME680_P5_LSB_REG\n");
158 calib
->par_p5
= le16_to_cpu(buf
);
160 ret
= regmap_read(data
->regmap
, BME680_P6_REG
, &tmp
);
162 dev_err(dev
, "failed to read BME680_P6_REG\n");
167 ret
= regmap_read(data
->regmap
, BME680_P7_REG
, &tmp
);
169 dev_err(dev
, "failed to read BME680_P7_REG\n");
174 ret
= regmap_bulk_read(data
->regmap
, BME680_P8_LSB_REG
, (u8
*) &buf
, 2);
176 dev_err(dev
, "failed to read BME680_P8_LSB_REG\n");
179 calib
->par_p8
= le16_to_cpu(buf
);
181 ret
= regmap_bulk_read(data
->regmap
, BME680_P9_LSB_REG
, (u8
*) &buf
, 2);
183 dev_err(dev
, "failed to read BME680_P9_LSB_REG\n");
186 calib
->par_p9
= le16_to_cpu(buf
);
188 ret
= regmap_read(data
->regmap
, BME680_P10_REG
, &tmp
);
190 dev_err(dev
, "failed to read BME680_P10_REG\n");
193 calib
->par_p10
= tmp
;
195 /* Humidity related coefficients */
196 ret
= regmap_read(data
->regmap
, BME680_H1_MSB_REG
, &tmp_msb
);
198 dev_err(dev
, "failed to read BME680_H1_MSB_REG\n");
201 ret
= regmap_read(data
->regmap
, BME680_H1_LSB_REG
, &tmp_lsb
);
203 dev_err(dev
, "failed to read BME680_H1_LSB_REG\n");
206 calib
->par_h1
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
207 (tmp_lsb
& BME680_BIT_H1_DATA_MASK
);
209 ret
= regmap_read(data
->regmap
, BME680_H2_MSB_REG
, &tmp_msb
);
211 dev_err(dev
, "failed to read BME680_H2_MSB_REG\n");
214 ret
= regmap_read(data
->regmap
, BME680_H2_LSB_REG
, &tmp_lsb
);
216 dev_err(dev
, "failed to read BME680_H2_LSB_REG\n");
219 calib
->par_h2
= (tmp_msb
<< BME680_HUM_REG_SHIFT_VAL
) |
220 (tmp_lsb
>> BME680_HUM_REG_SHIFT_VAL
);
222 ret
= regmap_read(data
->regmap
, BME680_H3_REG
, &tmp
);
224 dev_err(dev
, "failed to read BME680_H3_REG\n");
229 ret
= regmap_read(data
->regmap
, BME680_H4_REG
, &tmp
);
231 dev_err(dev
, "failed to read BME680_H4_REG\n");
236 ret
= regmap_read(data
->regmap
, BME680_H5_REG
, &tmp
);
238 dev_err(dev
, "failed to read BME680_H5_REG\n");
243 ret
= regmap_read(data
->regmap
, BME680_H6_REG
, &tmp
);
245 dev_err(dev
, "failed to read BME680_H6_REG\n");
250 ret
= regmap_read(data
->regmap
, BME680_H7_REG
, &tmp
);
252 dev_err(dev
, "failed to read BME680_H7_REG\n");
257 /* Gas heater related coefficients */
258 ret
= regmap_read(data
->regmap
, BME680_GH1_REG
, &tmp
);
260 dev_err(dev
, "failed to read BME680_GH1_REG\n");
263 calib
->par_gh1
= tmp
;
265 ret
= regmap_bulk_read(data
->regmap
, BME680_GH2_LSB_REG
, (u8
*) &buf
,
268 dev_err(dev
, "failed to read BME680_GH2_LSB_REG\n");
271 calib
->par_gh2
= le16_to_cpu(buf
);
273 ret
= regmap_read(data
->regmap
, BME680_GH3_REG
, &tmp
);
275 dev_err(dev
, "failed to read BME680_GH3_REG\n");
278 calib
->par_gh3
= tmp
;
280 /* Other coefficients */
281 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_RANGE
, &tmp
);
283 dev_err(dev
, "failed to read resistance heat range\n");
286 calib
->res_heat_range
= FIELD_GET(BME680_RHRANGE_MASK
, tmp
);
288 ret
= regmap_read(data
->regmap
, BME680_REG_RES_HEAT_VAL
, &tmp
);
290 dev_err(dev
, "failed to read resistance heat value\n");
293 calib
->res_heat_val
= tmp
;
295 ret
= regmap_read(data
->regmap
, BME680_REG_RANGE_SW_ERR
, &tmp
);
297 dev_err(dev
, "failed to read range software error\n");
300 calib
->range_sw_err
= FIELD_GET(BME680_RSERROR_MASK
, tmp
);
306 * Taken from Bosch BME680 API:
307 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876
309 * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore,
310 * output value of "3233" represents 32.33 DegC.
312 static s16
bme680_compensate_temp(struct bme680_data
*data
,
315 struct bme680_calib
*calib
= &data
->bme680
;
316 s64 var1
, var2
, var3
;
319 var1
= (adc_temp
>> 3) - (calib
->par_t1
<< 1);
320 var2
= (var1
* calib
->par_t2
) >> 11;
321 var3
= ((var1
>> 1) * (var1
>> 1)) >> 12;
322 var3
= (var3
* (calib
->par_t3
<< 4)) >> 14;
323 data
->t_fine
= var2
+ var3
;
324 calc_temp
= (data
->t_fine
* 5 + 128) >> 8;
330 * Taken from Bosch BME680 API:
331 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896
333 * Returns pressure measurement in Pa. Output value of "97356" represents
334 * 97356 Pa = 973.56 hPa.
336 static u32
bme680_compensate_press(struct bme680_data
*data
,
339 struct bme680_calib
*calib
= &data
->bme680
;
340 s32 var1
, var2
, var3
, press_comp
;
342 var1
= (data
->t_fine
>> 1) - 64000;
343 var2
= ((((var1
>> 2) * (var1
>> 2)) >> 11) * calib
->par_p6
) >> 2;
344 var2
= var2
+ (var1
* calib
->par_p5
<< 1);
345 var2
= (var2
>> 2) + (calib
->par_p4
<< 16);
346 var1
= (((((var1
>> 2) * (var1
>> 2)) >> 13) *
347 (calib
->par_p3
<< 5)) >> 3) +
348 ((calib
->par_p2
* var1
) >> 1);
350 var1
= ((32768 + var1
) * calib
->par_p1
) >> 15;
351 press_comp
= 1048576 - adc_press
;
352 press_comp
= ((press_comp
- (var2
>> 12)) * 3125);
354 if (press_comp
>= BME680_MAX_OVERFLOW_VAL
)
355 press_comp
= ((press_comp
/ (u32
)var1
) << 1);
357 press_comp
= ((press_comp
<< 1) / (u32
)var1
);
359 var1
= (calib
->par_p9
* (((press_comp
>> 3) *
360 (press_comp
>> 3)) >> 13)) >> 12;
361 var2
= ((press_comp
>> 2) * calib
->par_p8
) >> 13;
362 var3
= ((press_comp
>> 8) * (press_comp
>> 8) *
363 (press_comp
>> 8) * calib
->par_p10
) >> 17;
365 press_comp
+= (var1
+ var2
+ var3
+ (calib
->par_p7
<< 7)) >> 4;
371 * Taken from Bosch BME680 API:
372 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937
374 * Returns humidity measurement in percent, resolution is 0.001 percent. Output
375 * value of "43215" represents 43.215 %rH.
377 static u32
bme680_compensate_humid(struct bme680_data
*data
,
380 struct bme680_calib
*calib
= &data
->bme680
;
381 s32 var1
, var2
, var3
, var4
, var5
, var6
, temp_scaled
, calc_hum
;
383 temp_scaled
= (data
->t_fine
* 5 + 128) >> 8;
384 var1
= (adc_humid
- ((s32
) ((s32
) calib
->par_h1
* 16))) -
385 (((temp_scaled
* (s32
) calib
->par_h3
) / 100) >> 1);
386 var2
= ((s32
) calib
->par_h2
*
387 (((temp_scaled
* calib
->par_h4
) / 100) +
388 (((temp_scaled
* ((temp_scaled
* calib
->par_h5
) / 100))
389 >> 6) / 100) + (1 << 14))) >> 10;
391 var4
= calib
->par_h6
<< 7;
392 var4
= (var4
+ ((temp_scaled
* calib
->par_h7
) / 100)) >> 4;
393 var5
= ((var3
>> 14) * (var3
>> 14)) >> 10;
394 var6
= (var4
* var5
) >> 1;
395 calc_hum
= (((var3
+ var6
) >> 10) * 1000) >> 12;
397 calc_hum
= clamp(calc_hum
, 0, 100000); /* clamp between 0-100 %rH */
403 * Taken from Bosch BME680 API:
404 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973
406 * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms.
408 static u32
bme680_compensate_gas(struct bme680_data
*data
, u16 gas_res_adc
,
411 struct bme680_calib
*calib
= &data
->bme680
;
417 /* Look up table for the possible gas range values */
418 const u32 lookupTable
[16] = {2147483647u, 2147483647u,
419 2147483647u, 2147483647u, 2147483647u,
420 2126008810u, 2147483647u, 2130303777u,
421 2147483647u, 2147483647u, 2143188679u,
422 2136746228u, 2147483647u, 2126008810u,
423 2147483647u, 2147483647u};
425 var1
= ((1340 + (5 * (s64
) calib
->range_sw_err
)) *
426 ((s64
) lookupTable
[gas_range
])) >> 16;
427 var2
= ((gas_res_adc
<< 15) - 16777216) + var1
;
428 var3
= ((125000 << (15 - gas_range
)) * var1
) >> 9;
430 calc_gas_res
= div64_s64(var3
, (s64
) var2
);
436 * Taken from Bosch BME680 API:
437 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002
439 static u8
bme680_calc_heater_res(struct bme680_data
*data
, u16 temp
)
441 struct bme680_calib
*calib
= &data
->bme680
;
442 s32 var1
, var2
, var3
, var4
, var5
, heatr_res_x100
;
445 if (temp
> 400) /* Cap temperature */
448 var1
= (((s32
) BME680_AMB_TEMP
* calib
->par_gh3
) / 1000) * 256;
449 var2
= (calib
->par_gh1
+ 784) * (((((calib
->par_gh2
+ 154009) *
452 var3
= var1
+ (var2
/ 2);
453 var4
= (var3
/ (calib
->res_heat_range
+ 4));
454 var5
= 131 * calib
->res_heat_val
+ 65536;
455 heatr_res_x100
= ((var4
/ var5
) - 250) * 34;
456 heatr_res
= (heatr_res_x100
+ 50) / 100;
462 * Taken from Bosch BME680 API:
463 * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188
465 static u8
bme680_calc_heater_dur(u16 dur
)
467 u8 durval
, factor
= 0;
470 durval
= 0xff; /* Max duration */
476 durval
= dur
+ (factor
* 64);
482 static int bme680_set_mode(struct bme680_data
*data
, bool mode
)
484 struct device
*dev
= regmap_get_device(data
->regmap
);
488 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
489 BME680_MODE_MASK
, BME680_MODE_FORCED
);
491 dev_err(dev
, "failed to set forced mode\n");
494 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
495 BME680_MODE_MASK
, BME680_MODE_SLEEP
);
497 dev_err(dev
, "failed to set sleep mode\n");
504 static u8
bme680_oversampling_to_reg(u8 val
)
506 return ilog2(val
) + 1;
509 static int bme680_chip_config(struct bme680_data
*data
)
511 struct device
*dev
= regmap_get_device(data
->regmap
);
516 BME680_OSRS_HUMIDITY_MASK
,
517 bme680_oversampling_to_reg(data
->oversampling_humid
));
519 * Highly recommended to set oversampling of humidity before
520 * temperature/pressure oversampling.
522 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_HUMIDITY
,
523 BME680_OSRS_HUMIDITY_MASK
, osrs
);
525 dev_err(dev
, "failed to write ctrl_hum register\n");
529 /* IIR filter settings */
530 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CONFIG
,
532 BME680_FILTER_COEFF_VAL
);
534 dev_err(dev
, "failed to write config register\n");
538 osrs
= FIELD_PREP(BME680_OSRS_TEMP_MASK
,
539 bme680_oversampling_to_reg(data
->oversampling_temp
)) |
540 FIELD_PREP(BME680_OSRS_PRESS_MASK
,
541 bme680_oversampling_to_reg(data
->oversampling_press
));
542 ret
= regmap_write_bits(data
->regmap
, BME680_REG_CTRL_MEAS
,
543 BME680_OSRS_TEMP_MASK
| BME680_OSRS_PRESS_MASK
,
546 dev_err(dev
, "failed to write ctrl_meas register\n");
551 static int bme680_gas_config(struct bme680_data
*data
)
553 struct device
*dev
= regmap_get_device(data
->regmap
);
555 u8 heatr_res
, heatr_dur
;
557 heatr_res
= bme680_calc_heater_res(data
, data
->heater_temp
);
559 /* set target heater temperature */
560 ret
= regmap_write(data
->regmap
, BME680_REG_RES_HEAT_0
, heatr_res
);
562 dev_err(dev
, "failed to write res_heat_0 register\n");
566 heatr_dur
= bme680_calc_heater_dur(data
->heater_dur
);
568 /* set target heating duration */
569 ret
= regmap_write(data
->regmap
, BME680_REG_GAS_WAIT_0
, heatr_dur
);
571 dev_err(dev
, "failed to write gas_wait_0 register\n");
575 /* Enable the gas sensor and select heater profile set-point 0 */
576 ret
= regmap_update_bits(data
->regmap
, BME680_REG_CTRL_GAS_1
,
577 BME680_RUN_GAS_MASK
| BME680_NB_CONV_MASK
,
578 FIELD_PREP(BME680_RUN_GAS_MASK
, 1) |
579 FIELD_PREP(BME680_NB_CONV_MASK
, 0));
581 dev_err(dev
, "failed to write ctrl_gas_1 register\n");
586 static int bme680_read_temp(struct bme680_data
*data
,
589 struct device
*dev
= regmap_get_device(data
->regmap
);
595 /* set forced mode to trigger measurement */
596 ret
= bme680_set_mode(data
, true);
600 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_TEMP_MSB
,
603 dev_err(dev
, "failed to read temperature\n");
607 adc_temp
= be32_to_cpu(tmp
) >> 12;
608 if (adc_temp
== BME680_MEAS_SKIPPED
) {
609 /* reading was skipped */
610 dev_err(dev
, "reading temperature skipped\n");
613 comp_temp
= bme680_compensate_temp(data
, adc_temp
);
615 * val might be NULL if we're called by the read_press/read_humid
616 * routine which is callled to get t_fine value used in
617 * compensate_press/compensate_humid to get compensated
618 * pressure/humidity readings.
623 return IIO_VAL_FRACTIONAL
;
629 static int bme680_read_press(struct bme680_data
*data
,
632 struct device
*dev
= regmap_get_device(data
->regmap
);
637 /* Read and compensate temperature to get a reading of t_fine */
638 ret
= bme680_read_temp(data
, NULL
, NULL
);
642 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_PRESS_MSB
,
645 dev_err(dev
, "failed to read pressure\n");
649 adc_press
= be32_to_cpu(tmp
) >> 12;
650 if (adc_press
== BME680_MEAS_SKIPPED
) {
651 /* reading was skipped */
652 dev_err(dev
, "reading pressure skipped\n");
656 *val
= bme680_compensate_press(data
, adc_press
);
658 return IIO_VAL_FRACTIONAL
;
661 static int bme680_read_humid(struct bme680_data
*data
,
664 struct device
*dev
= regmap_get_device(data
->regmap
);
670 /* Read and compensate temperature to get a reading of t_fine */
671 ret
= bme680_read_temp(data
, NULL
, NULL
);
675 ret
= regmap_bulk_read(data
->regmap
, BM6880_REG_HUMIDITY_MSB
,
678 dev_err(dev
, "failed to read humidity\n");
682 adc_humidity
= be16_to_cpu(tmp
);
683 if (adc_humidity
== BME680_MEAS_SKIPPED
) {
684 /* reading was skipped */
685 dev_err(dev
, "reading humidity skipped\n");
688 comp_humidity
= bme680_compensate_humid(data
, adc_humidity
);
690 *val
= comp_humidity
;
692 return IIO_VAL_FRACTIONAL
;
695 static int bme680_read_gas(struct bme680_data
*data
,
698 struct device
*dev
= regmap_get_device(data
->regmap
);
705 /* Set heater settings */
706 ret
= bme680_gas_config(data
);
708 dev_err(dev
, "failed to set gas config\n");
712 /* set forced mode to trigger measurement */
713 ret
= bme680_set_mode(data
, true);
717 ret
= regmap_read(data
->regmap
, BME680_REG_MEAS_STAT_0
, &check
);
718 if (check
& BME680_GAS_MEAS_BIT
) {
719 dev_err(dev
, "gas measurement incomplete\n");
723 ret
= regmap_read(data
->regmap
, BME680_REG_GAS_R_LSB
, &check
);
725 dev_err(dev
, "failed to read gas_r_lsb register\n");
730 * occurs if either the gas heating duration was insuffient
731 * to reach the target heater temperature or the target
732 * heater temperature was too high for the heater sink to
735 if ((check
& BME680_GAS_STAB_BIT
) == 0) {
736 dev_err(dev
, "heater failed to reach the target temperature\n");
740 ret
= regmap_bulk_read(data
->regmap
, BME680_REG_GAS_MSB
,
743 dev_err(dev
, "failed to read gas resistance\n");
747 gas_range
= check
& BME680_GAS_RANGE_MASK
;
748 adc_gas_res
= be16_to_cpu(tmp
) >> BME680_ADC_GAS_RES_SHIFT
;
750 *val
= bme680_compensate_gas(data
, adc_gas_res
, gas_range
);
754 static int bme680_read_raw(struct iio_dev
*indio_dev
,
755 struct iio_chan_spec
const *chan
,
756 int *val
, int *val2
, long mask
)
758 struct bme680_data
*data
= iio_priv(indio_dev
);
761 case IIO_CHAN_INFO_PROCESSED
:
762 switch (chan
->type
) {
764 return bme680_read_temp(data
, val
, val2
);
766 return bme680_read_press(data
, val
, val2
);
767 case IIO_HUMIDITYRELATIVE
:
768 return bme680_read_humid(data
, val
, val2
);
770 return bme680_read_gas(data
, val
);
774 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
775 switch (chan
->type
) {
777 *val
= data
->oversampling_temp
;
780 *val
= data
->oversampling_press
;
782 case IIO_HUMIDITYRELATIVE
:
783 *val
= data
->oversampling_humid
;
793 static bool bme680_is_valid_oversampling(int rate
)
795 return (rate
> 0 && rate
<= 16 && is_power_of_2(rate
));
798 static int bme680_write_raw(struct iio_dev
*indio_dev
,
799 struct iio_chan_spec
const *chan
,
800 int val
, int val2
, long mask
)
802 struct bme680_data
*data
= iio_priv(indio_dev
);
808 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
810 if (!bme680_is_valid_oversampling(val
))
813 switch (chan
->type
) {
815 data
->oversampling_temp
= val
;
818 data
->oversampling_press
= val
;
820 case IIO_HUMIDITYRELATIVE
:
821 data
->oversampling_humid
= val
;
827 return bme680_chip_config(data
);
834 static const char bme680_oversampling_ratio_show
[] = "1 2 4 8 16";
836 static IIO_CONST_ATTR(oversampling_ratio_available
,
837 bme680_oversampling_ratio_show
);
839 static struct attribute
*bme680_attributes
[] = {
840 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
844 static const struct attribute_group bme680_attribute_group
= {
845 .attrs
= bme680_attributes
,
848 static const struct iio_info bme680_info
= {
849 .read_raw
= &bme680_read_raw
,
850 .write_raw
= &bme680_write_raw
,
851 .attrs
= &bme680_attribute_group
,
854 static const char *bme680_match_acpi_device(struct device
*dev
)
856 const struct acpi_device_id
*id
;
858 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
862 return dev_name(dev
);
865 int bme680_core_probe(struct device
*dev
, struct regmap
*regmap
,
868 struct iio_dev
*indio_dev
;
869 struct bme680_data
*data
;
872 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
876 if (!name
&& ACPI_HANDLE(dev
))
877 name
= bme680_match_acpi_device(dev
);
879 data
= iio_priv(indio_dev
);
880 dev_set_drvdata(dev
, indio_dev
);
881 data
->regmap
= regmap
;
882 indio_dev
->dev
.parent
= dev
;
883 indio_dev
->name
= name
;
884 indio_dev
->channels
= bme680_channels
;
885 indio_dev
->num_channels
= ARRAY_SIZE(bme680_channels
);
886 indio_dev
->info
= &bme680_info
;
887 indio_dev
->modes
= INDIO_DIRECT_MODE
;
889 /* default values for the sensor */
890 data
->oversampling_humid
= 2; /* 2X oversampling rate */
891 data
->oversampling_press
= 4; /* 4X oversampling rate */
892 data
->oversampling_temp
= 8; /* 8X oversampling rate */
893 data
->heater_temp
= 320; /* degree Celsius */
894 data
->heater_dur
= 150; /* milliseconds */
896 ret
= bme680_chip_config(data
);
898 dev_err(dev
, "failed to set chip_config data\n");
902 ret
= bme680_gas_config(data
);
904 dev_err(dev
, "failed to set gas config data\n");
908 ret
= bme680_read_calib(data
, &data
->bme680
);
911 "failed to read calibration coefficients at probe\n");
915 return devm_iio_device_register(dev
, indio_dev
);
917 EXPORT_SYMBOL_GPL(bme680_core_probe
);
919 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
920 MODULE_DESCRIPTION("Bosch BME680 Driver");
921 MODULE_LICENSE("GPL v2");