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