perf python: Do not force closing original perf descriptor in evlist.get_pollfd()
[linux/fpc-iii.git] / drivers / iio / chemical / bme680_core.c
blob70c1fe4366f4c6a17100b469452f8903281e665e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
8 * Datasheet:
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>
20 #include "bme680.h"
22 struct bme680_calib {
23 u16 par_t1;
24 s16 par_t2;
25 s8 par_t3;
26 u16 par_p1;
27 s16 par_p2;
28 s8 par_p3;
29 s16 par_p4;
30 s16 par_p5;
31 s8 par_p6;
32 s8 par_p7;
33 s16 par_p8;
34 s16 par_p9;
35 u8 par_p10;
36 u16 par_h1;
37 u16 par_h2;
38 s8 par_h3;
39 s8 par_h4;
40 s8 par_h5;
41 s8 par_h6;
42 s8 par_h7;
43 s8 par_gh1;
44 s16 par_gh2;
45 s8 par_gh3;
46 u8 res_heat_range;
47 s8 res_heat_val;
48 s8 range_sw_err;
51 struct bme680_data {
52 struct regmap *regmap;
53 struct bme680_calib bme680;
54 u8 oversampling_temp;
55 u8 oversampling_press;
56 u8 oversampling_humid;
57 u16 heater_dur;
58 u16 heater_temp;
60 * Carryover value from temperature conversion, used in pressure
61 * and humidity compensation calculations.
63 s32 t_fine;
66 const struct regmap_config bme680_regmap_config = {
67 .reg_bits = 8,
68 .val_bits = 8,
70 EXPORT_SYMBOL(bme680_regmap_config);
72 static const struct iio_chan_spec bme680_channels[] = {
74 .type = IIO_TEMP,
75 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
76 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
79 .type = IIO_PRESSURE,
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;
99 int ret;
100 __le16 buf;
102 /* Temperature related coefficients */
103 ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG, (u8 *) &buf, 2);
104 if (ret < 0) {
105 dev_err(dev, "failed to read BME680_T1_LSB_REG\n");
106 return ret;
108 calib->par_t1 = le16_to_cpu(buf);
110 ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG, (u8 *) &buf, 2);
111 if (ret < 0) {
112 dev_err(dev, "failed to read BME680_T2_LSB_REG\n");
113 return ret;
115 calib->par_t2 = le16_to_cpu(buf);
117 ret = regmap_read(data->regmap, BME680_T3_REG, &tmp);
118 if (ret < 0) {
119 dev_err(dev, "failed to read BME680_T3_REG\n");
120 return ret;
122 calib->par_t3 = tmp;
124 /* Pressure related coefficients */
125 ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG, (u8 *) &buf, 2);
126 if (ret < 0) {
127 dev_err(dev, "failed to read BME680_P1_LSB_REG\n");
128 return ret;
130 calib->par_p1 = le16_to_cpu(buf);
132 ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG, (u8 *) &buf, 2);
133 if (ret < 0) {
134 dev_err(dev, "failed to read BME680_P2_LSB_REG\n");
135 return ret;
137 calib->par_p2 = le16_to_cpu(buf);
139 ret = regmap_read(data->regmap, BME680_P3_REG, &tmp);
140 if (ret < 0) {
141 dev_err(dev, "failed to read BME680_P3_REG\n");
142 return ret;
144 calib->par_p3 = tmp;
146 ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG, (u8 *) &buf, 2);
147 if (ret < 0) {
148 dev_err(dev, "failed to read BME680_P4_LSB_REG\n");
149 return ret;
151 calib->par_p4 = le16_to_cpu(buf);
153 ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG, (u8 *) &buf, 2);
154 if (ret < 0) {
155 dev_err(dev, "failed to read BME680_P5_LSB_REG\n");
156 return ret;
158 calib->par_p5 = le16_to_cpu(buf);
160 ret = regmap_read(data->regmap, BME680_P6_REG, &tmp);
161 if (ret < 0) {
162 dev_err(dev, "failed to read BME680_P6_REG\n");
163 return ret;
165 calib->par_p6 = tmp;
167 ret = regmap_read(data->regmap, BME680_P7_REG, &tmp);
168 if (ret < 0) {
169 dev_err(dev, "failed to read BME680_P7_REG\n");
170 return ret;
172 calib->par_p7 = tmp;
174 ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG, (u8 *) &buf, 2);
175 if (ret < 0) {
176 dev_err(dev, "failed to read BME680_P8_LSB_REG\n");
177 return ret;
179 calib->par_p8 = le16_to_cpu(buf);
181 ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG, (u8 *) &buf, 2);
182 if (ret < 0) {
183 dev_err(dev, "failed to read BME680_P9_LSB_REG\n");
184 return ret;
186 calib->par_p9 = le16_to_cpu(buf);
188 ret = regmap_read(data->regmap, BME680_P10_REG, &tmp);
189 if (ret < 0) {
190 dev_err(dev, "failed to read BME680_P10_REG\n");
191 return ret;
193 calib->par_p10 = tmp;
195 /* Humidity related coefficients */
196 ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb);
197 if (ret < 0) {
198 dev_err(dev, "failed to read BME680_H1_MSB_REG\n");
199 return ret;
201 ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb);
202 if (ret < 0) {
203 dev_err(dev, "failed to read BME680_H1_LSB_REG\n");
204 return ret;
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);
210 if (ret < 0) {
211 dev_err(dev, "failed to read BME680_H2_MSB_REG\n");
212 return ret;
214 ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb);
215 if (ret < 0) {
216 dev_err(dev, "failed to read BME680_H2_LSB_REG\n");
217 return ret;
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);
223 if (ret < 0) {
224 dev_err(dev, "failed to read BME680_H3_REG\n");
225 return ret;
227 calib->par_h3 = tmp;
229 ret = regmap_read(data->regmap, BME680_H4_REG, &tmp);
230 if (ret < 0) {
231 dev_err(dev, "failed to read BME680_H4_REG\n");
232 return ret;
234 calib->par_h4 = tmp;
236 ret = regmap_read(data->regmap, BME680_H5_REG, &tmp);
237 if (ret < 0) {
238 dev_err(dev, "failed to read BME680_H5_REG\n");
239 return ret;
241 calib->par_h5 = tmp;
243 ret = regmap_read(data->regmap, BME680_H6_REG, &tmp);
244 if (ret < 0) {
245 dev_err(dev, "failed to read BME680_H6_REG\n");
246 return ret;
248 calib->par_h6 = tmp;
250 ret = regmap_read(data->regmap, BME680_H7_REG, &tmp);
251 if (ret < 0) {
252 dev_err(dev, "failed to read BME680_H7_REG\n");
253 return ret;
255 calib->par_h7 = tmp;
257 /* Gas heater related coefficients */
258 ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp);
259 if (ret < 0) {
260 dev_err(dev, "failed to read BME680_GH1_REG\n");
261 return ret;
263 calib->par_gh1 = tmp;
265 ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG, (u8 *) &buf,
267 if (ret < 0) {
268 dev_err(dev, "failed to read BME680_GH2_LSB_REG\n");
269 return ret;
271 calib->par_gh2 = le16_to_cpu(buf);
273 ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp);
274 if (ret < 0) {
275 dev_err(dev, "failed to read BME680_GH3_REG\n");
276 return ret;
278 calib->par_gh3 = tmp;
280 /* Other coefficients */
281 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp);
282 if (ret < 0) {
283 dev_err(dev, "failed to read resistance heat range\n");
284 return ret;
286 calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, tmp);
288 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp);
289 if (ret < 0) {
290 dev_err(dev, "failed to read resistance heat value\n");
291 return ret;
293 calib->res_heat_val = tmp;
295 ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp);
296 if (ret < 0) {
297 dev_err(dev, "failed to read range software error\n");
298 return ret;
300 calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, tmp);
302 return 0;
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,
313 s32 adc_temp)
315 struct bme680_calib *calib = &data->bme680;
316 s64 var1, var2, var3;
317 s16 calc_temp;
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;
326 return calc_temp;
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,
337 u32 adc_press)
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);
349 var1 = var1 >> 18;
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);
356 else
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;
367 return press_comp;
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,
378 u16 adc_humid)
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;
390 var3 = var1 * var2;
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 */
399 return calc_hum;
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,
409 u8 gas_range)
411 struct bme680_calib *calib = &data->bme680;
412 s64 var1;
413 u64 var2;
414 s64 var3;
415 u32 calc_gas_res;
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;
429 var3 += (var2 >> 1);
430 calc_gas_res = div64_s64(var3, (s64) var2);
432 return calc_gas_res;
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;
443 u8 heatr_res;
445 if (temp > 400) /* Cap temperature */
446 temp = 400;
448 var1 = (((s32) BME680_AMB_TEMP * calib->par_gh3) / 1000) * 256;
449 var2 = (calib->par_gh1 + 784) * (((((calib->par_gh2 + 154009) *
450 temp * 5) / 100)
451 + 3276800) / 10);
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;
458 return heatr_res;
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;
469 if (dur >= 0xfc0) {
470 durval = 0xff; /* Max duration */
471 } else {
472 while (dur > 0x3F) {
473 dur = dur / 4;
474 factor += 1;
476 durval = dur + (factor * 64);
479 return durval;
482 static int bme680_set_mode(struct bme680_data *data, bool mode)
484 struct device *dev = regmap_get_device(data->regmap);
485 int ret;
487 if (mode) {
488 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
489 BME680_MODE_MASK, BME680_MODE_FORCED);
490 if (ret < 0)
491 dev_err(dev, "failed to set forced mode\n");
493 } else {
494 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
495 BME680_MODE_MASK, BME680_MODE_SLEEP);
496 if (ret < 0)
497 dev_err(dev, "failed to set sleep mode\n");
501 return ret;
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);
512 int ret;
513 u8 osrs;
515 osrs = FIELD_PREP(
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);
524 if (ret < 0) {
525 dev_err(dev, "failed to write ctrl_hum register\n");
526 return ret;
529 /* IIR filter settings */
530 ret = regmap_update_bits(data->regmap, BME680_REG_CONFIG,
531 BME680_FILTER_MASK,
532 BME680_FILTER_COEFF_VAL);
533 if (ret < 0) {
534 dev_err(dev, "failed to write config register\n");
535 return ret;
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,
544 osrs);
545 if (ret < 0)
546 dev_err(dev, "failed to write ctrl_meas register\n");
548 return ret;
551 static int bme680_gas_config(struct bme680_data *data)
553 struct device *dev = regmap_get_device(data->regmap);
554 int ret;
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);
561 if (ret < 0) {
562 dev_err(dev, "failed to write res_heat_0 register\n");
563 return ret;
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);
570 if (ret < 0) {
571 dev_err(dev, "failed to write gas_wait_0 register\n");
572 return ret;
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));
580 if (ret < 0)
581 dev_err(dev, "failed to write ctrl_gas_1 register\n");
583 return ret;
586 static int bme680_read_temp(struct bme680_data *data,
587 int *val, int *val2)
589 struct device *dev = regmap_get_device(data->regmap);
590 int ret;
591 __be32 tmp = 0;
592 s32 adc_temp;
593 s16 comp_temp;
595 /* set forced mode to trigger measurement */
596 ret = bme680_set_mode(data, true);
597 if (ret < 0)
598 return ret;
600 ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
601 (u8 *) &tmp, 3);
602 if (ret < 0) {
603 dev_err(dev, "failed to read temperature\n");
604 return ret;
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");
611 return -EINVAL;
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.
620 if (val && val2) {
621 *val = comp_temp;
622 *val2 = 100;
623 return IIO_VAL_FRACTIONAL;
626 return ret;
629 static int bme680_read_press(struct bme680_data *data,
630 int *val, int *val2)
632 struct device *dev = regmap_get_device(data->regmap);
633 int ret;
634 __be32 tmp = 0;
635 s32 adc_press;
637 /* Read and compensate temperature to get a reading of t_fine */
638 ret = bme680_read_temp(data, NULL, NULL);
639 if (ret < 0)
640 return ret;
642 ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
643 (u8 *) &tmp, 3);
644 if (ret < 0) {
645 dev_err(dev, "failed to read pressure\n");
646 return ret;
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");
653 return -EINVAL;
656 *val = bme680_compensate_press(data, adc_press);
657 *val2 = 100;
658 return IIO_VAL_FRACTIONAL;
661 static int bme680_read_humid(struct bme680_data *data,
662 int *val, int *val2)
664 struct device *dev = regmap_get_device(data->regmap);
665 int ret;
666 __be16 tmp = 0;
667 s32 adc_humidity;
668 u32 comp_humidity;
670 /* Read and compensate temperature to get a reading of t_fine */
671 ret = bme680_read_temp(data, NULL, NULL);
672 if (ret < 0)
673 return ret;
675 ret = regmap_bulk_read(data->regmap, BM6880_REG_HUMIDITY_MSB,
676 (u8 *) &tmp, 2);
677 if (ret < 0) {
678 dev_err(dev, "failed to read humidity\n");
679 return ret;
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");
686 return -EINVAL;
688 comp_humidity = bme680_compensate_humid(data, adc_humidity);
690 *val = comp_humidity;
691 *val2 = 1000;
692 return IIO_VAL_FRACTIONAL;
695 static int bme680_read_gas(struct bme680_data *data,
696 int *val)
698 struct device *dev = regmap_get_device(data->regmap);
699 int ret;
700 __be16 tmp = 0;
701 unsigned int check;
702 u16 adc_gas_res;
703 u8 gas_range;
705 /* Set heater settings */
706 ret = bme680_gas_config(data);
707 if (ret < 0) {
708 dev_err(dev, "failed to set gas config\n");
709 return ret;
712 /* set forced mode to trigger measurement */
713 ret = bme680_set_mode(data, true);
714 if (ret < 0)
715 return ret;
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");
720 return -EBUSY;
723 ret = regmap_read(data->regmap, BME680_REG_GAS_R_LSB, &check);
724 if (ret < 0) {
725 dev_err(dev, "failed to read gas_r_lsb register\n");
726 return ret;
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
733 * reach.
735 if ((check & BME680_GAS_STAB_BIT) == 0) {
736 dev_err(dev, "heater failed to reach the target temperature\n");
737 return -EINVAL;
740 ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
741 (u8 *) &tmp, 2);
742 if (ret < 0) {
743 dev_err(dev, "failed to read gas resistance\n");
744 return ret;
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);
751 return IIO_VAL_INT;
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);
760 switch (mask) {
761 case IIO_CHAN_INFO_PROCESSED:
762 switch (chan->type) {
763 case IIO_TEMP:
764 return bme680_read_temp(data, val, val2);
765 case IIO_PRESSURE:
766 return bme680_read_press(data, val, val2);
767 case IIO_HUMIDITYRELATIVE:
768 return bme680_read_humid(data, val, val2);
769 case IIO_RESISTANCE:
770 return bme680_read_gas(data, val);
771 default:
772 return -EINVAL;
774 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
775 switch (chan->type) {
776 case IIO_TEMP:
777 *val = data->oversampling_temp;
778 return IIO_VAL_INT;
779 case IIO_PRESSURE:
780 *val = data->oversampling_press;
781 return IIO_VAL_INT;
782 case IIO_HUMIDITYRELATIVE:
783 *val = data->oversampling_humid;
784 return IIO_VAL_INT;
785 default:
786 return -EINVAL;
788 default:
789 return -EINVAL;
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);
804 if (val2 != 0)
805 return -EINVAL;
807 switch (mask) {
808 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
810 if (!bme680_is_valid_oversampling(val))
811 return -EINVAL;
813 switch (chan->type) {
814 case IIO_TEMP:
815 data->oversampling_temp = val;
816 break;
817 case IIO_PRESSURE:
818 data->oversampling_press = val;
819 break;
820 case IIO_HUMIDITYRELATIVE:
821 data->oversampling_humid = val;
822 break;
823 default:
824 return -EINVAL;
827 return bme680_chip_config(data);
829 default:
830 return -EINVAL;
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,
841 NULL,
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);
859 if (!id)
860 return NULL;
862 return dev_name(dev);
865 int bme680_core_probe(struct device *dev, struct regmap *regmap,
866 const char *name)
868 struct iio_dev *indio_dev;
869 struct bme680_data *data;
870 int ret;
872 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
873 if (!indio_dev)
874 return -ENOMEM;
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);
897 if (ret < 0) {
898 dev_err(dev, "failed to set chip_config data\n");
899 return ret;
902 ret = bme680_gas_config(data);
903 if (ret < 0) {
904 dev_err(dev, "failed to set gas config data\n");
905 return ret;
908 ret = bme680_read_calib(data, &data->bme680);
909 if (ret < 0) {
910 dev_err(dev,
911 "failed to read calibration coefficients at probe\n");
912 return ret;
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");