blob: d5ff84a75e2410faa13bfdc01cb1ca34ae1f314f [file] [log] [blame]
Daniel Ankersd282e142006-10-14 12:14:23 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Barry Wardell
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Daniel Ankersd282e142006-10-14 12:14:23 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22/* Taken from button-h10.c by Barry Wardell and reverse engineering by MrH. */
23
Daniel Ankersd282e142006-10-14 12:14:23 +000024#include "system.h"
25#include "button.h"
Daniel Ankersd282e142006-10-14 12:14:23 +000026#include "backlight.h"
Peter D'Hoye137ce712007-09-18 21:44:47 +000027#include "powermgmt.h"
Daniel Ankersd282e142006-10-14 12:14:23 +000028
Michael Sevakis873e0fd2007-07-22 21:02:24 +000029#define WHEEL_REPEAT_INTERVAL 300000
30#define WHEEL_FAST_ON_INTERVAL 20000
31#define WHEEL_FAST_OFF_INTERVAL 60000
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +000032#define WHEELCLICKS_PER_ROTATION 48 /* wheelclicks per full rotation */
Michael Sevakisd92e0952007-05-11 08:09:49 +000033
34/* Clickwheel */
Michael Sevakis31df21c2007-05-25 22:41:44 +000035#ifndef BOOTLOADER
Michael Sevakisd92e0952007-05-11 08:09:49 +000036static unsigned int old_wheel_value = 0;
37static unsigned int wheel_repeat = BUTTON_NONE;
38static unsigned int wheel_click_count = 0;
Michael Sevakis873e0fd2007-07-22 21:02:24 +000039static unsigned int wheel_delta = 0;
Michael Sevakisd92e0952007-05-11 08:09:49 +000040static int wheel_fast_mode = 0;
Michael Sevakis873e0fd2007-07-22 21:02:24 +000041static unsigned long last_wheel_usec = 0;
42static unsigned long wheel_velocity = 0;
43static long last_wheel_post = 0;
44static long next_backlight_on = 0;
Michael Sevakisd92e0952007-05-11 08:09:49 +000045/* Buttons */
46static bool hold_button = false;
47static bool hold_button_old = false;
Michael Sevakis5c32faa2007-06-03 14:31:42 +000048#define _button_hold() hold_button
49#else
50#define _button_hold() ((GPIOF_INPUT_VAL & 0x80) != 0)
Michael Sevakis31df21c2007-05-25 22:41:44 +000051#endif /* BOOTLOADER */
Michael Sevakisd92e0952007-05-11 08:09:49 +000052static int int_btn = BUTTON_NONE;
Daniel Ankersd282e142006-10-14 12:14:23 +000053
Michael Sevakisebe67b22007-06-03 15:17:01 +000054void button_int(void);
55
Daniel Ankersd282e142006-10-14 12:14:23 +000056void button_init_device(void)
57{
Barry Wardell331e34b2006-12-16 00:21:41 +000058 /* Enable all buttons */
Michael Sevakisd92e0952007-05-11 08:09:49 +000059 GPIOF_OUTPUT_EN &= ~0xff;
Daniel Ankersd282e142006-10-14 12:14:23 +000060 GPIOF_ENABLE |= 0xff;
Michael Sevakis5c32faa2007-06-03 14:31:42 +000061
Barry Wardella69b53d2006-12-18 19:08:41 +000062 /* Scrollwheel light - enable control through GPIOG pin 7 and set timeout */
Barry Wardella69b53d2006-12-18 19:08:41 +000063 GPIOG_OUTPUT_EN |= 0x80;
Michael Sevakisd92e0952007-05-11 08:09:49 +000064 GPIOG_ENABLE = 0x80;
65
Michael Sevakis31df21c2007-05-25 22:41:44 +000066#ifndef BOOTLOADER
Michael Sevakisebe67b22007-06-03 15:17:01 +000067 /* Mask these before performing init ... because init has possibly
68 occurred before */
69 GPIOF_INT_EN &= ~0xff;
70 GPIOH_INT_EN &= ~0xc0;
71
Michael Sevakis5c32faa2007-06-03 14:31:42 +000072 /* Get current tick before enabling button interrupts */
Michael Sevakis873e0fd2007-07-22 21:02:24 +000073 last_wheel_usec = USEC_TIMER;
74 last_wheel_post = last_wheel_usec;
Michael Sevakis5c32faa2007-06-03 14:31:42 +000075
Michael Sevakisd92e0952007-05-11 08:09:49 +000076 GPIOH_ENABLE |= 0xc0;
77 GPIOH_OUTPUT_EN &= ~0xc0;
78
Michael Sevakisd92e0952007-05-11 08:09:49 +000079 /* Read initial buttons */
Michael Sevakisebe67b22007-06-03 15:17:01 +000080 button_int();
81 GPIOF_INT_CLR = 0xff;
Michael Sevakisd92e0952007-05-11 08:09:49 +000082
Barry Wardell331e34b2006-12-16 00:21:41 +000083 /* Read initial wheel value (bit 6-7 of GPIOH) */
84 old_wheel_value = GPIOH_INPUT_VAL & 0xc0;
Michael Sevakisd92e0952007-05-11 08:09:49 +000085 GPIOH_INT_LEV = (GPIOH_INT_LEV & ~0xc0) | (old_wheel_value ^ 0xc0);
Michael Sevakisebe67b22007-06-03 15:17:01 +000086 GPIOH_INT_CLR = 0xc0;
Michael Sevakisd92e0952007-05-11 08:09:49 +000087
Michael Sevakis5c32faa2007-06-03 14:31:42 +000088 /* Enable button interrupts */
Michael Sevakisebe67b22007-06-03 15:17:01 +000089 GPIOF_INT_EN |= 0xff;
90 GPIOH_INT_EN |= 0xc0;
Michael Sevakisd92e0952007-05-11 08:09:49 +000091
Michael Sevakis5c32faa2007-06-03 14:31:42 +000092 CPU_INT_EN = HI_MASK;
Michael Sevakis8768ee82007-06-17 00:07:23 +000093 CPU_HI_INT_EN = GPIO1_MASK;
Michael Sevakis31df21c2007-05-25 22:41:44 +000094#endif /* BOOTLOADER */
Daniel Ankersd282e142006-10-14 12:14:23 +000095}
96
97bool button_hold(void)
98{
Michael Sevakis5c32faa2007-06-03 14:31:42 +000099 return _button_hold();
Michael Sevakisd92e0952007-05-11 08:09:49 +0000100}
101
Michael Sevakis31df21c2007-05-25 22:41:44 +0000102/* clickwheel */
103#ifndef BOOTLOADER
Michael Sevakisd92e0952007-05-11 08:09:49 +0000104void clickwheel_int(void)
105{
106 /* Read wheel
107 * Bits 6 and 7 of GPIOH change as follows:
108 * Clockwise rotation 01 -> 00 -> 10 -> 11
109 * Counter-clockwise 11 -> 10 -> 00 -> 01
110 *
111 * This is equivalent to wheel_value of:
112 * Clockwise rotation 0x40 -> 0x00 -> 0x80 -> 0xc0
113 * Counter-clockwise 0xc0 -> 0x80 -> 0x00 -> 0x40
114 */
115 static const unsigned char wheel_tbl[2][4] =
116 {
117 /* 0x00 0x40 0x80 0xc0 */ /* Wheel value */
118 { 0x40, 0xc0, 0x00, 0x80 }, /* Clockwise rotation */
119 { 0x80, 0x00, 0xc0, 0x40 }, /* Counter-clockwise */
120 };
121
122 unsigned int wheel_value;
123
Michael Sevakisd92e0952007-05-11 08:09:49 +0000124 wheel_value = GPIOH_INPUT_VAL & 0xc0;
125 GPIOH_INT_LEV = (GPIOH_INT_LEV & ~0xc0) | (wheel_value ^ 0xc0);
Michael Sevakis5c32faa2007-06-03 14:31:42 +0000126 GPIOH_INT_CLR = GPIOH_INT_STAT & 0xc0;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000127
128 if (!hold_button)
129 {
130 unsigned int btn = BUTTON_NONE;
131
132 if (old_wheel_value == wheel_tbl[0][wheel_value >> 6])
Jonathan Gordon81ba1462008-01-10 08:08:31 +0000133 btn = BUTTON_SCROLL_FWD;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000134 else if (old_wheel_value == wheel_tbl[1][wheel_value >> 6])
Jonathan Gordon81ba1462008-01-10 08:08:31 +0000135 btn = BUTTON_SCROLL_BACK;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000136
137 if (btn != BUTTON_NONE)
138 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000139 int repeat = 1; /* assume repeat */
140 unsigned long usec = USEC_TIMER;
141 unsigned v = (usec - last_wheel_usec) & 0x7fffffff;
142
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +0000143 v = (v>0) ? 1000000 / v : 0; /* clicks/sec = 1000000 * clicks/usec */
144 v = (v>0xffffff) ? 0xffffff : v; /* limit to 24 bit */
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000145
146 /* some velocity filtering to smooth things out */
147 wheel_velocity = (7*wheel_velocity + v) / 8;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000148
149 if (btn != wheel_repeat)
150 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000151 /* direction reversals nullify all fast mode states */
Michael Sevakisd92e0952007-05-11 08:09:49 +0000152 wheel_repeat = btn;
153 repeat =
154 wheel_fast_mode =
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000155 wheel_velocity =
Michael Sevakisd92e0952007-05-11 08:09:49 +0000156 wheel_click_count = 0;
157 }
158
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000159 if (wheel_fast_mode != 0)
Michael Sevakisd92e0952007-05-11 08:09:49 +0000160 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000161 /* fast OFF happens immediately when velocity drops below
162 threshold */
163 if (TIME_AFTER(usec,
164 last_wheel_usec + WHEEL_FAST_OFF_INTERVAL))
Michael Sevakisd92e0952007-05-11 08:09:49 +0000165 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000166 /* moving out of fast mode */
Michael Sevakisd92e0952007-05-11 08:09:49 +0000167 wheel_fast_mode = 0;
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000168 /* reset velocity */
169 wheel_velocity = 0;
170 /* wheel_delta is always 1 in slow mode */
171 wheel_delta = 1;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000172 }
173 }
174 else
175 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000176 /* fast ON gets filtered to avoid inadvertent jumps to fast mode */
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +0000177 if (repeat && wheel_velocity > 1000000/WHEEL_FAST_ON_INTERVAL)
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000178 {
179 /* moving into fast mode */
180 wheel_fast_mode = 1 << 31;
181 wheel_click_count = 0;
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +0000182 wheel_velocity = 1000000/WHEEL_FAST_OFF_INTERVAL;
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000183 }
Michael Sevakisd92e0952007-05-11 08:09:49 +0000184 else if (++wheel_click_count < 2)
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000185 {
Michael Sevakisd92e0952007-05-11 08:09:49 +0000186 btn = BUTTON_NONE;
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000187 }
188
189 /* wheel_delta is always 1 in slow mode */
190 wheel_delta = 1;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000191 }
192
Michael Sevakis89b53bf2007-08-06 12:58:38 +0000193 if (TIME_AFTER(current_tick, next_backlight_on) ||
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +0000194 v <= 4)
Michael Sevakisd92e0952007-05-11 08:09:49 +0000195 {
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000196 /* poke backlight to turn it on or maintain it no more often
197 than every 1/4 second*/
Michael Sevakis01492642007-08-06 13:01:40 +0000198 next_backlight_on = current_tick + HZ/4;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000199 backlight_on();
Jens Arnold5f1ec302007-10-07 15:02:02 +0000200 buttonlight_on();
Peter D'Hoye137ce712007-09-18 21:44:47 +0000201 reset_poweroff_timer();
Michael Sevakisd92e0952007-05-11 08:09:49 +0000202 }
Michael Sevakis31df21c2007-05-25 22:41:44 +0000203
Michael Sevakisd92e0952007-05-11 08:09:49 +0000204 if (btn != BUTTON_NONE)
205 {
206 wheel_click_count = 0;
207
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000208 /* generate repeats if quick enough */
209 if (repeat && TIME_BEFORE(usec,
210 last_wheel_post + WHEEL_REPEAT_INTERVAL))
Michael Sevakisd92e0952007-05-11 08:09:49 +0000211 btn |= BUTTON_REPEAT;
212
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000213 last_wheel_post = usec;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000214
215 if (queue_empty(&button_queue))
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000216 {
217 queue_post(&button_queue, btn, wheel_fast_mode |
Linus Nielsen Feltzinge75327b2007-11-19 11:05:54 +0000218 (wheel_delta << 24) | wheel_velocity*360/WHEELCLICKS_PER_ROTATION);
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000219 /* message posted - reset delta */
220 wheel_delta = 1;
221 }
222 else
223 {
224 /* skipped post - increment delta */
225 if (++wheel_delta > 0x7f)
226 wheel_delta = 0x7f;
227 }
Michael Sevakisd92e0952007-05-11 08:09:49 +0000228 }
229
Michael Sevakis873e0fd2007-07-22 21:02:24 +0000230 last_wheel_usec = usec;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000231 }
232 }
233
234 old_wheel_value = wheel_value;
235}
Michael Sevakis31df21c2007-05-25 22:41:44 +0000236#endif /* BOOTLOADER */
Michael Sevakisd92e0952007-05-11 08:09:49 +0000237
Michael Sevakis31df21c2007-05-25 22:41:44 +0000238/* device buttons */
Michael Sevakisd92e0952007-05-11 08:09:49 +0000239void button_int(void)
240{
241 unsigned char state;
242
Michael Sevakis31df21c2007-05-25 22:41:44 +0000243 int_btn = BUTTON_NONE;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000244
245 state = GPIOF_INPUT_VAL & 0xff;
246
Michael Sevakis31df21c2007-05-25 22:41:44 +0000247#ifndef BOOTLOADER
Michael Sevakisd92e0952007-05-11 08:09:49 +0000248 GPIOF_INT_LEV = (GPIOF_INT_LEV & ~0xff) | (state ^ 0xff);
Michael Sevakis5c32faa2007-06-03 14:31:42 +0000249 GPIOF_INT_CLR = GPIOF_INT_STAT;
Michael Sevakisd92e0952007-05-11 08:09:49 +0000250
Michael Sevakisd92e0952007-05-11 08:09:49 +0000251 hold_button = (state & 0x80) != 0;
Michael Sevakis5c32faa2007-06-03 14:31:42 +0000252#endif
Michael Sevakisd92e0952007-05-11 08:09:49 +0000253
Michael Sevakis5c32faa2007-06-03 14:31:42 +0000254 if (!_button_hold())
Michael Sevakisd92e0952007-05-11 08:09:49 +0000255 {
256 /* Read normal buttons */
257 if ((state & 0x01) == 0) int_btn |= BUTTON_REC;
258 if ((state & 0x02) == 0) int_btn |= BUTTON_DOWN;
259 if ((state & 0x04) == 0) int_btn |= BUTTON_RIGHT;
260 if ((state & 0x08) == 0) int_btn |= BUTTON_LEFT;
261 if ((state & 0x10) == 0) int_btn |= BUTTON_SELECT; /* The centre button */
262 if ((state & 0x20) == 0) int_btn |= BUTTON_UP; /* The "play" button */
263 if ((state & 0x40) != 0) int_btn |= BUTTON_POWER;
264 }
Daniel Ankersd282e142006-10-14 12:14:23 +0000265}
266
267/*
268 * Get button pressed from hardware
269 */
270int button_read_device(void)
271{
Michael Sevakis31df21c2007-05-25 22:41:44 +0000272#ifdef BOOTLOADER
273 /* Read buttons directly in the bootloader */
274 button_int();
275#else
Daniel Ankersd282e142006-10-14 12:14:23 +0000276 /* light handling */
277 if (hold_button != hold_button_old)
278 {
Michael Sevakisd92e0952007-05-11 08:09:49 +0000279 hold_button_old = hold_button;
Daniel Ankersd282e142006-10-14 12:14:23 +0000280 backlight_hold_changed(hold_button);
281 }
Michael Sevakis31df21c2007-05-25 22:41:44 +0000282#endif /* BOOTLOADER */
Daniel Ankersd282e142006-10-14 12:14:23 +0000283
Michael Sevakisd92e0952007-05-11 08:09:49 +0000284 /* The int_btn variable is set in the button interrupt handler */
285 return int_btn;
Daniel Ankersd282e142006-10-14 12:14:23 +0000286}