perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / iio / chemical / bme680_core.c
blob7d9bb62baa3fd3e8620f377143ecaca9dae69ae4
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 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;
101 int ret;
102 __le16 buf;
104 /* Temperature related coefficients */
105 ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG,
106 (u8 *) &buf, 2);
107 if (ret < 0) {
108 dev_err(dev, "failed to read BME680_T1_LSB_REG\n");
109 return ret;
111 calib->par_t1 = le16_to_cpu(buf);
113 ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG,
114 (u8 *) &buf, 2);
115 if (ret < 0) {
116 dev_err(dev, "failed to read BME680_T2_LSB_REG\n");
117 return ret;
119 calib->par_t2 = le16_to_cpu(buf);
121 ret = regmap_read(data->regmap, BME680_T3_REG, &tmp);
122 if (ret < 0) {
123 dev_err(dev, "failed to read BME680_T3_REG\n");
124 return ret;
126 calib->par_t3 = tmp;
128 /* Pressure related coefficients */
129 ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG,
130 (u8 *) &buf, 2);
131 if (ret < 0) {
132 dev_err(dev, "failed to read BME680_P1_LSB_REG\n");
133 return ret;
135 calib->par_p1 = le16_to_cpu(buf);
137 ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG,
138 (u8 *) &buf, 2);
139 if (ret < 0) {
140 dev_err(dev, "failed to read BME680_P2_LSB_REG\n");
141 return ret;
143 calib->par_p2 = le16_to_cpu(buf);
145 ret = regmap_read(data->regmap, BME680_P3_REG, &tmp);
146 if (ret < 0) {
147 dev_err(dev, "failed to read BME680_P3_REG\n");
148 return ret;
150 calib->par_p3 = tmp;
152 ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG,
153 (u8 *) &buf, 2);
154 if (ret < 0) {
155 dev_err(dev, "failed to read BME680_P4_LSB_REG\n");
156 return ret;
158 calib->par_p4 = le16_to_cpu(buf);
160 ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG,
161 (u8 *) &buf, 2);
162 if (ret < 0) {
163 dev_err(dev, "failed to read BME680_P5_LSB_REG\n");
164 return ret;
166 calib->par_p5 = le16_to_cpu(buf);
168 ret = regmap_read(data->regmap, BME680_P6_REG, &tmp);
169 if (ret < 0) {
170 dev_err(dev, "failed to read BME680_P6_REG\n");
171 return ret;
173 calib->par_p6 = tmp;
175 ret = regmap_read(data->regmap, BME680_P7_REG, &tmp);
176 if (ret < 0) {
177 dev_err(dev, "failed to read BME680_P7_REG\n");
178 return ret;
180 calib->par_p7 = tmp;
182 ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG,
183 (u8 *) &buf, 2);
184 if (ret < 0) {
185 dev_err(dev, "failed to read BME680_P8_LSB_REG\n");
186 return ret;
188 calib->par_p8 = le16_to_cpu(buf);
190 ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG,
191 (u8 *) &buf, 2);
192 if (ret < 0) {
193 dev_err(dev, "failed to read BME680_P9_LSB_REG\n");
194 return ret;
196 calib->par_p9 = le16_to_cpu(buf);
198 ret = regmap_read(data->regmap, BME680_P10_REG, &tmp);
199 if (ret < 0) {
200 dev_err(dev, "failed to read BME680_P10_REG\n");
201 return ret;
203 calib->par_p10 = tmp;
205 /* Humidity related coefficients */
206 ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb);
207 if (ret < 0) {
208 dev_err(dev, "failed to read BME680_H1_MSB_REG\n");
209 return ret;
212 ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb);
213 if (ret < 0) {
214 dev_err(dev, "failed to read BME680_H1_LSB_REG\n");
215 return ret;
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);
222 if (ret < 0) {
223 dev_err(dev, "failed to read BME680_H2_MSB_REG\n");
224 return ret;
227 ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb);
228 if (ret < 0) {
229 dev_err(dev, "failed to read BME680_H2_LSB_REG\n");
230 return ret;
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);
237 if (ret < 0) {
238 dev_err(dev, "failed to read BME680_H3_REG\n");
239 return ret;
241 calib->par_h3 = tmp;
243 ret = regmap_read(data->regmap, BME680_H4_REG, &tmp);
244 if (ret < 0) {
245 dev_err(dev, "failed to read BME680_H4_REG\n");
246 return ret;
248 calib->par_h4 = tmp;
250 ret = regmap_read(data->regmap, BME680_H5_REG, &tmp);
251 if (ret < 0) {
252 dev_err(dev, "failed to read BME680_H5_REG\n");
253 return ret;
255 calib->par_h5 = tmp;
257 ret = regmap_read(data->regmap, BME680_H6_REG, &tmp);
258 if (ret < 0) {
259 dev_err(dev, "failed to read BME680_H6_REG\n");
260 return ret;
262 calib->par_h6 = tmp;
264 ret = regmap_read(data->regmap, BME680_H7_REG, &tmp);
265 if (ret < 0) {
266 dev_err(dev, "failed to read BME680_H7_REG\n");
267 return ret;
269 calib->par_h7 = tmp;
271 /* Gas heater related coefficients */
272 ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp);
273 if (ret < 0) {
274 dev_err(dev, "failed to read BME680_GH1_REG\n");
275 return ret;
277 calib->par_gh1 = tmp;
279 ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG,
280 (u8 *) &buf, 2);
281 if (ret < 0) {
282 dev_err(dev, "failed to read BME680_GH2_LSB_REG\n");
283 return ret;
285 calib->par_gh2 = le16_to_cpu(buf);
287 ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp);
288 if (ret < 0) {
289 dev_err(dev, "failed to read BME680_GH3_REG\n");
290 return ret;
292 calib->par_gh3 = tmp;
294 /* Other coefficients */
295 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp);
296 if (ret < 0) {
297 dev_err(dev, "failed to read resistance heat range\n");
298 return ret;
300 calib->res_heat_range = (tmp & BME680_RHRANGE_MSK) / 16;
302 ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp);
303 if (ret < 0) {
304 dev_err(dev, "failed to read resistance heat value\n");
305 return ret;
307 calib->res_heat_val = tmp;
309 ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp);
310 if (ret < 0) {
311 dev_err(dev, "failed to read range software error\n");
312 return ret;
314 calib->range_sw_err = (tmp & BME680_RSERROR_MSK) / 16;
316 return 0;
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,
327 s32 adc_temp)
329 struct bme680_calib *calib = &data->bme680;
330 s64 var1, var2, var3;
331 s16 calc_temp;
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;
340 return calc_temp;
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,
351 u32 adc_press)
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);
363 var1 = var1 >> 18;
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);
370 else
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;
381 return press_comp;
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,
392 u16 adc_humid)
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;
404 var3 = var1 * var2;
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 */
412 calc_hum = 100000;
413 else if (calc_hum < 0)
414 calc_hum = 0;
416 return calc_hum;
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,
426 u8 gas_range)
428 struct bme680_calib *calib = &data->bme680;
429 s64 var1;
430 u64 var2;
431 s64 var3;
432 u32 calc_gas_res;
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;
446 var3 += (var2 >> 1);
447 calc_gas_res = div64_s64(var3, (s64) var2);
449 return calc_gas_res;
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;
460 u8 heatr_res;
462 if (temp > 400) /* Cap temperature */
463 temp = 400;
465 var1 = (((s32) BME680_AMB_TEMP * calib->par_gh3) / 1000) * 256;
466 var2 = (calib->par_gh1 + 784) * (((((calib->par_gh2 + 154009) *
467 temp * 5) / 100)
468 + 3276800) / 10);
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;
475 return heatr_res;
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;
486 if (dur >= 0xfc0) {
487 durval = 0xff; /* Max duration */
488 } else {
489 while (dur > 0x3F) {
490 dur = dur / 4;
491 factor += 1;
493 durval = dur + (factor * 64);
496 return durval;
499 static int bme680_set_mode(struct bme680_data *data, bool mode)
501 struct device *dev = regmap_get_device(data->regmap);
502 int ret;
504 if (mode) {
505 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
506 BME680_MODE_MASK, BME680_MODE_FORCED);
507 if (ret < 0)
508 dev_err(dev, "failed to set forced mode\n");
510 } else {
511 ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
512 BME680_MODE_MASK, BME680_MODE_SLEEP);
513 if (ret < 0)
514 dev_err(dev, "failed to set sleep mode\n");
518 return ret;
521 static int bme680_chip_config(struct bme680_data *data)
523 struct device *dev = regmap_get_device(data->regmap);
524 int ret;
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);
533 if (ret < 0) {
534 dev_err(dev, "failed to write ctrl_hum register\n");
535 return ret;
538 /* IIR filter settings */
539 ret = regmap_update_bits(data->regmap, BME680_REG_CONFIG,
540 BME680_FILTER_MASK,
541 BME680_FILTER_COEFF_VAL);
542 if (ret < 0) {
543 dev_err(dev, "failed to write config register\n");
544 return ret;
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,
553 osrs);
554 if (ret < 0)
555 dev_err(dev, "failed to write ctrl_meas register\n");
557 return ret;
560 static int bme680_gas_config(struct bme680_data *data)
562 struct device *dev = regmap_get_device(data->regmap);
563 int ret;
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);
570 if (ret < 0) {
571 dev_err(dev, "failed to write res_heat_0 register\n");
572 return ret;
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);
579 if (ret < 0) {
580 dev_err(dev, "failted to write gas_wait_0 register\n");
581 return ret;
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);
588 if (ret < 0)
589 dev_err(dev, "failed to write ctrl_gas_1 register\n");
591 return ret;
594 static int bme680_read_temp(struct bme680_data *data,
595 int *val, int *val2)
597 struct device *dev = regmap_get_device(data->regmap);
598 int ret;
599 __be32 tmp = 0;
600 s32 adc_temp;
601 s16 comp_temp;
603 /* set forced mode to trigger measurement */
604 ret = bme680_set_mode(data, true);
605 if (ret < 0)
606 return ret;
608 ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
609 (u8 *) &tmp, 3);
610 if (ret < 0) {
611 dev_err(dev, "failed to read temperature\n");
612 return ret;
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");
619 return -EINVAL;
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.
628 if (val && val2) {
629 *val = comp_temp;
630 *val2 = 100;
631 return IIO_VAL_FRACTIONAL;
634 return ret;
637 static int bme680_read_press(struct bme680_data *data,
638 int *val, int *val2)
640 struct device *dev = regmap_get_device(data->regmap);
641 int ret;
642 __be32 tmp = 0;
643 s32 adc_press;
645 /* Read and compensate temperature to get a reading of t_fine */
646 ret = bme680_read_temp(data, NULL, NULL);
647 if (ret < 0)
648 return ret;
650 ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
651 (u8 *) &tmp, 3);
652 if (ret < 0) {
653 dev_err(dev, "failed to read pressure\n");
654 return ret;
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");
661 return -EINVAL;
664 *val = bme680_compensate_press(data, adc_press);
665 *val2 = 100;
666 return IIO_VAL_FRACTIONAL;
669 static int bme680_read_humid(struct bme680_data *data,
670 int *val, int *val2)
672 struct device *dev = regmap_get_device(data->regmap);
673 int ret;
674 __be16 tmp = 0;
675 s32 adc_humidity;
676 u32 comp_humidity;
678 /* Read and compensate temperature to get a reading of t_fine */
679 ret = bme680_read_temp(data, NULL, NULL);
680 if (ret < 0)
681 return ret;
683 ret = regmap_bulk_read(data->regmap, BM6880_REG_HUMIDITY_MSB,
684 (u8 *) &tmp, 2);
685 if (ret < 0) {
686 dev_err(dev, "failed to read humidity\n");
687 return ret;
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");
694 return -EINVAL;
696 comp_humidity = bme680_compensate_humid(data, adc_humidity);
698 *val = comp_humidity;
699 *val2 = 1000;
700 return IIO_VAL_FRACTIONAL;
703 static int bme680_read_gas(struct bme680_data *data,
704 int *val)
706 struct device *dev = regmap_get_device(data->regmap);
707 int ret;
708 __be16 tmp = 0;
709 unsigned int check;
710 u16 adc_gas_res;
711 u8 gas_range;
713 /* Set heater settings */
714 ret = bme680_gas_config(data);
715 if (ret < 0) {
716 dev_err(dev, "failed to set gas config\n");
717 return ret;
720 /* set forced mode to trigger measurement */
721 ret = bme680_set_mode(data, true);
722 if (ret < 0)
723 return ret;
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");
728 return -EBUSY;
731 ret = regmap_read(data->regmap, BME680_REG_GAS_R_LSB, &check);
732 if (ret < 0) {
733 dev_err(dev, "failed to read gas_r_lsb register\n");
734 return ret;
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
741 * reach.
743 if ((check & BME680_GAS_STAB_BIT) == 0) {
744 dev_err(dev, "heater failed to reach the target temperature\n");
745 return -EINVAL;
748 ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
749 (u8 *) &tmp, 2);
750 if (ret < 0) {
751 dev_err(dev, "failed to read gas resistance\n");
752 return ret;
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);
759 return IIO_VAL_INT;
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);
768 switch (mask) {
769 case IIO_CHAN_INFO_PROCESSED:
770 switch (chan->type) {
771 case IIO_TEMP:
772 return bme680_read_temp(data, val, val2);
773 case IIO_PRESSURE:
774 return bme680_read_press(data, val, val2);
775 case IIO_HUMIDITYRELATIVE:
776 return bme680_read_humid(data, val, val2);
777 case IIO_RESISTANCE:
778 return bme680_read_gas(data, val);
779 default:
780 return -EINVAL;
782 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
783 switch (chan->type) {
784 case IIO_TEMP:
785 *val = 1 << data->oversampling_temp;
786 return IIO_VAL_INT;
787 case IIO_PRESSURE:
788 *val = 1 << data->oversampling_press;
789 return IIO_VAL_INT;
790 case IIO_HUMIDITYRELATIVE:
791 *val = 1 << data->oversampling_humid;
792 return IIO_VAL_INT;
793 default:
794 return -EINVAL;
796 default:
797 return -EINVAL;
801 static int bme680_write_oversampling_ratio_temp(struct bme680_data *data,
802 int val)
804 int i;
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);
814 return -EINVAL;
817 static int bme680_write_oversampling_ratio_press(struct bme680_data *data,
818 int val)
820 int i;
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);
830 return -EINVAL;
833 static int bme680_write_oversampling_ratio_humid(struct bme680_data *data,
834 int val)
836 int i;
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);
846 return -EINVAL;
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);
855 switch (mask) {
856 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
857 switch (chan->type) {
858 case IIO_TEMP:
859 return bme680_write_oversampling_ratio_temp(data, val);
860 case IIO_PRESSURE:
861 return bme680_write_oversampling_ratio_press(data, val);
862 case IIO_HUMIDITYRELATIVE:
863 return bme680_write_oversampling_ratio_humid(data, val);
864 default:
865 return -EINVAL;
867 default:
868 return -EINVAL;
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,
879 NULL,
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);
897 if (!id)
898 return NULL;
900 return dev_name(dev);
903 int bme680_core_probe(struct device *dev, struct regmap *regmap,
904 const char *name)
906 struct iio_dev *indio_dev;
907 struct bme680_data *data;
908 int ret;
910 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
911 if (!indio_dev)
912 return -ENOMEM;
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);
935 if (ret < 0) {
936 dev_err(dev, "failed to set chip_config data\n");
937 return ret;
940 ret = bme680_gas_config(data);
941 if (ret < 0) {
942 dev_err(dev, "failed to set gas config data\n");
943 return ret;
946 ret = bme680_read_calib(data, &data->bme680);
947 if (ret < 0) {
948 dev_err(dev,
949 "failed to read calibration coefficients at probe\n");
950 return ret;
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");