xref: /linux/drivers/iio/pressure/bmp280-core.c (revision 8732594017d32132ca741f4ec4eec91211f5d10c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
4  * Copyright (c) 2012 Bosch Sensortec GmbH
5  * Copyright (c) 2012 Unixphere AB
6  * Copyright (c) 2014 Intel Corporation
7  * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
8  *
9  * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
10  *
11  * Datasheet:
12  * https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf
13  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp280-ds001.pdf
14  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bme280-ds002.pdf
15  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp388-ds001.pdf
16  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf
17  * https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp581-ds004.pdf
18  *
19  * Notice:
20  * The link to the bmp180 datasheet points to an outdated version missing these changes:
21  * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
22  * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
23  * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
24  */
25 
26 #define pr_fmt(fmt) "bmp280: " fmt
27 
28 #include <linux/bitops.h>
29 #include <linux/bitfield.h>
30 #include <linux/completion.h>
31 #include <linux/delay.h>
32 #include <linux/device.h>
33 #include <linux/gpio/consumer.h>
34 #include <linux/interrupt.h>
35 #include <linux/irq.h> /* For irq_get_irq_data() */
36 #include <linux/module.h>
37 #include <linux/nvmem-provider.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/random.h>
40 #include <linux/regmap.h>
41 #include <linux/regulator/consumer.h>
42 
43 #include <linux/iio/iio.h>
44 
45 #include <asm/unaligned.h>
46 
47 #include "bmp280.h"
48 
49 /*
50  * These enums are used for indexing into the array of calibration
51  * coefficients for BMP180.
52  */
53 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
54 
55 
56 enum bmp380_odr {
57 	BMP380_ODR_200HZ,
58 	BMP380_ODR_100HZ,
59 	BMP380_ODR_50HZ,
60 	BMP380_ODR_25HZ,
61 	BMP380_ODR_12_5HZ,
62 	BMP380_ODR_6_25HZ,
63 	BMP380_ODR_3_125HZ,
64 	BMP380_ODR_1_5625HZ,
65 	BMP380_ODR_0_78HZ,
66 	BMP380_ODR_0_39HZ,
67 	BMP380_ODR_0_2HZ,
68 	BMP380_ODR_0_1HZ,
69 	BMP380_ODR_0_05HZ,
70 	BMP380_ODR_0_02HZ,
71 	BMP380_ODR_0_01HZ,
72 	BMP380_ODR_0_006HZ,
73 	BMP380_ODR_0_003HZ,
74 	BMP380_ODR_0_0015HZ,
75 };
76 
77 enum bmp580_odr {
78 	BMP580_ODR_240HZ,
79 	BMP580_ODR_218HZ,
80 	BMP580_ODR_199HZ,
81 	BMP580_ODR_179HZ,
82 	BMP580_ODR_160HZ,
83 	BMP580_ODR_149HZ,
84 	BMP580_ODR_140HZ,
85 	BMP580_ODR_129HZ,
86 	BMP580_ODR_120HZ,
87 	BMP580_ODR_110HZ,
88 	BMP580_ODR_100HZ,
89 	BMP580_ODR_89HZ,
90 	BMP580_ODR_80HZ,
91 	BMP580_ODR_70HZ,
92 	BMP580_ODR_60HZ,
93 	BMP580_ODR_50HZ,
94 	BMP580_ODR_45HZ,
95 	BMP580_ODR_40HZ,
96 	BMP580_ODR_35HZ,
97 	BMP580_ODR_30HZ,
98 	BMP580_ODR_25HZ,
99 	BMP580_ODR_20HZ,
100 	BMP580_ODR_15HZ,
101 	BMP580_ODR_10HZ,
102 	BMP580_ODR_5HZ,
103 	BMP580_ODR_4HZ,
104 	BMP580_ODR_3HZ,
105 	BMP580_ODR_2HZ,
106 	BMP580_ODR_1HZ,
107 	BMP580_ODR_0_5HZ,
108 	BMP580_ODR_0_25HZ,
109 	BMP580_ODR_0_125HZ,
110 };
111 
112 /*
113  * These enums are used for indexing into the array of compensation
114  * parameters for BMP280.
115  */
116 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
117 
118 enum {
119 	/* Temperature calib indexes */
120 	BMP380_T1 = 0,
121 	BMP380_T2 = 2,
122 	BMP380_T3 = 4,
123 	/* Pressure calib indexes */
124 	BMP380_P1 = 5,
125 	BMP380_P2 = 7,
126 	BMP380_P3 = 9,
127 	BMP380_P4 = 10,
128 	BMP380_P5 = 11,
129 	BMP380_P6 = 13,
130 	BMP380_P7 = 15,
131 	BMP380_P8 = 16,
132 	BMP380_P9 = 17,
133 	BMP380_P10 = 19,
134 	BMP380_P11 = 20,
135 };
136 
137 static const struct iio_chan_spec bmp280_channels[] = {
138 	{
139 		.type = IIO_PRESSURE,
140 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
141 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
142 	},
143 	{
144 		.type = IIO_TEMP,
145 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
146 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
147 	},
148 	{
149 		.type = IIO_HUMIDITYRELATIVE,
150 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
151 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
152 	},
153 };
154 
155 static const struct iio_chan_spec bmp380_channels[] = {
156 	{
157 		.type = IIO_PRESSURE,
158 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
159 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
160 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
161 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
162 	},
163 	{
164 		.type = IIO_TEMP,
165 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
166 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
167 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
168 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
169 	},
170 	{
171 		.type = IIO_HUMIDITYRELATIVE,
172 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
173 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
174 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
175 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
176 	},
177 };
178 
179 static int bmp280_read_calib(struct bmp280_data *data)
180 {
181 	struct bmp280_calib *calib = &data->calib.bmp280;
182 	int ret;
183 
184 
185 	/* Read temperature and pressure calibration values. */
186 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
187 			       data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
188 	if (ret < 0) {
189 		dev_err(data->dev,
190 			"failed to read temperature and pressure calibration parameters\n");
191 		return ret;
192 	}
193 
194 	/* Toss the temperature and pressure calibration data into the entropy pool */
195 	add_device_randomness(data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
196 
197 	/* Parse temperature calibration values. */
198 	calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
199 	calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
200 	calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
201 
202 	/* Parse pressure calibration values. */
203 	calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
204 	calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
205 	calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
206 	calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
207 	calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
208 	calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
209 	calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
210 	calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
211 	calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
212 
213 	return 0;
214 }
215 
216 static int bme280_read_calib(struct bmp280_data *data)
217 {
218 	struct bmp280_calib *calib = &data->calib.bmp280;
219 	struct device *dev = data->dev;
220 	unsigned int tmp;
221 	int ret;
222 
223 	/* Load shared calibration params with bmp280 first */
224 	ret = bmp280_read_calib(data);
225 	if  (ret < 0) {
226 		dev_err(dev, "failed to read common bmp280 calibration parameters\n");
227 		return ret;
228 	}
229 
230 	/*
231 	 * Read humidity calibration values.
232 	 * Due to some odd register addressing we cannot just
233 	 * do a big bulk read. Instead, we have to read each Hx
234 	 * value separately and sometimes do some bit shifting...
235 	 * Humidity data is only available on BME280.
236 	 */
237 
238 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
239 	if (ret < 0) {
240 		dev_err(dev, "failed to read H1 comp value\n");
241 		return ret;
242 	}
243 	calib->H1 = tmp;
244 
245 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2,
246 			       &data->le16, sizeof(data->le16));
247 	if (ret < 0) {
248 		dev_err(dev, "failed to read H2 comp value\n");
249 		return ret;
250 	}
251 	calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
252 
253 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
254 	if (ret < 0) {
255 		dev_err(dev, "failed to read H3 comp value\n");
256 		return ret;
257 	}
258 	calib->H3 = tmp;
259 
260 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4,
261 			       &data->be16, sizeof(data->be16));
262 	if (ret < 0) {
263 		dev_err(dev, "failed to read H4 comp value\n");
264 		return ret;
265 	}
266 	calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
267 				  (be16_to_cpu(data->be16) & 0xf), 11);
268 
269 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5,
270 			       &data->le16, sizeof(data->le16));
271 	if (ret < 0) {
272 		dev_err(dev, "failed to read H5 comp value\n");
273 		return ret;
274 	}
275 	calib->H5 = sign_extend32(FIELD_GET(BMP280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
276 
277 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
278 	if (ret < 0) {
279 		dev_err(dev, "failed to read H6 comp value\n");
280 		return ret;
281 	}
282 	calib->H6 = sign_extend32(tmp, 7);
283 
284 	return 0;
285 }
286 /*
287  * Returns humidity in percent, resolution is 0.01 percent. Output value of
288  * "47445" represents 47445/1024 = 46.333 %RH.
289  *
290  * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
291  */
292 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
293 				      s32 adc_humidity)
294 {
295 	struct bmp280_calib *calib = &data->calib.bmp280;
296 	s32 var;
297 
298 	var = ((s32)data->t_fine) - (s32)76800;
299 	var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
300 		+ (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
301 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
302 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
303 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
304 
305 	var = clamp_val(var, 0, 419430400);
306 
307 	return var >> 12;
308 };
309 
310 /*
311  * Returns temperature in DegC, resolution is 0.01 DegC.  Output value of
312  * "5123" equals 51.23 DegC.  t_fine carries fine temperature as global
313  * value.
314  *
315  * Taken from datasheet, Section 3.11.3, "Compensation formula".
316  */
317 static s32 bmp280_compensate_temp(struct bmp280_data *data,
318 				  s32 adc_temp)
319 {
320 	struct bmp280_calib *calib = &data->calib.bmp280;
321 	s32 var1, var2;
322 
323 	var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
324 		((s32)calib->T2)) >> 11;
325 	var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
326 		  ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
327 		((s32)calib->T3)) >> 14;
328 	data->t_fine = var1 + var2;
329 
330 	return (data->t_fine * 5 + 128) >> 8;
331 }
332 
333 /*
334  * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
335  * integer bits and 8 fractional bits).  Output value of "24674867"
336  * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
337  *
338  * Taken from datasheet, Section 3.11.3, "Compensation formula".
339  */
340 static u32 bmp280_compensate_press(struct bmp280_data *data,
341 				   s32 adc_press)
342 {
343 	struct bmp280_calib *calib = &data->calib.bmp280;
344 	s64 var1, var2, p;
345 
346 	var1 = ((s64)data->t_fine) - 128000;
347 	var2 = var1 * var1 * (s64)calib->P6;
348 	var2 += (var1 * (s64)calib->P5) << 17;
349 	var2 += ((s64)calib->P4) << 35;
350 	var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
351 		((var1 * (s64)calib->P2) << 12);
352 	var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
353 
354 	if (var1 == 0)
355 		return 0;
356 
357 	p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
358 	p = div64_s64(p, var1);
359 	var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
360 	var2 = ((s64)(calib->P8) * p) >> 19;
361 	p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
362 
363 	return (u32)p;
364 }
365 
366 static int bmp280_read_temp(struct bmp280_data *data,
367 			    int *val, int *val2)
368 {
369 	s32 adc_temp, comp_temp;
370 	int ret;
371 
372 	ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
373 			       data->buf, sizeof(data->buf));
374 	if (ret < 0) {
375 		dev_err(data->dev, "failed to read temperature\n");
376 		return ret;
377 	}
378 
379 	adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
380 	if (adc_temp == BMP280_TEMP_SKIPPED) {
381 		/* reading was skipped */
382 		dev_err(data->dev, "reading temperature skipped\n");
383 		return -EIO;
384 	}
385 	comp_temp = bmp280_compensate_temp(data, adc_temp);
386 
387 	/*
388 	 * val might be NULL if we're called by the read_press routine,
389 	 * who only cares about the carry over t_fine value.
390 	 */
391 	if (val) {
392 		*val = comp_temp * 10;
393 		return IIO_VAL_INT;
394 	}
395 
396 	return 0;
397 }
398 
399 static int bmp280_read_press(struct bmp280_data *data,
400 			     int *val, int *val2)
401 {
402 	u32 comp_press;
403 	s32 adc_press;
404 	int ret;
405 
406 	/* Read and compensate temperature so we get a reading of t_fine. */
407 	ret = bmp280_read_temp(data, NULL, NULL);
408 	if (ret < 0)
409 		return ret;
410 
411 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
412 			       data->buf, sizeof(data->buf));
413 	if (ret < 0) {
414 		dev_err(data->dev, "failed to read pressure\n");
415 		return ret;
416 	}
417 
418 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
419 	if (adc_press == BMP280_PRESS_SKIPPED) {
420 		/* reading was skipped */
421 		dev_err(data->dev, "reading pressure skipped\n");
422 		return -EIO;
423 	}
424 	comp_press = bmp280_compensate_press(data, adc_press);
425 
426 	*val = comp_press;
427 	*val2 = 256000;
428 
429 	return IIO_VAL_FRACTIONAL;
430 }
431 
432 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
433 {
434 	u32 comp_humidity;
435 	s32 adc_humidity;
436 	int ret;
437 
438 	/* Read and compensate temperature so we get a reading of t_fine. */
439 	ret = bmp280_read_temp(data, NULL, NULL);
440 	if (ret < 0)
441 		return ret;
442 
443 	ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
444 			       &data->be16, sizeof(data->be16));
445 	if (ret < 0) {
446 		dev_err(data->dev, "failed to read humidity\n");
447 		return ret;
448 	}
449 
450 	adc_humidity = be16_to_cpu(data->be16);
451 	if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
452 		/* reading was skipped */
453 		dev_err(data->dev, "reading humidity skipped\n");
454 		return -EIO;
455 	}
456 	comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
457 
458 	*val = comp_humidity * 1000 / 1024;
459 
460 	return IIO_VAL_INT;
461 }
462 
463 static int bmp280_read_raw(struct iio_dev *indio_dev,
464 			   struct iio_chan_spec const *chan,
465 			   int *val, int *val2, long mask)
466 {
467 	struct bmp280_data *data = iio_priv(indio_dev);
468 	int ret;
469 
470 	pm_runtime_get_sync(data->dev);
471 	mutex_lock(&data->lock);
472 
473 	switch (mask) {
474 	case IIO_CHAN_INFO_PROCESSED:
475 		switch (chan->type) {
476 		case IIO_HUMIDITYRELATIVE:
477 			ret = data->chip_info->read_humid(data, val, val2);
478 			break;
479 		case IIO_PRESSURE:
480 			ret = data->chip_info->read_press(data, val, val2);
481 			break;
482 		case IIO_TEMP:
483 			ret = data->chip_info->read_temp(data, val, val2);
484 			break;
485 		default:
486 			ret = -EINVAL;
487 			break;
488 		}
489 		break;
490 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
491 		switch (chan->type) {
492 		case IIO_HUMIDITYRELATIVE:
493 			*val = 1 << data->oversampling_humid;
494 			ret = IIO_VAL_INT;
495 			break;
496 		case IIO_PRESSURE:
497 			*val = 1 << data->oversampling_press;
498 			ret = IIO_VAL_INT;
499 			break;
500 		case IIO_TEMP:
501 			*val = 1 << data->oversampling_temp;
502 			ret = IIO_VAL_INT;
503 			break;
504 		default:
505 			ret = -EINVAL;
506 			break;
507 		}
508 		break;
509 	case IIO_CHAN_INFO_SAMP_FREQ:
510 		if (!data->chip_info->sampling_freq_avail) {
511 			ret = -EINVAL;
512 			break;
513 		}
514 
515 		*val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
516 		*val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
517 		ret = IIO_VAL_INT_PLUS_MICRO;
518 		break;
519 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
520 		if (!data->chip_info->iir_filter_coeffs_avail) {
521 			ret = -EINVAL;
522 			break;
523 		}
524 
525 		*val = (1 << data->iir_filter_coeff) - 1;
526 		ret = IIO_VAL_INT;
527 		break;
528 	default:
529 		ret = -EINVAL;
530 		break;
531 	}
532 
533 	mutex_unlock(&data->lock);
534 	pm_runtime_mark_last_busy(data->dev);
535 	pm_runtime_put_autosuspend(data->dev);
536 
537 	return ret;
538 }
539 
540 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
541 					       int val)
542 {
543 	const int *avail = data->chip_info->oversampling_humid_avail;
544 	const int n = data->chip_info->num_oversampling_humid_avail;
545 	int ret, prev;
546 	int i;
547 
548 	for (i = 0; i < n; i++) {
549 		if (avail[i] == val) {
550 			prev = data->oversampling_humid;
551 			data->oversampling_humid = ilog2(val);
552 
553 			ret = data->chip_info->chip_config(data);
554 			if (ret) {
555 				data->oversampling_humid = prev;
556 				data->chip_info->chip_config(data);
557 				return ret;
558 			}
559 			return 0;
560 		}
561 	}
562 	return -EINVAL;
563 }
564 
565 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
566 					       int val)
567 {
568 	const int *avail = data->chip_info->oversampling_temp_avail;
569 	const int n = data->chip_info->num_oversampling_temp_avail;
570 	int ret, prev;
571 	int i;
572 
573 	for (i = 0; i < n; i++) {
574 		if (avail[i] == val) {
575 			prev = data->oversampling_temp;
576 			data->oversampling_temp = ilog2(val);
577 
578 			ret = data->chip_info->chip_config(data);
579 			if (ret) {
580 				data->oversampling_temp = prev;
581 				data->chip_info->chip_config(data);
582 				return ret;
583 			}
584 			return 0;
585 		}
586 	}
587 	return -EINVAL;
588 }
589 
590 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
591 					       int val)
592 {
593 	const int *avail = data->chip_info->oversampling_press_avail;
594 	const int n = data->chip_info->num_oversampling_press_avail;
595 	int ret, prev;
596 	int i;
597 
598 	for (i = 0; i < n; i++) {
599 		if (avail[i] == val) {
600 			prev = data->oversampling_press;
601 			data->oversampling_press = ilog2(val);
602 
603 			ret = data->chip_info->chip_config(data);
604 			if (ret) {
605 				data->oversampling_press = prev;
606 				data->chip_info->chip_config(data);
607 				return ret;
608 			}
609 			return 0;
610 		}
611 	}
612 	return -EINVAL;
613 }
614 
615 static int bmp280_write_sampling_frequency(struct bmp280_data *data,
616 					   int val, int val2)
617 {
618 	const int (*avail)[2] = data->chip_info->sampling_freq_avail;
619 	const int n = data->chip_info->num_sampling_freq_avail;
620 	int ret, prev;
621 	int i;
622 
623 	for (i = 0; i < n; i++) {
624 		if (avail[i][0] == val && avail[i][1] == val2) {
625 			prev = data->sampling_freq;
626 			data->sampling_freq = i;
627 
628 			ret = data->chip_info->chip_config(data);
629 			if (ret) {
630 				data->sampling_freq = prev;
631 				data->chip_info->chip_config(data);
632 				return ret;
633 			}
634 			return 0;
635 		}
636 	}
637 	return -EINVAL;
638 }
639 
640 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
641 {
642 	const int *avail = data->chip_info->iir_filter_coeffs_avail;
643 	const int n = data->chip_info->num_iir_filter_coeffs_avail;
644 	int ret, prev;
645 	int i;
646 
647 	for (i = 0; i < n; i++) {
648 		if (avail[i] - 1  == val) {
649 			prev = data->iir_filter_coeff;
650 			data->iir_filter_coeff = i;
651 
652 			ret = data->chip_info->chip_config(data);
653 			if (ret) {
654 				data->iir_filter_coeff = prev;
655 				data->chip_info->chip_config(data);
656 				return ret;
657 
658 			}
659 			return 0;
660 		}
661 	}
662 	return -EINVAL;
663 }
664 
665 static int bmp280_write_raw(struct iio_dev *indio_dev,
666 			    struct iio_chan_spec const *chan,
667 			    int val, int val2, long mask)
668 {
669 	struct bmp280_data *data = iio_priv(indio_dev);
670 	int ret = 0;
671 
672 	/*
673 	 * Helper functions to update sensor running configuration.
674 	 * If an error happens applying new settings, will try restore
675 	 * previous parameters to ensure the sensor is left in a known
676 	 * working configuration.
677 	 */
678 	switch (mask) {
679 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
680 		pm_runtime_get_sync(data->dev);
681 		mutex_lock(&data->lock);
682 		switch (chan->type) {
683 		case IIO_HUMIDITYRELATIVE:
684 			ret = bmp280_write_oversampling_ratio_humid(data, val);
685 			break;
686 		case IIO_PRESSURE:
687 			ret = bmp280_write_oversampling_ratio_press(data, val);
688 			break;
689 		case IIO_TEMP:
690 			ret = bmp280_write_oversampling_ratio_temp(data, val);
691 			break;
692 		default:
693 			ret = -EINVAL;
694 			break;
695 		}
696 		mutex_unlock(&data->lock);
697 		pm_runtime_mark_last_busy(data->dev);
698 		pm_runtime_put_autosuspend(data->dev);
699 		break;
700 	case IIO_CHAN_INFO_SAMP_FREQ:
701 		pm_runtime_get_sync(data->dev);
702 		mutex_lock(&data->lock);
703 		ret = bmp280_write_sampling_frequency(data, val, val2);
704 		mutex_unlock(&data->lock);
705 		pm_runtime_mark_last_busy(data->dev);
706 		pm_runtime_put_autosuspend(data->dev);
707 		break;
708 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
709 		pm_runtime_get_sync(data->dev);
710 		mutex_lock(&data->lock);
711 		ret = bmp280_write_iir_filter_coeffs(data, val);
712 		mutex_unlock(&data->lock);
713 		pm_runtime_mark_last_busy(data->dev);
714 		pm_runtime_put_autosuspend(data->dev);
715 		break;
716 	default:
717 		return -EINVAL;
718 	}
719 
720 	return ret;
721 }
722 
723 static int bmp280_read_avail(struct iio_dev *indio_dev,
724 			     struct iio_chan_spec const *chan,
725 			     const int **vals, int *type, int *length,
726 			     long mask)
727 {
728 	struct bmp280_data *data = iio_priv(indio_dev);
729 
730 	switch (mask) {
731 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
732 		switch (chan->type) {
733 		case IIO_PRESSURE:
734 			*vals = data->chip_info->oversampling_press_avail;
735 			*length = data->chip_info->num_oversampling_press_avail;
736 			break;
737 		case IIO_TEMP:
738 			*vals = data->chip_info->oversampling_temp_avail;
739 			*length = data->chip_info->num_oversampling_temp_avail;
740 			break;
741 		default:
742 			return -EINVAL;
743 		}
744 		*type = IIO_VAL_INT;
745 		return IIO_AVAIL_LIST;
746 	case IIO_CHAN_INFO_SAMP_FREQ:
747 		*vals = (const int *)data->chip_info->sampling_freq_avail;
748 		*type = IIO_VAL_INT_PLUS_MICRO;
749 		/* Values are stored in a 2D matrix */
750 		*length = data->chip_info->num_sampling_freq_avail;
751 		return IIO_AVAIL_LIST;
752 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
753 		*vals = data->chip_info->iir_filter_coeffs_avail;
754 		*type = IIO_VAL_INT;
755 		*length = data->chip_info->num_iir_filter_coeffs_avail;
756 		return IIO_AVAIL_LIST;
757 	default:
758 		return -EINVAL;
759 	}
760 }
761 
762 static const struct iio_info bmp280_info = {
763 	.read_raw = &bmp280_read_raw,
764 	.read_avail = &bmp280_read_avail,
765 	.write_raw = &bmp280_write_raw,
766 };
767 
768 static int bmp280_chip_config(struct bmp280_data *data)
769 {
770 	u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
771 		  FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
772 	int ret;
773 
774 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
775 				 BMP280_OSRS_TEMP_MASK |
776 				 BMP280_OSRS_PRESS_MASK |
777 				 BMP280_MODE_MASK,
778 				 osrs | BMP280_MODE_NORMAL);
779 	if (ret < 0) {
780 		dev_err(data->dev,
781 			"failed to write ctrl_meas register\n");
782 		return ret;
783 	}
784 
785 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
786 				 BMP280_FILTER_MASK,
787 				 BMP280_FILTER_4X);
788 	if (ret < 0) {
789 		dev_err(data->dev,
790 			"failed to write config register\n");
791 		return ret;
792 	}
793 
794 	return ret;
795 }
796 
797 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
798 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID };
799 
800 const struct bmp280_chip_info bmp280_chip_info = {
801 	.id_reg = BMP280_REG_ID,
802 	.chip_id = bmp280_chip_ids,
803 	.num_chip_id = ARRAY_SIZE(bmp280_chip_ids),
804 	.regmap_config = &bmp280_regmap_config,
805 	.start_up_time = 2000,
806 	.channels = bmp280_channels,
807 	.num_channels = 2,
808 
809 	.oversampling_temp_avail = bmp280_oversampling_avail,
810 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
811 	/*
812 	 * Oversampling config values on BMx280 have one additional setting
813 	 * that other generations of the family don't:
814 	 * The value 0 means the measurement is bypassed instead of
815 	 * oversampling set to x1.
816 	 *
817 	 * To account for this difference, and preserve the same common
818 	 * config logic, this is handled later on chip_config callback
819 	 * incrementing one unit the oversampling setting.
820 	 */
821 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
822 
823 	.oversampling_press_avail = bmp280_oversampling_avail,
824 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
825 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
826 
827 	.chip_config = bmp280_chip_config,
828 	.read_temp = bmp280_read_temp,
829 	.read_press = bmp280_read_press,
830 	.read_calib = bmp280_read_calib,
831 };
832 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280);
833 
834 static int bme280_chip_config(struct bmp280_data *data)
835 {
836 	u8 osrs = FIELD_PREP(BMP280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
837 	int ret;
838 
839 	/*
840 	 * Oversampling of humidity must be set before oversampling of
841 	 * temperature/pressure is set to become effective.
842 	 */
843 	ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
844 				  BMP280_OSRS_HUMIDITY_MASK, osrs);
845 
846 	if (ret < 0)
847 		return ret;
848 
849 	return bmp280_chip_config(data);
850 }
851 
852 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID };
853 
854 const struct bmp280_chip_info bme280_chip_info = {
855 	.id_reg = BMP280_REG_ID,
856 	.chip_id = bme280_chip_ids,
857 	.num_chip_id = ARRAY_SIZE(bme280_chip_ids),
858 	.regmap_config = &bmp280_regmap_config,
859 	.start_up_time = 2000,
860 	.channels = bmp280_channels,
861 	.num_channels = 3,
862 
863 	.oversampling_temp_avail = bmp280_oversampling_avail,
864 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
865 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
866 
867 	.oversampling_press_avail = bmp280_oversampling_avail,
868 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
869 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
870 
871 	.oversampling_humid_avail = bmp280_oversampling_avail,
872 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
873 	.oversampling_humid_default = BMP280_OSRS_HUMIDITY_16X - 1,
874 
875 	.chip_config = bme280_chip_config,
876 	.read_temp = bmp280_read_temp,
877 	.read_press = bmp280_read_press,
878 	.read_humid = bmp280_read_humid,
879 	.read_calib = bme280_read_calib,
880 };
881 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280);
882 
883 /*
884  * Helper function to send a command to BMP3XX sensors.
885  *
886  * Sensor processes commands written to the CMD register and signals
887  * execution result through "cmd_rdy" and "cmd_error" flags available on
888  * STATUS and ERROR registers.
889  */
890 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
891 {
892 	unsigned int reg;
893 	int ret;
894 
895 	/* Check if device is ready to process a command */
896 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
897 	if (ret) {
898 		dev_err(data->dev, "failed to read error register\n");
899 		return ret;
900 	}
901 	if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
902 		dev_err(data->dev, "device is not ready to accept commands\n");
903 		return -EBUSY;
904 	}
905 
906 	/* Send command to process */
907 	ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
908 	if (ret) {
909 		dev_err(data->dev, "failed to send command to device\n");
910 		return ret;
911 	}
912 	/* Wait for 2ms for command to be processed */
913 	usleep_range(data->start_up_time, data->start_up_time + 100);
914 	/* Check for command processing error */
915 	ret = regmap_read(data->regmap, BMP380_REG_ERROR, &reg);
916 	if (ret) {
917 		dev_err(data->dev, "error reading ERROR reg\n");
918 		return ret;
919 	}
920 	if (reg & BMP380_ERR_CMD_MASK) {
921 		dev_err(data->dev, "error processing command 0x%X\n", cmd);
922 		return -EINVAL;
923 	}
924 
925 	return 0;
926 }
927 
928 /*
929  * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value of
930  * "5123" equals 51.2º C. t_fine carries fine temperature as global value.
931  *
932  * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
933  * https://github.com/BoschSensortec/BMP3-Sensor-API.
934  */
935 static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
936 {
937 	s64 var1, var2, var3, var4, var5, var6, comp_temp;
938 	struct bmp380_calib *calib = &data->calib.bmp380;
939 
940 	var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
941 	var2 = var1 * ((s64) calib->T2);
942 	var3 = var1 * var1;
943 	var4 = var3 * ((s64) calib->T3);
944 	var5 = (var2 << 18) + var4;
945 	var6 = var5 >> 32;
946 	data->t_fine = (s32) var6;
947 	comp_temp = (var6 * 25) >> 14;
948 
949 	comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
950 	return (s32) comp_temp;
951 }
952 
953 /*
954  * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
955  * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
956  *
957  * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
958  * https://github.com/BoschSensortec/BMP3-Sensor-API.
959  */
960 static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press)
961 {
962 	s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
963 	struct bmp380_calib *calib = &data->calib.bmp380;
964 	u32 comp_press;
965 
966 	var1 = (s64)data->t_fine * (s64)data->t_fine;
967 	var2 = var1 >> 6;
968 	var3 = (var2 * ((s64) data->t_fine)) >> 8;
969 	var4 = ((s64)calib->P8 * var3) >> 5;
970 	var5 = ((s64)calib->P7 * var1) << 4;
971 	var6 = ((s64)calib->P6 * (s64)data->t_fine) << 22;
972 	offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
973 	var2 = ((s64)calib->P4 * var3) >> 5;
974 	var4 = ((s64)calib->P3 * var1) << 2;
975 	var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
976 	       ((s64)data->t_fine << 21);
977 	sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
978 			var2 + var4 + var5;
979 	var1 = (sensitivity >> 24) * (s64)adc_press;
980 	var2 = (s64)calib->P10 * (s64)data->t_fine;
981 	var3 = var2 + ((s64)calib->P9 << 16);
982 	var4 = (var3 * (s64)adc_press) >> 13;
983 
984 	/*
985 	 * Dividing by 10 followed by multiplying by 10 to avoid
986 	 * possible overflow caused by (uncomp_data->pressure * partial_data4).
987 	 */
988 	var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
989 	var5 *= 10;
990 	var6 = (s64)adc_press * (s64)adc_press;
991 	var2 = ((s64)calib->P11 * var6) >> 16;
992 	var3 = (var2 * (s64)adc_press) >> 7;
993 	var4 = (offset >> 2) + var1 + var5 + var3;
994 	comp_press = ((u64)var4 * 25) >> 40;
995 
996 	comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
997 	return comp_press;
998 }
999 
1000 static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2)
1001 {
1002 	s32 comp_temp;
1003 	u32 adc_temp;
1004 	int ret;
1005 
1006 	ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1007 			       data->buf, sizeof(data->buf));
1008 	if (ret) {
1009 		dev_err(data->dev, "failed to read temperature\n");
1010 		return ret;
1011 	}
1012 
1013 	adc_temp = get_unaligned_le24(data->buf);
1014 	if (adc_temp == BMP380_TEMP_SKIPPED) {
1015 		dev_err(data->dev, "reading temperature skipped\n");
1016 		return -EIO;
1017 	}
1018 	comp_temp = bmp380_compensate_temp(data, adc_temp);
1019 
1020 	/*
1021 	 * Val might be NULL if we're called by the read_press routine,
1022 	 * who only cares about the carry over t_fine value.
1023 	 */
1024 	if (val) {
1025 		/* IIO reports temperatures in milli Celsius */
1026 		*val = comp_temp * 10;
1027 		return IIO_VAL_INT;
1028 	}
1029 
1030 	return 0;
1031 }
1032 
1033 static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
1034 {
1035 	s32 comp_press;
1036 	u32 adc_press;
1037 	int ret;
1038 
1039 	/* Read and compensate for temperature so we get a reading of t_fine */
1040 	ret = bmp380_read_temp(data, NULL, NULL);
1041 	if (ret)
1042 		return ret;
1043 
1044 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1045 			       data->buf, sizeof(data->buf));
1046 	if (ret) {
1047 		dev_err(data->dev, "failed to read pressure\n");
1048 		return ret;
1049 	}
1050 
1051 	adc_press = get_unaligned_le24(data->buf);
1052 	if (adc_press == BMP380_PRESS_SKIPPED) {
1053 		dev_err(data->dev, "reading pressure skipped\n");
1054 		return -EIO;
1055 	}
1056 	comp_press = bmp380_compensate_press(data, adc_press);
1057 
1058 	*val = comp_press;
1059 	/* Compensated pressure is in cPa (centipascals) */
1060 	*val2 = 100000;
1061 
1062 	return IIO_VAL_FRACTIONAL;
1063 }
1064 
1065 static int bmp380_read_calib(struct bmp280_data *data)
1066 {
1067 	struct bmp380_calib *calib = &data->calib.bmp380;
1068 	int ret;
1069 
1070 	/* Read temperature and pressure calibration data */
1071 	ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
1072 			       data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
1073 	if (ret) {
1074 		dev_err(data->dev,
1075 			"failed to read temperature calibration parameters\n");
1076 		return ret;
1077 	}
1078 
1079 	/* Toss the temperature calibration data into the entropy pool */
1080 	add_device_randomness(data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
1081 
1082 	/* Parse calibration values */
1083 	calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
1084 	calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
1085 	calib->T3 = data->bmp380_cal_buf[BMP380_T3];
1086 	calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
1087 	calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
1088 	calib->P3 = data->bmp380_cal_buf[BMP380_P3];
1089 	calib->P4 = data->bmp380_cal_buf[BMP380_P4];
1090 	calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
1091 	calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
1092 	calib->P7 = data->bmp380_cal_buf[BMP380_P7];
1093 	calib->P8 = data->bmp380_cal_buf[BMP380_P8];
1094 	calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
1095 	calib->P10 = data->bmp380_cal_buf[BMP380_P10];
1096 	calib->P11 = data->bmp380_cal_buf[BMP380_P11];
1097 
1098 	return 0;
1099 }
1100 
1101 static const int bmp380_odr_table[][2] = {
1102 	[BMP380_ODR_200HZ]	= {200, 0},
1103 	[BMP380_ODR_100HZ]	= {100, 0},
1104 	[BMP380_ODR_50HZ]	= {50, 0},
1105 	[BMP380_ODR_25HZ]	= {25, 0},
1106 	[BMP380_ODR_12_5HZ]	= {12, 500000},
1107 	[BMP380_ODR_6_25HZ]	= {6, 250000},
1108 	[BMP380_ODR_3_125HZ]	= {3, 125000},
1109 	[BMP380_ODR_1_5625HZ]	= {1, 562500},
1110 	[BMP380_ODR_0_78HZ]	= {0, 781250},
1111 	[BMP380_ODR_0_39HZ]	= {0, 390625},
1112 	[BMP380_ODR_0_2HZ]	= {0, 195313},
1113 	[BMP380_ODR_0_1HZ]	= {0, 97656},
1114 	[BMP380_ODR_0_05HZ]	= {0, 48828},
1115 	[BMP380_ODR_0_02HZ]	= {0, 24414},
1116 	[BMP380_ODR_0_01HZ]	= {0, 12207},
1117 	[BMP380_ODR_0_006HZ]	= {0, 6104},
1118 	[BMP380_ODR_0_003HZ]	= {0, 3052},
1119 	[BMP380_ODR_0_0015HZ]	= {0, 1526},
1120 };
1121 
1122 static int bmp380_preinit(struct bmp280_data *data)
1123 {
1124 	/* BMP3xx requires soft-reset as part of initialization */
1125 	return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1126 }
1127 
1128 static int bmp380_chip_config(struct bmp280_data *data)
1129 {
1130 	bool change = false, aux;
1131 	unsigned int tmp;
1132 	u8 osrs;
1133 	int ret;
1134 
1135 	/* Configure power control register */
1136 	ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1137 				 BMP380_CTRL_SENSORS_MASK,
1138 				 BMP380_CTRL_SENSORS_PRESS_EN |
1139 				 BMP380_CTRL_SENSORS_TEMP_EN);
1140 	if (ret) {
1141 		dev_err(data->dev,
1142 			"failed to write operation control register\n");
1143 		return ret;
1144 	}
1145 
1146 	/* Configure oversampling */
1147 	osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1148 	       FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1149 
1150 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1151 				       BMP380_OSRS_TEMP_MASK |
1152 				       BMP380_OSRS_PRESS_MASK,
1153 				       osrs, &aux);
1154 	if (ret) {
1155 		dev_err(data->dev, "failed to write oversampling register\n");
1156 		return ret;
1157 	}
1158 	change = change || aux;
1159 
1160 	/* Configure output data rate */
1161 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1162 				       BMP380_ODRS_MASK, data->sampling_freq, &aux);
1163 	if (ret) {
1164 		dev_err(data->dev, "failed to write ODR selection register\n");
1165 		return ret;
1166 	}
1167 	change = change || aux;
1168 
1169 	/* Set filter data */
1170 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK,
1171 				       FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff),
1172 				       &aux);
1173 	if (ret) {
1174 		dev_err(data->dev, "failed to write config register\n");
1175 		return ret;
1176 	}
1177 	change = change || aux;
1178 
1179 	if (change) {
1180 		/*
1181 		 * The configurations errors are detected on the fly during a measurement
1182 		 * cycle. If the sampling frequency is too low, it's faster to reset
1183 		 * the measurement loop than wait until the next measurement is due.
1184 		 *
1185 		 * Resets sensor measurement loop toggling between sleep and normal
1186 		 * operating modes.
1187 		 */
1188 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1189 					BMP380_MODE_MASK,
1190 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
1191 		if (ret) {
1192 			dev_err(data->dev, "failed to set sleep mode\n");
1193 			return ret;
1194 		}
1195 		usleep_range(2000, 2500);
1196 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1197 					BMP380_MODE_MASK,
1198 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
1199 		if (ret) {
1200 			dev_err(data->dev, "failed to set normal mode\n");
1201 			return ret;
1202 		}
1203 		/*
1204 		 * Waits for measurement before checking configuration error flag.
1205 		 * Selected longest measure time indicated in section 3.9.1
1206 		 * in the datasheet.
1207 		 */
1208 		msleep(80);
1209 
1210 		/* Check config error flag */
1211 		ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1212 		if (ret) {
1213 			dev_err(data->dev,
1214 				"failed to read error register\n");
1215 			return ret;
1216 		}
1217 		if (tmp & BMP380_ERR_CONF_MASK) {
1218 			dev_warn(data->dev,
1219 				"sensor flagged configuration as incompatible\n");
1220 			return -EINVAL;
1221 		}
1222 	}
1223 
1224 	return 0;
1225 }
1226 
1227 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
1228 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
1229 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID };
1230 
1231 const struct bmp280_chip_info bmp380_chip_info = {
1232 	.id_reg = BMP380_REG_ID,
1233 	.chip_id = bmp380_chip_ids,
1234 	.num_chip_id = ARRAY_SIZE(bmp380_chip_ids),
1235 	.regmap_config = &bmp380_regmap_config,
1236 	.spi_read_extra_byte = true,
1237 	.start_up_time = 2000,
1238 	.channels = bmp380_channels,
1239 	.num_channels = 2,
1240 
1241 	.oversampling_temp_avail = bmp380_oversampling_avail,
1242 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1243 	.oversampling_temp_default = ilog2(1),
1244 
1245 	.oversampling_press_avail = bmp380_oversampling_avail,
1246 	.num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1247 	.oversampling_press_default = ilog2(4),
1248 
1249 	.sampling_freq_avail = bmp380_odr_table,
1250 	.num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
1251 	.sampling_freq_default = BMP380_ODR_50HZ,
1252 
1253 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1254 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1255 	.iir_filter_coeff_default = 2,
1256 
1257 	.chip_config = bmp380_chip_config,
1258 	.read_temp = bmp380_read_temp,
1259 	.read_press = bmp380_read_press,
1260 	.read_calib = bmp380_read_calib,
1261 	.preinit = bmp380_preinit,
1262 };
1263 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280);
1264 
1265 static int bmp580_soft_reset(struct bmp280_data *data)
1266 {
1267 	unsigned int reg;
1268 	int ret;
1269 
1270 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
1271 	if (ret) {
1272 		dev_err(data->dev, "failed to send reset command to device\n");
1273 		return ret;
1274 	}
1275 	usleep_range(2000, 2500);
1276 
1277 	/* Dummy read of chip_id */
1278 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1279 	if (ret) {
1280 		dev_err(data->dev, "failed to reestablish comms after reset\n");
1281 		return ret;
1282 	}
1283 
1284 	ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &reg);
1285 	if (ret) {
1286 		dev_err(data->dev, "error reading interrupt status register\n");
1287 		return ret;
1288 	}
1289 	if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
1290 		dev_err(data->dev, "error resetting sensor\n");
1291 		return -EINVAL;
1292 	}
1293 
1294 	return 0;
1295 }
1296 
1297 /**
1298  * bmp580_nvm_operation() - Helper function to commit NVM memory operations
1299  * @data: sensor data struct
1300  * @is_write: flag to signal write operation
1301  */
1302 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
1303 {
1304 	unsigned long timeout, poll;
1305 	unsigned int reg;
1306 	int ret;
1307 
1308 	/* Check NVM ready flag */
1309 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1310 	if (ret) {
1311 		dev_err(data->dev, "failed to check nvm status\n");
1312 		return ret;
1313 	}
1314 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
1315 		dev_err(data->dev, "sensor's nvm is not ready\n");
1316 		return -EIO;
1317 	}
1318 
1319 	/* Start NVM operation sequence */
1320 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_NVM_OP_SEQ_0);
1321 	if (ret) {
1322 		dev_err(data->dev, "failed to send nvm operation's first sequence\n");
1323 		return ret;
1324 	}
1325 	if (is_write) {
1326 		/* Send NVM write sequence */
1327 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1328 				   BMP580_CMD_NVM_WRITE_SEQ_1);
1329 		if (ret) {
1330 			dev_err(data->dev, "failed to send nvm write sequence\n");
1331 			return ret;
1332 		}
1333 		/* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1334 		poll = 2000;
1335 		timeout = 12000;
1336 	} else {
1337 		/* Send NVM read sequence */
1338 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1339 				   BMP580_CMD_NVM_READ_SEQ_1);
1340 		if (ret) {
1341 			dev_err(data->dev, "failed to send nvm read sequence\n");
1342 			return ret;
1343 		}
1344 		/* Datasheet says on 4.8.1.1 it takes approximately 200us */
1345 		poll = 50;
1346 		timeout = 400;
1347 	}
1348 	if (ret) {
1349 		dev_err(data->dev, "failed to write command sequence\n");
1350 		return -EIO;
1351 	}
1352 
1353 	/* Wait until NVM is ready again */
1354 	ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
1355 				       (reg & BMP580_STATUS_NVM_RDY_MASK),
1356 				       poll, timeout);
1357 	if (ret) {
1358 		dev_err(data->dev, "error checking nvm operation status\n");
1359 		return ret;
1360 	}
1361 
1362 	/* Check NVM error flags */
1363 	if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
1364 		dev_err(data->dev, "error processing nvm operation\n");
1365 		return -EIO;
1366 	}
1367 
1368 	return 0;
1369 }
1370 
1371 /*
1372  * Contrary to previous sensors families, compensation algorithm is builtin.
1373  * We are only required to read the register raw data and adapt the ranges
1374  * for what is expected on IIO ABI.
1375  */
1376 
1377 static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
1378 {
1379 	s32 raw_temp;
1380 	int ret;
1381 
1382 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf,
1383 			       sizeof(data->buf));
1384 	if (ret) {
1385 		dev_err(data->dev, "failed to read temperature\n");
1386 		return ret;
1387 	}
1388 
1389 	raw_temp = get_unaligned_le24(data->buf);
1390 	if (raw_temp == BMP580_TEMP_SKIPPED) {
1391 		dev_err(data->dev, "reading temperature skipped\n");
1392 		return -EIO;
1393 	}
1394 
1395 	/*
1396 	 * Temperature is returned in Celsius degrees in fractional
1397 	 * form down 2^16. We rescale by x1000 to return millidegrees
1398 	 * Celsius to respect IIO ABI.
1399 	 */
1400 	raw_temp = sign_extend32(raw_temp, 23);
1401 	*val = ((s64)raw_temp * 1000) / (1 << 16);
1402 	return IIO_VAL_INT;
1403 }
1404 
1405 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)
1406 {
1407 	u32 raw_press;
1408 	int ret;
1409 
1410 	ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf,
1411 			       sizeof(data->buf));
1412 	if (ret) {
1413 		dev_err(data->dev, "failed to read pressure\n");
1414 		return ret;
1415 	}
1416 
1417 	raw_press = get_unaligned_le24(data->buf);
1418 	if (raw_press == BMP580_PRESS_SKIPPED) {
1419 		dev_err(data->dev, "reading pressure skipped\n");
1420 		return -EIO;
1421 	}
1422 	/*
1423 	 * Pressure is returned in Pascals in fractional form down 2^16.
1424 	 * We rescale /1000 to convert to kilopascal to respect IIO ABI.
1425 	 */
1426 	*val = raw_press;
1427 	*val2 = 64000; /* 2^6 * 1000 */
1428 	return IIO_VAL_FRACTIONAL;
1429 }
1430 
1431 static const int bmp580_odr_table[][2] = {
1432 	[BMP580_ODR_240HZ] =	{240, 0},
1433 	[BMP580_ODR_218HZ] =	{218, 0},
1434 	[BMP580_ODR_199HZ] =	{199, 0},
1435 	[BMP580_ODR_179HZ] =	{179, 0},
1436 	[BMP580_ODR_160HZ] =	{160, 0},
1437 	[BMP580_ODR_149HZ] =	{149, 0},
1438 	[BMP580_ODR_140HZ] =	{140, 0},
1439 	[BMP580_ODR_129HZ] =	{129, 0},
1440 	[BMP580_ODR_120HZ] =	{120, 0},
1441 	[BMP580_ODR_110HZ] =	{110, 0},
1442 	[BMP580_ODR_100HZ] =	{100, 0},
1443 	[BMP580_ODR_89HZ] =	{89, 0},
1444 	[BMP580_ODR_80HZ] =	{80, 0},
1445 	[BMP580_ODR_70HZ] =	{70, 0},
1446 	[BMP580_ODR_60HZ] =	{60, 0},
1447 	[BMP580_ODR_50HZ] =	{50, 0},
1448 	[BMP580_ODR_45HZ] =	{45, 0},
1449 	[BMP580_ODR_40HZ] =	{40, 0},
1450 	[BMP580_ODR_35HZ] =	{35, 0},
1451 	[BMP580_ODR_30HZ] =	{30, 0},
1452 	[BMP580_ODR_25HZ] =	{25, 0},
1453 	[BMP580_ODR_20HZ] =	{20, 0},
1454 	[BMP580_ODR_15HZ] =	{15, 0},
1455 	[BMP580_ODR_10HZ] =	{10, 0},
1456 	[BMP580_ODR_5HZ] =	{5, 0},
1457 	[BMP580_ODR_4HZ] =	{4, 0},
1458 	[BMP580_ODR_3HZ] =	{3, 0},
1459 	[BMP580_ODR_2HZ] =	{2, 0},
1460 	[BMP580_ODR_1HZ] =	{1, 0},
1461 	[BMP580_ODR_0_5HZ] =	{0, 500000},
1462 	[BMP580_ODR_0_25HZ] =	{0, 250000},
1463 	[BMP580_ODR_0_125HZ] =	{0, 125000},
1464 };
1465 
1466 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1467 
1468 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1469 			     size_t bytes)
1470 {
1471 	struct bmp280_data *data = priv;
1472 	u16 *dst = val;
1473 	int ret, addr;
1474 
1475 	pm_runtime_get_sync(data->dev);
1476 	mutex_lock(&data->lock);
1477 
1478 	/* Set sensor in standby mode */
1479 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1480 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1481 				 BMP580_ODR_DEEPSLEEP_DIS |
1482 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1483 	if (ret) {
1484 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1485 		goto exit;
1486 	}
1487 	/* Wait standby transition time */
1488 	usleep_range(2500, 3000);
1489 
1490 	while (bytes >= sizeof(*dst)) {
1491 		addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1492 
1493 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1494 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1495 		if (ret) {
1496 			dev_err(data->dev, "error writing nvm address\n");
1497 			goto exit;
1498 		}
1499 
1500 		ret = bmp580_nvm_operation(data, false);
1501 		if (ret)
1502 			goto exit;
1503 
1504 		ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1505 				       sizeof(data->le16));
1506 		if (ret) {
1507 			dev_err(data->dev, "error reading nvm data regs\n");
1508 			goto exit;
1509 		}
1510 
1511 		*dst++ = le16_to_cpu(data->le16);
1512 		bytes -= sizeof(*dst);
1513 		offset += sizeof(*dst);
1514 	}
1515 exit:
1516 	/* Restore chip config */
1517 	data->chip_info->chip_config(data);
1518 	mutex_unlock(&data->lock);
1519 	pm_runtime_mark_last_busy(data->dev);
1520 	pm_runtime_put_autosuspend(data->dev);
1521 	return ret;
1522 }
1523 
1524 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
1525 			      size_t bytes)
1526 {
1527 	struct bmp280_data *data = priv;
1528 	u16 *buf = val;
1529 	int ret, addr;
1530 
1531 	pm_runtime_get_sync(data->dev);
1532 	mutex_lock(&data->lock);
1533 
1534 	/* Set sensor in standby mode */
1535 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1536 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1537 				 BMP580_ODR_DEEPSLEEP_DIS |
1538 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1539 	if (ret) {
1540 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1541 		goto exit;
1542 	}
1543 	/* Wait standby transition time */
1544 	usleep_range(2500, 3000);
1545 
1546 	while (bytes >= sizeof(*buf)) {
1547 		addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1548 
1549 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, BMP580_NVM_PROG_EN |
1550 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1551 		if (ret) {
1552 			dev_err(data->dev, "error writing nvm address\n");
1553 			goto exit;
1554 		}
1555 		data->le16 = cpu_to_le16(*buf++);
1556 
1557 		ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1558 					sizeof(data->le16));
1559 		if (ret) {
1560 			dev_err(data->dev, "error writing LSB NVM data regs\n");
1561 			goto exit;
1562 		}
1563 
1564 		ret = bmp580_nvm_operation(data, true);
1565 		if (ret)
1566 			goto exit;
1567 
1568 		/* Disable programming mode bit */
1569 		ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
1570 					 BMP580_NVM_PROG_EN, 0);
1571 		if (ret) {
1572 			dev_err(data->dev, "error resetting nvm write\n");
1573 			goto exit;
1574 		}
1575 
1576 		bytes -= sizeof(*buf);
1577 		offset += sizeof(*buf);
1578 	}
1579 exit:
1580 	/* Restore chip config */
1581 	data->chip_info->chip_config(data);
1582 	mutex_unlock(&data->lock);
1583 	pm_runtime_mark_last_busy(data->dev);
1584 	pm_runtime_put_autosuspend(data->dev);
1585 	return ret;
1586 }
1587 
1588 static int bmp580_preinit(struct bmp280_data *data)
1589 {
1590 	struct nvmem_config config = {
1591 		.dev = data->dev,
1592 		.priv = data,
1593 		.name = "bmp580_nvmem",
1594 		.word_size = sizeof(u16),
1595 		.stride = sizeof(u16),
1596 		.size = 3 * sizeof(u16),
1597 		.reg_read = bmp580_nvmem_read,
1598 		.reg_write = bmp580_nvmem_write,
1599 	};
1600 	unsigned int reg;
1601 	int ret;
1602 
1603 	/* Issue soft-reset command */
1604 	ret = bmp580_soft_reset(data);
1605 	if (ret)
1606 		return ret;
1607 
1608 	/* Post powerup sequence */
1609 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1610 	if (ret)
1611 		return ret;
1612 
1613 	/* Print warn message if we don't know the chip id */
1614 	if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
1615 		dev_warn(data->dev, "preinit: unexpected chip_id\n");
1616 
1617 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1618 	if (ret)
1619 		return ret;
1620 
1621 	/* Check nvm status */
1622 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
1623 		dev_err(data->dev, "preinit: nvm error on powerup sequence\n");
1624 		return -EIO;
1625 	}
1626 
1627 	/* Register nvmem device */
1628 	return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
1629 }
1630 
1631 static int bmp580_chip_config(struct bmp280_data *data)
1632 {
1633 	bool change = false, aux;
1634 	unsigned int tmp;
1635 	u8 reg_val;
1636 	int ret;
1637 
1638 	/* Sets sensor in standby mode */
1639 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1640 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1641 				 BMP580_ODR_DEEPSLEEP_DIS |
1642 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1643 	if (ret) {
1644 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1645 		return ret;
1646 	}
1647 	/* From datasheet's table 4: electrical characteristics */
1648 	usleep_range(2500, 3000);
1649 
1650 	/* Set default DSP mode settings */
1651 	reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
1652 		  BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
1653 
1654 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
1655 				 BMP580_DSP_COMP_MASK |
1656 				 BMP580_DSP_SHDW_IIR_TEMP_EN |
1657 				 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
1658 
1659 	/* Configure oversampling */
1660 	reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
1661 		  FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
1662 		  BMP580_OSR_PRESS_EN;
1663 
1664 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
1665 				       BMP580_OSR_TEMP_MASK | BMP580_OSR_PRESS_MASK |
1666 				       BMP580_OSR_PRESS_EN,
1667 				       reg_val, &aux);
1668 	if (ret) {
1669 		dev_err(data->dev, "failed to write oversampling register\n");
1670 		return ret;
1671 	}
1672 	change = change || aux;
1673 
1674 	/* Configure output data rate */
1675 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
1676 				       FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
1677 				       &aux);
1678 	if (ret) {
1679 		dev_err(data->dev, "failed to write ODR configuration register\n");
1680 		return ret;
1681 	}
1682 	change = change || aux;
1683 
1684 	/* Set filter data */
1685 	reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
1686 		  FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
1687 
1688 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
1689 				       BMP580_DSP_IIR_PRESS_MASK |
1690 				       BMP580_DSP_IIR_TEMP_MASK,
1691 				       reg_val, &aux);
1692 	if (ret) {
1693 		dev_err(data->dev, "failed to write config register\n");
1694 		return ret;
1695 	}
1696 	change = change || aux;
1697 
1698 	/* Restore sensor to normal operation mode */
1699 	ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1700 				BMP580_MODE_MASK,
1701 				FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
1702 	if (ret) {
1703 		dev_err(data->dev, "failed to set normal mode\n");
1704 		return ret;
1705 	}
1706 	/* From datasheet's table 4: electrical characteristics */
1707 	usleep_range(3000, 3500);
1708 
1709 	if (change) {
1710 		/*
1711 		 * Check if ODR and OSR settings are valid or we are
1712 		 * operating in a degraded mode.
1713 		 */
1714 		ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
1715 		if (ret) {
1716 			dev_err(data->dev, "error reading effective OSR register\n");
1717 			return ret;
1718 		}
1719 		if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
1720 			dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
1721 			/* Set current OSR settings from data on effective OSR */
1722 			data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
1723 			data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
1724 			return -EINVAL;
1725 		}
1726 	}
1727 
1728 	return 0;
1729 }
1730 
1731 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
1732 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };
1733 
1734 const struct bmp280_chip_info bmp580_chip_info = {
1735 	.id_reg = BMP580_REG_CHIP_ID,
1736 	.chip_id = bmp580_chip_ids,
1737 	.num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
1738 	.regmap_config = &bmp580_regmap_config,
1739 	.start_up_time = 2000,
1740 	.channels = bmp380_channels,
1741 	.num_channels = 2,
1742 
1743 	.oversampling_temp_avail = bmp580_oversampling_avail,
1744 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1745 	.oversampling_temp_default = ilog2(1),
1746 
1747 	.oversampling_press_avail = bmp580_oversampling_avail,
1748 	.num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1749 	.oversampling_press_default = ilog2(4),
1750 
1751 	.sampling_freq_avail = bmp580_odr_table,
1752 	.num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
1753 	.sampling_freq_default = BMP580_ODR_50HZ,
1754 
1755 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1756 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1757 	.iir_filter_coeff_default = 2,
1758 
1759 	.chip_config = bmp580_chip_config,
1760 	.read_temp = bmp580_read_temp,
1761 	.read_press = bmp580_read_press,
1762 	.preinit = bmp580_preinit,
1763 };
1764 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
1765 
1766 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
1767 {
1768 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
1769 	unsigned int delay_us;
1770 	unsigned int ctrl;
1771 	int ret;
1772 
1773 	if (data->use_eoc)
1774 		reinit_completion(&data->done);
1775 
1776 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
1777 	if (ret)
1778 		return ret;
1779 
1780 	if (data->use_eoc) {
1781 		/*
1782 		 * If we have a completion interrupt, use it, wait up to
1783 		 * 100ms. The longest conversion time listed is 76.5 ms for
1784 		 * advanced resolution mode.
1785 		 */
1786 		ret = wait_for_completion_timeout(&data->done,
1787 						  1 + msecs_to_jiffies(100));
1788 		if (!ret)
1789 			dev_err(data->dev, "timeout waiting for completion\n");
1790 	} else {
1791 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
1792 			delay_us = 4500;
1793 		else
1794 			delay_us =
1795 				conversion_time_max[data->oversampling_press];
1796 
1797 		usleep_range(delay_us, delay_us + 1000);
1798 	}
1799 
1800 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
1801 	if (ret)
1802 		return ret;
1803 
1804 	/* The value of this bit reset to "0" after conversion is complete */
1805 	if (ctrl & BMP180_MEAS_SCO)
1806 		return -EIO;
1807 
1808 	return 0;
1809 }
1810 
1811 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
1812 {
1813 	int ret;
1814 
1815 	ret = bmp180_measure(data,
1816 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
1817 			     BMP180_MEAS_SCO);
1818 	if (ret)
1819 		return ret;
1820 
1821 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1822 			       &data->be16, sizeof(data->be16));
1823 	if (ret)
1824 		return ret;
1825 
1826 	*val = be16_to_cpu(data->be16);
1827 
1828 	return 0;
1829 }
1830 
1831 static int bmp180_read_calib(struct bmp280_data *data)
1832 {
1833 	struct bmp180_calib *calib = &data->calib.bmp180;
1834 	int ret;
1835 	int i;
1836 
1837 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1838 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1839 
1840 	if (ret < 0)
1841 		return ret;
1842 
1843 	/* None of the words has the value 0 or 0xFFFF */
1844 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
1845 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
1846 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
1847 			return -EIO;
1848 	}
1849 
1850 	/* Toss the calibration data into the entropy pool */
1851 	add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1852 
1853 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
1854 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
1855 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
1856 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
1857 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
1858 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
1859 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
1860 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
1861 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
1862 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
1863 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
1864 
1865 	return 0;
1866 }
1867 
1868 /*
1869  * Returns temperature in DegC, resolution is 0.1 DegC.
1870  * t_fine carries fine temperature as global value.
1871  *
1872  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1873  */
1874 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
1875 {
1876 	struct bmp180_calib *calib = &data->calib.bmp180;
1877 	s32 x1, x2;
1878 
1879 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1880 	x2 = (calib->MC << 11) / (x1 + calib->MD);
1881 	data->t_fine = x1 + x2;
1882 
1883 	return (data->t_fine + 8) >> 4;
1884 }
1885 
1886 static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2)
1887 {
1888 	s32 adc_temp, comp_temp;
1889 	int ret;
1890 
1891 	ret = bmp180_read_adc_temp(data, &adc_temp);
1892 	if (ret)
1893 		return ret;
1894 
1895 	comp_temp = bmp180_compensate_temp(data, adc_temp);
1896 
1897 	/*
1898 	 * val might be NULL if we're called by the read_press routine,
1899 	 * who only cares about the carry over t_fine value.
1900 	 */
1901 	if (val) {
1902 		*val = comp_temp * 100;
1903 		return IIO_VAL_INT;
1904 	}
1905 
1906 	return 0;
1907 }
1908 
1909 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
1910 {
1911 	u8 oss = data->oversampling_press;
1912 	int ret;
1913 
1914 	ret = bmp180_measure(data,
1915 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
1916 			     FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
1917 			     BMP180_MEAS_SCO);
1918 	if (ret)
1919 		return ret;
1920 
1921 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1922 			       data->buf, sizeof(data->buf));
1923 	if (ret)
1924 		return ret;
1925 
1926 	*val = get_unaligned_be24(data->buf) >> (8 - oss);
1927 
1928 	return 0;
1929 }
1930 
1931 /*
1932  * Returns pressure in Pa, resolution is 1 Pa.
1933  *
1934  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1935  */
1936 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
1937 {
1938 	struct bmp180_calib *calib = &data->calib.bmp180;
1939 	s32 oss = data->oversampling_press;
1940 	s32 x1, x2, x3, p;
1941 	s32 b3, b6;
1942 	u32 b4, b7;
1943 
1944 	b6 = data->t_fine - 4000;
1945 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1946 	x2 = calib->AC2 * b6 >> 11;
1947 	x3 = x1 + x2;
1948 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
1949 	x1 = calib->AC3 * b6 >> 13;
1950 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
1951 	x3 = (x1 + x2 + 2) >> 2;
1952 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
1953 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
1954 	if (b7 < 0x80000000)
1955 		p = (b7 * 2) / b4;
1956 	else
1957 		p = (b7 / b4) * 2;
1958 
1959 	x1 = (p >> 8) * (p >> 8);
1960 	x1 = (x1 * 3038) >> 16;
1961 	x2 = (-7357 * p) >> 16;
1962 
1963 	return p + ((x1 + x2 + 3791) >> 4);
1964 }
1965 
1966 static int bmp180_read_press(struct bmp280_data *data,
1967 			     int *val, int *val2)
1968 {
1969 	u32 comp_press;
1970 	s32 adc_press;
1971 	int ret;
1972 
1973 	/* Read and compensate temperature so we get a reading of t_fine. */
1974 	ret = bmp180_read_temp(data, NULL, NULL);
1975 	if (ret)
1976 		return ret;
1977 
1978 	ret = bmp180_read_adc_press(data, &adc_press);
1979 	if (ret)
1980 		return ret;
1981 
1982 	comp_press = bmp180_compensate_press(data, adc_press);
1983 
1984 	*val = comp_press;
1985 	*val2 = 1000;
1986 
1987 	return IIO_VAL_FRACTIONAL;
1988 }
1989 
1990 static int bmp180_chip_config(struct bmp280_data *data)
1991 {
1992 	return 0;
1993 }
1994 
1995 static const int bmp180_oversampling_temp_avail[] = { 1 };
1996 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1997 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID };
1998 
1999 const struct bmp280_chip_info bmp180_chip_info = {
2000 	.id_reg = BMP280_REG_ID,
2001 	.chip_id = bmp180_chip_ids,
2002 	.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
2003 	.regmap_config = &bmp180_regmap_config,
2004 	.start_up_time = 2000,
2005 	.channels = bmp280_channels,
2006 	.num_channels = 2,
2007 
2008 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
2009 	.num_oversampling_temp_avail =
2010 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
2011 	.oversampling_temp_default = 0,
2012 
2013 	.oversampling_press_avail = bmp180_oversampling_press_avail,
2014 	.num_oversampling_press_avail =
2015 		ARRAY_SIZE(bmp180_oversampling_press_avail),
2016 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
2017 
2018 	.chip_config = bmp180_chip_config,
2019 	.read_temp = bmp180_read_temp,
2020 	.read_press = bmp180_read_press,
2021 	.read_calib = bmp180_read_calib,
2022 };
2023 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
2024 
2025 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2026 {
2027 	struct bmp280_data *data = d;
2028 
2029 	complete(&data->done);
2030 
2031 	return IRQ_HANDLED;
2032 }
2033 
2034 static int bmp085_fetch_eoc_irq(struct device *dev,
2035 				const char *name,
2036 				int irq,
2037 				struct bmp280_data *data)
2038 {
2039 	unsigned long irq_trig;
2040 	int ret;
2041 
2042 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
2043 	if (irq_trig != IRQF_TRIGGER_RISING) {
2044 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
2045 		irq_trig = IRQF_TRIGGER_RISING;
2046 	}
2047 
2048 	init_completion(&data->done);
2049 
2050 	ret = devm_request_threaded_irq(dev,
2051 			irq,
2052 			bmp085_eoc_irq,
2053 			NULL,
2054 			irq_trig,
2055 			name,
2056 			data);
2057 	if (ret) {
2058 		/* Bail out without IRQ but keep the driver in place */
2059 		dev_err(dev, "unable to request DRDY IRQ\n");
2060 		return 0;
2061 	}
2062 
2063 	data->use_eoc = true;
2064 	return 0;
2065 }
2066 
2067 static void bmp280_pm_disable(void *data)
2068 {
2069 	struct device *dev = data;
2070 
2071 	pm_runtime_get_sync(dev);
2072 	pm_runtime_put_noidle(dev);
2073 	pm_runtime_disable(dev);
2074 }
2075 
2076 static void bmp280_regulators_disable(void *data)
2077 {
2078 	struct regulator_bulk_data *supplies = data;
2079 
2080 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
2081 }
2082 
2083 int bmp280_common_probe(struct device *dev,
2084 			struct regmap *regmap,
2085 			const struct bmp280_chip_info *chip_info,
2086 			const char *name,
2087 			int irq)
2088 {
2089 	struct iio_dev *indio_dev;
2090 	struct bmp280_data *data;
2091 	struct gpio_desc *gpiod;
2092 	unsigned int chip_id;
2093 	unsigned int i;
2094 	int ret;
2095 
2096 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2097 	if (!indio_dev)
2098 		return -ENOMEM;
2099 
2100 	data = iio_priv(indio_dev);
2101 	mutex_init(&data->lock);
2102 	data->dev = dev;
2103 
2104 	indio_dev->name = name;
2105 	indio_dev->info = &bmp280_info;
2106 	indio_dev->modes = INDIO_DIRECT_MODE;
2107 
2108 	data->chip_info = chip_info;
2109 
2110 	/* Apply initial values from chip info structure */
2111 	indio_dev->channels = chip_info->channels;
2112 	indio_dev->num_channels = chip_info->num_channels;
2113 	data->oversampling_press = chip_info->oversampling_press_default;
2114 	data->oversampling_humid = chip_info->oversampling_humid_default;
2115 	data->oversampling_temp = chip_info->oversampling_temp_default;
2116 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
2117 	data->sampling_freq = chip_info->sampling_freq_default;
2118 	data->start_up_time = chip_info->start_up_time;
2119 
2120 	/* Bring up regulators */
2121 	regulator_bulk_set_supply_names(data->supplies,
2122 					bmp280_supply_names,
2123 					BMP280_NUM_SUPPLIES);
2124 
2125 	ret = devm_regulator_bulk_get(dev,
2126 				      BMP280_NUM_SUPPLIES, data->supplies);
2127 	if (ret) {
2128 		dev_err(dev, "failed to get regulators\n");
2129 		return ret;
2130 	}
2131 
2132 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2133 	if (ret) {
2134 		dev_err(dev, "failed to enable regulators\n");
2135 		return ret;
2136 	}
2137 
2138 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
2139 				       data->supplies);
2140 	if (ret)
2141 		return ret;
2142 
2143 	/* Wait to make sure we started up properly */
2144 	usleep_range(data->start_up_time, data->start_up_time + 100);
2145 
2146 	/* Bring chip out of reset if there is an assigned GPIO line */
2147 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
2148 	/* Deassert the signal */
2149 	if (gpiod) {
2150 		dev_info(dev, "release reset\n");
2151 		gpiod_set_value(gpiod, 0);
2152 	}
2153 
2154 	data->regmap = regmap;
2155 
2156 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
2157 	if (ret < 0)
2158 		return ret;
2159 
2160 	for (i = 0; i < data->chip_info->num_chip_id; i++) {
2161 		if (chip_id == data->chip_info->chip_id[i]) {
2162 			dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name);
2163 			break;
2164 		}
2165 	}
2166 
2167 	if (i == data->chip_info->num_chip_id)
2168 		dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id);
2169 
2170 	if (data->chip_info->preinit) {
2171 		ret = data->chip_info->preinit(data);
2172 		if (ret)
2173 			return dev_err_probe(data->dev, ret,
2174 					     "error running preinit tasks\n");
2175 	}
2176 
2177 	ret = data->chip_info->chip_config(data);
2178 	if (ret < 0)
2179 		return ret;
2180 
2181 	dev_set_drvdata(dev, indio_dev);
2182 
2183 	/*
2184 	 * Some chips have calibration parameters "programmed into the devices'
2185 	 * non-volatile memory during production". Let's read them out at probe
2186 	 * time once. They will not change.
2187 	 */
2188 
2189 	if (data->chip_info->read_calib) {
2190 		ret = data->chip_info->read_calib(data);
2191 		if (ret < 0)
2192 			return dev_err_probe(data->dev, ret,
2193 					     "failed to read calibration coefficients\n");
2194 	}
2195 
2196 	/*
2197 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
2198 	 * however as it happens, the BMP085 shares the chip ID of BMP180
2199 	 * so we look for an IRQ if we have that.
2200 	 */
2201 	if (irq > 0 && (chip_id  == BMP180_CHIP_ID)) {
2202 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2203 		if (ret)
2204 			return ret;
2205 	}
2206 
2207 	/* Enable runtime PM */
2208 	pm_runtime_get_noresume(dev);
2209 	pm_runtime_set_active(dev);
2210 	pm_runtime_enable(dev);
2211 	/*
2212 	 * Set autosuspend to two orders of magnitude larger than the
2213 	 * start-up time.
2214 	 */
2215 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
2216 	pm_runtime_use_autosuspend(dev);
2217 	pm_runtime_put(dev);
2218 
2219 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
2220 	if (ret)
2221 		return ret;
2222 
2223 	return devm_iio_device_register(dev, indio_dev);
2224 }
2225 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
2226 
2227 static int bmp280_runtime_suspend(struct device *dev)
2228 {
2229 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2230 	struct bmp280_data *data = iio_priv(indio_dev);
2231 
2232 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
2233 }
2234 
2235 static int bmp280_runtime_resume(struct device *dev)
2236 {
2237 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2238 	struct bmp280_data *data = iio_priv(indio_dev);
2239 	int ret;
2240 
2241 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2242 	if (ret)
2243 		return ret;
2244 	usleep_range(data->start_up_time, data->start_up_time + 100);
2245 	return data->chip_info->chip_config(data);
2246 }
2247 
2248 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
2249 			  bmp280_runtime_resume, NULL);
2250 
2251 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
2252 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
2253 MODULE_LICENSE("GPL v2");
2254