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