blob: 77172f047f616f4faeac55daeb09f227af7f0840 [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>
Vlad Dogarud5c94562014-10-21 11:09:58 +030027
Akinobu Mita6dba72e2016-04-24 22:52:10 +090028/* BMP280 specific registers */
Matt Ranostay14beaa82016-05-04 22:57:30 -070029#define BMP280_REG_HUMIDITY_LSB 0xFE
30#define BMP280_REG_HUMIDITY_MSB 0xFD
Vlad Dogarud5c94562014-10-21 11:09:58 +030031#define BMP280_REG_TEMP_XLSB 0xFC
32#define BMP280_REG_TEMP_LSB 0xFB
33#define BMP280_REG_TEMP_MSB 0xFA
34#define BMP280_REG_PRESS_XLSB 0xF9
35#define BMP280_REG_PRESS_LSB 0xF8
36#define BMP280_REG_PRESS_MSB 0xF7
37
38#define BMP280_REG_CONFIG 0xF5
Matt Ranostay14beaa82016-05-04 22:57:30 -070039#define BMP280_REG_CTRL_MEAS 0xF4
Vlad Dogarud5c94562014-10-21 11:09:58 +030040#define BMP280_REG_STATUS 0xF3
Matt Ranostay14beaa82016-05-04 22:57:30 -070041#define BMP280_REG_CTRL_HUMIDITY 0xF2
42
43/* Due to non linear mapping, and data sizes we can't do a bulk read */
44#define BMP280_REG_COMP_H1 0xA1
45#define BMP280_REG_COMP_H2 0xE1
46#define BMP280_REG_COMP_H3 0xE3
47#define BMP280_REG_COMP_H4 0xE4
48#define BMP280_REG_COMP_H5 0xE5
49#define BMP280_REG_COMP_H6 0xE7
Vlad Dogarud5c94562014-10-21 11:09:58 +030050
51#define BMP280_REG_COMP_TEMP_START 0x88
52#define BMP280_COMP_TEMP_REG_COUNT 6
53
54#define BMP280_REG_COMP_PRESS_START 0x8E
55#define BMP280_COMP_PRESS_REG_COUNT 18
56
57#define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
58#define BMP280_FILTER_OFF 0
59#define BMP280_FILTER_2X BIT(2)
60#define BMP280_FILTER_4X BIT(3)
61#define BMP280_FILTER_8X (BIT(3) | BIT(2))
62#define BMP280_FILTER_16X BIT(4)
63
Matt Ranostay14beaa82016-05-04 22:57:30 -070064#define BMP280_OSRS_HUMIDITY_MASK (BIT(2) | BIT(1) | BIT(0))
65#define BMP280_OSRS_HUMIDITIY_X(osrs_h) ((osrs_h) << 0)
66#define BMP280_OSRS_HUMIDITY_SKIP 0
67#define BMP280_OSRS_HUMIDITY_1X BMP280_OSRS_HUMIDITIY_X(1)
68#define BMP280_OSRS_HUMIDITY_2X BMP280_OSRS_HUMIDITIY_X(2)
69#define BMP280_OSRS_HUMIDITY_4X BMP280_OSRS_HUMIDITIY_X(3)
70#define BMP280_OSRS_HUMIDITY_8X BMP280_OSRS_HUMIDITIY_X(4)
71#define BMP280_OSRS_HUMIDITY_16X BMP280_OSRS_HUMIDITIY_X(5)
72
Vlad Dogarud5c94562014-10-21 11:09:58 +030073#define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
74#define BMP280_OSRS_TEMP_SKIP 0
Akinobu Mita62979902016-04-24 22:52:11 +090075#define BMP280_OSRS_TEMP_X(osrs_t) ((osrs_t) << 5)
76#define BMP280_OSRS_TEMP_1X BMP280_OSRS_TEMP_X(1)
77#define BMP280_OSRS_TEMP_2X BMP280_OSRS_TEMP_X(2)
78#define BMP280_OSRS_TEMP_4X BMP280_OSRS_TEMP_X(3)
79#define BMP280_OSRS_TEMP_8X BMP280_OSRS_TEMP_X(4)
80#define BMP280_OSRS_TEMP_16X BMP280_OSRS_TEMP_X(5)
Vlad Dogarud5c94562014-10-21 11:09:58 +030081
82#define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
83#define BMP280_OSRS_PRESS_SKIP 0
Akinobu Mita62979902016-04-24 22:52:11 +090084#define BMP280_OSRS_PRESS_X(osrs_p) ((osrs_p) << 2)
85#define BMP280_OSRS_PRESS_1X BMP280_OSRS_PRESS_X(1)
86#define BMP280_OSRS_PRESS_2X BMP280_OSRS_PRESS_X(2)
87#define BMP280_OSRS_PRESS_4X BMP280_OSRS_PRESS_X(3)
88#define BMP280_OSRS_PRESS_8X BMP280_OSRS_PRESS_X(4)
89#define BMP280_OSRS_PRESS_16X BMP280_OSRS_PRESS_X(5)
Vlad Dogarud5c94562014-10-21 11:09:58 +030090
91#define BMP280_MODE_MASK (BIT(1) | BIT(0))
92#define BMP280_MODE_SLEEP 0
93#define BMP280_MODE_FORCED BIT(0)
94#define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
95
Akinobu Mita6dba72e2016-04-24 22:52:10 +090096/* BMP180 specific registers */
97#define BMP180_REG_OUT_XLSB 0xF8
98#define BMP180_REG_OUT_LSB 0xF7
99#define BMP180_REG_OUT_MSB 0xF6
100
101#define BMP180_REG_CALIB_START 0xAA
102#define BMP180_REG_CALIB_COUNT 22
103
104#define BMP180_MEAS_SCO BIT(5)
105#define BMP180_MEAS_TEMP (0x0E | BMP180_MEAS_SCO)
106#define BMP180_MEAS_PRESS_X(oss) ((oss) << 6 | 0x14 | BMP180_MEAS_SCO)
107#define BMP180_MEAS_PRESS_1X BMP180_MEAS_PRESS_X(0)
108#define BMP180_MEAS_PRESS_2X BMP180_MEAS_PRESS_X(1)
109#define BMP180_MEAS_PRESS_4X BMP180_MEAS_PRESS_X(2)
110#define BMP180_MEAS_PRESS_8X BMP180_MEAS_PRESS_X(3)
111
112/* BMP180 and BMP280 common registers */
113#define BMP280_REG_CTRL_MEAS 0xF4
114#define BMP280_REG_RESET 0xE0
115#define BMP280_REG_ID 0xD0
116
117#define BMP180_CHIP_ID 0x55
Vlad Dogarud5c94562014-10-21 11:09:58 +0300118#define BMP280_CHIP_ID 0x58
Matt Ranostay14beaa82016-05-04 22:57:30 -0700119#define BME280_CHIP_ID 0x60
Vlad Dogarud5c94562014-10-21 11:09:58 +0300120#define BMP280_SOFT_RESET_VAL 0xB6
121
122struct bmp280_data {
123 struct i2c_client *client;
124 struct mutex lock;
125 struct regmap *regmap;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900126 const struct bmp280_chip_info *chip_info;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300127
Akinobu Mita62979902016-04-24 22:52:11 +0900128 /* log of base 2 of oversampling rate */
129 u8 oversampling_press;
130 u8 oversampling_temp;
Matt Ranostay14beaa82016-05-04 22:57:30 -0700131 u8 oversampling_humid;
Akinobu Mita62979902016-04-24 22:52:11 +0900132
Vlad Dogarud5c94562014-10-21 11:09:58 +0300133 /*
134 * Carryover value from temperature conversion, used in pressure
135 * calculation.
136 */
137 s32 t_fine;
138};
139
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900140struct bmp280_chip_info {
141 const struct regmap_config *regmap_config;
142
Akinobu Mita62979902016-04-24 22:52:11 +0900143 const int *oversampling_temp_avail;
144 int num_oversampling_temp_avail;
145
146 const int *oversampling_press_avail;
147 int num_oversampling_press_avail;
148
Matt Ranostay14beaa82016-05-04 22:57:30 -0700149 const int *oversampling_humid_avail;
150 int num_oversampling_humid_avail;
151
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900152 int (*chip_config)(struct bmp280_data *);
153 int (*read_temp)(struct bmp280_data *, int *);
154 int (*read_press)(struct bmp280_data *, int *, int *);
Matt Ranostay14beaa82016-05-04 22:57:30 -0700155 int (*read_humid)(struct bmp280_data *, int *, int *);
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900156};
157
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200158/*
159 * These enums are used for indexing into the array of compensation
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900160 * parameters for BMP280.
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200161 */
162enum { T1, T2, T3 };
163enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
Vlad Dogarud5c94562014-10-21 11:09:58 +0300164
165static const struct iio_chan_spec bmp280_channels[] = {
166 {
167 .type = IIO_PRESSURE,
Akinobu Mita62979902016-04-24 22:52:11 +0900168 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
169 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300170 },
171 {
172 .type = IIO_TEMP,
Akinobu Mita62979902016-04-24 22:52:11 +0900173 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
174 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
Vlad Dogarud5c94562014-10-21 11:09:58 +0300175 },
Matt Ranostay14beaa82016-05-04 22:57:30 -0700176 {
177 .type = IIO_HUMIDITYRELATIVE,
178 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
179 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
180 },
Vlad Dogarud5c94562014-10-21 11:09:58 +0300181};
182
183static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
184{
185 switch (reg) {
186 case BMP280_REG_CONFIG:
Matt Ranostay14beaa82016-05-04 22:57:30 -0700187 case BMP280_REG_CTRL_HUMIDITY:
Vlad Dogarud5c94562014-10-21 11:09:58 +0300188 case BMP280_REG_CTRL_MEAS:
189 case BMP280_REG_RESET:
190 return true;
191 default:
192 return false;
193 };
194}
195
196static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
197{
198 switch (reg) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700199 case BMP280_REG_HUMIDITY_LSB:
200 case BMP280_REG_HUMIDITY_MSB:
Vlad Dogarud5c94562014-10-21 11:09:58 +0300201 case BMP280_REG_TEMP_XLSB:
202 case BMP280_REG_TEMP_LSB:
203 case BMP280_REG_TEMP_MSB:
204 case BMP280_REG_PRESS_XLSB:
205 case BMP280_REG_PRESS_LSB:
206 case BMP280_REG_PRESS_MSB:
207 case BMP280_REG_STATUS:
208 return true;
209 default:
210 return false;
211 }
212}
213
214static const struct regmap_config bmp280_regmap_config = {
215 .reg_bits = 8,
216 .val_bits = 8,
217
Matt Ranostay14beaa82016-05-04 22:57:30 -0700218 .max_register = BMP280_REG_HUMIDITY_LSB,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300219 .cache_type = REGCACHE_RBTREE,
220
221 .writeable_reg = bmp280_is_writeable_reg,
222 .volatile_reg = bmp280_is_volatile_reg,
223};
224
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200225/*
Matt Ranostay14beaa82016-05-04 22:57:30 -0700226 * Returns humidity in percent, resolution is 0.01 percent. Output value of
227 * "47445" represents 47445/1024 = 46.333 %RH.
228 *
229 * Taken from BME280 datasheet, Section 4.2.3, "Compensation formula".
230 */
231
232static u32 bmp280_compensate_humidity(struct bmp280_data *data,
233 s32 adc_humidity)
234{
235 struct device *dev = &data->client->dev;
236 unsigned int H1, H3, tmp;
237 int H2, H4, H5, H6, ret, var;
238
239 ret = regmap_read(data->regmap, BMP280_REG_COMP_H1, &H1);
240 if (ret < 0) {
241 dev_err(dev, "failed to read H1 comp value\n");
242 return ret;
243 }
244
245 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H2, &tmp, 2);
246 if (ret < 0) {
247 dev_err(dev, "failed to read H2 comp value\n");
248 return ret;
249 }
250 H2 = sign_extend32(le16_to_cpu(tmp), 15);
251
252 ret = regmap_read(data->regmap, BMP280_REG_COMP_H3, &H3);
253 if (ret < 0) {
254 dev_err(dev, "failed to read H3 comp value\n");
255 return ret;
256 }
257
258 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H4, &tmp, 2);
259 if (ret < 0) {
260 dev_err(dev, "failed to read H4 comp value\n");
261 return ret;
262 }
263 H4 = sign_extend32(((be16_to_cpu(tmp) >> 4) & 0xff0) |
264 (be16_to_cpu(tmp) & 0xf), 11);
265
266 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_H5, &tmp, 2);
267 if (ret < 0) {
268 dev_err(dev, "failed to read H5 comp value\n");
269 return ret;
270 }
271 H5 = sign_extend32(((le16_to_cpu(tmp) >> 4) & 0xfff), 11);
272
273 ret = regmap_read(data->regmap, BMP280_REG_COMP_H6, &tmp);
274 if (ret < 0) {
275 dev_err(dev, "failed to read H6 comp value\n");
276 return ret;
277 }
278 H6 = sign_extend32(tmp, 7);
279
280 var = ((s32)data->t_fine) - 76800;
281 var = ((((adc_humidity << 14) - (H4 << 20) - (H5 * var)) + 16384) >> 15)
282 * (((((((var * H6) >> 10) * (((var * H3) >> 11) + 32768)) >> 10)
283 + 2097152) * H2 + 8192) >> 14);
284 var -= ((((var >> 15) * (var >> 15)) >> 7) * H1) >> 4;
285
286 return var >> 12;
287};
288
289/*
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200290 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
291 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
292 * value.
293 *
294 * Taken from datasheet, Section 3.11.3, "Compensation formula".
295 */
296static s32 bmp280_compensate_temp(struct bmp280_data *data,
297 s32 adc_temp)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300298{
299 int ret;
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100300 s32 var1, var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300301 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
302
303 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
304 buf, BMP280_COMP_TEMP_REG_COUNT);
305 if (ret < 0) {
306 dev_err(&data->client->dev,
307 "failed to read temperature calibration parameters\n");
308 return ret;
309 }
310
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200311 /*
312 * The double casts are necessary because le16_to_cpu returns an
313 * unsigned 16-bit value. Casting that value directly to a
314 * signed 32-bit will not do proper sign extension.
315 *
316 * Conversely, T1 and P1 are unsigned values, so they can be
317 * cast straight to the larger type.
318 */
319 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
320 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
321 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
322 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
323 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
Irina Tirdeaabad3982015-04-08 18:26:12 +0300324 data->t_fine = var1 + var2;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300325
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100326 return (data->t_fine * 5 + 128) >> 8;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300327}
328
329/*
330 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
331 * integer bits and 8 fractional bits). Output value of "24674867"
332 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
333 *
334 * Taken from datasheet, Section 3.11.3, "Compensation formula".
335 */
336static u32 bmp280_compensate_press(struct bmp280_data *data,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300337 s32 adc_press)
338{
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200339 int ret;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300340 s64 var1, var2, p;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200341 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
Vlad Dogarud5c94562014-10-21 11:09:58 +0300342
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200343 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
344 buf, BMP280_COMP_PRESS_REG_COUNT);
345 if (ret < 0) {
346 dev_err(&data->client->dev,
347 "failed to read pressure calibration parameters\n");
348 return ret;
349 }
350
351 var1 = ((s64)data->t_fine) - 128000;
352 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100353 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
354 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200355 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
356 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
357 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300358
359 if (var1 == 0)
360 return 0;
361
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200362 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
Vlad Dogaru46ee98a2014-10-23 15:52:00 +0100363 p = div64_s64(p, var1);
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200364 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
365 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
366 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300367
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100368 return (u32)p;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300369}
370
371static int bmp280_read_temp(struct bmp280_data *data,
372 int *val)
373{
374 int ret;
375 __be32 tmp = 0;
376 s32 adc_temp, comp_temp;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300377
378 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
379 (u8 *) &tmp, 3);
380 if (ret < 0) {
381 dev_err(&data->client->dev, "failed to read temperature\n");
382 return ret;
383 }
384
385 adc_temp = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200386 comp_temp = bmp280_compensate_temp(data, adc_temp);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300387
388 /*
389 * val might be NULL if we're called by the read_press routine,
390 * who only cares about the carry over t_fine value.
391 */
392 if (val) {
393 *val = comp_temp * 10;
394 return IIO_VAL_INT;
395 }
396
397 return 0;
398}
399
400static int bmp280_read_press(struct bmp280_data *data,
401 int *val, int *val2)
402{
403 int ret;
404 __be32 tmp = 0;
405 s32 adc_press;
406 u32 comp_press;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300407
408 /* Read and compensate temperature so we get a reading of t_fine. */
409 ret = bmp280_read_temp(data, NULL);
410 if (ret < 0)
411 return ret;
412
413 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
414 (u8 *) &tmp, 3);
415 if (ret < 0) {
416 dev_err(&data->client->dev, "failed to read pressure\n");
417 return ret;
418 }
419
420 adc_press = be32_to_cpu(tmp) >> 12;
Vlad Dogaru0f8994b2014-11-20 14:00:48 +0200421 comp_press = bmp280_compensate_press(data, adc_press);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300422
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000423 *val = comp_press;
424 *val2 = 256000;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300425
Hartmut Knaack81ebe852014-10-31 01:22:00 +0000426 return IIO_VAL_FRACTIONAL;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300427}
428
Matt Ranostay14beaa82016-05-04 22:57:30 -0700429static int bmp280_read_humid(struct bmp280_data *data, int *val, int *val2)
430{
431 int ret;
432 __be16 tmp = 0;
433 s32 adc_humidity;
434 u32 comp_humidity;
435
436 /* Read and compensate temperature so we get a reading of t_fine. */
437 ret = bmp280_read_temp(data, NULL);
438 if (ret < 0)
439 return ret;
440
441 ret = regmap_bulk_read(data->regmap, BMP280_REG_HUMIDITY_MSB,
442 (u8 *) &tmp, 2);
443 if (ret < 0) {
444 dev_err(&data->client->dev, "failed to read humidity\n");
445 return ret;
446 }
447
448 adc_humidity = be16_to_cpu(tmp);
449 comp_humidity = bmp280_compensate_humidity(data, adc_humidity);
450
451 *val = comp_humidity;
452 *val2 = 1024;
453
454 return IIO_VAL_FRACTIONAL;
455}
456
Vlad Dogarud5c94562014-10-21 11:09:58 +0300457static int bmp280_read_raw(struct iio_dev *indio_dev,
458 struct iio_chan_spec const *chan,
459 int *val, int *val2, long mask)
460{
461 int ret;
462 struct bmp280_data *data = iio_priv(indio_dev);
463
464 mutex_lock(&data->lock);
465
466 switch (mask) {
467 case IIO_CHAN_INFO_PROCESSED:
468 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700469 case IIO_HUMIDITYRELATIVE:
470 ret = data->chip_info->read_humid(data, val, val2);
471 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300472 case IIO_PRESSURE:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900473 ret = data->chip_info->read_press(data, val, val2);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300474 break;
475 case IIO_TEMP:
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900476 ret = data->chip_info->read_temp(data, val);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300477 break;
478 default:
479 ret = -EINVAL;
480 break;
481 }
482 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900483 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
484 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700485 case IIO_HUMIDITYRELATIVE:
486 *val = 1 << data->oversampling_humid;
487 ret = IIO_VAL_INT;
488 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900489 case IIO_PRESSURE:
490 *val = 1 << data->oversampling_press;
491 ret = IIO_VAL_INT;
492 break;
493 case IIO_TEMP:
494 *val = 1 << data->oversampling_temp;
495 ret = IIO_VAL_INT;
496 break;
497 default:
498 ret = -EINVAL;
499 break;
500 }
501 break;
Vlad Dogarud5c94562014-10-21 11:09:58 +0300502 default:
503 ret = -EINVAL;
504 break;
505 }
506
507 mutex_unlock(&data->lock);
508
509 return ret;
510}
511
Matt Ranostay14beaa82016-05-04 22:57:30 -0700512static int bmp280_write_oversampling_ratio_humid(struct bmp280_data *data,
513 int val)
514{
515 int i;
516 const int *avail = data->chip_info->oversampling_humid_avail;
517 const int n = data->chip_info->num_oversampling_humid_avail;
518
519 for (i = 0; i < n; i++) {
520 if (avail[i] == val) {
521 data->oversampling_humid = ilog2(val);
522
523 return data->chip_info->chip_config(data);
524 }
525 }
526 return -EINVAL;
527}
528
Akinobu Mita62979902016-04-24 22:52:11 +0900529static int bmp280_write_oversampling_ratio_temp(struct bmp280_data *data,
530 int val)
531{
532 int i;
533 const int *avail = data->chip_info->oversampling_temp_avail;
534 const int n = data->chip_info->num_oversampling_temp_avail;
535
536 for (i = 0; i < n; i++) {
537 if (avail[i] == val) {
538 data->oversampling_temp = ilog2(val);
539
540 return data->chip_info->chip_config(data);
541 }
542 }
543 return -EINVAL;
544}
545
546static int bmp280_write_oversampling_ratio_press(struct bmp280_data *data,
547 int val)
548{
549 int i;
550 const int *avail = data->chip_info->oversampling_press_avail;
551 const int n = data->chip_info->num_oversampling_press_avail;
552
553 for (i = 0; i < n; i++) {
554 if (avail[i] == val) {
555 data->oversampling_press = ilog2(val);
556
557 return data->chip_info->chip_config(data);
558 }
559 }
560 return -EINVAL;
561}
562
563static int bmp280_write_raw(struct iio_dev *indio_dev,
564 struct iio_chan_spec const *chan,
565 int val, int val2, long mask)
566{
567 int ret = 0;
568 struct bmp280_data *data = iio_priv(indio_dev);
569
570 switch (mask) {
571 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
572 mutex_lock(&data->lock);
573 switch (chan->type) {
Matt Ranostay14beaa82016-05-04 22:57:30 -0700574 case IIO_HUMIDITYRELATIVE:
575 ret = bmp280_write_oversampling_ratio_humid(data, val);
576 break;
Akinobu Mita62979902016-04-24 22:52:11 +0900577 case IIO_PRESSURE:
578 ret = bmp280_write_oversampling_ratio_press(data, val);
579 break;
580 case IIO_TEMP:
581 ret = bmp280_write_oversampling_ratio_temp(data, val);
582 break;
583 default:
584 ret = -EINVAL;
585 break;
586 }
587 mutex_unlock(&data->lock);
588 break;
589 default:
590 return -EINVAL;
591 }
592
593 return ret;
594}
595
596static ssize_t bmp280_show_avail(char *buf, const int *vals, const int n)
597{
598 size_t len = 0;
599 int i;
600
601 for (i = 0; i < n; i++)
602 len += scnprintf(buf + len, PAGE_SIZE - len, "%d ", vals[i]);
603
604 buf[len - 1] = '\n';
605
606 return len;
607}
608
609static ssize_t bmp280_show_temp_oversampling_avail(struct device *dev,
610 struct device_attribute *attr, char *buf)
611{
612 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
613
614 return bmp280_show_avail(buf, data->chip_info->oversampling_temp_avail,
615 data->chip_info->num_oversampling_temp_avail);
616}
617
618static ssize_t bmp280_show_press_oversampling_avail(struct device *dev,
619 struct device_attribute *attr, char *buf)
620{
621 struct bmp280_data *data = iio_priv(dev_to_iio_dev(dev));
622
623 return bmp280_show_avail(buf, data->chip_info->oversampling_press_avail,
624 data->chip_info->num_oversampling_press_avail);
625}
626
627static IIO_DEVICE_ATTR(in_temp_oversampling_ratio_available,
628 S_IRUGO, bmp280_show_temp_oversampling_avail, NULL, 0);
629
630static IIO_DEVICE_ATTR(in_pressure_oversampling_ratio_available,
631 S_IRUGO, bmp280_show_press_oversampling_avail, NULL, 0);
632
633static struct attribute *bmp280_attributes[] = {
634 &iio_dev_attr_in_temp_oversampling_ratio_available.dev_attr.attr,
635 &iio_dev_attr_in_pressure_oversampling_ratio_available.dev_attr.attr,
636 NULL,
637};
638
639static const struct attribute_group bmp280_attrs_group = {
640 .attrs = bmp280_attributes,
641};
642
Vlad Dogarud5c94562014-10-21 11:09:58 +0300643static const struct iio_info bmp280_info = {
644 .driver_module = THIS_MODULE,
645 .read_raw = &bmp280_read_raw,
Akinobu Mita62979902016-04-24 22:52:11 +0900646 .write_raw = &bmp280_write_raw,
647 .attrs = &bmp280_attrs_group,
Vlad Dogarud5c94562014-10-21 11:09:58 +0300648};
649
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900650static int bmp280_chip_config(struct bmp280_data *data)
Vlad Dogarud5c94562014-10-21 11:09:58 +0300651{
652 int ret;
Akinobu Mita62979902016-04-24 22:52:11 +0900653 u8 osrs = BMP280_OSRS_TEMP_X(data->oversampling_temp + 1) |
654 BMP280_OSRS_PRESS_X(data->oversampling_press + 1);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300655
656 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
657 BMP280_OSRS_TEMP_MASK |
658 BMP280_OSRS_PRESS_MASK |
659 BMP280_MODE_MASK,
Akinobu Mita62979902016-04-24 22:52:11 +0900660 osrs | BMP280_MODE_NORMAL);
Vlad Dogarud5c94562014-10-21 11:09:58 +0300661 if (ret < 0) {
662 dev_err(&data->client->dev,
Hartmut Knaack44cf3792014-12-19 23:59:25 +0100663 "failed to write ctrl_meas register\n");
Vlad Dogarud5c94562014-10-21 11:09:58 +0300664 return ret;
665 }
666
667 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
668 BMP280_FILTER_MASK,
669 BMP280_FILTER_4X);
670 if (ret < 0) {
671 dev_err(&data->client->dev,
672 "failed to write config register\n");
673 return ret;
674 }
675
676 return ret;
677}
678
Akinobu Mita62979902016-04-24 22:52:11 +0900679static const int bmp280_oversampling_avail[] = { 1, 2, 4, 8, 16 };
680
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900681static const struct bmp280_chip_info bmp280_chip_info = {
682 .regmap_config = &bmp280_regmap_config,
Akinobu Mita62979902016-04-24 22:52:11 +0900683
684 .oversampling_temp_avail = bmp280_oversampling_avail,
685 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
686
687 .oversampling_press_avail = bmp280_oversampling_avail,
688 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
689
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900690 .chip_config = bmp280_chip_config,
691 .read_temp = bmp280_read_temp,
692 .read_press = bmp280_read_press,
693};
694
Matt Ranostay14beaa82016-05-04 22:57:30 -0700695static int bme280_chip_config(struct bmp280_data *data)
696{
697 int ret = bmp280_chip_config(data);
698 u8 osrs = BMP280_OSRS_HUMIDITIY_X(data->oversampling_humid + 1);
699
700 if (ret < 0)
701 return ret;
702
703 return regmap_update_bits(data->regmap, BMP280_REG_CTRL_HUMIDITY,
704 BMP280_OSRS_HUMIDITY_MASK, osrs);
705}
706
707static const struct bmp280_chip_info bme280_chip_info = {
708 .regmap_config = &bmp280_regmap_config,
709
710 .oversampling_temp_avail = bmp280_oversampling_avail,
711 .num_oversampling_temp_avail = ARRAY_SIZE(bmp280_oversampling_avail),
712
713 .oversampling_press_avail = bmp280_oversampling_avail,
714 .num_oversampling_press_avail = ARRAY_SIZE(bmp280_oversampling_avail),
715
716 .oversampling_humid_avail = bmp280_oversampling_avail,
717 .num_oversampling_humid_avail = ARRAY_SIZE(bmp280_oversampling_avail),
718
719 .chip_config = bme280_chip_config,
720 .read_temp = bmp280_read_temp,
721 .read_press = bmp280_read_press,
722 .read_humid = bmp280_read_humid,
723};
724
725
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900726static bool bmp180_is_writeable_reg(struct device *dev, unsigned int reg)
727{
728 switch (reg) {
729 case BMP280_REG_CTRL_MEAS:
730 case BMP280_REG_RESET:
731 return true;
732 default:
733 return false;
734 };
735}
736
737static bool bmp180_is_volatile_reg(struct device *dev, unsigned int reg)
738{
739 switch (reg) {
740 case BMP180_REG_OUT_XLSB:
741 case BMP180_REG_OUT_LSB:
742 case BMP180_REG_OUT_MSB:
743 case BMP280_REG_CTRL_MEAS:
744 return true;
745 default:
746 return false;
747 }
748}
749
750static const struct regmap_config bmp180_regmap_config = {
751 .reg_bits = 8,
752 .val_bits = 8,
753
754 .max_register = BMP180_REG_OUT_XLSB,
755 .cache_type = REGCACHE_RBTREE,
756
757 .writeable_reg = bmp180_is_writeable_reg,
758 .volatile_reg = bmp180_is_volatile_reg,
759};
760
761static int bmp180_measure(struct bmp280_data *data, u8 ctrl_meas)
762{
763 int ret;
764 const int conversion_time_max[] = { 4500, 7500, 13500, 25500 };
765 unsigned int delay_us;
766 unsigned int ctrl;
767
768 ret = regmap_write(data->regmap, BMP280_REG_CTRL_MEAS, ctrl_meas);
769 if (ret)
770 return ret;
771
772 if (ctrl_meas == BMP180_MEAS_TEMP)
773 delay_us = 4500;
774 else
Akinobu Mita62979902016-04-24 22:52:11 +0900775 delay_us = conversion_time_max[data->oversampling_press];
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900776
777 usleep_range(delay_us, delay_us + 1000);
778
779 ret = regmap_read(data->regmap, BMP280_REG_CTRL_MEAS, &ctrl);
780 if (ret)
781 return ret;
782
783 /* The value of this bit reset to "0" after conversion is complete */
784 if (ctrl & BMP180_MEAS_SCO)
785 return -EIO;
786
787 return 0;
788}
789
790static int bmp180_read_adc_temp(struct bmp280_data *data, int *val)
791{
792 int ret;
793 __be16 tmp = 0;
794
795 ret = bmp180_measure(data, BMP180_MEAS_TEMP);
796 if (ret)
797 return ret;
798
799 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 2);
800 if (ret)
801 return ret;
802
803 *val = be16_to_cpu(tmp);
804
805 return 0;
806}
807
808/*
809 * These enums are used for indexing into the array of calibration
810 * coefficients for BMP180.
811 */
812enum { AC1, AC2, AC3, AC4, AC5, AC6, B1, B2, MB, MC, MD };
813
814struct bmp180_calib {
815 s16 AC1;
816 s16 AC2;
817 s16 AC3;
818 u16 AC4;
819 u16 AC5;
820 u16 AC6;
821 s16 B1;
822 s16 B2;
823 s16 MB;
824 s16 MC;
825 s16 MD;
826};
827
828static int bmp180_read_calib(struct bmp280_data *data,
829 struct bmp180_calib *calib)
830{
831 int ret;
832 int i;
833 __be16 buf[BMP180_REG_CALIB_COUNT / 2];
834
835 ret = regmap_bulk_read(data->regmap, BMP180_REG_CALIB_START, buf,
836 sizeof(buf));
837
838 if (ret < 0)
839 return ret;
840
841 /* None of the words has the value 0 or 0xFFFF */
842 for (i = 0; i < ARRAY_SIZE(buf); i++) {
843 if (buf[i] == cpu_to_be16(0) || buf[i] == cpu_to_be16(0xffff))
844 return -EIO;
845 }
846
847 calib->AC1 = be16_to_cpu(buf[AC1]);
848 calib->AC2 = be16_to_cpu(buf[AC2]);
849 calib->AC3 = be16_to_cpu(buf[AC3]);
850 calib->AC4 = be16_to_cpu(buf[AC4]);
851 calib->AC5 = be16_to_cpu(buf[AC5]);
852 calib->AC6 = be16_to_cpu(buf[AC6]);
853 calib->B1 = be16_to_cpu(buf[B1]);
854 calib->B2 = be16_to_cpu(buf[B2]);
855 calib->MB = be16_to_cpu(buf[MB]);
856 calib->MC = be16_to_cpu(buf[MC]);
857 calib->MD = be16_to_cpu(buf[MD]);
858
859 return 0;
860}
861
862/*
863 * Returns temperature in DegC, resolution is 0.1 DegC.
864 * t_fine carries fine temperature as global value.
865 *
866 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
867 */
868static s32 bmp180_compensate_temp(struct bmp280_data *data, s32 adc_temp)
869{
870 int ret;
871 s32 x1, x2;
872 struct bmp180_calib calib;
873
874 ret = bmp180_read_calib(data, &calib);
875 if (ret < 0) {
876 dev_err(&data->client->dev,
877 "failed to read calibration coefficients\n");
878 return ret;
879 }
880
881 x1 = ((adc_temp - calib.AC6) * calib.AC5) >> 15;
882 x2 = (calib.MC << 11) / (x1 + calib.MD);
883 data->t_fine = x1 + x2;
884
885 return (data->t_fine + 8) >> 4;
886}
887
888static int bmp180_read_temp(struct bmp280_data *data, int *val)
889{
890 int ret;
891 s32 adc_temp, comp_temp;
892
893 ret = bmp180_read_adc_temp(data, &adc_temp);
894 if (ret)
895 return ret;
896
897 comp_temp = bmp180_compensate_temp(data, adc_temp);
898
899 /*
900 * val might be NULL if we're called by the read_press routine,
901 * who only cares about the carry over t_fine value.
902 */
903 if (val) {
904 *val = comp_temp * 100;
905 return IIO_VAL_INT;
906 }
907
908 return 0;
909}
910
911static int bmp180_read_adc_press(struct bmp280_data *data, int *val)
912{
913 int ret;
914 __be32 tmp = 0;
Akinobu Mita62979902016-04-24 22:52:11 +0900915 u8 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900916
917 ret = bmp180_measure(data, BMP180_MEAS_PRESS_X(oss));
918 if (ret)
919 return ret;
920
921 ret = regmap_bulk_read(data->regmap, BMP180_REG_OUT_MSB, (u8 *)&tmp, 3);
922 if (ret)
923 return ret;
924
925 *val = (be32_to_cpu(tmp) >> 8) >> (8 - oss);
926
927 return 0;
928}
929
930/*
931 * Returns pressure in Pa, resolution is 1 Pa.
932 *
933 * Taken from datasheet, Section 3.5, "Calculating pressure and temperature".
934 */
935static u32 bmp180_compensate_press(struct bmp280_data *data, s32 adc_press)
936{
937 int ret;
938 s32 x1, x2, x3, p;
939 s32 b3, b6;
940 u32 b4, b7;
Akinobu Mita62979902016-04-24 22:52:11 +0900941 s32 oss = data->oversampling_press;
Akinobu Mita6dba72e2016-04-24 22:52:10 +0900942 struct bmp180_calib calib;
943
944 ret = bmp180_read_calib(data, &calib);
945 if (ret < 0) {
946 dev_err(&data->client->dev,
947 "failed to read calibration coefficients\n");
948 return ret;
949 }
950
951 b6 = data->t_fine - 4000;
952 x1 = (calib.B2 * (b6 * b6 >> 12)) >> 11;
953 x2 = calib.AC2 * b6 >> 11;
954 x3 = x1 + x2;
955 b3 = ((((s32)calib.AC1 * 4 + x3) << oss) + 2) / 4;
956 x1 = calib.AC3 * b6 >> 13;
957 x2 = (calib.B1 * ((b6 * b6) >> 12)) >> 16;
958 x3 = (x1 + x2 + 2) >> 2;
959 b4 = calib.AC4 * (u32)(x3 + 32768) >> 15;
960 b7 = ((u32)adc_press - b3) * (50000 >> oss);
961 if (b7 < 0x80000000)
962 p = (b7 * 2) / b4;
963 else
964 p = (b7 / b4) * 2;
965
966 x1 = (p >> 8) * (p >> 8);
967 x1 = (x1 * 3038) >> 16;
968 x2 = (-7357 * p) >> 16;
969
970 return p + ((x1 + x2 + 3791) >> 4);
971}
972
973static int bmp180_read_press(struct bmp280_data *data,
974 int *val, int *val2)
975{
976 int ret;
977 s32 adc_press;
978 u32 comp_press;
979
980 /* Read and compensate temperature so we get a reading of t_fine. */
981 ret = bmp180_read_temp(data, NULL);
982 if (ret)
983 return ret;
984
985 ret = bmp180_read_adc_press(data, &adc_press);
986 if (ret)
987 return ret;
988
989 comp_press = bmp180_compensate_press(data, adc_press);
990
991 *val = comp_press;
992 *val2 = 1000;
993
994 return IIO_VAL_FRACTIONAL;
995}
996
997static int bmp180_chip_config(struct bmp280_data *data)
998{
999 return 0;
1000}
1001
Akinobu Mita62979902016-04-24 22:52:11 +09001002static const int bmp180_oversampling_temp_avail[] = { 1 };
1003static const int bmp180_oversampling_press_avail[] = { 1, 2, 4, 8 };
1004
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001005static const struct bmp280_chip_info bmp180_chip_info = {
1006 .regmap_config = &bmp180_regmap_config,
Akinobu Mita62979902016-04-24 22:52:11 +09001007
1008 .oversampling_temp_avail = bmp180_oversampling_temp_avail,
1009 .num_oversampling_temp_avail =
1010 ARRAY_SIZE(bmp180_oversampling_temp_avail),
1011
1012 .oversampling_press_avail = bmp180_oversampling_press_avail,
1013 .num_oversampling_press_avail =
1014 ARRAY_SIZE(bmp180_oversampling_press_avail),
1015
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001016 .chip_config = bmp180_chip_config,
1017 .read_temp = bmp180_read_temp,
1018 .read_press = bmp180_read_press,
1019};
1020
Vlad Dogarud5c94562014-10-21 11:09:58 +03001021static int bmp280_probe(struct i2c_client *client,
1022 const struct i2c_device_id *id)
1023{
1024 int ret;
1025 struct iio_dev *indio_dev;
1026 struct bmp280_data *data;
1027 unsigned int chip_id;
Linus Walleijc5842b42016-06-30 03:48:47 +02001028 struct gpio_desc *gpiod;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001029
1030 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
1031 if (!indio_dev)
1032 return -ENOMEM;
1033
Vlad Dogarud5c94562014-10-21 11:09:58 +03001034 data = iio_priv(indio_dev);
1035 mutex_init(&data->lock);
1036 data->client = client;
1037
1038 indio_dev->dev.parent = &client->dev;
1039 indio_dev->name = id->name;
1040 indio_dev->channels = bmp280_channels;
Vlad Dogarud5c94562014-10-21 11:09:58 +03001041 indio_dev->info = &bmp280_info;
1042 indio_dev->modes = INDIO_DIRECT_MODE;
1043
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001044 switch (id->driver_data) {
1045 case BMP180_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -07001046 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001047 data->chip_info = &bmp180_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +09001048 data->oversampling_press = ilog2(8);
1049 data->oversampling_temp = ilog2(1);
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001050 break;
1051 case BMP280_CHIP_ID:
Matt Ranostay14beaa82016-05-04 22:57:30 -07001052 indio_dev->num_channels = 2;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001053 data->chip_info = &bmp280_chip_info;
Akinobu Mita62979902016-04-24 22:52:11 +09001054 data->oversampling_press = ilog2(16);
1055 data->oversampling_temp = ilog2(2);
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001056 break;
Matt Ranostay14beaa82016-05-04 22:57:30 -07001057 case BME280_CHIP_ID:
1058 indio_dev->num_channels = 3;
1059 data->chip_info = &bme280_chip_info;
1060 data->oversampling_press = ilog2(16);
1061 data->oversampling_humid = ilog2(16);
1062 data->oversampling_temp = ilog2(2);
1063 break;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001064 default:
1065 return -EINVAL;
1066 }
1067
Linus Walleijc5842b42016-06-30 03:48:47 +02001068 /* Bring chip out of reset if there is an assigned GPIO line */
1069 gpiod = devm_gpiod_get(&client->dev, "reset", GPIOD_OUT_HIGH);
1070 /* Deassert the signal */
1071 if (!IS_ERR(gpiod)) {
1072 dev_info(&client->dev, "release reset\n");
1073 gpiod_set_value(gpiod, 0);
1074 }
1075
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001076 data->regmap = devm_regmap_init_i2c(client,
1077 data->chip_info->regmap_config);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001078 if (IS_ERR(data->regmap)) {
1079 dev_err(&client->dev, "failed to allocate register map\n");
1080 return PTR_ERR(data->regmap);
1081 }
1082
1083 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
1084 if (ret < 0)
1085 return ret;
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001086 if (chip_id != id->driver_data) {
Akinobu Mitaa3e5afe2016-04-28 23:39:53 +09001087 dev_err(&client->dev, "bad chip id. expected %lx got %x\n",
1088 id->driver_data, chip_id);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001089 return -EINVAL;
1090 }
1091
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001092 ret = data->chip_info->chip_config(data);
Vlad Dogarud5c94562014-10-21 11:09:58 +03001093 if (ret < 0)
1094 return ret;
1095
1096 return devm_iio_device_register(&client->dev, indio_dev);
1097}
1098
1099static const struct acpi_device_id bmp280_acpi_match[] = {
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001100 {"BMP0280", BMP280_CHIP_ID },
1101 {"BMP0180", BMP180_CHIP_ID },
1102 {"BMP0085", BMP180_CHIP_ID },
Matt Ranostay14beaa82016-05-04 22:57:30 -07001103 {"BME0280", BME280_CHIP_ID },
Vlad Dogarud5c94562014-10-21 11:09:58 +03001104 { },
1105};
1106MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
1107
Linus Walleij78f50272016-06-30 03:48:46 +02001108#ifdef CONFIG_OF
1109static const struct of_device_id bmp280_of_match[] = {
1110 { .compatible = "bosch,bme280", .data = (void *)BME280_CHIP_ID },
1111 { .compatible = "bosch,bmp280", .data = (void *)BMP280_CHIP_ID },
1112 { .compatible = "bosch,bmp180", .data = (void *)BMP180_CHIP_ID },
1113 { .compatible = "bosch,bmp085", .data = (void *)BMP180_CHIP_ID },
1114 { },
1115};
1116MODULE_DEVICE_TABLE(of, bmp280_of_match);
1117#else
1118#define bmp280_of_match NULL
1119#endif
1120
Vlad Dogarud5c94562014-10-21 11:09:58 +03001121static const struct i2c_device_id bmp280_id[] = {
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001122 {"bmp280", BMP280_CHIP_ID },
1123 {"bmp180", BMP180_CHIP_ID },
1124 {"bmp085", BMP180_CHIP_ID },
Matt Ranostay14beaa82016-05-04 22:57:30 -07001125 {"bme280", BME280_CHIP_ID },
Vlad Dogarud5c94562014-10-21 11:09:58 +03001126 { },
1127};
1128MODULE_DEVICE_TABLE(i2c, bmp280_id);
1129
1130static struct i2c_driver bmp280_driver = {
1131 .driver = {
1132 .name = "bmp280",
1133 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
Linus Walleij78f50272016-06-30 03:48:46 +02001134 .of_match_table = of_match_ptr(bmp280_of_match),
Vlad Dogarud5c94562014-10-21 11:09:58 +03001135 },
1136 .probe = bmp280_probe,
1137 .id_table = bmp280_id,
1138};
1139module_i2c_driver(bmp280_driver);
1140
1141MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
Akinobu Mita6dba72e2016-04-24 22:52:10 +09001142MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP180/BMP280 pressure and temperature sensor");
Vlad Dogarud5c94562014-10-21 11:09:58 +03001143MODULE_LICENSE("GPL v2");