summaryrefslogtreecommitdiff
path: root/drivers/misc/mvf_adc.c
blob: 30c417c98622070a48a83d8ba1210f246e20e0f2 (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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
/* Copyright 2012 Freescale Semiconductor, Inc.
 * Copyright 2013 Toradex AG
 *
 * Freescale Faraday Quad ADC driver
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License.
*/

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/slab.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/uaccess.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>
#include <linux/mvf_adc.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/input.h>
#include <linux/delay.h>
#include <linux/gpio.h>
#include <mach/colibri-ts.h>

#define DRIVER_NAME "mvf-adc"
#define DRIVER_TS_NAME "mvf-adc-ts"
#define DRV_VERSION "1.2"

#define MVF_ADC_MAX_DEVICES 4
#define MVF_ADC_MAX ((1 << 12) - 1)

#define COLI_TOUCH_MIN_DELAY_US 1000
#define COLI_TOUCH_MAX_DELAY_US 2000

#define COL_TS_GPIO_XP 0
#define COL_TS_GPIO_XM 1
#define COL_TS_GPIO_YP 2
#define COL_TS_GPIO_YM 3
#define COL_TS_GPIO_TOUCH 4
#define COL_TS_GPIO_PULLUP 5

#define COL_TS_WIRE_XP 0
#define COL_TS_WIRE_XM 1
#define COL_TS_WIRE_YP 2
#define COL_TS_WIRE_YM 3

struct col_ts_driver {
	int enable_state;
	struct gpio *control_gpio;
};

/* GPIO */
static struct gpio col_ts_gpios[] = {
	[COL_TS_GPIO_XP] = { 13, GPIOF_IN, "Touchscreen PX" },
	[COL_TS_GPIO_XM] = { 5, GPIOF_IN, "Touchscreen MX" },
	[COL_TS_GPIO_YP] = { 12, GPIOF_IN, "Touchscreen PY" },
	[COL_TS_GPIO_YM] = { 4, GPIOF_IN, "Touchscreen MY" },
	[COL_TS_GPIO_TOUCH] = { 8, GPIOF_IN, "Touchscreen (Touch interrupt)" },
	[COL_TS_GPIO_PULLUP] = { 9, GPIOF_IN, "Touchscreen (Pull-up)" },
};

/* GPIOs and their FET configuration */
static struct col_ts_driver col_ts_drivers[] = {
	[COL_TS_WIRE_XP] = {
		.enable_state = 0,
		.control_gpio = &col_ts_gpios[COL_TS_GPIO_XP],
	},
	[COL_TS_WIRE_XM] = {
		.enable_state = 1,
		.control_gpio = &col_ts_gpios[COL_TS_GPIO_XM],
	},
	[COL_TS_WIRE_YP] = {
		.enable_state = 0,
		.control_gpio = &col_ts_gpios[COL_TS_GPIO_YP],
	},
	[COL_TS_WIRE_YM] = {
		.enable_state = 1,
		.control_gpio  = &col_ts_gpios[COL_TS_GPIO_YM],
	},
};

#define col_ts_init_wire(ts_gpio) \
	gpio_direction_output(col_ts_drivers[ts_gpio].control_gpio->gpio, \
			!col_ts_drivers[ts_gpio].enable_state)

#define col_ts_enable_wire(ts_gpio) \
	gpio_set_value(col_ts_drivers[ts_gpio].control_gpio->gpio, \
			col_ts_drivers[ts_gpio].enable_state)

#define col_ts_disable_wire(ts_gpio) \
	gpio_set_value(col_ts_drivers[ts_gpio].control_gpio->gpio, \
			!col_ts_drivers[ts_gpio].enable_state)

/*
 * wait_event_interruptible(wait_queue_head_t suspendq, int suspend_flag)
 */
static struct class *adc_class;
static int mvf_adc_major;

struct adc_client {
	struct platform_device	*pdev;
	struct list_head	 list;
	wait_queue_head_t	*wait;

	unsigned int	result;
	unsigned int	channel;
};

static LIST_HEAD(client_list);/* Initialization client list head */
static DECLARE_COMPLETION(adc_tsi);

struct adc_device {
	struct platform_device	*pdev;
	struct platform_device	*owner;
	struct clk		*clk;
	struct adc_client	*cur;
	void __iomem		*regs;
	spinlock_t		 lock;

	struct cdev		 cdev;

	int			 irq;
};

static struct adc_device *adc_devices[MVF_ADC_MAX_DEVICES];

struct adc_touch_device {
	struct platform_device	*pdev;

	bool stop_touchscreen;

	int pen_irq;
	struct input_dev	*ts_input;
	struct workqueue_struct *ts_workqueue;
	struct work_struct	ts_work;
};

struct data {
	unsigned int res_value;
	bool flag;
};

struct adc_touch_device *touch;

struct data data_array[7];

#define adc_dbg(_adc, msg...) dev_dbg(&(_adc)->pdev->dev, msg)

static int res_proc(struct adc_device *adc);
struct adc_client *adc_register(struct platform_device *pdev,
		unsigned char channel);

/* select channel and enable conversion  */
static inline void adc_convert(struct adc_device *adc,
		struct adc_client *client)
{

	unsigned con = readl(adc->regs + ADC_HC0);
	con  = ADCHC0(client->channel);
	con |= IRQ_EN;
	writel(con, adc->regs + ADC_HC0);
}

/* find next conversion client */
static void adc_try(struct adc_device *adc)
{
	struct adc_client *next = adc->cur;
	if (!list_empty(&client_list)) {
		next = list_entry(client_list.next,
				struct adc_client, list);
		list_del(client_list.next);

		adc->cur = next;
		adc_convert(adc, adc->cur);
	}
}

int adc_initiate(struct adc_device *adc_dev)
{
	unsigned long reg, tmp, pin;
	struct adc_device *adc = adc_dev;


	/* PCTL: pin control 5 for Sliding rheostat */
	pin = readl(adc->regs+ADC_PCTL);
	pin &= ~CLEPCTL23;
	pin &= SETPCTL5;
	writel(pin, adc->regs+ADC_PCTL);

	/* CFG: Feature set */
	reg = readl(adc->regs+ADC_CFG);
	reg &= ~CLECFG16;
	reg |= SETCFG;
	writel(reg, adc->regs+ADC_CFG);

	/* GC: software trigger */
	tmp = readl(adc->regs+ADC_GC);
	tmp = 0;
	writel(tmp, adc->regs+ADC_GC);

	return 0;
}

static int adc_set(struct adc_device *adc_dev, struct adc_feature *adc_fea)
{
	struct adc_device *adc = adc_dev;
	int con, res;
	con = readl(adc->regs+ADC_CFG);
	res = readl(adc->regs+ADC_GC);

	/* clock select and clock devide */
	switch (adc_fea->clk_sel) {
	case ADCIOC_BUSCLK_SET:
			/* clear 1-0,6-5 */
		con &= ~CLEAR_CLK_BIT;
		switch (adc_fea->clk_div_num) {
		case 1:
			break;
		case 2:
			con |= CLK_DIV2;
			break;
		case 4:
			con |= CLK_DIV4;
			break;
		case 8:
			con |= CLK_DIV8;
			break;
		case 16:
			con |= BUSCLK2_SEL | CLK_DIV8;
			break;
		default:
			return -EINVAL;
	}
	writel(con, adc->regs+ADC_CFG);

	break;

	case ADCIOC_ALTCLK_SET:
	/* clear 1-0,6-5 */
		con &= ~CLEAR_CLK_BIT;

		con |= ALTCLK_SEL;
	switch (adc_fea->clk_div_num) {
	case 1:
		break;
	case 2:
		con |= CLK_DIV2;
		break;
	case 4:
		con |= CLK_DIV4;
		break;
	case 8:
		con |= CLK_DIV8;
		break;
	default:
		return -EINVAL;
	}
	writel(con, adc->regs+ADC_CFG);

	break;

	case ADCIOC_ADACK_SET:
		/* clear 1-0,6-5 */
		con &= ~CLEAR_CLK_BIT;

		con |= ADACK_SEL;
		switch (adc_fea->clk_div_num) {
		case 1:
			break;
		case 2:
			con |= CLK_DIV2;
			break;
		case 4:
			con |= CLK_DIV4;
			break;
		case 8:
			con |= CLK_DIV8;
			break;
		default:
			return -EINVAL;
		}
		writel(con, adc->regs+ADC_CFG);
	break;

	default:
		pr_debug("adc_ioctl: unsupported ioctl command 0x%x\n",
				adc_fea->clk_sel);
		return -EINVAL;
	}

	/* resolution mode,clear 3-2 */
	con &= ~CLEAR_MODE_BIT;
	switch (adc_fea->res_mode) {
	case 8:
		break;
	case 10:
		con |= BIT10;
		break;
	case 12:
		con |= BIT12;
		break;
	default:
		pr_debug("adc_ioctl: unsupported ioctl resolution mode 0x%x\n",
				adc_fea->res_mode);
		return -EINVAL;
	}
	writel(con, adc->regs+ADC_CFG);

	/* Defines the sample time duration */
	/* clear 4, 9-8 */
	con &= ~CLEAR_LSTC_BIT;
	switch (adc_fea->sam_time) {
	case 2:
		break;
	case 4:
		con |= ADSTS_SHORT;
		break;
	case 6:
		con |= ADSTS_NORMAL;
		break;
	case 8:
		con |= ADSTS_LONG;
		break;
	case 12:
		con |= ADLSMP_LONG;
		break;
	case 16:
		con |= ADLSMP_LONG | ADSTS_SHORT;
		break;
	case 20:
		con |= ADLSMP_LONG | ADSTS_NORMAL;
		break;
	case 24:
		con |= ADLSMP_LONG | ADSTS_LONG;
		break;
	default:
		pr_debug("adc_ioctl: unsupported ioctl sample"
				"time duration 0x%x\n",
				adc_fea->sam_time);
		return -EINVAL;
	}
	writel(con, adc->regs+ADC_CFG);

	/* low power configuration */
	/* */
	switch (adc_fea->lp_con) {
	case ADCIOC_LPOFF_SET:
		con &= ~CLEAR_ADLPC_BIT;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_LPON_SET:
		con &= ~CLEAR_ADLPC_BIT;
		con |= ADLPC_EN;
		writel(con, adc->regs+ADC_CFG);
		break;
	default:
		return -EINVAL;

	}
	/* high speed operation */
	switch (adc_fea->hs_oper) {
	case ADCIOC_HSON_SET:
		con &= ~CLEAR_ADHSC_BIT;
		con |= ADHSC_EN;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_HSOFF_SET:
		con &= ~CLEAR_ADHSC_BIT;
		writel(con, adc->regs+ADC_CFG);
		break;
	default:
		return -EINVAL;
	}

	/* voltage reference*/
	switch (adc_fea->vol_ref) {
	case ADCIOC_VR_VREF_SET:
		con &= ~CLEAR_REFSEL_BIT;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_VR_VALT_SET:
		con &= ~CLEAR_REFSEL_BIT;
		con |= REFSEL_VALT;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_VR_VBG_SET:
		con &= ~CLEAR_REFSEL_BIT;
		con |= REFSEL_VBG;
		writel(con, adc->regs+ADC_CFG);
		break;
	default:
		return -EINVAL;
	}

	/* trigger select */
	switch (adc_fea->tri_sel) {
	case ADCIOC_SOFTTS_SET:
		con &= ~CLEAR_ADTRG_BIT;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_HARDTS_SET:
		con &= ~CLEAR_ADTRG_BIT;
		con |= ADTRG_HARD;
		writel(con, adc->regs+ADC_CFG);
		break;
	default:
		return -EINVAL;
	}

	/* hardware average select */
	switch (adc_fea->ha_sel) {
	case ADCIOC_HA_DIS:
		res &= ~CLEAR_AVGE_BIT;
		writel(con, adc->regs+ADC_GC);
		break;

	case ADCIOC_HA_SET:
		con &= ~CLEAR_AVGS_BIT;
		switch (adc_fea->ha_sam) {
		case 4:
			break;
		case 8:
			con |= AVGS_8;
			break;
		case 16:
			con |= AVGS_16;
			break;
		case 32:
			con |= AVGS_32;
			break;
		default:
			return -EINVAL;
		}
		res &= ~CLEAR_AVGE_BIT;
		res |= AVGEN;
		writel(con, adc->regs+ADC_CFG);
		writel(res, adc->regs+ADC_GC);

		break;
	default:
		return -EINVAL;
	}

	/* data overwrite enable */
	switch (adc_fea->do_ena) {
	case ADCIOC_DOEON_SET:
		con &= ~CLEAR_OVWREN_BIT;
		writel(con, adc->regs+ADC_CFG);
		break;

	case ADCIOC_DOEOFF_SET:
		con |= OVWREN;
		writel(con, adc->regs+ADC_CFG);
		break;
	default:
		return -EINVAL;
	}

	/* Asynchronous clock output enable */
	switch (adc_fea->ac_ena) {
	case ADCIOC_ADACKENON_SET:
		res &= ~CLEAR_ADACKEN_BIT;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIOC_ADACKENOFF_SET:
		res &= ~CLEAR_ADACKEN_BIT;
		res |= ADACKEN;
		writel(res, adc->regs+ADC_GC);
		break;
	default:
		return -EINVAL;
	}

	/* dma enable */
	switch (adc_fea->dma_ena) {
	case ADCIDC_DMAON_SET:
		res &= ~CLEAR_DMAEN_BIT;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIDC_DMAOFF_SET:
		res &= ~CLEAR_DMAEN_BIT;
		res |= DMAEN;
		writel(res, adc->regs+ADC_GC);
		break;
	default:
		return -EINVAL;
	}

	/* continue function enable */
	switch (adc_fea->cc_ena) {
	case ADCIOC_CCEOFF_SET:
		res &= ~CLEAR_ADCO_BIT;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIOC_CCEON_SET:
		res &= ~CLEAR_ADCO_BIT;
		res |= ADCON;
		writel(res, adc->regs+ADC_GC);
		break;
	default:
		return -EINVAL;
	}

	/* compare function enable */
	switch (adc_fea->compare_func_ena) {
	case ADCIOC_ACFEON_SET:
		res &= ~CLEAR_ACFE_BIT;
		res |= ACFE;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIOC_ACFEOFF_SET:
		res &= ~CLEAR_ACFE_BIT;
		writel(res, adc->regs+ADC_GC);
		break;
	default:
		return -EINVAL;
	}

	/* greater than enable */
	switch (adc_fea->greater_ena) {
	case ADCIOC_ACFGTON_SET:
		res &= ~CLEAR_ACFGT_BIT;
		res |= ACFGT;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIOC_ACFGTOFF_SET:
		res &= ~CLEAR_ACFGT_BIT;
		writel(res, adc->regs+ADC_GC);
		break;
	default:
		return -EINVAL;
	}

	/* range enable */
	switch (adc_fea->range_ena) {
	case ADCIOC_ACRENON_SET:
		res &= ~CLEAR_ACREN_BIT;
		res |= ACREN;
		writel(res, adc->regs+ADC_GC);
		break;

	case ADCIOC_ACRENOFF_SET:
		res &= ~CLEAR_ACREN_BIT;
		writel(res, adc->regs+ADC_GC);
		break;

	default: return -ENOTTY;
	}
	return 0;
}

static int adc_open(struct inode *inode, struct file *file)
{
	struct adc_device *dev = container_of(inode->i_cdev,
			struct adc_device, cdev);

	file->private_data = dev;

	return 0;
}

static long adc_ioctl(struct file *file, unsigned int cmd,
		unsigned long arg)
{
	struct adc_device *adc_dev = file->private_data;
	void __user *argp = (void __user *)arg;
	struct adc_feature feature;
	int channel;

	if (_IOC_TYPE(cmd) != 'p')
		return -ENOTTY;

	if (copy_from_user(&feature, (struct adc_feature *)argp,
				sizeof(feature))) {
		return -EFAULT;
	}

	switch (cmd) {
	case ADC_INIT:
		adc_initiate(adc_dev);
		break;

	case ADC_CONFIGURATION:

		adc_set(adc_dev, &feature);
		break;

	case ADC_REG_CLIENT:
		channel = feature.channel;
		adc_register(adc_dev->pdev, channel);
		break;

	case ADC_CONVERT:
		INIT_COMPLETION(adc_tsi);
		adc_try(adc_dev);
		wait_for_completion_interruptible(&adc_tsi);
		if (data_array[feature.channel].flag) {
			feature.result0 = data_array[feature.channel].res_value;
			data_array[feature.channel].flag = 0;
		}
		if (copy_to_user((struct adc_feature *)argp, &feature,
				sizeof(feature)))
			return -EFAULT;

		kfree(adc_dev->cur);
		break;

	default:
		pr_debug("adc_ioctl: unsupported ioctl command 0x%x\n", cmd);
		return -EINVAL;
	}
	return 0;
}

/* client register */
struct adc_client *adc_register(struct platform_device *pdev,
		unsigned char channel)
{
	struct adc_client *client = kzalloc(sizeof(struct adc_client),
			GFP_KERNEL);

	WARN_ON(!pdev);

	if (!pdev)
		return ERR_PTR(-EINVAL);


	if (!client) {
		dev_err(&pdev->dev, "no memory for adc client\n");
		return ERR_PTR(-ENOMEM);
	}

	client->pdev = pdev;
	client->channel = channel;
	list_add(&client->list, &client_list);

	return client;
}


/*result process */
static int res_proc(struct adc_device *adc)
{
	int con, res;
	con = readl(adc->regs + ADC_CFG);

	if ((con & (1 << 2)) == 0) {
		if ((con & (1 << 3)) == 1)
			res = (0xFFF & readl(adc->regs + ADC_R0));
		else
			res = (0xFF & readl(adc->regs + ADC_R0));
	} else
		res = (0x3FF & readl(adc->regs + ADC_R0));

	return readl(adc->regs + ADC_R0);
	return res;
}

static irqreturn_t adc_irq(int irq, void *pw)
{
	int coco;
	struct adc_device *adc = pw;
	struct adc_client *client = adc->cur;

	if (!client) {
		dev_warn(&adc->pdev->dev, "%s: no adc pending\n", __func__);
		goto exit;
	}

	coco = readl(adc->regs + ADC_HS);
	if (coco & 1) {
		data_array[client->channel].res_value = res_proc(adc);
		data_array[client->channel].flag = 1;
		complete(&adc_tsi);
	}

exit:
	return IRQ_NONE;
}

static const struct file_operations adc_fops = {
	.owner			= THIS_MODULE,
	.open			= adc_open,
	.unlocked_ioctl		= adc_ioctl,
	.read			= NULL,
};

/*
 * Enables given plates and measures touch parameters using ADC
 */
static int adc_ts_measure(int plate1, int plate2, int adc, int adc_channel)
{
	int i, value = 0;
	col_ts_enable_wire(plate1);
	col_ts_enable_wire(plate2);

	/* Use hrtimer sleep since msleep sleeps 10ms+ */
	usleep_range(COLI_TOUCH_MIN_DELAY_US, COLI_TOUCH_MAX_DELAY_US);

	for (i = 0; i < 5; i++) {
		adc_register(adc_devices[adc]->pdev, adc_channel);

		INIT_COMPLETION(adc_tsi);
		adc_try(adc_devices[adc]);
		wait_for_completion(&adc_tsi);

		if (!data_array[adc_channel].flag)
			return -EINVAL;

		value += data_array[adc_channel].res_value;
		data_array[adc_channel].flag = 0;
	}

	value /= 5;

	col_ts_disable_wire(plate1);
	col_ts_disable_wire(plate2);

	return value;
}

static void adc_ts_enable_touch_detection(struct adc_touch_device *adc_ts)
{
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	/* Enable plate YM (needs to be strong 0) */
	col_ts_enable_wire(COL_TS_WIRE_YM);

	/* Let the platform mux to GPIO in order to enable Pull-Up on GPIO */
	if (pdata->mux_pen_interrupt)
		pdata->mux_pen_interrupt(adc_ts->pdev);
}

static void adc_ts_work(struct work_struct *ts_work)
{
	struct adc_touch_device *adc_ts = container_of(ts_work,
				struct adc_touch_device, ts_work);
	struct device *dev = &adc_ts->pdev->dev;
	int val_x, val_y, val_z1, val_z2, val_p = 0;

	struct adc_feature feature = {
		.channel = 0,
		.clk_sel = ADCIOC_BUSCLK_SET,
		.clk_div_num = 1,
//		.res_mode = 12,
		.ha_sam = 32,
		.sam_time = 24,
		.hs_oper = ADCIOC_HSOFF_SET
	};

	adc_initiate(adc_devices[0]);
	adc_set(adc_devices[0], &feature);

	adc_initiate(adc_devices[1]);
	adc_set(adc_devices[1], &feature);


	while (!adc_ts->stop_touchscreen)
	{
		/* X-Direction */
		val_x = adc_ts_measure(COL_TS_WIRE_XP, COL_TS_WIRE_XM, 1, 0);
		if (val_x < 0)
			continue;

		/* Y-Direction */
		val_y = adc_ts_measure(COL_TS_WIRE_YP, COL_TS_WIRE_YM, 0, 0);
		if (val_y < 0)
			continue;

		/* Touch pressure
		 * Measure on XP/YM
		 */
		val_z1 = adc_ts_measure(COL_TS_WIRE_YP, COL_TS_WIRE_XM, 0, 1);
		if (val_z1 < 0)
			continue;
		val_z2 = adc_ts_measure(COL_TS_WIRE_YP, COL_TS_WIRE_XM, 1, 2);
		if (val_z2 < 0)
			continue;

		/* According to datasheet of our touchscreen, 
		 * resistance on X axis is 400~1200.. 
		 */
	//	if ((val_z2 - val_z1) < (MVF_ADC_MAX - (1<<9)) {
		/* Validate signal (avoid calculation using noise) */
		if (val_z1 > 64 && val_x > 64) {
			/* Calculate resistance between the plates
			 * lower resistance means higher pressure */
			int r_x = (1000 * val_x) / MVF_ADC_MAX;
			val_p = (r_x * val_z2) / val_z1 - r_x;
		} else {
			val_p = 2000;
		}
		/*
		dev_dbg(dev, "Measured values: x: %d, y: %d, z1: %d, z2: %d, "
			"p: %d\n", val_x, val_y, val_z1, val_z2, val_p);
*/
		/* If difference of the AD levels of the two plates is near
		 * the maximum, there is no touch..
		 */
		/*
		 * If touch pressure is too low, stop measuring and reenable
		 * touch detection
		 */
		if (val_p > 1050)
			break;

		/* Report touch position and sleep for next measurement */
		input_report_abs(adc_ts->ts_input, ABS_X, MVF_ADC_MAX - val_x);
		input_report_abs(adc_ts->ts_input, ABS_Y, MVF_ADC_MAX - val_y);
		input_report_abs(adc_ts->ts_input, ABS_PRESSURE, 2000 - val_p);
		input_report_key(adc_ts->ts_input, BTN_TOUCH, 1);
		input_sync(adc_ts->ts_input);

		msleep(10);
	}

	/* Report no more touch, reenable touch detection */
	input_report_abs(adc_ts->ts_input, ABS_PRESSURE, 0);
	input_report_key(adc_ts->ts_input, BTN_TOUCH, 0);
	input_sync(adc_ts->ts_input);

	/* Wait the pull-up to be stable on high */
	adc_ts_enable_touch_detection(adc_ts);
	msleep(10);

	/* Reenable IRQ to detect touch */
	enable_irq(adc_ts->pen_irq);

	dev_dbg(dev, "Reenabled touch detection interrupt\n");
}

static irqreturn_t adc_tc_touched(int irq, void *dev_id)
{
	struct adc_touch_device *adc_ts = (struct adc_touch_device *)dev_id;
	struct device *dev = &adc_ts->pdev->dev;
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	dev_dbg(dev, "Touch detected, start worker thread\n");

	/* Stop IRQ */
	disable_irq_nosync(irq);

	/* Disable the touch detection plates */
	col_ts_disable_wire(COL_TS_WIRE_YM);

	/* Let the platform mux to GPIO in order to enable Pull-Up on GPIO */
	if (pdata->mux_adc)
		pdata->mux_adc(adc_ts->pdev);

	/* Start worker thread */
	queue_work(adc_ts->ts_workqueue, &adc_ts->ts_work);

	return IRQ_HANDLED;
}

static int adc_ts_open(struct input_dev *dev_input)
{
	int ret;
	struct adc_touch_device *adc_ts = input_get_drvdata(dev_input);
	struct device *dev = &adc_ts->pdev->dev;
	struct colibri_ts_platform_data *pdata = adc_ts->pdev->dev.platform_data;

	dev_dbg(dev, "Input device %s opened, starting touch detection\n", 
			dev_input->name);

	adc_ts->stop_touchscreen = false;

	/* Initialize GPIOs, leave FETs closed by default */
	col_ts_init_wire(COL_TS_WIRE_XP);
	col_ts_init_wire(COL_TS_WIRE_XM);
	col_ts_init_wire(COL_TS_WIRE_YP);
	col_ts_init_wire(COL_TS_WIRE_YM);

	/* Mux detection before request IRQ */
	adc_ts_enable_touch_detection(adc_ts);

	adc_ts->pen_irq = gpio_to_irq(pdata->gpio_pen);
	if (adc_ts->pen_irq < 0) {
		dev_err(dev, "Unable to get IRQ for GPIO %d\n", pdata->gpio_pen);
		return adc_ts->pen_irq;
	}

	ret = request_irq(adc_ts->pen_irq, adc_tc_touched, IRQF_TRIGGER_FALLING,
			"touch detected", adc_ts);
	if (ret < 0) {
		dev_err(dev, "Unable to request IRQ %d\n", adc_ts->pen_irq);
		return ret;
	}

	return 0;
}

static void adc_ts_close(struct input_dev *dev_input)
{
	struct adc_touch_device *adc_ts = input_get_drvdata(dev_input);
	struct device *dev = &adc_ts->pdev->dev;

	free_irq(gpio_to_irq(col_ts_gpios[COL_TS_GPIO_TOUCH].gpio), adc_ts);

	adc_ts->stop_touchscreen = true;

	/* Wait until touchscreen thread finishes any possible remnants. */
	cancel_work_sync(&adc_ts->ts_work);

	dev_dbg(dev, "Input device %s closed, disable touch detection\n", 
			dev_input->name);
}


static int __devinit adc_ts_probe(struct platform_device *pdev)
{
	int ret = 0;
	struct device *dev = &pdev->dev;
	struct input_dev *input;
	struct adc_touch_device *adc_ts;

	adc_ts = kzalloc(sizeof(struct adc_touch_device), GFP_KERNEL);
	if (!adc_ts) {
		dev_err(dev, "Failed to allocate TS device!\n");
		return -ENOMEM;
	}

	adc_ts->pdev = pdev;

	input = input_allocate_device();
	if (!input) {
		dev_err(dev, "Failed to allocate TS input device!\n");
		ret = -ENOMEM;
		goto err_input_allocate;
	}

	input->name = DRIVER_NAME;
	input->id.bustype = BUS_HOST;
	input->dev.parent = dev;
	input->open = adc_ts_open;
	input->close = adc_ts_close;

	__set_bit(EV_ABS, input->evbit);
	__set_bit(EV_KEY, input->evbit);
	__set_bit(BTN_TOUCH, input->keybit);
	input_set_abs_params(input, ABS_X, 0, MVF_ADC_MAX, 0, 0);
	input_set_abs_params(input, ABS_Y, 0, MVF_ADC_MAX, 0, 0);
	input_set_abs_params(input, ABS_PRESSURE, 0, MVF_ADC_MAX, 0, 0);

	adc_ts->ts_input = input;
	input_set_drvdata(input, adc_ts);
	ret = input_register_device(input);
	if (ret) {
		dev_err(dev, "failed to register input device\n");
		goto err;
	}

	/* Create workqueue for ADC sampling and calculation */
	INIT_WORK(&adc_ts->ts_work, adc_ts_work);
	adc_ts->ts_workqueue = create_singlethread_workqueue("mvf-adc-touch");

	if (!adc_ts->ts_workqueue) {
		dev_err(dev, "failed to create workqueue");
		goto err;
	}

	/* Request GPIOs for FETs and touch detection */
	ret = gpio_request_array(col_ts_gpios, COL_TS_GPIO_PULLUP + 1);
	if (ret) {
		dev_err(dev, "failed to request GPIOs\n");
		goto err;
	}

	return 0;
err:
	input_free_device(touch->ts_input);

err_input_allocate:
	kfree(adc_ts);

	return ret;
}

static int __devexit adc_ts_remove(struct platform_device *pdev)
{
	struct adc_touch_device *adc_ts = platform_get_drvdata(pdev);

	input_unregister_device(adc_ts->ts_input);

	destroy_workqueue(adc_ts->ts_workqueue);
	kfree(adc_ts->ts_input);
	kfree(adc_ts);

	return 0;
}


/* probe */
static int __devinit adc_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct adc_device *adc;
	struct resource *regs;
	int ret;


	adc = kzalloc(sizeof(struct adc_device), GFP_KERNEL);
	if (adc == NULL) {
		dev_err(dev, "failed to allocate adc_device\n");
		return -ENOMEM;
	}
	adc->pdev = pdev;

	/* Initialize spin_lock */
	spin_lock_init(&adc->lock);

	adc->irq = platform_get_irq(pdev, 0);
	if (adc->irq <= 0) {
		dev_err(dev, "failed to get adc irq\n");
		ret = -EINVAL;
		goto err_alloc;
	}

	ret = request_irq(adc->irq, adc_irq, 0, dev_name(dev), adc);
	if (ret < 0) {
		dev_err(dev, "failed to attach adc irq\n");
		goto err_alloc;
	}

	adc->clk = clk_get(dev, NULL);
	if (IS_ERR(adc->clk)) {
		dev_err(dev, "failed to get adc clock\n");
		ret = PTR_ERR(adc->clk);
		goto err_irq;
	}

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!regs) {
		dev_err(dev, "failed to find registers\n");
		ret = -ENXIO;
		goto err_clk;
	}

	adc->regs = ioremap(regs->start, resource_size(regs));
	if (!adc->regs) {
		dev_err(dev, "failed to map registers\n");
		ret = -ENXIO;
		goto err_clk;
	}


	cdev_init(&adc->cdev, &adc_fops);
	adc->cdev.owner = THIS_MODULE;
	ret = cdev_add(&adc->cdev, MKDEV(mvf_adc_major, pdev->id), 1);
	if (ret < 0)
		return ret;

	if (IS_ERR(device_create(adc_class, &pdev->dev,
				 MKDEV(mvf_adc_major, pdev->id),
				 NULL, "mvf-adc.%d", pdev->id)))
		dev_err(dev, "failed to create device\n");

	/* clk enable */
	clk_enable(adc->clk);
	/* Associated structures */
	platform_set_drvdata(pdev, adc);

	/* Save device structure by Platform device ID for touch */
	adc_devices[pdev->id] = adc;

	dev_info(dev, "attached adc driver\n");

	return 0;

err_clk:
	clk_put(adc->clk);

err_irq:
	free_irq(adc->irq, adc);

err_alloc:
	kfree(adc);

	return ret;
}

static int __devexit adc_remove(struct platform_device *pdev)
{
	struct adc_device *adc = platform_get_drvdata(pdev);

	iounmap(adc->regs);
	free_irq(adc->irq, adc);
	clk_disable(adc->clk);
	clk_put(adc->clk);
	kfree(adc);

	return 0;
}

static struct platform_driver adc_driver = {
	.driver		= {
		.name	= DRIVER_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= adc_probe,
	.remove		= __devexit_p(adc_remove),
};

static struct platform_driver adc_ts_driver = {
	.driver		= {
		.name	= DRIVER_TS_NAME,
		.owner	= THIS_MODULE,
	},
	.probe		= adc_ts_probe,
	.remove		= __devexit_p(adc_ts_remove),
};

static int __init adc_init(void)
{
	int ret;
	dev_t dev;

	adc_class = class_create(THIS_MODULE, "mvf-adc");
	if (IS_ERR(adc_class)) {
		ret = PTR_ERR(adc_class);
		printk(KERN_ERR "%s: can't register mvf-adc class\n",__func__);
		goto err;
	}

	/* Obtain device numbers and register char device */
	ret = alloc_chrdev_region(&dev, 0, MVF_ADC_MAX_DEVICES, "mvf-adc");
	if (ret)
	{
		printk(KERN_ERR "%s: can't register character device\n", 
				__func__);
		goto err_class;
	}
	mvf_adc_major = MAJOR(dev);

	ret = platform_driver_register(&adc_driver);
	if (ret)
		printk(KERN_ERR "%s: failed to add adc driver\n", __func__);

	ret = platform_driver_register(&adc_ts_driver);
	if (ret)
		printk(KERN_ERR "%s: failed to add adc touchscreen driver\n", 
				__func__);

err_class:
	class_destroy(adc_class);
err:
	return ret;
}

static void __exit adc_exit(void)
{
	platform_driver_unregister(&adc_ts_driver);
	platform_driver_unregister(&adc_driver);
	class_destroy(adc_class);
}
module_init(adc_init);
module_exit(adc_exit);

MODULE_AUTHOR("Xiaojun Wang, Stefan Agner");
MODULE_DESCRIPTION("Vybrid ADC driver");
MODULE_LICENSE("GPL v2");
MODULE_VERSION(DRV_VERSION);