summaryrefslogtreecommitdiff
path: root/drivers/mfd/tps8003x-gpadc.c
blob: 5db912cc01fd7c2d0d6c8fd510820e69aab32ec4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/*
 * drivers/mfd/tps8003x-gpadc.c
 *
 * Gpadc for TI's tps80031
 *
 * Copyright (c) 2011, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/slab.h>
#include <linux/hwmon-sysfs.h>
#include <linux/i2c/twl.h>
#include <linux/mfd/tps80031.h>
#include <linux/uaccess.h>
#include <linux/spinlock.h>

#define GPADC_CTRL	0x2e
#define GPSELECT_ISB	0x35
#define GPCH0_LSB	0x3b
#define GPCH0_MSB	0x3c
#define CTRL_P1		0x36
#define TOGGLE1		0x90
#define MISC1		0xe4

#define CTRL_P1_SP1	BIT(3)
#define TOGGLE1_GPADCR	BIT(1)
#define GPADC_BUSY	(1 << 0)
#define GPADC_EOC_SW	(1 << 1)
#define SCALE		(1 << 15)

#define TPS80031_GPADC_MAX_CHANNELS 17
#define TPS80031_GPADC_IOC_MAGIC '`'
#define TPS80031_GPADC_IOCX_ADC_RAW_READ _IO(TPS80031_GPADC_IOC_MAGIC, 0)

struct tps80031_gpadc_user_parms {
	int channel;
	int status;
	u16 result;
};

struct tps80031_calibration {
	s32 gain_error;
	s32 offset_error;
};

struct tps80031_ideal_code {
	s16 code1;
	s16 code2;
};

struct tps80031_scalar_channel {
	uint8_t delta1_addr;
	uint8_t delta1_mask;
	uint8_t delta2_addr;
	uint8_t delta2_mask;
};

static struct tps80031_calibration
	tps80031_calib_tbl[TPS80031_GPADC_MAX_CHANNELS];
static const uint32_t calibration_bit_map = 0x47FF;
static const uint32_t scalar_bit_map = 0x4785;

#define TPS80031_GPADC_TRIM1	0xCD
#define TPS80031_GPADC_TRIM2	0xCE
#define TPS80031_GPADC_TRIM3	0xCF
#define TPS80031_GPADC_TRIM4	0xD0
#define TPS80031_GPADC_TRIM5	0xD1
#define TPS80031_GPADC_TRIM6	0xD2
#define TPS80031_GPADC_TRIM7	0xD3
#define TPS80031_GPADC_TRIM8	0xD4
#define TPS80031_GPADC_TRIM9	0xD5
#define TPS80031_GPADC_TRIM10	0xD6
#define TPS80031_GPADC_TRIM11	0xD7
#define TPS80031_GPADC_TRIM12	0xD8
#define TPS80031_GPADC_TRIM13	0xD9
#define TPS80031_GPADC_TRIM14	0xDA
#define TPS80031_GPADC_TRIM15	0xDB
#define TPS80031_GPADC_TRIM16	0xDC
#define TPS80031_GPADC_TRIM19	0xFD

static const struct tps80031_scalar_channel
		tps80031_trim[TPS80031_GPADC_MAX_CHANNELS] = {
	{ TPS80031_GPADC_TRIM1, 0x7, TPS80031_GPADC_TRIM2, 0x07},
	{ 0x00, },
	{ TPS80031_GPADC_TRIM3, 0x1F, TPS80031_GPADC_TRIM4, 0x3F},
	{ 0x00, },
	{ 0x00, },
	{ 0x00, },
	{ 0x00, },
	{ TPS80031_GPADC_TRIM7, 0x1F, TPS80031_GPADC_TRIM8, 0x1F },
	{ TPS80031_GPADC_TRIM9, 0x0F, TPS80031_GPADC_TRIM10, 0x1F },
	{ TPS80031_GPADC_TRIM11, 0x0F, TPS80031_GPADC_TRIM12, 0x1F },
	{ TPS80031_GPADC_TRIM13, 0x0F, TPS80031_GPADC_TRIM14, 0x1F },
	{ 0x00, },
	{ 0x00, },
	{ 0x00, },
	{ TPS80031_GPADC_TRIM15, 0x0f, TPS80031_GPADC_TRIM16, 0x1F },
	{ 0x00, },
	{ 0x00 ,},
};

/*
* actual scaler gain is multiplied by 8 for fixed point operation
* 1.875 * 8 = 15
*/
static const uint16_t tps80031_gain[TPS80031_GPADC_MAX_CHANNELS] = {
	1142,   /* CHANNEL 0 */
	8,      /* CHANNEL 1 */
	/* 1.875 */
	15,     /* CHANNEL 2 */
	8,      /* CHANNEL 3 */
	8,      /* CHANNEL 4 */
	8,      /* CHANNEL 5 */
	8,      /* CHANNEL 6 */
	/* 5 */
	40,     /* CHANNEL 7 */
	/* 6.25 */
	50,     /* CHANNEL 8 */
	/* 11.25 */
	90,     /* CHANNEL 9 */
	/* 6.875 */
	55,     /* CHANNEL 10 */
	/* 1.875 */
	15,     /* CHANNEL 11 */
	8,      /* CHANNEL 12 */
	8,      /* CHANNEL 13 */
	/* 6.875 */
	55,     /* CHANNEL 14 */
	8,      /* CHANNEL 15 */
	8,      /* CHANNEL 16 */
};

/*
* calibration not needed for channel 11, 12, 13, 15 and 16
* calibration offset is same for channel 1, 3, 4, 5
*/
static const struct tps80031_ideal_code
	tps80031_ideal[TPS80031_GPADC_MAX_CHANNELS] = {
	{463,   2982},  /* CHANNEL 0 */
	{328,   3604},  /* CHANNEL 1 */
	{221,   3274},  /* CHANNEL 2 */
	{328,   3604},  /* CHANNEL 3 */
	{328,   3604},  /* CHANNEL 4 */
	{328,   3604},  /* CHANNEL 5 */
	{328,   3604},  /* CHANNEL 6 */
	{1966,  3013},  /* CHANNEL 7 */
	{328,   2754},  /* CHANNEL 8 */
	{728,   3275},  /* CHANNEL 9 */
	{596,   3274},  /* CHANNEL 10 */
	{0,     0},     /* CHANNEL 11 */
	{0,     0},     /* CHANNEL 12 */
	{0,     0},     /* CHANNEL 13 */
	{193,   2859},  /* CHANNEL 14 */
	{0,     0},     /* CHANNEL 15 */
	{0,     0},     /* CHANNEL 16 */
};

struct tps80031_gpadc_data {
	struct device		*dev;
	struct mutex		lock;
};

static struct tps80031_gpadc_data *the_gpadc;

static ssize_t show_gain(struct device *dev,
		struct device_attribute *devattr, char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	int value;
	int status;

	value = tps80031_calib_tbl[attr->index].gain_error;
	status = sprintf(buf, "%d\n", value);
	return status;
}

static ssize_t set_gain(struct device *dev,
	struct device_attribute *devattr, const char *buf, size_t count)
{
	long val;
	int status = count;

	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
				     || (val > 60000))
		return -EINVAL;
	tps80031_calib_tbl[attr->index].gain_error = val;
	return status;
}

static ssize_t show_offset(struct device *dev,
		struct device_attribute *devattr, char *buf)
{
	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	int value;
	int status;

	value = tps80031_calib_tbl[attr->index].offset_error;
	status = sprintf(buf, "%d\n", value);
	return status;
}

static ssize_t set_offset(struct device *dev,
	struct device_attribute *devattr, const char *buf, size_t count)
{
	long val;
	int status = count;

	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
	if ((strict_strtol(buf, 10, &val) < 0) || (val < 15000)
							|| (val > 60000))
		return -EINVAL;
	tps80031_calib_tbl[attr->index].offset_error = val;
	return status;
}

static int tps80031_reg_read(struct tps80031_gpadc_data *gpadc, int sid,
				int reg, uint8_t *val)
{
	int ret;

	ret = tps80031_read(gpadc->dev->parent, sid, reg, val);
	if (ret < 0)
		dev_err(gpadc->dev, "Failed read register 0x%02x\n", reg);
	return ret;
}

static int tps80031_reg_write(struct tps80031_gpadc_data *gpadc, int sid,
				int reg, uint8_t val)
{
	int ret;

	ret = tps80031_write(gpadc->dev->parent, sid, reg, val);
	if (ret < 0)
		dev_err(gpadc->dev, "Failed write register 0x%02x\n", reg);
	return ret;
}

static int tps80031_gpadc_channel_raw_read(struct tps80031_gpadc_data *gpadc)
{
	uint8_t msb, lsb;
	int ret;
	ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_LSB, &lsb);
	if (ret < 0)
		return ret;
	ret = tps80031_reg_read(gpadc, SLAVE_ID2, GPCH0_MSB, &msb);
	if (ret < 0)
		return ret;

	return (int)((msb << 8) | lsb);
}

static int tps80031_gpadc_read_channels(struct tps80031_gpadc_data *gpadc,
						uint32_t channel)
{
	uint8_t bits;
	int gain_error;
	int offset_error;
	int raw_code;
	int corrected_code;
	int channel_value;
	int raw_channel_value;

	/* TPS80031 has 12bit ADC */
	bits = 12;
	raw_code = tps80031_gpadc_channel_raw_read(gpadc);
	if (raw_code < 0)
		return raw_code;
	/*
	 * Channels 0,2,7,8,9,10,14 offst and gain cannot
	 * be fully compensated by software
	 */
	if (channel == 7)
		return raw_code;
	/*
	 * multiply by 1000 to convert the unit to milli
	 * division by 1024 (>> bits) for 10/12 bit ADC
	 * division by 8 (>> 3) for actual scaler gain
	 */
	raw_channel_value =
		(raw_code * tps80031_gain[channel] * 1000) >> (bits + 3);

	gain_error = tps80031_calib_tbl[channel].gain_error;
	offset_error = tps80031_calib_tbl[channel].offset_error;
	corrected_code = (raw_code * SCALE - offset_error) / gain_error;
	channel_value =
		(corrected_code * tps80031_gain[channel] * 1000) >> (bits + 3);
	return channel_value;
}

static int tps80031_gpadc_wait_conversion_ready(
	struct tps80031_gpadc_data *gpadc,
		unsigned int timeout_ms)
{
	int ret;
	unsigned long timeout;
	timeout = jiffies + msecs_to_jiffies(timeout_ms);
	do {
		uint8_t reg;
		ret = tps80031_reg_read(gpadc, SLAVE_ID2, CTRL_P1, &reg);
		if (ret < 0)
			return ret;
		if (!(reg & GPADC_BUSY) &&
				(reg & GPADC_EOC_SW))
			return 0;
	} while (!time_after(jiffies, timeout));
	return -EAGAIN;
}

static inline int tps80031_gpadc_config
	(struct tps80031_gpadc_data *gpadc, int channel_no)
{
	int ret = 0;

	ret = tps80031_reg_write(gpadc, SLAVE_ID2, TOGGLE1, TOGGLE1_GPADCR);
	if (ret < 0)
		return ret;

	ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPSELECT_ISB, channel_no);
	if (ret < 0)
		return ret;

	ret = tps80031_reg_write(gpadc, SLAVE_ID2, GPADC_CTRL, 0xef);
	if (ret < 0)
		return ret;

	ret = tps80031_reg_write(gpadc, SLAVE_ID1, MISC1, 0x02);
	if (ret < 0)
		return ret;

	return ret;
}

int tps80031_gpadc_conversion(int channel_no)
{
	int ret = 0;
	int read_value;

	mutex_lock(&the_gpadc->lock);

	ret = tps80031_gpadc_config(the_gpadc, channel_no);
	if (ret < 0)
		goto err;

	/* start ADC conversion */
	ret = tps80031_reg_write(the_gpadc, SLAVE_ID2, CTRL_P1, CTRL_P1_SP1);
	if (ret < 0)
		goto err;

	/* Wait until conversion is ready (ctrl register returns EOC) */
	ret = tps80031_gpadc_wait_conversion_ready(the_gpadc, 5);
	if (ret) {
		dev_dbg(the_gpadc->dev, "conversion timeout!\n");
		goto err;
	}

	read_value = tps80031_gpadc_read_channels(the_gpadc, channel_no);
	mutex_unlock(&the_gpadc->lock);
	return read_value;
err:
	mutex_unlock(&the_gpadc->lock);
	return ret;
}
EXPORT_SYMBOL_GPL(tps80031_gpadc_conversion);

static SENSOR_DEVICE_ATTR(in0_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 0);
static SENSOR_DEVICE_ATTR(in0_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 0);
static SENSOR_DEVICE_ATTR(in1_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 1);
static SENSOR_DEVICE_ATTR(in1_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 1);
static SENSOR_DEVICE_ATTR(in2_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 2);
static SENSOR_DEVICE_ATTR(in2_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 2);
static SENSOR_DEVICE_ATTR(in3_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 3);
static SENSOR_DEVICE_ATTR(in3_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 3);
static SENSOR_DEVICE_ATTR(in4_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 4);
static SENSOR_DEVICE_ATTR(in4_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 4);
static SENSOR_DEVICE_ATTR(in5_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 5);
static SENSOR_DEVICE_ATTR(in5_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 5);
static SENSOR_DEVICE_ATTR(in6_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 6);
static SENSOR_DEVICE_ATTR(in6_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 6);
static SENSOR_DEVICE_ATTR(in7_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 7);
static SENSOR_DEVICE_ATTR(in7_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 7);
static SENSOR_DEVICE_ATTR(in8_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 8);
static SENSOR_DEVICE_ATTR(in8_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 8);
static SENSOR_DEVICE_ATTR(in9_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 9);
static SENSOR_DEVICE_ATTR(in9_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 9);
static SENSOR_DEVICE_ATTR(in10_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 10);
static SENSOR_DEVICE_ATTR(in10_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 10);
static SENSOR_DEVICE_ATTR(in11_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 11);
static SENSOR_DEVICE_ATTR(in11_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 11);
static SENSOR_DEVICE_ATTR(in12_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 12);
static SENSOR_DEVICE_ATTR(in12_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 12);
static SENSOR_DEVICE_ATTR(in13_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 13);
static SENSOR_DEVICE_ATTR(in13_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 13);
static SENSOR_DEVICE_ATTR(in14_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 14);
static SENSOR_DEVICE_ATTR(in14_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 14);
static SENSOR_DEVICE_ATTR(in15_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 15);
static SENSOR_DEVICE_ATTR(in15_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 15);
static SENSOR_DEVICE_ATTR(in16_gain, S_IRUGO|S_IWUSR, show_gain, set_gain, 16);
static SENSOR_DEVICE_ATTR(in16_offset, S_IRUGO|S_IWUSR,
						show_offset, set_offset, 16);

#define IN_ATTRS(X)\
	&sensor_dev_attr_in##X##_gain.dev_attr.attr,    \
	&sensor_dev_attr_in##X##_offset.dev_attr.attr   \

static struct attribute *tps80031_gpadc_attributes[] = {
	IN_ATTRS(0),
	IN_ATTRS(1),
	IN_ATTRS(2),
	IN_ATTRS(3),
	IN_ATTRS(4),
	IN_ATTRS(5),
	IN_ATTRS(6),
	IN_ATTRS(7),
	IN_ATTRS(8),
	IN_ATTRS(9),
	IN_ATTRS(10),
	IN_ATTRS(11),
	IN_ATTRS(12),
	IN_ATTRS(13),
	IN_ATTRS(14),
	IN_ATTRS(15),
	IN_ATTRS(16),
	NULL
};

static const struct attribute_group tps80031_gpadc_group = {
	.attrs = tps80031_gpadc_attributes,
};

static long tps80031_gpadc_ioctl(struct file *filp, unsigned int cmd,
						unsigned long arg)
{
	struct tps80031_gpadc_user_parms par;
	int val, ret, channel_no;

	ret = copy_from_user(&par, (void __user *) arg, sizeof(par));
	if (ret) {
		dev_dbg(the_gpadc->dev, "copy_from_user: %d\n", ret);
		return -EACCES;
	}
	switch (cmd) {
	case TPS80031_GPADC_IOCX_ADC_RAW_READ:
		channel_no = par.channel;
		val = tps80031_gpadc_conversion(channel_no);
		if (likely(val > 0)) {
			par.status = 0;
			par.result = val;
		} else if (val == 0) {
			par.status = -ENODATA;
		} else {
			par.status = val;
		}
		break;
	default:
		return -EINVAL;
	}
	ret = copy_to_user((void __user *) arg, &par, sizeof(par));
	if (ret) {
		dev_dbg(the_gpadc->dev, "copy_to_user: %d\n", ret);
		return -EACCES;
	}
	return 0;
}

static const struct file_operations tps80031_gpadc_fileops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = tps80031_gpadc_ioctl,
};

static struct miscdevice tps80031_gpadc_device = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "tps80031-gpadc",
	.fops = &tps80031_gpadc_fileops
};

static int __devinit tps80031_gpadc_probe(struct platform_device *pdev)
{
	struct tps80031_gpadc_data *gpadc;

	s16 delta_error1 = 0, delta_error2 = 0;
	s16 ideal_code1, ideal_code2;
	s16 scalar_delta1 = 0, scalar_delta2 = 0;
	s32 gain_error_1;
	s32 offset_error;
	uint8_t l_delta1, l_delta2, h_delta2;
	uint8_t l_scalar1, l_scalar2;
	uint8_t sign;
	uint8_t index;
	int ret;

	gpadc = devm_kzalloc(&pdev->dev, sizeof *gpadc, GFP_KERNEL);
	if (!gpadc)
		return -ENOMEM;

	gpadc->dev = &pdev->dev;
	ret = misc_register(&tps80031_gpadc_device);
	if (ret) {
		dev_dbg(&pdev->dev, "could not register misc_device\n");
		return ret;
	}

	platform_set_drvdata(pdev, gpadc);
	mutex_init(&gpadc->lock);

	for (index = 0; index < TPS80031_GPADC_MAX_CHANNELS; index++) {
		if (~calibration_bit_map & (1 << index))
			continue;

		if (~scalar_bit_map & (1 << index)) {
			ret = tps80031_reg_read(gpadc, SLAVE_ID2,
				tps80031_trim[index].delta1_addr, &l_scalar1);
			if (ret < 0)
				goto err;
			ret = tps80031_reg_read(gpadc, SLAVE_ID2,
				tps80031_trim[index].delta2_addr, &l_scalar2);
			if (ret < 0)
				goto err;

			l_scalar1 &= tps80031_trim[index].delta1_mask;
			sign = l_scalar1 & 1;
			scalar_delta1 = l_scalar1 >> 1;
			if (sign)
				scalar_delta1 = 0 - scalar_delta1;
			l_scalar2 &= tps80031_trim[index].delta2_mask;
			sign = l_scalar2 & 1;
			scalar_delta2 = l_scalar2 >> 1;
			if (sign)
				scalar_delta2 = 0 - scalar_delta2;
		} else {
			scalar_delta1 = 0;
			scalar_delta2 = 0;
		}
		ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM5,
							&l_delta1);
		if (ret < 0)
			goto err;
		ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM6,
							&l_delta2);
		if (ret < 0)
			goto err;
		ret = tps80031_reg_read(gpadc, SLAVE_ID2, TPS80031_GPADC_TRIM19,
							&h_delta2);
		if (ret < 0)
			goto err;

		sign = l_delta1 & 1;

		delta_error1 = l_delta1 >> 1;
		if (sign)
			delta_error1 = (0 - delta_error1);
		sign = l_delta2 & 1;

		delta_error2 = (l_delta2 >> 1) | (h_delta2 << 7);
		if (sign)
			delta_error2 = (0 - delta_error2);
		ideal_code1 = tps80031_ideal[index].code1 * 4;
		ideal_code2 = tps80031_ideal[index].code2 * 4;

		gain_error_1 = ((delta_error2 + scalar_delta2) -
				(delta_error1 - scalar_delta1)) *
				SCALE / (ideal_code2 - ideal_code1);
		offset_error = (delta_error1 + scalar_delta1) *
				SCALE - gain_error_1 *  ideal_code1;

		tps80031_calib_tbl[index].gain_error = gain_error_1 + SCALE;
		tps80031_calib_tbl[index].offset_error = offset_error;
	}

	the_gpadc = gpadc;
	ret = sysfs_create_group(&pdev->dev.kobj, &tps80031_gpadc_group);
	if (ret) {
		dev_err(&pdev->dev, "could not create sysfs files\n");
		goto err;
	}
	return 0;
err:
	misc_deregister(&tps80031_gpadc_device);
	return ret;
}

static int __devexit tps80031_gpadc_remove(struct platform_device *pdev)
{
	sysfs_remove_group(&pdev->dev.kobj, &tps80031_gpadc_group);
	misc_deregister(&tps80031_gpadc_device);
	return 0;
}

static struct platform_driver tps80031_gpadc_driver = {
	.probe		= tps80031_gpadc_probe,
	.remove		= __devexit_p(tps80031_gpadc_remove),
	.driver		= {
		.name	= "tps80031-gpadc",
		.owner	= THIS_MODULE,
	},
};

static int __init tps80031_gpadc_init(void)
{
	return platform_driver_register(&tps80031_gpadc_driver);
}

module_init(tps80031_gpadc_init);

static void __exit tps80031_gpadc_exit(void)
{
	platform_driver_unregister(&tps80031_gpadc_driver);
}

module_exit(tps80031_gpadc_exit);
MODULE_ALIAS("platform:tps80031-gpadc");
MODULE_DESCRIPTION("tps80031 ADC driver");