10e5f11aaSEric Miao /* 20e5f11aaSEric Miao * linux/drivers/input/keyboard/pxa27x_keypad.c 30e5f11aaSEric Miao * 40e5f11aaSEric Miao * Driver for the pxa27x matrix keyboard controller. 50e5f11aaSEric Miao * 60e5f11aaSEric Miao * Created: Feb 22, 2007 70e5f11aaSEric Miao * Author: Rodolfo Giometti <giometti@linux.it> 80e5f11aaSEric Miao * 90e5f11aaSEric Miao * Based on a previous implementations by Kevin O'Connor 100e5f11aaSEric Miao * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and 11*2f82af08SNicolas Pitre * on some suggestions by Nicolas Pitre <nico@fluxnic.net>. 120e5f11aaSEric Miao * 130e5f11aaSEric Miao * This program is free software; you can redistribute it and/or modify 140e5f11aaSEric Miao * it under the terms of the GNU General Public License version 2 as 150e5f11aaSEric Miao * published by the Free Software Foundation. 160e5f11aaSEric Miao */ 170e5f11aaSEric Miao 180e5f11aaSEric Miao 190e5f11aaSEric Miao #include <linux/kernel.h> 200e5f11aaSEric Miao #include <linux/module.h> 210e5f11aaSEric Miao #include <linux/init.h> 220e5f11aaSEric Miao #include <linux/interrupt.h> 230e5f11aaSEric Miao #include <linux/input.h> 240e5f11aaSEric Miao #include <linux/device.h> 250e5f11aaSEric Miao #include <linux/platform_device.h> 260e5f11aaSEric Miao #include <linux/clk.h> 270e5f11aaSEric Miao #include <linux/err.h> 2852ec7752SDmitry Torokhov #include <linux/input/matrix_keypad.h> 290e5f11aaSEric Miao 300e5f11aaSEric Miao #include <asm/mach/arch.h> 310e5f11aaSEric Miao #include <asm/mach/map.h> 320e5f11aaSEric Miao 33a09e64fbSRussell King #include <mach/hardware.h> 34a09e64fbSRussell King #include <mach/pxa27x_keypad.h> 359c60debdSEric Miao /* 369c60debdSEric Miao * Keypad Controller registers 379c60debdSEric Miao */ 389c60debdSEric Miao #define KPC 0x0000 /* Keypad Control register */ 399c60debdSEric Miao #define KPDK 0x0008 /* Keypad Direct Key register */ 409c60debdSEric Miao #define KPREC 0x0010 /* Keypad Rotary Encoder register */ 419c60debdSEric Miao #define KPMK 0x0018 /* Keypad Matrix Key register */ 429c60debdSEric Miao #define KPAS 0x0020 /* Keypad Automatic Scan register */ 430e5f11aaSEric Miao 449c60debdSEric Miao /* Keypad Automatic Scan Multiple Key Presser register 0-3 */ 459c60debdSEric Miao #define KPASMKP0 0x0028 469c60debdSEric Miao #define KPASMKP1 0x0030 479c60debdSEric Miao #define KPASMKP2 0x0038 489c60debdSEric Miao #define KPASMKP3 0x0040 499c60debdSEric Miao #define KPKDI 0x0048 509c60debdSEric Miao 519c60debdSEric Miao /* bit definitions */ 52688dad4fSSamuel Ortiz #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */ 53688dad4fSSamuel Ortiz #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */ 54688dad4fSSamuel Ortiz #define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */ 55d7416f9eSEric Miao 569c60debdSEric Miao #define KPC_AS (0x1 << 30) /* Automatic Scan bit */ 579c60debdSEric Miao #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */ 589c60debdSEric Miao #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */ 599c60debdSEric Miao #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */ 609c60debdSEric Miao 619c60debdSEric Miao #define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */ 629c60debdSEric Miao #define KPC_MS_ALL (0xff << 13) 639c60debdSEric Miao 649c60debdSEric Miao #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */ 659c60debdSEric Miao #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */ 669c60debdSEric Miao #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */ 679c60debdSEric Miao #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */ 689c60debdSEric Miao #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */ 699c60debdSEric Miao #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */ 709c60debdSEric Miao #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */ 719c60debdSEric Miao #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */ 729c60debdSEric Miao #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */ 739c60debdSEric Miao 7462059d9eSEric Miao #define KPDK_DKP (0x1 << 31) 7562059d9eSEric Miao #define KPDK_DK(n) ((n) & 0xff) 7662059d9eSEric Miao 779c60debdSEric Miao #define KPREC_OF1 (0x1 << 31) 789c60debdSEric Miao #define kPREC_UF1 (0x1 << 30) 799c60debdSEric Miao #define KPREC_OF0 (0x1 << 15) 809c60debdSEric Miao #define KPREC_UF0 (0x1 << 14) 819c60debdSEric Miao 829c60debdSEric Miao #define KPREC_RECOUNT0(n) ((n) & 0xff) 839c60debdSEric Miao #define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff) 849c60debdSEric Miao 859c60debdSEric Miao #define KPMK_MKP (0x1 << 31) 869c60debdSEric Miao #define KPAS_SO (0x1 << 31) 879c60debdSEric Miao #define KPASMKPx_SO (0x1 << 31) 889c60debdSEric Miao 891814db69SEric Miao #define KPAS_MUKP(n) (((n) >> 26) & 0x1f) 901814db69SEric Miao #define KPAS_RP(n) (((n) >> 4) & 0xf) 911814db69SEric Miao #define KPAS_CP(n) ((n) & 0xf) 920e5f11aaSEric Miao 931814db69SEric Miao #define KPASMKP_MKC_MASK (0xff) 941814db69SEric Miao 959c60debdSEric Miao #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off)) 969c60debdSEric Miao #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off)) 979c60debdSEric Miao 98bd96f378SDmitry Torokhov #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS) 99bd96f378SDmitry Torokhov #define MAX_KEYPAD_KEYS (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM) 1001814db69SEric Miao 1011814db69SEric Miao struct pxa27x_keypad { 1021814db69SEric Miao struct pxa27x_keypad_platform_data *pdata; 1031814db69SEric Miao 1041814db69SEric Miao struct clk *clk; 1051814db69SEric Miao struct input_dev *input_dev; 1069c60debdSEric Miao void __iomem *mmio_base; 1071814db69SEric Miao 10839ab9ddeSEric Miao int irq; 10939ab9ddeSEric Miao 110bd96f378SDmitry Torokhov unsigned short keycodes[MAX_KEYPAD_KEYS]; 111bd96f378SDmitry Torokhov int rotary_rel_code[2]; 1121814db69SEric Miao 1131814db69SEric Miao /* state row bits of each column scan */ 1141814db69SEric Miao uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS]; 11562059d9eSEric Miao uint32_t direct_key_state; 11662059d9eSEric Miao 11762059d9eSEric Miao unsigned int direct_key_mask; 1181814db69SEric Miao }; 1191814db69SEric Miao 1201814db69SEric Miao static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad) 1211814db69SEric Miao { 1221814db69SEric Miao struct pxa27x_keypad_platform_data *pdata = keypad->pdata; 1231814db69SEric Miao struct input_dev *input_dev = keypad->input_dev; 124bd96f378SDmitry Torokhov unsigned short keycode; 1251814db69SEric Miao int i; 1261814db69SEric Miao 12752ec7752SDmitry Torokhov for (i = 0; i < pdata->matrix_key_map_size; i++) { 12852ec7752SDmitry Torokhov unsigned int key = pdata->matrix_key_map[i]; 12952ec7752SDmitry Torokhov unsigned int row = KEY_ROW(key); 13052ec7752SDmitry Torokhov unsigned int col = KEY_COL(key); 131bd96f378SDmitry Torokhov unsigned int scancode = MATRIX_SCAN_CODE(row, col, 132bd96f378SDmitry Torokhov MATRIX_ROW_SHIFT); 1331814db69SEric Miao 134bd96f378SDmitry Torokhov keycode = KEY_VAL(key); 135bd96f378SDmitry Torokhov keypad->keycodes[scancode] = keycode; 136bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 1371814db69SEric Miao } 13862059d9eSEric Miao 139bd96f378SDmitry Torokhov for (i = 0; i < pdata->direct_key_num; i++) { 140bd96f378SDmitry Torokhov keycode = pdata->direct_key_map[i]; 141bd96f378SDmitry Torokhov keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode; 142bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 143bd96f378SDmitry Torokhov } 14462059d9eSEric Miao 145471637a5SAntonio Ospite if (pdata->enable_rotary0) { 14662059d9eSEric Miao if (pdata->rotary0_up_key && pdata->rotary0_down_key) { 147bd96f378SDmitry Torokhov keycode = pdata->rotary0_up_key; 148bd96f378SDmitry Torokhov keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode; 149bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 150bd96f378SDmitry Torokhov 151bd96f378SDmitry Torokhov keycode = pdata->rotary0_down_key; 152bd96f378SDmitry Torokhov keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode; 153bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 154bd96f378SDmitry Torokhov 155bd96f378SDmitry Torokhov keypad->rotary_rel_code[0] = -1; 156bd96f378SDmitry Torokhov } else { 157bd96f378SDmitry Torokhov keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; 15852ec7752SDmitry Torokhov __set_bit(pdata->rotary0_rel_code, input_dev->relbit); 159471637a5SAntonio Ospite } 160bd96f378SDmitry Torokhov } 16162059d9eSEric Miao 162471637a5SAntonio Ospite if (pdata->enable_rotary1) { 16362059d9eSEric Miao if (pdata->rotary1_up_key && pdata->rotary1_down_key) { 164bd96f378SDmitry Torokhov keycode = pdata->rotary1_up_key; 165bd96f378SDmitry Torokhov keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode; 166bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 167bd96f378SDmitry Torokhov 168bd96f378SDmitry Torokhov keycode = pdata->rotary1_down_key; 169bd96f378SDmitry Torokhov keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode; 170bd96f378SDmitry Torokhov __set_bit(keycode, input_dev->keybit); 171bd96f378SDmitry Torokhov 172bd96f378SDmitry Torokhov keypad->rotary_rel_code[1] = -1; 173bd96f378SDmitry Torokhov } else { 174bd96f378SDmitry Torokhov keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; 17552ec7752SDmitry Torokhov __set_bit(pdata->rotary1_rel_code, input_dev->relbit); 1761814db69SEric Miao } 177471637a5SAntonio Ospite } 1781814db69SEric Miao 179bd96f378SDmitry Torokhov __clear_bit(KEY_RESERVED, input_dev->keybit); 1801814db69SEric Miao } 1811814db69SEric Miao 1821814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad) 1831814db69SEric Miao { 1841814db69SEric Miao struct pxa27x_keypad_platform_data *pdata = keypad->pdata; 185bd96f378SDmitry Torokhov struct input_dev *input_dev = keypad->input_dev; 1861814db69SEric Miao int row, col, num_keys_pressed = 0; 1871814db69SEric Miao uint32_t new_state[MAX_MATRIX_KEY_COLS]; 1889c60debdSEric Miao uint32_t kpas = keypad_readl(KPAS); 1891814db69SEric Miao 1901814db69SEric Miao num_keys_pressed = KPAS_MUKP(kpas); 1911814db69SEric Miao 1921814db69SEric Miao memset(new_state, 0, sizeof(new_state)); 1931814db69SEric Miao 1941814db69SEric Miao if (num_keys_pressed == 0) 1951814db69SEric Miao goto scan; 1961814db69SEric Miao 1971814db69SEric Miao if (num_keys_pressed == 1) { 1981814db69SEric Miao col = KPAS_CP(kpas); 1991814db69SEric Miao row = KPAS_RP(kpas); 2001814db69SEric Miao 2011814db69SEric Miao /* if invalid row/col, treat as no key pressed */ 2021814db69SEric Miao if (col >= pdata->matrix_key_cols || 2031814db69SEric Miao row >= pdata->matrix_key_rows) 2041814db69SEric Miao goto scan; 2051814db69SEric Miao 2061814db69SEric Miao new_state[col] = (1 << row); 2071814db69SEric Miao goto scan; 2081814db69SEric Miao } 2091814db69SEric Miao 2101814db69SEric Miao if (num_keys_pressed > 1) { 2119c60debdSEric Miao uint32_t kpasmkp0 = keypad_readl(KPASMKP0); 2129c60debdSEric Miao uint32_t kpasmkp1 = keypad_readl(KPASMKP1); 2139c60debdSEric Miao uint32_t kpasmkp2 = keypad_readl(KPASMKP2); 2149c60debdSEric Miao uint32_t kpasmkp3 = keypad_readl(KPASMKP3); 2151814db69SEric Miao 2161814db69SEric Miao new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK; 2171814db69SEric Miao new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK; 2181814db69SEric Miao new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK; 2191814db69SEric Miao new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK; 2201814db69SEric Miao new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK; 2211814db69SEric Miao new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK; 2221814db69SEric Miao new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK; 2231814db69SEric Miao new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK; 2241814db69SEric Miao } 2251814db69SEric Miao scan: 2261814db69SEric Miao for (col = 0; col < pdata->matrix_key_cols; col++) { 2271814db69SEric Miao uint32_t bits_changed; 228bd96f378SDmitry Torokhov int code; 2291814db69SEric Miao 2301814db69SEric Miao bits_changed = keypad->matrix_key_state[col] ^ new_state[col]; 2311814db69SEric Miao if (bits_changed == 0) 2321814db69SEric Miao continue; 2331814db69SEric Miao 2341814db69SEric Miao for (row = 0; row < pdata->matrix_key_rows; row++) { 2351814db69SEric Miao if ((bits_changed & (1 << row)) == 0) 2361814db69SEric Miao continue; 2371814db69SEric Miao 238bd96f378SDmitry Torokhov code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT); 239bd96f378SDmitry Torokhov input_event(input_dev, EV_MSC, MSC_SCAN, code); 240bd96f378SDmitry Torokhov input_report_key(input_dev, keypad->keycodes[code], 2411814db69SEric Miao new_state[col] & (1 << row)); 2421814db69SEric Miao } 2431814db69SEric Miao } 244bd96f378SDmitry Torokhov input_sync(input_dev); 2451814db69SEric Miao memcpy(keypad->matrix_key_state, new_state, sizeof(new_state)); 2461814db69SEric Miao } 2470e5f11aaSEric Miao 248d7416f9eSEric Miao #define DEFAULT_KPREC (0x007f007f) 249d7416f9eSEric Miao 25062059d9eSEric Miao static inline int rotary_delta(uint32_t kprec) 25162059d9eSEric Miao { 25262059d9eSEric Miao if (kprec & KPREC_OF0) 25362059d9eSEric Miao return (kprec & 0xff) + 0x7f; 25462059d9eSEric Miao else if (kprec & KPREC_UF0) 25562059d9eSEric Miao return (kprec & 0xff) - 0x7f - 0xff; 25662059d9eSEric Miao else 25762059d9eSEric Miao return (kprec & 0xff) - 0x7f; 25862059d9eSEric Miao } 25962059d9eSEric Miao 26062059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta) 26162059d9eSEric Miao { 26262059d9eSEric Miao struct input_dev *dev = keypad->input_dev; 26362059d9eSEric Miao 26462059d9eSEric Miao if (delta == 0) 26562059d9eSEric Miao return; 26662059d9eSEric Miao 267bd96f378SDmitry Torokhov if (keypad->rotary_rel_code[r] == -1) { 268bd96f378SDmitry Torokhov int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1); 269bd96f378SDmitry Torokhov unsigned char keycode = keypad->keycodes[code]; 27062059d9eSEric Miao 27162059d9eSEric Miao /* simulate a press-n-release */ 272bd96f378SDmitry Torokhov input_event(dev, EV_MSC, MSC_SCAN, code); 27362059d9eSEric Miao input_report_key(dev, keycode, 1); 27462059d9eSEric Miao input_sync(dev); 275bd96f378SDmitry Torokhov input_event(dev, EV_MSC, MSC_SCAN, code); 27662059d9eSEric Miao input_report_key(dev, keycode, 0); 27762059d9eSEric Miao input_sync(dev); 27862059d9eSEric Miao } else { 27962059d9eSEric Miao input_report_rel(dev, keypad->rotary_rel_code[r], delta); 28062059d9eSEric Miao input_sync(dev); 28162059d9eSEric Miao } 28262059d9eSEric Miao } 28362059d9eSEric Miao 28462059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad) 28562059d9eSEric Miao { 28662059d9eSEric Miao struct pxa27x_keypad_platform_data *pdata = keypad->pdata; 28762059d9eSEric Miao uint32_t kprec; 28862059d9eSEric Miao 28962059d9eSEric Miao /* read and reset to default count value */ 2909c60debdSEric Miao kprec = keypad_readl(KPREC); 2919c60debdSEric Miao keypad_writel(KPREC, DEFAULT_KPREC); 29262059d9eSEric Miao 29362059d9eSEric Miao if (pdata->enable_rotary0) 29462059d9eSEric Miao report_rotary_event(keypad, 0, rotary_delta(kprec)); 29562059d9eSEric Miao 29662059d9eSEric Miao if (pdata->enable_rotary1) 29762059d9eSEric Miao report_rotary_event(keypad, 1, rotary_delta(kprec >> 16)); 29862059d9eSEric Miao } 29962059d9eSEric Miao 30062059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad) 30162059d9eSEric Miao { 30262059d9eSEric Miao struct pxa27x_keypad_platform_data *pdata = keypad->pdata; 303bd96f378SDmitry Torokhov struct input_dev *input_dev = keypad->input_dev; 30462059d9eSEric Miao unsigned int new_state; 30562059d9eSEric Miao uint32_t kpdk, bits_changed; 30662059d9eSEric Miao int i; 30762059d9eSEric Miao 3089c60debdSEric Miao kpdk = keypad_readl(KPDK); 30962059d9eSEric Miao 31062059d9eSEric Miao if (pdata->enable_rotary0 || pdata->enable_rotary1) 31162059d9eSEric Miao pxa27x_keypad_scan_rotary(keypad); 31262059d9eSEric Miao 31362059d9eSEric Miao new_state = KPDK_DK(kpdk) & keypad->direct_key_mask; 31462059d9eSEric Miao bits_changed = keypad->direct_key_state ^ new_state; 31562059d9eSEric Miao 31662059d9eSEric Miao if (bits_changed == 0) 31762059d9eSEric Miao return; 31862059d9eSEric Miao 31962059d9eSEric Miao for (i = 0; i < pdata->direct_key_num; i++) { 320bd96f378SDmitry Torokhov if (bits_changed & (1 << i)) { 321bd96f378SDmitry Torokhov int code = MAX_MATRIX_KEY_NUM + i; 322bd96f378SDmitry Torokhov 323bd96f378SDmitry Torokhov input_event(input_dev, EV_MSC, MSC_SCAN, code); 324bd96f378SDmitry Torokhov input_report_key(input_dev, keypad->keycodes[code], 325bd96f378SDmitry Torokhov new_state & (1 << i)); 32662059d9eSEric Miao } 327bd96f378SDmitry Torokhov } 328bd96f378SDmitry Torokhov input_sync(input_dev); 32962059d9eSEric Miao keypad->direct_key_state = new_state; 33062059d9eSEric Miao } 33162059d9eSEric Miao 3320e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id) 3330e5f11aaSEric Miao { 3341814db69SEric Miao struct pxa27x_keypad *keypad = dev_id; 3359c60debdSEric Miao unsigned long kpc = keypad_readl(KPC); 3360e5f11aaSEric Miao 33762059d9eSEric Miao if (kpc & KPC_DI) 33862059d9eSEric Miao pxa27x_keypad_scan_direct(keypad); 3390e5f11aaSEric Miao 3401814db69SEric Miao if (kpc & KPC_MI) 3411814db69SEric Miao pxa27x_keypad_scan_matrix(keypad); 3420e5f11aaSEric Miao 3430e5f11aaSEric Miao return IRQ_HANDLED; 3440e5f11aaSEric Miao } 3450e5f11aaSEric Miao 346d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad) 347d7416f9eSEric Miao { 348d7416f9eSEric Miao struct pxa27x_keypad_platform_data *pdata = keypad->pdata; 34962059d9eSEric Miao unsigned int mask = 0, direct_key_num = 0; 350d7416f9eSEric Miao unsigned long kpc = 0; 351d7416f9eSEric Miao 352d7416f9eSEric Miao /* enable matrix keys with automatic scan */ 353d7416f9eSEric Miao if (pdata->matrix_key_rows && pdata->matrix_key_cols) { 354d7416f9eSEric Miao kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL; 355d7416f9eSEric Miao kpc |= KPC_MKRN(pdata->matrix_key_rows) | 356d7416f9eSEric Miao KPC_MKCN(pdata->matrix_key_cols); 357d7416f9eSEric Miao } 358d7416f9eSEric Miao 35962059d9eSEric Miao /* enable rotary key, debounce interval same as direct keys */ 36062059d9eSEric Miao if (pdata->enable_rotary0) { 36162059d9eSEric Miao mask |= 0x03; 36262059d9eSEric Miao direct_key_num = 2; 36362059d9eSEric Miao kpc |= KPC_REE0; 36462059d9eSEric Miao } 365d7416f9eSEric Miao 36662059d9eSEric Miao if (pdata->enable_rotary1) { 36762059d9eSEric Miao mask |= 0x0c; 36862059d9eSEric Miao direct_key_num = 4; 36962059d9eSEric Miao kpc |= KPC_REE1; 37062059d9eSEric Miao } 37162059d9eSEric Miao 37262059d9eSEric Miao if (pdata->direct_key_num > direct_key_num) 37362059d9eSEric Miao direct_key_num = pdata->direct_key_num; 37462059d9eSEric Miao 37562059d9eSEric Miao keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask; 37662059d9eSEric Miao 37762059d9eSEric Miao /* enable direct key */ 37862059d9eSEric Miao if (direct_key_num) 37962059d9eSEric Miao kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num); 38062059d9eSEric Miao 3819c60debdSEric Miao keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB); 3829c60debdSEric Miao keypad_writel(KPREC, DEFAULT_KPREC); 38376cb44e1SEric Miao keypad_writel(KPKDI, pdata->debounce_interval); 384d7416f9eSEric Miao } 385d7416f9eSEric Miao 3860e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev) 3870e5f11aaSEric Miao { 3881814db69SEric Miao struct pxa27x_keypad *keypad = input_get_drvdata(dev); 3891814db69SEric Miao 3900e5f11aaSEric Miao /* Enable unit clock */ 3911814db69SEric Miao clk_enable(keypad->clk); 392d7416f9eSEric Miao pxa27x_keypad_config(keypad); 3930e5f11aaSEric Miao 3940e5f11aaSEric Miao return 0; 3950e5f11aaSEric Miao } 3960e5f11aaSEric Miao 3970e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev) 3980e5f11aaSEric Miao { 3991814db69SEric Miao struct pxa27x_keypad *keypad = input_get_drvdata(dev); 4001814db69SEric Miao 4010e5f11aaSEric Miao /* Disable clock unit */ 4021814db69SEric Miao clk_disable(keypad->clk); 4030e5f11aaSEric Miao } 4040e5f11aaSEric Miao 4050e5f11aaSEric Miao #ifdef CONFIG_PM 406b0010911SDmitry Torokhov static int pxa27x_keypad_suspend(struct device *dev) 4070e5f11aaSEric Miao { 408b0010911SDmitry Torokhov struct platform_device *pdev = to_platform_device(dev); 4091814db69SEric Miao struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); 4100e5f11aaSEric Miao 411d7416f9eSEric Miao clk_disable(keypad->clk); 41239ab9ddeSEric Miao 41339ab9ddeSEric Miao if (device_may_wakeup(&pdev->dev)) 41439ab9ddeSEric Miao enable_irq_wake(keypad->irq); 41539ab9ddeSEric Miao 4160e5f11aaSEric Miao return 0; 4170e5f11aaSEric Miao } 4180e5f11aaSEric Miao 419b0010911SDmitry Torokhov static int pxa27x_keypad_resume(struct device *dev) 4200e5f11aaSEric Miao { 421b0010911SDmitry Torokhov struct platform_device *pdev = to_platform_device(dev); 4221814db69SEric Miao struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); 4231814db69SEric Miao struct input_dev *input_dev = keypad->input_dev; 4240e5f11aaSEric Miao 42539ab9ddeSEric Miao if (device_may_wakeup(&pdev->dev)) 42639ab9ddeSEric Miao disable_irq_wake(keypad->irq); 42739ab9ddeSEric Miao 4280e5f11aaSEric Miao mutex_lock(&input_dev->mutex); 4290e5f11aaSEric Miao 4300e5f11aaSEric Miao if (input_dev->users) { 4310e5f11aaSEric Miao /* Enable unit clock */ 4321814db69SEric Miao clk_enable(keypad->clk); 433d7416f9eSEric Miao pxa27x_keypad_config(keypad); 4340e5f11aaSEric Miao } 4350e5f11aaSEric Miao 4360e5f11aaSEric Miao mutex_unlock(&input_dev->mutex); 4370e5f11aaSEric Miao 4380e5f11aaSEric Miao return 0; 4390e5f11aaSEric Miao } 440b0010911SDmitry Torokhov 441b0010911SDmitry Torokhov static const struct dev_pm_ops pxa27x_keypad_pm_ops = { 442b0010911SDmitry Torokhov .suspend = pxa27x_keypad_suspend, 443b0010911SDmitry Torokhov .resume = pxa27x_keypad_resume, 444b0010911SDmitry Torokhov }; 4450e5f11aaSEric Miao #endif 4460e5f11aaSEric Miao 4470e5f11aaSEric Miao static int __devinit pxa27x_keypad_probe(struct platform_device *pdev) 4480e5f11aaSEric Miao { 449bd96f378SDmitry Torokhov struct pxa27x_keypad_platform_data *pdata = pdev->dev.platform_data; 4501814db69SEric Miao struct pxa27x_keypad *keypad; 4510e5f11aaSEric Miao struct input_dev *input_dev; 4529c60debdSEric Miao struct resource *res; 4539c60debdSEric Miao int irq, error; 4540e5f11aaSEric Miao 455bd96f378SDmitry Torokhov if (pdata == NULL) { 4561814db69SEric Miao dev_err(&pdev->dev, "no platform data defined\n"); 457bd96f378SDmitry Torokhov return -EINVAL; 4581814db69SEric Miao } 4591814db69SEric Miao 4609c60debdSEric Miao irq = platform_get_irq(pdev, 0); 4619c60debdSEric Miao if (irq < 0) { 4629c60debdSEric Miao dev_err(&pdev->dev, "failed to get keypad irq\n"); 463bd96f378SDmitry Torokhov return -ENXIO; 4649c60debdSEric Miao } 4659c60debdSEric Miao 4669c60debdSEric Miao res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 4679c60debdSEric Miao if (res == NULL) { 4689c60debdSEric Miao dev_err(&pdev->dev, "failed to get I/O memory\n"); 469bd96f378SDmitry Torokhov return -ENXIO; 470bd96f378SDmitry Torokhov } 471bd96f378SDmitry Torokhov 472bd96f378SDmitry Torokhov keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL); 473bd96f378SDmitry Torokhov input_dev = input_allocate_device(); 474bd96f378SDmitry Torokhov if (!keypad || !input_dev) { 475bd96f378SDmitry Torokhov dev_err(&pdev->dev, "failed to allocate memory\n"); 476bd96f378SDmitry Torokhov error = -ENOMEM; 4779c60debdSEric Miao goto failed_free; 4789c60debdSEric Miao } 4799c60debdSEric Miao 480bd96f378SDmitry Torokhov keypad->pdata = pdata; 481bd96f378SDmitry Torokhov keypad->input_dev = input_dev; 482bd96f378SDmitry Torokhov keypad->irq = irq; 483bd96f378SDmitry Torokhov 48452ec7752SDmitry Torokhov res = request_mem_region(res->start, resource_size(res), pdev->name); 4859c60debdSEric Miao if (res == NULL) { 4869c60debdSEric Miao dev_err(&pdev->dev, "failed to request I/O memory\n"); 4879c60debdSEric Miao error = -EBUSY; 4889c60debdSEric Miao goto failed_free; 4899c60debdSEric Miao } 4909c60debdSEric Miao 49152ec7752SDmitry Torokhov keypad->mmio_base = ioremap(res->start, resource_size(res)); 4929c60debdSEric Miao if (keypad->mmio_base == NULL) { 4939c60debdSEric Miao dev_err(&pdev->dev, "failed to remap I/O memory\n"); 4949c60debdSEric Miao error = -ENXIO; 4959c60debdSEric Miao goto failed_free_mem; 4969c60debdSEric Miao } 4979c60debdSEric Miao 498e0d8b13aSRussell King keypad->clk = clk_get(&pdev->dev, NULL); 4991814db69SEric Miao if (IS_ERR(keypad->clk)) { 5001814db69SEric Miao dev_err(&pdev->dev, "failed to get keypad clock\n"); 5011814db69SEric Miao error = PTR_ERR(keypad->clk); 5029c60debdSEric Miao goto failed_free_io; 5030e5f11aaSEric Miao } 5040e5f11aaSEric Miao 5059c60debdSEric Miao input_dev->name = pdev->name; 5060e5f11aaSEric Miao input_dev->id.bustype = BUS_HOST; 5070e5f11aaSEric Miao input_dev->open = pxa27x_keypad_open; 5080e5f11aaSEric Miao input_dev->close = pxa27x_keypad_close; 5090e5f11aaSEric Miao input_dev->dev.parent = &pdev->dev; 5100e5f11aaSEric Miao 511bd96f378SDmitry Torokhov input_dev->keycode = keypad->keycodes; 512bd96f378SDmitry Torokhov input_dev->keycodesize = sizeof(keypad->keycodes[0]); 513bd96f378SDmitry Torokhov input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); 514bd96f378SDmitry Torokhov 5151814db69SEric Miao input_set_drvdata(input_dev, keypad); 5161814db69SEric Miao 517471637a5SAntonio Ospite input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); 518bd96f378SDmitry Torokhov input_set_capability(input_dev, EV_MSC, MSC_SCAN); 5191814db69SEric Miao 5201814db69SEric Miao pxa27x_keypad_build_keycode(keypad); 521bd96f378SDmitry Torokhov 522bd96f378SDmitry Torokhov if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) || 523bd96f378SDmitry Torokhov (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) { 524bd96f378SDmitry Torokhov input_dev->evbit[0] |= BIT_MASK(EV_REL); 525bd96f378SDmitry Torokhov } 5260e5f11aaSEric Miao 5279c60debdSEric Miao error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED, 5289c60debdSEric Miao pdev->name, keypad); 5299c60debdSEric Miao if (error) { 5309c60debdSEric Miao dev_err(&pdev->dev, "failed to request IRQ\n"); 531bd96f378SDmitry Torokhov goto failed_put_clk; 5329c60debdSEric Miao } 5339c60debdSEric Miao 5340e5f11aaSEric Miao /* Register the input device */ 5350e5f11aaSEric Miao error = input_register_device(input_dev); 5369c60debdSEric Miao if (error) { 5379c60debdSEric Miao dev_err(&pdev->dev, "failed to register input device\n"); 5389c60debdSEric Miao goto failed_free_irq; 5399c60debdSEric Miao } 5400e5f11aaSEric Miao 541bd96f378SDmitry Torokhov platform_set_drvdata(pdev, keypad); 54239ab9ddeSEric Miao device_init_wakeup(&pdev->dev, 1); 54339ab9ddeSEric Miao 5440e5f11aaSEric Miao return 0; 5450e5f11aaSEric Miao 5469c60debdSEric Miao failed_free_irq: 5479c60debdSEric Miao free_irq(irq, pdev); 5481814db69SEric Miao failed_put_clk: 5491814db69SEric Miao clk_put(keypad->clk); 5509c60debdSEric Miao failed_free_io: 5519c60debdSEric Miao iounmap(keypad->mmio_base); 5529c60debdSEric Miao failed_free_mem: 55352ec7752SDmitry Torokhov release_mem_region(res->start, resource_size(res)); 5541814db69SEric Miao failed_free: 555bd96f378SDmitry Torokhov input_free_device(input_dev); 5561814db69SEric Miao kfree(keypad); 5570e5f11aaSEric Miao return error; 5580e5f11aaSEric Miao } 5590e5f11aaSEric Miao 5600e5f11aaSEric Miao static int __devexit pxa27x_keypad_remove(struct platform_device *pdev) 5610e5f11aaSEric Miao { 5621814db69SEric Miao struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); 5639c60debdSEric Miao struct resource *res; 5640e5f11aaSEric Miao 56539ab9ddeSEric Miao free_irq(keypad->irq, pdev); 5661814db69SEric Miao clk_put(keypad->clk); 5671814db69SEric Miao 5681814db69SEric Miao input_unregister_device(keypad->input_dev); 5699c60debdSEric Miao input_free_device(keypad->input_dev); 5709c60debdSEric Miao 5719c60debdSEric Miao iounmap(keypad->mmio_base); 5729c60debdSEric Miao 5739c60debdSEric Miao res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 57452ec7752SDmitry Torokhov release_mem_region(res->start, resource_size(res)); 5751814db69SEric Miao 5761814db69SEric Miao platform_set_drvdata(pdev, NULL); 5771814db69SEric Miao kfree(keypad); 578bd96f378SDmitry Torokhov 5790e5f11aaSEric Miao return 0; 5800e5f11aaSEric Miao } 5810e5f11aaSEric Miao 582d7b5247bSKay Sievers /* work with hotplug and coldplug */ 583d7b5247bSKay Sievers MODULE_ALIAS("platform:pxa27x-keypad"); 584d7b5247bSKay Sievers 5850e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = { 5860e5f11aaSEric Miao .probe = pxa27x_keypad_probe, 5870e5f11aaSEric Miao .remove = __devexit_p(pxa27x_keypad_remove), 5880e5f11aaSEric Miao .driver = { 5899c60debdSEric Miao .name = "pxa27x-keypad", 590d7b5247bSKay Sievers .owner = THIS_MODULE, 591b0010911SDmitry Torokhov #ifdef CONFIG_PM 592b0010911SDmitry Torokhov .pm = &pxa27x_keypad_pm_ops, 593b0010911SDmitry Torokhov #endif 5940e5f11aaSEric Miao }, 5950e5f11aaSEric Miao }; 5960e5f11aaSEric Miao 5970e5f11aaSEric Miao static int __init pxa27x_keypad_init(void) 5980e5f11aaSEric Miao { 5990e5f11aaSEric Miao return platform_driver_register(&pxa27x_keypad_driver); 6000e5f11aaSEric Miao } 6010e5f11aaSEric Miao 6020e5f11aaSEric Miao static void __exit pxa27x_keypad_exit(void) 6030e5f11aaSEric Miao { 6040e5f11aaSEric Miao platform_driver_unregister(&pxa27x_keypad_driver); 6050e5f11aaSEric Miao } 6060e5f11aaSEric Miao 6070e5f11aaSEric Miao module_init(pxa27x_keypad_init); 6080e5f11aaSEric Miao module_exit(pxa27x_keypad_exit); 6090e5f11aaSEric Miao 6100e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver"); 6110e5f11aaSEric Miao MODULE_LICENSE("GPL"); 612