1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Daniel Stenberg
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)
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.
23 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
24 * KIND, either express or implied.
26 ****************************************************************************/
29 * Rockbox button functions
38 #include "backlight.h"
41 #include "powermgmt.h"
43 #define WHEEL_FAST_OFF_TIMEOUT 250000 /* timeout for acceleration = 250ms */
44 #define WHEEL_REPEAT_TIMEOUT 250000 /* timeout for button repeat = 250ms */
45 #define WHEEL_UNTOUCH_TIMEOUT 150000 /* timeout for untouching wheel = 150ms */
48 #define CLICKWHEEL_DATA (*(volatile unsigned long*)(0x7000c140))
49 #elif CONFIG_CPU==S5L8701
50 #define CLICKWHEEL00 (*(volatile unsigned long*)(0x3c200000))
51 #define CLICKWHEEL10 (*(volatile unsigned long*)(0x3c200010))
52 #define CLICKWHEELINT (*(volatile unsigned long*)(0x3c200014))
53 #define CLICKWHEEL_DATA (*(volatile unsigned long*)(0x3c200018))
55 #error CPU architecture not supported!
58 #define WHEELCLICKS_PER_ROTATION 96 /* wheelclicks per full rotation */
60 /* This amount of clicks is needed for at least scrolling 1 item. Choose small values
61 * to have high sensitivity but few precision, choose large values to have less
62 * sensitivity and good precision. */
63 #if defined(IPOD_NANO) || defined(IPOD_NANO2G)
64 #define WHEEL_SENSITIVITY 6 /* iPod nano has smaller wheel, lower sensitivity needed */
66 #define WHEEL_SENSITIVITY 4 /* default sensitivity */
69 int old_wheel_value
= -1;
70 int new_wheel_value
= 0;
73 bool wheel_is_touched
= false;
74 unsigned int accumulated_wheel_delta
= 0;
75 unsigned int wheel_repeat
= 0;
76 unsigned int wheel_velocity
= 0;
77 unsigned long last_wheel_usec
= 0;
79 /* Variable to use for setting button status in interrupt handler */
80 int int_btn
= BUTTON_NONE
;
81 #ifdef HAVE_WHEEL_POSITION
82 static int wheel_position
= -1;
83 static bool send_events
= true;
87 static void opto_i2c_init(void)
92 DEV_RS
&= ~DEV_OPTO
; /* finish reset */
93 DEV_INIT1
|= INIT_BUTTONS
; /* enable buttons (needed for "hold"-detection) */
95 outl(0xc00a1f00, 0x7000c100);
96 outl(0x01000000, 0x7000c104);
100 static inline int ipod_4g_button_read(void)
103 int btn
= BUTTON_NONE
;
106 if ((inl(0x7000c104) & 0x04000000) != 0)
109 unsigned status
= CLICKWHEEL_DATA
;
111 if ((status
& 0x800000ff) == 0x8000001a)
113 if (status
& 0x00000100)
114 btn
|= BUTTON_SELECT
;
115 if (status
& 0x00000200)
117 if (status
& 0x00000400)
119 if (status
& 0x00000800)
121 if (status
& 0x00001000)
123 if (status
& 0x40000000)
125 unsigned long usec
= USEC_TIMER
;
127 /* Highest wheel = 0x5F, clockwise increases */
128 new_wheel_value
= (status
>> 16) & 0x7f;
129 whl
= new_wheel_value
;
131 /* switch on backlight (again), reset power-off timer */
133 reset_poweroff_timer();
135 /* Check whether the scrollwheel was untouched by accident or by will. */
136 /* This is needed because wheel may be untoched very shortly during rotation */
137 if ( (!wheel_is_touched
) && TIME_AFTER(usec
, last_wheel_usec
+ WHEEL_UNTOUCH_TIMEOUT
) )
139 /* wheel has been really untouched -> reset internal variables */
140 old_wheel_value
= -1;
142 accumulated_wheel_delta
= 0;
143 wheel_repeat
= BUTTON_NONE
;
147 /* wheel was shortly untouched by accident -> leave internal variables */
148 wheel_is_touched
= true;
151 if (old_wheel_value
>= 0)
153 /* This is for later = BUTTON_SCROLL_TOUCH;*/
154 wheel_delta
= new_wheel_value
- old_wheel_value
;
155 unsigned int wheel_keycode
= BUTTON_NONE
;
157 /* Taking into account wrapping during transition from highest
158 * to lowest wheel position and back */
159 if (wheel_delta
< -WHEELCLICKS_PER_ROTATION
/2)
160 wheel_delta
+= WHEELCLICKS_PER_ROTATION
; /* Forward wrapping case */
161 else if (wheel_delta
> WHEELCLICKS_PER_ROTATION
/2)
162 wheel_delta
-= WHEELCLICKS_PER_ROTATION
; /* Backward wrapping case */
164 /* Getting direction and wheel_keycode from wheel_delta.
165 * Need at least some clicks to be sure to avoid haptic fuzziness */
166 if (wheel_delta
>= WHEEL_SENSITIVITY
)
167 wheel_keycode
= BUTTON_SCROLL_FWD
;
168 else if (wheel_delta
<= -WHEEL_SENSITIVITY
)
169 wheel_keycode
= BUTTON_SCROLL_BACK
;
171 wheel_keycode
= BUTTON_NONE
;
173 if (wheel_keycode
!= BUTTON_NONE
)
175 long v
= (usec
- last_wheel_usec
) & 0x7fffffff;
177 /* undo signedness */
178 wheel_delta
= (wheel_delta
>0) ? wheel_delta
: -wheel_delta
;
180 /* add the current wheel_delta */
181 accumulated_wheel_delta
+= wheel_delta
;
183 v
= v
? (1000000 * wheel_delta
) / v
: 0; /* clicks/sec = 1000000 * clicks/usec */
184 v
= (v
* 360) / WHEELCLICKS_PER_ROTATION
; /* conversion to degree/sec */
185 v
= (v
<0) ? -v
: v
; /* undo signedness */
187 /* some velocity filtering to smooth things out */
188 wheel_velocity
= (31 * wheel_velocity
+ v
) / 32;
189 /* limit to 24 bit */
190 wheel_velocity
= (wheel_velocity
>0xffffff) ? 0xffffff : wheel_velocity
;
192 /* assume REPEAT = off */
195 /* direction reversals must nullify acceleration and accumulator */
196 if (wheel_keycode
!= wheel_repeat
)
198 wheel_repeat
= wheel_keycode
;
200 accumulated_wheel_delta
= 0;
202 /* on same direction REPEAT is assumed when new click is within timeout */
203 else if (TIME_BEFORE(usec
, last_wheel_usec
+ WHEEL_REPEAT_TIMEOUT
))
205 repeat
= BUTTON_REPEAT
;
207 /* timeout nullifies acceleration and accumulator */
208 if (TIME_AFTER(usec
, last_wheel_usec
+ WHEEL_FAST_OFF_TIMEOUT
))
211 accumulated_wheel_delta
= 0;
214 #ifdef HAVE_WHEEL_POSITION
217 /* The queue should have no other events when scrolling */
218 if (queue_empty(&button_queue
))
220 /* each WHEEL_SENSITIVITY clicks = scrolling 1 item */
221 accumulated_wheel_delta
/= WHEEL_SENSITIVITY
;
222 #ifdef HAVE_SCROLLWHEEL
223 /* use data-format for HAVE_SCROLLWHEEL */
224 /* always use acceleration mode (1<<31) */
225 /* always set message post count to (1<<24) for iPod */
226 /* this way the scrolling is always calculated from wheel_velocity */
227 queue_post(&button_queue
, wheel_keycode
| repeat
,
228 (1<<31) | (1 << 24) | wheel_velocity
);
231 queue_post(&button_queue
, wheel_keycode
| repeat
,
232 (accumulated_wheel_delta
<< 16) | new_wheel_value
);
234 accumulated_wheel_delta
= 0;
236 last_wheel_usec
= usec
;
237 old_wheel_value
= new_wheel_value
;
242 /* scrollwheel was touched for the first time after finger lifting */
243 old_wheel_value
= new_wheel_value
;
244 wheel_is_touched
= true;
249 /* In this case the finger was lifted from the scrollwheel. */
250 wheel_is_touched
= false;
258 #ifdef HAVE_WHEEL_POSITION
259 /* Save the new absolute wheel position */
260 wheel_position
= whl
;
265 #ifdef HAVE_WHEEL_POSITION
266 int wheel_status(void)
268 return wheel_position
;
271 void wheel_send_events(bool send
)
278 void ipod_4g_button_int(void)
280 CPU_HI_INT_DIS
= I2C_MASK
;
282 /* The following delay was 250 in the ipodlinux source, but 50 seems to
283 work fine - tested on Nano, Color/Photo and Video. */
286 int_btn
= ipod_4g_button_read();
288 outl(inl(0x7000c104) | 0x0c000000, 0x7000c104);
289 outl(0x400a1f00, 0x7000c100);
291 CPU_HI_INT_EN
= I2C_MASK
;
294 void button_init_device(void)
298 /* hold button - enable as input */
299 GPIOA_ENABLE
|= 0x20;
300 GPIOA_OUTPUT_EN
&= ~0x20;
302 /* unmask interrupt */
303 CPU_INT_EN
= HI_MASK
;
304 CPU_HI_INT_EN
= I2C_MASK
;
307 bool button_hold(void)
309 return (GPIOA_INPUT_VAL
& 0x20)?false:true;
312 bool headphones_inserted(void)
314 return (GPIOA_INPUT_VAL
& 0x80)?true:false;
319 int clickwheel_events
= CLICKWHEELINT
;
321 /* Clear interrupts */
322 if (clickwheel_events
& 4) CLICKWHEELINT
= 4;
323 if (clickwheel_events
& 2) CLICKWHEELINT
= 2;
324 if (clickwheel_events
& 1) CLICKWHEELINT
= 1;
326 int_btn
= ipod_4g_button_read();
329 void button_init_device(void)
331 CLICKWHEEL00
= 0x280000;
338 bool button_hold(void)
340 return ((PDAT14
& (1 << 6)) == 0);
343 bool headphones_inserted(void)
345 return ((PDAT14
& (1 << 5)) != 0);
350 * Get button pressed from hardware
352 int button_read_device(void)
354 static bool hold_button
= false;
355 bool hold_button_old
;
358 hold_button_old
= hold_button
;
359 hold_button
= button_hold();
361 if (hold_button
!= hold_button_old
)
363 backlight_hold_changed(hold_button
);
368 /* lock -> disable wheel sensor */
370 #elif CONFIG_CPU==S5L8701
378 /* unlock -> enable wheel sensor */
381 #elif CONFIG_CPU==S5L8701
382 CLICKWHEEL00
= 0x280000;
388 /* The int_btn variable is set in the button interrupt handler */
389 #ifdef IPOD_ACCESSORY_PROTOCOL
390 return int_btn
| remote_control_rx();