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
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
|
/*
* Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
*/
/*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*/
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/sched.h>
#include <linux/time.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/pmic_adc.h>
#include <linux/pmic_status.h>
#include "../core/pmic.h"
#define DEF_ADC_0 0x008000
#define DEF_ADC_3 0x0001c0
#define ADC_NB_AVAILABLE 2
#define MAX_CHANNEL 7
#define MC13892_ADC0_TS_M_LSH 14
#define MC13892_ADC0_TS_M_WID 3
static int pmic_adc_major;
static struct class *pmic_adc_class;
/*
* Maximun allowed variation in the three X/Y co-ordinates acquired from
* touch-screen
*/
#ifdef CONFIG_TOUCH_MXC_DELTA_X
#define DELTA_X_MAX CONFIG_TOUCH_MXC_DELTA_X
#else
#define DELTA_X_MAX 50
#endif
#ifdef CONFIG_TOUCH_MXC_DELTA_Y
#define DELTA_Y_MAX CONFIG_TOUCH_MXC_DELTA_Y
#else
#define DELTA_Y_MAX 50
#endif
/*
* ADC 0
*/
#define ADC_WAIT_TSI_0 0x001400
#define ADC_INC 0x030000
#define ADC_BIS 0x800000
#define ADC_CHRGRAW_D5 0x008000
/*
* ADC 1
*/
#define ADC_EN 0x000001
#define ADC_SGL_CH 0x000002
#define ADC_ADSEL 0x000008
#define ADC_CH_0_POS 5
#define ADC_CH_0_MASK 0x0000E0
#define ADC_CH_1_POS 8
#define ADC_CH_1_MASK 0x000700
#define ADC_DELAY_POS 11
#define ADC_DELAY_MASK 0x07F800
#define ADC_ATO 0x080000
#define ASC_ADC 0x100000
#define ADC_WAIT_TSI_1 0x200001
#define ADC_NO_ADTRIG 0x200000
/*
* ADC 2 - 4
*/
#define ADD1_RESULT_MASK 0x00000FFC
#define ADD2_RESULT_MASK 0x00FFC000
#define ADC_TS_MASK 0x00FFCFFC
#define ADC_WCOMP 0x040000
#define ADC_WCOMP_H_POS 0
#define ADC_WCOMP_L_POS 9
#define ADC_WCOMP_H_MASK 0x00003F
#define ADC_WCOMP_L_MASK 0x007E00
#define ADC_MODE_MASK 0x00003F
#define ADC_INT_BISDONEI 0x02
#define ADC_TSMODE_MASK 0x007000
typedef enum adc_state {
ADC_FREE,
ADC_USED,
ADC_MONITORING,
} t_adc_state;
typedef enum reading_mode {
/*!
* Enables lithium cell reading
*/
M_LITHIUM_CELL = 0x000001,
/*!
* Enables charge current reading
*/
M_CHARGE_CURRENT = 0x000002,
/*!
* Enables battery current reading
*/
M_BATTERY_CURRENT = 0x000004,
} t_reading_mode;
typedef struct {
/*!
* Delay before first conversion
*/
unsigned int delay;
/*!
* sets the ATX bit for delay on all conversion
*/
bool conv_delay;
/*!
* Sets the single channel mode
*/
bool single_channel;
/*!
* Channel selection 1
*/
t_channel channel_0;
/*!
* Channel selection 2
*/
t_channel channel_1;
/*!
* Used to configure ADC mode with t_reading_mode
*/
t_reading_mode read_mode;
/*!
* Sets the Touch screen mode
*/
bool read_ts;
/*!
* Wait TSI event before touch screen reading
*/
bool wait_tsi;
/*!
* Sets CHRGRAW scaling to divide by 5
* Only supported on 2.0 and higher
*/
bool chrgraw_devide_5;
/*!
* Return ADC values
*/
unsigned int value[8];
/*!
* Return touch screen values
*/
t_touch_screen ts_value;
} t_adc_param;
static int pmic_adc_filter(t_touch_screen *ts_curr);
int mc13892_adc_request(bool read_ts);
int mc13892_adc_release(int adc_index);
t_reading_mode mc13892_set_read_mode(t_channel channel);
PMIC_STATUS mc13892_adc_read_ts(t_touch_screen *touch_sample, int wait_tsi);
/* internal function */
static void callback_tsi(void *);
static void callback_adcdone(void *);
static void callback_adcbisdone(void *);
static int swait;
static int suspend_flag;
static wait_queue_head_t suspendq;
/* EXPORTED FUNCTIONS */
EXPORT_SYMBOL(pmic_adc_init);
EXPORT_SYMBOL(pmic_adc_deinit);
EXPORT_SYMBOL(pmic_adc_convert);
EXPORT_SYMBOL(pmic_adc_convert_8x);
EXPORT_SYMBOL(pmic_adc_set_touch_mode);
EXPORT_SYMBOL(pmic_adc_get_touch_mode);
EXPORT_SYMBOL(pmic_adc_get_touch_sample);
static DECLARE_COMPLETION(adcdone_it);
static DECLARE_COMPLETION(adcbisdone_it);
static DECLARE_COMPLETION(adc_tsi);
static pmic_event_callback_t tsi_event;
static pmic_event_callback_t event_adc;
static pmic_event_callback_t event_adc_bis;
static bool data_ready_adc_1;
static bool data_ready_adc_2;
static bool adc_ts;
static bool wait_ts;
static bool monitor_en;
static bool monitor_adc;
static DECLARE_MUTEX(convert_mutex);
static DECLARE_WAIT_QUEUE_HEAD(queue_adc_busy);
static t_adc_state adc_dev[2];
static unsigned channel_num[] = {
0,
1,
3,
4,
2,
0,
1,
3,
4,
-1,
5,
6,
7,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1,
-1
};
static bool pmic_adc_ready;
int is_pmic_adc_ready()
{
return pmic_adc_ready;
}
EXPORT_SYMBOL(is_pmic_adc_ready);
static int pmic_adc_suspend(struct platform_device *pdev, pm_message_t state)
{
suspend_flag = 1;
CHECK_ERROR(pmic_write_reg(REG_ADC0, DEF_ADC_0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC1, 0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC2, 0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC3, DEF_ADC_3, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC4, 0, PMIC_ALL_BITS));
return 0;
};
static int pmic_adc_resume(struct platform_device *pdev)
{
/* nothing for mc13892 adc */
unsigned int adc_0_reg, adc_1_reg, reg_mask;
suspend_flag = 0;
/* let interrupt of TSI again */
adc_0_reg = ADC_WAIT_TSI_0;
reg_mask = ADC_WAIT_TSI_0;
CHECK_ERROR(pmic_write_reg(REG_ADC0, adc_0_reg, reg_mask));
adc_1_reg = ADC_WAIT_TSI_1 | (ADC_BIS * adc_ts);
CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg, PMIC_ALL_BITS));
while (swait > 0) {
swait--;
wake_up_interruptible(&suspendq);
}
return 0;
};
static void callback_tsi(void *unused)
{
pr_debug("*** TSI IT mc13892 PMIC_ADC_GET_TOUCH_SAMPLE ***\n");
if (wait_ts) {
complete(&adc_tsi);
pmic_event_mask(EVENT_TSI);
}
}
static void callback_adcdone(void *unused)
{
if (data_ready_adc_1)
complete(&adcdone_it);
}
static void callback_adcbisdone(void *unused)
{
pr_debug("* adcdone bis it callback *\n");
if (data_ready_adc_2)
complete(&adcbisdone_it);
}
static int pmic_adc_filter(t_touch_screen *ts_curr)
{
unsigned int ydiff, xdiff;
unsigned int sample_sumx, sample_sumy;
if (ts_curr->contact_resistance == 0) {
ts_curr->x_position = 0;
ts_curr->y_position = 0;
return 0;
}
ydiff = abs(ts_curr->y_position1 - ts_curr->y_position2);
if (ydiff > DELTA_Y_MAX) {
pr_debug("pmic_adc_filter: Ret pos y\n");
return -1;
}
xdiff = abs(ts_curr->x_position1 - ts_curr->x_position2);
if (xdiff > DELTA_X_MAX) {
pr_debug("mc13892_adc_filter: Ret pos x\n");
return -1;
}
sample_sumx = ts_curr->x_position1 + ts_curr->x_position2;
sample_sumy = ts_curr->y_position1 + ts_curr->y_position2;
ts_curr->y_position = sample_sumy / 2;
ts_curr->x_position = sample_sumx / 2;
return 0;
}
int pmic_adc_init(void)
{
unsigned int reg_value = 0, i = 0;
if (suspend_flag == 1)
return -EBUSY;
for (i = 0; i < ADC_NB_AVAILABLE; i++)
adc_dev[i] = ADC_FREE;
CHECK_ERROR(pmic_write_reg(REG_ADC0, DEF_ADC_0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC1, 0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC2, 0, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC3, DEF_ADC_3, PMIC_ALL_BITS));
CHECK_ERROR(pmic_write_reg(REG_ADC4, 0, PMIC_ALL_BITS));
reg_value = 0x001000;
data_ready_adc_1 = false;
data_ready_adc_2 = false;
adc_ts = false;
wait_ts = false;
monitor_en = false;
monitor_adc = false;
/* sub to ADCDone IT */
event_adc.param = NULL;
event_adc.func = callback_adcdone;
CHECK_ERROR(pmic_event_subscribe(EVENT_ADCDONEI, event_adc));
/* sub to ADCDoneBis IT */
event_adc_bis.param = NULL;
event_adc_bis.func = callback_adcbisdone;
CHECK_ERROR(pmic_event_subscribe(EVENT_ADCBISDONEI, event_adc_bis));
/* sub to Touch Screen IT */
tsi_event.param = NULL;
tsi_event.func = callback_tsi;
CHECK_ERROR(pmic_event_subscribe(EVENT_TSI, tsi_event));
return PMIC_SUCCESS;
}
PMIC_STATUS pmic_adc_deinit(void)
{
CHECK_ERROR(pmic_event_unsubscribe(EVENT_ADCDONEI, event_adc));
CHECK_ERROR(pmic_event_unsubscribe(EVENT_ADCBISDONEI, event_adc_bis));
CHECK_ERROR(pmic_event_unsubscribe(EVENT_TSI, tsi_event));
return PMIC_SUCCESS;
}
int mc13892_adc_init_param(t_adc_param * adc_param)
{
int i = 0;
if (suspend_flag == 1)
return -EBUSY;
adc_param->delay = 0;
adc_param->conv_delay = false;
adc_param->single_channel = false;
adc_param->channel_0 = BATTERY_VOLTAGE;
adc_param->channel_1 = BATTERY_VOLTAGE;
adc_param->read_mode = 0;
adc_param->wait_tsi = 0;
adc_param->chrgraw_devide_5 = true;
adc_param->read_ts = false;
adc_param->ts_value.x_position = 0;
adc_param->ts_value.y_position = 0;
adc_param->ts_value.contact_resistance = 0;
for (i = 0; i <= MAX_CHANNEL; i++)
adc_param->value[i] = 0;
return 0;
}
PMIC_STATUS mc13892_adc_convert(t_adc_param * adc_param)
{
bool use_bis = false;
unsigned int adc_0_reg = 0, adc_1_reg = 0, reg_1 = 0, result_reg =
0, i = 0;
unsigned int result = 0, temp = 0;
pmic_version_t mc13892_ver;
pr_debug("mc13892 ADC - mc13892_adc_convert ....\n");
if (suspend_flag == 1)
return -EBUSY;
if (adc_param->wait_tsi) {
/* configure adc to wait tsi interrupt */
INIT_COMPLETION(adc_tsi);
/*for ts don't use bis */
/*put ts in interrupt mode */
/* still kep reference? */
adc_0_reg = 0x001400 | (ADC_BIS * 0);
pmic_event_unmask(EVENT_TSI);
CHECK_ERROR(pmic_write_reg(REG_ADC0, adc_0_reg, PMIC_ALL_BITS));
/*for ts don't use bis */
adc_1_reg = 0x200001 | (ADC_BIS * 0);
CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg, PMIC_ALL_BITS));
pr_debug("wait tsi ....\n");
wait_ts = true;
wait_for_completion_interruptible(&adc_tsi);
wait_ts = false;
}
down(&convert_mutex);
use_bis = mc13892_adc_request(adc_param->read_ts);
if (use_bis < 0) {
pr_debug("process has received a signal and got interrupted\n");
return -EINTR;
}
/* CONFIGURE ADC REG 0 */
adc_0_reg = 0;
adc_1_reg = 0;
if (adc_param->read_ts == false) {
adc_0_reg = adc_param->read_mode & 0x00003F;
/* add auto inc */
adc_0_reg |= ADC_INC;
if (use_bis) {
/* add adc bis */
adc_0_reg |= ADC_BIS;
}
mc13892_ver = pmic_get_version();
if (mc13892_ver.revision >= 20)
if (adc_param->chrgraw_devide_5)
adc_0_reg |= ADC_CHRGRAW_D5;
if (adc_param->single_channel)
adc_1_reg |= ADC_SGL_CH;
if (adc_param->conv_delay)
adc_1_reg |= ADC_ATO;
if (adc_param->single_channel)
adc_1_reg |= ADC_SGL_CH;
adc_1_reg |= (adc_param->channel_0 << ADC_CH_0_POS) &
ADC_CH_0_MASK;
adc_1_reg |= (adc_param->channel_1 << ADC_CH_1_POS) &
ADC_CH_1_MASK;
} else {
adc_0_reg = 0x002400 | (ADC_BIS * use_bis) | ADC_INC;
}
pr_debug("Write Reg %i = %x\n", REG_ADC0, adc_0_reg);
/*Change has been made here */
CHECK_ERROR(pmic_write_reg(REG_ADC0, adc_0_reg,
ADC_INC | ADC_BIS | ADC_CHRGRAW_D5 |
0xfff00ff));
/* CONFIGURE ADC REG 1 */
if (adc_param->read_ts == false) {
adc_1_reg |= ADC_NO_ADTRIG;
adc_1_reg |= ADC_EN;
adc_1_reg |= (adc_param->delay << ADC_DELAY_POS) &
ADC_DELAY_MASK;
if (use_bis)
adc_1_reg |= ADC_BIS;
} else {
/* configure and start convert to read x and y position */
/* configure to read 2 value in channel selection 1 & 2 */
adc_1_reg = 0x100409 | (ADC_BIS * use_bis) | ADC_NO_ADTRIG;
/* set ATOx = 5, it could be better for ts ADC */
adc_1_reg |= 0x002800;
}
reg_1 = adc_1_reg;
if (use_bis == 0) {
data_ready_adc_1 = false;
adc_1_reg |= ASC_ADC;
data_ready_adc_1 = true;
pr_debug("Write Reg %i = %x\n", REG_ADC1, adc_1_reg);
INIT_COMPLETION(adcdone_it);
CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg,
ADC_SGL_CH | ADC_ATO | ADC_ADSEL
| ADC_CH_0_MASK | ADC_CH_1_MASK |
ADC_NO_ADTRIG | ADC_EN |
ADC_DELAY_MASK | ASC_ADC | ADC_BIS));
pr_debug("wait adc done \n");
wait_for_completion_interruptible(&adcdone_it);
data_ready_adc_1 = false;
} else {
data_ready_adc_2 = false;
adc_1_reg |= ASC_ADC;
data_ready_adc_2 = true;
INIT_COMPLETION(adcbisdone_it);
CHECK_ERROR(pmic_write_reg(REG_ADC1, adc_1_reg, 0xFFFFFF));
temp = 0x800000;
CHECK_ERROR(pmic_write_reg(REG_ADC3, temp, 0xFFFFFF));
pr_debug("wait adc done bis\n");
wait_for_completion_interruptible(&adcbisdone_it);
data_ready_adc_2 = false;
}
/* read result and store in adc_param */
result = 0;
if (use_bis == 0)
result_reg = REG_ADC2;
else
result_reg = REG_ADC4;
CHECK_ERROR(pmic_write_reg(REG_ADC1, 4 << ADC_CH_1_POS,
ADC_CH_0_MASK | ADC_CH_1_MASK));
for (i = 0; i <= 3; i++) {
CHECK_ERROR(pmic_read_reg(result_reg, &result, PMIC_ALL_BITS));
adc_param->value[i] = ((result & ADD1_RESULT_MASK) >> 2);
adc_param->value[i + 4] = ((result & ADD2_RESULT_MASK) >> 14);
pr_debug("value[%d] = %d, value[%d] = %d\n",
i, adc_param->value[i],
i + 4, adc_param->value[i + 4]);
}
if (adc_param->read_ts) {
adc_param->ts_value.x_position = adc_param->value[0];
adc_param->ts_value.x_position1 = adc_param->value[0];
adc_param->ts_value.x_position2 = adc_param->value[1];
adc_param->ts_value.y_position = adc_param->value[3];
adc_param->ts_value.y_position1 = adc_param->value[3];
adc_param->ts_value.y_position2 = adc_param->value[4];
adc_param->ts_value.contact_resistance = adc_param->value[6];
CHECK_ERROR(pmic_write_reg(REG_ADC0, 0x0,
ADC_TSMODE_MASK));
}
/*if (adc_param->read_ts) {
adc_param->ts_value.x_position = adc_param->value[2];
adc_param->ts_value.y_position = adc_param->value[5];
adc_param->ts_value.contact_resistance = adc_param->value[6];
} */
mc13892_adc_release(use_bis);
up(&convert_mutex);
return PMIC_SUCCESS;
}
t_reading_mode mc13892_set_read_mode(t_channel channel)
{
t_reading_mode read_mode = 0;
switch (channel) {
case CHARGE_CURRENT:
read_mode = M_CHARGE_CURRENT;
break;
case BATTERY_CURRENT:
read_mode = M_BATTERY_CURRENT;
break;
default:
read_mode = 0;
}
return read_mode;
}
PMIC_STATUS pmic_adc_convert(t_channel channel, unsigned short *result)
{
t_adc_param adc_param;
PMIC_STATUS ret;
if (suspend_flag == 1)
return -EBUSY;
channel = channel_num[channel];
if (channel == -1) {
pr_debug("Wrong channel ID\n");
return PMIC_PARAMETER_ERROR;
}
mc13892_adc_init_param(&adc_param);
pr_debug("pmic_adc_convert\n");
adc_param.read_ts = false;
adc_param.single_channel = true;
adc_param.read_mode = mc13892_set_read_mode(channel);
/* Find the group */
if (channel <= 7)
adc_param.channel_0 = channel;
else
return PMIC_PARAMETER_ERROR;
ret = mc13892_adc_convert(&adc_param);
*result = adc_param.value[0];
return ret;
}
PMIC_STATUS pmic_adc_convert_8x(t_channel channel, unsigned short *result)
{
t_adc_param adc_param;
int i;
PMIC_STATUS ret;
if (suspend_flag == 1)
return -EBUSY;
channel = channel_num[channel];
if (channel == -1) {
pr_debug("Wrong channel ID\n");
return PMIC_PARAMETER_ERROR;
}
mc13892_adc_init_param(&adc_param);
pr_debug("pmic_adc_convert_8x\n");
adc_param.read_ts = false;
adc_param.single_channel = true;
adc_param.read_mode = mc13892_set_read_mode(channel);
if (channel <= 7) {
adc_param.channel_0 = channel;
adc_param.channel_1 = channel;
} else
return PMIC_PARAMETER_ERROR;
ret = mc13892_adc_convert(&adc_param);
for (i = 0; i <= 7; i++)
result[i] = adc_param.value[i];
return ret;
}
PMIC_STATUS pmic_adc_set_touch_mode(t_touch_mode touch_mode)
{
if (suspend_flag == 1)
return -EBUSY;
CHECK_ERROR(pmic_write_reg(REG_ADC0,
BITFVAL(MC13892_ADC0_TS_M, touch_mode),
BITFMASK(MC13892_ADC0_TS_M)));
return PMIC_SUCCESS;
}
PMIC_STATUS pmic_adc_get_touch_mode(t_touch_mode * touch_mode)
{
unsigned int value;
if (suspend_flag == 1)
return -EBUSY;
CHECK_ERROR(pmic_read_reg(REG_ADC0, &value, PMIC_ALL_BITS));
*touch_mode = BITFEXT(value, MC13892_ADC0_TS_M);
return PMIC_SUCCESS;
}
PMIC_STATUS pmic_adc_get_touch_sample(t_touch_screen *touch_sample, int wait)
{
if (mc13892_adc_read_ts(touch_sample, wait) != 0)
return PMIC_ERROR;
if (0 == pmic_adc_filter(touch_sample))
return PMIC_SUCCESS;
else
return PMIC_ERROR;
}
PMIC_STATUS mc13892_adc_read_ts(t_touch_screen *ts_value, int wait_tsi)
{
t_adc_param param;
pr_debug("mc13892_adc : mc13892_adc_read_ts\n");
if (suspend_flag == 1)
return -EBUSY;
if (wait_ts) {
pr_debug("mc13892_adc : error TS busy \n");
return PMIC_ERROR;
}
mc13892_adc_init_param(¶m);
param.wait_tsi = wait_tsi;
param.read_ts = true;
if (mc13892_adc_convert(¶m) != 0)
return PMIC_ERROR;
/* check if x-y is ok */
if (param.ts_value.contact_resistance < 1000) {
ts_value->x_position = param.ts_value.x_position;
ts_value->x_position1 = param.ts_value.x_position1;
ts_value->x_position2 = param.ts_value.x_position2;
ts_value->y_position = param.ts_value.y_position;
ts_value->y_position1 = param.ts_value.y_position1;
ts_value->y_position2 = param.ts_value.y_position2;
ts_value->contact_resistance =
param.ts_value.contact_resistance + 1;
} else {
ts_value->x_position = 0;
ts_value->y_position = 0;
ts_value->contact_resistance = 0;
}
return PMIC_SUCCESS;
}
int mc13892_adc_request(bool read_ts)
{
int adc_index = -1;
if (read_ts != 0) {
/*for ts we use bis=0 */
if (adc_dev[0] == ADC_USED)
return -1;
/*no wait here */
adc_dev[0] = ADC_USED;
adc_index = 0;
} else {
/*for other adc use bis = 1 */
if (adc_dev[1] == ADC_USED) {
return -1;
/*no wait here */
}
adc_dev[1] = ADC_USED;
adc_index = 1;
}
pr_debug("mc13892_adc : request ADC %d\n", adc_index);
return adc_index;
}
int mc13892_adc_release(int adc_index)
{
while (suspend_flag == 1) {
swait++;
/* Block if the device is suspended */
if (wait_event_interruptible(suspendq, (suspend_flag == 0)))
return -ERESTARTSYS;
}
pr_debug("mc13892_adc : release ADC %d\n", adc_index);
if ((adc_dev[adc_index] == ADC_MONITORING) ||
(adc_dev[adc_index] == ADC_USED)) {
adc_dev[adc_index] = ADC_FREE;
wake_up(&queue_adc_busy);
return 0;
}
return -1;
}
#ifdef DEBUG
static t_adc_param adc_param_db;
static ssize_t adc_info(struct device *dev, struct device_attribute *attr,
char *buf)
{
int *value = adc_param_db.value;
pr_debug("adc_info\n");
pr_debug("ch0\t\t%d\n", adc_param_db.channel_0);
pr_debug("ch1\t\t%d\n", adc_param_db.channel_1);
pr_debug("d5\t\t%d\n", adc_param_db.chrgraw_devide_5);
pr_debug("conv delay\t%d\n", adc_param_db.conv_delay);
pr_debug("delay\t\t%d\n", adc_param_db.delay);
pr_debug("read mode\t%d\n", adc_param_db.read_mode);
pr_debug("read ts\t\t%d\n", adc_param_db.read_ts);
pr_debug("single ch\t%d\n", adc_param_db.single_channel);
pr_debug("wait ts int\t%d\n", adc_param_db.wait_tsi);
pr_debug("value0-3:\t%d\t%d\t%d\t%d\n", value[0], value[1],
value[2], value[3]);
pr_debug("value4-7:\t%d\t%d\t%d\t%d\n", value[4], value[5],
value[6], value[7]);
return 0;
}
enum {
ADC_SET_CH0 = 0,
ADC_SET_CH1,
ADC_SET_DV5,
ADC_SET_CON_DELAY,
ADC_SET_DELAY,
ADC_SET_RM,
ADC_SET_RT,
ADC_SET_S_CH,
ADC_SET_WAIT_TS,
ADC_INIT_P,
ADC_START,
ADC_TS,
ADC_TS_READ,
ADC_TS_CAL,
ADC_CMD_MAX
};
static const char *const adc_cmd[ADC_CMD_MAX] = {
[ADC_SET_CH0] = "ch0",
[ADC_SET_CH1] = "ch1",
[ADC_SET_DV5] = "dv5",
[ADC_SET_CON_DELAY] = "cd",
[ADC_SET_DELAY] = "dl",
[ADC_SET_RM] = "rm",
[ADC_SET_RT] = "rt",
[ADC_SET_S_CH] = "sch",
[ADC_SET_WAIT_TS] = "wt",
[ADC_INIT_P] = "init",
[ADC_START] = "start",
[ADC_TS] = "touch",
[ADC_TS_READ] = "touchr",
[ADC_TS_CAL] = "cal"
};
static int cmd(unsigned int index, int value)
{
t_touch_screen ts;
switch (index) {
case ADC_SET_CH0:
adc_param_db.channel_0 = value;
break;
case ADC_SET_CH1:
adc_param_db.channel_1 = value;
break;
case ADC_SET_DV5:
adc_param_db.chrgraw_devide_5 = value;
break;
case ADC_SET_CON_DELAY:
adc_param_db.conv_delay = value;
break;
case ADC_SET_RM:
adc_param_db.read_mode = value;
break;
case ADC_SET_RT:
adc_param_db.read_ts = value;
break;
case ADC_SET_S_CH:
adc_param_db.single_channel = value;
break;
case ADC_SET_WAIT_TS:
adc_param_db.wait_tsi = value;
break;
case ADC_INIT_P:
mc13892_adc_init_param(&adc_param_db);
break;
case ADC_START:
mc13892_adc_convert(&adc_param_db);
break;
case ADC_TS:
pmic_adc_get_touch_sample(&ts, 1);
pr_debug("x = %d\n", ts.x_position);
pr_debug("y = %d\n", ts.y_position);
pr_debug("p = %d\n", ts.contact_resistance);
break;
case ADC_TS_READ:
pmic_adc_get_touch_sample(&ts, 0);
pr_debug("x = %d\n", ts.x_position);
pr_debug("y = %d\n", ts.y_position);
pr_debug("p = %d\n", ts.contact_resistance);
break;
case ADC_TS_CAL:
break;
default:
pr_debug("error command\n");
break;
}
return 0;
}
static ssize_t adc_ctl(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
int state = 0;
const char *const *s;
char *p, *q;
int error;
int len, value = 0;
pr_debug("adc_ctl\n");
q = NULL;
q = memchr(buf, ' ', count);
if (q != NULL) {
len = q - buf;
q += 1;
value = simple_strtoul(q, NULL, 10);
} else {
p = memchr(buf, '\n', count);
len = p ? p - buf : count;
}
for (s = &adc_cmd[state]; state < ADC_CMD_MAX; s++, state++) {
if (*s && !strncmp(buf, *s, len))
break;
}
if (state < ADC_CMD_MAX && *s)
error = cmd(state, value);
else
error = -EINVAL;
return count;
}
#else
static ssize_t adc_info(struct device *dev, struct device_attribute *attr,
char *buf)
{
return 0;
}
static ssize_t adc_ctl(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
return count;
}
#endif
/*!
* This function triggers a conversion and returns sampling results of each
* specified channel.
*
* @param channels This input parameter is bitmap to specify channels
* to be sampled.
* @param result The pointer to array to store sampling results.
* The memory should be allocated by the caller of this
* function.
*
* @return This function returns PMIC_SUCCESS if successful.
*/
PMIC_STATUS pmic_adc_convert_multichnnel(t_channel channels,
unsigned short *result)
{
t_adc_param adc_param;
int i;
PMIC_STATUS ret;
if (suspend_flag == 1) {
return -EBUSY;
}
mc13892_adc_init_param(&adc_param);
pr_debug("pmic_adc_convert_multichnnel\n");
channels = channel_num[channels];
if (channels == -1) {
pr_debug("Wrong channel ID\n");
return PMIC_PARAMETER_ERROR;
}
adc_param.read_ts = false;
adc_param.single_channel = false;
if ((channels >= 0) && (channels <= 7)) {
adc_param.channel_0 = channels;
adc_param.channel_1 = ((channels + 4) % 4) + 4;
} else {
return PMIC_PARAMETER_ERROR;
}
adc_param.read_mode = 0x00003f;
adc_param.read_ts = false;
ret = mc13892_adc_convert(&adc_param);
for (i = 0; i <= 7; i++) {
result[i] = adc_param.value[i];
}
return ret;
}
/*!
* This function starts a Battery Current mode conversion.
*
* @param mode Conversion mode.
* @param result Battery Current measurement result.
* if \a mode = ADC_8CHAN_1X, the result is \n
* result[0] = (BATTP - BATT_I) \n
* if \a mode = ADC_1CHAN_8X, the result is \n
* result[0] = BATTP \n
* result[1] = BATT_I \n
* result[2] = BATTP \n
* result[3] = BATT_I \n
* result[4] = BATTP \n
* result[5] = BATT_I \n
* result[6] = BATTP \n
* result[7] = BATT_I
*
* @return This function returns PMIC_SUCCESS if successful.
*/
PMIC_STATUS pmic_adc_get_battery_current(t_conversion_mode mode,
unsigned short *result)
{
PMIC_STATUS ret;
t_channel channel;
if (suspend_flag == 1) {
return -EBUSY;
}
channel = BATTERY_CURRENT;
if (mode == ADC_8CHAN_1X) {
ret = pmic_adc_convert(channel, result);
} else {
ret = pmic_adc_convert_8x(channel, result);
}
return ret;
}
/*!
* This function implements the open method on a MC13892 ADC device.
*
* @param inode pointer on the node
* @param file pointer on the file
* @return This function returns 0.
*/
static int pmic_adc_open(struct inode *inode, struct file *file)
{
while (suspend_flag == 1) {
swait++;
/* Block if the device is suspended */
if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
return -ERESTARTSYS;
}
}
pr_debug("mc13892_adc : mc13892_adc_open()\n");
return 0;
}
/*!
* This function implements the release method on a MC13892 ADC device.
*
* @param inode pointer on the node
* @param file pointer on the file
* @return This function returns 0.
*/
static int pmic_adc_free(struct inode *inode, struct file *file)
{
pr_debug("mc13892_adc : mc13892_adc_free()\n");
return 0;
}
/*!
* This function implements IOCTL controls on a MC13892 ADC device.
*
* @param inode pointer on the node
* @param file pointer on the file
* @param cmd the command
* @param arg the parameter
* @return This function returns 0 if successful.
*/
static int pmic_adc_ioctl(struct inode *inode, struct file *file,
unsigned int cmd, unsigned long arg)
{
t_adc_convert_param *convert_param;
t_touch_mode touch_mode;
t_touch_screen touch_sample;
unsigned short b_current;
if ((_IOC_TYPE(cmd) != 'p') && (_IOC_TYPE(cmd) != 'D'))
return -ENOTTY;
while (suspend_flag == 1) {
swait++;
/* Block if the device is suspended */
if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
return -ERESTARTSYS;
}
}
switch (cmd) {
case PMIC_ADC_INIT:
CHECK_ERROR(pmic_adc_init());
break;
case PMIC_ADC_DEINIT:
CHECK_ERROR(pmic_adc_deinit());
break;
case PMIC_ADC_CONVERT:
if ((convert_param = kmalloc(sizeof(t_adc_convert_param),
GFP_KERNEL)) == NULL) {
return -ENOMEM;
}
if (copy_from_user(convert_param, (t_adc_convert_param *) arg,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
CHECK_ERROR_KFREE(pmic_adc_convert(convert_param->channel,
convert_param->result),
(kfree(convert_param)));
if (copy_to_user((t_adc_convert_param *) arg, convert_param,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
kfree(convert_param);
break;
case PMIC_ADC_CONVERT_8X:
if ((convert_param = kmalloc(sizeof(t_adc_convert_param),
GFP_KERNEL)) == NULL) {
return -ENOMEM;
}
if (copy_from_user(convert_param, (t_adc_convert_param *) arg,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
CHECK_ERROR_KFREE(pmic_adc_convert_8x(convert_param->channel,
convert_param->result),
(kfree(convert_param)));
if (copy_to_user((t_adc_convert_param *) arg, convert_param,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
kfree(convert_param);
break;
case PMIC_ADC_CONVERT_MULTICHANNEL:
if ((convert_param = kmalloc(sizeof(t_adc_convert_param),
GFP_KERNEL)) == NULL) {
return -ENOMEM;
}
if (copy_from_user(convert_param, (t_adc_convert_param *) arg,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
CHECK_ERROR_KFREE(pmic_adc_convert_multichnnel
(convert_param->channel,
convert_param->result),
(kfree(convert_param)));
if (copy_to_user((t_adc_convert_param *) arg, convert_param,
sizeof(t_adc_convert_param))) {
kfree(convert_param);
return -EFAULT;
}
kfree(convert_param);
break;
case PMIC_ADC_SET_TOUCH_MODE:
CHECK_ERROR(pmic_adc_set_touch_mode((t_touch_mode) arg));
break;
case PMIC_ADC_GET_TOUCH_MODE:
CHECK_ERROR(pmic_adc_get_touch_mode(&touch_mode));
if (copy_to_user((t_touch_mode *) arg, &touch_mode,
sizeof(t_touch_mode))) {
return -EFAULT;
}
break;
case PMIC_ADC_GET_TOUCH_SAMPLE:
CHECK_ERROR(pmic_adc_get_touch_sample(&touch_sample, 1));
if (copy_to_user((t_touch_screen *) arg, &touch_sample,
sizeof(t_touch_screen))) {
return -EFAULT;
}
break;
case PMIC_ADC_GET_BATTERY_CURRENT:
CHECK_ERROR(pmic_adc_get_battery_current(ADC_8CHAN_1X,
&b_current));
if (copy_to_user((unsigned short *)arg, &b_current,
sizeof(unsigned short))) {
return -EFAULT;
}
break;
default:
pr_debug("pmic_adc_ioctl: unsupported ioctl command 0x%x\n",
cmd);
return -EINVAL;
}
return 0;
}
static struct file_operations mc13892_adc_fops = {
.owner = THIS_MODULE,
.ioctl = pmic_adc_ioctl,
.open = pmic_adc_open,
.release = pmic_adc_free,
};
static struct cdev pmic_adc_cdev;
static DEVICE_ATTR(adc, 0644, adc_info, adc_ctl);
static int pmic_adc_module_probe(struct platform_device *pdev)
{
int ret = 0;
struct device * sdev;
dev_t devid;
pr_debug("PMIC ADC start probe\n");
if( (ret = alloc_chrdev_region(&devid, 0, 8, "pmic_adc")) < 0 ) {
pr_debug(KERN_ERR "Unable to allocate device range for pmic_adc\n");
return ret;
}
pmic_adc_major = MAJOR(devid);
if (pmic_adc_major < 0) {
pr_debug(KERN_ERR "Unable to get a major for pmic_adc\n");
ret = pmic_adc_major;
goto unreg_char;
}
cdev_init(&pmic_adc_cdev, &mc13892_adc_fops);
ret =cdev_add(&pmic_adc_cdev, devid, 8);
if (ret < 0) {
pr_err("pmic_adc: cannot add character device\n");
goto unreg_char;
}
pmic_adc_class = class_create(THIS_MODULE, "pmic_adc");
if (IS_ERR(pmic_adc_class)) {
pr_debug(KERN_ERR "Error creating pmic_adc class.\n");
ret = PTR_ERR(pmic_adc_class);
goto unreg_char;
}
sdev = device_create(pmic_adc_class, NULL, devid, NULL, "pmic_adc");
if (IS_ERR(sdev) ) {
pr_debug(KERN_ERR "Error creating pmic_adc class device.\n");
ret = PTR_ERR(sdev);
goto cl_destroy;
}
ret = device_create_file(&(pdev->dev), &dev_attr_adc);
if (ret) {
pr_debug("Can't create device file!\n");
ret = -ENODEV;
goto dev_destroy;
}
init_waitqueue_head(&suspendq);
ret = pmic_adc_init();
if (ret != PMIC_SUCCESS) {
pr_debug("Error in pmic_adc_init.\n");
goto rm_dev_file;
}
pmic_adc_ready = 1;
printk(KERN_DEBUG"PMIC ADC successfully probed\n");
return 0;
rm_dev_file:
device_remove_file(&(pdev->dev), &dev_attr_adc);
dev_destroy:
device_destroy(pmic_adc_class, MKDEV(pmic_adc_major, 0));
cl_destroy:
class_destroy(pmic_adc_class);
unreg_char:
unregister_chrdev(pmic_adc_major, "pmic_adc");
return ret;
}
static int pmic_adc_module_remove(struct platform_device *pdev)
{
pmic_adc_deinit();
pmic_adc_ready = 0;
pr_debug("PMIC ADC successfully removed\n");
return 0;
}
static void pmic_adc_module_shutdown(struct platform_device *pdev)
{
/**
* Stop the ADC by calling the deinit function. Without this
* call, we have problems to 'shutdown' the system through a
* shell reboot call when the touch screen is enabled.
* The the system doesnt reboot.
*/
pmic_adc_deinit();
pmic_adc_ready = 0;
}
static struct platform_driver pmic_adc_driver_ldm = {
.driver = {
.name = "pmic_adc",
},
.suspend = pmic_adc_suspend,
.resume = pmic_adc_resume,
.probe = pmic_adc_module_probe,
.remove = pmic_adc_module_remove,
.shutdown = pmic_adc_module_shutdown,
};
static int __init pmic_adc_module_init(void)
{
pr_debug("PMIC ADC driver loading...\n");
return platform_driver_register(&pmic_adc_driver_ldm);
}
static void __exit pmic_adc_module_exit(void)
{
platform_driver_unregister(&pmic_adc_driver_ldm);
pr_debug("PMIC ADC driver successfully unloaded\n");
}
module_init(pmic_adc_module_init);
module_exit(pmic_adc_module_exit);
MODULE_DESCRIPTION("PMIC ADC device driver");
MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_LICENSE("GPL");
|