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