xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision 9c60debd2a666dc0e8466dee556af30ea68e97d2)
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
110e5f11aaSEric Miao  * on some suggestions by Nicolas Pitre <nico@cam.org>.
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>
280e5f11aaSEric Miao 
290e5f11aaSEric Miao #include <asm/mach-types.h>
300e5f11aaSEric Miao #include <asm/mach/arch.h>
310e5f11aaSEric Miao #include <asm/mach/map.h>
320e5f11aaSEric Miao 
330e5f11aaSEric Miao #include <asm/arch/hardware.h>
340e5f11aaSEric Miao #include <asm/arch/pxa27x_keypad.h>
350e5f11aaSEric Miao 
36*9c60debdSEric Miao /*
37*9c60debdSEric Miao  * Keypad Controller registers
38*9c60debdSEric Miao  */
39*9c60debdSEric Miao #define KPC             0x0000 /* Keypad Control register */
40*9c60debdSEric Miao #define KPDK            0x0008 /* Keypad Direct Key register */
41*9c60debdSEric Miao #define KPREC           0x0010 /* Keypad Rotary Encoder register */
42*9c60debdSEric Miao #define KPMK            0x0018 /* Keypad Matrix Key register */
43*9c60debdSEric Miao #define KPAS            0x0020 /* Keypad Automatic Scan register */
440e5f11aaSEric Miao 
45*9c60debdSEric Miao /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
46*9c60debdSEric Miao #define KPASMKP0        0x0028
47*9c60debdSEric Miao #define KPASMKP1        0x0030
48*9c60debdSEric Miao #define KPASMKP2        0x0038
49*9c60debdSEric Miao #define KPASMKP3        0x0040
50*9c60debdSEric Miao #define KPKDI           0x0048
51*9c60debdSEric Miao 
52*9c60debdSEric Miao /* bit definitions */
53d7416f9eSEric Miao #define KPC_MKRN(n)	((((n) & 0x7) - 1) << 26) /* matrix key row number */
54d7416f9eSEric Miao #define KPC_MKCN(n)	((((n) & 0x7) - 1) << 23) /* matrix key column number */
55d7416f9eSEric Miao #define KPC_DKN(n)	((((n) & 0x7) - 1) << 6)  /* direct key number */
56d7416f9eSEric Miao 
57*9c60debdSEric Miao #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
58*9c60debdSEric Miao #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
59*9c60debdSEric Miao #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
60*9c60debdSEric Miao #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
61*9c60debdSEric Miao 
62*9c60debdSEric Miao #define KPC_MS(n)	(0x1 << (13 + (n)))	/* Matrix scan line 'n' */
63*9c60debdSEric Miao #define KPC_MS_ALL      (0xff << 13)
64*9c60debdSEric Miao 
65*9c60debdSEric Miao #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
66*9c60debdSEric Miao #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
67*9c60debdSEric Miao #define KPC_DK_DEB_SEL	(0x1 <<  9)  /* Direct Keypad Debounce Select */
68*9c60debdSEric Miao #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
69*9c60debdSEric Miao #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
70*9c60debdSEric Miao #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
71*9c60debdSEric Miao #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
72*9c60debdSEric Miao #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
73*9c60debdSEric Miao #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
74*9c60debdSEric Miao 
7562059d9eSEric Miao #define KPDK_DKP        (0x1 << 31)
7662059d9eSEric Miao #define KPDK_DK(n)	((n) & 0xff)
7762059d9eSEric Miao 
78*9c60debdSEric Miao #define KPREC_OF1       (0x1 << 31)
79*9c60debdSEric Miao #define kPREC_UF1       (0x1 << 30)
80*9c60debdSEric Miao #define KPREC_OF0       (0x1 << 15)
81*9c60debdSEric Miao #define KPREC_UF0       (0x1 << 14)
82*9c60debdSEric Miao 
83*9c60debdSEric Miao #define KPREC_RECOUNT0(n)	((n) & 0xff)
84*9c60debdSEric Miao #define KPREC_RECOUNT1(n)	(((n) >> 16) & 0xff)
85*9c60debdSEric Miao 
86*9c60debdSEric Miao #define KPMK_MKP        (0x1 << 31)
87*9c60debdSEric Miao #define KPAS_SO         (0x1 << 31)
88*9c60debdSEric Miao #define KPASMKPx_SO     (0x1 << 31)
89*9c60debdSEric Miao 
901814db69SEric Miao #define KPAS_MUKP(n)	(((n) >> 26) & 0x1f)
911814db69SEric Miao #define KPAS_RP(n)	(((n) >> 4) & 0xf)
921814db69SEric Miao #define KPAS_CP(n)	((n) & 0xf)
930e5f11aaSEric Miao 
941814db69SEric Miao #define KPASMKP_MKC_MASK	(0xff)
951814db69SEric Miao 
96*9c60debdSEric Miao #define keypad_readl(off)	__raw_readl(keypad->mmio_base + (off))
97*9c60debdSEric Miao #define keypad_writel(off, v)	__raw_writel((v), keypad->mmio_base + (off))
98*9c60debdSEric Miao 
991814db69SEric Miao #define MAX_MATRIX_KEY_NUM	(8 * 8)
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;
106*9c60debdSEric Miao 	void __iomem *mmio_base;
1071814db69SEric Miao 
1081814db69SEric Miao 	/* matrix key code map */
1091814db69SEric Miao 	unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
1101814db69SEric Miao 
1111814db69SEric Miao 	/* state row bits of each column scan */
1121814db69SEric Miao 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
11362059d9eSEric Miao 	uint32_t direct_key_state;
11462059d9eSEric Miao 
11562059d9eSEric Miao 	unsigned int direct_key_mask;
11662059d9eSEric Miao 
11762059d9eSEric Miao 	int rotary_rel_code[2];
11862059d9eSEric Miao 	int rotary_up_key[2];
11962059d9eSEric Miao 	int rotary_down_key[2];
1201814db69SEric Miao };
1211814db69SEric Miao 
1221814db69SEric Miao static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
1231814db69SEric Miao {
1241814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
1251814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
1261814db69SEric Miao 	unsigned int *key;
1271814db69SEric Miao 	int i;
1281814db69SEric Miao 
1291814db69SEric Miao 	key = &pdata->matrix_key_map[0];
1301814db69SEric Miao 	for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
1311814db69SEric Miao 		int row = ((*key) >> 28) & 0xf;
1321814db69SEric Miao 		int col = ((*key) >> 24) & 0xf;
1331814db69SEric Miao 		int code = (*key) & 0xffffff;
1341814db69SEric Miao 
1351814db69SEric Miao 		keypad->matrix_keycodes[(row << 3) + col] = code;
1361814db69SEric Miao 		set_bit(code, input_dev->keybit);
1371814db69SEric Miao 	}
13862059d9eSEric Miao 
13962059d9eSEric Miao 	keypad->rotary_up_key[0] = pdata->rotary0_up_key;
14062059d9eSEric Miao 	keypad->rotary_up_key[1] = pdata->rotary1_up_key;
14162059d9eSEric Miao 	keypad->rotary_down_key[0] = pdata->rotary0_down_key;
14262059d9eSEric Miao 	keypad->rotary_down_key[1] = pdata->rotary1_down_key;
14362059d9eSEric Miao 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
14462059d9eSEric Miao 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
14562059d9eSEric Miao 
14662059d9eSEric Miao 	if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
14762059d9eSEric Miao 		set_bit(pdata->rotary0_up_key, input_dev->keybit);
14862059d9eSEric Miao 		set_bit(pdata->rotary0_down_key, input_dev->keybit);
14962059d9eSEric Miao 	} else
15062059d9eSEric Miao 		set_bit(pdata->rotary0_rel_code, input_dev->relbit);
15162059d9eSEric Miao 
15262059d9eSEric Miao 	if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
15362059d9eSEric Miao 		set_bit(pdata->rotary1_up_key, input_dev->keybit);
15462059d9eSEric Miao 		set_bit(pdata->rotary1_down_key, input_dev->keybit);
15562059d9eSEric Miao 	} else
15662059d9eSEric Miao 		set_bit(pdata->rotary1_rel_code, input_dev->relbit);
1571814db69SEric Miao }
1581814db69SEric Miao 
1591814db69SEric Miao static inline unsigned int lookup_matrix_keycode(
1601814db69SEric Miao 		struct pxa27x_keypad *keypad, int row, int col)
1611814db69SEric Miao {
1621814db69SEric Miao 	return keypad->matrix_keycodes[(row << 3) + col];
1631814db69SEric Miao }
1641814db69SEric Miao 
1651814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
1661814db69SEric Miao {
1671814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
1681814db69SEric Miao 	int row, col, num_keys_pressed = 0;
1691814db69SEric Miao 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
170*9c60debdSEric Miao 	uint32_t kpas = keypad_readl(KPAS);
1711814db69SEric Miao 
1721814db69SEric Miao 	num_keys_pressed = KPAS_MUKP(kpas);
1731814db69SEric Miao 
1741814db69SEric Miao 	memset(new_state, 0, sizeof(new_state));
1751814db69SEric Miao 
1761814db69SEric Miao 	if (num_keys_pressed == 0)
1771814db69SEric Miao 		goto scan;
1781814db69SEric Miao 
1791814db69SEric Miao 	if (num_keys_pressed == 1) {
1801814db69SEric Miao 		col = KPAS_CP(kpas);
1811814db69SEric Miao 		row = KPAS_RP(kpas);
1821814db69SEric Miao 
1831814db69SEric Miao 		/* if invalid row/col, treat as no key pressed */
1841814db69SEric Miao 		if (col >= pdata->matrix_key_cols ||
1851814db69SEric Miao 		    row >= pdata->matrix_key_rows)
1861814db69SEric Miao 			goto scan;
1871814db69SEric Miao 
1881814db69SEric Miao 		new_state[col] = (1 << row);
1891814db69SEric Miao 		goto scan;
1901814db69SEric Miao 	}
1911814db69SEric Miao 
1921814db69SEric Miao 	if (num_keys_pressed > 1) {
193*9c60debdSEric Miao 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
194*9c60debdSEric Miao 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
195*9c60debdSEric Miao 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
196*9c60debdSEric Miao 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
1971814db69SEric Miao 
1981814db69SEric Miao 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
1991814db69SEric Miao 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
2001814db69SEric Miao 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
2011814db69SEric Miao 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
2021814db69SEric Miao 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
2031814db69SEric Miao 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
2041814db69SEric Miao 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
2051814db69SEric Miao 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
2061814db69SEric Miao 	}
2071814db69SEric Miao scan:
2081814db69SEric Miao 	for (col = 0; col < pdata->matrix_key_cols; col++) {
2091814db69SEric Miao 		uint32_t bits_changed;
2101814db69SEric Miao 
2111814db69SEric Miao 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
2121814db69SEric Miao 		if (bits_changed == 0)
2131814db69SEric Miao 			continue;
2141814db69SEric Miao 
2151814db69SEric Miao 		for (row = 0; row < pdata->matrix_key_rows; row++) {
2161814db69SEric Miao 			if ((bits_changed & (1 << row)) == 0)
2171814db69SEric Miao 				continue;
2181814db69SEric Miao 
2191814db69SEric Miao 			input_report_key(keypad->input_dev,
2201814db69SEric Miao 				lookup_matrix_keycode(keypad, row, col),
2211814db69SEric Miao 				new_state[col] & (1 << row));
2221814db69SEric Miao 		}
2231814db69SEric Miao 	}
2241814db69SEric Miao 	input_sync(keypad->input_dev);
2251814db69SEric Miao 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
2261814db69SEric Miao }
2270e5f11aaSEric Miao 
228d7416f9eSEric Miao #define DEFAULT_KPREC	(0x007f007f)
229d7416f9eSEric Miao 
23062059d9eSEric Miao static inline int rotary_delta(uint32_t kprec)
23162059d9eSEric Miao {
23262059d9eSEric Miao 	if (kprec & KPREC_OF0)
23362059d9eSEric Miao 		return (kprec & 0xff) + 0x7f;
23462059d9eSEric Miao 	else if (kprec & KPREC_UF0)
23562059d9eSEric Miao 		return (kprec & 0xff) - 0x7f - 0xff;
23662059d9eSEric Miao 	else
23762059d9eSEric Miao 		return (kprec & 0xff) - 0x7f;
23862059d9eSEric Miao }
23962059d9eSEric Miao 
24062059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
24162059d9eSEric Miao {
24262059d9eSEric Miao 	struct input_dev *dev = keypad->input_dev;
24362059d9eSEric Miao 
24462059d9eSEric Miao 	if (delta == 0)
24562059d9eSEric Miao 		return;
24662059d9eSEric Miao 
24762059d9eSEric Miao 	if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
24862059d9eSEric Miao 		int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
24962059d9eSEric Miao 					    keypad->rotary_down_key[r];
25062059d9eSEric Miao 
25162059d9eSEric Miao 		/* simulate a press-n-release */
25262059d9eSEric Miao 		input_report_key(dev, keycode, 1);
25362059d9eSEric Miao 		input_sync(dev);
25462059d9eSEric Miao 		input_report_key(dev, keycode, 0);
25562059d9eSEric Miao 		input_sync(dev);
25662059d9eSEric Miao 	} else {
25762059d9eSEric Miao 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
25862059d9eSEric Miao 		input_sync(dev);
25962059d9eSEric Miao 	}
26062059d9eSEric Miao }
26162059d9eSEric Miao 
26262059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
26362059d9eSEric Miao {
26462059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
26562059d9eSEric Miao 	uint32_t kprec;
26662059d9eSEric Miao 
26762059d9eSEric Miao 	/* read and reset to default count value */
268*9c60debdSEric Miao 	kprec = keypad_readl(KPREC);
269*9c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
27062059d9eSEric Miao 
27162059d9eSEric Miao 	if (pdata->enable_rotary0)
27262059d9eSEric Miao 		report_rotary_event(keypad, 0, rotary_delta(kprec));
27362059d9eSEric Miao 
27462059d9eSEric Miao 	if (pdata->enable_rotary1)
27562059d9eSEric Miao 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
27662059d9eSEric Miao }
27762059d9eSEric Miao 
27862059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
27962059d9eSEric Miao {
28062059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
28162059d9eSEric Miao 	unsigned int new_state;
28262059d9eSEric Miao 	uint32_t kpdk, bits_changed;
28362059d9eSEric Miao 	int i;
28462059d9eSEric Miao 
285*9c60debdSEric Miao 	kpdk = keypad_readl(KPDK);
28662059d9eSEric Miao 
28762059d9eSEric Miao 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
28862059d9eSEric Miao 		pxa27x_keypad_scan_rotary(keypad);
28962059d9eSEric Miao 
29062059d9eSEric Miao 	if (pdata->direct_key_map == NULL)
29162059d9eSEric Miao 		return;
29262059d9eSEric Miao 
29362059d9eSEric Miao 	new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
29462059d9eSEric Miao 	bits_changed = keypad->direct_key_state ^ new_state;
29562059d9eSEric Miao 
29662059d9eSEric Miao 	if (bits_changed == 0)
29762059d9eSEric Miao 		return;
29862059d9eSEric Miao 
29962059d9eSEric Miao 	for (i = 0; i < pdata->direct_key_num; i++) {
30062059d9eSEric Miao 		if (bits_changed & (1 << i))
30162059d9eSEric Miao 			input_report_key(keypad->input_dev,
30262059d9eSEric Miao 					pdata->direct_key_map[i],
30362059d9eSEric Miao 					(new_state & (1 << i)));
30462059d9eSEric Miao 	}
30562059d9eSEric Miao 	input_sync(keypad->input_dev);
30662059d9eSEric Miao 	keypad->direct_key_state = new_state;
30762059d9eSEric Miao }
30862059d9eSEric Miao 
3090e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
3100e5f11aaSEric Miao {
3111814db69SEric Miao 	struct pxa27x_keypad *keypad = dev_id;
312*9c60debdSEric Miao 	unsigned long kpc = keypad_readl(KPC);
3130e5f11aaSEric Miao 
31462059d9eSEric Miao 	if (kpc & KPC_DI)
31562059d9eSEric Miao 		pxa27x_keypad_scan_direct(keypad);
3160e5f11aaSEric Miao 
3171814db69SEric Miao 	if (kpc & KPC_MI)
3181814db69SEric Miao 		pxa27x_keypad_scan_matrix(keypad);
3190e5f11aaSEric Miao 
3200e5f11aaSEric Miao 	return IRQ_HANDLED;
3210e5f11aaSEric Miao }
3220e5f11aaSEric Miao 
323d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
324d7416f9eSEric Miao {
325d7416f9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
32662059d9eSEric Miao 	unsigned int mask = 0, direct_key_num = 0;
327d7416f9eSEric Miao 	unsigned long kpc = 0;
328d7416f9eSEric Miao 
329d7416f9eSEric Miao 	/* enable matrix keys with automatic scan */
330d7416f9eSEric Miao 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
331d7416f9eSEric Miao 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
332d7416f9eSEric Miao 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
333d7416f9eSEric Miao 		       KPC_MKCN(pdata->matrix_key_cols);
334d7416f9eSEric Miao 	}
335d7416f9eSEric Miao 
33662059d9eSEric Miao 	/* enable rotary key, debounce interval same as direct keys */
33762059d9eSEric Miao 	if (pdata->enable_rotary0) {
33862059d9eSEric Miao 		mask |= 0x03;
33962059d9eSEric Miao 		direct_key_num = 2;
34062059d9eSEric Miao 		kpc |= KPC_REE0;
34162059d9eSEric Miao 	}
342d7416f9eSEric Miao 
34362059d9eSEric Miao 	if (pdata->enable_rotary1) {
34462059d9eSEric Miao 		mask |= 0x0c;
34562059d9eSEric Miao 		direct_key_num = 4;
34662059d9eSEric Miao 		kpc |= KPC_REE1;
34762059d9eSEric Miao 	}
34862059d9eSEric Miao 
34962059d9eSEric Miao 	if (pdata->direct_key_num > direct_key_num)
35062059d9eSEric Miao 		direct_key_num = pdata->direct_key_num;
35162059d9eSEric Miao 
35262059d9eSEric Miao 	keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
35362059d9eSEric Miao 
35462059d9eSEric Miao 	/* enable direct key */
35562059d9eSEric Miao 	if (direct_key_num)
35662059d9eSEric Miao 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
35762059d9eSEric Miao 
358*9c60debdSEric Miao 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
359*9c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
360d7416f9eSEric Miao }
361d7416f9eSEric Miao 
3620e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev)
3630e5f11aaSEric Miao {
3641814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
3651814db69SEric Miao 
3660e5f11aaSEric Miao 	/* Enable unit clock */
3671814db69SEric Miao 	clk_enable(keypad->clk);
368d7416f9eSEric Miao 	pxa27x_keypad_config(keypad);
3690e5f11aaSEric Miao 
3700e5f11aaSEric Miao 	return 0;
3710e5f11aaSEric Miao }
3720e5f11aaSEric Miao 
3730e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev)
3740e5f11aaSEric Miao {
3751814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
3761814db69SEric Miao 
3770e5f11aaSEric Miao 	/* Disable clock unit */
3781814db69SEric Miao 	clk_disable(keypad->clk);
3790e5f11aaSEric Miao }
3800e5f11aaSEric Miao 
3810e5f11aaSEric Miao #ifdef CONFIG_PM
3820e5f11aaSEric Miao static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
3830e5f11aaSEric Miao {
3841814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
3850e5f11aaSEric Miao 
386d7416f9eSEric Miao 	clk_disable(keypad->clk);
3870e5f11aaSEric Miao 	return 0;
3880e5f11aaSEric Miao }
3890e5f11aaSEric Miao 
3900e5f11aaSEric Miao static int pxa27x_keypad_resume(struct platform_device *pdev)
3910e5f11aaSEric Miao {
3921814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
3931814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
3940e5f11aaSEric Miao 
3950e5f11aaSEric Miao 	mutex_lock(&input_dev->mutex);
3960e5f11aaSEric Miao 
3970e5f11aaSEric Miao 	if (input_dev->users) {
3980e5f11aaSEric Miao 		/* Enable unit clock */
3991814db69SEric Miao 		clk_enable(keypad->clk);
400d7416f9eSEric Miao 		pxa27x_keypad_config(keypad);
4010e5f11aaSEric Miao 	}
4020e5f11aaSEric Miao 
4030e5f11aaSEric Miao 	mutex_unlock(&input_dev->mutex);
4040e5f11aaSEric Miao 
4050e5f11aaSEric Miao 	return 0;
4060e5f11aaSEric Miao }
4070e5f11aaSEric Miao #else
4080e5f11aaSEric Miao #define pxa27x_keypad_suspend	NULL
4090e5f11aaSEric Miao #define pxa27x_keypad_resume	NULL
4100e5f11aaSEric Miao #endif
4110e5f11aaSEric Miao 
412*9c60debdSEric Miao #define res_size(res)	((res)->end - (res)->start + 1)
413*9c60debdSEric Miao 
4140e5f11aaSEric Miao static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
4150e5f11aaSEric Miao {
4161814db69SEric Miao 	struct pxa27x_keypad *keypad;
4170e5f11aaSEric Miao 	struct input_dev *input_dev;
418*9c60debdSEric Miao 	struct resource *res;
419*9c60debdSEric Miao 	int irq, error;
4200e5f11aaSEric Miao 
4211814db69SEric Miao 	keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
4221814db69SEric Miao 	if (keypad == NULL) {
4231814db69SEric Miao 		dev_err(&pdev->dev, "failed to allocate driver data\n");
4241814db69SEric Miao 		return -ENOMEM;
4251814db69SEric Miao 	}
4261814db69SEric Miao 
4271814db69SEric Miao 	keypad->pdata = pdev->dev.platform_data;
4281814db69SEric Miao 	if (keypad->pdata == NULL) {
4291814db69SEric Miao 		dev_err(&pdev->dev, "no platform data defined\n");
4301814db69SEric Miao 		error = -EINVAL;
4311814db69SEric Miao 		goto failed_free;
4321814db69SEric Miao 	}
4331814db69SEric Miao 
434*9c60debdSEric Miao 	irq = platform_get_irq(pdev, 0);
435*9c60debdSEric Miao 	if (irq < 0) {
436*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get keypad irq\n");
437*9c60debdSEric Miao 		error = -ENXIO;
438*9c60debdSEric Miao 		goto failed_free;
439*9c60debdSEric Miao 	}
440*9c60debdSEric Miao 
441*9c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
442*9c60debdSEric Miao 	if (res == NULL) {
443*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get I/O memory\n");
444*9c60debdSEric Miao 		error = -ENXIO;
445*9c60debdSEric Miao 		goto failed_free;
446*9c60debdSEric Miao 	}
447*9c60debdSEric Miao 
448*9c60debdSEric Miao 	res = request_mem_region(res->start, res_size(res), pdev->name);
449*9c60debdSEric Miao 	if (res == NULL) {
450*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request I/O memory\n");
451*9c60debdSEric Miao 		error = -EBUSY;
452*9c60debdSEric Miao 		goto failed_free;
453*9c60debdSEric Miao 	}
454*9c60debdSEric Miao 
455*9c60debdSEric Miao 	keypad->mmio_base = ioremap(res->start, res_size(res));
456*9c60debdSEric Miao 	if (keypad->mmio_base == NULL) {
457*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to remap I/O memory\n");
458*9c60debdSEric Miao 		error = -ENXIO;
459*9c60debdSEric Miao 		goto failed_free_mem;
460*9c60debdSEric Miao 	}
461*9c60debdSEric Miao 
4621814db69SEric Miao 	keypad->clk = clk_get(&pdev->dev, "KBDCLK");
4631814db69SEric Miao 	if (IS_ERR(keypad->clk)) {
4641814db69SEric Miao 		dev_err(&pdev->dev, "failed to get keypad clock\n");
4651814db69SEric Miao 		error = PTR_ERR(keypad->clk);
466*9c60debdSEric Miao 		goto failed_free_io;
4670e5f11aaSEric Miao 	}
4680e5f11aaSEric Miao 
4690e5f11aaSEric Miao 	/* Create and register the input driver. */
4700e5f11aaSEric Miao 	input_dev = input_allocate_device();
4710e5f11aaSEric Miao 	if (!input_dev) {
4721814db69SEric Miao 		dev_err(&pdev->dev, "failed to allocate input device\n");
4730e5f11aaSEric Miao 		error = -ENOMEM;
4741814db69SEric Miao 		goto failed_put_clk;
4750e5f11aaSEric Miao 	}
4760e5f11aaSEric Miao 
477*9c60debdSEric Miao 	input_dev->name = pdev->name;
4780e5f11aaSEric Miao 	input_dev->id.bustype = BUS_HOST;
4790e5f11aaSEric Miao 	input_dev->open = pxa27x_keypad_open;
4800e5f11aaSEric Miao 	input_dev->close = pxa27x_keypad_close;
4810e5f11aaSEric Miao 	input_dev->dev.parent = &pdev->dev;
4820e5f11aaSEric Miao 
4831814db69SEric Miao 	keypad->input_dev = input_dev;
4841814db69SEric Miao 	input_set_drvdata(input_dev, keypad);
4851814db69SEric Miao 
4860e5f11aaSEric Miao 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
4870e5f11aaSEric Miao 		BIT_MASK(EV_REL);
4881814db69SEric Miao 
4891814db69SEric Miao 	pxa27x_keypad_build_keycode(keypad);
4901814db69SEric Miao 	platform_set_drvdata(pdev, keypad);
4910e5f11aaSEric Miao 
492*9c60debdSEric Miao 	error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED,
493*9c60debdSEric Miao 			    pdev->name, keypad);
494*9c60debdSEric Miao 	if (error) {
495*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request IRQ\n");
496*9c60debdSEric Miao 		goto failed_free_dev;
497*9c60debdSEric Miao 	}
498*9c60debdSEric Miao 
4990e5f11aaSEric Miao 	/* Register the input device */
5000e5f11aaSEric Miao 	error = input_register_device(input_dev);
501*9c60debdSEric Miao 	if (error) {
502*9c60debdSEric Miao 		dev_err(&pdev->dev, "failed to register input device\n");
503*9c60debdSEric Miao 		goto failed_free_irq;
504*9c60debdSEric Miao 	}
5050e5f11aaSEric Miao 
5060e5f11aaSEric Miao 	return 0;
5070e5f11aaSEric Miao 
508*9c60debdSEric Miao failed_free_irq:
509*9c60debdSEric Miao 	free_irq(irq, pdev);
5100e5f11aaSEric Miao 	platform_set_drvdata(pdev, NULL);
511*9c60debdSEric Miao failed_free_dev:
5120e5f11aaSEric Miao 	input_free_device(input_dev);
5131814db69SEric Miao failed_put_clk:
5141814db69SEric Miao 	clk_put(keypad->clk);
515*9c60debdSEric Miao failed_free_io:
516*9c60debdSEric Miao 	iounmap(keypad->mmio_base);
517*9c60debdSEric Miao failed_free_mem:
518*9c60debdSEric Miao 	release_mem_region(res->start, res_size(res));
5191814db69SEric Miao failed_free:
5201814db69SEric Miao 	kfree(keypad);
5210e5f11aaSEric Miao 	return error;
5220e5f11aaSEric Miao }
5230e5f11aaSEric Miao 
5240e5f11aaSEric Miao static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
5250e5f11aaSEric Miao {
5261814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
527*9c60debdSEric Miao 	struct resource *res;
5280e5f11aaSEric Miao 
529*9c60debdSEric Miao 	free_irq(platform_get_irq(pdev, 0), pdev);
5300e5f11aaSEric Miao 
5311814db69SEric Miao 	clk_disable(keypad->clk);
5321814db69SEric Miao 	clk_put(keypad->clk);
5331814db69SEric Miao 
5341814db69SEric Miao 	input_unregister_device(keypad->input_dev);
535*9c60debdSEric Miao 	input_free_device(keypad->input_dev);
536*9c60debdSEric Miao 
537*9c60debdSEric Miao 	iounmap(keypad->mmio_base);
538*9c60debdSEric Miao 
539*9c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
540*9c60debdSEric Miao 	release_mem_region(res->start, res_size(res));
5411814db69SEric Miao 
5421814db69SEric Miao 	platform_set_drvdata(pdev, NULL);
5431814db69SEric Miao 	kfree(keypad);
5440e5f11aaSEric Miao 	return 0;
5450e5f11aaSEric Miao }
5460e5f11aaSEric Miao 
5470e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = {
5480e5f11aaSEric Miao 	.probe		= pxa27x_keypad_probe,
5490e5f11aaSEric Miao 	.remove		= __devexit_p(pxa27x_keypad_remove),
5500e5f11aaSEric Miao 	.suspend	= pxa27x_keypad_suspend,
5510e5f11aaSEric Miao 	.resume		= pxa27x_keypad_resume,
5520e5f11aaSEric Miao 	.driver		= {
553*9c60debdSEric Miao 		.name	= "pxa27x-keypad",
5540e5f11aaSEric Miao 	},
5550e5f11aaSEric Miao };
5560e5f11aaSEric Miao 
5570e5f11aaSEric Miao static int __init pxa27x_keypad_init(void)
5580e5f11aaSEric Miao {
5590e5f11aaSEric Miao 	return platform_driver_register(&pxa27x_keypad_driver);
5600e5f11aaSEric Miao }
5610e5f11aaSEric Miao 
5620e5f11aaSEric Miao static void __exit pxa27x_keypad_exit(void)
5630e5f11aaSEric Miao {
5640e5f11aaSEric Miao 	platform_driver_unregister(&pxa27x_keypad_driver);
5650e5f11aaSEric Miao }
5660e5f11aaSEric Miao 
5670e5f11aaSEric Miao module_init(pxa27x_keypad_init);
5680e5f11aaSEric Miao module_exit(pxa27x_keypad_exit);
5690e5f11aaSEric Miao 
5700e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
5710e5f11aaSEric Miao MODULE_LICENSE("GPL");
572