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