mmc: rtsx_pci: Enable MMC_CAP_ERASE to allow erase/discard/trim requests
[linux/fpc-iii.git] / drivers / iio / pressure / bmp280.c
blob724452d61846c67155120876af55921f56e54c6a
1 /*
2 * Copyright (c) 2014 Intel Corporation
4 * Driver for Bosch Sensortec BMP180 and 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.
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
15 #define pr_fmt(fmt) "bmp280: " fmt
17 #include <linux/module.h>
18 #include <linux/i2c.h>
19 #include <linux/acpi.h>
20 #include <linux/regmap.h>
21 #include <linux/delay.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/sysfs.h>
25 /* BMP280 specific registers */
26 #define BMP280_REG_TEMP_XLSB 0xFC
27 #define BMP280_REG_TEMP_LSB 0xFB
28 #define BMP280_REG_TEMP_MSB 0xFA
29 #define BMP280_REG_PRESS_XLSB 0xF9
30 #define BMP280_REG_PRESS_LSB 0xF8
31 #define BMP280_REG_PRESS_MSB 0xF7
33 #define BMP280_REG_CONFIG 0xF5
34 #define BMP280_REG_STATUS 0xF3
36 #define BMP280_REG_COMP_TEMP_START 0x88
37 #define BMP280_COMP_TEMP_REG_COUNT 6
39 #define BMP280_REG_COMP_PRESS_START 0x8E
40 #define BMP280_COMP_PRESS_REG_COUNT 18
42 #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
43 #define BMP280_FILTER_OFF 0
44 #define BMP280_FILTER_2X BIT(2)
45 #define BMP280_FILTER_4X BIT(3)
46 #define BMP280_FILTER_8X (BIT(3) | BIT(2))
47 #define BMP280_FILTER_16X BIT(4)
49 #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
50 #define BMP280_OSRS_TEMP_SKIP 0
51 #define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
52 #define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
53 #define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
54 #define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
55 #define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
56 #define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
58 #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
59 #define BMP280_OSRS_PRESS_SKIP 0
60 #define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
61 #define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
62 #define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
63 #define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
64 #define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
65 #define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
67 #define BMP280_MODE_MASK (BIT(1) | BIT(0))
68 #define BMP280_MODE_SLEEP 0
69 #define BMP280_MODE_FORCED BIT(0)
70 #define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
72 /* BMP180 specific registers */
73 #define BMP180_REG_OUT_XLSB 0xF8
74 #define BMP180_REG_OUT_LSB 0xF7
75 #define BMP180_REG_OUT_MSB 0xF6
77 #define BMP180_REG_CALIB_START 0xAA
78 #define BMP180_REG_CALIB_COUNT 22
80 #define BMP180_MEAS_SCO BIT(5)
81 #define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
82 #define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
83 #define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
84 #define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
85 #define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
86 #define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
88 /* BMP180 and BMP280 common registers */
89 #define BMP280_REG_CTRL_MEAS 0xF4
90 #define BMP280_REG_RESET 0xE0
91 #define BMP280_REG_ID 0xD0
93 #define BMP180_CHIP_ID 0x55
94 #define BMP280_CHIP_ID 0x58
95 #define BMP280_SOFT_RESET_VAL 0xB6
97 struct bmp280_data {
98 struct i2c_client *client;
99 struct mutex lock;
100 struct regmap *regmap;
101 const struct bmp280_chip_info *chip_info;
103 /* log of base 2 of oversampling rate */
104 u8 oversampling_press;
105 u8 oversampling_temp;
108 * Carryover value from temperature conversion, used in pressure
109 * calculation.
111 s32 t_fine;
114 struct bmp280_chip_info {
115 const struct regmap_config *regmap_config;
117 const int *oversampling_temp_avail;
118 int num_oversampling_temp_avail;
120 const int *oversampling_press_avail;
121 int num_oversampling_press_avail;
123 int (*chip_config)(struct bmp280_data *);
124 int (*read_temp)(struct bmp280_data *, int *);
125 int (*read_press)(struct bmp280_data *, int *, int *);
129 * These enums are used for indexing into the array of compensation
130 * parameters for BMP280.
132 enum { T1, T2, T3 };
133 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
135 static const struct iio_chan_spec bmp280_channels[] = {
137 .type = IIO_PRESSURE,
138 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
139 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
142 .type = IIO_TEMP,
143 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
144 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
148 static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
150 switch (reg) {
151 case BMP280_REG_CONFIG:
152 case BMP280_REG_CTRL_MEAS:
153 case BMP280_REG_RESET:
154 return true;
155 default:
156 return false;
160 static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
162 switch (reg) {
163 case BMP280_REG_TEMP_XLSB:
164 case BMP280_REG_TEMP_LSB:
165 case BMP280_REG_TEMP_MSB:
166 case BMP280_REG_PRESS_XLSB:
167 case BMP280_REG_PRESS_LSB:
168 case BMP280_REG_PRESS_MSB:
169 case BMP280_REG_STATUS:
170 return true;
171 default:
172 return false;
176 static const struct regmap_config bmp280_regmap_config = {
177 .reg_bits = 8,
178 .val_bits = 8,
180 .max_register = BMP280_REG_TEMP_XLSB,
181 .cache_type = REGCACHE_RBTREE,
183 .writeable_reg = bmp280_is_writeable_reg,
184 .volatile_reg = bmp280_is_volatile_reg,
188 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
189 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
190 * value.
192 * Taken from datasheet, Section 3.11.3, "Compensation formula".
194 static s32 bmp280_compensate_temp(struct bmp280_data *data,
195 s32 adc_temp)
197 int ret;
198 s32 var1, var2;
199 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
201 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
202 buf, BMP280_COMP_TEMP_REG_COUNT);
203 if (ret < 0) {
204 dev_err(&data->client->dev,
205 "failed to read temperature calibration parameters\n");
206 return ret;
210 * The double casts are necessary because le16_to_cpu returns an
211 * unsigned 16-bit value. Casting that value directly to a
212 * signed 32-bit will not do proper sign extension.
214 * Conversely, T1 and P1 are unsigned values, so they can be
215 * cast straight to the larger type.
217 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
218 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
219 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
220 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
221 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
222 data->t_fine = var1 + var2;
224 return (data->t_fine * 5 + 128) >> 8;
228 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
229 * integer bits and 8 fractional bits). Output value of "24674867"
230 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
232 * Taken from datasheet, Section 3.11.3, "Compensation formula".
234 static u32 bmp280_compensate_press(struct bmp280_data *data,
235 s32 adc_press)
237 int ret;
238 s64 var1, var2, p;
239 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
241 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
242 buf, BMP280_COMP_PRESS_REG_COUNT);
243 if (ret < 0) {
244 dev_err(&data->client->dev,
245 "failed to read pressure calibration parameters\n");
246 return ret;
249 var1 = ((s64)data->t_fine) - 128000;
250 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
251 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
252 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
253 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
254 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
255 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
257 if (var1 == 0)
258 return 0;
260 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
261 p = div64_s64(p, var1);
262 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
263 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
264 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
266 return (u32)p;
269 static int bmp280_read_temp(struct bmp280_data *data,
270 int *val)
272 int ret;
273 __be32 tmp = 0;
274 s32 adc_temp, comp_temp;
276 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
277 (u8 *) &tmp, 3);
278 if (ret < 0) {
279 dev_err(&data->client->dev, "failed to read temperature\n");
280 return ret;
283 adc_temp = be32_to_cpu(tmp) >> 12;
284 comp_temp = bmp280_compensate_temp(data, adc_temp);
287 * val might be NULL if we're called by the read_press routine,
288 * who only cares about the carry over t_fine value.
290 if (val) {
291 *val = comp_temp * 10;
292 return IIO_VAL_INT;
295 return 0;
298 static int bmp280_read_press(struct bmp280_data *data,
299 int *val, int *val2)
301 int ret;
302 __be32 tmp = 0;
303 s32 adc_press;
304 u32 comp_press;
306 /* Read and compensate temperature so we get a reading of t_fine. */
307 ret = bmp280_read_temp(data, NULL);
308 if (ret < 0)
309 return ret;
311 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
312 (u8 *) &tmp, 3);
313 if (ret < 0) {
314 dev_err(&data->client->dev, "failed to read pressure\n");
315 return ret;
318 adc_press = be32_to_cpu(tmp) >> 12;
319 comp_press = bmp280_compensate_press(data, adc_press);
321 *val = comp_press;
322 *val2 = 256000;
324 return IIO_VAL_FRACTIONAL;
327 static int bmp280_read_raw(struct iio_dev *indio_dev,
328 struct iio_chan_spec const *chan,
329 int *val, int *val2, long mask)
331 int ret;
332 struct bmp280_data *data = iio_priv(indio_dev);
334 mutex_lock(&data->lock);
336 switch (mask) {
337 case IIO_CHAN_INFO_PROCESSED:
338 switch (chan->type) {
339 case IIO_PRESSURE:
340 ret = data->chip_info->read_press(data, val, val2);
341 break;
342 case IIO_TEMP:
343 ret = data->chip_info->read_temp(data, val);
344 break;
345 default:
346 ret = -EINVAL;
347 break;
349 break;
350 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
351 switch (chan->type) {
352 case IIO_PRESSURE:
353 *val = 1 << data->oversampling_press;
354 ret = IIO_VAL_INT;
355 break;
356 case IIO_TEMP:
357 *val = 1 << data->oversampling_temp;
358 ret = IIO_VAL_INT;
359 break;
360 default:
361 ret = -EINVAL;
362 break;
364 break;
365 default:
366 ret = -EINVAL;
367 break;
370 mutex_unlock(&data->lock);
372 return ret;
375 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
376 int val)
378 int i;
379 const int *avail = data->chip_info->oversampling_temp_avail;
380 const int n = data->chip_info->num_oversampling_temp_avail;
382 for (i = 0; i < n; i++) {
383 if (avail[i] == val) {
384 data->oversampling_temp = ilog2(val);
386 return data->chip_info->chip_config(data);
389 return -EINVAL;
392 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
393 int val)
395 int i;
396 const int *avail = data->chip_info->oversampling_press_avail;
397 const int n = data->chip_info->num_oversampling_press_avail;
399 for (i = 0; i < n; i++) {
400 if (avail[i] == val) {
401 data->oversampling_press = ilog2(val);
403 return data->chip_info->chip_config(data);
406 return -EINVAL;
409 static int bmp280_write_raw(struct iio_dev *indio_dev,
410 struct iio_chan_spec const *chan,
411 int val, int val2, long mask)
413 int ret = 0;
414 struct bmp280_data *data = iio_priv(indio_dev);
416 switch (mask) {
417 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
418 mutex_lock(&data->lock);
419 switch (chan->type) {
420 case IIO_PRESSURE:
421 ret = bmp280_write_oversampling_ratio_press(data, val);
422 break;
423 case IIO_TEMP:
424 ret = bmp280_write_oversampling_ratio_temp(data, val);
425 break;
426 default:
427 ret = -EINVAL;
428 break;
430 mutex_unlock(&data->lock);
431 break;
432 default:
433 return -EINVAL;
436 return ret;
439 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
441 size_t len = 0;
442 int i;
444 for (i = 0; i < n; i++)
445 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
447 buf[len - 1] = '\n';
449 return len;
452 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
453 struct device_attribute *attr, char *buf)
455 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
457 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
458 data->chip_info->num_oversampling_temp_avail);
461 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
462 struct device_attribute *attr, char *buf)
464 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
466 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
467 data->chip_info->num_oversampling_press_avail);
470 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
471 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
473 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
474 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
476 static struct attribute *bmp280_attributes[] = {
477 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
478 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
479 NULL,
482 static const struct attribute_group bmp280_attrs_group = {
483 .attrs = bmp280_attributes,
486 static const struct iio_info bmp280_info = {
487 .driver_module = THIS_MODULE,
488 .read_raw = &bmp280_read_raw,
489 .write_raw = &bmp280_write_raw,
490 .attrs = &bmp280_attrs_group,
493 static int bmp280_chip_config(struct bmp280_data *data)
495 int ret;
496 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
497 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
499 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
500 BMP280_OSRS_TEMP_MASK |
501 BMP280_OSRS_PRESS_MASK |
502 BMP280_MODE_MASK,
503 osrs | BMP280_MODE_NORMAL);
504 if (ret < 0) {
505 dev_err(&data->client->dev,
506 "failed to write ctrl_meas register\n");
507 return ret;
510 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
511 BMP280_FILTER_MASK,
512 BMP280_FILTER_4X);
513 if (ret < 0) {
514 dev_err(&data->client->dev,
515 "failed to write config register\n");
516 return ret;
519 return ret;
522 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
524 static const struct bmp280_chip_info bmp280_chip_info = {
525 .regmap_config = &bmp280_regmap_config,
527 .oversampling_temp_avail = bmp280_oversampling_avail,
528 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
530 .oversampling_press_avail = bmp280_oversampling_avail,
531 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
533 .chip_config = bmp280_chip_config,
534 .read_temp = bmp280_read_temp,
535 .read_press = bmp280_read_press,
538 static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
540 switch (reg) {
541 case BMP280_REG_CTRL_MEAS:
542 case BMP280_REG_RESET:
543 return true;
544 default:
545 return false;
549 static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
551 switch (reg) {
552 case BMP180_REG_OUT_XLSB:
553 case BMP180_REG_OUT_LSB:
554 case BMP180_REG_OUT_MSB:
555 case BMP280_REG_CTRL_MEAS:
556 return true;
557 default:
558 return false;
562 static const struct regmap_config bmp180_regmap_config = {
563 .reg_bits = 8,
564 .val_bits = 8,
566 .max_register = BMP180_REG_OUT_XLSB,
567 .cache_type = REGCACHE_RBTREE,
569 .writeable_reg = bmp180_is_writeable_reg,
570 .volatile_reg = bmp180_is_volatile_reg,
573 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
575 int ret;
576 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
577 unsigned int delay_us;
578 unsigned int ctrl;
580 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
581 if (ret)
582 return ret;
584 if (ctrl_meas == BMP180_MEAS_TEMP)
585 delay_us = 4500;
586 else
587 delay_us = conversion_time_max[data->oversampling_press];
589 usleep_range(delay_us, delay_us + 1000);
591 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
592 if (ret)
593 return ret;
595 /* The value of this bit reset to "0" after conversion is complete */
596 if (ctrl & BMP180_MEAS_SCO)
597 return -EIO;
599 return 0;
602 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
604 int ret;
605 __be16 tmp = 0;
607 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
608 if (ret)
609 return ret;
611 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
612 if (ret)
613 return ret;
615 *val = be16_to_cpu(tmp);
617 return 0;
621 * These enums are used for indexing into the array of calibration
622 * coefficients for BMP180.
624 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
626 struct bmp180_calib {
627 s16 AC1;
628 s16 AC2;
629 s16 AC3;
630 u16 AC4;
631 u16 AC5;
632 u16 AC6;
633 s16 B1;
634 s16 B2;
635 s16 MB;
636 s16 MC;
637 s16 MD;
640 static int bmp180_read_calib(struct bmp280_data *data,
641 struct bmp180_calib *calib)
643 int ret;
644 int i;
645 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
647 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
648 sizeof(buf));
650 if (ret < 0)
651 return ret;
653 /* None of the words has the value 0 or 0xFFFF */
654 for (i = 0; i < ARRAY_SIZE(buf); i++) {
655 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
656 return -EIO;
659 calib->AC1 = be16_to_cpu(buf[AC1]);
660 calib->AC2 = be16_to_cpu(buf[AC2]);
661 calib->AC3 = be16_to_cpu(buf[AC3]);
662 calib->AC4 = be16_to_cpu(buf[AC4]);
663 calib->AC5 = be16_to_cpu(buf[AC5]);
664 calib->AC6 = be16_to_cpu(buf[AC6]);
665 calib->B1 = be16_to_cpu(buf[B1]);
666 calib->B2 = be16_to_cpu(buf[B2]);
667 calib->MB = be16_to_cpu(buf[MB]);
668 calib->MC = be16_to_cpu(buf[MC]);
669 calib->MD = be16_to_cpu(buf[MD]);
671 return 0;
675 * Returns temperature in DegC, resolution is 0.1 DegC.
676 * t_fine carries fine temperature as global value.
678 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
680 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
682 int ret;
683 s32 x1, x2;
684 struct bmp180_calib calib;
686 ret = bmp180_read_calib(data, &calib);
687 if (ret < 0) {
688 dev_err(&data->client->dev,
689 "failed to read calibration coefficients\n");
690 return ret;
693 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
694 x2 = (calib.MC << 11) / (x1 + calib.MD);
695 data->t_fine = x1 + x2;
697 return (data->t_fine + 8) >> 4;
700 static int bmp180_read_temp(struct bmp280_data *data, int *val)
702 int ret;
703 s32 adc_temp, comp_temp;
705 ret = bmp180_read_adc_temp(data, &adc_temp);
706 if (ret)
707 return ret;
709 comp_temp = bmp180_compensate_temp(data, adc_temp);
712 * val might be NULL if we're called by the read_press routine,
713 * who only cares about the carry over t_fine value.
715 if (val) {
716 *val = comp_temp * 100;
717 return IIO_VAL_INT;
720 return 0;
723 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
725 int ret;
726 __be32 tmp = 0;
727 u8 oss = data->oversampling_press;
729 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
730 if (ret)
731 return ret;
733 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
734 if (ret)
735 return ret;
737 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
739 return 0;
743 * Returns pressure in Pa, resolution is 1 Pa.
745 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
747 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
749 int ret;
750 s32 x1, x2, x3, p;
751 s32 b3, b6;
752 u32 b4, b7;
753 s32 oss = data->oversampling_press;
754 struct bmp180_calib calib;
756 ret = bmp180_read_calib(data, &calib);
757 if (ret < 0) {
758 dev_err(&data->client->dev,
759 "failed to read calibration coefficients\n");
760 return ret;
763 b6 = data->t_fine - 4000;
764 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
765 x2 = calib.AC2 * b6 >> 11;
766 x3 = x1 + x2;
767 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
768 x1 = calib.AC3 * b6 >> 13;
769 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
770 x3 = (x1 + x2 + 2) >> 2;
771 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
772 b7 = ((u32)adc_press - b3) * (50000 >> oss);
773 if (b7 < 0x80000000)
774 p = (b7 * 2) / b4;
775 else
776 p = (b7 / b4) * 2;
778 x1 = (p >> 8) * (p >> 8);
779 x1 = (x1 * 3038) >> 16;
780 x2 = (-7357 * p) >> 16;
782 return p + ((x1 + x2 + 3791) >> 4);
785 static int bmp180_read_press(struct bmp280_data *data,
786 int *val, int *val2)
788 int ret;
789 s32 adc_press;
790 u32 comp_press;
792 /* Read and compensate temperature so we get a reading of t_fine. */
793 ret = bmp180_read_temp(data, NULL);
794 if (ret)
795 return ret;
797 ret = bmp180_read_adc_press(data, &adc_press);
798 if (ret)
799 return ret;
801 comp_press = bmp180_compensate_press(data, adc_press);
803 *val = comp_press;
804 *val2 = 1000;
806 return IIO_VAL_FRACTIONAL;
809 static int bmp180_chip_config(struct bmp280_data *data)
811 return 0;
814 static const int bmp180_oversampling_temp_avail[] = { 1 };
815 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
817 static const struct bmp280_chip_info bmp180_chip_info = {
818 .regmap_config = &bmp180_regmap_config,
820 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
821 .num_oversampling_temp_avail =
822 ARRAY_SIZE(bmp180_oversampling_temp_avail),
824 .oversampling_press_avail = bmp180_oversampling_press_avail,
825 .num_oversampling_press_avail =
826 ARRAY_SIZE(bmp180_oversampling_press_avail),
828 .chip_config = bmp180_chip_config,
829 .read_temp = bmp180_read_temp,
830 .read_press = bmp180_read_press,
833 static int bmp280_probe(struct i2c_client *client,
834 const struct i2c_device_id *id)
836 int ret;
837 struct iio_dev *indio_dev;
838 struct bmp280_data *data;
839 unsigned int chip_id;
841 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
842 if (!indio_dev)
843 return -ENOMEM;
845 data = iio_priv(indio_dev);
846 mutex_init(&data->lock);
847 data->client = client;
849 indio_dev->dev.parent = &client->dev;
850 indio_dev->name = id->name;
851 indio_dev->channels = bmp280_channels;
852 indio_dev->num_channels = ARRAY_SIZE(bmp280_channels);
853 indio_dev->info = &bmp280_info;
854 indio_dev->modes = INDIO_DIRECT_MODE;
856 switch (id->driver_data) {
857 case BMP180_CHIP_ID:
858 data->chip_info = &bmp180_chip_info;
859 data->oversampling_press = ilog2(8);
860 data->oversampling_temp = ilog2(1);
861 break;
862 case BMP280_CHIP_ID:
863 data->chip_info = &bmp280_chip_info;
864 data->oversampling_press = ilog2(16);
865 data->oversampling_temp = ilog2(2);
866 break;
867 default:
868 return -EINVAL;
871 data->regmap = devm_regmap_init_i2c(client,
872 data->chip_info->regmap_config);
873 if (IS_ERR(data->regmap)) {
874 dev_err(&client->dev, "failed to allocate register map\n");
875 return PTR_ERR(data->regmap);
878 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
879 if (ret < 0)
880 return ret;
881 if (chip_id != id->driver_data) {
882 dev_err(&client->dev, "bad chip id. expected %lx got %x\n",
883 id->driver_data, chip_id);
884 return -EINVAL;
887 ret = data->chip_info->chip_config(data);
888 if (ret < 0)
889 return ret;
891 return devm_iio_device_register(&client->dev, indio_dev);
894 static const struct acpi_device_id bmp280_acpi_match[] = {
895 {"BMP0280", BMP280_CHIP_ID },
896 {"BMP0180", BMP180_CHIP_ID },
897 {"BMP0085", BMP180_CHIP_ID },
898 { },
900 MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
902 static const struct i2c_device_id bmp280_id[] = {
903 {"bmp280", BMP280_CHIP_ID },
904 {"bmp180", BMP180_CHIP_ID },
905 {"bmp085", BMP180_CHIP_ID },
906 { },
908 MODULE_DEVICE_TABLE(i2c, bmp280_id);
910 static struct i2c_driver bmp280_driver = {
911 .driver = {
912 .name = "bmp280",
913 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
915 .probe = bmp280_probe,
916 .id_table = bmp280_id,
918 module_i2c_driver(bmp280_driver);
920 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
921 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
922 MODULE_LICENSE("GPL v2");