Fix autodetection with HWMON class.
[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 I2C_CLIENT_INSMOD_1(itg3200);
42
43 struct bmp085_calibration_data {
44         s16 AC1, AC2, AC3;
45         u16 AC4, AC5, AC6;
46         s16 B1, B2;
47         s16 MB, MC, MD;
48 };
49
50
51 /* Each client has this additional data */
52 struct bmp085_data {
53         struct i2c_client *client;
54         unsigned char version;
55         struct bmp085_calibration_data calibration;
56         unsigned long raw_temperature;
57         unsigned long raw_pressure;
58         unsigned char oversampling_setting;
59         unsigned long next_temp_measurement;
60         long b6; /* calculated temperature correction coefficient */
61 };
62
63
64 static void bmp085_init_client(struct i2c_client *client);
65
66
67 static s32 bmp085_read_calibration_data(struct i2c_client *client)
68 {
69         u8 tmp[BMP085_CALIBRATION_DATA_LENGTH];
70         struct bmp085_data *data = i2c_get_clientdata(client);
71         struct bmp085_calibration_data *cali = &(data->calibration);
72         s32 status = i2c_smbus_read_i2c_block_data(client,
73                                         BMP085_CALIBRATION_DATA_START,
74                                         BMP085_CALIBRATION_DATA_LENGTH, tmp);
75
76         cali->AC1 =  (tmp[0] << 8) | tmp[1];
77         cali->AC2 =  (tmp[2] << 8) | tmp[3];
78         cali->AC3 =  (tmp[4] << 8) | tmp[5];
79         cali->AC4 =  (tmp[6] << 8) | tmp[7];
80         cali->AC5 =  (tmp[8] << 8) | tmp[9];
81         cali->AC6 = (tmp[10] << 8) | tmp[11];
82         cali->B1 =  (tmp[12] << 8) | tmp[13];
83         cali->B2 =  (tmp[14] << 8) | tmp[15];
84         cali->MB =  (tmp[16] << 8) | tmp[17];
85         cali->MC =  (tmp[18] << 8) | tmp[19];
86         cali->MD =  (tmp[20] << 8) | tmp[21];
87         return status;
88 }
89
90
91 static s32 bmp085_update_raw_temperature(struct bmp085_data *data)
92 {
93         u8 tmp[2];
94         s32 status = i2c_smbus_write_byte_data(data->client, 0xF4, 0x2E);
95         if (status != 0) {
96                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
97                                                 " temperature measurement.\n");
98                 return status;
99         }
100         msleep(5);
101
102         status = i2c_smbus_read_i2c_block_data(data->client, 0xF6,
103                                                         sizeof(tmp), tmp);
104         if (status != sizeof(tmp)) {
105                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
106                                 " temperature measurement (II): %d\n", status);
107                 return status;
108         }
109         data->raw_temperature = (tmp[0] << 8) + tmp[1];
110         data->next_temp_measurement = jiffies+1*HZ;
111         return status;
112 }
113
114 static s32 bmp085_update_raw_pressure(struct bmp085_data *data)
115 {
116         u8 tmp[3];
117         s32 status = 0;
118
119         status = i2c_smbus_write_byte_data(data->client, 0xF4, 0x34 +
120                                         (data->oversampling_setting<<6));
121         if (status != 0) {
122                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while requesting"
123                                                 "pressure measurement.\n");
124                 return status;
125         }
126
127         /* wait for the end of conversion */
128         msleep(2+(3 << data->oversampling_setting<<1));
129
130         status = i2c_smbus_read_i2c_block_data(data->client, 0xF6,
131                                                         sizeof(tmp), tmp);
132         if (status != sizeof(tmp)) {
133                 printk(KERN_INFO "BMP085_CLIENT_NAME: Error while reading"
134                                 "pressure measurement results: %d\n", status);
135                 return status;
136         }
137         /* swap positions to correct the MSB/LSB positions */
138         data->raw_pressure = (tmp[0] << 16) | (tmp[1] << 8) | tmp[2];
139         data->raw_pressure >>= (8-data->oversampling_setting);
140         return status;
141 }
142
143
144 static s32 bmp085_get_temperature(struct bmp085_data *data)
145 {
146         struct bmp085_calibration_data *cali = &data->calibration;
147         long x1, x2;
148
149         if (data->next_temp_measurement < jiffies)
150                 bmp085_update_raw_temperature(data);
151
152         x1 = ((data->raw_temperature - cali->AC6) * cali->AC5) >> 15;
153         x2 = (cali->MC << 11) / (x1 + cali->MD);
154         data->b6 = x1 + x2 - 4000;
155         return (x1+x2+8) >> 4;
156 }
157
158
159 static s32 bmp085_get_pressure(struct bmp085_data *data)
160 {
161         struct bmp085_calibration_data *cali = &data->calibration;
162         long x1, x2, x3, b3;
163         unsigned long b4, b7;
164         long p;
165
166         if (data->next_temp_measurement < jiffies)
167                 bmp085_get_temperature(data);
168
169         bmp085_update_raw_pressure(data);
170
171         x1 = (data->b6 * data->b6) >> 12;
172         x1 *= cali->B2;
173         x1 >>= 11;
174
175         x2 = cali->AC2 * data->b6;
176         x2 >>= 11;
177
178         x3 = x1 + x2;
179
180         b3 = (((((long)cali->AC1) * 4 + x3) << data->oversampling_setting) + 2);
181         b3 >>= 2;
182
183         x1 = (cali->AC3 * data->b6) >> 13;
184         x2 = (cali->B1 * ((data->b6 * data->b6) >> 12)) >> 16;
185         x3 = (x1 + x2 + 2) >> 2;
186         b4 = (cali->AC4 * (unsigned long)(x3 + 32768)) >> 15;
187
188         b7 = ((unsigned long)data->raw_pressure - b3) *
189                                         (50000 >> data->oversampling_setting);
190         p = ((b7 < 0x80000000) ? ((b7 << 1) / b4) : ((b7 / b4) * 2));
191
192         x1 = p >> 8;
193         x1 *= x1;
194         x1 = (x1 * 3038) >> 16;
195         x2 = (-7357 * p) >> 16;
196         p += (x1 + x2 + 3791) >> 4;
197         return p;
198 }
199
200
201 static void bmp085_set_oversampling(struct bmp085_data *data,
202                                                 unsigned char oversampling)
203 {
204         if (oversampling > 3)
205                 oversampling = 3;
206         data->oversampling_setting = oversampling;
207 }
208
209
210 static unsigned char bmp085_get_oversampling(struct bmp085_data *data)
211 {
212         return data->oversampling_setting;
213 }
214
215
216
217
218
219
220
221 /* sysfs callbacks */
222 static ssize_t set_oversampling(struct device *dev,
223                                 struct device_attribute *attr,
224                                 const char *buf, size_t count)
225 {
226         struct i2c_client *client = to_i2c_client(dev);
227         struct bmp085_data *data = i2c_get_clientdata(client);
228         unsigned long oversampling;
229         strict_strtoul(buf, 10, &oversampling);
230         bmp085_set_oversampling(data, oversampling);
231         return count;
232 }
233
234 static ssize_t show_oversampling(struct device *dev,
235                                  struct device_attribute *attr, char *buf)
236 {
237         struct i2c_client *client = to_i2c_client(dev);
238         struct bmp085_data *data = i2c_get_clientdata(client);
239         return sprintf(buf, "%u\n", bmp085_get_oversampling(data));
240 }
241 static DEVICE_ATTR(oversampling, S_IWUSR | S_IRUGO,
242                                         show_oversampling, set_oversampling);
243
244
245 static ssize_t show_temperature(struct device *dev,
246                                 struct device_attribute *attr, char *buf)
247 {
248         struct i2c_client *client = to_i2c_client(dev);
249         struct bmp085_data *data = i2c_get_clientdata(client);
250         return sprintf(buf, "%d\n", bmp085_get_temperature(data));
251 }
252 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
253
254
255 static ssize_t show_pressure(struct device *dev,
256                              struct device_attribute *attr, char *buf)
257 {
258         struct i2c_client *client = to_i2c_client(dev);
259         struct bmp085_data *data = i2c_get_clientdata(client);
260         return sprintf(buf, "%d\n", bmp085_get_pressure(data));
261 }
262 static DEVICE_ATTR(pressure, S_IRUGO, show_pressure, NULL);
263
264
265 static struct attribute *bmp085_attributes[] = {
266         &dev_attr_temperature.attr,
267         &dev_attr_pressure.attr,
268         &dev_attr_oversampling.attr,
269         NULL
270 };
271
272 static const struct attribute_group bmp085_attr_group = {
273         .attrs = bmp085_attributes,
274 };
275
276 static int bmp085_detect(struct i2c_client *client, int kind,
277                           struct i2c_board_info *info)
278 {
279         if (client->addr != BMP085_I2C_ADDRESS)
280                 return -ENODEV;
281
282         if (i2c_smbus_read_byte_data(client, BMP085_CHIP_ID_REG) != BMP085_CHIP_ID)
283                 return -ENODEV;
284
285         strlcpy(info->type, "bmp085", I2C_NAME_SIZE);
286         return 0;
287 }
288
289 static int bmp085_probe(struct i2c_client *client,
290                          const struct i2c_device_id *id)
291 {
292         struct bmp085_data *data;
293         int err = 0;
294
295         data = kzalloc(sizeof(struct bmp085_data), GFP_KERNEL);
296         if (!data) {
297                 err = -ENOMEM;
298                 goto exit;
299         }
300
301         /* default settings after POR */
302         data->oversampling_setting = 0x00;
303
304         i2c_set_clientdata(client, data);
305
306         /* Initialize the BMP085 chip */
307         bmp085_init_client(client);
308
309         /* Register sysfs hooks */
310         err = sysfs_create_group(&client->dev.kobj, &bmp085_attr_group);
311         if (err)
312                 goto exit_free;
313
314         printk(KERN_INFO "succesfully initialized bmp085!\n");
315         return 0;
316
317 exit_free:
318         kfree(data);
319 exit:
320         return err;
321 }
322
323 static int bmp085_remove(struct i2c_client *client)
324 {
325         printk(KERN_INFO "bmp085 remove!\n");
326         sysfs_remove_group(&client->dev.kobj, &bmp085_attr_group);
327         kfree(i2c_get_clientdata(client));
328         return 0;
329 }
330
331 /* Called when we have found a new HMC5843. */
332 static void bmp085_init_client(struct i2c_client *client)
333 {
334         struct bmp085_data *data = i2c_get_clientdata(client);
335         data->client = client;
336         bmp085_read_calibration_data(client);
337         data->version = i2c_smbus_read_byte_data(client, BMP085_VERSION_REG);
338         data->next_temp_measurement = 0;
339         data->oversampling_setting = 3;
340         printk(KERN_INFO "BMP085 ver. %d.%d initialized\n",
341                         (data->version & 0x0F), (data->version & 0xF0) >> 4);
342 }
343
344 static const struct i2c_device_id bmp085_id[] = {
345         { "bmp085", 0 },
346         { }
347 };
348
349 static struct i2c_driver bmp085_driver = {
350         .driver = {
351                 .owner = THIS_MODULE,
352                 .name   = "bmp085"
353         },
354         .id_table       = bmp085_id,
355         .probe          = bmp085_probe,
356         .remove         = bmp085_remove,
357
358         .class          = I2C_CLASS_HWMON,
359         .detect         = bmp085_detect,
360         .address_data   = &addr_data
361 };
362
363 static int __init bmp085_init(void)
364 {
365         printk(KERN_INFO "init!\n");
366         return i2c_add_driver(&bmp085_driver);
367 }
368
369 static void __exit bmp085_exit(void)
370 {
371         printk(KERN_INFO "exit!\n");
372         i2c_del_driver(&bmp085_driver);
373 }
374
375
376 MODULE_AUTHOR("Christoph Mair <christoph.mair@gmail.com");
377 MODULE_DESCRIPTION("BMP085 driver");
378 MODULE_LICENSE("GPL");
379
380 module_init(bmp085_init);
381 module_exit(bmp085_exit);