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-bmp581-ds004.pdf
17  *
18  * Notice:
19  * The link to the bmp180 datasheet points to an outdated version missing these changes:
20  * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
21  * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
22  * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
23  */
24 
25 #define pr_fmt(fmt) "bmp280: " fmt
26 
27 #include <linux/bitops.h>
28 #include <linux/bitfield.h>
29 #include <linux/device.h>
30 #include <linux/module.h>
31 #include <linux/nvmem-provider.h>
32 #include <linux/regmap.h>
33 #include <linux/delay.h>
34 #include <linux/iio/iio.h>
35 #include <linux/iio/sysfs.h>
36 #include <linux/gpio/consumer.h>
37 #include <linux/regulator/consumer.h>
38 #include <linux/interrupt.h>
39 #include <linux/irq.h> /* For irq_get_irq_data() */
40 #include <linux/completion.h>
41 #include <linux/pm_runtime.h>
42 #include <linux/random.h>
43 
44 #include <asm/unaligned.h>
45 
46 #include "bmp280.h"
47 
48 /*
49  * These enums are used for indexing into the array of calibration
50  * coefficients for BMP180.
51  */
52 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
53 
54 enum bmp380_odr {
55 	BMP380_ODR_200HZ,
56 	BMP380_ODR_100HZ,
57 	BMP380_ODR_50HZ,
58 	BMP380_ODR_25HZ,
59 	BMP380_ODR_12_5HZ,
60 	BMP380_ODR_6_25HZ,
61 	BMP380_ODR_3_125HZ,
62 	BMP380_ODR_1_5625HZ,
63 	BMP380_ODR_0_78HZ,
64 	BMP380_ODR_0_39HZ,
65 	BMP380_ODR_0_2HZ,
66 	BMP380_ODR_0_1HZ,
67 	BMP380_ODR_0_05HZ,
68 	BMP380_ODR_0_02HZ,
69 	BMP380_ODR_0_01HZ,
70 	BMP380_ODR_0_006HZ,
71 	BMP380_ODR_0_003HZ,
72 	BMP380_ODR_0_0015HZ,
73 };
74 
75 enum bmp580_odr {
76 	BMP580_ODR_240HZ,
77 	BMP580_ODR_218HZ,
78 	BMP580_ODR_199HZ,
79 	BMP580_ODR_179HZ,
80 	BMP580_ODR_160HZ,
81 	BMP580_ODR_149HZ,
82 	BMP580_ODR_140HZ,
83 	BMP580_ODR_129HZ,
84 	BMP580_ODR_120HZ,
85 	BMP580_ODR_110HZ,
86 	BMP580_ODR_100HZ,
87 	BMP580_ODR_89HZ,
88 	BMP580_ODR_80HZ,
89 	BMP580_ODR_70HZ,
90 	BMP580_ODR_60HZ,
91 	BMP580_ODR_50HZ,
92 	BMP580_ODR_45HZ,
93 	BMP580_ODR_40HZ,
94 	BMP580_ODR_35HZ,
95 	BMP580_ODR_30HZ,
96 	BMP580_ODR_25HZ,
97 	BMP580_ODR_20HZ,
98 	BMP580_ODR_15HZ,
99 	BMP580_ODR_10HZ,
100 	BMP580_ODR_5HZ,
101 	BMP580_ODR_4HZ,
102 	BMP580_ODR_3HZ,
103 	BMP580_ODR_2HZ,
104 	BMP580_ODR_1HZ,
105 	BMP580_ODR_0_5HZ,
106 	BMP580_ODR_0_25HZ,
107 	BMP580_ODR_0_125HZ,
108 };
109 
110 /*
111  * These enums are used for indexing into the array of compensation
112  * parameters for BMP280.
113  */
114 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
115 
116 enum {
117 	/* Temperature calib indexes */
118 	BMP380_T1 = 0,
119 	BMP380_T2 = 2,
120 	BMP380_T3 = 4,
121 	/* Pressure calib indexes */
122 	BMP380_P1 = 5,
123 	BMP380_P2 = 7,
124 	BMP380_P3 = 9,
125 	BMP380_P4 = 10,
126 	BMP380_P5 = 11,
127 	BMP380_P6 = 13,
128 	BMP380_P7 = 15,
129 	BMP380_P8 = 16,
130 	BMP380_P9 = 17,
131 	BMP380_P10 = 19,
132 	BMP380_P11 = 20,
133 };
134 
135 static const struct iio_chan_spec bmp280_channels[] = {
136 	{
137 		.type = IIO_PRESSURE,
138 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
139 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
140 	},
141 	{
142 		.type = IIO_TEMP,
143 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
144 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
145 	},
146 	{
147 		.type = IIO_HUMIDITYRELATIVE,
148 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
149 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
150 	},
151 };
152 
153 static const struct iio_chan_spec bmp380_channels[] = {
154 	{
155 		.type = IIO_PRESSURE,
156 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
157 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
158 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
159 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
160 	},
161 	{
162 		.type = IIO_TEMP,
163 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
164 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
165 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
166 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
167 	},
168 	{
169 		.type = IIO_HUMIDITYRELATIVE,
170 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
171 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
172 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
173 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
174 	},
175 };
176 
177 static int bmp280_read_calib(struct bmp280_data *data)
178 {
179 	struct bmp280_calib *calib = &data->calib.bmp280;
180 	int ret;
181 
182 	/* Read temperature and pressure calibration values. */
183 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
184 			       data->bmp280_cal_buf,
185 			       sizeof(data->bmp280_cal_buf));
186 	if (ret < 0) {
187 		dev_err(data->dev,
188 			"failed to read calibration parameters\n");
189 		return ret;
190 	}
191 
192 	/* Toss calibration data into the entropy pool */
193 	add_device_randomness(data->bmp280_cal_buf,
194 			      sizeof(data->bmp280_cal_buf));
195 
196 	/* Parse temperature calibration values. */
197 	calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
198 	calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
199 	calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
200 
201 	/* Parse pressure calibration values. */
202 	calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
203 	calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
204 	calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
205 	calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
206 	calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
207 	calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
208 	calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
209 	calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
210 	calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
211 
212 	return 0;
213 }
214 
215 static int bme280_read_calib(struct bmp280_data *data)
216 {
217 	struct bmp280_calib *calib = &data->calib.bmp280;
218 	struct device *dev = data->dev;
219 	unsigned int tmp;
220 	int ret;
221 
222 	/* Load shared calibration params with bmp280 first */
223 	ret = bmp280_read_calib(data);
224 	if  (ret < 0) {
225 		dev_err(dev, "failed to read calibration parameters\n");
226 		return ret;
227 	}
228 
229 	/*
230 	 * Read humidity calibration values.
231 	 * Due to some odd register addressing we cannot just
232 	 * do a big bulk read. Instead, we have to read each Hx
233 	 * value separately and sometimes do some bit shifting...
234 	 * Humidity data is only available on BME280.
235 	 */
236 
237 	ret = regmap_read(data->regmap, BME280_REG_COMP_H1, &tmp);
238 	if (ret < 0) {
239 		dev_err(dev, "failed to read H1 comp value\n");
240 		return ret;
241 	}
242 	calib->H1 = tmp;
243 
244 	ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H2,
245 			       &data->le16, sizeof(data->le16));
246 	if (ret < 0) {
247 		dev_err(dev, "failed to read H2 comp value\n");
248 		return ret;
249 	}
250 	calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
251 
252 	ret = regmap_read(data->regmap, BME280_REG_COMP_H3, &tmp);
253 	if (ret < 0) {
254 		dev_err(dev, "failed to read H3 comp value\n");
255 		return ret;
256 	}
257 	calib->H3 = tmp;
258 
259 	ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H4,
260 			       &data->be16, sizeof(data->be16));
261 	if (ret < 0) {
262 		dev_err(dev, "failed to read H4 comp value\n");
263 		return ret;
264 	}
265 	calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
266 				  (be16_to_cpu(data->be16) & 0xf), 11);
267 
268 	ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H5,
269 			       &data->le16, sizeof(data->le16));
270 	if (ret < 0) {
271 		dev_err(dev, "failed to read H5 comp value\n");
272 		return ret;
273 	}
274 	calib->H5 = sign_extend32(FIELD_GET(BME280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
275 
276 	ret = regmap_read(data->regmap, BME280_REG_COMP_H6, &tmp);
277 	if (ret < 0) {
278 		dev_err(dev, "failed to read H6 comp value\n");
279 		return ret;
280 	}
281 	calib->H6 = sign_extend32(tmp, 7);
282 
283 	return 0;
284 }
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 bme280_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 bme280_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, BME280_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 = bme280_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 bme280_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 = bme280_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, "failed to write ctrl_meas register\n");
781 		return ret;
782 	}
783 
784 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
785 				 BMP280_FILTER_MASK,
786 				 BMP280_FILTER_4X);
787 	if (ret < 0) {
788 		dev_err(data->dev, "failed to write config register\n");
789 		return ret;
790 	}
791 
792 	return ret;
793 }
794 
795 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
796 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID };
797 
798 const struct bmp280_chip_info bmp280_chip_info = {
799 	.id_reg = BMP280_REG_ID,
800 	.chip_id = bmp280_chip_ids,
801 	.num_chip_id = ARRAY_SIZE(bmp280_chip_ids),
802 	.regmap_config = &bmp280_regmap_config,
803 	.start_up_time = 2000,
804 	.channels = bmp280_channels,
805 	.num_channels = 2,
806 
807 	.oversampling_temp_avail = bmp280_oversampling_avail,
808 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
809 	/*
810 	 * Oversampling config values on BMx280 have one additional setting
811 	 * that other generations of the family don't:
812 	 * The value 0 means the measurement is bypassed instead of
813 	 * oversampling set to x1.
814 	 *
815 	 * To account for this difference, and preserve the same common
816 	 * config logic, this is handled later on chip_config callback
817 	 * incrementing one unit the oversampling setting.
818 	 */
819 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
820 
821 	.oversampling_press_avail = bmp280_oversampling_avail,
822 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
823 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
824 
825 	.chip_config = bmp280_chip_config,
826 	.read_temp = bmp280_read_temp,
827 	.read_press = bmp280_read_press,
828 	.read_calib = bmp280_read_calib,
829 };
830 EXPORT_SYMBOL_NS(bmp280_chip_info, IIO_BMP280);
831 
832 static int bme280_chip_config(struct bmp280_data *data)
833 {
834 	u8 osrs = FIELD_PREP(BME280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
835 	int ret;
836 
837 	/*
838 	 * Oversampling of humidity must be set before oversampling of
839 	 * temperature/pressure is set to become effective.
840 	 */
841 	ret = regmap_update_bits(data->regmap, BME280_REG_CTRL_HUMIDITY,
842 				 BME280_OSRS_HUMIDITY_MASK, osrs);
843 	if (ret < 0)
844 		return ret;
845 
846 	return bmp280_chip_config(data);
847 }
848 
849 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID };
850 
851 const struct bmp280_chip_info bme280_chip_info = {
852 	.id_reg = BMP280_REG_ID,
853 	.chip_id = bme280_chip_ids,
854 	.num_chip_id = ARRAY_SIZE(bme280_chip_ids),
855 	.regmap_config = &bme280_regmap_config,
856 	.start_up_time = 2000,
857 	.channels = bmp280_channels,
858 	.num_channels = 3,
859 
860 	.oversampling_temp_avail = bmp280_oversampling_avail,
861 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
862 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
863 
864 	.oversampling_press_avail = bmp280_oversampling_avail,
865 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
866 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
867 
868 	.oversampling_humid_avail = bmp280_oversampling_avail,
869 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
870 	.oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1,
871 
872 	.chip_config = bme280_chip_config,
873 	.read_temp = bmp280_read_temp,
874 	.read_press = bmp280_read_press,
875 	.read_humid = bme280_read_humid,
876 	.read_calib = bme280_read_calib,
877 };
878 EXPORT_SYMBOL_NS(bme280_chip_info, IIO_BMP280);
879 
880 /*
881  * Helper function to send a command to BMP3XX sensors.
882  *
883  * Sensor processes commands written to the CMD register and signals
884  * execution result through "cmd_rdy" and "cmd_error" flags available on
885  * STATUS and ERROR registers.
886  */
887 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
888 {
889 	unsigned int reg;
890 	int ret;
891 
892 	/* Check if device is ready to process a command */
893 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
894 	if (ret) {
895 		dev_err(data->dev, "failed to read error register\n");
896 		return ret;
897 	}
898 	if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
899 		dev_err(data->dev, "device is not ready to accept commands\n");
900 		return -EBUSY;
901 	}
902 
903 	/* Send command to process */
904 	ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
905 	if (ret) {
906 		dev_err(data->dev, "failed to send command to device\n");
907 		return ret;
908 	}
909 	/* Wait for 2ms for command to be processed */
910 	usleep_range(data->start_up_time, data->start_up_time + 100);
911 	/* Check for command processing error */
912 	ret = regmap_read(data->regmap, BMP380_REG_ERROR, &reg);
913 	if (ret) {
914 		dev_err(data->dev, "error reading ERROR reg\n");
915 		return ret;
916 	}
917 	if (reg & BMP380_ERR_CMD_MASK) {
918 		dev_err(data->dev, "error processing command 0x%X\n", cmd);
919 		return -EINVAL;
920 	}
921 
922 	return 0;
923 }
924 
925 /*
926  * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value
927  * of "5123" equals 51.2º C. t_fine carries fine temperature as global value.
928  *
929  * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
930  * https://github.com/BoschSensortec/BMP3-Sensor-API.
931  */
932 static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
933 {
934 	s64 var1, var2, var3, var4, var5, var6, comp_temp;
935 	struct bmp380_calib *calib = &data->calib.bmp380;
936 
937 	var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
938 	var2 = var1 * ((s64) calib->T2);
939 	var3 = var1 * var1;
940 	var4 = var3 * ((s64) calib->T3);
941 	var5 = (var2 << 18) + var4;
942 	var6 = var5 >> 32;
943 	data->t_fine = (s32) var6;
944 	comp_temp = (var6 * 25) >> 14;
945 
946 	comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
947 	return (s32) comp_temp;
948 }
949 
950 /*
951  * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
952  * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
953  *
954  * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
955  * https://github.com/BoschSensortec/BMP3-Sensor-API.
956  */
957 static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press)
958 {
959 	s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
960 	struct bmp380_calib *calib = &data->calib.bmp380;
961 	u32 comp_press;
962 
963 	var1 = (s64)data->t_fine * (s64)data->t_fine;
964 	var2 = var1 >> 6;
965 	var3 = (var2 * ((s64) data->t_fine)) >> 8;
966 	var4 = ((s64)calib->P8 * var3) >> 5;
967 	var5 = ((s64)calib->P7 * var1) << 4;
968 	var6 = ((s64)calib->P6 * (s64)data->t_fine) << 22;
969 	offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
970 	var2 = ((s64)calib->P4 * var3) >> 5;
971 	var4 = ((s64)calib->P3 * var1) << 2;
972 	var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
973 	       ((s64)data->t_fine << 21);
974 	sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
975 			var2 + var4 + var5;
976 	var1 = (sensitivity >> 24) * (s64)adc_press;
977 	var2 = (s64)calib->P10 * (s64)data->t_fine;
978 	var3 = var2 + ((s64)calib->P9 << 16);
979 	var4 = (var3 * (s64)adc_press) >> 13;
980 
981 	/*
982 	 * Dividing by 10 followed by multiplying by 10 to avoid
983 	 * possible overflow caused by (uncomp_data->pressure * partial_data4).
984 	 */
985 	var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
986 	var5 *= 10;
987 	var6 = (s64)adc_press * (s64)adc_press;
988 	var2 = ((s64)calib->P11 * var6) >> 16;
989 	var3 = (var2 * (s64)adc_press) >> 7;
990 	var4 = (offset >> 2) + var1 + var5 + var3;
991 	comp_press = ((u64)var4 * 25) >> 40;
992 
993 	comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
994 	return comp_press;
995 }
996 
997 static int bmp380_read_temp(struct bmp280_data *data, int *val, int *val2)
998 {
999 	s32 comp_temp;
1000 	u32 adc_temp;
1001 	int ret;
1002 
1003 	ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1004 			       data->buf, sizeof(data->buf));
1005 	if (ret) {
1006 		dev_err(data->dev, "failed to read temperature\n");
1007 		return ret;
1008 	}
1009 
1010 	adc_temp = get_unaligned_le24(data->buf);
1011 	if (adc_temp == BMP380_TEMP_SKIPPED) {
1012 		dev_err(data->dev, "reading temperature skipped\n");
1013 		return -EIO;
1014 	}
1015 	comp_temp = bmp380_compensate_temp(data, adc_temp);
1016 
1017 	/*
1018 	 * Val might be NULL if we're called by the read_press routine,
1019 	 * who only cares about the carry over t_fine value.
1020 	 */
1021 	if (val) {
1022 		/* IIO reports temperatures in milli Celsius */
1023 		*val = comp_temp * 10;
1024 		return IIO_VAL_INT;
1025 	}
1026 
1027 	return 0;
1028 }
1029 
1030 static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
1031 {
1032 	s32 comp_press;
1033 	u32 adc_press;
1034 	int ret;
1035 
1036 	/* Read and compensate for temperature so we get a reading of t_fine */
1037 	ret = bmp380_read_temp(data, NULL, NULL);
1038 	if (ret)
1039 		return ret;
1040 
1041 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1042 			       data->buf, sizeof(data->buf));
1043 	if (ret) {
1044 		dev_err(data->dev, "failed to read pressure\n");
1045 		return ret;
1046 	}
1047 
1048 	adc_press = get_unaligned_le24(data->buf);
1049 	if (adc_press == BMP380_PRESS_SKIPPED) {
1050 		dev_err(data->dev, "reading pressure skipped\n");
1051 		return -EIO;
1052 	}
1053 	comp_press = bmp380_compensate_press(data, adc_press);
1054 
1055 	*val = comp_press;
1056 	/* Compensated pressure is in cPa (centipascals) */
1057 	*val2 = 100000;
1058 
1059 	return IIO_VAL_FRACTIONAL;
1060 }
1061 
1062 static int bmp380_read_calib(struct bmp280_data *data)
1063 {
1064 	struct bmp380_calib *calib = &data->calib.bmp380;
1065 	int ret;
1066 
1067 	/* Read temperature and pressure calibration data */
1068 	ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
1069 			       data->bmp380_cal_buf,
1070 			       sizeof(data->bmp380_cal_buf));
1071 	if (ret) {
1072 		dev_err(data->dev,
1073 			"failed to read temperature calibration parameters\n");
1074 		return ret;
1075 	}
1076 
1077 	/* Toss the temperature calibration data into the entropy pool */
1078 	add_device_randomness(data->bmp380_cal_buf,
1079 			      sizeof(data->bmp380_cal_buf));
1080 
1081 	/* Parse calibration values */
1082 	calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
1083 	calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
1084 	calib->T3 = data->bmp380_cal_buf[BMP380_T3];
1085 	calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
1086 	calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
1087 	calib->P3 = data->bmp380_cal_buf[BMP380_P3];
1088 	calib->P4 = data->bmp380_cal_buf[BMP380_P4];
1089 	calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
1090 	calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
1091 	calib->P7 = data->bmp380_cal_buf[BMP380_P7];
1092 	calib->P8 = data->bmp380_cal_buf[BMP380_P8];
1093 	calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
1094 	calib->P10 = data->bmp380_cal_buf[BMP380_P10];
1095 	calib->P11 = data->bmp380_cal_buf[BMP380_P11];
1096 
1097 	return 0;
1098 }
1099 
1100 static const int bmp380_odr_table[][2] = {
1101 	[BMP380_ODR_200HZ]	= {200, 0},
1102 	[BMP380_ODR_100HZ]	= {100, 0},
1103 	[BMP380_ODR_50HZ]	= {50, 0},
1104 	[BMP380_ODR_25HZ]	= {25, 0},
1105 	[BMP380_ODR_12_5HZ]	= {12, 500000},
1106 	[BMP380_ODR_6_25HZ]	= {6, 250000},
1107 	[BMP380_ODR_3_125HZ]	= {3, 125000},
1108 	[BMP380_ODR_1_5625HZ]	= {1, 562500},
1109 	[BMP380_ODR_0_78HZ]	= {0, 781250},
1110 	[BMP380_ODR_0_39HZ]	= {0, 390625},
1111 	[BMP380_ODR_0_2HZ]	= {0, 195313},
1112 	[BMP380_ODR_0_1HZ]	= {0, 97656},
1113 	[BMP380_ODR_0_05HZ]	= {0, 48828},
1114 	[BMP380_ODR_0_02HZ]	= {0, 24414},
1115 	[BMP380_ODR_0_01HZ]	= {0, 12207},
1116 	[BMP380_ODR_0_006HZ]	= {0, 6104},
1117 	[BMP380_ODR_0_003HZ]	= {0, 3052},
1118 	[BMP380_ODR_0_0015HZ]	= {0, 1526},
1119 };
1120 
1121 static int bmp380_preinit(struct bmp280_data *data)
1122 {
1123 	/* BMP3xx requires soft-reset as part of initialization */
1124 	return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1125 }
1126 
1127 static int bmp380_chip_config(struct bmp280_data *data)
1128 {
1129 	bool change = false, aux;
1130 	unsigned int tmp;
1131 	u8 osrs;
1132 	int ret;
1133 
1134 	/* Configure power control register */
1135 	ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1136 				 BMP380_CTRL_SENSORS_MASK,
1137 				 BMP380_CTRL_SENSORS_PRESS_EN |
1138 				 BMP380_CTRL_SENSORS_TEMP_EN);
1139 	if (ret) {
1140 		dev_err(data->dev,
1141 			"failed to write operation control register\n");
1142 		return ret;
1143 	}
1144 
1145 	/* Configure oversampling */
1146 	osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1147 	       FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1148 
1149 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1150 				       BMP380_OSRS_TEMP_MASK |
1151 				       BMP380_OSRS_PRESS_MASK,
1152 				       osrs, &aux);
1153 	if (ret) {
1154 		dev_err(data->dev, "failed to write oversampling register\n");
1155 		return ret;
1156 	}
1157 	change = change || aux;
1158 
1159 	/* Configure output data rate */
1160 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1161 				       BMP380_ODRS_MASK, data->sampling_freq,
1162 				       &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
1182 		 * measurement cycle. If the sampling frequency is too low, it's
1183 		 * faster to reset the measurement loop than wait until the next
1184 		 * measurement is due.
1185 		 *
1186 		 * Resets sensor measurement loop toggling between sleep and
1187 		 * normal operating modes.
1188 		 */
1189 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1190 					BMP380_MODE_MASK,
1191 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
1192 		if (ret) {
1193 			dev_err(data->dev, "failed to set sleep mode\n");
1194 			return ret;
1195 		}
1196 		usleep_range(2000, 2500);
1197 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1198 					BMP380_MODE_MASK,
1199 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
1200 		if (ret) {
1201 			dev_err(data->dev, "failed to set normal mode\n");
1202 			return ret;
1203 		}
1204 		/*
1205 		 * Waits for measurement before checking configuration error
1206 		 * flag. Selected longest measurement time, calculated from
1207 		 * formula in datasheet section 3.9.2 with an offset of ~+15%
1208 		 * as it seen as well in table 3.9.1.
1209 		 */
1210 		msleep(150);
1211 
1212 		/* Check config error flag */
1213 		ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1214 		if (ret) {
1215 			dev_err(data->dev, "failed to read error register\n");
1216 			return ret;
1217 		}
1218 		if (tmp & BMP380_ERR_CONF_MASK) {
1219 			dev_warn(data->dev,
1220 				 "sensor flagged configuration as incompatible\n");
1221 			return -EINVAL;
1222 		}
1223 	}
1224 
1225 	return 0;
1226 }
1227 
1228 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
1229 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
1230 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID };
1231 
1232 const struct bmp280_chip_info bmp380_chip_info = {
1233 	.id_reg = BMP380_REG_ID,
1234 	.chip_id = bmp380_chip_ids,
1235 	.num_chip_id = ARRAY_SIZE(bmp380_chip_ids),
1236 	.regmap_config = &bmp380_regmap_config,
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,
1321 			   BMP580_CMD_NVM_OP_SEQ_0);
1322 	if (ret) {
1323 		dev_err(data->dev,
1324 			"failed to send nvm operation's first sequence\n");
1325 		return ret;
1326 	}
1327 	if (is_write) {
1328 		/* Send NVM write sequence */
1329 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1330 				   BMP580_CMD_NVM_WRITE_SEQ_1);
1331 		if (ret) {
1332 			dev_err(data->dev,
1333 				"failed to send nvm write sequence\n");
1334 			return ret;
1335 		}
1336 		/* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1337 		poll = 2000;
1338 		timeout = 12000;
1339 	} else {
1340 		/* Send NVM read sequence */
1341 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1342 				   BMP580_CMD_NVM_READ_SEQ_1);
1343 		if (ret) {
1344 			dev_err(data->dev,
1345 				"failed to send nvm read sequence\n");
1346 			return ret;
1347 		}
1348 		/* Datasheet says on 4.8.1.1 it takes approximately 200us */
1349 		poll = 50;
1350 		timeout = 400;
1351 	}
1352 	if (ret) {
1353 		dev_err(data->dev, "failed to write command sequence\n");
1354 		return -EIO;
1355 	}
1356 
1357 	/* Wait until NVM is ready again */
1358 	ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
1359 				       (reg & BMP580_STATUS_NVM_RDY_MASK),
1360 				       poll, timeout);
1361 	if (ret) {
1362 		dev_err(data->dev, "error checking nvm operation status\n");
1363 		return ret;
1364 	}
1365 
1366 	/* Check NVM error flags */
1367 	if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
1368 		dev_err(data->dev, "error processing nvm operation\n");
1369 		return -EIO;
1370 	}
1371 
1372 	return 0;
1373 }
1374 
1375 /*
1376  * Contrary to previous sensors families, compensation algorithm is builtin.
1377  * We are only required to read the register raw data and adapt the ranges
1378  * for what is expected on IIO ABI.
1379  */
1380 
1381 static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
1382 {
1383 	s32 raw_temp;
1384 	int ret;
1385 
1386 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf,
1387 			       sizeof(data->buf));
1388 	if (ret) {
1389 		dev_err(data->dev, "failed to read temperature\n");
1390 		return ret;
1391 	}
1392 
1393 	raw_temp = get_unaligned_le24(data->buf);
1394 	if (raw_temp == BMP580_TEMP_SKIPPED) {
1395 		dev_err(data->dev, "reading temperature skipped\n");
1396 		return -EIO;
1397 	}
1398 
1399 	/*
1400 	 * Temperature is returned in Celsius degrees in fractional
1401 	 * form down 2^16. We rescale by x1000 to return millidegrees
1402 	 * Celsius to respect IIO ABI.
1403 	 */
1404 	raw_temp = sign_extend32(raw_temp, 23);
1405 	*val = ((s64)raw_temp * 1000) / (1 << 16);
1406 	return IIO_VAL_INT;
1407 }
1408 
1409 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)
1410 {
1411 	u32 raw_press;
1412 	int ret;
1413 
1414 	ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf,
1415 			       sizeof(data->buf));
1416 	if (ret) {
1417 		dev_err(data->dev, "failed to read pressure\n");
1418 		return ret;
1419 	}
1420 
1421 	raw_press = get_unaligned_le24(data->buf);
1422 	if (raw_press == BMP580_PRESS_SKIPPED) {
1423 		dev_err(data->dev, "reading pressure skipped\n");
1424 		return -EIO;
1425 	}
1426 	/*
1427 	 * Pressure is returned in Pascals in fractional form down 2^16.
1428 	 * We rescale /1000 to convert to kilopascal to respect IIO ABI.
1429 	 */
1430 	*val = raw_press;
1431 	*val2 = 64000; /* 2^6 * 1000 */
1432 	return IIO_VAL_FRACTIONAL;
1433 }
1434 
1435 static const int bmp580_odr_table[][2] = {
1436 	[BMP580_ODR_240HZ] =	{240, 0},
1437 	[BMP580_ODR_218HZ] =	{218, 0},
1438 	[BMP580_ODR_199HZ] =	{199, 0},
1439 	[BMP580_ODR_179HZ] =	{179, 0},
1440 	[BMP580_ODR_160HZ] =	{160, 0},
1441 	[BMP580_ODR_149HZ] =	{149, 0},
1442 	[BMP580_ODR_140HZ] =	{140, 0},
1443 	[BMP580_ODR_129HZ] =	{129, 0},
1444 	[BMP580_ODR_120HZ] =	{120, 0},
1445 	[BMP580_ODR_110HZ] =	{110, 0},
1446 	[BMP580_ODR_100HZ] =	{100, 0},
1447 	[BMP580_ODR_89HZ] =	{89, 0},
1448 	[BMP580_ODR_80HZ] =	{80, 0},
1449 	[BMP580_ODR_70HZ] =	{70, 0},
1450 	[BMP580_ODR_60HZ] =	{60, 0},
1451 	[BMP580_ODR_50HZ] =	{50, 0},
1452 	[BMP580_ODR_45HZ] =	{45, 0},
1453 	[BMP580_ODR_40HZ] =	{40, 0},
1454 	[BMP580_ODR_35HZ] =	{35, 0},
1455 	[BMP580_ODR_30HZ] =	{30, 0},
1456 	[BMP580_ODR_25HZ] =	{25, 0},
1457 	[BMP580_ODR_20HZ] =	{20, 0},
1458 	[BMP580_ODR_15HZ] =	{15, 0},
1459 	[BMP580_ODR_10HZ] =	{10, 0},
1460 	[BMP580_ODR_5HZ] =	{5, 0},
1461 	[BMP580_ODR_4HZ] =	{4, 0},
1462 	[BMP580_ODR_3HZ] =	{3, 0},
1463 	[BMP580_ODR_2HZ] =	{2, 0},
1464 	[BMP580_ODR_1HZ] =	{1, 0},
1465 	[BMP580_ODR_0_5HZ] =	{0, 500000},
1466 	[BMP580_ODR_0_25HZ] =	{0, 250000},
1467 	[BMP580_ODR_0_125HZ] =	{0, 125000},
1468 };
1469 
1470 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1471 
1472 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1473 			     size_t bytes)
1474 {
1475 	struct bmp280_data *data = priv;
1476 	u16 *dst = val;
1477 	int ret, addr;
1478 
1479 	pm_runtime_get_sync(data->dev);
1480 	mutex_lock(&data->lock);
1481 
1482 	/* Set sensor in standby mode */
1483 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1484 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1485 				 BMP580_ODR_DEEPSLEEP_DIS |
1486 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1487 	if (ret) {
1488 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1489 		goto exit;
1490 	}
1491 	/* Wait standby transition time */
1492 	usleep_range(2500, 3000);
1493 
1494 	while (bytes >= sizeof(*dst)) {
1495 		addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1496 
1497 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1498 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1499 		if (ret) {
1500 			dev_err(data->dev, "error writing nvm address\n");
1501 			goto exit;
1502 		}
1503 
1504 		ret = bmp580_nvm_operation(data, false);
1505 		if (ret)
1506 			goto exit;
1507 
1508 		ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB,
1509 				       &data->le16, sizeof(data->le16));
1510 		if (ret) {
1511 			dev_err(data->dev, "error reading nvm data regs\n");
1512 			goto exit;
1513 		}
1514 
1515 		*dst++ = le16_to_cpu(data->le16);
1516 		bytes -= sizeof(*dst);
1517 		offset += sizeof(*dst);
1518 	}
1519 exit:
1520 	/* Restore chip config */
1521 	data->chip_info->chip_config(data);
1522 	mutex_unlock(&data->lock);
1523 	pm_runtime_mark_last_busy(data->dev);
1524 	pm_runtime_put_autosuspend(data->dev);
1525 	return ret;
1526 }
1527 
1528 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
1529 			      size_t bytes)
1530 {
1531 	struct bmp280_data *data = priv;
1532 	u16 *buf = val;
1533 	int ret, addr;
1534 
1535 	pm_runtime_get_sync(data->dev);
1536 	mutex_lock(&data->lock);
1537 
1538 	/* Set sensor in standby mode */
1539 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1540 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1541 				 BMP580_ODR_DEEPSLEEP_DIS |
1542 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1543 	if (ret) {
1544 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1545 		goto exit;
1546 	}
1547 	/* Wait standby transition time */
1548 	usleep_range(2500, 3000);
1549 
1550 	while (bytes >= sizeof(*buf)) {
1551 		addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1552 
1553 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1554 				   BMP580_NVM_PROG_EN |
1555 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1556 		if (ret) {
1557 			dev_err(data->dev, "error writing nvm address\n");
1558 			goto exit;
1559 		}
1560 		data->le16 = cpu_to_le16(*buf++);
1561 
1562 		ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB,
1563 					&data->le16, sizeof(data->le16));
1564 		if (ret) {
1565 			dev_err(data->dev, "error writing LSB NVM data regs\n");
1566 			goto exit;
1567 		}
1568 
1569 		ret = bmp580_nvm_operation(data, true);
1570 		if (ret)
1571 			goto exit;
1572 
1573 		/* Disable programming mode bit */
1574 		ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
1575 					 BMP580_NVM_PROG_EN, 0);
1576 		if (ret) {
1577 			dev_err(data->dev, "error resetting nvm write\n");
1578 			goto exit;
1579 		}
1580 
1581 		bytes -= sizeof(*buf);
1582 		offset += sizeof(*buf);
1583 	}
1584 exit:
1585 	/* Restore chip config */
1586 	data->chip_info->chip_config(data);
1587 	mutex_unlock(&data->lock);
1588 	pm_runtime_mark_last_busy(data->dev);
1589 	pm_runtime_put_autosuspend(data->dev);
1590 	return ret;
1591 }
1592 
1593 static int bmp580_preinit(struct bmp280_data *data)
1594 {
1595 	struct nvmem_config config = {
1596 		.dev = data->dev,
1597 		.priv = data,
1598 		.name = "bmp580_nvmem",
1599 		.word_size = sizeof(u16),
1600 		.stride = sizeof(u16),
1601 		.size = 3 * sizeof(u16),
1602 		.reg_read = bmp580_nvmem_read,
1603 		.reg_write = bmp580_nvmem_write,
1604 	};
1605 	unsigned int reg;
1606 	int ret;
1607 
1608 	/* Issue soft-reset command */
1609 	ret = bmp580_soft_reset(data);
1610 	if (ret)
1611 		return ret;
1612 
1613 	/* Post powerup sequence */
1614 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1615 	if (ret)
1616 		return ret;
1617 
1618 	/* Print warn message if we don't know the chip id */
1619 	if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
1620 		dev_warn(data->dev, "preinit: unexpected chip_id\n");
1621 
1622 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1623 	if (ret)
1624 		return ret;
1625 
1626 	/* Check nvm status */
1627 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
1628 		dev_err(data->dev, "preinit: nvm error on powerup sequence\n");
1629 		return -EIO;
1630 	}
1631 
1632 	/* Register nvmem device */
1633 	return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
1634 }
1635 
1636 static int bmp580_chip_config(struct bmp280_data *data)
1637 {
1638 	bool change = false, aux;
1639 	unsigned int tmp;
1640 	u8 reg_val;
1641 	int ret;
1642 
1643 	/* Sets sensor in standby mode */
1644 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1645 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1646 				 BMP580_ODR_DEEPSLEEP_DIS |
1647 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1648 	if (ret) {
1649 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1650 		return ret;
1651 	}
1652 	/* From datasheet's table 4: electrical characteristics */
1653 	usleep_range(2500, 3000);
1654 
1655 	/* Set default DSP mode settings */
1656 	reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
1657 		  BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
1658 
1659 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
1660 				 BMP580_DSP_COMP_MASK |
1661 				 BMP580_DSP_SHDW_IIR_TEMP_EN |
1662 				 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
1663 
1664 	/* Configure oversampling */
1665 	reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
1666 		  FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
1667 		  BMP580_OSR_PRESS_EN;
1668 
1669 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
1670 				       BMP580_OSR_TEMP_MASK |
1671 				       BMP580_OSR_PRESS_MASK |
1672 				       BMP580_OSR_PRESS_EN,
1673 				       reg_val, &aux);
1674 	if (ret) {
1675 		dev_err(data->dev, "failed to write oversampling register\n");
1676 		return ret;
1677 	}
1678 	change = change || aux;
1679 
1680 	/* Configure output data rate */
1681 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
1682 				       FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
1683 				       &aux);
1684 	if (ret) {
1685 		dev_err(data->dev, "failed to write ODR configuration register\n");
1686 		return ret;
1687 	}
1688 	change = change || aux;
1689 
1690 	/* Set filter data */
1691 	reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
1692 		  FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
1693 
1694 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
1695 				       BMP580_DSP_IIR_PRESS_MASK |
1696 				       BMP580_DSP_IIR_TEMP_MASK,
1697 				       reg_val, &aux);
1698 	if (ret) {
1699 		dev_err(data->dev, "failed to write config register\n");
1700 		return ret;
1701 	}
1702 	change = change || aux;
1703 
1704 	/* Restore sensor to normal operation mode */
1705 	ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1706 				BMP580_MODE_MASK,
1707 				FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
1708 	if (ret) {
1709 		dev_err(data->dev, "failed to set normal mode\n");
1710 		return ret;
1711 	}
1712 	/* From datasheet's table 4: electrical characteristics */
1713 	usleep_range(3000, 3500);
1714 
1715 	if (change) {
1716 		/*
1717 		 * Check if ODR and OSR settings are valid or we are
1718 		 * operating in a degraded mode.
1719 		 */
1720 		ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
1721 		if (ret) {
1722 			dev_err(data->dev,
1723 				"error reading effective OSR register\n");
1724 			return ret;
1725 		}
1726 		if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
1727 			dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
1728 			/* Set current OSR settings from data on effective OSR */
1729 			data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
1730 			data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
1731 			return -EINVAL;
1732 		}
1733 	}
1734 
1735 	return 0;
1736 }
1737 
1738 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
1739 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };
1740 
1741 const struct bmp280_chip_info bmp580_chip_info = {
1742 	.id_reg = BMP580_REG_CHIP_ID,
1743 	.chip_id = bmp580_chip_ids,
1744 	.num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
1745 	.regmap_config = &bmp580_regmap_config,
1746 	.start_up_time = 2000,
1747 	.channels = bmp380_channels,
1748 	.num_channels = 2,
1749 
1750 	.oversampling_temp_avail = bmp580_oversampling_avail,
1751 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1752 	.oversampling_temp_default = ilog2(1),
1753 
1754 	.oversampling_press_avail = bmp580_oversampling_avail,
1755 	.num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1756 	.oversampling_press_default = ilog2(4),
1757 
1758 	.sampling_freq_avail = bmp580_odr_table,
1759 	.num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
1760 	.sampling_freq_default = BMP580_ODR_50HZ,
1761 
1762 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1763 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1764 	.iir_filter_coeff_default = 2,
1765 
1766 	.chip_config = bmp580_chip_config,
1767 	.read_temp = bmp580_read_temp,
1768 	.read_press = bmp580_read_press,
1769 	.preinit = bmp580_preinit,
1770 };
1771 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
1772 
1773 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
1774 {
1775 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
1776 	unsigned int delay_us;
1777 	unsigned int ctrl;
1778 	int ret;
1779 
1780 	if (data->use_eoc)
1781 		reinit_completion(&data->done);
1782 
1783 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
1784 	if (ret)
1785 		return ret;
1786 
1787 	if (data->use_eoc) {
1788 		/*
1789 		 * If we have a completion interrupt, use it, wait up to
1790 		 * 100ms. The longest conversion time listed is 76.5 ms for
1791 		 * advanced resolution mode.
1792 		 */
1793 		ret = wait_for_completion_timeout(&data->done,
1794 						  1 + msecs_to_jiffies(100));
1795 		if (!ret)
1796 			dev_err(data->dev, "timeout waiting for completion\n");
1797 	} else {
1798 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
1799 			delay_us = 4500;
1800 		else
1801 			delay_us =
1802 				conversion_time_max[data->oversampling_press];
1803 
1804 		usleep_range(delay_us, delay_us + 1000);
1805 	}
1806 
1807 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
1808 	if (ret)
1809 		return ret;
1810 
1811 	/* The value of this bit reset to "0" after conversion is complete */
1812 	if (ctrl & BMP180_MEAS_SCO)
1813 		return -EIO;
1814 
1815 	return 0;
1816 }
1817 
1818 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
1819 {
1820 	int ret;
1821 
1822 	ret = bmp180_measure(data,
1823 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
1824 			     BMP180_MEAS_SCO);
1825 	if (ret)
1826 		return ret;
1827 
1828 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1829 			       &data->be16, sizeof(data->be16));
1830 	if (ret)
1831 		return ret;
1832 
1833 	*val = be16_to_cpu(data->be16);
1834 
1835 	return 0;
1836 }
1837 
1838 static int bmp180_read_calib(struct bmp280_data *data)
1839 {
1840 	struct bmp180_calib *calib = &data->calib.bmp180;
1841 	int ret;
1842 	int i;
1843 
1844 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1845 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1846 
1847 	if (ret < 0)
1848 		return ret;
1849 
1850 	/* None of the words has the value 0 or 0xFFFF */
1851 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
1852 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
1853 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
1854 			return -EIO;
1855 	}
1856 
1857 	/* Toss the calibration data into the entropy pool */
1858 	add_device_randomness(data->bmp180_cal_buf,
1859 			      sizeof(data->bmp180_cal_buf));
1860 
1861 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
1862 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
1863 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
1864 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
1865 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
1866 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
1867 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
1868 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
1869 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
1870 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
1871 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
1872 
1873 	return 0;
1874 }
1875 
1876 /*
1877  * Returns temperature in DegC, resolution is 0.1 DegC.
1878  * t_fine carries fine temperature as global value.
1879  *
1880  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1881  */
1882 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
1883 {
1884 	struct bmp180_calib *calib = &data->calib.bmp180;
1885 	s32 x1, x2;
1886 
1887 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1888 	x2 = (calib->MC << 11) / (x1 + calib->MD);
1889 	data->t_fine = x1 + x2;
1890 
1891 	return (data->t_fine + 8) >> 4;
1892 }
1893 
1894 static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2)
1895 {
1896 	s32 adc_temp, comp_temp;
1897 	int ret;
1898 
1899 	ret = bmp180_read_adc_temp(data, &adc_temp);
1900 	if (ret)
1901 		return ret;
1902 
1903 	comp_temp = bmp180_compensate_temp(data, adc_temp);
1904 
1905 	/*
1906 	 * val might be NULL if we're called by the read_press routine,
1907 	 * who only cares about the carry over t_fine value.
1908 	 */
1909 	if (val) {
1910 		*val = comp_temp * 100;
1911 		return IIO_VAL_INT;
1912 	}
1913 
1914 	return 0;
1915 }
1916 
1917 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
1918 {
1919 	u8 oss = data->oversampling_press;
1920 	int ret;
1921 
1922 	ret = bmp180_measure(data,
1923 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
1924 			     FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
1925 			     BMP180_MEAS_SCO);
1926 	if (ret)
1927 		return ret;
1928 
1929 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1930 			       data->buf, sizeof(data->buf));
1931 	if (ret)
1932 		return ret;
1933 
1934 	*val = get_unaligned_be24(data->buf) >> (8 - oss);
1935 
1936 	return 0;
1937 }
1938 
1939 /*
1940  * Returns pressure in Pa, resolution is 1 Pa.
1941  *
1942  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1943  */
1944 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
1945 {
1946 	struct bmp180_calib *calib = &data->calib.bmp180;
1947 	s32 oss = data->oversampling_press;
1948 	s32 x1, x2, x3, p;
1949 	s32 b3, b6;
1950 	u32 b4, b7;
1951 
1952 	b6 = data->t_fine - 4000;
1953 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1954 	x2 = calib->AC2 * b6 >> 11;
1955 	x3 = x1 + x2;
1956 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
1957 	x1 = calib->AC3 * b6 >> 13;
1958 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
1959 	x3 = (x1 + x2 + 2) >> 2;
1960 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
1961 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
1962 	if (b7 < 0x80000000)
1963 		p = (b7 * 2) / b4;
1964 	else
1965 		p = (b7 / b4) * 2;
1966 
1967 	x1 = (p >> 8) * (p >> 8);
1968 	x1 = (x1 * 3038) >> 16;
1969 	x2 = (-7357 * p) >> 16;
1970 
1971 	return p + ((x1 + x2 + 3791) >> 4);
1972 }
1973 
1974 static int bmp180_read_press(struct bmp280_data *data, int *val, int *val2)
1975 {
1976 	u32 comp_press;
1977 	s32 adc_press;
1978 	int ret;
1979 
1980 	/* Read and compensate temperature so we get a reading of t_fine. */
1981 	ret = bmp180_read_temp(data, NULL, NULL);
1982 	if (ret)
1983 		return ret;
1984 
1985 	ret = bmp180_read_adc_press(data, &adc_press);
1986 	if (ret)
1987 		return ret;
1988 
1989 	comp_press = bmp180_compensate_press(data, adc_press);
1990 
1991 	*val = comp_press;
1992 	*val2 = 1000;
1993 
1994 	return IIO_VAL_FRACTIONAL;
1995 }
1996 
1997 static int bmp180_chip_config(struct bmp280_data *data)
1998 {
1999 	return 0;
2000 }
2001 
2002 static const int bmp180_oversampling_temp_avail[] = { 1 };
2003 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
2004 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID };
2005 
2006 const struct bmp280_chip_info bmp180_chip_info = {
2007 	.id_reg = BMP280_REG_ID,
2008 	.chip_id = bmp180_chip_ids,
2009 	.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
2010 	.regmap_config = &bmp180_regmap_config,
2011 	.start_up_time = 2000,
2012 	.channels = bmp280_channels,
2013 	.num_channels = 2,
2014 
2015 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
2016 	.num_oversampling_temp_avail =
2017 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
2018 	.oversampling_temp_default = 0,
2019 
2020 	.oversampling_press_avail = bmp180_oversampling_press_avail,
2021 	.num_oversampling_press_avail =
2022 		ARRAY_SIZE(bmp180_oversampling_press_avail),
2023 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
2024 
2025 	.chip_config = bmp180_chip_config,
2026 	.read_temp = bmp180_read_temp,
2027 	.read_press = bmp180_read_press,
2028 	.read_calib = bmp180_read_calib,
2029 };
2030 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
2031 
2032 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2033 {
2034 	struct bmp280_data *data = d;
2035 
2036 	complete(&data->done);
2037 
2038 	return IRQ_HANDLED;
2039 }
2040 
2041 static int bmp085_fetch_eoc_irq(struct device *dev,
2042 				const char *name,
2043 				int irq,
2044 				struct bmp280_data *data)
2045 {
2046 	unsigned long irq_trig;
2047 	int ret;
2048 
2049 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
2050 	if (irq_trig != IRQF_TRIGGER_RISING) {
2051 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
2052 		irq_trig = IRQF_TRIGGER_RISING;
2053 	}
2054 
2055 	init_completion(&data->done);
2056 
2057 	ret = devm_request_threaded_irq(dev,
2058 			irq,
2059 			bmp085_eoc_irq,
2060 			NULL,
2061 			irq_trig,
2062 			name,
2063 			data);
2064 	if (ret) {
2065 		/* Bail out without IRQ but keep the driver in place */
2066 		dev_err(dev, "unable to request DRDY IRQ\n");
2067 		return 0;
2068 	}
2069 
2070 	data->use_eoc = true;
2071 	return 0;
2072 }
2073 
2074 static void bmp280_pm_disable(void *data)
2075 {
2076 	struct device *dev = data;
2077 
2078 	pm_runtime_get_sync(dev);
2079 	pm_runtime_put_noidle(dev);
2080 	pm_runtime_disable(dev);
2081 }
2082 
2083 static void bmp280_regulators_disable(void *data)
2084 {
2085 	struct regulator_bulk_data *supplies = data;
2086 
2087 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
2088 }
2089 
2090 int bmp280_common_probe(struct device *dev,
2091 			struct regmap *regmap,
2092 			const struct bmp280_chip_info *chip_info,
2093 			const char *name,
2094 			int irq)
2095 {
2096 	struct iio_dev *indio_dev;
2097 	struct bmp280_data *data;
2098 	struct gpio_desc *gpiod;
2099 	unsigned int chip_id;
2100 	unsigned int i;
2101 	int ret;
2102 
2103 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2104 	if (!indio_dev)
2105 		return -ENOMEM;
2106 
2107 	data = iio_priv(indio_dev);
2108 	mutex_init(&data->lock);
2109 	data->dev = dev;
2110 
2111 	indio_dev->name = name;
2112 	indio_dev->info = &bmp280_info;
2113 	indio_dev->modes = INDIO_DIRECT_MODE;
2114 
2115 	data->chip_info = chip_info;
2116 
2117 	/* Apply initial values from chip info structure */
2118 	indio_dev->channels = chip_info->channels;
2119 	indio_dev->num_channels = chip_info->num_channels;
2120 	data->oversampling_press = chip_info->oversampling_press_default;
2121 	data->oversampling_humid = chip_info->oversampling_humid_default;
2122 	data->oversampling_temp = chip_info->oversampling_temp_default;
2123 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
2124 	data->sampling_freq = chip_info->sampling_freq_default;
2125 	data->start_up_time = chip_info->start_up_time;
2126 
2127 	/* Bring up regulators */
2128 	regulator_bulk_set_supply_names(data->supplies,
2129 					bmp280_supply_names,
2130 					BMP280_NUM_SUPPLIES);
2131 
2132 	ret = devm_regulator_bulk_get(dev,
2133 				      BMP280_NUM_SUPPLIES, data->supplies);
2134 	if (ret) {
2135 		dev_err(dev, "failed to get regulators\n");
2136 		return ret;
2137 	}
2138 
2139 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2140 	if (ret) {
2141 		dev_err(dev, "failed to enable regulators\n");
2142 		return ret;
2143 	}
2144 
2145 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
2146 				       data->supplies);
2147 	if (ret)
2148 		return ret;
2149 
2150 	/* Wait to make sure we started up properly */
2151 	usleep_range(data->start_up_time, data->start_up_time + 100);
2152 
2153 	/* Bring chip out of reset if there is an assigned GPIO line */
2154 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
2155 	/* Deassert the signal */
2156 	if (gpiod) {
2157 		dev_info(dev, "release reset\n");
2158 		gpiod_set_value(gpiod, 0);
2159 	}
2160 
2161 	data->regmap = regmap;
2162 
2163 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
2164 	if (ret < 0)
2165 		return ret;
2166 
2167 	for (i = 0; i < data->chip_info->num_chip_id; i++) {
2168 		if (chip_id == data->chip_info->chip_id[i]) {
2169 			dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name);
2170 			break;
2171 		}
2172 	}
2173 
2174 	if (i == data->chip_info->num_chip_id)
2175 		dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id);
2176 
2177 	if (data->chip_info->preinit) {
2178 		ret = data->chip_info->preinit(data);
2179 		if (ret)
2180 			return dev_err_probe(data->dev, ret,
2181 					     "error running preinit tasks\n");
2182 	}
2183 
2184 	ret = data->chip_info->chip_config(data);
2185 	if (ret < 0)
2186 		return ret;
2187 
2188 	dev_set_drvdata(dev, indio_dev);
2189 
2190 	/*
2191 	 * Some chips have calibration parameters "programmed into the devices'
2192 	 * non-volatile memory during production". Let's read them out at probe
2193 	 * time once. They will not change.
2194 	 */
2195 
2196 	if (data->chip_info->read_calib) {
2197 		ret = data->chip_info->read_calib(data);
2198 		if (ret < 0)
2199 			return dev_err_probe(data->dev, ret,
2200 					     "failed to read calibration coefficients\n");
2201 	}
2202 
2203 	/*
2204 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
2205 	 * however as it happens, the BMP085 shares the chip ID of BMP180
2206 	 * so we look for an IRQ if we have that.
2207 	 */
2208 	if (irq > 0 && (chip_id  == BMP180_CHIP_ID)) {
2209 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2210 		if (ret)
2211 			return ret;
2212 	}
2213 
2214 	/* Enable runtime PM */
2215 	pm_runtime_get_noresume(dev);
2216 	pm_runtime_set_active(dev);
2217 	pm_runtime_enable(dev);
2218 	/*
2219 	 * Set autosuspend to two orders of magnitude larger than the
2220 	 * start-up time.
2221 	 */
2222 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
2223 	pm_runtime_use_autosuspend(dev);
2224 	pm_runtime_put(dev);
2225 
2226 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
2227 	if (ret)
2228 		return ret;
2229 
2230 	return devm_iio_device_register(dev, indio_dev);
2231 }
2232 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
2233 
2234 static int bmp280_runtime_suspend(struct device *dev)
2235 {
2236 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2237 	struct bmp280_data *data = iio_priv(indio_dev);
2238 
2239 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
2240 }
2241 
2242 static int bmp280_runtime_resume(struct device *dev)
2243 {
2244 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2245 	struct bmp280_data *data = iio_priv(indio_dev);
2246 	int ret;
2247 
2248 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2249 	if (ret)
2250 		return ret;
2251 
2252 	usleep_range(data->start_up_time, data->start_up_time + 100);
2253 	return data->chip_info->chip_config(data);
2254 }
2255 
2256 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
2257 			  bmp280_runtime_resume, NULL);
2258 
2259 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
2260 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
2261 MODULE_LICENSE("GPL v2");
2262