/* * OMAP4 Keypad Driver * * Copyright (C) 2010 Texas Instruments * * Author: Abraham Arce * Initial Code: Syed Rafiuddin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #include #include #include #include #include #include #include #include /* OMAP4 registers */ #define OMAP4_KBD_REVISION 0x00 #define OMAP4_KBD_SYSCONFIG 0x10 #define OMAP4_KBD_SYSSTATUS 0x14 #define OMAP4_KBD_IRQSTATUS 0x18 #define OMAP4_KBD_IRQENABLE 0x1C #define OMAP4_KBD_WAKEUPENABLE 0x20 #define OMAP4_KBD_PENDING 0x24 #define OMAP4_KBD_CTRL 0x28 #define OMAP4_KBD_DEBOUNCINGTIME 0x2C #define OMAP4_KBD_LONGKEYTIME 0x30 #define OMAP4_KBD_TIMEOUT 0x34 #define OMAP4_KBD_STATEMACHINE 0x38 #define OMAP4_KBD_ROWINPUTS 0x3C #define OMAP4_KBD_COLUMNOUTPUTS 0x40 #define OMAP4_KBD_FULLCODE31_0 0x44 #define OMAP4_KBD_FULLCODE63_32 0x48 /* OMAP4 bit definitions */ #define OMAP4_DEF_IRQENABLE_EVENTEN BIT(0) #define OMAP4_DEF_IRQENABLE_LONGKEY BIT(1) #define OMAP4_DEF_WUP_EVENT_ENA BIT(0) #define OMAP4_DEF_WUP_LONG_KEY_ENA BIT(1) #define OMAP4_DEF_CTRL_NOSOFTMODE BIT(1) #define OMAP4_DEF_CTRL_PTV_SHIFT 2 /* OMAP4 values */ #define OMAP4_VAL_IRQDISABLE 0x0 #define OMAP4_VAL_DEBOUNCINGTIME 0x7 #define OMAP4_VAL_PVT 0x7 enum { KBD_REVISION_OMAP4 = 0, KBD_REVISION_OMAP5, }; struct omap4_keypad { struct input_dev *input; void __iomem *base; bool irq_wake_enabled; unsigned int irq; unsigned int rows; unsigned int cols; u32 reg_offset; u32 irqreg_offset; unsigned int row_shift; bool no_autorepeat; unsigned char key_state[8]; unsigned short *keymap; }; static int kbd_readl(struct omap4_keypad *keypad_data, u32 offset) { return __raw_readl(keypad_data->base + keypad_data->reg_offset + offset); } static void kbd_writel(struct omap4_keypad *keypad_data, u32 offset, u32 value) { __raw_writel(value, keypad_data->base + keypad_data->reg_offset + offset); } static int kbd_read_irqreg(struct omap4_keypad *keypad_data, u32 offset) { return __raw_readl(keypad_data->base + keypad_data->irqreg_offset + offset); } static void kbd_write_irqreg(struct omap4_keypad *keypad_data, u32 offset, u32 value) { __raw_writel(value, keypad_data->base + keypad_data->irqreg_offset + offset); } /* Interrupt handlers */ static irqreturn_t omap4_keypad_irq_handler(int irq, void *dev_id) { struct omap4_keypad *keypad_data = dev_id; if (kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)) { /* Disable interrupts */ kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, OMAP4_VAL_IRQDISABLE); return IRQ_WAKE_THREAD; } return IRQ_NONE; } static irqreturn_t omap4_keypad_irq_thread_fn(int irq, void *dev_id) { struct omap4_keypad *keypad_data = dev_id; struct input_dev *input_dev = keypad_data->input; unsigned char key_state[ARRAY_SIZE(keypad_data->key_state)]; unsigned int col, row, code, changed; u32 *new_state = (u32 *) key_state; *new_state = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE31_0); *(new_state + 1) = kbd_readl(keypad_data, OMAP4_KBD_FULLCODE63_32); for (row = 0; row < keypad_data->rows; row++) { changed = key_state[row] ^ keypad_data->key_state[row]; if (!changed) continue; for (col = 0; col < keypad_data->cols; col++) { if (changed & (1 << col)) { code = MATRIX_SCAN_CODE(row, col, keypad_data->row_shift); input_event(input_dev, EV_MSC, MSC_SCAN, code); input_report_key(input_dev, keypad_data->keymap[code], key_state[row] & (1 << col)); } } } input_sync(input_dev); memcpy(keypad_data->key_state, key_state, sizeof(keypad_data->key_state)); /* clear pending interrupts */ kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); /* enable interrupts */ kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY); return IRQ_HANDLED; } static int omap4_keypad_open(struct input_dev *input) { struct omap4_keypad *keypad_data = input_get_drvdata(input); pm_runtime_get_sync(input->dev.parent); disable_irq(keypad_data->irq); kbd_writel(keypad_data, OMAP4_KBD_CTRL, OMAP4_DEF_CTRL_NOSOFTMODE | (OMAP4_VAL_PVT << OMAP4_DEF_CTRL_PTV_SHIFT)); kbd_writel(keypad_data, OMAP4_KBD_DEBOUNCINGTIME, OMAP4_VAL_DEBOUNCINGTIME); /* clear pending interrupts */ kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS, kbd_read_irqreg(keypad_data, OMAP4_KBD_IRQSTATUS)); kbd_write_irqreg(keypad_data, OMAP4_KBD_IRQENABLE, OMAP4_DEF_IRQENABLE_EVENTEN | OMAP4_DEF_IRQENABLE_LONGKEY); kbd_writel(keypad_data, OMAP4_KBD_WAKEUPENABLE, OMAP4_DEF_WUP_EVENT_ENA | OMAP4_DEF_WUP_LONG_KEY_ENA); enable_irq(keypad_data->irq); return 0; } static void omap4_keypad