xref: /linux/drivers/iio/pressure/bmp280-core.c (revision ae22a94997b8a03dcb3c922857c203246711f9d4)
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/device.h>
31 #include <linux/module.h>
32 #include <linux/nvmem-provider.h>
33 #include <linux/regmap.h>
34 #include <linux/delay.h>
35 #include <linux/iio/iio.h>
36 #include <linux/iio/sysfs.h>
37 #include <linux/gpio/consumer.h>
38 #include <linux/regulator/consumer.h>
39 #include <linux/interrupt.h>
40 #include <linux/irq.h> /* For irq_get_irq_data() */
41 #include <linux/completion.h>
42 #include <linux/pm_runtime.h>
43 #include <linux/random.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 	.start_up_time = 2000,
1237 	.channels = bmp380_channels,
1238 	.num_channels = 2,
1239 
1240 	.oversampling_temp_avail = bmp380_oversampling_avail,
1241 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1242 	.oversampling_temp_default = ilog2(1),
1243 
1244 	.oversampling_press_avail = bmp380_oversampling_avail,
1245 	.num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1246 	.oversampling_press_default = ilog2(4),
1247 
1248 	.sampling_freq_avail = bmp380_odr_table,
1249 	.num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
1250 	.sampling_freq_default = BMP380_ODR_50HZ,
1251 
1252 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1253 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1254 	.iir_filter_coeff_default = 2,
1255 
1256 	.chip_config = bmp380_chip_config,
1257 	.read_temp = bmp380_read_temp,
1258 	.read_press = bmp380_read_press,
1259 	.read_calib = bmp380_read_calib,
1260 	.preinit = bmp380_preinit,
1261 };
1262 EXPORT_SYMBOL_NS(bmp380_chip_info, IIO_BMP280);
1263 
1264 static int bmp580_soft_reset(struct bmp280_data *data)
1265 {
1266 	unsigned int reg;
1267 	int ret;
1268 
1269 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
1270 	if (ret) {
1271 		dev_err(data->dev, "failed to send reset command to device\n");
1272 		return ret;
1273 	}
1274 	usleep_range(2000, 2500);
1275 
1276 	/* Dummy read of chip_id */
1277 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1278 	if (ret) {
1279 		dev_err(data->dev, "failed to reestablish comms after reset\n");
1280 		return ret;
1281 	}
1282 
1283 	ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &reg);
1284 	if (ret) {
1285 		dev_err(data->dev, "error reading interrupt status register\n");
1286 		return ret;
1287 	}
1288 	if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
1289 		dev_err(data->dev, "error resetting sensor\n");
1290 		return -EINVAL;
1291 	}
1292 
1293 	return 0;
1294 }
1295 
1296 /**
1297  * bmp580_nvm_operation() - Helper function to commit NVM memory operations
1298  * @data: sensor data struct
1299  * @is_write: flag to signal write operation
1300  */
1301 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
1302 {
1303 	unsigned long timeout, poll;
1304 	unsigned int reg;
1305 	int ret;
1306 
1307 	/* Check NVM ready flag */
1308 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1309 	if (ret) {
1310 		dev_err(data->dev, "failed to check nvm status\n");
1311 		return ret;
1312 	}
1313 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
1314 		dev_err(data->dev, "sensor's nvm is not ready\n");
1315 		return -EIO;
1316 	}
1317 
1318 	/* Start NVM operation sequence */
1319 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_NVM_OP_SEQ_0);
1320 	if (ret) {
1321 		dev_err(data->dev, "failed to send nvm operation's first sequence\n");
1322 		return ret;
1323 	}
1324 	if (is_write) {
1325 		/* Send NVM write sequence */
1326 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1327 				   BMP580_CMD_NVM_WRITE_SEQ_1);
1328 		if (ret) {
1329 			dev_err(data->dev, "failed to send nvm write sequence\n");
1330 			return ret;
1331 		}
1332 		/* Datasheet says on 4.8.1.2 it takes approximately 10ms */
1333 		poll = 2000;
1334 		timeout = 12000;
1335 	} else {
1336 		/* Send NVM read sequence */
1337 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
1338 				   BMP580_CMD_NVM_READ_SEQ_1);
1339 		if (ret) {
1340 			dev_err(data->dev, "failed to send nvm read sequence\n");
1341 			return ret;
1342 		}
1343 		/* Datasheet says on 4.8.1.1 it takes approximately 200us */
1344 		poll = 50;
1345 		timeout = 400;
1346 	}
1347 	if (ret) {
1348 		dev_err(data->dev, "failed to write command sequence\n");
1349 		return -EIO;
1350 	}
1351 
1352 	/* Wait until NVM is ready again */
1353 	ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
1354 				       (reg & BMP580_STATUS_NVM_RDY_MASK),
1355 				       poll, timeout);
1356 	if (ret) {
1357 		dev_err(data->dev, "error checking nvm operation status\n");
1358 		return ret;
1359 	}
1360 
1361 	/* Check NVM error flags */
1362 	if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
1363 		dev_err(data->dev, "error processing nvm operation\n");
1364 		return -EIO;
1365 	}
1366 
1367 	return 0;
1368 }
1369 
1370 /*
1371  * Contrary to previous sensors families, compensation algorithm is builtin.
1372  * We are only required to read the register raw data and adapt the ranges
1373  * for what is expected on IIO ABI.
1374  */
1375 
1376 static int bmp580_read_temp(struct bmp280_data *data, int *val, int *val2)
1377 {
1378 	s32 raw_temp;
1379 	int ret;
1380 
1381 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB, data->buf,
1382 			       sizeof(data->buf));
1383 	if (ret) {
1384 		dev_err(data->dev, "failed to read temperature\n");
1385 		return ret;
1386 	}
1387 
1388 	raw_temp = get_unaligned_le24(data->buf);
1389 	if (raw_temp == BMP580_TEMP_SKIPPED) {
1390 		dev_err(data->dev, "reading temperature skipped\n");
1391 		return -EIO;
1392 	}
1393 
1394 	/*
1395 	 * Temperature is returned in Celsius degrees in fractional
1396 	 * form down 2^16. We rescale by x1000 to return milli Celsius
1397 	 * to respect IIO ABI.
1398 	 */
1399 	*val = raw_temp * 1000;
1400 	*val2 = 16;
1401 	return IIO_VAL_FRACTIONAL_LOG2;
1402 }
1403 
1404 static int bmp580_read_press(struct bmp280_data *data, int *val, int *val2)
1405 {
1406 	u32 raw_press;
1407 	int ret;
1408 
1409 	ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB, data->buf,
1410 			       sizeof(data->buf));
1411 	if (ret) {
1412 		dev_err(data->dev, "failed to read pressure\n");
1413 		return ret;
1414 	}
1415 
1416 	raw_press = get_unaligned_le24(data->buf);
1417 	if (raw_press == BMP580_PRESS_SKIPPED) {
1418 		dev_err(data->dev, "reading pressure skipped\n");
1419 		return -EIO;
1420 	}
1421 	/*
1422 	 * Pressure is returned in Pascals in fractional form down 2^16.
1423 	 * We rescale /1000 to convert to kilopascal to respect IIO ABI.
1424 	 */
1425 	*val = raw_press;
1426 	*val2 = 64000; /* 2^6 * 1000 */
1427 	return IIO_VAL_FRACTIONAL;
1428 }
1429 
1430 static const int bmp580_odr_table[][2] = {
1431 	[BMP580_ODR_240HZ] =	{240, 0},
1432 	[BMP580_ODR_218HZ] =	{218, 0},
1433 	[BMP580_ODR_199HZ] =	{199, 0},
1434 	[BMP580_ODR_179HZ] =	{179, 0},
1435 	[BMP580_ODR_160HZ] =	{160, 0},
1436 	[BMP580_ODR_149HZ] =	{149, 0},
1437 	[BMP580_ODR_140HZ] =	{140, 0},
1438 	[BMP580_ODR_129HZ] =	{129, 0},
1439 	[BMP580_ODR_120HZ] =	{120, 0},
1440 	[BMP580_ODR_110HZ] =	{110, 0},
1441 	[BMP580_ODR_100HZ] =	{100, 0},
1442 	[BMP580_ODR_89HZ] =	{89, 0},
1443 	[BMP580_ODR_80HZ] =	{80, 0},
1444 	[BMP580_ODR_70HZ] =	{70, 0},
1445 	[BMP580_ODR_60HZ] =	{60, 0},
1446 	[BMP580_ODR_50HZ] =	{50, 0},
1447 	[BMP580_ODR_45HZ] =	{45, 0},
1448 	[BMP580_ODR_40HZ] =	{40, 0},
1449 	[BMP580_ODR_35HZ] =	{35, 0},
1450 	[BMP580_ODR_30HZ] =	{30, 0},
1451 	[BMP580_ODR_25HZ] =	{25, 0},
1452 	[BMP580_ODR_20HZ] =	{20, 0},
1453 	[BMP580_ODR_15HZ] =	{15, 0},
1454 	[BMP580_ODR_10HZ] =	{10, 0},
1455 	[BMP580_ODR_5HZ] =	{5, 0},
1456 	[BMP580_ODR_4HZ] =	{4, 0},
1457 	[BMP580_ODR_3HZ] =	{3, 0},
1458 	[BMP580_ODR_2HZ] =	{2, 0},
1459 	[BMP580_ODR_1HZ] =	{1, 0},
1460 	[BMP580_ODR_0_5HZ] =	{0, 500000},
1461 	[BMP580_ODR_0_25HZ] =	{0, 250000},
1462 	[BMP580_ODR_0_125HZ] =	{0, 125000},
1463 };
1464 
1465 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
1466 
1467 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
1468 			     size_t bytes)
1469 {
1470 	struct bmp280_data *data = priv;
1471 	u16 *dst = val;
1472 	int ret, addr;
1473 
1474 	pm_runtime_get_sync(data->dev);
1475 	mutex_lock(&data->lock);
1476 
1477 	/* Set sensor in standby mode */
1478 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1479 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1480 				 BMP580_ODR_DEEPSLEEP_DIS |
1481 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1482 	if (ret) {
1483 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1484 		goto exit;
1485 	}
1486 	/* Wait standby transition time */
1487 	usleep_range(2500, 3000);
1488 
1489 	while (bytes >= sizeof(*dst)) {
1490 		addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
1491 
1492 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
1493 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1494 		if (ret) {
1495 			dev_err(data->dev, "error writing nvm address\n");
1496 			goto exit;
1497 		}
1498 
1499 		ret = bmp580_nvm_operation(data, false);
1500 		if (ret)
1501 			goto exit;
1502 
1503 		ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1504 				       sizeof(data->le16));
1505 		if (ret) {
1506 			dev_err(data->dev, "error reading nvm data regs\n");
1507 			goto exit;
1508 		}
1509 
1510 		*dst++ = le16_to_cpu(data->le16);
1511 		bytes -= sizeof(*dst);
1512 		offset += sizeof(*dst);
1513 	}
1514 exit:
1515 	/* Restore chip config */
1516 	data->chip_info->chip_config(data);
1517 	mutex_unlock(&data->lock);
1518 	pm_runtime_mark_last_busy(data->dev);
1519 	pm_runtime_put_autosuspend(data->dev);
1520 	return ret;
1521 }
1522 
1523 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
1524 			      size_t bytes)
1525 {
1526 	struct bmp280_data *data = priv;
1527 	u16 *buf = val;
1528 	int ret, addr;
1529 
1530 	pm_runtime_get_sync(data->dev);
1531 	mutex_lock(&data->lock);
1532 
1533 	/* Set sensor in standby mode */
1534 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1535 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1536 				 BMP580_ODR_DEEPSLEEP_DIS |
1537 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1538 	if (ret) {
1539 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1540 		goto exit;
1541 	}
1542 	/* Wait standby transition time */
1543 	usleep_range(2500, 3000);
1544 
1545 	while (bytes >= sizeof(*buf)) {
1546 		addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
1547 
1548 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR, BMP580_NVM_PROG_EN |
1549 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
1550 		if (ret) {
1551 			dev_err(data->dev, "error writing nvm address\n");
1552 			goto exit;
1553 		}
1554 		data->le16 = cpu_to_le16(*buf++);
1555 
1556 		ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB, &data->le16,
1557 					sizeof(data->le16));
1558 		if (ret) {
1559 			dev_err(data->dev, "error writing LSB NVM data regs\n");
1560 			goto exit;
1561 		}
1562 
1563 		ret = bmp580_nvm_operation(data, true);
1564 		if (ret)
1565 			goto exit;
1566 
1567 		/* Disable programming mode bit */
1568 		ret = regmap_update_bits(data->regmap, BMP580_REG_NVM_ADDR,
1569 					 BMP580_NVM_PROG_EN, 0);
1570 		if (ret) {
1571 			dev_err(data->dev, "error resetting nvm write\n");
1572 			goto exit;
1573 		}
1574 
1575 		bytes -= sizeof(*buf);
1576 		offset += sizeof(*buf);
1577 	}
1578 exit:
1579 	/* Restore chip config */
1580 	data->chip_info->chip_config(data);
1581 	mutex_unlock(&data->lock);
1582 	pm_runtime_mark_last_busy(data->dev);
1583 	pm_runtime_put_autosuspend(data->dev);
1584 	return ret;
1585 }
1586 
1587 static int bmp580_preinit(struct bmp280_data *data)
1588 {
1589 	struct nvmem_config config = {
1590 		.dev = data->dev,
1591 		.priv = data,
1592 		.name = "bmp580_nvmem",
1593 		.word_size = sizeof(u16),
1594 		.stride = sizeof(u16),
1595 		.size = 3 * sizeof(u16),
1596 		.reg_read = bmp580_nvmem_read,
1597 		.reg_write = bmp580_nvmem_write,
1598 	};
1599 	unsigned int reg;
1600 	int ret;
1601 
1602 	/* Issue soft-reset command */
1603 	ret = bmp580_soft_reset(data);
1604 	if (ret)
1605 		return ret;
1606 
1607 	/* Post powerup sequence */
1608 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
1609 	if (ret)
1610 		return ret;
1611 
1612 	/* Print warn message if we don't know the chip id */
1613 	if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
1614 		dev_warn(data->dev, "preinit: unexpected chip_id\n");
1615 
1616 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
1617 	if (ret)
1618 		return ret;
1619 
1620 	/* Check nvm status */
1621 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
1622 		dev_err(data->dev, "preinit: nvm error on powerup sequence\n");
1623 		return -EIO;
1624 	}
1625 
1626 	/* Register nvmem device */
1627 	return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
1628 }
1629 
1630 static int bmp580_chip_config(struct bmp280_data *data)
1631 {
1632 	bool change = false, aux;
1633 	unsigned int tmp;
1634 	u8 reg_val;
1635 	int ret;
1636 
1637 	/* Sets sensor in standby mode */
1638 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1639 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
1640 				 BMP580_ODR_DEEPSLEEP_DIS |
1641 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
1642 	if (ret) {
1643 		dev_err(data->dev, "failed to change sensor to standby mode\n");
1644 		return ret;
1645 	}
1646 	/* From datasheet's table 4: electrical characteristics */
1647 	usleep_range(2500, 3000);
1648 
1649 	/* Set default DSP mode settings */
1650 	reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
1651 		  BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
1652 
1653 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
1654 				 BMP580_DSP_COMP_MASK |
1655 				 BMP580_DSP_SHDW_IIR_TEMP_EN |
1656 				 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
1657 
1658 	/* Configure oversampling */
1659 	reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
1660 		  FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
1661 		  BMP580_OSR_PRESS_EN;
1662 
1663 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
1664 				       BMP580_OSR_TEMP_MASK | BMP580_OSR_PRESS_MASK |
1665 				       BMP580_OSR_PRESS_EN,
1666 				       reg_val, &aux);
1667 	if (ret) {
1668 		dev_err(data->dev, "failed to write oversampling register\n");
1669 		return ret;
1670 	}
1671 	change = change || aux;
1672 
1673 	/* Configure output data rate */
1674 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
1675 				       FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
1676 				       &aux);
1677 	if (ret) {
1678 		dev_err(data->dev, "failed to write ODR configuration register\n");
1679 		return ret;
1680 	}
1681 	change = change || aux;
1682 
1683 	/* Set filter data */
1684 	reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
1685 		  FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
1686 
1687 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_DSP_IIR,
1688 				       BMP580_DSP_IIR_PRESS_MASK |
1689 				       BMP580_DSP_IIR_TEMP_MASK,
1690 				       reg_val, &aux);
1691 	if (ret) {
1692 		dev_err(data->dev, "failed to write config register\n");
1693 		return ret;
1694 	}
1695 	change = change || aux;
1696 
1697 	/* Restore sensor to normal operation mode */
1698 	ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
1699 				BMP580_MODE_MASK,
1700 				FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_NORMAL));
1701 	if (ret) {
1702 		dev_err(data->dev, "failed to set normal mode\n");
1703 		return ret;
1704 	}
1705 	/* From datasheet's table 4: electrical characteristics */
1706 	usleep_range(3000, 3500);
1707 
1708 	if (change) {
1709 		/*
1710 		 * Check if ODR and OSR settings are valid or we are
1711 		 * operating in a degraded mode.
1712 		 */
1713 		ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
1714 		if (ret) {
1715 			dev_err(data->dev, "error reading effective OSR register\n");
1716 			return ret;
1717 		}
1718 		if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
1719 			dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
1720 			/* Set current OSR settings from data on effective OSR */
1721 			data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
1722 			data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
1723 			return -EINVAL;
1724 		}
1725 	}
1726 
1727 	return 0;
1728 }
1729 
1730 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
1731 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };
1732 
1733 const struct bmp280_chip_info bmp580_chip_info = {
1734 	.id_reg = BMP580_REG_CHIP_ID,
1735 	.chip_id = bmp580_chip_ids,
1736 	.num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
1737 	.regmap_config = &bmp580_regmap_config,
1738 	.start_up_time = 2000,
1739 	.channels = bmp380_channels,
1740 	.num_channels = 2,
1741 
1742 	.oversampling_temp_avail = bmp580_oversampling_avail,
1743 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1744 	.oversampling_temp_default = ilog2(1),
1745 
1746 	.oversampling_press_avail = bmp580_oversampling_avail,
1747 	.num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
1748 	.oversampling_press_default = ilog2(4),
1749 
1750 	.sampling_freq_avail = bmp580_odr_table,
1751 	.num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
1752 	.sampling_freq_default = BMP580_ODR_50HZ,
1753 
1754 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1755 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1756 	.iir_filter_coeff_default = 2,
1757 
1758 	.chip_config = bmp580_chip_config,
1759 	.read_temp = bmp580_read_temp,
1760 	.read_press = bmp580_read_press,
1761 	.preinit = bmp580_preinit,
1762 };
1763 EXPORT_SYMBOL_NS(bmp580_chip_info, IIO_BMP280);
1764 
1765 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
1766 {
1767 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
1768 	unsigned int delay_us;
1769 	unsigned int ctrl;
1770 	int ret;
1771 
1772 	if (data->use_eoc)
1773 		reinit_completion(&data->done);
1774 
1775 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
1776 	if (ret)
1777 		return ret;
1778 
1779 	if (data->use_eoc) {
1780 		/*
1781 		 * If we have a completion interrupt, use it, wait up to
1782 		 * 100ms. The longest conversion time listed is 76.5 ms for
1783 		 * advanced resolution mode.
1784 		 */
1785 		ret = wait_for_completion_timeout(&data->done,
1786 						  1 + msecs_to_jiffies(100));
1787 		if (!ret)
1788 			dev_err(data->dev, "timeout waiting for completion\n");
1789 	} else {
1790 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
1791 			delay_us = 4500;
1792 		else
1793 			delay_us =
1794 				conversion_time_max[data->oversampling_press];
1795 
1796 		usleep_range(delay_us, delay_us + 1000);
1797 	}
1798 
1799 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
1800 	if (ret)
1801 		return ret;
1802 
1803 	/* The value of this bit reset to "0" after conversion is complete */
1804 	if (ctrl & BMP180_MEAS_SCO)
1805 		return -EIO;
1806 
1807 	return 0;
1808 }
1809 
1810 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
1811 {
1812 	int ret;
1813 
1814 	ret = bmp180_measure(data,
1815 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
1816 			     BMP180_MEAS_SCO);
1817 	if (ret)
1818 		return ret;
1819 
1820 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1821 			       &data->be16, sizeof(data->be16));
1822 	if (ret)
1823 		return ret;
1824 
1825 	*val = be16_to_cpu(data->be16);
1826 
1827 	return 0;
1828 }
1829 
1830 static int bmp180_read_calib(struct bmp280_data *data)
1831 {
1832 	struct bmp180_calib *calib = &data->calib.bmp180;
1833 	int ret;
1834 	int i;
1835 
1836 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1837 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1838 
1839 	if (ret < 0)
1840 		return ret;
1841 
1842 	/* None of the words has the value 0 or 0xFFFF */
1843 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
1844 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
1845 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
1846 			return -EIO;
1847 	}
1848 
1849 	/* Toss the calibration data into the entropy pool */
1850 	add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1851 
1852 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
1853 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
1854 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
1855 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
1856 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
1857 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
1858 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
1859 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
1860 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
1861 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
1862 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
1863 
1864 	return 0;
1865 }
1866 
1867 /*
1868  * Returns temperature in DegC, resolution is 0.1 DegC.
1869  * t_fine carries fine temperature as global value.
1870  *
1871  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1872  */
1873 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
1874 {
1875 	struct bmp180_calib *calib = &data->calib.bmp180;
1876 	s32 x1, x2;
1877 
1878 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1879 	x2 = (calib->MC << 11) / (x1 + calib->MD);
1880 	data->t_fine = x1 + x2;
1881 
1882 	return (data->t_fine + 8) >> 4;
1883 }
1884 
1885 static int bmp180_read_temp(struct bmp280_data *data, int *val, int *val2)
1886 {
1887 	s32 adc_temp, comp_temp;
1888 	int ret;
1889 
1890 	ret = bmp180_read_adc_temp(data, &adc_temp);
1891 	if (ret)
1892 		return ret;
1893 
1894 	comp_temp = bmp180_compensate_temp(data, adc_temp);
1895 
1896 	/*
1897 	 * val might be NULL if we're called by the read_press routine,
1898 	 * who only cares about the carry over t_fine value.
1899 	 */
1900 	if (val) {
1901 		*val = comp_temp * 100;
1902 		return IIO_VAL_INT;
1903 	}
1904 
1905 	return 0;
1906 }
1907 
1908 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
1909 {
1910 	u8 oss = data->oversampling_press;
1911 	int ret;
1912 
1913 	ret = bmp180_measure(data,
1914 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
1915 			     FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
1916 			     BMP180_MEAS_SCO);
1917 	if (ret)
1918 		return ret;
1919 
1920 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1921 			       data->buf, sizeof(data->buf));
1922 	if (ret)
1923 		return ret;
1924 
1925 	*val = get_unaligned_be24(data->buf) >> (8 - oss);
1926 
1927 	return 0;
1928 }
1929 
1930 /*
1931  * Returns pressure in Pa, resolution is 1 Pa.
1932  *
1933  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1934  */
1935 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
1936 {
1937 	struct bmp180_calib *calib = &data->calib.bmp180;
1938 	s32 oss = data->oversampling_press;
1939 	s32 x1, x2, x3, p;
1940 	s32 b3, b6;
1941 	u32 b4, b7;
1942 
1943 	b6 = data->t_fine - 4000;
1944 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1945 	x2 = calib->AC2 * b6 >> 11;
1946 	x3 = x1 + x2;
1947 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
1948 	x1 = calib->AC3 * b6 >> 13;
1949 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
1950 	x3 = (x1 + x2 + 2) >> 2;
1951 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
1952 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
1953 	if (b7 < 0x80000000)
1954 		p = (b7 * 2) / b4;
1955 	else
1956 		p = (b7 / b4) * 2;
1957 
1958 	x1 = (p >> 8) * (p >> 8);
1959 	x1 = (x1 * 3038) >> 16;
1960 	x2 = (-7357 * p) >> 16;
1961 
1962 	return p + ((x1 + x2 + 3791) >> 4);
1963 }
1964 
1965 static int bmp180_read_press(struct bmp280_data *data,
1966 			     int *val, int *val2)
1967 {
1968 	u32 comp_press;
1969 	s32 adc_press;
1970 	int ret;
1971 
1972 	/* Read and compensate temperature so we get a reading of t_fine. */
1973 	ret = bmp180_read_temp(data, NULL, NULL);
1974 	if (ret)
1975 		return ret;
1976 
1977 	ret = bmp180_read_adc_press(data, &adc_press);
1978 	if (ret)
1979 		return ret;
1980 
1981 	comp_press = bmp180_compensate_press(data, adc_press);
1982 
1983 	*val = comp_press;
1984 	*val2 = 1000;
1985 
1986 	return IIO_VAL_FRACTIONAL;
1987 }
1988 
1989 static int bmp180_chip_config(struct bmp280_data *data)
1990 {
1991 	return 0;
1992 }
1993 
1994 static const int bmp180_oversampling_temp_avail[] = { 1 };
1995 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1996 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID };
1997 
1998 const struct bmp280_chip_info bmp180_chip_info = {
1999 	.id_reg = BMP280_REG_ID,
2000 	.chip_id = bmp180_chip_ids,
2001 	.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
2002 	.regmap_config = &bmp180_regmap_config,
2003 	.start_up_time = 2000,
2004 	.channels = bmp280_channels,
2005 	.num_channels = 2,
2006 
2007 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
2008 	.num_oversampling_temp_avail =
2009 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
2010 	.oversampling_temp_default = 0,
2011 
2012 	.oversampling_press_avail = bmp180_oversampling_press_avail,
2013 	.num_oversampling_press_avail =
2014 		ARRAY_SIZE(bmp180_oversampling_press_avail),
2015 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
2016 
2017 	.chip_config = bmp180_chip_config,
2018 	.read_temp = bmp180_read_temp,
2019 	.read_press = bmp180_read_press,
2020 	.read_calib = bmp180_read_calib,
2021 };
2022 EXPORT_SYMBOL_NS(bmp180_chip_info, IIO_BMP280);
2023 
2024 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
2025 {
2026 	struct bmp280_data *data = d;
2027 
2028 	complete(&data->done);
2029 
2030 	return IRQ_HANDLED;
2031 }
2032 
2033 static int bmp085_fetch_eoc_irq(struct device *dev,
2034 				const char *name,
2035 				int irq,
2036 				struct bmp280_data *data)
2037 {
2038 	unsigned long irq_trig;
2039 	int ret;
2040 
2041 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
2042 	if (irq_trig != IRQF_TRIGGER_RISING) {
2043 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
2044 		irq_trig = IRQF_TRIGGER_RISING;
2045 	}
2046 
2047 	init_completion(&data->done);
2048 
2049 	ret = devm_request_threaded_irq(dev,
2050 			irq,
2051 			bmp085_eoc_irq,
2052 			NULL,
2053 			irq_trig,
2054 			name,
2055 			data);
2056 	if (ret) {
2057 		/* Bail out without IRQ but keep the driver in place */
2058 		dev_err(dev, "unable to request DRDY IRQ\n");
2059 		return 0;
2060 	}
2061 
2062 	data->use_eoc = true;
2063 	return 0;
2064 }
2065 
2066 static void bmp280_pm_disable(void *data)
2067 {
2068 	struct device *dev = data;
2069 
2070 	pm_runtime_get_sync(dev);
2071 	pm_runtime_put_noidle(dev);
2072 	pm_runtime_disable(dev);
2073 }
2074 
2075 static void bmp280_regulators_disable(void *data)
2076 {
2077 	struct regulator_bulk_data *supplies = data;
2078 
2079 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
2080 }
2081 
2082 int bmp280_common_probe(struct device *dev,
2083 			struct regmap *regmap,
2084 			const struct bmp280_chip_info *chip_info,
2085 			const char *name,
2086 			int irq)
2087 {
2088 	struct iio_dev *indio_dev;
2089 	struct bmp280_data *data;
2090 	struct gpio_desc *gpiod;
2091 	unsigned int chip_id;
2092 	unsigned int i;
2093 	int ret;
2094 
2095 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2096 	if (!indio_dev)
2097 		return -ENOMEM;
2098 
2099 	data = iio_priv(indio_dev);
2100 	mutex_init(&data->lock);
2101 	data->dev = dev;
2102 
2103 	indio_dev->name = name;
2104 	indio_dev->info = &bmp280_info;
2105 	indio_dev->modes = INDIO_DIRECT_MODE;
2106 
2107 	data->chip_info = chip_info;
2108 
2109 	/* Apply initial values from chip info structure */
2110 	indio_dev->channels = chip_info->channels;
2111 	indio_dev->num_channels = chip_info->num_channels;
2112 	data->oversampling_press = chip_info->oversampling_press_default;
2113 	data->oversampling_humid = chip_info->oversampling_humid_default;
2114 	data->oversampling_temp = chip_info->oversampling_temp_default;
2115 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
2116 	data->sampling_freq = chip_info->sampling_freq_default;
2117 	data->start_up_time = chip_info->start_up_time;
2118 
2119 	/* Bring up regulators */
2120 	regulator_bulk_set_supply_names(data->supplies,
2121 					bmp280_supply_names,
2122 					BMP280_NUM_SUPPLIES);
2123 
2124 	ret = devm_regulator_bulk_get(dev,
2125 				      BMP280_NUM_SUPPLIES, data->supplies);
2126 	if (ret) {
2127 		dev_err(dev, "failed to get regulators\n");
2128 		return ret;
2129 	}
2130 
2131 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2132 	if (ret) {
2133 		dev_err(dev, "failed to enable regulators\n");
2134 		return ret;
2135 	}
2136 
2137 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
2138 				       data->supplies);
2139 	if (ret)
2140 		return ret;
2141 
2142 	/* Wait to make sure we started up properly */
2143 	usleep_range(data->start_up_time, data->start_up_time + 100);
2144 
2145 	/* Bring chip out of reset if there is an assigned GPIO line */
2146 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
2147 	/* Deassert the signal */
2148 	if (gpiod) {
2149 		dev_info(dev, "release reset\n");
2150 		gpiod_set_value(gpiod, 0);
2151 	}
2152 
2153 	data->regmap = regmap;
2154 
2155 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
2156 	if (ret < 0)
2157 		return ret;
2158 
2159 	for (i = 0; i < data->chip_info->num_chip_id; i++) {
2160 		if (chip_id == data->chip_info->chip_id[i]) {
2161 			dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name);
2162 			break;
2163 		}
2164 	}
2165 
2166 	if (i == data->chip_info->num_chip_id)
2167 		dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id);
2168 
2169 	if (data->chip_info->preinit) {
2170 		ret = data->chip_info->preinit(data);
2171 		if (ret)
2172 			return dev_err_probe(data->dev, ret,
2173 					     "error running preinit tasks\n");
2174 	}
2175 
2176 	ret = data->chip_info->chip_config(data);
2177 	if (ret < 0)
2178 		return ret;
2179 
2180 	dev_set_drvdata(dev, indio_dev);
2181 
2182 	/*
2183 	 * Some chips have calibration parameters "programmed into the devices'
2184 	 * non-volatile memory during production". Let's read them out at probe
2185 	 * time once. They will not change.
2186 	 */
2187 
2188 	if (data->chip_info->read_calib) {
2189 		ret = data->chip_info->read_calib(data);
2190 		if (ret < 0)
2191 			return dev_err_probe(data->dev, ret,
2192 					     "failed to read calibration coefficients\n");
2193 	}
2194 
2195 	/*
2196 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
2197 	 * however as it happens, the BMP085 shares the chip ID of BMP180
2198 	 * so we look for an IRQ if we have that.
2199 	 */
2200 	if (irq > 0 && (chip_id  == BMP180_CHIP_ID)) {
2201 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
2202 		if (ret)
2203 			return ret;
2204 	}
2205 
2206 	/* Enable runtime PM */
2207 	pm_runtime_get_noresume(dev);
2208 	pm_runtime_set_active(dev);
2209 	pm_runtime_enable(dev);
2210 	/*
2211 	 * Set autosuspend to two orders of magnitude larger than the
2212 	 * start-up time.
2213 	 */
2214 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
2215 	pm_runtime_use_autosuspend(dev);
2216 	pm_runtime_put(dev);
2217 
2218 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
2219 	if (ret)
2220 		return ret;
2221 
2222 	return devm_iio_device_register(dev, indio_dev);
2223 }
2224 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
2225 
2226 static int bmp280_runtime_suspend(struct device *dev)
2227 {
2228 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2229 	struct bmp280_data *data = iio_priv(indio_dev);
2230 
2231 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
2232 }
2233 
2234 static int bmp280_runtime_resume(struct device *dev)
2235 {
2236 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
2237 	struct bmp280_data *data = iio_priv(indio_dev);
2238 	int ret;
2239 
2240 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
2241 	if (ret)
2242 		return ret;
2243 	usleep_range(data->start_up_time, data->start_up_time + 100);
2244 	return data->chip_info->chip_config(data);
2245 }
2246 
2247 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
2248 			  bmp280_runtime_resume, NULL);
2249 
2250 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
2251 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
2252 MODULE_LICENSE("GPL v2");
2253