blob: 47dfd347f66f88c77650ececa1fabcff60bbffb8 [file] [log] [blame]
Vlad Dogarud5c94562014-10-21 11:09:58 +03001/*
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
71struct 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
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020083/*
84 * These enums are used for indexing into the array of compensation
85 * parameters.
86 */
87enum { T1, T2, T3 };
88enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +030089
90static 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
101static 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
113static 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
129static 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
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200140/*
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 */
147static s32 bmp280_compensate_temp(struct bmp280_data *data,
148 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300149{
150 int ret;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200151 s32 var1, var2, t;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300152 __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 }
161
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200162 /*
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;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300175
176 data->t_fine = var1 + var2;
177 t = (data->t_fine * 5 + 128) >> 8;
178
179 return t;
180}
181
182/*
183 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
184 * integer bits and 8 fractional bits). Output value of "24674867"
185 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
186 *
187 * Taken from datasheet, Section 3.11.3, "Compensation formula".
188 */
189static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300190 s32 adc_press)
191{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200192 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300193 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200194 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300195
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200196 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
197 buf, BMP280_COMP_PRESS_REG_COUNT);
198 if (ret < 0) {
199 dev_err(&data->client->dev,
200 "failed to read pressure calibration parameters\n");
201 return ret;
202 }
203
204 var1 = ((s64)data->t_fine) - 128000;
205 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
206 var2 = var2 + ((var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17);
207 var2 = var2 + (((s64)(s16)le16_to_cpu(buf[P4])) << 35);
208 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
209 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
210 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300211
212 if (var1 == 0)
213 return 0;
214
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200215 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100216 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200217 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
218 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
219 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300220
221 return (u32) p;
222}
223
224static int bmp280_read_temp(struct bmp280_data *data,
225 int *val)
226{
227 int ret;
228 __be32 tmp = 0;
229 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300230
231 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
232 (u8 *) &tmp, 3);
233 if (ret < 0) {
234 dev_err(&data->client->dev, "failed to read temperature\n");
235 return ret;
236 }
237
238 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200239 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300240
241 /*
242 * val might be NULL if we're called by the read_press routine,
243 * who only cares about the carry over t_fine value.
244 */
245 if (val) {
246 *val = comp_temp * 10;
247 return IIO_VAL_INT;
248 }
249
250 return 0;
251}
252
253static int bmp280_read_press(struct bmp280_data *data,
254 int *val, int *val2)
255{
256 int ret;
257 __be32 tmp = 0;
258 s32 adc_press;
259 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300260
261 /* Read and compensate temperature so we get a reading of t_fine. */
262 ret = bmp280_read_temp(data, NULL);
263 if (ret < 0)
264 return ret;
265
266 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
267 (u8 *) &tmp, 3);
268 if (ret < 0) {
269 dev_err(&data->client->dev, "failed to read pressure\n");
270 return ret;
271 }
272
273 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200274 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300275
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000276 *val = comp_press;
277 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300278
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000279 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300280}
281
282static int bmp280_read_raw(struct iio_dev *indio_dev,
283 struct iio_chan_spec const *chan,
284 int *val, int *val2, long mask)
285{
286 int ret;
287 struct bmp280_data *data = iio_priv(indio_dev);
288
289 mutex_lock(&data->lock);
290
291 switch (mask) {
292 case IIO_CHAN_INFO_PROCESSED:
293 switch (chan->type) {
294 case IIO_PRESSURE:
295 ret = bmp280_read_press(data, val, val2);
296 break;
297 case IIO_TEMP:
298 ret = bmp280_read_temp(data, val);
299 break;
300 default:
301 ret = -EINVAL;
302 break;
303 }
304 break;
305 default:
306 ret = -EINVAL;
307 break;
308 }
309
310 mutex_unlock(&data->lock);
311
312 return ret;
313}
314
315static const struct iio_info bmp280_info = {
316 .driver_module = THIS_MODULE,
317 .read_raw = &bmp280_read_raw,
318};
319
320static int bmp280_chip_init(struct bmp280_data *data)
321{
322 int ret;
323
324 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
325 BMP280_OSRS_TEMP_MASK |
326 BMP280_OSRS_PRESS_MASK |
327 BMP280_MODE_MASK,
328 BMP280_OSRS_TEMP_2X |
329 BMP280_OSRS_PRESS_16X |
330 BMP280_MODE_NORMAL);
331 if (ret < 0) {
332 dev_err(&data->client->dev,
333 "failed to write config register\n");
334 return ret;
335 }
336
337 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
338 BMP280_FILTER_MASK,
339 BMP280_FILTER_4X);
340 if (ret < 0) {
341 dev_err(&data->client->dev,
342 "failed to write config register\n");
343 return ret;
344 }
345
346 return ret;
347}
348
349static int bmp280_probe(struct i2c_client *client,
350 const struct i2c_device_id *id)
351{
352 int ret;
353 struct iio_dev *indio_dev;
354 struct bmp280_data *data;
355 unsigned int chip_id;
356
357 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
358 if (!indio_dev)
359 return -ENOMEM;
360
361 i2c_set_clientdata(client, indio_dev);
362 data = iio_priv(indio_dev);
363 mutex_init(&data->lock);
364 data->client = client;
365
366 indio_dev->dev.parent = &client->dev;
367 indio_dev->name = id->name;
368 indio_dev->channels = bmp280_channels;
369 indio_dev->num_channels = ARRAY_SIZE(bmp280_channels);
370 indio_dev->info = &bmp280_info;
371 indio_dev->modes = INDIO_DIRECT_MODE;
372
373 data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config);
374 if (IS_ERR(data->regmap)) {
375 dev_err(&client->dev, "failed to allocate register map\n");
376 return PTR_ERR(data->regmap);
377 }
378
379 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
380 if (ret < 0)
381 return ret;
382 if (chip_id != BMP280_CHIP_ID) {
383 dev_err(&client->dev, "bad chip id. expected %x got %x\n",
384 BMP280_CHIP_ID, chip_id);
385 return -EINVAL;
386 }
387
388 ret = bmp280_chip_init(data);
389 if (ret < 0)
390 return ret;
391
392 return devm_iio_device_register(&client->dev, indio_dev);
393}
394
395static const struct acpi_device_id bmp280_acpi_match[] = {
396 {"BMP0280", 0},
397 { },
398};
399MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
400
401static const struct i2c_device_id bmp280_id[] = {
402 {"bmp280", 0},
403 { },
404};
405MODULE_DEVICE_TABLE(i2c, bmp280_id);
406
407static struct i2c_driver bmp280_driver = {
408 .driver = {
409 .name = "bmp280",
410 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
411 },
412 .probe = bmp280_probe,
413 .id_table = bmp280_id,
414};
415module_i2c_driver(bmp280_driver);
416
417MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
418MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor");
419MODULE_LICENSE("GPL v2");