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