blob: f5d0875d88e90eb6ffc3037fded040a6435d7934 [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
18#include <linux/module.h>
19#include <linux/i2c.h>
20#include <linux/acpi.h>
Linus Walleij78f50272016-06-30 03:48:46 +020021#include <linux/of.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030022#include <linux/regmap.h>
Akinobu Mita6dba72e2016-04-24 22:52:10 +090023#include <linux/delay.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030024#include <linux/iio/iio.h>
25#include <linux/iio/sysfs.h>
Linus Walleijc5842b42016-06-30 03:48:47 +020026#include <linux/gpio/consumer.h>
Linus Walleijbd525e62016-06-30 03:48:48 +020027#include <linux/regulator/consumer.h>
Vlad Dogarud5c94562014-10-21 11:09:58 +030028
Akinobu Mita6dba72e2016-04-24 22:52:10 +090029/* BMP280 specific registers */
Matt Ranostay14beaa82016-05-04 22:57:30 -070030#define BMP280_REG_HUMIDITY_LSB 0xFE
31#define BMP280_REG_HUMIDITY_MSB 0xFD
Vlad Dogarud5c94562014-10-21 11:09:58 +030032#define BMP280_REG_TEMP_XLSB 0xFC
33#define BMP280_REG_TEMP_LSB 0xFB
34#define BMP280_REG_TEMP_MSB 0xFA
35#define BMP280_REG_PRESS_XLSB 0xF9
36#define BMP280_REG_PRESS_LSB 0xF8
37#define BMP280_REG_PRESS_MSB 0xF7
38
39#define BMP280_REG_CONFIG 0xF5
Matt Ranostay14beaa82016-05-04 22:57:30 -070040#define BMP280_REG_CTRL_MEAS 0xF4
Vlad Dogarud5c94562014-10-21 11:09:58 +030041#define BMP280_REG_STATUS 0xF3
Matt Ranostay14beaa82016-05-04 22:57:30 -070042#define BMP280_REG_CTRL_HUMIDITY 0xF2
43
44/* Due to non linear mapping, and data sizes we can't do a bulk read */
45#define BMP280_REG_COMP_H1 0xA1
46#define BMP280_REG_COMP_H2 0xE1
47#define BMP280_REG_COMP_H3 0xE3
48#define BMP280_REG_COMP_H4 0xE4
49#define BMP280_REG_COMP_H5 0xE5
50#define BMP280_REG_COMP_H6 0xE7
Vlad Dogarud5c94562014-10-21 11:09:58 +030051
52#define BMP280_REG_COMP_TEMP_START 0x88
53#define BMP280_COMP_TEMP_REG_COUNT 6
54
55#define BMP280_REG_COMP_PRESS_START 0x8E
56#define BMP280_COMP_PRESS_REG_COUNT 18
57
58#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
59#define BMP280_FILTER_OFF 0
60#define BMP280_FILTER_2X BIT(2)
61#define BMP280_FILTER_4X BIT(3)
62#define BMP280_FILTER_8X (BIT(3) | BIT(2))
63#define BMP280_FILTER_16X BIT(4)
64
Matt Ranostay14beaa82016-05-04 22:57:30 -070065#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
66#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
67#define BMP280_OSRS_HUMIDITY_SKIP 0
68#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
69#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
70#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
71#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
72#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
73
Vlad Dogarud5c94562014-10-21 11:09:58 +030074#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
75#define BMP280_OSRS_TEMP_SKIP 0
Akinobu Mita62979902016-04-24 22:52:11 +090076#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
77#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
78#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
79#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
80#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
81#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
Vlad Dogarud5c94562014-10-21 11:09:58 +030082
83#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
84#define BMP280_OSRS_PRESS_SKIP 0
Akinobu Mita62979902016-04-24 22:52:11 +090085#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
86#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
87#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
88#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
89#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
90#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
Vlad Dogarud5c94562014-10-21 11:09:58 +030091
92#define BMP280_MODE_MASK (BIT(1) | BIT(0))
93#define BMP280_MODE_SLEEP 0
94#define BMP280_MODE_FORCED BIT(0)
95#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
96
Akinobu Mita6dba72e2016-04-24 22:52:10 +090097/* BMP180 specific registers */
98#define BMP180_REG_OUT_XLSB 0xF8
99#define BMP180_REG_OUT_LSB 0xF7
100#define BMP180_REG_OUT_MSB 0xF6
101
102#define BMP180_REG_CALIB_START 0xAA
103#define BMP180_REG_CALIB_COUNT 22
104
105#define BMP180_MEAS_SCO BIT(5)
106#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
107#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
108#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
109#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
110#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
111#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
112
113/* BMP180 and BMP280 common registers */
114#define BMP280_REG_CTRL_MEAS 0xF4
115#define BMP280_REG_RESET 0xE0
116#define BMP280_REG_ID 0xD0
117
118#define BMP180_CHIP_ID 0x55
Vlad Dogarud5c94562014-10-21 11:09:58 +0300119#define BMP280_CHIP_ID 0x58
Matt Ranostay14beaa82016-05-04 22:57:30 -0700120#define BME280_CHIP_ID 0x60
Vlad Dogarud5c94562014-10-21 11:09:58 +0300121#define BMP280_SOFT_RESET_VAL 0xB6
122
123struct bmp280_data {
124 struct i2c_client *client;
125 struct mutex lock;
126 struct regmap *regmap;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900127 const struct bmp280_chip_info *chip_info;
Linus Walleijbd525e62016-06-30 03:48:48 +0200128 struct regulator *vddd;
129 struct regulator *vdda;
130 unsigned int start_up_time; /* in milliseconds */
Vlad Dogarud5c94562014-10-21 11:09:58 +0300131
Akinobu Mita62979902016-04-24 22:52:11 +0900132 /* log of base 2 of oversampling rate */
133 u8 oversampling_press;
134 u8 oversampling_temp;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700135 u8 oversampling_humid;
Akinobu Mita62979902016-04-24 22:52:11 +0900136
Vlad Dogarud5c94562014-10-21 11:09:58 +0300137 /*
138 * Carryover value from temperature conversion, used in pressure
139 * calculation.
140 */
141 s32 t_fine;
142};
143
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900144struct bmp280_chip_info {
145 const struct regmap_config *regmap_config;
146
Akinobu Mita62979902016-04-24 22:52:11 +0900147 const int *oversampling_temp_avail;
148 int num_oversampling_temp_avail;
149
150 const int *oversampling_press_avail;
151 int num_oversampling_press_avail;
152
Matt Ranostay14beaa82016-05-04 22:57:30 -0700153 const int *oversampling_humid_avail;
154 int num_oversampling_humid_avail;
155
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900156 int (*chip_config)(struct bmp280_data *);
157 int (*read_temp)(struct bmp280_data *, int *);
158 int (*read_press)(struct bmp280_data *, int *, int *);
Matt Ranostay14beaa82016-05-04 22:57:30 -0700159 int (*read_humid)(struct bmp280_data *, int *, int *);
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900160};
161
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200162/*
163 * These enums are used for indexing into the array of compensation
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900164 * parameters for BMP280.
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200165 */
166enum { T1, T2, T3 };
167enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +0300168
169static const struct iio_chan_spec bmp280_channels[] = {
170 {
171 .type = IIO_PRESSURE,
Akinobu Mita62979902016-04-24 22:52:11 +0900172 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
173 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300174 },
175 {
176 .type = IIO_TEMP,
Akinobu Mita62979902016-04-24 22:52:11 +0900177 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
178 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300179 },
Matt Ranostay14beaa82016-05-04 22:57:30 -0700180 {
181 .type = IIO_HUMIDITYRELATIVE,
182 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
183 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
184 },
Vlad Dogarud5c94562014-10-21 11:09:58 +0300185};
186
187static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
188{
189 switch (reg) {
190 case BMP280_REG_CONFIG:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700191 case BMP280_REG_CTRL_HUMIDITY:
Vlad Dogarud5c94562014-10-21 11:09:58 +0300192 case BMP280_REG_CTRL_MEAS:
193 case BMP280_REG_RESET:
194 return true;
195 default:
196 return false;
197 };
198}
199
200static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
201{
202 switch (reg) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700203 case BMP280_REG_HUMIDITY_LSB:
204 case BMP280_REG_HUMIDITY_MSB:
Vlad Dogarud5c94562014-10-21 11:09:58 +0300205 case BMP280_REG_TEMP_XLSB:
206 case BMP280_REG_TEMP_LSB:
207 case BMP280_REG_TEMP_MSB:
208 case BMP280_REG_PRESS_XLSB:
209 case BMP280_REG_PRESS_LSB:
210 case BMP280_REG_PRESS_MSB:
211 case BMP280_REG_STATUS:
212 return true;
213 default:
214 return false;
215 }
216}
217
218static const struct regmap_config bmp280_regmap_config = {
219 .reg_bits = 8,
220 .val_bits = 8,
221
Matt Ranostay14beaa82016-05-04 22:57:30 -0700222 .max_register = BMP280_REG_HUMIDITY_LSB,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300223 .cache_type = REGCACHE_RBTREE,
224
225 .writeable_reg = bmp280_is_writeable_reg,
226 .volatile_reg = bmp280_is_volatile_reg,
227};
228
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200229/*
Matt Ranostay14beaa82016-05-04 22:57:30 -0700230 * Returns humidity in percent, resolution is 0.01 percent. Output value of
231 * "47445" represents 47445/1024 = 46.333 %RH.
232 *
233 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
234 */
235
236static u32 bmp280_compensate_humidity(struct bmp280_data *data,
237 s32 adc_humidity)
238{
239 struct device *dev = &data->client->dev;
240 unsigned int H1, H3, tmp;
241 int H2, H4, H5, H6, ret, var;
242
243 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
244 if (ret < 0) {
245 dev_err(dev, "failed to read H1 comp value\n");
246 return ret;
247 }
248
249 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
250 if (ret < 0) {
251 dev_err(dev, "failed to read H2 comp value\n");
252 return ret;
253 }
254 H2 = sign_extend32(le16_to_cpu(tmp), 15);
255
256 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
257 if (ret < 0) {
258 dev_err(dev, "failed to read H3 comp value\n");
259 return ret;
260 }
261
262 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
263 if (ret < 0) {
264 dev_err(dev, "failed to read H4 comp value\n");
265 return ret;
266 }
267 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
268 (be16_to_cpu(tmp) & 0xf), 11);
269
270 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
271 if (ret < 0) {
272 dev_err(dev, "failed to read H5 comp value\n");
273 return ret;
274 }
275 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
276
277 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
278 if (ret < 0) {
279 dev_err(dev, "failed to read H6 comp value\n");
280 return ret;
281 }
282 H6 = sign_extend32(tmp, 7);
283
284 var = ((s32)data->t_fine) - 76800;
285 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
286 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
287 + 2097152) * H2 + 8192) >> 14);
288 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
289
290 return var >> 12;
291};
292
293/*
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200294 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
295 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
296 * value.
297 *
298 * Taken from datasheet, Section 3.11.3, "Compensation formula".
299 */
300static s32 bmp280_compensate_temp(struct bmp280_data *data,
301 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300302{
303 int ret;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100304 s32 var1, var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300305 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
306
307 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
308 buf, BMP280_COMP_TEMP_REG_COUNT);
309 if (ret < 0) {
310 dev_err(&data->client->dev,
311 "failed to read temperature calibration parameters\n");
312 return ret;
313 }
314
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200315 /*
316 * The double casts are necessary because le16_to_cpu returns an
317 * unsigned 16-bit value. Casting that value directly to a
318 * signed 32-bit will not do proper sign extension.
319 *
320 * Conversely, T1 and P1 are unsigned values, so they can be
321 * cast straight to the larger type.
322 */
323 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
324 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
325 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
326 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
327 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
Irina Tirdeaabad3982015-04-08 18:26:12 +0300328 data->t_fine = var1 + var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300329
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100330 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300331}
332
333/*
334 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
335 * integer bits and 8 fractional bits). Output value of "24674867"
336 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
337 *
338 * Taken from datasheet, Section 3.11.3, "Compensation formula".
339 */
340static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300341 s32 adc_press)
342{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200343 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300344 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200345 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300346
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200347 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
348 buf, BMP280_COMP_PRESS_REG_COUNT);
349 if (ret < 0) {
350 dev_err(&data->client->dev,
351 "failed to read pressure calibration parameters\n");
352 return ret;
353 }
354
355 var1 = ((s64)data->t_fine) - 128000;
356 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100357 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
358 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200359 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
360 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
361 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300362
363 if (var1 == 0)
364 return 0;
365
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200366 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100367 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200368 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
369 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
370 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300371
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100372 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300373}
374
375static int bmp280_read_temp(struct bmp280_data *data,
376 int *val)
377{
378 int ret;
379 __be32 tmp = 0;
380 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300381
382 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
383 (u8 *) &tmp, 3);
384 if (ret < 0) {
385 dev_err(&data->client->dev, "failed to read temperature\n");
386 return ret;
387 }
388
389 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200390 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300391
392 /*
393 * val might be NULL if we're called by the read_press routine,
394 * who only cares about the carry over t_fine value.
395 */
396 if (val) {
397 *val = comp_temp * 10;
398 return IIO_VAL_INT;
399 }
400
401 return 0;
402}
403
404static int bmp280_read_press(struct bmp280_data *data,
405 int *val, int *val2)
406{
407 int ret;
408 __be32 tmp = 0;
409 s32 adc_press;
410 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300411
412 /* Read and compensate temperature so we get a reading of t_fine. */
413 ret = bmp280_read_temp(data, NULL);
414 if (ret < 0)
415 return ret;
416
417 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
418 (u8 *) &tmp, 3);
419 if (ret < 0) {
420 dev_err(&data->client->dev, "failed to read pressure\n");
421 return ret;
422 }
423
424 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200425 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300426
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000427 *val = comp_press;
428 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300429
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000430 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300431}
432
Matt Ranostay14beaa82016-05-04 22:57:30 -0700433static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
434{
435 int ret;
436 __be16 tmp = 0;
437 s32 adc_humidity;
438 u32 comp_humidity;
439
440 /* Read and compensate temperature so we get a reading of t_fine. */
441 ret = bmp280_read_temp(data, NULL);
442 if (ret < 0)
443 return ret;
444
445 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
446 (u8 *) &tmp, 2);
447 if (ret < 0) {
448 dev_err(&data->client->dev, "failed to read humidity\n");
449 return ret;
450 }
451
452 adc_humidity = be16_to_cpu(tmp);
453 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
454
455 *val = comp_humidity;
456 *val2 = 1024;
457
458 return IIO_VAL_FRACTIONAL;
459}
460
Vlad Dogarud5c94562014-10-21 11:09:58 +0300461static int bmp280_read_raw(struct iio_dev *indio_dev,
462 struct iio_chan_spec const *chan,
463 int *val, int *val2, long mask)
464{
465 int ret;
466 struct bmp280_data *data = iio_priv(indio_dev);
467
468 mutex_lock(&data->lock);
469
470 switch (mask) {
471 case IIO_CHAN_INFO_PROCESSED:
472 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700473 case IIO_HUMIDITYRELATIVE:
474 ret = data->chip_info->read_humid(data, val, val2);
475 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300476 case IIO_PRESSURE:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900477 ret = data->chip_info->read_press(data, val, val2);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300478 break;
479 case IIO_TEMP:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900480 ret = data->chip_info->read_temp(data, val);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300481 break;
482 default:
483 ret = -EINVAL;
484 break;
485 }
486 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900487 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
488 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700489 case IIO_HUMIDITYRELATIVE:
490 *val = 1 << data->oversampling_humid;
491 ret = IIO_VAL_INT;
492 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900493 case IIO_PRESSURE:
494 *val = 1 << data->oversampling_press;
495 ret = IIO_VAL_INT;
496 break;
497 case IIO_TEMP:
498 *val = 1 << data->oversampling_temp;
499 ret = IIO_VAL_INT;
500 break;
501 default:
502 ret = -EINVAL;
503 break;
504 }
505 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300506 default:
507 ret = -EINVAL;
508 break;
509 }
510
511 mutex_unlock(&data->lock);
512
513 return ret;
514}
515
Matt Ranostay14beaa82016-05-04 22:57:30 -0700516static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
517 int val)
518{
519 int i;
520 const int *avail = data->chip_info->oversampling_humid_avail;
521 const int n = data->chip_info->num_oversampling_humid_avail;
522
523 for (i = 0; i < n; i++) {
524 if (avail[i] == val) {
525 data->oversampling_humid = ilog2(val);
526
527 return data->chip_info->chip_config(data);
528 }
529 }
530 return -EINVAL;
531}
532
Akinobu Mita62979902016-04-24 22:52:11 +0900533static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
534 int val)
535{
536 int i;
537 const int *avail = data->chip_info->oversampling_temp_avail;
538 const int n = data->chip_info->num_oversampling_temp_avail;
539
540 for (i = 0; i < n; i++) {
541 if (avail[i] == val) {
542 data->oversampling_temp = ilog2(val);
543
544 return data->chip_info->chip_config(data);
545 }
546 }
547 return -EINVAL;
548}
549
550static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
551 int val)
552{
553 int i;
554 const int *avail = data->chip_info->oversampling_press_avail;
555 const int n = data->chip_info->num_oversampling_press_avail;
556
557 for (i = 0; i < n; i++) {
558 if (avail[i] == val) {
559 data->oversampling_press = ilog2(val);
560
561 return data->chip_info->chip_config(data);
562 }
563 }
564 return -EINVAL;
565}
566
567static int bmp280_write_raw(struct iio_dev *indio_dev,
568 struct iio_chan_spec const *chan,
569 int val, int val2, long mask)
570{
571 int ret = 0;
572 struct bmp280_data *data = iio_priv(indio_dev);
573
574 switch (mask) {
575 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
576 mutex_lock(&data->lock);
577 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700578 case IIO_HUMIDITYRELATIVE:
579 ret = bmp280_write_oversampling_ratio_humid(data, val);
580 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900581 case IIO_PRESSURE:
582 ret = bmp280_write_oversampling_ratio_press(data, val);
583 break;
584 case IIO_TEMP:
585 ret = bmp280_write_oversampling_ratio_temp(data, val);
586 break;
587 default:
588 ret = -EINVAL;
589 break;
590 }
591 mutex_unlock(&data->lock);
592 break;
593 default:
594 return -EINVAL;
595 }
596
597 return ret;
598}
599
600static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
601{
602 size_t len = 0;
603 int i;
604
605 for (i = 0; i < n; i++)
606 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
607
608 buf[len - 1] = '\n';
609
610 return len;
611}
612
613static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
614 struct device_attribute *attr, char *buf)
615{
616 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
617
618 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
619 data->chip_info->num_oversampling_temp_avail);
620}
621
622static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
623 struct device_attribute *attr, char *buf)
624{
625 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
626
627 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
628 data->chip_info->num_oversampling_press_avail);
629}
630
631static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
632 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
633
634static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
635 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
636
637static struct attribute *bmp280_attributes[] = {
638 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
639 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
640 NULL,
641};
642
643static const struct attribute_group bmp280_attrs_group = {
644 .attrs = bmp280_attributes,
645};
646
Vlad Dogarud5c94562014-10-21 11:09:58 +0300647static const struct iio_info bmp280_info = {
648 .driver_module = THIS_MODULE,
649 .read_raw = &bmp280_read_raw,
Akinobu Mita62979902016-04-24 22:52:11 +0900650 .write_raw = &bmp280_write_raw,
651 .attrs = &bmp280_attrs_group,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300652};
653
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900654static int bmp280_chip_config(struct bmp280_data *data)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300655{
656 int ret;
Akinobu Mita62979902016-04-24 22:52:11 +0900657 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
658 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300659
660 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
661 BMP280_OSRS_TEMP_MASK |
662 BMP280_OSRS_PRESS_MASK |
663 BMP280_MODE_MASK,
Akinobu Mita62979902016-04-24 22:52:11 +0900664 osrs | BMP280_MODE_NORMAL);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300665 if (ret < 0) {
666 dev_err(&data->client->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100667 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300668 return ret;
669 }
670
671 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
672 BMP280_FILTER_MASK,
673 BMP280_FILTER_4X);
674 if (ret < 0) {
675 dev_err(&data->client->dev,
676 "failed to write config register\n");
677 return ret;
678 }
679
680 return ret;
681}
682
Akinobu Mita62979902016-04-24 22:52:11 +0900683static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
684
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900685static const struct bmp280_chip_info bmp280_chip_info = {
686 .regmap_config = &bmp280_regmap_config,
Akinobu Mita62979902016-04-24 22:52:11 +0900687
688 .oversampling_temp_avail = bmp280_oversampling_avail,
689 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
690
691 .oversampling_press_avail = bmp280_oversampling_avail,
692 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
693
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900694 .chip_config = bmp280_chip_config,
695 .read_temp = bmp280_read_temp,
696 .read_press = bmp280_read_press,
697};
698
Matt Ranostay14beaa82016-05-04 22:57:30 -0700699static int bme280_chip_config(struct bmp280_data *data)
700{
701 int ret = bmp280_chip_config(data);
702 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
703
704 if (ret < 0)
705 return ret;
706
707 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
708 BMP280_OSRS_HUMIDITY_MASK, osrs);
709}
710
711static const struct bmp280_chip_info bme280_chip_info = {
712 .regmap_config = &bmp280_regmap_config,
713
714 .oversampling_temp_avail = bmp280_oversampling_avail,
715 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
716
717 .oversampling_press_avail = bmp280_oversampling_avail,
718 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
719
720 .oversampling_humid_avail = bmp280_oversampling_avail,
721 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
722
723 .chip_config = bme280_chip_config,
724 .read_temp = bmp280_read_temp,
725 .read_press = bmp280_read_press,
726 .read_humid = bmp280_read_humid,
727};
728
729
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900730static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
731{
732 switch (reg) {
733 case BMP280_REG_CTRL_MEAS:
734 case BMP280_REG_RESET:
735 return true;
736 default:
737 return false;
738 };
739}
740
741static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
742{
743 switch (reg) {
744 case BMP180_REG_OUT_XLSB:
745 case BMP180_REG_OUT_LSB:
746 case BMP180_REG_OUT_MSB:
747 case BMP280_REG_CTRL_MEAS:
748 return true;
749 default:
750 return false;
751 }
752}
753
754static const struct regmap_config bmp180_regmap_config = {
755 .reg_bits = 8,
756 .val_bits = 8,
757
758 .max_register = BMP180_REG_OUT_XLSB,
759 .cache_type = REGCACHE_RBTREE,
760
761 .writeable_reg = bmp180_is_writeable_reg,
762 .volatile_reg = bmp180_is_volatile_reg,
763};
764
765static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
766{
767 int ret;
768 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
769 unsigned int delay_us;
770 unsigned int ctrl;
771
772 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
773 if (ret)
774 return ret;
775
776 if (ctrl_meas == BMP180_MEAS_TEMP)
777 delay_us = 4500;
778 else
Akinobu Mita62979902016-04-24 22:52:11 +0900779 delay_us = conversion_time_max[data->oversampling_press];
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900780
781 usleep_range(delay_us, delay_us + 1000);
782
783 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
784 if (ret)
785 return ret;
786
787 /* The value of this bit reset to "0" after conversion is complete */
788 if (ctrl & BMP180_MEAS_SCO)
789 return -EIO;
790
791 return 0;
792}
793
794static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
795{
796 int ret;
797 __be16 tmp = 0;
798
799 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
800 if (ret)
801 return ret;
802
803 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
804 if (ret)
805 return ret;
806
807 *val = be16_to_cpu(tmp);
808
809 return 0;
810}
811
812/*
813 * These enums are used for indexing into the array of calibration
814 * coefficients for BMP180.
815 */
816enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
817
818struct bmp180_calib {
819 s16 AC1;
820 s16 AC2;
821 s16 AC3;
822 u16 AC4;
823 u16 AC5;
824 u16 AC6;
825 s16 B1;
826 s16 B2;
827 s16 MB;
828 s16 MC;
829 s16 MD;
830};
831
832static int bmp180_read_calib(struct bmp280_data *data,
833 struct bmp180_calib *calib)
834{
835 int ret;
836 int i;
837 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
838
839 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
840 sizeof(buf));
841
842 if (ret < 0)
843 return ret;
844
845 /* None of the words has the value 0 or 0xFFFF */
846 for (i = 0; i < ARRAY_SIZE(buf); i++) {
847 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
848 return -EIO;
849 }
850
851 calib->AC1 = be16_to_cpu(buf[AC1]);
852 calib->AC2 = be16_to_cpu(buf[AC2]);
853 calib->AC3 = be16_to_cpu(buf[AC3]);
854 calib->AC4 = be16_to_cpu(buf[AC4]);
855 calib->AC5 = be16_to_cpu(buf[AC5]);
856 calib->AC6 = be16_to_cpu(buf[AC6]);
857 calib->B1 = be16_to_cpu(buf[B1]);
858 calib->B2 = be16_to_cpu(buf[B2]);
859 calib->MB = be16_to_cpu(buf[MB]);
860 calib->MC = be16_to_cpu(buf[MC]);
861 calib->MD = be16_to_cpu(buf[MD]);
862
863 return 0;
864}
865
866/*
867 * Returns temperature in DegC, resolution is 0.1 DegC.
868 * t_fine carries fine temperature as global value.
869 *
870 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
871 */
872static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
873{
874 int ret;
875 s32 x1, x2;
876 struct bmp180_calib calib;
877
878 ret = bmp180_read_calib(data, &calib);
879 if (ret < 0) {
880 dev_err(&data->client->dev,
881 "failed to read calibration coefficients\n");
882 return ret;
883 }
884
885 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
886 x2 = (calib.MC << 11) / (x1 + calib.MD);
887 data->t_fine = x1 + x2;
888
889 return (data->t_fine + 8) >> 4;
890}
891
892static int bmp180_read_temp(struct bmp280_data *data, int *val)
893{
894 int ret;
895 s32 adc_temp, comp_temp;
896
897 ret = bmp180_read_adc_temp(data, &adc_temp);
898 if (ret)
899 return ret;
900
901 comp_temp = bmp180_compensate_temp(data, adc_temp);
902
903 /*
904 * val might be NULL if we're called by the read_press routine,
905 * who only cares about the carry over t_fine value.
906 */
907 if (val) {
908 *val = comp_temp * 100;
909 return IIO_VAL_INT;
910 }
911
912 return 0;
913}
914
915static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
916{
917 int ret;
918 __be32 tmp = 0;
Akinobu Mita62979902016-04-24 22:52:11 +0900919 u8 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900920
921 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
922 if (ret)
923 return ret;
924
925 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
926 if (ret)
927 return ret;
928
929 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
930
931 return 0;
932}
933
934/*
935 * Returns pressure in Pa, resolution is 1 Pa.
936 *
937 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
938 */
939static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
940{
941 int ret;
942 s32 x1, x2, x3, p;
943 s32 b3, b6;
944 u32 b4, b7;
Akinobu Mita62979902016-04-24 22:52:11 +0900945 s32 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900946 struct bmp180_calib calib;
947
948 ret = bmp180_read_calib(data, &calib);
949 if (ret < 0) {
950 dev_err(&data->client->dev,
951 "failed to read calibration coefficients\n");
952 return ret;
953 }
954
955 b6 = data->t_fine - 4000;
956 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
957 x2 = calib.AC2 * b6 >> 11;
958 x3 = x1 + x2;
959 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
960 x1 = calib.AC3 * b6 >> 13;
961 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
962 x3 = (x1 + x2 + 2) >> 2;
963 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
964 b7 = ((u32)adc_press - b3) * (50000 >> oss);
965 if (b7 < 0x80000000)
966 p = (b7 * 2) / b4;
967 else
968 p = (b7 / b4) * 2;
969
970 x1 = (p >> 8) * (p >> 8);
971 x1 = (x1 * 3038) >> 16;
972 x2 = (-7357 * p) >> 16;
973
974 return p + ((x1 + x2 + 3791) >> 4);
975}
976
977static int bmp180_read_press(struct bmp280_data *data,
978 int *val, int *val2)
979{
980 int ret;
981 s32 adc_press;
982 u32 comp_press;
983
984 /* Read and compensate temperature so we get a reading of t_fine. */
985 ret = bmp180_read_temp(data, NULL);
986 if (ret)
987 return ret;
988
989 ret = bmp180_read_adc_press(data, &adc_press);
990 if (ret)
991 return ret;
992
993 comp_press = bmp180_compensate_press(data, adc_press);
994
995 *val = comp_press;
996 *val2 = 1000;
997
998 return IIO_VAL_FRACTIONAL;
999}
1000
1001static int bmp180_chip_config(struct bmp280_data *data)
1002{
1003 return 0;
1004}
1005
Akinobu Mita62979902016-04-24 22:52:11 +09001006static const int bmp180_oversampling_temp_avail[] = { 1 };
1007static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1008
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001009static const struct bmp280_chip_info bmp180_chip_info = {
1010 .regmap_config = &bmp180_regmap_config,
Akinobu Mita62979902016-04-24 22:52:11 +09001011
1012 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
1013 .num_oversampling_temp_avail =
1014 ARRAY_SIZE(bmp180_oversampling_temp_avail),
1015
1016 .oversampling_press_avail = bmp180_oversampling_press_avail,
1017 .num_oversampling_press_avail =
1018 ARRAY_SIZE(bmp180_oversampling_press_avail),
1019
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001020 .chip_config = bmp180_chip_config,
1021 .read_temp = bmp180_read_temp,
1022 .read_press = bmp180_read_press,
1023};
1024
Vlad Dogarud5c94562014-10-21 11:09:58 +03001025static int bmp280_probe(struct i2c_client *client,
1026 const struct i2c_device_id *id)
1027{
1028 int ret;
1029 struct iio_dev *indio_dev;
1030 struct bmp280_data *data;
1031 unsigned int chip_id;
Linus Walleijc5842b42016-06-30 03:48:47 +02001032 struct gpio_desc *gpiod;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001033
1034 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1035 if (!indio_dev)
1036 return -ENOMEM;
1037
Vlad Dogarud5c94562014-10-21 11:09:58 +03001038 data = iio_priv(indio_dev);
1039 mutex_init(&data->lock);
1040 data->client = client;
1041
1042 indio_dev->dev.parent = &client->dev;
1043 indio_dev->name = id->name;
1044 indio_dev->channels = bmp280_channels;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001045 indio_dev->info = &bmp280_info;
1046 indio_dev->modes = INDIO_DIRECT_MODE;
1047
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001048 switch (id->driver_data) {
1049 case BMP180_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -07001050 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001051 data->chip_info = &bmp180_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +09001052 data->oversampling_press = ilog2(8);
1053 data->oversampling_temp = ilog2(1);
Linus Walleijbd525e62016-06-30 03:48:48 +02001054 data->start_up_time = 10;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001055 break;
1056 case BMP280_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -07001057 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001058 data->chip_info = &bmp280_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +09001059 data->oversampling_press = ilog2(16);
1060 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +02001061 data->start_up_time = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001062 break;
Matt Ranostay14beaa82016-05-04 22:57:30 -07001063 case BME280_CHIP_ID:
1064 indio_dev->num_channels = 3;
1065 data->chip_info = &bme280_chip_info;
1066 data->oversampling_press = ilog2(16);
1067 data->oversampling_humid = ilog2(16);
1068 data->oversampling_temp = ilog2(2);
Linus Walleijbd525e62016-06-30 03:48:48 +02001069 data->start_up_time = 2;
Matt Ranostay14beaa82016-05-04 22:57:30 -07001070 break;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001071 default:
1072 return -EINVAL;
1073 }
1074
Linus Walleijbd525e62016-06-30 03:48:48 +02001075 /* Bring up regulators */
1076 data->vddd = devm_regulator_get(&client->dev, "vddd");
1077 if (IS_ERR(data->vddd)) {
1078 dev_err(&client->dev, "failed to get VDDD regulator\n");
1079 return PTR_ERR(data->vddd);
1080 }
1081 ret = regulator_enable(data->vddd);
1082 if (ret) {
1083 dev_err(&client->dev, "failed to enable VDDD regulator\n");
1084 return ret;
1085 }
1086 data->vdda = devm_regulator_get(&client->dev, "vdda");
1087 if (IS_ERR(data->vdda)) {
1088 dev_err(&client->dev, "failed to get VDDA regulator\n");
1089 ret = PTR_ERR(data->vddd);
1090 goto out_disable_vddd;
1091 }
1092 ret = regulator_enable(data->vdda);
1093 if (ret) {
1094 dev_err(&client->dev, "failed to enable VDDA regulator\n");
1095 goto out_disable_vddd;
1096 }
1097 /* Wait to make sure we started up properly */
1098 mdelay(data->start_up_time);
1099
Linus Walleijc5842b42016-06-30 03:48:47 +02001100 /* Bring chip out of reset if there is an assigned GPIO line */
1101 gpiod = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
1102 /* Deassert the signal */
1103 if (!IS_ERR(gpiod)) {
1104 dev_info(&client->dev, "release reset\n");
1105 gpiod_set_value(gpiod, 0);
1106 }
1107
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001108 data->regmap = devm_regmap_init_i2c(client,
1109 data->chip_info->regmap_config);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001110 if (IS_ERR(data->regmap)) {
1111 dev_err(&client->dev, "failed to allocate register map\n");
Linus Walleijbd525e62016-06-30 03:48:48 +02001112 ret = PTR_ERR(data->regmap);
1113 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001114 }
1115
1116 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
1117 if (ret < 0)
1118 return ret;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001119 if (chip_id != id->driver_data) {
Akinobu Mitaa3e5afe2016-04-28 23:39:53 +09001120 dev_err(&client->dev, "bad chip id. expected %lx got %x\n",
1121 id->driver_data, chip_id);
Linus Walleijbd525e62016-06-30 03:48:48 +02001122 ret = -EINVAL;
1123 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001124 }
1125
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001126 ret = data->chip_info->chip_config(data);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001127 if (ret < 0)
Linus Walleijbd525e62016-06-30 03:48:48 +02001128 goto out_disable_vdda;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001129
Linus Walleijbd525e62016-06-30 03:48:48 +02001130 i2c_set_clientdata(client, indio_dev);
1131
1132 ret = iio_device_register(indio_dev);
1133 if (ret)
1134 goto out_disable_vdda;
1135
1136 return 0;
1137
1138out_disable_vdda:
1139 regulator_disable(data->vdda);
1140out_disable_vddd:
1141 regulator_disable(data->vddd);
1142 return ret;
1143}
1144
1145static int bmp280_remove(struct i2c_client *client)
1146{
1147 struct iio_dev *indio_dev = i2c_get_clientdata(client);
1148 struct bmp280_data *data = iio_priv(indio_dev);
1149
1150 iio_device_unregister(indio_dev);
1151 regulator_disable(data->vdda);
1152 regulator_disable(data->vddd);
1153 return 0;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001154}
1155
1156static const struct acpi_device_id bmp280_acpi_match[] = {
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001157 {"BMP0280", BMP280_CHIP_ID },
1158 {"BMP0180", BMP180_CHIP_ID },
1159 {"BMP0085", BMP180_CHIP_ID },
Matt Ranostay14beaa82016-05-04 22:57:30 -07001160 {"BME0280", BME280_CHIP_ID },
Vlad Dogarud5c94562014-10-21 11:09:58 +03001161 { },
1162};
1163MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
1164
Linus Walleij78f50272016-06-30 03:48:46 +02001165#ifdef CONFIG_OF
1166static const struct of_device_id bmp280_of_match[] = {
1167 { .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
1168 { .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
1169 { .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
1170 { .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
1171 { },
1172};
1173MODULE_DEVICE_TABLE(of, bmp280_of_match);
1174#else
1175#define bmp280_of_match NULL
1176#endif
1177
Vlad Dogarud5c94562014-10-21 11:09:58 +03001178static const struct i2c_device_id bmp280_id[] = {
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001179 {"bmp280", BMP280_CHIP_ID },
1180 {"bmp180", BMP180_CHIP_ID },
1181 {"bmp085", BMP180_CHIP_ID },
Matt Ranostay14beaa82016-05-04 22:57:30 -07001182 {"bme280", BME280_CHIP_ID },
Vlad Dogarud5c94562014-10-21 11:09:58 +03001183 { },
1184};
1185MODULE_DEVICE_TABLE(i2c, bmp280_id);
1186
1187static struct i2c_driver bmp280_driver = {
1188 .driver = {
1189 .name = "bmp280",
1190 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
Linus Walleij78f50272016-06-30 03:48:46 +02001191 .of_match_table = of_match_ptr(bmp280_of_match),
Vlad Dogarud5c94562014-10-21 11:09:58 +03001192 },
1193 .probe = bmp280_probe,
Linus Walleijbd525e62016-06-30 03:48:48 +02001194 .remove = bmp280_remove,
Vlad Dogarud5c94562014-10-21 11:09:58 +03001195 .id_table = bmp280_id,
1196};
1197module_i2c_driver(bmp280_driver);
1198
1199MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001200MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
Vlad Dogarud5c94562014-10-21 11:09:58 +03001201MODULE_LICENSE("GPL v2");