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  *
17  * Notice:
18  * The link to the bmp180 datasheet points to an outdated version missing these changes:
19  * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
20  * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
21  * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
22  */
23 
24 #define pr_fmt(fmt) "bmp280: " fmt
25 
26 #include <linux/bitops.h>
27 #include <linux/bitfield.h>
28 #include <linux/device.h>
29 #include <linux/module.h>
30 #include <linux/regmap.h>
31 #include <linux/delay.h>
32 #include <linux/iio/iio.h>
33 #include <linux/iio/sysfs.h>
34 #include <linux/gpio/consumer.h>
35 #include <linux/regulator/consumer.h>
36 #include <linux/interrupt.h>
37 #include <linux/irq.h> /* For irq_get_irq_data() */
38 #include <linux/completion.h>
39 #include <linux/pm_runtime.h>
40 #include <linux/random.h>
41 
42 #include <asm/unaligned.h>
43 
44 #include "bmp280.h"
45 
46 /*
47  * These enums are used for indexing into the array of calibration
48  * coefficients for BMP180.
49  */
50 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
51 
52 struct bmp180_calib {
53 	s16 AC1;
54 	s16 AC2;
55 	s16 AC3;
56 	u16 AC4;
57 	u16 AC5;
58 	u16 AC6;
59 	s16 B1;
60 	s16 B2;
61 	s16 MB;
62 	s16 MC;
63 	s16 MD;
64 };
65 
66 /* See datasheet Section 4.2.2. */
67 struct bmp280_calib {
68 	u16 T1;
69 	s16 T2;
70 	s16 T3;
71 	u16 P1;
72 	s16 P2;
73 	s16 P3;
74 	s16 P4;
75 	s16 P5;
76 	s16 P6;
77 	s16 P7;
78 	s16 P8;
79 	s16 P9;
80 	u8  H1;
81 	s16 H2;
82 	u8  H3;
83 	s16 H4;
84 	s16 H5;
85 	s8  H6;
86 };
87 
88 /* See datasheet Section 3.11.1. */
89 struct bmp380_calib {
90 	u16 T1;
91 	u16 T2;
92 	s8  T3;
93 	s16 P1;
94 	s16 P2;
95 	s8  P3;
96 	s8  P4;
97 	u16 P5;
98 	u16 P6;
99 	s8  P7;
100 	s8  P8;
101 	s16 P9;
102 	s8  P10;
103 	s8  P11;
104 };
105 
106 static const char *const bmp280_supply_names[] = {
107 	"vddd", "vdda"
108 };
109 
110 #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names)
111 
112 enum bmp380_odr {
113 	BMP380_ODR_200HZ,
114 	BMP380_ODR_100HZ,
115 	BMP380_ODR_50HZ,
116 	BMP380_ODR_25HZ,
117 	BMP380_ODR_12_5HZ,
118 	BMP380_ODR_6_25HZ,
119 	BMP380_ODR_3_125HZ,
120 	BMP380_ODR_1_5625HZ,
121 	BMP380_ODR_0_78HZ,
122 	BMP380_ODR_0_39HZ,
123 	BMP380_ODR_0_2HZ,
124 	BMP380_ODR_0_1HZ,
125 	BMP380_ODR_0_05HZ,
126 	BMP380_ODR_0_02HZ,
127 	BMP380_ODR_0_01HZ,
128 	BMP380_ODR_0_006HZ,
129 	BMP380_ODR_0_003HZ,
130 	BMP380_ODR_0_0015HZ,
131 };
132 
133 struct bmp280_data {
134 	struct device *dev;
135 	struct mutex lock;
136 	struct regmap *regmap;
137 	struct completion done;
138 	bool use_eoc;
139 	const struct bmp280_chip_info *chip_info;
140 	union {
141 		struct bmp180_calib bmp180;
142 		struct bmp280_calib bmp280;
143 		struct bmp380_calib bmp380;
144 	} calib;
145 	struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES];
146 	unsigned int start_up_time; /* in microseconds */
147 
148 	/* log of base 2 of oversampling rate */
149 	u8 oversampling_press;
150 	u8 oversampling_temp;
151 	u8 oversampling_humid;
152 	u8 iir_filter_coeff;
153 
154 	/*
155 	 * BMP380 devices introduce sampling frequency configuration. See
156 	 * datasheet sections 3.3.3. and 4.3.19 for more details.
157 	 *
158 	 * BMx280 devices allowed indirect configuration of sampling frequency
159 	 * changing the t_standby duration between measurements, as detailed on
160 	 * section 3.6.3 of the datasheet.
161 	 */
162 	int sampling_freq;
163 
164 	/*
165 	 * Carryover value from temperature conversion, used in pressure
166 	 * calculation.
167 	 */
168 	s32 t_fine;
169 
170 	/*
171 	 * DMA (thus cache coherency maintenance) may require the
172 	 * transfer buffers to live in their own cache lines.
173 	 */
174 	union {
175 		/* Sensor data buffer */
176 		u8 buf[3];
177 		/* Calibration data buffers */
178 		__le16 bmp280_cal_buf[BMP280_CONTIGUOUS_CALIB_REGS / 2];
179 		__be16 bmp180_cal_buf[BMP180_REG_CALIB_COUNT / 2];
180 		u8 bmp380_cal_buf[BMP380_CALIB_REG_COUNT];
181 		/* Miscellaneous, endianess-aware data buffers */
182 		__le16 le16;
183 		__be16 be16;
184 	} __aligned(IIO_DMA_MINALIGN);
185 };
186 
187 struct bmp280_chip_info {
188 	unsigned int id_reg;
189 
190 	const struct iio_chan_spec *channels;
191 	int num_channels;
192 	unsigned int start_up_time;
193 
194 	const int *oversampling_temp_avail;
195 	int num_oversampling_temp_avail;
196 	int oversampling_temp_default;
197 
198 	const int *oversampling_press_avail;
199 	int num_oversampling_press_avail;
200 	int oversampling_press_default;
201 
202 	const int *oversampling_humid_avail;
203 	int num_oversampling_humid_avail;
204 	int oversampling_humid_default;
205 
206 	const int *iir_filter_coeffs_avail;
207 	int num_iir_filter_coeffs_avail;
208 	int iir_filter_coeff_default;
209 
210 	const int (*sampling_freq_avail)[2];
211 	int num_sampling_freq_avail;
212 	int sampling_freq_default;
213 
214 	int (*chip_config)(struct bmp280_data *);
215 	int (*read_temp)(struct bmp280_data *, int *);
216 	int (*read_press)(struct bmp280_data *, int *, int *);
217 	int (*read_humid)(struct bmp280_data *, int *, int *);
218 	int (*read_calib)(struct bmp280_data *);
219 };
220 
221 /*
222  * These enums are used for indexing into the array of compensation
223  * parameters for BMP280.
224  */
225 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
226 
227 enum {
228 	/* Temperature calib indexes */
229 	BMP380_T1 = 0,
230 	BMP380_T2 = 2,
231 	BMP380_T3 = 4,
232 	/* Pressure calib indexes */
233 	BMP380_P1 = 5,
234 	BMP380_P2 = 7,
235 	BMP380_P3 = 9,
236 	BMP380_P4 = 10,
237 	BMP380_P5 = 11,
238 	BMP380_P6 = 13,
239 	BMP380_P7 = 15,
240 	BMP380_P8 = 16,
241 	BMP380_P9 = 17,
242 	BMP380_P10 = 19,
243 	BMP380_P11 = 20,
244 };
245 
246 static const struct iio_chan_spec bmp280_channels[] = {
247 	{
248 		.type = IIO_PRESSURE,
249 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
250 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
251 	},
252 	{
253 		.type = IIO_TEMP,
254 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
255 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
256 	},
257 	{
258 		.type = IIO_HUMIDITYRELATIVE,
259 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
260 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
261 	},
262 };
263 
264 static const struct iio_chan_spec bmp380_channels[] = {
265 	{
266 		.type = IIO_PRESSURE,
267 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
268 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
269 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
270 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
271 	},
272 	{
273 		.type = IIO_TEMP,
274 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
275 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
276 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
277 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
278 	},
279 	{
280 		.type = IIO_HUMIDITYRELATIVE,
281 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
282 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
283 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
284 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
285 	},
286 };
287 
288 static int bmp280_read_calib(struct bmp280_data *data)
289 {
290 	struct bmp280_calib *calib = &data->calib.bmp280;
291 	int ret;
292 
293 
294 	/* Read temperature and pressure calibration values. */
295 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
296 			       data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
297 	if (ret < 0) {
298 		dev_err(data->dev,
299 			"failed to read temperature and pressure calibration parameters\n");
300 		return ret;
301 	}
302 
303 	/* Toss the temperature and pressure calibration data into the entropy pool */
304 	add_device_randomness(data->bmp280_cal_buf, sizeof(data->bmp280_cal_buf));
305 
306 	/* Parse temperature calibration values. */
307 	calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
308 	calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
309 	calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
310 
311 	/* Parse pressure calibration values. */
312 	calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
313 	calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
314 	calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
315 	calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
316 	calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
317 	calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
318 	calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
319 	calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
320 	calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
321 
322 	return 0;
323 }
324 
325 static int bme280_read_calib(struct bmp280_data *data)
326 {
327 	struct bmp280_calib *calib = &data->calib.bmp280;
328 	struct device *dev = data->dev;
329 	unsigned int tmp;
330 	int ret;
331 
332 	/* Load shared calibration params with bmp280 first */
333 	ret = bmp280_read_calib(data);
334 	if  (ret < 0) {
335 		dev_err(dev, "failed to read common bmp280 calibration parameters\n");
336 		return ret;
337 	}
338 
339 	/*
340 	 * Read humidity calibration values.
341 	 * Due to some odd register addressing we cannot just
342 	 * do a big bulk read. Instead, we have to read each Hx
343 	 * value separately and sometimes do some bit shifting...
344 	 * Humidity data is only available on BME280.
345 	 */
346 
347 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
348 	if (ret < 0) {
349 		dev_err(dev, "failed to read H1 comp value\n");
350 		return ret;
351 	}
352 	calib->H1 = tmp;
353 
354 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2,
355 			       &data->le16, sizeof(data->le16));
356 	if (ret < 0) {
357 		dev_err(dev, "failed to read H2 comp value\n");
358 		return ret;
359 	}
360 	calib->H2 = sign_extend32(le16_to_cpu(data->le16), 15);
361 
362 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
363 	if (ret < 0) {
364 		dev_err(dev, "failed to read H3 comp value\n");
365 		return ret;
366 	}
367 	calib->H3 = tmp;
368 
369 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4,
370 			       &data->be16, sizeof(data->be16));
371 	if (ret < 0) {
372 		dev_err(dev, "failed to read H4 comp value\n");
373 		return ret;
374 	}
375 	calib->H4 = sign_extend32(((be16_to_cpu(data->be16) >> 4) & 0xff0) |
376 				  (be16_to_cpu(data->be16) & 0xf), 11);
377 
378 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5,
379 			       &data->le16, sizeof(data->le16));
380 	if (ret < 0) {
381 		dev_err(dev, "failed to read H5 comp value\n");
382 		return ret;
383 	}
384 	calib->H5 = sign_extend32(FIELD_GET(BMP280_COMP_H5_MASK, le16_to_cpu(data->le16)), 11);
385 
386 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
387 	if (ret < 0) {
388 		dev_err(dev, "failed to read H6 comp value\n");
389 		return ret;
390 	}
391 	calib->H6 = sign_extend32(tmp, 7);
392 
393 	return 0;
394 }
395 /*
396  * Returns humidity in percent, resolution is 0.01 percent. Output value of
397  * "47445" represents 47445/1024 = 46.333 %RH.
398  *
399  * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
400  */
401 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
402 				      s32 adc_humidity)
403 {
404 	struct bmp280_calib *calib = &data->calib.bmp280;
405 	s32 var;
406 
407 	var = ((s32)data->t_fine) - (s32)76800;
408 	var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
409 		+ (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
410 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
411 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
412 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
413 
414 	var = clamp_val(var, 0, 419430400);
415 
416 	return var >> 12;
417 };
418 
419 /*
420  * Returns temperature in DegC, resolution is 0.01 DegC.  Output value of
421  * "5123" equals 51.23 DegC.  t_fine carries fine temperature as global
422  * value.
423  *
424  * Taken from datasheet, Section 3.11.3, "Compensation formula".
425  */
426 static s32 bmp280_compensate_temp(struct bmp280_data *data,
427 				  s32 adc_temp)
428 {
429 	struct bmp280_calib *calib = &data->calib.bmp280;
430 	s32 var1, var2;
431 
432 	var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
433 		((s32)calib->T2)) >> 11;
434 	var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
435 		  ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
436 		((s32)calib->T3)) >> 14;
437 	data->t_fine = var1 + var2;
438 
439 	return (data->t_fine * 5 + 128) >> 8;
440 }
441 
442 /*
443  * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
444  * integer bits and 8 fractional bits).  Output value of "24674867"
445  * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
446  *
447  * Taken from datasheet, Section 3.11.3, "Compensation formula".
448  */
449 static u32 bmp280_compensate_press(struct bmp280_data *data,
450 				   s32 adc_press)
451 {
452 	struct bmp280_calib *calib = &data->calib.bmp280;
453 	s64 var1, var2, p;
454 
455 	var1 = ((s64)data->t_fine) - 128000;
456 	var2 = var1 * var1 * (s64)calib->P6;
457 	var2 += (var1 * (s64)calib->P5) << 17;
458 	var2 += ((s64)calib->P4) << 35;
459 	var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
460 		((var1 * (s64)calib->P2) << 12);
461 	var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
462 
463 	if (var1 == 0)
464 		return 0;
465 
466 	p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
467 	p = div64_s64(p, var1);
468 	var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
469 	var2 = ((s64)(calib->P8) * p) >> 19;
470 	p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
471 
472 	return (u32)p;
473 }
474 
475 static int bmp280_read_temp(struct bmp280_data *data,
476 			    int *val)
477 {
478 	s32 adc_temp, comp_temp;
479 	int ret;
480 
481 	ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
482 			       data->buf, sizeof(data->buf));
483 	if (ret < 0) {
484 		dev_err(data->dev, "failed to read temperature\n");
485 		return ret;
486 	}
487 
488 	adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
489 	if (adc_temp == BMP280_TEMP_SKIPPED) {
490 		/* reading was skipped */
491 		dev_err(data->dev, "reading temperature skipped\n");
492 		return -EIO;
493 	}
494 	comp_temp = bmp280_compensate_temp(data, adc_temp);
495 
496 	/*
497 	 * val might be NULL if we're called by the read_press routine,
498 	 * who only cares about the carry over t_fine value.
499 	 */
500 	if (val) {
501 		*val = comp_temp * 10;
502 		return IIO_VAL_INT;
503 	}
504 
505 	return 0;
506 }
507 
508 static int bmp280_read_press(struct bmp280_data *data,
509 			     int *val, int *val2)
510 {
511 	u32 comp_press;
512 	s32 adc_press;
513 	int ret;
514 
515 	/* Read and compensate temperature so we get a reading of t_fine. */
516 	ret = bmp280_read_temp(data, NULL);
517 	if (ret < 0)
518 		return ret;
519 
520 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
521 			       data->buf, sizeof(data->buf));
522 	if (ret < 0) {
523 		dev_err(data->dev, "failed to read pressure\n");
524 		return ret;
525 	}
526 
527 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
528 	if (adc_press == BMP280_PRESS_SKIPPED) {
529 		/* reading was skipped */
530 		dev_err(data->dev, "reading pressure skipped\n");
531 		return -EIO;
532 	}
533 	comp_press = bmp280_compensate_press(data, adc_press);
534 
535 	*val = comp_press;
536 	*val2 = 256000;
537 
538 	return IIO_VAL_FRACTIONAL;
539 }
540 
541 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
542 {
543 	u32 comp_humidity;
544 	s32 adc_humidity;
545 	int ret;
546 
547 	/* Read and compensate temperature so we get a reading of t_fine. */
548 	ret = bmp280_read_temp(data, NULL);
549 	if (ret < 0)
550 		return ret;
551 
552 	ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
553 			       &data->be16, sizeof(data->be16));
554 	if (ret < 0) {
555 		dev_err(data->dev, "failed to read humidity\n");
556 		return ret;
557 	}
558 
559 	adc_humidity = be16_to_cpu(data->be16);
560 	if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
561 		/* reading was skipped */
562 		dev_err(data->dev, "reading humidity skipped\n");
563 		return -EIO;
564 	}
565 	comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
566 
567 	*val = comp_humidity * 1000 / 1024;
568 
569 	return IIO_VAL_INT;
570 }
571 
572 static int bmp280_read_raw(struct iio_dev *indio_dev,
573 			   struct iio_chan_spec const *chan,
574 			   int *val, int *val2, long mask)
575 {
576 	struct bmp280_data *data = iio_priv(indio_dev);
577 	int ret;
578 
579 	pm_runtime_get_sync(data->dev);
580 	mutex_lock(&data->lock);
581 
582 	switch (mask) {
583 	case IIO_CHAN_INFO_PROCESSED:
584 		switch (chan->type) {
585 		case IIO_HUMIDITYRELATIVE:
586 			ret = data->chip_info->read_humid(data, val, val2);
587 			break;
588 		case IIO_PRESSURE:
589 			ret = data->chip_info->read_press(data, val, val2);
590 			break;
591 		case IIO_TEMP:
592 			ret = data->chip_info->read_temp(data, val);
593 			break;
594 		default:
595 			ret = -EINVAL;
596 			break;
597 		}
598 		break;
599 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
600 		switch (chan->type) {
601 		case IIO_HUMIDITYRELATIVE:
602 			*val = 1 << data->oversampling_humid;
603 			ret = IIO_VAL_INT;
604 			break;
605 		case IIO_PRESSURE:
606 			*val = 1 << data->oversampling_press;
607 			ret = IIO_VAL_INT;
608 			break;
609 		case IIO_TEMP:
610 			*val = 1 << data->oversampling_temp;
611 			ret = IIO_VAL_INT;
612 			break;
613 		default:
614 			ret = -EINVAL;
615 			break;
616 		}
617 		break;
618 	case IIO_CHAN_INFO_SAMP_FREQ:
619 		if (!data->chip_info->sampling_freq_avail) {
620 			ret = -EINVAL;
621 			break;
622 		}
623 
624 		*val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
625 		*val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
626 		ret = IIO_VAL_INT_PLUS_MICRO;
627 		break;
628 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
629 		if (!data->chip_info->iir_filter_coeffs_avail) {
630 			ret = -EINVAL;
631 			break;
632 		}
633 
634 		*val = (1 << data->iir_filter_coeff) - 1;
635 		ret = IIO_VAL_INT;
636 		break;
637 	default:
638 		ret = -EINVAL;
639 		break;
640 	}
641 
642 	mutex_unlock(&data->lock);
643 	pm_runtime_mark_last_busy(data->dev);
644 	pm_runtime_put_autosuspend(data->dev);
645 
646 	return ret;
647 }
648 
649 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
650 					       int val)
651 {
652 	const int *avail = data->chip_info->oversampling_humid_avail;
653 	const int n = data->chip_info->num_oversampling_humid_avail;
654 	int ret, prev;
655 	int i;
656 
657 	for (i = 0; i < n; i++) {
658 		if (avail[i] == val) {
659 			prev = data->oversampling_humid;
660 			data->oversampling_humid = ilog2(val);
661 
662 			ret = data->chip_info->chip_config(data);
663 			if (ret) {
664 				data->oversampling_humid = prev;
665 				data->chip_info->chip_config(data);
666 				return ret;
667 			}
668 			return 0;
669 		}
670 	}
671 	return -EINVAL;
672 }
673 
674 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
675 					       int val)
676 {
677 	const int *avail = data->chip_info->oversampling_temp_avail;
678 	const int n = data->chip_info->num_oversampling_temp_avail;
679 	int ret, prev;
680 	int i;
681 
682 	for (i = 0; i < n; i++) {
683 		if (avail[i] == val) {
684 			prev = data->oversampling_temp;
685 			data->oversampling_temp = ilog2(val);
686 
687 			ret = data->chip_info->chip_config(data);
688 			if (ret) {
689 				data->oversampling_temp = prev;
690 				data->chip_info->chip_config(data);
691 				return ret;
692 			}
693 			return 0;
694 		}
695 	}
696 	return -EINVAL;
697 }
698 
699 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
700 					       int val)
701 {
702 	const int *avail = data->chip_info->oversampling_press_avail;
703 	const int n = data->chip_info->num_oversampling_press_avail;
704 	int ret, prev;
705 	int i;
706 
707 	for (i = 0; i < n; i++) {
708 		if (avail[i] == val) {
709 			prev = data->oversampling_press;
710 			data->oversampling_press = ilog2(val);
711 
712 			ret = data->chip_info->chip_config(data);
713 			if (ret) {
714 				data->oversampling_press = prev;
715 				data->chip_info->chip_config(data);
716 				return ret;
717 			}
718 			return 0;
719 		}
720 	}
721 	return -EINVAL;
722 }
723 
724 static int bmp280_write_sampling_frequency(struct bmp280_data *data,
725 					   int val, int val2)
726 {
727 	const int (*avail)[2] = data->chip_info->sampling_freq_avail;
728 	const int n = data->chip_info->num_sampling_freq_avail;
729 	int ret, prev;
730 	int i;
731 
732 	for (i = 0; i < n; i++) {
733 		if (avail[i][0] == val && avail[i][1] == val2) {
734 			prev = data->sampling_freq;
735 			data->sampling_freq = i;
736 
737 			ret = data->chip_info->chip_config(data);
738 			if (ret) {
739 				data->sampling_freq = prev;
740 				data->chip_info->chip_config(data);
741 				return ret;
742 			}
743 			return 0;
744 		}
745 	}
746 	return -EINVAL;
747 }
748 
749 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
750 {
751 	const int *avail = data->chip_info->iir_filter_coeffs_avail;
752 	const int n = data->chip_info->num_iir_filter_coeffs_avail;
753 	int ret, prev;
754 	int i;
755 
756 	for (i = 0; i < n; i++) {
757 		if (avail[i] - 1  == val) {
758 			prev = data->iir_filter_coeff;
759 			data->iir_filter_coeff = i;
760 
761 			ret = data->chip_info->chip_config(data);
762 			if (ret) {
763 				data->iir_filter_coeff = prev;
764 				data->chip_info->chip_config(data);
765 				return ret;
766 
767 			}
768 			return 0;
769 		}
770 	}
771 	return -EINVAL;
772 }
773 
774 static int bmp280_write_raw(struct iio_dev *indio_dev,
775 			    struct iio_chan_spec const *chan,
776 			    int val, int val2, long mask)
777 {
778 	struct bmp280_data *data = iio_priv(indio_dev);
779 	int ret = 0;
780 
781 	/*
782 	 * Helper functions to update sensor running configuration.
783 	 * If an error happens applying new settings, will try restore
784 	 * previous parameters to ensure the sensor is left in a known
785 	 * working configuration.
786 	 */
787 	switch (mask) {
788 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
789 		pm_runtime_get_sync(data->dev);
790 		mutex_lock(&data->lock);
791 		switch (chan->type) {
792 		case IIO_HUMIDITYRELATIVE:
793 			ret = bmp280_write_oversampling_ratio_humid(data, val);
794 			break;
795 		case IIO_PRESSURE:
796 			ret = bmp280_write_oversampling_ratio_press(data, val);
797 			break;
798 		case IIO_TEMP:
799 			ret = bmp280_write_oversampling_ratio_temp(data, val);
800 			break;
801 		default:
802 			ret = -EINVAL;
803 			break;
804 		}
805 		mutex_unlock(&data->lock);
806 		pm_runtime_mark_last_busy(data->dev);
807 		pm_runtime_put_autosuspend(data->dev);
808 		break;
809 	case IIO_CHAN_INFO_SAMP_FREQ:
810 		pm_runtime_get_sync(data->dev);
811 		mutex_lock(&data->lock);
812 		ret = bmp280_write_sampling_frequency(data, val, val2);
813 		mutex_unlock(&data->lock);
814 		pm_runtime_mark_last_busy(data->dev);
815 		pm_runtime_put_autosuspend(data->dev);
816 		break;
817 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
818 		pm_runtime_get_sync(data->dev);
819 		mutex_lock(&data->lock);
820 		ret = bmp280_write_iir_filter_coeffs(data, val);
821 		mutex_unlock(&data->lock);
822 		pm_runtime_mark_last_busy(data->dev);
823 		pm_runtime_put_autosuspend(data->dev);
824 		break;
825 	default:
826 		return -EINVAL;
827 	}
828 
829 	return ret;
830 }
831 
832 static int bmp280_read_avail(struct iio_dev *indio_dev,
833 			     struct iio_chan_spec const *chan,
834 			     const int **vals, int *type, int *length,
835 			     long mask)
836 {
837 	struct bmp280_data *data = iio_priv(indio_dev);
838 
839 	switch (mask) {
840 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
841 		switch (chan->type) {
842 		case IIO_PRESSURE:
843 			*vals = data->chip_info->oversampling_press_avail;
844 			*length = data->chip_info->num_oversampling_press_avail;
845 			break;
846 		case IIO_TEMP:
847 			*vals = data->chip_info->oversampling_temp_avail;
848 			*length = data->chip_info->num_oversampling_temp_avail;
849 			break;
850 		default:
851 			return -EINVAL;
852 		}
853 		*type = IIO_VAL_INT;
854 		return IIO_AVAIL_LIST;
855 	case IIO_CHAN_INFO_SAMP_FREQ:
856 		*vals = (const int *)data->chip_info->sampling_freq_avail;
857 		*type = IIO_VAL_INT_PLUS_MICRO;
858 		/* Values are stored in a 2D matrix */
859 		*length = data->chip_info->num_sampling_freq_avail;
860 		return IIO_AVAIL_LIST;
861 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
862 		*vals = data->chip_info->iir_filter_coeffs_avail;
863 		*type = IIO_VAL_INT;
864 		*length = data->chip_info->num_iir_filter_coeffs_avail;
865 		return IIO_AVAIL_LIST;
866 	default:
867 		return -EINVAL;
868 	}
869 }
870 
871 static const struct iio_info bmp280_info = {
872 	.read_raw = &bmp280_read_raw,
873 	.read_avail = &bmp280_read_avail,
874 	.write_raw = &bmp280_write_raw,
875 };
876 
877 static int bmp280_chip_config(struct bmp280_data *data)
878 {
879 	u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
880 		  FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
881 	int ret;
882 
883 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
884 				 BMP280_OSRS_TEMP_MASK |
885 				 BMP280_OSRS_PRESS_MASK |
886 				 BMP280_MODE_MASK,
887 				 osrs | BMP280_MODE_NORMAL);
888 	if (ret < 0) {
889 		dev_err(data->dev,
890 			"failed to write ctrl_meas register\n");
891 		return ret;
892 	}
893 
894 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
895 				 BMP280_FILTER_MASK,
896 				 BMP280_FILTER_4X);
897 	if (ret < 0) {
898 		dev_err(data->dev,
899 			"failed to write config register\n");
900 		return ret;
901 	}
902 
903 	return ret;
904 }
905 
906 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
907 
908 static const struct bmp280_chip_info bmp280_chip_info = {
909 	.id_reg = BMP280_REG_ID,
910 	.start_up_time = 2000,
911 	.channels = bmp280_channels,
912 	.num_channels = 2,
913 
914 	.oversampling_temp_avail = bmp280_oversampling_avail,
915 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
916 	/*
917 	 * Oversampling config values on BMx280 have one additional setting
918 	 * that other generations of the family don't:
919 	 * The value 0 means the measurement is bypassed instead of
920 	 * oversampling set to x1.
921 	 *
922 	 * To account for this difference, and preserve the same common
923 	 * config logic, this is handled later on chip_config callback
924 	 * incrementing one unit the oversampling setting.
925 	 */
926 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
927 
928 	.oversampling_press_avail = bmp280_oversampling_avail,
929 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
930 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
931 
932 	.chip_config = bmp280_chip_config,
933 	.read_temp = bmp280_read_temp,
934 	.read_press = bmp280_read_press,
935 	.read_calib = bmp280_read_calib,
936 };
937 
938 static int bme280_chip_config(struct bmp280_data *data)
939 {
940 	u8 osrs = FIELD_PREP(BMP280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
941 	int ret;
942 
943 	/*
944 	 * Oversampling of humidity must be set before oversampling of
945 	 * temperature/pressure is set to become effective.
946 	 */
947 	ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
948 				  BMP280_OSRS_HUMIDITY_MASK, osrs);
949 
950 	if (ret < 0)
951 		return ret;
952 
953 	return bmp280_chip_config(data);
954 }
955 
956 static const struct bmp280_chip_info bme280_chip_info = {
957 	.id_reg = BMP280_REG_ID,
958 	.start_up_time = 2000,
959 	.channels = bmp280_channels,
960 	.num_channels = 3,
961 
962 	.oversampling_temp_avail = bmp280_oversampling_avail,
963 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
964 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
965 
966 	.oversampling_press_avail = bmp280_oversampling_avail,
967 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
968 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
969 
970 	.oversampling_humid_avail = bmp280_oversampling_avail,
971 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
972 	.oversampling_humid_default = BMP280_OSRS_HUMIDITY_16X - 1,
973 
974 	.chip_config = bme280_chip_config,
975 	.read_temp = bmp280_read_temp,
976 	.read_press = bmp280_read_press,
977 	.read_humid = bmp280_read_humid,
978 	.read_calib = bme280_read_calib,
979 };
980 
981 /*
982  * Helper function to send a command to BMP3XX sensors.
983  *
984  * Sensor processes commands written to the CMD register and signals
985  * execution result through "cmd_rdy" and "cmd_error" flags available on
986  * STATUS and ERROR registers.
987  */
988 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
989 {
990 	unsigned int reg;
991 	int ret;
992 
993 	/* Check if device is ready to process a command */
994 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
995 	if (ret) {
996 		dev_err(data->dev, "failed to read error register\n");
997 		return ret;
998 	}
999 	if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
1000 		dev_err(data->dev, "device is not ready to accept commands\n");
1001 		return -EBUSY;
1002 	}
1003 
1004 	/* Send command to process */
1005 	ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
1006 	if (ret) {
1007 		dev_err(data->dev, "failed to send command to device\n");
1008 		return ret;
1009 	}
1010 	/* Wait for 2ms for command to be processed */
1011 	usleep_range(data->start_up_time, data->start_up_time + 100);
1012 	/* Check for command processing error */
1013 	ret = regmap_read(data->regmap, BMP380_REG_ERROR, &reg);
1014 	if (ret) {
1015 		dev_err(data->dev, "error reading ERROR reg\n");
1016 		return ret;
1017 	}
1018 	if (reg & BMP380_ERR_CMD_MASK) {
1019 		dev_err(data->dev, "error processing command 0x%X\n", cmd);
1020 		return -EINVAL;
1021 	}
1022 
1023 	return 0;
1024 }
1025 
1026 /*
1027  * Returns temperature in Celsius dregrees, resolution is 0.01º C. Output value of
1028  * "5123" equals 51.2º C. t_fine carries fine temperature as global value.
1029  *
1030  * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
1031  * https://github.com/BoschSensortec/BMP3-Sensor-API.
1032  */
1033 static s32 bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
1034 {
1035 	s64 var1, var2, var3, var4, var5, var6, comp_temp;
1036 	struct bmp380_calib *calib = &data->calib.bmp380;
1037 
1038 	var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
1039 	var2 = var1 * ((s64) calib->T2);
1040 	var3 = var1 * var1;
1041 	var4 = var3 * ((s64) calib->T3);
1042 	var5 = (var2 << 18) + var4;
1043 	var6 = var5 >> 32;
1044 	data->t_fine = (s32) var6;
1045 	comp_temp = (var6 * 25) >> 14;
1046 
1047 	comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
1048 	return (s32) comp_temp;
1049 }
1050 
1051 /*
1052  * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
1053  * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
1054  *
1055  * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
1056  * https://github.com/BoschSensortec/BMP3-Sensor-API.
1057  */
1058 static u32 bmp380_compensate_press(struct bmp280_data *data, u32 adc_press)
1059 {
1060 	s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
1061 	struct bmp380_calib *calib = &data->calib.bmp380;
1062 	u32 comp_press;
1063 
1064 	var1 = (s64)data->t_fine * (s64)data->t_fine;
1065 	var2 = var1 >> 6;
1066 	var3 = (var2 * ((s64) data->t_fine)) >> 8;
1067 	var4 = ((s64)calib->P8 * var3) >> 5;
1068 	var5 = ((s64)calib->P7 * var1) << 4;
1069 	var6 = ((s64)calib->P6 * (s64)data->t_fine) << 22;
1070 	offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
1071 	var2 = ((s64)calib->P4 * var3) >> 5;
1072 	var4 = ((s64)calib->P3 * var1) << 2;
1073 	var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
1074 	       ((s64)data->t_fine << 21);
1075 	sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
1076 			var2 + var4 + var5;
1077 	var1 = (sensitivity >> 24) * (s64)adc_press;
1078 	var2 = (s64)calib->P10 * (s64)data->t_fine;
1079 	var3 = var2 + ((s64)calib->P9 << 16);
1080 	var4 = (var3 * (s64)adc_press) >> 13;
1081 
1082 	/*
1083 	 * Dividing by 10 followed by multiplying by 10 to avoid
1084 	 * possible overflow caused by (uncomp_data->pressure * partial_data4).
1085 	 */
1086 	var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
1087 	var5 *= 10;
1088 	var6 = (s64)adc_press * (s64)adc_press;
1089 	var2 = ((s64)calib->P11 * var6) >> 16;
1090 	var3 = (var2 * (s64)adc_press) >> 7;
1091 	var4 = (offset >> 2) + var1 + var5 + var3;
1092 	comp_press = ((u64)var4 * 25) >> 40;
1093 
1094 	comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
1095 	return comp_press;
1096 }
1097 
1098 static int bmp380_read_temp(struct bmp280_data *data, int *val)
1099 {
1100 	s32 comp_temp;
1101 	u32 adc_temp;
1102 	int ret;
1103 
1104 	ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1105 			       data->buf, sizeof(data->buf));
1106 	if (ret) {
1107 		dev_err(data->dev, "failed to read temperature\n");
1108 		return ret;
1109 	}
1110 
1111 	adc_temp = get_unaligned_le24(data->buf);
1112 	if (adc_temp == BMP380_TEMP_SKIPPED) {
1113 		dev_err(data->dev, "reading temperature skipped\n");
1114 		return -EIO;
1115 	}
1116 	comp_temp = bmp380_compensate_temp(data, adc_temp);
1117 
1118 	/*
1119 	 * Val might be NULL if we're called by the read_press routine,
1120 	 * who only cares about the carry over t_fine value.
1121 	 */
1122 	if (val) {
1123 		/* IIO reports temperatures in milli Celsius */
1124 		*val = comp_temp * 10;
1125 		return IIO_VAL_INT;
1126 	}
1127 
1128 	return 0;
1129 }
1130 
1131 static int bmp380_read_press(struct bmp280_data *data, int *val, int *val2)
1132 {
1133 	s32 comp_press;
1134 	u32 adc_press;
1135 	int ret;
1136 
1137 	/* Read and compensate for temperature so we get a reading of t_fine */
1138 	ret = bmp380_read_temp(data, NULL);
1139 	if (ret)
1140 		return ret;
1141 
1142 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1143 			       data->buf, sizeof(data->buf));
1144 	if (ret) {
1145 		dev_err(data->dev, "failed to read pressure\n");
1146 		return ret;
1147 	}
1148 
1149 	adc_press = get_unaligned_le24(data->buf);
1150 	if (adc_press == BMP380_PRESS_SKIPPED) {
1151 		dev_err(data->dev, "reading pressure skipped\n");
1152 		return -EIO;
1153 	}
1154 	comp_press = bmp380_compensate_press(data, adc_press);
1155 
1156 	*val = comp_press;
1157 	/* Compensated pressure is in cPa (centipascals) */
1158 	*val2 = 100000;
1159 
1160 	return IIO_VAL_FRACTIONAL;
1161 }
1162 
1163 static int bmp380_read_calib(struct bmp280_data *data)
1164 {
1165 	struct bmp380_calib *calib = &data->calib.bmp380;
1166 	int ret;
1167 
1168 	/* Read temperature and pressure calibration data */
1169 	ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
1170 			       data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
1171 	if (ret) {
1172 		dev_err(data->dev,
1173 			"failed to read temperature calibration parameters\n");
1174 		return ret;
1175 	}
1176 
1177 	/* Toss the temperature calibration data into the entropy pool */
1178 	add_device_randomness(data->bmp380_cal_buf, sizeof(data->bmp380_cal_buf));
1179 
1180 	/* Parse calibration values */
1181 	calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
1182 	calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
1183 	calib->T3 = data->bmp380_cal_buf[BMP380_T3];
1184 	calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
1185 	calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
1186 	calib->P3 = data->bmp380_cal_buf[BMP380_P3];
1187 	calib->P4 = data->bmp380_cal_buf[BMP380_P4];
1188 	calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
1189 	calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
1190 	calib->P7 = data->bmp380_cal_buf[BMP380_P7];
1191 	calib->P8 = data->bmp380_cal_buf[BMP380_P8];
1192 	calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
1193 	calib->P10 = data->bmp380_cal_buf[BMP380_P10];
1194 	calib->P11 = data->bmp380_cal_buf[BMP380_P11];
1195 
1196 	return 0;
1197 }
1198 
1199 static const int bmp380_odr_table[][2] = {
1200 	[BMP380_ODR_200HZ]	= {200, 0},
1201 	[BMP380_ODR_100HZ]	= {100, 0},
1202 	[BMP380_ODR_50HZ]	= {50, 0},
1203 	[BMP380_ODR_25HZ]	= {25, 0},
1204 	[BMP380_ODR_12_5HZ]	= {12, 500000},
1205 	[BMP380_ODR_6_25HZ]	= {6, 250000},
1206 	[BMP380_ODR_3_125HZ]	= {3, 125000},
1207 	[BMP380_ODR_1_5625HZ]	= {1, 562500},
1208 	[BMP380_ODR_0_78HZ]	= {0, 781250},
1209 	[BMP380_ODR_0_39HZ]	= {0, 390625},
1210 	[BMP380_ODR_0_2HZ]	= {0, 195313},
1211 	[BMP380_ODR_0_1HZ]	= {0, 97656},
1212 	[BMP380_ODR_0_05HZ]	= {0, 48828},
1213 	[BMP380_ODR_0_02HZ]	= {0, 24414},
1214 	[BMP380_ODR_0_01HZ]	= {0, 12207},
1215 	[BMP380_ODR_0_006HZ]	= {0, 6104},
1216 	[BMP380_ODR_0_003HZ]	= {0, 3052},
1217 	[BMP380_ODR_0_0015HZ]	= {0, 1526},
1218 };
1219 
1220 static int bmp380_chip_config(struct bmp280_data *data)
1221 {
1222 	bool change = false, aux;
1223 	unsigned int tmp;
1224 	u8 osrs;
1225 	int ret;
1226 
1227 	/* Configure power control register */
1228 	ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1229 				 BMP380_CTRL_SENSORS_MASK,
1230 				 BMP380_CTRL_SENSORS_PRESS_EN |
1231 				 BMP380_CTRL_SENSORS_TEMP_EN);
1232 	if (ret) {
1233 		dev_err(data->dev,
1234 			"failed to write operation control register\n");
1235 		return ret;
1236 	}
1237 
1238 	/* Configure oversampling */
1239 	osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1240 	       FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1241 
1242 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1243 				       BMP380_OSRS_TEMP_MASK |
1244 				       BMP380_OSRS_PRESS_MASK,
1245 				       osrs, &aux);
1246 	if (ret) {
1247 		dev_err(data->dev, "failed to write oversampling register\n");
1248 		return ret;
1249 	}
1250 	change = change || aux;
1251 
1252 	/* Configure output data rate */
1253 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1254 				       BMP380_ODRS_MASK, data->sampling_freq, &aux);
1255 	if (ret) {
1256 		dev_err(data->dev, "failed to write ODR selection register\n");
1257 		return ret;
1258 	}
1259 	change = change || aux;
1260 
1261 	/* Set filter data */
1262 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK,
1263 				       FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff),
1264 				       &aux);
1265 	if (ret) {
1266 		dev_err(data->dev, "failed to write config register\n");
1267 		return ret;
1268 	}
1269 	change = change || aux;
1270 
1271 	if (change) {
1272 		/*
1273 		 * The configurations errors are detected on the fly during a measurement
1274 		 * cycle. If the sampling frequency is too low, it's faster to reset
1275 		 * the measurement loop than wait until the next measurement is due.
1276 		 *
1277 		 * Resets sensor measurement loop toggling between sleep and normal
1278 		 * operating modes.
1279 		 */
1280 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1281 					BMP380_MODE_MASK,
1282 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_SLEEP));
1283 		if (ret) {
1284 			dev_err(data->dev, "failed to set sleep mode\n");
1285 			return ret;
1286 		}
1287 		usleep_range(2000, 2500);
1288 		ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1289 					BMP380_MODE_MASK,
1290 					FIELD_PREP(BMP380_MODE_MASK, BMP380_MODE_NORMAL));
1291 		if (ret) {
1292 			dev_err(data->dev, "failed to set normal mode\n");
1293 			return ret;
1294 		}
1295 		/*
1296 		 * Waits for measurement before checking configuration error flag.
1297 		 * Selected longest measure time indicated in section 3.9.1
1298 		 * in the datasheet.
1299 		 */
1300 		msleep(80);
1301 
1302 		/* Check config error flag */
1303 		ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1304 		if (ret) {
1305 			dev_err(data->dev,
1306 				"failed to read error register\n");
1307 			return ret;
1308 		}
1309 		if (tmp & BMP380_ERR_CONF_MASK) {
1310 			dev_warn(data->dev,
1311 				"sensor flagged configuration as incompatible\n");
1312 			return -EINVAL;
1313 		}
1314 	}
1315 
1316 	return 0;
1317 }
1318 
1319 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
1320 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
1321 
1322 static const struct bmp280_chip_info bmp380_chip_info = {
1323 	.id_reg = BMP380_REG_ID,
1324 	.start_up_time = 2000,
1325 	.channels = bmp380_channels,
1326 	.num_channels = 2,
1327 
1328 	.oversampling_temp_avail = bmp380_oversampling_avail,
1329 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1330 	.oversampling_temp_default = ilog2(1),
1331 
1332 	.oversampling_press_avail = bmp380_oversampling_avail,
1333 	.num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1334 	.oversampling_press_default = ilog2(4),
1335 
1336 	.sampling_freq_avail = bmp380_odr_table,
1337 	.num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
1338 	.sampling_freq_default = BMP380_ODR_50HZ,
1339 
1340 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1341 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1342 	.iir_filter_coeff_default = 2,
1343 
1344 	.chip_config = bmp380_chip_config,
1345 	.read_temp = bmp380_read_temp,
1346 	.read_press = bmp380_read_press,
1347 	.read_calib = bmp380_read_calib,
1348 };
1349 
1350 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
1351 {
1352 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
1353 	unsigned int delay_us;
1354 	unsigned int ctrl;
1355 	int ret;
1356 
1357 	if (data->use_eoc)
1358 		reinit_completion(&data->done);
1359 
1360 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
1361 	if (ret)
1362 		return ret;
1363 
1364 	if (data->use_eoc) {
1365 		/*
1366 		 * If we have a completion interrupt, use it, wait up to
1367 		 * 100ms. The longest conversion time listed is 76.5 ms for
1368 		 * advanced resolution mode.
1369 		 */
1370 		ret = wait_for_completion_timeout(&data->done,
1371 						  1 + msecs_to_jiffies(100));
1372 		if (!ret)
1373 			dev_err(data->dev, "timeout waiting for completion\n");
1374 	} else {
1375 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
1376 			delay_us = 4500;
1377 		else
1378 			delay_us =
1379 				conversion_time_max[data->oversampling_press];
1380 
1381 		usleep_range(delay_us, delay_us + 1000);
1382 	}
1383 
1384 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
1385 	if (ret)
1386 		return ret;
1387 
1388 	/* The value of this bit reset to "0" after conversion is complete */
1389 	if (ctrl & BMP180_MEAS_SCO)
1390 		return -EIO;
1391 
1392 	return 0;
1393 }
1394 
1395 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
1396 {
1397 	int ret;
1398 
1399 	ret = bmp180_measure(data,
1400 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
1401 			     BMP180_MEAS_SCO);
1402 	if (ret)
1403 		return ret;
1404 
1405 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1406 			       &data->be16, sizeof(data->be16));
1407 	if (ret)
1408 		return ret;
1409 
1410 	*val = be16_to_cpu(data->be16);
1411 
1412 	return 0;
1413 }
1414 
1415 static int bmp180_read_calib(struct bmp280_data *data)
1416 {
1417 	struct bmp180_calib *calib = &data->calib.bmp180;
1418 	int ret;
1419 	int i;
1420 
1421 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
1422 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1423 
1424 	if (ret < 0)
1425 		return ret;
1426 
1427 	/* None of the words has the value 0 or 0xFFFF */
1428 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
1429 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
1430 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
1431 			return -EIO;
1432 	}
1433 
1434 	/* Toss the calibration data into the entropy pool */
1435 	add_device_randomness(data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
1436 
1437 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
1438 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
1439 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
1440 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
1441 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
1442 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
1443 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
1444 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
1445 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
1446 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
1447 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
1448 
1449 	return 0;
1450 }
1451 
1452 /*
1453  * Returns temperature in DegC, resolution is 0.1 DegC.
1454  * t_fine carries fine temperature as global value.
1455  *
1456  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1457  */
1458 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
1459 {
1460 	struct bmp180_calib *calib = &data->calib.bmp180;
1461 	s32 x1, x2;
1462 
1463 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
1464 	x2 = (calib->MC << 11) / (x1 + calib->MD);
1465 	data->t_fine = x1 + x2;
1466 
1467 	return (data->t_fine + 8) >> 4;
1468 }
1469 
1470 static int bmp180_read_temp(struct bmp280_data *data, int *val)
1471 {
1472 	s32 adc_temp, comp_temp;
1473 	int ret;
1474 
1475 	ret = bmp180_read_adc_temp(data, &adc_temp);
1476 	if (ret)
1477 		return ret;
1478 
1479 	comp_temp = bmp180_compensate_temp(data, adc_temp);
1480 
1481 	/*
1482 	 * val might be NULL if we're called by the read_press routine,
1483 	 * who only cares about the carry over t_fine value.
1484 	 */
1485 	if (val) {
1486 		*val = comp_temp * 100;
1487 		return IIO_VAL_INT;
1488 	}
1489 
1490 	return 0;
1491 }
1492 
1493 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
1494 {
1495 	u8 oss = data->oversampling_press;
1496 	int ret;
1497 
1498 	ret = bmp180_measure(data,
1499 			     FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
1500 			     FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
1501 			     BMP180_MEAS_SCO);
1502 	if (ret)
1503 		return ret;
1504 
1505 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
1506 			       data->buf, sizeof(data->buf));
1507 	if (ret)
1508 		return ret;
1509 
1510 	*val = get_unaligned_be24(data->buf) >> (8 - oss);
1511 
1512 	return 0;
1513 }
1514 
1515 /*
1516  * Returns pressure in Pa, resolution is 1 Pa.
1517  *
1518  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
1519  */
1520 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
1521 {
1522 	struct bmp180_calib *calib = &data->calib.bmp180;
1523 	s32 oss = data->oversampling_press;
1524 	s32 x1, x2, x3, p;
1525 	s32 b3, b6;
1526 	u32 b4, b7;
1527 
1528 	b6 = data->t_fine - 4000;
1529 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
1530 	x2 = calib->AC2 * b6 >> 11;
1531 	x3 = x1 + x2;
1532 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
1533 	x1 = calib->AC3 * b6 >> 13;
1534 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
1535 	x3 = (x1 + x2 + 2) >> 2;
1536 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
1537 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
1538 	if (b7 < 0x80000000)
1539 		p = (b7 * 2) / b4;
1540 	else
1541 		p = (b7 / b4) * 2;
1542 
1543 	x1 = (p >> 8) * (p >> 8);
1544 	x1 = (x1 * 3038) >> 16;
1545 	x2 = (-7357 * p) >> 16;
1546 
1547 	return p + ((x1 + x2 + 3791) >> 4);
1548 }
1549 
1550 static int bmp180_read_press(struct bmp280_data *data,
1551 			     int *val, int *val2)
1552 {
1553 	u32 comp_press;
1554 	s32 adc_press;
1555 	int ret;
1556 
1557 	/* Read and compensate temperature so we get a reading of t_fine. */
1558 	ret = bmp180_read_temp(data, NULL);
1559 	if (ret)
1560 		return ret;
1561 
1562 	ret = bmp180_read_adc_press(data, &adc_press);
1563 	if (ret)
1564 		return ret;
1565 
1566 	comp_press = bmp180_compensate_press(data, adc_press);
1567 
1568 	*val = comp_press;
1569 	*val2 = 1000;
1570 
1571 	return IIO_VAL_FRACTIONAL;
1572 }
1573 
1574 static int bmp180_chip_config(struct bmp280_data *data)
1575 {
1576 	return 0;
1577 }
1578 
1579 static const int bmp180_oversampling_temp_avail[] = { 1 };
1580 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1581 
1582 static const struct bmp280_chip_info bmp180_chip_info = {
1583 	.id_reg = BMP280_REG_ID,
1584 	.start_up_time = 2000,
1585 	.channels = bmp280_channels,
1586 	.num_channels = 2,
1587 
1588 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
1589 	.num_oversampling_temp_avail =
1590 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
1591 	.oversampling_temp_default = 0,
1592 
1593 	.oversampling_press_avail = bmp180_oversampling_press_avail,
1594 	.num_oversampling_press_avail =
1595 		ARRAY_SIZE(bmp180_oversampling_press_avail),
1596 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
1597 
1598 	.chip_config = bmp180_chip_config,
1599 	.read_temp = bmp180_read_temp,
1600 	.read_press = bmp180_read_press,
1601 	.read_calib = bmp180_read_calib,
1602 };
1603 
1604 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
1605 {
1606 	struct bmp280_data *data = d;
1607 
1608 	complete(&data->done);
1609 
1610 	return IRQ_HANDLED;
1611 }
1612 
1613 static int bmp085_fetch_eoc_irq(struct device *dev,
1614 				const char *name,
1615 				int irq,
1616 				struct bmp280_data *data)
1617 {
1618 	unsigned long irq_trig;
1619 	int ret;
1620 
1621 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
1622 	if (irq_trig != IRQF_TRIGGER_RISING) {
1623 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
1624 		irq_trig = IRQF_TRIGGER_RISING;
1625 	}
1626 
1627 	init_completion(&data->done);
1628 
1629 	ret = devm_request_threaded_irq(dev,
1630 			irq,
1631 			bmp085_eoc_irq,
1632 			NULL,
1633 			irq_trig,
1634 			name,
1635 			data);
1636 	if (ret) {
1637 		/* Bail out without IRQ but keep the driver in place */
1638 		dev_err(dev, "unable to request DRDY IRQ\n");
1639 		return 0;
1640 	}
1641 
1642 	data->use_eoc = true;
1643 	return 0;
1644 }
1645 
1646 static void bmp280_pm_disable(void *data)
1647 {
1648 	struct device *dev = data;
1649 
1650 	pm_runtime_get_sync(dev);
1651 	pm_runtime_put_noidle(dev);
1652 	pm_runtime_disable(dev);
1653 }
1654 
1655 static void bmp280_regulators_disable(void *data)
1656 {
1657 	struct regulator_bulk_data *supplies = data;
1658 
1659 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
1660 }
1661 
1662 int bmp280_common_probe(struct device *dev,
1663 			struct regmap *regmap,
1664 			unsigned int chip,
1665 			const char *name,
1666 			int irq)
1667 {
1668 	const struct bmp280_chip_info *chip_info;
1669 	struct iio_dev *indio_dev;
1670 	struct bmp280_data *data;
1671 	struct gpio_desc *gpiod;
1672 	unsigned int chip_id;
1673 	int ret;
1674 
1675 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1676 	if (!indio_dev)
1677 		return -ENOMEM;
1678 
1679 	data = iio_priv(indio_dev);
1680 	mutex_init(&data->lock);
1681 	data->dev = dev;
1682 
1683 	indio_dev->name = name;
1684 	indio_dev->info = &bmp280_info;
1685 	indio_dev->modes = INDIO_DIRECT_MODE;
1686 
1687 	switch (chip) {
1688 	case BMP180_CHIP_ID:
1689 		chip_info = &bmp180_chip_info;
1690 		break;
1691 	case BMP280_CHIP_ID:
1692 		chip_info = &bmp280_chip_info;
1693 		break;
1694 	case BME280_CHIP_ID:
1695 		chip_info = &bme280_chip_info;
1696 		break;
1697 	case BMP380_CHIP_ID:
1698 		chip_info = &bmp380_chip_info;
1699 		break;
1700 	default:
1701 		return -EINVAL;
1702 	}
1703 	data->chip_info = chip_info;
1704 
1705 	/* Apply initial values from chip info structure */
1706 	indio_dev->channels = chip_info->channels;
1707 	indio_dev->num_channels = chip_info->num_channels;
1708 	data->oversampling_press = chip_info->oversampling_press_default;
1709 	data->oversampling_humid = chip_info->oversampling_humid_default;
1710 	data->oversampling_temp = chip_info->oversampling_temp_default;
1711 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
1712 	data->sampling_freq = chip_info->sampling_freq_default;
1713 	data->start_up_time = chip_info->start_up_time;
1714 
1715 	/* Bring up regulators */
1716 	regulator_bulk_set_supply_names(data->supplies,
1717 					bmp280_supply_names,
1718 					BMP280_NUM_SUPPLIES);
1719 
1720 	ret = devm_regulator_bulk_get(dev,
1721 				      BMP280_NUM_SUPPLIES, data->supplies);
1722 	if (ret) {
1723 		dev_err(dev, "failed to get regulators\n");
1724 		return ret;
1725 	}
1726 
1727 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1728 	if (ret) {
1729 		dev_err(dev, "failed to enable regulators\n");
1730 		return ret;
1731 	}
1732 
1733 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
1734 				       data->supplies);
1735 	if (ret)
1736 		return ret;
1737 
1738 	/* Wait to make sure we started up properly */
1739 	usleep_range(data->start_up_time, data->start_up_time + 100);
1740 
1741 	/* Bring chip out of reset if there is an assigned GPIO line */
1742 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
1743 	/* Deassert the signal */
1744 	if (gpiod) {
1745 		dev_info(dev, "release reset\n");
1746 		gpiod_set_value(gpiod, 0);
1747 	}
1748 
1749 	data->regmap = regmap;
1750 
1751 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
1752 	if (ret < 0)
1753 		return ret;
1754 	if (chip_id != chip) {
1755 		dev_err(dev, "bad chip id: expected %x got %x\n",
1756 			chip, chip_id);
1757 		return -EINVAL;
1758 	}
1759 
1760 	/* BMP3xx requires soft-reset as part of initialization */
1761 	if (chip_id == BMP380_CHIP_ID) {
1762 		ret = bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1763 		if (ret < 0)
1764 			return ret;
1765 	}
1766 
1767 	ret = data->chip_info->chip_config(data);
1768 	if (ret < 0)
1769 		return ret;
1770 
1771 	dev_set_drvdata(dev, indio_dev);
1772 
1773 	/*
1774 	 * Some chips have calibration parameters "programmed into the devices'
1775 	 * non-volatile memory during production". Let's read them out at probe
1776 	 * time once. They will not change.
1777 	 */
1778 
1779 	ret = data->chip_info->read_calib(data);
1780 	if (ret < 0)
1781 		return dev_err_probe(data->dev, ret,
1782 				     "failed to read calibration coefficients\n");
1783 
1784 	/*
1785 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1786 	 * however as it happens, the BMP085 shares the chip ID of BMP180
1787 	 * so we look for an IRQ if we have that.
1788 	 */
1789 	if (irq > 0 || (chip_id  == BMP180_CHIP_ID)) {
1790 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1791 		if (ret)
1792 			return ret;
1793 	}
1794 
1795 	/* Enable runtime PM */
1796 	pm_runtime_get_noresume(dev);
1797 	pm_runtime_set_active(dev);
1798 	pm_runtime_enable(dev);
1799 	/*
1800 	 * Set autosuspend to two orders of magnitude larger than the
1801 	 * start-up time.
1802 	 */
1803 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1804 	pm_runtime_use_autosuspend(dev);
1805 	pm_runtime_put(dev);
1806 
1807 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
1808 	if (ret)
1809 		return ret;
1810 
1811 	return devm_iio_device_register(dev, indio_dev);
1812 }
1813 EXPORT_SYMBOL_NS(bmp280_common_probe, IIO_BMP280);
1814 
1815 static int bmp280_runtime_suspend(struct device *dev)
1816 {
1817 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1818 	struct bmp280_data *data = iio_priv(indio_dev);
1819 
1820 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
1821 }
1822 
1823 static int bmp280_runtime_resume(struct device *dev)
1824 {
1825 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1826 	struct bmp280_data *data = iio_priv(indio_dev);
1827 	int ret;
1828 
1829 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1830 	if (ret)
1831 		return ret;
1832 	usleep_range(data->start_up_time, data->start_up_time + 100);
1833 	return data->chip_info->chip_config(data);
1834 }
1835 
1836 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
1837 			  bmp280_runtime_resume, NULL);
1838 
1839 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1840 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1841 MODULE_LICENSE("GPL v2");
1842