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