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
15 #define pr_fmt(fmt) "bmp280: " fmt
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/acpi.h>
20 #include <linux/regmap.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 /* BMP280 specific registers */
26 #define BMP280_REG_TEMP_XLSB 0xFC
27 #define BMP280_REG_TEMP_LSB 0xFB
28 #define BMP280_REG_TEMP_MSB 0xFA
29 #define BMP280_REG_PRESS_XLSB 0xF9
30 #define BMP280_REG_PRESS_LSB 0xF8
31 #define BMP280_REG_PRESS_MSB 0xF7
33 #define BMP280_REG_CONFIG 0xF5
34 #define BMP280_REG_STATUS 0xF3
36 #define BMP280_REG_COMP_TEMP_START 0x88
37 #define BMP280_COMP_TEMP_REG_COUNT 6
39 #define BMP280_REG_COMP_PRESS_START 0x8E
40 #define BMP280_COMP_PRESS_REG_COUNT 18
42 #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
43 #define BMP280_FILTER_OFF 0
44 #define BMP280_FILTER_2X BIT(2)
45 #define BMP280_FILTER_4X BIT(3)
46 #define BMP280_FILTER_8X (BIT(3) | BIT(2))
47 #define BMP280_FILTER_16X BIT(4)
49 #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
50 #define BMP280_OSRS_TEMP_SKIP 0
51 #define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
52 #define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
53 #define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
54 #define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
55 #define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
56 #define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
58 #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
59 #define BMP280_OSRS_PRESS_SKIP 0
60 #define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
61 #define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
62 #define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
63 #define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
64 #define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
65 #define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
67 #define BMP280_MODE_MASK (BIT(1) | BIT(0))
68 #define BMP280_MODE_SLEEP 0
69 #define BMP280_MODE_FORCED BIT(0)
70 #define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
72 /* BMP180 specific registers */
73 #define BMP180_REG_OUT_XLSB 0xF8
74 #define BMP180_REG_OUT_LSB 0xF7
75 #define BMP180_REG_OUT_MSB 0xF6
77 #define BMP180_REG_CALIB_START 0xAA
78 #define BMP180_REG_CALIB_COUNT 22
80 #define BMP180_MEAS_SCO BIT(5)
81 #define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
82 #define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
83 #define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
84 #define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
85 #define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
86 #define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
88 /* BMP180 and BMP280 common registers */
89 #define BMP280_REG_CTRL_MEAS 0xF4
90 #define BMP280_REG_RESET 0xE0
91 #define BMP280_REG_ID 0xD0
93 #define BMP180_CHIP_ID 0x55
94 #define BMP280_CHIP_ID 0x58
95 #define BMP280_SOFT_RESET_VAL 0xB6
98 struct i2c_client
*client
;
100 struct regmap
*regmap
;
101 const struct bmp280_chip_info
*chip_info
;
103 /* log of base 2 of oversampling rate */
104 u8 oversampling_press
;
105 u8 oversampling_temp
;
108 * Carryover value from temperature conversion, used in pressure
114 struct bmp280_chip_info
{
115 const struct regmap_config
*regmap_config
;
117 const int *oversampling_temp_avail
;
118 int num_oversampling_temp_avail
;
120 const int *oversampling_press_avail
;
121 int num_oversampling_press_avail
;
123 int (*chip_config
)(struct bmp280_data
*);
124 int (*read_temp
)(struct bmp280_data
*, int *);
125 int (*read_press
)(struct bmp280_data
*, int *, int *);
129 * These enums are used for indexing into the array of compensation
130 * parameters for BMP280.
133 enum { P1
, P2
, P3
, P4
, P5
, P6
, P7
, P8
, P9
};
135 static const struct iio_chan_spec bmp280_channels
[] = {
137 .type
= IIO_PRESSURE
,
138 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
139 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
143 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
144 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
148 static bool bmp280_is_writeable_reg(struct device
*dev
, unsigned int reg
)
151 case BMP280_REG_CONFIG
:
152 case BMP280_REG_CTRL_MEAS
:
153 case BMP280_REG_RESET
:
160 static bool bmp280_is_volatile_reg(struct device
*dev
, unsigned int reg
)
163 case BMP280_REG_TEMP_XLSB
:
164 case BMP280_REG_TEMP_LSB
:
165 case BMP280_REG_TEMP_MSB
:
166 case BMP280_REG_PRESS_XLSB
:
167 case BMP280_REG_PRESS_LSB
:
168 case BMP280_REG_PRESS_MSB
:
169 case BMP280_REG_STATUS
:
176 static const struct regmap_config bmp280_regmap_config
= {
180 .max_register
= BMP280_REG_TEMP_XLSB
,
181 .cache_type
= REGCACHE_RBTREE
,
183 .writeable_reg
= bmp280_is_writeable_reg
,
184 .volatile_reg
= bmp280_is_volatile_reg
,
188 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
189 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
192 * Taken from datasheet, Section 3.11.3, "Compensation formula".
194 static s32
bmp280_compensate_temp(struct bmp280_data
*data
,
199 __le16 buf
[BMP280_COMP_TEMP_REG_COUNT
/ 2];
201 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_TEMP_START
,
202 buf
, BMP280_COMP_TEMP_REG_COUNT
);
204 dev_err(&data
->client
->dev
,
205 "failed to read temperature calibration parameters\n");
210 * The double casts are necessary because le16_to_cpu returns an
211 * unsigned 16-bit value. Casting that value directly to a
212 * signed 32-bit will not do proper sign extension.
214 * Conversely, T1 and P1 are unsigned values, so they can be
215 * cast straight to the larger type.
217 var1
= (((adc_temp
>> 3) - ((s32
)le16_to_cpu(buf
[T1
]) << 1)) *
218 ((s32
)(s16
)le16_to_cpu(buf
[T2
]))) >> 11;
219 var2
= (((((adc_temp
>> 4) - ((s32
)le16_to_cpu(buf
[T1
]))) *
220 ((adc_temp
>> 4) - ((s32
)le16_to_cpu(buf
[T1
])))) >> 12) *
221 ((s32
)(s16
)le16_to_cpu(buf
[T3
]))) >> 14;
222 data
->t_fine
= var1
+ var2
;
224 return (data
->t_fine
* 5 + 128) >> 8;
228 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
229 * integer bits and 8 fractional bits). Output value of "24674867"
230 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
232 * Taken from datasheet, Section 3.11.3, "Compensation formula".
234 static u32
bmp280_compensate_press(struct bmp280_data
*data
,
239 __le16 buf
[BMP280_COMP_PRESS_REG_COUNT
/ 2];
241 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_PRESS_START
,
242 buf
, BMP280_COMP_PRESS_REG_COUNT
);
244 dev_err(&data
->client
->dev
,
245 "failed to read pressure calibration parameters\n");
249 var1
= ((s64
)data
->t_fine
) - 128000;
250 var2
= var1
* var1
* (s64
)(s16
)le16_to_cpu(buf
[P6
]);
251 var2
+= (var1
* (s64
)(s16
)le16_to_cpu(buf
[P5
])) << 17;
252 var2
+= ((s64
)(s16
)le16_to_cpu(buf
[P4
])) << 35;
253 var1
= ((var1
* var1
* (s64
)(s16
)le16_to_cpu(buf
[P3
])) >> 8) +
254 ((var1
* (s64
)(s16
)le16_to_cpu(buf
[P2
])) << 12);
255 var1
= ((((s64
)1) << 47) + var1
) * ((s64
)le16_to_cpu(buf
[P1
])) >> 33;
260 p
= ((((s64
)1048576 - adc_press
) << 31) - var2
) * 3125;
261 p
= div64_s64(p
, var1
);
262 var1
= (((s64
)(s16
)le16_to_cpu(buf
[P9
])) * (p
>> 13) * (p
>> 13)) >> 25;
263 var2
= (((s64
)(s16
)le16_to_cpu(buf
[P8
])) * p
) >> 19;
264 p
= ((p
+ var1
+ var2
) >> 8) + (((s64
)(s16
)le16_to_cpu(buf
[P7
])) << 4);
269 static int bmp280_read_temp(struct bmp280_data
*data
,
274 s32 adc_temp
, comp_temp
;
276 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_TEMP_MSB
,
279 dev_err(&data
->client
->dev
, "failed to read temperature\n");
283 adc_temp
= be32_to_cpu(tmp
) >> 12;
284 comp_temp
= bmp280_compensate_temp(data
, adc_temp
);
287 * val might be NULL if we're called by the read_press routine,
288 * who only cares about the carry over t_fine value.
291 *val
= comp_temp
* 10;
298 static int bmp280_read_press(struct bmp280_data
*data
,
306 /* Read and compensate temperature so we get a reading of t_fine. */
307 ret
= bmp280_read_temp(data
, NULL
);
311 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_PRESS_MSB
,
314 dev_err(&data
->client
->dev
, "failed to read pressure\n");
318 adc_press
= be32_to_cpu(tmp
) >> 12;
319 comp_press
= bmp280_compensate_press(data
, adc_press
);
324 return IIO_VAL_FRACTIONAL
;
327 static int bmp280_read_raw(struct iio_dev
*indio_dev
,
328 struct iio_chan_spec
const *chan
,
329 int *val
, int *val2
, long mask
)
332 struct bmp280_data
*data
= iio_priv(indio_dev
);
334 mutex_lock(&data
->lock
);
337 case IIO_CHAN_INFO_PROCESSED
:
338 switch (chan
->type
) {
340 ret
= data
->chip_info
->read_press(data
, val
, val2
);
343 ret
= data
->chip_info
->read_temp(data
, val
);
350 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
351 switch (chan
->type
) {
353 *val
= 1 << data
->oversampling_press
;
357 *val
= 1 << data
->oversampling_temp
;
370 mutex_unlock(&data
->lock
);
375 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data
*data
,
379 const int *avail
= data
->chip_info
->oversampling_temp_avail
;
380 const int n
= data
->chip_info
->num_oversampling_temp_avail
;
382 for (i
= 0; i
< n
; i
++) {
383 if (avail
[i
] == val
) {
384 data
->oversampling_temp
= ilog2(val
);
386 return data
->chip_info
->chip_config(data
);
392 static int bmp280_write_oversampling_ratio_press(struct bmp280_data
*data
,
396 const int *avail
= data
->chip_info
->oversampling_press_avail
;
397 const int n
= data
->chip_info
->num_oversampling_press_avail
;
399 for (i
= 0; i
< n
; i
++) {
400 if (avail
[i
] == val
) {
401 data
->oversampling_press
= ilog2(val
);
403 return data
->chip_info
->chip_config(data
);
409 static int bmp280_write_raw(struct iio_dev
*indio_dev
,
410 struct iio_chan_spec
const *chan
,
411 int val
, int val2
, long mask
)
414 struct bmp280_data
*data
= iio_priv(indio_dev
);
417 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
418 mutex_lock(&data
->lock
);
419 switch (chan
->type
) {
421 ret
= bmp280_write_oversampling_ratio_press(data
, val
);
424 ret
= bmp280_write_oversampling_ratio_temp(data
, val
);
430 mutex_unlock(&data
->lock
);
439 static ssize_t
bmp280_show_avail(char *buf
, const int *vals
, const int n
)
444 for (i
= 0; i
< n
; i
++)
445 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
, "%d ", vals
[i
]);
452 static ssize_t
bmp280_show_temp_oversampling_avail(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 struct bmp280_data
*data
= iio_priv(dev_to_iio_dev(dev
));
457 return bmp280_show_avail(buf
, data
->chip_info
->oversampling_temp_avail
,
458 data
->chip_info
->num_oversampling_temp_avail
);
461 static ssize_t
bmp280_show_press_oversampling_avail(struct device
*dev
,
462 struct device_attribute
*attr
, char *buf
)
464 struct bmp280_data
*data
= iio_priv(dev_to_iio_dev(dev
));
466 return bmp280_show_avail(buf
, data
->chip_info
->oversampling_press_avail
,
467 data
->chip_info
->num_oversampling_press_avail
);
470 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available
,
471 S_IRUGO
, bmp280_show_temp_oversampling_avail
, NULL
, 0);
473 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available
,
474 S_IRUGO
, bmp280_show_press_oversampling_avail
, NULL
, 0);
476 static struct attribute
*bmp280_attributes
[] = {
477 &iio_dev_attr_in_temp_oversampling_ratio_available
.dev_attr
.attr
,
478 &iio_dev_attr_in_pressure_oversampling_ratio_available
.dev_attr
.attr
,
482 static const struct attribute_group bmp280_attrs_group
= {
483 .attrs
= bmp280_attributes
,
486 static const struct iio_info bmp280_info
= {
487 .driver_module
= THIS_MODULE
,
488 .read_raw
= &bmp280_read_raw
,
489 .write_raw
= &bmp280_write_raw
,
490 .attrs
= &bmp280_attrs_group
,
493 static int bmp280_chip_config(struct bmp280_data
*data
)
496 u8 osrs
= BMP280_OSRS_TEMP_X(data
->oversampling_temp
+ 1) |
497 BMP280_OSRS_PRESS_X(data
->oversampling_press
+ 1);
499 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CTRL_MEAS
,
500 BMP280_OSRS_TEMP_MASK
|
501 BMP280_OSRS_PRESS_MASK
|
503 osrs
| BMP280_MODE_NORMAL
);
505 dev_err(&data
->client
->dev
,
506 "failed to write ctrl_meas register\n");
510 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CONFIG
,
514 dev_err(&data
->client
->dev
,
515 "failed to write config register\n");
522 static const int bmp280_oversampling_avail
[] = { 1, 2, 4, 8, 16 };
524 static const struct bmp280_chip_info bmp280_chip_info
= {
525 .regmap_config
= &bmp280_regmap_config
,
527 .oversampling_temp_avail
= bmp280_oversampling_avail
,
528 .num_oversampling_temp_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
530 .oversampling_press_avail
= bmp280_oversampling_avail
,
531 .num_oversampling_press_avail
= ARRAY_SIZE(bmp280_oversampling_avail
),
533 .chip_config
= bmp280_chip_config
,
534 .read_temp
= bmp280_read_temp
,
535 .read_press
= bmp280_read_press
,
538 static bool bmp180_is_writeable_reg(struct device
*dev
, unsigned int reg
)
541 case BMP280_REG_CTRL_MEAS
:
542 case BMP280_REG_RESET
:
549 static bool bmp180_is_volatile_reg(struct device
*dev
, unsigned int reg
)
552 case BMP180_REG_OUT_XLSB
:
553 case BMP180_REG_OUT_LSB
:
554 case BMP180_REG_OUT_MSB
:
555 case BMP280_REG_CTRL_MEAS
:
562 static const struct regmap_config bmp180_regmap_config
= {
566 .max_register
= BMP180_REG_OUT_XLSB
,
567 .cache_type
= REGCACHE_RBTREE
,
569 .writeable_reg
= bmp180_is_writeable_reg
,
570 .volatile_reg
= bmp180_is_volatile_reg
,
573 static int bmp180_measure(struct bmp280_data
*data
, u8 ctrl_meas
)
576 const int conversion_time_max
[] = { 4500, 7500, 13500, 25500 };
577 unsigned int delay_us
;
580 ret
= regmap_write(data
->regmap
, BMP280_REG_CTRL_MEAS
, ctrl_meas
);
584 if (ctrl_meas
== BMP180_MEAS_TEMP
)
587 delay_us
= conversion_time_max
[data
->oversampling_press
];
589 usleep_range(delay_us
, delay_us
+ 1000);
591 ret
= regmap_read(data
->regmap
, BMP280_REG_CTRL_MEAS
, &ctrl
);
595 /* The value of this bit reset to "0" after conversion is complete */
596 if (ctrl
& BMP180_MEAS_SCO
)
602 static int bmp180_read_adc_temp(struct bmp280_data
*data
, int *val
)
607 ret
= bmp180_measure(data
, BMP180_MEAS_TEMP
);
611 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 2);
615 *val
= be16_to_cpu(tmp
);
621 * These enums are used for indexing into the array of calibration
622 * coefficients for BMP180.
624 enum { AC1
, AC2
, AC3
, AC4
, AC5
, AC6
, B1
, B2
, MB
, MC
, MD
};
626 struct bmp180_calib
{
640 static int bmp180_read_calib(struct bmp280_data
*data
,
641 struct bmp180_calib
*calib
)
645 __be16 buf
[BMP180_REG_CALIB_COUNT
/ 2];
647 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_CALIB_START
, buf
,
653 /* None of the words has the value 0 or 0xFFFF */
654 for (i
= 0; i
< ARRAY_SIZE(buf
); i
++) {
655 if (buf
[i
] == cpu_to_be16(0) || buf
[i
] == cpu_to_be16(0xffff))
659 calib
->AC1
= be16_to_cpu(buf
[AC1
]);
660 calib
->AC2
= be16_to_cpu(buf
[AC2
]);
661 calib
->AC3
= be16_to_cpu(buf
[AC3
]);
662 calib
->AC4
= be16_to_cpu(buf
[AC4
]);
663 calib
->AC5
= be16_to_cpu(buf
[AC5
]);
664 calib
->AC6
= be16_to_cpu(buf
[AC6
]);
665 calib
->B1
= be16_to_cpu(buf
[B1
]);
666 calib
->B2
= be16_to_cpu(buf
[B2
]);
667 calib
->MB
= be16_to_cpu(buf
[MB
]);
668 calib
->MC
= be16_to_cpu(buf
[MC
]);
669 calib
->MD
= be16_to_cpu(buf
[MD
]);
675 * Returns temperature in DegC, resolution is 0.1 DegC.
676 * t_fine carries fine temperature as global value.
678 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
680 static s32
bmp180_compensate_temp(struct bmp280_data
*data
, s32 adc_temp
)
684 struct bmp180_calib calib
;
686 ret
= bmp180_read_calib(data
, &calib
);
688 dev_err(&data
->client
->dev
,
689 "failed to read calibration coefficients\n");
693 x1
= ((adc_temp
- calib
.AC6
) * calib
.AC5
) >> 15;
694 x2
= (calib
.MC
<< 11) / (x1
+ calib
.MD
);
695 data
->t_fine
= x1
+ x2
;
697 return (data
->t_fine
+ 8) >> 4;
700 static int bmp180_read_temp(struct bmp280_data
*data
, int *val
)
703 s32 adc_temp
, comp_temp
;
705 ret
= bmp180_read_adc_temp(data
, &adc_temp
);
709 comp_temp
= bmp180_compensate_temp(data
, adc_temp
);
712 * val might be NULL if we're called by the read_press routine,
713 * who only cares about the carry over t_fine value.
716 *val
= comp_temp
* 100;
723 static int bmp180_read_adc_press(struct bmp280_data
*data
, int *val
)
727 u8 oss
= data
->oversampling_press
;
729 ret
= bmp180_measure(data
, BMP180_MEAS_PRESS_X(oss
));
733 ret
= regmap_bulk_read(data
->regmap
, BMP180_REG_OUT_MSB
, (u8
*)&tmp
, 3);
737 *val
= (be32_to_cpu(tmp
) >> 8) >> (8 - oss
);
743 * Returns pressure in Pa, resolution is 1 Pa.
745 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
747 static u32
bmp180_compensate_press(struct bmp280_data
*data
, s32 adc_press
)
753 s32 oss
= data
->oversampling_press
;
754 struct bmp180_calib calib
;
756 ret
= bmp180_read_calib(data
, &calib
);
758 dev_err(&data
->client
->dev
,
759 "failed to read calibration coefficients\n");
763 b6
= data
->t_fine
- 4000;
764 x1
= (calib
.B2
* (b6
* b6
>> 12)) >> 11;
765 x2
= calib
.AC2
* b6
>> 11;
767 b3
= ((((s32
)calib
.AC1
* 4 + x3
) << oss
) + 2) / 4;
768 x1
= calib
.AC3
* b6
>> 13;
769 x2
= (calib
.B1
* ((b6
* b6
) >> 12)) >> 16;
770 x3
= (x1
+ x2
+ 2) >> 2;
771 b4
= calib
.AC4
* (u32
)(x3
+ 32768) >> 15;
772 b7
= ((u32
)adc_press
- b3
) * (50000 >> oss
);
778 x1
= (p
>> 8) * (p
>> 8);
779 x1
= (x1
* 3038) >> 16;
780 x2
= (-7357 * p
) >> 16;
782 return p
+ ((x1
+ x2
+ 3791) >> 4);
785 static int bmp180_read_press(struct bmp280_data
*data
,
792 /* Read and compensate temperature so we get a reading of t_fine. */
793 ret
= bmp180_read_temp(data
, NULL
);
797 ret
= bmp180_read_adc_press(data
, &adc_press
);
801 comp_press
= bmp180_compensate_press(data
, adc_press
);
806 return IIO_VAL_FRACTIONAL
;
809 static int bmp180_chip_config(struct bmp280_data
*data
)
814 static const int bmp180_oversampling_temp_avail
[] = { 1 };
815 static const int bmp180_oversampling_press_avail
[] = { 1, 2, 4, 8 };
817 static const struct bmp280_chip_info bmp180_chip_info
= {
818 .regmap_config
= &bmp180_regmap_config
,
820 .oversampling_temp_avail
= bmp180_oversampling_temp_avail
,
821 .num_oversampling_temp_avail
=
822 ARRAY_SIZE(bmp180_oversampling_temp_avail
),
824 .oversampling_press_avail
= bmp180_oversampling_press_avail
,
825 .num_oversampling_press_avail
=
826 ARRAY_SIZE(bmp180_oversampling_press_avail
),
828 .chip_config
= bmp180_chip_config
,
829 .read_temp
= bmp180_read_temp
,
830 .read_press
= bmp180_read_press
,
833 static int bmp280_probe(struct i2c_client
*client
,
834 const struct i2c_device_id
*id
)
837 struct iio_dev
*indio_dev
;
838 struct bmp280_data
*data
;
839 unsigned int chip_id
;
841 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
845 data
= iio_priv(indio_dev
);
846 mutex_init(&data
->lock
);
847 data
->client
= client
;
849 indio_dev
->dev
.parent
= &client
->dev
;
850 indio_dev
->name
= id
->name
;
851 indio_dev
->channels
= bmp280_channels
;
852 indio_dev
->num_channels
= ARRAY_SIZE(bmp280_channels
);
853 indio_dev
->info
= &bmp280_info
;
854 indio_dev
->modes
= INDIO_DIRECT_MODE
;
856 switch (id
->driver_data
) {
858 data
->chip_info
= &bmp180_chip_info
;
859 data
->oversampling_press
= ilog2(8);
860 data
->oversampling_temp
= ilog2(1);
863 data
->chip_info
= &bmp280_chip_info
;
864 data
->oversampling_press
= ilog2(16);
865 data
->oversampling_temp
= ilog2(2);
871 data
->regmap
= devm_regmap_init_i2c(client
,
872 data
->chip_info
->regmap_config
);
873 if (IS_ERR(data
->regmap
)) {
874 dev_err(&client
->dev
, "failed to allocate register map\n");
875 return PTR_ERR(data
->regmap
);
878 ret
= regmap_read(data
->regmap
, BMP280_REG_ID
, &chip_id
);
881 if (chip_id
!= id
->driver_data
) {
882 dev_err(&client
->dev
, "bad chip id. expected %lx got %x\n",
883 id
->driver_data
, chip_id
);
887 ret
= data
->chip_info
->chip_config(data
);
891 return devm_iio_device_register(&client
->dev
, indio_dev
);
894 static const struct acpi_device_id bmp280_acpi_match
[] = {
895 {"BMP0280", BMP280_CHIP_ID
},
896 {"BMP0180", BMP180_CHIP_ID
},
897 {"BMP0085", BMP180_CHIP_ID
},
900 MODULE_DEVICE_TABLE(acpi
, bmp280_acpi_match
);
902 static const struct i2c_device_id bmp280_id
[] = {
903 {"bmp280", BMP280_CHIP_ID
},
904 {"bmp180", BMP180_CHIP_ID
},
905 {"bmp085", BMP180_CHIP_ID
},
908 MODULE_DEVICE_TABLE(i2c
, bmp280_id
);
910 static struct i2c_driver bmp280_driver
= {
913 .acpi_match_table
= ACPI_PTR(bmp280_acpi_match
),
915 .probe
= bmp280_probe
,
916 .id_table
= bmp280_id
,
918 module_i2c_driver(bmp280_driver
);
920 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
921 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
922 MODULE_LICENSE("GPL v2");