Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Linus Nielsen Feltzing |
| 11 | * |
Daniel Stenberg | 2acc0ac | 2008-06-28 18:10:04 +0000 | [diff] [blame^] | 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. |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 16 | * |
| 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 Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 26 | #include "backlight.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 27 | #include "system.h" |
| 28 | #include "backlight-target.h" |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 29 | #include "avic-imx31.h" |
Nils Wallménius | f24eaab | 2008-05-08 16:27:23 +0000 | [diff] [blame] | 30 | #include "clkctl-imx31.h" |
Michael Sevakis | a9c20f5 | 2008-05-21 08:42:11 +0000 | [diff] [blame] | 31 | #include "mc13783.h" |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 32 | |
| 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 Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 35 | #ifdef HAVE_HEADPHONE_DETECTION |
Michael Sevakis | a7af9e4 | 2008-04-12 16:56:45 +0000 | [diff] [blame] | 36 | static bool headphones_detect = false; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 37 | #endif |
Michael Sevakis | a7af9e4 | 2008-04-12 16:56:45 +0000 | [diff] [blame] | 38 | static uint32_t int_btn = BUTTON_NONE; |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 39 | static bool hold_button = false; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 40 | #ifdef BOOTLOADER |
| 41 | static bool initialized = false; |
| 42 | #else |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 43 | static bool hold_button_old = false; |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 44 | #endif |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 45 | #define _button_hold() (GPIO3_DR & 0x10) |
| 46 | |
| 47 | static __attribute__((interrupt("IRQ"))) void KPP_HANDLER(void) |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 48 | { |
Michael Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 49 | static const struct key_mask_shift |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 50 | { |
Michael Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 51 | 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 Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 58 | }; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 59 | |
Michael Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 60 | int col; |
Michael Sevakis | a7af9e4 | 2008-04-12 16:56:45 +0000 | [diff] [blame] | 61 | /* Power button is handled separately on PMIC */ |
| 62 | int button = int_btn & BUTTON_POWER; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 63 | |
Michael Sevakis | 45786ca | 2008-04-24 20:18:41 +0000 | [diff] [blame] | 64 | int oldlevel = disable_irq_save(); |
| 65 | |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 66 | /* 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 Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 71 | int i; |
| 72 | |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 73 | /* 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 Sevakis | 45786ca | 2008-04-24 20:18:41 +0000 | [diff] [blame] | 81 | for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */ |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 82 | 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 Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 93 | KPP_KPDR &= ~(0x100 << col); |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 94 | |
| 95 | /* Delay added to avoid propagating the 0 from column to row |
| 96 | * when scanning. */ |
Michael Sevakis | 45786ca | 2008-04-24 20:18:41 +0000 | [diff] [blame] | 97 | for (i = 0; i < 128; i++) /* TODO: find minimum safe delay */ |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 98 | asm volatile (""); |
| 99 | |
| 100 | /* Read row input */ |
Michael Sevakis | 6df1a86 | 2008-05-09 21:19:58 +0000 | [diff] [blame] | 101 | button |= (~KPP_KPDR & kms[col].mask) << kms[col].shift; |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 102 | } |
| 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 Sevakis | 45786ca | 2008-04-24 20:18:41 +0000 | [diff] [blame] | 115 | if ((button & ~BUTTON_POWER) != BUTTON_NONE) |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 116 | KPP_KPSR |= KPP_KPSR_KRIE; |
| 117 | else |
| 118 | KPP_KPSR |= KPP_KPSR_KDIE; |
| 119 | |
Michael Sevakis | 45786ca | 2008-04-24 20:18:41 +0000 | [diff] [blame] | 120 | restore_irq(oldlevel); |
| 121 | |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 122 | int_btn = button; |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Michael Sevakis | a9c20f5 | 2008-05-21 08:42:11 +0000 | [diff] [blame] | 125 | bool button_hold(void) |
| 126 | { |
| 127 | return _button_hold(); |
| 128 | } |
| 129 | |
| 130 | int 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 */ |
| 155 | void 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 */ |
| 177 | void 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 | |
| 184 | bool headphones_inserted(void) |
| 185 | { |
| 186 | return headphones_detect; |
| 187 | } |
| 188 | #endif /* HAVE_HEADPHONE_DETECTION */ |
| 189 | |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 190 | void button_init_device(void) |
Will Robertson | 590501c | 2007-09-21 15:51:53 +0000 | [diff] [blame] | 191 | { |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 192 | #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 Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 200 | /* Enable keypad clock */ |
Nils Wallménius | f24eaab | 2008-05-08 16:27:23 +0000 | [diff] [blame] | 201 | imx31_clkctl_module_clock_gating(CG_KPP, CGM_ON_ALL); |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 202 | |
| 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 Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 220 | * 6. Set the KDIE control bit bit. */ |
| 221 | KPP_KPSR = KPP_KPSR_KDIE | KPP_KPSR_KRSS | KPP_KPSR_KDSC | KPP_KPSR_KPKD; |
Michael Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 222 | |
| 223 | /* KPP IRQ at priority 3 */ |
| 224 | avic_enable_int(KPP, IRQ, 3, KPP_HANDLER); |
Michael Sevakis | a9c20f5 | 2008-05-21 08:42:11 +0000 | [diff] [blame] | 225 | |
| 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 Sevakis | 02de55c | 2008-02-09 07:59:53 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Michael Sevakis | 80278e4 | 2008-05-10 18:00:11 +0000 | [diff] [blame] | 235 | #ifdef BUTTON_DRIVER_CLOSE |
| 236 | void 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 */ |