summaryrefslogtreecommitdiff
path: root/drivers/misc/pn547.c
blob: ff819341a46d1a5bfc1027c0dce14f2b5d567084 (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 (C) 2010 Trusted Logic S.A.
 *
 * 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, or
 * (at your option) any later version.
 *
 * 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/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/list.h>
#include <linux/i2c.h>
#include <linux/irq.h>
#include <linux/jiffies.h>
#include <linux/uaccess.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/platform_device.h>
#include <linux/gpio.h>
#include <linux/miscdevice.h>
#include <linux/spinlock.h>
#include <linux/nfc/pn544.h>

#include <mach/wmt_iomux.h>

#include <mach/hardware.h>
//#include <plat/gpio-core.h>
//#include <plat/gpio-cfg.h>
//#include <plat/gpio-cfg-helpers.h>


#undef pr_err
#define pr_err printk
//#define pr_debug printk
//#define pr_warning printk

#define DRIVER_DESC	"NFC driver for PN544"

#define CLIENT_ADDR 0x28 //0x2b mod 2014-7-10
#define WMT_PN544_I2C_CHANNEL 0

struct i2c_client *pn544_client;
static int g_chip_nr = 7;

#define MAX_BUFFER_SIZE	512

extern  int wmt_getsyspara(char *varname, unsigned char *varval, int *varlen);

struct pn544_dev	{
	wait_queue_head_t	read_wq;
	struct mutex		read_mutex;
	struct i2c_client	*client;
	struct miscdevice	pn544_device;
	
	
	int 		ven_gpio;
	int 		ven_active;
	int 		ven_on_off;
	
	int 		firm_gpio;
	int 		firm_active;
	
	int		irq_gpio;
	int 		irq_active;
	
	
	bool			irq_enabled;
	spinlock_t		irq_enabled_lock;
};

//**********************add 2014-7-16
static int irq_gpio = 0;

void wmt_clr_int(void)
{
	int num = irq_gpio; 
	
	int reg_shift = 20;
	if (num > /*11*/42)
	{
		return;
	}
	
	if (num < 20)
	{
		REG32_VAL(__GPIO_BASE+0x0360) = 1<<num;
	}
	else
	{
		switch (num)
		{
			case WMT_PIN_GP5_VDOUT11:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<reg_shift;
				break;
			case WMT_PIN_GP5_VDOUT12:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<(reg_shift+1);
				break;
			case WMT_PIN_GP6_VDOUT18:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<(reg_shift+2);
				break;
			case WMT_PIN_GP6_VDOUT19:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<(reg_shift+3);
				break;
			case WMT_PIN_GP6_VDOUT20:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<(reg_shift+4);
				break;
			case WMT_PIN_GP6_VDOUT21:
				REG32_VAL(__GPIO_BASE+0x0360) = 1<<(reg_shift+5);
				break;
			
		}
	}	
	//gpio 0-19 vdout11 12 18:21
	//REG32_VAL(__GPIO_BASE+0x0360) = 1<<num; //interrupt status register ,1:active 0:inactive
					// write 1:to clear 
}

void wmt_set_irqinput(void)
{
	int num = irq_gpio;
	
	if (num > /*19*/42)
		return;
	if (num < 20)
	{	
		REG32_VAL(__GPIO_BASE+0x0040) |= (1<<num); //enable gpio
		REG32_VAL(__GPIO_BASE+0x0080) &= ~(1<<num); //set input
	}
	
	//GPIO GP5 Enable Register for VDOUT[15:8] 
	//GPIO GP6 Enable Register for VDOUT[23:16] 

	switch (num) //gpio 0-19 vdout11 12 18:21 -->gpio 31 32  38:41
	{
		case WMT_PIN_GP5_VDOUT11:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input	
			break;
		case WMT_PIN_GP5_VDOUT12:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT18:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT19:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT20:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT21:
			REG32_VAL(__GPIO_BASE+0x0044) |= (1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		
	}	
}

static void wmt_disable_irqinput(void)
{
	int num = irq_gpio;
	
	if (num > /*19*/42)
		return;
	if (num < 20)
	{	
		REG32_VAL(__GPIO_BASE+0x0040) &= ~(1<<num); //enable gpio
		REG32_VAL(__GPIO_BASE+0x0080) &= ~(1<<num); //set input
	}
	
	//GPIO GP5 Enable Register for VDOUT[15:8] 
	//GPIO GP6 Enable Register for VDOUT[23:16] 

	switch (num) //gpio 0-19 vdout11 12 18:21 -->gpio 31 32  38:41
	{
		case WMT_PIN_GP5_VDOUT11:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //disable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input	
			break;
		case WMT_PIN_GP5_VDOUT12:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT18:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT19:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT20:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		case WMT_PIN_GP6_VDOUT21:
			REG32_VAL(__GPIO_BASE+0x0044) &= ~(1<<(num-20)); //enable gpio
			REG32_VAL(__GPIO_BASE+0x0084) &= ~(1<<(num-20)); //set input		
			break;
		
	}		
}

static void wmt_pullup(void)
{
	int num = irq_gpio;
	
	if(num >/*11*//*19*/42)
		return;
	if (num < 20)
	{
		REG32_VAL(__GPIO_BASE+0x04c0) |= (1<<num); //pull  up!
		REG32_VAL(__GPIO_BASE+0x0480) |= (1<<num); //enable pull up/down
	}
	switch (num)
	{	//vdout11 12 18:21-->gpio 31 32  38:41
		case WMT_PIN_GP5_VDOUT11:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down
			break;
		case WMT_PIN_GP5_VDOUT12:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down
			break;
		case WMT_PIN_GP6_VDOUT18:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down	
			break;
		case WMT_PIN_GP6_VDOUT19:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down	
			break;
		case WMT_PIN_GP6_VDOUT20:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down	
			break;
		case WMT_PIN_GP6_VDOUT21:
			REG32_VAL(__GPIO_BASE+0x04c4) |= (1<<(num-20)); //pull  up!
			REG32_VAL(__GPIO_BASE+0x0484) |= (1<<(num-20)); //enable pull up/down
			break;
		
	}	
}

int wmt_set_gpirq(int type) 
{
	int shift;
	int offset;
	unsigned long reg;
	int num = irq_gpio;
	
	if(num >/*11*//*19*/42)
		return -1;
	//if (num > 9)
		//GPIO_PIN_SHARING_SEL_4BYTE_VAL &= ~BIT4; // gpio10,11 as gpio
#if 0		
	REG32_VAL(__GPIO_BASE+0x0040) &= ~(1<<num); // gpio disable
	REG32_VAL(__GPIO_BASE+0x0080) &= ~(1<<num); //set input
#endif	
	wmt_disable_irqinput();//replace 2014-8-22
#if 0	
	REG32_VAL(__GPIO_BASE+0x04c0) |= (1<<num); //pull  up!
	REG32_VAL(__GPIO_BASE+0x0480) |= (1<<num); //enable pull up/down
#endif	
	wmt_pullup();//replace 2014-8-22
	
	//set gpio irq triger type
	if(num < 4){//[0,3]
		shift = num;
		offset = 0x0300;
	}else if(num >= 4 && num < 8){//[4,7]
		shift = num-4;
		offset = 0x0304;
	}else if (num >= 8 && num <12){// [8,11]
		shift = num-8;
		offset = 0x0308;
	}
	else if (num>=12 && num < 16)
	{
		shift = num -12;
		offset = 0x030c;
	}
	else if (num>=16 && num <20)
	{
		shift = num -16;
		offset = 0x0310;
	}
	else ////vdout11 12 18:21-->gpio 31 32  38:41
	{	// 0x0314----0x0319
		switch (num)
		{
			case WMT_PIN_GP5_VDOUT11:
				shift = 0;
				offset = 0x0314;
				break;
			case WMT_PIN_GP5_VDOUT12:
				shift = 1;
				offset = 0x0314;
				break;
			
			case WMT_PIN_GP6_VDOUT18:
				shift = 2;
				offset = 0x0314;
				break;
				
			case WMT_PIN_GP6_VDOUT19:
				shift = 3;
				offset = 0x0314;
				break;
			case WMT_PIN_GP6_VDOUT20:
				shift = 0;
				offset = 0x0318;
				break;
			
			case WMT_PIN_GP6_VDOUT21:
			
				shift = 1;
				offset = 0x0318;
				break;
		}
	#if 0
		else if (num>=20 && num<24) //videoOut11 12  18 19
		{
		shift = num -20;
		offset = 0x0314;
		}
		else if (num>=24 && num<28) //videoOut20 21
		{
		shift = num -24;
		offset = 0x0318;
		}
	#endif	
	}
	
	reg = REG32_VAL(__GPIO_BASE + offset);

	switch(type){
		case IRQ_TYPE_LEVEL_LOW:
			reg &= ~(1<<(shift*8+2)); 
			reg &= ~(1<<(shift*8+1));
			reg &= ~(1<<(shift*8));
			break;
		case IRQ_TYPE_LEVEL_HIGH:
			reg &= ~(1<<(shift*8+2)); 
			reg &= ~(1<<(shift*8+1));
			reg |= (1<<(shift*8));
			break;
		case IRQ_TYPE_EDGE_FALLING:
			reg &= ~(1<<(shift*8+2)); 
			reg |= (1<<(shift*8+1));
			reg &= ~(1<<(shift*8));
			break;
		case IRQ_TYPE_EDGE_RISING:
			reg &= ~(1<<(shift*8+2)); 
			reg |= (1<<(shift*8+1));
			reg |= (1<<(shift*8));
			break;
		default://both edge
			reg |= (1<<(shift*8+2)); 
			reg &= ~(1<<(shift*8+1));
			reg &= ~(1<<(shift*8));
			break;
			
	}
	//reg |= 1<<(shift*8+7);//enable interrupt
	reg &= ~(1<<(shift*8+7)); //disable int

	REG32_VAL(__GPIO_BASE + offset) = reg; 
	wmt_clr_int(); //replace 2014-8-22
	//REG32_VAL(__GPIO_BASE+0x0360) = 1<<num; //clear interrupt status// 1 bit per int
	msleep(5);
	return 0;
}

int wmt_enable_gpirq(void)
{
	int num = irq_gpio;
	
	if(num > /*11*/ /*19*/42)
		return -1;

	if(num<4)
		REG32_VAL(__GPIO_BASE+0x0300) |= 1<<(num*8+7); //enable interrupt 
	else if(num >= 4 && num < 8)
		REG32_VAL(__GPIO_BASE+0x0304) |= 1<<((num-4)*8+7); //enable interrupt 
	else if (num >= 8 && num < 12)
		REG32_VAL(__GPIO_BASE+0x0308) |= 1<<((num-8)*8+7); //enable interrupt
	else if (num >= 12 && num < 16)
		REG32_VAL(__GPIO_BASE+0x030c) |= 1<<((num-12)*8+7); //enable interrupt 
	else if (num >= 16 && num < 20)
		REG32_VAL(__GPIO_BASE+0x0310) |= 1<<((num-16)*8+7); //enable interrupt 
	else ////vdout11 12 18:21-->gpio 31 32  38:41
	{	// 0x0314----0x0319
		switch (num)
		{
			case WMT_PIN_GP5_VDOUT11:
				REG32_VAL(__GPIO_BASE+0x0314) |= 1<<(0*8+7); //enable interrupt 
				
				break;
			case WMT_PIN_GP5_VDOUT12:
				REG32_VAL(__GPIO_BASE+0x0314) |= 1<<(1*8+7); //enable interrupt 
				break;
			
			case WMT_PIN_GP6_VDOUT18:
				REG32_VAL(__GPIO_BASE+0x0314) |= 1<<(2*8+7); //enable interrupt 
				break;
				
			case WMT_PIN_GP6_VDOUT19:
				REG32_VAL(__GPIO_BASE+0x0314) |= 1<<(3*8+7); //enable interrupt 
				break;
			case WMT_PIN_GP6_VDOUT20:
				REG32_VAL(__GPIO_BASE+0x0318) |= 1<<(0*8+7); //enable interrupt 
				
				break;
			
			case WMT_PIN_GP6_VDOUT21:
			
				REG32_VAL(__GPIO_BASE+0x0318) |= 1<<(1*8+7); //enable interrupt 
				break;
		}	
			
	}		
	
	return 0;
}

int wmt_disable_gpirq(void)
{
	int num = irq_gpio;
	
	if(num > /*11*//*19*/42)
		return -1;
	
	if(num<4)
		REG32_VAL(__GPIO_BASE+0x0300) &= ~(1<<(num*8+7)); //disable interrupt 
	else if(num >= 4 && num < 8)
		REG32_VAL(__GPIO_BASE+0x0304) &= ~(1<<((num-4)*8+7)); //enable interrupt 
	else if (num >= 8 && num <12)
		REG32_VAL(__GPIO_BASE+0x0308) &= ~(1<<((num-8)*8+7)); //enable interrupt
	else if (num >= 12 && num <16)
		REG32_VAL(__GPIO_BASE+0x030c) &= ~(1<<((num-8)*8+7)); //enable interrupt 
	else if (num >= 16 && num <20)
		REG32_VAL(__GPIO_BASE+0x0310) &= ~(1<<((num-8)*8+7)); //enable interrupt
	else ////vdout11 12 18:21-->gpio 31 32  38:41
	{	// 0x0314----0x0319
		switch (num)
		{
			case WMT_PIN_GP5_VDOUT11:
				REG32_VAL(__GPIO_BASE+0x0314) &= ~(1<<(0*8+7)); //enable interrupt 
				
				break;
			case WMT_PIN_GP5_VDOUT12:
				REG32_VAL(__GPIO_BASE+0x0314) &= ~(1<<(1*8+7)); //enable interrupt 
				break;
			
			case WMT_PIN_GP6_VDOUT18:
				REG32_VAL(__GPIO_BASE+0x0314) &= ~(1<<(2*8+7)); //enable interrupt 
				break;
				
			case WMT_PIN_GP6_VDOUT19:
				REG32_VAL(__GPIO_BASE+0x0314) &= ~(1<<(3*8+7)); //enable interrupt 
				break;
			case WMT_PIN_GP6_VDOUT20:
				REG32_VAL(__GPIO_BASE+0x0318) &= ~(1<<(0*8+7)); //enable interrupt 
				
				break;
			
			case WMT_PIN_GP6_VDOUT21:
			
				REG32_VAL(__GPIO_BASE+0x0318) &= ~(1<<(1*8+7)); //enable interrupt 
				break;
		}	
			
	}			
	
	return 0;
}

static int pn544_wmt_is_int(int num)
{
	if (num > 42)
	{
		return 0;
	}
	if (num < 20)
	{
		return REG32_VAL(__GPIO_BASE+0x0360) & (1<<num) ? 1 : 0;
	}
	else
	{
		switch (num)
		{
			case WMT_PIN_GP5_VDOUT11:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<20) ? 1 : 0;
				break;
			case WMT_PIN_GP5_VDOUT12:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<21) ? 1 : 0;
				break;
			case WMT_PIN_GP6_VDOUT18:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<22) ? 1 : 0;
				break;
			case WMT_PIN_GP6_VDOUT19:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<23) ? 1 : 0;
				break;
			case WMT_PIN_GP6_VDOUT20:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<24) ? 1 : 0;
				break;
			case WMT_PIN_GP6_VDOUT21:
				return REG32_VAL(__GPIO_BASE+0x0360) & (1<<25) ? 1 : 0;
				break;
			
		}
		
	}
	//return (REG32_VAL(__GPIO_BASE+0x0360) & (1<<num)) ? 1: 0; 
}
//*************add end


static void pn544_disable_irq(struct pn544_dev *pn544_dev)
{
	unsigned long flags;

	spin_lock_irqsave(&pn544_dev->irq_enabled_lock, flags);
	if (pn544_dev->irq_enabled) {
		wmt_disable_gpirq();
		//disable_irq_nosync(pn544_dev->client->irq);
		pn544_dev->irq_enabled = false;
	}
	spin_unlock_irqrestore(&pn544_dev->irq_enabled_lock, flags);
}

static irqreturn_t pn544_dev_irq_handler(int irq, void *dev_id)
{
	struct pn544_dev *pn544_dev = dev_id;
	//printk("<<<<<<<%s!\n", __func__);
#if 0	
	if (!gpio_get_value(pn544_dev->irq_gpio)) {
		
		return IRQ_NONE;
		//return IRQ_HANDLED;//???
	}
#endif	
	if (pn544_wmt_is_int(pn544_dev->irq_gpio))
	{
		wmt_clr_int();
		pn544_disable_irq(pn544_dev);
	
	//printk("<<<<<<<%s! wakeup reader!\n", __func__);
	/* Wake up waiting readers */
		wake_up(&pn544_dev->read_wq);
		return IRQ_HANDLED;
	}
	else
	{
		return IRQ_NONE;
	}	
}

static ssize_t pn544_dev_read(struct file *filp, char __user *buf,
		size_t count, loff_t *offset)
{
	struct pn544_dev *pn544_dev = filp->private_data;
	char tmp[MAX_BUFFER_SIZE];
	int ret,i;

	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

	printk("%s : reading %zu bytes.\n", __func__, count);

	mutex_lock(&pn544_dev->read_mutex);
	
	printk("%s irq gpio %d val %d\n", __func__, pn544_dev->irq_gpio, gpio_get_value(pn544_dev->irq_gpio));
	if (/*!gpio_get_value(pn544_dev->irq_gpio)*/gpio_get_value(pn544_dev->irq_gpio)!=pn544_dev->irq_active) {
		printk("%s &&& waitting for interrupt!\n", __func__);
		if (filp->f_flags & O_NONBLOCK) {
			ret = -EAGAIN;
			goto fail;
		}

		while (1) {
			pn544_dev->irq_enabled = true;
			wmt_enable_gpirq();
		//enable_irq(pn544_dev->client->irq);
		
			printk("%s &&&  enter waitting for interrupt!\n", __func__);
			
				ret = wait_event_interruptible(
					pn544_dev->read_wq,
					!pn544_dev->irq_enabled/*false??*/);
					
		/*ret = wait_event_interruptible(pn544_dev->read_wq,
				gpio_get_value(pn544_dev->irq_gpio));
		*/

		pn544_disable_irq(pn544_dev);

		if (ret)
			goto fail;
			if (gpio_get_value(pn544_dev->irq_gpio)==pn544_dev->irq_active)
				break;

			pr_warning("%s: spurious interrupt detected\n", __func__);
		}
	}

	/* Read data */
	ret = i2c_master_recv(pn544_dev->client, tmp, count);
	mutex_unlock(&pn544_dev->read_mutex);
	/* pn544 seems to be slow in handling I2C read requests
	 * so add 1ms delay after recv operation */
	udelay(1000);

	if (ret < 0) {
		pr_err("%s: i2c_master_recv returned %d\n", __func__, ret);
		return ret;
	}
	if (ret > count) {
		pr_err("%s: received too many bytes from i2c (%d)\n",
			__func__, ret);
		return -EIO;
	}
	if (copy_to_user(buf, tmp, ret)) {
		pr_warning("%s : failed to copy to user space\n", __func__);
		return -EFAULT;
	}
	
	printk("IFD->PC:");
	for(i = 0; i < ret; i++){
		printk(" %02X", tmp[i]);
	}
	printk("\n");
	
	return ret;

fail:
	printk("%s fail end! ret :%x\n", __func__, ret);
	mutex_unlock(&pn544_dev->read_mutex);
	return ret;
}

static ssize_t pn544_dev_write(struct file *filp, const char __user *buf,
		size_t count, loff_t *offset)
{
	struct pn544_dev  *pn544_dev;
	char tmp[MAX_BUFFER_SIZE];
	int ret,i;

	pn544_dev = filp->private_data;

	if (count > MAX_BUFFER_SIZE)
		count = MAX_BUFFER_SIZE;

	if (copy_from_user(tmp, buf, count)) {
		pr_err("%s : failed to copy from user space\n", __func__);
		return -EFAULT;
	}

	printk("%s : writing %zu bytes.\n", __func__, count);
	/* Write data */
	ret = i2c_master_send(pn544_dev->client, tmp, count);
	if (ret != count) {
		pr_err("%s : i2c_master_send returned %d\n", __func__, ret);
		ret = -EIO;
	}
	printk("PC->IFD:");
	for(i = 0; i < count; i++){
		printk(" %02X", tmp[i]);
	}
	/* pn544 seems to be slow in handling I2C write requests
	 * so add 1ms delay after I2C send oparation */
	udelay(1000);

	return ret;
}

static int pn544_dev_open(struct inode *inode, struct file *filp)
{
	struct pn544_dev *pn544_dev = container_of(filp->private_data,
						struct pn544_dev,
						pn544_device);

	filp->private_data = pn544_dev;

	pr_debug("%s : %d,%d\n", __func__, imajor(inode), iminor(inode));

	return 0;
}

static /*int*/long pn544_dev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct pn544_dev *pn544_dev = filp->private_data;

	switch (cmd) {
	case PN544_SET_PWR:  
		if (arg == 2) {
			/* power on with firmware download (requires hw reset)
			 */
			printk("%s power on with firmware\n", __func__);
			gpio_set_value(pn544_dev->ven_gpio, /*1*/pn544_dev->ven_active);
			msleep(20);
			if (pn544_dev->firm_gpio >= 0)
				gpio_set_value(pn544_dev->firm_gpio, /*1*/pn544_dev->firm_active);
			msleep(20);
			gpio_set_value(pn544_dev->ven_gpio, /*0*/!pn544_dev->ven_active);
			msleep(100);
			gpio_set_value(pn544_dev->ven_gpio, /*1*/pn544_dev->ven_active);
			msleep(20);
			pn544_dev->ven_on_off  = 1;
		} else if (arg == 1) {
			/* power on */
			printk("%s power on\n", __func__);
			if (pn544_dev->firm_gpio >= 0)
				gpio_set_value(pn544_dev->firm_gpio, /*0*/!pn544_dev->firm_active);
			gpio_set_value(pn544_dev->ven_gpio, /*1*/pn544_dev->ven_active);
			msleep(100);
			pn544_dev->ven_on_off  = 1;
		} else  if (arg == 0) {
			/* power off */
			printk("%s power off ven %d \n", __func__, !pn544_dev->ven_active);
			if (pn544_dev->firm_gpio >= 0)
				gpio_set_value(pn544_dev->firm_gpio, /*0*/!pn544_dev->firm_active);
			gpio_set_value(pn544_dev->ven_gpio, /*0*/!pn544_dev->ven_active);
			
			pn544_dev->ven_on_off  = 0;
			//gpio_set_value(pn544_dev->ven_gpio, 0);
			msleep(100);
		} else {
			printk("%s bad arg %u\n", __func__, arg);
			return -EINVAL;
		}
		break;
	default:
		printk("%s bad ioctl %u\n", __func__, cmd);
		return -EINVAL;
	}

	return 0;
}

static const struct file_operations pn544_dev_fops = {
	.owner	= THIS_MODULE,
	.llseek	= no_llseek,
	.read	= pn544_dev_read,
	.write	= pn544_dev_write,
	.open	= pn544_dev_open,
	.unlocked_ioctl  = pn544_dev_ioctl,
};


static int pn544_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
{
	int ret;
	struct pn544_nfc_platform_data *platform_data;
	struct pn544_dev *pn544_dev;

	platform_data = client->dev.platform_data;

	if (platform_data == NULL) {
		pr_err("%s : nfc probe fail\n", __func__);
		return  -ENODEV;
	}

	printk("nfc probe step01 is ok\n");
	
	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
		pr_err("%s : need I2C_FUNC_I2C\n", __func__);
		return  -ENODEV;
	}

	printk("nfc probe step02 is ok\n");
	
	ret = gpio_request(platform_data->irq_gpio, "nfc_int");
	if (ret)
		return  -ENODEV;
	
	
	
	ret = gpio_request(platform_data->ven_gpio, "nfc_ven");
	if (ret)
		goto err_ven;
	
	
	if (platform_data->firm_gpio >= 0)
	{
		
		ret = gpio_request(platform_data->firm_gpio, "nfc_firm");
		if (ret)
			goto err_firm;
		
	}	

	pn544_dev = kzalloc(sizeof(*pn544_dev), GFP_KERNEL);
	if (pn544_dev == NULL) {
		dev_err(&client->dev,
				"failed to allocate memory for module data\n");
		ret = -ENOMEM;
		goto err_exit;
	}

	printk("nfc probe step04 is ok\n");
	
	//pn544_dev->irq_enable = platform_data->irq_enable;
	pn544_dev->irq_gpio = platform_data->irq_gpio;
	pn544_dev->irq_active = platform_data->irq_active;
	
	//pn544_dev->ven_enable  = platform_data->ven_enable;
	pn544_dev->ven_gpio  = platform_data->ven_gpio;
	pn544_dev->ven_active  = platform_data->ven_active;
	pn544_dev->ven_on_off  = 0;
	//pn544_dev->firm_enable  = platform_data->firm_enable;
	pn544_dev->firm_gpio  = platform_data->firm_gpio;
	pn544_dev->firm_active  = platform_data->firm_active;
	
	pn544_dev->client   = client;
	
	irq_gpio = pn544_dev->irq_gpio;
	/* init mutex and queues */
	init_waitqueue_head(&pn544_dev->read_wq);
	mutex_init(&pn544_dev->read_mutex);
	spin_lock_init(&pn544_dev->irq_enabled_lock);

	pn544_dev->pn544_device.minor = MISC_DYNAMIC_MINOR;
	switch (g_chip_nr)
	{
		case 7:
			pn544_dev->pn544_device.name = "pn547"; //"pn544" modify 2014-7-21 
			break;
		case 4:
			pn544_dev->pn544_device.name = "pn544"; //"pn544" modify 2014-8-22
			break;
	}	
	pn544_dev->pn544_device.fops = &pn544_dev_fops;

	ret = misc_register(&pn544_dev->pn544_device);
	if (ret) {
		pr_err("%s : misc_register failed\n", __FILE__);
		goto err_misc_register;
	}
	printk("nfc probe step05 is ok\n");

	/* request irq.  the irq is set whenever the chip has data available
	 * for reading.  it is cleared when all data has been read.
	 */
	
	
	gpio_direction_output(platform_data->ven_gpio,/*0*/ !platform_data->ven_active);
	
	if (platform_data->firm_gpio >= 0)
		gpio_direction_output(platform_data->firm_gpio,/*0*/!platform_data->firm_active);
	//s3c_gpio_setpull(platform_data->ven_gpio, S3C_GPIO_PULL_UP);
	//s3c_gpio_setpull(platform_data->firm_gpio, S3C_GPIO_PULL_DOWN);

	//eint16 setting
	
	gpio_direction_input(platform_data->irq_gpio);
	//wmt_gpio_setpull(platform_data->irq_gpio, WMT_GPIO_PULL_UP);
	if (platform_data->irq_active)
	{
		printk("%s irq pull down!\n", __func__);
		wmt_gpio_setpull(platform_data->irq_gpio, WMT_GPIO_PULL_DOWN); //modify 2014-7-16
	}	
	else
	{
		printk("%s irq pull up!\n", __func__);
		wmt_gpio_setpull(platform_data->irq_gpio, WMT_GPIO_PULL_UP);	
	}	
	//s3c_gpio_setpull(platform_data->irq_gpio, S3C_GPIO_PULL_UP);

	
	pr_info("%s : requesting IRQ %d\n", __func__, client->irq);
	pn544_dev->irq_enabled = true;

	ret = request_irq(client->irq, pn544_dev_irq_handler,
			  /*platform_data->irq_active?IRQF_TRIGGER_HIGH:IRQF_TRIGGER_LOW*/IRQF_SHARED, \
			  client->name, pn544_dev);//IRQF_TRIGGER_RISING  IRQF_TRIGGER_HIGH  
	if (ret) {
		printk(/*&client->dev, */"request_irq failed\n");
		goto err_request_irq_failed;
	}
	printk("nfc probe step06 is ok\n");
	//add 2014-7-16 for gpio irq config
	wmt_set_irqinput();
	wmt_set_gpirq(platform_data->irq_active?IRQF_TRIGGER_HIGH:IRQF_TRIGGER_LOW); //IRQF_TRIGGER_HIGH
	//wmt_enable_gpirq();
	//add end
	
	pn544_disable_irq(pn544_dev);
	i2c_set_clientdata(client, pn544_dev);
	
	//add debug 2014-7-10
#if 0		
	printk("%s power on\n", __func__);
	gpio_set_value(pn544_dev->firm_gpio, 0);
	gpio_set_value(pn544_dev->ven_gpio, 1);
	msleep(10);

	int maddr = 1;
	char mbuf[2] = {0};
	for (maddr=1; maddr<0x7f; maddr++)
	{
		mbuf[0] = maddr;
		/* Write data */
		//pn544_dev->client->addr = maddr;
	ret = i2c_master_send(pn544_dev->client, mbuf, 1);
	if (ret != 1) {
		pr_err("%s : reg 0x%x i2c_master_send returned %d\n", __func__, maddr, ret);
		//ret = -EIO;
	}
	else
	{
		pr_err("%s ok!!!: reg 0x%x i2c_master_send returned %d\n", __func__, maddr, ret);
		//break;
	}
	
	struct i2c_msg msg[2] = {
		{.addr = client->addr,
		 .flags = 0|I2C_M_NOSTART,
		 .len = 1,
		 .buf = &mbuf[0],
		},
		{ .addr = client->addr,
		  .flags = I2C_M_RD,
		  .len = 1,
		  .buf = &mbuf[1],
		},
		};
	
	ret = i2c_transfer(client->adapter, msg, 2);
	
	//ret = i2c_master_recv(pn544_dev->client, mbuf, 1);
	if (ret != 2) {
		pr_err("%s : addr 0x%x i2c_master_recv %d returned %d\n", __func__, maddr, mbuf[1], ret);
		//ret = -EIO;
	}
	else
	{
		pr_err("%s ok!!!: addr 0x%x i2c_master_recv %d returned %d\n", __func__, maddr, mbuf[1], ret);
		//break;
	}
	
	}
#endif	
	//add end
	
	printk("nfc probe step07 is ok\n");

	return 0;

err_request_irq_failed:
	misc_deregister(&pn544_dev->pn544_device);
err_misc_register:
	mutex_destroy(&pn544_dev->read_mutex);
	kfree(pn544_dev);
err_exit:
	if (platform_data->firm_gpio >= 0)
		gpio_free(platform_data->firm_gpio);
err_firm:
	
	gpio_free(platform_data->ven_gpio);
err_ven:
	
	gpio_free(platform_data->irq_gpio);
	return ret;
}

static int pn544_remove(struct i2c_client *client)
{
	struct pn544_dev *pn544_dev;

	pn544_dev = i2c_get_clientdata(client);
	free_irq(client->irq, pn544_dev);
	misc_deregister(&pn544_dev->pn544_device);
	mutex_destroy(&pn544_dev->read_mutex);
	gpio_free(pn544_dev->irq_gpio);
	gpio_free(pn544_dev->ven_gpio);
	gpio_free(pn544_dev->firm_gpio);
	kfree(pn544_dev);

	return 0;
}

static const struct i2c_device_id pn544_id[] = {
	{ "pn544", 0 },
	{ }
};

static int pn544_suspend(struct i2c_client *client, pm_message_t mesg)
{
	struct pn544_dev *pn544_dev;

	pn544_dev = i2c_get_clientdata(client);
	printk("\n%s on_off %d\n", __func__, pn544_dev->ven_on_off);
	return 0;
}

static int pn544_resume(struct i2c_client *client)
{
	struct pn544_dev *pn544_dev;

	pn544_dev = i2c_get_clientdata(client);
	
	printk("%s on_off %d\n", __func__, pn544_dev->ven_on_off);
	
	gpio_direction_input(pn544_dev->irq_gpio);
	//wmt_gpio_setpull(platform_data->irq_gpio, WMT_GPIO_PULL_UP);
	if (pn544_dev->irq_active)
		wmt_gpio_setpull(pn544_dev->irq_gpio, WMT_GPIO_PULL_DOWN); //modify 2014-7-16
	else
		wmt_gpio_setpull(pn544_dev->irq_gpio, WMT_GPIO_PULL_UP);	
		
	//add 2014-7-16 for gpio irq config
	wmt_set_irqinput();
	wmt_set_gpirq(pn544_dev->irq_active?IRQF_TRIGGER_HIGH:IRQF_TRIGGER_LOW); //IRQF_TRIGGER_HIGH
	//wmt_enable_gpirq();
	//add end
	
	pn544_disable_irq(pn544_dev);
	
	printk("%s active %d, !active %d\n", __func__, pn544_dev->ven_active, !pn544_dev->ven_active);
	if (pn544_dev->ven_on_off)
		gpio_set_value(pn544_dev->ven_gpio, pn544_dev->ven_active);
	else
		gpio_set_value(pn544_dev->ven_gpio, !pn544_dev->ven_active);
	
	//gpio_set_value(pn544_dev->ven_gpio, 0);	
	
	return 0;
}
static struct i2c_driver pn544_driver = {
	.id_table	= pn544_id,
	.probe		= pn544_probe,
	.remove		= pn544_remove,
	.suspend	= pn544_suspend,
	.resume		= pn544_resume,
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "pn544",
	},
};

/*
 * module load/unload record keeping
 */

static struct pn544_nfc_platform_data pn544_pdata = {
	//.irq_enable = 1,
	.irq_gpio = WMT_PIN_GP0_GPIO0,
	.irq_active = 1,
	//.ven_enable = 1,
	.ven_gpio= WMT_PIN_GP18_UART0RTS, // WMT_PIN_GP18_UART0RTS    WMT_PIN_GP0_GPIO1
	.ven_active = 1,
	//.firm_enable = 1,
	.firm_gpio=WMT_PIN_GP0_GPIO1,	//WMT_PIN_GP0_GPIO2,
	.firm_active = 1,
};
static int g_i2c_adapter = 0;
static int g_i2c_addr = 0x28;
static int get_board_info(void)
{
	int ret;
	char buf[100] = {0};
	int len = sizeof(buf);
	
	char *pbuf = "wmt.nfc.pn54x"; // wmt.nfc.pn547
	printk("%s\n", __func__);
	//wmt.nfc.pn54x 4:43:0:39:1:1:1:132:1  2b-->43  wmt.nfc.pn54x 4:43:0:39:1:1:0:132:1
	ret = wmt_getsyspara(pbuf, buf, &len);
	if (!ret)
	{
		printk("%s %s:%s\n", __func__, pbuf, buf);//irq ven firm
		
		ret = sscanf(buf, "%d:%d:%d:%d:%d:%d:%d:%d:%d",  \
			&g_chip_nr, &g_i2c_addr, &g_i2c_adapter, \
			&pn544_pdata.irq_gpio, &pn544_pdata.irq_active, \
			&pn544_pdata.ven_gpio, &pn544_pdata.ven_active, \
			&pn544_pdata.firm_gpio, &pn544_pdata.firm_active);
		
		printk("chip nr %d,i2c addr %d adapter %d, irq gpio %d active %d, ven %d %d, firm %d %d\n", \
			g_chip_nr, g_i2c_addr, g_i2c_adapter, \
			pn544_pdata.irq_gpio, pn544_pdata.irq_active, \
			pn544_pdata.ven_gpio, pn544_pdata.ven_active, \
			pn544_pdata.firm_gpio, pn544_pdata.firm_active);
			
		return 0;
	}
	else
	{
		printk("%s not get %s, use default\n", __func__, pbuf);
		return -1;
	}
	
	return 0;
}

static int __init pn544_dev_init(void)
{
	//pr_info("Loading pn544 driver\n");
	
	int r;
	struct i2c_adapter *adapter;
		
		
	
	struct i2c_board_info wmt_pn544_bi = {
		.type		   = PN544_DRIVER_NAME,
		.flags		   = 0x00,
		.addr		   = CLIENT_ADDR,
		.platform_data = &pn544_pdata, //custom 2014-7-25
		.archdata	   = NULL,
		.irq		   = IRQ_GPIO,
	};
	//wmt_pn544_bi.addr = g_i2c_addr;
	
	pr_debug(DRIVER_DESC ": %s\n", __func__);
	r = get_board_info();
	if (r < 0)
	{
		printk("%s no env!!\n", __func__);
		return r;
	}
	wmt_pn544_bi.addr = g_i2c_addr;
	adapter = i2c_get_adapter(/*WMT_PN544_I2C_CHANNEL*/g_i2c_adapter);
	if (adapter == NULL) {
		printk("can not get i2c adapter, client address error");
		return -ENODEV;
	}
	
	pn544_client = i2c_new_device(adapter, &wmt_pn544_bi);
	if ( pn544_client == NULL) {
		printk("allocate i2c client failed");
		return -ENOMEM;
	}
	
	i2c_put_adapter(adapter);
		
	r = i2c_add_driver(&pn544_driver);
	if (r) {
		pr_err(PN544_DRIVER_NAME ": driver registration failed\n");
		return r;
	}
	
	return 0;
	//return i2c_add_driver(&pn544_driver);
}
module_init(pn544_dev_init);

static void __exit pn544_dev_exit(void)
{
	pr_info("Unloading pn544 driver\n");
	i2c_del_driver(&pn544_driver);
	
	i2c_unregister_device(pn544_client);
}
module_exit(pn544_dev_exit);

MODULE_AUTHOR("Sylvain Fonteneau");
MODULE_DESCRIPTION("NFC PN544 driver");
MODULE_LICENSE("GPL");