xref: /linux/drivers/iio/pressure/bmp280-core.c (revision ca56951352ca26a15e9a62a7e682e56f1c01bf53)
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  * Sensor API:
20  * https://github.com/boschsensortec/BME280_SensorAPI
21  * https://github.com/boschsensortec/BMP3_SensorAPI
22  * https://github.com/boschsensortec/BMP5_SensorAPI
23  *
24  * Notice:
25  * The link to the bmp180 datasheet points to an outdated version missing these changes:
26  * - Changed document referral from ANP015 to BST-MPS-AN004-00 on page 26
27  * - Updated equation for B3 param on section 3.5 to ((((long)AC1 * 4 + X3) << oss) + 2) / 4
28  * - Updated RoHS directive to 2011/65/EU effective 8 June 2011 on page 26
29  */
30 
31 #define pr_fmt(fmt) "bmp280: " fmt
32 
33 #include <linux/bitops.h>
34 #include <linux/bitfield.h>
35 #include <linux/cleanup.h>
36 #include <linux/completion.h>
37 #include <linux/delay.h>
38 #include <linux/device.h>
39 #include <linux/gpio/consumer.h>
40 #include <linux/interrupt.h>
41 #include <linux/irq.h> /* For irq_get_irq_data() */
42 #include <linux/module.h>
43 #include <linux/nvmem-provider.h>
44 #include <linux/pm_runtime.h>
45 #include <linux/property.h>
46 #include <linux/random.h>
47 #include <linux/regmap.h>
48 #include <linux/regulator/consumer.h>
49 
50 #include <linux/iio/buffer.h>
51 #include <linux/iio/iio.h>
52 #include <linux/iio/trigger.h>
53 #include <linux/iio/trigger_consumer.h>
54 #include <linux/iio/triggered_buffer.h>
55 
56 #include <linux/unaligned.h>
57 
58 #include "bmp280.h"
59 
60 /*
61  * These enums are used for indexing into the array of calibration
62  * coefficients for BMP180.
63  */
64 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
65 
66 enum bmp380_odr {
67 	BMP380_ODR_200HZ,
68 	BMP380_ODR_100HZ,
69 	BMP380_ODR_50HZ,
70 	BMP380_ODR_25HZ,
71 	BMP380_ODR_12_5HZ,
72 	BMP380_ODR_6_25HZ,
73 	BMP380_ODR_3_125HZ,
74 	BMP380_ODR_1_5625HZ,
75 	BMP380_ODR_0_78HZ,
76 	BMP380_ODR_0_39HZ,
77 	BMP380_ODR_0_2HZ,
78 	BMP380_ODR_0_1HZ,
79 	BMP380_ODR_0_05HZ,
80 	BMP380_ODR_0_02HZ,
81 	BMP380_ODR_0_01HZ,
82 	BMP380_ODR_0_006HZ,
83 	BMP380_ODR_0_003HZ,
84 	BMP380_ODR_0_0015HZ,
85 };
86 
87 enum bmp580_odr {
88 	BMP580_ODR_240HZ,
89 	BMP580_ODR_218HZ,
90 	BMP580_ODR_199HZ,
91 	BMP580_ODR_179HZ,
92 	BMP580_ODR_160HZ,
93 	BMP580_ODR_149HZ,
94 	BMP580_ODR_140HZ,
95 	BMP580_ODR_129HZ,
96 	BMP580_ODR_120HZ,
97 	BMP580_ODR_110HZ,
98 	BMP580_ODR_100HZ,
99 	BMP580_ODR_89HZ,
100 	BMP580_ODR_80HZ,
101 	BMP580_ODR_70HZ,
102 	BMP580_ODR_60HZ,
103 	BMP580_ODR_50HZ,
104 	BMP580_ODR_45HZ,
105 	BMP580_ODR_40HZ,
106 	BMP580_ODR_35HZ,
107 	BMP580_ODR_30HZ,
108 	BMP580_ODR_25HZ,
109 	BMP580_ODR_20HZ,
110 	BMP580_ODR_15HZ,
111 	BMP580_ODR_10HZ,
112 	BMP580_ODR_5HZ,
113 	BMP580_ODR_4HZ,
114 	BMP580_ODR_3HZ,
115 	BMP580_ODR_2HZ,
116 	BMP580_ODR_1HZ,
117 	BMP580_ODR_0_5HZ,
118 	BMP580_ODR_0_25HZ,
119 	BMP580_ODR_0_125HZ,
120 };
121 
122 /*
123  * These enums are used for indexing into the array of compensation
124  * parameters for BMP280.
125  */
126 enum { T1, T2, T3, P1, P2, P3, P4, P5, P6, P7, P8, P9 };
127 
128 enum {
129 	/* Temperature calib indexes */
130 	BMP380_T1 = 0,
131 	BMP380_T2 = 2,
132 	BMP380_T3 = 4,
133 	/* Pressure calib indexes */
134 	BMP380_P1 = 5,
135 	BMP380_P2 = 7,
136 	BMP380_P3 = 9,
137 	BMP380_P4 = 10,
138 	BMP380_P5 = 11,
139 	BMP380_P6 = 13,
140 	BMP380_P7 = 15,
141 	BMP380_P8 = 16,
142 	BMP380_P9 = 17,
143 	BMP380_P10 = 19,
144 	BMP380_P11 = 20,
145 };
146 
147 enum bmp280_scan {
148 	BMP280_PRESS,
149 	BMP280_TEMP,
150 	BME280_HUMID,
151 };
152 
153 static const struct iio_chan_spec bmp280_channels[] = {
154 	{
155 		.type = IIO_PRESSURE,
156 		/* PROCESSED maintained for ABI backwards compatibility */
157 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
158 				      BIT(IIO_CHAN_INFO_RAW) |
159 				      BIT(IIO_CHAN_INFO_SCALE) |
160 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
161 		.scan_index = 0,
162 		.scan_type = {
163 			.sign = 'u',
164 			.realbits = 32,
165 			.storagebits = 32,
166 			.endianness = IIO_CPU,
167 		},
168 	},
169 	{
170 		.type = IIO_TEMP,
171 		/* PROCESSED maintained for ABI backwards compatibility */
172 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
173 				      BIT(IIO_CHAN_INFO_RAW) |
174 				      BIT(IIO_CHAN_INFO_SCALE) |
175 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
176 		.scan_index = 1,
177 		.scan_type = {
178 			.sign = 's',
179 			.realbits = 32,
180 			.storagebits = 32,
181 			.endianness = IIO_CPU,
182 		},
183 	},
184 	IIO_CHAN_SOFT_TIMESTAMP(2),
185 };
186 
187 static const struct iio_chan_spec bme280_channels[] = {
188 	{
189 		.type = IIO_PRESSURE,
190 		/* PROCESSED maintained for ABI backwards compatibility */
191 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
192 				      BIT(IIO_CHAN_INFO_RAW) |
193 				      BIT(IIO_CHAN_INFO_SCALE) |
194 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
195 		.scan_index = 0,
196 		.scan_type = {
197 			.sign = 'u',
198 			.realbits = 32,
199 			.storagebits = 32,
200 			.endianness = IIO_CPU,
201 		},
202 	},
203 	{
204 		.type = IIO_TEMP,
205 		/* PROCESSED maintained for ABI backwards compatibility */
206 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
207 				      BIT(IIO_CHAN_INFO_RAW) |
208 				      BIT(IIO_CHAN_INFO_SCALE) |
209 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
210 		.scan_index = 1,
211 		.scan_type = {
212 			.sign = 's',
213 			.realbits = 32,
214 			.storagebits = 32,
215 			.endianness = IIO_CPU,
216 		},
217 	},
218 	{
219 		.type = IIO_HUMIDITYRELATIVE,
220 		/* PROCESSED maintained for ABI backwards compatibility */
221 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
222 				      BIT(IIO_CHAN_INFO_RAW) |
223 				      BIT(IIO_CHAN_INFO_SCALE) |
224 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
225 		.scan_index = 2,
226 		.scan_type = {
227 			.sign = 'u',
228 			.realbits = 32,
229 			.storagebits = 32,
230 			.endianness = IIO_CPU,
231 		},
232 	},
233 	IIO_CHAN_SOFT_TIMESTAMP(3),
234 };
235 
236 static const struct iio_chan_spec bmp380_channels[] = {
237 	{
238 		.type = IIO_PRESSURE,
239 		/* PROCESSED maintained for ABI backwards compatibility */
240 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
241 				      BIT(IIO_CHAN_INFO_RAW) |
242 				      BIT(IIO_CHAN_INFO_SCALE) |
243 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
244 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
245 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
246 		.scan_index = 0,
247 		.scan_type = {
248 			.sign = 'u',
249 			.realbits = 32,
250 			.storagebits = 32,
251 			.endianness = IIO_CPU,
252 		},
253 	},
254 	{
255 		.type = IIO_TEMP,
256 		/* PROCESSED maintained for ABI backwards compatibility */
257 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
258 				      BIT(IIO_CHAN_INFO_RAW) |
259 				      BIT(IIO_CHAN_INFO_SCALE) |
260 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
261 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
262 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
263 		.scan_index = 1,
264 		.scan_type = {
265 			.sign = 's',
266 			.realbits = 32,
267 			.storagebits = 32,
268 			.endianness = IIO_CPU,
269 		},
270 	},
271 	IIO_CHAN_SOFT_TIMESTAMP(2),
272 };
273 
274 static const struct iio_chan_spec bmp580_channels[] = {
275 	{
276 		.type = IIO_PRESSURE,
277 		/* PROCESSED maintained for ABI backwards compatibility */
278 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
279 				      BIT(IIO_CHAN_INFO_RAW) |
280 				      BIT(IIO_CHAN_INFO_SCALE) |
281 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
282 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
283 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
284 		.scan_index = 0,
285 		.scan_type = {
286 			.sign = 'u',
287 			.realbits = 24,
288 			.storagebits = 32,
289 			.endianness = IIO_LE,
290 		},
291 	},
292 	{
293 		.type = IIO_TEMP,
294 		/* PROCESSED maintained for ABI backwards compatibility */
295 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
296 				      BIT(IIO_CHAN_INFO_RAW) |
297 				      BIT(IIO_CHAN_INFO_SCALE) |
298 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
299 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ) |
300 					   BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),
301 		.scan_index = 1,
302 		.scan_type = {
303 			.sign = 's',
304 			.realbits = 24,
305 			.storagebits = 32,
306 			.endianness = IIO_LE,
307 		},
308 	},
309 	IIO_CHAN_SOFT_TIMESTAMP(2),
310 };
311 
312 static int bmp280_read_calib(struct bmp280_data *data)
313 {
314 	struct bmp280_calib *calib = &data->calib.bmp280;
315 	int ret;
316 
317 	/* Read temperature and pressure calibration values. */
318 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
319 			       data->bmp280_cal_buf,
320 			       sizeof(data->bmp280_cal_buf));
321 	if (ret) {
322 		dev_err(data->dev,
323 			"failed to read calibration parameters\n");
324 		return ret;
325 	}
326 
327 	/* Toss calibration data into the entropy pool */
328 	add_device_randomness(data->bmp280_cal_buf,
329 			      sizeof(data->bmp280_cal_buf));
330 
331 	/* Parse temperature calibration values. */
332 	calib->T1 = le16_to_cpu(data->bmp280_cal_buf[T1]);
333 	calib->T2 = le16_to_cpu(data->bmp280_cal_buf[T2]);
334 	calib->T3 = le16_to_cpu(data->bmp280_cal_buf[T3]);
335 
336 	/* Parse pressure calibration values. */
337 	calib->P1 = le16_to_cpu(data->bmp280_cal_buf[P1]);
338 	calib->P2 = le16_to_cpu(data->bmp280_cal_buf[P2]);
339 	calib->P3 = le16_to_cpu(data->bmp280_cal_buf[P3]);
340 	calib->P4 = le16_to_cpu(data->bmp280_cal_buf[P4]);
341 	calib->P5 = le16_to_cpu(data->bmp280_cal_buf[P5]);
342 	calib->P6 = le16_to_cpu(data->bmp280_cal_buf[P6]);
343 	calib->P7 = le16_to_cpu(data->bmp280_cal_buf[P7]);
344 	calib->P8 = le16_to_cpu(data->bmp280_cal_buf[P8]);
345 	calib->P9 = le16_to_cpu(data->bmp280_cal_buf[P9]);
346 
347 	return 0;
348 }
349 
350 /*
351  * These enums are used for indexing into the array of humidity parameters
352  * for BME280. Due to some weird indexing, unaligned BE/LE accesses co-exist in
353  * order to prepare the FIELD_{GET/PREP}() fields. Table 16 in Section 4.2.2 of
354  * the datasheet.
355  */
356 enum { H2 = 0, H3 = 2, H4 = 3, H5 = 4, H6 = 6 };
357 
358 static int bme280_read_calib(struct bmp280_data *data)
359 {
360 	struct bmp280_calib *calib = &data->calib.bmp280;
361 	struct device *dev = data->dev;
362 	s16 h4_upper, h4_lower, tmp_1, tmp_2, tmp_3;
363 	unsigned int tmp;
364 	int ret;
365 
366 	/* Load shared calibration params with bmp280 first */
367 	ret = bmp280_read_calib(data);
368 	if (ret)
369 		return ret;
370 
371 	ret = regmap_read(data->regmap, BME280_REG_COMP_H1, &tmp);
372 	if (ret) {
373 		dev_err(dev, "failed to read H1 comp value\n");
374 		return ret;
375 	}
376 	calib->H1 = tmp;
377 
378 	ret = regmap_bulk_read(data->regmap, BME280_REG_COMP_H2,
379 			       data->bme280_humid_cal_buf,
380 			       sizeof(data->bme280_humid_cal_buf));
381 	if (ret) {
382 		dev_err(dev, "failed to read humidity calibration values\n");
383 		return ret;
384 	}
385 
386 	calib->H2 = get_unaligned_le16(&data->bme280_humid_cal_buf[H2]);
387 	calib->H3 = data->bme280_humid_cal_buf[H3];
388 	tmp_1 = get_unaligned_be16(&data->bme280_humid_cal_buf[H4]);
389 	tmp_2 = FIELD_GET(BME280_COMP_H4_GET_MASK_UP, tmp_1);
390 	h4_upper = FIELD_PREP(BME280_COMP_H4_PREP_MASK_UP, tmp_2);
391 	h4_lower = FIELD_GET(BME280_COMP_H4_MASK_LOW, tmp_1);
392 	calib->H4 = sign_extend32(h4_upper | h4_lower, 11);
393 	tmp_3 = get_unaligned_le16(&data->bme280_humid_cal_buf[H5]);
394 	calib->H5 = sign_extend32(FIELD_GET(BME280_COMP_H5_MASK, tmp_3), 11);
395 	calib->H6 = data->bme280_humid_cal_buf[H6];
396 
397 	return 0;
398 }
399 
400 static int bme280_read_humid_adc(struct bmp280_data *data, u16 *adc_humidity)
401 {
402 	u16 value_humidity;
403 	int ret;
404 
405 	ret = regmap_bulk_read(data->regmap, BME280_REG_HUMIDITY_MSB,
406 			       &data->be16, BME280_NUM_HUMIDITY_BYTES);
407 	if (ret) {
408 		dev_err(data->dev, "failed to read humidity\n");
409 		return ret;
410 	}
411 
412 	value_humidity = be16_to_cpu(data->be16);
413 	if (value_humidity == BMP280_HUMIDITY_SKIPPED) {
414 		dev_err(data->dev, "reading humidity skipped\n");
415 		return -EIO;
416 	}
417 	*adc_humidity = value_humidity;
418 
419 	return 0;
420 }
421 
422 /*
423  * Returns humidity in percent, resolution is 0.01 percent. Output value of
424  * "47445" represents 47445/1024 = 46.333 %RH.
425  *
426  * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
427  */
428 static u32 bme280_compensate_humidity(struct bmp280_data *data,
429 				      u16 adc_humidity, s32 t_fine)
430 {
431 	struct bmp280_calib *calib = &data->calib.bmp280;
432 	s32 var;
433 
434 	var = t_fine - (s32)76800;
435 	var = (((((s32)adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
436 		+ (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
437 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
438 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
439 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
440 
441 	var = clamp_val(var, 0, 419430400);
442 
443 	return var >> 12;
444 }
445 
446 static int bmp280_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
447 {
448 	u32 value_temp;
449 	int ret;
450 
451 	ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
452 			       data->buf, BMP280_NUM_TEMP_BYTES);
453 	if (ret) {
454 		dev_err(data->dev, "failed to read temperature\n");
455 		return ret;
456 	}
457 
458 	value_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
459 	if (value_temp == BMP280_TEMP_SKIPPED) {
460 		dev_err(data->dev, "reading temperature skipped\n");
461 		return -EIO;
462 	}
463 	*adc_temp = value_temp;
464 
465 	return 0;
466 }
467 
468 /*
469  * Returns temperature in DegC, resolution is 0.01 DegC.  Output value of
470  * "5123" equals 51.23 DegC.  t_fine carries fine temperature as global
471  * value.
472  *
473  * Taken from datasheet, Section 3.11.3, "Compensation formula".
474  */
475 static s32 bmp280_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
476 {
477 	struct bmp280_calib *calib = &data->calib.bmp280;
478 	s32 var1, var2;
479 
480 	var1 = (((((s32)adc_temp) >> 3) - ((s32)calib->T1 << 1)) *
481 		((s32)calib->T2)) >> 11;
482 	var2 = (((((((s32)adc_temp) >> 4) - ((s32)calib->T1)) *
483 		  ((((s32)adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
484 		((s32)calib->T3))) >> 14;
485 	return var1 + var2; /* t_fine = var1 + var2 */
486 }
487 
488 static int bmp280_get_t_fine(struct bmp280_data *data, s32 *t_fine)
489 {
490 	u32 adc_temp;
491 	int ret;
492 
493 	ret = bmp280_read_temp_adc(data, &adc_temp);
494 	if (ret)
495 		return ret;
496 
497 	*t_fine = bmp280_calc_t_fine(data, adc_temp);
498 
499 	return 0;
500 }
501 
502 static s32 bmp280_compensate_temp(struct bmp280_data *data, u32 adc_temp)
503 {
504 	return (bmp280_calc_t_fine(data, adc_temp) * 5 + 128) / 256;
505 }
506 
507 static int bmp280_read_press_adc(struct bmp280_data *data, u32 *adc_press)
508 {
509 	u32 value_press;
510 	int ret;
511 
512 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
513 			       data->buf, BMP280_NUM_PRESS_BYTES);
514 	if (ret) {
515 		dev_err(data->dev, "failed to read pressure\n");
516 		return ret;
517 	}
518 
519 	value_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(data->buf));
520 	if (value_press == BMP280_PRESS_SKIPPED) {
521 		dev_err(data->dev, "reading pressure skipped\n");
522 		return -EIO;
523 	}
524 	*adc_press = value_press;
525 
526 	return 0;
527 }
528 
529 /*
530  * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
531  * integer bits and 8 fractional bits).  Output value of "24674867"
532  * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
533  *
534  * Taken from datasheet, Section 3.11.3, "Compensation formula".
535  */
536 static u32 bmp280_compensate_press(struct bmp280_data *data,
537 				   u32 adc_press, s32 t_fine)
538 {
539 	struct bmp280_calib *calib = &data->calib.bmp280;
540 	s64 var1, var2, p;
541 
542 	var1 = ((s64)t_fine) - 128000;
543 	var2 = var1 * var1 * (s64)calib->P6;
544 	var2 += (var1 * (s64)calib->P5) << 17;
545 	var2 += ((s64)calib->P4) << 35;
546 	var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
547 		((var1 * (s64)calib->P2) << 12);
548 	var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
549 
550 	if (var1 == 0)
551 		return 0;
552 
553 	p = ((((s64)1048576 - (s32)adc_press) << 31) - var2) * 3125;
554 	p = div64_s64(p, var1);
555 	var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
556 	var2 = ((s64)(calib->P8) * p) >> 19;
557 	p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
558 
559 	return (u32)p;
560 }
561 
562 static int bmp280_read_temp(struct bmp280_data *data, s32 *comp_temp)
563 {
564 	u32 adc_temp;
565 	int ret;
566 
567 	ret = bmp280_read_temp_adc(data, &adc_temp);
568 	if (ret)
569 		return ret;
570 
571 	*comp_temp = bmp280_compensate_temp(data, adc_temp);
572 
573 	return 0;
574 }
575 
576 static int bmp280_read_press(struct bmp280_data *data, u32 *comp_press)
577 {
578 	u32 adc_press;
579 	s32 t_fine;
580 	int ret;
581 
582 	ret = bmp280_get_t_fine(data, &t_fine);
583 	if (ret)
584 		return ret;
585 
586 	ret = bmp280_read_press_adc(data, &adc_press);
587 	if (ret)
588 		return ret;
589 
590 	*comp_press = bmp280_compensate_press(data, adc_press, t_fine);
591 
592 	return 0;
593 }
594 
595 static int bme280_read_humid(struct bmp280_data *data, u32 *comp_humidity)
596 {
597 	u16 adc_humidity;
598 	s32 t_fine;
599 	int ret;
600 
601 	ret = bmp280_get_t_fine(data, &t_fine);
602 	if (ret)
603 		return ret;
604 
605 	ret = bme280_read_humid_adc(data, &adc_humidity);
606 	if (ret)
607 		return ret;
608 
609 	*comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
610 
611 	return 0;
612 }
613 
614 static int bmp280_read_raw_impl(struct iio_dev *indio_dev,
615 				struct iio_chan_spec const *chan,
616 				int *val, int *val2, long mask)
617 {
618 	struct bmp280_data *data = iio_priv(indio_dev);
619 	int chan_value;
620 	int ret;
621 
622 	guard(mutex)(&data->lock);
623 
624 	switch (mask) {
625 	case IIO_CHAN_INFO_PROCESSED:
626 		ret = data->chip_info->set_mode(data, BMP280_FORCED);
627 		if (ret)
628 			return ret;
629 
630 		ret = data->chip_info->wait_conv(data);
631 		if (ret)
632 			return ret;
633 
634 		switch (chan->type) {
635 		case IIO_HUMIDITYRELATIVE:
636 			ret = data->chip_info->read_humid(data, &chan_value);
637 			if (ret)
638 				return ret;
639 
640 			*val = data->chip_info->humid_coeffs[0] * chan_value;
641 			*val2 = data->chip_info->humid_coeffs[1];
642 			return data->chip_info->humid_coeffs_type;
643 		case IIO_PRESSURE:
644 			ret = data->chip_info->read_press(data, &chan_value);
645 			if (ret)
646 				return ret;
647 
648 			*val = data->chip_info->press_coeffs[0] * chan_value;
649 			*val2 = data->chip_info->press_coeffs[1];
650 			return data->chip_info->press_coeffs_type;
651 		case IIO_TEMP:
652 			ret = data->chip_info->read_temp(data, &chan_value);
653 			if (ret)
654 				return ret;
655 
656 			*val = data->chip_info->temp_coeffs[0] * chan_value;
657 			*val2 = data->chip_info->temp_coeffs[1];
658 			return data->chip_info->temp_coeffs_type;
659 		default:
660 			return -EINVAL;
661 		}
662 	case IIO_CHAN_INFO_RAW:
663 		ret = data->chip_info->set_mode(data, BMP280_FORCED);
664 		if (ret)
665 			return ret;
666 
667 		ret = data->chip_info->wait_conv(data);
668 		if (ret)
669 			return ret;
670 
671 		switch (chan->type) {
672 		case IIO_HUMIDITYRELATIVE:
673 			ret = data->chip_info->read_humid(data, &chan_value);
674 			if (ret)
675 				return ret;
676 
677 			*val = chan_value;
678 			return IIO_VAL_INT;
679 		case IIO_PRESSURE:
680 			ret = data->chip_info->read_press(data, &chan_value);
681 			if (ret)
682 				return ret;
683 
684 			*val = chan_value;
685 			return IIO_VAL_INT;
686 		case IIO_TEMP:
687 			ret = data->chip_info->read_temp(data, &chan_value);
688 			if (ret)
689 				return ret;
690 
691 			*val = chan_value;
692 			return IIO_VAL_INT;
693 		default:
694 			return -EINVAL;
695 		}
696 	case IIO_CHAN_INFO_SCALE:
697 		switch (chan->type) {
698 		case IIO_HUMIDITYRELATIVE:
699 			*val = data->chip_info->humid_coeffs[0];
700 			*val2 = data->chip_info->humid_coeffs[1];
701 			return data->chip_info->humid_coeffs_type;
702 		case IIO_PRESSURE:
703 			*val = data->chip_info->press_coeffs[0];
704 			*val2 = data->chip_info->press_coeffs[1];
705 			return data->chip_info->press_coeffs_type;
706 		case IIO_TEMP:
707 			*val = data->chip_info->temp_coeffs[0];
708 			*val2 = data->chip_info->temp_coeffs[1];
709 			return data->chip_info->temp_coeffs_type;
710 		default:
711 			return -EINVAL;
712 		}
713 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
714 		switch (chan->type) {
715 		case IIO_HUMIDITYRELATIVE:
716 			*val = 1 << data->oversampling_humid;
717 			return IIO_VAL_INT;
718 		case IIO_PRESSURE:
719 			*val = 1 << data->oversampling_press;
720 			return IIO_VAL_INT;
721 		case IIO_TEMP:
722 			*val = 1 << data->oversampling_temp;
723 			return IIO_VAL_INT;
724 		default:
725 			return -EINVAL;
726 		}
727 	case IIO_CHAN_INFO_SAMP_FREQ:
728 		if (!data->chip_info->sampling_freq_avail)
729 			return -EINVAL;
730 
731 		*val = data->chip_info->sampling_freq_avail[data->sampling_freq][0];
732 		*val2 = data->chip_info->sampling_freq_avail[data->sampling_freq][1];
733 		return IIO_VAL_INT_PLUS_MICRO;
734 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
735 		if (!data->chip_info->iir_filter_coeffs_avail)
736 			return -EINVAL;
737 
738 		*val = (1 << data->iir_filter_coeff) - 1;
739 		return IIO_VAL_INT;
740 	default:
741 		return -EINVAL;
742 	}
743 }
744 
745 static int bmp280_read_raw(struct iio_dev *indio_dev,
746 			   struct iio_chan_spec const *chan,
747 			   int *val, int *val2, long mask)
748 {
749 	struct bmp280_data *data = iio_priv(indio_dev);
750 	int ret;
751 
752 	pm_runtime_get_sync(data->dev);
753 	ret = bmp280_read_raw_impl(indio_dev, chan, val, val2, mask);
754 	pm_runtime_mark_last_busy(data->dev);
755 	pm_runtime_put_autosuspend(data->dev);
756 
757 	return ret;
758 }
759 
760 static int bme280_write_oversampling_ratio_humid(struct bmp280_data *data,
761 						 int val)
762 {
763 	const int *avail = data->chip_info->oversampling_humid_avail;
764 	const int n = data->chip_info->num_oversampling_humid_avail;
765 	int ret, prev;
766 	int i;
767 
768 	for (i = 0; i < n; i++) {
769 		if (avail[i] == val) {
770 			prev = data->oversampling_humid;
771 			data->oversampling_humid = ilog2(val);
772 
773 			ret = data->chip_info->chip_config(data);
774 			if (ret) {
775 				data->oversampling_humid = prev;
776 				data->chip_info->chip_config(data);
777 				return ret;
778 			}
779 			return 0;
780 		}
781 	}
782 	return -EINVAL;
783 }
784 
785 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
786 						int val)
787 {
788 	const int *avail = data->chip_info->oversampling_temp_avail;
789 	const int n = data->chip_info->num_oversampling_temp_avail;
790 	int ret, prev;
791 	int i;
792 
793 	for (i = 0; i < n; i++) {
794 		if (avail[i] == val) {
795 			prev = data->oversampling_temp;
796 			data->oversampling_temp = ilog2(val);
797 
798 			ret = data->chip_info->chip_config(data);
799 			if (ret) {
800 				data->oversampling_temp = prev;
801 				data->chip_info->chip_config(data);
802 				return ret;
803 			}
804 			return 0;
805 		}
806 	}
807 	return -EINVAL;
808 }
809 
810 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
811 						 int val)
812 {
813 	const int *avail = data->chip_info->oversampling_press_avail;
814 	const int n = data->chip_info->num_oversampling_press_avail;
815 	int ret, prev;
816 	int i;
817 
818 	for (i = 0; i < n; i++) {
819 		if (avail[i] == val) {
820 			prev = data->oversampling_press;
821 			data->oversampling_press = ilog2(val);
822 
823 			ret = data->chip_info->chip_config(data);
824 			if (ret) {
825 				data->oversampling_press = prev;
826 				data->chip_info->chip_config(data);
827 				return ret;
828 			}
829 			return 0;
830 		}
831 	}
832 	return -EINVAL;
833 }
834 
835 static int bmp280_write_sampling_frequency(struct bmp280_data *data,
836 					   int val, int val2)
837 {
838 	const int (*avail)[2] = data->chip_info->sampling_freq_avail;
839 	const int n = data->chip_info->num_sampling_freq_avail;
840 	int ret, prev;
841 	int i;
842 
843 	for (i = 0; i < n; i++) {
844 		if (avail[i][0] == val && avail[i][1] == val2) {
845 			prev = data->sampling_freq;
846 			data->sampling_freq = i;
847 
848 			ret = data->chip_info->chip_config(data);
849 			if (ret) {
850 				data->sampling_freq = prev;
851 				data->chip_info->chip_config(data);
852 				return ret;
853 			}
854 			return 0;
855 		}
856 	}
857 	return -EINVAL;
858 }
859 
860 static int bmp280_write_iir_filter_coeffs(struct bmp280_data *data, int val)
861 {
862 	const int *avail = data->chip_info->iir_filter_coeffs_avail;
863 	const int n = data->chip_info->num_iir_filter_coeffs_avail;
864 	int ret, prev;
865 	int i;
866 
867 	for (i = 0; i < n; i++) {
868 		if (avail[i] - 1  == val) {
869 			prev = data->iir_filter_coeff;
870 			data->iir_filter_coeff = i;
871 
872 			ret = data->chip_info->chip_config(data);
873 			if (ret) {
874 				data->iir_filter_coeff = prev;
875 				data->chip_info->chip_config(data);
876 				return ret;
877 
878 			}
879 			return 0;
880 		}
881 	}
882 	return -EINVAL;
883 }
884 
885 static int bmp280_write_raw_impl(struct iio_dev *indio_dev,
886 				 struct iio_chan_spec const *chan,
887 				 int val, int val2, long mask)
888 {
889 	struct bmp280_data *data = iio_priv(indio_dev);
890 
891 	guard(mutex)(&data->lock);
892 
893 	/*
894 	 * Helper functions to update sensor running configuration.
895 	 * If an error happens applying new settings, will try restore
896 	 * previous parameters to ensure the sensor is left in a known
897 	 * working configuration.
898 	 */
899 	switch (mask) {
900 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
901 		switch (chan->type) {
902 		case IIO_HUMIDITYRELATIVE:
903 			return bme280_write_oversampling_ratio_humid(data, val);
904 		case IIO_PRESSURE:
905 			return bmp280_write_oversampling_ratio_press(data, val);
906 		case IIO_TEMP:
907 			return bmp280_write_oversampling_ratio_temp(data, val);
908 		default:
909 			return -EINVAL;
910 		}
911 	case IIO_CHAN_INFO_SAMP_FREQ:
912 		return bmp280_write_sampling_frequency(data, val, val2);
913 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
914 		return bmp280_write_iir_filter_coeffs(data, val);
915 	default:
916 		return -EINVAL;
917 	}
918 }
919 
920 static int bmp280_write_raw(struct iio_dev *indio_dev,
921 			    struct iio_chan_spec const *chan,
922 			    int val, int val2, long mask)
923 {
924 	struct bmp280_data *data = iio_priv(indio_dev);
925 	int ret;
926 
927 	pm_runtime_get_sync(data->dev);
928 	ret = bmp280_write_raw_impl(indio_dev, chan, val, val2, mask);
929 	pm_runtime_mark_last_busy(data->dev);
930 	pm_runtime_put_autosuspend(data->dev);
931 
932 	return ret;
933 }
934 
935 static int bmp280_read_avail(struct iio_dev *indio_dev,
936 			     struct iio_chan_spec const *chan,
937 			     const int **vals, int *type, int *length,
938 			     long mask)
939 {
940 	struct bmp280_data *data = iio_priv(indio_dev);
941 
942 	switch (mask) {
943 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
944 		switch (chan->type) {
945 		case IIO_PRESSURE:
946 			*vals = data->chip_info->oversampling_press_avail;
947 			*length = data->chip_info->num_oversampling_press_avail;
948 			break;
949 		case IIO_TEMP:
950 			*vals = data->chip_info->oversampling_temp_avail;
951 			*length = data->chip_info->num_oversampling_temp_avail;
952 			break;
953 		default:
954 			return -EINVAL;
955 		}
956 		*type = IIO_VAL_INT;
957 		return IIO_AVAIL_LIST;
958 	case IIO_CHAN_INFO_SAMP_FREQ:
959 		*vals = (const int *)data->chip_info->sampling_freq_avail;
960 		*type = IIO_VAL_INT_PLUS_MICRO;
961 		/* Values are stored in a 2D matrix */
962 		*length = data->chip_info->num_sampling_freq_avail;
963 		return IIO_AVAIL_LIST;
964 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
965 		*vals = data->chip_info->iir_filter_coeffs_avail;
966 		*type = IIO_VAL_INT;
967 		*length = data->chip_info->num_iir_filter_coeffs_avail;
968 		return IIO_AVAIL_LIST;
969 	default:
970 		return -EINVAL;
971 	}
972 }
973 
974 static const struct iio_info bmp280_info = {
975 	.read_raw = &bmp280_read_raw,
976 	.read_avail = &bmp280_read_avail,
977 	.write_raw = &bmp280_write_raw,
978 };
979 
980 static const unsigned long bmp280_avail_scan_masks[] = {
981 	BIT(BMP280_TEMP) | BIT(BMP280_PRESS),
982 	0
983 };
984 
985 static const unsigned long bme280_avail_scan_masks[] = {
986 	BIT(BME280_HUMID) | BIT(BMP280_TEMP) | BIT(BMP280_PRESS),
987 	0
988 };
989 
990 static int bmp280_preinit(struct bmp280_data *data)
991 {
992 	struct device *dev = data->dev;
993 	unsigned int reg;
994 	int ret;
995 
996 	ret = regmap_write(data->regmap, BMP280_REG_RESET, BMP280_RST_SOFT_CMD);
997 	if (ret)
998 		return dev_err_probe(dev, ret, "Failed to reset device.\n");
999 
1000 	/*
1001 	 * According to the datasheet in Chapter 1: Specification, Table 2,
1002 	 * after resetting, the device uses the complete power-on sequence so
1003 	 * it needs to wait for the defined start-up time.
1004 	 */
1005 	fsleep(data->start_up_time_us);
1006 
1007 	ret = regmap_read(data->regmap, BMP280_REG_STATUS, &reg);
1008 	if (ret)
1009 		return dev_err_probe(dev, ret, "Failed to read status register.\n");
1010 
1011 	if (reg & BMP280_REG_STATUS_IM_UPDATE)
1012 		return dev_err_probe(dev, -EIO, "Failed to copy NVM contents.\n");
1013 
1014 	return 0;
1015 }
1016 
1017 static const u8 bmp280_operation_mode[] = {
1018 	[BMP280_SLEEP] = BMP280_MODE_SLEEP,
1019 	[BMP280_FORCED] = BMP280_MODE_FORCED,
1020 	[BMP280_NORMAL] = BMP280_MODE_NORMAL,
1021 };
1022 
1023 static int bmp280_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode)
1024 {
1025 	int ret;
1026 
1027 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
1028 				BMP280_MODE_MASK, bmp280_operation_mode[mode]);
1029 	if (ret) {
1030 		dev_err(data->dev, "failed to  write ctrl_meas register.\n");
1031 		return ret;
1032 	}
1033 
1034 	data->op_mode = mode;
1035 
1036 	return 0;
1037 }
1038 
1039 static int bmp280_wait_conv(struct bmp280_data *data)
1040 {
1041 	unsigned int reg, meas_time_us;
1042 	int ret;
1043 
1044 	/* Check if we are using a BME280 device */
1045 	if (data->oversampling_humid)
1046 		meas_time_us = BMP280_PRESS_HUMID_MEAS_OFFSET +
1047 				BIT(data->oversampling_humid) * BMP280_MEAS_DUR;
1048 
1049 	else
1050 		meas_time_us = 0;
1051 
1052 	/* Pressure measurement time */
1053 	meas_time_us += BMP280_PRESS_HUMID_MEAS_OFFSET +
1054 			BIT(data->oversampling_press) * BMP280_MEAS_DUR;
1055 
1056 	/* Temperature measurement time */
1057 	meas_time_us += BIT(data->oversampling_temp) * BMP280_MEAS_DUR;
1058 
1059 	/* Waiting time according to the BM(P/E)2 Sensor API */
1060 	fsleep(meas_time_us);
1061 
1062 	ret = regmap_read(data->regmap, BMP280_REG_STATUS, &reg);
1063 	if (ret) {
1064 		dev_err(data->dev, "failed to read status register.\n");
1065 		return ret;
1066 	}
1067 
1068 	if (reg & BMP280_REG_STATUS_MEAS_BIT) {
1069 		dev_err(data->dev, "Measurement cycle didn't complete.\n");
1070 		return -EBUSY;
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 static int bmp280_chip_config(struct bmp280_data *data)
1077 {
1078 	u8 osrs = FIELD_PREP(BMP280_OSRS_TEMP_MASK, data->oversampling_temp + 1) |
1079 		  FIELD_PREP(BMP280_OSRS_PRESS_MASK, data->oversampling_press + 1);
1080 	int ret;
1081 
1082 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
1083 				BMP280_OSRS_TEMP_MASK |
1084 				BMP280_OSRS_PRESS_MASK |
1085 				BMP280_MODE_MASK,
1086 				osrs | BMP280_MODE_SLEEP);
1087 	if (ret) {
1088 		dev_err(data->dev, "failed to write ctrl_meas register\n");
1089 		return ret;
1090 	}
1091 
1092 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
1093 				 BMP280_FILTER_MASK,
1094 				 BMP280_FILTER_4X);
1095 	if (ret) {
1096 		dev_err(data->dev, "failed to write config register\n");
1097 		return ret;
1098 	}
1099 
1100 	return ret;
1101 }
1102 
1103 static irqreturn_t bmp280_trigger_handler(int irq, void *p)
1104 {
1105 	struct iio_poll_func *pf = p;
1106 	struct iio_dev *indio_dev = pf->indio_dev;
1107 	struct bmp280_data *data = iio_priv(indio_dev);
1108 	u32 adc_temp, adc_press, comp_press;
1109 	s32 t_fine, comp_temp;
1110 	s32 *chans = (s32 *)data->sensor_data;
1111 	int ret;
1112 
1113 	guard(mutex)(&data->lock);
1114 
1115 	/* Burst read data registers */
1116 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
1117 			       data->buf, BMP280_BURST_READ_BYTES);
1118 	if (ret) {
1119 		dev_err(data->dev, "failed to burst read sensor data\n");
1120 		goto out;
1121 	}
1122 
1123 	/* Temperature calculations */
1124 	adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3]));
1125 	if (adc_temp == BMP280_TEMP_SKIPPED) {
1126 		dev_err(data->dev, "reading temperature skipped\n");
1127 		goto out;
1128 	}
1129 
1130 	comp_temp = bmp280_compensate_temp(data, adc_temp);
1131 
1132 	/* Pressure calculations */
1133 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
1134 	if (adc_press == BMP280_PRESS_SKIPPED) {
1135 		dev_err(data->dev, "reading pressure skipped\n");
1136 		goto out;
1137 	}
1138 
1139 	t_fine = bmp280_calc_t_fine(data, adc_temp);
1140 	comp_press = bmp280_compensate_press(data, adc_press, t_fine);
1141 
1142 	chans[0] = comp_press;
1143 	chans[1] = comp_temp;
1144 
1145 	iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data,
1146 					   iio_get_time_ns(indio_dev));
1147 
1148 out:
1149 	iio_trigger_notify_done(indio_dev->trig);
1150 
1151 	return IRQ_HANDLED;
1152 }
1153 
1154 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
1155 static const u8 bmp280_chip_ids[] = { BMP280_CHIP_ID };
1156 static const int bmp280_temp_coeffs[] = { 10, 1 };
1157 static const int bmp280_press_coeffs[] = { 1, 256000 };
1158 
1159 const struct bmp280_chip_info bmp280_chip_info = {
1160 	.id_reg = BMP280_REG_ID,
1161 	.chip_id = bmp280_chip_ids,
1162 	.num_chip_id = ARRAY_SIZE(bmp280_chip_ids),
1163 	.regmap_config = &bmp280_regmap_config,
1164 	.start_up_time_us = 2000,
1165 	.channels = bmp280_channels,
1166 	.num_channels = ARRAY_SIZE(bmp280_channels),
1167 	.avail_scan_masks = bmp280_avail_scan_masks,
1168 
1169 	.oversampling_temp_avail = bmp280_oversampling_avail,
1170 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1171 	/*
1172 	 * Oversampling config values on BMx280 have one additional setting
1173 	 * that other generations of the family don't:
1174 	 * The value 0 means the measurement is bypassed instead of
1175 	 * oversampling set to x1.
1176 	 *
1177 	 * To account for this difference, and preserve the same common
1178 	 * config logic, this is handled later on chip_config callback
1179 	 * incrementing one unit the oversampling setting.
1180 	 */
1181 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
1182 
1183 	.oversampling_press_avail = bmp280_oversampling_avail,
1184 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1185 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
1186 
1187 	.temp_coeffs = bmp280_temp_coeffs,
1188 	.temp_coeffs_type = IIO_VAL_FRACTIONAL,
1189 	.press_coeffs = bmp280_press_coeffs,
1190 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
1191 
1192 	.chip_config = bmp280_chip_config,
1193 	.read_temp = bmp280_read_temp,
1194 	.read_press = bmp280_read_press,
1195 	.read_calib = bmp280_read_calib,
1196 	.set_mode = bmp280_set_mode,
1197 	.wait_conv = bmp280_wait_conv,
1198 	.preinit = bmp280_preinit,
1199 
1200 	.trigger_handler = bmp280_trigger_handler,
1201 };
1202 EXPORT_SYMBOL_NS(bmp280_chip_info, "IIO_BMP280");
1203 
1204 static int bme280_chip_config(struct bmp280_data *data)
1205 {
1206 	u8 osrs = FIELD_PREP(BME280_OSRS_HUMIDITY_MASK, data->oversampling_humid + 1);
1207 	int ret;
1208 
1209 	/*
1210 	 * Oversampling of humidity must be set before oversampling of
1211 	 * temperature/pressure is set to become effective.
1212 	 */
1213 	ret = regmap_update_bits(data->regmap, BME280_REG_CTRL_HUMIDITY,
1214 				 BME280_OSRS_HUMIDITY_MASK, osrs);
1215 	if (ret) {
1216 		dev_err(data->dev, "failed to set humidity oversampling");
1217 		return ret;
1218 	}
1219 
1220 	return bmp280_chip_config(data);
1221 }
1222 
1223 static irqreturn_t bme280_trigger_handler(int irq, void *p)
1224 {
1225 	struct iio_poll_func *pf = p;
1226 	struct iio_dev *indio_dev = pf->indio_dev;
1227 	struct bmp280_data *data = iio_priv(indio_dev);
1228 	u32 adc_temp, adc_press, adc_humidity, comp_press, comp_humidity;
1229 	s32 t_fine, comp_temp;
1230 	s32 *chans = (s32 *)data->sensor_data;
1231 	int ret;
1232 
1233 	guard(mutex)(&data->lock);
1234 
1235 	/* Burst read data registers */
1236 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
1237 			       data->buf, BME280_BURST_READ_BYTES);
1238 	if (ret) {
1239 		dev_err(data->dev, "failed to burst read sensor data\n");
1240 		goto out;
1241 	}
1242 
1243 	/* Temperature calculations */
1244 	adc_temp = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[3]));
1245 	if (adc_temp == BMP280_TEMP_SKIPPED) {
1246 		dev_err(data->dev, "reading temperature skipped\n");
1247 		goto out;
1248 	}
1249 
1250 	comp_temp = bmp280_compensate_temp(data, adc_temp);
1251 
1252 	/* Pressure calculations */
1253 	adc_press = FIELD_GET(BMP280_MEAS_TRIM_MASK, get_unaligned_be24(&data->buf[0]));
1254 	if (adc_press == BMP280_PRESS_SKIPPED) {
1255 		dev_err(data->dev, "reading pressure skipped\n");
1256 		goto out;
1257 	}
1258 
1259 	t_fine = bmp280_calc_t_fine(data, adc_temp);
1260 	comp_press = bmp280_compensate_press(data, adc_press, t_fine);
1261 
1262 	/* Humidity calculations */
1263 	adc_humidity = get_unaligned_be16(&data->buf[6]);
1264 
1265 	if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
1266 		dev_err(data->dev, "reading humidity skipped\n");
1267 		goto out;
1268 	}
1269 
1270 	comp_humidity = bme280_compensate_humidity(data, adc_humidity, t_fine);
1271 
1272 	chans[0] = comp_press;
1273 	chans[1] = comp_temp;
1274 	chans[2] = comp_humidity;
1275 
1276 	iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data,
1277 					   iio_get_time_ns(indio_dev));
1278 
1279 out:
1280 	iio_trigger_notify_done(indio_dev->trig);
1281 
1282 	return IRQ_HANDLED;
1283 }
1284 
1285 static int __bmp280_trigger_probe(struct iio_dev *indio_dev,
1286 				  const struct iio_trigger_ops *trigger_ops,
1287 				  int (*int_pin_config)(struct bmp280_data *data),
1288 				  irq_handler_t irq_thread_handler)
1289 {
1290 	struct bmp280_data *data = iio_priv(indio_dev);
1291 	struct device *dev = data->dev;
1292 	u32 irq_type;
1293 	int ret, irq;
1294 
1295 	irq = fwnode_irq_get(dev_fwnode(dev), 0);
1296 	if (irq < 0)
1297 		return dev_err_probe(dev, irq, "No interrupt found.\n");
1298 
1299 	irq_type = irq_get_trigger_type(irq);
1300 	switch (irq_type) {
1301 	case IRQF_TRIGGER_RISING:
1302 		data->trig_active_high = true;
1303 		break;
1304 	case IRQF_TRIGGER_FALLING:
1305 		data->trig_active_high = false;
1306 		break;
1307 	default:
1308 		return dev_err_probe(dev, -EINVAL, "Invalid interrupt type specified.\n");
1309 	}
1310 
1311 	data->trig_open_drain =
1312 		fwnode_property_read_bool(dev_fwnode(dev), "int-open-drain");
1313 
1314 	ret = int_pin_config(data);
1315 	if (ret)
1316 		return ret;
1317 
1318 	data->trig = devm_iio_trigger_alloc(data->dev, "%s-dev%d",
1319 					    indio_dev->name,
1320 					    iio_device_id(indio_dev));
1321 	if (!data->trig)
1322 		return -ENOMEM;
1323 
1324 	data->trig->ops = trigger_ops;
1325 	iio_trigger_set_drvdata(data->trig, data);
1326 
1327 	ret = devm_request_threaded_irq(data->dev, irq, NULL,
1328 					irq_thread_handler, IRQF_ONESHOT,
1329 					indio_dev->name, indio_dev);
1330 	if (ret)
1331 		return dev_err_probe(dev, ret, "request IRQ failed.\n");
1332 
1333 	ret = devm_iio_trigger_register(data->dev, data->trig);
1334 	if (ret)
1335 		return dev_err_probe(dev, ret, "iio trigger register failed.\n");
1336 
1337 	indio_dev->trig = iio_trigger_get(data->trig);
1338 
1339 	return 0;
1340 }
1341 
1342 static const u8 bme280_chip_ids[] = { BME280_CHIP_ID };
1343 static const int bme280_humid_coeffs[] = { 1000, 1024 };
1344 
1345 const struct bmp280_chip_info bme280_chip_info = {
1346 	.id_reg = BMP280_REG_ID,
1347 	.chip_id = bme280_chip_ids,
1348 	.num_chip_id = ARRAY_SIZE(bme280_chip_ids),
1349 	.regmap_config = &bme280_regmap_config,
1350 	.start_up_time_us = 2000,
1351 	.channels = bme280_channels,
1352 	.num_channels = ARRAY_SIZE(bme280_channels),
1353 	.avail_scan_masks = bme280_avail_scan_masks,
1354 
1355 	.oversampling_temp_avail = bmp280_oversampling_avail,
1356 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1357 	.oversampling_temp_default = BMP280_OSRS_TEMP_2X - 1,
1358 
1359 	.oversampling_press_avail = bmp280_oversampling_avail,
1360 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1361 	.oversampling_press_default = BMP280_OSRS_PRESS_16X - 1,
1362 
1363 	.oversampling_humid_avail = bmp280_oversampling_avail,
1364 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
1365 	.oversampling_humid_default = BME280_OSRS_HUMIDITY_16X - 1,
1366 
1367 	.temp_coeffs = bmp280_temp_coeffs,
1368 	.temp_coeffs_type = IIO_VAL_FRACTIONAL,
1369 	.press_coeffs = bmp280_press_coeffs,
1370 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
1371 	.humid_coeffs = bme280_humid_coeffs,
1372 	.humid_coeffs_type = IIO_VAL_FRACTIONAL,
1373 
1374 	.chip_config = bme280_chip_config,
1375 	.read_temp = bmp280_read_temp,
1376 	.read_press = bmp280_read_press,
1377 	.read_humid = bme280_read_humid,
1378 	.read_calib = bme280_read_calib,
1379 	.set_mode = bmp280_set_mode,
1380 	.wait_conv = bmp280_wait_conv,
1381 	.preinit = bmp280_preinit,
1382 
1383 	.trigger_handler = bme280_trigger_handler,
1384 };
1385 EXPORT_SYMBOL_NS(bme280_chip_info, "IIO_BMP280");
1386 
1387 /*
1388  * Helper function to send a command to BMP3XX sensors.
1389  *
1390  * Sensor processes commands written to the CMD register and signals
1391  * execution result through "cmd_rdy" and "cmd_error" flags available on
1392  * STATUS and ERROR registers.
1393  */
1394 static int bmp380_cmd(struct bmp280_data *data, u8 cmd)
1395 {
1396 	unsigned int reg;
1397 	int ret;
1398 
1399 	/* Check if device is ready to process a command */
1400 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
1401 	if (ret) {
1402 		dev_err(data->dev, "failed to read error register\n");
1403 		return ret;
1404 	}
1405 	if (!(reg & BMP380_STATUS_CMD_RDY_MASK)) {
1406 		dev_err(data->dev, "device is not ready to accept commands\n");
1407 		return -EBUSY;
1408 	}
1409 
1410 	/* Send command to process */
1411 	ret = regmap_write(data->regmap, BMP380_REG_CMD, cmd);
1412 	if (ret) {
1413 		dev_err(data->dev, "failed to send command to device\n");
1414 		return ret;
1415 	}
1416 	/* Wait for 2ms for command to be processed */
1417 	fsleep(data->start_up_time_us);
1418 	/* Check for command processing error */
1419 	ret = regmap_read(data->regmap, BMP380_REG_ERROR, &reg);
1420 	if (ret) {
1421 		dev_err(data->dev, "error reading ERROR reg\n");
1422 		return ret;
1423 	}
1424 	if (reg & BMP380_ERR_CMD_MASK) {
1425 		dev_err(data->dev, "error processing command 0x%X\n", cmd);
1426 		return -EINVAL;
1427 	}
1428 
1429 	return 0;
1430 }
1431 
1432 static int bmp380_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
1433 {
1434 	u32 value_temp;
1435 	int ret;
1436 
1437 	ret = regmap_bulk_read(data->regmap, BMP380_REG_TEMP_XLSB,
1438 			       data->buf, BMP280_NUM_TEMP_BYTES);
1439 	if (ret) {
1440 		dev_err(data->dev, "failed to read temperature\n");
1441 		return ret;
1442 	}
1443 
1444 	value_temp = get_unaligned_le24(data->buf);
1445 	if (value_temp == BMP380_TEMP_SKIPPED) {
1446 		dev_err(data->dev, "reading temperature skipped\n");
1447 		return -EIO;
1448 	}
1449 	*adc_temp = value_temp;
1450 
1451 	return 0;
1452 }
1453 
1454 /*
1455  * Returns temperature in Celsius degrees, resolution is 0.01º C. Output value
1456  * of "5123" equals 51.2º C. t_fine carries fine temperature as global value.
1457  *
1458  * Taken from datasheet, Section Appendix 9, "Compensation formula" and repo
1459  * https://github.com/BoschSensortec/BMP3-Sensor-API.
1460  */
1461 static s32 bmp380_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
1462 {
1463 	s64 var1, var2, var3, var4, var5, var6;
1464 	struct bmp380_calib *calib = &data->calib.bmp380;
1465 
1466 	var1 = ((s64) adc_temp) - (((s64) calib->T1) << 8);
1467 	var2 = var1 * ((s64) calib->T2);
1468 	var3 = var1 * var1;
1469 	var4 = var3 * ((s64) calib->T3);
1470 	var5 = (var2 << 18) + var4;
1471 	var6 = var5 >> 32;
1472 	return (s32)var6; /* t_fine = var6 */
1473 }
1474 
1475 static int bmp380_get_t_fine(struct bmp280_data *data, s32 *t_fine)
1476 {
1477 	s32 adc_temp;
1478 	int ret;
1479 
1480 	ret = bmp380_read_temp_adc(data, &adc_temp);
1481 	if (ret)
1482 		return ret;
1483 
1484 	*t_fine = bmp380_calc_t_fine(data, adc_temp);
1485 
1486 	return 0;
1487 }
1488 
1489 static int bmp380_compensate_temp(struct bmp280_data *data, u32 adc_temp)
1490 {
1491 	s64 comp_temp;
1492 	s32 var6;
1493 
1494 	var6 = bmp380_calc_t_fine(data, adc_temp);
1495 	comp_temp = (var6 * 25) >> 14;
1496 
1497 	comp_temp = clamp_val(comp_temp, BMP380_MIN_TEMP, BMP380_MAX_TEMP);
1498 	return (s32) comp_temp;
1499 }
1500 
1501 static int bmp380_read_press_adc(struct bmp280_data *data, u32 *adc_press)
1502 {
1503 	u32 value_press;
1504 	int ret;
1505 
1506 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1507 			       data->buf, BMP280_NUM_PRESS_BYTES);
1508 	if (ret) {
1509 		dev_err(data->dev, "failed to read pressure\n");
1510 		return ret;
1511 	}
1512 
1513 	value_press = get_unaligned_le24(data->buf);
1514 	if (value_press == BMP380_PRESS_SKIPPED) {
1515 		dev_err(data->dev, "reading pressure skipped\n");
1516 		return -EIO;
1517 	}
1518 	*adc_press = value_press;
1519 
1520 	return 0;
1521 }
1522 
1523 /*
1524  * Returns pressure in Pa as an unsigned 32 bit integer in fractional Pascal.
1525  * Output value of "9528709" represents 9528709/100 = 95287.09 Pa = 952.8709 hPa.
1526  *
1527  * Taken from datasheet, Section 9.3. "Pressure compensation" and repository
1528  * https://github.com/BoschSensortec/BMP3-Sensor-API.
1529  */
1530 static u32 bmp380_compensate_press(struct bmp280_data *data,
1531 				   u32 adc_press, s32 t_fine)
1532 {
1533 	s64 var1, var2, var3, var4, var5, var6, offset, sensitivity;
1534 	struct bmp380_calib *calib = &data->calib.bmp380;
1535 	u32 comp_press;
1536 
1537 	var1 = (s64)t_fine * (s64)t_fine;
1538 	var2 = var1 >> 6;
1539 	var3 = (var2 * ((s64)t_fine)) >> 8;
1540 	var4 = ((s64)calib->P8 * var3) >> 5;
1541 	var5 = ((s64)calib->P7 * var1) << 4;
1542 	var6 = ((s64)calib->P6 * (s64)t_fine) << 22;
1543 	offset = ((s64)calib->P5 << 47) + var4 + var5 + var6;
1544 	var2 = ((s64)calib->P4 * var3) >> 5;
1545 	var4 = ((s64)calib->P3 * var1) << 2;
1546 	var5 = ((s64)calib->P2 - ((s64)1 << 14)) *
1547 	       ((s64)t_fine << 21);
1548 	sensitivity = (((s64) calib->P1 - ((s64) 1 << 14)) << 46) +
1549 			var2 + var4 + var5;
1550 	var1 = (sensitivity >> 24) * (s64)adc_press;
1551 	var2 = (s64)calib->P10 * (s64)t_fine;
1552 	var3 = var2 + ((s64)calib->P9 << 16);
1553 	var4 = (var3 * (s64)adc_press) >> 13;
1554 
1555 	/*
1556 	 * Dividing by 10 followed by multiplying by 10 to avoid
1557 	 * possible overflow caused by (uncomp_data->pressure * partial_data4).
1558 	 */
1559 	var5 = ((s64)adc_press * div_s64(var4, 10)) >> 9;
1560 	var5 *= 10;
1561 	var6 = (s64)adc_press * (s64)adc_press;
1562 	var2 = ((s64)calib->P11 * var6) >> 16;
1563 	var3 = (var2 * (s64)adc_press) >> 7;
1564 	var4 = (offset >> 2) + var1 + var5 + var3;
1565 	comp_press = ((u64)var4 * 25) >> 40;
1566 
1567 	comp_press = clamp_val(comp_press, BMP380_MIN_PRES, BMP380_MAX_PRES);
1568 	return comp_press;
1569 }
1570 
1571 static int bmp380_read_temp(struct bmp280_data *data, s32 *comp_temp)
1572 {
1573 	u32 adc_temp;
1574 	int ret;
1575 
1576 	ret = bmp380_read_temp_adc(data, &adc_temp);
1577 	if (ret)
1578 		return ret;
1579 
1580 	*comp_temp = bmp380_compensate_temp(data, adc_temp);
1581 
1582 	return 0;
1583 }
1584 
1585 static int bmp380_read_press(struct bmp280_data *data, u32 *comp_press)
1586 {
1587 	u32 adc_press, t_fine;
1588 	int ret;
1589 
1590 	ret = bmp380_get_t_fine(data, &t_fine);
1591 	if (ret)
1592 		return ret;
1593 
1594 	ret = bmp380_read_press_adc(data, &adc_press);
1595 	if (ret)
1596 		return ret;
1597 
1598 	*comp_press = bmp380_compensate_press(data, adc_press, t_fine);
1599 
1600 	return 0;
1601 }
1602 
1603 static int bmp380_read_calib(struct bmp280_data *data)
1604 {
1605 	struct bmp380_calib *calib = &data->calib.bmp380;
1606 	int ret;
1607 
1608 	/* Read temperature and pressure calibration data */
1609 	ret = regmap_bulk_read(data->regmap, BMP380_REG_CALIB_TEMP_START,
1610 			       data->bmp380_cal_buf,
1611 			       sizeof(data->bmp380_cal_buf));
1612 	if (ret) {
1613 		dev_err(data->dev,
1614 			"failed to read calibration parameters\n");
1615 		return ret;
1616 	}
1617 
1618 	/* Toss the temperature calibration data into the entropy pool */
1619 	add_device_randomness(data->bmp380_cal_buf,
1620 			      sizeof(data->bmp380_cal_buf));
1621 
1622 	/* Parse calibration values */
1623 	calib->T1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T1]);
1624 	calib->T2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_T2]);
1625 	calib->T3 = data->bmp380_cal_buf[BMP380_T3];
1626 	calib->P1 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P1]);
1627 	calib->P2 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P2]);
1628 	calib->P3 = data->bmp380_cal_buf[BMP380_P3];
1629 	calib->P4 = data->bmp380_cal_buf[BMP380_P4];
1630 	calib->P5 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P5]);
1631 	calib->P6 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P6]);
1632 	calib->P7 = data->bmp380_cal_buf[BMP380_P7];
1633 	calib->P8 = data->bmp380_cal_buf[BMP380_P8];
1634 	calib->P9 = get_unaligned_le16(&data->bmp380_cal_buf[BMP380_P9]);
1635 	calib->P10 = data->bmp380_cal_buf[BMP380_P10];
1636 	calib->P11 = data->bmp380_cal_buf[BMP380_P11];
1637 
1638 	return 0;
1639 }
1640 
1641 static const int bmp380_odr_table[][2] = {
1642 	[BMP380_ODR_200HZ]	= {200, 0},
1643 	[BMP380_ODR_100HZ]	= {100, 0},
1644 	[BMP380_ODR_50HZ]	= {50, 0},
1645 	[BMP380_ODR_25HZ]	= {25, 0},
1646 	[BMP380_ODR_12_5HZ]	= {12, 500000},
1647 	[BMP380_ODR_6_25HZ]	= {6, 250000},
1648 	[BMP380_ODR_3_125HZ]	= {3, 125000},
1649 	[BMP380_ODR_1_5625HZ]	= {1, 562500},
1650 	[BMP380_ODR_0_78HZ]	= {0, 781250},
1651 	[BMP380_ODR_0_39HZ]	= {0, 390625},
1652 	[BMP380_ODR_0_2HZ]	= {0, 195313},
1653 	[BMP380_ODR_0_1HZ]	= {0, 97656},
1654 	[BMP380_ODR_0_05HZ]	= {0, 48828},
1655 	[BMP380_ODR_0_02HZ]	= {0, 24414},
1656 	[BMP380_ODR_0_01HZ]	= {0, 12207},
1657 	[BMP380_ODR_0_006HZ]	= {0, 6104},
1658 	[BMP380_ODR_0_003HZ]	= {0, 3052},
1659 	[BMP380_ODR_0_0015HZ]	= {0, 1526},
1660 };
1661 
1662 static int bmp380_preinit(struct bmp280_data *data)
1663 {
1664 	/* BMP3xx requires soft-reset as part of initialization */
1665 	return bmp380_cmd(data, BMP380_CMD_SOFT_RESET);
1666 }
1667 
1668 static const u8 bmp380_operation_mode[] = {
1669 	[BMP280_SLEEP] = BMP380_MODE_SLEEP,
1670 	[BMP280_FORCED] = BMP380_MODE_FORCED,
1671 	[BMP280_NORMAL] = BMP380_MODE_NORMAL,
1672 };
1673 
1674 static int bmp380_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode)
1675 {
1676 	int ret;
1677 
1678 	ret = regmap_write_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1679 				BMP380_MODE_MASK,
1680 				FIELD_PREP(BMP380_MODE_MASK,
1681 					   bmp380_operation_mode[mode]));
1682 	if (ret) {
1683 		dev_err(data->dev, "failed to  write power control register.\n");
1684 		return ret;
1685 	}
1686 
1687 	data->op_mode = mode;
1688 
1689 	return 0;
1690 }
1691 
1692 static int bmp380_wait_conv(struct bmp280_data *data)
1693 {
1694 	unsigned int reg;
1695 	int ret, meas_time_us;
1696 
1697 	/* Offset measurement time */
1698 	meas_time_us = BMP380_MEAS_OFFSET;
1699 
1700 	/* Pressure measurement time */
1701 	meas_time_us += BMP380_PRESS_MEAS_OFFSET +
1702 		     BIT(data->oversampling_press) * BMP380_MEAS_DUR;
1703 
1704 	/* Temperature measurement time */
1705 	meas_time_us += BMP380_TEMP_MEAS_OFFSET +
1706 		     BIT(data->oversampling_temp) * BMP380_MEAS_DUR;
1707 
1708 	/* Measurement time defined in Datasheet Section 3.9.2 */
1709 	fsleep(meas_time_us);
1710 
1711 	ret = regmap_read(data->regmap, BMP380_REG_STATUS, &reg);
1712 	if (ret) {
1713 		dev_err(data->dev, "failed to read status register.\n");
1714 		return ret;
1715 	}
1716 
1717 	if (!((reg & BMP380_STATUS_DRDY_PRESS_MASK) &&
1718 	      (reg & BMP380_STATUS_DRDY_TEMP_MASK))) {
1719 		dev_err(data->dev, "Measurement cycle didn't complete.\n");
1720 		return -EBUSY;
1721 	}
1722 
1723 	return 0;
1724 }
1725 
1726 static int bmp380_chip_config(struct bmp280_data *data)
1727 {
1728 	bool change = false, aux;
1729 	unsigned int tmp;
1730 	u8 osrs;
1731 	int ret;
1732 
1733 	/* Configure power control register */
1734 	ret = regmap_update_bits(data->regmap, BMP380_REG_POWER_CONTROL,
1735 				 BMP380_CTRL_SENSORS_MASK,
1736 				 BMP380_CTRL_SENSORS_PRESS_EN |
1737 				 BMP380_CTRL_SENSORS_TEMP_EN);
1738 	if (ret) {
1739 		dev_err(data->dev,
1740 			"failed to write operation control register\n");
1741 		return ret;
1742 	}
1743 
1744 	/* Configure oversampling */
1745 	osrs = FIELD_PREP(BMP380_OSRS_TEMP_MASK, data->oversampling_temp) |
1746 	       FIELD_PREP(BMP380_OSRS_PRESS_MASK, data->oversampling_press);
1747 
1748 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_OSR,
1749 				       BMP380_OSRS_TEMP_MASK |
1750 				       BMP380_OSRS_PRESS_MASK,
1751 				       osrs, &aux);
1752 	if (ret) {
1753 		dev_err(data->dev, "failed to write oversampling register\n");
1754 		return ret;
1755 	}
1756 	change = change || aux;
1757 
1758 	/* Configure output data rate */
1759 	ret = regmap_update_bits_check(data->regmap, BMP380_REG_ODR,
1760 				       BMP380_ODRS_MASK, data->sampling_freq,
1761 				       &aux);
1762 	if (ret) {
1763 		dev_err(data->dev, "failed to write ODR selection register\n");
1764 		return ret;
1765 	}
1766 	change = change || aux;
1767 
1768 	/* Set filter data */
1769 	ret = regmap_update_bits(data->regmap, BMP380_REG_CONFIG, BMP380_FILTER_MASK,
1770 				 FIELD_PREP(BMP380_FILTER_MASK, data->iir_filter_coeff));
1771 	if (ret) {
1772 		dev_err(data->dev, "failed to write config register\n");
1773 		return ret;
1774 	}
1775 
1776 	if (change) {
1777 		/*
1778 		 * The configurations errors are detected on the fly during a
1779 		 * measurement cycle. If the sampling frequency is too low, it's
1780 		 * faster to reset the measurement loop than wait until the next
1781 		 * measurement is due.
1782 		 *
1783 		 * Resets sensor measurement loop toggling between sleep and
1784 		 * normal operating modes.
1785 		 */
1786 		ret = bmp380_set_mode(data, BMP280_SLEEP);
1787 		if (ret) {
1788 			dev_err(data->dev, "failed to set sleep mode\n");
1789 			return ret;
1790 		}
1791 
1792 		/*
1793 		 * According to the BMP3 Sensor API, the sensor needs 5ms
1794 		 * in order to go to the sleep mode.
1795 		 */
1796 		fsleep(5 * USEC_PER_MSEC);
1797 
1798 		ret = bmp380_set_mode(data, BMP280_NORMAL);
1799 		if (ret) {
1800 			dev_err(data->dev, "failed to set normal mode\n");
1801 			return ret;
1802 		}
1803 		/*
1804 		 * Waits for measurement before checking configuration error
1805 		 * flag. Selected longest measurement time, calculated from
1806 		 * formula in datasheet section 3.9.2 with an offset of ~+15%
1807 		 * as it seen as well in table 3.9.1.
1808 		 */
1809 		fsleep(150 * USEC_PER_MSEC);
1810 
1811 		/* Check config error flag */
1812 		ret = regmap_read(data->regmap, BMP380_REG_ERROR, &tmp);
1813 		if (ret) {
1814 			dev_err(data->dev, "failed to read error register\n");
1815 			return ret;
1816 		}
1817 		if (tmp & BMP380_ERR_CONF_MASK) {
1818 			dev_warn(data->dev,
1819 				 "sensor flagged configuration as incompatible\n");
1820 			return -EINVAL;
1821 		}
1822 	}
1823 
1824 	/* Dummy read to empty data registers. */
1825 	ret = bmp380_read_press(data, &tmp);
1826 	if (ret)
1827 		return ret;
1828 
1829 	ret = bmp380_set_mode(data, BMP280_SLEEP);
1830 	if (ret)
1831 		dev_err(data->dev, "failed to set sleep mode.\n");
1832 
1833 	return ret;
1834 }
1835 
1836 static int bmp380_data_rdy_trigger_set_state(struct iio_trigger *trig,
1837 					     bool state)
1838 {
1839 	struct bmp280_data *data = iio_trigger_get_drvdata(trig);
1840 	int ret;
1841 
1842 	guard(mutex)(&data->lock);
1843 
1844 	ret = regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL,
1845 				 BMP380_INT_CTRL_DRDY_EN,
1846 				 FIELD_PREP(BMP380_INT_CTRL_DRDY_EN, !!state));
1847 	if (ret)
1848 		dev_err(data->dev,
1849 			"Could not %s interrupt.\n", str_enable_disable(state));
1850 	return ret;
1851 }
1852 
1853 static const struct iio_trigger_ops bmp380_trigger_ops = {
1854 	.set_trigger_state = &bmp380_data_rdy_trigger_set_state,
1855 };
1856 
1857 static int bmp380_int_pin_config(struct bmp280_data *data)
1858 {
1859 	int pin_drive_cfg = FIELD_PREP(BMP380_INT_CTRL_OPEN_DRAIN,
1860 				       data->trig_open_drain);
1861 	int pin_level_cfg = FIELD_PREP(BMP380_INT_CTRL_LEVEL,
1862 				       data->trig_active_high);
1863 	int ret, int_pin_cfg = pin_drive_cfg | pin_level_cfg;
1864 
1865 	ret = regmap_update_bits(data->regmap, BMP380_REG_INT_CONTROL,
1866 				 BMP380_INT_CTRL_SETTINGS_MASK, int_pin_cfg);
1867 	if (ret)
1868 		dev_err(data->dev, "Could not set interrupt settings.\n");
1869 
1870 	return ret;
1871 }
1872 
1873 static irqreturn_t bmp380_irq_thread_handler(int irq, void *p)
1874 {
1875 	struct iio_dev *indio_dev = p;
1876 	struct bmp280_data *data = iio_priv(indio_dev);
1877 	unsigned int int_ctrl;
1878 	int ret;
1879 
1880 	ret = regmap_read(data->regmap, BMP380_REG_INT_STATUS, &int_ctrl);
1881 	if (ret)
1882 		return IRQ_NONE;
1883 
1884 	if (FIELD_GET(BMP380_INT_STATUS_DRDY, int_ctrl))
1885 		iio_trigger_poll_nested(data->trig);
1886 
1887 	return IRQ_HANDLED;
1888 }
1889 
1890 static int bmp380_trigger_probe(struct iio_dev *indio_dev)
1891 {
1892 	return __bmp280_trigger_probe(indio_dev, &bmp380_trigger_ops,
1893 				      bmp380_int_pin_config,
1894 				      bmp380_irq_thread_handler);
1895 }
1896 
1897 static irqreturn_t bmp380_trigger_handler(int irq, void *p)
1898 {
1899 	struct iio_poll_func *pf = p;
1900 	struct iio_dev *indio_dev = pf->indio_dev;
1901 	struct bmp280_data *data = iio_priv(indio_dev);
1902 	u32 adc_temp, adc_press, comp_press;
1903 	s32 t_fine, comp_temp;
1904 	s32 *chans = (s32 *)data->sensor_data;
1905 	int ret;
1906 
1907 	guard(mutex)(&data->lock);
1908 
1909 	/* Burst read data registers */
1910 	ret = regmap_bulk_read(data->regmap, BMP380_REG_PRESS_XLSB,
1911 			       data->buf, BMP280_BURST_READ_BYTES);
1912 	if (ret) {
1913 		dev_err(data->dev, "failed to burst read sensor data\n");
1914 		goto out;
1915 	}
1916 
1917 	/* Temperature calculations */
1918 	adc_temp = get_unaligned_le24(&data->buf[3]);
1919 	if (adc_temp == BMP380_TEMP_SKIPPED) {
1920 		dev_err(data->dev, "reading temperature skipped\n");
1921 		goto out;
1922 	}
1923 
1924 	comp_temp = bmp380_compensate_temp(data, adc_temp);
1925 
1926 	/* Pressure calculations */
1927 	adc_press = get_unaligned_le24(&data->buf[0]);
1928 	if (adc_press == BMP380_PRESS_SKIPPED) {
1929 		dev_err(data->dev, "reading pressure skipped\n");
1930 		goto out;
1931 	}
1932 
1933 	t_fine = bmp380_calc_t_fine(data, adc_temp);
1934 	comp_press = bmp380_compensate_press(data, adc_press, t_fine);
1935 
1936 	chans[0] = comp_press;
1937 	chans[1] = comp_temp;
1938 
1939 	iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data,
1940 					   iio_get_time_ns(indio_dev));
1941 
1942 out:
1943 	iio_trigger_notify_done(indio_dev->trig);
1944 
1945 	return IRQ_HANDLED;
1946 }
1947 
1948 static const int bmp380_oversampling_avail[] = { 1, 2, 4, 8, 16, 32 };
1949 static const int bmp380_iir_filter_coeffs_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128};
1950 static const u8 bmp380_chip_ids[] = { BMP380_CHIP_ID, BMP390_CHIP_ID };
1951 static const int bmp380_temp_coeffs[] = { 10, 1 };
1952 static const int bmp380_press_coeffs[] = { 1, 100000 };
1953 
1954 const struct bmp280_chip_info bmp380_chip_info = {
1955 	.id_reg = BMP380_REG_ID,
1956 	.chip_id = bmp380_chip_ids,
1957 	.num_chip_id = ARRAY_SIZE(bmp380_chip_ids),
1958 	.regmap_config = &bmp380_regmap_config,
1959 	.spi_read_extra_byte = true,
1960 	.start_up_time_us = 2000,
1961 	.channels = bmp380_channels,
1962 	.num_channels = ARRAY_SIZE(bmp380_channels),
1963 	.avail_scan_masks = bmp280_avail_scan_masks,
1964 
1965 	.oversampling_temp_avail = bmp380_oversampling_avail,
1966 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1967 	.oversampling_temp_default = ilog2(1),
1968 
1969 	.oversampling_press_avail = bmp380_oversampling_avail,
1970 	.num_oversampling_press_avail = ARRAY_SIZE(bmp380_oversampling_avail),
1971 	.oversampling_press_default = ilog2(4),
1972 
1973 	.sampling_freq_avail = bmp380_odr_table,
1974 	.num_sampling_freq_avail = ARRAY_SIZE(bmp380_odr_table) * 2,
1975 	.sampling_freq_default = BMP380_ODR_50HZ,
1976 
1977 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
1978 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
1979 	.iir_filter_coeff_default = 2,
1980 
1981 	.temp_coeffs = bmp380_temp_coeffs,
1982 	.temp_coeffs_type = IIO_VAL_FRACTIONAL,
1983 	.press_coeffs = bmp380_press_coeffs,
1984 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
1985 
1986 	.chip_config = bmp380_chip_config,
1987 	.read_temp = bmp380_read_temp,
1988 	.read_press = bmp380_read_press,
1989 	.read_calib = bmp380_read_calib,
1990 	.set_mode = bmp380_set_mode,
1991 	.wait_conv = bmp380_wait_conv,
1992 	.preinit = bmp380_preinit,
1993 
1994 	.trigger_probe = bmp380_trigger_probe,
1995 	.trigger_handler = bmp380_trigger_handler,
1996 };
1997 EXPORT_SYMBOL_NS(bmp380_chip_info, "IIO_BMP280");
1998 
1999 static int bmp580_soft_reset(struct bmp280_data *data)
2000 {
2001 	unsigned int reg;
2002 	int ret;
2003 
2004 	ret = regmap_write(data->regmap, BMP580_REG_CMD, BMP580_CMD_SOFT_RESET);
2005 	if (ret) {
2006 		dev_err(data->dev, "failed to send reset command to device\n");
2007 		return ret;
2008 	}
2009 	/* From datasheet's table 4: electrical characteristics */
2010 	fsleep(2000);
2011 
2012 	/* Dummy read of chip_id */
2013 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
2014 	if (ret) {
2015 		dev_err(data->dev, "failed to reestablish comms after reset\n");
2016 		return ret;
2017 	}
2018 
2019 	ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &reg);
2020 	if (ret) {
2021 		dev_err(data->dev, "error reading interrupt status register\n");
2022 		return ret;
2023 	}
2024 	if (!(reg & BMP580_INT_STATUS_POR_MASK)) {
2025 		dev_err(data->dev, "error resetting sensor\n");
2026 		return -EINVAL;
2027 	}
2028 
2029 	return 0;
2030 }
2031 
2032 /**
2033  * bmp580_nvm_operation() - Helper function to commit NVM memory operations
2034  * @data: sensor data struct
2035  * @is_write: flag to signal write operation
2036  */
2037 static int bmp580_nvm_operation(struct bmp280_data *data, bool is_write)
2038 {
2039 	unsigned long timeout, poll;
2040 	unsigned int reg;
2041 	int ret;
2042 
2043 	/* Check NVM ready flag */
2044 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
2045 	if (ret) {
2046 		dev_err(data->dev, "failed to check nvm status\n");
2047 		return ret;
2048 	}
2049 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK)) {
2050 		dev_err(data->dev, "sensor's nvm is not ready\n");
2051 		return -EIO;
2052 	}
2053 
2054 	/* Start NVM operation sequence */
2055 	ret = regmap_write(data->regmap, BMP580_REG_CMD,
2056 			   BMP580_CMD_NVM_OP_SEQ_0);
2057 	if (ret) {
2058 		dev_err(data->dev,
2059 			"failed to send nvm operation's first sequence\n");
2060 		return ret;
2061 	}
2062 	if (is_write) {
2063 		/* Send NVM write sequence */
2064 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
2065 				   BMP580_CMD_NVM_WRITE_SEQ_1);
2066 		if (ret) {
2067 			dev_err(data->dev,
2068 				"failed to send nvm write sequence\n");
2069 			return ret;
2070 		}
2071 		/* Datasheet says on 4.8.1.2 it takes approximately 10ms */
2072 		poll = 2000;
2073 		timeout = 12000;
2074 	} else {
2075 		/* Send NVM read sequence */
2076 		ret = regmap_write(data->regmap, BMP580_REG_CMD,
2077 				   BMP580_CMD_NVM_READ_SEQ_1);
2078 		if (ret) {
2079 			dev_err(data->dev,
2080 				"failed to send nvm read sequence\n");
2081 			return ret;
2082 		}
2083 		/* Datasheet says on 4.8.1.1 it takes approximately 200us */
2084 		poll = 50;
2085 		timeout = 400;
2086 	}
2087 
2088 	/* Wait until NVM is ready again */
2089 	ret = regmap_read_poll_timeout(data->regmap, BMP580_REG_STATUS, reg,
2090 				       (reg & BMP580_STATUS_NVM_RDY_MASK),
2091 				       poll, timeout);
2092 	if (ret) {
2093 		dev_err(data->dev, "error checking nvm operation status\n");
2094 		return ret;
2095 	}
2096 
2097 	/* Check NVM error flags */
2098 	if ((reg & BMP580_STATUS_NVM_ERR_MASK) || (reg & BMP580_STATUS_NVM_CMD_ERR_MASK)) {
2099 		dev_err(data->dev, "error processing nvm operation\n");
2100 		return -EIO;
2101 	}
2102 
2103 	return 0;
2104 }
2105 
2106 /*
2107  * Contrary to previous sensors families, compensation algorithm is builtin.
2108  * We are only required to read the register raw data and adapt the ranges
2109  * for what is expected on IIO ABI.
2110  */
2111 
2112 static int bmp580_read_temp(struct bmp280_data *data, s32 *raw_temp)
2113 {
2114 	s32 value_temp;
2115 	int ret;
2116 
2117 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB,
2118 			       data->buf, BMP280_NUM_TEMP_BYTES);
2119 	if (ret) {
2120 		dev_err(data->dev, "failed to read temperature\n");
2121 		return ret;
2122 	}
2123 
2124 	value_temp = get_unaligned_le24(data->buf);
2125 	if (value_temp == BMP580_TEMP_SKIPPED) {
2126 		dev_err(data->dev, "reading temperature skipped\n");
2127 		return -EIO;
2128 	}
2129 	*raw_temp = sign_extend32(value_temp, 23);
2130 
2131 	return 0;
2132 }
2133 
2134 static int bmp580_read_press(struct bmp280_data *data, u32 *raw_press)
2135 {
2136 	u32 value_press;
2137 	int ret;
2138 
2139 	ret = regmap_bulk_read(data->regmap, BMP580_REG_PRESS_XLSB,
2140 			       data->buf, BMP280_NUM_PRESS_BYTES);
2141 	if (ret) {
2142 		dev_err(data->dev, "failed to read pressure\n");
2143 		return ret;
2144 	}
2145 
2146 	value_press = get_unaligned_le24(data->buf);
2147 	if (value_press == BMP580_PRESS_SKIPPED) {
2148 		dev_err(data->dev, "reading pressure skipped\n");
2149 		return -EIO;
2150 	}
2151 	*raw_press = value_press;
2152 
2153 	return 0;
2154 }
2155 
2156 static const int bmp580_odr_table[][2] = {
2157 	[BMP580_ODR_240HZ] =	{240, 0},
2158 	[BMP580_ODR_218HZ] =	{218, 0},
2159 	[BMP580_ODR_199HZ] =	{199, 0},
2160 	[BMP580_ODR_179HZ] =	{179, 0},
2161 	[BMP580_ODR_160HZ] =	{160, 0},
2162 	[BMP580_ODR_149HZ] =	{149, 0},
2163 	[BMP580_ODR_140HZ] =	{140, 0},
2164 	[BMP580_ODR_129HZ] =	{129, 0},
2165 	[BMP580_ODR_120HZ] =	{120, 0},
2166 	[BMP580_ODR_110HZ] =	{110, 0},
2167 	[BMP580_ODR_100HZ] =	{100, 0},
2168 	[BMP580_ODR_89HZ] =	{89, 0},
2169 	[BMP580_ODR_80HZ] =	{80, 0},
2170 	[BMP580_ODR_70HZ] =	{70, 0},
2171 	[BMP580_ODR_60HZ] =	{60, 0},
2172 	[BMP580_ODR_50HZ] =	{50, 0},
2173 	[BMP580_ODR_45HZ] =	{45, 0},
2174 	[BMP580_ODR_40HZ] =	{40, 0},
2175 	[BMP580_ODR_35HZ] =	{35, 0},
2176 	[BMP580_ODR_30HZ] =	{30, 0},
2177 	[BMP580_ODR_25HZ] =	{25, 0},
2178 	[BMP580_ODR_20HZ] =	{20, 0},
2179 	[BMP580_ODR_15HZ] =	{15, 0},
2180 	[BMP580_ODR_10HZ] =	{10, 0},
2181 	[BMP580_ODR_5HZ] =	{5, 0},
2182 	[BMP580_ODR_4HZ] =	{4, 0},
2183 	[BMP580_ODR_3HZ] =	{3, 0},
2184 	[BMP580_ODR_2HZ] =	{2, 0},
2185 	[BMP580_ODR_1HZ] =	{1, 0},
2186 	[BMP580_ODR_0_5HZ] =	{0, 500000},
2187 	[BMP580_ODR_0_25HZ] =	{0, 250000},
2188 	[BMP580_ODR_0_125HZ] =	{0, 125000},
2189 };
2190 
2191 static const int bmp580_nvmem_addrs[] = { 0x20, 0x21, 0x22 };
2192 
2193 static int bmp580_nvmem_read_impl(void *priv, unsigned int offset, void *val,
2194 				  size_t bytes)
2195 {
2196 	struct bmp280_data *data = priv;
2197 	u16 *dst = val;
2198 	int ret, addr;
2199 
2200 	guard(mutex)(&data->lock);
2201 
2202 	/* Set sensor in standby mode */
2203 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2204 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
2205 				 BMP580_ODR_DEEPSLEEP_DIS |
2206 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
2207 	if (ret) {
2208 		dev_err(data->dev, "failed to change sensor to standby mode\n");
2209 		goto exit;
2210 	}
2211 	/* Wait standby transition time */
2212 	fsleep(2500);
2213 
2214 	while (bytes >= sizeof(*dst)) {
2215 		addr = bmp580_nvmem_addrs[offset / sizeof(*dst)];
2216 
2217 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
2218 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
2219 		if (ret) {
2220 			dev_err(data->dev, "error writing nvm address\n");
2221 			goto exit;
2222 		}
2223 
2224 		ret = bmp580_nvm_operation(data, false);
2225 		if (ret)
2226 			goto exit;
2227 
2228 		ret = regmap_bulk_read(data->regmap, BMP580_REG_NVM_DATA_LSB,
2229 				       &data->le16, sizeof(data->le16));
2230 		if (ret) {
2231 			dev_err(data->dev, "error reading nvm data regs\n");
2232 			goto exit;
2233 		}
2234 
2235 		*dst++ = le16_to_cpu(data->le16);
2236 		bytes -= sizeof(*dst);
2237 		offset += sizeof(*dst);
2238 	}
2239 exit:
2240 	/* Restore chip config */
2241 	data->chip_info->chip_config(data);
2242 	return ret;
2243 }
2244 
2245 static int bmp580_nvmem_read(void *priv, unsigned int offset, void *val,
2246 			     size_t bytes)
2247 {
2248 	struct bmp280_data *data = priv;
2249 	int ret;
2250 
2251 	pm_runtime_get_sync(data->dev);
2252 	ret = bmp580_nvmem_read_impl(priv, offset, val, bytes);
2253 	pm_runtime_mark_last_busy(data->dev);
2254 	pm_runtime_put_autosuspend(data->dev);
2255 
2256 	return ret;
2257 }
2258 
2259 static int bmp580_nvmem_write_impl(void *priv, unsigned int offset, void *val,
2260 				   size_t bytes)
2261 {
2262 	struct bmp280_data *data = priv;
2263 	u16 *buf = val;
2264 	int ret, addr;
2265 
2266 	guard(mutex)(&data->lock);
2267 
2268 	/* Set sensor in standby mode */
2269 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2270 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
2271 				 BMP580_ODR_DEEPSLEEP_DIS |
2272 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
2273 	if (ret) {
2274 		dev_err(data->dev, "failed to change sensor to standby mode\n");
2275 		goto exit;
2276 	}
2277 	/* Wait standby transition time */
2278 	fsleep(2500);
2279 
2280 	while (bytes >= sizeof(*buf)) {
2281 		addr = bmp580_nvmem_addrs[offset / sizeof(*buf)];
2282 
2283 		ret = regmap_write(data->regmap, BMP580_REG_NVM_ADDR,
2284 				   BMP580_NVM_PROG_EN |
2285 				   FIELD_PREP(BMP580_NVM_ROW_ADDR_MASK, addr));
2286 		if (ret) {
2287 			dev_err(data->dev, "error writing nvm address\n");
2288 			goto exit;
2289 		}
2290 		data->le16 = cpu_to_le16(*buf++);
2291 
2292 		ret = regmap_bulk_write(data->regmap, BMP580_REG_NVM_DATA_LSB,
2293 					&data->le16, sizeof(data->le16));
2294 		if (ret) {
2295 			dev_err(data->dev, "error writing LSB NVM data regs\n");
2296 			goto exit;
2297 		}
2298 
2299 		ret = bmp580_nvm_operation(data, true);
2300 		if (ret)
2301 			goto exit;
2302 
2303 		/* Disable programming mode bit */
2304 		ret = regmap_clear_bits(data->regmap, BMP580_REG_NVM_ADDR,
2305 					BMP580_NVM_PROG_EN);
2306 		if (ret) {
2307 			dev_err(data->dev, "error resetting nvm write\n");
2308 			goto exit;
2309 		}
2310 
2311 		bytes -= sizeof(*buf);
2312 		offset += sizeof(*buf);
2313 	}
2314 exit:
2315 	/* Restore chip config */
2316 	data->chip_info->chip_config(data);
2317 	return ret;
2318 }
2319 
2320 static int bmp580_nvmem_write(void *priv, unsigned int offset, void *val,
2321 			      size_t bytes)
2322 {
2323 	struct bmp280_data *data = priv;
2324 	int ret;
2325 
2326 	pm_runtime_get_sync(data->dev);
2327 	ret = bmp580_nvmem_write_impl(priv, offset, val, bytes);
2328 	pm_runtime_mark_last_busy(data->dev);
2329 	pm_runtime_put_autosuspend(data->dev);
2330 
2331 	return ret;
2332 }
2333 
2334 static int bmp580_preinit(struct bmp280_data *data)
2335 {
2336 	struct nvmem_config config = {
2337 		.dev = data->dev,
2338 		.priv = data,
2339 		.name = "bmp580_nvmem",
2340 		.word_size = sizeof(u16),
2341 		.stride = sizeof(u16),
2342 		.size = 3 * sizeof(u16),
2343 		.reg_read = bmp580_nvmem_read,
2344 		.reg_write = bmp580_nvmem_write,
2345 	};
2346 	unsigned int reg;
2347 	int ret;
2348 
2349 	/* Issue soft-reset command */
2350 	ret = bmp580_soft_reset(data);
2351 	if (ret)
2352 		return ret;
2353 
2354 	/* Post powerup sequence */
2355 	ret = regmap_read(data->regmap, BMP580_REG_CHIP_ID, &reg);
2356 	if (ret) {
2357 		dev_err(data->dev, "failed to establish comms with the chip\n");
2358 		return ret;
2359 	}
2360 
2361 	/* Print warn message if we don't know the chip id */
2362 	if (reg != BMP580_CHIP_ID && reg != BMP580_CHIP_ID_ALT)
2363 		dev_warn(data->dev, "unexpected chip_id\n");
2364 
2365 	ret = regmap_read(data->regmap, BMP580_REG_STATUS, &reg);
2366 	if (ret) {
2367 		dev_err(data->dev, "failed to read nvm status\n");
2368 		return ret;
2369 	}
2370 
2371 	/* Check nvm status */
2372 	if (!(reg & BMP580_STATUS_NVM_RDY_MASK) || (reg & BMP580_STATUS_NVM_ERR_MASK)) {
2373 		dev_err(data->dev, "nvm error on powerup sequence\n");
2374 		return -EIO;
2375 	}
2376 
2377 	/* Register nvmem device */
2378 	return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
2379 }
2380 
2381 static const u8 bmp580_operation_mode[] = {
2382 	[BMP280_SLEEP] = BMP580_MODE_SLEEP,
2383 	[BMP280_FORCED] = BMP580_MODE_FORCED,
2384 	[BMP280_NORMAL] = BMP580_MODE_NORMAL,
2385 };
2386 
2387 static int bmp580_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode)
2388 {
2389 	struct device *dev = data->dev;
2390 	int ret;
2391 
2392 	if (mode == BMP280_FORCED) {
2393 		ret = regmap_set_bits(data->regmap, BMP580_REG_DSP_CONFIG,
2394 				      BMP580_DSP_IIR_FORCED_FLUSH);
2395 		if (ret) {
2396 			dev_err(dev, "Could not flush IIR filter constants.\n");
2397 			return ret;
2398 		}
2399 	}
2400 
2401 	ret = regmap_write_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2402 				BMP580_MODE_MASK,
2403 				FIELD_PREP(BMP580_MODE_MASK,
2404 					   bmp580_operation_mode[mode]));
2405 	if (ret) {
2406 		dev_err(dev, "failed to  write power control register.\n");
2407 		return ret;
2408 	}
2409 
2410 	data->op_mode = mode;
2411 
2412 	return 0;
2413 }
2414 
2415 static int bmp580_wait_conv(struct bmp280_data *data)
2416 {
2417 	/*
2418 	 * Taken from datasheet, Section 2 "Specification, Table 3 "Electrical
2419 	 * characteristics.
2420 	 */
2421 	static const int time_conv_press[] = {
2422 		0, 1050, 1785, 3045, 5670, 10920, 21420, 42420,
2423 		84420,
2424 	};
2425 	static const int time_conv_temp[] = {
2426 		0, 1050, 1105, 1575, 2205, 3465, 6090, 11340,
2427 		21840,
2428 	};
2429 	int meas_time_us;
2430 
2431 	meas_time_us = 4 * USEC_PER_MSEC +
2432 		       time_conv_temp[data->oversampling_temp] +
2433 		       time_conv_press[data->oversampling_press];
2434 
2435 	/*
2436 	 * Measurement time mentioned in Chapter 2, Table 4 of the datasheet.
2437 	 * The extra 4ms is the required mode change to start of measurement
2438 	 * time.
2439 	 */
2440 	fsleep(meas_time_us);
2441 
2442 	return 0;
2443 }
2444 
2445 static int bmp580_chip_config(struct bmp280_data *data)
2446 {
2447 	bool change = false, aux;
2448 	unsigned int tmp;
2449 	u8 reg_val;
2450 	int ret;
2451 
2452 	/* Sets sensor in standby mode */
2453 	ret = regmap_update_bits(data->regmap, BMP580_REG_ODR_CONFIG,
2454 				 BMP580_MODE_MASK | BMP580_ODR_DEEPSLEEP_DIS,
2455 				 BMP580_ODR_DEEPSLEEP_DIS |
2456 				 FIELD_PREP(BMP580_MODE_MASK, BMP580_MODE_SLEEP));
2457 	if (ret) {
2458 		dev_err(data->dev, "failed to change sensor to standby mode\n");
2459 		return ret;
2460 	}
2461 	/* From datasheet's table 4: electrical characteristics */
2462 	fsleep(2500);
2463 
2464 	/* Set default DSP mode settings */
2465 	reg_val = FIELD_PREP(BMP580_DSP_COMP_MASK, BMP580_DSP_PRESS_TEMP_COMP_EN) |
2466 		  BMP580_DSP_SHDW_IIR_TEMP_EN | BMP580_DSP_SHDW_IIR_PRESS_EN;
2467 
2468 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_CONFIG,
2469 				 BMP580_DSP_COMP_MASK |
2470 				 BMP580_DSP_SHDW_IIR_TEMP_EN |
2471 				 BMP580_DSP_SHDW_IIR_PRESS_EN, reg_val);
2472 	if (ret) {
2473 		dev_err(data->dev, "failed to change DSP mode settings\n");
2474 		return ret;
2475 	}
2476 
2477 	/* Configure oversampling */
2478 	reg_val = FIELD_PREP(BMP580_OSR_TEMP_MASK, data->oversampling_temp) |
2479 		  FIELD_PREP(BMP580_OSR_PRESS_MASK, data->oversampling_press) |
2480 		  BMP580_OSR_PRESS_EN;
2481 
2482 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_OSR_CONFIG,
2483 				       BMP580_OSR_TEMP_MASK |
2484 				       BMP580_OSR_PRESS_MASK |
2485 				       BMP580_OSR_PRESS_EN,
2486 				       reg_val, &aux);
2487 	if (ret) {
2488 		dev_err(data->dev, "failed to write oversampling register\n");
2489 		return ret;
2490 	}
2491 	change = change || aux;
2492 
2493 	/* Configure output data rate */
2494 	ret = regmap_update_bits_check(data->regmap, BMP580_REG_ODR_CONFIG, BMP580_ODR_MASK,
2495 				       FIELD_PREP(BMP580_ODR_MASK, data->sampling_freq),
2496 				       &aux);
2497 	if (ret) {
2498 		dev_err(data->dev, "failed to write ODR configuration register\n");
2499 		return ret;
2500 	}
2501 	change = change || aux;
2502 
2503 	/* Set filter data */
2504 	reg_val = FIELD_PREP(BMP580_DSP_IIR_PRESS_MASK, data->iir_filter_coeff) |
2505 		  FIELD_PREP(BMP580_DSP_IIR_TEMP_MASK, data->iir_filter_coeff);
2506 
2507 	ret = regmap_update_bits(data->regmap, BMP580_REG_DSP_IIR,
2508 				 BMP580_DSP_IIR_PRESS_MASK | BMP580_DSP_IIR_TEMP_MASK,
2509 				 reg_val);
2510 	if (ret) {
2511 		dev_err(data->dev, "failed to write config register\n");
2512 		return ret;
2513 	}
2514 
2515 	if (change) {
2516 		/*
2517 		 * Check if ODR and OSR settings are valid or we are
2518 		 * operating in a degraded mode.
2519 		 */
2520 		ret = regmap_read(data->regmap, BMP580_REG_EFF_OSR, &tmp);
2521 		if (ret) {
2522 			dev_err(data->dev,
2523 				"error reading effective OSR register\n");
2524 			return ret;
2525 		}
2526 		if (!(tmp & BMP580_EFF_OSR_VALID_ODR)) {
2527 			dev_warn(data->dev, "OSR and ODR incompatible settings detected\n");
2528 			/* Set current OSR settings from data on effective OSR */
2529 			data->oversampling_temp = FIELD_GET(BMP580_EFF_OSR_TEMP_MASK, tmp);
2530 			data->oversampling_press = FIELD_GET(BMP580_EFF_OSR_PRESS_MASK, tmp);
2531 			return -EINVAL;
2532 		}
2533 	}
2534 
2535 	return 0;
2536 }
2537 
2538 static int bmp580_data_rdy_trigger_set_state(struct iio_trigger *trig,
2539 					     bool state)
2540 {
2541 	struct bmp280_data *data = iio_trigger_get_drvdata(trig);
2542 	int ret;
2543 
2544 	guard(mutex)(&data->lock);
2545 
2546 	ret = regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG,
2547 				 BMP580_INT_CONFIG_INT_EN,
2548 				 FIELD_PREP(BMP580_INT_CONFIG_INT_EN, !!state));
2549 	if (ret)
2550 		dev_err(data->dev,
2551 			"Could not %s interrupt.\n", str_enable_disable(state));
2552 	return ret;
2553 }
2554 
2555 static const struct iio_trigger_ops bmp580_trigger_ops = {
2556 	.set_trigger_state = &bmp580_data_rdy_trigger_set_state,
2557 };
2558 
2559 static int bmp580_int_pin_config(struct bmp280_data *data)
2560 {
2561 	int pin_drive_cfg = FIELD_PREP(BMP580_INT_CONFIG_OPEN_DRAIN,
2562 				       data->trig_open_drain);
2563 	int pin_level_cfg = FIELD_PREP(BMP580_INT_CONFIG_LEVEL,
2564 				       data->trig_active_high);
2565 	int ret, int_pin_cfg = pin_drive_cfg | pin_level_cfg;
2566 
2567 	ret = regmap_update_bits(data->regmap, BMP580_REG_INT_CONFIG,
2568 				 BMP580_INT_CONFIG_MASK, int_pin_cfg);
2569 	if (ret) {
2570 		dev_err(data->dev, "Could not set interrupt settings.\n");
2571 		return ret;
2572 	}
2573 
2574 	ret = regmap_set_bits(data->regmap, BMP580_REG_INT_SOURCE,
2575 			      BMP580_INT_SOURCE_DRDY);
2576 	if (ret)
2577 		dev_err(data->dev, "Could not set interrupt source.\n");
2578 
2579 	return ret;
2580 }
2581 
2582 static irqreturn_t bmp580_irq_thread_handler(int irq, void *p)
2583 {
2584 	struct iio_dev *indio_dev = p;
2585 	struct bmp280_data *data = iio_priv(indio_dev);
2586 	unsigned int int_ctrl;
2587 	int ret;
2588 
2589 	ret = regmap_read(data->regmap, BMP580_REG_INT_STATUS, &int_ctrl);
2590 	if (ret)
2591 		return IRQ_NONE;
2592 
2593 	if (FIELD_GET(BMP580_INT_STATUS_DRDY_MASK, int_ctrl))
2594 		iio_trigger_poll_nested(data->trig);
2595 
2596 	return IRQ_HANDLED;
2597 }
2598 
2599 static int bmp580_trigger_probe(struct iio_dev *indio_dev)
2600 {
2601 	return __bmp280_trigger_probe(indio_dev, &bmp580_trigger_ops,
2602 				      bmp580_int_pin_config,
2603 				      bmp580_irq_thread_handler);
2604 }
2605 
2606 static irqreturn_t bmp580_trigger_handler(int irq, void *p)
2607 {
2608 	struct iio_poll_func *pf = p;
2609 	struct iio_dev *indio_dev = pf->indio_dev;
2610 	struct bmp280_data *data = iio_priv(indio_dev);
2611 	int ret, offset;
2612 
2613 	guard(mutex)(&data->lock);
2614 
2615 	/* Burst read data registers */
2616 	ret = regmap_bulk_read(data->regmap, BMP580_REG_TEMP_XLSB,
2617 			       data->buf, BMP280_BURST_READ_BYTES);
2618 	if (ret) {
2619 		dev_err(data->dev, "failed to burst read sensor data\n");
2620 		goto out;
2621 	}
2622 
2623 	offset = 0;
2624 
2625 	/* Pressure calculations */
2626 	memcpy(&data->sensor_data[offset], &data->buf[3], 3);
2627 
2628 	offset += sizeof(s32);
2629 
2630 	/* Temperature calculations */
2631 	memcpy(&data->sensor_data[offset], &data->buf[0], 3);
2632 
2633 	iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data,
2634 					   iio_get_time_ns(indio_dev));
2635 
2636 out:
2637 	iio_trigger_notify_done(indio_dev->trig);
2638 
2639 	return IRQ_HANDLED;
2640 }
2641 
2642 static const int bmp580_oversampling_avail[] = { 1, 2, 4, 8, 16, 32, 64, 128 };
2643 static const u8 bmp580_chip_ids[] = { BMP580_CHIP_ID, BMP580_CHIP_ID_ALT };
2644 /* Instead of { 1000, 16 } we do this, to avoid overflow issues */
2645 static const int bmp580_temp_coeffs[] = { 125, 13 };
2646 static const int bmp580_press_coeffs[] = { 1, 64000};
2647 
2648 const struct bmp280_chip_info bmp580_chip_info = {
2649 	.id_reg = BMP580_REG_CHIP_ID,
2650 	.chip_id = bmp580_chip_ids,
2651 	.num_chip_id = ARRAY_SIZE(bmp580_chip_ids),
2652 	.regmap_config = &bmp580_regmap_config,
2653 	.start_up_time_us = 2000,
2654 	.channels = bmp580_channels,
2655 	.num_channels = ARRAY_SIZE(bmp580_channels),
2656 	.avail_scan_masks = bmp280_avail_scan_masks,
2657 
2658 	.oversampling_temp_avail = bmp580_oversampling_avail,
2659 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp580_oversampling_avail),
2660 	.oversampling_temp_default = ilog2(1),
2661 
2662 	.oversampling_press_avail = bmp580_oversampling_avail,
2663 	.num_oversampling_press_avail = ARRAY_SIZE(bmp580_oversampling_avail),
2664 	.oversampling_press_default = ilog2(4),
2665 
2666 	.sampling_freq_avail = bmp580_odr_table,
2667 	.num_sampling_freq_avail = ARRAY_SIZE(bmp580_odr_table) * 2,
2668 	.sampling_freq_default = BMP580_ODR_50HZ,
2669 
2670 	.iir_filter_coeffs_avail = bmp380_iir_filter_coeffs_avail,
2671 	.num_iir_filter_coeffs_avail = ARRAY_SIZE(bmp380_iir_filter_coeffs_avail),
2672 	.iir_filter_coeff_default = 2,
2673 
2674 	.temp_coeffs = bmp580_temp_coeffs,
2675 	.temp_coeffs_type = IIO_VAL_FRACTIONAL_LOG2,
2676 	.press_coeffs = bmp580_press_coeffs,
2677 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
2678 
2679 	.chip_config = bmp580_chip_config,
2680 	.read_temp = bmp580_read_temp,
2681 	.read_press = bmp580_read_press,
2682 	.set_mode = bmp580_set_mode,
2683 	.wait_conv = bmp580_wait_conv,
2684 	.preinit = bmp580_preinit,
2685 
2686 	.trigger_probe = bmp580_trigger_probe,
2687 	.trigger_handler = bmp580_trigger_handler,
2688 };
2689 EXPORT_SYMBOL_NS(bmp580_chip_info, "IIO_BMP280");
2690 
2691 static int bmp180_wait_for_eoc(struct bmp280_data *data, u8 ctrl_meas)
2692 {
2693 	static const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
2694 	unsigned int delay_us;
2695 	unsigned int ctrl;
2696 	int ret;
2697 
2698 	if (data->use_eoc)
2699 		reinit_completion(&data->done);
2700 
2701 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
2702 	if (ret) {
2703 		dev_err(data->dev, "failed to write crtl_meas register\n");
2704 		return ret;
2705 	}
2706 
2707 	if (data->use_eoc) {
2708 		/*
2709 		 * If we have a completion interrupt, use it, wait up to
2710 		 * 100ms. The longest conversion time listed is 76.5 ms for
2711 		 * advanced resolution mode.
2712 		 */
2713 		ret = wait_for_completion_timeout(&data->done,
2714 						  1 + msecs_to_jiffies(100));
2715 		if (!ret)
2716 			dev_err(data->dev, "timeout waiting for completion\n");
2717 	} else {
2718 		if (FIELD_GET(BMP180_MEAS_CTRL_MASK, ctrl_meas) == BMP180_MEAS_TEMP)
2719 			delay_us = 4500;
2720 		else
2721 			delay_us =
2722 				conversion_time_max[data->oversampling_press];
2723 
2724 		fsleep(delay_us);
2725 	}
2726 
2727 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
2728 	if (ret) {
2729 		dev_err(data->dev, "failed to read ctrl_meas register\n");
2730 		return ret;
2731 	}
2732 
2733 	/* The value of this bit reset to "0" after conversion is complete */
2734 	if (ctrl & BMP180_MEAS_SCO) {
2735 		dev_err(data->dev, "conversion didn't complete\n");
2736 		return -EIO;
2737 	}
2738 
2739 	return 0;
2740 }
2741 
2742 static int bmp180_read_temp_adc(struct bmp280_data *data, u32 *adc_temp)
2743 {
2744 	int ret;
2745 
2746 	ret = bmp180_wait_for_eoc(data,
2747 				  FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_TEMP) |
2748 				  BMP180_MEAS_SCO);
2749 	if (ret)
2750 		return ret;
2751 
2752 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
2753 			       &data->be16, sizeof(data->be16));
2754 	if (ret) {
2755 		dev_err(data->dev, "failed to read temperature\n");
2756 		return ret;
2757 	}
2758 
2759 	*adc_temp = be16_to_cpu(data->be16);
2760 
2761 	return 0;
2762 }
2763 
2764 static int bmp180_read_calib(struct bmp280_data *data)
2765 {
2766 	struct bmp180_calib *calib = &data->calib.bmp180;
2767 	int ret;
2768 	int i;
2769 
2770 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START,
2771 			       data->bmp180_cal_buf, sizeof(data->bmp180_cal_buf));
2772 	if (ret) {
2773 		dev_err(data->dev, "failed to read calibration parameters\n");
2774 		return ret;
2775 	}
2776 
2777 	/* None of the words has the value 0 or 0xFFFF */
2778 	for (i = 0; i < ARRAY_SIZE(data->bmp180_cal_buf); i++) {
2779 		if (data->bmp180_cal_buf[i] == cpu_to_be16(0) ||
2780 		    data->bmp180_cal_buf[i] == cpu_to_be16(0xffff))
2781 			return -EIO;
2782 	}
2783 
2784 	/* Toss the calibration data into the entropy pool */
2785 	add_device_randomness(data->bmp180_cal_buf,
2786 			      sizeof(data->bmp180_cal_buf));
2787 
2788 	calib->AC1 = be16_to_cpu(data->bmp180_cal_buf[AC1]);
2789 	calib->AC2 = be16_to_cpu(data->bmp180_cal_buf[AC2]);
2790 	calib->AC3 = be16_to_cpu(data->bmp180_cal_buf[AC3]);
2791 	calib->AC4 = be16_to_cpu(data->bmp180_cal_buf[AC4]);
2792 	calib->AC5 = be16_to_cpu(data->bmp180_cal_buf[AC5]);
2793 	calib->AC6 = be16_to_cpu(data->bmp180_cal_buf[AC6]);
2794 	calib->B1 = be16_to_cpu(data->bmp180_cal_buf[B1]);
2795 	calib->B2 = be16_to_cpu(data->bmp180_cal_buf[B2]);
2796 	calib->MB = be16_to_cpu(data->bmp180_cal_buf[MB]);
2797 	calib->MC = be16_to_cpu(data->bmp180_cal_buf[MC]);
2798 	calib->MD = be16_to_cpu(data->bmp180_cal_buf[MD]);
2799 
2800 	return 0;
2801 }
2802 
2803 /*
2804  * Returns temperature in DegC, resolution is 0.1 DegC.
2805  * t_fine carries fine temperature as global value.
2806  *
2807  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
2808  */
2809 
2810 static s32 bmp180_calc_t_fine(struct bmp280_data *data, u32 adc_temp)
2811 {
2812 	struct bmp180_calib *calib = &data->calib.bmp180;
2813 	s32 x1, x2;
2814 
2815 	x1 = ((((s32)adc_temp) - calib->AC6) * calib->AC5) >> 15;
2816 	x2 = (calib->MC << 11) / (x1 + calib->MD);
2817 	return x1 + x2; /* t_fine = x1 + x2; */
2818 }
2819 
2820 static int bmp180_get_t_fine(struct bmp280_data *data, s32 *t_fine)
2821 {
2822 	s32 adc_temp;
2823 	int ret;
2824 
2825 	ret = bmp180_read_temp_adc(data, &adc_temp);
2826 	if (ret)
2827 		return ret;
2828 
2829 	*t_fine = bmp180_calc_t_fine(data, adc_temp);
2830 
2831 	return 0;
2832 }
2833 
2834 static s32 bmp180_compensate_temp(struct bmp280_data *data, u32 adc_temp)
2835 {
2836 	return (bmp180_calc_t_fine(data, adc_temp) + 8) / 16;
2837 }
2838 
2839 static int bmp180_read_temp(struct bmp280_data *data, s32 *comp_temp)
2840 {
2841 	u32 adc_temp;
2842 	int ret;
2843 
2844 	ret = bmp180_read_temp_adc(data, &adc_temp);
2845 	if (ret)
2846 		return ret;
2847 
2848 	*comp_temp = bmp180_compensate_temp(data, adc_temp);
2849 
2850 	return 0;
2851 }
2852 
2853 static int bmp180_read_press_adc(struct bmp280_data *data, u32 *adc_press)
2854 {
2855 	u8 oss = data->oversampling_press;
2856 	int ret;
2857 
2858 	ret = bmp180_wait_for_eoc(data,
2859 				  FIELD_PREP(BMP180_MEAS_CTRL_MASK, BMP180_MEAS_PRESS) |
2860 				  FIELD_PREP(BMP180_OSRS_PRESS_MASK, oss) |
2861 				  BMP180_MEAS_SCO);
2862 	if (ret)
2863 		return ret;
2864 
2865 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB,
2866 			       data->buf, BMP280_NUM_PRESS_BYTES);
2867 	if (ret) {
2868 		dev_err(data->dev, "failed to read pressure\n");
2869 		return ret;
2870 	}
2871 
2872 	*adc_press = get_unaligned_be24(data->buf) >> (8 - oss);
2873 
2874 	return 0;
2875 }
2876 
2877 /*
2878  * Returns pressure in Pa, resolution is 1 Pa.
2879  *
2880  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
2881  */
2882 static u32 bmp180_compensate_press(struct bmp280_data *data, u32 adc_press,
2883 				   s32 t_fine)
2884 {
2885 	struct bmp180_calib *calib = &data->calib.bmp180;
2886 	s32 oss = data->oversampling_press;
2887 	s32 x1, x2, x3, p;
2888 	s32 b3, b6;
2889 	u32 b4, b7;
2890 
2891 	b6 = t_fine - 4000;
2892 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
2893 	x2 = calib->AC2 * b6 >> 11;
2894 	x3 = x1 + x2;
2895 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
2896 	x1 = calib->AC3 * b6 >> 13;
2897 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
2898 	x3 = (x1 + x2 + 2) >> 2;
2899 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
2900 	b7 = (adc_press - b3) * (50000 >> oss);
2901 	if (b7 < 0x80000000)
2902 		p = (b7 * 2) / b4;
2903 	else
2904 		p = (b7 / b4) * 2;
2905 
2906 	x1 = (p >> 8) * (p >> 8);
2907 	x1 = (x1 * 3038) >> 16;
2908 	x2 = (-7357 * p) >> 16;
2909 
2910 	return p + ((x1 + x2 + 3791) >> 4);
2911 }
2912 
2913 static int bmp180_read_press(struct bmp280_data *data, u32 *comp_press)
2914 {
2915 	u32 adc_press;
2916 	s32 t_fine;
2917 	int ret;
2918 
2919 	ret = bmp180_get_t_fine(data, &t_fine);
2920 	if (ret)
2921 		return ret;
2922 
2923 	ret = bmp180_read_press_adc(data, &adc_press);
2924 	if (ret)
2925 		return ret;
2926 
2927 	*comp_press = bmp180_compensate_press(data, adc_press, t_fine);
2928 
2929 	return 0;
2930 }
2931 
2932 /* Keep compatibility with newer generations of the sensor */
2933 static int bmp180_set_mode(struct bmp280_data *data, enum bmp280_op_mode mode)
2934 {
2935 	return 0;
2936 }
2937 
2938 /* Keep compatibility with newer generations of the sensor */
2939 static int bmp180_wait_conv(struct bmp280_data *data)
2940 {
2941 	return 0;
2942 }
2943 
2944 /* Keep compatibility with newer generations of the sensor */
2945 static int bmp180_chip_config(struct bmp280_data *data)
2946 {
2947 	return 0;
2948 }
2949 
2950 static irqreturn_t bmp180_trigger_handler(int irq, void *p)
2951 {
2952 	struct iio_poll_func *pf = p;
2953 	struct iio_dev *indio_dev = pf->indio_dev;
2954 	struct bmp280_data *data = iio_priv(indio_dev);
2955 	int ret, comp_temp, comp_press;
2956 	s32 *chans = (s32 *)data->sensor_data;
2957 
2958 	guard(mutex)(&data->lock);
2959 
2960 	ret = bmp180_read_temp(data, &comp_temp);
2961 	if (ret)
2962 		goto out;
2963 
2964 
2965 	ret = bmp180_read_press(data, &comp_press);
2966 	if (ret)
2967 		goto out;
2968 
2969 	chans[0] = comp_press;
2970 	chans[1] = comp_temp;
2971 
2972 	iio_push_to_buffers_with_timestamp(indio_dev, data->sensor_data,
2973 					   iio_get_time_ns(indio_dev));
2974 
2975 out:
2976 	iio_trigger_notify_done(indio_dev->trig);
2977 
2978 	return IRQ_HANDLED;
2979 }
2980 
2981 static const int bmp180_oversampling_temp_avail[] = { 1 };
2982 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
2983 static const u8 bmp180_chip_ids[] = { BMP180_CHIP_ID };
2984 static const int bmp180_temp_coeffs[] = { 100, 1 };
2985 static const int bmp180_press_coeffs[] = { 1, 1000 };
2986 
2987 const struct bmp280_chip_info bmp180_chip_info = {
2988 	.id_reg = BMP280_REG_ID,
2989 	.chip_id = bmp180_chip_ids,
2990 	.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
2991 	.regmap_config = &bmp180_regmap_config,
2992 	.start_up_time_us = 2000,
2993 	.channels = bmp280_channels,
2994 	.num_channels = ARRAY_SIZE(bmp280_channels),
2995 	.avail_scan_masks = bmp280_avail_scan_masks,
2996 
2997 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
2998 	.num_oversampling_temp_avail =
2999 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
3000 	.oversampling_temp_default = 0,
3001 
3002 	.oversampling_press_avail = bmp180_oversampling_press_avail,
3003 	.num_oversampling_press_avail =
3004 		ARRAY_SIZE(bmp180_oversampling_press_avail),
3005 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
3006 
3007 	.temp_coeffs = bmp180_temp_coeffs,
3008 	.temp_coeffs_type = IIO_VAL_FRACTIONAL,
3009 	.press_coeffs = bmp180_press_coeffs,
3010 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
3011 
3012 	.chip_config = bmp180_chip_config,
3013 	.read_temp = bmp180_read_temp,
3014 	.read_press = bmp180_read_press,
3015 	.read_calib = bmp180_read_calib,
3016 	.set_mode = bmp180_set_mode,
3017 	.wait_conv = bmp180_wait_conv,
3018 
3019 	.trigger_handler = bmp180_trigger_handler,
3020 };
3021 EXPORT_SYMBOL_NS(bmp180_chip_info, "IIO_BMP280");
3022 
3023 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
3024 {
3025 	struct bmp280_data *data = d;
3026 
3027 	complete(&data->done);
3028 
3029 	return IRQ_HANDLED;
3030 }
3031 
3032 static int bmp085_trigger_probe(struct iio_dev *indio_dev)
3033 {
3034 	struct bmp280_data *data = iio_priv(indio_dev);
3035 	struct device *dev = data->dev;
3036 	unsigned long irq_trig;
3037 	int ret, irq;
3038 
3039 	irq = fwnode_irq_get(dev_fwnode(dev), 0);
3040 	if (irq < 0)
3041 		return dev_err_probe(dev, irq, "No interrupt found.\n");
3042 
3043 	irq_trig = irq_get_trigger_type(irq);
3044 	if (irq_trig != IRQF_TRIGGER_RISING) {
3045 		dev_err(dev, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
3046 		irq_trig = IRQF_TRIGGER_RISING;
3047 	}
3048 
3049 	init_completion(&data->done);
3050 
3051 	ret = devm_request_irq(dev, irq, bmp085_eoc_irq, irq_trig,
3052 			       indio_dev->name, data);
3053 	if (ret) {
3054 		/* Bail out without IRQ but keep the driver in place */
3055 		dev_err(dev, "unable to request DRDY IRQ\n");
3056 		return 0;
3057 	}
3058 
3059 	data->use_eoc = true;
3060 
3061 	return 0;
3062 }
3063 
3064 /* Identical to bmp180_chip_info + bmp085_trigger_probe */
3065 const struct bmp280_chip_info bmp085_chip_info = {
3066 	.id_reg = BMP280_REG_ID,
3067 	.chip_id = bmp180_chip_ids,
3068 	.num_chip_id = ARRAY_SIZE(bmp180_chip_ids),
3069 	.regmap_config = &bmp180_regmap_config,
3070 	.start_up_time_us = 2000,
3071 	.channels = bmp280_channels,
3072 	.num_channels = ARRAY_SIZE(bmp280_channels),
3073 	.avail_scan_masks = bmp280_avail_scan_masks,
3074 
3075 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
3076 	.num_oversampling_temp_avail =
3077 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
3078 	.oversampling_temp_default = 0,
3079 
3080 	.oversampling_press_avail = bmp180_oversampling_press_avail,
3081 	.num_oversampling_press_avail =
3082 		ARRAY_SIZE(bmp180_oversampling_press_avail),
3083 	.oversampling_press_default = BMP180_MEAS_PRESS_8X,
3084 
3085 	.temp_coeffs = bmp180_temp_coeffs,
3086 	.temp_coeffs_type = IIO_VAL_FRACTIONAL,
3087 	.press_coeffs = bmp180_press_coeffs,
3088 	.press_coeffs_type = IIO_VAL_FRACTIONAL,
3089 
3090 	.chip_config = bmp180_chip_config,
3091 	.read_temp = bmp180_read_temp,
3092 	.read_press = bmp180_read_press,
3093 	.read_calib = bmp180_read_calib,
3094 	.set_mode = bmp180_set_mode,
3095 	.wait_conv = bmp180_wait_conv,
3096 
3097 	.trigger_probe = bmp085_trigger_probe,
3098 	.trigger_handler = bmp180_trigger_handler,
3099 };
3100 EXPORT_SYMBOL_NS(bmp085_chip_info, "IIO_BMP280");
3101 
3102 static int bmp280_buffer_preenable(struct iio_dev *indio_dev)
3103 {
3104 	struct bmp280_data *data = iio_priv(indio_dev);
3105 
3106 	pm_runtime_get_sync(data->dev);
3107 	data->chip_info->set_mode(data, BMP280_NORMAL);
3108 
3109 	return 0;
3110 }
3111 
3112 static int bmp280_buffer_postdisable(struct iio_dev *indio_dev)
3113 {
3114 	struct bmp280_data *data = iio_priv(indio_dev);
3115 
3116 	pm_runtime_mark_last_busy(data->dev);
3117 	pm_runtime_put_autosuspend(data->dev);
3118 
3119 	return 0;
3120 }
3121 
3122 static const struct iio_buffer_setup_ops bmp280_buffer_setup_ops = {
3123 	.preenable = bmp280_buffer_preenable,
3124 	.postdisable = bmp280_buffer_postdisable,
3125 };
3126 
3127 static void bmp280_pm_disable(void *data)
3128 {
3129 	struct device *dev = data;
3130 
3131 	pm_runtime_get_sync(dev);
3132 	pm_runtime_put_noidle(dev);
3133 	pm_runtime_disable(dev);
3134 }
3135 
3136 static void bmp280_regulators_disable(void *data)
3137 {
3138 	struct regulator_bulk_data *supplies = data;
3139 
3140 	regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
3141 }
3142 
3143 int bmp280_common_probe(struct device *dev,
3144 			struct regmap *regmap,
3145 			const struct bmp280_chip_info *chip_info,
3146 			const char *name,
3147 			int irq)
3148 {
3149 	struct iio_dev *indio_dev;
3150 	struct bmp280_data *data;
3151 	struct gpio_desc *gpiod;
3152 	unsigned int chip_id;
3153 	unsigned int i;
3154 	int ret;
3155 
3156 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
3157 	if (!indio_dev)
3158 		return -ENOMEM;
3159 
3160 	data = iio_priv(indio_dev);
3161 	mutex_init(&data->lock);
3162 	data->dev = dev;
3163 
3164 	indio_dev->name = name;
3165 	indio_dev->info = &bmp280_info;
3166 	indio_dev->modes = INDIO_DIRECT_MODE;
3167 
3168 	data->chip_info = chip_info;
3169 
3170 	/* Apply initial values from chip info structure */
3171 	indio_dev->channels = chip_info->channels;
3172 	indio_dev->num_channels = chip_info->num_channels;
3173 	indio_dev->available_scan_masks = chip_info->avail_scan_masks;
3174 	data->oversampling_press = chip_info->oversampling_press_default;
3175 	data->oversampling_humid = chip_info->oversampling_humid_default;
3176 	data->oversampling_temp = chip_info->oversampling_temp_default;
3177 	data->iir_filter_coeff = chip_info->iir_filter_coeff_default;
3178 	data->sampling_freq = chip_info->sampling_freq_default;
3179 	data->start_up_time_us = chip_info->start_up_time_us;
3180 
3181 	/* Bring up regulators */
3182 	regulator_bulk_set_supply_names(data->supplies,
3183 					bmp280_supply_names,
3184 					BMP280_NUM_SUPPLIES);
3185 
3186 	ret = devm_regulator_bulk_get(dev,
3187 				      BMP280_NUM_SUPPLIES, data->supplies);
3188 	if (ret) {
3189 		dev_err(dev, "failed to get regulators\n");
3190 		return ret;
3191 	}
3192 
3193 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
3194 	if (ret) {
3195 		dev_err(dev, "failed to enable regulators\n");
3196 		return ret;
3197 	}
3198 
3199 	ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
3200 				       data->supplies);
3201 	if (ret)
3202 		return ret;
3203 
3204 	/* Wait to make sure we started up properly */
3205 	fsleep(data->start_up_time_us);
3206 
3207 	/* Bring chip out of reset if there is an assigned GPIO line */
3208 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
3209 	/* Deassert the signal */
3210 	if (gpiod) {
3211 		dev_info(dev, "release reset\n");
3212 		gpiod_set_value(gpiod, 0);
3213 	}
3214 
3215 	data->regmap = regmap;
3216 
3217 	ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
3218 	if (ret) {
3219 		dev_err(data->dev, "failed to read chip id\n");
3220 		return ret;
3221 	}
3222 
3223 	for (i = 0; i < data->chip_info->num_chip_id; i++) {
3224 		if (chip_id == data->chip_info->chip_id[i]) {
3225 			dev_info(dev, "0x%x is a known chip id for %s\n", chip_id, name);
3226 			break;
3227 		}
3228 	}
3229 
3230 	if (i == data->chip_info->num_chip_id)
3231 		dev_warn(dev, "bad chip id: 0x%x is not a known chip id\n", chip_id);
3232 
3233 	if (data->chip_info->preinit) {
3234 		ret = data->chip_info->preinit(data);
3235 		if (ret)
3236 			return dev_err_probe(data->dev, ret,
3237 					     "error running preinit tasks\n");
3238 	}
3239 
3240 	ret = data->chip_info->chip_config(data);
3241 	if (ret)
3242 		return ret;
3243 
3244 	dev_set_drvdata(dev, indio_dev);
3245 
3246 	/*
3247 	 * Some chips have calibration parameters "programmed into the devices'
3248 	 * non-volatile memory during production". Let's read them out at probe
3249 	 * time once. They will not change.
3250 	 */
3251 
3252 	if (data->chip_info->read_calib) {
3253 		ret = data->chip_info->read_calib(data);
3254 		if (ret)
3255 			return dev_err_probe(data->dev, ret,
3256 					     "failed to read calibration coefficients\n");
3257 	}
3258 
3259 	ret = devm_iio_triggered_buffer_setup(data->dev, indio_dev,
3260 					      iio_pollfunc_store_time,
3261 					      data->chip_info->trigger_handler,
3262 					      &bmp280_buffer_setup_ops);
3263 	if (ret)
3264 		return dev_err_probe(data->dev, ret,
3265 				     "iio triggered buffer setup failed\n");
3266 
3267 	/*
3268 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
3269 	 * however as it happens, the BMP085 shares the chip ID of BMP180
3270 	 * so we look for an IRQ if we have that.
3271 	 */
3272 	if (irq > 0) {
3273 		if (data->chip_info->trigger_probe)
3274 			ret = data->chip_info->trigger_probe(indio_dev);
3275 		if (ret)
3276 			return ret;
3277 	}
3278 
3279 	ret = data->chip_info->set_mode(data, BMP280_SLEEP);
3280 	if (ret)
3281 		return dev_err_probe(dev, ret, "Failed to set sleep mode\n");
3282 
3283 	/* Enable runtime PM */
3284 	pm_runtime_get_noresume(dev);
3285 	pm_runtime_set_active(dev);
3286 	pm_runtime_enable(dev);
3287 	/*
3288 	 * Set autosuspend to two orders of magnitude larger than the
3289 	 * start-up time.
3290 	 */
3291 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time_us / 10);
3292 	pm_runtime_use_autosuspend(dev);
3293 	pm_runtime_put(dev);
3294 
3295 	ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
3296 	if (ret)
3297 		return ret;
3298 
3299 	return devm_iio_device_register(dev, indio_dev);
3300 }
3301 EXPORT_SYMBOL_NS(bmp280_common_probe, "IIO_BMP280");
3302 
3303 static int bmp280_runtime_suspend(struct device *dev)
3304 {
3305 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
3306 	struct bmp280_data *data = iio_priv(indio_dev);
3307 
3308 	data->chip_info->set_mode(data, BMP280_SLEEP);
3309 
3310 	fsleep(data->start_up_time_us);
3311 	return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
3312 }
3313 
3314 static int bmp280_runtime_resume(struct device *dev)
3315 {
3316 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
3317 	struct bmp280_data *data = iio_priv(indio_dev);
3318 	int ret;
3319 
3320 	ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
3321 	if (ret)
3322 		return ret;
3323 
3324 	fsleep(data->start_up_time_us);
3325 
3326 	ret = data->chip_info->chip_config(data);
3327 	if (ret)
3328 		return ret;
3329 
3330 	return data->chip_info->set_mode(data, data->op_mode);
3331 }
3332 
3333 EXPORT_RUNTIME_DEV_PM_OPS(bmp280_dev_pm_ops, bmp280_runtime_suspend,
3334 			  bmp280_runtime_resume, NULL);
3335 
3336 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
3337 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
3338 MODULE_LICENSE("GPL v2");
3339