xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision 62059d9e912717abbfb875440621d935d091f289)
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/pxa-regs.h>
350e5f11aaSEric Miao #include <asm/arch/irqs.h>
360e5f11aaSEric Miao #include <asm/arch/pxa27x_keypad.h>
370e5f11aaSEric Miao 
380e5f11aaSEric Miao #define DRIVER_NAME		"pxa27x-keypad"
390e5f11aaSEric Miao 
40d7416f9eSEric Miao #define KPC_MKRN(n)	((((n) & 0x7) - 1) << 26) /* matrix key row number */
41d7416f9eSEric Miao #define KPC_MKCN(n)	((((n) & 0x7) - 1) << 23) /* matrix key column number */
42d7416f9eSEric Miao #define KPC_DKN(n)	((((n) & 0x7) - 1) << 6)  /* direct key number */
43d7416f9eSEric Miao 
44*62059d9eSEric Miao #define KPDK_DKP        (0x1 << 31)
45*62059d9eSEric Miao #define KPDK_DK(n)	((n) & 0xff)
46*62059d9eSEric Miao 
471814db69SEric Miao #define KPAS_MUKP(n)		(((n) >> 26) & 0x1f)
481814db69SEric Miao #define KPAS_RP(n)		(((n) >> 4) & 0xf)
491814db69SEric Miao #define KPAS_CP(n)		((n) & 0xf)
500e5f11aaSEric Miao 
511814db69SEric Miao #define KPASMKP_MKC_MASK	(0xff)
521814db69SEric Miao 
531814db69SEric Miao #define MAX_MATRIX_KEY_NUM	(8 * 8)
541814db69SEric Miao 
551814db69SEric Miao struct pxa27x_keypad {
561814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata;
571814db69SEric Miao 
581814db69SEric Miao 	struct clk *clk;
591814db69SEric Miao 	struct input_dev *input_dev;
601814db69SEric Miao 
611814db69SEric Miao 	/* matrix key code map */
621814db69SEric Miao 	unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
631814db69SEric Miao 
641814db69SEric Miao 	/* state row bits of each column scan */
651814db69SEric Miao 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
66*62059d9eSEric Miao 	uint32_t direct_key_state;
67*62059d9eSEric Miao 
68*62059d9eSEric Miao 	unsigned int direct_key_mask;
69*62059d9eSEric Miao 
70*62059d9eSEric Miao 	int rotary_rel_code[2];
71*62059d9eSEric Miao 	int rotary_up_key[2];
72*62059d9eSEric Miao 	int rotary_down_key[2];
731814db69SEric Miao };
741814db69SEric Miao 
751814db69SEric Miao static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
761814db69SEric Miao {
771814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
781814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
791814db69SEric Miao 	unsigned int *key;
801814db69SEric Miao 	int i;
811814db69SEric Miao 
821814db69SEric Miao 	key = &pdata->matrix_key_map[0];
831814db69SEric Miao 	for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
841814db69SEric Miao 		int row = ((*key) >> 28) & 0xf;
851814db69SEric Miao 		int col = ((*key) >> 24) & 0xf;
861814db69SEric Miao 		int code = (*key) & 0xffffff;
871814db69SEric Miao 
881814db69SEric Miao 		keypad->matrix_keycodes[(row << 3) + col] = code;
891814db69SEric Miao 		set_bit(code, input_dev->keybit);
901814db69SEric Miao 	}
91*62059d9eSEric Miao 
92*62059d9eSEric Miao 	keypad->rotary_up_key[0] = pdata->rotary0_up_key;
93*62059d9eSEric Miao 	keypad->rotary_up_key[1] = pdata->rotary1_up_key;
94*62059d9eSEric Miao 	keypad->rotary_down_key[0] = pdata->rotary0_down_key;
95*62059d9eSEric Miao 	keypad->rotary_down_key[1] = pdata->rotary1_down_key;
96*62059d9eSEric Miao 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
97*62059d9eSEric Miao 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
98*62059d9eSEric Miao 
99*62059d9eSEric Miao 	if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
100*62059d9eSEric Miao 		set_bit(pdata->rotary0_up_key, input_dev->keybit);
101*62059d9eSEric Miao 		set_bit(pdata->rotary0_down_key, input_dev->keybit);
102*62059d9eSEric Miao 	} else
103*62059d9eSEric Miao 		set_bit(pdata->rotary0_rel_code, input_dev->relbit);
104*62059d9eSEric Miao 
105*62059d9eSEric Miao 	if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
106*62059d9eSEric Miao 		set_bit(pdata->rotary1_up_key, input_dev->keybit);
107*62059d9eSEric Miao 		set_bit(pdata->rotary1_down_key, input_dev->keybit);
108*62059d9eSEric Miao 	} else
109*62059d9eSEric Miao 		set_bit(pdata->rotary1_rel_code, input_dev->relbit);
1101814db69SEric Miao }
1111814db69SEric Miao 
1121814db69SEric Miao static inline unsigned int lookup_matrix_keycode(
1131814db69SEric Miao 		struct pxa27x_keypad *keypad, int row, int col)
1141814db69SEric Miao {
1151814db69SEric Miao 	return keypad->matrix_keycodes[(row << 3) + col];
1161814db69SEric Miao }
1171814db69SEric Miao 
1181814db69SEric Miao static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
1191814db69SEric Miao {
1201814db69SEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
1211814db69SEric Miao 	int row, col, num_keys_pressed = 0;
1221814db69SEric Miao 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
1231814db69SEric Miao 	uint32_t kpas = KPAS;
1241814db69SEric Miao 
1251814db69SEric Miao 	num_keys_pressed = KPAS_MUKP(kpas);
1261814db69SEric Miao 
1271814db69SEric Miao 	memset(new_state, 0, sizeof(new_state));
1281814db69SEric Miao 
1291814db69SEric Miao 	if (num_keys_pressed == 0)
1301814db69SEric Miao 		goto scan;
1311814db69SEric Miao 
1321814db69SEric Miao 	if (num_keys_pressed == 1) {
1331814db69SEric Miao 		col = KPAS_CP(kpas);
1341814db69SEric Miao 		row = KPAS_RP(kpas);
1351814db69SEric Miao 
1361814db69SEric Miao 		/* if invalid row/col, treat as no key pressed */
1371814db69SEric Miao 		if (col >= pdata->matrix_key_cols ||
1381814db69SEric Miao 		    row >= pdata->matrix_key_rows)
1391814db69SEric Miao 			goto scan;
1401814db69SEric Miao 
1411814db69SEric Miao 		new_state[col] = (1 << row);
1421814db69SEric Miao 		goto scan;
1431814db69SEric Miao 	}
1441814db69SEric Miao 
1451814db69SEric Miao 	if (num_keys_pressed > 1) {
1461814db69SEric Miao 		uint32_t kpasmkp0 = KPASMKP0;
1471814db69SEric Miao 		uint32_t kpasmkp1 = KPASMKP1;
1481814db69SEric Miao 		uint32_t kpasmkp2 = KPASMKP2;
1491814db69SEric Miao 		uint32_t kpasmkp3 = KPASMKP3;
1501814db69SEric Miao 
1511814db69SEric Miao 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
1521814db69SEric Miao 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
1531814db69SEric Miao 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
1541814db69SEric Miao 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
1551814db69SEric Miao 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
1561814db69SEric Miao 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
1571814db69SEric Miao 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
1581814db69SEric Miao 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
1591814db69SEric Miao 	}
1601814db69SEric Miao scan:
1611814db69SEric Miao 	for (col = 0; col < pdata->matrix_key_cols; col++) {
1621814db69SEric Miao 		uint32_t bits_changed;
1631814db69SEric Miao 
1641814db69SEric Miao 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
1651814db69SEric Miao 		if (bits_changed == 0)
1661814db69SEric Miao 			continue;
1671814db69SEric Miao 
1681814db69SEric Miao 		for (row = 0; row < pdata->matrix_key_rows; row++) {
1691814db69SEric Miao 			if ((bits_changed & (1 << row)) == 0)
1701814db69SEric Miao 				continue;
1711814db69SEric Miao 
1721814db69SEric Miao 			input_report_key(keypad->input_dev,
1731814db69SEric Miao 				lookup_matrix_keycode(keypad, row, col),
1741814db69SEric Miao 				new_state[col] & (1 << row));
1751814db69SEric Miao 		}
1761814db69SEric Miao 	}
1771814db69SEric Miao 	input_sync(keypad->input_dev);
1781814db69SEric Miao 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
1791814db69SEric Miao }
1800e5f11aaSEric Miao 
181d7416f9eSEric Miao #define DEFAULT_KPREC	(0x007f007f)
182d7416f9eSEric Miao 
183*62059d9eSEric Miao static inline int rotary_delta(uint32_t kprec)
184*62059d9eSEric Miao {
185*62059d9eSEric Miao 	if (kprec & KPREC_OF0)
186*62059d9eSEric Miao 		return (kprec & 0xff) + 0x7f;
187*62059d9eSEric Miao 	else if (kprec & KPREC_UF0)
188*62059d9eSEric Miao 		return (kprec & 0xff) - 0x7f - 0xff;
189*62059d9eSEric Miao 	else
190*62059d9eSEric Miao 		return (kprec & 0xff) - 0x7f;
191*62059d9eSEric Miao }
192*62059d9eSEric Miao 
193*62059d9eSEric Miao static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
194*62059d9eSEric Miao {
195*62059d9eSEric Miao 	struct input_dev *dev = keypad->input_dev;
196*62059d9eSEric Miao 
197*62059d9eSEric Miao 	if (delta == 0)
198*62059d9eSEric Miao 		return;
199*62059d9eSEric Miao 
200*62059d9eSEric Miao 	if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
201*62059d9eSEric Miao 		int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
202*62059d9eSEric Miao 					    keypad->rotary_down_key[r];
203*62059d9eSEric Miao 
204*62059d9eSEric Miao 		/* simulate a press-n-release */
205*62059d9eSEric Miao 		input_report_key(dev, keycode, 1);
206*62059d9eSEric Miao 		input_sync(dev);
207*62059d9eSEric Miao 		input_report_key(dev, keycode, 0);
208*62059d9eSEric Miao 		input_sync(dev);
209*62059d9eSEric Miao 	} else {
210*62059d9eSEric Miao 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
211*62059d9eSEric Miao 		input_sync(dev);
212*62059d9eSEric Miao 	}
213*62059d9eSEric Miao }
214*62059d9eSEric Miao 
215*62059d9eSEric Miao static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
216*62059d9eSEric Miao {
217*62059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
218*62059d9eSEric Miao 	uint32_t kprec;
219*62059d9eSEric Miao 
220*62059d9eSEric Miao 	/* read and reset to default count value */
221*62059d9eSEric Miao 	kprec = KPREC;
222*62059d9eSEric Miao 	KPREC = DEFAULT_KPREC;
223*62059d9eSEric Miao 
224*62059d9eSEric Miao 	if (pdata->enable_rotary0)
225*62059d9eSEric Miao 		report_rotary_event(keypad, 0, rotary_delta(kprec));
226*62059d9eSEric Miao 
227*62059d9eSEric Miao 	if (pdata->enable_rotary1)
228*62059d9eSEric Miao 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
229*62059d9eSEric Miao }
230*62059d9eSEric Miao 
231*62059d9eSEric Miao static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
232*62059d9eSEric Miao {
233*62059d9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
234*62059d9eSEric Miao 	unsigned int new_state;
235*62059d9eSEric Miao 	uint32_t kpdk, bits_changed;
236*62059d9eSEric Miao 	int i;
237*62059d9eSEric Miao 
238*62059d9eSEric Miao 	kpdk = KPDK;
239*62059d9eSEric Miao 
240*62059d9eSEric Miao 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
241*62059d9eSEric Miao 		pxa27x_keypad_scan_rotary(keypad);
242*62059d9eSEric Miao 
243*62059d9eSEric Miao 	if (pdata->direct_key_map == NULL)
244*62059d9eSEric Miao 		return;
245*62059d9eSEric Miao 
246*62059d9eSEric Miao 	new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
247*62059d9eSEric Miao 	bits_changed = keypad->direct_key_state ^ new_state;
248*62059d9eSEric Miao 
249*62059d9eSEric Miao 	if (bits_changed == 0)
250*62059d9eSEric Miao 		return;
251*62059d9eSEric Miao 
252*62059d9eSEric Miao 	for (i = 0; i < pdata->direct_key_num; i++) {
253*62059d9eSEric Miao 		if (bits_changed & (1 << i))
254*62059d9eSEric Miao 			input_report_key(keypad->input_dev,
255*62059d9eSEric Miao 					pdata->direct_key_map[i],
256*62059d9eSEric Miao 					(new_state & (1 << i)));
257*62059d9eSEric Miao 	}
258*62059d9eSEric Miao 	input_sync(keypad->input_dev);
259*62059d9eSEric Miao 	keypad->direct_key_state = new_state;
260*62059d9eSEric Miao }
261*62059d9eSEric Miao 
2620e5f11aaSEric Miao static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
2630e5f11aaSEric Miao {
2641814db69SEric Miao 	struct pxa27x_keypad *keypad = dev_id;
2650e5f11aaSEric Miao 	unsigned long kpc = KPC;
2660e5f11aaSEric Miao 
267*62059d9eSEric Miao 	if (kpc & KPC_DI)
268*62059d9eSEric Miao 		pxa27x_keypad_scan_direct(keypad);
2690e5f11aaSEric Miao 
2701814db69SEric Miao 	if (kpc & KPC_MI)
2711814db69SEric Miao 		pxa27x_keypad_scan_matrix(keypad);
2720e5f11aaSEric Miao 
2730e5f11aaSEric Miao 	return IRQ_HANDLED;
2740e5f11aaSEric Miao }
2750e5f11aaSEric Miao 
276d7416f9eSEric Miao static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
277d7416f9eSEric Miao {
278d7416f9eSEric Miao 	struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
279*62059d9eSEric Miao 	unsigned int mask = 0, direct_key_num = 0;
280d7416f9eSEric Miao 	unsigned long kpc = 0;
281d7416f9eSEric Miao 
282d7416f9eSEric Miao 	/* enable matrix keys with automatic scan */
283d7416f9eSEric Miao 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
284d7416f9eSEric Miao 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
285d7416f9eSEric Miao 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
286d7416f9eSEric Miao 		       KPC_MKCN(pdata->matrix_key_cols);
287d7416f9eSEric Miao 	}
288d7416f9eSEric Miao 
289*62059d9eSEric Miao 	/* enable rotary key, debounce interval same as direct keys */
290*62059d9eSEric Miao 	if (pdata->enable_rotary0) {
291*62059d9eSEric Miao 		mask |= 0x03;
292*62059d9eSEric Miao 		direct_key_num = 2;
293*62059d9eSEric Miao 		kpc |= KPC_REE0;
294*62059d9eSEric Miao 	}
295d7416f9eSEric Miao 
296*62059d9eSEric Miao 	if (pdata->enable_rotary1) {
297*62059d9eSEric Miao 		mask |= 0x0c;
298*62059d9eSEric Miao 		direct_key_num = 4;
299*62059d9eSEric Miao 		kpc |= KPC_REE1;
300*62059d9eSEric Miao 	}
301*62059d9eSEric Miao 
302*62059d9eSEric Miao 	if (pdata->direct_key_num > direct_key_num)
303*62059d9eSEric Miao 		direct_key_num = pdata->direct_key_num;
304*62059d9eSEric Miao 
305*62059d9eSEric Miao 	keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
306*62059d9eSEric Miao 
307*62059d9eSEric Miao 	/* enable direct key */
308*62059d9eSEric Miao 	if (direct_key_num)
309*62059d9eSEric Miao 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
310*62059d9eSEric Miao 
311*62059d9eSEric Miao 	KPC = kpc | KPC_RE_ZERO_DEB;
312d7416f9eSEric Miao 	KPREC = DEFAULT_KPREC;
313d7416f9eSEric Miao }
314d7416f9eSEric Miao 
3150e5f11aaSEric Miao static int pxa27x_keypad_open(struct input_dev *dev)
3160e5f11aaSEric Miao {
3171814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
3181814db69SEric Miao 
3190e5f11aaSEric Miao 	/* Enable unit clock */
3201814db69SEric Miao 	clk_enable(keypad->clk);
321d7416f9eSEric Miao 	pxa27x_keypad_config(keypad);
3220e5f11aaSEric Miao 
3230e5f11aaSEric Miao 	return 0;
3240e5f11aaSEric Miao }
3250e5f11aaSEric Miao 
3260e5f11aaSEric Miao static void pxa27x_keypad_close(struct input_dev *dev)
3270e5f11aaSEric Miao {
3281814db69SEric Miao 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
3291814db69SEric Miao 
3300e5f11aaSEric Miao 	/* Disable clock unit */
3311814db69SEric Miao 	clk_disable(keypad->clk);
3320e5f11aaSEric Miao }
3330e5f11aaSEric Miao 
3340e5f11aaSEric Miao #ifdef CONFIG_PM
3350e5f11aaSEric Miao static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
3360e5f11aaSEric Miao {
3371814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
3380e5f11aaSEric Miao 
339d7416f9eSEric Miao 	clk_disable(keypad->clk);
3400e5f11aaSEric Miao 	return 0;
3410e5f11aaSEric Miao }
3420e5f11aaSEric Miao 
3430e5f11aaSEric Miao static int pxa27x_keypad_resume(struct platform_device *pdev)
3440e5f11aaSEric Miao {
3451814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
3461814db69SEric Miao 	struct input_dev *input_dev = keypad->input_dev;
3470e5f11aaSEric Miao 
3480e5f11aaSEric Miao 	mutex_lock(&input_dev->mutex);
3490e5f11aaSEric Miao 
3500e5f11aaSEric Miao 	if (input_dev->users) {
3510e5f11aaSEric Miao 		/* Enable unit clock */
3521814db69SEric Miao 		clk_enable(keypad->clk);
353d7416f9eSEric Miao 		pxa27x_keypad_config(keypad);
3540e5f11aaSEric Miao 	}
3550e5f11aaSEric Miao 
3560e5f11aaSEric Miao 	mutex_unlock(&input_dev->mutex);
3570e5f11aaSEric Miao 
3580e5f11aaSEric Miao 	return 0;
3590e5f11aaSEric Miao }
3600e5f11aaSEric Miao #else
3610e5f11aaSEric Miao #define pxa27x_keypad_suspend	NULL
3620e5f11aaSEric Miao #define pxa27x_keypad_resume	NULL
3630e5f11aaSEric Miao #endif
3640e5f11aaSEric Miao 
3650e5f11aaSEric Miao static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
3660e5f11aaSEric Miao {
3671814db69SEric Miao 	struct pxa27x_keypad *keypad;
3680e5f11aaSEric Miao 	struct input_dev *input_dev;
369d7416f9eSEric Miao 	int error;
3700e5f11aaSEric Miao 
3711814db69SEric Miao 	keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
3721814db69SEric Miao 	if (keypad == NULL) {
3731814db69SEric Miao 		dev_err(&pdev->dev, "failed to allocate driver data\n");
3741814db69SEric Miao 		return -ENOMEM;
3751814db69SEric Miao 	}
3761814db69SEric Miao 
3771814db69SEric Miao 	keypad->pdata = pdev->dev.platform_data;
3781814db69SEric Miao 	if (keypad->pdata == NULL) {
3791814db69SEric Miao 		dev_err(&pdev->dev, "no platform data defined\n");
3801814db69SEric Miao 		error = -EINVAL;
3811814db69SEric Miao 		goto failed_free;
3821814db69SEric Miao 	}
3831814db69SEric Miao 
3841814db69SEric Miao 	keypad->clk = clk_get(&pdev->dev, "KBDCLK");
3851814db69SEric Miao 	if (IS_ERR(keypad->clk)) {
3861814db69SEric Miao 		dev_err(&pdev->dev, "failed to get keypad clock\n");
3871814db69SEric Miao 		error = PTR_ERR(keypad->clk);
3881814db69SEric Miao 		goto failed_free;
3890e5f11aaSEric Miao 	}
3900e5f11aaSEric Miao 
3910e5f11aaSEric Miao 	/* Create and register the input driver. */
3920e5f11aaSEric Miao 	input_dev = input_allocate_device();
3930e5f11aaSEric Miao 	if (!input_dev) {
3941814db69SEric Miao 		dev_err(&pdev->dev, "failed to allocate input device\n");
3950e5f11aaSEric Miao 		error = -ENOMEM;
3961814db69SEric Miao 		goto failed_put_clk;
3970e5f11aaSEric Miao 	}
3980e5f11aaSEric Miao 
3990e5f11aaSEric Miao 	input_dev->name = DRIVER_NAME;
4000e5f11aaSEric Miao 	input_dev->id.bustype = BUS_HOST;
4010e5f11aaSEric Miao 	input_dev->open = pxa27x_keypad_open;
4020e5f11aaSEric Miao 	input_dev->close = pxa27x_keypad_close;
4030e5f11aaSEric Miao 	input_dev->dev.parent = &pdev->dev;
4040e5f11aaSEric Miao 
4051814db69SEric Miao 	keypad->input_dev = input_dev;
4061814db69SEric Miao 	input_set_drvdata(input_dev, keypad);
4071814db69SEric Miao 
4080e5f11aaSEric Miao 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
4090e5f11aaSEric Miao 		BIT_MASK(EV_REL);
4101814db69SEric Miao 
4111814db69SEric Miao 	pxa27x_keypad_build_keycode(keypad);
4120e5f11aaSEric Miao 
4130e5f11aaSEric Miao 	error = request_irq(IRQ_KEYPAD, pxa27x_keypad_irq_handler, IRQF_DISABLED,
4141814db69SEric Miao 			    DRIVER_NAME, keypad);
4150e5f11aaSEric Miao 	if (error) {
4160e5f11aaSEric Miao 		printk(KERN_ERR "Cannot request keypad IRQ\n");
4170e5f11aaSEric Miao 		goto err_free_dev;
4180e5f11aaSEric Miao 	}
4190e5f11aaSEric Miao 
4201814db69SEric Miao 	platform_set_drvdata(pdev, keypad);
4210e5f11aaSEric Miao 
4220e5f11aaSEric Miao 	/* Register the input device */
4230e5f11aaSEric Miao 	error = input_register_device(input_dev);
4240e5f11aaSEric Miao 	if (error)
4250e5f11aaSEric Miao 		goto err_free_irq;
4260e5f11aaSEric Miao 
4270e5f11aaSEric Miao 	return 0;
4280e5f11aaSEric Miao 
4290e5f11aaSEric Miao  err_free_irq:
4300e5f11aaSEric Miao 	platform_set_drvdata(pdev, NULL);
4310e5f11aaSEric Miao 	free_irq(IRQ_KEYPAD, pdev);
4320e5f11aaSEric Miao  err_free_dev:
4330e5f11aaSEric Miao 	input_free_device(input_dev);
4341814db69SEric Miao failed_put_clk:
4351814db69SEric Miao 	clk_put(keypad->clk);
4361814db69SEric Miao failed_free:
4371814db69SEric Miao 	kfree(keypad);
4380e5f11aaSEric Miao 	return error;
4390e5f11aaSEric Miao }
4400e5f11aaSEric Miao 
4410e5f11aaSEric Miao static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
4420e5f11aaSEric Miao {
4431814db69SEric Miao 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
4440e5f11aaSEric Miao 
4450e5f11aaSEric Miao 	free_irq(IRQ_KEYPAD, pdev);
4460e5f11aaSEric Miao 
4471814db69SEric Miao 	clk_disable(keypad->clk);
4481814db69SEric Miao 	clk_put(keypad->clk);
4491814db69SEric Miao 
4501814db69SEric Miao 	input_unregister_device(keypad->input_dev);
4511814db69SEric Miao 
4521814db69SEric Miao 	platform_set_drvdata(pdev, NULL);
4531814db69SEric Miao 	kfree(keypad);
4540e5f11aaSEric Miao 	return 0;
4550e5f11aaSEric Miao }
4560e5f11aaSEric Miao 
4570e5f11aaSEric Miao static struct platform_driver pxa27x_keypad_driver = {
4580e5f11aaSEric Miao 	.probe		= pxa27x_keypad_probe,
4590e5f11aaSEric Miao 	.remove		= __devexit_p(pxa27x_keypad_remove),
4600e5f11aaSEric Miao 	.suspend	= pxa27x_keypad_suspend,
4610e5f11aaSEric Miao 	.resume		= pxa27x_keypad_resume,
4620e5f11aaSEric Miao 	.driver		= {
4630e5f11aaSEric Miao 		.name	= DRIVER_NAME,
4640e5f11aaSEric Miao 	},
4650e5f11aaSEric Miao };
4660e5f11aaSEric Miao 
4670e5f11aaSEric Miao static int __init pxa27x_keypad_init(void)
4680e5f11aaSEric Miao {
4690e5f11aaSEric Miao 	return platform_driver_register(&pxa27x_keypad_driver);
4700e5f11aaSEric Miao }
4710e5f11aaSEric Miao 
4720e5f11aaSEric Miao static void __exit pxa27x_keypad_exit(void)
4730e5f11aaSEric Miao {
4740e5f11aaSEric Miao 	platform_driver_unregister(&pxa27x_keypad_driver);
4750e5f11aaSEric Miao }
4760e5f11aaSEric Miao 
4770e5f11aaSEric Miao module_init(pxa27x_keypad_init);
4780e5f11aaSEric Miao module_exit(pxa27x_keypad_exit);
4790e5f11aaSEric Miao 
4800e5f11aaSEric Miao MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
4810e5f11aaSEric Miao MODULE_LICENSE("GPL");
482