1 // SPDX-License-Identifier: GPL-2.0-only
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.
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>
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 };
55 /* See datasheet Section 4.2.2. */
80 struct regmap *regmap;
81 struct completion done;
83 const struct bmp280_chip_info *chip_info;
85 struct bmp180_calib bmp180;
86 struct bmp280_calib bmp280;
88 struct regulator *vddd;
89 struct regulator *vdda;
90 unsigned int start_up_time; /* in microseconds */
92 /* log of base 2 of oversampling rate */
93 u8 oversampling_press;
95 u8 oversampling_humid;
98 * Carryover value from temperature conversion, used in pressure
104 struct bmp280_chip_info {
105 const int *oversampling_temp_avail;
106 int num_oversampling_temp_avail;
108 const int *oversampling_press_avail;
109 int num_oversampling_press_avail;
111 const int *oversampling_humid_avail;
112 int num_oversampling_humid_avail;
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 *);
121 * These enums are used for indexing into the array of compensation
122 * parameters for BMP280.
125 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
127 static const struct iio_chan_spec bmp280_channels[] = {
129 .type = IIO_PRESSURE,
130 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
131 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
135 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
136 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
139 .type = IIO_HUMIDITYRELATIVE,
140 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
141 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
145 static int bmp280_read_calib(struct bmp280_data *data,
146 struct bmp280_calib *calib,
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];
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);
160 "failed to read temperature calibration parameters\n");
164 /* Toss the temperature calibration data into the entropy pool */
165 add_device_randomness(t_buf, sizeof(t_buf));
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]);
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);
176 "failed to read pressure calibration parameters\n");
180 /* Toss the pressure calibration data into the entropy pool */
181 add_device_randomness(p_buf, sizeof(p_buf));
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]);
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.
200 if (chip != BME280_CHIP_ID)
203 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
205 dev_err(dev, "failed to read H1 comp value\n");
210 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
212 dev_err(dev, "failed to read H2 comp value\n");
215 calib->H2 = sign_extend32(le16_to_cpu(tmp), 15);
217 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
219 dev_err(dev, "failed to read H3 comp value\n");
224 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
226 dev_err(dev, "failed to read H4 comp value\n");
229 calib->H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
230 (be16_to_cpu(tmp) & 0xf), 11);
232 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
234 dev_err(dev, "failed to read H5 comp value\n");
237 calib->H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
239 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
241 dev_err(dev, "failed to read H6 comp value\n");
244 calib->H6 = sign_extend32(tmp, 7);
249 * Returns humidity in percent, resolution is 0.01 percent. Output value of
250 * "47445" represents 47445/1024 = 46.333 %RH.
252 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
254 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
258 struct bmp280_calib *calib = &data->calib.bmp280;
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;
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
275 * Taken from datasheet, Section 3.11.3, "Compensation formula".
277 static s32 bmp280_compensate_temp(struct bmp280_data *data,
281 struct bmp280_calib *calib = &data->calib.bmp280;
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;
290 return (data->t_fine * 5 + 128) >> 8;
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
298 * Taken from datasheet, Section 3.11.3, "Compensation formula".
300 static u32 bmp280_compensate_press(struct bmp280_data *data,
304 struct bmp280_calib *calib = &data->calib.bmp280;
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;
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);
326 static int bmp280_read_temp(struct bmp280_data *data,
331 s32 adc_temp, comp_temp;
333 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
336 dev_err(data->dev, "failed to read temperature\n");
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");
346 comp_temp = bmp280_compensate_temp(data, adc_temp);
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.
353 *val = comp_temp * 10;
360 static int bmp280_read_press(struct bmp280_data *data,
368 /* Read and compensate temperature so we get a reading of t_fine. */
369 ret = bmp280_read_temp(data, NULL);
373 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
376 dev_err(data->dev, "failed to read pressure\n");
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");
386 comp_press = bmp280_compensate_press(data, adc_press);
391 return IIO_VAL_FRACTIONAL;
394 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
401 /* Read and compensate temperature so we get a reading of t_fine. */
402 ret = bmp280_read_temp(data, NULL);
406 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
409 dev_err(data->dev, "failed to read humidity\n");
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");
419 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
421 *val = comp_humidity * 1000 / 1024;
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)
431 struct bmp280_data *data = iio_priv(indio_dev);
433 pm_runtime_get_sync(data->dev);
434 mutex_lock(&data->lock);
437 case IIO_CHAN_INFO_PROCESSED:
438 switch (chan->type) {
439 case IIO_HUMIDITYRELATIVE:
440 ret = data->chip_info->read_humid(data, val, val2);
443 ret = data->chip_info->read_press(data, val, val2);
446 ret = data->chip_info->read_temp(data, val);
453 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
454 switch (chan->type) {
455 case IIO_HUMIDITYRELATIVE:
456 *val = 1 << data->oversampling_humid;
460 *val = 1 << data->oversampling_press;
464 *val = 1 << data->oversampling_temp;
477 mutex_unlock(&data->lock);
478 pm_runtime_mark_last_busy(data->dev);
479 pm_runtime_put_autosuspend(data->dev);
484 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
488 const int *avail = data->chip_info->oversampling_humid_avail;
489 const int n = data->chip_info->num_oversampling_humid_avail;
491 for (i = 0; i < n; i++) {
492 if (avail[i] == val) {
493 data->oversampling_humid = ilog2(val);
495 return data->chip_info->chip_config(data);
501 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
505 const int *avail = data->chip_info->oversampling_temp_avail;
506 const int n = data->chip_info->num_oversampling_temp_avail;
508 for (i = 0; i < n; i++) {
509 if (avail[i] == val) {
510 data->oversampling_temp = ilog2(val);
512 return data->chip_info->chip_config(data);
518 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
522 const int *avail = data->chip_info->oversampling_press_avail;
523 const int n = data->chip_info->num_oversampling_press_avail;
525 for (i = 0; i < n; i++) {
526 if (avail[i] == val) {
527 data->oversampling_press = ilog2(val);
529 return data->chip_info->chip_config(data);
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)
540 struct bmp280_data *data = iio_priv(indio_dev);
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);
551 ret = bmp280_write_oversampling_ratio_press(data, val);
554 ret = bmp280_write_oversampling_ratio_temp(data, val);
560 mutex_unlock(&data->lock);
561 pm_runtime_mark_last_busy(data->dev);
562 pm_runtime_put_autosuspend(data->dev);
571 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
576 for (i = 0; i < n; i++)
577 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
584 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
585 struct device_attribute *attr, char *buf)
587 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
589 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
590 data->chip_info->num_oversampling_temp_avail);
593 static ssize_t bmp280_show_press_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_press_avail,
599 data->chip_info->num_oversampling_press_avail);
602 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
603 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
605 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
606 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
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,
614 static const struct attribute_group bmp280_attrs_group = {
615 .attrs = bmp280_attributes,
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,
624 static int bmp280_chip_config(struct bmp280_data *data)
627 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
628 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
630 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
631 BMP280_OSRS_TEMP_MASK |
632 BMP280_OSRS_PRESS_MASK |
634 osrs | BMP280_MODE_NORMAL);
637 "failed to write ctrl_meas register\n");
641 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
646 "failed to write config register\n");
653 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
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),
659 .oversampling_press_avail = bmp280_oversampling_avail,
660 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
662 .chip_config = bmp280_chip_config,
663 .read_temp = bmp280_read_temp,
664 .read_press = bmp280_read_press,
667 static int bme280_chip_config(struct bmp280_data *data)
670 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
673 * Oversampling of humidity must be set before oversampling of
674 * temperature/pressure is set to become effective.
676 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
677 BMP280_OSRS_HUMIDITY_MASK, osrs);
682 return bmp280_chip_config(data);
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),
689 .oversampling_press_avail = bmp280_oversampling_avail,
690 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
692 .oversampling_humid_avail = bmp280_oversampling_avail,
693 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
695 .chip_config = bme280_chip_config,
696 .read_temp = bmp280_read_temp,
697 .read_press = bmp280_read_press,
698 .read_humid = bmp280_read_humid,
701 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
704 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
705 unsigned int delay_us;
709 init_completion(&data->done);
711 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
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.
721 ret = wait_for_completion_timeout(&data->done,
722 1 + msecs_to_jiffies(100));
724 dev_err(data->dev, "timeout waiting for completion\n");
726 if (ctrl_meas == BMP180_MEAS_TEMP)
730 conversion_time_max[data->oversampling_press];
732 usleep_range(delay_us, delay_us + 1000);
735 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
739 /* The value of this bit reset to "0" after conversion is complete */
740 if (ctrl & BMP180_MEAS_SCO)
746 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
751 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
755 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
759 *val = be16_to_cpu(tmp);
764 static int bmp180_read_calib(struct bmp280_data *data,
765 struct bmp180_calib *calib)
769 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
771 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
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))
783 /* Toss the calibration data into the entropy pool */
784 add_device_randomness(buf, sizeof(buf));
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]);
802 * Returns temperature in DegC, resolution is 0.1 DegC.
803 * t_fine carries fine temperature as global value.
805 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
807 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
810 struct bmp180_calib *calib = &data->calib.bmp180;
812 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
813 x2 = (calib->MC << 11) / (x1 + calib->MD);
814 data->t_fine = x1 + x2;
816 return (data->t_fine + 8) >> 4;
819 static int bmp180_read_temp(struct bmp280_data *data, int *val)
822 s32 adc_temp, comp_temp;
824 ret = bmp180_read_adc_temp(data, &adc_temp);
828 comp_temp = bmp180_compensate_temp(data, adc_temp);
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.
835 *val = comp_temp * 100;
842 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
846 u8 oss = data->oversampling_press;
848 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
852 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
856 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
862 * Returns pressure in Pa, resolution is 1 Pa.
864 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
866 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
871 s32 oss = data->oversampling_press;
872 struct bmp180_calib *calib = &data->calib.bmp180;
874 b6 = data->t_fine - 4000;
875 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
876 x2 = calib->AC2 * b6 >> 11;
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);
889 x1 = (p >> 8) * (p >> 8);
890 x1 = (x1 * 3038) >> 16;
891 x2 = (-7357 * p) >> 16;
893 return p + ((x1 + x2 + 3791) >> 4);
896 static int bmp180_read_press(struct bmp280_data *data,
903 /* Read and compensate temperature so we get a reading of t_fine. */
904 ret = bmp180_read_temp(data, NULL);
908 ret = bmp180_read_adc_press(data, &adc_press);
912 comp_press = bmp180_compensate_press(data, adc_press);
917 return IIO_VAL_FRACTIONAL;
920 static int bmp180_chip_config(struct bmp280_data *data)
925 static const int bmp180_oversampling_temp_avail[] = { 1 };
926 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
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),
933 .oversampling_press_avail = bmp180_oversampling_press_avail,
934 .num_oversampling_press_avail =
935 ARRAY_SIZE(bmp180_oversampling_press_avail),
937 .chip_config = bmp180_chip_config,
938 .read_temp = bmp180_read_temp,
939 .read_press = bmp180_read_press,
942 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
944 struct bmp280_data *data = d;
946 complete(&data->done);
951 static int bmp085_fetch_eoc_irq(struct device *dev,
954 struct bmp280_data *data)
956 unsigned long irq_trig;
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;
965 ret = devm_request_threaded_irq(dev,
973 /* Bail out without IRQ but keep the driver in place */
974 dev_err(dev, "unable to request DRDY IRQ\n");
978 data->use_eoc = true;
982 int bmp280_common_probe(struct device *dev,
983 struct regmap *regmap,
989 struct iio_dev *indio_dev;
990 struct bmp280_data *data;
991 unsigned int chip_id;
992 struct gpio_desc *gpiod;
994 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
998 data = iio_priv(indio_dev);
999 mutex_init(&data->lock);
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;
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;
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;
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;
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);
1041 ret = regulator_enable(data->vddd);
1043 dev_err(dev, "failed to enable VDDD regulator\n");
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;
1052 ret = regulator_enable(data->vdda);
1054 dev_err(dev, "failed to enable VDDA regulator\n");
1055 goto out_disable_vddd;
1057 /* Wait to make sure we started up properly */
1058 usleep_range(data->start_up_time, data->start_up_time + 100);
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);
1068 data->regmap = regmap;
1069 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1071 goto out_disable_vdda;
1072 if (chip_id != chip) {
1073 dev_err(dev, "bad chip id: expected %x got %x\n",
1076 goto out_disable_vdda;
1079 ret = data->chip_info->chip_config(data);
1081 goto out_disable_vdda;
1083 dev_set_drvdata(dev, indio_dev);
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.
1090 if (chip_id == BMP180_CHIP_ID) {
1091 ret = bmp180_read_calib(data, &data->calib.bmp180);
1094 "failed to read calibration coefficients\n");
1095 goto out_disable_vdda;
1097 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1098 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1101 "failed to read calibration coefficients\n");
1102 goto out_disable_vdda;
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.
1111 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1112 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1114 goto out_disable_vdda;
1117 /* Enable runtime PM */
1118 pm_runtime_get_noresume(dev);
1119 pm_runtime_set_active(dev);
1120 pm_runtime_enable(dev);
1122 * Set autosuspend to two orders of magnitude larger than the
1125 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1126 pm_runtime_use_autosuspend(dev);
1127 pm_runtime_put(dev);
1129 ret = iio_device_register(indio_dev);
1131 goto out_runtime_pm_disable;
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);
1141 regulator_disable(data->vdda);
1143 regulator_disable(data->vddd);
1146 EXPORT_SYMBOL(bmp280_common_probe);
1148 int bmp280_common_remove(struct device *dev)
1150 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1151 struct bmp280_data *data = iio_priv(indio_dev);
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);
1161 EXPORT_SYMBOL(bmp280_common_remove);
1164 static int bmp280_runtime_suspend(struct device *dev)
1166 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1167 struct bmp280_data *data = iio_priv(indio_dev);
1170 ret = regulator_disable(data->vdda);
1173 return regulator_disable(data->vddd);
1176 static int bmp280_runtime_resume(struct device *dev)
1178 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1179 struct bmp280_data *data = iio_priv(indio_dev);
1182 ret = regulator_enable(data->vddd);
1185 ret = regulator_enable(data->vdda);
1188 usleep_range(data->start_up_time, data->start_up_time + 100);
1189 return data->chip_info->chip_config(data);
1191 #endif /* CONFIG_PM */
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)
1199 EXPORT_SYMBOL(bmp280_dev_pm_ops);
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");