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