1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Bosch BME680 - Temperature, Pressure, Humidity & Gas Sensor
4  *
5  * Copyright (C) 2017 - 2018 Bosch Sensortec GmbH
6  * Copyright (C) 2018 Himanshu Jha <himanshujha199640@gmail.com>
7  *
8  * Datasheet:
9  * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME680-DS001-00.pdf
10  */
11 #include <linux/acpi.h>
12 #include <linux/bitfield.h>
13 #include <linux/cleanup.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/module.h>
17 #include <linux/log2.h>
18 #include <linux/regmap.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
21 
22 #include "bme680.h"
23 
24 struct bme680_calib {
25 	u16 par_t1;
26 	s16 par_t2;
27 	s8  par_t3;
28 	u16 par_p1;
29 	s16 par_p2;
30 	s8  par_p3;
31 	s16 par_p4;
32 	s16 par_p5;
33 	s8  par_p6;
34 	s8  par_p7;
35 	s16 par_p8;
36 	s16 par_p9;
37 	u8  par_p10;
38 	u16 par_h1;
39 	u16 par_h2;
40 	s8  par_h3;
41 	s8  par_h4;
42 	s8  par_h5;
43 	u8  par_h6;
44 	s8  par_h7;
45 	s8  par_gh1;
46 	s16 par_gh2;
47 	s8  par_gh3;
48 	u8  res_heat_range;
49 	s8  res_heat_val;
50 	s8  range_sw_err;
51 };
52 
53 struct bme680_data {
54 	struct regmap *regmap;
55 	struct bme680_calib bme680;
56 	struct mutex lock; /* Protect multiple serial R/W ops to device. */
57 	u8 oversampling_temp;
58 	u8 oversampling_press;
59 	u8 oversampling_humid;
60 	u16 heater_dur;
61 	u16 heater_temp;
62 	/*
63 	 * Carryover value from temperature conversion, used in pressure
64 	 * and humidity compensation calculations.
65 	 */
66 	s32 t_fine;
67 };
68 
69 static const struct regmap_range bme680_volatile_ranges[] = {
70 	regmap_reg_range(BME680_REG_MEAS_STAT_0, BME680_REG_GAS_R_LSB),
71 	regmap_reg_range(BME680_REG_STATUS, BME680_REG_STATUS),
72 	regmap_reg_range(BME680_T2_LSB_REG, BME680_GH3_REG),
73 };
74 
75 static const struct regmap_access_table bme680_volatile_table = {
76 	.yes_ranges	= bme680_volatile_ranges,
77 	.n_yes_ranges	= ARRAY_SIZE(bme680_volatile_ranges),
78 };
79 
80 const struct regmap_config bme680_regmap_config = {
81 	.reg_bits = 8,
82 	.val_bits = 8,
83 	.max_register = 0xef,
84 	.volatile_table = &bme680_volatile_table,
85 	.cache_type = REGCACHE_RBTREE,
86 };
87 EXPORT_SYMBOL_NS(bme680_regmap_config, IIO_BME680);
88 
89 static const struct iio_chan_spec bme680_channels[] = {
90 	{
91 		.type = IIO_TEMP,
92 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
93 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
94 	},
95 	{
96 		.type = IIO_PRESSURE,
97 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
98 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
99 	},
100 	{
101 		.type = IIO_HUMIDITYRELATIVE,
102 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
103 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
104 	},
105 	{
106 		.type = IIO_RESISTANCE,
107 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
108 	},
109 };
110 
111 static int bme680_read_calib(struct bme680_data *data,
112 			     struct bme680_calib *calib)
113 {
114 	struct device *dev = regmap_get_device(data->regmap);
115 	unsigned int tmp, tmp_msb, tmp_lsb;
116 	int ret;
117 	__le16 buf;
118 
119 	/* Temperature related coefficients */
120 	ret = regmap_bulk_read(data->regmap, BME680_T1_LSB_REG,
121 			       &buf, sizeof(buf));
122 	if (ret < 0) {
123 		dev_err(dev, "failed to read BME680_T1_LSB_REG\n");
124 		return ret;
125 	}
126 	calib->par_t1 = le16_to_cpu(buf);
127 
128 	ret = regmap_bulk_read(data->regmap, BME680_T2_LSB_REG,
129 			       &buf, sizeof(buf));
130 	if (ret < 0) {
131 		dev_err(dev, "failed to read BME680_T2_LSB_REG\n");
132 		return ret;
133 	}
134 	calib->par_t2 = le16_to_cpu(buf);
135 
136 	ret = regmap_read(data->regmap, BME680_T3_REG, &tmp);
137 	if (ret < 0) {
138 		dev_err(dev, "failed to read BME680_T3_REG\n");
139 		return ret;
140 	}
141 	calib->par_t3 = tmp;
142 
143 	/* Pressure related coefficients */
144 	ret = regmap_bulk_read(data->regmap, BME680_P1_LSB_REG,
145 			       &buf, sizeof(buf));
146 	if (ret < 0) {
147 		dev_err(dev, "failed to read BME680_P1_LSB_REG\n");
148 		return ret;
149 	}
150 	calib->par_p1 = le16_to_cpu(buf);
151 
152 	ret = regmap_bulk_read(data->regmap, BME680_P2_LSB_REG,
153 			       &buf, sizeof(buf));
154 	if (ret < 0) {
155 		dev_err(dev, "failed to read BME680_P2_LSB_REG\n");
156 		return ret;
157 	}
158 	calib->par_p2 = le16_to_cpu(buf);
159 
160 	ret = regmap_read(data->regmap, BME680_P3_REG, &tmp);
161 	if (ret < 0) {
162 		dev_err(dev, "failed to read BME680_P3_REG\n");
163 		return ret;
164 	}
165 	calib->par_p3 = tmp;
166 
167 	ret = regmap_bulk_read(data->regmap, BME680_P4_LSB_REG,
168 			       &buf, sizeof(buf));
169 	if (ret < 0) {
170 		dev_err(dev, "failed to read BME680_P4_LSB_REG\n");
171 		return ret;
172 	}
173 	calib->par_p4 = le16_to_cpu(buf);
174 
175 	ret = regmap_bulk_read(data->regmap, BME680_P5_LSB_REG,
176 			       &buf, sizeof(buf));
177 	if (ret < 0) {
178 		dev_err(dev, "failed to read BME680_P5_LSB_REG\n");
179 		return ret;
180 	}
181 	calib->par_p5 = le16_to_cpu(buf);
182 
183 	ret = regmap_read(data->regmap, BME680_P6_REG, &tmp);
184 	if (ret < 0) {
185 		dev_err(dev, "failed to read BME680_P6_REG\n");
186 		return ret;
187 	}
188 	calib->par_p6 = tmp;
189 
190 	ret = regmap_read(data->regmap, BME680_P7_REG, &tmp);
191 	if (ret < 0) {
192 		dev_err(dev, "failed to read BME680_P7_REG\n");
193 		return ret;
194 	}
195 	calib->par_p7 = tmp;
196 
197 	ret = regmap_bulk_read(data->regmap, BME680_P8_LSB_REG,
198 			       &buf, sizeof(buf));
199 	if (ret < 0) {
200 		dev_err(dev, "failed to read BME680_P8_LSB_REG\n");
201 		return ret;
202 	}
203 	calib->par_p8 = le16_to_cpu(buf);
204 
205 	ret = regmap_bulk_read(data->regmap, BME680_P9_LSB_REG,
206 			       &buf, sizeof(buf));
207 	if (ret < 0) {
208 		dev_err(dev, "failed to read BME680_P9_LSB_REG\n");
209 		return ret;
210 	}
211 	calib->par_p9 = le16_to_cpu(buf);
212 
213 	ret = regmap_read(data->regmap, BME680_P10_REG, &tmp);
214 	if (ret < 0) {
215 		dev_err(dev, "failed to read BME680_P10_REG\n");
216 		return ret;
217 	}
218 	calib->par_p10 = tmp;
219 
220 	/* Humidity related coefficients */
221 	ret = regmap_read(data->regmap, BME680_H1_MSB_REG, &tmp_msb);
222 	if (ret < 0) {
223 		dev_err(dev, "failed to read BME680_H1_MSB_REG\n");
224 		return ret;
225 	}
226 	ret = regmap_read(data->regmap, BME680_H1_LSB_REG, &tmp_lsb);
227 	if (ret < 0) {
228 		dev_err(dev, "failed to read BME680_H1_LSB_REG\n");
229 		return ret;
230 	}
231 	calib->par_h1 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) |
232 			(tmp_lsb & BME680_BIT_H1_DATA_MASK);
233 
234 	ret = regmap_read(data->regmap, BME680_H2_MSB_REG, &tmp_msb);
235 	if (ret < 0) {
236 		dev_err(dev, "failed to read BME680_H2_MSB_REG\n");
237 		return ret;
238 	}
239 	ret = regmap_read(data->regmap, BME680_H2_LSB_REG, &tmp_lsb);
240 	if (ret < 0) {
241 		dev_err(dev, "failed to read BME680_H2_LSB_REG\n");
242 		return ret;
243 	}
244 	calib->par_h2 = (tmp_msb << BME680_HUM_REG_SHIFT_VAL) |
245 			(tmp_lsb >> BME680_HUM_REG_SHIFT_VAL);
246 
247 	ret = regmap_read(data->regmap, BME680_H3_REG, &tmp);
248 	if (ret < 0) {
249 		dev_err(dev, "failed to read BME680_H3_REG\n");
250 		return ret;
251 	}
252 	calib->par_h3 = tmp;
253 
254 	ret = regmap_read(data->regmap, BME680_H4_REG, &tmp);
255 	if (ret < 0) {
256 		dev_err(dev, "failed to read BME680_H4_REG\n");
257 		return ret;
258 	}
259 	calib->par_h4 = tmp;
260 
261 	ret = regmap_read(data->regmap, BME680_H5_REG, &tmp);
262 	if (ret < 0) {
263 		dev_err(dev, "failed to read BME680_H5_REG\n");
264 		return ret;
265 	}
266 	calib->par_h5 = tmp;
267 
268 	ret = regmap_read(data->regmap, BME680_H6_REG, &tmp);
269 	if (ret < 0) {
270 		dev_err(dev, "failed to read BME680_H6_REG\n");
271 		return ret;
272 	}
273 	calib->par_h6 = tmp;
274 
275 	ret = regmap_read(data->regmap, BME680_H7_REG, &tmp);
276 	if (ret < 0) {
277 		dev_err(dev, "failed to read BME680_H7_REG\n");
278 		return ret;
279 	}
280 	calib->par_h7 = tmp;
281 
282 	/* Gas heater related coefficients */
283 	ret = regmap_read(data->regmap, BME680_GH1_REG, &tmp);
284 	if (ret < 0) {
285 		dev_err(dev, "failed to read BME680_GH1_REG\n");
286 		return ret;
287 	}
288 	calib->par_gh1 = tmp;
289 
290 	ret = regmap_bulk_read(data->regmap, BME680_GH2_LSB_REG,
291 			       &buf, sizeof(buf));
292 	if (ret < 0) {
293 		dev_err(dev, "failed to read BME680_GH2_LSB_REG\n");
294 		return ret;
295 	}
296 	calib->par_gh2 = le16_to_cpu(buf);
297 
298 	ret = regmap_read(data->regmap, BME680_GH3_REG, &tmp);
299 	if (ret < 0) {
300 		dev_err(dev, "failed to read BME680_GH3_REG\n");
301 		return ret;
302 	}
303 	calib->par_gh3 = tmp;
304 
305 	/* Other coefficients */
306 	ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_RANGE, &tmp);
307 	if (ret < 0) {
308 		dev_err(dev, "failed to read resistance heat range\n");
309 		return ret;
310 	}
311 	calib->res_heat_range = FIELD_GET(BME680_RHRANGE_MASK, tmp);
312 
313 	ret = regmap_read(data->regmap, BME680_REG_RES_HEAT_VAL, &tmp);
314 	if (ret < 0) {
315 		dev_err(dev, "failed to read resistance heat value\n");
316 		return ret;
317 	}
318 	calib->res_heat_val = tmp;
319 
320 	ret = regmap_read(data->regmap, BME680_REG_RANGE_SW_ERR, &tmp);
321 	if (ret < 0) {
322 		dev_err(dev, "failed to read range software error\n");
323 		return ret;
324 	}
325 	calib->range_sw_err = FIELD_GET(BME680_RSERROR_MASK, tmp);
326 
327 	return 0;
328 }
329 
330 /*
331  * Taken from Bosch BME680 API:
332  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L876
333  *
334  * Returns temperature measurement in DegC, resolutions is 0.01 DegC. Therefore,
335  * output value of "3233" represents 32.33 DegC.
336  */
337 static s16 bme680_compensate_temp(struct bme680_data *data,
338 				  s32 adc_temp)
339 {
340 	struct bme680_calib *calib = &data->bme680;
341 	s64 var1, var2, var3;
342 	s16 calc_temp;
343 
344 	/* If the calibration is invalid, attempt to reload it */
345 	if (!calib->par_t2)
346 		bme680_read_calib(data, calib);
347 
348 	var1 = (adc_temp >> 3) - ((s32)calib->par_t1 << 1);
349 	var2 = (var1 * calib->par_t2) >> 11;
350 	var3 = ((var1 >> 1) * (var1 >> 1)) >> 12;
351 	var3 = (var3 * ((s32)calib->par_t3 << 4)) >> 14;
352 	data->t_fine = var2 + var3;
353 	calc_temp = (data->t_fine * 5 + 128) >> 8;
354 
355 	return calc_temp;
356 }
357 
358 /*
359  * Taken from Bosch BME680 API:
360  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L896
361  *
362  * Returns pressure measurement in Pa. Output value of "97356" represents
363  * 97356 Pa = 973.56 hPa.
364  */
365 static u32 bme680_compensate_press(struct bme680_data *data,
366 				   u32 adc_press)
367 {
368 	struct bme680_calib *calib = &data->bme680;
369 	s32 var1, var2, var3, press_comp;
370 
371 	var1 = (data->t_fine >> 1) - 64000;
372 	var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * calib->par_p6) >> 2;
373 	var2 = var2 + (var1 * calib->par_p5 << 1);
374 	var2 = (var2 >> 2) + ((s32)calib->par_p4 << 16);
375 	var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) *
376 			((s32)calib->par_p3 << 5)) >> 3) +
377 			((calib->par_p2 * var1) >> 1);
378 	var1 = var1 >> 18;
379 	var1 = ((32768 + var1) * calib->par_p1) >> 15;
380 	press_comp = 1048576 - adc_press;
381 	press_comp = ((press_comp - (var2 >> 12)) * 3125);
382 
383 	if (press_comp >= BME680_MAX_OVERFLOW_VAL)
384 		press_comp = ((press_comp / (u32)var1) << 1);
385 	else
386 		press_comp = ((press_comp << 1) / (u32)var1);
387 
388 	var1 = (calib->par_p9 * (((press_comp >> 3) *
389 			(press_comp >> 3)) >> 13)) >> 12;
390 	var2 = ((press_comp >> 2) * calib->par_p8) >> 13;
391 	var3 = ((press_comp >> 8) * (press_comp >> 8) *
392 			(press_comp >> 8) * calib->par_p10) >> 17;
393 
394 	press_comp += (var1 + var2 + var3 + ((s32)calib->par_p7 << 7)) >> 4;
395 
396 	return press_comp;
397 }
398 
399 /*
400  * Taken from Bosch BME680 API:
401  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L937
402  *
403  * Returns humidity measurement in percent, resolution is 0.001 percent. Output
404  * value of "43215" represents 43.215 %rH.
405  */
406 static u32 bme680_compensate_humid(struct bme680_data *data,
407 				   u16 adc_humid)
408 {
409 	struct bme680_calib *calib = &data->bme680;
410 	s32 var1, var2, var3, var4, var5, var6, temp_scaled, calc_hum;
411 
412 	temp_scaled = (data->t_fine * 5 + 128) >> 8;
413 	var1 = (adc_humid - ((s32) ((s32) calib->par_h1 * 16))) -
414 		(((temp_scaled * (s32) calib->par_h3) / 100) >> 1);
415 	var2 = ((s32) calib->par_h2 *
416 		(((temp_scaled * calib->par_h4) / 100) +
417 		 (((temp_scaled * ((temp_scaled * calib->par_h5) / 100))
418 		   >> 6) / 100) + (1 << 14))) >> 10;
419 	var3 = var1 * var2;
420 	var4 = (s32)calib->par_h6 << 7;
421 	var4 = (var4 + ((temp_scaled * calib->par_h7) / 100)) >> 4;
422 	var5 = ((var3 >> 14) * (var3 >> 14)) >> 10;
423 	var6 = (var4 * var5) >> 1;
424 	calc_hum = (((var3 + var6) >> 10) * 1000) >> 12;
425 
426 	calc_hum = clamp(calc_hum, 0, 100000); /* clamp between 0-100 %rH */
427 
428 	return calc_hum;
429 }
430 
431 /*
432  * Taken from Bosch BME680 API:
433  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L973
434  *
435  * Returns gas measurement in Ohm. Output value of "82986" represent 82986 ohms.
436  */
437 static u32 bme680_compensate_gas(struct bme680_data *data, u16 gas_res_adc,
438 				 u8 gas_range)
439 {
440 	struct bme680_calib *calib = &data->bme680;
441 	s64 var1;
442 	u64 var2;
443 	s64 var3;
444 	u32 calc_gas_res;
445 
446 	/* Look up table for the possible gas range values */
447 	const u32 lookupTable[16] = {2147483647u, 2147483647u,
448 				2147483647u, 2147483647u, 2147483647u,
449 				2126008810u, 2147483647u, 2130303777u,
450 				2147483647u, 2147483647u, 2143188679u,
451 				2136746228u, 2147483647u, 2126008810u,
452 				2147483647u, 2147483647u};
453 
454 	var1 = ((1340 + (5 * (s64) calib->range_sw_err)) *
455 			((s64) lookupTable[gas_range])) >> 16;
456 	var2 = ((gas_res_adc << 15) - 16777216) + var1;
457 	var3 = ((125000 << (15 - gas_range)) * var1) >> 9;
458 	var3 += (var2 >> 1);
459 	calc_gas_res = div64_s64(var3, (s64) var2);
460 
461 	return calc_gas_res;
462 }
463 
464 /*
465  * Taken from Bosch BME680 API:
466  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1002
467  */
468 static u8 bme680_calc_heater_res(struct bme680_data *data, u16 temp)
469 {
470 	struct bme680_calib *calib = &data->bme680;
471 	s32 var1, var2, var3, var4, var5, heatr_res_x100;
472 	u8 heatr_res;
473 
474 	if (temp > 400) /* Cap temperature */
475 		temp = 400;
476 
477 	var1 = (((s32) BME680_AMB_TEMP * calib->par_gh3) / 1000) * 256;
478 	var2 = (calib->par_gh1 + 784) * (((((calib->par_gh2 + 154009) *
479 						temp * 5) / 100)
480 						+ 3276800) / 10);
481 	var3 = var1 + (var2 / 2);
482 	var4 = (var3 / (calib->res_heat_range + 4));
483 	var5 = 131 * calib->res_heat_val + 65536;
484 	heatr_res_x100 = ((var4 / var5) - 250) * 34;
485 	heatr_res = DIV_ROUND_CLOSEST(heatr_res_x100, 100);
486 
487 	return heatr_res;
488 }
489 
490 /*
491  * Taken from Bosch BME680 API:
492  * https://github.com/BoschSensortec/BME680_driver/blob/63bb5336/bme680.c#L1188
493  */
494 static u8 bme680_calc_heater_dur(u16 dur)
495 {
496 	u8 durval, factor = 0;
497 
498 	if (dur >= 0xfc0) {
499 		durval = 0xff; /* Max duration */
500 	} else {
501 		while (dur > 0x3F) {
502 			dur = dur / 4;
503 			factor += 1;
504 		}
505 		durval = dur + (factor * 64);
506 	}
507 
508 	return durval;
509 }
510 
511 static int bme680_set_mode(struct bme680_data *data, bool mode)
512 {
513 	struct device *dev = regmap_get_device(data->regmap);
514 	int ret;
515 
516 	if (mode) {
517 		ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
518 					BME680_MODE_MASK, BME680_MODE_FORCED);
519 		if (ret < 0)
520 			dev_err(dev, "failed to set forced mode\n");
521 
522 	} else {
523 		ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
524 					BME680_MODE_MASK, BME680_MODE_SLEEP);
525 		if (ret < 0)
526 			dev_err(dev, "failed to set sleep mode\n");
527 
528 	}
529 
530 	return ret;
531 }
532 
533 static u8 bme680_oversampling_to_reg(u8 val)
534 {
535 	return ilog2(val) + 1;
536 }
537 
538 /*
539  * Taken from Bosch BME680 API:
540  * https://github.com/boschsensortec/BME68x_SensorAPI/blob/v4.4.8/bme68x.c#L490
541  */
542 static int bme680_wait_for_eoc(struct bme680_data *data)
543 {
544 	struct device *dev = regmap_get_device(data->regmap);
545 	unsigned int check;
546 	int ret;
547 	/*
548 	 * (Sum of oversampling ratios * time per oversampling) +
549 	 * TPH measurement + gas measurement + wait transition from forced mode
550 	 * + heater duration
551 	 */
552 	int wait_eoc_us = ((data->oversampling_temp + data->oversampling_press +
553 			   data->oversampling_humid) * 1936) + (477 * 4) +
554 			   (477 * 5) + 1000 + (data->heater_dur * 1000);
555 
556 	usleep_range(wait_eoc_us, wait_eoc_us + 100);
557 
558 	ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
559 	if (ret) {
560 		dev_err(dev, "failed to read measurement status register.\n");
561 		return ret;
562 	}
563 	if (check & BME680_MEAS_BIT) {
564 		dev_err(dev, "Device measurement cycle incomplete.\n");
565 		return -EBUSY;
566 	}
567 	if (!(check & BME680_NEW_DATA_BIT)) {
568 		dev_err(dev, "No new data available from the device.\n");
569 		return -ENODATA;
570 	}
571 
572 	return 0;
573 }
574 
575 static int bme680_chip_config(struct bme680_data *data)
576 {
577 	struct device *dev = regmap_get_device(data->regmap);
578 	int ret;
579 	u8 osrs;
580 
581 	osrs = FIELD_PREP(
582 		BME680_OSRS_HUMIDITY_MASK,
583 		bme680_oversampling_to_reg(data->oversampling_humid));
584 	/*
585 	 * Highly recommended to set oversampling of humidity before
586 	 * temperature/pressure oversampling.
587 	 */
588 	ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_HUMIDITY,
589 				 BME680_OSRS_HUMIDITY_MASK, osrs);
590 	if (ret < 0) {
591 		dev_err(dev, "failed to write ctrl_hum register\n");
592 		return ret;
593 	}
594 
595 	/* IIR filter settings */
596 	ret = regmap_update_bits(data->regmap, BME680_REG_CONFIG,
597 				 BME680_FILTER_MASK,
598 				 BME680_FILTER_COEFF_VAL);
599 	if (ret < 0) {
600 		dev_err(dev, "failed to write config register\n");
601 		return ret;
602 	}
603 
604 	osrs = FIELD_PREP(BME680_OSRS_TEMP_MASK,
605 			  bme680_oversampling_to_reg(data->oversampling_temp)) |
606 	       FIELD_PREP(BME680_OSRS_PRESS_MASK,
607 			  bme680_oversampling_to_reg(data->oversampling_press));
608 	ret = regmap_write_bits(data->regmap, BME680_REG_CTRL_MEAS,
609 				BME680_OSRS_TEMP_MASK | BME680_OSRS_PRESS_MASK,
610 				osrs);
611 	if (ret < 0)
612 		dev_err(dev, "failed to write ctrl_meas register\n");
613 
614 	return ret;
615 }
616 
617 static int bme680_gas_config(struct bme680_data *data)
618 {
619 	struct device *dev = regmap_get_device(data->regmap);
620 	int ret;
621 	u8 heatr_res, heatr_dur;
622 
623 	heatr_res = bme680_calc_heater_res(data, data->heater_temp);
624 
625 	/* set target heater temperature */
626 	ret = regmap_write(data->regmap, BME680_REG_RES_HEAT_0, heatr_res);
627 	if (ret < 0) {
628 		dev_err(dev, "failed to write res_heat_0 register\n");
629 		return ret;
630 	}
631 
632 	heatr_dur = bme680_calc_heater_dur(data->heater_dur);
633 
634 	/* set target heating duration */
635 	ret = regmap_write(data->regmap, BME680_REG_GAS_WAIT_0, heatr_dur);
636 	if (ret < 0) {
637 		dev_err(dev, "failed to write gas_wait_0 register\n");
638 		return ret;
639 	}
640 
641 	/* Enable the gas sensor and select heater profile set-point 0 */
642 	ret = regmap_update_bits(data->regmap, BME680_REG_CTRL_GAS_1,
643 				 BME680_RUN_GAS_MASK | BME680_NB_CONV_MASK,
644 				 FIELD_PREP(BME680_RUN_GAS_MASK, 1) |
645 				 FIELD_PREP(BME680_NB_CONV_MASK, 0));
646 	if (ret < 0)
647 		dev_err(dev, "failed to write ctrl_gas_1 register\n");
648 
649 	return ret;
650 }
651 
652 static int bme680_read_temp(struct bme680_data *data, int *val)
653 {
654 	struct device *dev = regmap_get_device(data->regmap);
655 	int ret;
656 	__be32 tmp = 0;
657 	s32 adc_temp;
658 	s16 comp_temp;
659 
660 	/* set forced mode to trigger measurement */
661 	ret = bme680_set_mode(data, true);
662 	if (ret < 0)
663 		return ret;
664 
665 	ret = bme680_wait_for_eoc(data);
666 	if (ret)
667 		return ret;
668 
669 	ret = regmap_bulk_read(data->regmap, BME680_REG_TEMP_MSB,
670 			       &tmp, 3);
671 	if (ret < 0) {
672 		dev_err(dev, "failed to read temperature\n");
673 		return ret;
674 	}
675 
676 	adc_temp = be32_to_cpu(tmp) >> 12;
677 	if (adc_temp == BME680_MEAS_SKIPPED) {
678 		/* reading was skipped */
679 		dev_err(dev, "reading temperature skipped\n");
680 		return -EINVAL;
681 	}
682 	comp_temp = bme680_compensate_temp(data, adc_temp);
683 	/*
684 	 * val might be NULL if we're called by the read_press/read_humid
685 	 * routine which is called to get t_fine value used in
686 	 * compensate_press/compensate_humid to get compensated
687 	 * pressure/humidity readings.
688 	 */
689 	if (val) {
690 		*val = comp_temp * 10; /* Centidegrees to millidegrees */
691 		return IIO_VAL_INT;
692 	}
693 
694 	return ret;
695 }
696 
697 static int bme680_read_press(struct bme680_data *data,
698 			     int *val, int *val2)
699 {
700 	struct device *dev = regmap_get_device(data->regmap);
701 	int ret;
702 	__be32 tmp = 0;
703 	s32 adc_press;
704 
705 	/* Read and compensate temperature to get a reading of t_fine */
706 	ret = bme680_read_temp(data, NULL);
707 	if (ret < 0)
708 		return ret;
709 
710 	ret = regmap_bulk_read(data->regmap, BME680_REG_PRESS_MSB,
711 			       &tmp, 3);
712 	if (ret < 0) {
713 		dev_err(dev, "failed to read pressure\n");
714 		return ret;
715 	}
716 
717 	adc_press = be32_to_cpu(tmp) >> 12;
718 	if (adc_press == BME680_MEAS_SKIPPED) {
719 		/* reading was skipped */
720 		dev_err(dev, "reading pressure skipped\n");
721 		return -EINVAL;
722 	}
723 
724 	*val = bme680_compensate_press(data, adc_press);
725 	*val2 = 1000;
726 	return IIO_VAL_FRACTIONAL;
727 }
728 
729 static int bme680_read_humid(struct bme680_data *data,
730 			     int *val, int *val2)
731 {
732 	struct device *dev = regmap_get_device(data->regmap);
733 	int ret;
734 	__be16 tmp = 0;
735 	s32 adc_humidity;
736 	u32 comp_humidity;
737 
738 	/* Read and compensate temperature to get a reading of t_fine */
739 	ret = bme680_read_temp(data, NULL);
740 	if (ret < 0)
741 		return ret;
742 
743 	ret = regmap_bulk_read(data->regmap, BM6880_REG_HUMIDITY_MSB,
744 			       &tmp, sizeof(tmp));
745 	if (ret < 0) {
746 		dev_err(dev, "failed to read humidity\n");
747 		return ret;
748 	}
749 
750 	adc_humidity = be16_to_cpu(tmp);
751 	if (adc_humidity == BME680_MEAS_SKIPPED) {
752 		/* reading was skipped */
753 		dev_err(dev, "reading humidity skipped\n");
754 		return -EINVAL;
755 	}
756 	comp_humidity = bme680_compensate_humid(data, adc_humidity);
757 
758 	*val = comp_humidity;
759 	*val2 = 1000;
760 	return IIO_VAL_FRACTIONAL;
761 }
762 
763 static int bme680_read_gas(struct bme680_data *data,
764 			   int *val)
765 {
766 	struct device *dev = regmap_get_device(data->regmap);
767 	int ret;
768 	__be16 tmp = 0;
769 	unsigned int check;
770 	u16 adc_gas_res;
771 	u8 gas_range;
772 
773 	/* Set heater settings */
774 	ret = bme680_gas_config(data);
775 	if (ret < 0) {
776 		dev_err(dev, "failed to set gas config\n");
777 		return ret;
778 	}
779 
780 	/* set forced mode to trigger measurement */
781 	ret = bme680_set_mode(data, true);
782 	if (ret < 0)
783 		return ret;
784 
785 	ret = bme680_wait_for_eoc(data);
786 	if (ret)
787 		return ret;
788 
789 	ret = regmap_read(data->regmap, BME680_REG_MEAS_STAT_0, &check);
790 	if (check & BME680_GAS_MEAS_BIT) {
791 		dev_err(dev, "gas measurement incomplete\n");
792 		return -EBUSY;
793 	}
794 
795 	ret = regmap_read(data->regmap, BME680_REG_GAS_R_LSB, &check);
796 	if (ret < 0) {
797 		dev_err(dev, "failed to read gas_r_lsb register\n");
798 		return ret;
799 	}
800 
801 	/*
802 	 * occurs if either the gas heating duration was insuffient
803 	 * to reach the target heater temperature or the target
804 	 * heater temperature was too high for the heater sink to
805 	 * reach.
806 	 */
807 	if ((check & BME680_GAS_STAB_BIT) == 0) {
808 		dev_err(dev, "heater failed to reach the target temperature\n");
809 		return -EINVAL;
810 	}
811 
812 	ret = regmap_bulk_read(data->regmap, BME680_REG_GAS_MSB,
813 			       &tmp, sizeof(tmp));
814 	if (ret < 0) {
815 		dev_err(dev, "failed to read gas resistance\n");
816 		return ret;
817 	}
818 
819 	gas_range = check & BME680_GAS_RANGE_MASK;
820 	adc_gas_res = be16_to_cpu(tmp) >> BME680_ADC_GAS_RES_SHIFT;
821 
822 	*val = bme680_compensate_gas(data, adc_gas_res, gas_range);
823 	return IIO_VAL_INT;
824 }
825 
826 static int bme680_read_raw(struct iio_dev *indio_dev,
827 			   struct iio_chan_spec const *chan,
828 			   int *val, int *val2, long mask)
829 {
830 	struct bme680_data *data = iio_priv(indio_dev);
831 
832 	guard(mutex)(&data->lock);
833 
834 	switch (mask) {
835 	case IIO_CHAN_INFO_PROCESSED:
836 		switch (chan->type) {
837 		case IIO_TEMP:
838 			return bme680_read_temp(data, val);
839 		case IIO_PRESSURE:
840 			return bme680_read_press(data, val, val2);
841 		case IIO_HUMIDITYRELATIVE:
842 			return bme680_read_humid(data, val, val2);
843 		case IIO_RESISTANCE:
844 			return bme680_read_gas(data, val);
845 		default:
846 			return -EINVAL;
847 		}
848 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
849 		switch (chan->type) {
850 		case IIO_TEMP:
851 			*val = data->oversampling_temp;
852 			return IIO_VAL_INT;
853 		case IIO_PRESSURE:
854 			*val = data->oversampling_press;
855 			return IIO_VAL_INT;
856 		case IIO_HUMIDITYRELATIVE:
857 			*val = data->oversampling_humid;
858 			return IIO_VAL_INT;
859 		default:
860 			return -EINVAL;
861 		}
862 	default:
863 		return -EINVAL;
864 	}
865 }
866 
867 static bool bme680_is_valid_oversampling(int rate)
868 {
869 	return (rate > 0 && rate <= 16 && is_power_of_2(rate));
870 }
871 
872 static int bme680_write_raw(struct iio_dev *indio_dev,
873 			    struct iio_chan_spec const *chan,
874 			    int val, int val2, long mask)
875 {
876 	struct bme680_data *data = iio_priv(indio_dev);
877 
878 	guard(mutex)(&data->lock);
879 
880 	if (val2 != 0)
881 		return -EINVAL;
882 
883 	switch (mask) {
884 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
885 	{
886 		if (!bme680_is_valid_oversampling(val))
887 			return -EINVAL;
888 
889 		switch (chan->type) {
890 		case IIO_TEMP:
891 			data->oversampling_temp = val;
892 			break;
893 		case IIO_PRESSURE:
894 			data->oversampling_press = val;
895 			break;
896 		case IIO_HUMIDITYRELATIVE:
897 			data->oversampling_humid = val;
898 			break;
899 		default:
900 			return -EINVAL;
901 		}
902 
903 		return bme680_chip_config(data);
904 	}
905 	default:
906 		return -EINVAL;
907 	}
908 }
909 
910 static const char bme680_oversampling_ratio_show[] = "1 2 4 8 16";
911 
912 static IIO_CONST_ATTR(oversampling_ratio_available,
913 		      bme680_oversampling_ratio_show);
914 
915 static struct attribute *bme680_attributes[] = {
916 	&iio_const_attr_oversampling_ratio_available.dev_attr.attr,
917 	NULL,
918 };
919 
920 static const struct attribute_group bme680_attribute_group = {
921 	.attrs = bme680_attributes,
922 };
923 
924 static const struct iio_info bme680_info = {
925 	.read_raw = &bme680_read_raw,
926 	.write_raw = &bme680_write_raw,
927 	.attrs = &bme680_attribute_group,
928 };
929 
930 static const char *bme680_match_acpi_device(struct device *dev)
931 {
932 	const struct acpi_device_id *id;
933 
934 	id = acpi_match_device(dev->driver->acpi_match_table, dev);
935 	if (!id)
936 		return NULL;
937 
938 	return dev_name(dev);
939 }
940 
941 int bme680_core_probe(struct device *dev, struct regmap *regmap,
942 		      const char *name)
943 {
944 	struct iio_dev *indio_dev;
945 	struct bme680_data *data;
946 	unsigned int val;
947 	int ret;
948 
949 	ret = regmap_write(regmap, BME680_REG_SOFT_RESET,
950 			   BME680_CMD_SOFTRESET);
951 	if (ret < 0) {
952 		dev_err(dev, "Failed to reset chip\n");
953 		return ret;
954 	}
955 
956 	ret = regmap_read(regmap, BME680_REG_CHIP_ID, &val);
957 	if (ret < 0) {
958 		dev_err(dev, "Error reading chip ID\n");
959 		return ret;
960 	}
961 
962 	if (val != BME680_CHIP_ID_VAL) {
963 		dev_err(dev, "Wrong chip ID, got %x expected %x\n",
964 				val, BME680_CHIP_ID_VAL);
965 		return -ENODEV;
966 	}
967 
968 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
969 	if (!indio_dev)
970 		return -ENOMEM;
971 
972 	if (!name && ACPI_HANDLE(dev))
973 		name = bme680_match_acpi_device(dev);
974 
975 	data = iio_priv(indio_dev);
976 	mutex_init(&data->lock);
977 	dev_set_drvdata(dev, indio_dev);
978 	data->regmap = regmap;
979 	indio_dev->name = name;
980 	indio_dev->channels = bme680_channels;
981 	indio_dev->num_channels = ARRAY_SIZE(bme680_channels);
982 	indio_dev->info = &bme680_info;
983 	indio_dev->modes = INDIO_DIRECT_MODE;
984 
985 	/* default values for the sensor */
986 	data->oversampling_humid = 2; /* 2X oversampling rate */
987 	data->oversampling_press = 4; /* 4X oversampling rate */
988 	data->oversampling_temp = 8;  /* 8X oversampling rate */
989 	data->heater_temp = 320; /* degree Celsius */
990 	data->heater_dur = 150;  /* milliseconds */
991 
992 	ret = bme680_chip_config(data);
993 	if (ret < 0) {
994 		dev_err(dev, "failed to set chip_config data\n");
995 		return ret;
996 	}
997 
998 	ret = bme680_gas_config(data);
999 	if (ret < 0) {
1000 		dev_err(dev, "failed to set gas config data\n");
1001 		return ret;
1002 	}
1003 
1004 	ret = bme680_read_calib(data, &data->bme680);
1005 	if (ret < 0) {
1006 		dev_err(dev,
1007 			"failed to read calibration coefficients at probe\n");
1008 		return ret;
1009 	}
1010 
1011 	return devm_iio_device_register(dev, indio_dev);
1012 }
1013 EXPORT_SYMBOL_NS_GPL(bme680_core_probe, IIO_BME680);
1014 
1015 MODULE_AUTHOR("Himanshu Jha <himanshujha199640@gmail.com>");
1016 MODULE_DESCRIPTION("Bosch BME680 Driver");
1017 MODULE_LICENSE("GPL v2");
1018