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