T108: add gpio factory AT test

Change-Id: Ief06dc240ee8f0fdb77c88403ba69a8d40914de0
diff --git a/mbtk/mbtk_lib/inc/mbtk_gpio_def.h b/mbtk/mbtk_lib/inc/mbtk_gpio_def.h
index 0fb961a..d6d9b51 100755
--- a/mbtk/mbtk_lib/inc/mbtk_gpio_def.h
+++ b/mbtk/mbtk_lib/inc/mbtk_gpio_def.h
@@ -25,25 +25,14 @@
 GPIO2_BASE 0xD4019008
 GPIO3_BASE 0xD4019100
  */
-/*
-MMC_CMD	    GPIO_41
-MMC_DATA0	GPIO_40
-MMC_DATA1	GPIO_39
-MMC_DATA2	GPIO_38
-MMC_DATA3	GPIO_37
 
-SD_DET	GPIO_43
-WLAN_DAT3	GPIO_48
-
-
-*/
 #define  GPIO_FUNC_MMC1_DAT3	0xD401E094
 #define  GPIO_FUNC_MMC1_DAT2	0xD401E098
 #define  GPIO_FUNC_MMC1_DAT1	0xD401E09C
 #define  GPIO_FUNC_MMC1_DAT0	0xD401E0A0
-#define  GPIO_FUNC_MMC1_CMD	0xD401E0A4
-#define  GPIO_FUNC_MMC1_CLK	0xD401E0A8
-#define  GPIO_FUNC_MMC1_CD	0xD401E0AC
+#define  GPIO_FUNC_MMC1_CMD	    0xD401E0A4
+#define  GPIO_FUNC_MMC1_CLK	    0xD401E0A8
+#define  GPIO_FUNC_MMC1_CD	    0xD401E0AC
 #define  GPIO_FUNC_USB_ID	    0xD401E0B0
 #define  GPIO_FUNC_PRI_TDI	0xD401E0B4
 #define  GPIO_FUNC_PRI_TMS	0xD401E0B8
@@ -54,6 +43,7 @@
 #define  GPIO_FUNC_CLK_REQ	0xD401E0CC
 #define  GPIO_FUNC_VCXO_REQ	0xD401E0D4
 #define  GPIO_FUNC_VCXO_OUT	0xD401E0D8
+
 #define  GPIO_FUNC_GPIO_00	0xD401E0DC
 #define  GPIO_FUNC_GPIO_01	0xD401E0E0
 #define  GPIO_FUNC_GPIO_02	0xD401E0E4
@@ -65,6 +55,7 @@
 #define  GPIO_FUNC_GPIO_08	0xD401E0FC
 #define  GPIO_FUNC_GPIO_09	0xD401E100
 #define  GPIO_FUNC_GPIO_10	0xD401E104
+
 #define  GPIO_FUNC_GPIO_11	0xD401E108
 #define  GPIO_FUNC_GPIO_12	0xD401E10C
 #define  GPIO_FUNC_GPIO_13	0xD401E110
@@ -75,6 +66,7 @@
 #define  GPIO_FUNC_GPIO_18	0xD401E124
 #define  GPIO_FUNC_GPIO_19	0xD401E128
 #define  GPIO_FUNC_GPIO_20	0xD401E12C
+
 #define  GPIO_FUNC_GPIO_21	0xD401E130
 #define  GPIO_FUNC_GPIO_22	0xD401E134
 #define  GPIO_FUNC_GPIO_23	0xD401E138
@@ -85,12 +77,20 @@
 #define  GPIO_FUNC_GPIO_28	0xD401E14C
 #define  GPIO_FUNC_GPIO_29	0xD401E150
 #define  GPIO_FUNC_GPIO_30	0xD401E154
+
 #define  GPIO_FUNC_GPIO_31	0xD401E158
 #define  GPIO_FUNC_GPIO_32	0xD401E15C
 #define  GPIO_FUNC_GPIO_33	0xD401E160
 #define  GPIO_FUNC_GPIO_34	0xD401E164
 #define  GPIO_FUNC_GPIO_35	0xD401E168
 #define  GPIO_FUNC_GPIO_36	0xD401E16C
+#define  GPIO_FUNC_GPIO_37	0xD401E170
+#define  GPIO_FUNC_GPIO_38	0xD401E174
+#define  GPIO_FUNC_GPIO_39	0xD401E178
+#define  GPIO_FUNC_GPIO_40	0xD401E17C
+
+#define  GPIO_FUNC_GPIO_41	0xD401E180
+#define  GPIO_FUNC_GPIO_42	0xD401E184
 #define  GPIO_FUNC_GPIO_43	0xD401E188
 #define  GPIO_FUNC_GPIO_44	0xD401E18C
 #define  GPIO_FUNC_GPIO_45	0xD401E190
@@ -99,6 +99,7 @@
 #define  GPIO_FUNC_GPIO_48	0xD401E19C
 #define  GPIO_FUNC_GPIO_49	0xD401E1A0
 #define  GPIO_FUNC_GPIO_50	0xD401E1A4
+
 #define  GPIO_FUNC_GPIO_51	0xD401E1A8
 #define  GPIO_FUNC_GPIO_52	0xD401E1AC
 #define  GPIO_FUNC_GPIO_53	0xD401E1B0
@@ -107,6 +108,7 @@
 #define  GPIO_FUNC_DVL_1      0xD401E2B8
 #define  GPIO_FUNC_GPIO_69	0xD401E2BC
 #define  GPIO_FUNC_GPIO_70	0xD401E2C0
+
 #define  GPIO_FUNC_QSPI_DAT3	0xD401E2C4
 #define  GPIO_FUNC_QSPI_DAT2	0xD401E2C8
 #define  GPIO_FUNC_QSPI_DAT1	0xD401E2CC
@@ -117,6 +119,7 @@
 #define  GPIO_FUNC_GPIO_78	0xD401E2E0
 #define  GPIO_FUNC_GPIO_79	0xD401E2E4
 #define  GPIO_FUNC_GPIO_80	0xD401E2E8
+
 #define  GPIO_FUNC_SDIO_DAT3	0xD401E2EC
 #define  GPIO_FUNC_SDIO_DAT2	0xD401E2F0
 #define  GPIO_FUNC_SDIO_DAT1	0xD401E2F4
diff --git a/mbtk/mbtk_lib/src/mbtk_gpio.c b/mbtk/mbtk_lib/src/mbtk_gpio.c
index de143ba..80cff21 100755
--- a/mbtk/mbtk_lib/src/mbtk_gpio.c
+++ b/mbtk/mbtk_lib/src/mbtk_gpio.c
@@ -36,7 +36,7 @@
 
 static int gpio_debug = 0;
 
-#if defined(MBTK_PROJECT_L508_X6)
+#if (defined(MBTK_PROJECT_L508_X6) || defined(MBTK_PROJECT_T108))
 struct gpio_register_function gpio_func_register[128] = {
     {GPIO_FUNC_GPIO_00, 0},
     {GPIO_FUNC_GPIO_01, 0},
@@ -74,13 +74,13 @@
     {GPIO_FUNC_GPIO_33, 0},
     {GPIO_FUNC_GPIO_34, 0},
     {GPIO_FUNC_GPIO_35, 0},
-    {GPIO_FUNC_GPIO_36,   0},
-	{GPIO_FUNC_MMC1_DAT3, 5}, // GPIO_37
-    {GPIO_FUNC_MMC1_DAT2, 5}, // GPIO_38
-    {GPIO_FUNC_MMC1_DAT1, 5}, // GPIO_39
-    {GPIO_FUNC_MMC1_DAT0, 5}, // GPIO_40
-    {GPIO_FUNC_MMC1_CMD, 5},  //GPIO_41
-    {GPIO_FUNC_MMC1_CLK, 5},  //GPIO_42
+    {GPIO_FUNC_GPIO_36, 0},
+	{GPIO_FUNC_GPIO_37, 0}, // GPIO_37
+    {GPIO_FUNC_GPIO_38, 0}, // GPIO_38
+    {GPIO_FUNC_GPIO_39, 0}, // GPIO_39
+    {GPIO_FUNC_GPIO_40, 0}, // GPIO_40
+    {GPIO_FUNC_GPIO_41, 0},  //GPIO_41
+    {GPIO_FUNC_GPIO_42, 0},  //GPIO_42
     {GPIO_FUNC_GPIO_43, 0},  //GPIO_43
     {GPIO_FUNC_GPIO_44,0},	//GPIO_44
 	{GPIO_FUNC_GPIO_45,0},	//GPIO_45
@@ -1426,6 +1426,172 @@
     return n;
 }
 
+#elif defined(MBTK_PROJECT_T108)
+int gpio_register_test_out(int port, int value)
+{
+    int ret;
+    int i;
+    int valueh = 0;
+    int valuel = 1;
+    
+    //printf("Gpio port [%d] test start!\n", port);
+    ret = gpio_register_set_func_0(port);           //设功能为GPIO
+    if(ret){
+        printf("gpio_port can't support!\n");
+        return -1;
+    }
+    gpio_register_set_direction(port, 1);           //设方向为输出
+    //ret = gpio_register_get_value(port);
+    //printf("gpio default value is: %d\n", ret);
+    
+    
+    //[High]
+    for(i = 0; i <= 9; i++){
+        gpio_register_set_value(port, 1);
+        usleep(50);
+        //valueh = gpio_register_get_value(port);
+        //printf("set high? %d\n", valueh);
+        if(1 == valueh){
+            break;
+        }
+    }
+
+    for(i = 0; i <= 35; i++){
+        ret = gpio_register_get_value(118);  
+        if(1 == ret){
+            break;
+        }
+		usleep(3000);
+    }
+	printf("******gpio should is high: %d.******\n", ret);
+    if(1 != ret){
+        ret=-1;
+        goto exit;
+    }
+
+    //[Low]
+	usleep(200);
+	for(i = 0; i <= 9; i++){
+		gpio_register_set_value(port, 0);               //输出低
+		usleep(50);
+		//valuel = gpio_register_get_value(port);
+		//printf("set low? %d\n", valuel);
+		if(0 == valuel){
+			break;
+		}
+	}
+
+    for(i = 0; i <= 35; i++){
+        ret = gpio_register_get_value(118);  
+        if(0 == ret){
+            break;
+        }
+        usleep(3000);
+    }
+    //printf("******gpio should is low: %d.******\n", ret);
+    if(0 != ret){
+        ret=-1;
+        goto exit;
+    }
+
+exit:
+	gpio_register_set_value(port, 0);
+    gpio_register_set_direction(port, 0);  //设方向为输入
+    return ret;
+}
+
+int gpio_test_init_test_gpio_mode(void)
+{
+    int test_gpio[] = {
+		99,
+		117,
+		21,22,23,24,44,41,120,
+		8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
+		50,25,28,26,122,20,10,11,
+		39,40,37,38,
+		51,52,31,32,
+	};
+	
+    int i, j, ret, total, n = 0;
+    total = (sizeof(test_gpio)/sizeof(int));
+    printf("[init]gpio_test_init_test_gpio_mode BEGIN\n");
+	
+	/* [1 all gpio set to low] */
+	//system("echo 118 > /sys/class/gpio/unexport");
+	//system("echo 118 > /sys/class/gpio/export");
+	gpio_register_set_func_0(118);
+	gpio_register_set_direction(118, 0);
+	system("echo in  > /sys/class/gpio/gpio118/direction");
+	
+    for(j = 0; j < 6 ; j++){
+        for(i = 0; i < total; i++){
+            gpio_register_set_func_0(test_gpio[i]);
+            gpio_register_set_direction(test_gpio[i], 1);
+            gpio_register_set_value(test_gpio[i], 0);
+			//ret = gpio_register_get_value(test_gpio[i]);
+			//printf("[init]get gpio%d=%d\n", test_gpio[i], ret);
+        }
+		//system("hwacc w 0xd401e1ac 0x1040");	//52
+
+		for(i = 0; i <= 35; i++){
+		  ret = gpio_register_get_value(118);
+		  printf("[init]get gpi118=%d\n", ret);
+		  usleep(3000);          
+		  if(0 == (ret)){
+			  break;
+		  }
+		}
+		
+		printf("pre set ADC: %d, times: %d\n",(ret), j);
+		if(0 == (ret)){
+		  break;
+		}
+    }
+	if(0 != (ret)){
+		printf("!!!Set all low FAIL, please retest\n");
+		return -1;
+	}
+	return 0;
+}
+
+int mbtk_at_gpio(void* arg)
+{
+    
+
+    int test_gpio[] = {
+		99,
+		117,
+		21,22,23,24,44,41,120,
+		8,127,46,59,58,57,56,55,48,19,34,33,35,36,49,
+		50,25,28,26,122,20,10,11,
+		39,40,37,38,
+		51,52,31,32,
+
+	};
+
+    int i, n = 0, ret, total;
+    int *fail_io = (int *)arg;
+    total = (sizeof(test_gpio)/sizeof(int));
+	printf("T108 Start test gpio V1.0, total gpio=%d\n", total);
+	
+    /* [1 all gpio Init] */
+    gpio_test_init_test_gpio_mode();
+
+    /* [2 GPIOTEST] */
+	for(i = 0; i < total; i++){
+        ret = gpio_register_test_out(test_gpio[i], 0);
+        if(-1 == ret){
+            printf("!!!!!!!!!!!!gpio [%d] test failed!!!!!!!!!!!!\n", test_gpio[i]);
+            fail_io[n] = test_gpio[i];
+            n++;
+        }else{
+            //printf("############gpio [%d] test success############\n", test_gpio[i]);
+        }
+    }
+    return n;
+}
+
+
 
 #else
 int mbtk_at_gpio(void* arg)