blob: 7c432450c2e892ba98f01846c1be9fb10ae0dcb6 [file] [log] [blame]
Will Robertson590501c2007-09-21 15:51:53 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
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.
Will Robertson590501c2007-09-21 15:51:53 +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#include "config.h"
23#include "cpu.h"
24#include "system.h"
25#include "button.h"
Will Robertson590501c2007-09-21 15:51:53 +000026#include "backlight.h"
Will Robertson590501c2007-09-21 15:51:53 +000027#include "system.h"
28#include "backlight-target.h"
Michael Sevakis02de55c2008-02-09 07:59:53 +000029#include "avic-imx31.h"
Nils Wallméniusf24eaab2008-05-08 16:27:23 +000030#include "clkctl-imx31.h"
Michael Sevakisa9c20f52008-05-21 08:42:11 +000031#include "mc13783.h"
Will Robertson590501c2007-09-21 15:51:53 +000032
33/* Most code in here is taken from the Linux BSP provided by Freescale
34 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */
Michael Sevakis80278e42008-05-10 18:00:11 +000035#ifdef HAVE_HEADPHONE_DETECTION
Michael Sevakisa7af9e42008-04-12 16:56:45 +000036static bool headphones_detect = false;
Michael Sevakis80278e42008-05-10 18:00:11 +000037#endif
Michael Sevakisa7af9e42008-04-12 16:56:45 +000038static uint32_t int_btn = BUTTON_NONE;
Michael Sevakis02de55c2008-02-09 07:59:53 +000039static bool hold_button = false;
Michael Sevakis80278e42008-05-10 18:00:11 +000040#ifdef BOOTLOADER
41static bool initialized = false;
42#else
Michael Sevakis02de55c2008-02-09 07:59:53 +000043static bool hold_button_old = false;
Michael Sevakis80278e42008-05-10 18:00:11 +000044#endif
Michael Sevakis02de55c2008-02-09 07:59:53 +000045#define _button_hold() (GPIO3_DR & 0x10)
46
47static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void)
Will Robertson590501c2007-09-21 15:51:53 +000048{
Michael Sevakis6df1a862008-05-09 21:19:58 +000049 static const struct key_mask_shift
Michael Sevakis02de55c2008-02-09 07:59:53 +000050 {
Michael Sevakis6df1a862008-05-09 21:19:58 +000051 uint8_t mask;
52 uint8_t shift;
53 } kms[3] =
54 {
55 { 0x1f, 0 }, /* BUTTON_LEFT...BUTTON_SELECT */
56 { 0x03, 5 }, /* BUTTON_BACK...BUTTON_MENU */
57 { 0x1f, 7 }, /* BUTTON_VOL_UP...BUTTON_NEXT */
Michael Sevakis02de55c2008-02-09 07:59:53 +000058 };
Will Robertson590501c2007-09-21 15:51:53 +000059
Michael Sevakis6df1a862008-05-09 21:19:58 +000060 int col;
Michael Sevakisa7af9e42008-04-12 16:56:45 +000061 /* Power button is handled separately on PMIC */
62 int button = int_btn & BUTTON_POWER;
Will Robertson590501c2007-09-21 15:51:53 +000063
Michael Sevakis45786ca2008-04-24 20:18:41 +000064 int oldlevel = disable_irq_save();
65
Michael Sevakis02de55c2008-02-09 07:59:53 +000066 /* 1. Disable both (depress and release) keypad interrupts. */
67 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
68
69 for (col = 0; col < 3; col++) /* Col */
70 {
Michael Sevakis6df1a862008-05-09 21:19:58 +000071 int i;
72
Michael Sevakis02de55c2008-02-09 07:59:53 +000073 /* 2. Write 1s to KPDR[10:8] setting column data to 1s */
74 KPP_KPDR |= (0x7 << 8);
75
76 /* 3. Configure columns as totem pole outputs(for quick
77 * discharging of keypad capacitance) */
78 KPP_KPCR &= ~(0x7 << 8);
79
80 /* Give the columns time to discharge */
Michael Sevakis45786ca2008-04-24 20:18:41 +000081 for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */
Michael Sevakis02de55c2008-02-09 07:59:53 +000082 asm volatile ("");
83
84 /* 4. Configure columns as open-drain */
85 KPP_KPCR |= (0x7 << 8);
86
87 /* 5. Write a single column to 0, others to 1.
88 * 6. Sample row inputs and save data. Multiple key presses
89 * can be detected on a single column.
90 * 7. Repeat steps 2 - 6 for remaining columns. */
91
92 /* Col bit starts at 8th bit in KPDR */
Michael Sevakis6df1a862008-05-09 21:19:58 +000093 KPP_KPDR &= ~(0x100 << col);
Michael Sevakis02de55c2008-02-09 07:59:53 +000094
95 /* Delay added to avoid propagating the 0 from column to row
96 * when scanning. */
Michael Sevakis45786ca2008-04-24 20:18:41 +000097 for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */
Michael Sevakis02de55c2008-02-09 07:59:53 +000098 asm volatile ("");
99
100 /* Read row input */
Michael Sevakis6df1a862008-05-09 21:19:58 +0000101 button |= (~KPP_KPDR & kms[col].mask) << kms[col].shift;
Michael Sevakis02de55c2008-02-09 07:59:53 +0000102 }
103
104 /* 8. Return all columns to 0 in preparation for standby mode. */
105 KPP_KPDR &= ~(0x7 << 8);
106
107 /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
108 * set the KPKR synchronizer chain by writing "1" to KRSS register,
109 * clear the KPKD synchronizer chain by writing "1" to KDSC register */
110 KPP_KPSR = KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKR | KPP_KPSR_KPKD;
111
112 /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE
113 * detects a key hold condition, or the KRIE detects a key-release
114 * event. */
Michael Sevakis45786ca2008-04-24 20:18:41 +0000115 if ((button & ~BUTTON_POWER) != BUTTON_NONE)
Michael Sevakis02de55c2008-02-09 07:59:53 +0000116 KPP_KPSR |= KPP_KPSR_KRIE;
117 else
118 KPP_KPSR |= KPP_KPSR_KDIE;
119
Michael Sevakis45786ca2008-04-24 20:18:41 +0000120 restore_irq(oldlevel);
121
Michael Sevakis02de55c2008-02-09 07:59:53 +0000122 int_btn = button;
Will Robertson590501c2007-09-21 15:51:53 +0000123}
124
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000125bool button_hold(void)
126{
127 return _button_hold();
128}
129
130int button_read_device(void)
131{
132 /* Simple poll of GPIO status */
133 hold_button = _button_hold();
134
135#ifndef BOOTLOADER
136 /* Backlight hold handling */
137 if (hold_button != hold_button_old)
138 {
139 hold_button_old = hold_button;
140 backlight_hold_changed(hold_button);
141 }
142#endif
143
144 /* Enable the keypad interrupt to cause it to fire if a key is down.
145 * KPP_HANDLER will clear and disable it after the scan. If no key
146 * is depressed then this bit will already be set in waiting for the
147 * first key down event. */
148 KPP_KPSR |= KPP_KPSR_KDIE;
149
150 /* If hold, ignore any pressed button */
151 return hold_button ? BUTTON_NONE : int_btn;
152}
153
154/* This is called from the mc13783 interrupt thread */
155void button_power_event(void)
156{
157 bool pressed =
158 (mc13783_read(MC13783_INTERRUPT_SENSE1) & MC13783_ONOFD1S) == 0;
159
160 /* Prevent KPP_HANDLER from changing things */
161 int oldlevel = disable_irq_save();
162
163 if (pressed)
164 {
165 int_btn |= BUTTON_POWER;
166 }
167 else
168 {
169 int_btn &= ~BUTTON_POWER;
170 }
171
172 restore_irq(oldlevel);
173}
174
175#ifdef HAVE_HEADPHONE_DETECTION
176/* This is called from the mc13783 interrupt thread */
177void headphone_detect_event(void)
178{
179 /* FIXME: Not really the correct method */
180 headphones_detect =
181 (mc13783_read(MC13783_INTERRUPT_SENSE1) & MC13783_ONOFD2S) == 0;
182}
183
184bool headphones_inserted(void)
185{
186 return headphones_detect;
187}
188#endif /* HAVE_HEADPHONE_DETECTION */
189
Michael Sevakis02de55c2008-02-09 07:59:53 +0000190void button_init_device(void)
Will Robertson590501c2007-09-21 15:51:53 +0000191{
Michael Sevakis80278e42008-05-10 18:00:11 +0000192#ifdef BOOTLOADER
193 /* Can be called more than once in the bootloader */
194 if (initialized)
195 return;
196
197 initialized = true;
198#endif
199
Michael Sevakis02de55c2008-02-09 07:59:53 +0000200 /* Enable keypad clock */
Nils Wallméniusf24eaab2008-05-08 16:27:23 +0000201 imx31_clkctl_module_clock_gating(CG_KPP, CGM_ON_ALL);
Michael Sevakis02de55c2008-02-09 07:59:53 +0000202
203 /* 1. Enable number of rows in keypad (KPCR[4:0])
204 *
205 * Configure the rows/cols in KPP
206 * LSB nybble in KPP is for 5 rows
207 * MSB nybble in KPP is for 3 cols */
208 KPP_KPCR |= 0x1f;
209
210 /* 2. Write 0's to KPDR[10:8] */
211 KPP_KPDR &= ~(0x7 << 8);
212
213 /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */
214 KPP_KPCR |= (0x7 << 8);
215
216 /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */
217 KPP_KDDR = (KPP_KDDR | (0x7 << 8)) & ~0x1f;
218
219 /* 5. Clear the KPKD Status Flag and Synchronizer chain.
Michael Sevakis80278e42008-05-10 18:00:11 +0000220 * 6. Set the KDIE control bit bit. */
221 KPP_KPSR = KPP_KPSR_KDIE | KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKD;
Michael Sevakis02de55c2008-02-09 07:59:53 +0000222
223 /* KPP IRQ at priority 3 */
224 avic_enable_int(KPP, IRQ, 3, KPP_HANDLER);
Michael Sevakisa9c20f52008-05-21 08:42:11 +0000225
226 button_power_event();
227 mc13783_enable_event(MC13783_ONOFD1_EVENT);
228
229#ifdef HAVE_HEADPHONE_DETECTION
230 headphone_detect_event();
231 mc13783_enable_event(MC13783_ONOFD2_EVENT);
232#endif
Michael Sevakis02de55c2008-02-09 07:59:53 +0000233}
234
Michael Sevakis80278e42008-05-10 18:00:11 +0000235#ifdef BUTTON_DRIVER_CLOSE
236void button_close_device(void)
237{
238 int oldlevel = disable_irq_save();
239
240 avic_disable_int(KPP);
241 KPP_KPSR &= ~(KPP_KPSR_KRIE | KPP_KPSR_KDIE);
242 int_btn = BUTTON_NONE;
243
244 restore_irq(oldlevel);
245}
246#endif /* BUTTON_DRIVER_CLOSE */