summaryrefslogtreecommitdiff
path: root/cmd/odroidtest.c
blob: f7b4f5d23634afcef8be82a7cb003fa047b5c7d1 (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
/*
 *  (C) Copyright 2019 Hardkernel Co., Ltd
 *
 *  SPDX-License-Identifier:	GPL-2.0+
 */

#include "odroidtest.h"

extern bool is_odroidgo3(void);

static int yoffs;

static int do_odroidtest_all(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[]);

int wait_key_event(bool timeout)
{
	struct udevice *dev;
	struct dm_key_uclass_platdata *key;
	int evt;
	unsigned int cnt = 15;

	while (!ctrlc()) {
		for (uclass_first_device(UCLASS_KEY, &dev);
		     dev;
		     uclass_next_device(&dev)) {
			key = dev_get_uclass_platdata(dev);
			evt = key_read(key->code);
			if (evt == KEY_PRESS_DOWN) {
				printf("'%s [%d]' key pressed...\n",
					key->name, key->code);

				return key->code;
			} else if ((evt == KEY_PRESS_LONG_DOWN)
					&& (key->code == KEY_POWER)) {
				lcd_setbg_color("black");
				lcd_clear();
				lcd_setfg_color("white");
				lcd_printf(0, 10 + yoffs, 1, "Power key pressed... system will shut down.");
				mdelay(500);
				run_command("poweroff", 0);
			}

			if (timeout == true && cnt == 0)
				return -1;

			cnt--;
		}
	}

	return 0;
}

bool check_termination_key(int key)
{
	if (is_odroidgo3()) {
		/* termination using F3+F6 */
		if ((key == BTN_TRIGGER_HAPPY3) || (key == BTN_TRIGGER_HAPPY6)) {
			if(!run_command("gpio input C2", 0)
					&& !run_command("gpio input C5", 0)) {
				printf("Got termination key!\n");
				return true;
			}
		}
	} else {
		/* termination using F1+F6 */
		if ((key == BTN_TRIGGER_HAPPY1) || (key == BTN_TRIGGER_HAPPY6)) {
			if(!run_command("gpio input C0", 0)
					&& !run_command("gpio input C5", 0)) {
				printf("Got termination key!\n");
				return true;
			}
		}
	}

	return false;
}

/* adc mux controls */
#define GPIO_ADCMUX_EN		109 /* GPIO3_B5 */
#define GPIO_ADCMUX_SEL_A	107 /* GPIO3_B3 */
#define GPIO_ADCMUX_SEL_B	104 /* GPIO3_B0 */

static unsigned int num_adcch;

#define NUM_ADC_CH	4
#define ADC_RIGHT_Y	0
#define ADC_RIGHT_X	1
#define ADC_LEFT_Y	2
#define ADC_LEFT_X	3

struct key_adc {
	unsigned int center;
	unsigned int value;
};

void adc_draw_key_arrays(struct key_adc *adcs, int key_idx)
{
	int i;
	char cmd[16];

	lcd_setfg_color("yellow");

	if (is_odroidgo3()) {
		/* center */
		sprintf(cmd, "X %d", adcs[ADC_RIGHT_X].center);
		lcd_printf(599, 10, 0, cmd);
		sprintf(cmd, "Y %d", adcs[ADC_RIGHT_Y].center);
		lcd_printf(599, 12, 0, cmd);

		sprintf(cmd, "X %d", adcs[ADC_LEFT_X].center);
		lcd_printf(184, 10, 0, cmd);
		sprintf(cmd, "Y %d", adcs[ADC_LEFT_Y].center);
		lcd_printf(184, 12, 0, cmd);

		/* value */
		sprintf(cmd, "X %d   ",
				(int)adcs[ADC_RIGHT_X].value - adcs[ADC_RIGHT_X].center);
		lcd_printf(599, 18, 0, cmd);
		sprintf(cmd, "Y %d   ",
				(int)adcs[ADC_RIGHT_Y].value - adcs[ADC_RIGHT_Y].center);
		lcd_printf(599, 20, 0, cmd);

		sprintf(cmd, "X %d   ",
				(int)adcs[ADC_LEFT_X].value - adcs[ADC_LEFT_X].center);
		lcd_printf(184, 18, 0, cmd);
		sprintf(cmd, "Y %d   ",
				(int)adcs[ADC_LEFT_Y].value - adcs[ADC_LEFT_Y].center);
		lcd_printf(184, 20, 0, cmd);

		for (i = 0; i < 8; i++) {
			if (adckeys[i].chk) {
				if (i != key_idx)
					lcd_setfg_color("green");
				else
					lcd_setfg_color("blue");
			}
			else
				lcd_setfg_color("red");

			lcd_printf(adckeys[i].x, adckeys[i].y,
					0, adckeys[i].name);
		}
	} else {
		/* center */
		sprintf(cmd, "X %d", adcs[ADC_LEFT_X].center);
		lcd_printf(222, 6, 0, cmd);
		sprintf(cmd, "Y %d", adcs[ADC_LEFT_Y].center);
		lcd_printf(222, 8, 0, cmd);

		/* value */
		sprintf(cmd, "X %d   ",
				(int)adcs[ADC_LEFT_X].value - adcs[ADC_LEFT_X].center);
		lcd_printf(222, 13, 0, cmd);
		sprintf(cmd, "Y %d   ",
				(int)adcs[ADC_LEFT_Y].value - adcs[ADC_LEFT_Y].center);
		lcd_printf(222, 14, 0, cmd);

		for (i = 0; i < 4; i++) {
			if (adckeys[i].chk) {
				if (i != key_idx)
					lcd_setfg_color("green");
				else
					lcd_setfg_color("blue");
			}
			else
				lcd_setfg_color("red");

			lcd_printf(adckeys[i].x, adckeys[i].y,
					0, adckeys[i].name);
		}
	}

	mdelay(200);
	if (key_idx > 0) {
		lcd_setfg_color("green");
		lcd_printf(adckeys[key_idx].x, adckeys[key_idx].y,
				0, adckeys[key_idx].name);
	}
}

int adc_amux_select(int channel)
{
	/* enable mux */
	gpio_set_value(GPIO_ADCMUX_EN, 0);

	switch(channel) {
		case ADC_RIGHT_Y: /* Right Y */
			gpio_set_value(GPIO_ADCMUX_SEL_A, 0);
			gpio_set_value(GPIO_ADCMUX_SEL_B, 0);
			break;
		case ADC_RIGHT_X: /* Right X */
			gpio_set_value(GPIO_ADCMUX_SEL_A, 0);
			gpio_set_value(GPIO_ADCMUX_SEL_B, 1);
			break;
		case ADC_LEFT_Y: /* Left Y */
			gpio_set_value(GPIO_ADCMUX_SEL_A, 1);
			gpio_set_value(GPIO_ADCMUX_SEL_B, 0);
			break;
		case ADC_LEFT_X: /* Left X */
			gpio_set_value(GPIO_ADCMUX_SEL_A, 1);
			gpio_set_value(GPIO_ADCMUX_SEL_B, 1);
			break;
		default:
			/* disable adcmux */
			gpio_set_value(GPIO_ADCMUX_EN, 1);
			return -1;
	}

	mdelay(50);

	return 0;
}

#define ADC_CENTER_CHECK_COUNT	5
int adc_get_center(struct key_adc *adc, int adc_ch)
{
	int i = 0;
	unsigned int val;

	printf("adc_get_center : adc_ch %d\n", adc_ch);
	adc->center = 0;

	if (is_odroidgo3()) {
		adc_amux_select(adc_ch);

		while (i < ADC_CENTER_CHECK_COUNT) {
			if (adc_channel_single_shot("saradc", 1, &val)) {
				printf("adc_channel_single_shot fail!\n");
				return CMD_RET_FAILURE;
			}

			adc->center += val;

			mdelay(50);
			i++;
		}
	} else {
		while (i < ADC_CENTER_CHECK_COUNT) {
			/* Left X (CH3) */
			if (adc_ch == ADC_LEFT_X) {
				if (adc_channel_single_shot("saradc", 1, &val)) {
					printf("adc_channel_single_shot fail!\n");
					return CMD_RET_FAILURE;
				}
			}

			/* Left Y (CH2) */
			if (adc_ch == ADC_LEFT_Y) {
				if (adc_channel_single_shot("saradc", 2, &val)) {
					printf("adc_channel_single_shot fail!\n");
					return CMD_RET_FAILURE;
				}
			}

			adc->center += val;

			mdelay(50);
			i++;
		}
	}

	adc->center /= ADC_CENTER_CHECK_COUNT;

	return CMD_RET_SUCCESS;
}

int adc_read_value(struct key_adc *adc, int adc_ch)
{
	if (is_odroidgo3()) {
		adc_amux_select(adc_ch);

		if (adc_channel_single_shot("saradc", 1, &adc->value)) {
			printf("adc_channel_single_shot fail!\n");
			return CMD_RET_FAILURE;
		}
	} else {
		/* Left X */
		if (adc_ch == ADC_LEFT_X) {
			if (adc_channel_single_shot("saradc", 1, &adc->value)) {
				printf("adc_channel_single_shot fail!\n");
				return CMD_RET_FAILURE;
			}
		}

		/* Left Y */
		if (adc_ch == ADC_LEFT_Y) {
			if (adc_channel_single_shot("saradc", 2, &adc->value)) {
				printf("adc_channel_single_shot fail!\n");
				return CMD_RET_FAILURE;
			}
		}
	}

	return CMD_RET_SUCCESS;
}

#define ADC_CHECK_OFFSET	100
static int do_odroidtest_adc(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	struct udevice *dev;
	struct key_adc adcs[NUM_ADC_CH];

	int i;
	int center_x, center_y;
	int val_x, val_y;
	int key;
	int key_idx = -1;

	if (uclass_get_device(UCLASS_ADC, 0, &dev))
		return CMD_RET_FAILURE;

	/* init adcs arrays */
	memset(adcs, 0, sizeof(adcs));

	/* set adc key table */
	if (is_odroidgo3()) {
		adckeys = adckeys_go3;
		num_adcch = 4;
	} else {
		adckeys = adckeys_go2;
		num_adcch = 2;
	}

	/* draw background */
	lcd_setbg_color("black");
	lcd_clear();
	lcd_setfg_color("white");
	lcd_printf(0, 1, 1, "[ ADC KEY TEST ]");

	/* calibration preparation */
	lcd_printf(0, 4 + yoffs, 1, "For the Accuracy of Analog Joysticks Test,");
	lcd_printf(0, 6 + yoffs, 1, "Calibration will be Run First.");
	mdelay(1000);
	lcd_setfg_color("red");
	lcd_printf(0, 9+ yoffs, 1, "DO NOT Control Analog Joysticks During Calibration.");
	mdelay(1000);
	lcd_setfg_color("white");
	lcd_printf(0, 13 + yoffs, 1, "Now, Press Any Button to Start.");
	lcd_printf(0, 15 + yoffs, 1, "Then, Calibration will Start in 2 Seconds.");
	mdelay(500);

	key = wait_key_event(false);

	lcd_printf(0, 17 + yoffs, 1, "...          ");
	mdelay(400);
	lcd_printf(0, 17 + yoffs, 1, "......       ");
	mdelay(400);
	lcd_printf(0, 17 + yoffs, 1, ".........    ");
	mdelay(400);
	lcd_printf(0, 17 + yoffs, 1, ".............");
	mdelay(400);

	/* start calibration */
	lcd_clear();
	lcd_setfg_color("white");
	lcd_printf(0, 1, 1, "[ ADC KEY TEST ]");
	lcd_printf(0, 10 + yoffs, 1, "Calibration Starts...");
	lcd_printf(0, 12 + yoffs, 1, "It may take 1 or 2 seconds.");

	if (is_odroidgo3()) {
		gpio_request(GPIO_ADCMUX_EN, "adcmux_en");
		gpio_direction_output(GPIO_ADCMUX_EN, 1); /* default disable */
		gpio_request(GPIO_ADCMUX_SEL_A, "adcmux_sel_a");
		gpio_direction_output(GPIO_ADCMUX_SEL_A, 0);
		gpio_request(GPIO_ADCMUX_SEL_B, "adcmux_sel_b");
		gpio_direction_output(GPIO_ADCMUX_SEL_B, 0);
	}

	for (i = 0; i < NUM_ADC_CH; i++) {
		if (adc_get_center(&adcs[i], i))
			return CMD_RET_FAILURE;
	}

	mdelay(500);
	lcd_printf(0, 10 + yoffs, 1, "  Calibration Done !  ");
	lcd_printf(0, 12 + yoffs, 1, "Press Any Button to Start Analog Joysticks Test.");
	mdelay(500);

	key = wait_key_event(false);
	mdelay(1000);

	/* clear background */
	lcd_clear();
	lcd_setfg_color("white");
	lcd_printf(0, 1, 1, "[ ADC KEY TEST ]");
	lcd_setfg_color("grey");
	if (is_odroidgo3()) {
		lcd_printf(0, 26, 1, "F3+F6 press to exit ADC KEY test");
		lcd_printf(0, 28, 1, "Long press on PWR key to turn off power");
	} else {
		lcd_printf(0, 16, 1, "F1+F6 press to exit ADC KEY test");
		lcd_printf(0, 17, 1, "Long press on PWR key to turn off power");
	}

	adc_draw_key_arrays(adcs, key_idx);

	while (1) {
		if (is_odroidgo3()) {
			/* LEFT ADC */
			for (i = 2; i < 4; i++) {
				if (adc_read_value(&adcs[i], i))
					return CMD_RET_FAILURE;
			}

			center_x = adcs[ADC_LEFT_X].center;
			center_y = adcs[ADC_LEFT_Y].center;
			val_x = adcs[ADC_LEFT_X].value;
			val_y = adcs[ADC_LEFT_Y].value;

			debug("[LEFT]center x %d y %d / value x %d, y %d ",
					center_x, center_y,
					val_x, val_y);

			/* WEST */
			if (val_x < center_x - ADC_CHECK_OFFSET) {
				if (!adckeys[0].chk)
					adckeys[0].chk = 1;
				key_idx = 0;
				/* EAST */
			} else if (val_x > center_x + ADC_CHECK_OFFSET) {
				if (!adckeys[1].chk)
					adckeys[1].chk = 1;
				key_idx = 1;
				/* NORTH */
			} else if (val_y < center_y - ADC_CHECK_OFFSET) {
				if (!adckeys[2].chk)
					adckeys[2].chk = 1;
				key_idx = 2;
				/* SOUTH */
			} else if (val_y > center_y + ADC_CHECK_OFFSET) {
				if (!adckeys[3].chk)
					adckeys[3].chk = 1;
				key_idx = 3;
			}

			/* RIGHT ADC */
			for (i = 0; i < 2; i++) {
				if (adc_read_value(&adcs[i], i))
					return CMD_RET_FAILURE;
			}

			center_x = adcs[ADC_RIGHT_X].center;
			center_y = adcs[ADC_RIGHT_Y].center;
			val_x = adcs[ADC_RIGHT_X].value;
			val_y = adcs[ADC_RIGHT_Y].value;

			debug("[RIGHT]center x %d y %d / value x %d, y %d\n",
					center_x, center_y,
					val_x, val_y);

			/* WEST */
			if (val_x < center_x - ADC_CHECK_OFFSET) {
				if (!adckeys[4].chk)
					adckeys[4].chk = 1;
				key_idx = 4;
				/* EAST */
			} else if (val_x > center_x + ADC_CHECK_OFFSET) {
				if (!adckeys[5].chk)
					adckeys[5].chk = 1;
				key_idx = 5;
				/* NORTH */
			} else if (val_y < center_y - ADC_CHECK_OFFSET) {
				if (!adckeys[6].chk)
					adckeys[6].chk = 1;
				key_idx = 6;
				/* SOUTH */
			} else if (val_y > center_y + ADC_CHECK_OFFSET) {
				if (!adckeys[7].chk)
					adckeys[7].chk = 1;
				key_idx = 7;
			}
		} else {
			/* LEFT ADC Only */
			for (i = 2; i < 4; i++) {
				if (adc_read_value(&adcs[i], i))
					return CMD_RET_FAILURE;
			}

			center_x = adcs[ADC_LEFT_X].center;
			center_y = adcs[ADC_LEFT_Y].center;
			val_x = adcs[ADC_LEFT_X].value;
			val_y = adcs[ADC_LEFT_Y].value;

			printf("[LEFT]center x %d y %d / value x %d, y %d \n",
					center_x, center_y,
					val_x, val_y);

			/* WEST : plus value */
			if (val_x > center_x + ADC_CHECK_OFFSET) {
				if (!adckeys[0].chk)
					adckeys[0].chk = 1;
				key_idx = 0;
			/* EAST : minus value */
			} else if (val_x < center_x - ADC_CHECK_OFFSET) {
				if (!adckeys[1].chk)
					adckeys[1].chk = 1;
				key_idx = 1;
			/* NORTH : minus value */
			} else if (val_y < center_y - ADC_CHECK_OFFSET) {
				if (!adckeys[2].chk)
					adckeys[2].chk = 1;
				key_idx = 2;
			/* SOUTH : plus value */
			} else if (val_y > center_y + ADC_CHECK_OFFSET) {
				if (!adckeys[3].chk)
					adckeys[3].chk = 1;
				key_idx = 3;
			}
		}

		adc_draw_key_arrays(adcs, key_idx);
		key_idx = -1;
		mdelay(200);

		/* termination using F3+F6 */
		key = wait_key_event(true);
		if (check_termination_key(key))
			break;
	}

	lcd_setfg_color("white");
	lcd_printf(0, 18 + yoffs, 1, "ADC KEY TEST TERMINATED!");

	for (i = 0; i < 8; i++)
		adckeys[i].chk = 0;

	mdelay(1000);

	if (is_odroidgo3()) {
		gpio_free(GPIO_ADCMUX_EN);
		gpio_free(GPIO_ADCMUX_SEL_A);
		gpio_free(GPIO_ADCMUX_SEL_B);
	}

	return CMD_RET_SUCCESS;
}

#define BL_MAX_LEVEL		255
#define BL_OGA_MAX_LEVEL	160
#define BL_OGA_DEFAULT_LEVEL	80
static int do_odroidtest_backlight(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	int loop;
	int key;
	uint period_ns, duty_ns;

	u8 active[4] = {10, 30, 80, 100};
	struct udevice *dev;

	if (uclass_get_device(UCLASS_PWM, 0, &dev))
		return CMD_RET_FAILURE;

	lcd_setbg_color("red");
	lcd_clear();
	lcd_setfg_color("black");
	lcd_printf(0, 3, 1, "[ BACKLIGHT TEST ]");
	lcd_printf(0, 12 + yoffs, 1, "Press any key to go on next step");

	lcd_setfg_color("grey");
	if (is_odroidgo3()) {
		lcd_printf(0, 22 + yoffs, 1, "F3+F6 press to exit BACKLIGHT test");
		lcd_printf(0, 24 + yoffs, 1,
				"Long press on PWR key to turn off power");
	} else {
		lcd_printf(0, 16, 1, "F1+F6 press to exit BACKLIGHT test");
		lcd_printf(0, 17, 1,
				"Long press on PWR key to turn off power");
	}

	loop = 0;

	lcd_setfg_color("black");
	while (1) {
		period_ns = 25000;
		if (is_odroidgo3())
			duty_ns = period_ns * active[loop] / 100 * BL_OGA_MAX_LEVEL / BL_MAX_LEVEL;
		else
			duty_ns = period_ns * active[loop] / 100;

		printf("active percentage %d, duty_ns %d\n", active[loop], duty_ns);
		lcd_printf(0, 8 + yoffs, 1, "PERCENTAGE : %d %", active[loop]);

		if(pwm_set_config(dev, 1, period_ns, duty_ns))
			return CMD_RET_FAILURE;

		if (loop < 3)
			loop++;
		else
			loop = 0;

		key = wait_key_event(false);
		if (check_termination_key(key))
			break;

		mdelay(500);
	}

	if (is_odroidgo3()) {
		/* set backlight as default */
		duty_ns = period_ns * BL_OGA_DEFAULT_LEVEL / BL_MAX_LEVEL;
		pwm_set_config(dev, 1, period_ns, duty_ns);
	} else {
		/* set backlight as max */
		pwm_set_config(dev, 1, period_ns, period_ns);
	}

	lcd_setfg_color("black");
	lcd_printf(0, 18 + yoffs, 1, "BACKLIGHT TEST TERMINATED!");

	mdelay(1000);

	return CMD_RET_SUCCESS;
}

static int do_odroidtest_lcd(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	int loop = 0;
	int ret = 0;
	int key;
	char cmd[128];
	char *colors[] =
		{"red", "blue", "green", "white", "black"};

	while (1) {
		sprintf(cmd, "lcd setbg %s", colors[loop]);
		run_command(cmd, 0);
		run_command("lcd clear", 0);

		lcd_setfg_color("grey");
		if (is_odroidgo3()) {
			lcd_printf(0, 22 + yoffs, 1, "F3+F6 press to exit LCD test");
			lcd_printf(0, 24 + yoffs, 1,
					"Long press on PWR key to turn off power");
		} else {
			lcd_printf(0, 16, 1, "F1+F6 press to exit LCD test");
			lcd_printf(0, 17, 1,
					"Long press on PWR key to turn off power");
		}

		if (!strcmp(colors[loop], "red"))
			lcd_setfg_color("black");
		else
			lcd_setfg_color("magenta");

		sprintf(cmd, "%s", colors[loop]);
		lcd_printf(0, 4, 1, "[ LCD TEST ]");
		lcd_printf(0, 8 + yoffs, 1, cmd);
		sprintf(cmd, "%d / 5", (loop + 1));
		lcd_printf(0, 10 + yoffs, 1, cmd);
		lcd_printf(0, 12 + yoffs, 1, "Press any key to go on next step");

		if (loop < 4)	loop++;
		else		loop = 0;

		key = wait_key_event(false);
		if (check_termination_key(key))
			break;

		mdelay(500);
	}

	lcd_printf(0, 18 + yoffs, 1, "LCD TEST TERMINATED!");

	mdelay(1000);

	return ret;
}

void btn_draw_key_arrays(int numkeys, int key_idx)
{
	int i;

	for (i = 0; i < numkeys; i++) {
		if (gpiokeys[i].chk) {
			if (i != key_idx)
				lcd_setfg_color("green");
			else
				lcd_setfg_color("blue");
		} else {
			lcd_setfg_color("red");
		}

		lcd_printf(gpiokeys[i].x, gpiokeys[i].y,
				0, gpiokeys[i].name);
	}

	mdelay(200);

	if (key_idx > 0) {
		lcd_setfg_color("green");
		lcd_printf(gpiokeys[key_idx].x, gpiokeys[key_idx].y,
				0, gpiokeys[key_idx].name);
	}
}

static int btn_passed;
int btn_update_key_status(int key, int numkeys)
{
	int i = 0;

	while (i < numkeys) {
		if (gpiokeys[i].code == key) {
			if(!(gpiokeys[i].chk)) {
				gpiokeys[i].chk = 1;
				btn_passed++;
			}
			return i;
		}
		i++;
	}

	return -1;
}

void btn_set_default(int numkeys)
{
	int i;

	btn_passed = 0;
	for (i = 0; i < numkeys; i++)
		gpiokeys[i].chk = 0;
}

static int do_odroidtest_btn(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	int key, numkeys, key_idx;
	const char *hwrev = env_get("hwrev");

	if (is_odroidgo3()) {
		numkeys = NUMGPIOKEYS_GO3;
		gpiokeys = gpiokeys_go3;
	} else {
		if (hwrev && !strcmp(hwrev, "v10"))
			numkeys = NUMGPIOKEYS_GO2 - 2;
		else
			numkeys = NUMGPIOKEYS_GO2;
		gpiokeys = gpiokeys_go2;
	}

	/* draw background */
	lcd_setbg_color("black");
	lcd_clear();

	lcd_setfg_color("white");
	lcd_printf(0, 1, 1, "[ GPIO KEY TEST ]");

	lcd_setfg_color("grey");
	if (is_odroidgo3()) {
		lcd_printf(0, 22 + yoffs, 1, "F3+F6 press to exit BUTTON test");
		lcd_printf(0, 24 + yoffs, 1,
				"Long press on PWR key to turn off power");
	} else {
		lcd_printf(0, 16, 1, "F1+F6 press to exit BUTTON test");
		lcd_printf(0, 17, 1,
				"Long press on PWR key to turn off power");
	}

	/* key initialization */
	key_idx = -1;
	btn_draw_key_arrays(numkeys, key_idx);
	mdelay(2000);

	while (1) {
		key = wait_key_event(false);
		key_idx = btn_update_key_status(key, numkeys);

		/* update display */
		btn_draw_key_arrays(numkeys, key_idx);
		printf("key 0x%x, passed %d\n", key, btn_passed);

		if (check_termination_key(key))
			break;
	}

	lcd_setfg_color("white");
	lcd_printf(0, 18 + yoffs, 1, "GPIO KEY TEST TERMINATED!");

	btn_set_default(numkeys);

	mdelay(1000);

	return 0;
}

#define PWR_LED_GPIO	18	/* GPIO0_C2 */
#define DC_DET_GPIO	11	/* GPIO0_B3 */
#define CHG_LED_GPIO	13	/* GPIO0_B5 */
static int do_odroidtest_bat(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	struct udevice *fg;
	int ret;

	int key;

	int is_bat_exist;
	bool is_dcjack_exist;
	bool is_chrg_online;

	int bat_voltage;
	int chrg_current;
	int soc;

	char cmd[64];

	/* draw background */
	lcd_setbg_color("black");
	lcd_clear();

	lcd_setfg_color("white");
	lcd_printf(0, 1, 1, "[ BATTERY & CHARGE TEST ]");

	lcd_setfg_color("grey");
	if (is_odroidgo3()) {
		lcd_printf(0, 26, 1, "F3+F6 press to exit BATTERY test");
		lcd_printf(0, 28, 1,
				"Long press on PWR key to turn off power");
	} else {
		lcd_printf(0, 18, 1, "F1+F6 press to exit BATTERY test");
		lcd_printf(0, 19 + yoffs, 1,
				"Long press on PWR key to turn off power");
	}

	/* check fuel gauge device */
	ret = uclass_get_device(UCLASS_FG, 0, &fg);
	if (ret) {
		if (ret == -ENODEV)
			printf("Can't find FG\n");
		else
			printf("Get UCLASS FG failed: %d\n", ret);
		return ret;
	}

	gpio_request(PWR_LED_GPIO, "pwr_led");
	gpio_request(CHG_LED_GPIO, "chg_led");

	while (1) {
		lcd_setfg_color("yellow");

		/* 1. check battery connection */
		is_bat_exist = fuel_gauge_bat_is_exist(fg);
		printf("is_bat_exist %d\n", is_bat_exist);
		if (is_bat_exist) {
			lcd_printf(0, 3 + yoffs, 1, "  BATTERY : Connected  ");
		} else {
			lcd_printf(0, 3 + yoffs, 1, "BATTERY : Disconnected");
		}

		/* 2. check dc cable connection */
		is_dcjack_exist = gpio_get_value(DC_DET_GPIO) ? 1 : 0;
		printf("is_dcjack_exist %d\n", is_dcjack_exist);
		if (is_dcjack_exist) {
			lcd_printf(0, 6 + yoffs, 1, "  DC JACK : Connected  ");
			gpio_direction_output(PWR_LED_GPIO, 1);
		} else {
			lcd_printf(0, 6 + yoffs, 1, "DC JACK : Disconnected");
			gpio_direction_output(PWR_LED_GPIO, 0);
		}

		/* 3. get soc */
		if (is_bat_exist) {
			soc = fuel_gauge_update_get_soc(fg);
			printf("charging online : soc %d\n", soc);
		}

		/* 4. check charging online */
		is_chrg_online = fuel_gauge_get_chrg_online(fg);
		printf("is_chrg_online %d\n", is_chrg_online);
		if (is_chrg_online) {
			lcd_printf(0, 9 + yoffs, 1, "  CHARGING STATE : Online  ");
			gpio_direction_output(CHG_LED_GPIO, 1);
		} else {
			lcd_printf(0, 9 + yoffs, 1, "CHARGING STATE : Offline");
			gpio_direction_output(CHG_LED_GPIO, 0);
		}

		/* 5. show battery level */
		bat_voltage = fuel_gauge_get_voltage(fg);
		printf("bat_voltage %d\n", bat_voltage);
		sprintf(cmd, "BATTERY VOLTAGE : %d (mV)", bat_voltage);
		lcd_printf(0, 12 + yoffs, 1, cmd);

		/* 6. show charging current */
		chrg_current = fuel_gauge_get_current(fg);
		printf("chrg_current %d\n\n", chrg_current);
		sprintf(cmd, "CHARGING CURRENT : %d (mA)", chrg_current);
		lcd_printf(0, 15 + yoffs, 1, cmd);

		/* 7. check key event */
		key = wait_key_event(true);

		if (check_termination_key(key))
			break;

		/* loop delay 500ms+alpha */
		mdelay(500);
	}

	gpio_free(PWR_LED_GPIO);
	gpio_free(CHG_LED_GPIO);

	return 0;
}

static cmd_tbl_t cmd_sub_odroidtest[] = {
	U_BOOT_CMD_MKENT(all, 1, 0, do_odroidtest_all, "", ""),
	U_BOOT_CMD_MKENT(bat, 1, 0, do_odroidtest_bat, "", ""),
	U_BOOT_CMD_MKENT(btn, 1, 0, do_odroidtest_btn, "", ""),
	U_BOOT_CMD_MKENT(lcd, 1, 0, do_odroidtest_lcd, "", ""),
	U_BOOT_CMD_MKENT(backlight, 1, 0, do_odroidtest_backlight, "", ""),
	U_BOOT_CMD_MKENT(adc, 1, 0, do_odroidtest_adc, "", ""),
};

static int do_odroidtest_all(cmd_tbl_t * cmdtp, int flag,
				int argc, char * const argv[])
{
	int i, ret = 0;
	unsigned int state;
	unsigned int delay = 5000;

	for (i = 1; i < ARRAY_SIZE(cmd_sub_odroidtest); i++) {
		printf("----- [ %s ] -----\n",
				cmd_sub_odroidtest[i].name);
		ret = cmd_sub_odroidtest[i].cmd(cmdtp, flag,
				1, &cmd_sub_odroidtest[i].name);
	}

	lcd_clear();
	lcd_setfg_color("yellow");
	lcd_printf(0, 9, 1, "AUTO TEST DONE!");
	lcd_printf(0, 16 + yoffs, 1, "wait power key...");

	/* check power key */
	while (delay) {
		state = key_read(KEY_POWER);
		if (state == KEY_PRESS_DOWN)
			break;

		mdelay(25);
		delay -= 25;
	}

	printf("power key long pressed...\n");
	lcd_printf(0, 18 + yoffs, 1, "%s", "power off...");
	mdelay(500);
	run_command("poweroff", 0);

	return ret;
}

static int do_odroidtest(cmd_tbl_t *cmdtp,
			int flag,
			int argc,
			char * const argv[])
{
	cmd_tbl_t *cp;

	if (argc < 2)
		return CMD_RET_USAGE;

	if (is_odroidgo3())
		yoffs = 4;
	else
		yoffs = 0;

	cp = find_cmd_tbl(argv[1], cmd_sub_odroidtest,
			ARRAY_SIZE(cmd_sub_odroidtest));

	argc--;
	argv++;

	if (cp == NULL || argc > cp->maxargs)
		return CMD_RET_USAGE;

	return cp->cmd(cmdtp, flag, argc, argv);
}

U_BOOT_CMD(
	odroidtest, 2, 1, do_odroidtest,
	"ODROID GO Advanced HW Test",
	"odroidtest <subcmd>\n"
	"odroidtest all - do all test\n"
	"odroidtest bat - battery and charge test \n"
	"odroidtest btn - gpio button test\n"
	"odroidtest lcd - lcd test\n"
	"odroidtest backlight - backlight test\n"
	"odroidtest adc - analog switch test"
);