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