xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision e4156979c7d34e5197b16fa31c1c7549eae675e5)
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*e4156979SChao Xie #ifdef CONFIG_OF
122*e4156979SChao Xie static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad)
123*e4156979SChao Xie {
124*e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
125*e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
126*e4156979SChao Xie 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
127*e4156979SChao Xie 	u32 rows, cols;
128*e4156979SChao Xie 	int error;
129*e4156979SChao Xie 
130*e4156979SChao Xie 	error = matrix_keypad_parse_of_params(dev, &rows, &cols);
131*e4156979SChao Xie 	if (error)
132*e4156979SChao Xie 		return error;
133*e4156979SChao Xie 
134*e4156979SChao Xie 	if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
135*e4156979SChao Xie 		dev_err(dev, "rows or cols exceeds maximum value\n");
136*e4156979SChao Xie 		return -EINVAL;
137*e4156979SChao Xie 	}
138*e4156979SChao Xie 
139*e4156979SChao Xie 	pdata->matrix_key_rows = rows;
140*e4156979SChao Xie 	pdata->matrix_key_cols = cols;
141*e4156979SChao Xie 
142*e4156979SChao Xie 	error = matrix_keypad_build_keymap(NULL, NULL,
143*e4156979SChao Xie 					   pdata->matrix_key_rows,
144*e4156979SChao Xie 					   pdata->matrix_key_cols,
145*e4156979SChao Xie 					   keypad->keycodes, input_dev);
146*e4156979SChao Xie 	if (error)
147*e4156979SChao Xie 		return error;
148*e4156979SChao Xie 
149*e4156979SChao Xie 	return 0;
150*e4156979SChao Xie }
151*e4156979SChao Xie 
152*e4156979SChao Xie static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad)
153*e4156979SChao Xie {
154*e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
155*e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
156*e4156979SChao Xie 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
157*e4156979SChao Xie 	struct device_node *np = dev->of_node;
158*e4156979SChao Xie 	const __be16 *prop;
159*e4156979SChao Xie 	unsigned short code;
160*e4156979SChao Xie 	unsigned int proplen, size;
161*e4156979SChao Xie 	int i;
162*e4156979SChao Xie 	int error;
163*e4156979SChao Xie 
164*e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,direct-key-count",
165*e4156979SChao Xie 				     &pdata->direct_key_num);
166*e4156979SChao Xie 	if (error) {
167*e4156979SChao Xie 		/*
168*e4156979SChao Xie 		 * If do not have marvel,direct-key-count defined,
169*e4156979SChao Xie 		 * it means direct key is not supported.
170*e4156979SChao Xie 		 */
171*e4156979SChao Xie 		return error == -EINVAL ? 0 : error;
172*e4156979SChao Xie 	}
173*e4156979SChao Xie 
174*e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,direct-key-mask",
175*e4156979SChao Xie 				     &pdata->direct_key_mask);
176*e4156979SChao Xie 	if (error) {
177*e4156979SChao Xie 		if (error != -EINVAL)
178*e4156979SChao Xie 			return error;
179*e4156979SChao Xie 
180*e4156979SChao Xie 		/*
181*e4156979SChao Xie 		 * If marvell,direct-key-mask is not defined, driver will use
182*e4156979SChao Xie 		 * default value. Default value is set when configure the keypad.
183*e4156979SChao Xie 		 */
184*e4156979SChao Xie 		pdata->direct_key_mask = 0;
185*e4156979SChao Xie 	}
186*e4156979SChao Xie 
187*e4156979SChao Xie 	pdata->direct_key_low_active = of_property_read_bool(np,
188*e4156979SChao Xie 					"marvell,direct-key-low-active");
189*e4156979SChao Xie 
190*e4156979SChao Xie 	prop = of_get_property(np, "marvell,direct-key-map", &proplen);
191*e4156979SChao Xie 	if (!prop)
192*e4156979SChao Xie 		return -EINVAL;
193*e4156979SChao Xie 
194*e4156979SChao Xie 	if (proplen % sizeof(u16))
195*e4156979SChao Xie 		return -EINVAL;
196*e4156979SChao Xie 
197*e4156979SChao Xie 	size = proplen / sizeof(u16);
198*e4156979SChao Xie 
199*e4156979SChao Xie 	/* Only MAX_DIRECT_KEY_NUM is accepted.*/
200*e4156979SChao Xie 	if (size > MAX_DIRECT_KEY_NUM)
201*e4156979SChao Xie 		return -EINVAL;
202*e4156979SChao Xie 
203*e4156979SChao Xie 	for (i = 0; i < size; i++) {
204*e4156979SChao Xie 		code = be16_to_cpup(prop + i);
205*e4156979SChao Xie 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
206*e4156979SChao Xie 		__set_bit(code, input_dev->keybit);
207*e4156979SChao Xie 	}
208*e4156979SChao Xie 
209*e4156979SChao Xie 	return 0;
210*e4156979SChao Xie }
211*e4156979SChao Xie 
212*e4156979SChao Xie static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad)
213*e4156979SChao Xie {
214*e4156979SChao Xie 	const __be32 *prop;
215*e4156979SChao Xie 	int i, relkey_ret;
216*e4156979SChao Xie 	unsigned int code, proplen;
217*e4156979SChao Xie 	const char *rotaryname[2] = {
218*e4156979SChao Xie 			"marvell,rotary0", "marvell,rotary1"};
219*e4156979SChao Xie 	const char relkeyname[] = {"marvell,rotary-rel-key"};
220*e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
221*e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
222*e4156979SChao Xie 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
223*e4156979SChao Xie 	struct device_node *np = dev->of_node;
224*e4156979SChao Xie 
225*e4156979SChao Xie 	relkey_ret = of_property_read_u32(np, relkeyname, &code);
226*e4156979SChao Xie 	/* if can read correct rotary key-code, we do not need this. */
227*e4156979SChao Xie 	if (relkey_ret == 0) {
228*e4156979SChao Xie 		unsigned short relcode;
229*e4156979SChao Xie 
230*e4156979SChao Xie 		/* rotary0 taks lower half, rotary1 taks upper half. */
231*e4156979SChao Xie 		relcode = code & 0xffff;
232*e4156979SChao Xie 		pdata->rotary0_rel_code = (code & 0xffff);
233*e4156979SChao Xie 		__set_bit(relcode, input_dev->relbit);
234*e4156979SChao Xie 
235*e4156979SChao Xie 		relcode = code >> 16;
236*e4156979SChao Xie 		pdata->rotary1_rel_code = relcode;
237*e4156979SChao Xie 		__set_bit(relcode, input_dev->relbit);
238*e4156979SChao Xie 	}
239*e4156979SChao Xie 
240*e4156979SChao Xie 	for (i = 0; i < 2; i++) {
241*e4156979SChao Xie 		prop = of_get_property(np, rotaryname[i], &proplen);
242*e4156979SChao Xie 		/*
243*e4156979SChao Xie 		 * If the prop is not set, it means keypad does not need
244*e4156979SChao Xie 		 * initialize the rotaryX.
245*e4156979SChao Xie 		 */
246*e4156979SChao Xie 		if (!prop)
247*e4156979SChao Xie 			continue;
248*e4156979SChao Xie 
249*e4156979SChao Xie 		code = be32_to_cpup(prop);
250*e4156979SChao Xie 		/*
251*e4156979SChao Xie 		 * Not all up/down key code are valid.
252*e4156979SChao Xie 		 * Now we depends on direct-rel-code.
253*e4156979SChao Xie 		 */
254*e4156979SChao Xie 		if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
255*e4156979SChao Xie 			return relkey_ret;
256*e4156979SChao Xie 		} else {
257*e4156979SChao Xie 			unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
258*e4156979SChao Xie 			unsigned short keycode;
259*e4156979SChao Xie 
260*e4156979SChao Xie 			keycode = code & 0xffff;
261*e4156979SChao Xie 			keypad->keycodes[n] = keycode;
262*e4156979SChao Xie 			__set_bit(keycode, input_dev->keybit);
263*e4156979SChao Xie 
264*e4156979SChao Xie 			keycode = code >> 16;
265*e4156979SChao Xie 			keypad->keycodes[n + 1] = keycode;
266*e4156979SChao Xie 			__set_bit(keycode, input_dev->keybit);
267*e4156979SChao Xie 
268*e4156979SChao Xie 			if (i == 0)
269*e4156979SChao Xie 				pdata->rotary0_rel_code = -1;
270*e4156979SChao Xie 			else
271*e4156979SChao Xie 				pdata->rotary1_rel_code = -1;
272*e4156979SChao Xie 		}
273*e4156979SChao Xie 		if (i == 0)
274*e4156979SChao Xie 			pdata->enable_rotary0 = 1;
275*e4156979SChao Xie 		else
276*e4156979SChao Xie 			pdata->enable_rotary1 = 1;
277*e4156979SChao Xie 	}
278*e4156979SChao Xie 
279*e4156979SChao Xie 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
280*e4156979SChao Xie 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
281*e4156979SChao Xie 
282*e4156979SChao Xie 	return 0;
283*e4156979SChao Xie }
284*e4156979SChao Xie 
285*e4156979SChao Xie static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
286*e4156979SChao Xie {
287*e4156979SChao Xie 	struct input_dev *input_dev = keypad->input_dev;
288*e4156979SChao Xie 	struct device *dev = input_dev->dev.parent;
289*e4156979SChao Xie 	struct device_node *np = dev->of_node;
290*e4156979SChao Xie 	int error;
291*e4156979SChao Xie 
292*e4156979SChao Xie 	keypad->pdata = devm_kzalloc(dev, sizeof(*keypad->pdata),
293*e4156979SChao Xie 				     GFP_KERNEL);
294*e4156979SChao Xie 	if (!keypad->pdata) {
295*e4156979SChao Xie 		dev_err(dev, "failed to allocate memory for pdata\n");
296*e4156979SChao Xie 		return -ENOMEM;
297*e4156979SChao Xie 	}
298*e4156979SChao Xie 
299*e4156979SChao Xie 	error = pxa27x_keypad_matrix_key_parse_dt(keypad);
300*e4156979SChao Xie 	if (error) {
301*e4156979SChao Xie 		dev_err(dev, "failed to parse matrix key\n");
302*e4156979SChao Xie 		return error;
303*e4156979SChao Xie 	}
304*e4156979SChao Xie 
305*e4156979SChao Xie 	error = pxa27x_keypad_direct_key_parse_dt(keypad);
306*e4156979SChao Xie 	if (error) {
307*e4156979SChao Xie 		dev_err(dev, "failed to parse direct key\n");
308*e4156979SChao Xie 		return error;
309*e4156979SChao Xie 	}
310*e4156979SChao Xie 
311*e4156979SChao Xie 	error = pxa27x_keypad_rotary_parse_dt(keypad);
312*e4156979SChao Xie 	if (error) {
313*e4156979SChao Xie 		dev_err(dev, "failed to parse rotary key\n");
314*e4156979SChao Xie 		return error;
315*e4156979SChao Xie 	}
316*e4156979SChao Xie 
317*e4156979SChao Xie 	error = of_property_read_u32(np, "marvell,debounce-interval",
318*e4156979SChao Xie 				    &keypad->pdata->debounce_interval);
319*e4156979SChao Xie 	if (error) {
320*e4156979SChao Xie 		dev_err(dev, "failed to parse debpunce-interval\n");
321*e4156979SChao Xie 		return error;
322*e4156979SChao Xie 	}
323*e4156979SChao Xie 
324*e4156979SChao Xie 	/*
325*e4156979SChao Xie 	 * The keycodes may not only includes matrix key but also the direct
326*e4156979SChao Xie 	 * key or rotary key.
327*e4156979SChao Xie 	 */
328*e4156979SChao Xie 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
329*e4156979SChao Xie 
330*e4156979SChao Xie 	return 0;
331*e4156979SChao Xie }
332*e4156979SChao Xie 
333*e4156979SChao Xie #else
334*e4156979SChao Xie 
335*e4156979SChao Xie static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
336*e4156979SChao Xie {
337*e4156979SChao Xie 	dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
338*e4156979SChao Xie 
339*e4156979SChao Xie 	return -EINVAL;
340*e4156979SChao Xie }
341*e4156979SChao Xie 
342*e4156979SChao Xie #endif
343*e4156979SChao Xie 
3440a085a94SChao Xie static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
3451814db69SEric Miao {
3461814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
3471814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
3480a085a94SChao Xie 	const struct matrix_keymap_data *keymap_data =
3490a085a94SChao Xie 				pdata ? pdata->matrix_keymap_data : NULL;
350bd96f378SDmitry Torokhov 	unsigned short keycode;
3511814db69SEric Miao 	int i;
3520a085a94SChao Xie 	int error;
3531814db69SEric Miao 
3540a085a94SChao Xie 	error = matrix_keypad_build_keymap(keymap_data, NULL,
3550a085a94SChao Xie 					   pdata->matrix_key_rows,
3560a085a94SChao Xie 					   pdata->matrix_key_cols,
3570a085a94SChao Xie 					   keypad->keycodes, input_dev);
3580a085a94SChao Xie 	if (error)
3590a085a94SChao Xie 		return error;
3601814db69SEric Miao 
3610a085a94SChao Xie 	/*
3620a085a94SChao Xie 	 * The keycodes may not only include matrix keys but also the direct
3630a085a94SChao Xie 	 * or rotary keys.
3640a085a94SChao Xie 	 */
3650a085a94SChao Xie 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
36662059d9eSEric Miao 
3670a085a94SChao Xie 	/* For direct keys. */
368bd96f378SDmitry Torokhov 	for (i = 0; i < pdata->direct_key_num; i++) {
369bd96f378SDmitry Torokhov 		keycode = pdata->direct_key_map[i];
370bd96f378SDmitry Torokhov 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
371bd96f378SDmitry Torokhov 		__set_bit(keycode, input_dev->keybit);
372bd96f378SDmitry Torokhov 	}
37362059d9eSEric Miao 
374471637a5SAntonio Ospite 	if (pdata->enable_rotary0) {
37562059d9eSEric Miao 		if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
376bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_up_key;
377bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
378bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
379bd96f378SDmitry Torokhov 
380bd96f378SDmitry Torokhov 			keycode = pdata->rotary0_down_key;
381bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
382bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
383bd96f378SDmitry Torokhov 
384bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = -1;
385bd96f378SDmitry Torokhov 		} else {
386bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
38752ec7752SDmitry Torokhov 			__set_bit(pdata->rotary0_rel_code, input_dev->relbit);
388471637a5SAntonio Ospite 		}
389bd96f378SDmitry Torokhov 	}
39062059d9eSEric Miao 
391471637a5SAntonio Ospite 	if (pdata->enable_rotary1) {
39262059d9eSEric Miao 		if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
393bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_up_key;
394bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
395bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
396bd96f378SDmitry Torokhov 
397bd96f378SDmitry Torokhov 			keycode = pdata->rotary1_down_key;
398bd96f378SDmitry Torokhov 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
399bd96f378SDmitry Torokhov 			__set_bit(keycode, input_dev->keybit);
400bd96f378SDmitry Torokhov 
401bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = -1;
402bd96f378SDmitry Torokhov 		} else {
403bd96f378SDmitry Torokhov 			keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
40452ec7752SDmitry Torokhov 			__set_bit(pdata->rotary1_rel_code, input_dev->relbit);
4051814db69SEric Miao 		}
406471637a5SAntonio Ospite 	}
4071814db69SEric Miao 
408bd96f378SDmitry Torokhov 	__clear_bit(KEY_RESERVED, input_dev->keybit);
4090a085a94SChao Xie 
4100a085a94SChao Xie 	return 0;
4111814db69SEric Miao }
4121814db69SEric Miao 
4131814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
4141814db69SEric Miao {
4151814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
416bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
4171814db69SEric Miao 	int row, col, num_keys_pressed = 0;
4181814db69SEric Miao 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
4199c60debdSEric Miao 	uint32_t kpas = keypad_readl(KPAS);
4201814db69SEric Miao 
4211814db69SEric Miao 	num_keys_pressed = KPAS_MUKP(kpas);
4221814db69SEric Miao 
4231814db69SEric Miao 	memset(new_state, 0, sizeof(new_state));
4241814db69SEric Miao 
4251814db69SEric Miao 	if (num_keys_pressed == 0)
4261814db69SEric Miao 		goto scan;
4271814db69SEric Miao 
4281814db69SEric Miao 	if (num_keys_pressed == 1) {
4291814db69SEric Miao 		col = KPAS_CP(kpas);
4301814db69SEric Miao 		row = KPAS_RP(kpas);
4311814db69SEric Miao 
4321814db69SEric Miao 		/* if invalid row/col, treat as no key pressed */
4331814db69SEric Miao 		if (col >= pdata->matrix_key_cols ||
4341814db69SEric Miao 		    row >= pdata->matrix_key_rows)
4351814db69SEric Miao 			goto scan;
4361814db69SEric Miao 
4371814db69SEric Miao 		new_state[col] = (1 << row);
4381814db69SEric Miao 		goto scan;
4391814db69SEric Miao 	}
4401814db69SEric Miao 
4411814db69SEric Miao 	if (num_keys_pressed > 1) {
4429c60debdSEric Miao 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
4439c60debdSEric Miao 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
4449c60debdSEric Miao 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
4459c60debdSEric Miao 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
4461814db69SEric Miao 
4471814db69SEric Miao 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
4481814db69SEric Miao 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
4491814db69SEric Miao 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
4501814db69SEric Miao 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
4511814db69SEric Miao 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
4521814db69SEric Miao 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
4531814db69SEric Miao 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
4541814db69SEric Miao 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
4551814db69SEric Miao 	}
4561814db69SEric Miao scan:
4571814db69SEric Miao 	for (col = 0; col < pdata->matrix_key_cols; col++) {
4581814db69SEric Miao 		uint32_t bits_changed;
459bd96f378SDmitry Torokhov 		int code;
4601814db69SEric Miao 
4611814db69SEric Miao 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
4621814db69SEric Miao 		if (bits_changed == 0)
4631814db69SEric Miao 			continue;
4641814db69SEric Miao 
4651814db69SEric Miao 		for (row = 0; row < pdata->matrix_key_rows; row++) {
4661814db69SEric Miao 			if ((bits_changed & (1 << row)) == 0)
4671814db69SEric Miao 				continue;
4681814db69SEric Miao 
469bd96f378SDmitry Torokhov 			code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
470bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
471bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
4721814db69SEric Miao 					 new_state[col] & (1 << row));
4731814db69SEric Miao 		}
4741814db69SEric Miao 	}
475bd96f378SDmitry Torokhov 	input_sync(input_dev);
4761814db69SEric Miao 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
4771814db69SEric Miao }
4780e5f11aaSEric Miao 
479d7416f9eSEric Miao #define DEFAULT_KPREC	(0x007f007f)
480d7416f9eSEric Miao 
48162059d9eSEric Miao static inline int rotary_delta(uint32_t kprec)
48262059d9eSEric Miao {
48362059d9eSEric Miao 	if (kprec & KPREC_OF0)
48462059d9eSEric Miao 		return (kprec & 0xff) + 0x7f;
48562059d9eSEric Miao 	else if (kprec & KPREC_UF0)
48662059d9eSEric Miao 		return (kprec & 0xff) - 0x7f - 0xff;
48762059d9eSEric Miao 	else
48862059d9eSEric Miao 		return (kprec & 0xff) - 0x7f;
48962059d9eSEric Miao }
49062059d9eSEric Miao 
49162059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
49262059d9eSEric Miao {
49362059d9eSEric Miao 	struct input_dev *dev = keypad->input_dev;
49462059d9eSEric Miao 
49562059d9eSEric Miao 	if (delta == 0)
49662059d9eSEric Miao 		return;
49762059d9eSEric Miao 
498bd96f378SDmitry Torokhov 	if (keypad->rotary_rel_code[r] == -1) {
499bd96f378SDmitry Torokhov 		int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
500bd96f378SDmitry Torokhov 		unsigned char keycode = keypad->keycodes[code];
50162059d9eSEric Miao 
50262059d9eSEric Miao 		/* simulate a press-n-release */
503bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
50462059d9eSEric Miao 		input_report_key(dev, keycode, 1);
50562059d9eSEric Miao 		input_sync(dev);
506bd96f378SDmitry Torokhov 		input_event(dev, EV_MSC, MSC_SCAN, code);
50762059d9eSEric Miao 		input_report_key(dev, keycode, 0);
50862059d9eSEric Miao 		input_sync(dev);
50962059d9eSEric Miao 	} else {
51062059d9eSEric Miao 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
51162059d9eSEric Miao 		input_sync(dev);
51262059d9eSEric Miao 	}
51362059d9eSEric Miao }
51462059d9eSEric Miao 
51562059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
51662059d9eSEric Miao {
51762059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
51862059d9eSEric Miao 	uint32_t kprec;
51962059d9eSEric Miao 
52062059d9eSEric Miao 	/* read and reset to default count value */
5219c60debdSEric Miao 	kprec = keypad_readl(KPREC);
5229c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
52362059d9eSEric Miao 
52462059d9eSEric Miao 	if (pdata->enable_rotary0)
52562059d9eSEric Miao 		report_rotary_event(keypad, 0, rotary_delta(kprec));
52662059d9eSEric Miao 
52762059d9eSEric Miao 	if (pdata->enable_rotary1)
52862059d9eSEric Miao 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
52962059d9eSEric Miao }
53062059d9eSEric Miao 
53162059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
53262059d9eSEric Miao {
53362059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
534bd96f378SDmitry Torokhov 	struct input_dev *input_dev = keypad->input_dev;
53562059d9eSEric Miao 	unsigned int new_state;
53662059d9eSEric Miao 	uint32_t kpdk, bits_changed;
53762059d9eSEric Miao 	int i;
53862059d9eSEric Miao 
5399c60debdSEric Miao 	kpdk = keypad_readl(KPDK);
54062059d9eSEric Miao 
54162059d9eSEric Miao 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
54262059d9eSEric Miao 		pxa27x_keypad_scan_rotary(keypad);
54362059d9eSEric Miao 
544ee1d8040SChao Xie 	/*
545ee1d8040SChao Xie 	 * The KPDR_DK only output the key pin level, so it relates to board,
546ee1d8040SChao Xie 	 * and low level may be active.
547ee1d8040SChao Xie 	 */
548ee1d8040SChao Xie 	if (pdata->direct_key_low_active)
549ee1d8040SChao Xie 		new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
550ee1d8040SChao Xie 	else
55162059d9eSEric Miao 		new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
552ee1d8040SChao Xie 
55362059d9eSEric Miao 	bits_changed = keypad->direct_key_state ^ new_state;
55462059d9eSEric Miao 
55562059d9eSEric Miao 	if (bits_changed == 0)
55662059d9eSEric Miao 		return;
55762059d9eSEric Miao 
55862059d9eSEric Miao 	for (i = 0; i < pdata->direct_key_num; i++) {
559bd96f378SDmitry Torokhov 		if (bits_changed & (1 << i)) {
560bd96f378SDmitry Torokhov 			int code = MAX_MATRIX_KEY_NUM + i;
561bd96f378SDmitry Torokhov 
562bd96f378SDmitry Torokhov 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
563bd96f378SDmitry Torokhov 			input_report_key(input_dev, keypad->keycodes[code],
564bd96f378SDmitry Torokhov 					 new_state & (1 << i));
56562059d9eSEric Miao 		}
566bd96f378SDmitry Torokhov 	}
567bd96f378SDmitry Torokhov 	input_sync(input_dev);
56862059d9eSEric Miao 	keypad->direct_key_state = new_state;
56962059d9eSEric Miao }
57062059d9eSEric Miao 
5713587c258SMark F. Brown static void clear_wakeup_event(struct pxa27x_keypad *keypad)
5723587c258SMark F. Brown {
5733587c258SMark F. Brown 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
5743587c258SMark F. Brown 
5753587c258SMark F. Brown 	if (pdata->clear_wakeup_event)
5763587c258SMark F. Brown 		(pdata->clear_wakeup_event)();
5773587c258SMark F. Brown }
5783587c258SMark F. Brown 
5790e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
5800e5f11aaSEric Miao {
5811814db69SEric Miao 	struct pxa27x_keypad *keypad = dev_id;
5829c60debdSEric Miao 	unsigned long kpc = keypad_readl(KPC);
5830e5f11aaSEric Miao 
5843587c258SMark F. Brown 	clear_wakeup_event(keypad);
5853587c258SMark F. Brown 
58662059d9eSEric Miao 	if (kpc & KPC_DI)
58762059d9eSEric Miao 		pxa27x_keypad_scan_direct(keypad);
5880e5f11aaSEric Miao 
5891814db69SEric Miao 	if (kpc & KPC_MI)
5901814db69SEric Miao 		pxa27x_keypad_scan_matrix(keypad);
5910e5f11aaSEric Miao 
5920e5f11aaSEric Miao 	return IRQ_HANDLED;
5930e5f11aaSEric Miao }
5940e5f11aaSEric Miao 
595d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
596d7416f9eSEric Miao {
597d7416f9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
59862059d9eSEric Miao 	unsigned int mask = 0, direct_key_num = 0;
599d7416f9eSEric Miao 	unsigned long kpc = 0;
600d7416f9eSEric Miao 
601904adedeSVasily Khoruzhick 	/* clear pending interrupt bit */
602904adedeSVasily Khoruzhick 	keypad_readl(KPC);
603904adedeSVasily Khoruzhick 
604d7416f9eSEric Miao 	/* enable matrix keys with automatic scan */
605d7416f9eSEric Miao 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
606d7416f9eSEric Miao 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
607d7416f9eSEric Miao 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
608d7416f9eSEric Miao 		       KPC_MKCN(pdata->matrix_key_cols);
609d7416f9eSEric Miao 	}
610d7416f9eSEric Miao 
61162059d9eSEric Miao 	/* enable rotary key, debounce interval same as direct keys */
61262059d9eSEric Miao 	if (pdata->enable_rotary0) {
61362059d9eSEric Miao 		mask |= 0x03;
61462059d9eSEric Miao 		direct_key_num = 2;
61562059d9eSEric Miao 		kpc |= KPC_REE0;
61662059d9eSEric Miao 	}
617d7416f9eSEric Miao 
61862059d9eSEric Miao 	if (pdata->enable_rotary1) {
61962059d9eSEric Miao 		mask |= 0x0c;
62062059d9eSEric Miao 		direct_key_num = 4;
62162059d9eSEric Miao 		kpc |= KPC_REE1;
62262059d9eSEric Miao 	}
62362059d9eSEric Miao 
62462059d9eSEric Miao 	if (pdata->direct_key_num > direct_key_num)
62562059d9eSEric Miao 		direct_key_num = pdata->direct_key_num;
62662059d9eSEric Miao 
627fb054bf2SChao Xie 	/*
628fb054bf2SChao Xie 	 * Direct keys usage may not start from KP_DKIN0, check the platfrom
629fb054bf2SChao Xie 	 * mask data to config the specific.
630fb054bf2SChao Xie 	 */
631fb054bf2SChao Xie 	if (pdata->direct_key_mask)
632fb054bf2SChao Xie 		keypad->direct_key_mask = pdata->direct_key_mask;
633fb054bf2SChao Xie 	else
6345545fa89SChao Xie 		keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
63562059d9eSEric Miao 
63662059d9eSEric Miao 	/* enable direct key */
63762059d9eSEric Miao 	if (direct_key_num)
63862059d9eSEric Miao 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
63962059d9eSEric Miao 
6409c60debdSEric Miao 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
6419c60debdSEric Miao 	keypad_writel(KPREC, DEFAULT_KPREC);
64276cb44e1SEric Miao 	keypad_writel(KPKDI, pdata->debounce_interval);
643d7416f9eSEric Miao }
644d7416f9eSEric Miao 
6450e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev)
6460e5f11aaSEric Miao {
6471814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
6481814db69SEric Miao 
6490e5f11aaSEric Miao 	/* Enable unit clock */
6506ce34a5fSChao Xie 	clk_prepare_enable(keypad->clk);
651d7416f9eSEric Miao 	pxa27x_keypad_config(keypad);
6520e5f11aaSEric Miao 
6530e5f11aaSEric Miao 	return 0;
6540e5f11aaSEric Miao }
6550e5f11aaSEric Miao 
6560e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev)
6570e5f11aaSEric Miao {
6581814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
6591814db69SEric Miao 
6600e5f11aaSEric Miao 	/* Disable clock unit */
6616ce34a5fSChao Xie 	clk_disable_unprepare(keypad->clk);
6620e5f11aaSEric Miao }
6630e5f11aaSEric Miao 
6640e5f11aaSEric Miao #ifdef CONFIG_PM
665b0010911SDmitry Torokhov static int pxa27x_keypad_suspend(struct device *dev)
6660e5f11aaSEric Miao {
667b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
6681814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
6690e5f11aaSEric Miao 
6706ce34a5fSChao Xie 	/*
6716ce34a5fSChao Xie 	 * If the keypad is used a wake up source, clock can not be disabled.
6726ce34a5fSChao Xie 	 * Or it can not detect the key pressing.
6736ce34a5fSChao Xie 	 */
67439ab9ddeSEric Miao 	if (device_may_wakeup(&pdev->dev))
67539ab9ddeSEric Miao 		enable_irq_wake(keypad->irq);
6766ce34a5fSChao Xie 	else
6776ce34a5fSChao Xie 		clk_disable_unprepare(keypad->clk);
67839ab9ddeSEric Miao 
6790e5f11aaSEric Miao 	return 0;
6800e5f11aaSEric Miao }
6810e5f11aaSEric Miao 
682b0010911SDmitry Torokhov static int pxa27x_keypad_resume(struct device *dev)
6830e5f11aaSEric Miao {
684b0010911SDmitry Torokhov 	struct platform_device *pdev = to_platform_device(dev);
6851814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
6861814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
6870e5f11aaSEric Miao 
6886ce34a5fSChao Xie 	/*
6896ce34a5fSChao Xie 	 * If the keypad is used as wake up source, the clock is not turned
6906ce34a5fSChao Xie 	 * off. So do not need configure it again.
6916ce34a5fSChao Xie 	 */
6926ce34a5fSChao Xie 	if (device_may_wakeup(&pdev->dev)) {
69339ab9ddeSEric Miao 		disable_irq_wake(keypad->irq);
6946ce34a5fSChao Xie 	} else {
6950e5f11aaSEric Miao 		mutex_lock(&input_dev->mutex);
6960e5f11aaSEric Miao 
6970e5f11aaSEric Miao 		if (input_dev->users) {
6980e5f11aaSEric Miao 			/* Enable unit clock */
6996ce34a5fSChao Xie 			clk_prepare_enable(keypad->clk);
700d7416f9eSEric Miao 			pxa27x_keypad_config(keypad);
7010e5f11aaSEric Miao 		}
7020e5f11aaSEric Miao 
7030e5f11aaSEric Miao 		mutex_unlock(&input_dev->mutex);
7046ce34a5fSChao Xie 	}
7050e5f11aaSEric Miao 
7060e5f11aaSEric Miao 	return 0;
7070e5f11aaSEric Miao }
708b0010911SDmitry Torokhov 
709b0010911SDmitry Torokhov static const struct dev_pm_ops pxa27x_keypad_pm_ops = {
710b0010911SDmitry Torokhov 	.suspend	= pxa27x_keypad_suspend,
711b0010911SDmitry Torokhov 	.resume		= pxa27x_keypad_resume,
712b0010911SDmitry Torokhov };
7130e5f11aaSEric Miao #endif
7140e5f11aaSEric Miao 
7155298cc4cSBill Pemberton static int pxa27x_keypad_probe(struct platform_device *pdev)
7160e5f11aaSEric Miao {
717bd96f378SDmitry Torokhov 	struct pxa27x_keypad_platform_data *pdata = pdev->dev.platform_data;
718*e4156979SChao Xie 	struct device_node *np = pdev->dev.of_node;
7191814db69SEric Miao 	struct pxa27x_keypad *keypad;
7200e5f11aaSEric Miao 	struct input_dev *input_dev;
7219c60debdSEric Miao 	struct resource *res;
7229c60debdSEric Miao 	int irq, error;
7230e5f11aaSEric Miao 
724*e4156979SChao Xie 	/* Driver need build keycode from device tree or pdata */
725*e4156979SChao Xie 	if (!np && !pdata)
726bd96f378SDmitry Torokhov 		return -EINVAL;
7271814db69SEric Miao 
7289c60debdSEric Miao 	irq = platform_get_irq(pdev, 0);
7299c60debdSEric Miao 	if (irq < 0) {
7309c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get keypad irq\n");
731bd96f378SDmitry Torokhov 		return -ENXIO;
7329c60debdSEric Miao 	}
7339c60debdSEric Miao 
7349c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7359c60debdSEric Miao 	if (res == NULL) {
7369c60debdSEric Miao 		dev_err(&pdev->dev, "failed to get I/O memory\n");
737bd96f378SDmitry Torokhov 		return -ENXIO;
738bd96f378SDmitry Torokhov 	}
739bd96f378SDmitry Torokhov 
740bd96f378SDmitry Torokhov 	keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
741bd96f378SDmitry Torokhov 	input_dev = input_allocate_device();
742bd96f378SDmitry Torokhov 	if (!keypad || !input_dev) {
743bd96f378SDmitry Torokhov 		dev_err(&pdev->dev, "failed to allocate memory\n");
744bd96f378SDmitry Torokhov 		error = -ENOMEM;
7459c60debdSEric Miao 		goto failed_free;
7469c60debdSEric Miao 	}
7479c60debdSEric Miao 
748bd96f378SDmitry Torokhov 	keypad->pdata = pdata;
749bd96f378SDmitry Torokhov 	keypad->input_dev = input_dev;
750bd96f378SDmitry Torokhov 	keypad->irq = irq;
751bd96f378SDmitry Torokhov 
75252ec7752SDmitry Torokhov 	res = request_mem_region(res->start, resource_size(res), pdev->name);
7539c60debdSEric Miao 	if (res == NULL) {
7549c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request I/O memory\n");
7559c60debdSEric Miao 		error = -EBUSY;
7569c60debdSEric Miao 		goto failed_free;
7579c60debdSEric Miao 	}
7589c60debdSEric Miao 
75952ec7752SDmitry Torokhov 	keypad->mmio_base = ioremap(res->start, resource_size(res));
7609c60debdSEric Miao 	if (keypad->mmio_base == NULL) {
7619c60debdSEric Miao 		dev_err(&pdev->dev, "failed to remap I/O memory\n");
7629c60debdSEric Miao 		error = -ENXIO;
7639c60debdSEric Miao 		goto failed_free_mem;
7649c60debdSEric Miao 	}
7659c60debdSEric Miao 
766e0d8b13aSRussell King 	keypad->clk = clk_get(&pdev->dev, NULL);
7671814db69SEric Miao 	if (IS_ERR(keypad->clk)) {
7681814db69SEric Miao 		dev_err(&pdev->dev, "failed to get keypad clock\n");
7691814db69SEric Miao 		error = PTR_ERR(keypad->clk);
7709c60debdSEric Miao 		goto failed_free_io;
7710e5f11aaSEric Miao 	}
7720e5f11aaSEric Miao 
7739c60debdSEric Miao 	input_dev->name = pdev->name;
7740e5f11aaSEric Miao 	input_dev->id.bustype = BUS_HOST;
7750e5f11aaSEric Miao 	input_dev->open = pxa27x_keypad_open;
7760e5f11aaSEric Miao 	input_dev->close = pxa27x_keypad_close;
7770e5f11aaSEric Miao 	input_dev->dev.parent = &pdev->dev;
7780e5f11aaSEric Miao 
779bd96f378SDmitry Torokhov 	input_dev->keycode = keypad->keycodes;
780bd96f378SDmitry Torokhov 	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
781bd96f378SDmitry Torokhov 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
782bd96f378SDmitry Torokhov 
7831814db69SEric Miao 	input_set_drvdata(input_dev, keypad);
7841814db69SEric Miao 
785471637a5SAntonio Ospite 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
786bd96f378SDmitry Torokhov 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
7871814db69SEric Miao 
788*e4156979SChao Xie 	if (pdata)
7890a085a94SChao Xie 		error = pxa27x_keypad_build_keycode(keypad);
790*e4156979SChao Xie 	else
791*e4156979SChao Xie 		error = pxa27x_keypad_build_keycode_from_dt(keypad);
7920a085a94SChao Xie 	if (error) {
7930a085a94SChao Xie 		dev_err(&pdev->dev, "failed to build keycode\n");
7940a085a94SChao Xie 		goto failed_put_clk;
7950a085a94SChao Xie 	}
796bd96f378SDmitry Torokhov 
797*e4156979SChao Xie 	/* If device tree is supported, pdata will be allocated. */
798*e4156979SChao Xie 	pdata = keypad->pdata;
799*e4156979SChao Xie 
800bd96f378SDmitry Torokhov 	if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
801bd96f378SDmitry Torokhov 	    (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
802bd96f378SDmitry Torokhov 		input_dev->evbit[0] |= BIT_MASK(EV_REL);
803bd96f378SDmitry Torokhov 	}
8040e5f11aaSEric Miao 
805ec4665c4SYong Zhang 	error = request_irq(irq, pxa27x_keypad_irq_handler, 0,
8069c60debdSEric Miao 			    pdev->name, keypad);
8079c60debdSEric Miao 	if (error) {
8089c60debdSEric Miao 		dev_err(&pdev->dev, "failed to request IRQ\n");
809bd96f378SDmitry Torokhov 		goto failed_put_clk;
8109c60debdSEric Miao 	}
8119c60debdSEric Miao 
8120e5f11aaSEric Miao 	/* Register the input device */
8130e5f11aaSEric Miao 	error = input_register_device(input_dev);
8149c60debdSEric Miao 	if (error) {
8159c60debdSEric Miao 		dev_err(&pdev->dev, "failed to register input device\n");
8169c60debdSEric Miao 		goto failed_free_irq;
8179c60debdSEric Miao 	}
8180e5f11aaSEric Miao 
819bd96f378SDmitry Torokhov 	platform_set_drvdata(pdev, keypad);
82039ab9ddeSEric Miao 	device_init_wakeup(&pdev->dev, 1);
82139ab9ddeSEric Miao 
8220e5f11aaSEric Miao 	return 0;
8230e5f11aaSEric Miao 
8249c60debdSEric Miao failed_free_irq:
8258c7f5f58SLars-Peter Clausen 	free_irq(irq, keypad);
8261814db69SEric Miao failed_put_clk:
8271814db69SEric Miao 	clk_put(keypad->clk);
8289c60debdSEric Miao failed_free_io:
8299c60debdSEric Miao 	iounmap(keypad->mmio_base);
8309c60debdSEric Miao failed_free_mem:
83152ec7752SDmitry Torokhov 	release_mem_region(res->start, resource_size(res));
8321814db69SEric Miao failed_free:
833bd96f378SDmitry Torokhov 	input_free_device(input_dev);
8341814db69SEric Miao 	kfree(keypad);
8350e5f11aaSEric Miao 	return error;
8360e5f11aaSEric Miao }
8370e5f11aaSEric Miao 
838e2619cf7SBill Pemberton static int pxa27x_keypad_remove(struct platform_device *pdev)
8390e5f11aaSEric Miao {
8401814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
8419c60debdSEric Miao 	struct resource *res;
8420e5f11aaSEric Miao 
8438c7f5f58SLars-Peter Clausen 	free_irq(keypad->irq, keypad);
8441814db69SEric Miao 	clk_put(keypad->clk);
8451814db69SEric Miao 
8461814db69SEric Miao 	input_unregister_device(keypad->input_dev);
8479c60debdSEric Miao 	iounmap(keypad->mmio_base);
8489c60debdSEric Miao 
8499c60debdSEric Miao 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
85052ec7752SDmitry Torokhov 	release_mem_region(res->start, resource_size(res));
8511814db69SEric Miao 
8521814db69SEric Miao 	kfree(keypad);
853bd96f378SDmitry Torokhov 
8540e5f11aaSEric Miao 	return 0;
8550e5f11aaSEric Miao }
8560e5f11aaSEric Miao 
857d7b5247bSKay Sievers /* work with hotplug and coldplug */
858d7b5247bSKay Sievers MODULE_ALIAS("platform:pxa27x-keypad");
859d7b5247bSKay Sievers 
860*e4156979SChao Xie #ifdef CONFIG_OF
861*e4156979SChao Xie static const struct of_device_id pxa27x_keypad_dt_match[] = {
862*e4156979SChao Xie 	{ .compatible = "marvell,pxa27x-keypad" },
863*e4156979SChao Xie 	{},
864*e4156979SChao Xie };
865*e4156979SChao Xie MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
866*e4156979SChao Xie #endif
867*e4156979SChao Xie 
8680e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = {
8690e5f11aaSEric Miao 	.probe		= pxa27x_keypad_probe,
8701cb0aa88SBill Pemberton 	.remove		= pxa27x_keypad_remove,
8710e5f11aaSEric Miao 	.driver		= {
8729c60debdSEric Miao 		.name	= "pxa27x-keypad",
873*e4156979SChao Xie 		.of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
874d7b5247bSKay Sievers 		.owner	= THIS_MODULE,
875b0010911SDmitry Torokhov #ifdef CONFIG_PM
876b0010911SDmitry Torokhov 		.pm	= &pxa27x_keypad_pm_ops,
877b0010911SDmitry Torokhov #endif
8780e5f11aaSEric Miao 	},
8790e5f11aaSEric Miao };
8805146c84fSJJ Ding module_platform_driver(pxa27x_keypad_driver);
8810e5f11aaSEric Miao 
8820e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
8830e5f11aaSEric Miao MODULE_LICENSE("GPL");
884