diff options
Diffstat (limited to 'firmware/target/arm/imx31/gigabeat-s/button-imx31.c')
-rw-r--r-- | firmware/target/arm/imx31/gigabeat-s/button-imx31.c | 247 |
1 files changed, 117 insertions, 130 deletions
diff --git a/firmware/target/arm/imx31/gigabeat-s/button-imx31.c b/firmware/target/arm/imx31/gigabeat-s/button-imx31.c index eea0faa4eb..493ce3af3e 100644 --- a/firmware/target/arm/imx31/gigabeat-s/button-imx31.c +++ b/firmware/target/arm/imx31/gigabeat-s/button-imx31.c | |||
@@ -21,156 +21,143 @@ | |||
21 | #include "cpu.h" | 21 | #include "cpu.h" |
22 | #include "system.h" | 22 | #include "system.h" |
23 | #include "button.h" | 23 | #include "button.h" |
24 | #include "kernel.h" | ||
25 | #include "backlight.h" | 24 | #include "backlight.h" |
26 | #include "adc.h" | ||
27 | #include "system.h" | 25 | #include "system.h" |
28 | #include "backlight-target.h" | 26 | #include "backlight-target.h" |
29 | #include "debug.h" | 27 | #include "avic-imx31.h" |
30 | #include "stdio.h" | ||
31 | 28 | ||
32 | /* Most code in here is taken from the Linux BSP provided by Freescale | 29 | /* Most code in here is taken from the Linux BSP provided by Freescale |
33 | * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */ | 30 | * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */ |
34 | 31 | ||
32 | static uint32_t int_btn = BUTTON_NONE; | ||
33 | static bool hold_button = false; | ||
34 | static bool hold_button_old = false; | ||
35 | #define _button_hold() (GPIO3_DR & 0x10) | ||
36 | |||
37 | static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void) | ||
38 | { | ||
39 | static const int key_mtx[5][3] = | ||
40 | { | ||
41 | { BUTTON_LEFT, BUTTON_BACK, BUTTON_VOL_UP }, | ||
42 | { BUTTON_UP, BUTTON_MENU, BUTTON_VOL_DOWN }, | ||
43 | { BUTTON_DOWN, BUTTON_NONE, BUTTON_PREV }, | ||
44 | { BUTTON_RIGHT, BUTTON_NONE, BUTTON_PLAY }, | ||
45 | { BUTTON_SELECT, BUTTON_NONE, BUTTON_NEXT }, | ||
46 | }; | ||
47 | |||
48 | unsigned short reg_val; | ||
49 | int col, row; | ||
50 | int i; | ||
51 | int button = BUTTON_NONE; | ||
52 | |||
53 | /* 1. Disable both (depress and release) keypad interrupts. */ | ||
54 | KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE); | ||
55 | |||
56 | for (col = 0; col < 3; col++) /* Col */ | ||
57 | { | ||
58 | /* 2. Write 1s to KPDR[10:8] setting column data to 1s */ | ||
59 | KPP_KPDR |= (0x7 << 8); | ||
60 | |||
61 | /* 3. Configure columns as totem pole outputs(for quick | ||
62 | * discharging of keypad capacitance) */ | ||
63 | KPP_KPCR &= ~(0x7 << 8); | ||
64 | |||
65 | /* Give the columns time to discharge */ | ||
66 | for (i = 0; i < 256; i++) | ||
67 | asm volatile (""); | ||
68 | |||
69 | /* 4. Configure columns as open-drain */ | ||
70 | KPP_KPCR |= (0x7 << 8); | ||
71 | |||
72 | /* 5. Write a single column to 0, others to 1. | ||
73 | * 6. Sample row inputs and save data. Multiple key presses | ||
74 | * can be detected on a single column. | ||
75 | * 7. Repeat steps 2 - 6 for remaining columns. */ | ||
76 | |||
77 | /* Col bit starts at 8th bit in KPDR */ | ||
78 | KPP_KPDR &= ~(1 << (8 + col)); | ||
79 | |||
80 | /* Delay added to avoid propagating the 0 from column to row | ||
81 | * when scanning. */ | ||
82 | for (i = 0; i < 256; i++) | ||
83 | asm volatile (""); | ||
84 | |||
85 | /* Read row input */ | ||
86 | reg_val = KPP_KPDR; | ||
87 | for (row = 0; row < 5; row++) /* sample row */ | ||
88 | { | ||
89 | if (!(reg_val & (1 << row))) | ||
90 | button |= key_mtx[row][col]; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | /* 8. Return all columns to 0 in preparation for standby mode. */ | ||
95 | KPP_KPDR &= ~(0x7 << 8); | ||
96 | |||
97 | /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1., | ||
98 | * set the KPKR synchronizer chain by writing "1" to KRSS register, | ||
99 | * clear the KPKD synchronizer chain by writing "1" to KDSC register */ | ||
100 | KPP_KPSR = KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD; | ||
101 | |||
102 | /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE | ||
103 | * detects a key hold condition, or the KRIE detects a key-release | ||
104 | * event. */ | ||
105 | if (int_btn != BUTTON_NONE) | ||
106 | KPP_KPSR |= KPP_KPSR_KRIE; | ||
107 | else | ||
108 | KPP_KPSR |= KPP_KPSR_KDIE; | ||
109 | |||
110 | int_btn = button; | ||
111 | } | ||
112 | |||
35 | void button_init_device(void) | 113 | void button_init_device(void) |
36 | { | 114 | { |
37 | unsigned int reg_val; | ||
38 | /* Enable keypad clock */ | 115 | /* Enable keypad clock */ |
39 | CLKCTL_CGR1 |= (3 << 2*10); | 116 | CLKCTL_CGR1 |= (3 << 2*10); |
40 | 117 | ||
41 | /* Enable number of rows in keypad (KPCR[7:0]) | 118 | /* 1. Enable number of rows in keypad (KPCR[4:0]) |
42 | * Configure keypad columns as open-drain (KPCR[15:8]) | ||
43 | * | 119 | * |
44 | * Configure the rows/cols in KPP | 120 | * Configure the rows/cols in KPP |
45 | * LSB nibble in KPP is for 8 rows | 121 | * LSB nybble in KPP is for 5 rows |
46 | * MSB nibble in KPP is for 8 cols | 122 | * MSB nybble in KPP is for 3 cols */ |
47 | */ | 123 | KPP_KPCR |= 0x1f; |
48 | #if 0 | 124 | |
49 | KPP_KPCR = (0xff << 8) | 0xff; | 125 | /* 2. Write 0's to KPDR[10:8] */ |
50 | /* Write 0's to KPDR[15:8] */ | 126 | KPP_KPDR &= ~(0x7 << 8); |
51 | reg_val = KPP_KPDR; | 127 | |
52 | reg_val &= 0x00ff; | 128 | /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */ |
53 | KPP_KPDR = reg_val; | 129 | KPP_KPCR |= (0x7 << 8); |
54 | 130 | ||
55 | /* Configure columns as output, rows as input (KDDR[15:0]) */ | 131 | /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */ |
56 | KPP_KDDR = 0xff00; | 132 | KPP_KDDR = (KPP_KDDR | (0x7 << 8)) & ~0x1f; |
57 | #endif | 133 | |
58 | 134 | /* 5. Clear the KPKD Status Flag and Synchronizer chain. | |
59 | KPP_KPSR = (1 << 3) | (1 << 2); | 135 | * 6. Set the KDIE control bit, and set the KRIE control |
136 | * bit (to force immediate scan). */ | ||
137 | KPP_KPSR = KPP_KPSR_KRIE | KPP_KPSR_KDIE | KPP_KPSR_KRSS | | ||
138 | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD; | ||
139 | |||
140 | /* KPP IRQ at priority 3 */ | ||
141 | avic_enable_int(KPP, IRQ, 3, KPP_HANDLER); | ||
60 | } | 142 | } |
61 | 143 | ||
62 | inline bool button_hold(void) | 144 | bool button_hold(void) |
63 | { | 145 | { |
64 | return GPIO3_DR & 0x10; | 146 | return _button_hold(); |
65 | } | 147 | } |
66 | 148 | ||
67 | int button_read_device(void) | 149 | int button_read_device(void) |
68 | { | 150 | { |
69 | unsigned short reg_val; | 151 | /* Simple poll of GPIO status */ |
70 | int col, row; | 152 | hold_button = _button_hold(); |
71 | int button = BUTTON_NONE; | 153 | |
72 | 154 | /* Backlight hold handling */ | |
73 | if(!button_hold()) { | 155 | if (hold_button != hold_button_old) |
74 | for (col = 0; col < 3; col++) { /* Col */ | 156 | { |
75 | /* 1. Write 1s to KPDR[15:8] setting column data to 1s */ | 157 | hold_button_old = hold_button; |
76 | reg_val = KPP_KPDR; | 158 | backlight_hold_changed(hold_button); |
77 | reg_val |= 0xff00; | ||
78 | KPP_KPDR = reg_val; | ||
79 | |||
80 | /* | ||
81 | * 2. Configure columns as totem pole outputs(for quick | ||
82 | * discharging of keypad capacitance) | ||
83 | */ | ||
84 | reg_val = KPP_KPCR; | ||
85 | reg_val &= 0x00ff; | ||
86 | KPP_KPCR = reg_val; | ||
87 | |||
88 | /* Give the columns time to discharge */ | ||
89 | udelay(2); | ||
90 | |||
91 | /* 3. Configure columns as open-drain */ | ||
92 | reg_val = KPP_KPCR; | ||
93 | reg_val |= ((1 << 8) - 1) << 8; | ||
94 | KPP_KPCR = reg_val; | ||
95 | |||
96 | /* 4. Write a single column to 0, others to 1. | ||
97 | * 5. Sample row inputs and save data. Multiple key presses | ||
98 | * can be detected on a single column. | ||
99 | * 6. Repeat steps 1 - 5 for remaining columns. | ||
100 | */ | ||
101 | |||
102 | /* Col bit starts at 8th bit in KPDR */ | ||
103 | reg_val = KPP_KPDR; | ||
104 | reg_val &= ~(1 << (8 + col)); | ||
105 | KPP_KPDR = reg_val; | ||
106 | |||
107 | /* Delay added to avoid propagating the 0 from column to row | ||
108 | * when scanning. */ | ||
109 | udelay(2); | ||
110 | |||
111 | /* Read row input */ | ||
112 | reg_val = KPP_KPDR; | ||
113 | for (row = 0; row < 5; row++) { /* sample row */ | ||
114 | if (!(reg_val & (1 << row))) { | ||
115 | if(row == 0) { | ||
116 | if(col == 0) { | ||
117 | button |= BUTTON_LEFT; | ||
118 | } | ||
119 | if(col == 1) { | ||
120 | button |= BUTTON_BACK; | ||
121 | } | ||
122 | if(col == 2) { | ||
123 | button |= BUTTON_VOL_UP; | ||
124 | } | ||
125 | } else if(row == 1) { | ||
126 | if(col == 0) { | ||
127 | button |= BUTTON_UP; | ||
128 | } | ||
129 | if(col == 1) { | ||
130 | button |= BUTTON_MENU; | ||
131 | } | ||
132 | if(col == 2) { | ||
133 | button |= BUTTON_VOL_DOWN; | ||
134 | } | ||
135 | } else if(row == 2) { | ||
136 | if(col == 0) { | ||
137 | button |= BUTTON_DOWN; | ||
138 | } | ||
139 | if(col == 2) { | ||
140 | button |= BUTTON_PREV; | ||
141 | } | ||
142 | } else if(row == 3) { | ||
143 | if(col == 0) { | ||
144 | button |= BUTTON_RIGHT; | ||
145 | } | ||
146 | if(col == 2) { | ||
147 | button |= BUTTON_PLAY; | ||
148 | } | ||
149 | } else if(row == 4) { | ||
150 | if(col == 0) { | ||
151 | button |= BUTTON_SELECT; | ||
152 | } | ||
153 | if(col == 2) { | ||
154 | button |= BUTTON_NEXT; | ||
155 | } | ||
156 | } | ||
157 | } | ||
158 | } | ||
159 | } | ||
160 | |||
161 | /* | ||
162 | * 7. Return all columns to 0 in preparation for standby mode. | ||
163 | * 8. Clear KPKD and KPKR status bit(s) by writing to a .1., | ||
164 | * set the KPKR synchronizer chain by writing "1" to KRSS register, | ||
165 | * clear the KPKD synchronizer chain by writing "1" to KDSC register | ||
166 | */ | ||
167 | reg_val = 0x00; | ||
168 | KPP_KPDR = reg_val; | ||
169 | reg_val = KPP_KPDR; | ||
170 | reg_val = KPP_KPSR; | ||
171 | reg_val |= 0xF; | ||
172 | KPP_KPSR = reg_val; | ||
173 | } | 159 | } |
174 | 160 | ||
175 | return button; | 161 | /* If hold, ignore any pressed button */ |
162 | return hold_button ? BUTTON_NONE : int_btn; | ||
176 | } | 163 | } |