Create a simple demo application which uses the library.
[freerunner-navigation-board:bmp085.git] / kernel / bmp085.c
1 /*  Copyright (c) 2010  Christoph Mair <christoph.mair@gmail.com>
2
3     This program is free software; you can redistribute it and/or modify
4     it under the terms of the GNU General Public License as published by
5     the Free Software Foundation; either version 2 of the License, or
6     (at your option) any later version.
7
8     This program is distributed in the hope that it will be useful,
9     but WITHOUT ANY WARRANTY; without even the implied warranty of
10     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11     GNU General Public License for more details.
12
13     You should have received a copy of the GNU General Public License
14     along with this program; if not, write to the Free Software
15     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 */
17
18
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/i2c.h>
22 #include <linux/delay.h>
23
24
25
26 #define BMP085_I2C_ADDRESS              0x77
27
28 #define BMP085_CALIBRATION_DATA_START   0xAA
29 #define BMP085_CALIBRATION_DATA_LENGTH  22
30
31 #define BMP085_CHIP_ID_REG              0xD0
32 #define BMP085_VERSION_REG              0xD1
33
34 #define BMP085_CHIP_ID                  0x55 /* 85 */
35 #define BMP085_CLIENT_NAME              "bmp085"
36
37
38 /* Addresses to scan: 0x77 */
39 static const unsigned short normal_i2c[] = { BMP085_I2C_ADDRESS,
40                                                         I2C_CLIENT_END };
41
42 struct bmp085_calibration_data {
43         s16 AC1, AC2, AC3;
44         u16 AC4, AC5, AC6;
45         s16 B1, B2;
46         s16 MB, MC, MD;
47 };
48
49
50 /* Each client has this additional data */
51 struct bmp085_data {
52         struct i2c_client *client;
53         unsigned char version;
54         struct bmp085_calibration_data calibration;
55         unsigned long raw_temperature;
56         unsigned long raw_pressure;
57         unsigned char oversampling_setting;
58         unsigned long next_temp_measurement;
59         long b6; /* calculated temperature correction coefficient */
60 };
61
62
63 static void bmp085_init_client(struct i2c_client *client);
64
65
66 static s32 bmp085_read_calibration_data(struct i2c_client *client)
67 {
68         u8 tmp[BMP085_CALIBRATION_DATA_LENGTH];
69         struct bmp085_data *data = i2c_get_clientdata(client);
70         struct bmp085_calibration_data *cali = &(data->calibration);
71         s32 status = i2c_smbus_read_i2c_block_data(client,
72                                         BMP085_CALIBRATION_DATA_START,
73                                         BMP085_CALIBRATION_DATA_LENGTH, tmp);
74
75         cali->AC1 =  (tmp[0] << 8) | tmp[1];
76         cali->AC2 =  (tmp[2] << 8) | tmp[3];
77         cali->AC3 =  (tmp[4] << 8) | tmp[5];
78         cali->AC4 =  (tmp[6] << 8) | tmp[7];
79         cali->AC5 =  (tmp[8] << 8) | tmp[9];
80         cali->AC6 = (tmp[10] << 8) | tmp[11];
81         cali->B1 =  (tmp[12] << 8) | tmp[13];
82         cali->B2 =  (tmp[14] << 8) | tmp[15];
83         cali->MB =  (tmp[16] << 8) | tmp[17];
84         cali->MC =  (tmp[18] << 8) | tmp[19];
85         cali->MD =  (tmp[20] << 8) | tmp[21];
86         return status;
87 }
88
89
90 static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
91 {
92         u8 tmp[2];
93         s32 status = i2c_smbus_write_byte_data(data->client, 0xF4, 0x2E);
94         if (status != 0) {
95                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
96                                                 " temperature measurement.\n");
97                 return status;
98         }
99         msleep(5);
100
101         status = i2c_smbus_read_i2c_block_data(data->client, 0xF6,
102                                                         sizeof(tmp), tmp);
103         if (status != sizeof(tmp)) {
104                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
105                                 " temperature measurement (II): %d\n", status);
106                 return status;
107         }
108         data->raw_temperature = (tmp[0] << 8) + tmp[1];
109         data->next_temp_measurement = jiffies+1*HZ;
110         return status;
111 }
112
113 static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
114 {
115         u8 tmp[3];
116         s32 status = 0;
117
118         status = i2c_smbus_write_byte_data(data->client, 0xF4, 0x34 +
119                                         (data->oversampling_setting<<6));
120         if (status != 0) {
121                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
122                                                 "pressure measurement.\n");
123                 return status;
124         }
125
126         /* wait for the end of conversion */
127         msleep(2+(3 << data->oversampling_setting<<1));
128
129         status = i2c_smbus_read_i2c_block_data(data->client, 0xF6,
130                                                         sizeof(tmp), tmp);
131         if (status != sizeof(tmp)) {
132                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while reading"
133                                 "pressure measurement results: %d\n", status);
134                 return status;
135         }
136         /* swap positions to correct the MSB/LSB positions */
137         data->raw_pressure = (tmp[0] << 16) | (tmp[1] << 8) | tmp[2];
138         data->raw_pressure >>= (8-data->oversampling_setting);
139         return status;
140 }
141
142
143 static s32 bmp085_get_temperature(struct bmp085_data *data)
144 {
145         struct bmp085_calibration_data *cali = &data->calibration;
146         long x1, x2;
147
148         if (data->next_temp_measurement < jiffies)
149                 bmp085_update_raw_temperature(data);
150
151         x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
152         x2 = (cali->MC << 11) / (x1 + cali->MD);
153         data->b6 = x1 + x2 - 4000;
154         return (x1+x2+8) >> 4;
155 }
156
157
158 static s32 bmp085_get_pressure(struct bmp085_data *data)
159 {
160         struct bmp085_calibration_data *cali = &data->calibration;
161         long x1, x2, x3, b3;
162         unsigned long b4, b7;
163         long p;
164
165         if (data->next_temp_measurement < jiffies)
166                 bmp085_get_temperature(data);
167
168         bmp085_update_raw_pressure(data);
169
170         x1 = (data->b6 * data->b6) >> 12;
171         x1 *= cali->B2;
172         x1 >>= 11;
173
174         x2 = cali->AC2 * data->b6;
175         x2 >>= 11;
176
177         x3 = x1 + x2;
178
179         b3 = (((((long)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
180         b3 >>= 2;
181
182         x1 = (cali->AC3 * data->b6) >> 13;
183         x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
184         x3 = (x1 + x2 + 2) >> 2;
185         b4 = (cali->AC4 * (unsigned long)(x3 + 32768)) >> 15;
186
187         b7 = ((unsigned long)data->raw_pressure - b3) *
188                                         (50000 >> data->oversampling_setting);
189         p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
190
191         x1 = p >> 8;
192         x1 *= x1;
193         x1 = (x1 * 3038) >> 16;
194         x2 = (-7357 * p) >> 16;
195         p += (x1 + x2 + 3791) >> 4;
196         return p;
197 }
198
199
200 static void bmp085_set_oversampling(struct bmp085_data *data,
201                                                 unsigned char oversampling)
202 {
203         if (oversampling > 3)
204                 oversampling = 3;
205         data->oversampling_setting = oversampling;
206 }
207
208
209 static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
210 {
211         return data->oversampling_setting;
212 }
213
214
215
216
217
218
219
220 /* sysfs callbacks */
221 static ssize_t set_oversampling(struct device *dev,
222                                 struct device_attribute *attr,
223                                 const char *buf, size_t count)
224 {
225         struct i2c_client *client = to_i2c_client(dev);
226         struct bmp085_data *data = i2c_get_clientdata(client);
227         bmp085_set_oversampling(data, strict_strtoul(buf, NULL, 10));
228         return count;
229 }
230
231 static ssize_t show_oversampling(struct device *dev,
232                                  struct device_attribute *attr, char *buf)
233 {
234         struct i2c_client *client = to_i2c_client(dev);
235         struct bmp085_data *data = i2c_get_clientdata(client);
236         return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
237 }
238 static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
239                                         show_oversampling, set_oversampling);
240
241
242 static ssize_t show_temperature(struct device *dev,
243                                 struct device_attribute *attr, char *buf)
244 {
245         struct i2c_client *client = to_i2c_client(dev);
246         struct bmp085_data *data = i2c_get_clientdata(client);
247         return sprintf(buf, "%d\n", bmp085_get_temperature(data));
248 }
249 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
250
251
252 static ssize_t show_pressure(struct device *dev,
253                              struct device_attribute *attr, char *buf)
254 {
255         struct i2c_client *client = to_i2c_client(dev);
256         struct bmp085_data *data = i2c_get_clientdata(client);
257         return sprintf(buf, "%d\n", bmp085_get_pressure(data));
258 }
259 static DEVICE_ATTR(pressure, S_IRUGO, show_pressure, NULL);
260
261
262 static struct attribute *bmp085_attributes[] = {
263         &dev_attr_temperature.attr,
264         &dev_attr_pressure.attr,
265         &dev_attr_oversampling.attr,
266         NULL
267 };
268
269 static const struct attribute_group bmp085_attr_group = {
270         .attrs = bmp085_attributes,
271 };
272
273
274 static int bmp085_probe(struct i2c_client *client,
275                          const struct i2c_device_id *id)
276 {
277         struct bmp085_data *data;
278         int err = 0;
279
280         if (client->addr != BMP085_I2C_ADDRESS)
281                 goto exit;
282
283         if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID)
284                 goto exit;
285
286         data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
287         if (!data) {
288                 err = -ENOMEM;
289                 goto exit;
290         }
291
292         /* default settings after POR */
293         data->oversampling_setting = 0x00;
294
295         i2c_set_clientdata(client, data);
296
297         /* Initialize the BMP085 chip */
298         bmp085_init_client(client);
299
300         /* Register sysfs hooks */
301         err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group);
302         if (err)
303                 goto exit_free;
304
305         printk(KERN_INFO "succesfully initialized bmp085!\n");
306         return 0;
307
308 exit_free:
309         kfree(data);
310 exit:
311         return err;
312 }
313
314 static int bmp085_remove(struct i2c_client *client)
315 {
316         printk(KERN_INFO "bmp085 remove!\n");
317         sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group);
318         kfree(i2c_get_clientdata(client));
319         return 0;
320 }
321
322 /* Called when we have found a new HMC5843. */
323 static void bmp085_init_client(struct i2c_client *client)
324 {
325         struct bmp085_data *data = i2c_get_clientdata(client);
326         bmp085_read_calibration_data(client);
327         data->version = i2c_smbus_read_byte_data(client, BMP085_VERSION_REG);
328         data->next_temp_measurement = 0;
329         data->oversampling_setting = 3;
330         printk(KERN_INFO "BMP085 ver. %d.%d initialized\n",
331                         (data->version & 0x0F), (data->version & 0xF0) >> 4);
332 }
333
334 static const struct i2c_device_id bmp085_id[] = {
335         { "bmp085", 0 },
336         { }
337 };
338
339 static struct i2c_driver bmp085_driver = {
340         .driver = {
341                 .name   = "bmp085",
342                 .owner = THIS_MODULE
343         },
344         .probe          = bmp085_probe,
345         .remove         = bmp085_remove,
346         .id_table       = bmp085_id
347 };
348
349 static int __init bmp085_init(void)
350 {
351         printk(KERN_INFO "init!\n");
352         return i2c_add_driver(&bmp085_driver);
353 }
354
355 static void __exit bmp085_exit(void)
356 {
357         printk(KERN_INFO "exit!\n");
358         i2c_del_driver(&bmp085_driver);
359 }
360
361
362 MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com");
363 MODULE_DESCRIPTION("BMP085 driver");
364 MODULE_LICENSE("GPL");
365
366 module_init(bmp085_init);
367 module_exit(bmp085_exit);