xref: /linux/drivers/input/keyboard/pxa27x_keypad.c (revision d05426779b96767f0c306cbeb8bb77d7e70eb36e)
1 /*
2  * linux/drivers/input/keyboard/pxa27x_keypad.c
3  *
4  * Driver for the pxa27x matrix keyboard controller.
5  *
6  * Created:	Feb 22, 2007
7  * Author:	Rodolfo Giometti <giometti@linux.it>
8  *
9  * Based on a previous implementations by Kevin O'Connor
10  * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
11  * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17 
18 
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/input.h>
23 #include <linux/io.h>
24 #include <linux/device.h>
25 #include <linux/platform_device.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/input/matrix_keypad.h>
29 #include <linux/slab.h>
30 #include <linux/of.h>
31 
32 #include <linux/platform_data/keypad-pxa27x.h>
33 /*
34  * Keypad Controller registers
35  */
36 #define KPC             0x0000 /* Keypad Control register */
37 #define KPDK            0x0008 /* Keypad Direct Key register */
38 #define KPREC           0x0010 /* Keypad Rotary Encoder register */
39 #define KPMK            0x0018 /* Keypad Matrix Key register */
40 #define KPAS            0x0020 /* Keypad Automatic Scan register */
41 
42 /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
43 #define KPASMKP0        0x0028
44 #define KPASMKP1        0x0030
45 #define KPASMKP2        0x0038
46 #define KPASMKP3        0x0040
47 #define KPKDI           0x0048
48 
49 /* bit definitions */
50 #define KPC_MKRN(n)	((((n) - 1) & 0x7) << 26) /* matrix key row number */
51 #define KPC_MKCN(n)	((((n) - 1) & 0x7) << 23) /* matrix key column number */
52 #define KPC_DKN(n)	((((n) - 1) & 0x7) << 6)  /* direct key number */
53 
54 #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
55 #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
56 #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
57 #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
58 
59 #define KPC_MS(n)	(0x1 << (13 + (n)))	/* Matrix scan line 'n' */
60 #define KPC_MS_ALL      (0xff << 13)
61 
62 #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
63 #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
64 #define KPC_DK_DEB_SEL	(0x1 <<  9)  /* Direct Keypad Debounce Select */
65 #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
66 #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
67 #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
68 #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
69 #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
70 #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
71 
72 #define KPDK_DKP        (0x1 << 31)
73 #define KPDK_DK(n)	((n) & 0xff)
74 
75 #define KPREC_OF1       (0x1 << 31)
76 #define kPREC_UF1       (0x1 << 30)
77 #define KPREC_OF0       (0x1 << 15)
78 #define KPREC_UF0       (0x1 << 14)
79 
80 #define KPREC_RECOUNT0(n)	((n) & 0xff)
81 #define KPREC_RECOUNT1(n)	(((n) >> 16) & 0xff)
82 
83 #define KPMK_MKP        (0x1 << 31)
84 #define KPAS_SO         (0x1 << 31)
85 #define KPASMKPx_SO     (0x1 << 31)
86 
87 #define KPAS_MUKP(n)	(((n) >> 26) & 0x1f)
88 #define KPAS_RP(n)	(((n) >> 4) & 0xf)
89 #define KPAS_CP(n)	((n) & 0xf)
90 
91 #define KPASMKP_MKC_MASK	(0xff)
92 
93 #define keypad_readl(off)	__raw_readl(keypad->mmio_base + (off))
94 #define keypad_writel(off, v)	__raw_writel((v), keypad->mmio_base + (off))
95 
96 #define MAX_MATRIX_KEY_NUM	(MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
97 #define MAX_KEYPAD_KEYS		(MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
98 
99 struct pxa27x_keypad {
100 	const struct pxa27x_keypad_platform_data *pdata;
101 
102 	struct clk *clk;
103 	struct input_dev *input_dev;
104 	void __iomem *mmio_base;
105 
106 	int irq;
107 
108 	unsigned short keycodes[MAX_KEYPAD_KEYS];
109 	int rotary_rel_code[2];
110 
111 	unsigned int row_shift;
112 
113 	/* state row bits of each column scan */
114 	uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
115 	uint32_t direct_key_state;
116 
117 	unsigned int direct_key_mask;
118 };
119 
120 #ifdef CONFIG_OF
121 static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad,
122 				struct pxa27x_keypad_platform_data *pdata)
123 {
124 	struct input_dev *input_dev = keypad->input_dev;
125 	struct device *dev = input_dev->dev.parent;
126 	u32 rows, cols;
127 	int error;
128 
129 	error = matrix_keypad_parse_of_params(dev, &rows, &cols);
130 	if (error)
131 		return error;
132 
133 	if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
134 		dev_err(dev, "rows or cols exceeds maximum value\n");
135 		return -EINVAL;
136 	}
137 
138 	pdata->matrix_key_rows = rows;
139 	pdata->matrix_key_cols = cols;
140 
141 	error = matrix_keypad_build_keymap(NULL, NULL,
142 					   pdata->matrix_key_rows,
143 					   pdata->matrix_key_cols,
144 					   keypad->keycodes, input_dev);
145 	if (error)
146 		return error;
147 
148 	return 0;
149 }
150 
151 static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad,
152 				struct pxa27x_keypad_platform_data *pdata)
153 {
154 	struct input_dev *input_dev = keypad->input_dev;
155 	struct device *dev = input_dev->dev.parent;
156 	struct device_node *np = dev->of_node;
157 	const __be16 *prop;
158 	unsigned short code;
159 	unsigned int proplen, size;
160 	int i;
161 	int error;
162 
163 	error = of_property_read_u32(np, "marvell,direct-key-count",
164 				     &pdata->direct_key_num);
165 	if (error) {
166 		/*
167 		 * If do not have marvel,direct-key-count defined,
168 		 * it means direct key is not supported.
169 		 */
170 		return error == -EINVAL ? 0 : error;
171 	}
172 
173 	error = of_property_read_u32(np, "marvell,direct-key-mask",
174 				     &pdata->direct_key_mask);
175 	if (error) {
176 		if (error != -EINVAL)
177 			return error;
178 
179 		/*
180 		 * If marvell,direct-key-mask is not defined, driver will use
181 		 * default value. Default value is set when configure the keypad.
182 		 */
183 		pdata->direct_key_mask = 0;
184 	}
185 
186 	pdata->direct_key_low_active = of_property_read_bool(np,
187 					"marvell,direct-key-low-active");
188 
189 	prop = of_get_property(np, "marvell,direct-key-map", &proplen);
190 	if (!prop)
191 		return -EINVAL;
192 
193 	if (proplen % sizeof(u16))
194 		return -EINVAL;
195 
196 	size = proplen / sizeof(u16);
197 
198 	/* Only MAX_DIRECT_KEY_NUM is accepted.*/
199 	if (size > MAX_DIRECT_KEY_NUM)
200 		return -EINVAL;
201 
202 	for (i = 0; i < size; i++) {
203 		code = be16_to_cpup(prop + i);
204 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
205 		__set_bit(code, input_dev->keybit);
206 	}
207 
208 	return 0;
209 }
210 
211 static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad,
212 				struct pxa27x_keypad_platform_data *pdata)
213 {
214 	const __be32 *prop;
215 	int i, relkey_ret;
216 	unsigned int code, proplen;
217 	const char *rotaryname[2] = {
218 			"marvell,rotary0", "marvell,rotary1"};
219 	const char relkeyname[] = {"marvell,rotary-rel-key"};
220 	struct input_dev *input_dev = keypad->input_dev;
221 	struct device *dev = input_dev->dev.parent;
222 	struct device_node *np = dev->of_node;
223 
224 	relkey_ret = of_property_read_u32(np, relkeyname, &code);
225 	/* if can read correct rotary key-code, we do not need this. */
226 	if (relkey_ret == 0) {
227 		unsigned short relcode;
228 
229 		/* rotary0 taks lower half, rotary1 taks upper half. */
230 		relcode = code & 0xffff;
231 		pdata->rotary0_rel_code = (code & 0xffff);
232 		__set_bit(relcode, input_dev->relbit);
233 
234 		relcode = code >> 16;
235 		pdata->rotary1_rel_code = relcode;
236 		__set_bit(relcode, input_dev->relbit);
237 	}
238 
239 	for (i = 0; i < 2; i++) {
240 		prop = of_get_property(np, rotaryname[i], &proplen);
241 		/*
242 		 * If the prop is not set, it means keypad does not need
243 		 * initialize the rotaryX.
244 		 */
245 		if (!prop)
246 			continue;
247 
248 		code = be32_to_cpup(prop);
249 		/*
250 		 * Not all up/down key code are valid.
251 		 * Now we depends on direct-rel-code.
252 		 */
253 		if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
254 			return relkey_ret;
255 		} else {
256 			unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
257 			unsigned short keycode;
258 
259 			keycode = code & 0xffff;
260 			keypad->keycodes[n] = keycode;
261 			__set_bit(keycode, input_dev->keybit);
262 
263 			keycode = code >> 16;
264 			keypad->keycodes[n + 1] = keycode;
265 			__set_bit(keycode, input_dev->keybit);
266 
267 			if (i == 0)
268 				pdata->rotary0_rel_code = -1;
269 			else
270 				pdata->rotary1_rel_code = -1;
271 		}
272 		if (i == 0)
273 			pdata->enable_rotary0 = 1;
274 		else
275 			pdata->enable_rotary1 = 1;
276 	}
277 
278 	keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
279 	keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
280 
281 	return 0;
282 }
283 
284 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
285 {
286 	struct input_dev *input_dev = keypad->input_dev;
287 	struct device *dev = input_dev->dev.parent;
288 	struct device_node *np = dev->of_node;
289 	struct pxa27x_keypad_platform_data *pdata;
290 	int error;
291 
292 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
293 	if (!pdata) {
294 		dev_err(dev, "failed to allocate memory for pdata\n");
295 		return -ENOMEM;
296 	}
297 
298 	error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata);
299 	if (error) {
300 		dev_err(dev, "failed to parse matrix key\n");
301 		return error;
302 	}
303 
304 	error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata);
305 	if (error) {
306 		dev_err(dev, "failed to parse direct key\n");
307 		return error;
308 	}
309 
310 	error = pxa27x_keypad_rotary_parse_dt(keypad, pdata);
311 	if (error) {
312 		dev_err(dev, "failed to parse rotary key\n");
313 		return error;
314 	}
315 
316 	error = of_property_read_u32(np, "marvell,debounce-interval",
317 				     &pdata->debounce_interval);
318 	if (error) {
319 		dev_err(dev, "failed to parse debpunce-interval\n");
320 		return error;
321 	}
322 
323 	/*
324 	 * The keycodes may not only includes matrix key but also the direct
325 	 * key or rotary key.
326 	 */
327 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
328 
329 	keypad->pdata = pdata;
330 	return 0;
331 }
332 
333 #else
334 
335 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
336 {
337 	dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
338 
339 	return -EINVAL;
340 }
341 
342 #endif
343 
344 static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
345 {
346 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
347 	struct input_dev *input_dev = keypad->input_dev;
348 	const struct matrix_keymap_data *keymap_data =
349 				pdata ? pdata->matrix_keymap_data : NULL;
350 	unsigned short keycode;
351 	int i;
352 	int error;
353 
354 	error = matrix_keypad_build_keymap(keymap_data, NULL,
355 					   pdata->matrix_key_rows,
356 					   pdata->matrix_key_cols,
357 					   keypad->keycodes, input_dev);
358 	if (error)
359 		return error;
360 
361 	/*
362 	 * The keycodes may not only include matrix keys but also the direct
363 	 * or rotary keys.
364 	 */
365 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
366 
367 	/* For direct keys. */
368 	for (i = 0; i < pdata->direct_key_num; i++) {
369 		keycode = pdata->direct_key_map[i];
370 		keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
371 		__set_bit(keycode, input_dev->keybit);
372 	}
373 
374 	if (pdata->enable_rotary0) {
375 		if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
376 			keycode = pdata->rotary0_up_key;
377 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
378 			__set_bit(keycode, input_dev->keybit);
379 
380 			keycode = pdata->rotary0_down_key;
381 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
382 			__set_bit(keycode, input_dev->keybit);
383 
384 			keypad->rotary_rel_code[0] = -1;
385 		} else {
386 			keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
387 			__set_bit(pdata->rotary0_rel_code, input_dev->relbit);
388 		}
389 	}
390 
391 	if (pdata->enable_rotary1) {
392 		if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
393 			keycode = pdata->rotary1_up_key;
394 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
395 			__set_bit(keycode, input_dev->keybit);
396 
397 			keycode = pdata->rotary1_down_key;
398 			keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
399 			__set_bit(keycode, input_dev->keybit);
400 
401 			keypad->rotary_rel_code[1] = -1;
402 		} else {
403 			keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
404 			__set_bit(pdata->rotary1_rel_code, input_dev->relbit);
405 		}
406 	}
407 
408 	__clear_bit(KEY_RESERVED, input_dev->keybit);
409 
410 	return 0;
411 }
412 
413 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
414 {
415 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
416 	struct input_dev *input_dev = keypad->input_dev;
417 	int row, col, num_keys_pressed = 0;
418 	uint32_t new_state[MAX_MATRIX_KEY_COLS];
419 	uint32_t kpas = keypad_readl(KPAS);
420 
421 	num_keys_pressed = KPAS_MUKP(kpas);
422 
423 	memset(new_state, 0, sizeof(new_state));
424 
425 	if (num_keys_pressed == 0)
426 		goto scan;
427 
428 	if (num_keys_pressed == 1) {
429 		col = KPAS_CP(kpas);
430 		row = KPAS_RP(kpas);
431 
432 		/* if invalid row/col, treat as no key pressed */
433 		if (col >= pdata->matrix_key_cols ||
434 		    row >= pdata->matrix_key_rows)
435 			goto scan;
436 
437 		new_state[col] = (1 << row);
438 		goto scan;
439 	}
440 
441 	if (num_keys_pressed > 1) {
442 		uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
443 		uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
444 		uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
445 		uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
446 
447 		new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
448 		new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
449 		new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
450 		new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
451 		new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
452 		new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
453 		new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
454 		new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
455 	}
456 scan:
457 	for (col = 0; col < pdata->matrix_key_cols; col++) {
458 		uint32_t bits_changed;
459 		int code;
460 
461 		bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
462 		if (bits_changed == 0)
463 			continue;
464 
465 		for (row = 0; row < pdata->matrix_key_rows; row++) {
466 			if ((bits_changed & (1 << row)) == 0)
467 				continue;
468 
469 			code = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
470 
471 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
472 			input_report_key(input_dev, keypad->keycodes[code],
473 					 new_state[col] & (1 << row));
474 		}
475 	}
476 	input_sync(input_dev);
477 	memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
478 }
479 
480 #define DEFAULT_KPREC	(0x007f007f)
481 
482 static inline int rotary_delta(uint32_t kprec)
483 {
484 	if (kprec & KPREC_OF0)
485 		return (kprec & 0xff) + 0x7f;
486 	else if (kprec & KPREC_UF0)
487 		return (kprec & 0xff) - 0x7f - 0xff;
488 	else
489 		return (kprec & 0xff) - 0x7f;
490 }
491 
492 static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
493 {
494 	struct input_dev *dev = keypad->input_dev;
495 
496 	if (delta == 0)
497 		return;
498 
499 	if (keypad->rotary_rel_code[r] == -1) {
500 		int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
501 		unsigned char keycode = keypad->keycodes[code];
502 
503 		/* simulate a press-n-release */
504 		input_event(dev, EV_MSC, MSC_SCAN, code);
505 		input_report_key(dev, keycode, 1);
506 		input_sync(dev);
507 		input_event(dev, EV_MSC, MSC_SCAN, code);
508 		input_report_key(dev, keycode, 0);
509 		input_sync(dev);
510 	} else {
511 		input_report_rel(dev, keypad->rotary_rel_code[r], delta);
512 		input_sync(dev);
513 	}
514 }
515 
516 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
517 {
518 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
519 	uint32_t kprec;
520 
521 	/* read and reset to default count value */
522 	kprec = keypad_readl(KPREC);
523 	keypad_writel(KPREC, DEFAULT_KPREC);
524 
525 	if (pdata->enable_rotary0)
526 		report_rotary_event(keypad, 0, rotary_delta(kprec));
527 
528 	if (pdata->enable_rotary1)
529 		report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
530 }
531 
532 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
533 {
534 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
535 	struct input_dev *input_dev = keypad->input_dev;
536 	unsigned int new_state;
537 	uint32_t kpdk, bits_changed;
538 	int i;
539 
540 	kpdk = keypad_readl(KPDK);
541 
542 	if (pdata->enable_rotary0 || pdata->enable_rotary1)
543 		pxa27x_keypad_scan_rotary(keypad);
544 
545 	/*
546 	 * The KPDR_DK only output the key pin level, so it relates to board,
547 	 * and low level may be active.
548 	 */
549 	if (pdata->direct_key_low_active)
550 		new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
551 	else
552 		new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
553 
554 	bits_changed = keypad->direct_key_state ^ new_state;
555 
556 	if (bits_changed == 0)
557 		return;
558 
559 	for (i = 0; i < pdata->direct_key_num; i++) {
560 		if (bits_changed & (1 << i)) {
561 			int code = MAX_MATRIX_KEY_NUM + i;
562 
563 			input_event(input_dev, EV_MSC, MSC_SCAN, code);
564 			input_report_key(input_dev, keypad->keycodes[code],
565 					 new_state & (1 << i));
566 		}
567 	}
568 	input_sync(input_dev);
569 	keypad->direct_key_state = new_state;
570 }
571 
572 static void clear_wakeup_event(struct pxa27x_keypad *keypad)
573 {
574 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
575 
576 	if (pdata->clear_wakeup_event)
577 		(pdata->clear_wakeup_event)();
578 }
579 
580 static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
581 {
582 	struct pxa27x_keypad *keypad = dev_id;
583 	unsigned long kpc = keypad_readl(KPC);
584 
585 	clear_wakeup_event(keypad);
586 
587 	if (kpc & KPC_DI)
588 		pxa27x_keypad_scan_direct(keypad);
589 
590 	if (kpc & KPC_MI)
591 		pxa27x_keypad_scan_matrix(keypad);
592 
593 	return IRQ_HANDLED;
594 }
595 
596 static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
597 {
598 	const struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
599 	unsigned int mask = 0, direct_key_num = 0;
600 	unsigned long kpc = 0;
601 
602 	/* clear pending interrupt bit */
603 	keypad_readl(KPC);
604 
605 	/* enable matrix keys with automatic scan */
606 	if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
607 		kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
608 		kpc |= KPC_MKRN(pdata->matrix_key_rows) |
609 		       KPC_MKCN(pdata->matrix_key_cols);
610 	}
611 
612 	/* enable rotary key, debounce interval same as direct keys */
613 	if (pdata->enable_rotary0) {
614 		mask |= 0x03;
615 		direct_key_num = 2;
616 		kpc |= KPC_REE0;
617 	}
618 
619 	if (pdata->enable_rotary1) {
620 		mask |= 0x0c;
621 		direct_key_num = 4;
622 		kpc |= KPC_REE1;
623 	}
624 
625 	if (pdata->direct_key_num > direct_key_num)
626 		direct_key_num = pdata->direct_key_num;
627 
628 	/*
629 	 * Direct keys usage may not start from KP_DKIN0, check the platfrom
630 	 * mask data to config the specific.
631 	 */
632 	if (pdata->direct_key_mask)
633 		keypad->direct_key_mask = pdata->direct_key_mask;
634 	else
635 		keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
636 
637 	/* enable direct key */
638 	if (direct_key_num)
639 		kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
640 
641 	keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
642 	keypad_writel(KPREC, DEFAULT_KPREC);
643 	keypad_writel(KPKDI, pdata->debounce_interval);
644 }
645 
646 static int pxa27x_keypad_open(struct input_dev *dev)
647 {
648 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
649 
650 	/* Enable unit clock */
651 	clk_prepare_enable(keypad->clk);
652 	pxa27x_keypad_config(keypad);
653 
654 	return 0;
655 }
656 
657 static void pxa27x_keypad_close(struct input_dev *dev)
658 {
659 	struct pxa27x_keypad *keypad = input_get_drvdata(dev);
660 
661 	/* Disable clock unit */
662 	clk_disable_unprepare(keypad->clk);
663 }
664 
665 #ifdef CONFIG_PM_SLEEP
666 static int pxa27x_keypad_suspend(struct device *dev)
667 {
668 	struct platform_device *pdev = to_platform_device(dev);
669 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
670 
671 	/*
672 	 * If the keypad is used a wake up source, clock can not be disabled.
673 	 * Or it can not detect the key pressing.
674 	 */
675 	if (device_may_wakeup(&pdev->dev))
676 		enable_irq_wake(keypad->irq);
677 	else
678 		clk_disable_unprepare(keypad->clk);
679 
680 	return 0;
681 }
682 
683 static int pxa27x_keypad_resume(struct device *dev)
684 {
685 	struct platform_device *pdev = to_platform_device(dev);
686 	struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
687 	struct input_dev *input_dev = keypad->input_dev;
688 
689 	/*
690 	 * If the keypad is used as wake up source, the clock is not turned
691 	 * off. So do not need configure it again.
692 	 */
693 	if (device_may_wakeup(&pdev->dev)) {
694 		disable_irq_wake(keypad->irq);
695 	} else {
696 		mutex_lock(&input_dev->mutex);
697 
698 		if (input_dev->users) {
699 			/* Enable unit clock */
700 			clk_prepare_enable(keypad->clk);
701 			pxa27x_keypad_config(keypad);
702 		}
703 
704 		mutex_unlock(&input_dev->mutex);
705 	}
706 
707 	return 0;
708 }
709 #endif
710 
711 static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
712 			 pxa27x_keypad_suspend, pxa27x_keypad_resume);
713 
714 
715 static int pxa27x_keypad_probe(struct platform_device *pdev)
716 {
717 	const struct pxa27x_keypad_platform_data *pdata =
718 					dev_get_platdata(&pdev->dev);
719 	struct device_node *np = pdev->dev.of_node;
720 	struct pxa27x_keypad *keypad;
721 	struct input_dev *input_dev;
722 	struct resource *res;
723 	int irq, error;
724 
725 	/* Driver need build keycode from device tree or pdata */
726 	if (!np && !pdata)
727 		return -EINVAL;
728 
729 	irq = platform_get_irq(pdev, 0);
730 	if (irq < 0) {
731 		dev_err(&pdev->dev, "failed to get keypad irq\n");
732 		return -ENXIO;
733 	}
734 
735 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
736 	if (res == NULL) {
737 		dev_err(&pdev->dev, "failed to get I/O memory\n");
738 		return -ENXIO;
739 	}
740 
741 	keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad),
742 			      GFP_KERNEL);
743 	if (!keypad)
744 		return -ENOMEM;
745 
746 	input_dev = devm_input_allocate_device(&pdev->dev);
747 	if (!input_dev)
748 		return -ENOMEM;
749 
750 	keypad->pdata = pdata;
751 	keypad->input_dev = input_dev;
752 	keypad->irq = irq;
753 
754 	keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
755 	if (IS_ERR(keypad->mmio_base))
756 		return PTR_ERR(keypad->mmio_base);
757 
758 	keypad->clk = devm_clk_get(&pdev->dev, NULL);
759 	if (IS_ERR(keypad->clk)) {
760 		dev_err(&pdev->dev, "failed to get keypad clock\n");
761 		return PTR_ERR(keypad->clk);
762 	}
763 
764 	input_dev->name = pdev->name;
765 	input_dev->id.bustype = BUS_HOST;
766 	input_dev->open = pxa27x_keypad_open;
767 	input_dev->close = pxa27x_keypad_close;
768 	input_dev->dev.parent = &pdev->dev;
769 
770 	input_dev->keycode = keypad->keycodes;
771 	input_dev->keycodesize = sizeof(keypad->keycodes[0]);
772 	input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
773 
774 	input_set_drvdata(input_dev, keypad);
775 
776 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
777 	input_set_capability(input_dev, EV_MSC, MSC_SCAN);
778 
779 	if (pdata) {
780 		error = pxa27x_keypad_build_keycode(keypad);
781 	} else {
782 		error = pxa27x_keypad_build_keycode_from_dt(keypad);
783 		/*
784 		 * Data that we get from DT resides in dynamically
785 		 * allocated memory so we need to update our pdata
786 		 * pointer.
787 		 */
788 		pdata = keypad->pdata;
789 	}
790 	if (error) {
791 		dev_err(&pdev->dev, "failed to build keycode\n");
792 		return error;
793 	}
794 
795 	keypad->row_shift = get_count_order(pdata->matrix_key_cols);
796 
797 	if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
798 	    (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
799 		input_dev->evbit[0] |= BIT_MASK(EV_REL);
800 	}
801 
802 	error = devm_request_irq(&pdev->dev, irq, pxa27x_keypad_irq_handler,
803 				 0, pdev->name, keypad);
804 	if (error) {
805 		dev_err(&pdev->dev, "failed to request IRQ\n");
806 		return error;
807 	}
808 
809 	/* Register the input device */
810 	error = input_register_device(input_dev);
811 	if (error) {
812 		dev_err(&pdev->dev, "failed to register input device\n");
813 		return error;
814 	}
815 
816 	platform_set_drvdata(pdev, keypad);
817 	device_init_wakeup(&pdev->dev, 1);
818 
819 	return 0;
820 }
821 
822 #ifdef CONFIG_OF
823 static const struct of_device_id pxa27x_keypad_dt_match[] = {
824 	{ .compatible = "marvell,pxa27x-keypad" },
825 	{},
826 };
827 MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
828 #endif
829 
830 static struct platform_driver pxa27x_keypad_driver = {
831 	.probe		= pxa27x_keypad_probe,
832 	.driver		= {
833 		.name	= "pxa27x-keypad",
834 		.of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
835 		.pm	= &pxa27x_keypad_pm_ops,
836 	},
837 };
838 module_platform_driver(pxa27x_keypad_driver);
839 
840 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
841 MODULE_LICENSE("GPL");
842 /* work with hotplug and coldplug */
843 MODULE_ALIAS("platform:pxa27x-keypad");
844