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
,
345 dev_err(data
->dev
, "failed to read temperature\n");
349 adc_temp
= be32_to_cpu(tmp
) >> 12;
350 if (adc_temp
== BMP280_TEMP_SKIPPED
) {
351 /* reading was skipped */
352 dev_err(data
->dev
, "reading temperature skipped\n");
355 comp_temp
= bmp280_compensate_temp(data
, adc_temp
);
358 * val might be NULL if we're called by the read_press routine,
359 * who only cares about the carry over t_fine value.
362 *val
= comp_temp
* 10;
369 static int bmp280_read_press(struct bmp280_data
*data
,
377 /* Read and compensate temperature so we get a reading of t_fine. */
378 ret
= bmp280_read_temp(data
, NULL
);
382 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_PRESS_MSB
,
385 dev_err(data
->dev
, "failed to read pressure\n");
389 adc_press
= be32_to_cpu(tmp
) >> 12;
390 if (adc_press
== BMP280_PRESS_SKIPPED
) {
391 /* reading was skipped */
392 dev_err(data
->dev
, "reading pressure skipped\n");
395 comp_press
= bmp280_compensate_press(data
, adc_press
);
400 return IIO_VAL_FRACTIONAL
;
403 static int bmp280_read_humid(struct bmp280_data
*data
, int *val
, int *val2
)
410 /* Read and compensate temperature so we get a reading of t_fine. */
411 ret
= bmp280_read_temp(data
, NULL
);
415 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_HUMIDITY_MSB
,
418 dev_err(data
->dev
, "failed to read humidity\n");
422 adc_humidity
= be16_to_cpu(tmp
);
423 if (adc_humidity
== BMP280_HUMIDITY_SKIPPED
) {
424 /* reading was skipped */
425 dev_err(data
->dev
, "reading humidity skipped\n");
428 comp_humidity
= bmp280_compensate_humidity(data
, adc_humidity
);
430 *val
= comp_humidity
* 1000 / 1024;
435 static int bmp280_read_raw(struct iio_dev
*indio_dev
,
436 struct iio_chan_spec
const *chan
,
437 int *val
, int *val2
, long mask
)
440 struct bmp280_data
*data
= iio_priv(indio_dev
);
442 pm_runtime_get_sync(data
->dev
);
443 mutex_lock(&data
->lock
);
446 case IIO_CHAN_INFO_PROCESSED
:
447 switch (chan
->type
) {
448 case IIO_HUMIDITYRELATIVE
:
449 ret
= data
->chip_info
->read_humid(data
, val
, val2
);
452 ret
= data
->chip_info
->read_press(data
, val
, val2
);
455 ret
= data
->chip_info
->read_temp(data
, val
);
462 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
463 switch (chan
->type
) {
464 case IIO_HUMIDITYRELATIVE
:
465 *val
= 1 << data
->oversampling_humid
;
469 *val
= 1 << data
->oversampling_press
;
473 *val
= 1 << data
->oversampling_temp
;
486 mutex_unlock(&data
->lock
);
487 pm_runtime_mark_last_busy(data
->dev
);
488 pm_runtime_put_autosuspend(data
->dev
);
493 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data
*data
,
497 const int *avail
= data
->chip_info
->oversampling_humid_avail
;
498 const int n
= data
->chip_info
->num_oversampling_humid_avail
;
500 for (i
= 0; i
< n
; i
++) {
501 if (avail
[i
] == val
) {
502 data
->oversampling_humid
= ilog2(val
);
504 return data
->chip_info
->chip_config(data
);
510 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data
*data
,
514 const int *avail
= data
->chip_info
->oversampling_temp_avail
;
515 const int n
= data
->chip_info
->num_oversampling_temp_avail
;
517 for (i
= 0; i
< n
; i
++) {
518 if (avail
[i
] == val
) {
519 data
->oversampling_temp
= ilog2(val
);
521 return data
->chip_info
->chip_config(data
);
527 static int bmp280_write_oversampling_ratio_press(struct bmp280_data
*data
,
531 const int *avail
= data
->chip_info
->oversampling_press_avail
;
532 const int n
= data
->chip_info
->num_oversampling_press_avail
;
534 for (i
= 0; i
< n
; i
++) {
535 if (avail
[i
] == val
) {
536 data
->oversampling_press
= ilog2(val
);
538 return data
->chip_info
->chip_config(data
);
544 static int bmp280_write_raw(struct iio_dev
*indio_dev
,
545 struct iio_chan_spec
const *chan
,
546 int val
, int val2
, long mask
)
549 struct bmp280_data
*data
= iio_priv(indio_dev
);
552 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
553 pm_runtime_get_sync(data
->dev
);
554 mutex_lock(&data
->lock
);
555 switch (chan
->type
) {
556 case IIO_HUMIDITYRELATIVE
:
557 ret
= bmp280_write_oversampling_ratio_humid(data
, val
);
560 ret
= bmp280_write_oversampling_ratio_press(data
, val
);
563 ret
= bmp280_write_oversampling_ratio_temp(data
, val
);
569 mutex_unlock(&data
->lock
);
570 pm_runtime_mark_last_busy(data
->dev
);
571 pm_runtime_put_autosuspend(data
->dev
);
580 static ssize_t
bmp280_show_avail(char *buf
, const int *vals
, const int n
)
585 for (i
= 0; i
< n
; i
++)
586 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ", vals
[i
]);
593 static ssize_t
bmp280_show_temp_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_temp_avail
,
599 data
->chip_info
->num_oversampling_temp_avail
);
602 static ssize_t
bmp280_show_press_oversampling_avail(struct device
*dev
,
603 struct device_attribute
*attr
, char *buf
)
605 struct bmp280_data
*data
= iio_priv(dev_to_iio_dev(dev
));
607 return bmp280_show_avail(buf
, data
->chip_info
->oversampling_press_avail
,
608 data
->chip_info
->num_oversampling_press_avail
);
611 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available
,
612 S_IRUGO
, bmp280_show_temp_oversampling_avail
, NULL
, 0);
614 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available
,
615 S_IRUGO
, bmp280_show_press_oversampling_avail
, NULL
, 0);
617 static struct attribute
*bmp280_attributes
[] = {
618 &iio_dev_attr_in_temp_oversampling_ratio_available
.dev_attr
.attr
,
619 &iio_dev_attr_in_pressure_oversampling_ratio_available
.dev_attr
.attr
,
623 static const struct attribute_group bmp280_attrs_group
= {
624 .attrs
= bmp280_attributes
,
627 static const struct iio_info bmp280_info
= {
628 .read_raw
= &bmp280_read_raw
,
629 .write_raw
= &bmp280_write_raw
,
630 .attrs
= &bmp280_attrs_group
,
633 static int bmp280_chip_config(struct bmp280_data
*data
)
636 u8 osrs
= BMP280_OSRS_TEMP_X(data
->oversampling_temp
+ 1) |
637 BMP280_OSRS_PRESS_X(data
->oversampling_press
+ 1);
639 ret
= regmap_write_bits(data
->regmap
, BMP280_REG_CTRL_MEAS
,
640 BMP280_OSRS_TEMP_MASK
|
641 BMP280_OSRS_PRESS_MASK
|
643 osrs
| BMP280_MODE_NORMAL
);
646 "failed to write ctrl_meas register\n");
650 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CONFIG
,
655 "failed to write config register\n");
662 static const int bmp280_oversampling_avail
[] = { 1, 2, 4, 8, 16 };
664 static const struct bmp280_chip_info bmp280_chip_info
= {
665 .oversampling_temp_avail
= bmp280_oversampling_avail
,
666 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
668 .oversampling_press_avail
= bmp280_oversampling_avail
,
669 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
671 .chip_config
= bmp280_chip_config
,
672 .read_temp
= bmp280_read_temp
,
673 .read_press
= bmp280_read_press
,
676 static int bme280_chip_config(struct bmp280_data
*data
)
679 u8 osrs
= BMP280_OSRS_HUMIDITIY_X(data
->oversampling_humid
+ 1);
682 * Oversampling of humidity must be set before oversampling of
683 * temperature/pressure is set to become effective.
685 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CTRL_HUMIDITY
,
686 BMP280_OSRS_HUMIDITY_MASK
, osrs
);
691 return bmp280_chip_config(data
);
694 static const struct bmp280_chip_info bme280_chip_info
= {
695 .oversampling_temp_avail
= bmp280_oversampling_avail
,
696 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
698 .oversampling_press_avail
= bmp280_oversampling_avail
,
699 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
701 .oversampling_humid_avail
= bmp280_oversampling_avail
,
702 .num_oversampling_humid_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
704 .chip_config
= bme280_chip_config
,
705 .read_temp
= bmp280_read_temp
,
706 .read_press
= bmp280_read_press
,
707 .read_humid
= bmp280_read_humid
,
710 static int bmp180_measure(struct bmp280_data
*data
, u8 ctrl_meas
)
713 const int conversion_time_max
[] = { 4500, 7500, 13500, 25500 };
714 unsigned int delay_us
;
718 reinit_completion(&data
->done
);
720 ret
= regmap_write(data
->regmap
, BMP280_REG_CTRL_MEAS
, ctrl_meas
);
726 * If we have a completion interrupt, use it, wait up to
727 * 100ms. The longest conversion time listed is 76.5 ms for
728 * advanced resolution mode.
730 ret
= wait_for_completion_timeout(&data
->done
,
731 1 + msecs_to_jiffies(100));
733 dev_err(data
->dev
, "timeout waiting for completion\n");
735 if (ctrl_meas
== BMP180_MEAS_TEMP
)
739 conversion_time_max
[data
->oversampling_press
];
741 usleep_range(delay_us
, delay_us
+ 1000);
744 ret
= regmap_read(data
->regmap
, BMP280_REG_CTRL_MEAS
, &ctrl
);
748 /* The value of this bit reset to "0" after conversion is complete */
749 if (ctrl
& BMP180_MEAS_SCO
)
755 static int bmp180_read_adc_temp(struct bmp280_data
*data
, int *val
)
760 ret
= bmp180_measure(data
, BMP180_MEAS_TEMP
);
764 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 2);
768 *val
= be16_to_cpu(tmp
);
773 static int bmp180_read_calib(struct bmp280_data
*data
,
774 struct bmp180_calib
*calib
)
778 __be16 buf
[BMP180_REG_CALIB_COUNT
/ 2];
780 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_CALIB_START
, buf
,
786 /* None of the words has the value 0 or 0xFFFF */
787 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++) {
788 if (buf
[i
] == cpu_to_be16(0) || buf
[i
] == cpu_to_be16(0xffff))
792 /* Toss the calibration data into the entropy pool */
793 add_device_randomness(buf
, sizeof(buf
));
795 calib
->AC1
= be16_to_cpu(buf
[AC1
]);
796 calib
->AC2
= be16_to_cpu(buf
[AC2
]);
797 calib
->AC3
= be16_to_cpu(buf
[AC3
]);
798 calib
->AC4
= be16_to_cpu(buf
[AC4
]);
799 calib
->AC5
= be16_to_cpu(buf
[AC5
]);
800 calib
->AC6
= be16_to_cpu(buf
[AC6
]);
801 calib
->B1
= be16_to_cpu(buf
[B1
]);
802 calib
->B2
= be16_to_cpu(buf
[B2
]);
803 calib
->MB
= be16_to_cpu(buf
[MB
]);
804 calib
->MC
= be16_to_cpu(buf
[MC
]);
805 calib
->MD
= be16_to_cpu(buf
[MD
]);
811 * Returns temperature in DegC, resolution is 0.1 DegC.
812 * t_fine carries fine temperature as global value.
814 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
816 static s32
bmp180_compensate_temp(struct bmp280_data
*data
, s32 adc_temp
)
819 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
821 x1
= ((adc_temp
- calib
->AC6
) * calib
->AC5
) >> 15;
822 x2
= (calib
->MC
<< 11) / (x1
+ calib
->MD
);
823 data
->t_fine
= x1
+ x2
;
825 return (data
->t_fine
+ 8) >> 4;
828 static int bmp180_read_temp(struct bmp280_data
*data
, int *val
)
831 s32 adc_temp
, comp_temp
;
833 ret
= bmp180_read_adc_temp(data
, &adc_temp
);
837 comp_temp
= bmp180_compensate_temp(data
, adc_temp
);
840 * val might be NULL if we're called by the read_press routine,
841 * who only cares about the carry over t_fine value.
844 *val
= comp_temp
* 100;
851 static int bmp180_read_adc_press(struct bmp280_data
*data
, int *val
)
855 u8 oss
= data
->oversampling_press
;
857 ret
= bmp180_measure(data
, BMP180_MEAS_PRESS_X(oss
));
861 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 3);
865 *val
= (be32_to_cpu(tmp
) >> 8) >> (8 - oss
);
871 * Returns pressure in Pa, resolution is 1 Pa.
873 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
875 static u32
bmp180_compensate_press(struct bmp280_data
*data
, s32 adc_press
)
880 s32 oss
= data
->oversampling_press
;
881 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
883 b6
= data
->t_fine
- 4000;
884 x1
= (calib
->B2
* (b6
* b6
>> 12)) >> 11;
885 x2
= calib
->AC2
* b6
>> 11;
887 b3
= ((((s32
)calib
->AC1
* 4 + x3
) << oss
) + 2) / 4;
888 x1
= calib
->AC3
* b6
>> 13;
889 x2
= (calib
->B1
* ((b6
* b6
) >> 12)) >> 16;
890 x3
= (x1
+ x2
+ 2) >> 2;
891 b4
= calib
->AC4
* (u32
)(x3
+ 32768) >> 15;
892 b7
= ((u32
)adc_press
- b3
) * (50000 >> oss
);
898 x1
= (p
>> 8) * (p
>> 8);
899 x1
= (x1
* 3038) >> 16;
900 x2
= (-7357 * p
) >> 16;
902 return p
+ ((x1
+ x2
+ 3791) >> 4);
905 static int bmp180_read_press(struct bmp280_data
*data
,
912 /* Read and compensate temperature so we get a reading of t_fine. */
913 ret
= bmp180_read_temp(data
, NULL
);
917 ret
= bmp180_read_adc_press(data
, &adc_press
);
921 comp_press
= bmp180_compensate_press(data
, adc_press
);
926 return IIO_VAL_FRACTIONAL
;
929 static int bmp180_chip_config(struct bmp280_data
*data
)
934 static const int bmp180_oversampling_temp_avail
[] = { 1 };
935 static const int bmp180_oversampling_press_avail
[] = { 1, 2, 4, 8 };
937 static const struct bmp280_chip_info bmp180_chip_info
= {
938 .oversampling_temp_avail
= bmp180_oversampling_temp_avail
,
939 .num_oversampling_temp_avail
=
940 ARRAY_SIZE(bmp180_oversampling_temp_avail
),
942 .oversampling_press_avail
= bmp180_oversampling_press_avail
,
943 .num_oversampling_press_avail
=
944 ARRAY_SIZE(bmp180_oversampling_press_avail
),
946 .chip_config
= bmp180_chip_config
,
947 .read_temp
= bmp180_read_temp
,
948 .read_press
= bmp180_read_press
,
951 static irqreturn_t
bmp085_eoc_irq(int irq
, void *d
)
953 struct bmp280_data
*data
= d
;
955 complete(&data
->done
);
960 static int bmp085_fetch_eoc_irq(struct device
*dev
,
963 struct bmp280_data
*data
)
965 unsigned long irq_trig
;
968 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
969 if (irq_trig
!= IRQF_TRIGGER_RISING
) {
970 dev_err(dev
, "non-rising trigger given for EOC interrupt, "
971 "trying to enforce it\n");
972 irq_trig
= IRQF_TRIGGER_RISING
;
975 init_completion(&data
->done
);
977 ret
= devm_request_threaded_irq(dev
,
985 /* Bail out without IRQ but keep the driver in place */
986 dev_err(dev
, "unable to request DRDY IRQ\n");
990 data
->use_eoc
= true;
994 static void bmp280_pm_disable(void *data
)
996 struct device
*dev
= data
;
998 pm_runtime_get_sync(dev
);
999 pm_runtime_put_noidle(dev
);
1000 pm_runtime_disable(dev
);
1003 static void bmp280_regulators_disable(void *data
)
1005 struct regulator_bulk_data
*supplies
= data
;
1007 regulator_bulk_disable(BMP280_NUM_SUPPLIES
, supplies
);
1010 int bmp280_common_probe(struct device
*dev
,
1011 struct regmap
*regmap
,
1017 struct iio_dev
*indio_dev
;
1018 struct bmp280_data
*data
;
1019 unsigned int chip_id
;
1020 struct gpio_desc
*gpiod
;
1022 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1026 data
= iio_priv(indio_dev
);
1027 mutex_init(&data
->lock
);
1030 indio_dev
->dev
.parent
= dev
;
1031 indio_dev
->name
= name
;
1032 indio_dev
->channels
= bmp280_channels
;
1033 indio_dev
->info
= &bmp280_info
;
1034 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1037 case BMP180_CHIP_ID
:
1038 indio_dev
->num_channels
= 2;
1039 data
->chip_info
= &bmp180_chip_info
;
1040 data
->oversampling_press
= ilog2(8);
1041 data
->oversampling_temp
= ilog2(1);
1042 data
->start_up_time
= 10000;
1044 case BMP280_CHIP_ID
:
1045 indio_dev
->num_channels
= 2;
1046 data
->chip_info
= &bmp280_chip_info
;
1047 data
->oversampling_press
= ilog2(16);
1048 data
->oversampling_temp
= ilog2(2);
1049 data
->start_up_time
= 2000;
1051 case BME280_CHIP_ID
:
1052 indio_dev
->num_channels
= 3;
1053 data
->chip_info
= &bme280_chip_info
;
1054 data
->oversampling_press
= ilog2(16);
1055 data
->oversampling_humid
= ilog2(16);
1056 data
->oversampling_temp
= ilog2(2);
1057 data
->start_up_time
= 2000;
1063 /* Bring up regulators */
1064 regulator_bulk_set_supply_names(data
->supplies
,
1065 bmp280_supply_names
,
1066 BMP280_NUM_SUPPLIES
);
1068 ret
= devm_regulator_bulk_get(dev
,
1069 BMP280_NUM_SUPPLIES
, data
->supplies
);
1071 dev_err(dev
, "failed to get regulators\n");
1075 ret
= regulator_bulk_enable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1077 dev_err(dev
, "failed to enable regulators\n");
1081 ret
= devm_add_action_or_reset(dev
, bmp280_regulators_disable
,
1086 /* Wait to make sure we started up properly */
1087 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1089 /* Bring chip out of reset if there is an assigned GPIO line */
1090 gpiod
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1091 /* Deassert the signal */
1092 if (!IS_ERR(gpiod
)) {
1093 dev_info(dev
, "release reset\n");
1094 gpiod_set_value(gpiod
, 0);
1097 data
->regmap
= regmap
;
1098 ret
= regmap_read(regmap
, BMP280_REG_ID
, &chip_id
);
1101 if (chip_id
!= chip
) {
1102 dev_err(dev
, "bad chip id: expected %x got %x\n",
1107 ret
= data
->chip_info
->chip_config(data
);
1111 dev_set_drvdata(dev
, indio_dev
);
1114 * Some chips have calibration parameters "programmed into the devices'
1115 * non-volatile memory during production". Let's read them out at probe
1116 * time once. They will not change.
1118 if (chip_id
== BMP180_CHIP_ID
) {
1119 ret
= bmp180_read_calib(data
, &data
->calib
.bmp180
);
1122 "failed to read calibration coefficients\n");
1125 } else if (chip_id
== BMP280_CHIP_ID
|| chip_id
== BME280_CHIP_ID
) {
1126 ret
= bmp280_read_calib(data
, &data
->calib
.bmp280
, chip_id
);
1129 "failed to read calibration coefficients\n");
1135 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1136 * however as it happens, the BMP085 shares the chip ID of BMP180
1137 * so we look for an IRQ if we have that.
1139 if (irq
> 0 || (chip_id
== BMP180_CHIP_ID
)) {
1140 ret
= bmp085_fetch_eoc_irq(dev
, name
, irq
, data
);
1145 /* Enable runtime PM */
1146 pm_runtime_get_noresume(dev
);
1147 pm_runtime_set_active(dev
);
1148 pm_runtime_enable(dev
);
1150 * Set autosuspend to two orders of magnitude larger than the
1153 pm_runtime_set_autosuspend_delay(dev
, data
->start_up_time
/ 10);
1154 pm_runtime_use_autosuspend(dev
);
1155 pm_runtime_put(dev
);
1157 ret
= devm_add_action_or_reset(dev
, bmp280_pm_disable
, dev
);
1161 return devm_iio_device_register(dev
, indio_dev
);
1163 EXPORT_SYMBOL(bmp280_common_probe
);
1166 static int bmp280_runtime_suspend(struct device
*dev
)
1168 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1169 struct bmp280_data
*data
= iio_priv(indio_dev
);
1171 return regulator_bulk_disable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1174 static int bmp280_runtime_resume(struct device
*dev
)
1176 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1177 struct bmp280_data
*data
= iio_priv(indio_dev
);
1180 ret
= regulator_bulk_enable(BMP280_NUM_SUPPLIES
, data
->supplies
);
1183 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1184 return data
->chip_info
->chip_config(data
);
1186 #endif /* CONFIG_PM */
1188 const struct dev_pm_ops bmp280_dev_pm_ops
= {
1189 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1190 pm_runtime_force_resume
)
1191 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend
,
1192 bmp280_runtime_resume
, NULL
)
1194 EXPORT_SYMBOL(bmp280_dev_pm_ops
);
1196 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1197 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1198 MODULE_LICENSE("GPL v2");