FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / button-imx31.c
blobae158b811d8cb68e32dc53c688fc0376f5232058
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * 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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "config.h"
23 #include "cpu.h"
24 #include "system.h"
25 #include "button.h"
26 #include "backlight.h"
27 #include "backlight-target.h"
28 #include "avic-imx31.h"
29 #include "ccm-imx31.h"
30 #include "mc13783.h"
32 /* Most code in here is taken from the Linux BSP provided by Freescale
33 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */
34 static uint32_t int_btn = BUTTON_NONE;
35 static bool hold_button = false;
36 #ifdef BOOTLOADER
37 static bool initialized = false;
38 #else
39 static bool hold_button_old = false;
40 #endif
41 #define _button_hold() (GPIO3_DR & 0x10)
43 static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void)
45 static const struct key_mask_shift
47 uint8_t mask;
48 uint8_t shift;
49 } kms[3] =
51 { 0x1f, 0 }, /* BUTTON_LEFT...BUTTON_SELECT */
52 { 0x03, 5 }, /* BUTTON_BACK...BUTTON_MENU */
53 { 0x1f, 7 }, /* BUTTON_VOL_UP...BUTTON_NEXT */
56 int col;
57 /* Power button is handled separately on PMIC, remote read in headphone
58 * jack driver. */
59 #ifdef HAVE_HEADPHONE_DETECTION
60 int button = int_btn & (BUTTON_POWER | BUTTON_REMOTE);
61 #else
62 int button = int_btn & BUTTON_POWER;
63 #endif
65 int oldlevel = disable_irq_save();
67 /* 1. Disable both (depress and release) keypad interrupts. */
68 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
70 for (col = 0; col < 3; col++) /* Col */
72 /* 2. Write 1s to KPDR[10:8] setting column data to 1s */
73 KPP_KPDR |= (0x7 << 8);
75 /* 3. Configure columns as totem pole outputs(for quick
76 * discharging of keypad capacitance) */
77 KPP_KPCR &= ~(0x7 << 8);
79 /* Give the columns time to discharge */
80 udelay(2);
82 /* 4. Configure columns as open-drain */
83 KPP_KPCR |= (0x7 << 8);
85 /* 5. Write a single column to 0, others to 1.
86 * 6. Sample row inputs and save data. Multiple key presses
87 * can be detected on a single column.
88 * 7. Repeat steps 2 - 6 for remaining columns. */
90 /* Col bit starts at 8th bit in KPDR */
91 KPP_KPDR &= ~(0x100 << col);
93 /* Delay added to avoid propagating the 0 from column to row
94 * when scanning. */
95 udelay(2);
97 /* Read row input */
98 button |= (~KPP_KPDR & kms[col].mask) << kms[col].shift;
101 /* 8. Return all columns to 0 in preparation for standby mode. */
102 KPP_KPDR &= ~(0x7 << 8);
104 /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
105 * set the KPKR synchronizer chain by writing "1" to KRSS register,
106 * clear the KPKD synchronizer chain by writing "1" to KDSC register */
107 KPP_KPSR = KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
109 /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE
110 * detects a key hold condition, or the KRIE detects a key-release
111 * event. */
112 if ((button & ~BUTTON_POWER) != BUTTON_NONE)
113 KPP_KPSR |= KPP_KPSR_KRIE;
114 else
115 KPP_KPSR |= KPP_KPSR_KDIE;
117 int_btn = button;
119 restore_irq(oldlevel);
122 bool button_hold(void)
124 return _button_hold();
127 #ifdef HAVE_HEADPHONE_DETECTION
128 /* Headphone driver pushes the data here */
129 void button_headphone_set(int button)
131 int oldstatus = disable_irq_save();
132 int_btn = (int_btn & ~BUTTON_REMOTE) | button;
133 restore_irq(oldstatus);
135 #endif
137 int button_read_device(void)
139 /* Simple poll of GPIO status */
140 hold_button = _button_hold();
142 #ifndef BOOTLOADER
143 /* Backlight hold handling */
144 if (hold_button != hold_button_old)
146 hold_button_old = hold_button;
147 backlight_hold_changed(hold_button);
149 #endif
151 /* Enable the keypad interrupt to cause it to fire if a key is down.
152 * KPP_HANDLER will clear and disable it after the scan. If no key
153 * is depressed then this bit will already be set in waiting for the
154 * first key down event. */
155 KPP_KPSR |= KPP_KPSR_KDIE;
157 #ifdef HAVE_HEADPHONE_DETECTION
158 /* If hold, ignore any pressed button. Remote has its own hold
159 * switch, so return state regardless. */
160 return hold_button ? (int_btn & BUTTON_REMOTE) : int_btn;
161 #else
162 /* If hold, ignore any pressed button. */
163 return hold_button ? BUTTON_NONE : int_btn;
164 #endif
167 /* This is called from the mc13783 interrupt thread */
168 void button_power_event(void)
170 bool pressed =
171 (mc13783_read(MC13783_INTERRUPT_SENSE1) & MC13783_ONOFD1S) == 0;
173 /* Prevent KPP_HANDLER from changing things */
174 int oldlevel = disable_irq_save();
176 if (pressed)
178 int_btn |= BUTTON_POWER;
180 else
182 int_btn &= ~BUTTON_POWER;
185 restore_irq(oldlevel);
188 void button_init_device(void)
190 #ifdef BOOTLOADER
191 /* Can be called more than once in the bootloader */
192 if (initialized)
193 return;
195 initialized = true;
196 #endif
198 /* Enable keypad clock */
199 ccm_module_clock_gating(CG_KPP, CGM_ON_RUN_WAIT);
201 /* 1. Enable number of rows in keypad (KPCR[4:0])
203 * Configure the rows/cols in KPP
204 * LSB nybble in KPP is for 5 rows
205 * MSB nybble in KPP is for 3 cols */
206 KPP_KPCR |= 0x1f;
208 /* 2. Write 0's to KPDR[10:8] */
209 KPP_KPDR &= ~(0x7 << 8);
211 /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */
212 KPP_KPCR |= (0x7 << 8);
214 /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */
215 KPP_KDDR = (KPP_KDDR | (0x7 << 8)) & ~0x1f;
217 /* 5. Clear the KPKD Status Flag and Synchronizer chain.
218 * 6. Set the KDIE control bit bit. */
219 KPP_KPSR = KPP_KPSR_KDIE | KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKD;
221 avic_enable_int(INT_KPP, INT_TYPE_IRQ, INT_PRIO_DEFAULT, KPP_HANDLER);
223 button_power_event();
224 mc13783_enable_event(MC13783_ONOFD1_EVENT);
226 #ifdef HAVE_HEADPHONE_DETECTION
227 headphone_init();
228 #endif
231 #ifdef BUTTON_DRIVER_CLOSE
232 void button_close_device(void)
234 int oldlevel = disable_irq_save();
236 avic_disable_int(INT_KPP);
237 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
238 int_btn = BUTTON_NONE;
240 restore_irq(oldlevel);
242 #endif /* BUTTON_DRIVER_CLOSE */