blob: 7c623e2bd6336c0968254e73547de8945efe4a42 [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;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100151 s32 var1, var2;
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
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100176 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300177}
178
179/*
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
183 *
184 * Taken from datasheet, Section 3.11.3, "Compensation formula".
185 */
186static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300187 s32 adc_press)
188{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200189 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300190 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200191 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300192
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200193 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
194 buf, BMP280_COMP_PRESS_REG_COUNT);
195 if (ret < 0) {
196 dev_err(&data->client->dev,
197 "failed to read pressure calibration parameters\n");
198 return ret;
199 }
200
201 var1 = ((s64)data->t_fine) - 128000;
202 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100203 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
204 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200205 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;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300208
209 if (var1 == 0)
210 return 0;
211
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200212 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100213 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200214 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);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300217
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100218 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300219}
220
221static int bmp280_read_temp(struct bmp280_data *data,
222 int *val)
223{
224 int ret;
225 __be32 tmp = 0;
226 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300227
228 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
229 (u8 *) &tmp, 3);
230 if (ret < 0) {
231 dev_err(&data->client->dev, "failed to read temperature\n");
232 return ret;
233 }
234
235 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200236 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300237
238 /*
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.
241 */
242 if (val) {
243 *val = comp_temp * 10;
244 return IIO_VAL_INT;
245 }
246
247 return 0;
248}
249
250static int bmp280_read_press(struct bmp280_data *data,
251 int *val, int *val2)
252{
253 int ret;
254 __be32 tmp = 0;
255 s32 adc_press;
256 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300257
258 /* Read and compensate temperature so we get a reading of t_fine. */
259 ret = bmp280_read_temp(data, NULL);
260 if (ret < 0)
261 return ret;
262
263 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
264 (u8 *) &tmp, 3);
265 if (ret < 0) {
266 dev_err(&data->client->dev, "failed to read pressure\n");
267 return ret;
268 }
269
270 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200271 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300272
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000273 *val = comp_press;
274 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300275
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000276 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300277}
278
279static int bmp280_read_raw(struct iio_dev *indio_dev,
280 struct iio_chan_spec const *chan,
281 int *val, int *val2, long mask)
282{
283 int ret;
284 struct bmp280_data *data = iio_priv(indio_dev);
285
286 mutex_lock(&data->lock);
287
288 switch (mask) {
289 case IIO_CHAN_INFO_PROCESSED:
290 switch (chan->type) {
291 case IIO_PRESSURE:
292 ret = bmp280_read_press(data, val, val2);
293 break;
294 case IIO_TEMP:
295 ret = bmp280_read_temp(data, val);
296 break;
297 default:
298 ret = -EINVAL;
299 break;
300 }
301 break;
302 default:
303 ret = -EINVAL;
304 break;
305 }
306
307 mutex_unlock(&data->lock);
308
309 return ret;
310}
311
312static const struct iio_info bmp280_info = {
313 .driver_module = THIS_MODULE,
314 .read_raw = &bmp280_read_raw,
315};
316
317static int bmp280_chip_init(struct bmp280_data *data)
318{
319 int ret;
320
321 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
322 BMP280_OSRS_TEMP_MASK |
323 BMP280_OSRS_PRESS_MASK |
324 BMP280_MODE_MASK,
325 BMP280_OSRS_TEMP_2X |
326 BMP280_OSRS_PRESS_16X |
327 BMP280_MODE_NORMAL);
328 if (ret < 0) {
329 dev_err(&data->client->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100330 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300331 return ret;
332 }
333
334 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
335 BMP280_FILTER_MASK,
336 BMP280_FILTER_4X);
337 if (ret < 0) {
338 dev_err(&data->client->dev,
339 "failed to write config register\n");
340 return ret;
341 }
342
343 return ret;
344}
345
346static int bmp280_probe(struct i2c_client *client,
347 const struct i2c_device_id *id)
348{
349 int ret;
350 struct iio_dev *indio_dev;
351 struct bmp280_data *data;
352 unsigned int chip_id;
353
354 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
355 if (!indio_dev)
356 return -ENOMEM;
357
Vlad Dogarud5c94562014-10-21 11:09:58 +0300358 data = iio_priv(indio_dev);
359 mutex_init(&data->lock);
360 data->client = client;
361
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;
368
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);
373 }
374
375 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
376 if (ret < 0)
377 return ret;
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);
381 return -EINVAL;
382 }
383
384 ret = bmp280_chip_init(data);
385 if (ret < 0)
386 return ret;
387
388 return devm_iio_device_register(&client->dev, indio_dev);
389}
390
391static const struct acpi_device_id bmp280_acpi_match[] = {
392 {"BMP0280", 0},
393 { },
394};
395MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
396
397static const struct i2c_device_id bmp280_id[] = {
398 {"bmp280", 0},
399 { },
400};
401MODULE_DEVICE_TABLE(i2c, bmp280_id);
402
403static struct i2c_driver bmp280_driver = {
404 .driver = {
405 .name = "bmp280",
406 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
407 },
408 .probe = bmp280_probe,
409 .id_table = bmp280_id,
410};
411module_i2c_driver(bmp280_driver);
412
413MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
414MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor");
415MODULE_LICENSE("GPL v2");