config: tegra3: enable /dev mount with ACL
[linux-2.6.git] / drivers / mfd / tps8003x-gpadc.c
1 /*
2  * drivers/mfd/tps8003x-gpadc.c
3  *
4  * Gpadc for TI's tps80031
5  *
6  * Copyright (c) 2011, NVIDIA Corporation.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 #include <linux/init.h>
24 #include <linux/interrupt.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/module.h>
28 #include <linux/delay.h>
29 #include <linux/fs.h>
30 #include <linux/platform_device.h>
31 #include <linux/miscdevice.h>
32 #include <linux/slab.h>
33 #include <linux/hwmon-sysfs.h>
34 #include <linux/i2c/twl.h>
35 #include <linux/mfd/tps80031.h>
36 #include <linux/uaccess.h>
37 #include <linux/spinlock.h>
38
39 #define GPADC_CTRL      0x2e
40 #define GPSELECT_ISB    0x35
41 #define GPCH0_LSB       0x3b
42 #define GPCH0_MSB       0x3c
43 #define CTRL_P1         0x36
44 #define TOGGLE1         0x90
45 #define MISC1           0xe4
46
47 #define CTRL_P1_SP1     BIT(3)
48 #define TOGGLE1_GPADCR  BIT(1)
49 #define GPADC_BUSY      (1 << 0)
50 #define GPADC_EOC_SW    (1 << 1)
51 #define SCALE           (1 << 15)
52
53 #define TPS80031_GPADC_MAX_CHANNELS 17
54 #define TPS80031_GPADC_IOC_MAGIC '`'
55 #define TPS80031_GPADC_IOCX_ADC_RAW_READ _IO(TPS80031_GPADC_IOC_MAGIC, 0)
56
57 struct tps80031_gpadc_user_parms {
58         int channel;
59         int status;
60         u16 result;
61 };
62
63 struct tps80031_calibration {
64         s32 gain_error;
65         s32 offset_error;
66 };
67
68 struct tps80031_ideal_code {
69         s16 code1;
70         s16 code2;
71 };
72
73 struct tps80031_scalar_channel {
74         uint8_t delta1_addr;
75         uint8_t delta1_mask;
76         uint8_t delta2_addr;
77         uint8_t delta2_mask;
78 };
79
80 static struct tps80031_calibration
81         tps80031_calib_tbl[TPS80031_GPADC_MAX_CHANNELS];
82 static const uint32_t calibration_bit_map = 0x47FF;
83 static const uint32_t scalar_bit_map = 0x4785;
84
85 #define TPS80031_GPADC_TRIM1    0xCD
86 #define TPS80031_GPADC_TRIM2    0xCE
87 #define TPS80031_GPADC_TRIM3    0xCF
88 #define TPS80031_GPADC_TRIM4    0xD0
89 #define TPS80031_GPADC_TRIM5    0xD1
90 #define TPS80031_GPADC_TRIM6    0xD2
91 #define TPS80031_GPADC_TRIM7    0xD3
92 #define TPS80031_GPADC_TRIM8    0xD4
93 #define TPS80031_GPADC_TRIM9    0xD5
94 #define TPS80031_GPADC_TRIM10   0xD6
95 #define TPS80031_GPADC_TRIM11   0xD7
96 #define TPS80031_GPADC_TRIM12   0xD8
97 #define TPS80031_GPADC_TRIM13   0xD9
98 #define TPS80031_GPADC_TRIM14   0xDA
99 #define TPS80031_GPADC_TRIM15   0xDB
100 #define TPS80031_GPADC_TRIM16   0xDC
101 #define TPS80031_GPADC_TRIM19   0xFD
102
103 static const struct tps80031_scalar_channel
104                 tps80031_trim[TPS80031_GPADC_MAX_CHANNELS] = {
105         { TPS80031_GPADC_TRIM1, 0x7, TPS80031_GPADC_TRIM2, 0x07},
106         { 0x00, },
107         { TPS80031_GPADC_TRIM3, 0x1F, TPS80031_GPADC_TRIM4, 0x3F},
108         { 0x00, },
109         { 0x00, },
110         { 0x00, },
111         { 0x00, },
112         { TPS80031_GPADC_TRIM7, 0x1F, TPS80031_GPADC_TRIM8, 0x1F },
113         { TPS80031_GPADC_TRIM9, 0x0F, TPS80031_GPADC_TRIM10, 0x1F },
114         { TPS80031_GPADC_TRIM11, 0x0F, TPS80031_GPADC_TRIM12, 0x1F },
115         { TPS80031_GPADC_TRIM13, 0x0F, TPS80031_GPADC_TRIM14, 0x1F },
116         { 0x00, },
117         { 0x00, },
118         { 0x00, },
119         { TPS80031_GPADC_TRIM15, 0x0f, TPS80031_GPADC_TRIM16, 0x1F },
120         { 0x00, },
121         { 0x00 ,},
122 };
123
124 /*
125 * actual scaler gain is multiplied by 8 for fixed point operation
126 * 1.875 * 8 = 15
127 */
128 static const uint16_t tps80031_gain[TPS80031_GPADC_MAX_CHANNELS] = {
129         1142,   /* CHANNEL 0 */
130         8,      /* CHANNEL 1 */
131         /* 1.875 */
132         15,     /* CHANNEL 2 */
133         8,      /* CHANNEL 3 */
134         8,      /* CHANNEL 4 */
135         8,      /* CHANNEL 5 */
136         8,      /* CHANNEL 6 */
137         /* 5 */
138         40,     /* CHANNEL 7 */
139         /* 6.25 */
140         50,     /* CHANNEL 8 */
141         /* 11.25 */
142         90,     /* CHANNEL 9 */
143         /* 6.875 */
144         55,     /* CHANNEL 10 */
145         /* 1.875 */
146         15,     /* CHANNEL 11 */
147         8,      /* CHANNEL 12 */
148         8,      /* CHANNEL 13 */
149         /* 6.875 */
150         55,     /* CHANNEL 14 */
151         8,      /* CHANNEL 15 */
152         8,      /* CHANNEL 16 */
153 };
154
155 /*
156 * calibration not needed for channel 11, 12, 13, 15 and 16
157 * calibration offset is same for channel 1, 3, 4, 5
158 */
159 static const struct tps80031_ideal_code
160         tps80031_ideal[TPS80031_GPADC_MAX_CHANNELS] = {
161         {463,   2982},  /* CHANNEL 0 */
162         {328,   3604},  /* CHANNEL 1 */
163         {221,   3274},  /* CHANNEL 2 */
164         {328,   3604},  /* CHANNEL 3 */
165         {328,   3604},  /* CHANNEL 4 */
166         {328,   3604},  /* CHANNEL 5 */
167         {328,   3604},  /* CHANNEL 6 */
168         {1966,  3013},  /* CHANNEL 7 */
169         {328,   2754},  /* CHANNEL 8 */
170         {728,   3275},  /* CHANNEL 9 */
171         {596,   3274},  /* CHANNEL 10 */
172         {0,     0},     /* CHANNEL 11 */
173         {0,     0},     /* CHANNEL 12 */
174         {0,     0},     /* CHANNEL 13 */
175         {193,   2859},  /* CHANNEL 14 */
176         {0,     0},     /* CHANNEL 15 */
177         {0,     0},     /* CHANNEL 16 */
178 };
179
180 struct tps80031_gpadc_data {
181         struct device           *dev;
182         struct mutex            lock;
183 };
184
185 static struct tps80031_gpadc_data *the_gpadc;
186
187 static ssize_t show_gain(struct device *dev,
188                 struct device_attribute *devattr, char *buf)
189 {
190         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
191         int value;
192         int status;
193
194         value = tps80031_calib_tbl[attr->index].gain_error;
195         status = sprintf(buf, "%d\n", value);
196         return status;
197 }
198
199 static ssize_t set_gain(struct device *dev,
200         struct device_attribute *devattr, const char *buf, size_t count)
201 {
202         long val;
203         int status = count;
204
205         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
206         if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
207                                      || (val > 60000))
208                 return -EINVAL;
209         tps80031_calib_tbl[attr->index].gain_error = val;
210         return status;
211 }
212
213 static ssize_t show_offset(struct device *dev,
214                 struct device_attribute *devattr, char *buf)
215 {
216         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
217         int value;
218         int status;
219
220         value = tps80031_calib_tbl[attr->index].offset_error;
221         status = sprintf(buf, "%d\n", value);
222         return status;
223 }
224
225 static ssize_t set_offset(struct device *dev,
226         struct device_attribute *devattr, const char *buf, size_t count)
227 {
228         long val;
229         int status = count;
230
231         struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
232         if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
233                                                         || (val > 60000))
234                 return -EINVAL;
235         tps80031_calib_tbl[attr->index].offset_error = val;
236         return status;
237 }
238
239 static int tps80031_reg_read(struct tps80031_gpadc_data *gpadc, int sid,
240                                 int reg, uint8_t *val)
241 {
242         int ret;
243
244         ret = tps80031_read(gpadc->dev->parent, sid, reg, val);
245         if (ret < 0)
246                 dev_err(gpadc->dev, "Failed read register 0x%02x\n", reg);
247         return ret;
248 }
249
250 static int tps80031_reg_write(struct tps80031_gpadc_data *gpadc, int sid,
251                                 int reg, uint8_t val)
252 {
253         int ret;
254
255         ret = tps80031_write(gpadc->dev->parent, sid, reg, val);
256         if (ret < 0)
257                 dev_err(gpadc->dev, "Failed write register 0x%02x\n", reg);
258         return ret;
259 }
260
261 static int tps80031_gpadc_channel_raw_read(struct tps80031_gpadc_data *gpadc)
262 {
263         uint8_t msb, lsb;
264         int ret;
265         ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_LSB, &lsb);
266         if (ret < 0)
267                 return ret;
268         ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_MSB, &msb);
269         if (ret < 0)
270                 return ret;
271
272         return (int)((msb << 8) | lsb);
273 }
274
275 static int tps80031_gpadc_read_channels(struct tps80031_gpadc_data *gpadc,
276                                                 uint32_t channel)
277 {
278         uint8_t bits;
279         int gain_error;
280         int offset_error;
281         int raw_code;
282         int corrected_code;
283         int channel_value;
284         int raw_channel_value;
285
286         /* TPS80031 has 12bit ADC */
287         bits = 12;
288         raw_code = tps80031_gpadc_channel_raw_read(gpadc);
289         if (raw_code < 0)
290                 return raw_code;
291         /*
292          * Channels 0,2,7,8,9,10,14 offst and gain cannot
293          * be fully compensated by software
294          */
295         if (channel == 7)
296                 return raw_code;
297         /*
298          * multiply by 1000 to convert the unit to milli
299          * division by 1024 (>> bits) for 10/12 bit ADC
300          * division by 8 (>> 3) for actual scaler gain
301          */
302         raw_channel_value =
303                 (raw_code * tps80031_gain[channel] * 1000) >> (bits + 3);
304
305         gain_error = tps80031_calib_tbl[channel].gain_error;
306         offset_error = tps80031_calib_tbl[channel].offset_error;
307         corrected_code = (raw_code * SCALE - offset_error) / gain_error;
308         channel_value =
309                 (corrected_code * tps80031_gain[channel] * 1000) >> (bits + 3);
310         return channel_value;
311 }
312
313 static int tps80031_gpadc_wait_conversion_ready(
314         struct tps80031_gpadc_data *gpadc,
315                 unsigned int timeout_ms)
316 {
317         int ret;
318         unsigned long timeout;
319         timeout = jiffies + msecs_to_jiffies(timeout_ms);
320         do {
321                 uint8_t reg;
322                 ret = tps80031_reg_read(gpadc, SLAVE_ID2, CTRL_P1, &reg);
323                 if (ret < 0)
324                         return ret;
325                 if (!(reg & GPADC_BUSY) &&
326                                 (reg & GPADC_EOC_SW))
327                         return 0;
328         } while (!time_after(jiffies, timeout));
329         return -EAGAIN;
330 }
331
332 static inline int tps80031_gpadc_config
333         (struct tps80031_gpadc_data *gpadc, int channel_no)
334 {
335         int ret = 0;
336
337         ret = tps80031_reg_write(gpadc, SLAVE_ID2, TOGGLE1, TOGGLE1_GPADCR);
338         if (ret < 0)
339                 return ret;
340
341         ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPSELECT_ISB, channel_no);
342         if (ret < 0)
343                 return ret;
344
345         ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPADC_CTRL, 0xef);
346         if (ret < 0)
347                 return ret;
348
349         ret = tps80031_reg_write(gpadc, SLAVE_ID1, MISC1, 0x02);
350         if (ret < 0)
351                 return ret;
352
353         return ret;
354 }
355
356 int tps80031_gpadc_conversion(int channel_no)
357 {
358         int ret = 0;
359         int read_value;
360
361         mutex_lock(&the_gpadc->lock);
362
363         ret = tps80031_gpadc_config(the_gpadc, channel_no);
364         if (ret < 0)
365                 goto err;
366
367         /* start ADC conversion */
368         ret = tps80031_reg_write(the_gpadc, SLAVE_ID2, CTRL_P1, CTRL_P1_SP1);
369         if (ret < 0)
370                 goto err;
371
372         /* Wait until conversion is ready (ctrl register returns EOC) */
373         ret = tps80031_gpadc_wait_conversion_ready(the_gpadc, 5);
374         if (ret) {
375                 dev_dbg(the_gpadc->dev, "conversion timeout!\n");
376                 goto err;
377         }
378
379         read_value = tps80031_gpadc_read_channels(the_gpadc, channel_no);
380         mutex_unlock(&the_gpadc->lock);
381         return read_value;
382 err:
383         mutex_unlock(&the_gpadc->lock);
384         return ret;
385 }
386 EXPORT_SYMBOL_GPL(tps80031_gpadc_conversion);
387
388 static SENSOR_DEVICE_ATTR(in0_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 0);
389 static SENSOR_DEVICE_ATTR(in0_offset, S_IRUGO|S_IWUSR,
390                                                 show_offset, set_offset, 0);
391 static SENSOR_DEVICE_ATTR(in1_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 1);
392 static SENSOR_DEVICE_ATTR(in1_offset, S_IRUGO|S_IWUSR,
393                                                 show_offset, set_offset, 1);
394 static SENSOR_DEVICE_ATTR(in2_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 2);
395 static SENSOR_DEVICE_ATTR(in2_offset, S_IRUGO|S_IWUSR,
396                                                 show_offset, set_offset, 2);
397 static SENSOR_DEVICE_ATTR(in3_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 3);
398 static SENSOR_DEVICE_ATTR(in3_offset, S_IRUGO|S_IWUSR,
399                                                 show_offset, set_offset, 3);
400 static SENSOR_DEVICE_ATTR(in4_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 4);
401 static SENSOR_DEVICE_ATTR(in4_offset, S_IRUGO|S_IWUSR,
402                                                 show_offset, set_offset, 4);
403 static SENSOR_DEVICE_ATTR(in5_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 5);
404 static SENSOR_DEVICE_ATTR(in5_offset, S_IRUGO|S_IWUSR,
405                                                 show_offset, set_offset, 5);
406 static SENSOR_DEVICE_ATTR(in6_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 6);
407 static SENSOR_DEVICE_ATTR(in6_offset, S_IRUGO|S_IWUSR,
408                                                 show_offset, set_offset, 6);
409 static SENSOR_DEVICE_ATTR(in7_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 7);
410 static SENSOR_DEVICE_ATTR(in7_offset, S_IRUGO|S_IWUSR,
411                                                 show_offset, set_offset, 7);
412 static SENSOR_DEVICE_ATTR(in8_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 8);
413 static SENSOR_DEVICE_ATTR(in8_offset, S_IRUGO|S_IWUSR,
414                                                 show_offset, set_offset, 8);
415 static SENSOR_DEVICE_ATTR(in9_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 9);
416 static SENSOR_DEVICE_ATTR(in9_offset, S_IRUGO|S_IWUSR,
417                                                 show_offset, set_offset, 9);
418 static SENSOR_DEVICE_ATTR(in10_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 10);
419 static SENSOR_DEVICE_ATTR(in10_offset, S_IRUGO|S_IWUSR,
420                                                 show_offset, set_offset, 10);
421 static SENSOR_DEVICE_ATTR(in11_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 11);
422 static SENSOR_DEVICE_ATTR(in11_offset, S_IRUGO|S_IWUSR,
423                                                 show_offset, set_offset, 11);
424 static SENSOR_DEVICE_ATTR(in12_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 12);
425 static SENSOR_DEVICE_ATTR(in12_offset, S_IRUGO|S_IWUSR,
426                                                 show_offset, set_offset, 12);
427 static SENSOR_DEVICE_ATTR(in13_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 13);
428 static SENSOR_DEVICE_ATTR(in13_offset, S_IRUGO|S_IWUSR,
429                                                 show_offset, set_offset, 13);
430 static SENSOR_DEVICE_ATTR(in14_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 14);
431 static SENSOR_DEVICE_ATTR(in14_offset, S_IRUGO|S_IWUSR,
432                                                 show_offset, set_offset, 14);
433 static SENSOR_DEVICE_ATTR(in15_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 15);
434 static SENSOR_DEVICE_ATTR(in15_offset, S_IRUGO|S_IWUSR,
435                                                 show_offset, set_offset, 15);
436 static SENSOR_DEVICE_ATTR(in16_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 16);
437 static SENSOR_DEVICE_ATTR(in16_offset, S_IRUGO|S_IWUSR,
438                                                 show_offset, set_offset, 16);
439
440 #define IN_ATTRS(X)\
441         &sensor_dev_attr_in##X##_gain.dev_attr.attr,    \
442         &sensor_dev_attr_in##X##_offset.dev_attr.attr   \
443
444 static struct attribute *tps80031_gpadc_attributes[] = {
445         IN_ATTRS(0),
446         IN_ATTRS(1),
447         IN_ATTRS(2),
448         IN_ATTRS(3),
449         IN_ATTRS(4),
450         IN_ATTRS(5),
451         IN_ATTRS(6),
452         IN_ATTRS(7),
453         IN_ATTRS(8),
454         IN_ATTRS(9),
455         IN_ATTRS(10),
456         IN_ATTRS(11),
457         IN_ATTRS(12),
458         IN_ATTRS(13),
459         IN_ATTRS(14),
460         IN_ATTRS(15),
461         IN_ATTRS(16),
462         NULL
463 };
464
465 static const struct attribute_group tps80031_gpadc_group = {
466         .attrs = tps80031_gpadc_attributes,
467 };
468
469 static long tps80031_gpadc_ioctl(struct file *filp, unsigned int cmd,
470                                                 unsigned long arg)
471 {
472         struct tps80031_gpadc_user_parms par;
473         int val, ret, channel_no;
474
475         ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
476         if (ret) {
477                 dev_dbg(the_gpadc->dev, "copy_from_user: %d\n", ret);
478                 return -EACCES;
479         }
480         switch (cmd) {
481         case TPS80031_GPADC_IOCX_ADC_RAW_READ:
482                 channel_no = par.channel;
483                 val = tps80031_gpadc_conversion(channel_no);
484                 if (likely(val > 0)) {
485                         par.status = 0;
486                         par.result = val;
487                 } else if (val == 0) {
488                         par.status = -ENODATA;
489                 } else {
490                         par.status = val;
491                 }
492                 break;
493         default:
494                 return -EINVAL;
495         }
496         ret = copy_to_user((void __user *) arg, &par, sizeof(par));
497         if (ret) {
498                 dev_dbg(the_gpadc->dev, "copy_to_user: %d\n", ret);
499                 return -EACCES;
500         }
501         return 0;
502 }
503
504 static const struct file_operations tps80031_gpadc_fileops = {
505         .owner = THIS_MODULE,
506         .unlocked_ioctl = tps80031_gpadc_ioctl,
507 };
508
509 static struct miscdevice tps80031_gpadc_device = {
510         .minor = MISC_DYNAMIC_MINOR,
511         .name = "tps80031-gpadc",
512         .fops = &tps80031_gpadc_fileops
513 };
514
515 static int __devinit tps80031_gpadc_probe(struct platform_device *pdev)
516 {
517         struct tps80031_gpadc_data *gpadc;
518
519         s16 delta_error1 = 0, delta_error2 = 0;
520         s16 ideal_code1, ideal_code2;
521         s16 scalar_delta1 = 0, scalar_delta2 = 0;
522         s32 gain_error_1;
523         s32 offset_error;
524         uint8_t l_delta1, l_delta2, h_delta2;
525         uint8_t l_scalar1, l_scalar2;
526         uint8_t sign;
527         uint8_t index;
528         int ret;
529
530         gpadc = devm_kzalloc(&pdev->dev, sizeof *gpadc, GFP_KERNEL);
531         if (!gpadc)
532                 return -ENOMEM;
533
534         gpadc->dev = &pdev->dev;
535         ret = misc_register(&tps80031_gpadc_device);
536         if (ret) {
537                 dev_dbg(&pdev->dev, "could not register misc_device\n");
538                 return ret;
539         }
540
541         platform_set_drvdata(pdev, gpadc);
542         mutex_init(&gpadc->lock);
543
544         for (index = 0; index < TPS80031_GPADC_MAX_CHANNELS; index++) {
545                 if (~calibration_bit_map & (1 << index))
546                         continue;
547
548                 if (~scalar_bit_map & (1 << index)) {
549                         ret = tps80031_reg_read(gpadc, SLAVE_ID2,
550                                 tps80031_trim[index].delta1_addr, &l_scalar1);
551                         if (ret < 0)
552                                 goto err;
553                         ret = tps80031_reg_read(gpadc, SLAVE_ID2,
554                                 tps80031_trim[index].delta2_addr, &l_scalar2);
555                         if (ret < 0)
556                                 goto err;
557
558                         l_scalar1 &= tps80031_trim[index].delta1_mask;
559                         sign = l_scalar1 & 1;
560                         scalar_delta1 = l_scalar1 >> 1;
561                         if (sign)
562                                 scalar_delta1 = 0 - scalar_delta1;
563                         l_scalar2 &= tps80031_trim[index].delta2_mask;
564                         sign = l_scalar2 & 1;
565                         scalar_delta2 = l_scalar2 >> 1;
566                         if (sign)
567                                 scalar_delta2 = 0 - scalar_delta2;
568                 } else {
569                         scalar_delta1 = 0;
570                         scalar_delta2 = 0;
571                 }
572                 ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM5,
573                                                         &l_delta1);
574                 if (ret < 0)
575                         goto err;
576                 ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM6,
577                                                         &l_delta2);
578                 if (ret < 0)
579                         goto err;
580                 ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM19,
581                                                         &h_delta2);
582                 if (ret < 0)
583                         goto err;
584
585                 sign = l_delta1 & 1;
586
587                 delta_error1 = l_delta1 >> 1;
588                 if (sign)
589                         delta_error1 = (0 - delta_error1);
590                 sign = l_delta2 & 1;
591
592                 delta_error2 = (l_delta2 >> 1) | (h_delta2 << 7);
593                 if (sign)
594                         delta_error2 = (0 - delta_error2);
595                 ideal_code1 = tps80031_ideal[index].code1 * 4;
596                 ideal_code2 = tps80031_ideal[index].code2 * 4;
597
598                 gain_error_1 = ((delta_error2 + scalar_delta2) -
599                                 (delta_error1 - scalar_delta1)) *
600                                 SCALE / (ideal_code2 - ideal_code1);
601                 offset_error = (delta_error1 + scalar_delta1) *
602                                 SCALE - gain_error_1 *  ideal_code1;
603
604                 tps80031_calib_tbl[index].gain_error = gain_error_1 + SCALE;
605                 tps80031_calib_tbl[index].offset_error = offset_error;
606         }
607
608         the_gpadc = gpadc;
609         ret = sysfs_create_group(&pdev->dev.kobj, &tps80031_gpadc_group);
610         if (ret) {
611                 dev_err(&pdev->dev, "could not create sysfs files\n");
612                 goto err;
613         }
614         return 0;
615 err:
616         misc_deregister(&tps80031_gpadc_device);
617         return ret;
618 }
619
620 static int __devexit tps80031_gpadc_remove(struct platform_device *pdev)
621 {
622         sysfs_remove_group(&pdev->dev.kobj, &tps80031_gpadc_group);
623         misc_deregister(&tps80031_gpadc_device);
624         return 0;
625 }
626
627 static struct platform_driver tps80031_gpadc_driver = {
628         .probe          = tps80031_gpadc_probe,
629         .remove         = __devexit_p(tps80031_gpadc_remove),
630         .driver         = {
631                 .name   = "tps80031-gpadc",
632                 .owner  = THIS_MODULE,
633         },
634 };
635
636 static int __init tps80031_gpadc_init(void)
637 {
638         return platform_driver_register(&tps80031_gpadc_driver);
639 }
640
641 module_init(tps80031_gpadc_init);
642
643 static void __exit tps80031_gpadc_exit(void)
644 {
645         platform_driver_unregister(&tps80031_gpadc_driver);
646 }
647
648 module_exit(tps80031_gpadc_exit);
649 MODULE_ALIAS("platform:tps80031-gpadc");
650 MODULE_DESCRIPTION("tps80031 ADC driver");