Commit | Line | Data |
---|---|---|
d5c94568 VD |
1 | /* |
2 | * Copyright (c) 2014 Intel Corporation | |
3 | * | |
4 | * Driver for Bosch Sensortec BMP280 digital pressure sensor. | |
5 | * | |
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. | |
9 | * | |
10 | */ | |
11 | ||
12 | #define pr_fmt(fmt) "bmp280: " fmt | |
13 | ||
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> | |
20 | ||
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 | |
27 | ||
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 | |
33 | ||
34 | #define BMP280_REG_COMP_TEMP_START 0x88 | |
35 | #define BMP280_COMP_TEMP_REG_COUNT 6 | |
36 | ||
37 | #define BMP280_REG_COMP_PRESS_START 0x8E | |
38 | #define BMP280_COMP_PRESS_REG_COUNT 18 | |
39 | ||
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) | |
46 | ||
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)) | |
54 | ||
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)) | |
62 | ||
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)) | |
67 | ||
68 | #define BMP280_CHIP_ID 0x58 | |
69 | #define BMP280_SOFT_RESET_VAL 0xB6 | |
70 | ||
71 | struct bmp280_data { | |
72 | struct i2c_client *client; | |
73 | struct mutex lock; | |
74 | struct regmap *regmap; | |
75 | ||
76 | /* | |
77 | * Carryover value from temperature conversion, used in pressure | |
78 | * calculation. | |
79 | */ | |
80 | s32 t_fine; | |
81 | }; | |
82 | ||
0f8994b1 VD |
83 | /* |
84 | * These enums are used for indexing into the array of compensation | |
85 | * parameters. | |
86 | */ | |
87 | enum { T1, T2, T3 }; | |
88 | enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 }; | |
d5c94568 VD |
89 | |
90 | static const struct iio_chan_spec bmp280_channels[] = { | |
91 | { | |
92 | .type = IIO_PRESSURE, | |
93 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | |
94 | }, | |
95 | { | |
96 | .type = IIO_TEMP, | |
97 | .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), | |
98 | }, | |
99 | }; | |
100 | ||
101 | static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg) | |
102 | { | |
103 | switch (reg) { | |
104 | case BMP280_REG_CONFIG: | |
105 | case BMP280_REG_CTRL_MEAS: | |
106 | case BMP280_REG_RESET: | |
107 | return true; | |
108 | default: | |
109 | return false; | |
110 | }; | |
111 | } | |
112 | ||
113 | static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg) | |
114 | { | |
115 | switch (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: | |
123 | return true; | |
124 | default: | |
125 | return false; | |
126 | } | |
127 | } | |
128 | ||
129 | static const struct regmap_config bmp280_regmap_config = { | |
130 | .reg_bits = 8, | |
131 | .val_bits = 8, | |
132 | ||
133 | .max_register = BMP280_REG_TEMP_XLSB, | |
134 | .cache_type = REGCACHE_RBTREE, | |
135 | ||
136 | .writeable_reg = bmp280_is_writeable_reg, | |
137 | .volatile_reg = bmp280_is_volatile_reg, | |
138 | }; | |
139 | ||
d5c94568 VD |
140 | /* |
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 | |
143 | * value. | |
144 | * | |
145 | * Taken from datasheet, Section 3.11.3, "Compensation formula". | |
146 | */ | |
147 | static s32 bmp280_compensate_temp(struct bmp280_data *data, | |
d5c94568 VD |
148 | s32 adc_temp) |
149 | { | |
0f8994b1 | 150 | int ret; |
44cf3798 | 151 | s32 var1, var2; |
0f8994b1 VD |
152 | __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2]; |
153 | ||
154 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START, | |
155 | buf, BMP280_COMP_TEMP_REG_COUNT); | |
156 | if (ret < 0) { | |
157 | dev_err(&data->client->dev, | |
158 | "failed to read temperature calibration parameters\n"); | |
159 | return ret; | |
160 | } | |
d5c94568 | 161 | |
0f8994b1 VD |
162 | /* |
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. | |
166 | * | |
167 | * Conversely, T1 and P1 are unsigned values, so they can be | |
168 | * cast straight to the larger type. | |
169 | */ | |
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; | |
abad3983 | 175 | data->t_fine = var1 + var2; |
d5c94568 | 176 | |
44cf3798 | 177 | return (data->t_fine * 5 + 128) >> 8; |
d5c94568 VD |
178 | } |
179 | ||
180 | /* | |
181 | * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24 | |
182 | * integer bits and 8 fractional bits). Output value of "24674867" | |
183 | * represents 24674867/256 = 96386.2 Pa = 963.862 hPa | |
184 | * | |
185 | * Taken from datasheet, Section 3.11.3, "Compensation formula". | |
186 | */ | |
187 | static u32 bmp280_compensate_press(struct bmp280_data *data, | |
d5c94568 VD |
188 | s32 adc_press) |
189 | { | |
0f8994b1 | 190 | int ret; |
d5c94568 | 191 | s64 var1, var2, p; |
0f8994b1 VD |
192 | __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2]; |
193 | ||
194 | ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START, | |
195 | buf, BMP280_COMP_PRESS_REG_COUNT); | |
196 | if (ret < 0) { | |
197 | dev_err(&data->client->dev, | |
198 | "failed to read pressure calibration parameters\n"); | |
199 | return ret; | |
200 | } | |
d5c94568 | 201 | |
0f8994b1 VD |
202 | var1 = ((s64)data->t_fine) - 128000; |
203 | var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]); | |
44cf3798 HK |
204 | var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17; |
205 | var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35; | |
0f8994b1 VD |
206 | var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) + |
207 | ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12); | |
208 | var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33; | |
d5c94568 VD |
209 | |
210 | if (var1 == 0) | |
211 | return 0; | |
212 | ||
0f8994b1 | 213 | p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125; |
46ee98a2 | 214 | p = div64_s64(p, var1); |
0f8994b1 VD |
215 | var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25; |
216 | var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19; | |
217 | p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4); | |
d5c94568 | 218 | |
44cf3798 | 219 | return (u32)p; |
d5c94568 VD |
220 | } |
221 | ||
222 | static int bmp280_read_temp(struct bmp280_data *data, | |
223 | int *val) | |
224 | { | |
225 | int ret; | |
226 | __be32 tmp = 0; | |
227 | s32 adc_temp, comp_temp; | |
d5c94568 VD |
228 | |
229 | ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB, | |
230 | (u8 *) &tmp, 3); | |
231 | if (ret < 0) { | |
232 | dev_err(&data->client->dev, "failed to read temperature\n"); | |
233 | return ret; | |
234 | } | |
235 | ||
236 | adc_temp = be32_to_cpu(tmp) >> 12; | |
0f8994b1 | 237 | comp_temp = bmp280_compensate_temp(data, adc_temp); |
d5c94568 VD |
238 | |
239 | /* | |
240 | * val might be NULL if we're called by the read_press routine, | |
241 | * who only cares about the carry over t_fine value. | |
242 | */ | |
243 | if (val) { | |
244 | *val = comp_temp * 10; | |
245 | return IIO_VAL_INT; | |
246 | } | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
251 | static int bmp280_read_press(struct bmp280_data *data, | |
252 | int *val, int *val2) | |
253 | { | |
254 | int ret; | |
255 | __be32 tmp = 0; | |
256 | s32 adc_press; | |
257 | u32 comp_press; | |
d5c94568 VD |
258 | |
259 | /* Read and compensate temperature so we get a reading of t_fine. */ | |
260 | ret = bmp280_read_temp(data, NULL); | |
261 | if (ret < 0) | |
262 | return ret; | |
263 | ||
264 | ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB, | |
265 | (u8 *) &tmp, 3); | |
266 | if (ret < 0) { | |
267 | dev_err(&data->client->dev, "failed to read pressure\n"); | |
268 | return ret; | |
269 | } | |
270 | ||
271 | adc_press = be32_to_cpu(tmp) >> 12; | |
0f8994b1 | 272 | comp_press = bmp280_compensate_press(data, adc_press); |
d5c94568 | 273 | |
81ebe850 HK |
274 | *val = comp_press; |
275 | *val2 = 256000; | |
d5c94568 | 276 | |
81ebe850 | 277 | return IIO_VAL_FRACTIONAL; |
d5c94568 VD |
278 | } |
279 | ||
280 | static int bmp280_read_raw(struct iio_dev *indio_dev, | |
281 | struct iio_chan_spec const *chan, | |
282 | int *val, int *val2, long mask) | |
283 | { | |
284 | int ret; | |
285 | struct bmp280_data *data = iio_priv(indio_dev); | |
286 | ||
287 | mutex_lock(&data->lock); | |
288 | ||
289 | switch (mask) { | |
290 | case IIO_CHAN_INFO_PROCESSED: | |
291 | switch (chan->type) { | |
292 | case IIO_PRESSURE: | |
293 | ret = bmp280_read_press(data, val, val2); | |
294 | break; | |
295 | case IIO_TEMP: | |
296 | ret = bmp280_read_temp(data, val); | |
297 | break; | |
298 | default: | |
299 | ret = -EINVAL; | |
300 | break; | |
301 | } | |
302 | break; | |
303 | default: | |
304 | ret = -EINVAL; | |
305 | break; | |
306 | } | |
307 | ||
308 | mutex_unlock(&data->lock); | |
309 | ||
310 | return ret; | |
311 | } | |
312 | ||
313 | static const struct iio_info bmp280_info = { | |
314 | .driver_module = THIS_MODULE, | |
315 | .read_raw = &bmp280_read_raw, | |
316 | }; | |
317 | ||
318 | static int bmp280_chip_init(struct bmp280_data *data) | |
319 | { | |
320 | int ret; | |
321 | ||
322 | ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS, | |
323 | BMP280_OSRS_TEMP_MASK | | |
324 | BMP280_OSRS_PRESS_MASK | | |
325 | BMP280_MODE_MASK, | |
326 | BMP280_OSRS_TEMP_2X | | |
327 | BMP280_OSRS_PRESS_16X | | |
328 | BMP280_MODE_NORMAL); | |
329 | if (ret < 0) { | |
330 | dev_err(&data->client->dev, | |
44cf3798 | 331 | "failed to write ctrl_meas register\n"); |
d5c94568 VD |
332 | return ret; |
333 | } | |
334 | ||
335 | ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG, | |
336 | BMP280_FILTER_MASK, | |
337 | BMP280_FILTER_4X); | |
338 | if (ret < 0) { | |
339 | dev_err(&data->client->dev, | |
340 | "failed to write config register\n"); | |
341 | return ret; | |
342 | } | |
343 | ||
344 | return ret; | |
345 | } | |
346 | ||
347 | static int bmp280_probe(struct i2c_client *client, | |
348 | const struct i2c_device_id *id) | |
349 | { | |
350 | int ret; | |
351 | struct iio_dev *indio_dev; | |
352 | struct bmp280_data *data; | |
353 | unsigned int chip_id; | |
354 | ||
355 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); | |
356 | if (!indio_dev) | |
357 | return -ENOMEM; | |
358 | ||
d5c94568 VD |
359 | data = iio_priv(indio_dev); |
360 | mutex_init(&data->lock); | |
361 | data->client = client; | |
362 | ||
363 | indio_dev->dev.parent = &client->dev; | |
364 | indio_dev->name = id->name; | |
365 | indio_dev->channels = bmp280_channels; | |
366 | indio_dev->num_channels = ARRAY_SIZE(bmp280_channels); | |
367 | indio_dev->info = &bmp280_info; | |
368 | indio_dev->modes = INDIO_DIRECT_MODE; | |
369 | ||
370 | data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config); | |
371 | if (IS_ERR(data->regmap)) { | |
372 | dev_err(&client->dev, "failed to allocate register map\n"); | |
373 | return PTR_ERR(data->regmap); | |
374 | } | |
375 | ||
376 | ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id); | |
377 | if (ret < 0) | |
378 | return ret; | |
379 | if (chip_id != BMP280_CHIP_ID) { | |
380 | dev_err(&client->dev, "bad chip id. expected %x got %x\n", | |
381 | BMP280_CHIP_ID, chip_id); | |
382 | return -EINVAL; | |
383 | } | |
384 | ||
385 | ret = bmp280_chip_init(data); | |
386 | if (ret < 0) | |
387 | return ret; | |
388 | ||
389 | return devm_iio_device_register(&client->dev, indio_dev); | |
390 | } | |
391 | ||
392 | static const struct acpi_device_id bmp280_acpi_match[] = { | |
393 | {"BMP0280", 0}, | |
394 | { }, | |
395 | }; | |
396 | MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match); | |
397 | ||
398 | static const struct i2c_device_id bmp280_id[] = { | |
399 | {"bmp280", 0}, | |
400 | { }, | |
401 | }; | |
402 | MODULE_DEVICE_TABLE(i2c, bmp280_id); | |
403 | ||
404 | static struct i2c_driver bmp280_driver = { | |
405 | .driver = { | |
406 | .name = "bmp280", | |
407 | .acpi_match_table = ACPI_PTR(bmp280_acpi_match), | |
408 | }, | |
409 | .probe = bmp280_probe, | |
410 | .id_table = bmp280_id, | |
411 | }; | |
412 | module_i2c_driver(bmp280_driver); | |
413 | ||
414 | MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>"); | |
415 | MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor"); | |
416 | MODULE_LICENSE("GPL v2"); |