summaryrefslogtreecommitdiffstats
path: root/target/linux/danube/files/drivers/char/danube_led.c
blob: 531c7ed0c1a72f58c15ba993f630a4f23b7bcf77 (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
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
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
/*
 *   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., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 *
 *   Copyright (C) 2006 infineon
 *   Copyright (C) 2007 John Crispin <blogic@openwrt.org> 
 *
 */

#include <linux/config.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/init.h>
#include <asm/uaccess.h>
#include <asm/unistd.h>
#include <linux/errno.h>

/*
 *  Chip Specific Head File
 */
#include <asm/danube/port.h>

#include <asm/danube/danube_led.h>
#include <asm/danube/danube_gptu.h>


/*
 * ####################################
 *              Definition
 * ####################################
 */

#define DEBUG_ON_AMAZON                 0

#define DATA_CLOCKING_EDGE              FALLING_EDGE

#define BOARD_TYPE                      REFERENCE_BOARD

#define DEBUG_WRITE_REGISTER            0

#define RISING_EDGE                     0
#define FALLING_EDGE                    1

#define EVALUATION_BOARD                0
#define REFERENCE_BOARD                 1

/*
 *  GPIO Driver Function Wrapping
 */
#define port_reserve_pin                danube_port_reserve_pin
#define port_free_pin                   danube_port_free_pin
#define port_set_altsel0                danube_port_set_altsel0
#define port_clear_altsel0              danube_port_clear_altsel0
#define port_set_altsel1                danube_port_set_altsel1
#define port_clear_altsel1              danube_port_clear_altsel1
#define port_set_dir_out                danube_port_set_dir_out
#define port_clear_dir_out              danube_port_clear_dir_out
#define port_set_open_drain             danube_port_set_open_drain
#define port_clear_open_drain           danube_port_clear_open_drain

/*
 *  GPIO Port Used By LED
 */
#define LED_SH_PORT                     0
#define LED_SH_PIN                      4
#define LED_SH_DIR                      1
#define LED_SH_ALTSEL0                  1
#define LED_SH_ALTSEL1                  0
#define LED_SH_OPENDRAIN                1
#define LED_D_PORT                      0
#define LED_D_PIN                       5
#define LED_D_DIR                       1
#define LED_D_ALTSEL0                   1
#define LED_D_ALTSEL1                   0
#define LED_D_OPENDRAIN                 1
#define LED_ST_PORT                     0
#define LED_ST_PIN                      6
#define LED_ST_DIR                      1
#define LED_ST_ALTSEL0                  1
#define LED_ST_ALTSEL1                  0
#define LED_ST_OPENDRAIN                1

#define LED_ADSL0_PORT                  0
#define LED_ADSL0_PIN                   4
#define LED_ADSL0_DIR                   1
#define LED_ADSL0_ALTSEL0               0
#define LED_ADSL0_ALTSEL1               1
#define LED_ADSL0_OPENDRAIN             1
#define LED_ADSL1_PORT                  0
#define LED_ADSL1_PIN                   5
#define LED_ADSL1_DIR                   1
#define LED_ADSL1_ALTSEL0               1
#define LED_ADSL1_ALTSEL1               1
#define LED_ADSL1_OPENDRAIN             1

#if (LED_SH_PORT == LED_ADSL0_PORT && LED_SH_PIN == LED_ADSL0_PIN)      \
    || (LED_D_PORT == LED_ADSL0_PORT && LED_D_PIN == LED_ADSL0_PIN)     \
    || (LED_ST_PORT == LED_ADSL0_PORT && LED_ST_PIN == LED_ADSL0_PIN)   \
    || (LED_SH_PORT == LED_ADSL1_PORT && LED_SH_PIN == LED_ADSL1_PIN)   \
    || (LED_D_PORT == LED_ADSL1_PORT && LED_D_PIN == LED_ADSL1_PIN)     \
    || (LED_ST_PORT == LED_ADSL1_PORT && LED_ST_PIN == LED_ADSL1_PIN)
  #define ADSL_LED_IS_EXCLUSIVE         1
#else
  #define ADSL_LED_IS_EXCLUSIVE         0
#endif

/*
 *  Define GPIO Functions
 */
#if LED_SH_DIR
  #define LED_SH_DIR_SETUP              port_set_dir_out
#else
  #define LED_SH_DIR_SETUP              port_clear_dir_out
#endif
#if LED_SH_ALTSEL0
  #define LED_SH_ALTSEL0_SETUP          port_set_altsel0
#else
  #define LED_SH_ALTSEL0_SETUP          port_clear_altsel0
#endif
#if LED_SH_ALTSEL1
  #define LED_SH_ALTSEL1_SETUP          port_set_altsel1
#else
  #define LED_SH_ALTSEL1_SETUP          port_clear_altsel1
#endif
#if LED_SH_OPENDRAIN
  #define LED_SH_OPENDRAIN_SETUP        port_set_open_drain
#else
  #define LED_SH_OPENDRAIN_SETUP        port_clear_open_drain
#endif

#if LED_D_DIR
  #define LED_D_DIR_SETUP               port_set_dir_out
#else
  #define LED_D_DIR_SETUP               port_clear_dir_out
#endif
#if LED_D_ALTSEL0
  #define LED_D_ALTSEL0_SETUP           port_set_altsel0
#else
  #define LED_D_ALTSEL0_SETUP           port_clear_altsel0
#endif
#if LED_D_ALTSEL1
  #define LED_D_ALTSEL1_SETUP           port_set_altsel1
#else
  #define LED_D_ALTSEL1_SETUP           port_clear_altsel1
#endif
#if LED_D_OPENDRAIN
  #define LED_D_OPENDRAIN_SETUP         port_set_open_drain
#else
  #define LED_D_OPENDRAIN_SETUP         port_clear_open_drain
#endif

#if LED_ST_DIR
  #define LED_ST_DIR_SETUP              port_set_dir_out
#else
  #define LED_ST_DIR_SETUP              port_clear_dir_out
#endif
#if LED_ST_ALTSEL0
  #define LED_ST_ALTSEL0_SETUP          port_set_altsel0
#else
  #define LED_ST_ALTSEL0_SETUP          port_clear_altsel0
#endif
#if LED_ST_ALTSEL1
  #define LED_ST_ALTSEL1_SETUP          port_set_altsel1
#else
  #define LED_ST_ALTSEL1_SETUP          port_clear_altsel1
#endif
#if LED_ST_OPENDRAIN
  #define LED_ST_OPENDRAIN_SETUP        port_set_open_drain
#else
  #define LED_ST_OPENDRAIN_SETUP        port_clear_open_drain
#endif

#if LED_ADSL0_DIR
  #define LED_ADSL0_DIR_SETUP           port_set_dir_out
#else
  #define LED_ADSL0_DIR_SETUP           port_clear_dir_out
#endif
#if LED_ADSL0_ALTSEL0
  #define LED_ADSL0_ALTSEL0_SETUP       port_set_altsel0
#else
  #define LED_ADSL0_ALTSEL0_SETUP       port_clear_altsel0
#endif
#if LED_ADSL0_ALTSEL1
  #define LED_ADSL0_ALTSEL1_SETUP       port_set_altsel1
#else
  #define LED_ADSL0_ALTSEL1_SETUP       port_clear_altsel1
#endif
#if LED_ADSL0_OPENDRAIN
  #define LED_ADSL0_OPENDRAIN_SETUP     port_set_open_drain
#else
  #define LED_ADSL0_OPENDRAIN_SETUP     port_clear_open_drain
#endif

#if LED_ADSL1_DIR
  #define LED_ADSL1_DIR_SETUP           port_set_dir_out
#else
  #define LED_ADSL1_DIR_SETUP           port_clear_dir_out
#endif
#if LED_ADSL1_ALTSEL0
  #define LED_ADSL1_ALTSEL0_SETUP       port_set_altsel0
#else
  #define LED_ADSL1_ALTSEL0_SETUP       port_clear_altsel0
#endif
#if LED_ADSL1_ALTSEL1
  #define LED_ADSL1_ALTSEL1_SETUP       port_set_altsel1
#else
  #define LED_ADSL1_ALTSEL1_SETUP       port_clear_altsel1
#endif
#if LED_ADSL1_OPENDRAIN
  #define LED_ADSL1_OPENDRAIN_SETUP     port_set_open_drain
#else
  #define LED_ADSL1_OPENDRAIN_SETUP     port_clear_open_drain
#endif

/*
 *  LED Device Minor Number
 */
#if !defined(LED_MINOR)
    #define LED_MINOR                   151 //  This number is written in Linux kernel document "devices.txt"
#endif  //  !defined(LED_MINOR)

/*
 *  Bits Operation
 */
#define GET_BITS(x, msb, lsb)           (((x) & ((1 << ((msb) + 1)) - 1)) >> (lsb))
#define SET_BITS(x, msb, lsb, value)    (((x) & ~(((1 << ((msb) + 1)) - 1) ^ ((1 << (lsb)) - 1))) | (((value) & ((1 << (1 + (msb) - (lsb))) - 1)) << (lsb)))

/*
 *  LED Registers Mapping
 */
#define DANUBE_LED                      (KSEG1 + 0x1E100BB0)
#define DANUBE_LED_CON0                 ((volatile u32*)(DANUBE_LED + 0x0000))
#define DANUBE_LED_CON1                 ((volatile u32*)(DANUBE_LED + 0x0004))
#define DANUBE_LED_CPU0                 ((volatile u32*)(DANUBE_LED + 0x0008))
#define DANUBE_LED_CPU1                 ((volatile u32*)(DANUBE_LED + 0x000C))
#define DANUBE_LED_AR                   ((volatile u32*)(DANUBE_LED + 0x0010))

/*
 *  LED Control 0 Register
 */
#define LED_CON0_SWU                    (*DANUBE_LED_CON0 & (1 << 31))
#define LED_CON0_FALLING_EDGE           (*DANUBE_LED_CON0 & (1 << 26))
#define LED_CON0_AD1                    (*DANUBE_LED_CON0 & (1 << 25))
#define LED_CON0_AD0                    (*DANUBE_LED_CON0 & (1 << 24))
#define LED_CON0_LBn(n)                 (*DANUBE_LED_CON0 & (1 << n))
#define LED_CON0_DEFAULT_VALUE          (0x80000000 | (DATA_CLOCKING_EDGE << 26))

/*
 *  LED Control 1 Register
 */
#define LED_CON1_US                     (*DANUBE_LED_CON1 >> 30)
#define LED_CON1_SCS                    (*DANUBE_LED_CON1 & (1 << 28))
#define LED_CON1_FPID                   GET_BITS(*DANUBE_LED_CON1, 27, 23)
#define LED_CON1_FPIS                   GET_BITS(*DANUBE_LED_CON1, 21, 20)
#define LED_CON1_DO                     GET_BITS(*DANUBE_LED_CON1, 19, 18)
#define LED_CON1_G2                     (*DANUBE_LED_CON1 & (1 << 2))
#define LED_CON1_G1                     (*DANUBE_LED_CON1 & (1 << 1))
#define LED_CON1_G0                     (*DANUBE_LED_CON1 & 0x01)
#define LED_CON1_G                      (*DANUBE_LED_CON1 & 0x07)
#define LED_CON1_DEFAULT_VALUE          0x00000000

/*
 *  LED Data Output CPU 0 Register
 */
#define LED_CPU0_Ln(n)                  (*DANUBE_LED_CPU0 & (1 << n))
#define LED_LED_CPU0_DEFAULT_VALUE      0x00000000

/*
 *  LED Data Output CPU 1 Register
 */
#define LED_CPU1_Ln(n)                  (*DANUBE_LED_CPU1 & (1 << n))
#define LED_LED_CPU1_DEFAULT_VALUE      0x00000000

/*
 *  LED Data Output Access Rights Register
 */
#define LED_AR_Ln(n)                    (*DANUBE_LED_AR & (1 << n))
#define LED_AR_DEFAULT_VALUE            0x00000000


/*
 * ####################################
 * Preparation of Debug on Amazon Chip
 * ####################################
 */

/*
 *  If try module on Amazon chip, prepare some tricks to prevent invalid memory write.
 */
#if defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
    char g_pFakeRegisters[0x50];

    #undef  DEBUG_WRITE_REGISTER

    #undef  DANUBE_LED
    #define DANUBE_LED                  g_pFakeRegisters

    #undef  port_reserve_pin
    #undef  port_free_pin
    #undef  port_set_altsel0
    #undef  port_clear_altsel0
    #undef  port_set_altsel1
    #undef  port_clear_altsel1
    #undef  port_set_dir_out

    #define port_reserve_pin            amazon_port_reserve_pin
    #define port_free_pin               amazon_port_free_pin
    #define port_set_altsel0            amazon_port_set_altsel0
    #define port_clear_altsel0          amazon_port_clear_altsel0
    #define port_set_altsel1            amazon_port_set_altsel1
    #define port_clear_altsel1          amazon_port_clear_altsel1
    #define port_set_dir_out            amazon_port_set_dir_out
#endif  //  defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON


/*
 * ####################################
 *             Declaration
 * ####################################
 */

/*
 *  File Operations
 */
static int led_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
static int led_open(struct inode *, struct file *);
static int led_release(struct inode *, struct file *);

/*
 *  Software Update LED
 */
static inline int update_led(void);

/*
 *  LED Configuration Functions
 */
static inline u32 set_update_source(u32, unsigned long, unsigned long);
static inline u32 set_blink_in_batch(u32, unsigned long, unsigned long);
static inline u32 set_data_clock_edge(u32, unsigned long);
static inline u32 set_update_clock(u32, unsigned long, unsigned long);
static inline u32 set_store_mode(u32, unsigned long);
static inline u32 set_shift_clock(u32, unsigned long);
static inline u32 set_data_offset(u32, unsigned long);
static inline u32 set_number_of_enabled_led(u32, unsigned long);
static inline u32 set_data_in_batch(u32, unsigned long, unsigned long);
static inline u32 set_access_right(u32, unsigned long, unsigned long);

/*
 *  PMU Operation
 */
static inline void enable_led(void);
static inline void disable_led(void);

/*
 *  GPIO Setup & Release
 */
static inline int setup_gpio_port(unsigned long);
static inline void release_gpio_port(unsigned long);

/*
 *  GPT Setup & Release
 */
static inline int setup_gpt(int, unsigned long);
static inline void release_gpt(int);

/*
 *  Turn On/Off LED
 */
static inline int turn_on_led(unsigned long);
static inline void turn_off_led(unsigned long);


/*
 * ####################################
 *            Local Variable
 * ####################################
 */

static struct semaphore led_sem;

static struct file_operations led_fops = {
    owner:      THIS_MODULE,
    ioctl:      led_ioctl,
    open:       led_open,
    release:    led_release
};

static struct miscdevice led_miscdev = {
    LED_MINOR,
    "led",
    &led_fops,
    NULL,
    NULL,
    NULL
};

static unsigned long gpt_on = 0;
static unsigned long gpt_freq = 0;

static unsigned long adsl_on = 0;
static unsigned long f_led_on = 0;

static int module_id;


/*
 * ####################################
 *           Global Variable
 * ####################################
 */


/*
 * ####################################
 *            Local Function
 * ####################################
 */

static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
{
    int ret = -EINVAL;
    struct led_config_param param;

    switch ( cmd )
    {
    case LED_CONFIG:
        copy_from_user(&param, (char*)arg, sizeof(param));
        ret = danube_led_config(&param);
        break;
    }

    return ret;
}

static int led_open(struct inode *inode, struct file *file)
{
    return 0;
}

static int led_release(struct inode *inode, struct file *file)
{
    return 0;
}

/*
 *  Description:
 *    Update LEDs with data stored in register.
 *  Input:
 *    none
 *  Output:
 *    int --- 0:    Success
 *            else: Error Code
 */
static inline int update_led(void)
{
    int i, j;

    /*
     *  GPT2 or FPID is the clock to update LEDs automatically.
     */
    if ( LED_CON1_US != 0 )
        return 0;

    /*
     *  Check the status to prevent conflict of two consecutive update
     */
    for ( i = 100000; i != 0; i -= j / 16 )
    {
        down(&led_sem);
        if ( !LED_CON0_SWU )
        {
            *DANUBE_LED_CON0 |= 1 << 31;
            up(&led_sem);
            return 0;
        }
        else
            up(&led_sem);
        for ( j = 0; j < 1000 * 16; j++ );
    }

    return -EBUSY;
}

/*
 *  Description:
 *    Select update source for LED bit 0 and bit 1.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    led    --- unsigned long, bit 0 stands for LED 0, and bit 1 stands for
 *               LED 1. If the bit is set, the source value is valid, else
 *               the source value is invalid.
 *    source --- unsigned long, bit 0 stands for LED 0, and bit 1 stands for
 *               LED 1. If the corresponding is cleared, LED is updated with
 *               value in data register, else LED is updated with ARC module.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_update_source(u32 reg, unsigned long led, unsigned long source)
{
    return (reg & ~((led & 0x03) << 24)) | ((source & 0x03) << 24);
}

/*
 *  Description:
 *    Define which of the LEDs should change their value based on the US pulse.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    mask   --- unsigned long, if the corresponding bit is set, the blink value
 *               is valid, else the blink value is invalid.
 *    blink  --- unsigned long, if the corresponding bit is set, the LED should
 *               change its value based on the US pulse.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_blink_in_batch(u32 reg, unsigned long mask, unsigned long blink)
{
    return (reg & (~(mask & 0x00FFFFFF) & 0x87FFFFFF)) | (blink & 0x00FFFFFF);
}

static inline u32 set_data_clock_edge(u32 reg, unsigned long f_on_rising_edge)
{
    return f_on_rising_edge ? (reg & ~(1 << 26)) : (reg | (1 << 26));
}

/*
 *  Description:
 *    Select the clock source for US pulse.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    clock  --- unsigned long, there 3 available values:
 *               0x00 - use software update bit (SWU) as source.
 *               0x01 - use GPT2 as clock source.
 *               0x02 - use FPI as clock source.
 *    fpid   --- unsigned long, if FPI is selected as clock source, this field
 *               specify the divider. Please refer to specification for detail
 *               description.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_update_clock(u32 reg, unsigned long clock, unsigned long fpid)
{
    switch ( clock )
    {
    case 0: reg &= ~0xC0000000; break;
    case 1: reg = (reg & ~0xC0000000) | 0x40000000; break;
    case 2: reg = (reg & ~0xCF800000) | 0x80000000 | ((fpid & 0x1F) << 23); break;
    }
    return reg;
}

/*
 *  Description:
 *    Set the behavior of the LED_ST (shift register) signal.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    mode   --- unsigned long, there 2 available values:
 *               zero     - LED controller generate single pulse.
 *               non-zero - LED controller generate inverted shift clock.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_store_mode(u32 reg, unsigned long mode)
{
    return mode ? (reg | (1 << 28)) : (reg & ~(1 << 28));
}

/*
 *  Description:
 *    Select the clock source for shift clock LED_SH.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    fpis   --- unsigned long, if FPI is selected as clock source, this field
 *               specify the divider. Please refer to specification for detail
 *               description.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_shift_clock(u32 reg, unsigned long fpis)
{
    return SET_BITS(reg, 21, 20, fpis);
}

/*
 *  Description:
 *    Set the clock cycle offset before data is transmitted to LED_D pin.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    offset --- unsigned long, the number of clock cycles would be inserted
 *               before data is transmitted to LED_D pin. Zero means no cycle
 *               inserted.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_data_offset(u32 reg, unsigned long offset)
{
    return SET_BITS(reg, 19, 18, offset);
}

/*
 *  Description:
 *    Enable or disable LEDs.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    number --- unsigned long, the number of LED to be enabled. This field
 *               could 0, 8, 16 or 24. Zero means disable all LEDs.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_number_of_enabled_led(u32 reg, unsigned long number)
{
    u32 bit_mask;

    bit_mask = number > 16 ? 0x07 : (number > 8 ? 0x03 : (number ? 0x01 : 0x00));
    return (reg & ~0x07) | bit_mask;
}

/*
 *  Description:
 *    Turn on/off LEDs.
 *  Input:
 *    reg    --- u32, the original register value going to be modified.
 *    mask   --- unsigned long, if the corresponding bit is set, the data value
 *               is valid, else the data value is invalid.
 *    data   --- unsigned long, if the corresponding bit is set, the LED should
 *               be on, else be off.
 *  Output:
 *    u32    --- The updated register value.
 */
static inline u32 set_data_in_batch(u32 reg, unsigned long mask, unsigned long data)
{
    return (reg & ~(mask & 0x00FFFFFF)) | (data & 0x00FFFFFF);
}

static inline u32 set_access_right(u32 reg, unsigned long mask, unsigned long ar)
{
    return (reg & ~(mask & 0x00FFFFFF)) | (~ar & mask);
}

/*
 *  Description:
 *    Enable LED control module.
 *  Input:
 *    none
 *  Output:
 *    none
 */
static inline void enable_led(void)
{
#if !defined(DEBUG_ON_AMAZON) || !DEBUG_ON_AMAZON
    /*  Activate LED module in PMU. */
    int i = 1000000;

    *(unsigned long *)0xBF10201C &= ~(1 << 11);
    while ( --i && (*(unsigned long *)0xBF102020 & (1 << 11)) );
    if ( !i )
        panic("Activating LED in PMU failed!");
#endif
}

/*
 *  Description:
 *    Disable LED control module.
 *  Input:
 *    none
 *  Output:
 *    none
 */
static inline void disable_led(void)
{
#if !defined(DEBUG_ON_AMAZON) || !DEBUG_ON_AMAZON
    /*  Inactivating LED module in PMU.    */
    *(unsigned long *)0xBF10201C |= 1 << 11;
#endif
}

/*
 *  Description:
 *    If LEDs are enabled, GPIO must be setup to enable LED pins.
 *  Input:
 *    none
 *  Output:
 *    int --- 0:    Success
 *            else: Error Code
 */
static inline int setup_gpio_port(unsigned long adsl)
{
#if !defined(DEBUG_ON_AMAZON) || !DEBUG_ON_AMAZON
    int ret = 0;

  #if defined(DEBUG_WRITE_REGISTER) && DEBUG_WRITE_REGISTER
    if ( adsl )
    {
        *(unsigned long *)0xBE100B18 |=  0x30;
        *(unsigned long *)0xBE100B1C |=  0x20;
        *(unsigned long *)0xBE100B1C &= ~0x10;
        *(unsigned long *)0xBE100B20 |=  0x30;
        *(unsigned long *)0xBE100B24 |=  0x30;
    }
    else
    {
        *(unsigned long *)0xBE100B18 |=  0x70;
        *(unsigned long *)0xBE100B1C |=  0x70;
        *(unsigned long *)0xBE100B20 &= ~0x70;
        *(unsigned long *)0xBE100B24 |=  0x70;
    }
  #else

    /*
     *  Reserve all pins before config them.
     */
    if ( adsl )
    {
        ret |= port_reserve_pin(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);
        ret |= port_reserve_pin(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
    }
    else
    {
        ret |= port_reserve_pin(LED_ST_PORT, LED_ST_PIN, module_id);
        ret |= port_reserve_pin(LED_D_PORT, LED_D_PIN, module_id);
        ret |= port_reserve_pin(LED_SH_PORT, LED_SH_PIN, module_id);
    }
    if ( ret )
    {
        release_gpio_port(adsl);
        return ret; //  Should be -EBUSY
    }

    if ( adsl )
    {
        LED_ADSL0_ALTSEL0_SETUP(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);
        LED_ADSL0_ALTSEL1_SETUP(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);
        LED_ADSL0_DIR_SETUP(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);
        LED_ADSL0_OPENDRAIN_SETUP(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);

        LED_ADSL1_ALTSEL0_SETUP(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
        LED_ADSL1_ALTSEL1_SETUP(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
        LED_ADSL1_DIR_SETUP(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
        LED_ADSL1_OPENDRAIN_SETUP(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
    }
    else
    {
        /*
         *  Set LED_ST
         *    I don't check the return value, because I'm sure the value is valid
         *    and the pins are reserved already.
         */
        LED_ST_ALTSEL0_SETUP(LED_ST_PORT, LED_ST_PIN, module_id);
        LED_ST_ALTSEL1_SETUP(LED_ST_PORT, LED_ST_PIN, module_id);
        LED_ST_DIR_SETUP(LED_ST_PORT, LED_ST_PIN, module_id);
        LED_ST_OPENDRAIN_SETUP(LED_ST_PORT, LED_ST_PIN, module_id);

        /*
         *  Set LED_D
         */
        LED_D_ALTSEL0_SETUP(LED_D_PORT, LED_D_PIN, module_id);
        LED_D_ALTSEL1_SETUP(LED_D_PORT, LED_D_PIN, module_id);
        LED_D_DIR_SETUP(LED_D_PORT, LED_D_PIN, module_id);
        LED_D_OPENDRAIN_SETUP(LED_D_PORT, LED_D_PIN, module_id);

        /*
         *  Set LED_SH
         */
        LED_SH_ALTSEL0_SETUP(LED_SH_PORT, LED_SH_PIN, module_id);
        LED_SH_ALTSEL1_SETUP(LED_SH_PORT, LED_SH_PIN, module_id);
        LED_SH_DIR_SETUP(LED_SH_PORT, LED_SH_PIN, module_id);
        LED_SH_OPENDRAIN_SETUP(LED_SH_PORT, LED_SH_PIN, module_id);
    }
  #endif
#endif

    return 0;
}

/*
 *  Description:
 *    If LEDs are all disabled, GPIO must be released so that other application
 *    could reuse it.
 *  Input:
 *    none
 *  Output:
 *    none
 */
static inline void release_gpio_port(unsigned long adsl)
{
#if !defined(DEBUG_ON_AMAZON) || !DEBUG_ON_AMAZON
  #if !defined(DEBUG_WRITE_REGISTER) || !DEBUG_WRITE_REGISTER
    if ( adsl )
    {
        port_free_pin(LED_ADSL0_PORT, LED_ADSL0_PIN, module_id);
        port_free_pin(LED_ADSL1_PORT, LED_ADSL1_PIN, module_id);
    }
    else
    {
        port_free_pin(LED_ST_PORT, LED_ST_PIN, module_id);
        port_free_pin(LED_D_PORT, LED_D_PIN, module_id);
        port_free_pin(LED_SH_PORT, LED_SH_PIN, module_id);
    }
  #endif
#endif
}

/*
 *  Description:
 *    If shifter or update select GPT as clock source, this function would be
 *    invoked to setup corresponding GPT module.
 *    Attention please, this function is not working since the GPTU driver is
 *    not ready.
 *  Input:
 *    timer  --- int, index of timer.
 *    freq   --- unsigned long, frequency of timer (0.001Hz). This value will be
 *               rounded off to nearest possible value.
 *  Output:
 *    int --- 0:    Success
 *            else: Error Code
 */
static inline int setup_gpt(int timer, unsigned long freq)
{
    int ret;

#if 0
    timer = TIMER(timer, 0);
#else
    timer = TIMER(timer, 1);    //  2B
#endif

#if 0
    ret  = set_timer(timer, freq, 1, 0, TIMER_FLAG_NO_HANDLE, 0, 0);
#else
    ret  = request_timer(timer,
                           TIMER_FLAG_SYNC
                         | TIMER_FLAG_16BIT
                         | TIMER_FLAG_INT_SRC
                         | TIMER_FLAG_CYCLIC | TIMER_FLAG_COUNTER | TIMER_FLAG_DOWN
                         | TIMER_FLAG_ANY_EDGE
                         | TIMER_FLAG_NO_HANDLE,
                         8000000 / freq,
                         0,
                         0);

#endif
//    printk("setup_gpt: timer = %d, freq = %d, return = %d\n", timer, freq, ret);
    if ( !ret )
    {
        ret = start_timer(timer, 0);
        if ( ret )
            free_timer(timer);
    }

    return ret;
}

/*
 *  Description:
 *    If shifter or update select other clock source, allocated GPT must be
 *    released so that other application can use it.
 *    Attention please, this function is not working since the GPTU driver is
 *    not ready.
 *  Input:
 *    none
 *  Output:
 *    none
 */
static inline void release_gpt(int timer)
{
#if 0
    timer = TIMER(timer, 0);
#else
    timer = TIMER(timer, 1);
#endif
    stop_timer(timer);
    free_timer(timer);
}

static inline int turn_on_led(unsigned long adsl)
{
    int ret;

    ret = setup_gpio_port(adsl);
    if ( ret )
        return ret;

    enable_led();

    return 0;
}

static inline void turn_off_led(unsigned long adsl)
{
    release_gpio_port(adsl);
    disable_led();
}


/*
 * ####################################
 *           Global Function
 * ####################################
 */

/*
 *  Description:
 *    Define which of the LEDs should change its value based on the US pulse.
 *  Input:
 *    led    --- unsigned int, index of the LED to be set.
 *    blink  --- unsigned int, zero means normal mode, and non-zero means blink
 *               mode.
 *  Output:
 *    int    --- 0:    Success
 *               else: Error Code
 */
int danube_led_set_blink(unsigned int led, unsigned int blink)
{
    u32 bit_mask;

    if ( led > 23 )
        return -EINVAL;

    bit_mask = 1 << led;
    down(&led_sem);
    if ( blink )
        *DANUBE_LED_CON0 |= bit_mask;
    else
        *DANUBE_LED_CON0 &= ~bit_mask;
    up(&led_sem);

    return (led == 0 && LED_CON0_AD0) || (led == 1 && LED_CON0_AD1) ? -EINVAL : 0;
}

/*
 *  Description:
 *    Turn on/off LED.
 *  Input:
 *    led    --- unsigned int, index of the LED to be set.
 *    data   --- unsigned int, zero means off, and non-zero means on.
 *  Output:
 *    int    --- 0:    Success
 *               else: Error Code
 */
int danube_led_set_data(unsigned int led, unsigned int data)
{
    unsigned long f_update;
    u32 bit_mask;

    if ( led > 23 )
        return -EINVAL;

    bit_mask = 1 << led;
    down(&led_sem);
    if ( data )
        *DANUBE_LED_CPU0 |= bit_mask;
    else
        *DANUBE_LED_CPU0 &= ~bit_mask;
    f_update = !(*DANUBE_LED_AR & bit_mask);
    up(&led_sem);

    return f_update ? update_led() : 0;
}

/*
 *  Description:
 *    Config LED controller.
 *  Input:
 *    param   --- struct led_config_param*, the members are listed below:
 *                  operation_mask         - Select operations to be performed
 *                  led                    - LED to change update source
 *                  source                 - Corresponding update source
 *                  blink_mask             - LEDs to set blink mode
 *                  blink                  - Set to blink mode or normal mode
 *                  update_clock           - Select the source of update clock
 *                  fpid                   - If FPI is the source of update clock, set the divider
 *                  store_mode             - Set clock mode or single pulse mode for store signal
 *                  fpis                   - If FPI is the source of shift clock, set the divider
 *                  data_offset            - Set cycles to be inserted before data is transmitted
 *                  number_of_enabled_led  - Total number of LED to be enabled
 *                  data_mask              - LEDs to set value
 *                  data                   - Corresponding value
 *                  mips0_access_mask      - LEDs to set access right
 *                  mips0_access;          - 1: the corresponding data is output from MIPS0, 0: MIPS1
 *                  f_data_clock_on_rising - 1: data clock on rising edge, 0: data clock on falling edge
 *  Output:
 *    int    --- 0:    Success
 *               else: Error Code
 */
int danube_led_config(struct led_config_param* param)
{
    int ret;
    u32 reg_con0, reg_con1, reg_cpu0, reg_ar;
    u32 clean_reg_con0, clean_reg_con1, clean_reg_cpu0, clean_reg_ar;
    u32 f_setup_gpt2;
    u32 f_software_update;
    u32 new_led_on, new_adsl_on;

    if ( !param )
        return -EINVAL;

    down(&led_sem);

    reg_con0 = *DANUBE_LED_CON0;
    reg_con1 = *DANUBE_LED_CON1;
    reg_cpu0 = *DANUBE_LED_CPU0;
    reg_ar   = *DANUBE_LED_AR;

    clean_reg_con0 = 1;
    clean_reg_con1 = 1;
    clean_reg_cpu0 = 1;
    clean_reg_ar   = 1;

    f_setup_gpt2 = 0;

    f_software_update = LED_CON0_SWU ? 0 : 1;

    new_led_on = f_led_on;
    new_adsl_on = adsl_on;

    /*  ADSL or LED */
    if ( (param->operation_mask & CONFIG_OPERATION_UPDATE_SOURCE) )
    {
        if ( param->led > 0x03 || param->source > 0x03 )
            goto INVALID_PARAM;
        clean_reg_con0 = 0;
        reg_con0 = set_update_source(reg_con0, param->led, param->source);
#if 0   //  ADSL0,1 is source for bit 0, 1 in shift register
        new_adsl_on = param->source;
#endif
    }

    /*  Blink   */
    if ( (param->operation_mask & CONFIG_OPERATION_BLINK) )
    {
        if ( (param->blink_mask & 0xFF000000) || (param->blink & 0xFF000000) )
            goto INVALID_PARAM;
        clean_reg_con0 = 0;
        reg_con0 = set_blink_in_batch(reg_con0, param->blink_mask, param->blink);
    }

    /*  Edge    */
    if ( (param->operation_mask & CONFIG_DATA_CLOCK_EDGE) )
    {
        clean_reg_con0 = 0;
        reg_con0 = set_data_clock_edge(reg_con0, param->f_data_clock_on_rising);
    }

    /*  Update Clock    */
    if ( (param->operation_mask & CONFIG_OPERATION_UPDATE_CLOCK) )
    {
        if ( param->update_clock > 0x02 || (param->update_clock == 0x02 && param->fpid > 0x3) )
            goto INVALID_PARAM;
        clean_reg_con1 = 0;
        f_software_update = param->update_clock == 0 ? 1 : 0;
        if ( param->update_clock == 0x01 )
            f_setup_gpt2 = 1;
        reg_con1 = set_update_clock(reg_con1, param->update_clock, param->fpid);
    }

    /*  Store Mode  */
    if ( (param->operation_mask & CONFIG_OPERATION_STORE_MODE) )
    {
        clean_reg_con1 = 0;
        reg_con1 = set_store_mode(reg_con1, param->store_mode);
    }

    /*  Shift Clock */
    if ( (param->operation_mask & CONFIG_OPERATION_SHIFT_CLOCK) )
    {
        if ( param->fpis > 0x03 )
            goto INVALID_PARAM;
        clean_reg_con1 = 0;
        reg_con1 = set_shift_clock(reg_con1, param->fpis);
    }

    /*  Data Offset */
    if ( (param->operation_mask & CONFIG_OPERATION_DATA_OFFSET) )
    {
        if ( param->data_offset > 0x03 )
            goto INVALID_PARAM;
        clean_reg_con1 = 0;
        reg_con1 = set_data_offset(reg_con1, param->data_offset);
    }

    /*  Number of LED   */
    if ( (param->operation_mask & CONFIG_OPERATION_NUMBER_OF_LED) )
    {
        if ( param->number_of_enabled_led > 0x24 )
            goto INVALID_PARAM;

        /*
         *  If there is at lease one LED enabled, the GPIO pin must be setup.
         */
        new_led_on = param->number_of_enabled_led ? 1 : 0;

        clean_reg_con1 = 0;
        reg_con1 = set_number_of_enabled_led(reg_con1, param->number_of_enabled_led);
    }

    /*  LED Data    */
    if ( (param->operation_mask & CONFIG_OPERATION_DATA) )
    {
        if ( (param->data_mask & 0xFF000000) || (param->data & 0xFF000000) )
            goto INVALID_PARAM;
        clean_reg_cpu0 = 0;
        reg_cpu0 = set_data_in_batch(reg_cpu0, param->data_mask, param->data);
        if ( f_software_update )
        {
            clean_reg_con0 = 0;
            reg_con0 |= 0x80000000;
        }
    }

    /*  Access Right    */
    if ( (param->operation_mask & CONFIG_OPERATION_MIPS0_ACCESS) )
    {
        if ( (param->mips0_access_mask & 0xFF000000) || (param->mips0_access & 0xFF000000) )
            goto INVALID_PARAM;
        clean_reg_ar = 0;
        reg_ar = set_access_right(reg_ar, param->mips0_access_mask, param->mips0_access);
    }

    /*  Setup GPT   */
    if ( f_setup_gpt2 && !new_adsl_on )     //  If ADSL led is on, GPT is disabled.
    {
        ret = 0;

        if ( gpt_on )
        {
            if ( gpt_freq != param->fpid )
            {
                release_gpt(2);
                gpt_on = 0;
                ret = setup_gpt(2, param->fpid);
            }
        }
        else
            ret = setup_gpt(2, param->fpid);

        if ( ret )
        {
#if 1
            printk("Setup GPT error!\n");
#endif
            goto SETUP_GPT_ERROR;
        }
        else
        {
#if 0
            printk("Setup GPT successfully!\n");
#endif
            gpt_on = 1;
        }
    }
    else
        if ( gpt_on )
        {
            release_gpt(2);
            gpt_on = 0;
        }

    /*  Turn on LED */
    if ( new_adsl_on )
        new_led_on = 1;
    if ( !new_led_on || adsl_on != new_adsl_on )
    {
        turn_off_led(adsl_on);
        f_led_on = 0;
        adsl_on = 0;
    }
    if ( !f_led_on && new_led_on )
    {
        ret = turn_on_led(new_adsl_on);
        if ( ret )
        {
#if 1
            printk("Setup GPIO error!\n");
#endif
            goto SETUP_GPIO_ERROR;
        }
        adsl_on = new_adsl_on;
        f_led_on = 1;
    }

#if 0
    if ( (reg_con0 & 0x80000000) )
        printk("software update\n");
#endif

    /*  Write Register  */
    if ( !f_led_on )
        enable_led();
    if ( !clean_reg_ar )
        *DANUBE_LED_AR   = reg_ar;
    if ( !clean_reg_cpu0 )
        *DANUBE_LED_CPU0 = reg_cpu0;
    if ( !clean_reg_con1 )
        *DANUBE_LED_CON1 = reg_con1;
    if ( !clean_reg_con0 )
        *DANUBE_LED_CON0 = reg_con0;
    if ( !f_led_on )
        disable_led();

#if defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
    *DANUBE_LED_CON0 &= 0x7FFFFFFF;
#endif

#if 0
  #if !defined(DEBUG_ON_AMAZON) || !DEBUG_ON_AMAZON
    printk("*0xBF10201C      = 0x%08lX\n", *(unsigned long *)0xBF10201C);
    printk("*0xBE100B18      = 0x%08lX\n", *(unsigned long *)0xBE100B18);
    printk("*0xBE100B1C      = 0x%08lX\n", *(unsigned long *)0xBE100B1C);
    printk("*0xBE100B20      = 0x%08lX\n", *(unsigned long *)0xBE100B20);
    printk("*0xBE100B24      = 0x%08lX\n", *(unsigned long *)0xBE100B24);
  #endif
    printk("*DANUBE_LED_CON0 = 0x%08X\n", *DANUBE_LED_CON0);
    printk("*DANUBE_LED_CON1 = 0x%08X\n", *DANUBE_LED_CON1);
    printk("*DANUBE_LED_CPU0 = 0x%08X\n", *DANUBE_LED_CPU0);
    printk("*DANUBE_LED_CPU1 = 0x%08X\n", *DANUBE_LED_CPU1);
    printk("*DANUBE_LED_AR   = 0x%08X\n", *DANUBE_LED_AR);
#endif

    up(&led_sem);
    return 0;

SETUP_GPIO_ERROR:
    release_gpt(2);
    gpt_on = 0;
SETUP_GPT_ERROR:
    up(&led_sem);
    return ret;

INVALID_PARAM:
    up(&led_sem);
    return -EINVAL;
}


/*
 * ####################################
 *           Init/Cleanup API
 * ####################################
 */

/*
 *  Description:
 *    register device
 *  Input:
 *    none
 *  Output:
 *    0    --- successful
 *    else --- failure, usually it is negative value of error code
 */
int __init danube_led_init(void)
{
    int ret;
    struct led_config_param param = {0};

    enable_led();

    /*
     *  Set default value to registers to turn off all LED light.
     */
    *DANUBE_LED_AR   = LED_AR_DEFAULT_VALUE;
    *DANUBE_LED_CPU0 = LED_LED_CPU0_DEFAULT_VALUE;
    *DANUBE_LED_CPU1 = LED_LED_CPU1_DEFAULT_VALUE;
    *DANUBE_LED_CON1 = LED_CON1_DEFAULT_VALUE;
    *DANUBE_LED_CON0 = LED_CON0_DEFAULT_VALUE;

#if defined(DEBUG_ON_AMAZON) && DEBUG_ON_AMAZON
    *DANUBE_LED_CON0 &= 0x7FFFFFFF;
#endif

    disable_led();

    sema_init(&led_sem, 0);

    ret = misc_register(&led_miscdev);
    if ( ret == -EBUSY )
    {
        led_miscdev.minor = MISC_DYNAMIC_MINOR;
        ret = misc_register(&led_miscdev);
    }
    if ( ret )
    {
        printk(KERN_ERR "led: can't misc_register\n");
        return ret;
    }
    else
        printk(KERN_INFO "led: misc_register on minor = %d\n", led_miscdev.minor);

    module_id = THIS_MODULE ? (int)THIS_MODULE : ((MISC_MAJOR << 8) | led_miscdev.minor);

    up(&led_sem);

#if BOARD_TYPE == REFERENCE_BOARD
    /*  Add to enable hardware relay    */
        /*  Map for LED on reference board
              WLAN_READ     LED11   OUT1    15
              WARNING       LED12   OUT2    14
              FXS1_LINK     LED13   OUT3    13
              FXS2_LINK     LED14   OUT4    12
              FXO_ACT       LED15   OUT5    11
              USB_LINK      LED16   OUT6    10
              ADSL2_LINK    LED19   OUT7    9
              BT_LINK       LED17   OUT8    8
              SD_LINK       LED20   OUT9    7
              ADSL2_TRAFFIC LED31   OUT16   0
            Map for hardware relay on reference board
              USB Power On          OUT11   5
              RELAY                 OUT12   4
        */
    param.operation_mask = CONFIG_OPERATION_NUMBER_OF_LED;
    param.number_of_enabled_led = 16;
    danube_led_config(&param);
    param.operation_mask = CONFIG_OPERATION_DATA;
    param.data_mask = 1 << 4;
    param.data = 1 << 4;
    danube_led_config(&param);
#endif

    //  by default, update by FSC clock (FPID)
    param.operation_mask = CONFIG_OPERATION_UPDATE_CLOCK;
    param.update_clock   = 2;   //  FPID
    param.fpid           = 3;   //  10Hz
    danube_led_config(&param);

    //  source of LED 0, 1 is ADSL
    param.operation_mask = CONFIG_OPERATION_UPDATE_SOURCE;
    param.led            = 3;   //  LED 0, 1
    param.source         = 3;   //  ADSL
    danube_led_config(&param);

    //  turn on USB
    param.operation_mask = CONFIG_OPERATION_DATA;
    param.data_mask = 1 << 5;
    param.data = 1 << 5;
    danube_led_config(&param);

    return 0;
}

/*
 *  Description:
 *    deregister device
 *  Input:
 *    none
 *  Output:
 *    none
 */
void __exit danube_led_exit(void)
{
    int ret;

    ret = misc_deregister(&led_miscdev);
    if ( ret )
        printk(KERN_ERR "led: can't misc_deregister, get error number %d\n", -ret);
    else
        printk(KERN_INFO "led: misc_deregister successfully\n");
}

EXPORT_SYMBOL(danube_led_set_blink);
EXPORT_SYMBOL(danube_led_set_data);
EXPORT_SYMBOL(danube_led_config);

module_init(danube_led_init);
module_exit(danube_led_exit);