tizen 2.4 release
[profile/mobile/platform/kernel/u-boot-tm1.git] / board / spreadtrum / sp8830ec_nwcn / 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     REG32(REG_AON_APB_APB_RST0) |= BIT_8;
37     udelay(2000);
38      REG32(REG_AON_APB_APB_RST0) &= ~BIT_8;
39
40     REG_KPD_INT_CLR = KPD_INT_ALL;
41     REG_KPD_POLARITY = CFG_ROW_POLARITY | CFG_COL_POLARITY;
42     REG_KPD_CLK_DIV_CNT = CFG_CLK_DIV & KPDCLK0_CLK_DIV0;
43     REG_KPD_LONG_KEY_CNT = CONFIG_KEYPAD_LONG_CNT;
44     REG_KPD_DEBOUNCE_CNT = CONFIG_KEYPAD_DEBOUNCE_CNT;//0x8;0x13
45     REG_KPD_CTRL  = (7<<8)/*Col0-Col2 Enable*/|(7<<16)/*Row0-Row2 Enable*/;
46     REG_KPD_CTRL |= 1; /*Keypad Enable*/;
47 }
48
49 static char handle_scan_code(unsigned char scan_code)
50 {
51     int cnt;
52     int key_map_cnt;
53     unsigned char * key_map;
54     int pos = 0;
55
56     if(NULL == sprd_key_map){
57         printf("plase call board_keypad_init first\n");
58         return 0;
59     }
60
61     key_map_cnt = sprd_key_map->total_size / sprd_key_map->keycode_size;
62     key_map = sprd_key_map->key_map;
63 #ifdef KEYPAD_DEBUG
64     printf("scan code %d\n", scan_code);
65 #endif
66     for(cnt = 0; cnt<key_map_cnt; cnt++){
67         pos = cnt * 2;
68         if(key_map[pos] == scan_code)
69           return key_map[pos + 1];
70     }
71     return 0;
72 }
73
74 //it can only handle one key now
75 unsigned char board_key_scan(void)
76 {
77     uint32_t s_int_status = REG_KPD_INT_RAW_STATUS;
78     uint32_t s_key_status = REG_KPD_KEY_STATUS;
79     uint32_t scan_code = 0;
80     uint32_t key_code =0;
81 #ifdef KEYPAD_DEBUG
82         printf("key operation flags is %08x, key %08x\n", REG_KPD_INT_RAW_STATUS, REG_KPD_KEY_STATUS);
83 #endif
84     if((s_int_status & KPD_PRESS_INT0) || (s_int_status & KPD_LONG_KEY_INT0)){
85         scan_code = s_key_status & (KPD1_ROW_CNT | KPD1_COL_CNT);
86         key_code += handle_scan_code(scan_code);
87     } if((s_int_status & KPD_PRESS_INT1) || (s_int_status & KPD_LONG_KEY_INT1)){
88         scan_code = (s_key_status & (KPD2_ROW_CNT | KPD2_COL_CNT))>>8;
89         key_code += handle_scan_code(scan_code);
90     }if((s_int_status & KPD_PRESS_INT2) || (s_int_status & KPD_LONG_KEY_INT2)){
91         scan_code = (s_key_status & (KPD3_ROW_CNT | KPD3_COL_CNT))>>16;
92         key_code += handle_scan_code(scan_code);
93     }if((s_int_status & KPD_PRESS_INT3) || (s_int_status & KPD_LONG_KEY_INT3)){
94         scan_code = (s_key_status & (KPD4_ROW_CNT | KPD4_COL_CNT))>>24;
95         key_code += handle_scan_code(scan_code);
96     }
97
98     if(s_int_status)
99         REG_KPD_INT_CLR = KPD_INT_ALL;
100     return key_code;
101 }
102
103 unsigned int check_key_boot(unsigned char key)
104 {
105     if(KEY_VOLUMEUP == key)
106       return BOOT_CALIBRATE;
107     else if(KEY_HOME == key)
108       return BOOT_FASTBOOT;
109     else if(KEY_VOLUMEDOWN== key)
110       return BOOT_RECOVERY;
111     else 
112       return 0;
113 }