tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / board / spreadtrum / grandprime3g_ve / sprd_kp.c
1 #include <common.h>
2 #include <malloc.h>
3 #include "key_map.h"
4 #include <boot_mode.h>
5 #include <asm/arch/mfp.h>
6 #include <asm/arch/sprd_keypad.h>
7 #include <asm/arch/chip_drv_common_io.h>
8 #include <asm/arch/sprd_eic.h>
9
10 struct key_map_info * sprd_key_map = 0;
11
12 void board_keypad_init(void)
13 {
14     unsigned int key_type;
15
16     sprd_key_map = malloc(sizeof(struct key_map_info));
17
18     if(NULL == sprd_key_map){
19       printf("%s malloc faild\n", __FUNCTION__);
20       return;
21     }
22
23     sprd_key_map->total_size = ARRAY_SIZE(board_key_map);
24     sprd_key_map->keycode_size = sizeof(board_key_map[0])*2;
25     sprd_key_map->key_map = board_key_map;
26     sprd_key_map->total_row = CONFIG_KEYPAD_ROW_CNT;
27     sprd_key_map->total_col = CONFIG_KEYPAD_COL_CNT;
28
29     if(sprd_key_map->total_size % sprd_key_map->keycode_size){
30         printf("%s: board_key_map config error, it should be %d aligned\n", __FUNCTION__, sprd_key_map->keycode_size);
31         return;
32     }
33
34     /* init sprd keypad controller */
35     REG32(REG_AON_APB_APB_EB0) |= BIT_8;
36     REG32(REG_AON_APB_APB_RTC_EB) |= BIT_1;
37     REG32(REG_AON_APB_APB_RST0) |= BIT_8;
38     udelay(2000);
39      REG32(REG_AON_APB_APB_RST0) &= ~BIT_8;
40
41     REG_KPD_INT_CLR = KPD_INT_ALL;
42     REG_KPD_POLARITY = CFG_ROW_POLARITY | CFG_COL_POLARITY;
43     REG_KPD_CLK_DIV_CNT = CFG_CLK_DIV & KPDCLK0_CLK_DIV0;
44     REG_KPD_LONG_KEY_CNT = CONFIG_KEYPAD_LONG_CNT;
45     REG_KPD_DEBOUNCE_CNT = CONFIG_KEYPAD_DEBOUNCE_CNT;//0x8;0x13
46     REG_KPD_CTRL  = (7<<8)/*Col0-Col2 Enable*/|(7<<16)/*Row0-Row2 Enable*/;
47     REG_KPD_CTRL |= 1; /*Keypad Enable*/;
48 }
49
50 static char handle_scan_code(unsigned char scan_code)
51 {
52     int cnt;
53     int key_map_cnt;
54     unsigned char * key_map;
55     int pos = 0;
56
57     if(NULL == sprd_key_map){
58         printf("plase call board_keypad_init first\n");
59         return 0;
60     }
61
62     key_map_cnt = sprd_key_map->total_size / sprd_key_map->keycode_size;
63     key_map = sprd_key_map->key_map;
64 #ifdef KEYPAD_DEBUG
65     printf("scan code %d\n", scan_code);
66 #endif
67     for(cnt = 0; cnt<key_map_cnt; cnt++){
68         pos = cnt * 2;
69         if(key_map[pos] == scan_code)
70           return key_map[pos + 1];
71     }
72     return 0;
73 }
74
75 //it can only handle one key now
76 unsigned char board_key_scan(void)
77 {
78     uint32_t s_int_status = REG_KPD_INT_RAW_STATUS;
79     uint32_t s_key_status = REG_KPD_KEY_STATUS;
80     uint32_t scan_code = 0;
81     uint32_t key_code =0;
82 #ifdef KEYPAD_DEBUG
83         printf("key operation flags is %08x, key %08x\n", REG_KPD_INT_RAW_STATUS, REG_KPD_KEY_STATUS);
84 #endif
85     if((s_int_status & KPD_PRESS_INT0) || (s_int_status & KPD_LONG_KEY_INT0)){
86         scan_code = s_key_status & (KPD1_ROW_CNT | KPD1_COL_CNT);
87         key_code += handle_scan_code(scan_code);
88     } if((s_int_status & KPD_PRESS_INT1) || (s_int_status & KPD_LONG_KEY_INT1)){
89         scan_code = (s_key_status & (KPD2_ROW_CNT | KPD2_COL_CNT))>>8;
90         key_code += handle_scan_code(scan_code);
91     }if((s_int_status & KPD_PRESS_INT2) || (s_int_status & KPD_LONG_KEY_INT2)){
92         scan_code = (s_key_status & (KPD3_ROW_CNT | KPD3_COL_CNT))>>16;
93         key_code += handle_scan_code(scan_code);
94     }if((s_int_status & KPD_PRESS_INT3) || (s_int_status & KPD_LONG_KEY_INT3)){
95         scan_code = (s_key_status & (KPD4_ROW_CNT | KPD4_COL_CNT))>>24;
96         key_code += handle_scan_code(scan_code);
97     }
98
99     if(s_int_status)
100         REG_KPD_INT_CLR = KPD_INT_ALL;
101
102     /*Adie EIC10 as Key_Volumeup*/
103         int volumeup = -1;
104         sprd_eic_request(EIC_KEY2_7S_RST_EXT_RSTN_ACTIVE);
105         udelay(3000);
106         volumeup = sprd_eic_get(EIC_KEY2_7S_RST_EXT_RSTN_ACTIVE);
107         if(volumeup < 0)
108                 printf("[eic keys] volumeup : sprd_eic_get return ERROR!\n");
109
110         if(volumeup > 0) {
111                 key_code = KEY_VOLUMEUP;
112                 printf("[eic keys] volumeup pressed!\n");
113         }
114
115     return key_code;
116 }
117
118 unsigned int check_key_boot(unsigned char key)
119 {
120     if(KEY_VOLUMEUP == key)
121       return BOOT_CALIBRATE;
122     else if(KEY_HOME == key)
123       return BOOT_FASTBOOT;
124     else if(KEY_VOLUMEDOWN== key)
125       return BOOT_RECOVERY;
126     else 
127       return 0;
128 }