UPSTREAM: nvmem: core: remove regmap dependency
[firefly-linux-kernel-4.4.55.git] / drivers / mfd / twl6030-gpadc.c
1 /*
2  * drivers/i2c/chips/twl6030-gpadc.c
3  *
4  * TWL6030 GPADC module driver
5  *
6  * Copyright (C) 2009 Texas Instruments Inc.
7  * Nishant Kamat <nskamat@ti.com>
8  *
9  * Based on twl4030-madc.c
10  * Copyright (C) 2008 Nokia Corporation
11  * Mikko Ylinen <mikko.k.ylinen@nokia.com>
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * version 2 as published by the Free Software Foundation.
16  *
17  * This program is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
25  * 02110-1301 USA
26  *
27  */
28
29 #include <linux/init.h>
30 #include <linux/interrupt.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/module.h>
34 #include <linux/delay.h>
35 #include <linux/fs.h>
36 #include <linux/platform_device.h>
37 #include <linux/miscdevice.h>
38 #include <linux/slab.h>
39 #include <linux/hwmon-sysfs.h>
40 #include <linux/i2c/twl.h>
41 #include <linux/i2c/twl6030-gpadc.h>
42
43 #include <linux/uaccess.h>
44
45 #define TWL6030_GPADC_PFX       "twl6030-gpadc: "
46 #define ENABLE_GPADC    0x02
47 #define REG_TOGGLE1     0x90
48 #define GPADCS          (1 << 1)
49 #define GPADCR          (1 << 0)
50
51 #define SCALE                           (1 << 15)
52
53 struct twl6030_chnl_calib {
54         s32 gain_error;
55         s32 offset_error;
56 };
57
58 struct twl6030_ideal_code {
59         s16 code1;
60         s16 code2;
61 };
62
63 struct twl6032_chnl_calib {
64         s32 gain;
65         s32 gain_error;
66         s32 offset_error;
67 };
68
69 struct twl6032_ideal_code {
70         s16 code1;
71         s16 code2;
72         s16 v1;
73         s16 v2;
74 };
75
76 static struct twl6030_chnl_calib
77         twl6030_calib_tbl[GPADC_MAX_CHANNELS];
78 static const u32 calibration_bit_map = 0x47FF;
79
80 /* Trim address where measured offset from ideal code is stored */
81 static const u8 twl6030_trim_addr[GPADC_MAX_CHANNELS] = {
82         0xCD, /* CHANNEL 0 */
83         0xD1, /* CHANNEL 1 */
84         0xD9, /* CHANNEL 2 */
85         0xD1, /* CHANNEL 3 */
86         0xD1, /* CHANNEL 4 */
87         0xD1, /* CHANNEL 5 */
88         0xD1, /* CHANNEL 6 */
89         0xD3, /* CHANNEL 7 */
90         0xCF, /* CHANNEL 8 */
91         0xD5, /* CHANNEL 9 */
92         0xD7, /* CHANNEL 10 */
93         0x00, /* CHANNEL 11 */
94         0x00, /* CHANNEL 12 */
95         0x00, /* CHANNEL 13 */
96         0xDB, /* CHANNEL 14 */
97         0x00, /* CHANNEL 15 */
98         0x00, /* CHANNEL 16 */
99 };
100
101 #define TWL6032_GPADC_TRIM1     0xCD
102 #define TWL6032_GPADC_TRIM2     0xCE
103 #define TWL6032_GPADC_TRIM3     0xCF
104 #define TWL6032_GPADC_TRIM4     0xD0
105 #define TWL6032_GPADC_TRIM5     0xD1
106 #define TWL6032_GPADC_TRIM6     0xD2
107 #define TWL6032_GPADC_TRIM7     0xD3
108 #define TWL6032_GPADC_TRIM8     0xD4
109 #define TWL6032_GPADC_TRIM9     0xD5
110 #define TWL6032_GPADC_TRIM10    0xD6
111 #define TWL6032_GPADC_TRIM11    0xD7
112 #define TWL6032_GPADC_TRIM12    0xD8
113 #define TWL6032_GPADC_TRIM13    0xD9
114 #define TWL6032_GPADC_TRIM14    0xDA
115 #define TWL6032_GPADC_TRIM15    0xDB
116 #define TWL6032_GPADC_TRIM16    0xDC
117 #define TWL6032_GPADC_TRIM19    0xFD
118
119 /*
120  * actual scaler gain is multiplied by 8 for fixed point operation
121  * 1.875 * 8 = 15
122  * For channels 0, 1, 3, 4, 5, 6, 12, 13
123  * 1.25 * 8 = 10
124  * is used, as scaler is Vref * divider
125  * Vref = 1.25
126  */
127 static const u16 twl6030_gain[TWL6030_GPADC_MAX_CHANNELS] = {
128         10,     /* CHANNEL 0 */
129         10,     /* CHANNEL 1 */
130
131         /* 1.875 */
132         15,     /* CHANNEL 2 */
133
134         10,     /* CHANNEL 3 */
135         10,     /* CHANNEL 4 */
136         10,     /* CHANNEL 5 */
137         10,     /* CHANNEL 6 */
138
139         /* 5 */
140         40,     /* CHANNEL 7 */
141
142         /* 6.25 */
143         50,     /* CHANNEL 8 */
144
145         /* 11.25 */
146         90,     /* CHANNEL 9 */
147
148         /* 6.875 */
149         55,     /* CHANNEL 10 */
150
151         /* 1.875 */
152         15,     /* CHANNEL 11 */
153
154         10,     /* CHANNEL 12 */
155         10,     /* CHANNEL 13 */
156
157         /* 6.875 */
158         55,     /* CHANNEL 14 */
159
160         /* 6.25 */
161         50,     /* CHANNEL 15 */
162
163         /* 4.75 */
164         38,     /* CHANNEL 16 */
165 };
166
167 /*
168  * calibration not needed for channel 11, 12, 13, 15 and 16
169  * calibration offset is same for channel 1, 3, 4, 5
170  */
171 static const struct twl6030_ideal_code
172         twl6030_ideal[GPADC_MAX_CHANNELS] = {
173         {116,   745},   /* CHANNEL 0 */
174         {82,    900},   /* CHANNEL 1 */
175         {55,    818},   /* CHANNEL 2 */
176         {82,    900},   /* CHANNEL 3 */
177         {82,    900},   /* CHANNEL 4 */
178         {82,    900},   /* CHANNEL 5 */
179         {82,    900},   /* CHANNEL 6 */
180         {614,   941},   /* CHANNEL 7 */
181         {82,    688},   /* CHANNEL 8 */
182         {182,   818},   /* CHANNEL 9 */
183         {149,   818},   /* CHANNEL 10 */
184         {0,     0},     /* CHANNEL 11 */
185         {0,     0},     /* CHANNEL 12 */
186         {0,     0},     /* CHANNEL 13 */
187         {48,    714},   /* CHANNEL 14 */
188         {0,     0},     /* CHANNEL 15 */
189         {0,     0},     /* CHANNEL 16 */
190 };
191
192 /* PhoenixLite has a different calibration sysem to the Phoenix */
193 static const struct twl6032_ideal_code
194                         twl6032_ideal[GPADC_MAX_CHANNELS] = {
195         {       /* CHANNEL 0 */
196                 .code1 = 1441,
197                 .code2 = 3276,
198                 .v1 = 440,
199                 .v2 = 1000,
200         },
201         {       /* CHANNEL 1 */
202                 .code1 = 1441,
203                 .code2 = 3276,
204                 .v1 = 440,
205                 .v2 = 1000,
206         },
207         {       /* CHANNEL 2 */
208                 .code1 = 1441,
209                 .code2 = 3276,
210                 .v1 = 660,
211                 .v2 = 1500,
212         },
213         {       /* CHANNEL 3 */
214                 .code1 = 1441,
215                 .code2 = 3276,
216                 .v1 = 440,
217                 .v2 = 1000,
218         },
219         {       /* CHANNEL 4 */
220                 .code1 = 1441,
221                 .code2 = 3276,
222                 .v1 = 440,
223                 .v2 = 1000,
224         },
225         {       /* CHANNEL 5 */
226                 .code1 = 1441,
227                 .code2 = 3276,
228                 .v1 = 440,
229                 .v2 = 1000,
230         },
231         {       /* CHANNEL 6 */
232                 .code1 = 1441,
233                 .code2 = 3276,
234                 .v1 = 440,
235                 .v2 = 1000,
236         },
237         {       /* CHANNEL 7 */
238                 .code1 = 1441,
239                 .code2 = 3276,
240                 .v1 = 2200,
241                 .v2 = 5000,
242         },
243         {       /* CHANNEL 8 */
244                 .code1 = 1441,
245                 .code2 = 3276,
246                 .v1 = 2200,
247                 .v2 = 5000,
248         },
249         {       /* CHANNEL 9 */
250                 .code1 = 1441,
251                 .code2 = 3276,
252                 .v1 = 3960,
253                 .v2 = 9000,
254         },
255         {       /* CHANNEL 10 */
256                 .code1 = 150,
257                 .code2 = 751,
258                 .v1 = 1000,
259                 .v2 = 5000,
260         },
261         {       /* CHANNEL 11 */
262                 .code1 = 1441,
263                 .code2 = 3276,
264                 .v1 = 660,
265                 .v2 = 1500,
266         },
267         {       /* CHANNEL 12 */
268                 .code1 = 1441,
269                 .code2 = 3276,
270                 .v1 = 440,
271                 .v2 = 1000,
272         },
273         {       /* CHANNEL 13 */
274                 .code1 = 1441,
275                 .code2 = 3276,
276                 .v1 = 440,
277                 .v2 = 1000,
278         },
279         {       /* CHANNEL 14 */
280                 .code1 = 1441,
281                 .code2 = 3276,
282                 .v1 = 2420,
283                 .v2 = 5500,
284         },
285         {},     /* CHANNEL 15 - UNUSED */
286         {},     /* CHANNEL 16 - UNUSED */
287         {},     /* CHANNEL 17 - UNUSED */
288         {       /* CHANNEL 18 */
289                 .code1 = 1441,
290                 .code2 = 3276,
291                 .v1 = 2200,
292                 .v2 = 5000,
293         },
294 };
295
296
297 struct twl6030_gpadc_data {
298         struct device           *dev;
299         struct mutex            lock;
300         struct work_struct      ws;
301         struct twl6030_gpadc_request    requests[TWL6030_GPADC_NUM_METHODS];
302         int irq_n;
303         struct twl6032_chnl_calib *twl6032_cal_tbl;
304         unsigned long features;
305 };
306
307 static struct twl6030_gpadc_data *the_gpadc;
308
309 static const
310 struct twl6030_gpadc_conversion_method twl6030_conversion_methods_table[] = {
311         [TWL6030_GPADC_RT] = {
312                 .sel    = TWL6030_GPADC_RTSELECT_LSB,
313                 .rbase  = TWL6030_GPADC_RTCH0_LSB,
314                 .mask   = TWL6030_GPADC_RT_SW1_EOC_MASK,
315         },
316         /*
317          * TWL6030_GPADC_SW1 is not supported as
318          * interrupt from RT and SW1 cannot be differentiated
319          */
320         [TWL6030_GPADC_SW2] = {
321                 .rbase  = TWL6030_GPADC_GPCH0_LSB,
322                 .ctrl   = TWL6030_GPADC_CTRL_P2,
323                 .enable = TWL6030_GPADC_CTRL_P2_SP2,
324                 .mask   = TWL6030_GPADC_SW2_EOC_MASK,
325         },
326 };
327
328 static const
329 struct twl6030_gpadc_conversion_method twl6032_conversion_methods_table[] = {
330         [TWL6030_GPADC_RT] = {
331                 .sel    = TWL6032_GPADC_RTSELECT_LSB,
332                 .rbase  = TWL6032_RTCH0_LSB,
333                 .mask   = TWL6032_GPADC_RT_EOC_MASK,
334         },
335         [TWL6030_GPADC_SW2] = {
336                 .sel    = TWL6032_GPADC_GPSELECT_ISB,
337                 .rbase  = TWL6032_GPCH0_LSB,
338                 .ctrl   = TWL6032_GPADC_CTRL_P1,
339                 .enable = TWL6030_GPADC_CTRL_P1_SP1,
340                 .mask   = TWL6032_GPADC_SW_EOC_MASK,
341         },
342 };
343
344 static const
345 struct twl6030_gpadc_conversion_method *twl6030_conversion_methods;
346
347 static ssize_t show_gain(struct device *dev,
348                 struct device_attribute *devattr, char *buf)
349 {
350         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
351         int value;
352         int status;
353
354         value = twl6030_calib_tbl[attr->index].gain_error;
355
356         status = sprintf(buf, "%d\n", value);
357         return status;
358 }
359
360 static ssize_t set_gain(struct device *dev,
361         struct device_attribute *devattr, const char *buf, size_t count)
362 {
363         long val;
364         int status = count;
365         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
366
367         if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
368                                                         || (val > 60000))
369                 return -EINVAL;
370
371         twl6030_calib_tbl[attr->index].gain_error = val;
372
373         return status;
374 }
375
376 static ssize_t show_offset(struct device *dev,
377                 struct device_attribute *devattr, char *buf)
378 {
379         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
380         int value;
381         int status;
382
383         value = twl6030_calib_tbl[attr->index].offset_error;
384
385         status = sprintf(buf, "%d\n", value);
386         return status;
387 }
388
389 static ssize_t set_offset(struct device *dev,
390         struct device_attribute *devattr, const char *buf, size_t count)
391 {
392         long val;
393         int status = count;
394         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
395
396         if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
397                                                         || (val > 60000))
398                 return -EINVAL;
399
400         twl6030_calib_tbl[attr->index].offset_error = val;
401
402         return status;
403 }
404
405 static int twl6030_gpadc_read(struct twl6030_gpadc_data *gpadc, u8 reg)
406 {
407         int ret;
408         u8 val = 0;
409
410         ret = twl_i2c_read_u8(TWL_MODULE_MADC, &val, reg);
411         if (ret) {
412                 dev_dbg(gpadc->dev, "unable to read register 0x%X\n", reg);
413                 return ret;
414         }
415
416         return val;
417 }
418
419 static void twl6030_gpadc_write(struct twl6030_gpadc_data *gpadc,
420                                 u8 reg, u8 val)
421 {
422         int ret;
423
424         ret = twl_i2c_write_u8(TWL_MODULE_MADC, val, reg);
425         if (ret)
426                 dev_err(gpadc->dev, "unable to write register 0x%X\n", reg);
427 }
428
429 static int twl6030_gpadc_channel_raw_read(struct twl6030_gpadc_data *gpadc,
430                                           u8 reg)
431 {
432         u8 msb, lsb;
433
434         /* For each ADC channel, we have MSB and LSB register pair.
435          * MSB address is always LSB address+1. reg parameter is the
436          * addr of LSB register
437          */
438         msb = twl6030_gpadc_read(gpadc, reg + 1);
439         lsb = twl6030_gpadc_read(gpadc, reg);
440         return (int)((msb << 8) | lsb);
441 }
442
443 static int twl6030_gpadc_read_channels(struct twl6030_gpadc_data *gpadc,
444                 u8 reg_base, u32 channels, struct twl6030_gpadc_request *req)
445 {
446         int count = 0;
447         u8 reg, i;
448         s32 gain_error;
449         s32 offset_error;
450         s32 raw_code;
451         s32 corrected_code;
452         s32 raw_channel_value;
453
454         channels = ~channels;
455         if (gpadc->features & TWL6032_SUBCLASS) {
456                 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
457                         if (channels & BIT(i))
458                                 continue;
459
460                         reg = reg_base + 2 * count;
461
462                         dev_dbg(gpadc->dev, "GPADC chn: %d\n", i);
463                         raw_code = twl6030_gpadc_channel_raw_read(gpadc, reg);
464                         dev_dbg(gpadc->dev, "GPADC raw: %d\n", raw_code);
465                         count++;
466                         req->buf[i].raw_code = raw_code;
467
468                         /* No correction for channels 15-17 */
469                         if (unlikely((i >= 15) && (i <= 17))) {
470                                 raw_channel_value = raw_code;
471                                 req->buf[i].code = raw_code;
472                                 req->rbuf[i] = raw_code;
473                         } else {
474                                 raw_channel_value = (raw_code *
475                                         gpadc->twl6032_cal_tbl[i].gain);
476
477                                 /* Shift back into mV range */
478                                 raw_channel_value /= 1000;
479
480                                 req->buf[i].code = corrected_code =
481                                 ((raw_code * 1000) -
482                                 gpadc->twl6032_cal_tbl[i].offset_error) /
483                                 gpadc->twl6032_cal_tbl[i].gain_error;
484
485                                 dev_dbg(gpadc->dev, "GPADC cor: %d\n",
486                                         corrected_code);
487
488                                 req->rbuf[i] = corrected_code *
489                                         gpadc->twl6032_cal_tbl[i].gain;
490
491                                 /* Shift back into mV range */
492                                 req->rbuf[i] /= 1000;
493                         }
494                         req->buf[i].raw_channel_value = raw_channel_value;
495                         dev_dbg(gpadc->dev, "GPADC val: %d\n", req->rbuf[i]);
496                 }
497         } else {
498                 for (i = 0; i < TWL6030_GPADC_MAX_CHANNELS; i++) {
499                         if (channels & BIT(i))
500                                 continue;
501                         reg = reg_base + 2 * i;
502                         raw_code = twl6030_gpadc_channel_raw_read(gpadc, reg);
503                         req->buf[i].raw_code = raw_code;
504                         count++;
505                         /*
506                          * multiply by 1000 to convert the unit to milli
507                          * division by 1024 (>> 10) for 10 bit ADC
508                          * division by 8 (>> 3) for actual scaler gain
509                          */
510                         raw_channel_value = (raw_code * twl6030_gain[i]
511                                                                 * 1000) >> 13;
512                         req->buf[i].raw_channel_value = raw_channel_value;
513
514                         if (~calibration_bit_map & BIT(i)) {
515                                 req->buf[i].code = raw_code;
516                                 req->rbuf[i] = raw_channel_value;
517                         } else {
518                                 gain_error = twl6030_calib_tbl[i].gain_error;
519                                 offset_error = twl6030_calib_tbl[i].offset_error;
520                                 req->buf[i].code = corrected_code =
521                                         (raw_code * SCALE - offset_error) /
522                                                 gain_error;
523                                 req->rbuf[i] = (corrected_code * twl6030_gain[i]
524                                                                 * 1000) >> 13;
525                         }
526                         dev_dbg(gpadc->dev, "GPADC val: %d", req->rbuf[i]);
527                 }
528         }
529         return count;
530 }
531
532 static void twl6030_gpadc_enable_irq(u16 method)
533 {
534         twl6030_interrupt_unmask(twl6030_conversion_methods[method].mask,
535                                                 REG_INT_MSK_LINE_B);
536         twl6030_interrupt_unmask(twl6030_conversion_methods[method].mask,
537                                                 REG_INT_MSK_STS_B);
538 }
539
540 static void twl6030_gpadc_disable_irq(u16 method)
541 {
542         twl6030_interrupt_mask(twl6030_conversion_methods[method].mask,
543                                                 REG_INT_MSK_LINE_B);
544         twl6030_interrupt_mask(twl6030_conversion_methods[method].mask,
545                                                 REG_INT_MSK_STS_B);
546 }
547
548 static irqreturn_t twl6030_gpadc_irq_handler(int irq, void *_req)
549 {
550         struct twl6030_gpadc_request *req = _req;
551
552 #ifdef CONFIG_LOCKDEP
553         /* WORKAROUND for lockdep forcing IRQF_DISABLED on us, which
554          * we don't want and can't tolerate.  Although it might be
555          * friendlier not to borrow this thread context...
556          */
557         local_irq_enable();
558 #endif
559
560         /* Find the cause of the interrupt and enable the pending
561            bit for the corresponding method */
562         twl6030_gpadc_disable_irq(req->method);
563         req->result_pending = 1;
564
565         schedule_work(&the_gpadc->ws);
566
567         return IRQ_HANDLED;
568 }
569
570 static void twl6030_gpadc_work(struct work_struct *ws)
571 {
572         const struct twl6030_gpadc_conversion_method *method;
573         struct twl6030_gpadc_data *gpadc;
574         struct twl6030_gpadc_request *r;
575         int len, i;
576
577         gpadc = container_of(ws, struct twl6030_gpadc_data, ws);
578         mutex_lock(&gpadc->lock);
579
580         for (i = 0; i < TWL6030_GPADC_NUM_METHODS; i++) {
581
582                 r = &gpadc->requests[i];
583
584                 /* No pending results for this method, move to next one */
585                 if (!r->result_pending)
586                         continue;
587
588                 method = &twl6030_conversion_methods[r->method];
589
590                 /* Read results */
591                 len = twl6030_gpadc_read_channels(gpadc, method->rbase,
592                                                  r->channels, r);
593
594                 /* Return results to caller */
595                 if (r->func_cb != NULL) {
596                         r->func_cb(r);
597                         r->func_cb = NULL;
598                 }
599
600                 /* Free request */
601                 r->result_pending = 0;
602                 r->active         = 0;
603         }
604
605         mutex_unlock(&gpadc->lock);
606 }
607
608 static int twl6030_gpadc_set_irq(struct twl6030_gpadc_data *gpadc,
609                 struct twl6030_gpadc_request *req)
610 {
611         struct twl6030_gpadc_request *p;
612
613         p = &gpadc->requests[req->method];
614         p->channels = req->channels;
615         p->method = req->method;
616         p->func_cb = req->func_cb;
617         p->type = req->type;
618
619         twl6030_gpadc_enable_irq(req->method);
620
621         return 0;
622 }
623
624 static inline void
625 twl6030_gpadc_start_conversion(struct twl6030_gpadc_data *gpadc,
626                                int conv_method)
627 {
628         const struct twl6030_gpadc_conversion_method *method;
629
630         method = &twl6030_conversion_methods[conv_method];
631         twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
632
633         switch (conv_method) {
634         case TWL6030_GPADC_SW2:
635                 twl6030_gpadc_write(gpadc, method->ctrl, method->enable);
636                 break;
637         case TWL6030_GPADC_RT:
638         default:
639                 break;
640         }
641 }
642
643 static int twl6030_gpadc_is_conversion_ready(
644                 struct twl6030_gpadc_data *gpadc, u8 status_reg)
645 {
646         u8 reg = twl6030_gpadc_read(gpadc, status_reg);
647         return !(reg & TWL6030_GPADC_BUSY) && (reg & TWL6030_GPADC_EOC_SW);
648 }
649
650 static int twl6030_gpadc_wait_conversion_ready(
651                 struct twl6030_gpadc_data *gpadc,
652                 unsigned int timeout_ms, u8 status_reg)
653 {
654         unsigned long timeout;
655
656         timeout = jiffies + msecs_to_jiffies(timeout_ms);
657         do {
658                 if (twl6030_gpadc_is_conversion_ready(gpadc, status_reg))
659                         return 0;
660                 msleep_interruptible(1);
661         } while (!time_after(jiffies, timeout));
662
663         /* one more checking against scheduler-caused timeout */
664         if (twl6030_gpadc_is_conversion_ready(gpadc, status_reg))
665                 return 0;
666         else
667                 return -EAGAIN;
668 }
669
670 /* locks held by caller */
671 static int _twl6030_gpadc_conversion(struct twl6030_gpadc_request *req,
672         const struct twl6030_gpadc_conversion_method *method)
673 {
674         u8 ch_msb, ch_lsb, ch_isb;
675         int ret = 0;
676
677         if (req->method == TWL6030_GPADC_RT) {
678                 ch_msb = (req->channels >> 16) & 0x01;
679                 ch_isb = (req->channels >> 8) & 0xff;
680                 ch_lsb = req->channels & 0xff;
681                 twl6030_gpadc_write(the_gpadc, method->sel + 2, ch_msb);
682                 twl6030_gpadc_write(the_gpadc, method->sel + 1, ch_isb);
683                 twl6030_gpadc_write(the_gpadc, method->sel, ch_lsb);
684         }
685
686         if ((req->type == TWL6030_GPADC_IRQ_ONESHOT) &&
687                  (req->func_cb != NULL)) {
688                 twl6030_gpadc_set_irq(the_gpadc, req);
689                 twl6030_gpadc_start_conversion(the_gpadc, req->method);
690                 the_gpadc->requests[req->method].active = 1;
691                 ret = 0;
692                 goto out;
693         }
694
695         /* With RT method we should not be here anymore */
696         if (req->method == TWL6030_GPADC_RT) {
697                 ret = -EINVAL;
698                 goto out;
699         }
700
701         twl6030_gpadc_start_conversion(the_gpadc, req->method);
702         the_gpadc->requests[req->method].active = 1;
703
704         /* Wait until conversion is ready (ctrl register returns EOC) */
705         ret = twl6030_gpadc_wait_conversion_ready(the_gpadc, 5, method->ctrl);
706         if (ret) {
707                 dev_dbg(the_gpadc->dev, "conversion timeout!\n");
708                 the_gpadc->requests[req->method].active = 0;
709                 goto out;
710         }
711
712         ret = twl6030_gpadc_read_channels(the_gpadc, method->rbase,
713                                           req->channels, req);
714         the_gpadc->requests[req->method].active = 0;
715 out:
716         return ret;
717 }
718
719 /* locks held by caller */
720 static int _twl6032_gpadc_conversion(struct twl6030_gpadc_request *req,
721         const struct twl6030_gpadc_conversion_method *method)
722 {
723         int i, ret, count = 0, channelcnt = 0;
724         u8 ch_msb, ch_lsb, ch_isb;
725
726         if ((req->type == TWL6030_GPADC_IRQ_ONESHOT) &&
727                 (req->func_cb == NULL)) {
728                 ret = -EINVAL;
729                 goto out;
730         }
731
732         for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++)
733                 if (req->channels & BIT(i))
734                         channelcnt++;
735
736         if (req->method == TWL6030_GPADC_RT) {
737                 /*
738                  * For the TWL6032 real time conversion
739                  * maximum channels count is 2
740                  */
741                 if ((req->type != TWL6030_GPADC_IRQ_ONESHOT) ||
742                          (channelcnt > 2)) {
743                         ret = -EINVAL;
744                         goto out;
745                 }
746
747                 ch_msb = (req->channels >> 16) & 0x07;
748                 ch_isb = (req->channels >> 8) & 0xff;
749                 ch_lsb = req->channels & 0xff;
750                 twl6030_gpadc_write(the_gpadc, method->sel + 2, ch_msb);
751                 twl6030_gpadc_write(the_gpadc, method->sel + 1, ch_isb);
752                 twl6030_gpadc_write(the_gpadc, method->sel, ch_lsb);
753         }
754
755         /*
756          * For the TWL6032 Asynchronous Conversion
757          * maximum channels count is 1
758          */
759         if ((req->method == TWL6030_GPADC_SW2) &&
760                  (req->type == TWL6030_GPADC_IRQ_ONESHOT)) {
761                 if (channelcnt > 1) {
762                         ret = -EINVAL;
763                         goto out;
764                 }
765
766                 for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
767                         if (!(req->channels & BIT(i)))
768                                 continue;
769
770                         /* select the ADC channel to be read */
771                         twl6030_gpadc_write(the_gpadc, method->sel, i);
772                 }
773         }
774
775         if (req->type == TWL6030_GPADC_IRQ_ONESHOT) {
776                 twl6030_gpadc_set_irq(the_gpadc, req);
777                 twl6030_gpadc_start_conversion(the_gpadc, req->method);
778                 the_gpadc->requests[req->method].active = 1;
779                 ret = 0;
780                 goto out;
781         }
782
783         for (i = 0; i < TWL6032_GPADC_MAX_CHANNELS; i++) {
784                 if (!(req->channels & BIT(i)))
785                         continue;
786
787                 /* select the ADC channel to be read */
788                 twl6030_gpadc_write(the_gpadc, method->sel, i);
789
790                 twl6030_gpadc_start_conversion(the_gpadc, req->method);
791                 the_gpadc->requests[req->method].active = 1;
792
793                 /* Wait until conversion is ready (ctrl register is EOC) */
794                 ret = twl6030_gpadc_wait_conversion_ready(the_gpadc, 5,
795                                 method->ctrl);
796                 if (ret) {
797                         dev_dbg(the_gpadc->dev, "conversion timeout!\n");
798                         the_gpadc->requests[req->method].active = 0;
799                         goto out;
800                 }
801
802                 ret = twl6030_gpadc_read_channels(the_gpadc, method->rbase,
803                                         1 << i, req);
804                 if (!ret)
805                         dev_err(the_gpadc->dev, "%s: channel error %d\n",
806                                         __func__, i);
807
808                 count += ret;
809                 the_gpadc->requests[req->method].active = 0;
810         }
811         ret = count;
812 out:
813         return ret;
814 }
815
816 int twl6030_gpadc_conversion(struct twl6030_gpadc_request *req)
817 {
818         const struct twl6030_gpadc_conversion_method *method;
819         int ret = 0;
820
821         if (unlikely(!req))
822                 return -EINVAL;
823
824         if (!the_gpadc)
825                 return -EAGAIN;
826
827         mutex_lock(&the_gpadc->lock);
828
829         if (req->method >= TWL6030_GPADC_NUM_METHODS) {
830                 dev_err(the_gpadc->dev, "unsupported conversion method\n");
831                 ret = -EINVAL;
832                 goto out;
833         }
834
835         /* Do we have a conversion request ongoing */
836         if (the_gpadc->requests[req->method].active) {
837                 ret = -EBUSY;
838                 goto out;
839         }
840
841         method = &twl6030_conversion_methods[req->method];
842
843         if (the_gpadc->features & TWL6032_SUBCLASS)
844                 ret = _twl6032_gpadc_conversion(req, method);
845         else
846                 ret = _twl6030_gpadc_conversion(req, method);
847
848 out:
849         mutex_unlock(&the_gpadc->lock);
850
851         return ret;
852 }
853 EXPORT_SYMBOL(twl6030_gpadc_conversion);
854
855 static ssize_t show_channel(struct device *dev,
856                 struct device_attribute *devattr, char *buf)
857 {
858         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
859         struct twl6030_gpadc_request req;
860         int temp = 0;
861         int ret;
862
863         req.channels = (1 << attr->index);
864         req.method = TWL6030_GPADC_SW2;
865         req.active = 0;
866         req.func_cb = NULL;
867         ret = twl6030_gpadc_conversion(&req);
868         if (ret < 0)
869                 return ret;
870
871         if (req.rbuf[attr->index] > 0)
872                 temp = req.rbuf[attr->index];
873
874         ret = sprintf(buf, "%d\n", temp);
875
876         return ret;
877 }
878
879 static ssize_t show_raw_code(struct device *dev,
880                 struct device_attribute *devattr, char *buf)
881 {
882         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
883         struct twl6030_gpadc_request req;
884         int temp = 0;
885         int ret;
886
887         req.channels = (1 << attr->index);
888         req.method = TWL6030_GPADC_SW2;
889         req.active = 0;
890         req.func_cb = NULL;
891         ret = twl6030_gpadc_conversion(&req);
892         if (ret < 0)
893                 return ret;
894
895         if (req.buf[attr->index].raw_channel_value > 0)
896                 temp = req.buf[attr->index].raw_code;
897
898         ret = sprintf(buf, "%d\n", temp);
899
900         return ret;
901 }
902
903 #define in_gain(index) \
904 static SENSOR_DEVICE_ATTR(in##index##_gain, S_IRUGO|S_IWUSR, show_gain, \
905         set_gain, index); \
906 static SENSOR_DEVICE_ATTR(in##index##_offset, S_IRUGO|S_IWUSR, show_offset, \
907         set_offset, index)
908
909 in_gain(0);
910 in_gain(1);
911 in_gain(2);
912 in_gain(3);
913 in_gain(4);
914 in_gain(5);
915 in_gain(6);
916 in_gain(7);
917 in_gain(8);
918 in_gain(9);
919 in_gain(10);
920 in_gain(11);
921 in_gain(12);
922 in_gain(13);
923 in_gain(14);
924 in_gain(15);
925 in_gain(16);
926
927 #define in_channel(index) \
928 static SENSOR_DEVICE_ATTR(in##index##_channel, S_IRUGO, show_channel, \
929         NULL, index); \
930 static SENSOR_DEVICE_ATTR(in##index##_raw_code, S_IRUGO, show_raw_code, \
931         NULL, index)
932
933 in_channel(0);
934 in_channel(1);
935 in_channel(2);
936 in_channel(3);
937 in_channel(4);
938 in_channel(5);
939 in_channel(6);
940 in_channel(7);
941 in_channel(8);
942 in_channel(9);
943 in_channel(10);
944 in_channel(11);
945 in_channel(12);
946 in_channel(13);
947 in_channel(14);
948 in_channel(15);
949 in_channel(16);
950 in_channel(17);
951 in_channel(18);
952
953 #define IN_ATTRS(X)\
954         &sensor_dev_attr_in##X##_gain.dev_attr.attr,    \
955         &sensor_dev_attr_in##X##_offset.dev_attr.attr   \
956
957 #define IN_ATTRS_CHANNEL(X)\
958         &sensor_dev_attr_in##X##_channel.dev_attr.attr,         \
959         &sensor_dev_attr_in##X##_raw_code.dev_attr.attr \
960
961 static struct attribute *twl6030_gpadc_attributes[] = {
962         IN_ATTRS(0),
963         IN_ATTRS(1),
964         IN_ATTRS(2),
965         IN_ATTRS(3),
966         IN_ATTRS(4),
967         IN_ATTRS(5),
968         IN_ATTRS(6),
969         IN_ATTRS(7),
970         IN_ATTRS(8),
971         IN_ATTRS(9),
972         IN_ATTRS(10),
973         IN_ATTRS(11),
974         IN_ATTRS(12),
975         IN_ATTRS(13),
976         IN_ATTRS(14),
977         IN_ATTRS(15),
978         IN_ATTRS(16),
979         IN_ATTRS_CHANNEL(0),
980         IN_ATTRS_CHANNEL(1),
981         IN_ATTRS_CHANNEL(2),
982         IN_ATTRS_CHANNEL(3),
983         IN_ATTRS_CHANNEL(4),
984         IN_ATTRS_CHANNEL(5),
985         IN_ATTRS_CHANNEL(6),
986         IN_ATTRS_CHANNEL(7),
987         IN_ATTRS_CHANNEL(8),
988         IN_ATTRS_CHANNEL(9),
989         IN_ATTRS_CHANNEL(10),
990         IN_ATTRS_CHANNEL(11),
991         IN_ATTRS_CHANNEL(12),
992         IN_ATTRS_CHANNEL(13),
993         IN_ATTRS_CHANNEL(14),
994         IN_ATTRS_CHANNEL(15),
995         IN_ATTRS_CHANNEL(16),
996         IN_ATTRS_CHANNEL(17),
997         IN_ATTRS_CHANNEL(18),
998         NULL
999 };
1000
1001 static const struct attribute_group twl6030_gpadc_group = {
1002         .attrs = twl6030_gpadc_attributes,
1003 };
1004
1005 static long twl6030_gpadc_ioctl(struct file *filp, unsigned int cmd,
1006                                unsigned long arg)
1007 {
1008         struct twl6030_gpadc_user_parms par;
1009         int val, ret;
1010
1011         ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
1012         if (ret) {
1013                 dev_dbg(the_gpadc->dev, "copy_from_user: %d\n", ret);
1014                 return -EACCES;
1015         }
1016
1017         switch (cmd) {
1018         case TWL6030_GPADC_IOCX_ADC_READ:
1019         case TWL6030_GPADC_IOCX_ADC_RAW_READ: {
1020                 struct twl6030_gpadc_request req;
1021                 if (the_gpadc->features & TWL6032_SUBCLASS) {
1022                         if (par.channel >= TWL6032_GPADC_MAX_CHANNELS)
1023                                 return -EINVAL;
1024                 } else {
1025                         if (par.channel >= TWL6030_GPADC_MAX_CHANNELS)
1026                                 return -EINVAL;
1027                 }
1028
1029                 req.channels = (1 << par.channel);
1030                 req.method      = TWL6030_GPADC_SW2;
1031                 req.func_cb     = NULL;
1032
1033                 val = twl6030_gpadc_conversion(&req);
1034                 if (likely(val > 0)) {
1035                         par.status = 0;
1036                         if (cmd == TWL6030_GPADC_IOCX_ADC_READ)
1037                                 par.result = (u16)req.rbuf[par.channel];
1038                         else
1039                                 par.result = (u16)req.buf[par.channel].raw_code;
1040
1041                 } else if (val == 0) {
1042                         par.status = -ENODATA;
1043                 } else {
1044                         par.status = val;
1045                 }
1046                 break;
1047                                              }
1048         default:
1049                 return -EINVAL;
1050         }
1051
1052         ret = copy_to_user((void __user *) arg, &par, sizeof(par));
1053         if (ret) {
1054                 dev_dbg(the_gpadc->dev, "copy_to_user: %d\n", ret);
1055                 return -EACCES;
1056         }
1057
1058         return 0;
1059 }
1060
1061 static const struct file_operations twl6030_gpadc_fileops = {
1062         .owner = THIS_MODULE,
1063         .unlocked_ioctl = twl6030_gpadc_ioctl
1064 };
1065
1066 static struct miscdevice twl6030_gpadc_device = {
1067         .minor = MISC_DYNAMIC_MINOR,
1068         .name = "twl6030-gpadc",
1069         .fops = &twl6030_gpadc_fileops
1070 };
1071
1072 static int twl6030_calibration(void)
1073 {
1074         s8 delta_error1 = 0, delta_error2 = 0;
1075         s16 ideal_code1, ideal_code2;
1076         s32 gain_error_1;
1077         s32 offset_error;
1078         u8 index;
1079         int ret;
1080
1081         for (index = 0; index < TWL6030_GPADC_MAX_CHANNELS; index++) {
1082                 if (~calibration_bit_map & (1 << index))
1083                         continue;
1084
1085                 ret = twl_i2c_read_u8(TWL6030_MODULE_ID2, &delta_error1,
1086                                         twl6030_trim_addr[index]);
1087                 if (ret < 0)
1088                         return ret;
1089
1090                 twl_i2c_read_u8(TWL6030_MODULE_ID2, &delta_error2,
1091                                         (twl6030_trim_addr[index] + 1));
1092                 if (ret < 0)
1093                         return ret;
1094
1095                 /* convert 7 bit to 8 bit signed number */
1096                 delta_error1 = ((s8)(delta_error1 << 1) >> 1);
1097                 delta_error2 = ((s8)(delta_error2 << 1) >> 1);
1098                 ideal_code1 = twl6030_ideal[index].code1;
1099                 ideal_code2 = twl6030_ideal[index].code2;
1100
1101                 gain_error_1 = (delta_error2 - delta_error1) * SCALE
1102                                                 / (ideal_code2 - ideal_code1);
1103                 offset_error = delta_error1 * SCALE - gain_error_1
1104                                                         *  ideal_code1;
1105                 twl6030_calib_tbl[index].gain_error = gain_error_1 + SCALE;
1106                 twl6030_calib_tbl[index].offset_error = offset_error;
1107         }
1108
1109         return 0;
1110 }
1111
1112 static int twl6032_calibration(struct twl6030_gpadc_data *gpadc)
1113 {
1114         int chn, d1 = 0, d2 = 0, b, k, gain, x1, x2, temp;
1115         u8 trim_regs[17];
1116         int ret;
1117
1118         ret = twl_i2c_read(TWL6030_MODULE_ID2, trim_regs + 1,
1119                         TWL6032_GPADC_TRIM1, 16);
1120         if (ret < 0)
1121                 return ret;
1122
1123         /* Loop to calculate the value needed for returning voltages from
1124          * GPADC not values.
1125          *
1126          * gain is calculated to 3 decimal places fixed point.
1127          */
1128         for (chn = 0; chn < TWL6032_GPADC_MAX_CHANNELS; chn++) {
1129
1130                 switch (chn) {
1131                 case 0:
1132                 case 1:
1133                 case 2:
1134                 case 3:
1135                 case 4:
1136                 case 5:
1137                 case 6:
1138                 case 11:
1139                 case 12:
1140                 case 13:
1141                 case 14:
1142                         /* D1 */
1143                         d1 = (trim_regs[3] & 0x1F) << 2;
1144                         d1 |= (trim_regs[1] & 0x06) >> 1;
1145                         if (trim_regs[1] & 0x01)
1146                                 d1 = -d1;
1147
1148                         /* D2 */
1149                         d2 = (trim_regs[4] & 0x3F) << 2;
1150                         d2 |= (trim_regs[2] & 0x06) >> 1;
1151                         if (trim_regs[2] & 0x01)
1152                                 d2 = -d2;
1153                         break;
1154                 case 8:
1155                         /* D1 */
1156                         temp = (trim_regs[3] & 0x1F) << 2;
1157                         temp |= (trim_regs[1] & 0x06) >> 1;
1158                         if (trim_regs[1] & 0x01)
1159                                 temp = -temp;
1160
1161                         d1 = (trim_regs[8] & 0x18) << 1;
1162                         d1 |= (trim_regs[7] & 0x1E) >> 1;
1163                         if (trim_regs[7] & 0x01)
1164                                 d1 = -d1;
1165
1166                         d1 += temp;
1167
1168                         /* D2 */
1169                         temp = (trim_regs[4] & 0x3F) << 2;
1170                         temp |= (trim_regs[2] & 0x06) >> 1;
1171                         if (trim_regs[2] & 0x01)
1172                                 temp = -temp;
1173
1174                         d2 = (trim_regs[10] & 0x1F) << 2;
1175                         d2 |= (trim_regs[8] & 0x06) >> 1;
1176                         if (trim_regs[8] & 0x01)
1177                                 d2 = -d2;
1178
1179                         d2 += temp;
1180                         break;
1181                 case 9:
1182                         /* D1 */
1183                         temp = (trim_regs[3] & 0x1F) << 2;
1184                         temp |= (trim_regs[1] & 0x06) >> 1;
1185                         if (trim_regs[1] & 0x01)
1186                                 temp = -temp;
1187
1188                         d1 = (trim_regs[14] & 0x18) << 1;
1189                         d1 |= (trim_regs[12] & 0x1E) >> 1;
1190                         if (trim_regs[12] & 0x01)
1191                                 d1 = -d1;
1192
1193                         d1 += temp;
1194
1195                         /* D2 */
1196                         temp = (trim_regs[4] & 0x3F) << 2;
1197                         temp |= (trim_regs[2] & 0x06) >> 1;
1198                         if (trim_regs[2] & 0x01)
1199                                 temp = -temp;
1200
1201                         d2 = (trim_regs[16] & 0x1F) << 2;
1202                         d2 |= (trim_regs[14] & 0x06) >> 1;
1203                         if (trim_regs[14] & 0x01)
1204                                 d2 = -d2;
1205
1206                         d2 += temp;
1207                 case 10:
1208                         /* D1 */
1209                         d1 = (trim_regs[11] & 0x0F) << 3;
1210                         d1 |= (trim_regs[9] & 0x0E) >> 1;
1211                         if (trim_regs[9] & 0x01)
1212                                 d1 = -d1;
1213
1214                         /* D2 */
1215                         d2 = (trim_regs[15] & 0x0F) << 3;
1216                         d2 |= (trim_regs[13] & 0x0E) >> 1;
1217                         if (trim_regs[13] & 0x01)
1218                                 d2 = -d2;
1219                         break;
1220                 case 7:
1221                 case 18:
1222                         /* D1 */
1223                         temp = (trim_regs[3] & 0x1F) << 2;
1224                         temp |= (trim_regs[1] & 0x06) >> 1;
1225                         if (trim_regs[1] & 0x01)
1226                                 temp = -temp;
1227
1228                         d1 = (trim_regs[5] & 0x7E) >> 1;
1229                         if (trim_regs[5] & 0x01)
1230                                 d1 = -d1;
1231
1232                         d1 += temp;
1233
1234                         /* D2 */
1235                         temp = (trim_regs[4] & 0x3F) << 2;
1236                         temp |= (trim_regs[2] & 0x06) >> 1;
1237                         if (trim_regs[2] & 0x01)
1238                                 temp = -temp;
1239
1240                         d2 = (trim_regs[6] & 0xFF) >> 1;
1241                         if (trim_regs[6] & 0x01)
1242                                 d2 = -d2;
1243
1244                         d2 += temp;
1245                         break;
1246                 default:
1247                         /* No data for other channels */
1248                         continue;
1249                 }
1250
1251                 dev_dbg(gpadc->dev, "GPADC d1   for Chn: %d = %d\n", chn, d1);
1252                 dev_dbg(gpadc->dev, "GPADC d2   for Chn: %d = %d\n", chn, d2);
1253
1254                 /* Gain */
1255                 gain = ((twl6032_ideal[chn].v2 -
1256                         twl6032_ideal[chn].v1) * 1000) /
1257                         ((twl6032_ideal[chn].code2 -
1258                         twl6032_ideal[chn].code1));
1259
1260                 x1 = twl6032_ideal[chn].code1;
1261                 x2 = twl6032_ideal[chn].code2;
1262
1263                 /* k */
1264                 k = 1000 + (((d2 - d1) * 1000) / (x2 - x1));
1265
1266                 /* b */
1267                 b = (d1 * 1000) - (k - 1000) * x1;
1268
1269                 gpadc->twl6032_cal_tbl[chn].gain = gain;
1270                 gpadc->twl6032_cal_tbl[chn].gain_error = k;
1271                 gpadc->twl6032_cal_tbl[chn].offset_error = b;
1272
1273                 dev_dbg(gpadc->dev, "GPADC x1   for Chn: %d = %d\n", chn, x1);
1274                 dev_dbg(gpadc->dev, "GPADC x2   for Chn: %d = %d\n", chn, x2);
1275                 dev_dbg(gpadc->dev, "GPADC Gain for Chn: %d = %d\n", chn, gain);
1276                 dev_dbg(gpadc->dev, "GPADC k    for Chn: %d = %d\n", chn, k);
1277                 dev_dbg(gpadc->dev, "GPADC b    for Chn: %d = %d\n", chn, b);
1278
1279         }
1280
1281         return 0;
1282 }
1283
1284 static int __devinit twl6030_gpadc_probe(struct platform_device *pdev)
1285 {
1286         struct twl6030_gpadc_data *gpadc;
1287         struct twl4030_madc_platform_data *pdata = pdev->dev.platform_data;
1288         int irq;
1289         int irq_rt;
1290         int ret = 0;
1291
1292         gpadc = kzalloc(sizeof *gpadc, GFP_KERNEL);
1293         if (!gpadc)
1294                 return -ENOMEM;
1295
1296         if (!pdata) {
1297                 dev_dbg(&pdev->dev, "platform_data not available\n");
1298                 ret = -EINVAL;
1299                 goto err_pdata;
1300         }
1301
1302         if (pdata->features & TWL6032_SUBCLASS) {
1303                 gpadc->twl6032_cal_tbl = kzalloc(
1304                                 sizeof(struct twl6032_chnl_calib) *
1305                                 TWL6032_GPADC_MAX_CHANNELS,
1306                                 GFP_KERNEL);
1307                 if (!gpadc->twl6032_cal_tbl) {
1308                         ret = -ENOMEM;
1309                         goto err_pdata;
1310                 }
1311         }
1312
1313         gpadc->dev = &pdev->dev;
1314
1315         gpadc->features = pdata->features;
1316
1317         twl6030_conversion_methods = twl6030_conversion_methods_table;
1318
1319         if (gpadc->features & TWL6032_SUBCLASS)
1320                 twl6030_conversion_methods = twl6032_conversion_methods_table;
1321
1322         ret = misc_register(&twl6030_gpadc_device);
1323         if (ret) {
1324                 dev_dbg(&pdev->dev, "could not register misc_device\n");
1325                 goto err_misc;
1326         }
1327
1328         irq_rt = platform_get_irq(pdev, 0);
1329         if (irq_rt < 0) {
1330                 dev_err(&pdev->dev, "failed to get irq\n");
1331                 goto err_irq;
1332         }
1333
1334         ret = request_threaded_irq(irq_rt, NULL, twl6030_gpadc_irq_handler,
1335                 0, "twl6030_gpadc", &gpadc->requests[TWL6030_GPADC_RT]);
1336         if (ret) {
1337                 dev_dbg(&pdev->dev, "could not request irq\n");
1338                 goto err_irq;
1339         }
1340
1341         irq = platform_get_irq(pdev, 1);
1342         if (irq < 0) {
1343                 dev_err(&pdev->dev, "failed to get irq\n");
1344                 goto err_irq_rt;
1345         }
1346
1347         ret = request_threaded_irq(irq, NULL, twl6030_gpadc_irq_handler,
1348                 0, "twl6030_gpadc", &gpadc->requests[TWL6030_GPADC_SW2]);
1349         if (ret) {
1350                 dev_dbg(&pdev->dev, "could not request irq\n");
1351                 goto err_irq_rt;
1352         }
1353
1354         platform_set_drvdata(pdev, gpadc);
1355         mutex_init(&gpadc->lock);
1356         INIT_WORK(&gpadc->ws, twl6030_gpadc_work);
1357
1358         if (gpadc->features & TWL6032_SUBCLASS)
1359                 ret = twl6032_calibration(gpadc);
1360         else
1361                 ret = twl6030_calibration();
1362         if (ret < 0) {
1363                 dev_err(&pdev->dev, "Failed to read calibration registers\n");
1364                 goto err_calib;
1365         }
1366         the_gpadc = gpadc;
1367
1368         ret = sysfs_create_group(&pdev->dev.kobj, &twl6030_gpadc_group);
1369         if (ret)
1370                 dev_err(&pdev->dev, "could not create sysfs files\n");
1371
1372         return 0;
1373
1374 err_calib:
1375         free_irq(irq, &gpadc->requests[TWL6030_GPADC_SW2]);
1376 err_irq_rt:
1377         free_irq(irq_rt, &gpadc->requests[TWL6030_GPADC_RT]);
1378 err_irq:
1379         misc_deregister(&twl6030_gpadc_device);
1380
1381 err_misc:
1382         if (pdata->features & TWL6032_SUBCLASS)
1383                 kfree(gpadc->twl6032_cal_tbl);
1384 err_pdata:
1385         kfree(gpadc);
1386
1387         return ret;
1388 }
1389
1390 static int __devexit twl6030_gpadc_remove(struct platform_device *pdev)
1391 {
1392         struct twl6030_gpadc_data *gpadc = platform_get_drvdata(pdev);
1393
1394         twl6030_gpadc_disable_irq(TWL6030_GPADC_RT);
1395         twl6030_gpadc_disable_irq(TWL6030_GPADC_SW2);
1396         free_irq(platform_get_irq(pdev, 0), gpadc);
1397         sysfs_remove_group(&pdev->dev.kobj, &twl6030_gpadc_group);
1398         cancel_work_sync(&gpadc->ws);
1399         misc_deregister(&twl6030_gpadc_device);
1400
1401         return 0;
1402 }
1403
1404 static int twl6030_gpadc_suspend(struct device *pdev)
1405 {
1406         int ret;
1407
1408         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCR, REG_TOGGLE1);
1409         if (ret)
1410                 pr_err("%s: Error reseting GPADC (%d)!\n", __func__, ret);
1411
1412         return 0;
1413 };
1414
1415 static int twl6030_gpadc_resume(struct device *pdev)
1416 {
1417         int ret;
1418
1419         ret = twl_i2c_write_u8(TWL6030_MODULE_ID1, GPADCS, REG_TOGGLE1);
1420         if (ret)
1421                 pr_err("%s: Error setting GPADC (%d)!\n", __func__, ret);
1422
1423         return 0;
1424 };
1425 static const struct dev_pm_ops twl6030_gpadc_pm_ops = {
1426         .suspend = twl6030_gpadc_suspend,
1427         .resume = twl6030_gpadc_resume,
1428 };
1429
1430 static struct platform_driver twl6030_gpadc_driver = {
1431         .probe          = twl6030_gpadc_probe,
1432         .remove         = __devexit_p(twl6030_gpadc_remove),
1433         .driver         = {
1434                 .name   = "twl6030_gpadc",
1435                 .owner  = THIS_MODULE,
1436                 .pm = &twl6030_gpadc_pm_ops,
1437         },
1438 };
1439
1440 static int __init twl6030_gpadc_init(void)
1441 {
1442         return platform_driver_register(&twl6030_gpadc_driver);
1443 }
1444 module_init(twl6030_gpadc_init);
1445
1446 static void __exit twl6030_gpadc_exit(void)
1447 {
1448         platform_driver_unregister(&twl6030_gpadc_driver);
1449 }
1450 module_exit(twl6030_gpadc_exit);
1451
1452 MODULE_ALIAS("platform:twl6030-gpadc");
1453 MODULE_AUTHOR("Texas Instruments Inc.");
1454 MODULE_DESCRIPTION("twl6030 ADC driver");
1455 MODULE_LICENSE("GPL");