1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
26 #include "backlight.h"
28 #include "backlight-target.h"
29 #include "avic-imx31.h"
30 #include "clkctl-imx31.h"
33 /* Most code in here is taken from the Linux BSP provided by Freescale
34 * Copyright 2004-2006 Freescale Semiconductor, Inc. All Rights Reserved. */
35 #ifdef HAVE_HEADPHONE_DETECTION
36 static bool headphones_detect
= false;
38 static uint32_t int_btn
= BUTTON_NONE
;
39 static bool hold_button
= false;
41 static bool initialized
= false;
43 static bool hold_button_old
= false;
45 #define _button_hold() (GPIO3_DR & 0x10)
47 static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void)
49 static const struct key_mask_shift
55 { 0x1f, 0 }, /* BUTTON_LEFT...BUTTON_SELECT */
56 { 0x03, 5 }, /* BUTTON_BACK...BUTTON_MENU */
57 { 0x1f, 7 }, /* BUTTON_VOL_UP...BUTTON_NEXT */
61 /* Power button is handled separately on PMIC */
62 int button
= int_btn
& BUTTON_POWER
;
64 int oldlevel
= disable_irq_save();
66 /* 1. Disable both (depress and release) keypad interrupts. */
67 KPP_KPSR
&= ~(KPP_KPSR_KRIE
| KPP_KPSR_KDIE
);
69 for (col
= 0; col
< 3; col
++) /* Col */
71 /* 2. Write 1s to KPDR[10:8] setting column data to 1s */
72 KPP_KPDR
|= (0x7 << 8);
74 /* 3. Configure columns as totem pole outputs(for quick
75 * discharging of keypad capacitance) */
76 KPP_KPCR
&= ~(0x7 << 8);
78 /* Give the columns time to discharge */
81 /* 4. Configure columns as open-drain */
82 KPP_KPCR
|= (0x7 << 8);
84 /* 5. Write a single column to 0, others to 1.
85 * 6. Sample row inputs and save data. Multiple key presses
86 * can be detected on a single column.
87 * 7. Repeat steps 2 - 6 for remaining columns. */
89 /* Col bit starts at 8th bit in KPDR */
90 KPP_KPDR
&= ~(0x100 << col
);
92 /* Delay added to avoid propagating the 0 from column to row
97 button
|= (~KPP_KPDR
& kms
[col
].mask
) << kms
[col
].shift
;
100 /* 8. Return all columns to 0 in preparation for standby mode. */
101 KPP_KPDR
&= ~(0x7 << 8);
103 /* 9. Clear KPKD and KPKR status bit(s) by writing to a .1.,
104 * set the KPKR synchronizer chain by writing "1" to KRSS register,
105 * clear the KPKD synchronizer chain by writing "1" to KDSC register */
106 KPP_KPSR
= KPP_KPSR_KRSS
| KPP_KPSR_KDSC
| KPP_KPSR_KPKR
| KPP_KPSR_KPKD
;
108 /* 10. Re-enable the appropriate keypad interrupt(s) so that the KDIE
109 * detects a key hold condition, or the KRIE detects a key-release
111 if ((button
& ~BUTTON_POWER
) != BUTTON_NONE
)
112 KPP_KPSR
|= KPP_KPSR_KRIE
;
114 KPP_KPSR
|= KPP_KPSR_KDIE
;
116 restore_irq(oldlevel
);
121 bool button_hold(void)
123 return _button_hold();
126 int button_read_device(void)
128 /* Simple poll of GPIO status */
129 hold_button
= _button_hold();
132 /* Backlight hold handling */
133 if (hold_button
!= hold_button_old
)
135 hold_button_old
= hold_button
;
136 backlight_hold_changed(hold_button
);
140 /* Enable the keypad interrupt to cause it to fire if a key is down.
141 * KPP_HANDLER will clear and disable it after the scan. If no key
142 * is depressed then this bit will already be set in waiting for the
143 * first key down event. */
144 KPP_KPSR
|= KPP_KPSR_KDIE
;
146 /* If hold, ignore any pressed button */
147 return hold_button
? BUTTON_NONE
: int_btn
;
150 /* This is called from the mc13783 interrupt thread */
151 void button_power_event(void)
154 (mc13783_read(MC13783_INTERRUPT_SENSE1
) & MC13783_ONOFD1S
) == 0;
156 /* Prevent KPP_HANDLER from changing things */
157 int oldlevel
= disable_irq_save();
161 int_btn
|= BUTTON_POWER
;
165 int_btn
&= ~BUTTON_POWER
;
168 restore_irq(oldlevel
);
171 #ifdef HAVE_HEADPHONE_DETECTION
172 /* This is called from the mc13783 interrupt thread */
173 void headphone_detect_event(void)
175 /* FIXME: Not really the correct method */
177 (mc13783_read(MC13783_INTERRUPT_SENSE1
) & MC13783_ONOFD2S
) == 0;
180 bool headphones_inserted(void)
182 return headphones_detect
;
184 #endif /* HAVE_HEADPHONE_DETECTION */
186 void button_init_device(void)
189 /* Can be called more than once in the bootloader */
196 /* Enable keypad clock */
197 imx31_clkctl_module_clock_gating(CG_KPP
, CGM_ON_ALL
);
199 /* 1. Enable number of rows in keypad (KPCR[4:0])
201 * Configure the rows/cols in KPP
202 * LSB nybble in KPP is for 5 rows
203 * MSB nybble in KPP is for 3 cols */
206 /* 2. Write 0's to KPDR[10:8] */
207 KPP_KPDR
&= ~(0x7 << 8);
209 /* 3. Configure the keypad columns as open-drain (KPCR[10:8]). */
210 KPP_KPCR
|= (0x7 << 8);
212 /* 4. Configure columns as output, rows as input (KDDR[10:8,4:0]) */
213 KPP_KDDR
= (KPP_KDDR
| (0x7 << 8)) & ~0x1f;
215 /* 5. Clear the KPKD Status Flag and Synchronizer chain.
216 * 6. Set the KDIE control bit bit. */
217 KPP_KPSR
= KPP_KPSR_KDIE
| KPP_KPSR_KRSS
| KPP_KPSR_KDSC
| KPP_KPSR_KPKD
;
219 /* KPP IRQ at priority 3 */
220 avic_enable_int(KPP
, IRQ
, 3, KPP_HANDLER
);
222 button_power_event();
223 mc13783_enable_event(MC13783_ONOFD1_EVENT
);
225 #ifdef HAVE_HEADPHONE_DETECTION
226 headphone_detect_event();
227 mc13783_enable_event(MC13783_ONOFD2_EVENT
);
231 #ifdef BUTTON_DRIVER_CLOSE
232 void button_close_device(void)
234 int oldlevel
= disable_irq_save();
236 avic_disable_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 */