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