Bluetooth: hci_uart: Use generic functionality from Broadcom module
[linux/fpc-iii.git] / drivers / iio / pressure / bmp280.c
blob7c623e2bd6336c0968254e73547de8945efe4a42
1 /*
2 * Copyright (c) 2014 Intel Corporation
4 * Driver for Bosch Sensortec BMP280 digital pressure sensor.
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.
12 #define pr_fmt(fmt) "bmp280: " fmt
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/acpi.h>
17 #include <linux/regmap.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
21 #define BMP280_REG_TEMP_XLSB 0xFC
22 #define BMP280_REG_TEMP_LSB 0xFB
23 #define BMP280_REG_TEMP_MSB 0xFA
24 #define BMP280_REG_PRESS_XLSB 0xF9
25 #define BMP280_REG_PRESS_LSB 0xF8
26 #define BMP280_REG_PRESS_MSB 0xF7
28 #define BMP280_REG_CONFIG 0xF5
29 #define BMP280_REG_CTRL_MEAS 0xF4
30 #define BMP280_REG_STATUS 0xF3
31 #define BMP280_REG_RESET 0xE0
32 #define BMP280_REG_ID 0xD0
34 #define BMP280_REG_COMP_TEMP_START 0x88
35 #define BMP280_COMP_TEMP_REG_COUNT 6
37 #define BMP280_REG_COMP_PRESS_START 0x8E
38 #define BMP280_COMP_PRESS_REG_COUNT 18
40 #define BMP280_FILTER_MASK (BIT(4) | BIT(3) | BIT(2))
41 #define BMP280_FILTER_OFF 0
42 #define BMP280_FILTER_2X BIT(2)
43 #define BMP280_FILTER_4X BIT(3)
44 #define BMP280_FILTER_8X (BIT(3) | BIT(2))
45 #define BMP280_FILTER_16X BIT(4)
47 #define BMP280_OSRS_TEMP_MASK (BIT(7) | BIT(6) | BIT(5))
48 #define BMP280_OSRS_TEMP_SKIP 0
49 #define BMP280_OSRS_TEMP_1X BIT(5)
50 #define BMP280_OSRS_TEMP_2X BIT(6)
51 #define BMP280_OSRS_TEMP_4X (BIT(6) | BIT(5))
52 #define BMP280_OSRS_TEMP_8X BIT(7)
53 #define BMP280_OSRS_TEMP_16X (BIT(7) | BIT(5))
55 #define BMP280_OSRS_PRESS_MASK (BIT(4) | BIT(3) | BIT(2))
56 #define BMP280_OSRS_PRESS_SKIP 0
57 #define BMP280_OSRS_PRESS_1X BIT(2)
58 #define BMP280_OSRS_PRESS_2X BIT(3)
59 #define BMP280_OSRS_PRESS_4X (BIT(3) | BIT(2))
60 #define BMP280_OSRS_PRESS_8X BIT(4)
61 #define BMP280_OSRS_PRESS_16X (BIT(4) | BIT(2))
63 #define BMP280_MODE_MASK (BIT(1) | BIT(0))
64 #define BMP280_MODE_SLEEP 0
65 #define BMP280_MODE_FORCED BIT(0)
66 #define BMP280_MODE_NORMAL (BIT(1) | BIT(0))
68 #define BMP280_CHIP_ID 0x58
69 #define BMP280_SOFT_RESET_VAL 0xB6
71 struct bmp280_data {
72 struct i2c_client *client;
73 struct mutex lock;
74 struct regmap *regmap;
77 * Carryover value from temperature conversion, used in pressure
78 * calculation.
80 s32 t_fine;
84 * These enums are used for indexing into the array of compensation
85 * parameters.
87 enum { T1, T2, T3 };
88 enum { P1, P2, P3, P4, P5, P6, P7, P8, P9 };
90 static const struct iio_chan_spec bmp280_channels[] = {
92 .type = IIO_PRESSURE,
93 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
96 .type = IIO_TEMP,
97 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
101 static bool bmp280_is_writeable_reg(struct device *dev, unsigned int reg)
103 switch (reg) {
104 case BMP280_REG_CONFIG:
105 case BMP280_REG_CTRL_MEAS:
106 case BMP280_REG_RESET:
107 return true;
108 default:
109 return false;
113 static bool bmp280_is_volatile_reg(struct device *dev, unsigned int reg)
115 switch (reg) {
116 case BMP280_REG_TEMP_XLSB:
117 case BMP280_REG_TEMP_LSB:
118 case BMP280_REG_TEMP_MSB:
119 case BMP280_REG_PRESS_XLSB:
120 case BMP280_REG_PRESS_LSB:
121 case BMP280_REG_PRESS_MSB:
122 case BMP280_REG_STATUS:
123 return true;
124 default:
125 return false;
129 static const struct regmap_config bmp280_regmap_config = {
130 .reg_bits = 8,
131 .val_bits = 8,
133 .max_register = BMP280_REG_TEMP_XLSB,
134 .cache_type = REGCACHE_RBTREE,
136 .writeable_reg = bmp280_is_writeable_reg,
137 .volatile_reg = bmp280_is_volatile_reg,
141 * Returns temperature in DegC, resolution is 0.01 DegC. Output value of
142 * "5123" equals 51.23 DegC. t_fine carries fine temperature as global
143 * value.
145 * Taken from datasheet, Section 3.11.3, "Compensation formula".
147 static s32 bmp280_compensate_temp(struct bmp280_data *data,
148 s32 adc_temp)
150 int ret;
151 s32 var1, var2;
152 __le16 buf[BMP280_COMP_TEMP_REG_COUNT / 2];
154 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_TEMP_START,
155 buf, BMP280_COMP_TEMP_REG_COUNT);
156 if (ret < 0) {
157 dev_err(&data->client->dev,
158 "failed to read temperature calibration parameters\n");
159 return ret;
163 * The double casts are necessary because le16_to_cpu returns an
164 * unsigned 16-bit value. Casting that value directly to a
165 * signed 32-bit will not do proper sign extension.
167 * Conversely, T1 and P1 are unsigned values, so they can be
168 * cast straight to the larger type.
170 var1 = (((adc_temp >> 3) - ((s32)le16_to_cpu(buf[T1]) << 1)) *
171 ((s32)(s16)le16_to_cpu(buf[T2]))) >> 11;
172 var2 = (((((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1]))) *
173 ((adc_temp >> 4) - ((s32)le16_to_cpu(buf[T1])))) >> 12) *
174 ((s32)(s16)le16_to_cpu(buf[T3]))) >> 14;
176 return (data->t_fine * 5 + 128) >> 8;
180 * Returns pressure in Pa as unsigned 32 bit integer in Q24.8 format (24
181 * integer bits and 8 fractional bits). Output value of "24674867"
182 * represents 24674867/256 = 96386.2 Pa = 963.862 hPa
184 * Taken from datasheet, Section 3.11.3, "Compensation formula".
186 static u32 bmp280_compensate_press(struct bmp280_data *data,
187 s32 adc_press)
189 int ret;
190 s64 var1, var2, p;
191 __le16 buf[BMP280_COMP_PRESS_REG_COUNT / 2];
193 ret = regmap_bulk_read(data->regmap, BMP280_REG_COMP_PRESS_START,
194 buf, BMP280_COMP_PRESS_REG_COUNT);
195 if (ret < 0) {
196 dev_err(&data->client->dev,
197 "failed to read pressure calibration parameters\n");
198 return ret;
201 var1 = ((s64)data->t_fine) - 128000;
202 var2 = var1 * var1 * (s64)(s16)le16_to_cpu(buf[P6]);
203 var2 += (var1 * (s64)(s16)le16_to_cpu(buf[P5])) << 17;
204 var2 += ((s64)(s16)le16_to_cpu(buf[P4])) << 35;
205 var1 = ((var1 * var1 * (s64)(s16)le16_to_cpu(buf[P3])) >> 8) +
206 ((var1 * (s64)(s16)le16_to_cpu(buf[P2])) << 12);
207 var1 = ((((s64)1) << 47) + var1) * ((s64)le16_to_cpu(buf[P1])) >> 33;
209 if (var1 == 0)
210 return 0;
212 p = ((((s64)1048576 - adc_press) << 31) - var2) * 3125;
213 p = div64_s64(p, var1);
214 var1 = (((s64)(s16)le16_to_cpu(buf[P9])) * (p >> 13) * (p >> 13)) >> 25;
215 var2 = (((s64)(s16)le16_to_cpu(buf[P8])) * p) >> 19;
216 p = ((p + var1 + var2) >> 8) + (((s64)(s16)le16_to_cpu(buf[P7])) << 4);
218 return (u32)p;
221 static int bmp280_read_temp(struct bmp280_data *data,
222 int *val)
224 int ret;
225 __be32 tmp = 0;
226 s32 adc_temp, comp_temp;
228 ret = regmap_bulk_read(data->regmap, BMP280_REG_TEMP_MSB,
229 (u8 *) &tmp, 3);
230 if (ret < 0) {
231 dev_err(&data->client->dev, "failed to read temperature\n");
232 return ret;
235 adc_temp = be32_to_cpu(tmp) >> 12;
236 comp_temp = bmp280_compensate_temp(data, adc_temp);
239 * val might be NULL if we're called by the read_press routine,
240 * who only cares about the carry over t_fine value.
242 if (val) {
243 *val = comp_temp * 10;
244 return IIO_VAL_INT;
247 return 0;
250 static int bmp280_read_press(struct bmp280_data *data,
251 int *val, int *val2)
253 int ret;
254 __be32 tmp = 0;
255 s32 adc_press;
256 u32 comp_press;
258 /* Read and compensate temperature so we get a reading of t_fine. */
259 ret = bmp280_read_temp(data, NULL);
260 if (ret < 0)
261 return ret;
263 ret = regmap_bulk_read(data->regmap, BMP280_REG_PRESS_MSB,
264 (u8 *) &tmp, 3);
265 if (ret < 0) {
266 dev_err(&data->client->dev, "failed to read pressure\n");
267 return ret;
270 adc_press = be32_to_cpu(tmp) >> 12;
271 comp_press = bmp280_compensate_press(data, adc_press);
273 *val = comp_press;
274 *val2 = 256000;
276 return IIO_VAL_FRACTIONAL;
279 static int bmp280_read_raw(struct iio_dev *indio_dev,
280 struct iio_chan_spec const *chan,
281 int *val, int *val2, long mask)
283 int ret;
284 struct bmp280_data *data = iio_priv(indio_dev);
286 mutex_lock(&data->lock);
288 switch (mask) {
289 case IIO_CHAN_INFO_PROCESSED:
290 switch (chan->type) {
291 case IIO_PRESSURE:
292 ret = bmp280_read_press(data, val, val2);
293 break;
294 case IIO_TEMP:
295 ret = bmp280_read_temp(data, val);
296 break;
297 default:
298 ret = -EINVAL;
299 break;
301 break;
302 default:
303 ret = -EINVAL;
304 break;
307 mutex_unlock(&data->lock);
309 return ret;
312 static const struct iio_info bmp280_info = {
313 .driver_module = THIS_MODULE,
314 .read_raw = &bmp280_read_raw,
317 static int bmp280_chip_init(struct bmp280_data *data)
319 int ret;
321 ret = regmap_update_bits(data->regmap, BMP280_REG_CTRL_MEAS,
322 BMP280_OSRS_TEMP_MASK |
323 BMP280_OSRS_PRESS_MASK |
324 BMP280_MODE_MASK,
325 BMP280_OSRS_TEMP_2X |
326 BMP280_OSRS_PRESS_16X |
327 BMP280_MODE_NORMAL);
328 if (ret < 0) {
329 dev_err(&data->client->dev,
330 "failed to write ctrl_meas register\n");
331 return ret;
334 ret = regmap_update_bits(data->regmap, BMP280_REG_CONFIG,
335 BMP280_FILTER_MASK,
336 BMP280_FILTER_4X);
337 if (ret < 0) {
338 dev_err(&data->client->dev,
339 "failed to write config register\n");
340 return ret;
343 return ret;
346 static int bmp280_probe(struct i2c_client *client,
347 const struct i2c_device_id *id)
349 int ret;
350 struct iio_dev *indio_dev;
351 struct bmp280_data *data;
352 unsigned int chip_id;
354 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
355 if (!indio_dev)
356 return -ENOMEM;
358 data = iio_priv(indio_dev);
359 mutex_init(&data->lock);
360 data->client = client;
362 indio_dev->dev.parent = &client->dev;
363 indio_dev->name = id->name;
364 indio_dev->channels = bmp280_channels;
365 indio_dev->num_channels = ARRAY_SIZE(bmp280_channels);
366 indio_dev->info = &bmp280_info;
367 indio_dev->modes = INDIO_DIRECT_MODE;
369 data->regmap = devm_regmap_init_i2c(client, &bmp280_regmap_config);
370 if (IS_ERR(data->regmap)) {
371 dev_err(&client->dev, "failed to allocate register map\n");
372 return PTR_ERR(data->regmap);
375 ret = regmap_read(data->regmap, BMP280_REG_ID, &chip_id);
376 if (ret < 0)
377 return ret;
378 if (chip_id != BMP280_CHIP_ID) {
379 dev_err(&client->dev, "bad chip id. expected %x got %x\n",
380 BMP280_CHIP_ID, chip_id);
381 return -EINVAL;
384 ret = bmp280_chip_init(data);
385 if (ret < 0)
386 return ret;
388 return devm_iio_device_register(&client->dev, indio_dev);
391 static const struct acpi_device_id bmp280_acpi_match[] = {
392 {"BMP0280", 0},
393 { },
395 MODULE_DEVICE_TABLE(acpi, bmp280_acpi_match);
397 static const struct i2c_device_id bmp280_id[] = {
398 {"bmp280", 0},
399 { },
401 MODULE_DEVICE_TABLE(i2c, bmp280_id);
403 static struct i2c_driver bmp280_driver = {
404 .driver = {
405 .name = "bmp280",
406 .acpi_match_table = ACPI_PTR(bmp280_acpi_match),
408 .probe = bmp280_probe,
409 .id_table = bmp280_id,
411 module_i2c_driver(bmp280_driver);
413 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
414 MODULE_DESCRIPTION("Driver for Bosch Sensortec BMP280 pressure and temperature sensor");
415 MODULE_LICENSE("GPL v2");