blob: 83b96fe71f3b077e39d70c7acb8be7e994e4a2cb [file] [log] [blame]
Vlad Dogarud5c94562014-10-21 11:09:58 +03001/*
2 * Copyright (c) 2014 Intel Corporation
3 *
Akinobu Mita6dba72e2016-04-24 22:52:10 +09004 * Driver for Bosch Sensortec BMP180 and BMP280 digital pressure sensor.
Vlad Dogarud5c94562014-10-21 11:09:58 +03005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
Akinobu Mita6dba72e2016-04-24 22:52:10 +090010 * Datasheet:
11 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP180-DS000-121.pdf
12 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMP280-DS001-12.pdf
Matt Ranostay14beaa82016-05-04 22:57:30 -070013 * https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BME280_DS001-11.pdf
Vlad Dogarud5c94562014-10-21 11:09:58 +030014 */
15
16#define pr_fmt(fmt) "bmp280: " fmt
17
Linus Walleij14e80152016-06-30 03:48:49 +020018#include <linux/device.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030019#include <linux/regmap.h>
Akinobu Mita6dba72e2016-04-24 22:52:10 +090020#include <linux/delay.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030021#include <linux/iio/iio.h>
22#include <linux/iio/sysfs.h>
Linus Walleijc5842b42016-06-30 03:48:47 +020023#include <linux/gpio/consumer.h>
Linus Walleijbd525e62016-06-30 03:48:48 +020024#include <linux/regulator/consumer.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030025
Linus Walleij14e80152016-06-30 03:48:49 +020026#include "bmp280.h"
Vlad Dogarud5c94562014-10-21 11:09:58 +030027
28struct bmp280_data {
Linus Walleij14e80152016-06-30 03:48:49 +020029 struct device *dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +030030 struct mutex lock;
31 struct regmap *regmap;
Akinobu Mita6dba72e2016-04-24 22:52:10 +090032 const struct bmp280_chip_info *chip_info;
Linus Walleijbd525e62016-06-30 03:48:48 +020033 struct regulator *vddd;
34 struct regulator *vdda;
35 unsigned int start_up_time; /* in milliseconds */
Vlad Dogarud5c94562014-10-21 11:09:58 +030036
Akinobu Mita62979902016-04-24 22:52:11 +090037 /* log of base 2 of oversampling rate */
38 u8 oversampling_press;
39 u8 oversampling_temp;
Matt Ranostay14beaa82016-05-04 22:57:30 -070040 u8 oversampling_humid;
Akinobu Mita62979902016-04-24 22:52:11 +090041
Vlad Dogarud5c94562014-10-21 11:09:58 +030042 /*
43 * Carryover value from temperature conversion, used in pressure
44 * calculation.
45 */
46 s32 t_fine;
47};
48
Akinobu Mita6dba72e2016-04-24 22:52:10 +090049struct bmp280_chip_info {
Akinobu Mita62979902016-04-24 22:52:11 +090050 const int *oversampling_temp_avail;
51 int num_oversampling_temp_avail;
52
53 const int *oversampling_press_avail;
54 int num_oversampling_press_avail;
55
Matt Ranostay14beaa82016-05-04 22:57:30 -070056 const int *oversampling_humid_avail;
57 int num_oversampling_humid_avail;
58
Akinobu Mita6dba72e2016-04-24 22:52:10 +090059 int (*chip_config)(struct bmp280_data *);
60 int (*read_temp)(struct bmp280_data *, int *);
61 int (*read_press)(struct bmp280_data *, int *, int *);
Matt Ranostay14beaa82016-05-04 22:57:30 -070062 int (*read_humid)(struct bmp280_data *, int *, int *);
Akinobu Mita6dba72e2016-04-24 22:52:10 +090063};
64
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020065/*
66 * These enums are used for indexing into the array of compensation
Akinobu Mita6dba72e2016-04-24 22:52:10 +090067 * parameters for BMP280.
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020068 */
69enum { T1, T2, T3 };
70enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +030071
72static const struct iio_chan_spec bmp280_channels[] = {
73 {
74 .type = IIO_PRESSURE,
Akinobu Mita62979902016-04-24 22:52:11 +090075 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
76 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +030077 },
78 {
79 .type = IIO_TEMP,
Akinobu Mita62979902016-04-24 22:52:11 +090080 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
81 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +030082 },
Matt Ranostay14beaa82016-05-04 22:57:30 -070083 {
84 .type = IIO_HUMIDITYRELATIVE,
85 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
86 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
87 },
Vlad Dogarud5c94562014-10-21 11:09:58 +030088};
89
Vlad Dogaru0f8994b2014-11-20 14:00:48 +020090/*
Matt Ranostay14beaa82016-05-04 22:57:30 -070091 * Returns humidity in percent, resolution is 0.01 percent. Output value of
92 * "47445" represents 47445/1024 = 46.333 %RH.
93 *
94 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
95 */
96
97static u32 bmp280_compensate_humidity(struct bmp280_data *data,
98 s32 adc_humidity)
99{
Linus Walleij14e80152016-06-30 03:48:49 +0200100 struct device *dev = data->dev;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700101 unsigned int H1, H3, tmp;
102 int H2, H4, H5, H6, ret, var;
103
104 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
105 if (ret < 0) {
106 dev_err(dev, "failed to read H1 comp value\n");
107 return ret;
108 }
109
110 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
111 if (ret < 0) {
112 dev_err(dev, "failed to read H2 comp value\n");
113 return ret;
114 }
115 H2 = sign_extend32(le16_to_cpu(tmp), 15);
116
117 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
118 if (ret < 0) {
119 dev_err(dev, "failed to read H3 comp value\n");
120 return ret;
121 }
122
123 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
124 if (ret < 0) {
125 dev_err(dev, "failed to read H4 comp value\n");
126 return ret;
127 }
128 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
129 (be16_to_cpu(tmp) & 0xf), 11);
130
131 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
132 if (ret < 0) {
133 dev_err(dev, "failed to read H5 comp value\n");
134 return ret;
135 }
136 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
137
138 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
139 if (ret < 0) {
140 dev_err(dev, "failed to read H6 comp value\n");
141 return ret;
142 }
143 H6 = sign_extend32(tmp, 7);
144
145 var = ((s32)data->t_fine) - 76800;
146 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
147 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
148 + 2097152) * H2 + 8192) >> 14);
149 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
150
151 return var >> 12;
152};
153
154/*
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200155 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
156 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
157 * value.
158 *
159 * Taken from datasheet, Section 3.11.3, "Compensation formula".
160 */
161static s32 bmp280_compensate_temp(struct bmp280_data *data,
162 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300163{
164 int ret;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100165 s32 var1, var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300166 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
167
168 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
169 buf, BMP280_COMP_TEMP_REG_COUNT);
170 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200171 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300172 "failed to read temperature calibration parameters\n");
173 return ret;
174 }
175
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200176 /*
177 * The double casts are necessary because le16_to_cpu returns an
178 * unsigned 16-bit value. Casting that value directly to a
179 * signed 32-bit will not do proper sign extension.
180 *
181 * Conversely, T1 and P1 are unsigned values, so they can be
182 * cast straight to the larger type.
183 */
184 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
185 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
186 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
187 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
188 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
Irina Tirdeaabad3982015-04-08 18:26:12 +0300189 data->t_fine = var1 + var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300190
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100191 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300192}
193
194/*
195 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
196 * integer bits and 8 fractional bits). Output value of "24674867"
197 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
198 *
199 * Taken from datasheet, Section 3.11.3, "Compensation formula".
200 */
201static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300202 s32 adc_press)
203{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200204 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300205 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200206 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300207
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200208 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
209 buf, BMP280_COMP_PRESS_REG_COUNT);
210 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200211 dev_err(data->dev,
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200212 "failed to read pressure calibration parameters\n");
213 return ret;
214 }
215
216 var1 = ((s64)data->t_fine) - 128000;
217 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100218 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
219 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200220 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
221 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
222 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300223
224 if (var1 == 0)
225 return 0;
226
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200227 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100228 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200229 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
230 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
231 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300232
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100233 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300234}
235
236static int bmp280_read_temp(struct bmp280_data *data,
237 int *val)
238{
239 int ret;
240 __be32 tmp = 0;
241 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300242
243 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
244 (u8 *) &tmp, 3);
245 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200246 dev_err(data->dev, "failed to read temperature\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300247 return ret;
248 }
249
250 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200251 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300252
253 /*
254 * val might be NULL if we're called by the read_press routine,
255 * who only cares about the carry over t_fine value.
256 */
257 if (val) {
258 *val = comp_temp * 10;
259 return IIO_VAL_INT;
260 }
261
262 return 0;
263}
264
265static int bmp280_read_press(struct bmp280_data *data,
266 int *val, int *val2)
267{
268 int ret;
269 __be32 tmp = 0;
270 s32 adc_press;
271 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300272
273 /* Read and compensate temperature so we get a reading of t_fine. */
274 ret = bmp280_read_temp(data, NULL);
275 if (ret < 0)
276 return ret;
277
278 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
279 (u8 *) &tmp, 3);
280 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200281 dev_err(data->dev, "failed to read pressure\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300282 return ret;
283 }
284
285 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200286 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300287
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000288 *val = comp_press;
289 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300290
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000291 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300292}
293
Matt Ranostay14beaa82016-05-04 22:57:30 -0700294static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
295{
296 int ret;
297 __be16 tmp = 0;
298 s32 adc_humidity;
299 u32 comp_humidity;
300
301 /* Read and compensate temperature so we get a reading of t_fine. */
302 ret = bmp280_read_temp(data, NULL);
303 if (ret < 0)
304 return ret;
305
306 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
307 (u8 *) &tmp, 2);
308 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200309 dev_err(data->dev, "failed to read humidity\n");
Matt Ranostay14beaa82016-05-04 22:57:30 -0700310 return ret;
311 }
312
313 adc_humidity = be16_to_cpu(tmp);
314 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
315
316 *val = comp_humidity;
317 *val2 = 1024;
318
319 return IIO_VAL_FRACTIONAL;
320}
321
Vlad Dogarud5c94562014-10-21 11:09:58 +0300322static int bmp280_read_raw(struct iio_dev *indio_dev,
323 struct iio_chan_spec const *chan,
324 int *val, int *val2, long mask)
325{
326 int ret;
327 struct bmp280_data *data = iio_priv(indio_dev);
328
329 mutex_lock(&data->lock);
330
331 switch (mask) {
332 case IIO_CHAN_INFO_PROCESSED:
333 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700334 case IIO_HUMIDITYRELATIVE:
335 ret = data->chip_info->read_humid(data, val, val2);
336 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300337 case IIO_PRESSURE:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900338 ret = data->chip_info->read_press(data, val, val2);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300339 break;
340 case IIO_TEMP:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900341 ret = data->chip_info->read_temp(data, val);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300342 break;
343 default:
344 ret = -EINVAL;
345 break;
346 }
347 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900348 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
349 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700350 case IIO_HUMIDITYRELATIVE:
351 *val = 1 << data->oversampling_humid;
352 ret = IIO_VAL_INT;
353 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900354 case IIO_PRESSURE:
355 *val = 1 << data->oversampling_press;
356 ret = IIO_VAL_INT;
357 break;
358 case IIO_TEMP:
359 *val = 1 << data->oversampling_temp;
360 ret = IIO_VAL_INT;
361 break;
362 default:
363 ret = -EINVAL;
364 break;
365 }
366 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300367 default:
368 ret = -EINVAL;
369 break;
370 }
371
372 mutex_unlock(&data->lock);
373
374 return ret;
375}
376
Matt Ranostay14beaa82016-05-04 22:57:30 -0700377static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
378 int val)
379{
380 int i;
381 const int *avail = data->chip_info->oversampling_humid_avail;
382 const int n = data->chip_info->num_oversampling_humid_avail;
383
384 for (i = 0; i < n; i++) {
385 if (avail[i] == val) {
386 data->oversampling_humid = ilog2(val);
387
388 return data->chip_info->chip_config(data);
389 }
390 }
391 return -EINVAL;
392}
393
Akinobu Mita62979902016-04-24 22:52:11 +0900394static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
395 int val)
396{
397 int i;
398 const int *avail = data->chip_info->oversampling_temp_avail;
399 const int n = data->chip_info->num_oversampling_temp_avail;
400
401 for (i = 0; i < n; i++) {
402 if (avail[i] == val) {
403 data->oversampling_temp = ilog2(val);
404
405 return data->chip_info->chip_config(data);
406 }
407 }
408 return -EINVAL;
409}
410
411static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
412 int val)
413{
414 int i;
415 const int *avail = data->chip_info->oversampling_press_avail;
416 const int n = data->chip_info->num_oversampling_press_avail;
417
418 for (i = 0; i < n; i++) {
419 if (avail[i] == val) {
420 data->oversampling_press = ilog2(val);
421
422 return data->chip_info->chip_config(data);
423 }
424 }
425 return -EINVAL;
426}
427
428static int bmp280_write_raw(struct iio_dev *indio_dev,
429 struct iio_chan_spec const *chan,
430 int val, int val2, long mask)
431{
432 int ret = 0;
433 struct bmp280_data *data = iio_priv(indio_dev);
434
435 switch (mask) {
436 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
437 mutex_lock(&data->lock);
438 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700439 case IIO_HUMIDITYRELATIVE:
440 ret = bmp280_write_oversampling_ratio_humid(data, val);
441 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900442 case IIO_PRESSURE:
443 ret = bmp280_write_oversampling_ratio_press(data, val);
444 break;
445 case IIO_TEMP:
446 ret = bmp280_write_oversampling_ratio_temp(data, val);
447 break;
448 default:
449 ret = -EINVAL;
450 break;
451 }
452 mutex_unlock(&data->lock);
453 break;
454 default:
455 return -EINVAL;
456 }
457
458 return ret;
459}
460
461static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
462{
463 size_t len = 0;
464 int i;
465
466 for (i = 0; i < n; i++)
467 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
468
469 buf[len - 1] = '\n';
470
471 return len;
472}
473
474static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
475 struct device_attribute *attr, char *buf)
476{
477 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
478
479 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
480 data->chip_info->num_oversampling_temp_avail);
481}
482
483static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
484 struct device_attribute *attr, char *buf)
485{
486 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
487
488 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
489 data->chip_info->num_oversampling_press_avail);
490}
491
492static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
493 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
494
495static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
496 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
497
498static struct attribute *bmp280_attributes[] = {
499 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
500 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
501 NULL,
502};
503
504static const struct attribute_group bmp280_attrs_group = {
505 .attrs = bmp280_attributes,
506};
507
Vlad Dogarud5c94562014-10-21 11:09:58 +0300508static const struct iio_info bmp280_info = {
509 .driver_module = THIS_MODULE,
510 .read_raw = &bmp280_read_raw,
Akinobu Mita62979902016-04-24 22:52:11 +0900511 .write_raw = &bmp280_write_raw,
512 .attrs = &bmp280_attrs_group,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300513};
514
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900515static int bmp280_chip_config(struct bmp280_data *data)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300516{
517 int ret;
Akinobu Mita62979902016-04-24 22:52:11 +0900518 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
519 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300520
521 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
522 BMP280_OSRS_TEMP_MASK |
523 BMP280_OSRS_PRESS_MASK |
524 BMP280_MODE_MASK,
Akinobu Mita62979902016-04-24 22:52:11 +0900525 osrs | BMP280_MODE_NORMAL);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300526 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200527 dev_err(data->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100528 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300529 return ret;
530 }
531
532 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
533 BMP280_FILTER_MASK,
534 BMP280_FILTER_4X);
535 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200536 dev_err(data->dev,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300537 "failed to write config register\n");
538 return ret;
539 }
540
541 return ret;
542}
543
Akinobu Mita62979902016-04-24 22:52:11 +0900544static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
545
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900546static const struct bmp280_chip_info bmp280_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900547 .oversampling_temp_avail = bmp280_oversampling_avail,
548 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
549
550 .oversampling_press_avail = bmp280_oversampling_avail,
551 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
552
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900553 .chip_config = bmp280_chip_config,
554 .read_temp = bmp280_read_temp,
555 .read_press = bmp280_read_press,
556};
557
Matt Ranostay14beaa82016-05-04 22:57:30 -0700558static int bme280_chip_config(struct bmp280_data *data)
559{
560 int ret = bmp280_chip_config(data);
561 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
562
563 if (ret < 0)
564 return ret;
565
566 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
567 BMP280_OSRS_HUMIDITY_MASK, osrs);
568}
569
570static const struct bmp280_chip_info bme280_chip_info = {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700571 .oversampling_temp_avail = bmp280_oversampling_avail,
572 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
573
574 .oversampling_press_avail = bmp280_oversampling_avail,
575 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
576
577 .oversampling_humid_avail = bmp280_oversampling_avail,
578 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
579
580 .chip_config = bme280_chip_config,
581 .read_temp = bmp280_read_temp,
582 .read_press = bmp280_read_press,
583 .read_humid = bmp280_read_humid,
584};
585
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900586static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
587{
588 int ret;
589 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
590 unsigned int delay_us;
591 unsigned int ctrl;
592
593 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
594 if (ret)
595 return ret;
596
597 if (ctrl_meas == BMP180_MEAS_TEMP)
598 delay_us = 4500;
599 else
Akinobu Mita62979902016-04-24 22:52:11 +0900600 delay_us = conversion_time_max[data->oversampling_press];
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900601
602 usleep_range(delay_us, delay_us + 1000);
603
604 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
605 if (ret)
606 return ret;
607
608 /* The value of this bit reset to "0" after conversion is complete */
609 if (ctrl & BMP180_MEAS_SCO)
610 return -EIO;
611
612 return 0;
613}
614
615static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
616{
617 int ret;
618 __be16 tmp = 0;
619
620 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
621 if (ret)
622 return ret;
623
624 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
625 if (ret)
626 return ret;
627
628 *val = be16_to_cpu(tmp);
629
630 return 0;
631}
632
633/*
634 * These enums are used for indexing into the array of calibration
635 * coefficients for BMP180.
636 */
637enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
638
639struct bmp180_calib {
640 s16 AC1;
641 s16 AC2;
642 s16 AC3;
643 u16 AC4;
644 u16 AC5;
645 u16 AC6;
646 s16 B1;
647 s16 B2;
648 s16 MB;
649 s16 MC;
650 s16 MD;
651};
652
653static int bmp180_read_calib(struct bmp280_data *data,
654 struct bmp180_calib *calib)
655{
656 int ret;
657 int i;
658 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
659
660 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
661 sizeof(buf));
662
663 if (ret < 0)
664 return ret;
665
666 /* None of the words has the value 0 or 0xFFFF */
667 for (i = 0; i < ARRAY_SIZE(buf); i++) {
668 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
669 return -EIO;
670 }
671
672 calib->AC1 = be16_to_cpu(buf[AC1]);
673 calib->AC2 = be16_to_cpu(buf[AC2]);
674 calib->AC3 = be16_to_cpu(buf[AC3]);
675 calib->AC4 = be16_to_cpu(buf[AC4]);
676 calib->AC5 = be16_to_cpu(buf[AC5]);
677 calib->AC6 = be16_to_cpu(buf[AC6]);
678 calib->B1 = be16_to_cpu(buf[B1]);
679 calib->B2 = be16_to_cpu(buf[B2]);
680 calib->MB = be16_to_cpu(buf[MB]);
681 calib->MC = be16_to_cpu(buf[MC]);
682 calib->MD = be16_to_cpu(buf[MD]);
683
684 return 0;
685}
686
687/*
688 * Returns temperature in DegC, resolution is 0.1 DegC.
689 * t_fine carries fine temperature as global value.
690 *
691 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
692 */
693static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
694{
695 int ret;
696 s32 x1, x2;
697 struct bmp180_calib calib;
698
699 ret = bmp180_read_calib(data, &calib);
700 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200701 dev_err(data->dev,
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900702 "failed to read calibration coefficients\n");
703 return ret;
704 }
705
706 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
707 x2 = (calib.MC << 11) / (x1 + calib.MD);
708 data->t_fine = x1 + x2;
709
710 return (data->t_fine + 8) >> 4;
711}
712
713static int bmp180_read_temp(struct bmp280_data *data, int *val)
714{
715 int ret;
716 s32 adc_temp, comp_temp;
717
718 ret = bmp180_read_adc_temp(data, &adc_temp);
719 if (ret)
720 return ret;
721
722 comp_temp = bmp180_compensate_temp(data, adc_temp);
723
724 /*
725 * val might be NULL if we're called by the read_press routine,
726 * who only cares about the carry over t_fine value.
727 */
728 if (val) {
729 *val = comp_temp * 100;
730 return IIO_VAL_INT;
731 }
732
733 return 0;
734}
735
736static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
737{
738 int ret;
739 __be32 tmp = 0;
Akinobu Mita62979902016-04-24 22:52:11 +0900740 u8 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900741
742 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
743 if (ret)
744 return ret;
745
746 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
747 if (ret)
748 return ret;
749
750 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
751
752 return 0;
753}
754
755/*
756 * Returns pressure in Pa, resolution is 1 Pa.
757 *
758 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
759 */
760static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
761{
762 int ret;
763 s32 x1, x2, x3, p;
764 s32 b3, b6;
765 u32 b4, b7;
Akinobu Mita62979902016-04-24 22:52:11 +0900766 s32 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900767 struct bmp180_calib calib;
768
769 ret = bmp180_read_calib(data, &calib);
770 if (ret < 0) {
Linus Walleij14e80152016-06-30 03:48:49 +0200771 dev_err(data->dev,
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900772 "failed to read calibration coefficients\n");
773 return ret;
774 }
775
776 b6 = data->t_fine - 4000;
777 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
778 x2 = calib.AC2 * b6 >> 11;
779 x3 = x1 + x2;
780 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
781 x1 = calib.AC3 * b6 >> 13;
782 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
783 x3 = (x1 + x2 + 2) >> 2;
784 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
785 b7 = ((u32)adc_press - b3) * (50000 >> oss);
786 if (b7 < 0x80000000)
787 p = (b7 * 2) / b4;
788 else
789 p = (b7 / b4) * 2;
790
791 x1 = (p >> 8) * (p >> 8);
792 x1 = (x1 * 3038) >> 16;
793 x2 = (-7357 * p) >> 16;
794
795 return p + ((x1 + x2 + 3791) >> 4);
796}
797
798static int bmp180_read_press(struct bmp280_data *data,
799 int *val, int *val2)
800{
801 int ret;
802 s32 adc_press;
803 u32 comp_press;
804
805 /* Read and compensate temperature so we get a reading of t_fine. */
806 ret = bmp180_read_temp(data, NULL);
807 if (ret)
808 return ret;
809
810 ret = bmp180_read_adc_press(data, &adc_press);
811 if (ret)
812 return ret;
813
814 comp_press = bmp180_compensate_press(data, adc_press);
815
816 *val = comp_press;
817 *val2 = 1000;
818
819 return IIO_VAL_FRACTIONAL;
820}
821
822static int bmp180_chip_config(struct bmp280_data *data)
823{
824 return 0;
825}
826
Akinobu Mita62979902016-04-24 22:52:11 +0900827static const int bmp180_oversampling_temp_avail[] = { 1 };
828static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
829
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900830static const struct bmp280_chip_info bmp180_chip_info = {
Akinobu Mita62979902016-04-24 22:52:11 +0900831 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
832 .num_oversampling_temp_avail =
833 ARRAY_SIZE(bmp180_oversampling_temp_avail),
834
835 .oversampling_press_avail = bmp180_oversampling_press_avail,
836 .num_oversampling_press_avail =
837 ARRAY_SIZE(bmp180_oversampling_press_avail),
838
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900839 .chip_config = bmp180_chip_config,
840 .read_temp = bmp180_read_temp,
841 .read_press = bmp180_read_press,
842};
843
Linus Walleij14e80152016-06-30 03:48:49 +0200844int bmp280_common_probe(struct device *dev,
845 struct regmap *regmap,
846 unsigned int chip,
847 const char *name)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300848{
849 int ret;
850 struct iio_dev *indio_dev;
851 struct bmp280_data *data;
852 unsigned int chip_id;
Linus Walleijc5842b42016-06-30 03:48:47 +0200853 struct gpio_desc *gpiod;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300854
Linus Walleij14e80152016-06-30 03:48:49 +0200855 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
Vlad Dogarud5c94562014-10-21 11:09:58 +0300856 if (!indio_dev)
857 return -ENOMEM;
858
Vlad Dogarud5c94562014-10-21 11:09:58 +0300859 data = iio_priv(indio_dev);
860 mutex_init(&data->lock);
Linus Walleij14e80152016-06-30 03:48:49 +0200861 data->dev = dev;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300862
Linus Walleij14e80152016-06-30 03:48:49 +0200863 indio_dev->dev.parent = dev;
864 indio_dev->name = name;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300865 indio_dev->channels = bmp280_channels;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300866 indio_dev->info = &bmp280_info;
867 indio_dev->modes = INDIO_DIRECT_MODE;
868
Linus Walleij14e80152016-06-30 03:48:49 +0200869 switch (chip) {
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900870 case BMP180_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700871 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900872 data->chip_info = &bmp180_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900873 data->oversampling_press = ilog2(8);
874 data->oversampling_temp = ilog2(1);
Linus Walleijbd525e62016-06-30 03:48:48 +0200875 data->start_up_time = 10;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900876 break;
877 case BMP280_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700878 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900879 data->chip_info = &bmp280_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +0900880 data->oversampling_press = ilog2(16);
881 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200882 data->start_up_time = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900883 break;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700884 case BME280_CHIP_ID:
885 indio_dev->num_channels = 3;
886 data->chip_info = &bme280_chip_info;
887 data->oversampling_press = ilog2(16);
888 data->oversampling_humid = ilog2(16);
889 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +0200890 data->start_up_time = 2;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700891 break;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900892 default:
893 return -EINVAL;
894 }
895
Linus Walleijbd525e62016-06-30 03:48:48 +0200896 /* Bring up regulators */
Linus Walleij14e80152016-06-30 03:48:49 +0200897 data->vddd = devm_regulator_get(dev, "vddd");
Linus Walleijbd525e62016-06-30 03:48:48 +0200898 if (IS_ERR(data->vddd)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200899 dev_err(dev, "failed to get VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200900 return PTR_ERR(data->vddd);
901 }
902 ret = regulator_enable(data->vddd);
903 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200904 dev_err(dev, "failed to enable VDDD regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200905 return ret;
906 }
Linus Walleij14e80152016-06-30 03:48:49 +0200907 data->vdda = devm_regulator_get(dev, "vdda");
Linus Walleijbd525e62016-06-30 03:48:48 +0200908 if (IS_ERR(data->vdda)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200909 dev_err(dev, "failed to get VDDA regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200910 ret = PTR_ERR(data->vddd);
911 goto out_disable_vddd;
912 }
913 ret = regulator_enable(data->vdda);
914 if (ret) {
Linus Walleij14e80152016-06-30 03:48:49 +0200915 dev_err(dev, "failed to enable VDDA regulator\n");
Linus Walleijbd525e62016-06-30 03:48:48 +0200916 goto out_disable_vddd;
917 }
918 /* Wait to make sure we started up properly */
919 mdelay(data->start_up_time);
920
Linus Walleijc5842b42016-06-30 03:48:47 +0200921 /* Bring chip out of reset if there is an assigned GPIO line */
Linus Walleij14e80152016-06-30 03:48:49 +0200922 gpiod = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
Linus Walleijc5842b42016-06-30 03:48:47 +0200923 /* Deassert the signal */
924 if (!IS_ERR(gpiod)) {
Linus Walleij14e80152016-06-30 03:48:49 +0200925 dev_info(dev, "release reset\n");
Linus Walleijc5842b42016-06-30 03:48:47 +0200926 gpiod_set_value(gpiod, 0);
927 }
928
Linus Walleij14e80152016-06-30 03:48:49 +0200929 data->regmap = regmap;
930 ret = regmap_read(regmap, BMP280_REG_ID, &chip_id);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300931 if (ret < 0)
Linus Walleij14e80152016-06-30 03:48:49 +0200932 goto out_disable_vdda;
933 if (chip_id != chip) {
934 dev_err(dev, "bad chip id: expected %x got %x\n",
935 chip, chip_id);
Linus Walleijbd525e62016-06-30 03:48:48 +0200936 ret = -EINVAL;
937 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300938 }
939
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900940 ret = data->chip_info->chip_config(data);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300941 if (ret < 0)
Linus Walleijbd525e62016-06-30 03:48:48 +0200942 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300943
Linus Walleij14e80152016-06-30 03:48:49 +0200944 dev_set_drvdata(dev, indio_dev);
Linus Walleijbd525e62016-06-30 03:48:48 +0200945
946 ret = iio_device_register(indio_dev);
947 if (ret)
948 goto out_disable_vdda;
949
950 return 0;
951
952out_disable_vdda:
953 regulator_disable(data->vdda);
954out_disable_vddd:
955 regulator_disable(data->vddd);
956 return ret;
957}
958
Linus Walleij14e80152016-06-30 03:48:49 +0200959int bmp280_common_remove(struct device *dev)
Linus Walleijbd525e62016-06-30 03:48:48 +0200960{
Linus Walleij14e80152016-06-30 03:48:49 +0200961 struct iio_dev *indio_dev = dev_get_drvdata(dev);
Linus Walleijbd525e62016-06-30 03:48:48 +0200962 struct bmp280_data *data = iio_priv(indio_dev);
963
964 iio_device_unregister(indio_dev);
965 regulator_disable(data->vdda);
966 regulator_disable(data->vddd);
967 return 0;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300968}