blob: 6f572bc21879a61326825cffa913aa37216d319e [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>
53
Eric Anderssone939ca02012-04-09 22:16:15 +020054#define BMP085_NAME "bmp085"
Christoph Mair5bf1d292010-08-09 17:20:28 -070055#define BMP085_I2C_ADDRESS 0x77
56#define BMP085_CHIP_ID 0x55
57
58#define BMP085_CALIBRATION_DATA_START 0xAA
59#define BMP085_CALIBRATION_DATA_LENGTH 11 /* 16 bit values */
60#define BMP085_CHIP_ID_REG 0xD0
Christoph Mair5bf1d292010-08-09 17:20:28 -070061#define BMP085_CTRL_REG 0xF4
62#define BMP085_TEMP_MEASUREMENT 0x2E
63#define BMP085_PRESSURE_MEASUREMENT 0x34
64#define BMP085_CONVERSION_REGISTER_MSB 0xF6
65#define BMP085_CONVERSION_REGISTER_LSB 0xF7
66#define BMP085_CONVERSION_REGISTER_XLSB 0xF8
67#define BMP085_TEMP_CONVERSION_TIME 5
68
Christoph Mair5bf1d292010-08-09 17:20:28 -070069static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS,
70 I2C_CLIENT_END };
71
72struct bmp085_calibration_data {
73 s16 AC1, AC2, AC3;
74 u16 AC4, AC5, AC6;
75 s16 B1, B2;
76 s16 MB, MC, MD;
77};
78
Christoph Mair5bf1d292010-08-09 17:20:28 -070079struct bmp085_data {
Eric Anderssone939ca02012-04-09 22:16:15 +020080 struct i2c_client *client;
81 struct mutex lock;
82 struct bmp085_calibration_data calibration;
83 u8 oversampling_setting;
84 u32 raw_temperature;
85 u32 raw_pressure;
86 u32 temp_measurement_period;
Bernhard Walleb2222582012-02-25 10:28:12 +010087 unsigned long last_temp_measurement;
Eric Anderssone939ca02012-04-09 22:16:15 +020088 s32 b6; /* calculated temperature correction coefficient */
Christoph Mair5bf1d292010-08-09 17:20:28 -070089};
90
Christoph Mair5bf1d292010-08-09 17:20:28 -070091static s32 bmp085_read_calibration_data(struct i2c_client *client)
92{
93 u16 tmp[BMP085_CALIBRATION_DATA_LENGTH];
94 struct bmp085_data *data = i2c_get_clientdata(client);
95 struct bmp085_calibration_data *cali = &(data->calibration);
96 s32 status = i2c_smbus_read_i2c_block_data(client,
97 BMP085_CALIBRATION_DATA_START,
Eric Anderssone939ca02012-04-09 22:16:15 +020098 (BMP085_CALIBRATION_DATA_LENGTH << 1),
Christoph Mair5bf1d292010-08-09 17:20:28 -070099 (u8 *)tmp);
100 if (status < 0)
101 return status;
102
Eric Anderssone939ca02012-04-09 22:16:15 +0200103 if (status != (BMP085_CALIBRATION_DATA_LENGTH << 1))
Christoph Mair5bf1d292010-08-09 17:20:28 -0700104 return -EIO;
105
106 cali->AC1 = be16_to_cpu(tmp[0]);
107 cali->AC2 = be16_to_cpu(tmp[1]);
108 cali->AC3 = be16_to_cpu(tmp[2]);
109 cali->AC4 = be16_to_cpu(tmp[3]);
110 cali->AC5 = be16_to_cpu(tmp[4]);
111 cali->AC6 = be16_to_cpu(tmp[5]);
112 cali->B1 = be16_to_cpu(tmp[6]);
113 cali->B2 = be16_to_cpu(tmp[7]);
114 cali->MB = be16_to_cpu(tmp[8]);
115 cali->MC = be16_to_cpu(tmp[9]);
116 cali->MD = be16_to_cpu(tmp[10]);
117 return 0;
118}
119
Christoph Mair5bf1d292010-08-09 17:20:28 -0700120static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
121{
122 u16 tmp;
123 s32 status;
124
125 mutex_lock(&data->lock);
126 status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
127 BMP085_TEMP_MEASUREMENT);
Eric Anderssone939ca02012-04-09 22:16:15 +0200128 if (status < 0) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700129 dev_err(&data->client->dev,
130 "Error while requesting temperature measurement.\n");
131 goto exit;
132 }
133 msleep(BMP085_TEMP_CONVERSION_TIME);
134
135 status = i2c_smbus_read_i2c_block_data(data->client,
136 BMP085_CONVERSION_REGISTER_MSB, sizeof(tmp), (u8 *)&tmp);
137 if (status < 0)
138 goto exit;
139 if (status != sizeof(tmp)) {
140 dev_err(&data->client->dev,
141 "Error while reading temperature measurement result\n");
142 status = -EIO;
143 goto exit;
144 }
145 data->raw_temperature = be16_to_cpu(tmp);
146 data->last_temp_measurement = jiffies;
147 status = 0; /* everything ok, return 0 */
148
149exit:
150 mutex_unlock(&data->lock);
151 return status;
152}
153
154static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
155{
156 u32 tmp = 0;
157 s32 status;
158
159 mutex_lock(&data->lock);
160 status = i2c_smbus_write_byte_data(data->client, BMP085_CTRL_REG,
Eric Anderssone939ca02012-04-09 22:16:15 +0200161 BMP085_PRESSURE_MEASUREMENT +
162 (data->oversampling_setting << 6));
163 if (status < 0) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700164 dev_err(&data->client->dev,
165 "Error while requesting pressure measurement.\n");
166 goto exit;
167 }
168
169 /* wait for the end of conversion */
170 msleep(2+(3 << data->oversampling_setting));
171
172 /* copy data into a u32 (4 bytes), but skip the first byte. */
173 status = i2c_smbus_read_i2c_block_data(data->client,
174 BMP085_CONVERSION_REGISTER_MSB, 3, ((u8 *)&tmp)+1);
175 if (status < 0)
176 goto exit;
177 if (status != 3) {
178 dev_err(&data->client->dev,
179 "Error while reading pressure measurement results\n");
180 status = -EIO;
181 goto exit;
182 }
183 data->raw_pressure = be32_to_cpu((tmp));
184 data->raw_pressure >>= (8-data->oversampling_setting);
185 status = 0; /* everything ok, return 0 */
186
187exit:
188 mutex_unlock(&data->lock);
189 return status;
190}
191
Christoph Mair5bf1d292010-08-09 17:20:28 -0700192/*
193 * This function starts the temperature measurement and returns the value
194 * in tenth of a degree celsius.
195 */
196static s32 bmp085_get_temperature(struct bmp085_data *data, int *temperature)
197{
198 struct bmp085_calibration_data *cali = &data->calibration;
199 long x1, x2;
200 int status;
201
202 status = bmp085_update_raw_temperature(data);
Eric Anderssone939ca02012-04-09 22:16:15 +0200203 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700204 goto exit;
205
206 x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
207 x2 = (cali->MC << 11) / (x1 + cali->MD);
208 data->b6 = x1 + x2 - 4000;
209 /* if NULL just update b6. Used for pressure only measurements */
210 if (temperature != NULL)
211 *temperature = (x1+x2+8) >> 4;
212
213exit:
Jesper Juhlf80ea662011-12-17 23:52:27 +0100214 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700215}
216
217/*
218 * This function starts the pressure measurement and returns the value
219 * in millibar. Since the pressure depends on the ambient temperature,
Eric Anderssone939ca02012-04-09 22:16:15 +0200220 * a temperature measurement is executed according to the given temperature
221 * measurement period (default is 1 sec boundary). This period could vary
222 * and needs to be adjusted according to the sensor environment, i.e. if big
223 * temperature variations then the temperature needs to be read out often.
Christoph Mair5bf1d292010-08-09 17:20:28 -0700224 */
225static s32 bmp085_get_pressure(struct bmp085_data *data, int *pressure)
226{
227 struct bmp085_calibration_data *cali = &data->calibration;
228 s32 x1, x2, x3, b3;
229 u32 b4, b7;
230 s32 p;
231 int status;
232
233 /* alt least every second force an update of the ambient temperature */
Eric Anderssone939ca02012-04-09 22:16:15 +0200234 if ((data->last_temp_measurement == 0) ||
235 time_is_before_jiffies(data->last_temp_measurement + 1*HZ)) {
Christoph Mair5bf1d292010-08-09 17:20:28 -0700236 status = bmp085_get_temperature(data, NULL);
Eric Anderssone939ca02012-04-09 22:16:15 +0200237 if (status < 0)
238 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700239 }
240
241 status = bmp085_update_raw_pressure(data);
Eric Anderssone939ca02012-04-09 22:16:15 +0200242 if (status < 0)
243 return status;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700244
245 x1 = (data->b6 * data->b6) >> 12;
246 x1 *= cali->B2;
247 x1 >>= 11;
248
249 x2 = cali->AC2 * data->b6;
250 x2 >>= 11;
251
252 x3 = x1 + x2;
253
254 b3 = (((((s32)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
255 b3 >>= 2;
256
257 x1 = (cali->AC3 * data->b6) >> 13;
258 x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
259 x3 = (x1 + x2 + 2) >> 2;
260 b4 = (cali->AC4 * (u32)(x3 + 32768)) >> 15;
261
262 b7 = ((u32)data->raw_pressure - b3) *
263 (50000 >> data->oversampling_setting);
264 p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
265
266 x1 = p >> 8;
267 x1 *= x1;
268 x1 = (x1 * 3038) >> 16;
269 x2 = (-7357 * p) >> 16;
270 p += (x1 + x2 + 3791) >> 4;
271
272 *pressure = p;
273
Eric Anderssone939ca02012-04-09 22:16:15 +0200274 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700275}
276
277/*
278 * This function sets the chip-internal oversampling. Valid values are 0..3.
279 * The chip will use 2^oversampling samples for internal averaging.
280 * This influences the measurement time and the accuracy; larger values
Eric Anderssone939ca02012-04-09 22:16:15 +0200281 * increase both. The datasheet gives an overview on how measurement time,
Christoph Mair5bf1d292010-08-09 17:20:28 -0700282 * accuracy and noise correlate.
283 */
284static void bmp085_set_oversampling(struct bmp085_data *data,
285 unsigned char oversampling)
286{
287 if (oversampling > 3)
288 oversampling = 3;
289 data->oversampling_setting = oversampling;
290}
291
292/*
293 * Returns the currently selected oversampling. Range: 0..3
294 */
295static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
296{
297 return data->oversampling_setting;
298}
299
300/* sysfs callbacks */
301static ssize_t set_oversampling(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
304{
305 struct i2c_client *client = to_i2c_client(dev);
306 struct bmp085_data *data = i2c_get_clientdata(client);
307 unsigned long oversampling;
Eric Anderssone939ca02012-04-09 22:16:15 +0200308 int err = kstrtoul(buf, 10, &oversampling);
309
310 if (err == 0) {
311 mutex_lock(&data->lock);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700312 bmp085_set_oversampling(data, oversampling);
Eric Anderssone939ca02012-04-09 22:16:15 +0200313 mutex_unlock(&data->lock);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700314 return count;
315 }
Eric Anderssone939ca02012-04-09 22:16:15 +0200316
317 return err;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700318}
319
320static ssize_t show_oversampling(struct device *dev,
321 struct device_attribute *attr, char *buf)
322{
323 struct i2c_client *client = to_i2c_client(dev);
324 struct bmp085_data *data = i2c_get_clientdata(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200325
Christoph Mair5bf1d292010-08-09 17:20:28 -0700326 return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
327}
328static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
329 show_oversampling, set_oversampling);
330
331
332static ssize_t show_temperature(struct device *dev,
333 struct device_attribute *attr, char *buf)
334{
335 int temperature;
336 int status;
337 struct i2c_client *client = to_i2c_client(dev);
338 struct bmp085_data *data = i2c_get_clientdata(client);
339
340 status = bmp085_get_temperature(data, &temperature);
Eric Anderssone939ca02012-04-09 22:16:15 +0200341 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700342 return status;
343 else
344 return sprintf(buf, "%d\n", temperature);
345}
346static DEVICE_ATTR(temp0_input, S_IRUGO, show_temperature, NULL);
347
348
349static ssize_t show_pressure(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
352 int pressure;
353 int status;
354 struct i2c_client *client = to_i2c_client(dev);
355 struct bmp085_data *data = i2c_get_clientdata(client);
356
357 status = bmp085_get_pressure(data, &pressure);
Eric Anderssone939ca02012-04-09 22:16:15 +0200358 if (status < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700359 return status;
360 else
361 return sprintf(buf, "%d\n", pressure);
362}
363static DEVICE_ATTR(pressure0_input, S_IRUGO, show_pressure, NULL);
364
365
366static struct attribute *bmp085_attributes[] = {
367 &dev_attr_temp0_input.attr,
368 &dev_attr_pressure0_input.attr,
369 &dev_attr_oversampling.attr,
370 NULL
371};
372
373static const struct attribute_group bmp085_attr_group = {
374 .attrs = bmp085_attributes,
375};
376
377static int bmp085_detect(struct i2c_client *client, struct i2c_board_info *info)
378{
379 if (client->addr != BMP085_I2C_ADDRESS)
380 return -ENODEV;
381
382 if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID)
383 return -ENODEV;
384
385 return 0;
386}
387
388static int bmp085_init_client(struct i2c_client *client)
389{
Christoph Mair5bf1d292010-08-09 17:20:28 -0700390 struct bmp085_data *data = i2c_get_clientdata(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200391 int status = bmp085_read_calibration_data(client);
392
393 if (status < 0)
394 return status;
395
Christoph Mair5bf1d292010-08-09 17:20:28 -0700396 data->client = client;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700397 data->last_temp_measurement = 0;
Eric Anderssone939ca02012-04-09 22:16:15 +0200398 data->temp_measurement_period = 1*HZ;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700399 data->oversampling_setting = 3;
400 mutex_init(&data->lock);
Eric Anderssone939ca02012-04-09 22:16:15 +0200401
402 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700403}
404
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700405static int __devinit bmp085_probe(struct i2c_client *client,
Eric Anderssone939ca02012-04-09 22:16:15 +0200406 const struct i2c_device_id *id)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700407{
408 struct bmp085_data *data;
409 int err = 0;
410
411 data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
412 if (!data) {
413 err = -ENOMEM;
414 goto exit;
415 }
416
Christoph Mair5bf1d292010-08-09 17:20:28 -0700417 i2c_set_clientdata(client, data);
418
419 /* Initialize the BMP085 chip */
420 err = bmp085_init_client(client);
Eric Anderssone939ca02012-04-09 22:16:15 +0200421 if (err < 0)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700422 goto exit_free;
423
424 /* Register sysfs hooks */
425 err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group);
426 if (err)
427 goto exit_free;
428
Eric Anderssone939ca02012-04-09 22:16:15 +0200429 dev_info(&client->dev, "Successfully initialized %s!\n", BMP085_NAME);
430
431 return 0;
Christoph Mair5bf1d292010-08-09 17:20:28 -0700432
433exit_free:
434 kfree(data);
435exit:
436 return err;
437}
438
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700439static int __devexit bmp085_remove(struct i2c_client *client)
Christoph Mair5bf1d292010-08-09 17:20:28 -0700440{
Eric Anderssone939ca02012-04-09 22:16:15 +0200441 struct bmp085_data *data = i2c_get_clientdata(client);
442
Christoph Mair5bf1d292010-08-09 17:20:28 -0700443 sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group);
Eric Anderssone939ca02012-04-09 22:16:15 +0200444 kfree(data);
445
Christoph Mair5bf1d292010-08-09 17:20:28 -0700446 return 0;
447}
448
449static const struct i2c_device_id bmp085_id[] = {
Eric Anderssone939ca02012-04-09 22:16:15 +0200450 { BMP085_NAME, 0 },
Christoph Mair5bf1d292010-08-09 17:20:28 -0700451 { }
452};
Axel Lin97e419a2011-03-04 17:36:22 -0800453MODULE_DEVICE_TABLE(i2c, bmp085_id);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700454
455static struct i2c_driver bmp085_driver = {
456 .driver = {
457 .owner = THIS_MODULE,
Eric Anderssone939ca02012-04-09 22:16:15 +0200458 .name = BMP085_NAME
Christoph Mair5bf1d292010-08-09 17:20:28 -0700459 },
460 .id_table = bmp085_id,
461 .probe = bmp085_probe,
Shubhrajyoti Datta45bff2e2011-03-22 16:33:57 -0700462 .remove = __devexit_p(bmp085_remove),
Christoph Mair5bf1d292010-08-09 17:20:28 -0700463
464 .detect = bmp085_detect,
465 .address_list = normal_i2c
466};
467
Axel Lina64fe2e2012-01-22 15:36:45 +0800468module_i2c_driver(bmp085_driver);
Christoph Mair5bf1d292010-08-09 17:20:28 -0700469
Eric Anderssone939ca02012-04-09 22:16:15 +0200470MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com>");
Christoph Mair5bf1d292010-08-09 17:20:28 -0700471MODULE_DESCRIPTION("BMP085 driver");
472MODULE_LICENSE("GPL");