temp revert rk change
[firefly-linux-kernel-4.4.55.git] / drivers / misc / moto_bmp085.c
1 /*
2  * Copyright (C) 2010 Motorola, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
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., 59 Temple Place, Suite 330, Boston, MA
16  * 02111-1307, USA
17  */
18
19 #include <linux/err.h>
20 #include <linux/errno.h>
21 #include <linux/delay.h>
22 #include <linux/fs.h>
23 #include <linux/i2c.h>
24 #include <linux/input.h>
25 #include <linux/input-polldev.h>
26 #include <linux/interrupt.h>
27 #include <linux/irq.h>
28 #include <linux/miscdevice.h>
29 #include <linux/slab.h>
30 #include <linux/regulator/consumer.h>
31 #include <linux/uaccess.h>
32 #include <linux/workqueue.h>
33
34 #include <linux/moto_bmp085.h>
35
36 #define DEBUG
37
38 #define NO_CYCLE 0
39 #define TEMP_CYCLE 1
40 #define PRESSURE_CYCLE 2
41
42 /* Register definitions */
43 #define BMP085_TAKE_MEAS_REG            0xf4
44 #define BMP085_READ_MEAS_REG_U          0xf6
45 #define BMP085_READ_MEAS_REG_L          0xf7
46 #define BMP085_READ_MEAS_REG_XL         0xf8
47
48 /* Bytes defined by the spec to take measurements
49 Temperature will take 4.5ms before EOC */
50 #define BMP085_MEAS_TEMP        0x2e
51 /* 4.5ms wait for measurement */
52 #define BMP085_MEAS_PRESS_OVERSAMP_0    0x34
53 /* 7.5ms wait for measurement */
54 #define BMP085_MEAS_PRESS_OVERSAMP_1    0x74
55 /* 13.5ms wait for measurement */
56 #define BMP085_MEAS_PRESS_OVERSAMP_2    0xb4
57 /* 25.5ms wait for measurement */
58 #define BMP085_MEAS_PRESS_OVERSAMP_3    0xf4
59
60
61 /* EEPROM registers each is a two byte value so there is
62 an upper byte and a lower byte */
63 #define BMP085_EEPROM_AC1_U     0xaa
64 #define BMP085_EEPROM_AC1_L     0xab
65 #define BMP085_EEPROM_AC2_U     0xac
66 #define BMP085_EEPROM_AC2_L     0xad
67 #define BMP085_EEPROM_AC3_U     0xae
68 #define BMP085_EEPROM_AC3_L     0xaf
69 #define BMP085_EEPROM_AC4_U     0xb0
70 #define BMP085_EEPROM_AC4_L     0xb1
71 #define BMP085_EEPROM_AC5_U     0xb2
72 #define BMP085_EEPROM_AC5_L     0xb3
73 #define BMP085_EEPROM_AC6_U     0xb4
74 #define BMP085_EEPROM_AC6_L     0xb5
75 #define BMP085_EEPROM_B1_U      0xb6
76 #define BMP085_EEPROM_B1_L      0xb7
77 #define BMP085_EEPROM_B2_U      0xb8
78 #define BMP085_EEPROM_B2_L      0xb9
79 #define BMP085_EEPROM_MB_U      0xba
80 #define BMP085_EEPROM_MB_L      0xbb
81 #define BMP085_EEPROM_MC_U      0xbc
82 #define BMP085_EEPROM_MC_L      0xbd
83 #define BMP085_EEPROM_MD_U      0xbe
84 #define BMP085_EEPROM_MD_L      0xbf
85
86 #ifdef DEBUG
87 struct bmp085_reg {
88         const char *name;
89         uint8_t reg;
90 } bmp085_regs[] = {
91         {"MEASURE_REG", BMP085_TAKE_MEAS_REG},
92         {"CNTRL_1", BMP085_READ_MEAS_REG_U},
93         {"CNTRL_2", BMP085_READ_MEAS_REG_L},
94         {"CNTRL_3", BMP085_READ_MEAS_REG_XL},
95         {"EE_AC1_U", BMP085_EEPROM_AC1_U},
96         {"EE_AC1_U", BMP085_EEPROM_AC1_L},
97         {"EE_AC2_U", BMP085_EEPROM_AC2_U},
98         {"EE_AC2_L", BMP085_EEPROM_AC2_L},
99         {"EE_AC3_U", BMP085_EEPROM_AC3_U},
100         {"EE_AC3_L", BMP085_EEPROM_AC3_L},
101         {"EE_AC4_U", BMP085_EEPROM_AC4_U},
102         {"EE_AC4_L", BMP085_EEPROM_AC4_L},
103         {"EE_AC5_U", BMP085_EEPROM_AC5_U},
104         {"EE_AC5_L", BMP085_EEPROM_AC5_L},
105         {"EE_AC6_U", BMP085_EEPROM_AC6_U},
106         {"EE_AC6_L", BMP085_EEPROM_AC6_L},
107         {"EE_B1_U", BMP085_EEPROM_B1_U},
108         {"EE_B1_L", BMP085_EEPROM_B1_L},
109         {"EE_B2_U", BMP085_EEPROM_B2_U},
110         {"EE_B2_L", BMP085_EEPROM_B2_L},
111         {"EE_MB_U", BMP085_EEPROM_MB_U},
112         {"EE_MB_L", BMP085_EEPROM_MB_L},
113         {"EE_MC_U", BMP085_EEPROM_MC_U},
114         {"EE_MC_L", BMP085_EEPROM_MC_L},
115         {"EE_MD_U", BMP085_EEPROM_MD_U},
116         {"EE_MD_L", BMP085_EEPROM_MD_L},
117 };
118 #endif
119 static uint32_t bmp085_debug = 0x00;
120 module_param_named(baro_debug, bmp085_debug, uint, 0664);
121
122 #define I2C_RETRY_DELAY         5
123 #define I2C_RETRIES             5
124 #define AUTO_INCREMENT          0x80
125
126 static struct workqueue_struct *barom_wq;
127
128 struct bmp085_eeprom_data {
129         s16 AC1, AC2, AC3;
130         u16 AC4, AC5, AC6;
131         s16 B1, B2;
132         s16 MB, MC, MD;
133 };
134
135 struct bmp085_data {
136         struct i2c_client *client;
137         struct bmp085_platform_data *pdata;
138         struct mutex lock;
139         struct delayed_work input_work;
140         struct work_struct wq;
141         struct workqueue_struct *working_queue;
142         struct input_dev *input_dev;
143
144         u8 oversampling_rate;
145         u8 measurement_cycle;
146
147         int uncalib_temperature;
148         int uncalib_pressure;
149         int calib_temperature;
150         long calib_pressure;
151         long b5;                /* Needed for pressure calculation */
152
153         struct bmp085_eeprom_data bmp085_eeprom_vals;
154
155         atomic_t enabled;
156         int on_before_suspend;
157         struct regulator *regulator;
158         struct regulator *io_regulator;
159         u8 resume_state[5];
160 };
161
162 /*
163  * Because misc devices can not carry a pointer from driver register to
164  * open, we keep this global.  This limits the driver to a single instance.
165  */
166 struct bmp085_data *bmp085_misc_data;
167
168 static int bmp085_i2c_read(struct bmp085_data *barom, u8 * buf, int len)
169 {
170         int err;
171         int tries = 0;
172         struct i2c_msg msgs[] = {
173                 {
174                  .addr = barom->client->addr,
175                  .flags = barom->client->flags & I2C_M_TEN,
176                  .len = 1,
177                  .buf = buf,
178                  },
179                 {
180                  .addr = barom->client->addr,
181                  .flags = (barom->client->flags & I2C_M_TEN) | I2C_M_RD,
182                  .len = len,
183                  .buf = buf,
184                  },
185         };
186
187         do {
188                 err = i2c_transfer(barom->client->adapter, msgs, 2);
189                 if (err != 2)
190                         msleep_interruptible(I2C_RETRY_DELAY);
191         } while ((err != 2) && (++tries < I2C_RETRIES));
192
193         if (err != 2) {
194                 pr_err("%s:read transfer error\n", __func__);
195                 err = -EIO;
196         } else {
197                 err = 0;
198         }
199
200         return err;
201 }
202
203 static int bmp085_i2c_write(struct bmp085_data *barom, u8 * buf, int len)
204 {
205         int err;
206         int tries = 0;
207         struct i2c_msg msgs[] = {
208                 {
209                  .addr = barom->client->addr,
210                  .flags = barom->client->flags & I2C_M_TEN,
211                  .len = len + 1,
212                  .buf = buf,
213                  },
214         };
215
216         do {
217                 err = i2c_transfer(barom->client->adapter, msgs, 1);
218                 if (err != 1)
219                         msleep_interruptible(I2C_RETRY_DELAY);
220         } while ((err != 1) && (++tries < I2C_RETRIES));
221
222         if (err != 1) {
223                 pr_err("%s:write transfer error\n", __func__);
224                 err = -EIO;
225         } else {
226                 err = 0;
227         }
228
229         return err;
230 }
231
232 static int bmp085_update_measurement_accuracy(struct bmp085_data *barom,
233                                        int accuracy)
234 {
235         if (accuracy > 3)
236                 accuracy = 3;
237         barom->oversampling_rate = accuracy;
238
239         return 0;
240 }
241
242 static void bmp085_schedule_work(struct bmp085_data *barom)
243 {
244         schedule_delayed_work(&barom->input_work,
245                               msecs_to_jiffies(barom->pdata->poll_interval));
246 }
247
248 static int bmp085_enable(struct bmp085_data *barom)
249 {
250         int err = 0;
251
252         if (!atomic_cmpxchg(&barom->enabled, 0, 1)) {
253                 if (barom->regulator)
254                         err = regulator_enable(barom->regulator);
255                         err = regulator_enable(barom->io_regulator);
256                 if (err < 0) {
257                         atomic_set(&barom->enabled, 0);
258                         return err;
259                 }
260                 schedule_delayed_work(&barom->input_work,
261                                       msecs_to_jiffies(barom->pdata->
262                                                        poll_interval));
263         }
264
265         return 0;
266 }
267
268 static int bmp085_disable(struct bmp085_data *barom)
269 {
270         if (atomic_cmpxchg(&barom->enabled, 1, 0)) {
271                 cancel_delayed_work_sync(&barom->input_work);
272                 if (barom->regulator)
273                         regulator_disable(barom->regulator);
274                         regulator_disable(barom->io_regulator);
275         }
276         barom->measurement_cycle = NO_CYCLE;
277
278         return 0;
279 }
280
281 static int bmp085_misc_open(struct inode *inode, struct file *file)
282 {
283         int err;
284         err = nonseekable_open(inode, file);
285         if (err < 0)
286                 return err;
287
288         file->private_data = bmp085_misc_data;
289
290         return 0;
291 }
292
293 static long bmp085_misc_ioctl(struct file *file,
294                               unsigned int cmd, unsigned long arg)
295 {
296         void __user *argp = (void __user *)arg;
297         u8 buf[4];
298         int err;
299         int interval;
300         struct bmp085_data *barom = file->private_data;
301
302         switch (cmd) {
303         case BMP085_IOCTL_GET_DELAY:
304                 interval = barom->pdata->poll_interval;
305                 if (copy_to_user(argp, &interval, sizeof(interval)))
306                         return -EFAULT;
307                 break;
308
309         case BMP085_IOCTL_SET_DELAY:
310                 if (copy_from_user(&interval, argp, sizeof(interval)))
311                         return -EFAULT;
312                 if (interval < 0 || interval > 200)
313                         return -EINVAL;
314
315                 barom->pdata->poll_interval =
316                     max(interval, barom->pdata->min_interval);
317                 break;
318
319         case BMP085_IOCTL_SET_ENABLE:
320                 if (copy_from_user(&interval, argp, sizeof(interval)))
321                         return -EFAULT;
322                 if (interval > 1)
323                         return -EINVAL;
324
325                 if (interval)
326                         bmp085_enable(barom);
327                 else
328                         bmp085_disable(barom);
329
330                 break;
331
332         case BMP085_IOCTL_GET_ENABLE:
333                 interval = atomic_read(&barom->enabled);
334                 if (copy_to_user(argp, &interval, sizeof(interval)))
335                         return -EINVAL;
336
337                 break;
338
339         case BMP085_IOCTL_ACCURACY:
340                 if (copy_from_user(&buf, argp, 1))
341                         return -EFAULT;
342                 err = bmp085_update_measurement_accuracy(barom, arg);
343                 if (err < 0)
344                         return err;
345
346                 break;
347
348         default:
349                 return -EINVAL;
350         }
351
352         return 0;
353 }
354
355 static const struct file_operations bmp085_misc_fops = {
356         .owner = THIS_MODULE,
357         .open = bmp085_misc_open,
358         .unlocked_ioctl = bmp085_misc_ioctl,
359 };
360
361 static struct miscdevice bmp085_misc_device = {
362         .minor = MISC_DYNAMIC_MINOR,
363         .name = BMP085_NAME,
364         .fops = &bmp085_misc_fops,
365 };
366
367 #ifdef BMP085_OPEN_ENABLE
368 int bmp085_input_open(struct input_dev *input)
369 {
370         struct bmp085_data *barom = input_get_drvdata(input);
371
372         return bmp085_enable(barom);
373 }
374
375 void bmp085_input_close(struct input_dev *dev)
376 {
377         struct bmp085_data *barom = input_get_drvdata(dev);
378
379         bmp085_disable(barom);
380 }
381 #endif
382 #ifdef DEBUG
383 static ssize_t bmp085_registers_show(struct device *dev,
384                                      struct device_attribute *attr, char *buf)
385 {
386         struct i2c_client *client = container_of(dev, struct i2c_client,
387                                                  dev);
388         struct bmp085_data *barom_data = i2c_get_clientdata(client);
389         u8 barom_reg[2];
390         unsigned i, n, reg_count;
391
392         reg_count = sizeof(bmp085_regs) / sizeof(bmp085_regs[0]);
393         for (i = 0, n = 0; i < reg_count; i++) {
394                 barom_reg[0] = (AUTO_INCREMENT | bmp085_regs[i].reg);
395                 bmp085_i2c_read(barom_data, barom_reg, 1);
396                 n += scnprintf(buf + n, PAGE_SIZE - n,
397                                "%-20s = 0x%02X\n",
398                                bmp085_regs[i].name, barom_reg[0]);
399         }
400         return n;
401 }
402
403 static ssize_t bmp085_registers_store(struct device *dev,
404                                       struct device_attribute *attr,
405                                       const char *buf, size_t count)
406 {
407         struct i2c_client *client = container_of(dev, struct i2c_client,
408                                                  dev);
409         struct bmp085_data *barom_data = i2c_get_clientdata(client);
410         unsigned i, reg_count, value;
411         int error;
412         u8 barom_reg[2];
413         char name[30];
414
415         if (count >= 30) {
416                 pr_err("%s:input too long\n", __func__);
417                 return -1;
418         }
419
420         if (sscanf(buf, "%s %x", name, &value) != 2) {
421                 pr_err("%s:unable to parse input\n", __func__);
422                 return -1;
423         }
424
425         reg_count = sizeof(bmp085_regs) / sizeof(bmp085_regs[0]);
426         for (i = 0; i < reg_count; i++) {
427                 if (!strcmp(name, bmp085_regs[i].name)) {
428                         barom_reg[0] = (AUTO_INCREMENT | bmp085_regs[i].reg);
429                         barom_reg[1] = value;
430                         error = bmp085_i2c_write(barom_data, barom_reg, 2);
431                         if (error) {
432                                 pr_err("%s:Failed to write register %s\n",
433                                        __func__, name);
434                                 return -1;
435                         }
436                         return count;
437                 }
438         }
439         if (!strcmp("Go", name)) {
440                 if (value > 0)
441                         bmp085_enable(barom_data);
442                 else
443                         bmp085_disable(barom_data);
444
445                 return 0;
446         }
447         if (!strcmp("acc", name)) {
448                 barom_data->oversampling_rate = value;
449                 return 0;
450         }
451
452         pr_err("%s:no such register %s\n", __func__, name);
453         return -1;
454 }
455
456 static DEVICE_ATTR(registers, 0644, bmp085_registers_show,
457                    bmp085_registers_store);
458 #endif
459 static int bmp085_get_temperature_data(struct bmp085_data *barom)
460 {
461         int err = -1;
462         u8 buf[2] = { BMP085_READ_MEAS_REG_U, 0 };
463         int x1;
464         unsigned int x2;
465
466         err = bmp085_i2c_read(barom, buf, 2);
467         if (err) {
468                 pr_err("%s:Cannot read pressure measurement\n", __func__);
469                 return err;
470         }
471         if (bmp085_debug & 2)
472                 pr_err("%s:Read Temp 0x%X 0x%X\n", __func__, buf[0], buf[1]);
473
474         barom->uncalib_temperature = (buf[0] << 8) + buf[1];
475
476         /* The math is derived from the data sheet. */
477         x1 = ((barom->uncalib_temperature - barom->bmp085_eeprom_vals.AC6) *
478               barom->bmp085_eeprom_vals.AC5) >> 15;
479         x2 = (barom->bmp085_eeprom_vals.MC << 11) /
480             (x1 + barom->bmp085_eeprom_vals.MD);
481         barom->b5 = x1 + x2;
482         barom->calib_temperature = (barom->b5 + 8) >> 4;
483         if (bmp085_debug & 1)
484                 pr_err("%s:Calibrated Temp %d\n",
485                 __func__, barom->calib_temperature);
486
487         return err;
488 }
489
490 static int bmp085_get_barometer_data(struct bmp085_data *barom)
491 {
492         int err = -1;
493         long x1, x2, x3, b3, b6;
494         unsigned long b4, b7;
495         long p;
496         u8 buf[3] = { BMP085_READ_MEAS_REG_U, 0, 0 };
497
498         err = bmp085_i2c_read(barom, buf, 3);
499         if (err) {
500                 pr_err("%s:Cannot read pressure measurement\n", __func__);
501                 return err;
502         }
503
504         /* Raw data to uncalibrate pressure.  Conversion compliments of the
505         data sheet */
506         barom->uncalib_pressure = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) >>
507                 (8 - barom->oversampling_rate);
508         if (bmp085_debug & 2)
509                 pr_err("%s:Uncalibrated pressure %d\n", __func__,
510                        barom->uncalib_pressure);
511
512         /* Complicated math compliments of the data sheet */
513         b6 = (barom->b5 - 4000);
514         x1 = (barom->bmp085_eeprom_vals.B2 * ((b6 * b6) >> 12)) >> 11;
515         x2 = (barom->bmp085_eeprom_vals.AC2 * b6) >> 11;
516         x3 = x1 + x2;
517         b3 = (((((long)barom->bmp085_eeprom_vals.AC1) * 4 +
518                 x3) << barom->oversampling_rate) + 2) >> 2;
519         x1 = (barom->bmp085_eeprom_vals.AC3 * b6) >> 13;
520         x2 = (barom->bmp085_eeprom_vals.B1 * (b6 * b6 >> 12)) >> 16;
521         x3 = ((x1 + x2) + 2) >> 2;
522         b4 = (barom->bmp085_eeprom_vals.AC4 *
523               (unsigned long)(x3 + 32768)) >> 15;
524         b7 = ((unsigned long)barom->uncalib_pressure -
525               b3) * (50000 >> barom->oversampling_rate);
526         if (b7 < 0x80000000)
527                 p = (b7 * 2) / b4;
528         else
529                 p = (b7 / b4) * 2;
530         x1 = (p >> 8) * (p >> 8);
531         x1 = (x1 * 3038) >> 16;
532         x2 = (-7357 * p) >> 16;
533         barom->calib_pressure = p + ((x1 + x2 + 3791) >> 4);
534         if (bmp085_debug & 1)
535                 pr_info("%s:Calibrated Pressure is %li\n",
536                 __func__, barom->calib_pressure);
537
538         return err;
539 }
540
541 static void bmp085_input_work_func(struct work_struct *work)
542 {
543         struct bmp085_data *barom = container_of((struct delayed_work *)work,
544                                                  struct bmp085_data,
545                                                  input_work);
546         int err;
547         u8 buf[2];
548
549         buf[0] = (AUTO_INCREMENT | BMP085_TAKE_MEAS_REG);
550         buf[1] = BMP085_MEAS_TEMP;
551
552         if ((barom->measurement_cycle == TEMP_CYCLE) ||
553             (barom->measurement_cycle == PRESSURE_CYCLE)) {
554                 /* One of the measurements took to long so
555                 reset the state machine */
556                 barom->measurement_cycle = NO_CYCLE;
557         } else {
558                 barom->measurement_cycle = TEMP_CYCLE;
559                 err = bmp085_i2c_write(barom, buf, 2);
560                 if (err) {
561                         pr_err("%s:Cannot start temp measurement\n", __func__);
562                         barom->measurement_cycle = NO_CYCLE;
563                         return;
564                 }
565         }
566         bmp085_schedule_work(barom);
567         return;
568 }
569
570 void bmp085_work_queue(struct work_struct *work)
571 {
572         int err = 0;
573         struct bmp085_data *barom_data =
574             container_of(work, struct bmp085_data, wq);
575         u8 buf[2];
576
577         if (barom_data->measurement_cycle == NO_CYCLE) {
578                 pr_err("%s:No cycle defined\n", __func__);
579         } else if (barom_data->measurement_cycle == TEMP_CYCLE) {
580
581                 if (bmp085_debug & 1)
582                         pr_err("%s:Temp cycle\n", __func__);
583
584                 err = bmp085_get_temperature_data(barom_data);
585                 if (err) {
586                         pr_err("%s:Cannot read temp measurement\n", __func__);
587                         return;
588                 }
589                 /* Setup for a pressure measurement */
590                 buf[0] = (AUTO_INCREMENT | BMP085_TAKE_MEAS_REG);
591                 buf[1] = BMP085_MEAS_PRESS_OVERSAMP_0 |
592                         (barom_data->oversampling_rate << 6);
593
594                 barom_data->measurement_cycle = PRESSURE_CYCLE;
595
596                 err = bmp085_i2c_write(barom_data, buf, 2);
597                 if (err) {
598                         pr_err("%s:Cannot start temp measurement\n", __func__);
599                         barom_data->measurement_cycle = NO_CYCLE;
600                         return;
601                 }
602         } else {
603                 /* Get and report the pressure */
604                 if (bmp085_debug & 1)
605                         pr_err("%s:Pressure cycle\n", __func__);
606
607                 err = bmp085_get_barometer_data(barom_data);
608                 if (err) {
609                         pr_err("%s:Pressure measurement failed\n", __func__);
610                         return;
611                 }
612
613                 input_report_abs(barom_data->input_dev, ABS_PRESSURE,
614                                  barom_data->calib_pressure);
615                 input_sync(barom_data->input_dev);
616
617                 barom_data->measurement_cycle = NO_CYCLE;
618         }
619         enable_irq(barom_data->client->irq);
620         return;
621 }
622
623 irqreturn_t bmp085_irq_handler(int irq, void *dev)
624 {
625         struct bmp085_data *barom_data = dev;
626         disable_irq_nosync(barom_data->client->irq);
627         queue_work(barom_wq, &barom_data->wq);
628
629         return IRQ_HANDLED;
630 }
631
632 static int bmp085_validate_pdata(struct bmp085_data *barom)
633 {
634         barom->pdata->poll_interval = max(barom->pdata->poll_interval,
635                                           barom->pdata->min_interval);
636
637         /* Enforce minimum polling interval */
638         if (barom->pdata->poll_interval < barom->pdata->min_interval) {
639                 pr_err("%s:minimum poll interval violated\n", __func__);
640                 return -EINVAL;
641         }
642
643         return 0;
644 }
645
646 static int bmp085_input_init(struct bmp085_data *barom)
647 {
648         int err;
649
650         INIT_DELAYED_WORK(&barom->input_work, bmp085_input_work_func);
651
652         barom->input_dev = input_allocate_device();
653         if (!barom->input_dev) {
654                 err = -ENOMEM;
655                 dev_err(&barom->client->dev, "input device allocate failed\n");
656                 goto err0;
657         }
658 #ifdef BMP085_OPEN_ENABLE
659         barom->input_dev->open = bmp085_input_open;
660         barom->input_dev->close = bmp085_input_close;
661 #endif
662
663         input_set_drvdata(barom->input_dev, barom);
664
665         set_bit(EV_ABS, barom->input_dev->evbit);
666
667         /* Need to define the correct min and max */
668         input_set_abs_params(barom->input_dev, ABS_PRESSURE,
669                                 barom->pdata->min_p, barom->pdata->max_p,
670                                 barom->pdata->fuzz, barom->pdata->flat);
671
672         barom->input_dev->name = "barometer";
673
674         err = input_register_device(barom->input_dev);
675         if (err) {
676                 dev_err(&barom->client->dev,
677                         "unable to register input polled device %s\n",
678                         barom->input_dev->name);
679                 goto err1;
680         }
681
682         return 0;
683
684 err1:
685         input_free_device(barom->input_dev);
686 err0:
687         return err;
688 }
689
690 static void bmp085_input_cleanup(struct bmp085_data *barom)
691 {
692         input_unregister_device(barom->input_dev);
693         input_free_device(barom->input_dev);
694 }
695
696 static int bmp085_read_store_eeprom_val(struct bmp085_data *barom)
697 {
698         int err = 0;
699         u8 buf[22];
700
701         buf[0] = BMP085_EEPROM_AC1_U;
702         err = bmp085_i2c_read(barom, buf, 22);
703         if (err) {
704                 pr_err("%s:Cannot read EEPROM values\n", __func__);
705                 return err;
706         }
707
708         barom->bmp085_eeprom_vals.AC1 = (buf[0] << 8) | buf[1];
709         barom->bmp085_eeprom_vals.AC2 = (buf[2] << 8) | buf[3];
710         barom->bmp085_eeprom_vals.AC3 = (buf[4] << 8) | buf[5];
711         barom->bmp085_eeprom_vals.AC4 = (buf[6] << 8) | buf[7];
712         barom->bmp085_eeprom_vals.AC5 = (buf[8] << 8) | buf[9];
713         barom->bmp085_eeprom_vals.AC6 = (buf[10] << 8) | buf[11];
714         barom->bmp085_eeprom_vals.B1 = (buf[12] << 8) | buf[13];
715         barom->bmp085_eeprom_vals.B2 = (buf[14] << 8) | buf[15];
716         barom->bmp085_eeprom_vals.MB = (buf[16] << 8) | buf[17];
717         barom->bmp085_eeprom_vals.MC = (buf[18] << 8) | buf[19];
718         barom->bmp085_eeprom_vals.MD = (buf[20] << 8) | buf[21];
719
720         return 0;
721 }
722
723 static int bmp085_probe(struct i2c_client *client,
724                         const struct i2c_device_id *id)
725 {
726         struct bmp085_data *barom;
727         int err = -1;
728
729         if (client->dev.platform_data == NULL) {
730                 pr_err("%s:platform data is NULL. exiting.\n", __func__);
731                 err = -ENODEV;
732                 goto err0;
733         }
734
735         if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
736                 pr_err("%s:client not i2c capable\n", __func__);
737                 err = -ENODEV;
738                 goto err0;
739         }
740
741         barom = kzalloc(sizeof(*barom), GFP_KERNEL);
742         if (barom == NULL) {
743                 pr_err("%s:failed to allocate memory for module data\n",
744                        __func__);
745                 err = -ENOMEM;
746                 goto err0;
747         }
748         mutex_init(&barom->lock);
749         mutex_lock(&barom->lock);
750         barom->client = client;
751         barom->oversampling_rate = 0;
752         barom->b5 = 0;
753
754         barom->pdata = kzalloc(sizeof(*barom->pdata), GFP_KERNEL);
755         if (barom->pdata == NULL)
756                 goto err1;
757
758         memcpy(barom->pdata, client->dev.platform_data, sizeof(*barom->pdata));
759
760         err = bmp085_validate_pdata(barom);
761         if (err < 0) {
762                 pr_err("%s:failed to validate platform data\n", __func__);
763                 goto err1_1;
764         }
765
766         i2c_set_clientdata(client, barom);
767
768         err = bmp085_read_store_eeprom_val(barom);
769         if (err) {
770                 pr_err("%s: Reading the EEPROM failed\n", __func__);
771                 err = -ENODEV;
772                 goto err_req_irq_failed;
773         }
774
775         INIT_WORK(&barom->wq, bmp085_work_queue);
776
777         err = request_irq(barom->client->irq, bmp085_irq_handler,
778                           IRQF_TRIGGER_RISING, BMP085_NAME, barom);
779         if (err != 0) {
780                 pr_err("%s: irq request failed: %d\n", __func__, err);
781                 err = -ENODEV;
782                 goto err_req_irq_failed;
783         }
784
785         barom->regulator = regulator_get(&client->dev, "vcc");
786         if (IS_ERR_OR_NULL(barom->regulator)) {
787                 dev_err(&client->dev, "unable to get regulator\n");
788                 barom->regulator = NULL;
789         }
790         barom->io_regulator = regulator_get(&client->dev, "vio");
791         if (IS_ERR(barom->io_regulator))
792                 barom->io_regulator = NULL;
793
794         err = bmp085_input_init(barom);
795         if (err < 0)
796                 goto err3;
797
798         bmp085_misc_data = barom;
799
800         err = misc_register(&bmp085_misc_device);
801         if (err < 0) {
802                 dev_err(&client->dev, "barom_device register failed\n");
803                 goto err4;
804         }
805 #ifdef DEBUG
806         err = device_create_file(&client->dev, &dev_attr_registers);
807         if (err < 0)
808                 pr_err("%s:File device creation failed: %d\n", __func__, err);
809 #endif
810
811         /* As default, do not report information */
812         atomic_set(&barom->enabled, 0);
813
814         mutex_unlock(&barom->lock);
815
816         return 0;
817
818 err4:
819         bmp085_input_cleanup(barom);
820 err3:
821         if (barom->regulator)
822                 regulator_put(barom->regulator);
823         if (barom->io_regulator)
824                 regulator_put(barom->io_regulator);
825 err_req_irq_failed:
826 err1_1:
827         mutex_unlock(&barom->lock);
828         kfree(barom->pdata);
829 err1:
830         kfree(barom);
831 err0:
832         return err;
833 }
834
835 static int __devexit bmp085_remove(struct i2c_client *client)
836 {
837         /* TO DO: revisit ordering here once _probe order is finalized */
838         struct bmp085_data *barom = i2c_get_clientdata(client);
839
840         misc_deregister(&bmp085_misc_device);
841         bmp085_input_cleanup(barom);
842         bmp085_disable(barom);
843         if (barom->regulator)
844                 regulator_put(barom->regulator);
845         if (barom->io_regulator)
846                 regulator_put(barom->io_regulator);
847 #ifdef DEBUG
848         device_remove_file(&client->dev, &dev_attr_registers);
849 #endif
850         destroy_workqueue(barom_wq);
851
852         kfree(barom->pdata);
853         kfree(barom);
854
855         return 0;
856 }
857
858 static int bmp085_resume(struct i2c_client *client)
859 {
860         struct bmp085_data *barom = i2c_get_clientdata(client);
861
862         if (barom->on_before_suspend)
863                 return bmp085_enable(barom);
864         return 0;
865 }
866
867 static int bmp085_suspend(struct i2c_client *client, pm_message_t mesg)
868 {
869         struct bmp085_data *barom = i2c_get_clientdata(client);
870
871         barom->on_before_suspend = atomic_read(&barom->enabled);
872         return bmp085_disable(barom);
873 }
874
875 static const struct i2c_device_id bmp085_id[] = {
876         {BMP085_NAME, 0},
877         {},
878 };
879
880 MODULE_DEVICE_TABLE(i2c, bmp085_id);
881
882 static struct i2c_driver bmp085_driver = {
883         .driver = {
884                    .name = BMP085_NAME,
885                    },
886         .probe = bmp085_probe,
887         .remove = __devexit_p(bmp085_remove),
888         .resume = bmp085_resume,
889         .suspend = bmp085_suspend,
890         .id_table = bmp085_id,
891 };
892
893 static int __init bmp085_init(void)
894 {
895         barom_wq = create_singlethread_workqueue("barometer_wq");
896         if (!barom_wq) {
897                 pr_err("%s: Cannot create work queue\n", __func__);
898                 return -ENOMEM;
899         }
900         pr_info("BMP085 barometer driver\n");
901         return i2c_add_driver(&bmp085_driver);
902 }
903
904 static void __exit bmp085_exit(void)
905 {
906         i2c_del_driver(&bmp085_driver);
907         return;
908 }
909
910 module_init(bmp085_init);
911 module_exit(bmp085_exit);
912
913 MODULE_DESCRIPTION("bmp085 barometer driver");
914 MODULE_AUTHOR("Dan Murphy D.Murphy@Motorola.com");
915 MODULE_LICENSE("GPL");