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 };
58 /* See datasheet Section 4.2.2. */
83 struct regmap *regmap;
84 struct completion done;
86 const struct bmp280_chip_info *chip_info;
88 struct bmp180_calib bmp180;
89 struct bmp280_calib bmp280;
91 struct regulator *vddd;
92 struct regulator *vdda;
93 unsigned int start_up_time; /* in microseconds */
95 /* log of base 2 of oversampling rate */
96 u8 oversampling_press;
98 u8 oversampling_humid;
101 * Carryover value from temperature conversion, used in pressure
107 struct bmp280_chip_info {
108 const int *oversampling_temp_avail;
109 int num_oversampling_temp_avail;
111 const int *oversampling_press_avail;
112 int num_oversampling_press_avail;
114 const int *oversampling_humid_avail;
115 int num_oversampling_humid_avail;
117 int (*chip_config)(struct bmp280_data *);
118 int (*read_temp)(struct bmp280_data *, int *);
119 int (*read_press)(struct bmp280_data *, int *, int *);
120 int (*read_humid)(struct bmp280_data *, int *, int *);
124 * These enums are used for indexing into the array of compensation
125 * parameters for BMP280.
128 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
130 static const struct iio_chan_spec bmp280_channels[] = {
132 .type = IIO_PRESSURE,
133 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
134 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
138 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
139 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
142 .type = IIO_HUMIDITYRELATIVE,
143 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
144 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
148 static int bmp280_read_calib(struct bmp280_data *data,
149 struct bmp280_calib *calib,
154 struct device *dev = data->dev;
155 __le16 t_buf[BMP280_COMP_TEMP_REG_COUNT / 2];
156 __le16 p_buf[BMP280_COMP_PRESS_REG_COUNT / 2];
158 /* Read temperature calibration values. */
159 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
160 t_buf, BMP280_COMP_TEMP_REG_COUNT);
163 "failed to read temperature calibration parameters\n");
167 /* Toss the temperature calibration data into the entropy pool */
168 add_device_randomness(t_buf, sizeof(t_buf));
170 calib->T1 = le16_to_cpu(t_buf[T1]);
171 calib->T2 = le16_to_cpu(t_buf[T2]);
172 calib->T3 = le16_to_cpu(t_buf[T3]);
174 /* Read pressure calibration values. */
175 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
176 p_buf, BMP280_COMP_PRESS_REG_COUNT);
179 "failed to read pressure calibration parameters\n");
183 /* Toss the pressure calibration data into the entropy pool */
184 add_device_randomness(p_buf, sizeof(p_buf));
186 calib->P1 = le16_to_cpu(p_buf[P1]);
187 calib->P2 = le16_to_cpu(p_buf[P2]);
188 calib->P3 = le16_to_cpu(p_buf[P3]);
189 calib->P4 = le16_to_cpu(p_buf[P4]);
190 calib->P5 = le16_to_cpu(p_buf[P5]);
191 calib->P6 = le16_to_cpu(p_buf[P6]);
192 calib->P7 = le16_to_cpu(p_buf[P7]);
193 calib->P8 = le16_to_cpu(p_buf[P8]);
194 calib->P9 = le16_to_cpu(p_buf[P9]);
197 * Read humidity calibration values.
198 * Due to some odd register addressing we cannot just
199 * do a big bulk read. Instead, we have to read each Hx
200 * value separately and sometimes do some bit shifting...
201 * Humidity data is only available on BME280.
203 if (chip != BME280_CHIP_ID)
206 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
208 dev_err(dev, "failed to read H1 comp value\n");
213 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
215 dev_err(dev, "failed to read H2 comp value\n");
218 calib->H2 = sign_extend32(le16_to_cpu(tmp), 15);
220 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
222 dev_err(dev, "failed to read H3 comp value\n");
227 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
229 dev_err(dev, "failed to read H4 comp value\n");
232 calib->H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
233 (be16_to_cpu(tmp) & 0xf), 11);
235 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
237 dev_err(dev, "failed to read H5 comp value\n");
240 calib->H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
242 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
244 dev_err(dev, "failed to read H6 comp value\n");
247 calib->H6 = sign_extend32(tmp, 7);
252 * Returns humidity in percent, resolution is 0.01 percent. Output value of
253 * "47445" represents 47445/1024 = 46.333 %RH.
255 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
257 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
261 struct bmp280_calib *calib = &data->calib.bmp280;
263 var = ((s32)data->t_fine) - (s32)76800;
264 var = ((((adc_humidity << 14) - (calib->H4 << 20) - (calib->H5 * var))
265 + (s32)16384) >> 15) * (((((((var * calib->H6) >> 10)
266 * (((var * (s32)calib->H3) >> 11) + (s32)32768)) >> 10)
267 + (s32)2097152) * calib->H2 + 8192) >> 14);
268 var -= ((((var >> 15) * (var >> 15)) >> 7) * (s32)calib->H1) >> 4;
274 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
275 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
278 * Taken from datasheet, Section 3.11.3, "Compensation formula".
280 static s32 bmp280_compensate_temp(struct bmp280_data *data,
284 struct bmp280_calib *calib = &data->calib.bmp280;
286 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
287 ((s32)calib->T2)) >> 11;
288 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
289 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
290 ((s32)calib->T3)) >> 14;
291 data->t_fine = var1 + var2;
293 return (data->t_fine * 5 + 128) >> 8;
297 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
298 * integer bits and 8 fractional bits). Output value of "24674867"
299 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
301 * Taken from datasheet, Section 3.11.3, "Compensation formula".
303 static u32 bmp280_compensate_press(struct bmp280_data *data,
307 struct bmp280_calib *calib = &data->calib.bmp280;
309 var1 = ((s64)data->t_fine) - 128000;
310 var2 = var1 * var1 * (s64)calib->P6;
311 var2 += (var1 * (s64)calib->P5) << 17;
312 var2 += ((s64)calib->P4) << 35;
313 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
314 ((var1 * (s64)calib->P2) << 12);
315 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
320 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
321 p = div64_s64(p, var1);
322 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
323 var2 = ((s64)(calib->P8) * p) >> 19;
324 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
329 static int bmp280_read_temp(struct bmp280_data *data,
334 s32 adc_temp, comp_temp;
336 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
339 dev_err(data->dev, "failed to read temperature\n");
343 adc_temp = be32_to_cpu(tmp) >> 12;
344 if (adc_temp == BMP280_TEMP_SKIPPED) {
345 /* reading was skipped */
346 dev_err(data->dev, "reading temperature skipped\n");
349 comp_temp = bmp280_compensate_temp(data, adc_temp);
352 * val might be NULL if we're called by the read_press routine,
353 * who only cares about the carry over t_fine value.
356 *val = comp_temp * 10;
363 static int bmp280_read_press(struct bmp280_data *data,
371 /* Read and compensate temperature so we get a reading of t_fine. */
372 ret = bmp280_read_temp(data, NULL);
376 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
379 dev_err(data->dev, "failed to read pressure\n");
383 adc_press = be32_to_cpu(tmp) >> 12;
384 if (adc_press == BMP280_PRESS_SKIPPED) {
385 /* reading was skipped */
386 dev_err(data->dev, "reading pressure skipped\n");
389 comp_press = bmp280_compensate_press(data, adc_press);
394 return IIO_VAL_FRACTIONAL;
397 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
404 /* Read and compensate temperature so we get a reading of t_fine. */
405 ret = bmp280_read_temp(data, NULL);
409 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
412 dev_err(data->dev, "failed to read humidity\n");
416 adc_humidity = be16_to_cpu(tmp);
417 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
418 /* reading was skipped */
419 dev_err(data->dev, "reading humidity skipped\n");
422 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
424 *val = comp_humidity * 1000 / 1024;
429 static int bmp280_read_raw(struct iio_dev *indio_dev,
430 struct iio_chan_spec const *chan,
431 int *val, int *val2, long mask)
434 struct bmp280_data *data = iio_priv(indio_dev);
436 pm_runtime_get_sync(data->dev);
437 mutex_lock(&data->lock);
440 case IIO_CHAN_INFO_PROCESSED:
441 switch (chan->type) {
442 case IIO_HUMIDITYRELATIVE:
443 ret = data->chip_info->read_humid(data, val, val2);
446 ret = data->chip_info->read_press(data, val, val2);
449 ret = data->chip_info->read_temp(data, val);
456 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
457 switch (chan->type) {
458 case IIO_HUMIDITYRELATIVE:
459 *val = 1 << data->oversampling_humid;
463 *val = 1 << data->oversampling_press;
467 *val = 1 << data->oversampling_temp;
480 mutex_unlock(&data->lock);
481 pm_runtime_mark_last_busy(data->dev);
482 pm_runtime_put_autosuspend(data->dev);
487 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
491 const int *avail = data->chip_info->oversampling_humid_avail;
492 const int n = data->chip_info->num_oversampling_humid_avail;
494 for (i = 0; i < n; i++) {
495 if (avail[i] == val) {
496 data->oversampling_humid = ilog2(val);
498 return data->chip_info->chip_config(data);
504 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
508 const int *avail = data->chip_info->oversampling_temp_avail;
509 const int n = data->chip_info->num_oversampling_temp_avail;
511 for (i = 0; i < n; i++) {
512 if (avail[i] == val) {
513 data->oversampling_temp = ilog2(val);
515 return data->chip_info->chip_config(data);
521 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
525 const int *avail = data->chip_info->oversampling_press_avail;
526 const int n = data->chip_info->num_oversampling_press_avail;
528 for (i = 0; i < n; i++) {
529 if (avail[i] == val) {
530 data->oversampling_press = ilog2(val);
532 return data->chip_info->chip_config(data);
538 static int bmp280_write_raw(struct iio_dev *indio_dev,
539 struct iio_chan_spec const *chan,
540 int val, int val2, long mask)
543 struct bmp280_data *data = iio_priv(indio_dev);
546 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
547 pm_runtime_get_sync(data->dev);
548 mutex_lock(&data->lock);
549 switch (chan->type) {
550 case IIO_HUMIDITYRELATIVE:
551 ret = bmp280_write_oversampling_ratio_humid(data, val);
554 ret = bmp280_write_oversampling_ratio_press(data, val);
557 ret = bmp280_write_oversampling_ratio_temp(data, val);
563 mutex_unlock(&data->lock);
564 pm_runtime_mark_last_busy(data->dev);
565 pm_runtime_put_autosuspend(data->dev);
574 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
579 for (i = 0; i < n; i++)
580 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
587 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
588 struct device_attribute *attr, char *buf)
590 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
592 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
593 data->chip_info->num_oversampling_temp_avail);
596 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
597 struct device_attribute *attr, char *buf)
599 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
601 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
602 data->chip_info->num_oversampling_press_avail);
605 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
606 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
608 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
609 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
611 static struct attribute *bmp280_attributes[] = {
612 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
613 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
617 static const struct attribute_group bmp280_attrs_group = {
618 .attrs = bmp280_attributes,
621 static const struct iio_info bmp280_info = {
622 .read_raw = &bmp280_read_raw,
623 .write_raw = &bmp280_write_raw,
624 .attrs = &bmp280_attrs_group,
627 static int bmp280_chip_config(struct bmp280_data *data)
630 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
631 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
633 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
634 BMP280_OSRS_TEMP_MASK |
635 BMP280_OSRS_PRESS_MASK |
637 osrs | BMP280_MODE_NORMAL);
640 "failed to write ctrl_meas register\n");
644 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
649 "failed to write config register\n");
656 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
658 static const struct bmp280_chip_info bmp280_chip_info = {
659 .oversampling_temp_avail = bmp280_oversampling_avail,
660 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
662 .oversampling_press_avail = bmp280_oversampling_avail,
663 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
665 .chip_config = bmp280_chip_config,
666 .read_temp = bmp280_read_temp,
667 .read_press = bmp280_read_press,
670 static int bme280_chip_config(struct bmp280_data *data)
673 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
676 * Oversampling of humidity must be set before oversampling of
677 * temperature/pressure is set to become effective.
679 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
680 BMP280_OSRS_HUMIDITY_MASK, osrs);
685 return bmp280_chip_config(data);
688 static const struct bmp280_chip_info bme280_chip_info = {
689 .oversampling_temp_avail = bmp280_oversampling_avail,
690 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
692 .oversampling_press_avail = bmp280_oversampling_avail,
693 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
695 .oversampling_humid_avail = bmp280_oversampling_avail,
696 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
698 .chip_config = bme280_chip_config,
699 .read_temp = bmp280_read_temp,
700 .read_press = bmp280_read_press,
701 .read_humid = bmp280_read_humid,
704 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
707 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
708 unsigned int delay_us;
712 init_completion(&data->done);
714 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
720 * If we have a completion interrupt, use it, wait up to
721 * 100ms. The longest conversion time listed is 76.5 ms for
722 * advanced resolution mode.
724 ret = wait_for_completion_timeout(&data->done,
725 1 + msecs_to_jiffies(100));
727 dev_err(data->dev, "timeout waiting for completion\n");
729 if (ctrl_meas == BMP180_MEAS_TEMP)
733 conversion_time_max[data->oversampling_press];
735 usleep_range(delay_us, delay_us + 1000);
738 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
742 /* The value of this bit reset to "0" after conversion is complete */
743 if (ctrl & BMP180_MEAS_SCO)
749 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
754 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
758 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
762 *val = be16_to_cpu(tmp);
767 static int bmp180_read_calib(struct bmp280_data *data,
768 struct bmp180_calib *calib)
772 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
774 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
780 /* None of the words has the value 0 or 0xFFFF */
781 for (i = 0; i < ARRAY_SIZE(buf); i++) {
782 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
786 /* Toss the calibration data into the entropy pool */
787 add_device_randomness(buf, sizeof(buf));
789 calib->AC1 = be16_to_cpu(buf[AC1]);
790 calib->AC2 = be16_to_cpu(buf[AC2]);
791 calib->AC3 = be16_to_cpu(buf[AC3]);
792 calib->AC4 = be16_to_cpu(buf[AC4]);
793 calib->AC5 = be16_to_cpu(buf[AC5]);
794 calib->AC6 = be16_to_cpu(buf[AC6]);
795 calib->B1 = be16_to_cpu(buf[B1]);
796 calib->B2 = be16_to_cpu(buf[B2]);
797 calib->MB = be16_to_cpu(buf[MB]);
798 calib->MC = be16_to_cpu(buf[MC]);
799 calib->MD = be16_to_cpu(buf[MD]);
805 * Returns temperature in DegC, resolution is 0.1 DegC.
806 * t_fine carries fine temperature as global value.
808 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
810 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
813 struct bmp180_calib *calib = &data->calib.bmp180;
815 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
816 x2 = (calib->MC << 11) / (x1 + calib->MD);
817 data->t_fine = x1 + x2;
819 return (data->t_fine + 8) >> 4;
822 static int bmp180_read_temp(struct bmp280_data *data, int *val)
825 s32 adc_temp, comp_temp;
827 ret = bmp180_read_adc_temp(data, &adc_temp);
831 comp_temp = bmp180_compensate_temp(data, adc_temp);
834 * val might be NULL if we're called by the read_press routine,
835 * who only cares about the carry over t_fine value.
838 *val = comp_temp * 100;
845 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
849 u8 oss = data->oversampling_press;
851 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
855 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
859 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
865 * Returns pressure in Pa, resolution is 1 Pa.
867 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
869 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
874 s32 oss = data->oversampling_press;
875 struct bmp180_calib *calib = &data->calib.bmp180;
877 b6 = data->t_fine - 4000;
878 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
879 x2 = calib->AC2 * b6 >> 11;
881 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
882 x1 = calib->AC3 * b6 >> 13;
883 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
884 x3 = (x1 + x2 + 2) >> 2;
885 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
886 b7 = ((u32)adc_press - b3) * (50000 >> oss);
892 x1 = (p >> 8) * (p >> 8);
893 x1 = (x1 * 3038) >> 16;
894 x2 = (-7357 * p) >> 16;
896 return p + ((x1 + x2 + 3791) >> 4);
899 static int bmp180_read_press(struct bmp280_data *data,
906 /* Read and compensate temperature so we get a reading of t_fine. */
907 ret = bmp180_read_temp(data, NULL);
911 ret = bmp180_read_adc_press(data, &adc_press);
915 comp_press = bmp180_compensate_press(data, adc_press);
920 return IIO_VAL_FRACTIONAL;
923 static int bmp180_chip_config(struct bmp280_data *data)
928 static const int bmp180_oversampling_temp_avail[] = { 1 };
929 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
931 static const struct bmp280_chip_info bmp180_chip_info = {
932 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
933 .num_oversampling_temp_avail =
934 ARRAY_SIZE(bmp180_oversampling_temp_avail),
936 .oversampling_press_avail = bmp180_oversampling_press_avail,
937 .num_oversampling_press_avail =
938 ARRAY_SIZE(bmp180_oversampling_press_avail),
940 .chip_config = bmp180_chip_config,
941 .read_temp = bmp180_read_temp,
942 .read_press = bmp180_read_press,
945 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
947 struct bmp280_data *data = d;
949 complete(&data->done);
954 static int bmp085_fetch_eoc_irq(struct device *dev,
957 struct bmp280_data *data)
959 unsigned long irq_trig;
962 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
963 if (irq_trig != IRQF_TRIGGER_RISING) {
964 dev_err(dev, "non-rising trigger given for EOC interrupt, "
965 "trying to enforce it\n");
966 irq_trig = IRQF_TRIGGER_RISING;
968 ret = devm_request_threaded_irq(dev,
976 /* Bail out without IRQ but keep the driver in place */
977 dev_err(dev, "unable to request DRDY IRQ\n");
981 data->use_eoc = true;
985 int bmp280_common_probe(struct device *dev,
986 struct regmap *regmap,
992 struct iio_dev *indio_dev;
993 struct bmp280_data *data;
994 unsigned int chip_id;
995 struct gpio_desc *gpiod;
997 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1001 data = iio_priv(indio_dev);
1002 mutex_init(&data->lock);
1005 indio_dev->dev.parent = dev;
1006 indio_dev->name = name;
1007 indio_dev->channels = bmp280_channels;
1008 indio_dev->info = &bmp280_info;
1009 indio_dev->modes = INDIO_DIRECT_MODE;
1012 case BMP180_CHIP_ID:
1013 indio_dev->num_channels = 2;
1014 data->chip_info = &bmp180_chip_info;
1015 data->oversampling_press = ilog2(8);
1016 data->oversampling_temp = ilog2(1);
1017 data->start_up_time = 10000;
1019 case BMP280_CHIP_ID:
1020 indio_dev->num_channels = 2;
1021 data->chip_info = &bmp280_chip_info;
1022 data->oversampling_press = ilog2(16);
1023 data->oversampling_temp = ilog2(2);
1024 data->start_up_time = 2000;
1026 case BME280_CHIP_ID:
1027 indio_dev->num_channels = 3;
1028 data->chip_info = &bme280_chip_info;
1029 data->oversampling_press = ilog2(16);
1030 data->oversampling_humid = ilog2(16);
1031 data->oversampling_temp = ilog2(2);
1032 data->start_up_time = 2000;
1038 /* Bring up regulators */
1039 data->vddd = devm_regulator_get(dev, "vddd");
1040 if (IS_ERR(data->vddd)) {
1041 dev_err(dev, "failed to get VDDD regulator\n");
1042 return PTR_ERR(data->vddd);
1044 ret = regulator_enable(data->vddd);
1046 dev_err(dev, "failed to enable VDDD regulator\n");
1049 data->vdda = devm_regulator_get(dev, "vdda");
1050 if (IS_ERR(data->vdda)) {
1051 dev_err(dev, "failed to get VDDA regulator\n");
1052 ret = PTR_ERR(data->vdda);
1053 goto out_disable_vddd;
1055 ret = regulator_enable(data->vdda);
1057 dev_err(dev, "failed to enable VDDA regulator\n");
1058 goto out_disable_vddd;
1060 /* Wait to make sure we started up properly */
1061 usleep_range(data->start_up_time, data->start_up_time + 100);
1063 /* Bring chip out of reset if there is an assigned GPIO line */
1064 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1065 /* Deassert the signal */
1066 if (!IS_ERR(gpiod)) {
1067 dev_info(dev, "release reset\n");
1068 gpiod_set_value(gpiod, 0);
1071 data->regmap = regmap;
1072 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1074 goto out_disable_vdda;
1075 if (chip_id != chip) {
1076 dev_err(dev, "bad chip id: expected %x got %x\n",
1079 goto out_disable_vdda;
1082 ret = data->chip_info->chip_config(data);
1084 goto out_disable_vdda;
1086 dev_set_drvdata(dev, indio_dev);
1089 * Some chips have calibration parameters "programmed into the devices'
1090 * non-volatile memory during production". Let's read them out at probe
1091 * time once. They will not change.
1093 if (chip_id == BMP180_CHIP_ID) {
1094 ret = bmp180_read_calib(data, &data->calib.bmp180);
1097 "failed to read calibration coefficients\n");
1098 goto out_disable_vdda;
1100 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1101 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1104 "failed to read calibration coefficients\n");
1105 goto out_disable_vdda;
1110 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1111 * however as it happens, the BMP085 shares the chip ID of BMP180
1112 * so we look for an IRQ if we have that.
1114 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1115 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1117 goto out_disable_vdda;
1120 /* Enable runtime PM */
1121 pm_runtime_get_noresume(dev);
1122 pm_runtime_set_active(dev);
1123 pm_runtime_enable(dev);
1125 * Set autosuspend to two orders of magnitude larger than the
1128 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1129 pm_runtime_use_autosuspend(dev);
1130 pm_runtime_put(dev);
1132 ret = iio_device_register(indio_dev);
1134 goto out_runtime_pm_disable;
1139 out_runtime_pm_disable:
1140 pm_runtime_get_sync(data->dev);
1141 pm_runtime_put_noidle(data->dev);
1142 pm_runtime_disable(data->dev);
1144 regulator_disable(data->vdda);
1146 regulator_disable(data->vddd);
1149 EXPORT_SYMBOL(bmp280_common_probe);
1151 int bmp280_common_remove(struct device *dev)
1153 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1154 struct bmp280_data *data = iio_priv(indio_dev);
1156 iio_device_unregister(indio_dev);
1157 pm_runtime_get_sync(data->dev);
1158 pm_runtime_put_noidle(data->dev);
1159 pm_runtime_disable(data->dev);
1160 regulator_disable(data->vdda);
1161 regulator_disable(data->vddd);
1164 EXPORT_SYMBOL(bmp280_common_remove);
1167 static int bmp280_runtime_suspend(struct device *dev)
1169 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1170 struct bmp280_data *data = iio_priv(indio_dev);
1173 ret = regulator_disable(data->vdda);
1176 return regulator_disable(data->vddd);
1179 static int bmp280_runtime_resume(struct device *dev)
1181 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1182 struct bmp280_data *data = iio_priv(indio_dev);
1185 ret = regulator_enable(data->vddd);
1188 ret = regulator_enable(data->vdda);
1191 usleep_range(data->start_up_time, data->start_up_time + 100);
1192 return data->chip_info->chip_config(data);
1194 #endif /* CONFIG_PM */
1196 const struct dev_pm_ops bmp280_dev_pm_ops = {
1197 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1198 pm_runtime_force_resume)
1199 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1200 bmp280_runtime_resume, NULL)
1202 EXPORT_SYMBOL(bmp280_dev_pm_ops);
1204 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1205 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1206 MODULE_LICENSE("GPL v2");