xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision 0a085a9482fa51efb58c9d351ea98e83c5df93fc)
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
112f82af08SNicolas 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>
295a0e3ad6STejun Heo #include <linux/slab.h>
300e5f11aaSEric Miao 
310e5f11aaSEric Miao #include <asm/mach/arch.h>
320e5f11aaSEric Miao #include <asm/mach/map.h>
330e5f11aaSEric Miao 
34a09e64fbSRussell King #include <mach/hardware.h>
35293b2da1SArnd Bergmann #include <linux/platform_data/keypad-pxa27x.h>
369c60debdSEric Miao /*
379c60debdSEric Miao  * Keypad Controller registers
389c60debdSEric Miao  */
399c60debdSEric Miao #define KPC             0x0000 /* Keypad Control register */
409c60debdSEric Miao #define KPDK            0x0008 /* Keypad Direct Key register */
419c60debdSEric Miao #define KPREC           0x0010 /* Keypad Rotary Encoder register */
429c60debdSEric Miao #define KPMK            0x0018 /* Keypad Matrix Key register */
439c60debdSEric Miao #define KPAS            0x0020 /* Keypad Automatic Scan register */
440e5f11aaSEric Miao 
459c60debdSEric Miao /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
469c60debdSEric Miao #define KPASMKP0        0x0028
479c60debdSEric Miao #define KPASMKP1        0x0030
489c60debdSEric Miao #define KPASMKP2        0x0038
499c60debdSEric Miao #define KPASMKP3        0x0040
509c60debdSEric Miao #define KPKDI           0x0048
519c60debdSEric Miao 
529c60debdSEric Miao /* bit definitions */
53688dad4fSSamuel Ortiz #define KPC_MKRN(n)	((((n) - 1) & 0x7) << 26) /* matrix key row number */
54688dad4fSSamuel Ortiz #define KPC_MKCN(n)	((((n) - 1) & 0x7) << 23) /* matrix key column number */
55688dad4fSSamuel Ortiz #define KPC_DKN(n)	((((n) - 1) & 0x7) << 6)  /* direct key number */
56d7416f9eSEric Miao 
579c60debdSEric Miao #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
589c60debdSEric Miao #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
599c60debdSEric Miao #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
609c60debdSEric Miao #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
619c60debdSEric Miao 
629c60debdSEric Miao #define KPC_MS(n)	(0x1 << (13 + (n)))	/* Matrix scan line 'n' */
639c60debdSEric Miao #define KPC_MS_ALL      (0xff << 13)
649c60debdSEric Miao 
659c60debdSEric Miao #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
669c60debdSEric Miao #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
679c60debdSEric Miao #define KPC_DK_DEB_SEL	(0x1 <<  9)  /* Direct Keypad Debounce Select */
689c60debdSEric Miao #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
699c60debdSEric Miao #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
709c60debdSEric Miao #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
719c60debdSEric Miao #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
729c60debdSEric Miao #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
739c60debdSEric Miao #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
749c60debdSEric Miao 
7562059d9eSEric Miao #define KPDK_DKP        (0x1 << 31)
7662059d9eSEric Miao #define KPDK_DK(n)	((n) & 0xff)
7762059d9eSEric Miao 
789c60debdSEric Miao #define KPREC_OF1       (0x1 << 31)
799c60debdSEric Miao #define kPREC_UF1       (0x1 << 30)
809c60debdSEric Miao #define KPREC_OF0       (0x1 << 15)
819c60debdSEric Miao #define KPREC_UF0       (0x1 << 14)
829c60debdSEric Miao 
839c60debdSEric Miao #define KPREC_RECOUNT0(n)	((n) & 0xff)
849c60debdSEric Miao #define KPREC_RECOUNT1(n)	(((n) >> 16) & 0xff)
859c60debdSEric Miao 
869c60debdSEric Miao #define KPMK_MKP        (0x1 << 31)
879c60debdSEric Miao #define KPAS_SO         (0x1 << 31)
889c60debdSEric Miao #define KPASMKPx_SO     (0x1 << 31)
899c60debdSEric 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 
969c60debdSEric Miao #define keypad_readl(off)	__raw_readl(keypad->mmio_base + (off))
979c60debdSEric Miao #define keypad_writel(off, v)	__raw_writel((v), keypad->mmio_base + (off))
989c60debdSEric Miao 
99bd96f378SDmitry Torokhov #define MAX_MATRIX_KEY_NUM	(MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
100bd96f378SDmitry Torokhov #define MAX_KEYPAD_KEYS		(MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
1011814db69SEric Miao 
1021814db69SEric Miao struct pxa27x_keypad {
1031814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata;
1041814db69SEric Miao 
1051814db69SEric Miao 	struct clk *clk;
1061814db69SEric Miao 	struct input_dev *input_dev;
1079c60debdSEric Miao 	void __iomem *mmio_base;
1081814db69SEric Miao 
10939ab9ddeSEric Miao 	int irq;
11039ab9ddeSEric Miao 
111bd96f378SDmitry Torokhov 	unsigned short keycodes[MAX_KEYPAD_KEYS];
112bd96f378SDmitry Torokhov 	int rotary_rel_code[2];
1131814db69SEric Miao 
1141814db69SEric Miao 	/* state row bits of each column scan */
1151814db69SEric Miao 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
11662059d9eSEric Miao 	uint32_t direct_key_state;
11762059d9eSEric Miao 
11862059d9eSEric Miao 	unsigned int direct_key_mask;
1191814db69SEric Miao };
1201814db69SEric Miao 
121*0a085a94SChao Xie static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
1221814db69SEric Miao {
1231814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
1241814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
125*0a085a94SChao Xie 	const struct matrix_keymap_data *keymap_data =
126*0a085a94SChao Xie 				pdata ? pdata->matrix_keymap_data : NULL;
127bd96f378SDmitry Torokhov 	unsigned short keycode;
1281814db69SEric Miao 	int i;
129*0a085a94SChao Xie 	int error;
1301814db69SEric Miao 
131*0a085a94SChao Xie 	error = matrix_keypad_build_keymap(keymap_data, NULL,
132*0a085a94SChao Xie 					   pdata->matrix_key_rows,
133*0a085a94SChao Xie 					   pdata->matrix_key_cols,
134*0a085a94SChao Xie 					   keypad->keycodes, input_dev);
135*0a085a94SChao Xie 	if (error)
136*0a085a94SChao Xie 		return error;
1371814db69SEric Miao 
138*0a085a94SChao Xie 	/*
139*0a085a94SChao Xie 	 * The keycodes may not only include matrix keys but also the direct
140*0a085a94SChao Xie 	 * or rotary keys.
141*0a085a94SChao Xie 	 */
142*0a085a94SChao Xie 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
14362059d9eSEric Miao 
144*0a085a94SChao Xie 	/* For direct keys. */
145bd96f378SDmitry Torokhov 	for (i = 0; i < pdata->direct_key_num; i++) {
146bd96f378SDmitry Torokhov 		keycode = pdata->direct_key_map[i];
147bd96f378SDmitry Torokhov 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
148bd96f378SDmitry Torokhov 		__set_bit(keycode, input_dev->keybit);
149bd96f378SDmitry Torokhov 	}
15062059d9eSEric Miao 
151471637a5SAntonio Ospite 	if (pdata->enable_rotary0) {
15262059d9eSEric Miao 		if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
153bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_up_key;
154bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
155bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
156bd96f378SDmitry Torokhov 
157bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_down_key;
158bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
159bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
160bd96f378SDmitry Torokhov 
161bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = -1;
162bd96f378SDmitry Torokhov 		} else {
163bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
16452ec7752SDmitry Torokhov 			__set_bit(pdata->rotary0_rel_code, input_dev->relbit);
165471637a5SAntonio Ospite 		}
166bd96f378SDmitry Torokhov 	}
16762059d9eSEric Miao 
168471637a5SAntonio Ospite 	if (pdata->enable_rotary1) {
16962059d9eSEric Miao 		if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
170bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_up_key;
171bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
172bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
173bd96f378SDmitry Torokhov 
174bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_down_key;
175bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
176bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
177bd96f378SDmitry Torokhov 
178bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = -1;
179bd96f378SDmitry Torokhov 		} else {
180bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
18152ec7752SDmitry Torokhov 			__set_bit(pdata->rotary1_rel_code, input_dev->relbit);
1821814db69SEric Miao 		}
183471637a5SAntonio Ospite 	}
1841814db69SEric Miao 
185bd96f378SDmitry Torokhov 	__clear_bit(KEY_RESERVED, input_dev->keybit);
186*0a085a94SChao Xie 
187*0a085a94SChao Xie 	return 0;
1881814db69SEric Miao }
1891814db69SEric Miao 
1901814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
1911814db69SEric Miao {
1921814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
193bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
1941814db69SEric Miao 	int row, col, num_keys_pressed = 0;
1951814db69SEric Miao 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
1969c60debdSEric Miao 	uint32_t kpas = keypad_readl(KPAS);
1971814db69SEric Miao 
1981814db69SEric Miao 	num_keys_pressed = KPAS_MUKP(kpas);
1991814db69SEric Miao 
2001814db69SEric Miao 	memset(new_state, 0, sizeof(new_state));
2011814db69SEric Miao 
2021814db69SEric Miao 	if (num_keys_pressed == 0)
2031814db69SEric Miao 		goto scan;
2041814db69SEric Miao 
2051814db69SEric Miao 	if (num_keys_pressed == 1) {
2061814db69SEric Miao 		col = KPAS_CP(kpas);
2071814db69SEric Miao 		row = KPAS_RP(kpas);
2081814db69SEric Miao 
2091814db69SEric Miao 		/* if invalid row/col, treat as no key pressed */
2101814db69SEric Miao 		if (col >= pdata->matrix_key_cols ||
2111814db69SEric Miao 		    row >= pdata->matrix_key_rows)
2121814db69SEric Miao 			goto scan;
2131814db69SEric Miao 
2141814db69SEric Miao 		new_state[col] = (1 << row);
2151814db69SEric Miao 		goto scan;
2161814db69SEric Miao 	}
2171814db69SEric Miao 
2181814db69SEric Miao 	if (num_keys_pressed > 1) {
2199c60debdSEric Miao 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
2209c60debdSEric Miao 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
2219c60debdSEric Miao 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
2229c60debdSEric Miao 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
2231814db69SEric Miao 
2241814db69SEric Miao 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
2251814db69SEric Miao 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
2261814db69SEric Miao 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
2271814db69SEric Miao 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
2281814db69SEric Miao 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
2291814db69SEric Miao 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
2301814db69SEric Miao 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
2311814db69SEric Miao 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
2321814db69SEric Miao 	}
2331814db69SEric Miao scan:
2341814db69SEric Miao 	for (col = 0; col < pdata->matrix_key_cols; col++) {
2351814db69SEric Miao 		uint32_t bits_changed;
236bd96f378SDmitry Torokhov 		int code;
2371814db69SEric Miao 
2381814db69SEric Miao 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
2391814db69SEric Miao 		if (bits_changed == 0)
2401814db69SEric Miao 			continue;
2411814db69SEric Miao 
2421814db69SEric Miao 		for (row = 0; row < pdata->matrix_key_rows; row++) {
2431814db69SEric Miao 			if ((bits_changed & (1 << row)) == 0)
2441814db69SEric Miao 				continue;
2451814db69SEric Miao 
246bd96f378SDmitry Torokhov 			code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
247bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
248bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
2491814db69SEric Miao 					 new_state[col] & (1 << row));
2501814db69SEric Miao 		}
2511814db69SEric Miao 	}
252bd96f378SDmitry Torokhov 	input_sync(input_dev);
2531814db69SEric Miao 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
2541814db69SEric Miao }
2550e5f11aaSEric Miao 
256d7416f9eSEric Miao #define DEFAULT_KPREC	(0x007f007f)
257d7416f9eSEric Miao 
25862059d9eSEric Miao static inline int rotary_delta(uint32_t kprec)
25962059d9eSEric Miao {
26062059d9eSEric Miao 	if (kprec & KPREC_OF0)
26162059d9eSEric Miao 		return (kprec & 0xff) + 0x7f;
26262059d9eSEric Miao 	else if (kprec & KPREC_UF0)
26362059d9eSEric Miao 		return (kprec & 0xff) - 0x7f - 0xff;
26462059d9eSEric Miao 	else
26562059d9eSEric Miao 		return (kprec & 0xff) - 0x7f;
26662059d9eSEric Miao }
26762059d9eSEric Miao 
26862059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
26962059d9eSEric Miao {
27062059d9eSEric Miao 	struct input_dev *dev = keypad->input_dev;
27162059d9eSEric Miao 
27262059d9eSEric Miao 	if (delta == 0)
27362059d9eSEric Miao 		return;
27462059d9eSEric Miao 
275bd96f378SDmitry Torokhov 	if (keypad->rotary_rel_code[r] == -1) {
276bd96f378SDmitry Torokhov 		int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
277bd96f378SDmitry Torokhov 		unsigned char keycode = keypad->keycodes[code];
27862059d9eSEric Miao 
27962059d9eSEric Miao 		/* simulate a press-n-release */
280bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
28162059d9eSEric Miao 		input_report_key(dev, keycode, 1);
28262059d9eSEric Miao 		input_sync(dev);
283bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
28462059d9eSEric Miao 		input_report_key(dev, keycode, 0);
28562059d9eSEric Miao 		input_sync(dev);
28662059d9eSEric Miao 	} else {
28762059d9eSEric Miao 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
28862059d9eSEric Miao 		input_sync(dev);
28962059d9eSEric Miao 	}
29062059d9eSEric Miao }
29162059d9eSEric Miao 
29262059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
29362059d9eSEric Miao {
29462059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
29562059d9eSEric Miao 	uint32_t kprec;
29662059d9eSEric Miao 
29762059d9eSEric Miao 	/* read and reset to default count value */
2989c60debdSEric Miao 	kprec = keypad_readl(KPREC);
2999c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
30062059d9eSEric Miao 
30162059d9eSEric Miao 	if (pdata->enable_rotary0)
30262059d9eSEric Miao 		report_rotary_event(keypad, 0, rotary_delta(kprec));
30362059d9eSEric Miao 
30462059d9eSEric Miao 	if (pdata->enable_rotary1)
30562059d9eSEric Miao 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
30662059d9eSEric Miao }
30762059d9eSEric Miao 
30862059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
30962059d9eSEric Miao {
31062059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
311bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
31262059d9eSEric Miao 	unsigned int new_state;
31362059d9eSEric Miao 	uint32_t kpdk, bits_changed;
31462059d9eSEric Miao 	int i;
31562059d9eSEric Miao 
3169c60debdSEric Miao 	kpdk = keypad_readl(KPDK);
31762059d9eSEric Miao 
31862059d9eSEric Miao 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
31962059d9eSEric Miao 		pxa27x_keypad_scan_rotary(keypad);
32062059d9eSEric Miao 
321ee1d8040SChao Xie 	/*
322ee1d8040SChao Xie 	 * The KPDR_DK only output the key pin level, so it relates to board,
323ee1d8040SChao Xie 	 * and low level may be active.
324ee1d8040SChao Xie 	 */
325ee1d8040SChao Xie 	if (pdata->direct_key_low_active)
326ee1d8040SChao Xie 		new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
327ee1d8040SChao Xie 	else
32862059d9eSEric Miao 		new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
329ee1d8040SChao Xie 
33062059d9eSEric Miao 	bits_changed = keypad->direct_key_state ^ new_state;
33162059d9eSEric Miao 
33262059d9eSEric Miao 	if (bits_changed == 0)
33362059d9eSEric Miao 		return;
33462059d9eSEric Miao 
33562059d9eSEric Miao 	for (i = 0; i < pdata->direct_key_num; i++) {
336bd96f378SDmitry Torokhov 		if (bits_changed & (1 << i)) {
337bd96f378SDmitry Torokhov 			int code = MAX_MATRIX_KEY_NUM + i;
338bd96f378SDmitry Torokhov 
339bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
340bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
341bd96f378SDmitry Torokhov 					 new_state & (1 << i));
34262059d9eSEric Miao 		}
343bd96f378SDmitry Torokhov 	}
344bd96f378SDmitry Torokhov 	input_sync(input_dev);
34562059d9eSEric Miao 	keypad->direct_key_state = new_state;
34662059d9eSEric Miao }
34762059d9eSEric Miao 
3483587c258SMark F. Brown static void clear_wakeup_event(struct pxa27x_keypad *keypad)
3493587c258SMark F. Brown {
3503587c258SMark F. Brown 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
3513587c258SMark F. Brown 
3523587c258SMark F. Brown 	if (pdata->clear_wakeup_event)
3533587c258SMark F. Brown 		(pdata->clear_wakeup_event)();
3543587c258SMark F. Brown }
3553587c258SMark F. Brown 
3560e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
3570e5f11aaSEric Miao {
3581814db69SEric Miao 	struct pxa27x_keypad *keypad = dev_id;
3599c60debdSEric Miao 	unsigned long kpc = keypad_readl(KPC);
3600e5f11aaSEric Miao 
3613587c258SMark F. Brown 	clear_wakeup_event(keypad);
3623587c258SMark F. Brown 
36362059d9eSEric Miao 	if (kpc & KPC_DI)
36462059d9eSEric Miao 		pxa27x_keypad_scan_direct(keypad);
3650e5f11aaSEric Miao 
3661814db69SEric Miao 	if (kpc & KPC_MI)
3671814db69SEric Miao 		pxa27x_keypad_scan_matrix(keypad);
3680e5f11aaSEric Miao 
3690e5f11aaSEric Miao 	return IRQ_HANDLED;
3700e5f11aaSEric Miao }
3710e5f11aaSEric Miao 
372d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
373d7416f9eSEric Miao {
374d7416f9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
37562059d9eSEric Miao 	unsigned int mask = 0, direct_key_num = 0;
376d7416f9eSEric Miao 	unsigned long kpc = 0;
377d7416f9eSEric Miao 
378904adedeSVasily Khoruzhick 	/* clear pending interrupt bit */
379904adedeSVasily Khoruzhick 	keypad_readl(KPC);
380904adedeSVasily Khoruzhick 
381d7416f9eSEric Miao 	/* enable matrix keys with automatic scan */
382d7416f9eSEric Miao 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
383d7416f9eSEric Miao 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
384d7416f9eSEric Miao 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
385d7416f9eSEric Miao 		       KPC_MKCN(pdata->matrix_key_cols);
386d7416f9eSEric Miao 	}
387d7416f9eSEric Miao 
38862059d9eSEric Miao 	/* enable rotary key, debounce interval same as direct keys */
38962059d9eSEric Miao 	if (pdata->enable_rotary0) {
39062059d9eSEric Miao 		mask |= 0x03;
39162059d9eSEric Miao 		direct_key_num = 2;
39262059d9eSEric Miao 		kpc |= KPC_REE0;
39362059d9eSEric Miao 	}
394d7416f9eSEric Miao 
39562059d9eSEric Miao 	if (pdata->enable_rotary1) {
39662059d9eSEric Miao 		mask |= 0x0c;
39762059d9eSEric Miao 		direct_key_num = 4;
39862059d9eSEric Miao 		kpc |= KPC_REE1;
39962059d9eSEric Miao 	}
40062059d9eSEric Miao 
40162059d9eSEric Miao 	if (pdata->direct_key_num > direct_key_num)
40262059d9eSEric Miao 		direct_key_num = pdata->direct_key_num;
40362059d9eSEric Miao 
404fb054bf2SChao Xie 	/*
405fb054bf2SChao Xie 	 * Direct keys usage may not start from KP_DKIN0, check the platfrom
406fb054bf2SChao Xie 	 * mask data to config the specific.
407fb054bf2SChao Xie 	 */
408fb054bf2SChao Xie 	if (pdata->direct_key_mask)
409fb054bf2SChao Xie 		keypad->direct_key_mask = pdata->direct_key_mask;
410fb054bf2SChao Xie 	else
4115545fa89SChao Xie 		keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
41262059d9eSEric Miao 
41362059d9eSEric Miao 	/* enable direct key */
41462059d9eSEric Miao 	if (direct_key_num)
41562059d9eSEric Miao 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
41662059d9eSEric Miao 
4179c60debdSEric Miao 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
4189c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
41976cb44e1SEric Miao 	keypad_writel(KPKDI, pdata->debounce_interval);
420d7416f9eSEric Miao }
421d7416f9eSEric Miao 
4220e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev)
4230e5f11aaSEric Miao {
4241814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
4251814db69SEric Miao 
4260e5f11aaSEric Miao 	/* Enable unit clock */
4276ce34a5fSChao Xie 	clk_prepare_enable(keypad->clk);
428d7416f9eSEric Miao 	pxa27x_keypad_config(keypad);
4290e5f11aaSEric Miao 
4300e5f11aaSEric Miao 	return 0;
4310e5f11aaSEric Miao }
4320e5f11aaSEric Miao 
4330e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev)
4340e5f11aaSEric Miao {
4351814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
4361814db69SEric Miao 
4370e5f11aaSEric Miao 	/* Disable clock unit */
4386ce34a5fSChao Xie 	clk_disable_unprepare(keypad->clk);
4390e5f11aaSEric Miao }
4400e5f11aaSEric Miao 
4410e5f11aaSEric Miao #ifdef CONFIG_PM
442b0010911SDmitry Torokhov static int pxa27x_keypad_suspend(struct device *dev)
4430e5f11aaSEric Miao {
444b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
4451814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
4460e5f11aaSEric Miao 
4476ce34a5fSChao Xie 	/*
4486ce34a5fSChao Xie 	 * If the keypad is used a wake up source, clock can not be disabled.
4496ce34a5fSChao Xie 	 * Or it can not detect the key pressing.
4506ce34a5fSChao Xie 	 */
45139ab9ddeSEric Miao 	if (device_may_wakeup(&pdev->dev))
45239ab9ddeSEric Miao 		enable_irq_wake(keypad->irq);
4536ce34a5fSChao Xie 	else
4546ce34a5fSChao Xie 		clk_disable_unprepare(keypad->clk);
45539ab9ddeSEric Miao 
4560e5f11aaSEric Miao 	return 0;
4570e5f11aaSEric Miao }
4580e5f11aaSEric Miao 
459b0010911SDmitry Torokhov static int pxa27x_keypad_resume(struct device *dev)
4600e5f11aaSEric Miao {
461b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
4621814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
4631814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
4640e5f11aaSEric Miao 
4656ce34a5fSChao Xie 	/*
4666ce34a5fSChao Xie 	 * If the keypad is used as wake up source, the clock is not turned
4676ce34a5fSChao Xie 	 * off. So do not need configure it again.
4686ce34a5fSChao Xie 	 */
4696ce34a5fSChao Xie 	if (device_may_wakeup(&pdev->dev)) {
47039ab9ddeSEric Miao 		disable_irq_wake(keypad->irq);
4716ce34a5fSChao Xie 	} else {
4720e5f11aaSEric Miao 		mutex_lock(&input_dev->mutex);
4730e5f11aaSEric Miao 
4740e5f11aaSEric Miao 		if (input_dev->users) {
4750e5f11aaSEric Miao 			/* Enable unit clock */
4766ce34a5fSChao Xie 			clk_prepare_enable(keypad->clk);
477d7416f9eSEric Miao 			pxa27x_keypad_config(keypad);
4780e5f11aaSEric Miao 		}
4790e5f11aaSEric Miao 
4800e5f11aaSEric Miao 		mutex_unlock(&input_dev->mutex);
4816ce34a5fSChao Xie 	}
4820e5f11aaSEric Miao 
4830e5f11aaSEric Miao 	return 0;
4840e5f11aaSEric Miao }
485b0010911SDmitry Torokhov 
486b0010911SDmitry Torokhov static const struct dev_pm_ops pxa27x_keypad_pm_ops = {
487b0010911SDmitry Torokhov 	.suspend	= pxa27x_keypad_suspend,
488b0010911SDmitry Torokhov 	.resume		= pxa27x_keypad_resume,
489b0010911SDmitry Torokhov };
4900e5f11aaSEric Miao #endif
4910e5f11aaSEric Miao 
4925298cc4cSBill Pemberton static int pxa27x_keypad_probe(struct platform_device *pdev)
4930e5f11aaSEric Miao {
494bd96f378SDmitry Torokhov 	struct pxa27x_keypad_platform_data *pdata = pdev->dev.platform_data;
4951814db69SEric Miao 	struct pxa27x_keypad *keypad;
4960e5f11aaSEric Miao 	struct input_dev *input_dev;
4979c60debdSEric Miao 	struct resource *res;
4989c60debdSEric Miao 	int irq, error;
4990e5f11aaSEric Miao 
500bd96f378SDmitry Torokhov 	if (pdata == NULL) {
5011814db69SEric Miao 		dev_err(&pdev->dev, "no platform data defined\n");
502bd96f378SDmitry Torokhov 		return -EINVAL;
5031814db69SEric Miao 	}
5041814db69SEric Miao 
5059c60debdSEric Miao 	irq = platform_get_irq(pdev, 0);
5069c60debdSEric Miao 	if (irq < 0) {
5079c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get keypad irq\n");
508bd96f378SDmitry Torokhov 		return -ENXIO;
5099c60debdSEric Miao 	}
5109c60debdSEric Miao 
5119c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5129c60debdSEric Miao 	if (res == NULL) {
5139c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get I/O memory\n");
514bd96f378SDmitry Torokhov 		return -ENXIO;
515bd96f378SDmitry Torokhov 	}
516bd96f378SDmitry Torokhov 
517bd96f378SDmitry Torokhov 	keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
518bd96f378SDmitry Torokhov 	input_dev = input_allocate_device();
519bd96f378SDmitry Torokhov 	if (!keypad || !input_dev) {
520bd96f378SDmitry Torokhov 		dev_err(&pdev->dev, "failed to allocate memory\n");
521bd96f378SDmitry Torokhov 		error = -ENOMEM;
5229c60debdSEric Miao 		goto failed_free;
5239c60debdSEric Miao 	}
5249c60debdSEric Miao 
525bd96f378SDmitry Torokhov 	keypad->pdata = pdata;
526bd96f378SDmitry Torokhov 	keypad->input_dev = input_dev;
527bd96f378SDmitry Torokhov 	keypad->irq = irq;
528bd96f378SDmitry Torokhov 
52952ec7752SDmitry Torokhov 	res = request_mem_region(res->start, resource_size(res), pdev->name);
5309c60debdSEric Miao 	if (res == NULL) {
5319c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request I/O memory\n");
5329c60debdSEric Miao 		error = -EBUSY;
5339c60debdSEric Miao 		goto failed_free;
5349c60debdSEric Miao 	}
5359c60debdSEric Miao 
53652ec7752SDmitry Torokhov 	keypad->mmio_base = ioremap(res->start, resource_size(res));
5379c60debdSEric Miao 	if (keypad->mmio_base == NULL) {
5389c60debdSEric Miao 		dev_err(&pdev->dev, "failed to remap I/O memory\n");
5399c60debdSEric Miao 		error = -ENXIO;
5409c60debdSEric Miao 		goto failed_free_mem;
5419c60debdSEric Miao 	}
5429c60debdSEric Miao 
543e0d8b13aSRussell King 	keypad->clk = clk_get(&pdev->dev, NULL);
5441814db69SEric Miao 	if (IS_ERR(keypad->clk)) {
5451814db69SEric Miao 		dev_err(&pdev->dev, "failed to get keypad clock\n");
5461814db69SEric Miao 		error = PTR_ERR(keypad->clk);
5479c60debdSEric Miao 		goto failed_free_io;
5480e5f11aaSEric Miao 	}
5490e5f11aaSEric Miao 
5509c60debdSEric Miao 	input_dev->name = pdev->name;
5510e5f11aaSEric Miao 	input_dev->id.bustype = BUS_HOST;
5520e5f11aaSEric Miao 	input_dev->open = pxa27x_keypad_open;
5530e5f11aaSEric Miao 	input_dev->close = pxa27x_keypad_close;
5540e5f11aaSEric Miao 	input_dev->dev.parent = &pdev->dev;
5550e5f11aaSEric Miao 
556bd96f378SDmitry Torokhov 	input_dev->keycode = keypad->keycodes;
557bd96f378SDmitry Torokhov 	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
558bd96f378SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
559bd96f378SDmitry Torokhov 
5601814db69SEric Miao 	input_set_drvdata(input_dev, keypad);
5611814db69SEric Miao 
562471637a5SAntonio Ospite 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
563bd96f378SDmitry Torokhov 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
5641814db69SEric Miao 
565*0a085a94SChao Xie 	error = pxa27x_keypad_build_keycode(keypad);
566*0a085a94SChao Xie 	if (error) {
567*0a085a94SChao Xie 		dev_err(&pdev->dev, "failed to build keycode\n");
568*0a085a94SChao Xie 		goto failed_put_clk;
569*0a085a94SChao Xie 	}
570bd96f378SDmitry Torokhov 
571bd96f378SDmitry Torokhov 	if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
572bd96f378SDmitry Torokhov 	    (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
573bd96f378SDmitry Torokhov 		input_dev->evbit[0] |= BIT_MASK(EV_REL);
574bd96f378SDmitry Torokhov 	}
5750e5f11aaSEric Miao 
576ec4665c4SYong Zhang 	error = request_irq(irq, pxa27x_keypad_irq_handler, 0,
5779c60debdSEric Miao 			    pdev->name, keypad);
5789c60debdSEric Miao 	if (error) {
5799c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request IRQ\n");
580bd96f378SDmitry Torokhov 		goto failed_put_clk;
5819c60debdSEric Miao 	}
5829c60debdSEric Miao 
5830e5f11aaSEric Miao 	/* Register the input device */
5840e5f11aaSEric Miao 	error = input_register_device(input_dev);
5859c60debdSEric Miao 	if (error) {
5869c60debdSEric Miao 		dev_err(&pdev->dev, "failed to register input device\n");
5879c60debdSEric Miao 		goto failed_free_irq;
5889c60debdSEric Miao 	}
5890e5f11aaSEric Miao 
590bd96f378SDmitry Torokhov 	platform_set_drvdata(pdev, keypad);
59139ab9ddeSEric Miao 	device_init_wakeup(&pdev->dev, 1);
59239ab9ddeSEric Miao 
5930e5f11aaSEric Miao 	return 0;
5940e5f11aaSEric Miao 
5959c60debdSEric Miao failed_free_irq:
5968c7f5f58SLars-Peter Clausen 	free_irq(irq, keypad);
5971814db69SEric Miao failed_put_clk:
5981814db69SEric Miao 	clk_put(keypad->clk);
5999c60debdSEric Miao failed_free_io:
6009c60debdSEric Miao 	iounmap(keypad->mmio_base);
6019c60debdSEric Miao failed_free_mem:
60252ec7752SDmitry Torokhov 	release_mem_region(res->start, resource_size(res));
6031814db69SEric Miao failed_free:
604bd96f378SDmitry Torokhov 	input_free_device(input_dev);
6051814db69SEric Miao 	kfree(keypad);
6060e5f11aaSEric Miao 	return error;
6070e5f11aaSEric Miao }
6080e5f11aaSEric Miao 
609e2619cf7SBill Pemberton static int pxa27x_keypad_remove(struct platform_device *pdev)
6100e5f11aaSEric Miao {
6111814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
6129c60debdSEric Miao 	struct resource *res;
6130e5f11aaSEric Miao 
6148c7f5f58SLars-Peter Clausen 	free_irq(keypad->irq, keypad);
6151814db69SEric Miao 	clk_put(keypad->clk);
6161814db69SEric Miao 
6171814db69SEric Miao 	input_unregister_device(keypad->input_dev);
6189c60debdSEric Miao 	iounmap(keypad->mmio_base);
6199c60debdSEric Miao 
6209c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
62152ec7752SDmitry Torokhov 	release_mem_region(res->start, resource_size(res));
6221814db69SEric Miao 
6231814db69SEric Miao 	kfree(keypad);
624bd96f378SDmitry Torokhov 
6250e5f11aaSEric Miao 	return 0;
6260e5f11aaSEric Miao }
6270e5f11aaSEric Miao 
628d7b5247bSKay Sievers /* work with hotplug and coldplug */
629d7b5247bSKay Sievers MODULE_ALIAS("platform:pxa27x-keypad");
630d7b5247bSKay Sievers 
6310e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = {
6320e5f11aaSEric Miao 	.probe		= pxa27x_keypad_probe,
6331cb0aa88SBill Pemberton 	.remove		= pxa27x_keypad_remove,
6340e5f11aaSEric Miao 	.driver		= {
6359c60debdSEric Miao 		.name	= "pxa27x-keypad",
636d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
637b0010911SDmitry Torokhov #ifdef CONFIG_PM
638b0010911SDmitry Torokhov 		.pm	= &pxa27x_keypad_pm_ops,
639b0010911SDmitry Torokhov #endif
6400e5f11aaSEric Miao 	},
6410e5f11aaSEric Miao };
6425146c84fSJJ Ding module_platform_driver(pxa27x_keypad_driver);
6430e5f11aaSEric Miao 
6440e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
6450e5f11aaSEric Miao MODULE_LICENSE("GPL");
646