dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / pressure / bmp280-core.c
blob973264a088f9811b5bcfcd0e330ef9f8405f1ecd
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>
9 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
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
17 #define pr_fmt(fmt) "bmp280: " fmt
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>
33 #include "bmp280.h"
36 * These enums are used for indexing into the array of calibration
37 * coefficients for BMP180.
39 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
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;
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;
77 static const char *const bmp280_supply_names[] = {
78 "vddd", "vdda"
81 #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names)
83 struct bmp280_data {
84 struct device *dev;
85 struct mutex lock;
86 struct regmap *regmap;
87 struct completion done;
88 bool use_eoc;
89 const struct bmp280_chip_info *chip_info;
90 union {
91 struct bmp180_calib bmp180;
92 struct bmp280_calib bmp280;
93 } calib;
94 struct regulator_bulk_data supplies[BMP280_NUM_SUPPLIES];
95 unsigned int start_up_time; /* in microseconds */
97 /* log of base 2 of oversampling rate */
98 u8 oversampling_press;
99 u8 oversampling_temp;
100 u8 oversampling_humid;
103 * Carryover value from temperature conversion, used in pressure
104 * calculation.
106 s32 t_fine;
109 struct bmp280_chip_info {
110 const int *oversampling_temp_avail;
111 int num_oversampling_temp_avail;
113 const int *oversampling_press_avail;
114 int num_oversampling_press_avail;
116 const int *oversampling_humid_avail;
117 int num_oversampling_humid_avail;
119 int (*chip_config)(struct bmp280_data *);
120 int (*read_temp)(struct bmp280_data *, int *);
121 int (*read_press)(struct bmp280_data *, int *, int *);
122 int (*read_humid)(struct bmp280_data *, int *, int *);
126 * These enums are used for indexing into the array of compensation
127 * parameters for BMP280.
129 enum { T1, T2, T3 };
130 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
132 static const struct iio_chan_spec bmp280_channels[] = {
134 .type = IIO_PRESSURE,
135 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
136 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
139 .type = IIO_TEMP,
140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
141 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
144 .type = IIO_HUMIDITYRELATIVE,
145 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
146 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
150 static int bmp280_read_calib(struct bmp280_data *data,
151 struct bmp280_calib *calib,
152 unsigned int chip)
154 int ret;
155 unsigned int tmp;
156 __le16 l16;
157 __be16 b16;
158 struct device *dev = data->dev;
159 __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
160 __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];
162 /* Read temperature calibration values. */
163 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
164 t_buf, BMP280_COMP_TEMP_REG_COUNT);
165 if (ret < 0) {
166 dev_err(data->dev,
167 "failed to read temperature calibration parameters\n");
168 return ret;
171 /* Toss the temperature calibration data into the entropy pool */
172 add_device_randomness(t_buf, sizeof(t_buf));
174 calib->T1 = le16_to_cpu(t_buf[T1]);
175 calib->T2 = le16_to_cpu(t_buf[T2]);
176 calib->T3 = le16_to_cpu(t_buf[T3]);
178 /* Read pressure calibration values. */
179 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
180 p_buf, BMP280_COMP_PRESS_REG_COUNT);
181 if (ret < 0) {
182 dev_err(data->dev,
183 "failed to read pressure calibration parameters\n");
184 return ret;
187 /* Toss the pressure calibration data into the entropy pool */
188 add_device_randomness(p_buf, sizeof(p_buf));
190 calib->P1 = le16_to_cpu(p_buf[P1]);
191 calib->P2 = le16_to_cpu(p_buf[P2]);
192 calib->P3 = le16_to_cpu(p_buf[P3]);
193 calib->P4 = le16_to_cpu(p_buf[P4]);
194 calib->P5 = le16_to_cpu(p_buf[P5]);
195 calib->P6 = le16_to_cpu(p_buf[P6]);
196 calib->P7 = le16_to_cpu(p_buf[P7]);
197 calib->P8 = le16_to_cpu(p_buf[P8]);
198 calib->P9 = le16_to_cpu(p_buf[P9]);
201 * Read humidity calibration values.
202 * Due to some odd register addressing we cannot just
203 * do a big bulk read. Instead, we have to read each Hx
204 * value separately and sometimes do some bit shifting...
205 * Humidity data is only available on BME280.
207 if (chip != BME280_CHIP_ID)
208 return 0;
210 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
211 if (ret < 0) {
212 dev_err(dev, "failed to read H1 comp value\n");
213 return ret;
215 calib->H1 = tmp;
217 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &l16, 2);
218 if (ret < 0) {
219 dev_err(dev, "failed to read H2 comp value\n");
220 return ret;
222 calib->H2 = sign_extend32(le16_to_cpu(l16), 15);
224 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
225 if (ret < 0) {
226 dev_err(dev, "failed to read H3 comp value\n");
227 return ret;
229 calib->H3 = tmp;
231 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &b16, 2);
232 if (ret < 0) {
233 dev_err(dev, "failed to read H4 comp value\n");
234 return ret;
236 calib->H4 = sign_extend32(((be16_to_cpu(b16) >> 4) & 0xff0) |
237 (be16_to_cpu(b16) & 0xf), 11);
239 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &l16, 2);
240 if (ret < 0) {
241 dev_err(dev, "failed to read H5 comp value\n");
242 return ret;
244 calib->H5 = sign_extend32(((le16_to_cpu(l16) >> 4) & 0xfff), 11);
246 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
247 if (ret < 0) {
248 dev_err(dev, "failed to read H6 comp value\n");
249 return ret;
251 calib->H6 = sign_extend32(tmp, 7);
253 return 0;
256 * Returns humidity in percent, resolution is 0.01 percent. Output value of
257 * "47445" represents 47445/1024 = 46.333 %RH.
259 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
261 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
262 s32 adc_humidity)
264 s32 var;
265 struct bmp280_calib *calib = &data->calib.bmp280;
267 var = ((s32)data->t_fine) - (s32)76800;
268 var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
269 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
270 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
271 + (s32)2097152) * calib->H2 + 8192) >> 14);
272 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
274 var = clamp_val(var, 0, 419430400);
276 return var >> 12;
280 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
281 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
282 * value.
284 * Taken from datasheet, Section 3.11.3, "Compensation formula".
286 static s32 bmp280_compensate_temp(struct bmp280_data *data,
287 s32 adc_temp)
289 s32 var1, var2;
290 struct bmp280_calib *calib = &data->calib.bmp280;
292 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
293 ((s32)calib->T2)) >> 11;
294 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
295 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
296 ((s32)calib->T3)) >> 14;
297 data->t_fine = var1 + var2;
299 return (data->t_fine * 5 + 128) >> 8;
303 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
304 * integer bits and 8 fractional bits). Output value of "24674867"
305 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
307 * Taken from datasheet, Section 3.11.3, "Compensation formula".
309 static u32 bmp280_compensate_press(struct bmp280_data *data,
310 s32 adc_press)
312 s64 var1, var2, p;
313 struct bmp280_calib *calib = &data->calib.bmp280;
315 var1 = ((s64)data->t_fine) - 128000;
316 var2 = var1 * var1 * (s64)calib->P6;
317 var2 += (var1 * (s64)calib->P5) << 17;
318 var2 += ((s64)calib->P4) << 35;
319 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
320 ((var1 * (s64)calib->P2) << 12);
321 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
323 if (var1 == 0)
324 return 0;
326 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
327 p = div64_s64(p, var1);
328 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
329 var2 = ((s64)(calib->P8) * p) >> 19;
330 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
332 return (u32)p;
335 static int bmp280_read_temp(struct bmp280_data *data,
336 int *val)
338 int ret;
339 __be32 tmp = 0;
340 s32 adc_temp, comp_temp;
342 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
343 (u8 *) &tmp, 3);
344 if (ret < 0) {
345 dev_err(data->dev, "failed to read temperature\n");
346 return ret;
349 adc_temp = be32_to_cpu(tmp) >> 12;
350 if (adc_temp == BMP280_TEMP_SKIPPED) {
351 /* reading was skipped */
352 dev_err(data->dev, "reading temperature skipped\n");
353 return -EIO;
355 comp_temp = bmp280_compensate_temp(data, adc_temp);
358 * val might be NULL if we're called by the read_press routine,
359 * who only cares about the carry over t_fine value.
361 if (val) {
362 *val = comp_temp * 10;
363 return IIO_VAL_INT;
366 return 0;
369 static int bmp280_read_press(struct bmp280_data *data,
370 int *val, int *val2)
372 int ret;
373 __be32 tmp = 0;
374 s32 adc_press;
375 u32 comp_press;
377 /* Read and compensate temperature so we get a reading of t_fine. */
378 ret = bmp280_read_temp(data, NULL);
379 if (ret < 0)
380 return ret;
382 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
383 (u8 *) &tmp, 3);
384 if (ret < 0) {
385 dev_err(data->dev, "failed to read pressure\n");
386 return ret;
389 adc_press = be32_to_cpu(tmp) >> 12;
390 if (adc_press == BMP280_PRESS_SKIPPED) {
391 /* reading was skipped */
392 dev_err(data->dev, "reading pressure skipped\n");
393 return -EIO;
395 comp_press = bmp280_compensate_press(data, adc_press);
397 *val = comp_press;
398 *val2 = 256000;
400 return IIO_VAL_FRACTIONAL;
403 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
405 int ret;
406 __be16 tmp = 0;
407 s32 adc_humidity;
408 u32 comp_humidity;
410 /* Read and compensate temperature so we get a reading of t_fine. */
411 ret = bmp280_read_temp(data, NULL);
412 if (ret < 0)
413 return ret;
415 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
416 (u8 *) &tmp, 2);
417 if (ret < 0) {
418 dev_err(data->dev, "failed to read humidity\n");
419 return ret;
422 adc_humidity = be16_to_cpu(tmp);
423 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
424 /* reading was skipped */
425 dev_err(data->dev, "reading humidity skipped\n");
426 return -EIO;
428 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
430 *val = comp_humidity * 1000 / 1024;
432 return IIO_VAL_INT;
435 static int bmp280_read_raw(struct iio_dev *indio_dev,
436 struct iio_chan_spec const *chan,
437 int *val, int *val2, long mask)
439 int ret;
440 struct bmp280_data *data = iio_priv(indio_dev);
442 pm_runtime_get_sync(data->dev);
443 mutex_lock(&data->lock);
445 switch (mask) {
446 case IIO_CHAN_INFO_PROCESSED:
447 switch (chan->type) {
448 case IIO_HUMIDITYRELATIVE:
449 ret = data->chip_info->read_humid(data, val, val2);
450 break;
451 case IIO_PRESSURE:
452 ret = data->chip_info->read_press(data, val, val2);
453 break;
454 case IIO_TEMP:
455 ret = data->chip_info->read_temp(data, val);
456 break;
457 default:
458 ret = -EINVAL;
459 break;
461 break;
462 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
463 switch (chan->type) {
464 case IIO_HUMIDITYRELATIVE:
465 *val = 1 << data->oversampling_humid;
466 ret = IIO_VAL_INT;
467 break;
468 case IIO_PRESSURE:
469 *val = 1 << data->oversampling_press;
470 ret = IIO_VAL_INT;
471 break;
472 case IIO_TEMP:
473 *val = 1 << data->oversampling_temp;
474 ret = IIO_VAL_INT;
475 break;
476 default:
477 ret = -EINVAL;
478 break;
480 break;
481 default:
482 ret = -EINVAL;
483 break;
486 mutex_unlock(&data->lock);
487 pm_runtime_mark_last_busy(data->dev);
488 pm_runtime_put_autosuspend(data->dev);
490 return ret;
493 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
494 int val)
496 int i;
497 const int *avail = data->chip_info->oversampling_humid_avail;
498 const int n = data->chip_info->num_oversampling_humid_avail;
500 for (i = 0; i < n; i++) {
501 if (avail[i] == val) {
502 data->oversampling_humid = ilog2(val);
504 return data->chip_info->chip_config(data);
507 return -EINVAL;
510 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
511 int val)
513 int i;
514 const int *avail = data->chip_info->oversampling_temp_avail;
515 const int n = data->chip_info->num_oversampling_temp_avail;
517 for (i = 0; i < n; i++) {
518 if (avail[i] == val) {
519 data->oversampling_temp = ilog2(val);
521 return data->chip_info->chip_config(data);
524 return -EINVAL;
527 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
528 int val)
530 int i;
531 const int *avail = data->chip_info->oversampling_press_avail;
532 const int n = data->chip_info->num_oversampling_press_avail;
534 for (i = 0; i < n; i++) {
535 if (avail[i] == val) {
536 data->oversampling_press = ilog2(val);
538 return data->chip_info->chip_config(data);
541 return -EINVAL;
544 static int bmp280_write_raw(struct iio_dev *indio_dev,
545 struct iio_chan_spec const *chan,
546 int val, int val2, long mask)
548 int ret = 0;
549 struct bmp280_data *data = iio_priv(indio_dev);
551 switch (mask) {
552 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
553 pm_runtime_get_sync(data->dev);
554 mutex_lock(&data->lock);
555 switch (chan->type) {
556 case IIO_HUMIDITYRELATIVE:
557 ret = bmp280_write_oversampling_ratio_humid(data, val);
558 break;
559 case IIO_PRESSURE:
560 ret = bmp280_write_oversampling_ratio_press(data, val);
561 break;
562 case IIO_TEMP:
563 ret = bmp280_write_oversampling_ratio_temp(data, val);
564 break;
565 default:
566 ret = -EINVAL;
567 break;
569 mutex_unlock(&data->lock);
570 pm_runtime_mark_last_busy(data->dev);
571 pm_runtime_put_autosuspend(data->dev);
572 break;
573 default:
574 return -EINVAL;
577 return ret;
580 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
582 size_t len = 0;
583 int i;
585 for (i = 0; i < n; i++)
586 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
588 buf[len - 1] = '\n';
590 return len;
593 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
594 struct device_attribute *attr, char *buf)
596 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
598 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
599 data->chip_info->num_oversampling_temp_avail);
602 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
603 struct device_attribute *attr, char *buf)
605 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
607 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
608 data->chip_info->num_oversampling_press_avail);
611 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
612 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
614 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
615 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
617 static struct attribute *bmp280_attributes[] = {
618 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
619 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
620 NULL,
623 static const struct attribute_group bmp280_attrs_group = {
624 .attrs = bmp280_attributes,
627 static const struct iio_info bmp280_info = {
628 .read_raw = &bmp280_read_raw,
629 .write_raw = &bmp280_write_raw,
630 .attrs = &bmp280_attrs_group,
633 static int bmp280_chip_config(struct bmp280_data *data)
635 int ret;
636 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
637 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
639 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
640 BMP280_OSRS_TEMP_MASK |
641 BMP280_OSRS_PRESS_MASK |
642 BMP280_MODE_MASK,
643 osrs | BMP280_MODE_NORMAL);
644 if (ret < 0) {
645 dev_err(data->dev,
646 "failed to write ctrl_meas register\n");
647 return ret;
650 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
651 BMP280_FILTER_MASK,
652 BMP280_FILTER_4X);
653 if (ret < 0) {
654 dev_err(data->dev,
655 "failed to write config register\n");
656 return ret;
659 return ret;
662 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
664 static const struct bmp280_chip_info bmp280_chip_info = {
665 .oversampling_temp_avail = bmp280_oversampling_avail,
666 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
668 .oversampling_press_avail = bmp280_oversampling_avail,
669 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
671 .chip_config = bmp280_chip_config,
672 .read_temp = bmp280_read_temp,
673 .read_press = bmp280_read_press,
676 static int bme280_chip_config(struct bmp280_data *data)
678 int ret;
679 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
682 * Oversampling of humidity must be set before oversampling of
683 * temperature/pressure is set to become effective.
685 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
686 BMP280_OSRS_HUMIDITY_MASK, osrs);
688 if (ret < 0)
689 return ret;
691 return bmp280_chip_config(data);
694 static const struct bmp280_chip_info bme280_chip_info = {
695 .oversampling_temp_avail = bmp280_oversampling_avail,
696 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
698 .oversampling_press_avail = bmp280_oversampling_avail,
699 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
701 .oversampling_humid_avail = bmp280_oversampling_avail,
702 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
704 .chip_config = bme280_chip_config,
705 .read_temp = bmp280_read_temp,
706 .read_press = bmp280_read_press,
707 .read_humid = bmp280_read_humid,
710 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
712 int ret;
713 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
714 unsigned int delay_us;
715 unsigned int ctrl;
717 if (data->use_eoc)
718 reinit_completion(&data->done);
720 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
721 if (ret)
722 return ret;
724 if (data->use_eoc) {
726 * If we have a completion interrupt, use it, wait up to
727 * 100ms. The longest conversion time listed is 76.5 ms for
728 * advanced resolution mode.
730 ret = wait_for_completion_timeout(&data->done,
731 1 + msecs_to_jiffies(100));
732 if (!ret)
733 dev_err(data->dev, "timeout waiting for completion\n");
734 } else {
735 if (ctrl_meas == BMP180_MEAS_TEMP)
736 delay_us = 4500;
737 else
738 delay_us =
739 conversion_time_max[data->oversampling_press];
741 usleep_range(delay_us, delay_us + 1000);
744 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
745 if (ret)
746 return ret;
748 /* The value of this bit reset to "0" after conversion is complete */
749 if (ctrl & BMP180_MEAS_SCO)
750 return -EIO;
752 return 0;
755 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
757 int ret;
758 __be16 tmp = 0;
760 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
761 if (ret)
762 return ret;
764 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
765 if (ret)
766 return ret;
768 *val = be16_to_cpu(tmp);
770 return 0;
773 static int bmp180_read_calib(struct bmp280_data *data,
774 struct bmp180_calib *calib)
776 int ret;
777 int i;
778 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
780 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
781 sizeof(buf));
783 if (ret < 0)
784 return ret;
786 /* None of the words has the value 0 or 0xFFFF */
787 for (i = 0; i < ARRAY_SIZE(buf); i++) {
788 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
789 return -EIO;
792 /* Toss the calibration data into the entropy pool */
793 add_device_randomness(buf, sizeof(buf));
795 calib->AC1 = be16_to_cpu(buf[AC1]);
796 calib->AC2 = be16_to_cpu(buf[AC2]);
797 calib->AC3 = be16_to_cpu(buf[AC3]);
798 calib->AC4 = be16_to_cpu(buf[AC4]);
799 calib->AC5 = be16_to_cpu(buf[AC5]);
800 calib->AC6 = be16_to_cpu(buf[AC6]);
801 calib->B1 = be16_to_cpu(buf[B1]);
802 calib->B2 = be16_to_cpu(buf[B2]);
803 calib->MB = be16_to_cpu(buf[MB]);
804 calib->MC = be16_to_cpu(buf[MC]);
805 calib->MD = be16_to_cpu(buf[MD]);
807 return 0;
811 * Returns temperature in DegC, resolution is 0.1 DegC.
812 * t_fine carries fine temperature as global value.
814 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
816 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
818 s32 x1, x2;
819 struct bmp180_calib *calib = &data->calib.bmp180;
821 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
822 x2 = (calib->MC << 11) / (x1 + calib->MD);
823 data->t_fine = x1 + x2;
825 return (data->t_fine + 8) >> 4;
828 static int bmp180_read_temp(struct bmp280_data *data, int *val)
830 int ret;
831 s32 adc_temp, comp_temp;
833 ret = bmp180_read_adc_temp(data, &adc_temp);
834 if (ret)
835 return ret;
837 comp_temp = bmp180_compensate_temp(data, adc_temp);
840 * val might be NULL if we're called by the read_press routine,
841 * who only cares about the carry over t_fine value.
843 if (val) {
844 *val = comp_temp * 100;
845 return IIO_VAL_INT;
848 return 0;
851 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
853 int ret;
854 __be32 tmp = 0;
855 u8 oss = data->oversampling_press;
857 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
858 if (ret)
859 return ret;
861 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
862 if (ret)
863 return ret;
865 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
867 return 0;
871 * Returns pressure in Pa, resolution is 1 Pa.
873 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
875 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
877 s32 x1, x2, x3, p;
878 s32 b3, b6;
879 u32 b4, b7;
880 s32 oss = data->oversampling_press;
881 struct bmp180_calib *calib = &data->calib.bmp180;
883 b6 = data->t_fine - 4000;
884 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
885 x2 = calib->AC2 * b6 >> 11;
886 x3 = x1 + x2;
887 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
888 x1 = calib->AC3 * b6 >> 13;
889 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
890 x3 = (x1 + x2 + 2) >> 2;
891 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
892 b7 = ((u32)adc_press - b3) * (50000 >> oss);
893 if (b7 < 0x80000000)
894 p = (b7 * 2) / b4;
895 else
896 p = (b7 / b4) * 2;
898 x1 = (p >> 8) * (p >> 8);
899 x1 = (x1 * 3038) >> 16;
900 x2 = (-7357 * p) >> 16;
902 return p + ((x1 + x2 + 3791) >> 4);
905 static int bmp180_read_press(struct bmp280_data *data,
906 int *val, int *val2)
908 int ret;
909 s32 adc_press;
910 u32 comp_press;
912 /* Read and compensate temperature so we get a reading of t_fine. */
913 ret = bmp180_read_temp(data, NULL);
914 if (ret)
915 return ret;
917 ret = bmp180_read_adc_press(data, &adc_press);
918 if (ret)
919 return ret;
921 comp_press = bmp180_compensate_press(data, adc_press);
923 *val = comp_press;
924 *val2 = 1000;
926 return IIO_VAL_FRACTIONAL;
929 static int bmp180_chip_config(struct bmp280_data *data)
931 return 0;
934 static const int bmp180_oversampling_temp_avail[] = { 1 };
935 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
937 static const struct bmp280_chip_info bmp180_chip_info = {
938 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
939 .num_oversampling_temp_avail =
940 ARRAY_SIZE(bmp180_oversampling_temp_avail),
942 .oversampling_press_avail = bmp180_oversampling_press_avail,
943 .num_oversampling_press_avail =
944 ARRAY_SIZE(bmp180_oversampling_press_avail),
946 .chip_config = bmp180_chip_config,
947 .read_temp = bmp180_read_temp,
948 .read_press = bmp180_read_press,
951 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
953 struct bmp280_data *data = d;
955 complete(&data->done);
957 return IRQ_HANDLED;
960 static int bmp085_fetch_eoc_irq(struct device *dev,
961 const char *name,
962 int irq,
963 struct bmp280_data *data)
965 unsigned long irq_trig;
966 int ret;
968 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
969 if (irq_trig != IRQF_TRIGGER_RISING) {
970 dev_err(dev, "non-rising trigger given for EOC interrupt, "
971 "trying to enforce it\n");
972 irq_trig = IRQF_TRIGGER_RISING;
975 init_completion(&data->done);
977 ret = devm_request_threaded_irq(dev,
978 irq,
979 bmp085_eoc_irq,
980 NULL,
981 irq_trig,
982 name,
983 data);
984 if (ret) {
985 /* Bail out without IRQ but keep the driver in place */
986 dev_err(dev, "unable to request DRDY IRQ\n");
987 return 0;
990 data->use_eoc = true;
991 return 0;
994 static void bmp280_pm_disable(void *data)
996 struct device *dev = data;
998 pm_runtime_get_sync(dev);
999 pm_runtime_put_noidle(dev);
1000 pm_runtime_disable(dev);
1003 static void bmp280_regulators_disable(void *data)
1005 struct regulator_bulk_data *supplies = data;
1007 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
1010 int bmp280_common_probe(struct device *dev,
1011 struct regmap *regmap,
1012 unsigned int chip,
1013 const char *name,
1014 int irq)
1016 int ret;
1017 struct iio_dev *indio_dev;
1018 struct bmp280_data *data;
1019 unsigned int chip_id;
1020 struct gpio_desc *gpiod;
1022 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1023 if (!indio_dev)
1024 return -ENOMEM;
1026 data = iio_priv(indio_dev);
1027 mutex_init(&data->lock);
1028 data->dev = dev;
1030 indio_dev->dev.parent = dev;
1031 indio_dev->name = name;
1032 indio_dev->channels = bmp280_channels;
1033 indio_dev->info = &bmp280_info;
1034 indio_dev->modes = INDIO_DIRECT_MODE;
1036 switch (chip) {
1037 case BMP180_CHIP_ID:
1038 indio_dev->num_channels = 2;
1039 data->chip_info = &bmp180_chip_info;
1040 data->oversampling_press = ilog2(8);
1041 data->oversampling_temp = ilog2(1);
1042 data->start_up_time = 10000;
1043 break;
1044 case BMP280_CHIP_ID:
1045 indio_dev->num_channels = 2;
1046 data->chip_info = &bmp280_chip_info;
1047 data->oversampling_press = ilog2(16);
1048 data->oversampling_temp = ilog2(2);
1049 data->start_up_time = 2000;
1050 break;
1051 case BME280_CHIP_ID:
1052 indio_dev->num_channels = 3;
1053 data->chip_info = &bme280_chip_info;
1054 data->oversampling_press = ilog2(16);
1055 data->oversampling_humid = ilog2(16);
1056 data->oversampling_temp = ilog2(2);
1057 data->start_up_time = 2000;
1058 break;
1059 default:
1060 return -EINVAL;
1063 /* Bring up regulators */
1064 regulator_bulk_set_supply_names(data->supplies,
1065 bmp280_supply_names,
1066 BMP280_NUM_SUPPLIES);
1068 ret = devm_regulator_bulk_get(dev,
1069 BMP280_NUM_SUPPLIES, data->supplies);
1070 if (ret) {
1071 dev_err(dev, "failed to get regulators\n");
1072 return ret;
1075 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1076 if (ret) {
1077 dev_err(dev, "failed to enable regulators\n");
1078 return ret;
1081 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
1082 data->supplies);
1083 if (ret)
1084 return ret;
1086 /* Wait to make sure we started up properly */
1087 usleep_range(data->start_up_time, data->start_up_time + 100);
1089 /* Bring chip out of reset if there is an assigned GPIO line */
1090 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1091 /* Deassert the signal */
1092 if (!IS_ERR(gpiod)) {
1093 dev_info(dev, "release reset\n");
1094 gpiod_set_value(gpiod, 0);
1097 data->regmap = regmap;
1098 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1099 if (ret < 0)
1100 return ret;
1101 if (chip_id != chip) {
1102 dev_err(dev, "bad chip id: expected %x got %x\n",
1103 chip, chip_id);
1104 return -EINVAL;
1107 ret = data->chip_info->chip_config(data);
1108 if (ret < 0)
1109 return ret;
1111 dev_set_drvdata(dev, indio_dev);
1114 * Some chips have calibration parameters "programmed into the devices'
1115 * non-volatile memory during production". Let's read them out at probe
1116 * time once. They will not change.
1118 if (chip_id == BMP180_CHIP_ID) {
1119 ret = bmp180_read_calib(data, &data->calib.bmp180);
1120 if (ret < 0) {
1121 dev_err(data->dev,
1122 "failed to read calibration coefficients\n");
1123 return ret;
1125 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1126 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1127 if (ret < 0) {
1128 dev_err(data->dev,
1129 "failed to read calibration coefficients\n");
1130 return ret;
1135 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1136 * however as it happens, the BMP085 shares the chip ID of BMP180
1137 * so we look for an IRQ if we have that.
1139 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1140 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1141 if (ret)
1142 return ret;
1145 /* Enable runtime PM */
1146 pm_runtime_get_noresume(dev);
1147 pm_runtime_set_active(dev);
1148 pm_runtime_enable(dev);
1150 * Set autosuspend to two orders of magnitude larger than the
1151 * start-up time.
1153 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1154 pm_runtime_use_autosuspend(dev);
1155 pm_runtime_put(dev);
1157 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
1158 if (ret)
1159 return ret;
1161 return devm_iio_device_register(dev, indio_dev);
1163 EXPORT_SYMBOL(bmp280_common_probe);
1165 #ifdef CONFIG_PM
1166 static int bmp280_runtime_suspend(struct device *dev)
1168 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1169 struct bmp280_data *data = iio_priv(indio_dev);
1171 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
1174 static int bmp280_runtime_resume(struct device *dev)
1176 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1177 struct bmp280_data *data = iio_priv(indio_dev);
1178 int ret;
1180 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1181 if (ret)
1182 return ret;
1183 usleep_range(data->start_up_time, data->start_up_time + 100);
1184 return data->chip_info->chip_config(data);
1186 #endif /* CONFIG_PM */
1188 const struct dev_pm_ops bmp280_dev_pm_ops = {
1189 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1190 pm_runtime_force_resume)
1191 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1192 bmp280_runtime_resume, NULL)
1194 EXPORT_SYMBOL(bmp280_dev_pm_ops);
1196 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1197 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1198 MODULE_LICENSE("GPL v2");