xref: /linux/drivers/iio/pressure/bmp280-core.c (revision 44f57d78)
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://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
13  * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
14  * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
15  */
16 
17 #define pr_fmt(fmt) "bmp280: " fmt
18 
19 #include <linux/device.h>
20 #include <linux/module.h>
21 #include <linux/regmap.h>
22 #include <linux/delay.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/gpio/consumer.h>
26 #include <linux/regulator/consumer.h>
27 #include <linux/interrupt.h>
28 #include <linux/irq.h> /* For irq_get_irq_data() */
29 #include <linux/completion.h>
30 #include <linux/pm_runtime.h>
31 #include <linux/random.h>
32 
33 #include "bmp280.h"
34 
35 /*
36  * These enums are used for indexing into the array of calibration
37  * coefficients for BMP180.
38  */
39 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
40 
41 struct bmp180_calib {
42 	s16 AC1;
43 	s16 AC2;
44 	s16 AC3;
45 	u16 AC4;
46 	u16 AC5;
47 	u16 AC6;
48 	s16 B1;
49 	s16 B2;
50 	s16 MB;
51 	s16 MC;
52 	s16 MD;
53 };
54 
55 /* See datasheet Section 4.2.2. */
56 struct bmp280_calib {
57 	u16 T1;
58 	s16 T2;
59 	s16 T3;
60 	u16 P1;
61 	s16 P2;
62 	s16 P3;
63 	s16 P4;
64 	s16 P5;
65 	s16 P6;
66 	s16 P7;
67 	s16 P8;
68 	s16 P9;
69 	u8  H1;
70 	s16 H2;
71 	u8  H3;
72 	s16 H4;
73 	s16 H5;
74 	s8  H6;
75 };
76 
77 struct bmp280_data {
78 	struct device *dev;
79 	struct mutex lock;
80 	struct regmap *regmap;
81 	struct completion done;
82 	bool use_eoc;
83 	const struct bmp280_chip_info *chip_info;
84 	union {
85 		struct bmp180_calib bmp180;
86 		struct bmp280_calib bmp280;
87 	} calib;
88 	struct regulator *vddd;
89 	struct regulator *vdda;
90 	unsigned int start_up_time; /* in microseconds */
91 
92 	/* log of base 2 of oversampling rate */
93 	u8 oversampling_press;
94 	u8 oversampling_temp;
95 	u8 oversampling_humid;
96 
97 	/*
98 	 * Carryover value from temperature conversion, used in pressure
99 	 * calculation.
100 	 */
101 	s32 t_fine;
102 };
103 
104 struct bmp280_chip_info {
105 	const int *oversampling_temp_avail;
106 	int num_oversampling_temp_avail;
107 
108 	const int *oversampling_press_avail;
109 	int num_oversampling_press_avail;
110 
111 	const int *oversampling_humid_avail;
112 	int num_oversampling_humid_avail;
113 
114 	int (*chip_config)(struct bmp280_data *);
115 	int (*read_temp)(struct bmp280_data *, int *);
116 	int (*read_press)(struct bmp280_data *, int *, int *);
117 	int (*read_humid)(struct bmp280_data *, int *, int *);
118 };
119 
120 /*
121  * These enums are used for indexing into the array of compensation
122  * parameters for BMP280.
123  */
124 enum { T1, T2, T3 };
125 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
126 
127 static const struct iio_chan_spec bmp280_channels[] = {
128 	{
129 		.type = IIO_PRESSURE,
130 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
131 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
132 	},
133 	{
134 		.type = IIO_TEMP,
135 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
136 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
137 	},
138 	{
139 		.type = IIO_HUMIDITYRELATIVE,
140 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
141 				      BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
142 	},
143 };
144 
145 static int bmp280_read_calib(struct bmp280_data *data,
146 			     struct bmp280_calib *calib,
147 			     unsigned int chip)
148 {
149 	int ret;
150 	unsigned int tmp;
151 	struct device *dev = data->dev;
152 	__le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
153 	__le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];
154 
155 	/* Read temperature calibration values. */
156 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
157 			       t_buf, BMP280_COMP_TEMP_REG_COUNT);
158 	if (ret < 0) {
159 		dev_err(data->dev,
160 			"failed to read temperature calibration parameters\n");
161 		return ret;
162 	}
163 
164 	/* Toss the temperature calibration data into the entropy pool */
165 	add_device_randomness(t_buf, sizeof(t_buf));
166 
167 	calib->T1 = le16_to_cpu(t_buf[T1]);
168 	calib->T2 = le16_to_cpu(t_buf[T2]);
169 	calib->T3 = le16_to_cpu(t_buf[T3]);
170 
171 	/* Read pressure calibration values. */
172 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
173 			       p_buf, BMP280_COMP_PRESS_REG_COUNT);
174 	if (ret < 0) {
175 		dev_err(data->dev,
176 			"failed to read pressure calibration parameters\n");
177 		return ret;
178 	}
179 
180 	/* Toss the pressure calibration data into the entropy pool */
181 	add_device_randomness(p_buf, sizeof(p_buf));
182 
183 	calib->P1 = le16_to_cpu(p_buf[P1]);
184 	calib->P2 = le16_to_cpu(p_buf[P2]);
185 	calib->P3 = le16_to_cpu(p_buf[P3]);
186 	calib->P4 = le16_to_cpu(p_buf[P4]);
187 	calib->P5 = le16_to_cpu(p_buf[P5]);
188 	calib->P6 = le16_to_cpu(p_buf[P6]);
189 	calib->P7 = le16_to_cpu(p_buf[P7]);
190 	calib->P8 = le16_to_cpu(p_buf[P8]);
191 	calib->P9 = le16_to_cpu(p_buf[P9]);
192 
193 	/*
194 	 * Read humidity calibration values.
195 	 * Due to some odd register addressing we cannot just
196 	 * do a big bulk read. Instead, we have to read each Hx
197 	 * value separately and sometimes do some bit shifting...
198 	 * Humidity data is only available on BME280.
199 	 */
200 	if (chip != BME280_CHIP_ID)
201 		return 0;
202 
203 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
204 	if (ret < 0) {
205 		dev_err(dev, "failed to read H1 comp value\n");
206 		return ret;
207 	}
208 	calib->H1 = tmp;
209 
210 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
211 	if (ret < 0) {
212 		dev_err(dev, "failed to read H2 comp value\n");
213 		return ret;
214 	}
215 	calib->H2 = sign_extend32(le16_to_cpu(tmp), 15);
216 
217 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
218 	if (ret < 0) {
219 		dev_err(dev, "failed to read H3 comp value\n");
220 		return ret;
221 	}
222 	calib->H3 = tmp;
223 
224 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
225 	if (ret < 0) {
226 		dev_err(dev, "failed to read H4 comp value\n");
227 		return ret;
228 	}
229 	calib->H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
230 				  (be16_to_cpu(tmp) & 0xf), 11);
231 
232 	ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
233 	if (ret < 0) {
234 		dev_err(dev, "failed to read H5 comp value\n");
235 		return ret;
236 	}
237 	calib->H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
238 
239 	ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
240 	if (ret < 0) {
241 		dev_err(dev, "failed to read H6 comp value\n");
242 		return ret;
243 	}
244 	calib->H6 = sign_extend32(tmp, 7);
245 
246 	return 0;
247 }
248 /*
249  * Returns humidity in percent, resolution is 0.01 percent. Output value of
250  * "47445" represents 47445/1024 = 46.333 %RH.
251  *
252  * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
253  */
254 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
255 				      s32 adc_humidity)
256 {
257 	s32 var;
258 	struct bmp280_calib *calib = &data->calib.bmp280;
259 
260 	var = ((s32)data->t_fine) - (s32)76800;
261 	var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
262 		+ (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
263 		* (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
264 		+ (s32)2097152) * calib->H2 + 8192) >> 14);
265 	var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
266 
267 	return var >> 12;
268 };
269 
270 /*
271  * Returns temperature in DegC, resolution is 0.01 DegC.  Output value of
272  * "5123" equals 51.23 DegC.  t_fine carries fine temperature as global
273  * value.
274  *
275  * Taken from datasheet, Section 3.11.3, "Compensation formula".
276  */
277 static s32 bmp280_compensate_temp(struct bmp280_data *data,
278 				  s32 adc_temp)
279 {
280 	s32 var1, var2;
281 	struct bmp280_calib *calib = &data->calib.bmp280;
282 
283 	var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
284 		((s32)calib->T2)) >> 11;
285 	var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
286 		  ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
287 		((s32)calib->T3)) >> 14;
288 	data->t_fine = var1 + var2;
289 
290 	return (data->t_fine * 5 + 128) >> 8;
291 }
292 
293 /*
294  * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
295  * integer bits and 8 fractional bits).  Output value of "24674867"
296  * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
297  *
298  * Taken from datasheet, Section 3.11.3, "Compensation formula".
299  */
300 static u32 bmp280_compensate_press(struct bmp280_data *data,
301 				   s32 adc_press)
302 {
303 	s64 var1, var2, p;
304 	struct bmp280_calib *calib = &data->calib.bmp280;
305 
306 	var1 = ((s64)data->t_fine) - 128000;
307 	var2 = var1 * var1 * (s64)calib->P6;
308 	var2 += (var1 * (s64)calib->P5) << 17;
309 	var2 += ((s64)calib->P4) << 35;
310 	var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
311 		((var1 * (s64)calib->P2) << 12);
312 	var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
313 
314 	if (var1 == 0)
315 		return 0;
316 
317 	p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
318 	p = div64_s64(p, var1);
319 	var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
320 	var2 = ((s64)(calib->P8) * p) >> 19;
321 	p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
322 
323 	return (u32)p;
324 }
325 
326 static int bmp280_read_temp(struct bmp280_data *data,
327 			    int *val)
328 {
329 	int ret;
330 	__be32 tmp = 0;
331 	s32 adc_temp, comp_temp;
332 
333 	ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
334 			       (u8 *) &tmp, 3);
335 	if (ret < 0) {
336 		dev_err(data->dev, "failed to read temperature\n");
337 		return ret;
338 	}
339 
340 	adc_temp = be32_to_cpu(tmp) >> 12;
341 	if (adc_temp == BMP280_TEMP_SKIPPED) {
342 		/* reading was skipped */
343 		dev_err(data->dev, "reading temperature skipped\n");
344 		return -EIO;
345 	}
346 	comp_temp = bmp280_compensate_temp(data, adc_temp);
347 
348 	/*
349 	 * val might be NULL if we're called by the read_press routine,
350 	 * who only cares about the carry over t_fine value.
351 	 */
352 	if (val) {
353 		*val = comp_temp * 10;
354 		return IIO_VAL_INT;
355 	}
356 
357 	return 0;
358 }
359 
360 static int bmp280_read_press(struct bmp280_data *data,
361 			     int *val, int *val2)
362 {
363 	int ret;
364 	__be32 tmp = 0;
365 	s32 adc_press;
366 	u32 comp_press;
367 
368 	/* Read and compensate temperature so we get a reading of t_fine. */
369 	ret = bmp280_read_temp(data, NULL);
370 	if (ret < 0)
371 		return ret;
372 
373 	ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
374 			       (u8 *) &tmp, 3);
375 	if (ret < 0) {
376 		dev_err(data->dev, "failed to read pressure\n");
377 		return ret;
378 	}
379 
380 	adc_press = be32_to_cpu(tmp) >> 12;
381 	if (adc_press == BMP280_PRESS_SKIPPED) {
382 		/* reading was skipped */
383 		dev_err(data->dev, "reading pressure skipped\n");
384 		return -EIO;
385 	}
386 	comp_press = bmp280_compensate_press(data, adc_press);
387 
388 	*val = comp_press;
389 	*val2 = 256000;
390 
391 	return IIO_VAL_FRACTIONAL;
392 }
393 
394 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
395 {
396 	int ret;
397 	__be16 tmp = 0;
398 	s32 adc_humidity;
399 	u32 comp_humidity;
400 
401 	/* Read and compensate temperature so we get a reading of t_fine. */
402 	ret = bmp280_read_temp(data, NULL);
403 	if (ret < 0)
404 		return ret;
405 
406 	ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
407 			       (u8 *) &tmp, 2);
408 	if (ret < 0) {
409 		dev_err(data->dev, "failed to read humidity\n");
410 		return ret;
411 	}
412 
413 	adc_humidity = be16_to_cpu(tmp);
414 	if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
415 		/* reading was skipped */
416 		dev_err(data->dev, "reading humidity skipped\n");
417 		return -EIO;
418 	}
419 	comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
420 
421 	*val = comp_humidity * 1000 / 1024;
422 
423 	return IIO_VAL_INT;
424 }
425 
426 static int bmp280_read_raw(struct iio_dev *indio_dev,
427 			   struct iio_chan_spec const *chan,
428 			   int *val, int *val2, long mask)
429 {
430 	int ret;
431 	struct bmp280_data *data = iio_priv(indio_dev);
432 
433 	pm_runtime_get_sync(data->dev);
434 	mutex_lock(&data->lock);
435 
436 	switch (mask) {
437 	case IIO_CHAN_INFO_PROCESSED:
438 		switch (chan->type) {
439 		case IIO_HUMIDITYRELATIVE:
440 			ret = data->chip_info->read_humid(data, val, val2);
441 			break;
442 		case IIO_PRESSURE:
443 			ret = data->chip_info->read_press(data, val, val2);
444 			break;
445 		case IIO_TEMP:
446 			ret = data->chip_info->read_temp(data, val);
447 			break;
448 		default:
449 			ret = -EINVAL;
450 			break;
451 		}
452 		break;
453 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
454 		switch (chan->type) {
455 		case IIO_HUMIDITYRELATIVE:
456 			*val = 1 << data->oversampling_humid;
457 			ret = IIO_VAL_INT;
458 			break;
459 		case IIO_PRESSURE:
460 			*val = 1 << data->oversampling_press;
461 			ret = IIO_VAL_INT;
462 			break;
463 		case IIO_TEMP:
464 			*val = 1 << data->oversampling_temp;
465 			ret = IIO_VAL_INT;
466 			break;
467 		default:
468 			ret = -EINVAL;
469 			break;
470 		}
471 		break;
472 	default:
473 		ret = -EINVAL;
474 		break;
475 	}
476 
477 	mutex_unlock(&data->lock);
478 	pm_runtime_mark_last_busy(data->dev);
479 	pm_runtime_put_autosuspend(data->dev);
480 
481 	return ret;
482 }
483 
484 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
485 					       int val)
486 {
487 	int i;
488 	const int *avail = data->chip_info->oversampling_humid_avail;
489 	const int n = data->chip_info->num_oversampling_humid_avail;
490 
491 	for (i = 0; i < n; i++) {
492 		if (avail[i] == val) {
493 			data->oversampling_humid = ilog2(val);
494 
495 			return data->chip_info->chip_config(data);
496 		}
497 	}
498 	return -EINVAL;
499 }
500 
501 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
502 					       int val)
503 {
504 	int i;
505 	const int *avail = data->chip_info->oversampling_temp_avail;
506 	const int n = data->chip_info->num_oversampling_temp_avail;
507 
508 	for (i = 0; i < n; i++) {
509 		if (avail[i] == val) {
510 			data->oversampling_temp = ilog2(val);
511 
512 			return data->chip_info->chip_config(data);
513 		}
514 	}
515 	return -EINVAL;
516 }
517 
518 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
519 					       int val)
520 {
521 	int i;
522 	const int *avail = data->chip_info->oversampling_press_avail;
523 	const int n = data->chip_info->num_oversampling_press_avail;
524 
525 	for (i = 0; i < n; i++) {
526 		if (avail[i] == val) {
527 			data->oversampling_press = ilog2(val);
528 
529 			return data->chip_info->chip_config(data);
530 		}
531 	}
532 	return -EINVAL;
533 }
534 
535 static int bmp280_write_raw(struct iio_dev *indio_dev,
536 			    struct iio_chan_spec const *chan,
537 			    int val, int val2, long mask)
538 {
539 	int ret = 0;
540 	struct bmp280_data *data = iio_priv(indio_dev);
541 
542 	switch (mask) {
543 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
544 		pm_runtime_get_sync(data->dev);
545 		mutex_lock(&data->lock);
546 		switch (chan->type) {
547 		case IIO_HUMIDITYRELATIVE:
548 			ret = bmp280_write_oversampling_ratio_humid(data, val);
549 			break;
550 		case IIO_PRESSURE:
551 			ret = bmp280_write_oversampling_ratio_press(data, val);
552 			break;
553 		case IIO_TEMP:
554 			ret = bmp280_write_oversampling_ratio_temp(data, val);
555 			break;
556 		default:
557 			ret = -EINVAL;
558 			break;
559 		}
560 		mutex_unlock(&data->lock);
561 		pm_runtime_mark_last_busy(data->dev);
562 		pm_runtime_put_autosuspend(data->dev);
563 		break;
564 	default:
565 		return -EINVAL;
566 	}
567 
568 	return ret;
569 }
570 
571 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
572 {
573 	size_t len = 0;
574 	int i;
575 
576 	for (i = 0; i < n; i++)
577 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
578 
579 	buf[len - 1] = '\n';
580 
581 	return len;
582 }
583 
584 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
585 				struct device_attribute *attr, char *buf)
586 {
587 	struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
588 
589 	return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
590 				 data->chip_info->num_oversampling_temp_avail);
591 }
592 
593 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
594 				struct device_attribute *attr, char *buf)
595 {
596 	struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
597 
598 	return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
599 				 data->chip_info->num_oversampling_press_avail);
600 }
601 
602 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
603 	S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
604 
605 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
606 	S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
607 
608 static struct attribute *bmp280_attributes[] = {
609 	&iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
610 	&iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
611 	NULL,
612 };
613 
614 static const struct attribute_group bmp280_attrs_group = {
615 	.attrs = bmp280_attributes,
616 };
617 
618 static const struct iio_info bmp280_info = {
619 	.read_raw = &bmp280_read_raw,
620 	.write_raw = &bmp280_write_raw,
621 	.attrs = &bmp280_attrs_group,
622 };
623 
624 static int bmp280_chip_config(struct bmp280_data *data)
625 {
626 	int ret;
627 	u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
628 		  BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
629 
630 	ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
631 				 BMP280_OSRS_TEMP_MASK |
632 				 BMP280_OSRS_PRESS_MASK |
633 				 BMP280_MODE_MASK,
634 				 osrs | BMP280_MODE_NORMAL);
635 	if (ret < 0) {
636 		dev_err(data->dev,
637 			"failed to write ctrl_meas register\n");
638 		return ret;
639 	}
640 
641 	ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
642 				 BMP280_FILTER_MASK,
643 				 BMP280_FILTER_4X);
644 	if (ret < 0) {
645 		dev_err(data->dev,
646 			"failed to write config register\n");
647 		return ret;
648 	}
649 
650 	return ret;
651 }
652 
653 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
654 
655 static const struct bmp280_chip_info bmp280_chip_info = {
656 	.oversampling_temp_avail = bmp280_oversampling_avail,
657 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
658 
659 	.oversampling_press_avail = bmp280_oversampling_avail,
660 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
661 
662 	.chip_config = bmp280_chip_config,
663 	.read_temp = bmp280_read_temp,
664 	.read_press = bmp280_read_press,
665 };
666 
667 static int bme280_chip_config(struct bmp280_data *data)
668 {
669 	int ret;
670 	u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
671 
672 	/*
673 	 * Oversampling of humidity must be set before oversampling of
674 	 * temperature/pressure is set to become effective.
675 	 */
676 	ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
677 				  BMP280_OSRS_HUMIDITY_MASK, osrs);
678 
679 	if (ret < 0)
680 		return ret;
681 
682 	return bmp280_chip_config(data);
683 }
684 
685 static const struct bmp280_chip_info bme280_chip_info = {
686 	.oversampling_temp_avail = bmp280_oversampling_avail,
687 	.num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
688 
689 	.oversampling_press_avail = bmp280_oversampling_avail,
690 	.num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
691 
692 	.oversampling_humid_avail = bmp280_oversampling_avail,
693 	.num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
694 
695 	.chip_config = bme280_chip_config,
696 	.read_temp = bmp280_read_temp,
697 	.read_press = bmp280_read_press,
698 	.read_humid = bmp280_read_humid,
699 };
700 
701 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
702 {
703 	int ret;
704 	const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
705 	unsigned int delay_us;
706 	unsigned int ctrl;
707 
708 	if (data->use_eoc)
709 		init_completion(&data->done);
710 
711 	ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
712 	if (ret)
713 		return ret;
714 
715 	if (data->use_eoc) {
716 		/*
717 		 * If we have a completion interrupt, use it, wait up to
718 		 * 100ms. The longest conversion time listed is 76.5 ms for
719 		 * advanced resolution mode.
720 		 */
721 		ret = wait_for_completion_timeout(&data->done,
722 						  1 + msecs_to_jiffies(100));
723 		if (!ret)
724 			dev_err(data->dev, "timeout waiting for completion\n");
725 	} else {
726 		if (ctrl_meas == BMP180_MEAS_TEMP)
727 			delay_us = 4500;
728 		else
729 			delay_us =
730 				conversion_time_max[data->oversampling_press];
731 
732 		usleep_range(delay_us, delay_us + 1000);
733 	}
734 
735 	ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
736 	if (ret)
737 		return ret;
738 
739 	/* The value of this bit reset to "0" after conversion is complete */
740 	if (ctrl & BMP180_MEAS_SCO)
741 		return -EIO;
742 
743 	return 0;
744 }
745 
746 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
747 {
748 	int ret;
749 	__be16 tmp = 0;
750 
751 	ret = bmp180_measure(data, BMP180_MEAS_TEMP);
752 	if (ret)
753 		return ret;
754 
755 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
756 	if (ret)
757 		return ret;
758 
759 	*val = be16_to_cpu(tmp);
760 
761 	return 0;
762 }
763 
764 static int bmp180_read_calib(struct bmp280_data *data,
765 			     struct bmp180_calib *calib)
766 {
767 	int ret;
768 	int i;
769 	__be16 buf[BMP180_REG_CALIB_COUNT / 2];
770 
771 	ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
772 			       sizeof(buf));
773 
774 	if (ret < 0)
775 		return ret;
776 
777 	/* None of the words has the value 0 or 0xFFFF */
778 	for (i = 0; i < ARRAY_SIZE(buf); i++) {
779 		if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
780 			return -EIO;
781 	}
782 
783 	/* Toss the calibration data into the entropy pool */
784 	add_device_randomness(buf, sizeof(buf));
785 
786 	calib->AC1 = be16_to_cpu(buf[AC1]);
787 	calib->AC2 = be16_to_cpu(buf[AC2]);
788 	calib->AC3 = be16_to_cpu(buf[AC3]);
789 	calib->AC4 = be16_to_cpu(buf[AC4]);
790 	calib->AC5 = be16_to_cpu(buf[AC5]);
791 	calib->AC6 = be16_to_cpu(buf[AC6]);
792 	calib->B1 = be16_to_cpu(buf[B1]);
793 	calib->B2 = be16_to_cpu(buf[B2]);
794 	calib->MB = be16_to_cpu(buf[MB]);
795 	calib->MC = be16_to_cpu(buf[MC]);
796 	calib->MD = be16_to_cpu(buf[MD]);
797 
798 	return 0;
799 }
800 
801 /*
802  * Returns temperature in DegC, resolution is 0.1 DegC.
803  * t_fine carries fine temperature as global value.
804  *
805  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
806  */
807 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
808 {
809 	s32 x1, x2;
810 	struct bmp180_calib *calib = &data->calib.bmp180;
811 
812 	x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
813 	x2 = (calib->MC << 11) / (x1 + calib->MD);
814 	data->t_fine = x1 + x2;
815 
816 	return (data->t_fine + 8) >> 4;
817 }
818 
819 static int bmp180_read_temp(struct bmp280_data *data, int *val)
820 {
821 	int ret;
822 	s32 adc_temp, comp_temp;
823 
824 	ret = bmp180_read_adc_temp(data, &adc_temp);
825 	if (ret)
826 		return ret;
827 
828 	comp_temp = bmp180_compensate_temp(data, adc_temp);
829 
830 	/*
831 	 * val might be NULL if we're called by the read_press routine,
832 	 * who only cares about the carry over t_fine value.
833 	 */
834 	if (val) {
835 		*val = comp_temp * 100;
836 		return IIO_VAL_INT;
837 	}
838 
839 	return 0;
840 }
841 
842 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
843 {
844 	int ret;
845 	__be32 tmp = 0;
846 	u8 oss = data->oversampling_press;
847 
848 	ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
849 	if (ret)
850 		return ret;
851 
852 	ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
853 	if (ret)
854 		return ret;
855 
856 	*val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
857 
858 	return 0;
859 }
860 
861 /*
862  * Returns pressure in Pa, resolution is 1 Pa.
863  *
864  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
865  */
866 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
867 {
868 	s32 x1, x2, x3, p;
869 	s32 b3, b6;
870 	u32 b4, b7;
871 	s32 oss = data->oversampling_press;
872 	struct bmp180_calib *calib = &data->calib.bmp180;
873 
874 	b6 = data->t_fine - 4000;
875 	x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
876 	x2 = calib->AC2 * b6 >> 11;
877 	x3 = x1 + x2;
878 	b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
879 	x1 = calib->AC3 * b6 >> 13;
880 	x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
881 	x3 = (x1 + x2 + 2) >> 2;
882 	b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
883 	b7 = ((u32)adc_press - b3) * (50000 >> oss);
884 	if (b7 < 0x80000000)
885 		p = (b7 * 2) / b4;
886 	else
887 		p = (b7 / b4) * 2;
888 
889 	x1 = (p >> 8) * (p >> 8);
890 	x1 = (x1 * 3038) >> 16;
891 	x2 = (-7357 * p) >> 16;
892 
893 	return p + ((x1 + x2 + 3791) >> 4);
894 }
895 
896 static int bmp180_read_press(struct bmp280_data *data,
897 			     int *val, int *val2)
898 {
899 	int ret;
900 	s32 adc_press;
901 	u32 comp_press;
902 
903 	/* Read and compensate temperature so we get a reading of t_fine. */
904 	ret = bmp180_read_temp(data, NULL);
905 	if (ret)
906 		return ret;
907 
908 	ret = bmp180_read_adc_press(data, &adc_press);
909 	if (ret)
910 		return ret;
911 
912 	comp_press = bmp180_compensate_press(data, adc_press);
913 
914 	*val = comp_press;
915 	*val2 = 1000;
916 
917 	return IIO_VAL_FRACTIONAL;
918 }
919 
920 static int bmp180_chip_config(struct bmp280_data *data)
921 {
922 	return 0;
923 }
924 
925 static const int bmp180_oversampling_temp_avail[] = { 1 };
926 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
927 
928 static const struct bmp280_chip_info bmp180_chip_info = {
929 	.oversampling_temp_avail = bmp180_oversampling_temp_avail,
930 	.num_oversampling_temp_avail =
931 		ARRAY_SIZE(bmp180_oversampling_temp_avail),
932 
933 	.oversampling_press_avail = bmp180_oversampling_press_avail,
934 	.num_oversampling_press_avail =
935 		ARRAY_SIZE(bmp180_oversampling_press_avail),
936 
937 	.chip_config = bmp180_chip_config,
938 	.read_temp = bmp180_read_temp,
939 	.read_press = bmp180_read_press,
940 };
941 
942 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
943 {
944 	struct bmp280_data *data = d;
945 
946 	complete(&data->done);
947 
948 	return IRQ_HANDLED;
949 }
950 
951 static int bmp085_fetch_eoc_irq(struct device *dev,
952 				const char *name,
953 				int irq,
954 				struct bmp280_data *data)
955 {
956 	unsigned long irq_trig;
957 	int ret;
958 
959 	irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
960 	if (irq_trig != IRQF_TRIGGER_RISING) {
961 		dev_err(dev, "non-rising trigger given for EOC interrupt, "
962 			"trying to enforce it\n");
963 		irq_trig = IRQF_TRIGGER_RISING;
964 	}
965 	ret = devm_request_threaded_irq(dev,
966 			irq,
967 			bmp085_eoc_irq,
968 			NULL,
969 			irq_trig,
970 			name,
971 			data);
972 	if (ret) {
973 		/* Bail out without IRQ but keep the driver in place */
974 		dev_err(dev, "unable to request DRDY IRQ\n");
975 		return 0;
976 	}
977 
978 	data->use_eoc = true;
979 	return 0;
980 }
981 
982 int bmp280_common_probe(struct device *dev,
983 			struct regmap *regmap,
984 			unsigned int chip,
985 			const char *name,
986 			int irq)
987 {
988 	int ret;
989 	struct iio_dev *indio_dev;
990 	struct bmp280_data *data;
991 	unsigned int chip_id;
992 	struct gpio_desc *gpiod;
993 
994 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
995 	if (!indio_dev)
996 		return -ENOMEM;
997 
998 	data = iio_priv(indio_dev);
999 	mutex_init(&data->lock);
1000 	data->dev = dev;
1001 
1002 	indio_dev->dev.parent = dev;
1003 	indio_dev->name = name;
1004 	indio_dev->channels = bmp280_channels;
1005 	indio_dev->info = &bmp280_info;
1006 	indio_dev->modes = INDIO_DIRECT_MODE;
1007 
1008 	switch (chip) {
1009 	case BMP180_CHIP_ID:
1010 		indio_dev->num_channels = 2;
1011 		data->chip_info = &bmp180_chip_info;
1012 		data->oversampling_press = ilog2(8);
1013 		data->oversampling_temp = ilog2(1);
1014 		data->start_up_time = 10000;
1015 		break;
1016 	case BMP280_CHIP_ID:
1017 		indio_dev->num_channels = 2;
1018 		data->chip_info = &bmp280_chip_info;
1019 		data->oversampling_press = ilog2(16);
1020 		data->oversampling_temp = ilog2(2);
1021 		data->start_up_time = 2000;
1022 		break;
1023 	case BME280_CHIP_ID:
1024 		indio_dev->num_channels = 3;
1025 		data->chip_info = &bme280_chip_info;
1026 		data->oversampling_press = ilog2(16);
1027 		data->oversampling_humid = ilog2(16);
1028 		data->oversampling_temp = ilog2(2);
1029 		data->start_up_time = 2000;
1030 		break;
1031 	default:
1032 		return -EINVAL;
1033 	}
1034 
1035 	/* Bring up regulators */
1036 	data->vddd = devm_regulator_get(dev, "vddd");
1037 	if (IS_ERR(data->vddd)) {
1038 		dev_err(dev, "failed to get VDDD regulator\n");
1039 		return PTR_ERR(data->vddd);
1040 	}
1041 	ret = regulator_enable(data->vddd);
1042 	if (ret) {
1043 		dev_err(dev, "failed to enable VDDD regulator\n");
1044 		return ret;
1045 	}
1046 	data->vdda = devm_regulator_get(dev, "vdda");
1047 	if (IS_ERR(data->vdda)) {
1048 		dev_err(dev, "failed to get VDDA regulator\n");
1049 		ret = PTR_ERR(data->vdda);
1050 		goto out_disable_vddd;
1051 	}
1052 	ret = regulator_enable(data->vdda);
1053 	if (ret) {
1054 		dev_err(dev, "failed to enable VDDA regulator\n");
1055 		goto out_disable_vddd;
1056 	}
1057 	/* Wait to make sure we started up properly */
1058 	usleep_range(data->start_up_time, data->start_up_time + 100);
1059 
1060 	/* Bring chip out of reset if there is an assigned GPIO line */
1061 	gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1062 	/* Deassert the signal */
1063 	if (!IS_ERR(gpiod)) {
1064 		dev_info(dev, "release reset\n");
1065 		gpiod_set_value(gpiod, 0);
1066 	}
1067 
1068 	data->regmap = regmap;
1069 	ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1070 	if (ret < 0)
1071 		goto out_disable_vdda;
1072 	if (chip_id != chip) {
1073 		dev_err(dev, "bad chip id: expected %x got %x\n",
1074 			chip, chip_id);
1075 		ret = -EINVAL;
1076 		goto out_disable_vdda;
1077 	}
1078 
1079 	ret = data->chip_info->chip_config(data);
1080 	if (ret < 0)
1081 		goto out_disable_vdda;
1082 
1083 	dev_set_drvdata(dev, indio_dev);
1084 
1085 	/*
1086 	 * Some chips have calibration parameters "programmed into the devices'
1087 	 * non-volatile memory during production". Let's read them out at probe
1088 	 * time once. They will not change.
1089 	 */
1090 	if (chip_id  == BMP180_CHIP_ID) {
1091 		ret = bmp180_read_calib(data, &data->calib.bmp180);
1092 		if (ret < 0) {
1093 			dev_err(data->dev,
1094 				"failed to read calibration coefficients\n");
1095 			goto out_disable_vdda;
1096 		}
1097 	} else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1098 		ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1099 		if (ret < 0) {
1100 			dev_err(data->dev,
1101 				"failed to read calibration coefficients\n");
1102 			goto out_disable_vdda;
1103 		}
1104 	}
1105 
1106 	/*
1107 	 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1108 	 * however as it happens, the BMP085 shares the chip ID of BMP180
1109 	 * so we look for an IRQ if we have that.
1110 	 */
1111 	if (irq > 0 || (chip_id  == BMP180_CHIP_ID)) {
1112 		ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1113 		if (ret)
1114 			goto out_disable_vdda;
1115 	}
1116 
1117 	/* Enable runtime PM */
1118 	pm_runtime_get_noresume(dev);
1119 	pm_runtime_set_active(dev);
1120 	pm_runtime_enable(dev);
1121 	/*
1122 	 * Set autosuspend to two orders of magnitude larger than the
1123 	 * start-up time.
1124 	 */
1125 	pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1126 	pm_runtime_use_autosuspend(dev);
1127 	pm_runtime_put(dev);
1128 
1129 	ret = iio_device_register(indio_dev);
1130 	if (ret)
1131 		goto out_runtime_pm_disable;
1132 
1133 
1134 	return 0;
1135 
1136 out_runtime_pm_disable:
1137 	pm_runtime_get_sync(data->dev);
1138 	pm_runtime_put_noidle(data->dev);
1139 	pm_runtime_disable(data->dev);
1140 out_disable_vdda:
1141 	regulator_disable(data->vdda);
1142 out_disable_vddd:
1143 	regulator_disable(data->vddd);
1144 	return ret;
1145 }
1146 EXPORT_SYMBOL(bmp280_common_probe);
1147 
1148 int bmp280_common_remove(struct device *dev)
1149 {
1150 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1151 	struct bmp280_data *data = iio_priv(indio_dev);
1152 
1153 	iio_device_unregister(indio_dev);
1154 	pm_runtime_get_sync(data->dev);
1155 	pm_runtime_put_noidle(data->dev);
1156 	pm_runtime_disable(data->dev);
1157 	regulator_disable(data->vdda);
1158 	regulator_disable(data->vddd);
1159 	return 0;
1160 }
1161 EXPORT_SYMBOL(bmp280_common_remove);
1162 
1163 #ifdef CONFIG_PM
1164 static int bmp280_runtime_suspend(struct device *dev)
1165 {
1166 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1167 	struct bmp280_data *data = iio_priv(indio_dev);
1168 	int ret;
1169 
1170 	ret = regulator_disable(data->vdda);
1171 	if (ret)
1172 		return ret;
1173 	return regulator_disable(data->vddd);
1174 }
1175 
1176 static int bmp280_runtime_resume(struct device *dev)
1177 {
1178 	struct iio_dev *indio_dev = dev_get_drvdata(dev);
1179 	struct bmp280_data *data = iio_priv(indio_dev);
1180 	int ret;
1181 
1182 	ret = regulator_enable(data->vddd);
1183 	if (ret)
1184 		return ret;
1185 	ret = regulator_enable(data->vdda);
1186 	if (ret)
1187 		return ret;
1188 	usleep_range(data->start_up_time, data->start_up_time + 100);
1189 	return data->chip_info->chip_config(data);
1190 }
1191 #endif /* CONFIG_PM */
1192 
1193 const struct dev_pm_ops bmp280_dev_pm_ops = {
1194 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1195 				pm_runtime_force_resume)
1196 	SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1197 			   bmp280_runtime_resume, NULL)
1198 };
1199 EXPORT_SYMBOL(bmp280_dev_pm_ops);
1200 
1201 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1202 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1203 MODULE_LICENSE("GPL v2");
1204