Remove inner loop from button scanning on Gigabeat S. Requires changing the button...
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / button-imx31.c
blobe80166bca747176ae96b14ad994ba980e16bb7f0
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "config.h"
21 #include "cpu.h"
22 #include "system.h"
23 #include "button.h"
24 #include "backlight.h"
25 #include "system.h"
26 #include "backlight-target.h"
27 #include "avic-imx31.h"
28 #include "clkctl-imx31.h"
30 /* Most code in here is taken from the Linux BSP provided by Freescale
31 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */
33 static bool headphones_detect = false;
34 static uint32_t int_btn = BUTTON_NONE;
35 static bool hold_button = false;
36 static bool hold_button_old = false;
37 #define _button_hold() (GPIO3_DR & 0x10)
39 static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void)
41 static const struct key_mask_shift
43 uint8_t mask;
44 uint8_t shift;
45 } kms[3] =
47 { 0x1f, 0 }, /* BUTTON_LEFT...BUTTON_SELECT */
48 { 0x03, 5 }, /* BUTTON_BACK...BUTTON_MENU */
49 { 0x1f, 7 }, /* BUTTON_VOL_UP...BUTTON_NEXT */
52 int col;
53 /* Power button is handled separately on PMIC */
54 int button = int_btn & BUTTON_POWER;
56 int oldlevel = disable_irq_save();
58 /* 1. Disable both (depress and release) keypad interrupts. */
59 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
61 for (col = 0; col < 3; col++) /* Col */
63 int i;
65 /* 2. Write 1s to KPDR[10:8] setting column data to 1s */
66 KPP_KPDR |= (0x7 << 8);
68 /* 3. Configure columns as totem pole outputs(for quick
69 * discharging of keypad capacitance) */
70 KPP_KPCR &= ~(0x7 << 8);
72 /* Give the columns time to discharge */
73 for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */
74 asm volatile ("");
76 /* 4. Configure columns as open-drain */
77 KPP_KPCR |= (0x7 << 8);
79 /* 5. Write a single column to 0, others to 1.
80 * 6. Sample row inputs and save data. Multiple key presses
81 * can be detected on a single column.
82 * 7. Repeat steps 2 - 6 for remaining columns. */
84 /* Col bit starts at 8th bit in KPDR */
85 KPP_KPDR &= ~(0x100 << col);
87 /* Delay added to avoid propagating the 0 from column to row
88 * when scanning. */
89 for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */
90 asm volatile ("");
92 /* Read row input */
93 button |= (~KPP_KPDR & kms[col].mask) << kms[col].shift;
96 /* 8. Return all columns to 0 in preparation for standby mode. */
97 KPP_KPDR &= ~(0x7 << 8);
99 /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
100 * set the KPKR synchronizer chain by writing "1" to KRSS register,
101 * clear the KPKD synchronizer chain by writing "1" to KDSC register */
102 KPP_KPSR = KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
104 /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE
105 * detects a key hold condition, or the KRIE detects a key-release
106 * event. */
107 if ((button & ~BUTTON_POWER) != BUTTON_NONE)
108 KPP_KPSR |= KPP_KPSR_KRIE;
109 else
110 KPP_KPSR |= KPP_KPSR_KDIE;
112 restore_irq(oldlevel);
114 int_btn = button;
117 void button_init_device(void)
119 /* Enable keypad clock */
120 imx31_clkctl_module_clock_gating(CG_KPP, CGM_ON_ALL);
122 /* 1. Enable number of rows in keypad (KPCR[4:0])
124 * Configure the rows/cols in KPP
125 * LSB nybble in KPP is for 5 rows
126 * MSB nybble in KPP is for 3 cols */
127 KPP_KPCR |= 0x1f;
129 /* 2. Write 0's to KPDR[10:8] */
130 KPP_KPDR &= ~(0x7 << 8);
132 /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */
133 KPP_KPCR |= (0x7 << 8);
135 /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */
136 KPP_KDDR = (KPP_KDDR | (0x7 << 8)) & ~0x1f;
138 /* 5. Clear the KPKD Status Flag and Synchronizer chain.
139 * 6. Set the KDIE control bit, and set the KRIE control
140 * bit (to force immediate scan). */
141 KPP_KPSR = KPP_KPSR_KRIE | KPP_KPSR_KDIE | KPP_KPSR_KRSS |
142 KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
144 /* KPP IRQ at priority 3 */
145 avic_enable_int(KPP, IRQ, 3, KPP_HANDLER);
148 bool button_hold(void)
150 return _button_hold();
153 int button_read_device(void)
155 /* Simple poll of GPIO status */
156 hold_button = _button_hold();
158 /* Backlight hold handling */
159 if (hold_button != hold_button_old)
161 hold_button_old = hold_button;
162 backlight_hold_changed(hold_button);
165 /* Enable the keypad interrupt to cause it to fire if a key is down.
166 * KPP_HANDLER will clear and disable it after the scan. If no key
167 * is depressed then this bit will already be set in waiting for the
168 * first key down event. */
169 KPP_KPSR |= KPP_KPSR_KDIE;
171 /* If hold, ignore any pressed button */
172 return hold_button ? BUTTON_NONE : int_btn;
175 /* This is called from the mc13783 interrupt thread */
176 void button_power_set_state(bool pressed)
178 /* Prevent KPP_HANDLER from changing things */
179 int oldlevel = disable_irq_save();
181 if (pressed)
183 int_btn |= BUTTON_POWER;
185 else
187 int_btn &= ~BUTTON_POWER;
190 restore_irq(oldlevel);
193 /* This is called from the mc13783 interrupt thread */
194 void set_headphones_inserted(bool inserted)
196 headphones_detect = inserted;
199 /* This is called from the mc13783 interrupt thread */
200 /* TODO: Just do a post to the button queue directly - implement the
201 * appropriate variant in the driver. */
202 bool headphones_inserted(void)
204 return headphones_detect;