2 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
3 * Copyright (c) 2012 Bosch Sensortec GmbH
4 * Copyright (c) 2012 Unixphere AB
5 * Copyright (c) 2014 Intel Corporation
6 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
8 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
16 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
17 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
20 #define pr_fmt(fmt) "bmp280: " fmt
22 #include <linux/device.h>
23 #include <linux/module.h>
24 #include <linux/regmap.h>
25 #include <linux/delay.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/regulator/consumer.h>
30 #include <linux/interrupt.h>
31 #include <linux/irq.h> /* For irq_get_irq_data() */
32 #include <linux/completion.h>
33 #include <linux/pm_runtime.h>
34 #include <linux/random.h>
39 * These enums are used for indexing into the array of calibration
40 * coefficients for BMP180.
42 enum { AC1
, AC2
, AC3
, AC4
, AC5
, AC6
, B1
, B2
, MB
, MC
, MD
};
58 /* See datasheet Section 4.2.2. */
83 struct regmap
*regmap
;
84 struct completion done
;
86 const struct bmp280_chip_info
*chip_info
;
88 struct bmp180_calib bmp180
;
89 struct bmp280_calib bmp280
;
91 struct regulator
*vddd
;
92 struct regulator
*vdda
;
93 unsigned int start_up_time
; /* in microseconds */
95 /* log of base 2 of oversampling rate */
96 u8 oversampling_press
;
98 u8 oversampling_humid
;
101 * Carryover value from temperature conversion, used in pressure
107 struct bmp280_chip_info
{
108 const int *oversampling_temp_avail
;
109 int num_oversampling_temp_avail
;
111 const int *oversampling_press_avail
;
112 int num_oversampling_press_avail
;
114 const int *oversampling_humid_avail
;
115 int num_oversampling_humid_avail
;
117 int (*chip_config
)(struct bmp280_data
*);
118 int (*read_temp
)(struct bmp280_data
*, int *);
119 int (*read_press
)(struct bmp280_data
*, int *, int *);
120 int (*read_humid
)(struct bmp280_data
*, int *, int *);
124 * These enums are used for indexing into the array of compensation
125 * parameters for BMP280.
128 enum { P1
, P2
, P3
, P4
, P5
, P6
, P7
, P8
, P9
};
130 static const struct iio_chan_spec bmp280_channels
[] = {
132 .type
= IIO_PRESSURE
,
133 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
134 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
138 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
139 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
142 .type
= IIO_HUMIDITYRELATIVE
,
143 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
144 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
148 static int bmp280_read_calib(struct bmp280_data
*data
,
149 struct bmp280_calib
*calib
,
154 struct device
*dev
= data
->dev
;
155 __le16 t_buf
[BMP280_COMP_TEMP_REG_COUNT
/ 2];
156 __le16 p_buf
[BMP280_COMP_PRESS_REG_COUNT
/ 2];
158 /* Read temperature calibration values. */
159 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_TEMP_START
,
160 t_buf
, BMP280_COMP_TEMP_REG_COUNT
);
163 "failed to read temperature calibration parameters\n");
167 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 calib
->P1
= le16_to_cpu(p_buf
[P1
]);
181 calib
->P2
= le16_to_cpu(p_buf
[P2
]);
182 calib
->P3
= le16_to_cpu(p_buf
[P3
]);
183 calib
->P4
= le16_to_cpu(p_buf
[P4
]);
184 calib
->P5
= le16_to_cpu(p_buf
[P5
]);
185 calib
->P6
= le16_to_cpu(p_buf
[P6
]);
186 calib
->P7
= le16_to_cpu(p_buf
[P7
]);
187 calib
->P8
= le16_to_cpu(p_buf
[P8
]);
188 calib
->P9
= le16_to_cpu(p_buf
[P9
]);
191 * Read humidity calibration values.
192 * Due to some odd register addressing we cannot just
193 * do a big bulk read. Instead, we have to read each Hx
194 * value separately and sometimes do some bit shifting...
195 * Humidity data is only available on BME280.
197 if (chip
!= BME280_CHIP_ID
)
200 ret
= regmap_read(data
->regmap
, BMP280_REG_COMP_H1
, &tmp
);
202 dev_err(dev
, "failed to read H1 comp value\n");
207 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_H2
, &tmp
, 2);
209 dev_err(dev
, "failed to read H2 comp value\n");
212 calib
->H2
= sign_extend32(le16_to_cpu(tmp
), 15);
214 ret
= regmap_read(data
->regmap
, BMP280_REG_COMP_H3
, &tmp
);
216 dev_err(dev
, "failed to read H3 comp value\n");
221 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_H4
, &tmp
, 2);
223 dev_err(dev
, "failed to read H4 comp value\n");
226 calib
->H4
= sign_extend32(((be16_to_cpu(tmp
) >> 4) & 0xff0) |
227 (be16_to_cpu(tmp
) & 0xf), 11);
229 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_H5
, &tmp
, 2);
231 dev_err(dev
, "failed to read H5 comp value\n");
234 calib
->H5
= sign_extend32(((le16_to_cpu(tmp
) >> 4) & 0xfff), 11);
236 ret
= regmap_read(data
->regmap
, BMP280_REG_COMP_H6
, &tmp
);
238 dev_err(dev
, "failed to read H6 comp value\n");
241 calib
->H6
= sign_extend32(tmp
, 7);
246 * Returns humidity in percent, resolution is 0.01 percent. Output value of
247 * "47445" represents 47445/1024 = 46.333 %RH.
249 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
251 static u32
bmp280_compensate_humidity(struct bmp280_data
*data
,
255 struct bmp280_calib
*calib
= &data
->calib
.bmp280
;
257 var
= ((s32
)data
->t_fine
) - (s32
)76800;
258 var
= ((((adc_humidity
<< 14) - (calib
->H4
<< 20) - (calib
->H5
* var
))
259 + (s32
)16384) >> 15) * (((((((var
* calib
->H6
) >> 10)
260 * (((var
* (s32
)calib
->H3
) >> 11) + (s32
)32768)) >> 10)
261 + (s32
)2097152) * calib
->H2
+ 8192) >> 14);
262 var
-= ((((var
>> 15) * (var
>> 15)) >> 7) * (s32
)calib
->H1
) >> 4;
268 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
269 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
272 * Taken from datasheet, Section 3.11.3, "Compensation formula".
274 static s32
bmp280_compensate_temp(struct bmp280_data
*data
,
278 struct bmp280_calib
*calib
= &data
->calib
.bmp280
;
280 var1
= (((adc_temp
>> 3) - ((s32
)calib
->T1
<< 1)) *
281 ((s32
)calib
->T2
)) >> 11;
282 var2
= (((((adc_temp
>> 4) - ((s32
)calib
->T1
)) *
283 ((adc_temp
>> 4) - ((s32
)calib
->T1
))) >> 12) *
284 ((s32
)calib
->T3
)) >> 14;
285 data
->t_fine
= var1
+ var2
;
287 return (data
->t_fine
* 5 + 128) >> 8;
291 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
292 * integer bits and 8 fractional bits). Output value of "24674867"
293 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
295 * Taken from datasheet, Section 3.11.3, "Compensation formula".
297 static u32
bmp280_compensate_press(struct bmp280_data
*data
,
301 struct bmp280_calib
*calib
= &data
->calib
.bmp280
;
303 var1
= ((s64
)data
->t_fine
) - 128000;
304 var2
= var1
* var1
* (s64
)calib
->P6
;
305 var2
+= (var1
* (s64
)calib
->P5
) << 17;
306 var2
+= ((s64
)calib
->P4
) << 35;
307 var1
= ((var1
* var1
* (s64
)calib
->P3
) >> 8) +
308 ((var1
* (s64
)calib
->P2
) << 12);
309 var1
= ((((s64
)1) << 47) + var1
) * ((s64
)calib
->P1
) >> 33;
314 p
= ((((s64
)1048576 - adc_press
) << 31) - var2
) * 3125;
315 p
= div64_s64(p
, var1
);
316 var1
= (((s64
)calib
->P9
) * (p
>> 13) * (p
>> 13)) >> 25;
317 var2
= ((s64
)(calib
->P8
) * p
) >> 19;
318 p
= ((p
+ var1
+ var2
) >> 8) + (((s64
)calib
->P7
) << 4);
323 static int bmp280_read_temp(struct bmp280_data
*data
,
328 s32 adc_temp
, comp_temp
;
330 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_TEMP_MSB
,
333 dev_err(data
->dev
, "failed to read temperature\n");
337 adc_temp
= be32_to_cpu(tmp
) >> 12;
338 if (adc_temp
== BMP280_TEMP_SKIPPED
) {
339 /* reading was skipped */
340 dev_err(data
->dev
, "reading temperature skipped\n");
343 comp_temp
= bmp280_compensate_temp(data
, adc_temp
);
346 * val might be NULL if we're called by the read_press routine,
347 * who only cares about the carry over t_fine value.
350 *val
= comp_temp
* 10;
357 static int bmp280_read_press(struct bmp280_data
*data
,
365 /* Read and compensate temperature so we get a reading of t_fine. */
366 ret
= bmp280_read_temp(data
, NULL
);
370 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_PRESS_MSB
,
373 dev_err(data
->dev
, "failed to read pressure\n");
377 adc_press
= be32_to_cpu(tmp
) >> 12;
378 if (adc_press
== BMP280_PRESS_SKIPPED
) {
379 /* reading was skipped */
380 dev_err(data
->dev
, "reading pressure skipped\n");
383 comp_press
= bmp280_compensate_press(data
, adc_press
);
388 return IIO_VAL_FRACTIONAL
;
391 static int bmp280_read_humid(struct bmp280_data
*data
, int *val
, int *val2
)
398 /* Read and compensate temperature so we get a reading of t_fine. */
399 ret
= bmp280_read_temp(data
, NULL
);
403 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_HUMIDITY_MSB
,
406 dev_err(data
->dev
, "failed to read humidity\n");
410 adc_humidity
= be16_to_cpu(tmp
);
411 if (adc_humidity
== BMP280_HUMIDITY_SKIPPED
) {
412 /* reading was skipped */
413 dev_err(data
->dev
, "reading humidity skipped\n");
416 comp_humidity
= bmp280_compensate_humidity(data
, adc_humidity
);
418 *val
= comp_humidity
;
421 return IIO_VAL_FRACTIONAL
;
424 static int bmp280_read_raw(struct iio_dev
*indio_dev
,
425 struct iio_chan_spec
const *chan
,
426 int *val
, int *val2
, long mask
)
429 struct bmp280_data
*data
= iio_priv(indio_dev
);
431 pm_runtime_get_sync(data
->dev
);
432 mutex_lock(&data
->lock
);
435 case IIO_CHAN_INFO_PROCESSED
:
436 switch (chan
->type
) {
437 case IIO_HUMIDITYRELATIVE
:
438 ret
= data
->chip_info
->read_humid(data
, val
, val2
);
441 ret
= data
->chip_info
->read_press(data
, val
, val2
);
444 ret
= data
->chip_info
->read_temp(data
, val
);
451 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
452 switch (chan
->type
) {
453 case IIO_HUMIDITYRELATIVE
:
454 *val
= 1 << data
->oversampling_humid
;
458 *val
= 1 << data
->oversampling_press
;
462 *val
= 1 << data
->oversampling_temp
;
475 mutex_unlock(&data
->lock
);
476 pm_runtime_mark_last_busy(data
->dev
);
477 pm_runtime_put_autosuspend(data
->dev
);
482 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data
*data
,
486 const int *avail
= data
->chip_info
->oversampling_humid_avail
;
487 const int n
= data
->chip_info
->num_oversampling_humid_avail
;
489 for (i
= 0; i
< n
; i
++) {
490 if (avail
[i
] == val
) {
491 data
->oversampling_humid
= ilog2(val
);
493 return data
->chip_info
->chip_config(data
);
499 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data
*data
,
503 const int *avail
= data
->chip_info
->oversampling_temp_avail
;
504 const int n
= data
->chip_info
->num_oversampling_temp_avail
;
506 for (i
= 0; i
< n
; i
++) {
507 if (avail
[i
] == val
) {
508 data
->oversampling_temp
= ilog2(val
);
510 return data
->chip_info
->chip_config(data
);
516 static int bmp280_write_oversampling_ratio_press(struct bmp280_data
*data
,
520 const int *avail
= data
->chip_info
->oversampling_press_avail
;
521 const int n
= data
->chip_info
->num_oversampling_press_avail
;
523 for (i
= 0; i
< n
; i
++) {
524 if (avail
[i
] == val
) {
525 data
->oversampling_press
= ilog2(val
);
527 return data
->chip_info
->chip_config(data
);
533 static int bmp280_write_raw(struct iio_dev
*indio_dev
,
534 struct iio_chan_spec
const *chan
,
535 int val
, int val2
, long mask
)
538 struct bmp280_data
*data
= iio_priv(indio_dev
);
541 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
542 pm_runtime_get_sync(data
->dev
);
543 mutex_lock(&data
->lock
);
544 switch (chan
->type
) {
545 case IIO_HUMIDITYRELATIVE
:
546 ret
= bmp280_write_oversampling_ratio_humid(data
, val
);
549 ret
= bmp280_write_oversampling_ratio_press(data
, val
);
552 ret
= bmp280_write_oversampling_ratio_temp(data
, val
);
558 mutex_unlock(&data
->lock
);
559 pm_runtime_mark_last_busy(data
->dev
);
560 pm_runtime_put_autosuspend(data
->dev
);
569 static ssize_t
bmp280_show_avail(char *buf
, const int *vals
, const int n
)
574 for (i
= 0; i
< n
; i
++)
575 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ", vals
[i
]);
582 static ssize_t
bmp280_show_temp_oversampling_avail(struct device
*dev
,
583 struct device_attribute
*attr
, char *buf
)
585 struct bmp280_data
*data
= iio_priv(dev_to_iio_dev(dev
));
587 return bmp280_show_avail(buf
, data
->chip_info
->oversampling_temp_avail
,
588 data
->chip_info
->num_oversampling_temp_avail
);
591 static ssize_t
bmp280_show_press_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_press_avail
,
597 data
->chip_info
->num_oversampling_press_avail
);
600 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available
,
601 S_IRUGO
, bmp280_show_temp_oversampling_avail
, NULL
, 0);
603 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available
,
604 S_IRUGO
, bmp280_show_press_oversampling_avail
, NULL
, 0);
606 static struct attribute
*bmp280_attributes
[] = {
607 &iio_dev_attr_in_temp_oversampling_ratio_available
.dev_attr
.attr
,
608 &iio_dev_attr_in_pressure_oversampling_ratio_available
.dev_attr
.attr
,
612 static const struct attribute_group bmp280_attrs_group
= {
613 .attrs
= bmp280_attributes
,
616 static const struct iio_info bmp280_info
= {
617 .read_raw
= &bmp280_read_raw
,
618 .write_raw
= &bmp280_write_raw
,
619 .attrs
= &bmp280_attrs_group
,
622 static int bmp280_chip_config(struct bmp280_data
*data
)
625 u8 osrs
= BMP280_OSRS_TEMP_X(data
->oversampling_temp
+ 1) |
626 BMP280_OSRS_PRESS_X(data
->oversampling_press
+ 1);
628 ret
= regmap_write_bits(data
->regmap
, BMP280_REG_CTRL_MEAS
,
629 BMP280_OSRS_TEMP_MASK
|
630 BMP280_OSRS_PRESS_MASK
|
632 osrs
| BMP280_MODE_NORMAL
);
635 "failed to write ctrl_meas register\n");
639 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CONFIG
,
644 "failed to write config register\n");
651 static const int bmp280_oversampling_avail
[] = { 1, 2, 4, 8, 16 };
653 static const struct bmp280_chip_info bmp280_chip_info
= {
654 .oversampling_temp_avail
= bmp280_oversampling_avail
,
655 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
657 .oversampling_press_avail
= bmp280_oversampling_avail
,
658 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
660 .chip_config
= bmp280_chip_config
,
661 .read_temp
= bmp280_read_temp
,
662 .read_press
= bmp280_read_press
,
665 static int bme280_chip_config(struct bmp280_data
*data
)
668 u8 osrs
= BMP280_OSRS_HUMIDITIY_X(data
->oversampling_humid
+ 1);
671 * Oversampling of humidity must be set before oversampling of
672 * temperature/pressure is set to become effective.
674 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CTRL_HUMIDITY
,
675 BMP280_OSRS_HUMIDITY_MASK
, osrs
);
680 return bmp280_chip_config(data
);
683 static const struct bmp280_chip_info bme280_chip_info
= {
684 .oversampling_temp_avail
= bmp280_oversampling_avail
,
685 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
687 .oversampling_press_avail
= bmp280_oversampling_avail
,
688 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
690 .oversampling_humid_avail
= bmp280_oversampling_avail
,
691 .num_oversampling_humid_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
693 .chip_config
= bme280_chip_config
,
694 .read_temp
= bmp280_read_temp
,
695 .read_press
= bmp280_read_press
,
696 .read_humid
= bmp280_read_humid
,
699 static int bmp180_measure(struct bmp280_data
*data
, u8 ctrl_meas
)
702 const int conversion_time_max
[] = { 4500, 7500, 13500, 25500 };
703 unsigned int delay_us
;
707 init_completion(&data
->done
);
709 ret
= regmap_write(data
->regmap
, BMP280_REG_CTRL_MEAS
, ctrl_meas
);
715 * If we have a completion interrupt, use it, wait up to
716 * 100ms. The longest conversion time listed is 76.5 ms for
717 * advanced resolution mode.
719 ret
= wait_for_completion_timeout(&data
->done
,
720 1 + msecs_to_jiffies(100));
722 dev_err(data
->dev
, "timeout waiting for completion\n");
724 if (ctrl_meas
== BMP180_MEAS_TEMP
)
728 conversion_time_max
[data
->oversampling_press
];
730 usleep_range(delay_us
, delay_us
+ 1000);
733 ret
= regmap_read(data
->regmap
, BMP280_REG_CTRL_MEAS
, &ctrl
);
737 /* The value of this bit reset to "0" after conversion is complete */
738 if (ctrl
& BMP180_MEAS_SCO
)
744 static int bmp180_read_adc_temp(struct bmp280_data
*data
, int *val
)
749 ret
= bmp180_measure(data
, BMP180_MEAS_TEMP
);
753 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 2);
757 *val
= be16_to_cpu(tmp
);
762 static int bmp180_read_calib(struct bmp280_data
*data
,
763 struct bmp180_calib
*calib
)
767 __be16 buf
[BMP180_REG_CALIB_COUNT
/ 2];
769 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_CALIB_START
, buf
,
775 /* None of the words has the value 0 or 0xFFFF */
776 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++) {
777 if (buf
[i
] == cpu_to_be16(0) || buf
[i
] == cpu_to_be16(0xffff))
781 /* Toss the calibration data into the entropy pool */
782 add_device_randomness(buf
, sizeof(buf
));
784 calib
->AC1
= be16_to_cpu(buf
[AC1
]);
785 calib
->AC2
= be16_to_cpu(buf
[AC2
]);
786 calib
->AC3
= be16_to_cpu(buf
[AC3
]);
787 calib
->AC4
= be16_to_cpu(buf
[AC4
]);
788 calib
->AC5
= be16_to_cpu(buf
[AC5
]);
789 calib
->AC6
= be16_to_cpu(buf
[AC6
]);
790 calib
->B1
= be16_to_cpu(buf
[B1
]);
791 calib
->B2
= be16_to_cpu(buf
[B2
]);
792 calib
->MB
= be16_to_cpu(buf
[MB
]);
793 calib
->MC
= be16_to_cpu(buf
[MC
]);
794 calib
->MD
= be16_to_cpu(buf
[MD
]);
800 * Returns temperature in DegC, resolution is 0.1 DegC.
801 * t_fine carries fine temperature as global value.
803 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
805 static s32
bmp180_compensate_temp(struct bmp280_data
*data
, s32 adc_temp
)
808 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
810 x1
= ((adc_temp
- calib
->AC6
) * calib
->AC5
) >> 15;
811 x2
= (calib
->MC
<< 11) / (x1
+ calib
->MD
);
812 data
->t_fine
= x1
+ x2
;
814 return (data
->t_fine
+ 8) >> 4;
817 static int bmp180_read_temp(struct bmp280_data
*data
, int *val
)
820 s32 adc_temp
, comp_temp
;
822 ret
= bmp180_read_adc_temp(data
, &adc_temp
);
826 comp_temp
= bmp180_compensate_temp(data
, adc_temp
);
829 * val might be NULL if we're called by the read_press routine,
830 * who only cares about the carry over t_fine value.
833 *val
= comp_temp
* 100;
840 static int bmp180_read_adc_press(struct bmp280_data
*data
, int *val
)
844 u8 oss
= data
->oversampling_press
;
846 ret
= bmp180_measure(data
, BMP180_MEAS_PRESS_X(oss
));
850 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 3);
854 *val
= (be32_to_cpu(tmp
) >> 8) >> (8 - oss
);
860 * Returns pressure in Pa, resolution is 1 Pa.
862 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
864 static u32
bmp180_compensate_press(struct bmp280_data
*data
, s32 adc_press
)
869 s32 oss
= data
->oversampling_press
;
870 struct bmp180_calib
*calib
= &data
->calib
.bmp180
;
872 b6
= data
->t_fine
- 4000;
873 x1
= (calib
->B2
* (b6
* b6
>> 12)) >> 11;
874 x2
= calib
->AC2
* b6
>> 11;
876 b3
= ((((s32
)calib
->AC1
* 4 + x3
) << oss
) + 2) / 4;
877 x1
= calib
->AC3
* b6
>> 13;
878 x2
= (calib
->B1
* ((b6
* b6
) >> 12)) >> 16;
879 x3
= (x1
+ x2
+ 2) >> 2;
880 b4
= calib
->AC4
* (u32
)(x3
+ 32768) >> 15;
881 b7
= ((u32
)adc_press
- b3
) * (50000 >> oss
);
887 x1
= (p
>> 8) * (p
>> 8);
888 x1
= (x1
* 3038) >> 16;
889 x2
= (-7357 * p
) >> 16;
891 return p
+ ((x1
+ x2
+ 3791) >> 4);
894 static int bmp180_read_press(struct bmp280_data
*data
,
901 /* Read and compensate temperature so we get a reading of t_fine. */
902 ret
= bmp180_read_temp(data
, NULL
);
906 ret
= bmp180_read_adc_press(data
, &adc_press
);
910 comp_press
= bmp180_compensate_press(data
, adc_press
);
915 return IIO_VAL_FRACTIONAL
;
918 static int bmp180_chip_config(struct bmp280_data
*data
)
923 static const int bmp180_oversampling_temp_avail
[] = { 1 };
924 static const int bmp180_oversampling_press_avail
[] = { 1, 2, 4, 8 };
926 static const struct bmp280_chip_info bmp180_chip_info
= {
927 .oversampling_temp_avail
= bmp180_oversampling_temp_avail
,
928 .num_oversampling_temp_avail
=
929 ARRAY_SIZE(bmp180_oversampling_temp_avail
),
931 .oversampling_press_avail
= bmp180_oversampling_press_avail
,
932 .num_oversampling_press_avail
=
933 ARRAY_SIZE(bmp180_oversampling_press_avail
),
935 .chip_config
= bmp180_chip_config
,
936 .read_temp
= bmp180_read_temp
,
937 .read_press
= bmp180_read_press
,
940 static irqreturn_t
bmp085_eoc_irq(int irq
, void *d
)
942 struct bmp280_data
*data
= d
;
944 complete(&data
->done
);
949 static int bmp085_fetch_eoc_irq(struct device
*dev
,
952 struct bmp280_data
*data
)
954 unsigned long irq_trig
;
957 irq_trig
= irqd_get_trigger_type(irq_get_irq_data(irq
));
958 if (irq_trig
!= IRQF_TRIGGER_RISING
) {
959 dev_err(dev
, "non-rising trigger given for EOC interrupt, "
960 "trying to enforce it\n");
961 irq_trig
= IRQF_TRIGGER_RISING
;
963 ret
= devm_request_threaded_irq(dev
,
971 /* Bail out without IRQ but keep the driver in place */
972 dev_err(dev
, "unable to request DRDY IRQ\n");
976 data
->use_eoc
= true;
980 int bmp280_common_probe(struct device
*dev
,
981 struct regmap
*regmap
,
987 struct iio_dev
*indio_dev
;
988 struct bmp280_data
*data
;
989 unsigned int chip_id
;
990 struct gpio_desc
*gpiod
;
992 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
996 data
= iio_priv(indio_dev
);
997 mutex_init(&data
->lock
);
1000 indio_dev
->dev
.parent
= dev
;
1001 indio_dev
->name
= name
;
1002 indio_dev
->channels
= bmp280_channels
;
1003 indio_dev
->info
= &bmp280_info
;
1004 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1007 case BMP180_CHIP_ID
:
1008 indio_dev
->num_channels
= 2;
1009 data
->chip_info
= &bmp180_chip_info
;
1010 data
->oversampling_press
= ilog2(8);
1011 data
->oversampling_temp
= ilog2(1);
1012 data
->start_up_time
= 10000;
1014 case BMP280_CHIP_ID
:
1015 indio_dev
->num_channels
= 2;
1016 data
->chip_info
= &bmp280_chip_info
;
1017 data
->oversampling_press
= ilog2(16);
1018 data
->oversampling_temp
= ilog2(2);
1019 data
->start_up_time
= 2000;
1021 case BME280_CHIP_ID
:
1022 indio_dev
->num_channels
= 3;
1023 data
->chip_info
= &bme280_chip_info
;
1024 data
->oversampling_press
= ilog2(16);
1025 data
->oversampling_humid
= ilog2(16);
1026 data
->oversampling_temp
= ilog2(2);
1027 data
->start_up_time
= 2000;
1033 /* Bring up regulators */
1034 data
->vddd
= devm_regulator_get(dev
, "vddd");
1035 if (IS_ERR(data
->vddd
)) {
1036 dev_err(dev
, "failed to get VDDD regulator\n");
1037 return PTR_ERR(data
->vddd
);
1039 ret
= regulator_enable(data
->vddd
);
1041 dev_err(dev
, "failed to enable VDDD regulator\n");
1044 data
->vdda
= devm_regulator_get(dev
, "vdda");
1045 if (IS_ERR(data
->vdda
)) {
1046 dev_err(dev
, "failed to get VDDA regulator\n");
1047 ret
= PTR_ERR(data
->vdda
);
1048 goto out_disable_vddd
;
1050 ret
= regulator_enable(data
->vdda
);
1052 dev_err(dev
, "failed to enable VDDA regulator\n");
1053 goto out_disable_vddd
;
1055 /* Wait to make sure we started up properly */
1056 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1058 /* Bring chip out of reset if there is an assigned GPIO line */
1059 gpiod
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_HIGH
);
1060 /* Deassert the signal */
1061 if (!IS_ERR(gpiod
)) {
1062 dev_info(dev
, "release reset\n");
1063 gpiod_set_value(gpiod
, 0);
1066 data
->regmap
= regmap
;
1067 ret
= regmap_read(regmap
, BMP280_REG_ID
, &chip_id
);
1069 goto out_disable_vdda
;
1070 if (chip_id
!= chip
) {
1071 dev_err(dev
, "bad chip id: expected %x got %x\n",
1074 goto out_disable_vdda
;
1077 ret
= data
->chip_info
->chip_config(data
);
1079 goto out_disable_vdda
;
1081 dev_set_drvdata(dev
, indio_dev
);
1084 * Some chips have calibration parameters "programmed into the devices'
1085 * non-volatile memory during production". Let's read them out at probe
1086 * time once. They will not change.
1088 if (chip_id
== BMP180_CHIP_ID
) {
1089 ret
= bmp180_read_calib(data
, &data
->calib
.bmp180
);
1092 "failed to read calibration coefficients\n");
1093 goto out_disable_vdda
;
1095 } else if (chip_id
== BMP280_CHIP_ID
|| chip_id
== BME280_CHIP_ID
) {
1096 ret
= bmp280_read_calib(data
, &data
->calib
.bmp280
, chip_id
);
1099 "failed to read calibration coefficients\n");
1100 goto out_disable_vdda
;
1105 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1106 * however as it happens, the BMP085 shares the chip ID of BMP180
1107 * so we look for an IRQ if we have that.
1109 if (irq
> 0 || (chip_id
== BMP180_CHIP_ID
)) {
1110 ret
= bmp085_fetch_eoc_irq(dev
, name
, irq
, data
);
1112 goto out_disable_vdda
;
1115 /* Enable runtime PM */
1116 pm_runtime_get_noresume(dev
);
1117 pm_runtime_set_active(dev
);
1118 pm_runtime_enable(dev
);
1120 * Set autosuspend to two orders of magnitude larger than the
1123 pm_runtime_set_autosuspend_delay(dev
, data
->start_up_time
/ 10);
1124 pm_runtime_use_autosuspend(dev
);
1125 pm_runtime_put(dev
);
1127 ret
= iio_device_register(indio_dev
);
1129 goto out_runtime_pm_disable
;
1134 out_runtime_pm_disable
:
1135 pm_runtime_get_sync(data
->dev
);
1136 pm_runtime_put_noidle(data
->dev
);
1137 pm_runtime_disable(data
->dev
);
1139 regulator_disable(data
->vdda
);
1141 regulator_disable(data
->vddd
);
1144 EXPORT_SYMBOL(bmp280_common_probe
);
1146 int bmp280_common_remove(struct device
*dev
)
1148 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1149 struct bmp280_data
*data
= iio_priv(indio_dev
);
1151 iio_device_unregister(indio_dev
);
1152 pm_runtime_get_sync(data
->dev
);
1153 pm_runtime_put_noidle(data
->dev
);
1154 pm_runtime_disable(data
->dev
);
1155 regulator_disable(data
->vdda
);
1156 regulator_disable(data
->vddd
);
1159 EXPORT_SYMBOL(bmp280_common_remove
);
1162 static int bmp280_runtime_suspend(struct device
*dev
)
1164 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
1165 struct bmp280_data
*data
= iio_priv(indio_dev
);
1168 ret
= regulator_disable(data
->vdda
);
1171 return regulator_disable(data
->vddd
);
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_enable(data
->vddd
);
1183 ret
= regulator_enable(data
->vdda
);
1186 usleep_range(data
->start_up_time
, data
->start_up_time
+ 100);
1187 return data
->chip_info
->chip_config(data
);
1189 #endif /* CONFIG_PM */
1191 const struct dev_pm_ops bmp280_dev_pm_ops
= {
1192 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
1193 pm_runtime_force_resume
)
1194 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend
,
1195 bmp280_runtime_resume
, NULL
)
1197 EXPORT_SYMBOL(bmp280_dev_pm_ops
);
1199 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1200 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1201 MODULE_LICENSE("GPL v2");