2 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
3 * Copyright (c) 2012 Bosch Sensortec GmbH
4 * Copyright (c) 2012 Unixphere AB
5 * Copyright (c) 2014 Intel Corporation
6 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
8 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
16 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
17 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
20 #define pr_fmt(fmt) "bmp280: " fmt
22 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <linux/delay.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/interrupt.h>
31 #include <linux/irq.h> /* For irq_get_irq_data() */
32 #include <linux/completion.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/random.h>
39 * These enums are used for indexing into the array of calibration
40 * coefficients for BMP180.
42 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
61 struct regmap *regmap;
62 struct completion done;
64 const struct bmp280_chip_info *chip_info;
65 struct bmp180_calib calib;
66 struct regulator *vddd;
67 struct regulator *vdda;
68 unsigned int start_up_time; /* in milliseconds */
70 /* log of base 2 of oversampling rate */
71 u8 oversampling_press;
73 u8 oversampling_humid;
76 * Carryover value from temperature conversion, used in pressure
82 struct bmp280_chip_info {
83 const int *oversampling_temp_avail;
84 int num_oversampling_temp_avail;
86 const int *oversampling_press_avail;
87 int num_oversampling_press_avail;
89 const int *oversampling_humid_avail;
90 int num_oversampling_humid_avail;
92 int (*chip_config)(struct bmp280_data *);
93 int (*read_temp)(struct bmp280_data *, int *);
94 int (*read_press)(struct bmp280_data *, int *, int *);
95 int (*read_humid)(struct bmp280_data *, int *, int *);
99 * These enums are used for indexing into the array of compensation
100 * parameters for BMP280.
103 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
105 static const struct iio_chan_spec bmp280_channels[] = {
107 .type = IIO_PRESSURE,
108 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
109 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
113 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
114 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
117 .type = IIO_HUMIDITYRELATIVE,
118 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
119 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
124 * Returns humidity in percent, resolution is 0.01 percent. Output value of
125 * "47445" represents 47445/1024 = 46.333 %RH.
127 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
130 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
133 struct device *dev = data->dev;
134 unsigned int H1, H3, tmp;
135 int H2, H4, H5, H6, ret, var;
137 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
139 dev_err(dev, "failed to read H1 comp value\n");
143 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
145 dev_err(dev, "failed to read H2 comp value\n");
148 H2 = sign_extend32(le16_to_cpu(tmp), 15);
150 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
152 dev_err(dev, "failed to read H3 comp value\n");
156 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
158 dev_err(dev, "failed to read H4 comp value\n");
161 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
162 (be16_to_cpu(tmp) & 0xf), 11);
164 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
166 dev_err(dev, "failed to read H5 comp value\n");
169 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
171 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
173 dev_err(dev, "failed to read H6 comp value\n");
176 H6 = sign_extend32(tmp, 7);
178 var = ((s32)data->t_fine) - (s32)76800;
179 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var))
180 + (s32)16384) >> 15) * (((((((var * H6) >> 10)
181 * (((var * (s32)H3) >> 11) + (s32)32768)) >> 10)
182 + (s32)2097152) * H2 + 8192) >> 14);
183 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)H1) >> 4;
185 var = clamp_val(var, 0, 419430400);
191 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
192 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
195 * Taken from datasheet, Section 3.11.3, "Compensation formula".
197 static s32 bmp280_compensate_temp(struct bmp280_data *data,
202 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
204 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
205 buf, BMP280_COMP_TEMP_REG_COUNT);
208 "failed to read temperature calibration parameters\n");
213 * The double casts are necessary because le16_to_cpu returns an
214 * unsigned 16-bit value. Casting that value directly to a
215 * signed 32-bit will not do proper sign extension.
217 * Conversely, T1 and P1 are unsigned values, so they can be
218 * cast straight to the larger type.
220 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
221 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
222 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
223 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
224 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
225 data->t_fine = var1 + var2;
227 return (data->t_fine * 5 + 128) >> 8;
231 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
232 * integer bits and 8 fractional bits). Output value of "24674867"
233 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
235 * Taken from datasheet, Section 3.11.3, "Compensation formula".
237 static u32 bmp280_compensate_press(struct bmp280_data *data,
242 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
244 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
245 buf, BMP280_COMP_PRESS_REG_COUNT);
248 "failed to read pressure calibration parameters\n");
252 var1 = ((s64)data->t_fine) - 128000;
253 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
254 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
255 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
256 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
257 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
258 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
263 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
264 p = div64_s64(p, var1);
265 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
266 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
267 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
272 static int bmp280_read_temp(struct bmp280_data *data,
277 s32 adc_temp, comp_temp;
279 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
282 dev_err(data->dev, "failed to read temperature\n");
286 adc_temp = be32_to_cpu(tmp) >> 12;
287 comp_temp = bmp280_compensate_temp(data, adc_temp);
290 * val might be NULL if we're called by the read_press routine,
291 * who only cares about the carry over t_fine value.
294 *val = comp_temp * 10;
301 static int bmp280_read_press(struct bmp280_data *data,
309 /* Read and compensate temperature so we get a reading of t_fine. */
310 ret = bmp280_read_temp(data, NULL);
314 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
317 dev_err(data->dev, "failed to read pressure\n");
321 adc_press = be32_to_cpu(tmp) >> 12;
322 comp_press = bmp280_compensate_press(data, adc_press);
327 return IIO_VAL_FRACTIONAL;
330 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
337 /* Read and compensate temperature so we get a reading of t_fine. */
338 ret = bmp280_read_temp(data, NULL);
342 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
345 dev_err(data->dev, "failed to read humidity\n");
349 adc_humidity = be16_to_cpu(tmp);
350 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
352 *val = comp_humidity * 1000 / 1024;
357 static int bmp280_read_raw(struct iio_dev *indio_dev,
358 struct iio_chan_spec const *chan,
359 int *val, int *val2, long mask)
362 struct bmp280_data *data = iio_priv(indio_dev);
364 pm_runtime_get_sync(data->dev);
365 mutex_lock(&data->lock);
368 case IIO_CHAN_INFO_PROCESSED:
369 switch (chan->type) {
370 case IIO_HUMIDITYRELATIVE:
371 ret = data->chip_info->read_humid(data, val, val2);
374 ret = data->chip_info->read_press(data, val, val2);
377 ret = data->chip_info->read_temp(data, val);
384 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
385 switch (chan->type) {
386 case IIO_HUMIDITYRELATIVE:
387 *val = 1 << data->oversampling_humid;
391 *val = 1 << data->oversampling_press;
395 *val = 1 << data->oversampling_temp;
408 mutex_unlock(&data->lock);
409 pm_runtime_mark_last_busy(data->dev);
410 pm_runtime_put_autosuspend(data->dev);
415 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
419 const int *avail = data->chip_info->oversampling_humid_avail;
420 const int n = data->chip_info->num_oversampling_humid_avail;
422 for (i = 0; i < n; i++) {
423 if (avail[i] == val) {
424 data->oversampling_humid = ilog2(val);
426 return data->chip_info->chip_config(data);
432 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
436 const int *avail = data->chip_info->oversampling_temp_avail;
437 const int n = data->chip_info->num_oversampling_temp_avail;
439 for (i = 0; i < n; i++) {
440 if (avail[i] == val) {
441 data->oversampling_temp = ilog2(val);
443 return data->chip_info->chip_config(data);
449 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
453 const int *avail = data->chip_info->oversampling_press_avail;
454 const int n = data->chip_info->num_oversampling_press_avail;
456 for (i = 0; i < n; i++) {
457 if (avail[i] == val) {
458 data->oversampling_press = ilog2(val);
460 return data->chip_info->chip_config(data);
466 static int bmp280_write_raw(struct iio_dev *indio_dev,
467 struct iio_chan_spec const *chan,
468 int val, int val2, long mask)
471 struct bmp280_data *data = iio_priv(indio_dev);
474 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
475 pm_runtime_get_sync(data->dev);
476 mutex_lock(&data->lock);
477 switch (chan->type) {
478 case IIO_HUMIDITYRELATIVE:
479 ret = bmp280_write_oversampling_ratio_humid(data, val);
482 ret = bmp280_write_oversampling_ratio_press(data, val);
485 ret = bmp280_write_oversampling_ratio_temp(data, val);
491 mutex_unlock(&data->lock);
492 pm_runtime_mark_last_busy(data->dev);
493 pm_runtime_put_autosuspend(data->dev);
502 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
507 for (i = 0; i < n; i++)
508 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
515 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
516 struct device_attribute *attr, char *buf)
518 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
520 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
521 data->chip_info->num_oversampling_temp_avail);
524 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
525 struct device_attribute *attr, char *buf)
527 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
529 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
530 data->chip_info->num_oversampling_press_avail);
533 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
534 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
536 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
537 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
539 static struct attribute *bmp280_attributes[] = {
540 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
541 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
545 static const struct attribute_group bmp280_attrs_group = {
546 .attrs = bmp280_attributes,
549 static const struct iio_info bmp280_info = {
550 .driver_module = THIS_MODULE,
551 .read_raw = &bmp280_read_raw,
552 .write_raw = &bmp280_write_raw,
553 .attrs = &bmp280_attrs_group,
556 static int bmp280_chip_config(struct bmp280_data *data)
559 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
560 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
562 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
563 BMP280_OSRS_TEMP_MASK |
564 BMP280_OSRS_PRESS_MASK |
566 osrs | BMP280_MODE_NORMAL);
569 "failed to write ctrl_meas register\n");
573 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
578 "failed to write config register\n");
585 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
587 static const struct bmp280_chip_info bmp280_chip_info = {
588 .oversampling_temp_avail = bmp280_oversampling_avail,
589 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
591 .oversampling_press_avail = bmp280_oversampling_avail,
592 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
594 .chip_config = bmp280_chip_config,
595 .read_temp = bmp280_read_temp,
596 .read_press = bmp280_read_press,
599 static int bme280_chip_config(struct bmp280_data *data)
601 int ret = bmp280_chip_config(data);
602 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
607 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
608 BMP280_OSRS_HUMIDITY_MASK, osrs);
611 static const struct bmp280_chip_info bme280_chip_info = {
612 .oversampling_temp_avail = bmp280_oversampling_avail,
613 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
615 .oversampling_press_avail = bmp280_oversampling_avail,
616 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
618 .oversampling_humid_avail = bmp280_oversampling_avail,
619 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
621 .chip_config = bme280_chip_config,
622 .read_temp = bmp280_read_temp,
623 .read_press = bmp280_read_press,
624 .read_humid = bmp280_read_humid,
627 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
630 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
631 unsigned int delay_us;
635 reinit_completion(&data->done);
637 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
643 * If we have a completion interrupt, use it, wait up to
644 * 100ms. The longest conversion time listed is 76.5 ms for
645 * advanced resolution mode.
647 ret = wait_for_completion_timeout(&data->done,
648 1 + msecs_to_jiffies(100));
650 dev_err(data->dev, "timeout waiting for completion\n");
652 if (ctrl_meas == BMP180_MEAS_TEMP)
656 conversion_time_max[data->oversampling_press];
658 usleep_range(delay_us, delay_us + 1000);
661 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
665 /* The value of this bit reset to "0" after conversion is complete */
666 if (ctrl & BMP180_MEAS_SCO)
672 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
677 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
681 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
685 *val = be16_to_cpu(tmp);
690 static int bmp180_read_calib(struct bmp280_data *data,
691 struct bmp180_calib *calib)
695 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
697 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
703 /* None of the words has the value 0 or 0xFFFF */
704 for (i = 0; i < ARRAY_SIZE(buf); i++) {
705 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
709 /* Toss the calibration data into the entropy pool */
710 add_device_randomness(buf, sizeof(buf));
712 calib->AC1 = be16_to_cpu(buf[AC1]);
713 calib->AC2 = be16_to_cpu(buf[AC2]);
714 calib->AC3 = be16_to_cpu(buf[AC3]);
715 calib->AC4 = be16_to_cpu(buf[AC4]);
716 calib->AC5 = be16_to_cpu(buf[AC5]);
717 calib->AC6 = be16_to_cpu(buf[AC6]);
718 calib->B1 = be16_to_cpu(buf[B1]);
719 calib->B2 = be16_to_cpu(buf[B2]);
720 calib->MB = be16_to_cpu(buf[MB]);
721 calib->MC = be16_to_cpu(buf[MC]);
722 calib->MD = be16_to_cpu(buf[MD]);
728 * Returns temperature in DegC, resolution is 0.1 DegC.
729 * t_fine carries fine temperature as global value.
731 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
733 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
736 struct bmp180_calib *calib = &data->calib;
738 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
739 x2 = (calib->MC << 11) / (x1 + calib->MD);
740 data->t_fine = x1 + x2;
742 return (data->t_fine + 8) >> 4;
745 static int bmp180_read_temp(struct bmp280_data *data, int *val)
748 s32 adc_temp, comp_temp;
750 ret = bmp180_read_adc_temp(data, &adc_temp);
754 comp_temp = bmp180_compensate_temp(data, adc_temp);
757 * val might be NULL if we're called by the read_press routine,
758 * who only cares about the carry over t_fine value.
761 *val = comp_temp * 100;
768 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
772 u8 oss = data->oversampling_press;
774 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
778 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
782 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
788 * Returns pressure in Pa, resolution is 1 Pa.
790 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
792 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
797 s32 oss = data->oversampling_press;
798 struct bmp180_calib *calib = &data->calib;
800 b6 = data->t_fine - 4000;
801 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
802 x2 = calib->AC2 * b6 >> 11;
804 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
805 x1 = calib->AC3 * b6 >> 13;
806 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
807 x3 = (x1 + x2 + 2) >> 2;
808 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
809 b7 = ((u32)adc_press - b3) * (50000 >> oss);
815 x1 = (p >> 8) * (p >> 8);
816 x1 = (x1 * 3038) >> 16;
817 x2 = (-7357 * p) >> 16;
819 return p + ((x1 + x2 + 3791) >> 4);
822 static int bmp180_read_press(struct bmp280_data *data,
829 /* Read and compensate temperature so we get a reading of t_fine. */
830 ret = bmp180_read_temp(data, NULL);
834 ret = bmp180_read_adc_press(data, &adc_press);
838 comp_press = bmp180_compensate_press(data, adc_press);
843 return IIO_VAL_FRACTIONAL;
846 static int bmp180_chip_config(struct bmp280_data *data)
851 static const int bmp180_oversampling_temp_avail[] = { 1 };
852 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
854 static const struct bmp280_chip_info bmp180_chip_info = {
855 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
856 .num_oversampling_temp_avail =
857 ARRAY_SIZE(bmp180_oversampling_temp_avail),
859 .oversampling_press_avail = bmp180_oversampling_press_avail,
860 .num_oversampling_press_avail =
861 ARRAY_SIZE(bmp180_oversampling_press_avail),
863 .chip_config = bmp180_chip_config,
864 .read_temp = bmp180_read_temp,
865 .read_press = bmp180_read_press,
868 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
870 struct bmp280_data *data = d;
872 complete(&data->done);
877 static int bmp085_fetch_eoc_irq(struct device *dev,
880 struct bmp280_data *data)
882 unsigned long irq_trig;
885 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
886 if (irq_trig != IRQF_TRIGGER_RISING) {
887 dev_err(dev, "non-rising trigger given for EOC interrupt, "
888 "trying to enforce it\n");
889 irq_trig = IRQF_TRIGGER_RISING;
892 init_completion(&data->done);
894 ret = devm_request_threaded_irq(dev,
902 /* Bail out without IRQ but keep the driver in place */
903 dev_err(dev, "unable to request DRDY IRQ\n");
907 data->use_eoc = true;
911 int bmp280_common_probe(struct device *dev,
912 struct regmap *regmap,
918 struct iio_dev *indio_dev;
919 struct bmp280_data *data;
920 unsigned int chip_id;
921 struct gpio_desc *gpiod;
923 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
927 data = iio_priv(indio_dev);
928 mutex_init(&data->lock);
931 indio_dev->dev.parent = dev;
932 indio_dev->name = name;
933 indio_dev->channels = bmp280_channels;
934 indio_dev->info = &bmp280_info;
935 indio_dev->modes = INDIO_DIRECT_MODE;
939 indio_dev->num_channels = 2;
940 data->chip_info = &bmp180_chip_info;
941 data->oversampling_press = ilog2(8);
942 data->oversampling_temp = ilog2(1);
943 data->start_up_time = 10;
946 indio_dev->num_channels = 2;
947 data->chip_info = &bmp280_chip_info;
948 data->oversampling_press = ilog2(16);
949 data->oversampling_temp = ilog2(2);
950 data->start_up_time = 2;
953 indio_dev->num_channels = 3;
954 data->chip_info = &bme280_chip_info;
955 data->oversampling_press = ilog2(16);
956 data->oversampling_humid = ilog2(16);
957 data->oversampling_temp = ilog2(2);
958 data->start_up_time = 2;
964 /* Bring up regulators */
965 data->vddd = devm_regulator_get(dev, "vddd");
966 if (IS_ERR(data->vddd)) {
967 dev_err(dev, "failed to get VDDD regulator\n");
968 return PTR_ERR(data->vddd);
970 ret = regulator_enable(data->vddd);
972 dev_err(dev, "failed to enable VDDD regulator\n");
975 data->vdda = devm_regulator_get(dev, "vdda");
976 if (IS_ERR(data->vdda)) {
977 dev_err(dev, "failed to get VDDA regulator\n");
978 ret = PTR_ERR(data->vdda);
979 goto out_disable_vddd;
981 ret = regulator_enable(data->vdda);
983 dev_err(dev, "failed to enable VDDA regulator\n");
984 goto out_disable_vddd;
986 /* Wait to make sure we started up properly */
987 mdelay(data->start_up_time);
989 /* Bring chip out of reset if there is an assigned GPIO line */
990 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
991 /* Deassert the signal */
992 if (!IS_ERR(gpiod)) {
993 dev_info(dev, "release reset\n");
994 gpiod_set_value(gpiod, 0);
997 data->regmap = regmap;
998 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1000 goto out_disable_vdda;
1001 if (chip_id != chip) {
1002 dev_err(dev, "bad chip id: expected %x got %x\n",
1005 goto out_disable_vdda;
1008 ret = data->chip_info->chip_config(data);
1010 goto out_disable_vdda;
1012 dev_set_drvdata(dev, indio_dev);
1015 * The BMP085 and BMP180 has calibration in an E2PROM, read it out
1016 * at probe time. It will not change.
1018 if (chip_id == BMP180_CHIP_ID) {
1019 ret = bmp180_read_calib(data, &data->calib);
1022 "failed to read calibration coefficients\n");
1023 goto out_disable_vdda;
1028 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1029 * however as it happens, the BMP085 shares the chip ID of BMP180
1030 * so we look for an IRQ if we have that.
1032 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1033 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1035 goto out_disable_vdda;
1038 /* Enable runtime PM */
1039 pm_runtime_get_noresume(dev);
1040 pm_runtime_set_active(dev);
1041 pm_runtime_enable(dev);
1043 * Set autosuspend to two orders of magnitude larger than the
1046 pm_runtime_set_autosuspend_delay(dev, data->start_up_time *100);
1047 pm_runtime_use_autosuspend(dev);
1048 pm_runtime_put(dev);
1050 ret = iio_device_register(indio_dev);
1052 goto out_runtime_pm_disable;
1057 out_runtime_pm_disable:
1058 pm_runtime_get_sync(data->dev);
1059 pm_runtime_put_noidle(data->dev);
1060 pm_runtime_disable(data->dev);
1062 regulator_disable(data->vdda);
1064 regulator_disable(data->vddd);
1067 EXPORT_SYMBOL(bmp280_common_probe);
1069 int bmp280_common_remove(struct device *dev)
1071 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1072 struct bmp280_data *data = iio_priv(indio_dev);
1074 iio_device_unregister(indio_dev);
1075 pm_runtime_get_sync(data->dev);
1076 pm_runtime_put_noidle(data->dev);
1077 pm_runtime_disable(data->dev);
1078 regulator_disable(data->vdda);
1079 regulator_disable(data->vddd);
1082 EXPORT_SYMBOL(bmp280_common_remove);
1085 static int bmp280_runtime_suspend(struct device *dev)
1087 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1088 struct bmp280_data *data = iio_priv(indio_dev);
1091 ret = regulator_disable(data->vdda);
1094 return regulator_disable(data->vddd);
1097 static int bmp280_runtime_resume(struct device *dev)
1099 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1100 struct bmp280_data *data = iio_priv(indio_dev);
1103 ret = regulator_enable(data->vddd);
1106 ret = regulator_enable(data->vdda);
1109 msleep(data->start_up_time);
1110 return data->chip_info->chip_config(data);
1112 #endif /* CONFIG_PM */
1114 const struct dev_pm_ops bmp280_dev_pm_ops = {
1115 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1116 pm_runtime_force_resume)
1117 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1118 bmp280_runtime_resume, NULL)
1120 EXPORT_SYMBOL(bmp280_dev_pm_ops);
1122 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1123 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1124 MODULE_LICENSE("GPL v2");