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;
274 var
= clamp_val(var
, 0, 419430400);
280 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
281 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
284 * Taken from datasheet, Section 3.11.3, "Compensation formula".
286 static s32
bmp280_compensate_temp(struct bmp280_data
*data
,
290 struct bmp280_calib
*calib
= &data
->calib
.bmp280
;
292 var1
= (((adc_temp
>> 3) - ((s32
)calib
->T1
<< 1)) *
293 ((s32
)calib
->T2
)) >> 11;
294 var2
= (((((adc_temp
>> 4) - ((s32
)calib
->T1
)) *
295 ((adc_temp
>> 4) - ((s32
)calib
->T1
))) >> 12) *
296 ((s32
)calib
->T3
)) >> 14;
297 data
->t_fine
= var1
+ var2
;
299 return (data
->t_fine
* 5 + 128) >> 8;
303 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
304 * integer bits and 8 fractional bits). Output value of "24674867"
305 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
307 * Taken from datasheet, Section 3.11.3, "Compensation formula".
309 static u32
bmp280_compensate_press(struct bmp280_data
*data
,
313 struct bmp280_calib
*calib
= &data
->calib
.bmp280
;
315 var1
= ((s64
)data
->t_fine
) - 128000;
316 var2
= var1
* var1
* (s64
)calib
->P6
;
317 var2
+= (var1
* (s64
)calib
->P5
) << 17;
318 var2
+= ((s64
)calib
->P4
) << 35;
319 var1
= ((var1
* var1
* (s64
)calib
->P3
) >> 8) +
320 ((var1
* (s64
)calib
->P2
) << 12);
321 var1
= ((((s64
)1) << 47) + var1
) * ((s64
)calib
->P1
) >> 33;
326 p
= ((((s64
)1048576 - adc_press
) << 31) - var2
) * 3125;
327 p
= div64_s64(p
, var1
);
328 var1
= (((s64
)calib
->P9
) * (p
>> 13) * (p
>> 13)) >> 25;
329 var2
= ((s64
)(calib
->P8
) * p
) >> 19;
330 p
= ((p
+ var1
+ var2
) >> 8) + (((s64
)calib
->P7
) << 4);
335 static int bmp280_read_temp(struct bmp280_data
*data
,
340 s32 adc_temp
, comp_temp
;
342 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_TEMP_MSB
, &tmp
, 3);
344 dev_err(data
->dev
, "failed to read temperature\n");
348 adc_temp
= be32_to_cpu(tmp
) >> 12;
349 if (adc_temp
== BMP280_TEMP_SKIPPED
) {
350 /* reading was skipped */
351 dev_err(data
->dev
, "reading temperature skipped\n");
354 comp_temp
= bmp280_compensate_temp(data
, adc_temp
);
357 * val might be NULL if we're called by the read_press routine,
358 * who only cares about the carry over t_fine value.
361 *val
= comp_temp
* 10;
368 static int bmp280_read_press(struct bmp280_data
*data
,
376 /* Read and compensate temperature so we get a reading of t_fine. */
377 ret
= bmp280_read_temp(data
, NULL
);
381 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_PRESS_MSB
, &tmp
, 3);
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
, &tmp
, 2);
415 dev_err(data
->dev
, "failed to read humidity\n");
419 adc_humidity
= be16_to_cpu(tmp
);
420 if (adc_humidity
== BMP280_HUMIDITY_SKIPPED
) {
421 /* reading was skipped */
422 dev_err(data
->dev
, "reading humidity skipped\n");
425 comp_humidity
= bmp280_compensate_humidity(data
, adc_humidity
);
427 *val
= comp_humidity
* 1000 / 1024;
432 static int bmp280_read_raw(struct iio_dev
*indio_dev
,
433 struct iio_chan_spec
const *chan
,
434 int *val
, int *val2
, long mask
)
437 struct bmp280_data
*data
= iio_priv(indio_dev
);
439 pm_runtime_get_sync(data
->dev
);
440 mutex_lock(&data
->lock
);
443 case IIO_CHAN_INFO_PROCESSED
:
444 switch (chan
->type
) {
445 case IIO_HUMIDITYRELATIVE
:
446 ret
= data
->chip_info
->read_humid(data
, val
, val2
);
449 ret
= data
->chip_info
->read_press(data
, val
, val2
);
452 ret
= data
->chip_info
->read_temp(data
, val
);
459 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
460 switch (chan
->type
) {
461 case IIO_HUMIDITYRELATIVE
:
462 *val
= 1 << data
->oversampling_humid
;
466 *val
= 1 << data
->oversampling_press
;
470 *val
= 1 << data
->oversampling_temp
;
483 mutex_unlock(&data
->lock
);
484 pm_runtime_mark_last_busy(data
->dev
);
485 pm_runtime_put_autosuspend(data
->dev
);
490 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data
*data
,
494 const int *avail
= data
->chip_info
->oversampling_humid_avail
;
495 const int n
= data
->chip_info
->num_oversampling_humid_avail
;
497 for (i
= 0; i
< n
; i
++) {
498 if (avail
[i
] == val
) {
499 data
->oversampling_humid
= ilog2(val
);
501 return data
->chip_info
->chip_config(data
);
507 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data
*data
,
511 const int *avail
= data
->chip_info
->oversampling_temp_avail
;
512 const int n
= data
->chip_info
->num_oversampling_temp_avail
;
514 for (i
= 0; i
< n
; i
++) {
515 if (avail
[i
] == val
) {
516 data
->oversampling_temp
= ilog2(val
);
518 return data
->chip_info
->chip_config(data
);
524 static int bmp280_write_oversampling_ratio_press(struct bmp280_data
*data
,
528 const int *avail
= data
->chip_info
->oversampling_press_avail
;
529 const int n
= data
->chip_info
->num_oversampling_press_avail
;
531 for (i
= 0; i
< n
; i
++) {
532 if (avail
[i
] == val
) {
533 data
->oversampling_press
= ilog2(val
);
535 return data
->chip_info
->chip_config(data
);
541 static int bmp280_write_raw(struct iio_dev
*indio_dev
,
542 struct iio_chan_spec
const *chan
,
543 int val
, int val2
, long mask
)
546 struct bmp280_data
*data
= iio_priv(indio_dev
);
549 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
550 pm_runtime_get_sync(data
->dev
);
551 mutex_lock(&data
->lock
);
552 switch (chan
->type
) {
553 case IIO_HUMIDITYRELATIVE
:
554 ret
= bmp280_write_oversampling_ratio_humid(data
, val
);
557 ret
= bmp280_write_oversampling_ratio_press(data
, val
);
560 ret
= bmp280_write_oversampling_ratio_temp(data
, val
);
566 mutex_unlock(&data
->lock
);
567 pm_runtime_mark_last_busy(data
->dev
);
568 pm_runtime_put_autosuspend(data
->dev
);
577 static int bmp280_read_avail(struct iio_dev
*indio_dev
,
578 struct iio_chan_spec
const *chan
,
579 const int **vals
, int *type
, int *length
,
582 struct bmp280_data
*data
= iio_priv(indio_dev
);
585 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
586 switch (chan
->type
) {
588 *vals
= data
->chip_info
->oversampling_press_avail
;
589 *length
= data
->chip_info
->num_oversampling_press_avail
;
592 *vals
= data
->chip_info
->oversampling_temp_avail
;
593 *length
= data
->chip_info
->num_oversampling_temp_avail
;
599 return IIO_AVAIL_LIST
;
605 static const struct iio_info bmp280_info
= {
606 .read_raw
= &bmp280_read_raw
,
607 .read_avail
= &bmp280_read_avail
,
608 .write_raw
= &bmp280_write_raw
,
611 static int bmp280_chip_config(struct bmp280_data
*data
)
614 u8 osrs
= BMP280_OSRS_TEMP_X(data
->oversampling_temp
+ 1) |
615 BMP280_OSRS_PRESS_X(data
->oversampling_press
+ 1);
617 ret
= regmap_write_bits(data
->regmap
, BMP280_REG_CTRL_MEAS
,
618 BMP280_OSRS_TEMP_MASK
|
619 BMP280_OSRS_PRESS_MASK
|
621 osrs
| BMP280_MODE_NORMAL
);
624 "failed to write ctrl_meas register\n");
628 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CONFIG
,
633 "failed to write config register\n");
640 static const int bmp280_oversampling_avail
[] = { 1, 2, 4, 8, 16 };
642 static const struct bmp280_chip_info bmp280_chip_info
= {
643 .oversampling_temp_avail
= bmp280_oversampling_avail
,
644 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
646 .oversampling_press_avail
= bmp280_oversampling_avail
,
647 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
649 .chip_config
= bmp280_chip_config
,
650 .read_temp
= bmp280_read_temp
,
651 .read_press
= bmp280_read_press
,
654 static int bme280_chip_config(struct bmp280_data
*data
)
657 u8 osrs
= BMP280_OSRS_HUMIDITIY_X(data
->oversampling_humid
+ 1);
660 * Oversampling of humidity must be set before oversampling of
661 * temperature/pressure is set to become effective.
663 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CTRL_HUMIDITY
,
664 BMP280_OSRS_HUMIDITY_MASK
, osrs
);
669 return bmp280_chip_config(data
);
672 static const struct bmp280_chip_info bme280_chip_info
= {
673 .oversampling_temp_avail
= bmp280_oversampling_avail
,
674 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
676 .oversampling_press_avail
= bmp280_oversampling_avail
,
677 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
679 .oversampling_humid_avail
= bmp280_oversampling_avail
,
680 .num_oversampling_humid_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
682 .chip_config
= bme280_chip_config
,
683 .read_temp
= bmp280_read_temp
,
684 .read_press
= bmp280_read_press
,
685 .read_humid
= bmp280_read_humid
,
688 static int bmp180_measure(struct bmp280_data
*data
, u8 ctrl_meas
)
691 const int conversion_time_max
[] = { 4500, 7500, 13500, 25500 };
692 unsigned int delay_us
;
696 reinit_completion(&data
->done
);
698 ret
= regmap_write(data
->regmap
, BMP280_REG_CTRL_MEAS
, ctrl_meas
);
704 * If we have a completion interrupt, use it, wait up to
705 * 100ms. The longest conversion time listed is 76.5 ms for
706 * advanced resolution mode.
708 ret
= wait_for_completion_timeout(&data
->done
,
709 1 + msecs_to_jiffies(100));
711 dev_err(data
->dev
, "timeout waiting for completion\n");
713 if (ctrl_meas
== BMP180_MEAS_TEMP
)
717 conversion_time_max
[data
->oversampling_press
];
719 usleep_range(delay_us
, delay_us
+ 1000);
722 ret
= regmap_read(data
->regmap
, BMP280_REG_CTRL_MEAS
, &ctrl
);
726 /* The value of this bit reset to "0" after conversion is complete */
727 if (ctrl
& BMP180_MEAS_SCO
)
733 static int bmp180_read_adc_temp(struct bmp280_data
*data
, int *val
)
738 ret
= bmp180_measure(data
, BMP180_MEAS_TEMP
);
742 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, &tmp
, 2);
746 *val
= be16_to_cpu(tmp
);
751 static int bmp180_read_calib(struct bmp280_data
*data
,
752 struct bmp180_calib
*calib
)
756 __be16 buf
[BMP180_REG_CALIB_COUNT
/ 2];
758 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_CALIB_START
, buf
,
764 /* None of the words has the value 0 or 0xFFFF */
765 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++) {
766 if (buf
[i
] == cpu_to_be16(0) || buf
[i
] == cpu_to_be16(0xffff))
770 /* Toss the calibration data into the entropy pool */
771 add_device_randomness(buf
, sizeof(buf
));
773 calib
->AC1
= be16_to_cpu(buf
[AC1
]);
774 calib
->AC2
= be16_to_cpu(buf
[AC2
]);
775 calib
->AC3
= be16_to_cpu(buf
[AC3
]);
776 calib
->AC4
= be16_to_cpu(buf
[AC4
]);
777 calib
->AC5
= be16_to_cpu(buf
[AC5
]);
778 calib
->AC6
= be16_to_cpu(buf
[AC6
]);
779 calib
->B1
= be16_to_cpu(buf
[B1
]);
780 calib
->B2
= be16_to_cpu(buf
[B2
]);
781 calib
->MB
= be16_to_cpu(buf
[MB
]);
782 calib
->MC
= be16_to_cpu(buf
[MC
]);
783 calib
->MD
= be16_to_cpu(buf
[MD
]);
789 * Returns temperature in DegC, resolution is 0.1 DegC.
790 * t_fine carries fine temperature as global value.
792 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
794 static s32
bmp180_compensate_temp(struct bmp280_data
*data
, s32 adc_temp
)
797 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
799 x1
= ((adc_temp
- calib
->AC6
) * calib
->AC5
) >> 15;
800 x2
= (calib
->MC
<< 11) / (x1
+ calib
->MD
);
801 data
->t_fine
= x1
+ x2
;
803 return (data
->t_fine
+ 8) >> 4;
806 static int bmp180_read_temp(struct bmp280_data
*data
, int *val
)
809 s32 adc_temp
, comp_temp
;
811 ret
= bmp180_read_adc_temp(data
, &adc_temp
);
815 comp_temp
= bmp180_compensate_temp(data
, adc_temp
);
818 * val might be NULL if we're called by the read_press routine,
819 * who only cares about the carry over t_fine value.
822 *val
= comp_temp
* 100;
829 static int bmp180_read_adc_press(struct bmp280_data
*data
, int *val
)
833 u8 oss
= data
->oversampling_press
;
835 ret
= bmp180_measure(data
, BMP180_MEAS_PRESS_X(oss
));
839 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, &tmp
, 3);
843 *val
= (be32_to_cpu(tmp
) >> 8) >> (8 - oss
);
849 * Returns pressure in Pa, resolution is 1 Pa.
851 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
853 static u32
bmp180_compensate_press(struct bmp280_data
*data
, s32 adc_press
)
858 s32 oss
= data
->oversampling_press
;
859 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
861 b6
= data
->t_fine
- 4000;
862 x1
= (calib
->B2
* (b6
* b6
>> 12)) >> 11;
863 x2
= calib
->AC2
* b6
>> 11;
865 b3
= ((((s32
)calib
->AC1
* 4 + x3
) << oss
) + 2) / 4;
866 x1
= calib
->AC3
* b6
>> 13;
867 x2
= (calib
->B1
* ((b6
* b6
) >> 12)) >> 16;
868 x3
= (x1
+ x2
+ 2) >> 2;
869 b4
= calib
->AC4
* (u32
)(x3
+ 32768) >> 15;
870 b7
= ((u32
)adc_press
- b3
) * (50000 >> oss
);
876 x1
= (p
>> 8) * (p
>> 8);
877 x1
= (x1
* 3038) >> 16;
878 x2
= (-7357 * p
) >> 16;
880 return p
+ ((x1
+ x2
+ 3791) >> 4);
883 static int bmp180_read_press(struct bmp280_data
*data
,
890 /* Read and compensate temperature so we get a reading of t_fine. */
891 ret
= bmp180_read_temp(data
, NULL
);
895 ret
= bmp180_read_adc_press(data
, &adc_press
);
899 comp_press
= bmp180_compensate_press(data
, adc_press
);
904 return IIO_VAL_FRACTIONAL
;
907 static int bmp180_chip_config(struct bmp280_data
*data
)
912 static const int bmp180_oversampling_temp_avail
[] = { 1 };
913 static const int bmp180_oversampling_press_avail
[] = { 1, 2, 4, 8 };
915 static const struct bmp280_chip_info bmp180_chip_info
= {
916 .oversampling_temp_avail
= bmp180_oversampling_temp_avail
,
917 .num_oversampling_temp_avail
=
918 ARRAY_SIZE(bmp180_oversampling_temp_avail
),
920 .oversampling_press_avail
= bmp180_oversampling_press_avail
,
921 .num_oversampling_press_avail
=
922 ARRAY_SIZE(bmp180_oversampling_press_avail
),
924 .chip_config
= bmp180_chip_config
,
925 .read_temp
= bmp180_read_temp
,
926 .read_press
= bmp180_read_press
,
929 static irqreturn_t
bmp085_eoc_irq(int irq
, void *d
)
931 struct bmp280_data
*data
= d
;
933 complete(&data
->done
);
938 static int bmp085_fetch_eoc_irq(struct device
*dev
,
941 struct bmp280_data
*data
)
943 unsigned long irq_trig
;
946 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
947 if (irq_trig
!= IRQF_TRIGGER_RISING
) {
948 dev_err(dev
, "non-rising trigger given for EOC interrupt, trying to enforce it\n");
949 irq_trig
= IRQF_TRIGGER_RISING
;
952 init_completion(&data
->done
);
954 ret
= devm_request_threaded_irq(dev
,
962 /* Bail out without IRQ but keep the driver in place */
963 dev_err(dev
, "unable to request DRDY IRQ\n");
967 data
->use_eoc
= true;
971 static void bmp280_pm_disable(void *data
)
973 struct device
*dev
= data
;
975 pm_runtime_get_sync(dev
);
976 pm_runtime_put_noidle(dev
);
977 pm_runtime_disable(dev
);
980 static void bmp280_regulators_disable(void *data
)
982 struct regulator_bulk_data
*supplies
= data
;
984 regulator_bulk_disable(BMP280_NUM_SUPPLIES
, supplies
);
987 int bmp280_common_probe(struct device
*dev
,
988 struct regmap
*regmap
,
994 struct iio_dev
*indio_dev
;
995 struct bmp280_data
*data
;
996 unsigned int chip_id
;
997 struct gpio_desc
*gpiod
;
999 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1003 data
= iio_priv(indio_dev
);
1004 mutex_init(&data
->lock
);
1007 indio_dev
->name
= name
;
1008 indio_dev
->channels
= bmp280_channels
;
1009 indio_dev
->info
= &bmp280_info
;
1010 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1013 case BMP180_CHIP_ID
:
1014 indio_dev
->num_channels
= 2;
1015 data
->chip_info
= &bmp180_chip_info
;
1016 data
->oversampling_press
= ilog2(8);
1017 data
->oversampling_temp
= ilog2(1);
1018 data
->start_up_time
= 10000;
1020 case BMP280_CHIP_ID
:
1021 indio_dev
->num_channels
= 2;
1022 data
->chip_info
= &bmp280_chip_info
;
1023 data
->oversampling_press
= ilog2(16);
1024 data
->oversampling_temp
= ilog2(2);
1025 data
->start_up_time
= 2000;
1027 case BME280_CHIP_ID
:
1028 indio_dev
->num_channels
= 3;
1029 data
->chip_info
= &bme280_chip_info
;
1030 data
->oversampling_press
= ilog2(16);
1031 data
->oversampling_humid
= ilog2(16);
1032 data
->oversampling_temp
= ilog2(2);
1033 data
->start_up_time
= 2000;
1039 /* Bring up regulators */
1040 regulator_bulk_set_supply_names(data
->supplies
,
1041 bmp280_supply_names
,
1042 BMP280_NUM_SUPPLIES
);
1044 ret
= devm_regulator_bulk_get(dev
,
1045 BMP280_NUM_SUPPLIES
, data
->supplies
);
1047 dev_err(dev
, "failed to get regulators\n");
1051 ret
= regulator_bulk_enable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1053 dev_err(dev
, "failed to enable regulators\n");
1057 ret
= devm_add_action_or_reset(dev
, bmp280_regulators_disable
,
1062 /* Wait to make sure we started up properly */
1063 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1065 /* Bring chip out of reset if there is an assigned GPIO line */
1066 gpiod
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_HIGH
);
1067 /* Deassert the signal */
1069 dev_info(dev
, "release reset\n");
1070 gpiod_set_value(gpiod
, 0);
1073 data
->regmap
= regmap
;
1074 ret
= regmap_read(regmap
, BMP280_REG_ID
, &chip_id
);
1077 if (chip_id
!= chip
) {
1078 dev_err(dev
, "bad chip id: expected %x got %x\n",
1083 ret
= data
->chip_info
->chip_config(data
);
1087 dev_set_drvdata(dev
, indio_dev
);
1090 * Some chips have calibration parameters "programmed into the devices'
1091 * non-volatile memory during production". Let's read them out at probe
1092 * time once. They will not change.
1094 if (chip_id
== BMP180_CHIP_ID
) {
1095 ret
= bmp180_read_calib(data
, &data
->calib
.bmp180
);
1098 "failed to read calibration coefficients\n");
1101 } else if (chip_id
== BMP280_CHIP_ID
|| chip_id
== BME280_CHIP_ID
) {
1102 ret
= bmp280_read_calib(data
, &data
->calib
.bmp280
, chip_id
);
1105 "failed to read calibration coefficients\n");
1111 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1112 * however as it happens, the BMP085 shares the chip ID of BMP180
1113 * so we look for an IRQ if we have that.
1115 if (irq
> 0 || (chip_id
== BMP180_CHIP_ID
)) {
1116 ret
= bmp085_fetch_eoc_irq(dev
, name
, irq
, data
);
1121 /* Enable runtime PM */
1122 pm_runtime_get_noresume(dev
);
1123 pm_runtime_set_active(dev
);
1124 pm_runtime_enable(dev
);
1126 * Set autosuspend to two orders of magnitude larger than the
1129 pm_runtime_set_autosuspend_delay(dev
, data
->start_up_time
/ 10);
1130 pm_runtime_use_autosuspend(dev
);
1131 pm_runtime_put(dev
);
1133 ret
= devm_add_action_or_reset(dev
, bmp280_pm_disable
, dev
);
1137 return devm_iio_device_register(dev
, indio_dev
);
1139 EXPORT_SYMBOL(bmp280_common_probe
);
1142 static int bmp280_runtime_suspend(struct device
*dev
)
1144 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1145 struct bmp280_data
*data
= iio_priv(indio_dev
);
1147 return regulator_bulk_disable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1150 static int bmp280_runtime_resume(struct device
*dev
)
1152 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1153 struct bmp280_data
*data
= iio_priv(indio_dev
);
1156 ret
= regulator_bulk_enable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1159 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1160 return data
->chip_info
->chip_config(data
);
1162 #endif /* CONFIG_PM */
1164 const struct dev_pm_ops bmp280_dev_pm_ops
= {
1165 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1166 pm_runtime_force_resume
)
1167 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend
,
1168 bmp280_runtime_resume
, NULL
)
1170 EXPORT_SYMBOL(bmp280_dev_pm_ops
);
1172 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1173 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1174 MODULE_LICENSE("GPL v2");