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. */
77 static const char *const bmp280_supply_names[] = {
81 #define BMP280_NUM_SUPPLIES ARRAY_SIZE(bmp280_supply_names)
86 struct regmap *regmap;
87 struct completion done;
89 const struct bmp280_chip_info *chip_info;
91 struct bmp180_calib bmp180;
92 struct bmp280_calib bmp280;
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;
100 u8 oversampling_humid;
103 * Carryover value from temperature conversion, used in pressure
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.
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),
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,
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);
167 "failed to read temperature calibration parameters\n");
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);
183 "failed to read pressure calibration parameters\n");
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)
210 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &tmp);
212 dev_err(dev, "failed to read H1 comp value\n");
217 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &l16, 2);
219 dev_err(dev, "failed to read H2 comp value\n");
222 calib->H2 = sign_extend32(le16_to_cpu(l16), 15);
224 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &tmp);
226 dev_err(dev, "failed to read H3 comp value\n");
231 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &b16, 2);
233 dev_err(dev, "failed to read H4 comp value\n");
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);
241 dev_err(dev, "failed to read H5 comp value\n");
244 calib->H5 = sign_extend32(((le16_to_cpu(l16) >> 4) & 0xfff), 11);
246 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
248 dev_err(dev, "failed to read H6 comp value\n");
251 calib->H6 = sign_extend32(tmp, 7);
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,
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;
278 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
279 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
282 * Taken from datasheet, Section 3.11.3, "Compensation formula".
284 static s32 bmp280_compensate_temp(struct bmp280_data *data,
288 struct bmp280_calib *calib = &data->calib.bmp280;
290 var1 = (((adc_temp >> 3) - ((s32)calib->T1 << 1)) *
291 ((s32)calib->T2)) >> 11;
292 var2 = (((((adc_temp >> 4) - ((s32)calib->T1)) *
293 ((adc_temp >> 4) - ((s32)calib->T1))) >> 12) *
294 ((s32)calib->T3)) >> 14;
295 data->t_fine = var1 + var2;
297 return (data->t_fine * 5 + 128) >> 8;
301 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
302 * integer bits and 8 fractional bits). Output value of "24674867"
303 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
305 * Taken from datasheet, Section 3.11.3, "Compensation formula".
307 static u32 bmp280_compensate_press(struct bmp280_data *data,
311 struct bmp280_calib *calib = &data->calib.bmp280;
313 var1 = ((s64)data->t_fine) - 128000;
314 var2 = var1 * var1 * (s64)calib->P6;
315 var2 += (var1 * (s64)calib->P5) << 17;
316 var2 += ((s64)calib->P4) << 35;
317 var1 = ((var1 * var1 * (s64)calib->P3) >> 8) +
318 ((var1 * (s64)calib->P2) << 12);
319 var1 = ((((s64)1) << 47) + var1) * ((s64)calib->P1) >> 33;
324 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
325 p = div64_s64(p, var1);
326 var1 = (((s64)calib->P9) * (p >> 13) * (p >> 13)) >> 25;
327 var2 = ((s64)(calib->P8) * p) >> 19;
328 p = ((p + var1 + var2) >> 8) + (((s64)calib->P7) << 4);
333 static int bmp280_read_temp(struct bmp280_data *data,
338 s32 adc_temp, comp_temp;
340 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
343 dev_err(data->dev, "failed to read temperature\n");
347 adc_temp = be32_to_cpu(tmp) >> 12;
348 if (adc_temp == BMP280_TEMP_SKIPPED) {
349 /* reading was skipped */
350 dev_err(data->dev, "reading temperature skipped\n");
353 comp_temp = bmp280_compensate_temp(data, adc_temp);
356 * val might be NULL if we're called by the read_press routine,
357 * who only cares about the carry over t_fine value.
360 *val = comp_temp * 10;
367 static int bmp280_read_press(struct bmp280_data *data,
375 /* Read and compensate temperature so we get a reading of t_fine. */
376 ret = bmp280_read_temp(data, NULL);
380 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
383 dev_err(data->dev, "failed to read pressure\n");
387 adc_press = be32_to_cpu(tmp) >> 12;
388 if (adc_press == BMP280_PRESS_SKIPPED) {
389 /* reading was skipped */
390 dev_err(data->dev, "reading pressure skipped\n");
393 comp_press = bmp280_compensate_press(data, adc_press);
398 return IIO_VAL_FRACTIONAL;
401 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
408 /* Read and compensate temperature so we get a reading of t_fine. */
409 ret = bmp280_read_temp(data, NULL);
413 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
416 dev_err(data->dev, "failed to read humidity\n");
420 adc_humidity = be16_to_cpu(tmp);
421 if (adc_humidity == BMP280_HUMIDITY_SKIPPED) {
422 /* reading was skipped */
423 dev_err(data->dev, "reading humidity skipped\n");
426 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
428 *val = comp_humidity * 1000 / 1024;
433 static int bmp280_read_raw(struct iio_dev *indio_dev,
434 struct iio_chan_spec const *chan,
435 int *val, int *val2, long mask)
438 struct bmp280_data *data = iio_priv(indio_dev);
440 pm_runtime_get_sync(data->dev);
441 mutex_lock(&data->lock);
444 case IIO_CHAN_INFO_PROCESSED:
445 switch (chan->type) {
446 case IIO_HUMIDITYRELATIVE:
447 ret = data->chip_info->read_humid(data, val, val2);
450 ret = data->chip_info->read_press(data, val, val2);
453 ret = data->chip_info->read_temp(data, val);
460 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
461 switch (chan->type) {
462 case IIO_HUMIDITYRELATIVE:
463 *val = 1 << data->oversampling_humid;
467 *val = 1 << data->oversampling_press;
471 *val = 1 << data->oversampling_temp;
484 mutex_unlock(&data->lock);
485 pm_runtime_mark_last_busy(data->dev);
486 pm_runtime_put_autosuspend(data->dev);
491 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
495 const int *avail = data->chip_info->oversampling_humid_avail;
496 const int n = data->chip_info->num_oversampling_humid_avail;
498 for (i = 0; i < n; i++) {
499 if (avail[i] == val) {
500 data->oversampling_humid = ilog2(val);
502 return data->chip_info->chip_config(data);
508 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
512 const int *avail = data->chip_info->oversampling_temp_avail;
513 const int n = data->chip_info->num_oversampling_temp_avail;
515 for (i = 0; i < n; i++) {
516 if (avail[i] == val) {
517 data->oversampling_temp = ilog2(val);
519 return data->chip_info->chip_config(data);
525 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
529 const int *avail = data->chip_info->oversampling_press_avail;
530 const int n = data->chip_info->num_oversampling_press_avail;
532 for (i = 0; i < n; i++) {
533 if (avail[i] == val) {
534 data->oversampling_press = ilog2(val);
536 return data->chip_info->chip_config(data);
542 static int bmp280_write_raw(struct iio_dev *indio_dev,
543 struct iio_chan_spec const *chan,
544 int val, int val2, long mask)
547 struct bmp280_data *data = iio_priv(indio_dev);
550 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
551 pm_runtime_get_sync(data->dev);
552 mutex_lock(&data->lock);
553 switch (chan->type) {
554 case IIO_HUMIDITYRELATIVE:
555 ret = bmp280_write_oversampling_ratio_humid(data, val);
558 ret = bmp280_write_oversampling_ratio_press(data, val);
561 ret = bmp280_write_oversampling_ratio_temp(data, val);
567 mutex_unlock(&data->lock);
568 pm_runtime_mark_last_busy(data->dev);
569 pm_runtime_put_autosuspend(data->dev);
578 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
583 for (i = 0; i < n; i++)
584 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
591 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
592 struct device_attribute *attr, char *buf)
594 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
596 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
597 data->chip_info->num_oversampling_temp_avail);
600 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
601 struct device_attribute *attr, char *buf)
603 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
605 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
606 data->chip_info->num_oversampling_press_avail);
609 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
610 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
612 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
613 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
615 static struct attribute *bmp280_attributes[] = {
616 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
617 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
621 static const struct attribute_group bmp280_attrs_group = {
622 .attrs = bmp280_attributes,
625 static const struct iio_info bmp280_info = {
626 .read_raw = &bmp280_read_raw,
627 .write_raw = &bmp280_write_raw,
628 .attrs = &bmp280_attrs_group,
631 static int bmp280_chip_config(struct bmp280_data *data)
634 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
635 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
637 ret = regmap_write_bits(data->regmap, BMP280_REG_CTRL_MEAS,
638 BMP280_OSRS_TEMP_MASK |
639 BMP280_OSRS_PRESS_MASK |
641 osrs | BMP280_MODE_NORMAL);
644 "failed to write ctrl_meas register\n");
648 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
653 "failed to write config register\n");
660 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
662 static const struct bmp280_chip_info bmp280_chip_info = {
663 .oversampling_temp_avail = bmp280_oversampling_avail,
664 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
666 .oversampling_press_avail = bmp280_oversampling_avail,
667 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
669 .chip_config = bmp280_chip_config,
670 .read_temp = bmp280_read_temp,
671 .read_press = bmp280_read_press,
674 static int bme280_chip_config(struct bmp280_data *data)
677 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
680 * Oversampling of humidity must be set before oversampling of
681 * temperature/pressure is set to become effective.
683 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
684 BMP280_OSRS_HUMIDITY_MASK, osrs);
689 return bmp280_chip_config(data);
692 static const struct bmp280_chip_info bme280_chip_info = {
693 .oversampling_temp_avail = bmp280_oversampling_avail,
694 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
696 .oversampling_press_avail = bmp280_oversampling_avail,
697 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
699 .oversampling_humid_avail = bmp280_oversampling_avail,
700 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
702 .chip_config = bme280_chip_config,
703 .read_temp = bmp280_read_temp,
704 .read_press = bmp280_read_press,
705 .read_humid = bmp280_read_humid,
708 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
711 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
712 unsigned int delay_us;
716 init_completion(&data->done);
718 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
724 * If we have a completion interrupt, use it, wait up to
725 * 100ms. The longest conversion time listed is 76.5 ms for
726 * advanced resolution mode.
728 ret = wait_for_completion_timeout(&data->done,
729 1 + msecs_to_jiffies(100));
731 dev_err(data->dev, "timeout waiting for completion\n");
733 if (ctrl_meas == BMP180_MEAS_TEMP)
737 conversion_time_max[data->oversampling_press];
739 usleep_range(delay_us, delay_us + 1000);
742 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
746 /* The value of this bit reset to "0" after conversion is complete */
747 if (ctrl & BMP180_MEAS_SCO)
753 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
758 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
762 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
766 *val = be16_to_cpu(tmp);
771 static int bmp180_read_calib(struct bmp280_data *data,
772 struct bmp180_calib *calib)
776 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
778 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
784 /* None of the words has the value 0 or 0xFFFF */
785 for (i = 0; i < ARRAY_SIZE(buf); i++) {
786 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
790 /* Toss the calibration data into the entropy pool */
791 add_device_randomness(buf, sizeof(buf));
793 calib->AC1 = be16_to_cpu(buf[AC1]);
794 calib->AC2 = be16_to_cpu(buf[AC2]);
795 calib->AC3 = be16_to_cpu(buf[AC3]);
796 calib->AC4 = be16_to_cpu(buf[AC4]);
797 calib->AC5 = be16_to_cpu(buf[AC5]);
798 calib->AC6 = be16_to_cpu(buf[AC6]);
799 calib->B1 = be16_to_cpu(buf[B1]);
800 calib->B2 = be16_to_cpu(buf[B2]);
801 calib->MB = be16_to_cpu(buf[MB]);
802 calib->MC = be16_to_cpu(buf[MC]);
803 calib->MD = be16_to_cpu(buf[MD]);
809 * Returns temperature in DegC, resolution is 0.1 DegC.
810 * t_fine carries fine temperature as global value.
812 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
814 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
817 struct bmp180_calib *calib = &data->calib.bmp180;
819 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
820 x2 = (calib->MC << 11) / (x1 + calib->MD);
821 data->t_fine = x1 + x2;
823 return (data->t_fine + 8) >> 4;
826 static int bmp180_read_temp(struct bmp280_data *data, int *val)
829 s32 adc_temp, comp_temp;
831 ret = bmp180_read_adc_temp(data, &adc_temp);
835 comp_temp = bmp180_compensate_temp(data, adc_temp);
838 * val might be NULL if we're called by the read_press routine,
839 * who only cares about the carry over t_fine value.
842 *val = comp_temp * 100;
849 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
853 u8 oss = data->oversampling_press;
855 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
859 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
863 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
869 * Returns pressure in Pa, resolution is 1 Pa.
871 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
873 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
878 s32 oss = data->oversampling_press;
879 struct bmp180_calib *calib = &data->calib.bmp180;
881 b6 = data->t_fine - 4000;
882 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
883 x2 = calib->AC2 * b6 >> 11;
885 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
886 x1 = calib->AC3 * b6 >> 13;
887 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
888 x3 = (x1 + x2 + 2) >> 2;
889 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
890 b7 = ((u32)adc_press - b3) * (50000 >> oss);
896 x1 = (p >> 8) * (p >> 8);
897 x1 = (x1 * 3038) >> 16;
898 x2 = (-7357 * p) >> 16;
900 return p + ((x1 + x2 + 3791) >> 4);
903 static int bmp180_read_press(struct bmp280_data *data,
910 /* Read and compensate temperature so we get a reading of t_fine. */
911 ret = bmp180_read_temp(data, NULL);
915 ret = bmp180_read_adc_press(data, &adc_press);
919 comp_press = bmp180_compensate_press(data, adc_press);
924 return IIO_VAL_FRACTIONAL;
927 static int bmp180_chip_config(struct bmp280_data *data)
932 static const int bmp180_oversampling_temp_avail[] = { 1 };
933 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
935 static const struct bmp280_chip_info bmp180_chip_info = {
936 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
937 .num_oversampling_temp_avail =
938 ARRAY_SIZE(bmp180_oversampling_temp_avail),
940 .oversampling_press_avail = bmp180_oversampling_press_avail,
941 .num_oversampling_press_avail =
942 ARRAY_SIZE(bmp180_oversampling_press_avail),
944 .chip_config = bmp180_chip_config,
945 .read_temp = bmp180_read_temp,
946 .read_press = bmp180_read_press,
949 static irqreturn_t bmp085_eoc_irq(int irq, void *d)
951 struct bmp280_data *data = d;
953 complete(&data->done);
958 static int bmp085_fetch_eoc_irq(struct device *dev,
961 struct bmp280_data *data)
963 unsigned long irq_trig;
966 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
967 if (irq_trig != IRQF_TRIGGER_RISING) {
968 dev_err(dev, "non-rising trigger given for EOC interrupt, "
969 "trying to enforce it\n");
970 irq_trig = IRQF_TRIGGER_RISING;
972 ret = devm_request_threaded_irq(dev,
980 /* Bail out without IRQ but keep the driver in place */
981 dev_err(dev, "unable to request DRDY IRQ\n");
985 data->use_eoc = true;
989 static void bmp280_pm_disable(void *data)
991 struct device *dev = data;
993 pm_runtime_get_sync(dev);
994 pm_runtime_put_noidle(dev);
995 pm_runtime_disable(dev);
998 static void bmp280_regulators_disable(void *data)
1000 struct regulator_bulk_data *supplies = data;
1002 regulator_bulk_disable(BMP280_NUM_SUPPLIES, supplies);
1005 int bmp280_common_probe(struct device *dev,
1006 struct regmap *regmap,
1012 struct iio_dev *indio_dev;
1013 struct bmp280_data *data;
1014 unsigned int chip_id;
1015 struct gpio_desc *gpiod;
1017 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1021 data = iio_priv(indio_dev);
1022 mutex_init(&data->lock);
1025 indio_dev->dev.parent = dev;
1026 indio_dev->name = name;
1027 indio_dev->channels = bmp280_channels;
1028 indio_dev->info = &bmp280_info;
1029 indio_dev->modes = INDIO_DIRECT_MODE;
1032 case BMP180_CHIP_ID:
1033 indio_dev->num_channels = 2;
1034 data->chip_info = &bmp180_chip_info;
1035 data->oversampling_press = ilog2(8);
1036 data->oversampling_temp = ilog2(1);
1037 data->start_up_time = 10000;
1039 case BMP280_CHIP_ID:
1040 indio_dev->num_channels = 2;
1041 data->chip_info = &bmp280_chip_info;
1042 data->oversampling_press = ilog2(16);
1043 data->oversampling_temp = ilog2(2);
1044 data->start_up_time = 2000;
1046 case BME280_CHIP_ID:
1047 indio_dev->num_channels = 3;
1048 data->chip_info = &bme280_chip_info;
1049 data->oversampling_press = ilog2(16);
1050 data->oversampling_humid = ilog2(16);
1051 data->oversampling_temp = ilog2(2);
1052 data->start_up_time = 2000;
1058 /* Bring up regulators */
1059 regulator_bulk_set_supply_names(data->supplies,
1060 bmp280_supply_names,
1061 BMP280_NUM_SUPPLIES);
1063 ret = devm_regulator_bulk_get(dev,
1064 BMP280_NUM_SUPPLIES, data->supplies);
1066 dev_err(dev, "failed to get regulators\n");
1070 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1072 dev_err(dev, "failed to enable regulators\n");
1076 ret = devm_add_action_or_reset(dev, bmp280_regulators_disable,
1081 /* Wait to make sure we started up properly */
1082 usleep_range(data->start_up_time, data->start_up_time + 100);
1084 /* Bring chip out of reset if there is an assigned GPIO line */
1085 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
1086 /* Deassert the signal */
1087 if (!IS_ERR(gpiod)) {
1088 dev_info(dev, "release reset\n");
1089 gpiod_set_value(gpiod, 0);
1092 data->regmap = regmap;
1093 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
1096 if (chip_id != chip) {
1097 dev_err(dev, "bad chip id: expected %x got %x\n",
1102 ret = data->chip_info->chip_config(data);
1106 dev_set_drvdata(dev, indio_dev);
1109 * Some chips have calibration parameters "programmed into the devices'
1110 * non-volatile memory during production". Let's read them out at probe
1111 * time once. They will not change.
1113 if (chip_id == BMP180_CHIP_ID) {
1114 ret = bmp180_read_calib(data, &data->calib.bmp180);
1117 "failed to read calibration coefficients\n");
1120 } else if (chip_id == BMP280_CHIP_ID || chip_id == BME280_CHIP_ID) {
1121 ret = bmp280_read_calib(data, &data->calib.bmp280, chip_id);
1124 "failed to read calibration coefficients\n");
1130 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1131 * however as it happens, the BMP085 shares the chip ID of BMP180
1132 * so we look for an IRQ if we have that.
1134 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1135 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1140 /* Enable runtime PM */
1141 pm_runtime_get_noresume(dev);
1142 pm_runtime_set_active(dev);
1143 pm_runtime_enable(dev);
1145 * Set autosuspend to two orders of magnitude larger than the
1148 pm_runtime_set_autosuspend_delay(dev, data->start_up_time / 10);
1149 pm_runtime_use_autosuspend(dev);
1150 pm_runtime_put(dev);
1152 ret = devm_add_action_or_reset(dev, bmp280_pm_disable, dev);
1156 return devm_iio_device_register(dev, indio_dev);
1158 EXPORT_SYMBOL(bmp280_common_probe);
1161 static int bmp280_runtime_suspend(struct device *dev)
1163 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1164 struct bmp280_data *data = iio_priv(indio_dev);
1166 return regulator_bulk_disable(BMP280_NUM_SUPPLIES, data->supplies);
1169 static int bmp280_runtime_resume(struct device *dev)
1171 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1172 struct bmp280_data *data = iio_priv(indio_dev);
1175 ret = regulator_bulk_enable(BMP280_NUM_SUPPLIES, data->supplies);
1178 usleep_range(data->start_up_time, data->start_up_time + 100);
1179 return data->chip_info->chip_config(data);
1181 #endif /* CONFIG_PM */
1183 const struct dev_pm_ops bmp280_dev_pm_ops = {
1184 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1185 pm_runtime_force_resume)
1186 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1187 bmp280_runtime_resume, NULL)
1189 EXPORT_SYMBOL(bmp280_dev_pm_ops);
1191 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1192 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1193 MODULE_LICENSE("GPL v2");