blob: 6adde9e1a0d1d6992e78ddc679d57f3231d0430c [file] [log] [blame]
Christoph Mair5bf1d292010-08-09 17:20:28 -07001/* Copyright (c) 2010 Christoph Mair <christoph.mair@gmail.com>
Eric Anderssone939ca02012-04-09 22:16:15 +02002 * Copyright (c) 2012 Bosch Sensortec GmbH
3 * Copyright (c) 2012 Unixphere AB
4 *
5 * This driver supports the bmp085 digital barometric pressure
6 * and temperature sensor from Bosch Sensortec. The datasheet
7 * is available from their website:
8 * http://www.bosch-sensortec.com/content/language1/downloads/BST-BMP085-DS000-05.pdf
9 *
10 * A pressure measurement is issued by reading from pressure0_input.
11 * The return value ranges from 30000 to 110000 pascal with a resulution
12 * of 1 pascal (0.01 millibar) which enables measurements from 9000m above
13 * to 500m below sea level.
14 *
15 * The temperature can be read from temp0_input. Values range from
16 * -400 to 850 representing the ambient temperature in degree celsius
17 * multiplied by 10.The resolution is 0.1 celsius.
18 *
19 * Because ambient pressure is temperature dependent, a temperature
20 * measurement will be executed automatically even if the user is reading
21 * from pressure0_input. This happens if the last temperature measurement
22 * has been executed more then one second ago.
23 *
24 * To decrease RMS noise from pressure measurements, the bmp085 can
25 * autonomously calculate the average of up to eight samples. This is
26 * set up by writing to the oversampling sysfs file. Accepted values
27 * are 0, 1, 2 and 3. 2^x when x is the value written to this file
28 * specifies the number of samples used to calculate the ambient pressure.
29 * RMS noise is specified with six pascal (without averaging) and decreases
30 * down to 3 pascal when using an oversampling setting of 3.
31 *
32 * This program is free software; you can redistribute it and/or modify
33 * it under the terms of the GNU General Public License as published by
34 * the Free Software Foundation; either version 2 of the License, or
35 * (at your option) any later version.
36 *
37 * This program is distributed in the hope that it will be useful,
38 * but WITHOUT ANY WARRANTY; without even the implied warranty of
39 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 * GNU General Public License for more details.
41 *
42 * You should have received a copy of the GNU General Public License
43 * along with this program; if not, write to the Free Software
44 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
45 */
Christoph Mair5bf1d292010-08-09 17:20:28 -070046
47#include <linux/module.h>
Eric Anderssone939ca02012-04-09 22:16:15 +020048#include <linux/device.h>
Christoph Mair5bf1d292010-08-09 17:20:28 -070049#include <linux/init.h>
50#include <linux/i2c.h>
51#include <linux/slab.h>
52#include <linux/delay.h>
Eric Anderssonc5a86ab2012-04-09 22:16:16 +020053#include <linux/of.h>
Christoph Mair5bf1d292010-08-09 17:20:28 -070054
Eric Anderssone939ca02012-04-09 22:16:15 +020055#define BMP085_NAME "bmp085"
Christoph Mair5bf1d292010-08-09 17:20:28 -070056#define BMP085_I2C_ADDRESS 0x77
57#define BMP085_CHIP_ID 0x55
58
59#define BMP085_CALIBRATION_DATA_START 0xAA
60#define BMP085_CALIBRATION_DATA_LENGTH 11 /* 16 bit values */
61#define BMP085_CHIP_ID_REG 0xD0
Christoph Mair5bf1d292010-08-09 17:20:28 -070062#define BMP085_CTRL_REG 0xF4
63#define BMP085_TEMP_MEASUREMENT 0x2E
64#define BMP085_PRESSURE_MEASUREMENT 0x34
65#define BMP085_CONVERSION_REGISTER_MSB 0xF6
66#define BMP085_CONVERSION_REGISTER_LSB 0xF7
67#define BMP085_CONVERSION_REGISTER_XLSB 0xF8
68#define BMP085_TEMP_CONVERSION_TIME 5
69
Christoph Mair5bf1d292010-08-09 17:20:28 -070070static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS,
71 I2C_CLIENT_END };
72
73struct bmp085_calibration_data {
74 s16 AC1, AC2, AC3;
75 u16 AC4, AC5, AC6;
76 s16 B1, B2;
77 s16 MB, MC, MD;
78};
79
Christoph Mair5bf1d292010-08-09 17:20:28 -070080struct bmp085_data {
Eric Anderssone939ca02012-04-09 22:16:15 +020081 struct i2c_client *client;
82 struct mutex lock;
83 struct bmp085_calibration_data calibration;
84 u8 oversampling_setting;
85 u32 raw_temperature;
86 u32 raw_pressure;
87 u32 temp_measurement_period;
Bernhard Walleb2222582012-02-25 10:28:12 +010088 unsigned long last_temp_measurement;
Eric Anderssonc5a86ab2012-04-09 22:16:16 +020089 u8 chip_id;
Eric Anderssone939ca02012-04-09 22:16:15 +020090 s32 b6; /* calculated temperature correction coefficient */
Christoph Mair5bf1d292010-08-09 17:20:28 -070091};
92
Christoph Mair5bf1d292010-08-09 17:20:28 -070093static s32 bmp085_read_calibration_data(struct i2c_client *client)
94{
95 u16 tmp[BMP085_CALIBRATION_DATA_LENGTH];
96 struct bmp085_data *data = i2c_get_clientdata(client);
97 struct bmp085_calibration_data *cali = &(data->calibration);
98 s32 status = i2c_smbus_read_i2c_block_data(client,
99 BMP085_CALIBRATION_DATA_START,
Eric Anderssone939ca02012-04-09 22:16:15 +0200100 (BMP085_CALIBRATION_DATA_LENGTH << 1),
Christoph Mair5bf1d292010-08-09 17:20:28 -0700101 (u8 *)tmp);
102 if (status < 0)
103 return status;
104
Eric Anderssone939ca02012-04-09 22:16:15 +0200105 if (status != (BMP085_CALIBRATION_DATA_LENGTH << 1))
Christoph Mair5bf1d292010-08-09 17:20:28 -0700106 return -EIO;
107
108 cali->AC1 = be16_to_cpu(tmp[0]);
109 cali->AC2 = be16_to_cpu(tmp[1]);
110 cali->AC3 = be16_to_cpu(tmp[2]);
111 cali->AC4 = be16_to_cpu(tmp[3]);
112 cali->AC5 = be16_to_cpu(tmp[4]);
113 cali->AC6 = be16_to_cpu(tmp[5]);
114 cali->B1 = be16_to_cpu(tmp[6]);
115 cali->B2 = be16_to_cpu(tmp[7]);
116 cali->MB = be16_to_cpu(tmp[8]);
117 cali->MC = be16_to_cpu(tmp[9]);
118 cali->MD = be16_to_cpu(tmp[10]);
119 return 0;
120}
121
Christoph Mair5bf1d292010-08-09 17:20:28 -0700122static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
123{
124 u16 tmp;
125 s32 status;
126
127 mutex_lock(&data->lock);
128 status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
129 BMP085_TEMP_MEASUREMENT);
Eric Anderssone939ca02012-04-09 22:16:15 +0200130 if (status < 0) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700131 dev_err(&data->client->dev,
132 "Error while requesting temperature measurement.\n");
133 goto exit;
134 }
135 msleep(BMP085_TEMP_CONVERSION_TIME);
136
137 status = i2c_smbus_read_i2c_block_data(data->client,
138 BMP085_CONVERSION_REGISTER_MSB, sizeof(tmp), (u8 *)&tmp);
139 if (status < 0)
140 goto exit;
141 if (status != sizeof(tmp)) {
142 dev_err(&data->client->dev,
143 "Error while reading temperature measurement result\n");
144 status = -EIO;
145 goto exit;
146 }
147 data->raw_temperature = be16_to_cpu(tmp);
148 data->last_temp_measurement = jiffies;
149 status = 0; /* everything ok, return 0 */
150
151exit:
152 mutex_unlock(&data->lock);
153 return status;
154}
155
156static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
157{
158 u32 tmp = 0;
159 s32 status;
160
161 mutex_lock(&data->lock);
162 status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
Eric Anderssone939ca02012-04-09 22:16:15 +0200163 BMP085_PRESSURE_MEASUREMENT +
164 (data->oversampling_setting << 6));
165 if (status < 0) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700166 dev_err(&data->client->dev,
167 "Error while requesting pressure measurement.\n");
168 goto exit;
169 }
170
171 /* wait for the end of conversion */
172 msleep(2+(3 << data->oversampling_setting));
173
174 /* copy data into a u32 (4 bytes), but skip the first byte. */
175 status = i2c_smbus_read_i2c_block_data(data->client,
176 BMP085_CONVERSION_REGISTER_MSB, 3, ((u8 *)&tmp)+1);
177 if (status < 0)
178 goto exit;
179 if (status != 3) {
180 dev_err(&data->client->dev,
181 "Error while reading pressure measurement results\n");
182 status = -EIO;
183 goto exit;
184 }
185 data->raw_pressure = be32_to_cpu((tmp));
186 data->raw_pressure >>= (8-data->oversampling_setting);
187 status = 0; /* everything ok, return 0 */
188
189exit:
190 mutex_unlock(&data->lock);
191 return status;
192}
193
Christoph Mair5bf1d292010-08-09 17:20:28 -0700194/*
195 * This function starts the temperature measurement and returns the value
196 * in tenth of a degree celsius.
197 */
198static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature)
199{
200 struct bmp085_calibration_data *cali = &data->calibration;
201 long x1, x2;
202 int status;
203
204 status = bmp085_update_raw_temperature(data);
Eric Anderssone939ca02012-04-09 22:16:15 +0200205 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700206 goto exit;
207
208 x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
209 x2 = (cali->MC << 11) / (x1 + cali->MD);
210 data->b6 = x1 + x2 - 4000;
211 /* if NULL just update b6. Used for pressure only measurements */
212 if (temperature != NULL)
213 *temperature = (x1+x2+8) >> 4;
214
215exit:
Jesper Juhlf80ea662011-12-17 23:52:27 +0100216 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700217}
218
219/*
220 * This function starts the pressure measurement and returns the value
221 * in millibar. Since the pressure depends on the ambient temperature,
Eric Anderssone939ca02012-04-09 22:16:15 +0200222 * a temperature measurement is executed according to the given temperature
223 * measurement period (default is 1 sec boundary). This period could vary
224 * and needs to be adjusted according to the sensor environment, i.e. if big
225 * temperature variations then the temperature needs to be read out often.
Christoph Mair5bf1d292010-08-09 17:20:28 -0700226 */
227static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure)
228{
229 struct bmp085_calibration_data *cali = &data->calibration;
230 s32 x1, x2, x3, b3;
231 u32 b4, b7;
232 s32 p;
233 int status;
234
235 /* alt least every second force an update of the ambient temperature */
Eric Anderssone939ca02012-04-09 22:16:15 +0200236 if ((data->last_temp_measurement == 0) ||
237 time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700238 status = bmp085_get_temperature(data, NULL);
Eric Anderssone939ca02012-04-09 22:16:15 +0200239 if (status < 0)
240 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700241 }
242
243 status = bmp085_update_raw_pressure(data);
Eric Anderssone939ca02012-04-09 22:16:15 +0200244 if (status < 0)
245 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700246
247 x1 = (data->b6 * data->b6) >> 12;
248 x1 *= cali->B2;
249 x1 >>= 11;
250
251 x2 = cali->AC2 * data->b6;
252 x2 >>= 11;
253
254 x3 = x1 + x2;
255
256 b3 = (((((s32)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
257 b3 >>= 2;
258
259 x1 = (cali->AC3 * data->b6) >> 13;
260 x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
261 x3 = (x1 + x2 + 2) >> 2;
262 b4 = (cali->AC4 * (u32)(x3 + 32768)) >> 15;
263
264 b7 = ((u32)data->raw_pressure - b3) *
265 (50000 >> data->oversampling_setting);
266 p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
267
268 x1 = p >> 8;
269 x1 *= x1;
270 x1 = (x1 * 3038) >> 16;
271 x2 = (-7357 * p) >> 16;
272 p += (x1 + x2 + 3791) >> 4;
273
274 *pressure = p;
275
Eric Anderssone939ca02012-04-09 22:16:15 +0200276 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700277}
278
279/*
280 * This function sets the chip-internal oversampling. Valid values are 0..3.
281 * The chip will use 2^oversampling samples for internal averaging.
282 * This influences the measurement time and the accuracy; larger values
Eric Anderssone939ca02012-04-09 22:16:15 +0200283 * increase both. The datasheet gives an overview on how measurement time,
Christoph Mair5bf1d292010-08-09 17:20:28 -0700284 * accuracy and noise correlate.
285 */
286static void bmp085_set_oversampling(struct bmp085_data *data,
287 unsigned char oversampling)
288{
289 if (oversampling > 3)
290 oversampling = 3;
291 data->oversampling_setting = oversampling;
292}
293
294/*
295 * Returns the currently selected oversampling. Range: 0..3
296 */
297static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
298{
299 return data->oversampling_setting;
300}
301
302/* sysfs callbacks */
303static ssize_t set_oversampling(struct device *dev,
304 struct device_attribute *attr,
305 const char *buf, size_t count)
306{
307 struct i2c_client *client = to_i2c_client(dev);
308 struct bmp085_data *data = i2c_get_clientdata(client);
309 unsigned long oversampling;
Eric Anderssone939ca02012-04-09 22:16:15 +0200310 int err = kstrtoul(buf, 10, &oversampling);
311
312 if (err == 0) {
313 mutex_lock(&data->lock);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700314 bmp085_set_oversampling(data, oversampling);
Eric Anderssone939ca02012-04-09 22:16:15 +0200315 mutex_unlock(&data->lock);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700316 return count;
317 }
Eric Anderssone939ca02012-04-09 22:16:15 +0200318
319 return err;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700320}
321
322static ssize_t show_oversampling(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 struct i2c_client *client = to_i2c_client(dev);
326 struct bmp085_data *data = i2c_get_clientdata(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200327
Christoph Mair5bf1d292010-08-09 17:20:28 -0700328 return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
329}
330static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
331 show_oversampling, set_oversampling);
332
333
334static ssize_t show_temperature(struct device *dev,
335 struct device_attribute *attr, char *buf)
336{
337 int temperature;
338 int status;
339 struct i2c_client *client = to_i2c_client(dev);
340 struct bmp085_data *data = i2c_get_clientdata(client);
341
342 status = bmp085_get_temperature(data, &temperature);
Eric Anderssone939ca02012-04-09 22:16:15 +0200343 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700344 return status;
345 else
346 return sprintf(buf, "%d\n", temperature);
347}
348static DEVICE_ATTR(temp0_input, S_IRUGO, show_temperature, NULL);
349
350
351static ssize_t show_pressure(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 int pressure;
355 int status;
356 struct i2c_client *client = to_i2c_client(dev);
357 struct bmp085_data *data = i2c_get_clientdata(client);
358
359 status = bmp085_get_pressure(data, &pressure);
Eric Anderssone939ca02012-04-09 22:16:15 +0200360 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700361 return status;
362 else
363 return sprintf(buf, "%d\n", pressure);
364}
365static DEVICE_ATTR(pressure0_input, S_IRUGO, show_pressure, NULL);
366
367
368static struct attribute *bmp085_attributes[] = {
369 &dev_attr_temp0_input.attr,
370 &dev_attr_pressure0_input.attr,
371 &dev_attr_oversampling.attr,
372 NULL
373};
374
375static const struct attribute_group bmp085_attr_group = {
376 .attrs = bmp085_attributes,
377};
378
379static int bmp085_detect(struct i2c_client *client, struct i2c_board_info *info)
380{
381 if (client->addr != BMP085_I2C_ADDRESS)
382 return -ENODEV;
383
384 if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID)
385 return -ENODEV;
386
387 return 0;
388}
389
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200390static void __init bmp085_get_of_properties(struct i2c_client *client,
391 struct bmp085_data *data)
392{
393#ifdef CONFIG_OF
394 struct device_node *np = client->dev.of_node;
395 u32 prop;
396
397 if (!np)
398 return;
399
400 if (!of_property_read_u32(np, "chip-id", &prop))
401 data->chip_id = prop & 0xff;
402
403 if (!of_property_read_u32(np, "temp-measurement-period", &prop))
404 data->temp_measurement_period = (prop/100)*HZ;
405
406 if (!of_property_read_u32(np, "default-oversampling", &prop))
407 data->oversampling_setting = prop & 0xff;
408#endif
409}
410
Christoph Mair5bf1d292010-08-09 17:20:28 -0700411static int bmp085_init_client(struct i2c_client *client)
412{
Christoph Mair5bf1d292010-08-09 17:20:28 -0700413 struct bmp085_data *data = i2c_get_clientdata(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200414 int status = bmp085_read_calibration_data(client);
415
416 if (status < 0)
417 return status;
418
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200419 /* default settings */
Christoph Mair5bf1d292010-08-09 17:20:28 -0700420 data->client = client;
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200421 data->chip_id = BMP085_CHIP_ID;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700422 data->last_temp_measurement = 0;
Eric Anderssone939ca02012-04-09 22:16:15 +0200423 data->temp_measurement_period = 1*HZ;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700424 data->oversampling_setting = 3;
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200425
426 bmp085_get_of_properties(client, data);
427
Christoph Mair5bf1d292010-08-09 17:20:28 -0700428 mutex_init(&data->lock);
Eric Anderssone939ca02012-04-09 22:16:15 +0200429
430 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700431}
432
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700433static int __devinit bmp085_probe(struct i2c_client *client,
Eric Anderssone939ca02012-04-09 22:16:15 +0200434 const struct i2c_device_id *id)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700435{
436 struct bmp085_data *data;
437 int err = 0;
438
439 data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
440 if (!data) {
441 err = -ENOMEM;
442 goto exit;
443 }
444
Christoph Mair5bf1d292010-08-09 17:20:28 -0700445 i2c_set_clientdata(client, data);
446
447 /* Initialize the BMP085 chip */
448 err = bmp085_init_client(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200449 if (err < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700450 goto exit_free;
451
452 /* Register sysfs hooks */
453 err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group);
454 if (err)
455 goto exit_free;
456
Eric Anderssone939ca02012-04-09 22:16:15 +0200457 dev_info(&client->dev, "Successfully initialized %s!\n", BMP085_NAME);
458
459 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700460
461exit_free:
462 kfree(data);
463exit:
464 return err;
465}
466
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700467static int __devexit bmp085_remove(struct i2c_client *client)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700468{
Eric Anderssone939ca02012-04-09 22:16:15 +0200469 struct bmp085_data *data = i2c_get_clientdata(client);
470
Christoph Mair5bf1d292010-08-09 17:20:28 -0700471 sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group);
Eric Anderssone939ca02012-04-09 22:16:15 +0200472 kfree(data);
473
Christoph Mair5bf1d292010-08-09 17:20:28 -0700474 return 0;
475}
476
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200477static const struct of_device_id bmp085_of_match[] = {
478 { .compatible = "bosch,bmp085", },
479 { },
480};
481MODULE_DEVICE_TABLE(of, bmp085_of_match);
482
Christoph Mair5bf1d292010-08-09 17:20:28 -0700483static const struct i2c_device_id bmp085_id[] = {
Eric Anderssone939ca02012-04-09 22:16:15 +0200484 { BMP085_NAME, 0 },
Christoph Mair5bf1d292010-08-09 17:20:28 -0700485 { }
486};
Axel Lin97e419a2011-03-04 17:36:22 -0800487MODULE_DEVICE_TABLE(i2c, bmp085_id);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700488
489static struct i2c_driver bmp085_driver = {
490 .driver = {
491 .owner = THIS_MODULE,
Eric Anderssonc5a86ab2012-04-09 22:16:16 +0200492 .name = BMP085_NAME,
493 .of_match_table = bmp085_of_match
Christoph Mair5bf1d292010-08-09 17:20:28 -0700494 },
495 .id_table = bmp085_id,
496 .probe = bmp085_probe,
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700497 .remove = __devexit_p(bmp085_remove),
Christoph Mair5bf1d292010-08-09 17:20:28 -0700498
499 .detect = bmp085_detect,
500 .address_list = normal_i2c
501};
502
Axel Lina64fe2e2012-01-22 15:36:45 +0800503module_i2c_driver(bmp085_driver);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700504
Eric Anderssone939ca02012-04-09 22:16:15 +0200505MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com>");
Christoph Mair5bf1d292010-08-09 17:20:28 -0700506MODULE_DESCRIPTION("BMP085 driver");
507MODULE_LICENSE("GPL");