iio: pressure: bmp280: support supply regulators
[deliverable/linux.git] / drivers / iio / pressure / bmp280.c
CommitLineData
d5c94568
VD
1/*
2 * Copyright (c) 2014 Intel Corporation
3 *
6dba72ec 4 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
d5c94568
VD
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 *
6dba72ec
AM
10 * Datasheet:
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
14beaa8f 13 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
d5c94568
VD
14 */
15
16#define pr_fmt(fmt) "bmp280: " fmt
17
18#include <linux/module.h>
19#include <linux/i2c.h>
20#include <linux/acpi.h>
78f50271 21#include <linux/of.h>
d5c94568 22#include <linux/regmap.h>
6dba72ec 23#include <linux/delay.h>
d5c94568
VD
24#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
c5842b47 26#include <linux/gpio/consumer.h>
bd525e6c 27#include <linux/regulator/consumer.h>
d5c94568 28
6dba72ec 29/* BMP280 specific registers */
14beaa8f
MR
30#define BMP280_REG_HUMIDITY_LSB 0xFE
31#define BMP280_REG_HUMIDITY_MSB 0xFD
d5c94568
VD
32#define BMP280_REG_TEMP_XLSB 0xFC
33#define BMP280_REG_TEMP_LSB 0xFB
34#define BMP280_REG_TEMP_MSB 0xFA
35#define BMP280_REG_PRESS_XLSB 0xF9
36#define BMP280_REG_PRESS_LSB 0xF8
37#define BMP280_REG_PRESS_MSB 0xF7
38
39#define BMP280_REG_CONFIG 0xF5
14beaa8f 40#define BMP280_REG_CTRL_MEAS 0xF4
d5c94568 41#define BMP280_REG_STATUS 0xF3
14beaa8f
MR
42#define BMP280_REG_CTRL_HUMIDITY 0xF2
43
44/* Due to non linear mapping, and data sizes we can't do a bulk read */
45#define BMP280_REG_COMP_H1 0xA1
46#define BMP280_REG_COMP_H2 0xE1
47#define BMP280_REG_COMP_H3 0xE3
48#define BMP280_REG_COMP_H4 0xE4
49#define BMP280_REG_COMP_H5 0xE5
50#define BMP280_REG_COMP_H6 0xE7
d5c94568
VD
51
52#define BMP280_REG_COMP_TEMP_START 0x88
53#define BMP280_COMP_TEMP_REG_COUNT 6
54
55#define BMP280_REG_COMP_PRESS_START 0x8E
56#define BMP280_COMP_PRESS_REG_COUNT 18
57
58#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
59#define BMP280_FILTER_OFF 0
60#define BMP280_FILTER_2X BIT(2)
61#define BMP280_FILTER_4X BIT(3)
62#define BMP280_FILTER_8X (BIT(3) | BIT(2))
63#define BMP280_FILTER_16X BIT(4)
64
14beaa8f
MR
65#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
66#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
67#define BMP280_OSRS_HUMIDITY_SKIP 0
68#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
69#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
70#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
71#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
72#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
73
d5c94568
VD
74#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
75#define BMP280_OSRS_TEMP_SKIP 0
62979904
AM
76#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
77#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
78#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
79#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
80#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
81#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
d5c94568
VD
82
83#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
84#define BMP280_OSRS_PRESS_SKIP 0
62979904
AM
85#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
86#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
87#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
88#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
89#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
90#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
d5c94568
VD
91
92#define BMP280_MODE_MASK (BIT(1) | BIT(0))
93#define BMP280_MODE_SLEEP 0
94#define BMP280_MODE_FORCED BIT(0)
95#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
96
6dba72ec
AM
97/* BMP180 specific registers */
98#define BMP180_REG_OUT_XLSB 0xF8
99#define BMP180_REG_OUT_LSB 0xF7
100#define BMP180_REG_OUT_MSB 0xF6
101
102#define BMP180_REG_CALIB_START 0xAA
103#define BMP180_REG_CALIB_COUNT 22
104
105#define BMP180_MEAS_SCO BIT(5)
106#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
107#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
108#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
109#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
110#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
111#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
112
113/* BMP180 and BMP280 common registers */
114#define BMP280_REG_CTRL_MEAS 0xF4
115#define BMP280_REG_RESET 0xE0
116#define BMP280_REG_ID 0xD0
117
118#define BMP180_CHIP_ID 0x55
d5c94568 119#define BMP280_CHIP_ID 0x58
14beaa8f 120#define BME280_CHIP_ID 0x60
d5c94568
VD
121#define BMP280_SOFT_RESET_VAL 0xB6
122
123struct bmp280_data {
124 struct i2c_client *client;
125 struct mutex lock;
126 struct regmap *regmap;
6dba72ec 127 const struct bmp280_chip_info *chip_info;
bd525e6c
LW
128 struct regulator *vddd;
129 struct regulator *vdda;
130 unsigned int start_up_time; /* in milliseconds */
d5c94568 131
62979904
AM
132 /* log of base 2 of oversampling rate */
133 u8 oversampling_press;
134 u8 oversampling_temp;
14beaa8f 135 u8 oversampling_humid;
62979904 136
d5c94568
VD
137 /*
138 * Carryover value from temperature conversion, used in pressure
139 * calculation.
140 */
141 s32 t_fine;
142};
143
6dba72ec
AM
144struct bmp280_chip_info {
145 const struct regmap_config *regmap_config;
146
62979904
AM
147 const int *oversampling_temp_avail;
148 int num_oversampling_temp_avail;
149
150 const int *oversampling_press_avail;
151 int num_oversampling_press_avail;
152
14beaa8f
MR
153 const int *oversampling_humid_avail;
154 int num_oversampling_humid_avail;
155
6dba72ec
AM
156 int (*chip_config)(struct bmp280_data *);
157 int (*read_temp)(struct bmp280_data *, int *);
158 int (*read_press)(struct bmp280_data *, int *, int *);
14beaa8f 159 int (*read_humid)(struct bmp280_data *, int *, int *);
6dba72ec
AM
160};
161
0f8994b1
VD
162/*
163 * These enums are used for indexing into the array of compensation
6dba72ec 164 * parameters for BMP280.
0f8994b1
VD
165 */
166enum { T1, T2, T3 };
167enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
d5c94568
VD
168
169static const struct iio_chan_spec bmp280_channels[] = {
170 {
171 .type = IIO_PRESSURE,
62979904
AM
172 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
173 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
d5c94568
VD
174 },
175 {
176 .type = IIO_TEMP,
62979904
AM
177 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
178 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
d5c94568 179 },
14beaa8f
MR
180 {
181 .type = IIO_HUMIDITYRELATIVE,
182 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
183 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
184 },
d5c94568
VD
185};
186
187static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
188{
189 switch (reg) {
190 case BMP280_REG_CONFIG:
14beaa8f 191 case BMP280_REG_CTRL_HUMIDITY:
d5c94568
VD
192 case BMP280_REG_CTRL_MEAS:
193 case BMP280_REG_RESET:
194 return true;
195 default:
196 return false;
197 };
198}
199
200static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
201{
202 switch (reg) {
14beaa8f
MR
203 case BMP280_REG_HUMIDITY_LSB:
204 case BMP280_REG_HUMIDITY_MSB:
d5c94568
VD
205 case BMP280_REG_TEMP_XLSB:
206 case BMP280_REG_TEMP_LSB:
207 case BMP280_REG_TEMP_MSB:
208 case BMP280_REG_PRESS_XLSB:
209 case BMP280_REG_PRESS_LSB:
210 case BMP280_REG_PRESS_MSB:
211 case BMP280_REG_STATUS:
212 return true;
213 default:
214 return false;
215 }
216}
217
218static const struct regmap_config bmp280_regmap_config = {
219 .reg_bits = 8,
220 .val_bits = 8,
221
14beaa8f 222 .max_register = BMP280_REG_HUMIDITY_LSB,
d5c94568
VD
223 .cache_type = REGCACHE_RBTREE,
224
225 .writeable_reg = bmp280_is_writeable_reg,
226 .volatile_reg = bmp280_is_volatile_reg,
227};
228
14beaa8f
MR
229/*
230 * Returns humidity in percent, resolution is 0.01 percent. Output value of
231 * "47445" represents 47445/1024 = 46.333 %RH.
232 *
233 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
234 */
235
236static u32 bmp280_compensate_humidity(struct bmp280_data *data,
237 s32 adc_humidity)
238{
239 struct device *dev = &data->client->dev;
240 unsigned int H1, H3, tmp;
241 int H2, H4, H5, H6, ret, var;
242
243 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
244 if (ret < 0) {
245 dev_err(dev, "failed to read H1 comp value\n");
246 return ret;
247 }
248
249 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
250 if (ret < 0) {
251 dev_err(dev, "failed to read H2 comp value\n");
252 return ret;
253 }
254 H2 = sign_extend32(le16_to_cpu(tmp), 15);
255
256 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
257 if (ret < 0) {
258 dev_err(dev, "failed to read H3 comp value\n");
259 return ret;
260 }
261
262 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
263 if (ret < 0) {
264 dev_err(dev, "failed to read H4 comp value\n");
265 return ret;
266 }
267 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
268 (be16_to_cpu(tmp) & 0xf), 11);
269
270 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
271 if (ret < 0) {
272 dev_err(dev, "failed to read H5 comp value\n");
273 return ret;
274 }
275 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
276
277 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
278 if (ret < 0) {
279 dev_err(dev, "failed to read H6 comp value\n");
280 return ret;
281 }
282 H6 = sign_extend32(tmp, 7);
283
284 var = ((s32)data->t_fine) - 76800;
285 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
286 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
287 + 2097152) * H2 + 8192) >> 14);
288 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
289
290 return var >> 12;
291};
292
d5c94568
VD
293/*
294 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
295 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
296 * value.
297 *
298 * Taken from datasheet, Section 3.11.3, "Compensation formula".
299 */
300static s32 bmp280_compensate_temp(struct bmp280_data *data,
d5c94568
VD
301 s32 adc_temp)
302{
0f8994b1 303 int ret;
44cf3798 304 s32 var1, var2;
0f8994b1
VD
305 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
306
307 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
308 buf, BMP280_COMP_TEMP_REG_COUNT);
309 if (ret < 0) {
310 dev_err(&data->client->dev,
311 "failed to read temperature calibration parameters\n");
312 return ret;
313 }
d5c94568 314
0f8994b1
VD
315 /*
316 * The double casts are necessary because le16_to_cpu returns an
317 * unsigned 16-bit value. Casting that value directly to a
318 * signed 32-bit will not do proper sign extension.
319 *
320 * Conversely, T1 and P1 are unsigned values, so they can be
321 * cast straight to the larger type.
322 */
323 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
324 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
325 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
326 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
327 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
abad3983 328 data->t_fine = var1 + var2;
d5c94568 329
44cf3798 330 return (data->t_fine * 5 + 128) >> 8;
d5c94568
VD
331}
332
333/*
334 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
335 * integer bits and 8 fractional bits). Output value of "24674867"
336 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
337 *
338 * Taken from datasheet, Section 3.11.3, "Compensation formula".
339 */
340static u32 bmp280_compensate_press(struct bmp280_data *data,
d5c94568
VD
341 s32 adc_press)
342{
0f8994b1 343 int ret;
d5c94568 344 s64 var1, var2, p;
0f8994b1
VD
345 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
346
347 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
348 buf, BMP280_COMP_PRESS_REG_COUNT);
349 if (ret < 0) {
350 dev_err(&data->client->dev,
351 "failed to read pressure calibration parameters\n");
352 return ret;
353 }
d5c94568 354
0f8994b1
VD
355 var1 = ((s64)data->t_fine) - 128000;
356 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
44cf3798
HK
357 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
358 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
0f8994b1
VD
359 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
360 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
361 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
d5c94568
VD
362
363 if (var1 == 0)
364 return 0;
365
0f8994b1 366 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
46ee98a2 367 p = div64_s64(p, var1);
0f8994b1
VD
368 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
369 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
370 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
d5c94568 371
44cf3798 372 return (u32)p;
d5c94568
VD
373}
374
375static int bmp280_read_temp(struct bmp280_data *data,
376 int *val)
377{
378 int ret;
379 __be32 tmp = 0;
380 s32 adc_temp, comp_temp;
d5c94568
VD
381
382 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
383 (u8 *) &tmp, 3);
384 if (ret < 0) {
385 dev_err(&data->client->dev, "failed to read temperature\n");
386 return ret;
387 }
388
389 adc_temp = be32_to_cpu(tmp) >> 12;
0f8994b1 390 comp_temp = bmp280_compensate_temp(data, adc_temp);
d5c94568
VD
391
392 /*
393 * val might be NULL if we're called by the read_press routine,
394 * who only cares about the carry over t_fine value.
395 */
396 if (val) {
397 *val = comp_temp * 10;
398 return IIO_VAL_INT;
399 }
400
401 return 0;
402}
403
404static int bmp280_read_press(struct bmp280_data *data,
405 int *val, int *val2)
406{
407 int ret;
408 __be32 tmp = 0;
409 s32 adc_press;
410 u32 comp_press;
d5c94568
VD
411
412 /* Read and compensate temperature so we get a reading of t_fine. */
413 ret = bmp280_read_temp(data, NULL);
414 if (ret < 0)
415 return ret;
416
417 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
418 (u8 *) &tmp, 3);
419 if (ret < 0) {
420 dev_err(&data->client->dev, "failed to read pressure\n");
421 return ret;
422 }
423
424 adc_press = be32_to_cpu(tmp) >> 12;
0f8994b1 425 comp_press = bmp280_compensate_press(data, adc_press);
d5c94568 426
81ebe850
HK
427 *val = comp_press;
428 *val2 = 256000;
d5c94568 429
81ebe850 430 return IIO_VAL_FRACTIONAL;
d5c94568
VD
431}
432
14beaa8f
MR
433static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
434{
435 int ret;
436 __be16 tmp = 0;
437 s32 adc_humidity;
438 u32 comp_humidity;
439
440 /* Read and compensate temperature so we get a reading of t_fine. */
441 ret = bmp280_read_temp(data, NULL);
442 if (ret < 0)
443 return ret;
444
445 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
446 (u8 *) &tmp, 2);
447 if (ret < 0) {
448 dev_err(&data->client->dev, "failed to read humidity\n");
449 return ret;
450 }
451
452 adc_humidity = be16_to_cpu(tmp);
453 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
454
455 *val = comp_humidity;
456 *val2 = 1024;
457
458 return IIO_VAL_FRACTIONAL;
459}
460
d5c94568
VD
461static int bmp280_read_raw(struct iio_dev *indio_dev,
462 struct iio_chan_spec const *chan,
463 int *val, int *val2, long mask)
464{
465 int ret;
466 struct bmp280_data *data = iio_priv(indio_dev);
467
468 mutex_lock(&data->lock);
469
470 switch (mask) {
471 case IIO_CHAN_INFO_PROCESSED:
472 switch (chan->type) {
14beaa8f
MR
473 case IIO_HUMIDITYRELATIVE:
474 ret = data->chip_info->read_humid(data, val, val2);
475 break;
d5c94568 476 case IIO_PRESSURE:
6dba72ec 477 ret = data->chip_info->read_press(data, val, val2);
d5c94568
VD
478 break;
479 case IIO_TEMP:
6dba72ec 480 ret = data->chip_info->read_temp(data, val);
d5c94568
VD
481 break;
482 default:
483 ret = -EINVAL;
484 break;
485 }
486 break;
62979904
AM
487 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
488 switch (chan->type) {
14beaa8f
MR
489 case IIO_HUMIDITYRELATIVE:
490 *val = 1 << data->oversampling_humid;
491 ret = IIO_VAL_INT;
492 break;
62979904
AM
493 case IIO_PRESSURE:
494 *val = 1 << data->oversampling_press;
495 ret = IIO_VAL_INT;
496 break;
497 case IIO_TEMP:
498 *val = 1 << data->oversampling_temp;
499 ret = IIO_VAL_INT;
500 break;
501 default:
502 ret = -EINVAL;
503 break;
504 }
505 break;
d5c94568
VD
506 default:
507 ret = -EINVAL;
508 break;
509 }
510
511 mutex_unlock(&data->lock);
512
513 return ret;
514}
515
14beaa8f
MR
516static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
517 int val)
518{
519 int i;
520 const int *avail = data->chip_info->oversampling_humid_avail;
521 const int n = data->chip_info->num_oversampling_humid_avail;
522
523 for (i = 0; i < n; i++) {
524 if (avail[i] == val) {
525 data->oversampling_humid = ilog2(val);
526
527 return data->chip_info->chip_config(data);
528 }
529 }
530 return -EINVAL;
531}
532
62979904
AM
533static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
534 int val)
535{
536 int i;
537 const int *avail = data->chip_info->oversampling_temp_avail;
538 const int n = data->chip_info->num_oversampling_temp_avail;
539
540 for (i = 0; i < n; i++) {
541 if (avail[i] == val) {
542 data->oversampling_temp = ilog2(val);
543
544 return data->chip_info->chip_config(data);
545 }
546 }
547 return -EINVAL;
548}
549
550static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
551 int val)
552{
553 int i;
554 const int *avail = data->chip_info->oversampling_press_avail;
555 const int n = data->chip_info->num_oversampling_press_avail;
556
557 for (i = 0; i < n; i++) {
558 if (avail[i] == val) {
559 data->oversampling_press = ilog2(val);
560
561 return data->chip_info->chip_config(data);
562 }
563 }
564 return -EINVAL;
565}
566
567static int bmp280_write_raw(struct iio_dev *indio_dev,
568 struct iio_chan_spec const *chan,
569 int val, int val2, long mask)
570{
571 int ret = 0;
572 struct bmp280_data *data = iio_priv(indio_dev);
573
574 switch (mask) {
575 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
576 mutex_lock(&data->lock);
577 switch (chan->type) {
14beaa8f
MR
578 case IIO_HUMIDITYRELATIVE:
579 ret = bmp280_write_oversampling_ratio_humid(data, val);
580 break;
62979904
AM
581 case IIO_PRESSURE:
582 ret = bmp280_write_oversampling_ratio_press(data, val);
583 break;
584 case IIO_TEMP:
585 ret = bmp280_write_oversampling_ratio_temp(data, val);
586 break;
587 default:
588 ret = -EINVAL;
589 break;
590 }
591 mutex_unlock(&data->lock);
592 break;
593 default:
594 return -EINVAL;
595 }
596
597 return ret;
598}
599
600static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
601{
602 size_t len = 0;
603 int i;
604
605 for (i = 0; i < n; i++)
606 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
607
608 buf[len - 1] = '\n';
609
610 return len;
611}
612
613static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
614 struct device_attribute *attr, char *buf)
615{
616 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
617
618 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
619 data->chip_info->num_oversampling_temp_avail);
620}
621
622static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
623 struct device_attribute *attr, char *buf)
624{
625 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
626
627 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
628 data->chip_info->num_oversampling_press_avail);
629}
630
631static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
632 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
633
634static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
635 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
636
637static struct attribute *bmp280_attributes[] = {
638 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
639 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
640 NULL,
641};
642
643static const struct attribute_group bmp280_attrs_group = {
644 .attrs = bmp280_attributes,
645};
646
d5c94568
VD
647static const struct iio_info bmp280_info = {
648 .driver_module = THIS_MODULE,
649 .read_raw = &bmp280_read_raw,
62979904
AM
650 .write_raw = &bmp280_write_raw,
651 .attrs = &bmp280_attrs_group,
d5c94568
VD
652};
653
6dba72ec 654static int bmp280_chip_config(struct bmp280_data *data)
d5c94568
VD
655{
656 int ret;
62979904
AM
657 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
658 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
d5c94568
VD
659
660 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
661 BMP280_OSRS_TEMP_MASK |
662 BMP280_OSRS_PRESS_MASK |
663 BMP280_MODE_MASK,
62979904 664 osrs | BMP280_MODE_NORMAL);
d5c94568
VD
665 if (ret < 0) {
666 dev_err(&data->client->dev,
44cf3798 667 "failed to write ctrl_meas register\n");
d5c94568
VD
668 return ret;
669 }
670
671 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
672 BMP280_FILTER_MASK,
673 BMP280_FILTER_4X);
674 if (ret < 0) {
675 dev_err(&data->client->dev,
676 "failed to write config register\n");
677 return ret;
678 }
679
680 return ret;
681}
682
62979904
AM
683static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
684
6dba72ec
AM
685static const struct bmp280_chip_info bmp280_chip_info = {
686 .regmap_config = &bmp280_regmap_config,
62979904
AM
687
688 .oversampling_temp_avail = bmp280_oversampling_avail,
689 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
690
691 .oversampling_press_avail = bmp280_oversampling_avail,
692 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
693
6dba72ec
AM
694 .chip_config = bmp280_chip_config,
695 .read_temp = bmp280_read_temp,
696 .read_press = bmp280_read_press,
697};
698
14beaa8f
MR
699static int bme280_chip_config(struct bmp280_data *data)
700{
701 int ret = bmp280_chip_config(data);
702 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
703
704 if (ret < 0)
705 return ret;
706
707 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
708 BMP280_OSRS_HUMIDITY_MASK, osrs);
709}
710
711static const struct bmp280_chip_info bme280_chip_info = {
712 .regmap_config = &bmp280_regmap_config,
713
714 .oversampling_temp_avail = bmp280_oversampling_avail,
715 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
716
717 .oversampling_press_avail = bmp280_oversampling_avail,
718 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
719
720 .oversampling_humid_avail = bmp280_oversampling_avail,
721 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
722
723 .chip_config = bme280_chip_config,
724 .read_temp = bmp280_read_temp,
725 .read_press = bmp280_read_press,
726 .read_humid = bmp280_read_humid,
727};
728
729
6dba72ec
AM
730static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
731{
732 switch (reg) {
733 case BMP280_REG_CTRL_MEAS:
734 case BMP280_REG_RESET:
735 return true;
736 default:
737 return false;
738 };
739}
740
741static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
742{
743 switch (reg) {
744 case BMP180_REG_OUT_XLSB:
745 case BMP180_REG_OUT_LSB:
746 case BMP180_REG_OUT_MSB:
747 case BMP280_REG_CTRL_MEAS:
748 return true;
749 default:
750 return false;
751 }
752}
753
754static const struct regmap_config bmp180_regmap_config = {
755 .reg_bits = 8,
756 .val_bits = 8,
757
758 .max_register = BMP180_REG_OUT_XLSB,
759 .cache_type = REGCACHE_RBTREE,
760
761 .writeable_reg = bmp180_is_writeable_reg,
762 .volatile_reg = bmp180_is_volatile_reg,
763};
764
765static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
766{
767 int ret;
768 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
769 unsigned int delay_us;
770 unsigned int ctrl;
771
772 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
773 if (ret)
774 return ret;
775
776 if (ctrl_meas == BMP180_MEAS_TEMP)
777 delay_us = 4500;
778 else
62979904 779 delay_us = conversion_time_max[data->oversampling_press];
6dba72ec
AM
780
781 usleep_range(delay_us, delay_us + 1000);
782
783 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
784 if (ret)
785 return ret;
786
787 /* The value of this bit reset to "0" after conversion is complete */
788 if (ctrl & BMP180_MEAS_SCO)
789 return -EIO;
790
791 return 0;
792}
793
794static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
795{
796 int ret;
797 __be16 tmp = 0;
798
799 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
800 if (ret)
801 return ret;
802
803 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
804 if (ret)
805 return ret;
806
807 *val = be16_to_cpu(tmp);
808
809 return 0;
810}
811
812/*
813 * These enums are used for indexing into the array of calibration
814 * coefficients for BMP180.
815 */
816enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
817
818struct bmp180_calib {
819 s16 AC1;
820 s16 AC2;
821 s16 AC3;
822 u16 AC4;
823 u16 AC5;
824 u16 AC6;
825 s16 B1;
826 s16 B2;
827 s16 MB;
828 s16 MC;
829 s16 MD;
830};
831
832static int bmp180_read_calib(struct bmp280_data *data,
833 struct bmp180_calib *calib)
834{
835 int ret;
836 int i;
837 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
838
839 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
840 sizeof(buf));
841
842 if (ret < 0)
843 return ret;
844
845 /* None of the words has the value 0 or 0xFFFF */
846 for (i = 0; i < ARRAY_SIZE(buf); i++) {
847 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
848 return -EIO;
849 }
850
851 calib->AC1 = be16_to_cpu(buf[AC1]);
852 calib->AC2 = be16_to_cpu(buf[AC2]);
853 calib->AC3 = be16_to_cpu(buf[AC3]);
854 calib->AC4 = be16_to_cpu(buf[AC4]);
855 calib->AC5 = be16_to_cpu(buf[AC5]);
856 calib->AC6 = be16_to_cpu(buf[AC6]);
857 calib->B1 = be16_to_cpu(buf[B1]);
858 calib->B2 = be16_to_cpu(buf[B2]);
859 calib->MB = be16_to_cpu(buf[MB]);
860 calib->MC = be16_to_cpu(buf[MC]);
861 calib->MD = be16_to_cpu(buf[MD]);
862
863 return 0;
864}
865
866/*
867 * Returns temperature in DegC, resolution is 0.1 DegC.
868 * t_fine carries fine temperature as global value.
869 *
870 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
871 */
872static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
873{
874 int ret;
875 s32 x1, x2;
876 struct bmp180_calib calib;
877
878 ret = bmp180_read_calib(data, &calib);
879 if (ret < 0) {
880 dev_err(&data->client->dev,
881 "failed to read calibration coefficients\n");
882 return ret;
883 }
884
885 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
886 x2 = (calib.MC << 11) / (x1 + calib.MD);
887 data->t_fine = x1 + x2;
888
889 return (data->t_fine + 8) >> 4;
890}
891
892static int bmp180_read_temp(struct bmp280_data *data, int *val)
893{
894 int ret;
895 s32 adc_temp, comp_temp;
896
897 ret = bmp180_read_adc_temp(data, &adc_temp);
898 if (ret)
899 return ret;
900
901 comp_temp = bmp180_compensate_temp(data, adc_temp);
902
903 /*
904 * val might be NULL if we're called by the read_press routine,
905 * who only cares about the carry over t_fine value.
906 */
907 if (val) {
908 *val = comp_temp * 100;
909 return IIO_VAL_INT;
910 }
911
912 return 0;
913}
914
915static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
916{
917 int ret;
918 __be32 tmp = 0;
62979904 919 u8 oss = data->oversampling_press;
6dba72ec
AM
920
921 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
922 if (ret)
923 return ret;
924
925 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
926 if (ret)
927 return ret;
928
929 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
930
931 return 0;
932}
933
934/*
935 * Returns pressure in Pa, resolution is 1 Pa.
936 *
937 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
938 */
939static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
940{
941 int ret;
942 s32 x1, x2, x3, p;
943 s32 b3, b6;
944 u32 b4, b7;
62979904 945 s32 oss = data->oversampling_press;
6dba72ec
AM
946 struct bmp180_calib calib;
947
948 ret = bmp180_read_calib(data, &calib);
949 if (ret < 0) {
950 dev_err(&data->client->dev,
951 "failed to read calibration coefficients\n");
952 return ret;
953 }
954
955 b6 = data->t_fine - 4000;
956 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
957 x2 = calib.AC2 * b6 >> 11;
958 x3 = x1 + x2;
959 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
960 x1 = calib.AC3 * b6 >> 13;
961 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
962 x3 = (x1 + x2 + 2) >> 2;
963 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
964 b7 = ((u32)adc_press - b3) * (50000 >> oss);
965 if (b7 < 0x80000000)
966 p = (b7 * 2) / b4;
967 else
968 p = (b7 / b4) * 2;
969
970 x1 = (p >> 8) * (p >> 8);
971 x1 = (x1 * 3038) >> 16;
972 x2 = (-7357 * p) >> 16;
973
974 return p + ((x1 + x2 + 3791) >> 4);
975}
976
977static int bmp180_read_press(struct bmp280_data *data,
978 int *val, int *val2)
979{
980 int ret;
981 s32 adc_press;
982 u32 comp_press;
983
984 /* Read and compensate temperature so we get a reading of t_fine. */
985 ret = bmp180_read_temp(data, NULL);
986 if (ret)
987 return ret;
988
989 ret = bmp180_read_adc_press(data, &adc_press);
990 if (ret)
991 return ret;
992
993 comp_press = bmp180_compensate_press(data, adc_press);
994
995 *val = comp_press;
996 *val2 = 1000;
997
998 return IIO_VAL_FRACTIONAL;
999}
1000
1001static int bmp180_chip_config(struct bmp280_data *data)
1002{
1003 return 0;
1004}
1005
62979904
AM
1006static const int bmp180_oversampling_temp_avail[] = { 1 };
1007static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1008
6dba72ec
AM
1009static const struct bmp280_chip_info bmp180_chip_info = {
1010 .regmap_config = &bmp180_regmap_config,
62979904
AM
1011
1012 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
1013 .num_oversampling_temp_avail =
1014 ARRAY_SIZE(bmp180_oversampling_temp_avail),
1015
1016 .oversampling_press_avail = bmp180_oversampling_press_avail,
1017 .num_oversampling_press_avail =
1018 ARRAY_SIZE(bmp180_oversampling_press_avail),
1019
6dba72ec
AM
1020 .chip_config = bmp180_chip_config,
1021 .read_temp = bmp180_read_temp,
1022 .read_press = bmp180_read_press,
1023};
1024
d5c94568
VD
1025static int bmp280_probe(struct i2c_client *client,
1026 const struct i2c_device_id *id)
1027{
1028 int ret;
1029 struct iio_dev *indio_dev;
1030 struct bmp280_data *data;
1031 unsigned int chip_id;
c5842b47 1032 struct gpio_desc *gpiod;
d5c94568
VD
1033
1034 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1035 if (!indio_dev)
1036 return -ENOMEM;
1037
d5c94568
VD
1038 data = iio_priv(indio_dev);
1039 mutex_init(&data->lock);
1040 data->client = client;
1041
1042 indio_dev->dev.parent = &client->dev;
1043 indio_dev->name = id->name;
1044 indio_dev->channels = bmp280_channels;
d5c94568
VD
1045 indio_dev->info = &bmp280_info;
1046 indio_dev->modes = INDIO_DIRECT_MODE;
1047
6dba72ec
AM
1048 switch (id->driver_data) {
1049 case BMP180_CHIP_ID:
14beaa8f 1050 indio_dev->num_channels = 2;
6dba72ec 1051 data->chip_info = &bmp180_chip_info;
62979904
AM
1052 data->oversampling_press = ilog2(8);
1053 data->oversampling_temp = ilog2(1);
bd525e6c 1054 data->start_up_time = 10;
6dba72ec
AM
1055 break;
1056 case BMP280_CHIP_ID:
14beaa8f 1057 indio_dev->num_channels = 2;
6dba72ec 1058 data->chip_info = &bmp280_chip_info;
62979904
AM
1059 data->oversampling_press = ilog2(16);
1060 data->oversampling_temp = ilog2(2);
bd525e6c 1061 data->start_up_time = 2;
6dba72ec 1062 break;
14beaa8f
MR
1063 case BME280_CHIP_ID:
1064 indio_dev->num_channels = 3;
1065 data->chip_info = &bme280_chip_info;
1066 data->oversampling_press = ilog2(16);
1067 data->oversampling_humid = ilog2(16);
1068 data->oversampling_temp = ilog2(2);
bd525e6c 1069 data->start_up_time = 2;
14beaa8f 1070 break;
6dba72ec
AM
1071 default:
1072 return -EINVAL;
1073 }
1074
bd525e6c
LW
1075 /* Bring up regulators */
1076 data->vddd = devm_regulator_get(&client->dev, "vddd");
1077 if (IS_ERR(data->vddd)) {
1078 dev_err(&client->dev, "failed to get VDDD regulator\n");
1079 return PTR_ERR(data->vddd);
1080 }
1081 ret = regulator_enable(data->vddd);
1082 if (ret) {
1083 dev_err(&client->dev, "failed to enable VDDD regulator\n");
1084 return ret;
1085 }
1086 data->vdda = devm_regulator_get(&client->dev, "vdda");
1087 if (IS_ERR(data->vdda)) {
1088 dev_err(&client->dev, "failed to get VDDA regulator\n");
1089 ret = PTR_ERR(data->vddd);
1090 goto out_disable_vddd;
1091 }
1092 ret = regulator_enable(data->vdda);
1093 if (ret) {
1094 dev_err(&client->dev, "failed to enable VDDA regulator\n");
1095 goto out_disable_vddd;
1096 }
1097 /* Wait to make sure we started up properly */
1098 mdelay(data->start_up_time);
1099
c5842b47
LW
1100 /* Bring chip out of reset if there is an assigned GPIO line */
1101 gpiod = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
1102 /* Deassert the signal */
1103 if (!IS_ERR(gpiod)) {
1104 dev_info(&client->dev, "release reset\n");
1105 gpiod_set_value(gpiod, 0);
1106 }
1107
6dba72ec
AM
1108 data->regmap = devm_regmap_init_i2c(client,
1109 data->chip_info->regmap_config);
d5c94568
VD
1110 if (IS_ERR(data->regmap)) {
1111 dev_err(&client->dev, "failed to allocate register map\n");
bd525e6c
LW
1112 ret = PTR_ERR(data->regmap);
1113 goto out_disable_vdda;
d5c94568
VD
1114 }
1115
1116 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
1117 if (ret < 0)
1118 return ret;
6dba72ec 1119 if (chip_id != id->driver_data) {
a3e5afe4
AM
1120 dev_err(&client->dev, "bad chip id. expected %lx got %x\n",
1121 id->driver_data, chip_id);
bd525e6c
LW
1122 ret = -EINVAL;
1123 goto out_disable_vdda;
d5c94568
VD
1124 }
1125
6dba72ec 1126 ret = data->chip_info->chip_config(data);
d5c94568 1127 if (ret < 0)
bd525e6c 1128 goto out_disable_vdda;
d5c94568 1129
bd525e6c
LW
1130 i2c_set_clientdata(client, indio_dev);
1131
1132 ret = iio_device_register(indio_dev);
1133 if (ret)
1134 goto out_disable_vdda;
1135
1136 return 0;
1137
1138out_disable_vdda:
1139 regulator_disable(data->vdda);
1140out_disable_vddd:
1141 regulator_disable(data->vddd);
1142 return ret;
1143}
1144
1145static int bmp280_remove(struct i2c_client *client)
1146{
1147 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1148 struct bmp280_data *data = iio_priv(indio_dev);
1149
1150 iio_device_unregister(indio_dev);
1151 regulator_disable(data->vdda);
1152 regulator_disable(data->vddd);
1153 return 0;
d5c94568
VD
1154}
1155
1156static const struct acpi_device_id bmp280_acpi_match[] = {
6dba72ec
AM
1157 {"BMP0280", BMP280_CHIP_ID },
1158 {"BMP0180", BMP180_CHIP_ID },
1159 {"BMP0085", BMP180_CHIP_ID },
14beaa8f 1160 {"BME0280", BME280_CHIP_ID },
d5c94568
VD
1161 { },
1162};
1163MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
1164
78f50271
LW
1165#ifdef CONFIG_OF
1166static const struct of_device_id bmp280_of_match[] = {
1167 { .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
1168 { .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
1169 { .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
1170 { .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
1171 { },
1172};
1173MODULE_DEVICE_TABLE(of, bmp280_of_match);
1174#else
1175#define bmp280_of_match NULL
1176#endif
1177
d5c94568 1178static const struct i2c_device_id bmp280_id[] = {
6dba72ec
AM
1179 {"bmp280", BMP280_CHIP_ID },
1180 {"bmp180", BMP180_CHIP_ID },
1181 {"bmp085", BMP180_CHIP_ID },
14beaa8f 1182 {"bme280", BME280_CHIP_ID },
d5c94568
VD
1183 { },
1184};
1185MODULE_DEVICE_TABLE(i2c, bmp280_id);
1186
1187static struct i2c_driver bmp280_driver = {
1188 .driver = {
1189 .name = "bmp280",
1190 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
78f50271 1191 .of_match_table = of_match_ptr(bmp280_of_match),
d5c94568
VD
1192 },
1193 .probe = bmp280_probe,
bd525e6c 1194 .remove = bmp280_remove,
d5c94568
VD
1195 .id_table = bmp280_id,
1196};
1197module_i2c_driver(bmp280_driver);
1198
1199MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
6dba72ec 1200MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
d5c94568 1201MODULE_LICENSE("GPL v2");
This page took 0.166739 seconds and 5 git commands to generate.