blob: f762eb8b174ae56ec39d62ff063963e85167a728 [file] [log] [blame]
Vlad Dogarud5c94562014-10-21 11:09:58 +03001/*
Linus Walleijb26b4e92016-06-30 03:48:51 +02002 * Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
3 * Copyright (c) 2012 Bosch Sensortec GmbH
4 * Copyright (c) 2012 Unixphere AB
Vlad Dogarud5c94562014-10-21 11:09:58 +03005 * Copyright (c) 2014 Intel Corporation
Linus Walleijb26b4e92016-06-30 03:48:51 +02006 * Copyright (c) 2016 Linus Walleij <linus.walleij@linaro.org>
Vlad Dogarud5c94562014-10-21 11:09:58 +03007 *
Akinobu Mita6dba72e2016-04-24 22:52:10 +09008 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
Vlad Dogarud5c94562014-10-21 11:09:58 +03009 *
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 *
Akinobu Mita6dba72e2016-04-24 22:52:10 +090014 * 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
Matt Ranostay14beaa82016-05-04 22:57:30 -070017 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
Vlad Dogarud5c94562014-10-21 11:09:58 +030018 */
19
20#define pr_fmt(fmt) "bmp280: " fmt
21
Linus Walleij14e80152016-06-30 03:48:49 +020022#include <linux/device.h>
Linus Walleij17118842016-06-30 03:48:50 +020023#include <linux/module.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030024#include <linux/regmap.h>
Akinobu Mita6dba72e2016-04-24 22:52:10 +090025#include <linux/delay.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030026#include <linux/iio/iio.h>
27#include <linux/iio/sysfs.h>
Linus Walleijc5842b42016-06-30 03:48:47 +020028#include <linux/gpio/consumer.h>
Linus Walleijbd525e62016-06-30 03:48:48 +020029#include <linux/regulator/consumer.h>
Linus Walleijaae95392016-06-30 03:48:52 +020030#include <linux/interrupt.h>
31#include <linux/irq.h> /* For irq_get_irq_data() */
32#include <linux/completion.h>
Linus Walleij3d838112016-06-30 03:48:53 +020033#include <linux/pm_runtime.h>
Linus Walleijb33b7d52016-06-30 03:48:54 +020034#include <linux/random.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030035
Linus Walleij14e80152016-06-30 03:48:49 +020036#include "bmp280.h"
Vlad Dogarud5c94562014-10-21 11:09:58 +030037
Linus Walleijb33b7d52016-06-30 03:48:54 +020038/*
39 * These enums are used for indexing into the array of calibration
40 * coefficients for BMP180.
41 */
42enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
43
44struct 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
Vlad Dogarud5c94562014-10-21 11:09:58 +030058struct bmp280_data {
Linus Walleij14e80152016-06-30 03:48:49 +020059 struct device *dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +030060 struct mutex lock;
61 struct regmap *regmap;
Linus Walleijaae95392016-06-30 03:48:52 +020062 struct completion done;
63 bool use_eoc;
Akinobu Mita6dba72e2016-04-24 22:52:10 +090064 const struct bmp280_chip_info *chip_info;
Linus Walleijb33b7d52016-06-30 03:48:54 +020065 struct bmp180_calib calib;
Linus Walleijbd525e62016-06-30 03:48:48 +020066 struct regulator *vddd;
67 struct regulator *vdda;
68 unsigned int start_up_time; /* in milliseconds */
Vlad Dogarud5c94562014-10-21 11:09:58 +030069
Akinobu Mita62979902016-04-24 22:52:11 +090070 /* log of base 2 of oversampling rate */
71 u8 oversampling_press;
72 u8 oversampling_temp;
Matt Ranostay14beaa82016-05-04 22:57:30 -070073 u8 oversampling_humid;
Akinobu Mita62979902016-04-24 22:52:11 +090074
Vlad Dogarud5c94562014-10-21 11:09:58 +030075 /*
76 * Carryover value from temperature conversion, used in pressure
77 * calculation.
78 */
79 s32 t_fine;
80};
81
Akinobu Mita6dba72e2016-04-24 22:52:10 +090082struct bmp280_chip_info {
Akinobu Mita62979902016-04-24 22:52:11 +090083 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
Matt Ranostay14beaa82016-05-04 22:57:30 -070089 const int *oversampling_humid_avail;
90 int num_oversampling_humid_avail;
91
Akinobu Mita6dba72e2016-04-24 22:52:10 +090092 int (*chip_config)(struct bmp280_data *);
93 int (*read_temp)(struct bmp280_data *, int *);
94 int (*read_press)(struct bmp280_data *, int *, int *);
Matt Ranostay14beaa82016-05-04 22:57:30 -070095 int (*read_humid)(struct bmp280_data *, int *, int *);
Akinobu Mita6dba72e2016-04-24 22:52:10 +090096};
97
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020098/*
99 * These enums are used for indexing into the array of compensation
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900100 * parameters for BMP280.
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200101 */
102enum { T1, T2, T3 };
103enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +0300104
105static const struct iio_chan_spec bmp280_channels[] = {
106 {
107 .type = IIO_PRESSURE,
Akinobu Mita62979902016-04-24 22:52:11 +0900108 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
109 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300110 },
111 {
112 .type = IIO_TEMP,
Akinobu Mita62979902016-04-24 22:52:11 +0900113 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
114 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300115 },
Matt Ranostay14beaa82016-05-04 22:57:30 -0700116 {
117 .type = IIO_HUMIDITYRELATIVE,
118 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
119 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
120 },
Vlad Dogarud5c94562014-10-21 11:09:58 +0300121};
122
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200123/*
Matt Ranostay14beaa82016-05-04 22:57:30 -0700124 * 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
130static u32 bmp280_compensate_humidity(struct bmp280_data *data,
131 s32 adc_humidity)
132{
Linus Walleij14e80152016-06-30 03:48:49 +0200133 struct device *dev = data->dev;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700134 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
Andreas Klingerdfb450b2017-04-10 19:00:01 +0200178 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;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700184
185 return var >> 12;
186};
187
188/*
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200189 * 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 */
195static s32 bmp280_compensate_temp(struct bmp280_data *data,
196 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300197{
198 int ret;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100199 s32 var1, var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300200 __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) {
Linus Walleij14e80152016-06-30 03:48:49 +0200205 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300206 "failed to read temperature calibration parameters\n");
207 return ret;
208 }
209
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200210 /*
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;
Irina Tirdeaabad3982015-04-08 18:26:12 +0300223 data->t_fine = var1 + var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300224
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100225 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300226}
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 */
235static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300236 s32 adc_press)
237{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200238 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300239 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200240 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300241
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200242 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
243 buf, BMP280_COMP_PRESS_REG_COUNT);
244 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200245 dev_err(data->dev,
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200246 "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]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100252 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
253 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200254 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;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300257
258 if (var1 == 0)
259 return 0;
260
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200261 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100262 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200263 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);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300266
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100267 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300268}
269
270static 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;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300276
277 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
278 (u8 *) &tmp, 3);
279 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200280 dev_err(data->dev, "failed to read temperature\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300281 return ret;
282 }
283
284 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200285 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300286
287 /*
288 * val might be NULL if we're called by the read_press routine,
289 * who only cares about the carry over t_fine value.
290 */
291 if (val) {
292 *val = comp_temp * 10;
293 return IIO_VAL_INT;
294 }
295
296 return 0;
297}
298
299static int bmp280_read_press(struct bmp280_data *data,
300 int *val, int *val2)
301{
302 int ret;
303 __be32 tmp = 0;
304 s32 adc_press;
305 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300306
307 /* Read and compensate temperature so we get a reading of t_fine. */
308 ret = bmp280_read_temp(data, NULL);
309 if (ret < 0)
310 return ret;
311
312 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
313 (u8 *) &tmp, 3);
314 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200315 dev_err(data->dev, "failed to read pressure\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300316 return ret;
317 }
318
319 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200320 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300321
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000322 *val = comp_press;
323 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300324
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000325 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300326}
327
Matt Ranostay14beaa82016-05-04 22:57:30 -0700328static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
329{
330 int ret;
331 __be16 tmp = 0;
332 s32 adc_humidity;
333 u32 comp_humidity;
334
335 /* Read and compensate temperature so we get a reading of t_fine. */
336 ret = bmp280_read_temp(data, NULL);
337 if (ret < 0)
338 return ret;
339
340 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
341 (u8 *) &tmp, 2);
342 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200343 dev_err(data->dev, "failed to read humidity\n");
Matt Ranostay14beaa82016-05-04 22:57:30 -0700344 return ret;
345 }
346
347 adc_humidity = be16_to_cpu(tmp);
348 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
349
350 *val = comp_humidity;
351 *val2 = 1024;
352
353 return IIO_VAL_FRACTIONAL;
354}
355
Vlad Dogarud5c94562014-10-21 11:09:58 +0300356static int bmp280_read_raw(struct iio_dev *indio_dev,
357 struct iio_chan_spec const *chan,
358 int *val, int *val2, long mask)
359{
360 int ret;
361 struct bmp280_data *data = iio_priv(indio_dev);
362
Linus Walleij3d838112016-06-30 03:48:53 +0200363 pm_runtime_get_sync(data->dev);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300364 mutex_lock(&data->lock);
365
366 switch (mask) {
367 case IIO_CHAN_INFO_PROCESSED:
368 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700369 case IIO_HUMIDITYRELATIVE:
370 ret = data->chip_info->read_humid(data, val, val2);
371 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300372 case IIO_PRESSURE:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900373 ret = data->chip_info->read_press(data, val, val2);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300374 break;
375 case IIO_TEMP:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900376 ret = data->chip_info->read_temp(data, val);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300377 break;
378 default:
379 ret = -EINVAL;
380 break;
381 }
382 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900383 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
384 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700385 case IIO_HUMIDITYRELATIVE:
386 *val = 1 << data->oversampling_humid;
387 ret = IIO_VAL_INT;
388 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900389 case IIO_PRESSURE:
390 *val = 1 << data->oversampling_press;
391 ret = IIO_VAL_INT;
392 break;
393 case IIO_TEMP:
394 *val = 1 << data->oversampling_temp;
395 ret = IIO_VAL_INT;
396 break;
397 default:
398 ret = -EINVAL;
399 break;
400 }
401 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300402 default:
403 ret = -EINVAL;
404 break;
405 }
406
407 mutex_unlock(&data->lock);
Linus Walleij3d838112016-06-30 03:48:53 +0200408 pm_runtime_mark_last_busy(data->dev);
409 pm_runtime_put_autosuspend(data->dev);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300410
411 return ret;
412}
413
Matt Ranostay14beaa82016-05-04 22:57:30 -0700414static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
415 int val)
416{
417 int i;
418 const int *avail = data->chip_info->oversampling_humid_avail;
419 const int n = data->chip_info->num_oversampling_humid_avail;
420
421 for (i = 0; i < n; i++) {
422 if (avail[i] == val) {
423 data->oversampling_humid = ilog2(val);
424
425 return data->chip_info->chip_config(data);
426 }
427 }
428 return -EINVAL;
429}
430
Akinobu Mita62979902016-04-24 22:52:11 +0900431static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
432 int val)
433{
434 int i;
435 const int *avail = data->chip_info->oversampling_temp_avail;
436 const int n = data->chip_info->num_oversampling_temp_avail;
437
438 for (i = 0; i < n; i++) {
439 if (avail[i] == val) {
440 data->oversampling_temp = ilog2(val);
441
442 return data->chip_info->chip_config(data);
443 }
444 }
445 return -EINVAL;
446}
447
448static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
449 int val)
450{
451 int i;
452 const int *avail = data->chip_info->oversampling_press_avail;
453 const int n = data->chip_info->num_oversampling_press_avail;
454
455 for (i = 0; i < n; i++) {
456 if (avail[i] == val) {
457 data->oversampling_press = ilog2(val);
458
459 return data->chip_info->chip_config(data);
460 }
461 }
462 return -EINVAL;
463}
464
465static int bmp280_write_raw(struct iio_dev *indio_dev,
466 struct iio_chan_spec const *chan,
467 int val, int val2, long mask)
468{
469 int ret = 0;
470 struct bmp280_data *data = iio_priv(indio_dev);
471
472 switch (mask) {
473 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
Linus Walleij3d838112016-06-30 03:48:53 +0200474 pm_runtime_get_sync(data->dev);
Akinobu Mita62979902016-04-24 22:52:11 +0900475 mutex_lock(&data->lock);
476 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700477 case IIO_HUMIDITYRELATIVE:
478 ret = bmp280_write_oversampling_ratio_humid(data, val);
479 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900480 case IIO_PRESSURE:
481 ret = bmp280_write_oversampling_ratio_press(data, val);
482 break;
483 case IIO_TEMP:
484 ret = bmp280_write_oversampling_ratio_temp(data, val);
485 break;
486 default:
487 ret = -EINVAL;
488 break;
489 }
490 mutex_unlock(&data->lock);
Linus Walleij3d838112016-06-30 03:48:53 +0200491 pm_runtime_mark_last_busy(data->dev);
492 pm_runtime_put_autosuspend(data->dev);
Akinobu Mita62979902016-04-24 22:52:11 +0900493 break;
494 default:
495 return -EINVAL;
496 }
497
498 return ret;
499}
500
501static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
502{
503 size_t len = 0;
504 int i;
505
506 for (i = 0; i < n; i++)
507 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
508
509 buf[len - 1] = '\n';
510
511 return len;
512}
513
514static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
515 struct device_attribute *attr, char *buf)
516{
517 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
518
519 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
520 data->chip_info->num_oversampling_temp_avail);
521}
522
523static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
524 struct device_attribute *attr, char *buf)
525{
526 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
527
528 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
529 data->chip_info->num_oversampling_press_avail);
530}
531
532static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
533 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
534
535static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
536 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
537
538static struct attribute *bmp280_attributes[] = {
539 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
540 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
541 NULL,
542};
543
544static const struct attribute_group bmp280_attrs_group = {
545 .attrs = bmp280_attributes,
546};
547
Vlad Dogarud5c94562014-10-21 11:09:58 +0300548static const struct iio_info bmp280_info = {
549 .driver_module = THIS_MODULE,
550 .read_raw = &bmp280_read_raw,
Akinobu Mita62979902016-04-24 22:52:11 +0900551 .write_raw = &bmp280_write_raw,
552 .attrs = &bmp280_attrs_group,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300553};
554
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900555static int bmp280_chip_config(struct bmp280_data *data)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300556{
557 int ret;
Akinobu Mita62979902016-04-24 22:52:11 +0900558 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
559 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300560
561 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
562 BMP280_OSRS_TEMP_MASK |
563 BMP280_OSRS_PRESS_MASK |
564 BMP280_MODE_MASK,
Akinobu Mita62979902016-04-24 22:52:11 +0900565 osrs | BMP280_MODE_NORMAL);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300566 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200567 dev_err(data->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100568 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300569 return ret;
570 }
571
572 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
573 BMP280_FILTER_MASK,
574 BMP280_FILTER_4X);
575 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200576 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300577 "failed to write config register\n");
578 return ret;
579 }
580
581 return ret;
582}
583
Akinobu Mita62979902016-04-24 22:52:11 +0900584static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
585
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900586static const struct bmp280_chip_info bmp280_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900587 .oversampling_temp_avail = bmp280_oversampling_avail,
588 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
589
590 .oversampling_press_avail = bmp280_oversampling_avail,
591 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
592
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900593 .chip_config = bmp280_chip_config,
594 .read_temp = bmp280_read_temp,
595 .read_press = bmp280_read_press,
596};
597
Matt Ranostay14beaa82016-05-04 22:57:30 -0700598static int bme280_chip_config(struct bmp280_data *data)
599{
600 int ret = bmp280_chip_config(data);
601 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
602
603 if (ret < 0)
604 return ret;
605
606 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
607 BMP280_OSRS_HUMIDITY_MASK, osrs);
608}
609
610static const struct bmp280_chip_info bme280_chip_info = {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700611 .oversampling_temp_avail = bmp280_oversampling_avail,
612 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
613
614 .oversampling_press_avail = bmp280_oversampling_avail,
615 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
616
617 .oversampling_humid_avail = bmp280_oversampling_avail,
618 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
619
620 .chip_config = bme280_chip_config,
621 .read_temp = bmp280_read_temp,
622 .read_press = bmp280_read_press,
623 .read_humid = bmp280_read_humid,
624};
625
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900626static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
627{
628 int ret;
629 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
630 unsigned int delay_us;
631 unsigned int ctrl;
632
Linus Walleijaae95392016-06-30 03:48:52 +0200633 if (data->use_eoc)
634 init_completion(&data->done);
635
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900636 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
637 if (ret)
638 return ret;
639
Linus Walleijaae95392016-06-30 03:48:52 +0200640 if (data->use_eoc) {
641 /*
642 * If we have a completion interrupt, use it, wait up to
643 * 100ms. The longest conversion time listed is 76.5 ms for
644 * advanced resolution mode.
645 */
646 ret = wait_for_completion_timeout(&data->done,
647 1 + msecs_to_jiffies(100));
648 if (!ret)
649 dev_err(data->dev, "timeout waiting for completion\n");
650 } else {
651 if (ctrl_meas == BMP180_MEAS_TEMP)
652 delay_us = 4500;
653 else
654 delay_us =
655 conversion_time_max[data->oversampling_press];
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900656
Linus Walleijaae95392016-06-30 03:48:52 +0200657 usleep_range(delay_us, delay_us + 1000);
658 }
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900659
660 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
661 if (ret)
662 return ret;
663
664 /* The value of this bit reset to "0" after conversion is complete */
665 if (ctrl & BMP180_MEAS_SCO)
666 return -EIO;
667
668 return 0;
669}
670
671static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
672{
673 int ret;
674 __be16 tmp = 0;
675
676 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
677 if (ret)
678 return ret;
679
680 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
681 if (ret)
682 return ret;
683
684 *val = be16_to_cpu(tmp);
685
686 return 0;
687}
688
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900689static int bmp180_read_calib(struct bmp280_data *data,
690 struct bmp180_calib *calib)
691{
692 int ret;
693 int i;
694 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
695
696 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
697 sizeof(buf));
698
699 if (ret < 0)
700 return ret;
701
702 /* None of the words has the value 0 or 0xFFFF */
703 for (i = 0; i < ARRAY_SIZE(buf); i++) {
704 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
705 return -EIO;
706 }
707
Linus Walleijb33b7d52016-06-30 03:48:54 +0200708 /* Toss the calibration data into the entropy pool */
709 add_device_randomness(buf, sizeof(buf));
710
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900711 calib->AC1 = be16_to_cpu(buf[AC1]);
712 calib->AC2 = be16_to_cpu(buf[AC2]);
713 calib->AC3 = be16_to_cpu(buf[AC3]);
714 calib->AC4 = be16_to_cpu(buf[AC4]);
715 calib->AC5 = be16_to_cpu(buf[AC5]);
716 calib->AC6 = be16_to_cpu(buf[AC6]);
717 calib->B1 = be16_to_cpu(buf[B1]);
718 calib->B2 = be16_to_cpu(buf[B2]);
719 calib->MB = be16_to_cpu(buf[MB]);
720 calib->MC = be16_to_cpu(buf[MC]);
721 calib->MD = be16_to_cpu(buf[MD]);
722
723 return 0;
724}
725
726/*
727 * Returns temperature in DegC, resolution is 0.1 DegC.
728 * t_fine carries fine temperature as global value.
729 *
730 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
731 */
732static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
733{
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900734 s32 x1, x2;
Linus Walleijb33b7d52016-06-30 03:48:54 +0200735 struct bmp180_calib *calib = &data->calib;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900736
Linus Walleijb33b7d52016-06-30 03:48:54 +0200737 x1 = ((adc_temp - calib->AC6) * calib->AC5) >> 15;
738 x2 = (calib->MC << 11) / (x1 + calib->MD);
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900739 data->t_fine = x1 + x2;
740
741 return (data->t_fine + 8) >> 4;
742}
743
744static int bmp180_read_temp(struct bmp280_data *data, int *val)
745{
746 int ret;
747 s32 adc_temp, comp_temp;
748
749 ret = bmp180_read_adc_temp(data, &adc_temp);
750 if (ret)
751 return ret;
752
753 comp_temp = bmp180_compensate_temp(data, adc_temp);
754
755 /*
756 * val might be NULL if we're called by the read_press routine,
757 * who only cares about the carry over t_fine value.
758 */
759 if (val) {
760 *val = comp_temp * 100;
761 return IIO_VAL_INT;
762 }
763
764 return 0;
765}
766
767static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
768{
769 int ret;
770 __be32 tmp = 0;
Akinobu Mita62979902016-04-24 22:52:11 +0900771 u8 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900772
773 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
774 if (ret)
775 return ret;
776
777 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
778 if (ret)
779 return ret;
780
781 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
782
783 return 0;
784}
785
786/*
787 * Returns pressure in Pa, resolution is 1 Pa.
788 *
789 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
790 */
791static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
792{
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900793 s32 x1, x2, x3, p;
794 s32 b3, b6;
795 u32 b4, b7;
Akinobu Mita62979902016-04-24 22:52:11 +0900796 s32 oss = data->oversampling_press;
Linus Walleijb33b7d52016-06-30 03:48:54 +0200797 struct bmp180_calib *calib = &data->calib;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900798
799 b6 = data->t_fine - 4000;
Linus Walleijb33b7d52016-06-30 03:48:54 +0200800 x1 = (calib->B2 * (b6 * b6 >> 12)) >> 11;
801 x2 = calib->AC2 * b6 >> 11;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900802 x3 = x1 + x2;
Linus Walleijb33b7d52016-06-30 03:48:54 +0200803 b3 = ((((s32)calib->AC1 * 4 + x3) << oss) + 2) / 4;
804 x1 = calib->AC3 * b6 >> 13;
805 x2 = (calib->B1 * ((b6 * b6) >> 12)) >> 16;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900806 x3 = (x1 + x2 + 2) >> 2;
Linus Walleijb33b7d52016-06-30 03:48:54 +0200807 b4 = calib->AC4 * (u32)(x3 + 32768) >> 15;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900808 b7 = ((u32)adc_press - b3) * (50000 >> oss);
809 if (b7 < 0x80000000)
810 p = (b7 * 2) / b4;
811 else
812 p = (b7 / b4) * 2;
813
814 x1 = (p >> 8) * (p >> 8);
815 x1 = (x1 * 3038) >> 16;
816 x2 = (-7357 * p) >> 16;
817
818 return p + ((x1 + x2 + 3791) >> 4);
819}
820
821static int bmp180_read_press(struct bmp280_data *data,
822 int *val, int *val2)
823{
824 int ret;
825 s32 adc_press;
826 u32 comp_press;
827
828 /* Read and compensate temperature so we get a reading of t_fine. */
829 ret = bmp180_read_temp(data, NULL);
830 if (ret)
831 return ret;
832
833 ret = bmp180_read_adc_press(data, &adc_press);
834 if (ret)
835 return ret;
836
837 comp_press = bmp180_compensate_press(data, adc_press);
838
839 *val = comp_press;
840 *val2 = 1000;
841
842 return IIO_VAL_FRACTIONAL;
843}
844
845static int bmp180_chip_config(struct bmp280_data *data)
846{
847 return 0;
848}
849
Akinobu Mita62979902016-04-24 22:52:11 +0900850static const int bmp180_oversampling_temp_avail[] = { 1 };
851static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
852
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900853static const struct bmp280_chip_info bmp180_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900854 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
855 .num_oversampling_temp_avail =
856 ARRAY_SIZE(bmp180_oversampling_temp_avail),
857
858 .oversampling_press_avail = bmp180_oversampling_press_avail,
859 .num_oversampling_press_avail =
860 ARRAY_SIZE(bmp180_oversampling_press_avail),
861
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900862 .chip_config = bmp180_chip_config,
863 .read_temp = bmp180_read_temp,
864 .read_press = bmp180_read_press,
865};
866
Linus Walleijaae95392016-06-30 03:48:52 +0200867static irqreturn_t bmp085_eoc_irq(int irq, void *d)
868{
869 struct bmp280_data *data = d;
870
871 complete(&data->done);
872
873 return IRQ_HANDLED;
874}
875
876static int bmp085_fetch_eoc_irq(struct device *dev,
877 const char *name,
878 int irq,
879 struct bmp280_data *data)
880{
881 unsigned long irq_trig;
882 int ret;
883
884 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
885 if (irq_trig != IRQF_TRIGGER_RISING) {
886 dev_err(dev, "non-rising trigger given for EOC interrupt, "
887 "trying to enforce it\n");
888 irq_trig = IRQF_TRIGGER_RISING;
889 }
890 ret = devm_request_threaded_irq(dev,
891 irq,
892 bmp085_eoc_irq,
893 NULL,
894 irq_trig,
895 name,
896 data);
897 if (ret) {
898 /* Bail out without IRQ but keep the driver in place */
899 dev_err(dev, "unable to request DRDY IRQ\n");
900 return 0;
901 }
902
903 data->use_eoc = true;
904 return 0;
905}
906
Linus Walleij14e80152016-06-30 03:48:49 +0200907int bmp280_common_probe(struct device *dev,
908 struct regmap *regmap,
909 unsigned int chip,
Linus Walleijaae95392016-06-30 03:48:52 +0200910 const char *name,
911 int irq)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300912{
913 int ret;
914 struct iio_dev *indio_dev;
915 struct bmp280_data *data;
916 unsigned int chip_id;
Linus Walleijc5842b42016-06-30 03:48:47 +0200917 struct gpio_desc *gpiod;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300918
Linus Walleij14e80152016-06-30 03:48:49 +0200919 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
Vlad Dogarud5c94562014-10-21 11:09:58 +0300920 if (!indio_dev)
921 return -ENOMEM;
922
Vlad Dogarud5c94562014-10-21 11:09:58 +0300923 data = iio_priv(indio_dev);
924 mutex_init(&data->lock);
Linus Walleij14e80152016-06-30 03:48:49 +0200925 data->dev = dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300926
Linus Walleij14e80152016-06-30 03:48:49 +0200927 indio_dev->dev.parent = dev;
928 indio_dev->name = name;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300929 indio_dev->channels = bmp280_channels;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300930 indio_dev->info = &bmp280_info;
931 indio_dev->modes = INDIO_DIRECT_MODE;
932
Linus Walleij14e80152016-06-30 03:48:49 +0200933 switch (chip) {
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900934 case BMP180_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700935 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900936 data->chip_info = &bmp180_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900937 data->oversampling_press = ilog2(8);
938 data->oversampling_temp = ilog2(1);
Linus Walleijbd525e62016-06-30 03:48:48 +0200939 data->start_up_time = 10;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900940 break;
941 case BMP280_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700942 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900943 data->chip_info = &bmp280_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900944 data->oversampling_press = ilog2(16);
945 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200946 data->start_up_time = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900947 break;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700948 case BME280_CHIP_ID:
949 indio_dev->num_channels = 3;
950 data->chip_info = &bme280_chip_info;
951 data->oversampling_press = ilog2(16);
952 data->oversampling_humid = ilog2(16);
953 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200954 data->start_up_time = 2;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700955 break;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900956 default:
957 return -EINVAL;
958 }
959
Linus Walleijbd525e62016-06-30 03:48:48 +0200960 /* Bring up regulators */
Linus Walleij14e80152016-06-30 03:48:49 +0200961 data->vddd = devm_regulator_get(dev, "vddd");
Linus Walleijbd525e62016-06-30 03:48:48 +0200962 if (IS_ERR(data->vddd)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200963 dev_err(dev, "failed to get VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200964 return PTR_ERR(data->vddd);
965 }
966 ret = regulator_enable(data->vddd);
967 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200968 dev_err(dev, "failed to enable VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200969 return ret;
970 }
Linus Walleij14e80152016-06-30 03:48:49 +0200971 data->vdda = devm_regulator_get(dev, "vdda");
Linus Walleijbd525e62016-06-30 03:48:48 +0200972 if (IS_ERR(data->vdda)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200973 dev_err(dev, "failed to get VDDA regulator\n");
Wei Yongjunbb9947c2016-07-14 11:06:11 +0000974 ret = PTR_ERR(data->vdda);
Linus Walleijbd525e62016-06-30 03:48:48 +0200975 goto out_disable_vddd;
976 }
977 ret = regulator_enable(data->vdda);
978 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200979 dev_err(dev, "failed to enable VDDA regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200980 goto out_disable_vddd;
981 }
982 /* Wait to make sure we started up properly */
983 mdelay(data->start_up_time);
984
Linus Walleijc5842b42016-06-30 03:48:47 +0200985 /* Bring chip out of reset if there is an assigned GPIO line */
Linus Walleij14e80152016-06-30 03:48:49 +0200986 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
Linus Walleijc5842b42016-06-30 03:48:47 +0200987 /* Deassert the signal */
988 if (!IS_ERR(gpiod)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200989 dev_info(dev, "release reset\n");
Linus Walleijc5842b42016-06-30 03:48:47 +0200990 gpiod_set_value(gpiod, 0);
991 }
992
Linus Walleij14e80152016-06-30 03:48:49 +0200993 data->regmap = regmap;
994 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300995 if (ret < 0)
Linus Walleij14e80152016-06-30 03:48:49 +0200996 goto out_disable_vdda;
997 if (chip_id != chip) {
998 dev_err(dev, "bad chip id: expected %x got %x\n",
999 chip, chip_id);
Linus Walleijbd525e62016-06-30 03:48:48 +02001000 ret = -EINVAL;
1001 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001002 }
1003
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001004 ret = data->chip_info->chip_config(data);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001005 if (ret < 0)
Linus Walleijbd525e62016-06-30 03:48:48 +02001006 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001007
Linus Walleij14e80152016-06-30 03:48:49 +02001008 dev_set_drvdata(dev, indio_dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001009
Linus Walleijaae95392016-06-30 03:48:52 +02001010 /*
Linus Walleijb33b7d52016-06-30 03:48:54 +02001011 * The BMP085 and BMP180 has calibration in an E2PROM, read it out
1012 * at probe time. It will not change.
1013 */
1014 if (chip_id == BMP180_CHIP_ID) {
1015 ret = bmp180_read_calib(data, &data->calib);
1016 if (ret < 0) {
1017 dev_err(data->dev,
1018 "failed to read calibration coefficients\n");
1019 goto out_disable_vdda;
1020 }
1021 }
1022
1023 /*
Linus Walleijaae95392016-06-30 03:48:52 +02001024 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1025 * however as it happens, the BMP085 shares the chip ID of BMP180
1026 * so we look for an IRQ if we have that.
1027 */
1028 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1029 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1030 if (ret)
1031 goto out_disable_vdda;
1032 }
1033
Linus Walleij3d838112016-06-30 03:48:53 +02001034 /* Enable runtime PM */
1035 pm_runtime_get_noresume(dev);
1036 pm_runtime_set_active(dev);
1037 pm_runtime_enable(dev);
1038 /*
1039 * Set autosuspend to two orders of magnitude larger than the
1040 * start-up time.
1041 */
1042 pm_runtime_set_autosuspend_delay(dev, data->start_up_time *100);
1043 pm_runtime_use_autosuspend(dev);
1044 pm_runtime_put(dev);
1045
Linus Walleijbd525e62016-06-30 03:48:48 +02001046 ret = iio_device_register(indio_dev);
1047 if (ret)
Linus Walleij3d838112016-06-30 03:48:53 +02001048 goto out_runtime_pm_disable;
1049
Linus Walleijbd525e62016-06-30 03:48:48 +02001050
1051 return 0;
1052
Linus Walleij3d838112016-06-30 03:48:53 +02001053out_runtime_pm_disable:
1054 pm_runtime_get_sync(data->dev);
1055 pm_runtime_put_noidle(data->dev);
1056 pm_runtime_disable(data->dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001057out_disable_vdda:
1058 regulator_disable(data->vdda);
1059out_disable_vddd:
1060 regulator_disable(data->vddd);
1061 return ret;
1062}
Linus Walleij17118842016-06-30 03:48:50 +02001063EXPORT_SYMBOL(bmp280_common_probe);
Linus Walleijbd525e62016-06-30 03:48:48 +02001064
Linus Walleij14e80152016-06-30 03:48:49 +02001065int bmp280_common_remove(struct device *dev)
Linus Walleijbd525e62016-06-30 03:48:48 +02001066{
Linus Walleij14e80152016-06-30 03:48:49 +02001067 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001068 struct bmp280_data *data = iio_priv(indio_dev);
1069
1070 iio_device_unregister(indio_dev);
Linus Walleij3d838112016-06-30 03:48:53 +02001071 pm_runtime_get_sync(data->dev);
1072 pm_runtime_put_noidle(data->dev);
1073 pm_runtime_disable(data->dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001074 regulator_disable(data->vdda);
1075 regulator_disable(data->vddd);
1076 return 0;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001077}
Linus Walleij17118842016-06-30 03:48:50 +02001078EXPORT_SYMBOL(bmp280_common_remove);
1079
Linus Walleij3d838112016-06-30 03:48:53 +02001080#ifdef CONFIG_PM
1081static int bmp280_runtime_suspend(struct device *dev)
1082{
Linus Walleij31f453e2016-07-27 22:32:58 +02001083 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1084 struct bmp280_data *data = iio_priv(indio_dev);
Linus Walleij3d838112016-06-30 03:48:53 +02001085 int ret;
1086
1087 ret = regulator_disable(data->vdda);
1088 if (ret)
1089 return ret;
1090 return regulator_disable(data->vddd);
1091}
1092
1093static int bmp280_runtime_resume(struct device *dev)
1094{
Linus Walleij31f453e2016-07-27 22:32:58 +02001095 struct iio_dev *indio_dev = dev_get_drvdata(dev);
1096 struct bmp280_data *data = iio_priv(indio_dev);
Linus Walleij3d838112016-06-30 03:48:53 +02001097 int ret;
1098
1099 ret = regulator_enable(data->vddd);
1100 if (ret)
1101 return ret;
1102 ret = regulator_enable(data->vdda);
1103 if (ret)
1104 return ret;
1105 msleep(data->start_up_time);
1106 return data->chip_info->chip_config(data);
1107}
1108#endif /* CONFIG_PM */
1109
1110const struct dev_pm_ops bmp280_dev_pm_ops = {
1111 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1112 pm_runtime_force_resume)
1113 SET_RUNTIME_PM_OPS(bmp280_runtime_suspend,
1114 bmp280_runtime_resume, NULL)
1115};
1116EXPORT_SYMBOL(bmp280_dev_pm_ops);
1117
Linus Walleij17118842016-06-30 03:48:50 +02001118MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1119MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1120MODULE_LICENSE("GPL v2");