blob: 3ebd84f06e36609f85292e3a13721ad9505d5c5f [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>
Vlad Dogarud5c94562014-10-21 11:09:58 +030033
Linus Walleij14e80152016-06-30 03:48:49 +020034#include "bmp280.h"
Vlad Dogarud5c94562014-10-21 11:09:58 +030035
36struct bmp280_data {
Linus Walleij14e80152016-06-30 03:48:49 +020037 struct device *dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +030038 struct mutex lock;
39 struct regmap *regmap;
Linus Walleijaae95392016-06-30 03:48:52 +020040 struct completion done;
41 bool use_eoc;
Akinobu Mita6dba72e2016-04-24 22:52:10 +090042 const struct bmp280_chip_info *chip_info;
Linus Walleijbd525e62016-06-30 03:48:48 +020043 struct regulator *vddd;
44 struct regulator *vdda;
45 unsigned int start_up_time; /* in milliseconds */
Vlad Dogarud5c94562014-10-21 11:09:58 +030046
Akinobu Mita62979902016-04-24 22:52:11 +090047 /* log of base 2 of oversampling rate */
48 u8 oversampling_press;
49 u8 oversampling_temp;
Matt Ranostay14beaa82016-05-04 22:57:30 -070050 u8 oversampling_humid;
Akinobu Mita62979902016-04-24 22:52:11 +090051
Vlad Dogarud5c94562014-10-21 11:09:58 +030052 /*
53 * Carryover value from temperature conversion, used in pressure
54 * calculation.
55 */
56 s32 t_fine;
57};
58
Akinobu Mita6dba72e2016-04-24 22:52:10 +090059struct bmp280_chip_info {
Akinobu Mita62979902016-04-24 22:52:11 +090060 const int *oversampling_temp_avail;
61 int num_oversampling_temp_avail;
62
63 const int *oversampling_press_avail;
64 int num_oversampling_press_avail;
65
Matt Ranostay14beaa82016-05-04 22:57:30 -070066 const int *oversampling_humid_avail;
67 int num_oversampling_humid_avail;
68
Akinobu Mita6dba72e2016-04-24 22:52:10 +090069 int (*chip_config)(struct bmp280_data *);
70 int (*read_temp)(struct bmp280_data *, int *);
71 int (*read_press)(struct bmp280_data *, int *, int *);
Matt Ranostay14beaa82016-05-04 22:57:30 -070072 int (*read_humid)(struct bmp280_data *, int *, int *);
Akinobu Mita6dba72e2016-04-24 22:52:10 +090073};
74
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020075/*
76 * These enums are used for indexing into the array of compensation
Akinobu Mita6dba72e2016-04-24 22:52:10 +090077 * parameters for BMP280.
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020078 */
79enum { T1, T2, T3 };
80enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +030081
82static const struct iio_chan_spec bmp280_channels[] = {
83 {
84 .type = IIO_PRESSURE,
Akinobu Mita62979902016-04-24 22:52:11 +090085 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
86 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +030087 },
88 {
89 .type = IIO_TEMP,
Akinobu Mita62979902016-04-24 22:52:11 +090090 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
91 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +030092 },
Matt Ranostay14beaa82016-05-04 22:57:30 -070093 {
94 .type = IIO_HUMIDITYRELATIVE,
95 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
96 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
97 },
Vlad Dogarud5c94562014-10-21 11:09:58 +030098};
99
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200100/*
Matt Ranostay14beaa82016-05-04 22:57:30 -0700101 * Returns humidity in percent, resolution is 0.01 percent. Output value of
102 * "47445" represents 47445/1024 = 46.333 %RH.
103 *
104 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
105 */
106
107static u32 bmp280_compensate_humidity(struct bmp280_data *data,
108 s32 adc_humidity)
109{
Linus Walleij14e80152016-06-30 03:48:49 +0200110 struct device *dev = data->dev;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700111 unsigned int H1, H3, tmp;
112 int H2, H4, H5, H6, ret, var;
113
114 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
115 if (ret < 0) {
116 dev_err(dev, "failed to read H1 comp value\n");
117 return ret;
118 }
119
120 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
121 if (ret < 0) {
122 dev_err(dev, "failed to read H2 comp value\n");
123 return ret;
124 }
125 H2 = sign_extend32(le16_to_cpu(tmp), 15);
126
127 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
128 if (ret < 0) {
129 dev_err(dev, "failed to read H3 comp value\n");
130 return ret;
131 }
132
133 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
134 if (ret < 0) {
135 dev_err(dev, "failed to read H4 comp value\n");
136 return ret;
137 }
138 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
139 (be16_to_cpu(tmp) & 0xf), 11);
140
141 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
142 if (ret < 0) {
143 dev_err(dev, "failed to read H5 comp value\n");
144 return ret;
145 }
146 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
147
148 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
149 if (ret < 0) {
150 dev_err(dev, "failed to read H6 comp value\n");
151 return ret;
152 }
153 H6 = sign_extend32(tmp, 7);
154
155 var = ((s32)data->t_fine) - 76800;
156 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
157 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
158 + 2097152) * H2 + 8192) >> 14);
159 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
160
161 return var >> 12;
162};
163
164/*
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200165 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
166 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
167 * value.
168 *
169 * Taken from datasheet, Section 3.11.3, "Compensation formula".
170 */
171static s32 bmp280_compensate_temp(struct bmp280_data *data,
172 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300173{
174 int ret;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100175 s32 var1, var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300176 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
177
178 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
179 buf, BMP280_COMP_TEMP_REG_COUNT);
180 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200181 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300182 "failed to read temperature calibration parameters\n");
183 return ret;
184 }
185
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200186 /*
187 * The double casts are necessary because le16_to_cpu returns an
188 * unsigned 16-bit value. Casting that value directly to a
189 * signed 32-bit will not do proper sign extension.
190 *
191 * Conversely, T1 and P1 are unsigned values, so they can be
192 * cast straight to the larger type.
193 */
194 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
195 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
196 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
197 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
198 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
Irina Tirdeaabad3982015-04-08 18:26:12 +0300199 data->t_fine = var1 + var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300200
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100201 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300202}
203
204/*
205 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
206 * integer bits and 8 fractional bits). Output value of "24674867"
207 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
208 *
209 * Taken from datasheet, Section 3.11.3, "Compensation formula".
210 */
211static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300212 s32 adc_press)
213{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200214 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300215 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200216 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300217
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200218 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
219 buf, BMP280_COMP_PRESS_REG_COUNT);
220 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200221 dev_err(data->dev,
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200222 "failed to read pressure calibration parameters\n");
223 return ret;
224 }
225
226 var1 = ((s64)data->t_fine) - 128000;
227 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100228 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
229 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200230 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
231 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
232 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300233
234 if (var1 == 0)
235 return 0;
236
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200237 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100238 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200239 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
240 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
241 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300242
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100243 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300244}
245
246static int bmp280_read_temp(struct bmp280_data *data,
247 int *val)
248{
249 int ret;
250 __be32 tmp = 0;
251 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300252
253 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
254 (u8 *) &tmp, 3);
255 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200256 dev_err(data->dev, "failed to read temperature\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300257 return ret;
258 }
259
260 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200261 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300262
263 /*
264 * val might be NULL if we're called by the read_press routine,
265 * who only cares about the carry over t_fine value.
266 */
267 if (val) {
268 *val = comp_temp * 10;
269 return IIO_VAL_INT;
270 }
271
272 return 0;
273}
274
275static int bmp280_read_press(struct bmp280_data *data,
276 int *val, int *val2)
277{
278 int ret;
279 __be32 tmp = 0;
280 s32 adc_press;
281 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300282
283 /* Read and compensate temperature so we get a reading of t_fine. */
284 ret = bmp280_read_temp(data, NULL);
285 if (ret < 0)
286 return ret;
287
288 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
289 (u8 *) &tmp, 3);
290 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200291 dev_err(data->dev, "failed to read pressure\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300292 return ret;
293 }
294
295 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200296 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300297
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000298 *val = comp_press;
299 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300300
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000301 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300302}
303
Matt Ranostay14beaa82016-05-04 22:57:30 -0700304static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
305{
306 int ret;
307 __be16 tmp = 0;
308 s32 adc_humidity;
309 u32 comp_humidity;
310
311 /* Read and compensate temperature so we get a reading of t_fine. */
312 ret = bmp280_read_temp(data, NULL);
313 if (ret < 0)
314 return ret;
315
316 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
317 (u8 *) &tmp, 2);
318 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200319 dev_err(data->dev, "failed to read humidity\n");
Matt Ranostay14beaa82016-05-04 22:57:30 -0700320 return ret;
321 }
322
323 adc_humidity = be16_to_cpu(tmp);
324 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
325
326 *val = comp_humidity;
327 *val2 = 1024;
328
329 return IIO_VAL_FRACTIONAL;
330}
331
Vlad Dogarud5c94562014-10-21 11:09:58 +0300332static int bmp280_read_raw(struct iio_dev *indio_dev,
333 struct iio_chan_spec const *chan,
334 int *val, int *val2, long mask)
335{
336 int ret;
337 struct bmp280_data *data = iio_priv(indio_dev);
338
339 mutex_lock(&data->lock);
340
341 switch (mask) {
342 case IIO_CHAN_INFO_PROCESSED:
343 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700344 case IIO_HUMIDITYRELATIVE:
345 ret = data->chip_info->read_humid(data, val, val2);
346 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300347 case IIO_PRESSURE:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900348 ret = data->chip_info->read_press(data, val, val2);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300349 break;
350 case IIO_TEMP:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900351 ret = data->chip_info->read_temp(data, val);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300352 break;
353 default:
354 ret = -EINVAL;
355 break;
356 }
357 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900358 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
359 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700360 case IIO_HUMIDITYRELATIVE:
361 *val = 1 << data->oversampling_humid;
362 ret = IIO_VAL_INT;
363 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900364 case IIO_PRESSURE:
365 *val = 1 << data->oversampling_press;
366 ret = IIO_VAL_INT;
367 break;
368 case IIO_TEMP:
369 *val = 1 << data->oversampling_temp;
370 ret = IIO_VAL_INT;
371 break;
372 default:
373 ret = -EINVAL;
374 break;
375 }
376 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300377 default:
378 ret = -EINVAL;
379 break;
380 }
381
382 mutex_unlock(&data->lock);
383
384 return ret;
385}
386
Matt Ranostay14beaa82016-05-04 22:57:30 -0700387static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
388 int val)
389{
390 int i;
391 const int *avail = data->chip_info->oversampling_humid_avail;
392 const int n = data->chip_info->num_oversampling_humid_avail;
393
394 for (i = 0; i < n; i++) {
395 if (avail[i] == val) {
396 data->oversampling_humid = ilog2(val);
397
398 return data->chip_info->chip_config(data);
399 }
400 }
401 return -EINVAL;
402}
403
Akinobu Mita62979902016-04-24 22:52:11 +0900404static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
405 int val)
406{
407 int i;
408 const int *avail = data->chip_info->oversampling_temp_avail;
409 const int n = data->chip_info->num_oversampling_temp_avail;
410
411 for (i = 0; i < n; i++) {
412 if (avail[i] == val) {
413 data->oversampling_temp = ilog2(val);
414
415 return data->chip_info->chip_config(data);
416 }
417 }
418 return -EINVAL;
419}
420
421static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
422 int val)
423{
424 int i;
425 const int *avail = data->chip_info->oversampling_press_avail;
426 const int n = data->chip_info->num_oversampling_press_avail;
427
428 for (i = 0; i < n; i++) {
429 if (avail[i] == val) {
430 data->oversampling_press = ilog2(val);
431
432 return data->chip_info->chip_config(data);
433 }
434 }
435 return -EINVAL;
436}
437
438static int bmp280_write_raw(struct iio_dev *indio_dev,
439 struct iio_chan_spec const *chan,
440 int val, int val2, long mask)
441{
442 int ret = 0;
443 struct bmp280_data *data = iio_priv(indio_dev);
444
445 switch (mask) {
446 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
447 mutex_lock(&data->lock);
448 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700449 case IIO_HUMIDITYRELATIVE:
450 ret = bmp280_write_oversampling_ratio_humid(data, val);
451 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900452 case IIO_PRESSURE:
453 ret = bmp280_write_oversampling_ratio_press(data, val);
454 break;
455 case IIO_TEMP:
456 ret = bmp280_write_oversampling_ratio_temp(data, val);
457 break;
458 default:
459 ret = -EINVAL;
460 break;
461 }
462 mutex_unlock(&data->lock);
463 break;
464 default:
465 return -EINVAL;
466 }
467
468 return ret;
469}
470
471static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
472{
473 size_t len = 0;
474 int i;
475
476 for (i = 0; i < n; i++)
477 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
478
479 buf[len - 1] = '\n';
480
481 return len;
482}
483
484static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
485 struct device_attribute *attr, char *buf)
486{
487 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
488
489 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
490 data->chip_info->num_oversampling_temp_avail);
491}
492
493static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
494 struct device_attribute *attr, char *buf)
495{
496 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
497
498 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
499 data->chip_info->num_oversampling_press_avail);
500}
501
502static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
503 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
504
505static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
506 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
507
508static struct attribute *bmp280_attributes[] = {
509 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
510 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
511 NULL,
512};
513
514static const struct attribute_group bmp280_attrs_group = {
515 .attrs = bmp280_attributes,
516};
517
Vlad Dogarud5c94562014-10-21 11:09:58 +0300518static const struct iio_info bmp280_info = {
519 .driver_module = THIS_MODULE,
520 .read_raw = &bmp280_read_raw,
Akinobu Mita62979902016-04-24 22:52:11 +0900521 .write_raw = &bmp280_write_raw,
522 .attrs = &bmp280_attrs_group,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300523};
524
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900525static int bmp280_chip_config(struct bmp280_data *data)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300526{
527 int ret;
Akinobu Mita62979902016-04-24 22:52:11 +0900528 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
529 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300530
531 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
532 BMP280_OSRS_TEMP_MASK |
533 BMP280_OSRS_PRESS_MASK |
534 BMP280_MODE_MASK,
Akinobu Mita62979902016-04-24 22:52:11 +0900535 osrs | BMP280_MODE_NORMAL);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300536 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200537 dev_err(data->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100538 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300539 return ret;
540 }
541
542 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
543 BMP280_FILTER_MASK,
544 BMP280_FILTER_4X);
545 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200546 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300547 "failed to write config register\n");
548 return ret;
549 }
550
551 return ret;
552}
553
Akinobu Mita62979902016-04-24 22:52:11 +0900554static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
555
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900556static const struct bmp280_chip_info bmp280_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900557 .oversampling_temp_avail = bmp280_oversampling_avail,
558 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
559
560 .oversampling_press_avail = bmp280_oversampling_avail,
561 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
562
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900563 .chip_config = bmp280_chip_config,
564 .read_temp = bmp280_read_temp,
565 .read_press = bmp280_read_press,
566};
567
Matt Ranostay14beaa82016-05-04 22:57:30 -0700568static int bme280_chip_config(struct bmp280_data *data)
569{
570 int ret = bmp280_chip_config(data);
571 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
572
573 if (ret < 0)
574 return ret;
575
576 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
577 BMP280_OSRS_HUMIDITY_MASK, osrs);
578}
579
580static const struct bmp280_chip_info bme280_chip_info = {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700581 .oversampling_temp_avail = bmp280_oversampling_avail,
582 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
583
584 .oversampling_press_avail = bmp280_oversampling_avail,
585 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
586
587 .oversampling_humid_avail = bmp280_oversampling_avail,
588 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
589
590 .chip_config = bme280_chip_config,
591 .read_temp = bmp280_read_temp,
592 .read_press = bmp280_read_press,
593 .read_humid = bmp280_read_humid,
594};
595
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900596static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
597{
598 int ret;
599 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
600 unsigned int delay_us;
601 unsigned int ctrl;
602
Linus Walleijaae95392016-06-30 03:48:52 +0200603 if (data->use_eoc)
604 init_completion(&data->done);
605
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900606 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
607 if (ret)
608 return ret;
609
Linus Walleijaae95392016-06-30 03:48:52 +0200610 if (data->use_eoc) {
611 /*
612 * If we have a completion interrupt, use it, wait up to
613 * 100ms. The longest conversion time listed is 76.5 ms for
614 * advanced resolution mode.
615 */
616 ret = wait_for_completion_timeout(&data->done,
617 1 + msecs_to_jiffies(100));
618 if (!ret)
619 dev_err(data->dev, "timeout waiting for completion\n");
620 } else {
621 if (ctrl_meas == BMP180_MEAS_TEMP)
622 delay_us = 4500;
623 else
624 delay_us =
625 conversion_time_max[data->oversampling_press];
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900626
Linus Walleijaae95392016-06-30 03:48:52 +0200627 usleep_range(delay_us, delay_us + 1000);
628 }
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900629
630 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
631 if (ret)
632 return ret;
633
634 /* The value of this bit reset to "0" after conversion is complete */
635 if (ctrl & BMP180_MEAS_SCO)
636 return -EIO;
637
638 return 0;
639}
640
641static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
642{
643 int ret;
644 __be16 tmp = 0;
645
646 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
647 if (ret)
648 return ret;
649
650 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
651 if (ret)
652 return ret;
653
654 *val = be16_to_cpu(tmp);
655
656 return 0;
657}
658
659/*
660 * These enums are used for indexing into the array of calibration
661 * coefficients for BMP180.
662 */
663enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
664
665struct bmp180_calib {
666 s16 AC1;
667 s16 AC2;
668 s16 AC3;
669 u16 AC4;
670 u16 AC5;
671 u16 AC6;
672 s16 B1;
673 s16 B2;
674 s16 MB;
675 s16 MC;
676 s16 MD;
677};
678
679static int bmp180_read_calib(struct bmp280_data *data,
680 struct bmp180_calib *calib)
681{
682 int ret;
683 int i;
684 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
685
686 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
687 sizeof(buf));
688
689 if (ret < 0)
690 return ret;
691
692 /* None of the words has the value 0 or 0xFFFF */
693 for (i = 0; i < ARRAY_SIZE(buf); i++) {
694 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
695 return -EIO;
696 }
697
698 calib->AC1 = be16_to_cpu(buf[AC1]);
699 calib->AC2 = be16_to_cpu(buf[AC2]);
700 calib->AC3 = be16_to_cpu(buf[AC3]);
701 calib->AC4 = be16_to_cpu(buf[AC4]);
702 calib->AC5 = be16_to_cpu(buf[AC5]);
703 calib->AC6 = be16_to_cpu(buf[AC6]);
704 calib->B1 = be16_to_cpu(buf[B1]);
705 calib->B2 = be16_to_cpu(buf[B2]);
706 calib->MB = be16_to_cpu(buf[MB]);
707 calib->MC = be16_to_cpu(buf[MC]);
708 calib->MD = be16_to_cpu(buf[MD]);
709
710 return 0;
711}
712
713/*
714 * Returns temperature in DegC, resolution is 0.1 DegC.
715 * t_fine carries fine temperature as global value.
716 *
717 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
718 */
719static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
720{
721 int ret;
722 s32 x1, x2;
723 struct bmp180_calib calib;
724
725 ret = bmp180_read_calib(data, &calib);
726 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200727 dev_err(data->dev,
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900728 "failed to read calibration coefficients\n");
729 return ret;
730 }
731
732 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
733 x2 = (calib.MC << 11) / (x1 + calib.MD);
734 data->t_fine = x1 + x2;
735
736 return (data->t_fine + 8) >> 4;
737}
738
739static int bmp180_read_temp(struct bmp280_data *data, int *val)
740{
741 int ret;
742 s32 adc_temp, comp_temp;
743
744 ret = bmp180_read_adc_temp(data, &adc_temp);
745 if (ret)
746 return ret;
747
748 comp_temp = bmp180_compensate_temp(data, adc_temp);
749
750 /*
751 * val might be NULL if we're called by the read_press routine,
752 * who only cares about the carry over t_fine value.
753 */
754 if (val) {
755 *val = comp_temp * 100;
756 return IIO_VAL_INT;
757 }
758
759 return 0;
760}
761
762static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
763{
764 int ret;
765 __be32 tmp = 0;
Akinobu Mita62979902016-04-24 22:52:11 +0900766 u8 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900767
768 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
769 if (ret)
770 return ret;
771
772 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
773 if (ret)
774 return ret;
775
776 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
777
778 return 0;
779}
780
781/*
782 * Returns pressure in Pa, resolution is 1 Pa.
783 *
784 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
785 */
786static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
787{
788 int ret;
789 s32 x1, x2, x3, p;
790 s32 b3, b6;
791 u32 b4, b7;
Akinobu Mita62979902016-04-24 22:52:11 +0900792 s32 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900793 struct bmp180_calib calib;
794
795 ret = bmp180_read_calib(data, &calib);
796 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200797 dev_err(data->dev,
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900798 "failed to read calibration coefficients\n");
799 return ret;
800 }
801
802 b6 = data->t_fine - 4000;
803 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
804 x2 = calib.AC2 * b6 >> 11;
805 x3 = x1 + x2;
806 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
807 x1 = calib.AC3 * b6 >> 13;
808 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
809 x3 = (x1 + x2 + 2) >> 2;
810 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
811 b7 = ((u32)adc_press - b3) * (50000 >> oss);
812 if (b7 < 0x80000000)
813 p = (b7 * 2) / b4;
814 else
815 p = (b7 / b4) * 2;
816
817 x1 = (p >> 8) * (p >> 8);
818 x1 = (x1 * 3038) >> 16;
819 x2 = (-7357 * p) >> 16;
820
821 return p + ((x1 + x2 + 3791) >> 4);
822}
823
824static int bmp180_read_press(struct bmp280_data *data,
825 int *val, int *val2)
826{
827 int ret;
828 s32 adc_press;
829 u32 comp_press;
830
831 /* Read and compensate temperature so we get a reading of t_fine. */
832 ret = bmp180_read_temp(data, NULL);
833 if (ret)
834 return ret;
835
836 ret = bmp180_read_adc_press(data, &adc_press);
837 if (ret)
838 return ret;
839
840 comp_press = bmp180_compensate_press(data, adc_press);
841
842 *val = comp_press;
843 *val2 = 1000;
844
845 return IIO_VAL_FRACTIONAL;
846}
847
848static int bmp180_chip_config(struct bmp280_data *data)
849{
850 return 0;
851}
852
Akinobu Mita62979902016-04-24 22:52:11 +0900853static const int bmp180_oversampling_temp_avail[] = { 1 };
854static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
855
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900856static const struct bmp280_chip_info bmp180_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900857 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
858 .num_oversampling_temp_avail =
859 ARRAY_SIZE(bmp180_oversampling_temp_avail),
860
861 .oversampling_press_avail = bmp180_oversampling_press_avail,
862 .num_oversampling_press_avail =
863 ARRAY_SIZE(bmp180_oversampling_press_avail),
864
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900865 .chip_config = bmp180_chip_config,
866 .read_temp = bmp180_read_temp,
867 .read_press = bmp180_read_press,
868};
869
Linus Walleijaae95392016-06-30 03:48:52 +0200870static irqreturn_t bmp085_eoc_irq(int irq, void *d)
871{
872 struct bmp280_data *data = d;
873
874 complete(&data->done);
875
876 return IRQ_HANDLED;
877}
878
879static int bmp085_fetch_eoc_irq(struct device *dev,
880 const char *name,
881 int irq,
882 struct bmp280_data *data)
883{
884 unsigned long irq_trig;
885 int ret;
886
887 irq_trig = irqd_get_trigger_type(irq_get_irq_data(irq));
888 if (irq_trig != IRQF_TRIGGER_RISING) {
889 dev_err(dev, "non-rising trigger given for EOC interrupt, "
890 "trying to enforce it\n");
891 irq_trig = IRQF_TRIGGER_RISING;
892 }
893 ret = devm_request_threaded_irq(dev,
894 irq,
895 bmp085_eoc_irq,
896 NULL,
897 irq_trig,
898 name,
899 data);
900 if (ret) {
901 /* Bail out without IRQ but keep the driver in place */
902 dev_err(dev, "unable to request DRDY IRQ\n");
903 return 0;
904 }
905
906 data->use_eoc = true;
907 return 0;
908}
909
Linus Walleij14e80152016-06-30 03:48:49 +0200910int bmp280_common_probe(struct device *dev,
911 struct regmap *regmap,
912 unsigned int chip,
Linus Walleijaae95392016-06-30 03:48:52 +0200913 const char *name,
914 int irq)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300915{
916 int ret;
917 struct iio_dev *indio_dev;
918 struct bmp280_data *data;
919 unsigned int chip_id;
Linus Walleijc5842b42016-06-30 03:48:47 +0200920 struct gpio_desc *gpiod;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300921
Linus Walleij14e80152016-06-30 03:48:49 +0200922 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
Vlad Dogarud5c94562014-10-21 11:09:58 +0300923 if (!indio_dev)
924 return -ENOMEM;
925
Vlad Dogarud5c94562014-10-21 11:09:58 +0300926 data = iio_priv(indio_dev);
927 mutex_init(&data->lock);
Linus Walleij14e80152016-06-30 03:48:49 +0200928 data->dev = dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300929
Linus Walleij14e80152016-06-30 03:48:49 +0200930 indio_dev->dev.parent = dev;
931 indio_dev->name = name;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300932 indio_dev->channels = bmp280_channels;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300933 indio_dev->info = &bmp280_info;
934 indio_dev->modes = INDIO_DIRECT_MODE;
935
Linus Walleij14e80152016-06-30 03:48:49 +0200936 switch (chip) {
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900937 case BMP180_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700938 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900939 data->chip_info = &bmp180_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900940 data->oversampling_press = ilog2(8);
941 data->oversampling_temp = ilog2(1);
Linus Walleijbd525e62016-06-30 03:48:48 +0200942 data->start_up_time = 10;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900943 break;
944 case BMP280_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700945 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900946 data->chip_info = &bmp280_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900947 data->oversampling_press = ilog2(16);
948 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200949 data->start_up_time = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900950 break;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700951 case BME280_CHIP_ID:
952 indio_dev->num_channels = 3;
953 data->chip_info = &bme280_chip_info;
954 data->oversampling_press = ilog2(16);
955 data->oversampling_humid = ilog2(16);
956 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200957 data->start_up_time = 2;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700958 break;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900959 default:
960 return -EINVAL;
961 }
962
Linus Walleijbd525e62016-06-30 03:48:48 +0200963 /* Bring up regulators */
Linus Walleij14e80152016-06-30 03:48:49 +0200964 data->vddd = devm_regulator_get(dev, "vddd");
Linus Walleijbd525e62016-06-30 03:48:48 +0200965 if (IS_ERR(data->vddd)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200966 dev_err(dev, "failed to get VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200967 return PTR_ERR(data->vddd);
968 }
969 ret = regulator_enable(data->vddd);
970 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200971 dev_err(dev, "failed to enable VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200972 return ret;
973 }
Linus Walleij14e80152016-06-30 03:48:49 +0200974 data->vdda = devm_regulator_get(dev, "vdda");
Linus Walleijbd525e62016-06-30 03:48:48 +0200975 if (IS_ERR(data->vdda)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200976 dev_err(dev, "failed to get VDDA regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200977 ret = PTR_ERR(data->vddd);
978 goto out_disable_vddd;
979 }
980 ret = regulator_enable(data->vdda);
981 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200982 dev_err(dev, "failed to enable VDDA regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200983 goto out_disable_vddd;
984 }
985 /* Wait to make sure we started up properly */
986 mdelay(data->start_up_time);
987
Linus Walleijc5842b42016-06-30 03:48:47 +0200988 /* Bring chip out of reset if there is an assigned GPIO line */
Linus Walleij14e80152016-06-30 03:48:49 +0200989 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
Linus Walleijc5842b42016-06-30 03:48:47 +0200990 /* Deassert the signal */
991 if (!IS_ERR(gpiod)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200992 dev_info(dev, "release reset\n");
Linus Walleijc5842b42016-06-30 03:48:47 +0200993 gpiod_set_value(gpiod, 0);
994 }
995
Linus Walleij14e80152016-06-30 03:48:49 +0200996 data->regmap = regmap;
997 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300998 if (ret < 0)
Linus Walleij14e80152016-06-30 03:48:49 +0200999 goto out_disable_vdda;
1000 if (chip_id != chip) {
1001 dev_err(dev, "bad chip id: expected %x got %x\n",
1002 chip, chip_id);
Linus Walleijbd525e62016-06-30 03:48:48 +02001003 ret = -EINVAL;
1004 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001005 }
1006
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001007 ret = data->chip_info->chip_config(data);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001008 if (ret < 0)
Linus Walleijbd525e62016-06-30 03:48:48 +02001009 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001010
Linus Walleij14e80152016-06-30 03:48:49 +02001011 dev_set_drvdata(dev, indio_dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001012
Linus Walleijaae95392016-06-30 03:48:52 +02001013 /*
1014 * Attempt to grab an optional EOC IRQ - only the BMP085 has this
1015 * however as it happens, the BMP085 shares the chip ID of BMP180
1016 * so we look for an IRQ if we have that.
1017 */
1018 if (irq > 0 || (chip_id == BMP180_CHIP_ID)) {
1019 ret = bmp085_fetch_eoc_irq(dev, name, irq, data);
1020 if (ret)
1021 goto out_disable_vdda;
1022 }
1023
Linus Walleijbd525e62016-06-30 03:48:48 +02001024 ret = iio_device_register(indio_dev);
1025 if (ret)
1026 goto out_disable_vdda;
1027
1028 return 0;
1029
1030out_disable_vdda:
1031 regulator_disable(data->vdda);
1032out_disable_vddd:
1033 regulator_disable(data->vddd);
1034 return ret;
1035}
Linus Walleij17118842016-06-30 03:48:50 +02001036EXPORT_SYMBOL(bmp280_common_probe);
Linus Walleijbd525e62016-06-30 03:48:48 +02001037
Linus Walleij14e80152016-06-30 03:48:49 +02001038int bmp280_common_remove(struct device *dev)
Linus Walleijbd525e62016-06-30 03:48:48 +02001039{
Linus Walleij14e80152016-06-30 03:48:49 +02001040 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Linus Walleijbd525e62016-06-30 03:48:48 +02001041 struct bmp280_data *data = iio_priv(indio_dev);
1042
1043 iio_device_unregister(indio_dev);
1044 regulator_disable(data->vdda);
1045 regulator_disable(data->vddd);
1046 return 0;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001047}
Linus Walleij17118842016-06-30 03:48:50 +02001048EXPORT_SYMBOL(bmp280_common_remove);
1049
1050MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1051MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
1052MODULE_LICENSE("GPL v2");