]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/iio/pressure/bmp280.c
block: Fix front merge check
[karo-tx-linux.git] / drivers / iio / pressure / bmp280.c
1 /*
2  * Copyright (c) 2014 Intel Corporation
3  *
4  * Driver for Bosch Sensortec BMP180 and 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  * 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
13  */
14
15 #define pr_fmt(fmt) "bmp280: " fmt
16
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>
24
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
32
33 #define BMP280_REG_CONFIG               0xF5
34 #define BMP280_REG_STATUS               0xF3
35
36 #define BMP280_REG_COMP_TEMP_START      0x88
37 #define BMP280_COMP_TEMP_REG_COUNT      6
38
39 #define BMP280_REG_COMP_PRESS_START     0x8E
40 #define BMP280_COMP_PRESS_REG_COUNT     18
41
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)
48
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)
57
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)
66
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))
71
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
76
77 #define BMP180_REG_CALIB_START          0xAA
78 #define BMP180_REG_CALIB_COUNT          22
79
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)
87
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
92
93 #define BMP180_CHIP_ID                  0x55
94 #define BMP280_CHIP_ID                  0x58
95 #define BMP280_SOFT_RESET_VAL           0xB6
96
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;
102
103         /* log of base 2 of oversampling rate */
104         u8 oversampling_press;
105         u8 oversampling_temp;
106
107         /*
108          * Carryover value from temperature conversion, used in pressure
109          * calculation.
110          */
111         s32 t_fine;
112 };
113
114 struct bmp280_chip_info {
115         const struct regmap_config *regmap_config;
116
117         const int *oversampling_temp_avail;
118         int num_oversampling_temp_avail;
119
120         const int *oversampling_press_avail;
121         int num_oversampling_press_avail;
122
123         int (*chip_config)(struct bmp280_data *);
124         int (*read_temp)(struct bmp280_data *, int *);
125         int (*read_press)(struct bmp280_data *, int *, int *);
126 };
127
128 /*
129  * These enums are used for indexing into the array of compensation
130  * parameters for BMP280.
131  */
132 enum { T1, T2, T3 };
133 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
134
135 static const struct iio_chan_spec bmp280_channels[] = {
136         {
137                 .type = IIO_PRESSURE,
138                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
139                                       BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
140         },
141         {
142                 .type = IIO_TEMP,
143                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
144                                       BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
145         },
146 };
147
148 static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
149 {
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;
157         };
158 }
159
160 static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
161 {
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;
173         }
174 }
175
176 static const struct regmap_config bmp280_regmap_config = {
177         .reg_bits = 8,
178         .val_bits = 8,
179
180         .max_register = BMP280_REG_TEMP_XLSB,
181         .cache_type = REGCACHE_RBTREE,
182
183         .writeable_reg = bmp280_is_writeable_reg,
184         .volatile_reg = bmp280_is_volatile_reg,
185 };
186
187 /*
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.
191  *
192  * Taken from datasheet, Section 3.11.3, "Compensation formula".
193  */
194 static s32 bmp280_compensate_temp(struct bmp280_data *data,
195                                   s32 adc_temp)
196 {
197         int ret;
198         s32 var1, var2;
199         __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
200
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;
207         }
208
209         /*
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.
213          *
214          * Conversely, T1 and P1 are unsigned values, so they can be
215          * cast straight to the larger type.
216          */
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;
223
224         return (data->t_fine * 5 + 128) >> 8;
225 }
226
227 /*
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
231  *
232  * Taken from datasheet, Section 3.11.3, "Compensation formula".
233  */
234 static u32 bmp280_compensate_press(struct bmp280_data *data,
235                                    s32 adc_press)
236 {
237         int ret;
238         s64 var1, var2, p;
239         __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
240
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;
247         }
248
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;
256
257         if (var1 == 0)
258                 return 0;
259
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);
265
266         return (u32)p;
267 }
268
269 static int bmp280_read_temp(struct bmp280_data *data,
270                             int *val)
271 {
272         int ret;
273         __be32 tmp = 0;
274         s32 adc_temp, comp_temp;
275
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;
281         }
282
283         adc_temp = be32_to_cpu(tmp) >> 12;
284         comp_temp = bmp280_compensate_temp(data, adc_temp);
285
286         /*
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.
289          */
290         if (val) {
291                 *val = comp_temp * 10;
292                 return IIO_VAL_INT;
293         }
294
295         return 0;
296 }
297
298 static int bmp280_read_press(struct bmp280_data *data,
299                              int *val, int *val2)
300 {
301         int ret;
302         __be32 tmp = 0;
303         s32 adc_press;
304         u32 comp_press;
305
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;
310
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;
316         }
317
318         adc_press = be32_to_cpu(tmp) >> 12;
319         comp_press = bmp280_compensate_press(data, adc_press);
320
321         *val = comp_press;
322         *val2 = 256000;
323
324         return IIO_VAL_FRACTIONAL;
325 }
326
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)
330 {
331         int ret;
332         struct bmp280_data *data = iio_priv(indio_dev);
333
334         mutex_lock(&data->lock);
335
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;
348                 }
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;
363                 }
364                 break;
365         default:
366                 ret = -EINVAL;
367                 break;
368         }
369
370         mutex_unlock(&data->lock);
371
372         return ret;
373 }
374
375 static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
376                                                int val)
377 {
378         int i;
379         const int *avail = data->chip_info->oversampling_temp_avail;
380         const int n = data->chip_info->num_oversampling_temp_avail;
381
382         for (i = 0; i < n; i++) {
383                 if (avail[i] == val) {
384                         data->oversampling_temp = ilog2(val);
385
386                         return data->chip_info->chip_config(data);
387                 }
388         }
389         return -EINVAL;
390 }
391
392 static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
393                                                int val)
394 {
395         int i;
396         const int *avail = data->chip_info->oversampling_press_avail;
397         const int n = data->chip_info->num_oversampling_press_avail;
398
399         for (i = 0; i < n; i++) {
400                 if (avail[i] == val) {
401                         data->oversampling_press = ilog2(val);
402
403                         return data->chip_info->chip_config(data);
404                 }
405         }
406         return -EINVAL;
407 }
408
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)
412 {
413         int ret = 0;
414         struct bmp280_data *data = iio_priv(indio_dev);
415
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;
429                 }
430                 mutex_unlock(&data->lock);
431                 break;
432         default:
433                 return -EINVAL;
434         }
435
436         return ret;
437 }
438
439 static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
440 {
441         size_t len = 0;
442         int i;
443
444         for (i = 0; i < n; i++)
445                 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
446
447         buf[len - 1] = '\n';
448
449         return len;
450 }
451
452 static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
453                                 struct device_attribute *attr, char *buf)
454 {
455         struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
456
457         return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
458                                  data->chip_info->num_oversampling_temp_avail);
459 }
460
461 static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
462                                 struct device_attribute *attr, char *buf)
463 {
464         struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
465
466         return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
467                                  data->chip_info->num_oversampling_press_avail);
468 }
469
470 static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
471         S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
472
473 static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
474         S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
475
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,
480 };
481
482 static const struct attribute_group bmp280_attrs_group = {
483         .attrs = bmp280_attributes,
484 };
485
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,
491 };
492
493 static int bmp280_chip_config(struct bmp280_data *data)
494 {
495         int ret;
496         u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
497                   BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
498
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;
508         }
509
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;
517         }
518
519         return ret;
520 }
521
522 static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
523
524 static const struct bmp280_chip_info bmp280_chip_info = {
525         .regmap_config = &bmp280_regmap_config,
526
527         .oversampling_temp_avail = bmp280_oversampling_avail,
528         .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
529
530         .oversampling_press_avail = bmp280_oversampling_avail,
531         .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
532
533         .chip_config = bmp280_chip_config,
534         .read_temp = bmp280_read_temp,
535         .read_press = bmp280_read_press,
536 };
537
538 static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
539 {
540         switch (reg) {
541         case BMP280_REG_CTRL_MEAS:
542         case BMP280_REG_RESET:
543                 return true;
544         default:
545                 return false;
546         };
547 }
548
549 static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
550 {
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;
559         }
560 }
561
562 static const struct regmap_config bmp180_regmap_config = {
563         .reg_bits = 8,
564         .val_bits = 8,
565
566         .max_register = BMP180_REG_OUT_XLSB,
567         .cache_type = REGCACHE_RBTREE,
568
569         .writeable_reg = bmp180_is_writeable_reg,
570         .volatile_reg = bmp180_is_volatile_reg,
571 };
572
573 static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
574 {
575         int ret;
576         const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
577         unsigned int delay_us;
578         unsigned int ctrl;
579
580         ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
581         if (ret)
582                 return ret;
583
584         if (ctrl_meas == BMP180_MEAS_TEMP)
585                 delay_us = 4500;
586         else
587                 delay_us = conversion_time_max[data->oversampling_press];
588
589         usleep_range(delay_us, delay_us + 1000);
590
591         ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
592         if (ret)
593                 return ret;
594
595         /* The value of this bit reset to "0" after conversion is complete */
596         if (ctrl & BMP180_MEAS_SCO)
597                 return -EIO;
598
599         return 0;
600 }
601
602 static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
603 {
604         int ret;
605         __be16 tmp = 0;
606
607         ret = bmp180_measure(data, BMP180_MEAS_TEMP);
608         if (ret)
609                 return ret;
610
611         ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
612         if (ret)
613                 return ret;
614
615         *val = be16_to_cpu(tmp);
616
617         return 0;
618 }
619
620 /*
621  * These enums are used for indexing into the array of calibration
622  * coefficients for BMP180.
623  */
624 enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
625
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;
638 };
639
640 static int bmp180_read_calib(struct bmp280_data *data,
641                              struct bmp180_calib *calib)
642 {
643         int ret;
644         int i;
645         __be16 buf[BMP180_REG_CALIB_COUNT / 2];
646
647         ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
648                                sizeof(buf));
649
650         if (ret < 0)
651                 return ret;
652
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;
657         }
658
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]);
670
671         return 0;
672 }
673
674 /*
675  * Returns temperature in DegC, resolution is 0.1 DegC.
676  * t_fine carries fine temperature as global value.
677  *
678  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
679  */
680 static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
681 {
682         int ret;
683         s32 x1, x2;
684         struct bmp180_calib calib;
685
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;
691         }
692
693         x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
694         x2 = (calib.MC << 11) / (x1 + calib.MD);
695         data->t_fine = x1 + x2;
696
697         return (data->t_fine + 8) >> 4;
698 }
699
700 static int bmp180_read_temp(struct bmp280_data *data, int *val)
701 {
702         int ret;
703         s32 adc_temp, comp_temp;
704
705         ret = bmp180_read_adc_temp(data, &adc_temp);
706         if (ret)
707                 return ret;
708
709         comp_temp = bmp180_compensate_temp(data, adc_temp);
710
711         /*
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.
714          */
715         if (val) {
716                 *val = comp_temp * 100;
717                 return IIO_VAL_INT;
718         }
719
720         return 0;
721 }
722
723 static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
724 {
725         int ret;
726         __be32 tmp = 0;
727         u8 oss = data->oversampling_press;
728
729         ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
730         if (ret)
731                 return ret;
732
733         ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
734         if (ret)
735                 return ret;
736
737         *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
738
739         return 0;
740 }
741
742 /*
743  * Returns pressure in Pa, resolution is 1 Pa.
744  *
745  * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
746  */
747 static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
748 {
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;
755
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;
761         }
762
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;
777
778         x1 = (p >> 8) * (p >> 8);
779         x1 = (x1 * 3038) >> 16;
780         x2 = (-7357 * p) >> 16;
781
782         return p + ((x1 + x2 + 3791) >> 4);
783 }
784
785 static int bmp180_read_press(struct bmp280_data *data,
786                              int *val, int *val2)
787 {
788         int ret;
789         s32 adc_press;
790         u32 comp_press;
791
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;
796
797         ret = bmp180_read_adc_press(data, &adc_press);
798         if (ret)
799                 return ret;
800
801         comp_press = bmp180_compensate_press(data, adc_press);
802
803         *val = comp_press;
804         *val2 = 1000;
805
806         return IIO_VAL_FRACTIONAL;
807 }
808
809 static int bmp180_chip_config(struct bmp280_data *data)
810 {
811         return 0;
812 }
813
814 static const int bmp180_oversampling_temp_avail[] = { 1 };
815 static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
816
817 static const struct bmp280_chip_info bmp180_chip_info = {
818         .regmap_config = &bmp180_regmap_config,
819
820         .oversampling_temp_avail = bmp180_oversampling_temp_avail,
821         .num_oversampling_temp_avail =
822                 ARRAY_SIZE(bmp180_oversampling_temp_avail),
823
824         .oversampling_press_avail = bmp180_oversampling_press_avail,
825         .num_oversampling_press_avail =
826                 ARRAY_SIZE(bmp180_oversampling_press_avail),
827
828         .chip_config = bmp180_chip_config,
829         .read_temp = bmp180_read_temp,
830         .read_press = bmp180_read_press,
831 };
832
833 static int bmp280_probe(struct i2c_client *client,
834                         const struct i2c_device_id *id)
835 {
836         int ret;
837         struct iio_dev *indio_dev;
838         struct bmp280_data *data;
839         unsigned int chip_id;
840
841         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
842         if (!indio_dev)
843                 return -ENOMEM;
844
845         data = iio_priv(indio_dev);
846         mutex_init(&data->lock);
847         data->client = client;
848
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;
855
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;
869         }
870
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);
876         }
877
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 %x got %x\n",
883                         BMP280_CHIP_ID, chip_id);
884                 return -EINVAL;
885         }
886
887         ret = data->chip_info->chip_config(data);
888         if (ret < 0)
889                 return ret;
890
891         return devm_iio_device_register(&client->dev, indio_dev);
892 }
893
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         { },
899 };
900 MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
901
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         { },
907 };
908 MODULE_DEVICE_TABLE(i2c, bmp280_id);
909
910 static struct i2c_driver bmp280_driver = {
911         .driver = {
912                 .name   = "bmp280",
913                 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
914         },
915         .probe          = bmp280_probe,
916         .id_table       = bmp280_id,
917 };
918 module_i2c_driver(bmp280_driver);
919
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");