Oops. Tiny goof.
[kugel-rb.git] / firmware / target / arm / imx31 / gigabeat-s / button-imx31.c
blob602f41abb9f6cdbb3508be868603fe6f3b02ffc8
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"
29 /* Most code in here is taken from the Linux BSP provided by Freescale
30 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */
32 static uint32_t int_btn = BUTTON_NONE;
33 static bool hold_button = false;
34 static bool hold_button_old = false;
35 #define _button_hold() (GPIO3_DR & 0x10)
37 static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void)
39 static const int key_mtx[5][3] =
41 { BUTTON_LEFT, BUTTON_BACK, BUTTON_VOL_UP },
42 { BUTTON_UP, BUTTON_MENU, BUTTON_VOL_DOWN },
43 { BUTTON_DOWN, BUTTON_NONE, BUTTON_PREV },
44 { BUTTON_RIGHT, BUTTON_NONE, BUTTON_PLAY },
45 { BUTTON_SELECT, BUTTON_NONE, BUTTON_NEXT },
48 unsigned short reg_val;
49 int col, row;
50 int i;
51 int button = BUTTON_NONE;
53 /* 1. Disable both (depress and release) keypad interrupts. */
54 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
56 for (col = 0; col < 3; col++) /* Col */
58 /* 2. Write 1s to KPDR[10:8] setting column data to 1s */
59 KPP_KPDR |= (0x7 << 8);
61 /* 3. Configure columns as totem pole outputs(for quick
62 * discharging of keypad capacitance) */
63 KPP_KPCR &= ~(0x7 << 8);
65 /* Give the columns time to discharge */
66 for (i = 0; i < 256; i++)
67 asm volatile ("");
69 /* 4. Configure columns as open-drain */
70 KPP_KPCR |= (0x7 << 8);
72 /* 5. Write a single column to 0, others to 1.
73 * 6. Sample row inputs and save data. Multiple key presses
74 * can be detected on a single column.
75 * 7. Repeat steps 2 - 6 for remaining columns. */
77 /* Col bit starts at 8th bit in KPDR */
78 KPP_KPDR &= ~(1 << (8 + col));
80 /* Delay added to avoid propagating the 0 from column to row
81 * when scanning. */
82 for (i = 0; i < 256; i++)
83 asm volatile ("");
85 /* Read row input */
86 reg_val = KPP_KPDR;
87 for (row = 0; row < 5; row++) /* sample row */
89 if (!(reg_val & (1 << row)))
90 button |= key_mtx[row][col];
94 /* 8. Return all columns to 0 in preparation for standby mode. */
95 KPP_KPDR &= ~(0x7 << 8);
97 /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
98 * set the KPKR synchronizer chain by writing "1" to KRSS register,
99 * clear the KPKD synchronizer chain by writing "1" to KDSC register */
100 KPP_KPSR = KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
102 /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE
103 * detects a key hold condition, or the KRIE detects a key-release
104 * event. */
105 if (button != BUTTON_NONE)
106 KPP_KPSR |= KPP_KPSR_KRIE;
107 else
108 KPP_KPSR |= KPP_KPSR_KDIE;
110 int_btn = button;
113 void button_init_device(void)
115 /* Enable keypad clock */
116 CLKCTL_CGR1 |= (3 << 2*10);
118 /* 1. Enable number of rows in keypad (KPCR[4:0])
120 * Configure the rows/cols in KPP
121 * LSB nybble in KPP is for 5 rows
122 * MSB nybble in KPP is for 3 cols */
123 KPP_KPCR |= 0x1f;
125 /* 2. Write 0's to KPDR[10:8] */
126 KPP_KPDR &= ~(0x7 << 8);
128 /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */
129 KPP_KPCR |= (0x7 << 8);
131 /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */
132 KPP_KDDR = (KPP_KDDR | (0x7 << 8)) & ~0x1f;
134 /* 5. Clear the KPKD Status Flag and Synchronizer chain.
135 * 6. Set the KDIE control bit, and set the KRIE control
136 * bit (to force immediate scan). */
137 KPP_KPSR = KPP_KPSR_KRIE | KPP_KPSR_KDIE | KPP_KPSR_KRSS |
138 KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
140 /* KPP IRQ at priority 3 */
141 avic_enable_int(KPP, IRQ, 3, KPP_HANDLER);
144 bool button_hold(void)
146 return _button_hold();
149 int button_read_device(void)
151 /* Simple poll of GPIO status */
152 hold_button = _button_hold();
154 /* Backlight hold handling */
155 if (hold_button != hold_button_old)
157 hold_button_old = hold_button;
158 backlight_hold_changed(hold_button);
161 /* If hold, ignore any pressed button */
162 return hold_button ? BUTTON_NONE : int_btn;