Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2002 by Daniel Stenberg |
| 11 | * |
| 12 | * iPod driver based on code from the ipodlinux project - http://ipodlinux.org |
| 13 | * Adapted for Rockbox in December 2005 |
| 14 | * Original file: linux/arch/armnommu/mach-ipod/keyboard.c |
| 15 | * Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org) |
| 16 | * |
| 17 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 18 | * This program is free software; you can redistribute it and/or |
| 19 | * modify it under the terms of the GNU General Public License |
| 20 | * as published by the Free Software Foundation; either version 2 |
| 21 | * of the License, or (at your option) any later version. |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 22 | * |
| 23 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 24 | * KIND, either express or implied. |
| 25 | * |
| 26 | ****************************************************************************/ |
| 27 | |
| 28 | /* |
| 29 | * Rockbox button functions |
| 30 | */ |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | #include "config.h" |
| 34 | #include "cpu.h" |
| 35 | #include "system.h" |
| 36 | #include "button.h" |
| 37 | #include "kernel.h" |
| 38 | #include "backlight.h" |
| 39 | #include "adc.h" |
| 40 | #include "serial.h" |
| 41 | #include "power.h" |
| 42 | #include "system.h" |
| 43 | #include "powermgmt.h" |
Jens Arnold | c68e3cc | 2007-08-15 17:53:23 +0000 | [diff] [blame] | 44 | #include "hwcompat.h" |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 45 | |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 46 | static int int_btn = BUTTON_NONE; |
| 47 | #ifdef IPOD_1G2G |
| 48 | /* The 1st Gen wheel draws ~12mA when enabled permanently. Therefore |
| 49 | * we only enable it for a very short time to check for changes every |
| 50 | * tick, and only keep it enabled if there is activity. */ |
| 51 | #define WHEEL_TIMEOUT (HZ/4) |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 52 | #endif |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 53 | |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 54 | static void handle_scroll_wheel(int new_scroll, int was_hold) |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 55 | { |
| 56 | int wheel_keycode = BUTTON_NONE; |
| 57 | static int prev_scroll = -1; |
| 58 | static int direction = 0; |
| 59 | static int count = 0; |
| 60 | static int scroll_state[4][4] = { |
| 61 | {0, 1, -1, 0}, |
| 62 | {-1, 0, 0, 1}, |
| 63 | {1, 0, 0, -1}, |
| 64 | {0, -1, 1, 0} |
| 65 | }; |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 66 | |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 67 | if ( prev_scroll == -1 ) { |
| 68 | prev_scroll = new_scroll; |
| 69 | } |
| 70 | else if (direction != scroll_state[prev_scroll][new_scroll]) { |
| 71 | direction = scroll_state[prev_scroll][new_scroll]; |
| 72 | count = 0; |
| 73 | } |
| 74 | else if (!was_hold) { |
| 75 | backlight_on(); |
Jens Arnold | 4012aae | 2007-01-19 07:33:23 +0000 | [diff] [blame] | 76 | reset_poweroff_timer(); |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 77 | if (++count == 6) { /* reduce sensitivity */ |
| 78 | count = 0; |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 79 | /* 1st..3rd Gen wheel has inverse direction mapping |
| 80 | * compared to Mini 1st Gen wheel. */ |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 81 | switch (direction) { |
| 82 | case 1: |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 83 | wheel_keycode = BUTTON_SCROLL_BACK; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 84 | break; |
| 85 | case -1: |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 86 | wheel_keycode = BUTTON_SCROLL_FWD; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 87 | break; |
| 88 | default: |
| 89 | /* only happens if we get out of sync */ |
| 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | if (wheel_keycode != BUTTON_NONE && queue_empty(&button_queue)) |
Michael Sevakis | 4b90267 | 2006-12-19 16:50:07 +0000 | [diff] [blame] | 95 | queue_post(&button_queue, wheel_keycode, 0); |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 96 | prev_scroll = new_scroll; |
| 97 | } |
| 98 | |
| 99 | static int ipod_3g_button_read(void) |
| 100 | { |
| 101 | unsigned char source, state; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 102 | static bool was_hold = false; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 103 | int btn = BUTTON_NONE; |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 104 | |
| 105 | #ifdef IPOD_3G |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 106 | /* The following delay was 250 in the ipodlinux source, |
| 107 | * but 50 seems to work fine. 250 causes the wheel to stop |
| 108 | * working when spinning it real fast. */ |
| 109 | udelay(50); |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 110 | #endif |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 111 | |
| 112 | /* get source of interupts */ |
| 113 | source = GPIOA_INT_STAT; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 114 | |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 115 | /* get current keypad status */ |
| 116 | state = GPIOA_INPUT_VAL; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 117 | |
| 118 | /* toggle interrupt level */ |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 119 | GPIOA_INT_LEV = ~state; |
| 120 | |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 121 | #ifdef IPOD_3G |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 122 | if (was_hold && source == 0x40 && state == 0xbf) { |
| 123 | /* ack any active interrupts */ |
| 124 | GPIOA_INT_CLR = source; |
| 125 | return BUTTON_NONE; |
| 126 | } |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 127 | was_hold = false; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 128 | |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 129 | if ((state & 0x20) == 0) { |
| 130 | /* 3g hold switch is active low */ |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 131 | was_hold = true; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 132 | /* hold switch on 3g causes all outputs to go low */ |
| 133 | /* we shouldn't interpret these as key presses */ |
| 134 | GPIOA_INT_CLR = source; |
| 135 | return BUTTON_NONE; |
| 136 | } |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 137 | #elif defined IPOD_1G2G |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 138 | if (state & 0x20) { |
| 139 | /* 1g/2g hold switch is active high */ |
| 140 | GPIOA_INT_CLR = source; |
| 141 | return BUTTON_NONE; |
| 142 | } |
Jens Arnold | b701322 | 2007-07-27 09:57:27 +0000 | [diff] [blame] | 143 | #endif |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 144 | if ((state & 0x1) == 0) { |
| 145 | btn |= BUTTON_RIGHT; |
| 146 | } |
| 147 | if ((state & 0x2) == 0) { |
| 148 | btn |= BUTTON_SELECT; |
| 149 | } |
| 150 | if ((state & 0x4) == 0) { |
| 151 | btn |= BUTTON_PLAY; |
| 152 | } |
| 153 | if ((state & 0x8) == 0) { |
| 154 | btn |= BUTTON_LEFT; |
| 155 | } |
| 156 | if ((state & 0x10) == 0) { |
| 157 | btn |= BUTTON_MENU; |
| 158 | } |
| 159 | |
| 160 | if (source & 0xc0) { |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 161 | handle_scroll_wheel((state & 0xc0) >> 6, was_hold); |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* ack any active interrupts */ |
| 165 | GPIOA_INT_CLR = source; |
| 166 | |
| 167 | return btn; |
| 168 | } |
| 169 | |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 170 | void ipod_3g_button_int(void) |
| 171 | { |
Michael Sevakis | 191320c | 2008-06-03 05:08:24 +0000 | [diff] [blame] | 172 | CPU_INT_DIS = GPIO_MASK; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 173 | int_btn = ipod_3g_button_read(); |
| 174 | CPU_INT_EN = GPIO_MASK; |
| 175 | } |
| 176 | |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 177 | void button_init_device(void) |
| 178 | { |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 179 | GPIOA_ENABLE = 0xff; |
| 180 | GPIOA_OUTPUT_EN = 0; |
| 181 | |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 182 | GPIOA_INT_LEV = ~GPIOA_INPUT_VAL; |
| 183 | GPIOA_INT_CLR = GPIOA_INT_STAT; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 184 | |
Jens Arnold | c68e3cc | 2007-08-15 17:53:23 +0000 | [diff] [blame] | 185 | #ifdef IPOD_1G2G |
| 186 | if ((IPOD_HW_REVISION >> 16) == 1) |
| 187 | { /* enable scroll wheel */ |
| 188 | GPIOB_ENABLE |= 0x01; |
| 189 | GPIOB_OUTPUT_EN |= 0x01; |
| 190 | GPIOB_OUTPUT_VAL |= 0x01; |
| 191 | } |
| 192 | #endif |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 193 | GPIOA_INT_EN = 0xff; |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 194 | |
| 195 | CPU_INT_EN = GPIO_MASK; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* |
| 199 | * Get button pressed from hardware |
| 200 | */ |
| 201 | int button_read_device(void) |
| 202 | { |
| 203 | static bool hold_button = false; |
| 204 | bool hold_button_old; |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 205 | #ifdef IPOD_1G2G |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 206 | static int wheel_timeout = 0; |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 207 | static unsigned char last_wheel_value = 0; |
| 208 | unsigned char wheel_value; |
| 209 | |
| 210 | if ((IPOD_HW_REVISION >> 16) == 1) |
| 211 | { |
| 212 | if (!hold_button && (wheel_timeout == 0)) |
| 213 | { |
| 214 | GPIOB_OUTPUT_VAL |= 0x01; /* enable wheel */ |
| 215 | udelay(50); /* let the voltage settle */ |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 216 | } |
| 217 | wheel_value = GPIOA_INPUT_VAL >> 6; |
| 218 | if (wheel_value != last_wheel_value) |
| 219 | { |
| 220 | last_wheel_value = wheel_value; |
| 221 | wheel_timeout = WHEEL_TIMEOUT; /* keep wheel enabled */ |
Jens Arnold | 1f75bd5 | 2008-04-01 18:53:34 +0000 | [diff] [blame] | 222 | GPIOA_INT_EN = 0xff; /* enable wheel interrupts */ |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 223 | } |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 224 | if (wheel_timeout) |
| 225 | wheel_timeout--; |
| 226 | else |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 227 | { |
| 228 | GPIOA_INT_EN = 0x3f; /* disable wheel interrupts */ |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 229 | GPIOB_OUTPUT_VAL &= ~0x01; /* disable wheel */ |
Jens Arnold | e249ecc | 2008-04-01 17:52:22 +0000 | [diff] [blame] | 230 | } |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 231 | } |
| 232 | #endif |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 233 | |
| 234 | /* normal buttons */ |
| 235 | hold_button_old = hold_button; |
| 236 | hold_button = button_hold(); |
| 237 | |
| 238 | if (hold_button != hold_button_old) |
| 239 | backlight_hold_changed(hold_button); |
Jens Arnold | 7c37c54 | 2007-08-18 09:47:19 +0000 | [diff] [blame] | 240 | |
Jens Arnold | ffb121c | 2007-07-29 08:03:21 +0000 | [diff] [blame] | 241 | return int_btn; |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | bool button_hold(void) |
| 245 | { |
Jens Arnold | 021c086 | 2007-07-27 12:05:54 +0000 | [diff] [blame] | 246 | #ifdef IPOD_1G2G |
| 247 | return (GPIOA_INPUT_VAL & 0x20); |
| 248 | #else |
| 249 | return !(GPIOA_INPUT_VAL & 0x20); |
| 250 | #endif |
Barry Wardell | d4945dc | 2006-10-05 10:58:51 +0000 | [diff] [blame] | 251 | } |
Daniel Ankers | 5c6f32a | 2006-10-07 12:19:34 +0000 | [diff] [blame] | 252 | |
| 253 | bool headphones_inserted(void) |
| 254 | { |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 255 | #ifdef IPOD_1G2G |
Michael Giacomelli | 5fc95a3 | 2008-05-03 22:07:31 +0000 | [diff] [blame] | 256 | if ((IPOD_HW_REVISION >> 16) == 2) |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 257 | { |
Michael Giacomelli | 5fc95a3 | 2008-05-03 22:07:31 +0000 | [diff] [blame] | 258 | /* 2G uses GPIO B bit 0 */ |
| 259 | return (GPIOB_INPUT_VAL & 0x1)?true:false; |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 260 | } |
Michael Giacomelli | 5fc95a3 | 2008-05-03 22:07:31 +0000 | [diff] [blame] | 261 | else |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 262 | { |
Michael Giacomelli | 5fc95a3 | 2008-05-03 22:07:31 +0000 | [diff] [blame] | 263 | /* 1G has no headphone detection, so fake insertion */ |
| 264 | return (true); |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 265 | } |
| 266 | #else |
| 267 | /* 3G uses GPIO C bit 0 */ |
Daniel Ankers | 5c6f32a | 2006-10-07 12:19:34 +0000 | [diff] [blame] | 268 | return (GPIOC_INPUT_VAL & 0x1)?true:false; |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 269 | #endif |
Daniel Ankers | 5c6f32a | 2006-10-07 12:19:34 +0000 | [diff] [blame] | 270 | } |
Michael Giacomelli | e243db6 | 2008-05-03 21:15:13 +0000 | [diff] [blame] | 271 | |
| 272 | |