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