2 * Copyright (c) 2014 Intel Corporation
4 * Driver for Bosch Sensortec 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.
12 #define pr_fmt(fmt) "bmp280: " fmt
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
17 #include <linux/regmap.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define BMP280_REG_TEMP_XLSB 0xFC
22 #define BMP280_REG_TEMP_LSB 0xFB
23 #define BMP280_REG_TEMP_MSB 0xFA
24 #define BMP280_REG_PRESS_XLSB 0xF9
25 #define BMP280_REG_PRESS_LSB 0xF8
26 #define BMP280_REG_PRESS_MSB 0xF7
28 #define BMP280_REG_CONFIG 0xF5
29 #define BMP280_REG_CTRL_MEAS 0xF4
30 #define BMP280_REG_STATUS 0xF3
31 #define BMP280_REG_RESET 0xE0
32 #define BMP280_REG_ID 0xD0
34 #define BMP280_REG_COMP_TEMP_START 0x88
35 #define BMP280_COMP_TEMP_REG_COUNT 6
37 #define BMP280_REG_COMP_PRESS_START 0x8E
38 #define BMP280_COMP_PRESS_REG_COUNT 18
40 #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
41 #define BMP280_FILTER_OFF 0
42 #define BMP280_FILTER_2X BIT(2)
43 #define BMP280_FILTER_4X BIT(3)
44 #define BMP280_FILTER_8X (BIT(3) | BIT(2))
45 #define BMP280_FILTER_16X BIT(4)
47 #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
48 #define BMP280_OSRS_TEMP_SKIP 0
49 #define BMP280_OSRS_TEMP_1X BIT(5)
50 #define BMP280_OSRS_TEMP_2X BIT(6)
51 #define BMP280_OSRS_TEMP_4X (BIT(6) | BIT(5))
52 #define BMP280_OSRS_TEMP_8X BIT(7)
53 #define BMP280_OSRS_TEMP_16X (BIT(7) | BIT(5))
55 #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
56 #define BMP280_OSRS_PRESS_SKIP 0
57 #define BMP280_OSRS_PRESS_1X BIT(2)
58 #define BMP280_OSRS_PRESS_2X BIT(3)
59 #define BMP280_OSRS_PRESS_4X (BIT(3) | BIT(2))
60 #define BMP280_OSRS_PRESS_8X BIT(4)
61 #define BMP280_OSRS_PRESS_16X (BIT(4) | BIT(2))
63 #define BMP280_MODE_MASK (BIT(1) | BIT(0))
64 #define BMP280_MODE_SLEEP 0
65 #define BMP280_MODE_FORCED BIT(0)
66 #define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
68 #define BMP280_CHIP_ID 0x58
69 #define BMP280_SOFT_RESET_VAL 0xB6
72 struct i2c_client
*client
;
74 struct regmap
*regmap
;
77 * Carryover value from temperature conversion, used in pressure
84 * These enums are used for indexing into the array of compensation
88 enum { P1
, P2
, P3
, P4
, P5
, P6
, P7
, P8
, P9
};
90 static const struct iio_chan_spec bmp280_channels
[] = {
93 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
97 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
101 static bool bmp280_is_writeable_reg(struct device
*dev
, unsigned int reg
)
104 case BMP280_REG_CONFIG
:
105 case BMP280_REG_CTRL_MEAS
:
106 case BMP280_REG_RESET
:
113 static bool bmp280_is_volatile_reg(struct device
*dev
, unsigned int reg
)
116 case BMP280_REG_TEMP_XLSB
:
117 case BMP280_REG_TEMP_LSB
:
118 case BMP280_REG_TEMP_MSB
:
119 case BMP280_REG_PRESS_XLSB
:
120 case BMP280_REG_PRESS_LSB
:
121 case BMP280_REG_PRESS_MSB
:
122 case BMP280_REG_STATUS
:
129 static const struct regmap_config bmp280_regmap_config
= {
133 .max_register
= BMP280_REG_TEMP_XLSB
,
134 .cache_type
= REGCACHE_RBTREE
,
136 .writeable_reg
= bmp280_is_writeable_reg
,
137 .volatile_reg
= bmp280_is_volatile_reg
,
141 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
142 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
145 * Taken from datasheet, Section 3.11.3, "Compensation formula".
147 static s32
bmp280_compensate_temp(struct bmp280_data
*data
,
152 __le16 buf
[BMP280_COMP_TEMP_REG_COUNT
/ 2];
154 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_TEMP_START
,
155 buf
, BMP280_COMP_TEMP_REG_COUNT
);
157 dev_err(&data
->client
->dev
,
158 "failed to read temperature calibration parameters\n");
163 * The double casts are necessary because le16_to_cpu returns an
164 * unsigned 16-bit value. Casting that value directly to a
165 * signed 32-bit will not do proper sign extension.
167 * Conversely, T1 and P1 are unsigned values, so they can be
168 * cast straight to the larger type.
170 var1
= (((adc_temp
>> 3) - ((s32
)le16_to_cpu(buf
[T1
]) << 1)) *
171 ((s32
)(s16
)le16_to_cpu(buf
[T2
]))) >> 11;
172 var2
= (((((adc_temp
>> 4) - ((s32
)le16_to_cpu(buf
[T1
]))) *
173 ((adc_temp
>> 4) - ((s32
)le16_to_cpu(buf
[T1
])))) >> 12) *
174 ((s32
)(s16
)le16_to_cpu(buf
[T3
]))) >> 14;
176 return (data
->t_fine
* 5 + 128) >> 8;
180 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
181 * integer bits and 8 fractional bits). Output value of "24674867"
182 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
184 * Taken from datasheet, Section 3.11.3, "Compensation formula".
186 static u32
bmp280_compensate_press(struct bmp280_data
*data
,
191 __le16 buf
[BMP280_COMP_PRESS_REG_COUNT
/ 2];
193 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_COMP_PRESS_START
,
194 buf
, BMP280_COMP_PRESS_REG_COUNT
);
196 dev_err(&data
->client
->dev
,
197 "failed to read pressure calibration parameters\n");
201 var1
= ((s64
)data
->t_fine
) - 128000;
202 var2
= var1
* var1
* (s64
)(s16
)le16_to_cpu(buf
[P6
]);
203 var2
+= (var1
* (s64
)(s16
)le16_to_cpu(buf
[P5
])) << 17;
204 var2
+= ((s64
)(s16
)le16_to_cpu(buf
[P4
])) << 35;
205 var1
= ((var1
* var1
* (s64
)(s16
)le16_to_cpu(buf
[P3
])) >> 8) +
206 ((var1
* (s64
)(s16
)le16_to_cpu(buf
[P2
])) << 12);
207 var1
= ((((s64
)1) << 47) + var1
) * ((s64
)le16_to_cpu(buf
[P1
])) >> 33;
212 p
= ((((s64
)1048576 - adc_press
) << 31) - var2
) * 3125;
213 p
= div64_s64(p
, var1
);
214 var1
= (((s64
)(s16
)le16_to_cpu(buf
[P9
])) * (p
>> 13) * (p
>> 13)) >> 25;
215 var2
= (((s64
)(s16
)le16_to_cpu(buf
[P8
])) * p
) >> 19;
216 p
= ((p
+ var1
+ var2
) >> 8) + (((s64
)(s16
)le16_to_cpu(buf
[P7
])) << 4);
221 static int bmp280_read_temp(struct bmp280_data
*data
,
226 s32 adc_temp
, comp_temp
;
228 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_TEMP_MSB
,
231 dev_err(&data
->client
->dev
, "failed to read temperature\n");
235 adc_temp
= be32_to_cpu(tmp
) >> 12;
236 comp_temp
= bmp280_compensate_temp(data
, adc_temp
);
239 * val might be NULL if we're called by the read_press routine,
240 * who only cares about the carry over t_fine value.
243 *val
= comp_temp
* 10;
250 static int bmp280_read_press(struct bmp280_data
*data
,
258 /* Read and compensate temperature so we get a reading of t_fine. */
259 ret
= bmp280_read_temp(data
, NULL
);
263 ret
= regmap_bulk_read(data
->regmap
, BMP280_REG_PRESS_MSB
,
266 dev_err(&data
->client
->dev
, "failed to read pressure\n");
270 adc_press
= be32_to_cpu(tmp
) >> 12;
271 comp_press
= bmp280_compensate_press(data
, adc_press
);
276 return IIO_VAL_FRACTIONAL
;
279 static int bmp280_read_raw(struct iio_dev
*indio_dev
,
280 struct iio_chan_spec
const *chan
,
281 int *val
, int *val2
, long mask
)
284 struct bmp280_data
*data
= iio_priv(indio_dev
);
286 mutex_lock(&data
->lock
);
289 case IIO_CHAN_INFO_PROCESSED
:
290 switch (chan
->type
) {
292 ret
= bmp280_read_press(data
, val
, val2
);
295 ret
= bmp280_read_temp(data
, val
);
307 mutex_unlock(&data
->lock
);
312 static const struct iio_info bmp280_info
= {
313 .driver_module
= THIS_MODULE
,
314 .read_raw
= &bmp280_read_raw
,
317 static int bmp280_chip_init(struct bmp280_data
*data
)
321 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CTRL_MEAS
,
322 BMP280_OSRS_TEMP_MASK
|
323 BMP280_OSRS_PRESS_MASK
|
325 BMP280_OSRS_TEMP_2X
|
326 BMP280_OSRS_PRESS_16X
|
329 dev_err(&data
->client
->dev
,
330 "failed to write ctrl_meas register\n");
334 ret
= regmap_update_bits(data
->regmap
, BMP280_REG_CONFIG
,
338 dev_err(&data
->client
->dev
,
339 "failed to write config register\n");
346 static int bmp280_probe(struct i2c_client
*client
,
347 const struct i2c_device_id
*id
)
350 struct iio_dev
*indio_dev
;
351 struct bmp280_data
*data
;
352 unsigned int chip_id
;
354 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*data
));
358 data
= iio_priv(indio_dev
);
359 mutex_init(&data
->lock
);
360 data
->client
= client
;
362 indio_dev
->dev
.parent
= &client
->dev
;
363 indio_dev
->name
= id
->name
;
364 indio_dev
->channels
= bmp280_channels
;
365 indio_dev
->num_channels
= ARRAY_SIZE(bmp280_channels
);
366 indio_dev
->info
= &bmp280_info
;
367 indio_dev
->modes
= INDIO_DIRECT_MODE
;
369 data
->regmap
= devm_regmap_init_i2c(client
, &bmp280_regmap_config
);
370 if (IS_ERR(data
->regmap
)) {
371 dev_err(&client
->dev
, "failed to allocate register map\n");
372 return PTR_ERR(data
->regmap
);
375 ret
= regmap_read(data
->regmap
, BMP280_REG_ID
, &chip_id
);
378 if (chip_id
!= BMP280_CHIP_ID
) {
379 dev_err(&client
->dev
, "bad chip id. expected %x got %x\n",
380 BMP280_CHIP_ID
, chip_id
);
384 ret
= bmp280_chip_init(data
);
388 return devm_iio_device_register(&client
->dev
, indio_dev
);
391 static const struct acpi_device_id bmp280_acpi_match
[] = {
395 MODULE_DEVICE_TABLE(acpi
, bmp280_acpi_match
);
397 static const struct i2c_device_id bmp280_id
[] = {
401 MODULE_DEVICE_TABLE(i2c
, bmp280_id
);
403 static struct i2c_driver bmp280_driver
= {
406 .acpi_match_table
= ACPI_PTR(bmp280_acpi_match
),
408 .probe
= bmp280_probe
,
409 .id_table
= bmp280_id
,
411 module_i2c_driver(bmp280_driver
);
413 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
414 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor");
415 MODULE_LICENSE("GPL v2");