2 * Copyright (c) 2014 Intel Corporation
4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
12 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
13 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
16 #define pr_fmt(fmt) "bmp280: " fmt
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/acpi.h>
21 #include <linux/regmap.h>
22 #include <linux/delay.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/sysfs.h>
26 /* BMP280 specific registers */
27 #define BMP280_REG_HUMIDITY_LSB 0xFE
28 #define BMP280_REG_HUMIDITY_MSB 0xFD
29 #define BMP280_REG_TEMP_XLSB 0xFC
30 #define BMP280_REG_TEMP_LSB 0xFB
31 #define BMP280_REG_TEMP_MSB 0xFA
32 #define BMP280_REG_PRESS_XLSB 0xF9
33 #define BMP280_REG_PRESS_LSB 0xF8
34 #define BMP280_REG_PRESS_MSB 0xF7
36 #define BMP280_REG_CONFIG 0xF5
37 #define BMP280_REG_CTRL_MEAS 0xF4
38 #define BMP280_REG_STATUS 0xF3
39 #define BMP280_REG_CTRL_HUMIDITY 0xF2
41 /* Due to non linear mapping, and data sizes we can't do a bulk read */
42 #define BMP280_REG_COMP_H1 0xA1
43 #define BMP280_REG_COMP_H2 0xE1
44 #define BMP280_REG_COMP_H3 0xE3
45 #define BMP280_REG_COMP_H4 0xE4
46 #define BMP280_REG_COMP_H5 0xE5
47 #define BMP280_REG_COMP_H6 0xE7
49 #define BMP280_REG_COMP_TEMP_START 0x88
50 #define BMP280_COMP_TEMP_REG_COUNT 6
52 #define BMP280_REG_COMP_PRESS_START 0x8E
53 #define BMP280_COMP_PRESS_REG_COUNT 18
55 #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
56 #define BMP280_FILTER_OFF 0
57 #define BMP280_FILTER_2X BIT(2)
58 #define BMP280_FILTER_4X BIT(3)
59 #define BMP280_FILTER_8X (BIT(3) | BIT(2))
60 #define BMP280_FILTER_16X BIT(4)
62 #define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
63 #define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
64 #define BMP280_OSRS_HUMIDITY_SKIP 0
65 #define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
66 #define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
67 #define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
68 #define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
69 #define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
71 #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
72 #define BMP280_OSRS_TEMP_SKIP 0
73 #define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
74 #define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
75 #define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
76 #define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
77 #define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
78 #define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
80 #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
81 #define BMP280_OSRS_PRESS_SKIP 0
82 #define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
83 #define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
84 #define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
85 #define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
86 #define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
87 #define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
89 #define BMP280_MODE_MASK (BIT(1) | BIT(0))
90 #define BMP280_MODE_SLEEP 0
91 #define BMP280_MODE_FORCED BIT(0)
92 #define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
94 /* BMP180 specific registers */
95 #define BMP180_REG_OUT_XLSB 0xF8
96 #define BMP180_REG_OUT_LSB 0xF7
97 #define BMP180_REG_OUT_MSB 0xF6
99 #define BMP180_REG_CALIB_START 0xAA
100 #define BMP180_REG_CALIB_COUNT 22
102 #define BMP180_MEAS_SCO BIT(5)
103 #define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
104 #define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
105 #define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
106 #define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
107 #define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
108 #define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
110 /* BMP180 and BMP280 common registers */
111 #define BMP280_REG_CTRL_MEAS 0xF4
112 #define BMP280_REG_RESET 0xE0
113 #define BMP280_REG_ID 0xD0
115 #define BMP180_CHIP_ID 0x55
116 #define BMP280_CHIP_ID 0x58
117 #define BME280_CHIP_ID 0x60
118 #define BMP280_SOFT_RESET_VAL 0xB6
121 struct i2c_client *client;
123 struct regmap *regmap;
124 const struct bmp280_chip_info *chip_info;
126 /* log of base 2 of oversampling rate */
127 u8 oversampling_press;
128 u8 oversampling_temp;
129 u8 oversampling_humid;
132 * Carryover value from temperature conversion, used in pressure
138 struct bmp280_chip_info {
139 const struct regmap_config *regmap_config;
141 const int *oversampling_temp_avail;
142 int num_oversampling_temp_avail;
144 const int *oversampling_press_avail;
145 int num_oversampling_press_avail;
147 const int *oversampling_humid_avail;
148 int num_oversampling_humid_avail;
150 int (*chip_config)(struct bmp280_data *);
151 int (*read_temp)(struct bmp280_data *, int *);
152 int (*read_press)(struct bmp280_data *, int *, int *);
153 int (*read_humid)(struct bmp280_data *, int *, int *);
157 * These enums are used for indexing into the array of compensation
158 * parameters for BMP280.
161 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
163 static const struct iio_chan_spec bmp280_channels[] = {
165 .type = IIO_PRESSURE,
166 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
167 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
171 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
172 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
175 .type = IIO_HUMIDITYRELATIVE,
176 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
177 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
181 static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
184 case BMP280_REG_CONFIG:
185 case BMP280_REG_CTRL_HUMIDITY:
186 case BMP280_REG_CTRL_MEAS:
187 case BMP280_REG_RESET:
194 static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
197 case BMP280_REG_HUMIDITY_LSB:
198 case BMP280_REG_HUMIDITY_MSB:
199 case BMP280_REG_TEMP_XLSB:
200 case BMP280_REG_TEMP_LSB:
201 case BMP280_REG_TEMP_MSB:
202 case BMP280_REG_PRESS_XLSB:
203 case BMP280_REG_PRESS_LSB:
204 case BMP280_REG_PRESS_MSB:
205 case BMP280_REG_STATUS:
212 static const struct regmap_config bmp280_regmap_config = {
216 .max_register = BMP280_REG_HUMIDITY_LSB,
217 .cache_type = REGCACHE_RBTREE,
219 .writeable_reg = bmp280_is_writeable_reg,
220 .volatile_reg = bmp280_is_volatile_reg,
224 * Returns humidity in percent, resolution is 0.01 percent. Output value of
225 * "47445" represents 47445/1024 = 46.333 %RH.
227 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
230 static u32 bmp280_compensate_humidity(struct bmp280_data *data,
233 struct device *dev = &data->client->dev;
234 unsigned int H1, H3, tmp;
235 int H2, H4, H5, H6, ret, var;
237 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
239 dev_err(dev, "failed to read H1 comp value\n");
243 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
245 dev_err(dev, "failed to read H2 comp value\n");
248 H2 = sign_extend32(le16_to_cpu(tmp), 15);
250 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
252 dev_err(dev, "failed to read H3 comp value\n");
256 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
258 dev_err(dev, "failed to read H4 comp value\n");
261 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
262 (be16_to_cpu(tmp) & 0xf), 11);
264 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
266 dev_err(dev, "failed to read H5 comp value\n");
269 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
271 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
273 dev_err(dev, "failed to read H6 comp value\n");
276 H6 = sign_extend32(tmp, 7);
278 var = ((s32)data->t_fine) - 76800;
279 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
280 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
281 + 2097152) * H2 + 8192) >> 14);
282 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
288 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
289 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
292 * Taken from datasheet, Section 3.11.3, "Compensation formula".
294 static s32 bmp280_compensate_temp(struct bmp280_data *data,
299 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
301 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
302 buf, BMP280_COMP_TEMP_REG_COUNT);
304 dev_err(&data->client->dev,
305 "failed to read temperature calibration parameters\n");
310 * The double casts are necessary because le16_to_cpu returns an
311 * unsigned 16-bit value. Casting that value directly to a
312 * signed 32-bit will not do proper sign extension.
314 * Conversely, T1 and P1 are unsigned values, so they can be
315 * cast straight to the larger type.
317 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
318 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
319 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
320 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
321 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
322 data->t_fine = var1 + var2;
324 return (data->t_fine * 5 + 128) >> 8;
328 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
329 * integer bits and 8 fractional bits). Output value of "24674867"
330 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
332 * Taken from datasheet, Section 3.11.3, "Compensation formula".
334 static u32 bmp280_compensate_press(struct bmp280_data *data,
339 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
341 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
342 buf, BMP280_COMP_PRESS_REG_COUNT);
344 dev_err(&data->client->dev,
345 "failed to read pressure calibration parameters\n");
349 var1 = ((s64)data->t_fine) - 128000;
350 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
351 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
352 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
353 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
354 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
355 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
360 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
361 p = div64_s64(p, var1);
362 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
363 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
364 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
369 static int bmp280_read_temp(struct bmp280_data *data,
374 s32 adc_temp, comp_temp;
376 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
379 dev_err(&data->client->dev, "failed to read temperature\n");
383 adc_temp = be32_to_cpu(tmp) >> 12;
384 comp_temp = bmp280_compensate_temp(data, adc_temp);
387 * val might be NULL if we're called by the read_press routine,
388 * who only cares about the carry over t_fine value.
391 *val = comp_temp * 10;
398 static int bmp280_read_press(struct bmp280_data *data,
406 /* Read and compensate temperature so we get a reading of t_fine. */
407 ret = bmp280_read_temp(data, NULL);
411 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
414 dev_err(&data->client->dev, "failed to read pressure\n");
418 adc_press = be32_to_cpu(tmp) >> 12;
419 comp_press = bmp280_compensate_press(data, adc_press);
424 return IIO_VAL_FRACTIONAL;
427 static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
434 /* Read and compensate temperature so we get a reading of t_fine. */
435 ret = bmp280_read_temp(data, NULL);
439 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
442 dev_err(&data->client->dev, "failed to read humidity\n");
446 adc_humidity = be16_to_cpu(tmp);
447 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
449 *val = comp_humidity;
452 return IIO_VAL_FRACTIONAL;
455 static int bmp280_read_raw(struct iio_dev *indio_dev,
456 struct iio_chan_spec const *chan,
457 int *val, int *val2, long mask)
460 struct bmp280_data *data = iio_priv(indio_dev);
462 mutex_lock(&data->lock);
465 case IIO_CHAN_INFO_PROCESSED:
466 switch (chan->type) {
467 case IIO_HUMIDITYRELATIVE:
468 ret = data->chip_info->read_humid(data, val, val2);
471 ret = data->chip_info->read_press(data, val, val2);
474 ret = data->chip_info->read_temp(data, val);
481 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
482 switch (chan->type) {
483 case IIO_HUMIDITYRELATIVE:
484 *val = 1 << data->oversampling_humid;
488 *val = 1 << data->oversampling_press;
492 *val = 1 << data->oversampling_temp;
505 mutex_unlock(&data->lock);
510 static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
514 const int *avail = data->chip_info->oversampling_humid_avail;
515 const int n = data->chip_info->num_oversampling_humid_avail;
517 for (i = 0; i < n; i++) {
518 if (avail[i] == val) {
519 data->oversampling_humid = ilog2(val);
521 return data->chip_info->chip_config(data);
527 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
531 const int *avail = data->chip_info->oversampling_temp_avail;
532 const int n = data->chip_info->num_oversampling_temp_avail;
534 for (i = 0; i < n; i++) {
535 if (avail[i] == val) {
536 data->oversampling_temp = ilog2(val);
538 return data->chip_info->chip_config(data);
544 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
548 const int *avail = data->chip_info->oversampling_press_avail;
549 const int n = data->chip_info->num_oversampling_press_avail;
551 for (i = 0; i < n; i++) {
552 if (avail[i] == val) {
553 data->oversampling_press = ilog2(val);
555 return data->chip_info->chip_config(data);
561 static int bmp280_write_raw(struct iio_dev *indio_dev,
562 struct iio_chan_spec const *chan,
563 int val, int val2, long mask)
566 struct bmp280_data *data = iio_priv(indio_dev);
569 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
570 mutex_lock(&data->lock);
571 switch (chan->type) {
572 case IIO_HUMIDITYRELATIVE:
573 ret = bmp280_write_oversampling_ratio_humid(data, val);
576 ret = bmp280_write_oversampling_ratio_press(data, val);
579 ret = bmp280_write_oversampling_ratio_temp(data, val);
585 mutex_unlock(&data->lock);
594 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
599 for (i = 0; i < n; i++)
600 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
607 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
608 struct device_attribute *attr, char *buf)
610 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
612 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
613 data->chip_info->num_oversampling_temp_avail);
616 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
621 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
622 data->chip_info->num_oversampling_press_avail);
625 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
626 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
628 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
629 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
631 static struct attribute *bmp280_attributes[] = {
632 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
633 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
637 static const struct attribute_group bmp280_attrs_group = {
638 .attrs = bmp280_attributes,
641 static const struct iio_info bmp280_info = {
642 .driver_module = THIS_MODULE,
643 .read_raw = &bmp280_read_raw,
644 .write_raw = &bmp280_write_raw,
645 .attrs = &bmp280_attrs_group,
648 static int bmp280_chip_config(struct bmp280_data *data)
651 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
652 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
654 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
655 BMP280_OSRS_TEMP_MASK |
656 BMP280_OSRS_PRESS_MASK |
658 osrs | BMP280_MODE_NORMAL);
660 dev_err(&data->client->dev,
661 "failed to write ctrl_meas register\n");
665 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
669 dev_err(&data->client->dev,
670 "failed to write config register\n");
677 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
679 static const struct bmp280_chip_info bmp280_chip_info = {
680 .regmap_config = &bmp280_regmap_config,
682 .oversampling_temp_avail = bmp280_oversampling_avail,
683 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
685 .oversampling_press_avail = bmp280_oversampling_avail,
686 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
688 .chip_config = bmp280_chip_config,
689 .read_temp = bmp280_read_temp,
690 .read_press = bmp280_read_press,
693 static int bme280_chip_config(struct bmp280_data *data)
695 int ret = bmp280_chip_config(data);
696 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
701 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
702 BMP280_OSRS_HUMIDITY_MASK, osrs);
705 static const struct bmp280_chip_info bme280_chip_info = {
706 .regmap_config = &bmp280_regmap_config,
708 .oversampling_temp_avail = bmp280_oversampling_avail,
709 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
711 .oversampling_press_avail = bmp280_oversampling_avail,
712 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
714 .oversampling_humid_avail = bmp280_oversampling_avail,
715 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
717 .chip_config = bme280_chip_config,
718 .read_temp = bmp280_read_temp,
719 .read_press = bmp280_read_press,
720 .read_humid = bmp280_read_humid,
724 static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
727 case BMP280_REG_CTRL_MEAS:
728 case BMP280_REG_RESET:
735 static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
738 case BMP180_REG_OUT_XLSB:
739 case BMP180_REG_OUT_LSB:
740 case BMP180_REG_OUT_MSB:
741 case BMP280_REG_CTRL_MEAS:
748 static const struct regmap_config bmp180_regmap_config = {
752 .max_register = BMP180_REG_OUT_XLSB,
753 .cache_type = REGCACHE_RBTREE,
755 .writeable_reg = bmp180_is_writeable_reg,
756 .volatile_reg = bmp180_is_volatile_reg,
759 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
762 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
763 unsigned int delay_us;
766 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
770 if (ctrl_meas == BMP180_MEAS_TEMP)
773 delay_us = conversion_time_max[data->oversampling_press];
775 usleep_range(delay_us, delay_us + 1000);
777 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
781 /* The value of this bit reset to "0" after conversion is complete */
782 if (ctrl & BMP180_MEAS_SCO)
788 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
793 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
797 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
801 *val = be16_to_cpu(tmp);
807 * These enums are used for indexing into the array of calibration
808 * coefficients for BMP180.
810 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
812 struct bmp180_calib {
826 static int bmp180_read_calib(struct bmp280_data *data,
827 struct bmp180_calib *calib)
831 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
833 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
839 /* None of the words has the value 0 or 0xFFFF */
840 for (i = 0; i < ARRAY_SIZE(buf); i++) {
841 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
845 calib->AC1 = be16_to_cpu(buf[AC1]);
846 calib->AC2 = be16_to_cpu(buf[AC2]);
847 calib->AC3 = be16_to_cpu(buf[AC3]);
848 calib->AC4 = be16_to_cpu(buf[AC4]);
849 calib->AC5 = be16_to_cpu(buf[AC5]);
850 calib->AC6 = be16_to_cpu(buf[AC6]);
851 calib->B1 = be16_to_cpu(buf[B1]);
852 calib->B2 = be16_to_cpu(buf[B2]);
853 calib->MB = be16_to_cpu(buf[MB]);
854 calib->MC = be16_to_cpu(buf[MC]);
855 calib->MD = be16_to_cpu(buf[MD]);
861 * Returns temperature in DegC, resolution is 0.1 DegC.
862 * t_fine carries fine temperature as global value.
864 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
866 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
870 struct bmp180_calib calib;
872 ret = bmp180_read_calib(data, &calib);
874 dev_err(&data->client->dev,
875 "failed to read calibration coefficients\n");
879 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
880 x2 = (calib.MC << 11) / (x1 + calib.MD);
881 data->t_fine = x1 + x2;
883 return (data->t_fine + 8) >> 4;
886 static int bmp180_read_temp(struct bmp280_data *data, int *val)
889 s32 adc_temp, comp_temp;
891 ret = bmp180_read_adc_temp(data, &adc_temp);
895 comp_temp = bmp180_compensate_temp(data, adc_temp);
898 * val might be NULL if we're called by the read_press routine,
899 * who only cares about the carry over t_fine value.
902 *val = comp_temp * 100;
909 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
913 u8 oss = data->oversampling_press;
915 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
919 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
923 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
929 * Returns pressure in Pa, resolution is 1 Pa.
931 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
933 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
939 s32 oss = data->oversampling_press;
940 struct bmp180_calib calib;
942 ret = bmp180_read_calib(data, &calib);
944 dev_err(&data->client->dev,
945 "failed to read calibration coefficients\n");
949 b6 = data->t_fine - 4000;
950 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
951 x2 = calib.AC2 * b6 >> 11;
953 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
954 x1 = calib.AC3 * b6 >> 13;
955 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
956 x3 = (x1 + x2 + 2) >> 2;
957 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
958 b7 = ((u32)adc_press - b3) * (50000 >> oss);
964 x1 = (p >> 8) * (p >> 8);
965 x1 = (x1 * 3038) >> 16;
966 x2 = (-7357 * p) >> 16;
968 return p + ((x1 + x2 + 3791) >> 4);
971 static int bmp180_read_press(struct bmp280_data *data,
978 /* Read and compensate temperature so we get a reading of t_fine. */
979 ret = bmp180_read_temp(data, NULL);
983 ret = bmp180_read_adc_press(data, &adc_press);
987 comp_press = bmp180_compensate_press(data, adc_press);
992 return IIO_VAL_FRACTIONAL;
995 static int bmp180_chip_config(struct bmp280_data *data)
1000 static const int bmp180_oversampling_temp_avail[] = { 1 };
1001 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1003 static const struct bmp280_chip_info bmp180_chip_info = {
1004 .regmap_config = &bmp180_regmap_config,
1006 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
1007 .num_oversampling_temp_avail =
1008 ARRAY_SIZE(bmp180_oversampling_temp_avail),
1010 .oversampling_press_avail = bmp180_oversampling_press_avail,
1011 .num_oversampling_press_avail =
1012 ARRAY_SIZE(bmp180_oversampling_press_avail),
1014 .chip_config = bmp180_chip_config,
1015 .read_temp = bmp180_read_temp,
1016 .read_press = bmp180_read_press,
1019 static int bmp280_probe(struct i2c_client *client,
1020 const struct i2c_device_id *id)
1023 struct iio_dev *indio_dev;
1024 struct bmp280_data *data;
1025 unsigned int chip_id;
1027 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1031 data = iio_priv(indio_dev);
1032 mutex_init(&data->lock);
1033 data->client = client;
1035 indio_dev->dev.parent = &client->dev;
1036 indio_dev->name = id->name;
1037 indio_dev->channels = bmp280_channels;
1038 indio_dev->info = &bmp280_info;
1039 indio_dev->modes = INDIO_DIRECT_MODE;
1041 switch (id->driver_data) {
1042 case BMP180_CHIP_ID:
1043 indio_dev->num_channels = 2;
1044 data->chip_info = &bmp180_chip_info;
1045 data->oversampling_press = ilog2(8);
1046 data->oversampling_temp = ilog2(1);
1048 case BMP280_CHIP_ID:
1049 indio_dev->num_channels = 2;
1050 data->chip_info = &bmp280_chip_info;
1051 data->oversampling_press = ilog2(16);
1052 data->oversampling_temp = ilog2(2);
1054 case BME280_CHIP_ID:
1055 indio_dev->num_channels = 3;
1056 data->chip_info = &bme280_chip_info;
1057 data->oversampling_press = ilog2(16);
1058 data->oversampling_humid = ilog2(16);
1059 data->oversampling_temp = ilog2(2);
1065 data->regmap = devm_regmap_init_i2c(client,
1066 data->chip_info->regmap_config);
1067 if (IS_ERR(data->regmap)) {
1068 dev_err(&client->dev, "failed to allocate register map\n");
1069 return PTR_ERR(data->regmap);
1072 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
1075 if (chip_id != id->driver_data) {
1076 dev_err(&client->dev, "bad chip id. expected %lx got %x\n",
1077 id->driver_data, chip_id);
1081 ret = data->chip_info->chip_config(data);
1085 return devm_iio_device_register(&client->dev, indio_dev);
1088 static const struct acpi_device_id bmp280_acpi_match[] = {
1089 {"BMP0280", BMP280_CHIP_ID },
1090 {"BMP0180", BMP180_CHIP_ID },
1091 {"BMP0085", BMP180_CHIP_ID },
1092 {"BME0280", BME280_CHIP_ID },
1095 MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
1097 static const struct i2c_device_id bmp280_id[] = {
1098 {"bmp280", BMP280_CHIP_ID },
1099 {"bmp180", BMP180_CHIP_ID },
1100 {"bmp085", BMP180_CHIP_ID },
1101 {"bme280", BME280_CHIP_ID },
1104 MODULE_DEVICE_TABLE(i2c, bmp280_id);
1106 static struct i2c_driver bmp280_driver = {
1109 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
1111 .probe = bmp280_probe,
1112 .id_table = bmp280_id,
1114 module_i2c_driver(bmp280_driver);
1116 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1117 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1118 MODULE_LICENSE("GPL v2");