2 * linux/drivers/input/keyboard/pxa27x_keypad.c
4 * Driver for the pxa27x matrix keyboard controller.
6 * Created: Feb 22, 2007
7 * Author: Rodolfo Giometti <giometti@linux.it>
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>.
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.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/interrupt.h>
22 #include <linux/input.h>
23 #include <linux/device.h>
24 #include <linux/platform_device.h>
25 #include <linux/clk.h>
26 #include <linux/err.h>
27 #include <linux/input/matrix_keypad.h>
28 #include <linux/slab.h>
31 #include <asm/mach/arch.h>
32 #include <asm/mach/map.h>
34 #include <mach/hardware.h>
35 #include <linux/platform_data/keypad-pxa27x.h>
37 * Keypad Controller registers
39 #define KPC 0x0000 /* Keypad Control register */
40 #define KPDK 0x0008 /* Keypad Direct Key register */
41 #define KPREC 0x0010 /* Keypad Rotary Encoder register */
42 #define KPMK 0x0018 /* Keypad Matrix Key register */
43 #define KPAS 0x0020 /* Keypad Automatic Scan register */
45 /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
46 #define KPASMKP0 0x0028
47 #define KPASMKP1 0x0030
48 #define KPASMKP2 0x0038
49 #define KPASMKP3 0x0040
53 #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */
54 #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */
55 #define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */
57 #define KPC_AS (0x1 << 30) /* Automatic Scan bit */
58 #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
59 #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
60 #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
62 #define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */
63 #define KPC_MS_ALL (0xff << 13)
65 #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
66 #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
67 #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
68 #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
69 #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
70 #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
71 #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
72 #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
73 #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
75 #define KPDK_DKP (0x1 << 31)
76 #define KPDK_DK(n) ((n) & 0xff)
78 #define KPREC_OF1 (0x1 << 31)
79 #define kPREC_UF1 (0x1 << 30)
80 #define KPREC_OF0 (0x1 << 15)
81 #define KPREC_UF0 (0x1 << 14)
83 #define KPREC_RECOUNT0(n) ((n) & 0xff)
84 #define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff)
86 #define KPMK_MKP (0x1 << 31)
87 #define KPAS_SO (0x1 << 31)
88 #define KPASMKPx_SO (0x1 << 31)
90 #define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
91 #define KPAS_RP(n) (((n) >> 4) & 0xf)
92 #define KPAS_CP(n) ((n) & 0xf)
94 #define KPASMKP_MKC_MASK (0xff)
96 #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off))
97 #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off))
99 #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
100 #define MAX_KEYPAD_KEYS (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
102 struct pxa27x_keypad
{
103 const struct pxa27x_keypad_platform_data
*pdata
;
106 struct input_dev
*input_dev
;
107 void __iomem
*mmio_base
;
111 unsigned short keycodes
[MAX_KEYPAD_KEYS
];
112 int rotary_rel_code
[2];
114 /* state row bits of each column scan */
115 uint32_t matrix_key_state
[MAX_MATRIX_KEY_COLS
];
116 uint32_t direct_key_state
;
118 unsigned int direct_key_mask
;
122 static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad
*keypad
,
123 struct pxa27x_keypad_platform_data
*pdata
)
125 struct input_dev
*input_dev
= keypad
->input_dev
;
126 struct device
*dev
= input_dev
->dev
.parent
;
130 error
= matrix_keypad_parse_of_params(dev
, &rows
, &cols
);
134 if (rows
> MAX_MATRIX_KEY_ROWS
|| cols
> MAX_MATRIX_KEY_COLS
) {
135 dev_err(dev
, "rows or cols exceeds maximum value\n");
139 pdata
->matrix_key_rows
= rows
;
140 pdata
->matrix_key_cols
= cols
;
142 error
= matrix_keypad_build_keymap(NULL
, NULL
,
143 pdata
->matrix_key_rows
,
144 pdata
->matrix_key_cols
,
145 keypad
->keycodes
, input_dev
);
152 static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad
*keypad
,
153 struct pxa27x_keypad_platform_data
*pdata
)
155 struct input_dev
*input_dev
= keypad
->input_dev
;
156 struct device
*dev
= input_dev
->dev
.parent
;
157 struct device_node
*np
= dev
->of_node
;
160 unsigned int proplen
, size
;
164 error
= of_property_read_u32(np
, "marvell,direct-key-count",
165 &pdata
->direct_key_num
);
168 * If do not have marvel,direct-key-count defined,
169 * it means direct key is not supported.
171 return error
== -EINVAL
? 0 : error
;
174 error
= of_property_read_u32(np
, "marvell,direct-key-mask",
175 &pdata
->direct_key_mask
);
177 if (error
!= -EINVAL
)
181 * If marvell,direct-key-mask is not defined, driver will use
182 * default value. Default value is set when configure the keypad.
184 pdata
->direct_key_mask
= 0;
187 pdata
->direct_key_low_active
= of_property_read_bool(np
,
188 "marvell,direct-key-low-active");
190 prop
= of_get_property(np
, "marvell,direct-key-map", &proplen
);
194 if (proplen
% sizeof(u16
))
197 size
= proplen
/ sizeof(u16
);
199 /* Only MAX_DIRECT_KEY_NUM is accepted.*/
200 if (size
> MAX_DIRECT_KEY_NUM
)
203 for (i
= 0; i
< size
; i
++) {
204 code
= be16_to_cpup(prop
+ i
);
205 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ i
] = code
;
206 __set_bit(code
, input_dev
->keybit
);
212 static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad
*keypad
,
213 struct pxa27x_keypad_platform_data
*pdata
)
217 unsigned int code
, proplen
;
218 const char *rotaryname
[2] = {
219 "marvell,rotary0", "marvell,rotary1"};
220 const char relkeyname
[] = {"marvell,rotary-rel-key"};
221 struct input_dev
*input_dev
= keypad
->input_dev
;
222 struct device
*dev
= input_dev
->dev
.parent
;
223 struct device_node
*np
= dev
->of_node
;
225 relkey_ret
= of_property_read_u32(np
, relkeyname
, &code
);
226 /* if can read correct rotary key-code, we do not need this. */
227 if (relkey_ret
== 0) {
228 unsigned short relcode
;
230 /* rotary0 taks lower half, rotary1 taks upper half. */
231 relcode
= code
& 0xffff;
232 pdata
->rotary0_rel_code
= (code
& 0xffff);
233 __set_bit(relcode
, input_dev
->relbit
);
235 relcode
= code
>> 16;
236 pdata
->rotary1_rel_code
= relcode
;
237 __set_bit(relcode
, input_dev
->relbit
);
240 for (i
= 0; i
< 2; i
++) {
241 prop
= of_get_property(np
, rotaryname
[i
], &proplen
);
243 * If the prop is not set, it means keypad does not need
244 * initialize the rotaryX.
249 code
= be32_to_cpup(prop
);
251 * Not all up/down key code are valid.
252 * Now we depends on direct-rel-code.
254 if ((!(code
& 0xffff) || !(code
>> 16)) && relkey_ret
) {
257 unsigned int n
= MAX_MATRIX_KEY_NUM
+ (i
<< 1);
258 unsigned short keycode
;
260 keycode
= code
& 0xffff;
261 keypad
->keycodes
[n
] = keycode
;
262 __set_bit(keycode
, input_dev
->keybit
);
264 keycode
= code
>> 16;
265 keypad
->keycodes
[n
+ 1] = keycode
;
266 __set_bit(keycode
, input_dev
->keybit
);
269 pdata
->rotary0_rel_code
= -1;
271 pdata
->rotary1_rel_code
= -1;
274 pdata
->enable_rotary0
= 1;
276 pdata
->enable_rotary1
= 1;
279 keypad
->rotary_rel_code
[0] = pdata
->rotary0_rel_code
;
280 keypad
->rotary_rel_code
[1] = pdata
->rotary1_rel_code
;
285 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad
*keypad
)
287 struct input_dev
*input_dev
= keypad
->input_dev
;
288 struct device
*dev
= input_dev
->dev
.parent
;
289 struct device_node
*np
= dev
->of_node
;
290 struct pxa27x_keypad_platform_data
*pdata
;
293 pdata
= devm_kzalloc(dev
, sizeof(*pdata
), GFP_KERNEL
);
295 dev_err(dev
, "failed to allocate memory for pdata\n");
299 error
= pxa27x_keypad_matrix_key_parse_dt(keypad
, pdata
);
301 dev_err(dev
, "failed to parse matrix key\n");
305 error
= pxa27x_keypad_direct_key_parse_dt(keypad
, pdata
);
307 dev_err(dev
, "failed to parse direct key\n");
311 error
= pxa27x_keypad_rotary_parse_dt(keypad
, pdata
);
313 dev_err(dev
, "failed to parse rotary key\n");
317 error
= of_property_read_u32(np
, "marvell,debounce-interval",
318 &pdata
->debounce_interval
);
320 dev_err(dev
, "failed to parse debpunce-interval\n");
325 * The keycodes may not only includes matrix key but also the direct
328 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
330 keypad
->pdata
= pdata
;
336 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad
*keypad
)
338 dev_info(keypad
->input_dev
->dev
.parent
, "missing platform data\n");
345 static int pxa27x_keypad_build_keycode(struct pxa27x_keypad
*keypad
)
347 const struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
348 struct input_dev
*input_dev
= keypad
->input_dev
;
349 const struct matrix_keymap_data
*keymap_data
=
350 pdata
? pdata
->matrix_keymap_data
: NULL
;
351 unsigned short keycode
;
355 error
= matrix_keypad_build_keymap(keymap_data
, NULL
,
356 pdata
->matrix_key_rows
,
357 pdata
->matrix_key_cols
,
358 keypad
->keycodes
, input_dev
);
363 * The keycodes may not only include matrix keys but also the direct
366 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
368 /* For direct keys. */
369 for (i
= 0; i
< pdata
->direct_key_num
; i
++) {
370 keycode
= pdata
->direct_key_map
[i
];
371 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ i
] = keycode
;
372 __set_bit(keycode
, input_dev
->keybit
);
375 if (pdata
->enable_rotary0
) {
376 if (pdata
->rotary0_up_key
&& pdata
->rotary0_down_key
) {
377 keycode
= pdata
->rotary0_up_key
;
378 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 0] = keycode
;
379 __set_bit(keycode
, input_dev
->keybit
);
381 keycode
= pdata
->rotary0_down_key
;
382 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 1] = keycode
;
383 __set_bit(keycode
, input_dev
->keybit
);
385 keypad
->rotary_rel_code
[0] = -1;
387 keypad
->rotary_rel_code
[0] = pdata
->rotary0_rel_code
;
388 __set_bit(pdata
->rotary0_rel_code
, input_dev
->relbit
);
392 if (pdata
->enable_rotary1
) {
393 if (pdata
->rotary1_up_key
&& pdata
->rotary1_down_key
) {
394 keycode
= pdata
->rotary1_up_key
;
395 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 2] = keycode
;
396 __set_bit(keycode
, input_dev
->keybit
);
398 keycode
= pdata
->rotary1_down_key
;
399 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 3] = keycode
;
400 __set_bit(keycode
, input_dev
->keybit
);
402 keypad
->rotary_rel_code
[1] = -1;
404 keypad
->rotary_rel_code
[1] = pdata
->rotary1_rel_code
;
405 __set_bit(pdata
->rotary1_rel_code
, input_dev
->relbit
);
409 __clear_bit(KEY_RESERVED
, input_dev
->keybit
);
414 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad
*keypad
)
416 const struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
417 struct input_dev
*input_dev
= keypad
->input_dev
;
418 int row
, col
, num_keys_pressed
= 0;
419 uint32_t new_state
[MAX_MATRIX_KEY_COLS
];
420 uint32_t kpas
= keypad_readl(KPAS
);
422 num_keys_pressed
= KPAS_MUKP(kpas
);
424 memset(new_state
, 0, sizeof(new_state
));
426 if (num_keys_pressed
== 0)
429 if (num_keys_pressed
== 1) {
433 /* if invalid row/col, treat as no key pressed */
434 if (col
>= pdata
->matrix_key_cols
||
435 row
>= pdata
->matrix_key_rows
)
438 new_state
[col
] = (1 << row
);
442 if (num_keys_pressed
> 1) {
443 uint32_t kpasmkp0
= keypad_readl(KPASMKP0
);
444 uint32_t kpasmkp1
= keypad_readl(KPASMKP1
);
445 uint32_t kpasmkp2
= keypad_readl(KPASMKP2
);
446 uint32_t kpasmkp3
= keypad_readl(KPASMKP3
);
448 new_state
[0] = kpasmkp0
& KPASMKP_MKC_MASK
;
449 new_state
[1] = (kpasmkp0
>> 16) & KPASMKP_MKC_MASK
;
450 new_state
[2] = kpasmkp1
& KPASMKP_MKC_MASK
;
451 new_state
[3] = (kpasmkp1
>> 16) & KPASMKP_MKC_MASK
;
452 new_state
[4] = kpasmkp2
& KPASMKP_MKC_MASK
;
453 new_state
[5] = (kpasmkp2
>> 16) & KPASMKP_MKC_MASK
;
454 new_state
[6] = kpasmkp3
& KPASMKP_MKC_MASK
;
455 new_state
[7] = (kpasmkp3
>> 16) & KPASMKP_MKC_MASK
;
458 for (col
= 0; col
< pdata
->matrix_key_cols
; col
++) {
459 uint32_t bits_changed
;
462 bits_changed
= keypad
->matrix_key_state
[col
] ^ new_state
[col
];
463 if (bits_changed
== 0)
466 for (row
= 0; row
< pdata
->matrix_key_rows
; row
++) {
467 if ((bits_changed
& (1 << row
)) == 0)
470 code
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
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
));
476 input_sync(input_dev
);
477 memcpy(keypad
->matrix_key_state
, new_state
, sizeof(new_state
));
480 #define DEFAULT_KPREC (0x007f007f)
482 static inline int rotary_delta(uint32_t kprec
)
484 if (kprec
& KPREC_OF0
)
485 return (kprec
& 0xff) + 0x7f;
486 else if (kprec
& KPREC_UF0
)
487 return (kprec
& 0xff) - 0x7f - 0xff;
489 return (kprec
& 0xff) - 0x7f;
492 static void report_rotary_event(struct pxa27x_keypad
*keypad
, int r
, int delta
)
494 struct input_dev
*dev
= keypad
->input_dev
;
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
];
503 /* simulate a press-n-release */
504 input_event(dev
, EV_MSC
, MSC_SCAN
, code
);
505 input_report_key(dev
, keycode
, 1);
507 input_event(dev
, EV_MSC
, MSC_SCAN
, code
);
508 input_report_key(dev
, keycode
, 0);
511 input_report_rel(dev
, keypad
->rotary_rel_code
[r
], delta
);
516 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad
*keypad
)
518 const struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
521 /* read and reset to default count value */
522 kprec
= keypad_readl(KPREC
);
523 keypad_writel(KPREC
, DEFAULT_KPREC
);
525 if (pdata
->enable_rotary0
)
526 report_rotary_event(keypad
, 0, rotary_delta(kprec
));
528 if (pdata
->enable_rotary1
)
529 report_rotary_event(keypad
, 1, rotary_delta(kprec
>> 16));
532 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad
*keypad
)
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
;
540 kpdk
= keypad_readl(KPDK
);
542 if (pdata
->enable_rotary0
|| pdata
->enable_rotary1
)
543 pxa27x_keypad_scan_rotary(keypad
);
546 * The KPDR_DK only output the key pin level, so it relates to board,
547 * and low level may be active.
549 if (pdata
->direct_key_low_active
)
550 new_state
= ~KPDK_DK(kpdk
) & keypad
->direct_key_mask
;
552 new_state
= KPDK_DK(kpdk
) & keypad
->direct_key_mask
;
554 bits_changed
= keypad
->direct_key_state
^ new_state
;
556 if (bits_changed
== 0)
559 for (i
= 0; i
< pdata
->direct_key_num
; i
++) {
560 if (bits_changed
& (1 << i
)) {
561 int code
= MAX_MATRIX_KEY_NUM
+ i
;
563 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
564 input_report_key(input_dev
, keypad
->keycodes
[code
],
565 new_state
& (1 << i
));
568 input_sync(input_dev
);
569 keypad
->direct_key_state
= new_state
;
572 static void clear_wakeup_event(struct pxa27x_keypad
*keypad
)
574 const struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
576 if (pdata
->clear_wakeup_event
)
577 (pdata
->clear_wakeup_event
)();
580 static irqreturn_t
pxa27x_keypad_irq_handler(int irq
, void *dev_id
)
582 struct pxa27x_keypad
*keypad
= dev_id
;
583 unsigned long kpc
= keypad_readl(KPC
);
585 clear_wakeup_event(keypad
);
588 pxa27x_keypad_scan_direct(keypad
);
591 pxa27x_keypad_scan_matrix(keypad
);
596 static void pxa27x_keypad_config(struct pxa27x_keypad
*keypad
)
598 const struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
599 unsigned int mask
= 0, direct_key_num
= 0;
600 unsigned long kpc
= 0;
602 /* clear pending interrupt bit */
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
);
612 /* enable rotary key, debounce interval same as direct keys */
613 if (pdata
->enable_rotary0
) {
619 if (pdata
->enable_rotary1
) {
625 if (pdata
->direct_key_num
> direct_key_num
)
626 direct_key_num
= pdata
->direct_key_num
;
629 * Direct keys usage may not start from KP_DKIN0, check the platfrom
630 * mask data to config the specific.
632 if (pdata
->direct_key_mask
)
633 keypad
->direct_key_mask
= pdata
->direct_key_mask
;
635 keypad
->direct_key_mask
= ((1 << direct_key_num
) - 1) & ~mask
;
637 /* enable direct key */
639 kpc
|= KPC_DE
| KPC_DIE
| KPC_DKN(direct_key_num
);
641 keypad_writel(KPC
, kpc
| KPC_RE_ZERO_DEB
);
642 keypad_writel(KPREC
, DEFAULT_KPREC
);
643 keypad_writel(KPKDI
, pdata
->debounce_interval
);
646 static int pxa27x_keypad_open(struct input_dev
*dev
)
648 struct pxa27x_keypad
*keypad
= input_get_drvdata(dev
);
650 /* Enable unit clock */
651 clk_prepare_enable(keypad
->clk
);
652 pxa27x_keypad_config(keypad
);
657 static void pxa27x_keypad_close(struct input_dev
*dev
)
659 struct pxa27x_keypad
*keypad
= input_get_drvdata(dev
);
661 /* Disable clock unit */
662 clk_disable_unprepare(keypad
->clk
);
665 #ifdef CONFIG_PM_SLEEP
666 static int pxa27x_keypad_suspend(struct device
*dev
)
668 struct platform_device
*pdev
= to_platform_device(dev
);
669 struct pxa27x_keypad
*keypad
= platform_get_drvdata(pdev
);
672 * If the keypad is used a wake up source, clock can not be disabled.
673 * Or it can not detect the key pressing.
675 if (device_may_wakeup(&pdev
->dev
))
676 enable_irq_wake(keypad
->irq
);
678 clk_disable_unprepare(keypad
->clk
);
683 static int pxa27x_keypad_resume(struct device
*dev
)
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
;
690 * If the keypad is used as wake up source, the clock is not turned
691 * off. So do not need configure it again.
693 if (device_may_wakeup(&pdev
->dev
)) {
694 disable_irq_wake(keypad
->irq
);
696 mutex_lock(&input_dev
->mutex
);
698 if (input_dev
->users
) {
699 /* Enable unit clock */
700 clk_prepare_enable(keypad
->clk
);
701 pxa27x_keypad_config(keypad
);
704 mutex_unlock(&input_dev
->mutex
);
711 static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops
,
712 pxa27x_keypad_suspend
, pxa27x_keypad_resume
);
715 static int pxa27x_keypad_probe(struct platform_device
*pdev
)
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
;
725 /* Driver need build keycode from device tree or pdata */
729 irq
= platform_get_irq(pdev
, 0);
731 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
735 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
737 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
741 keypad
= kzalloc(sizeof(struct pxa27x_keypad
), GFP_KERNEL
);
742 input_dev
= input_allocate_device();
743 if (!keypad
|| !input_dev
) {
744 dev_err(&pdev
->dev
, "failed to allocate memory\n");
749 keypad
->pdata
= pdata
;
750 keypad
->input_dev
= input_dev
;
753 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
755 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
760 keypad
->mmio_base
= ioremap(res
->start
, resource_size(res
));
761 if (keypad
->mmio_base
== NULL
) {
762 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
764 goto failed_free_mem
;
767 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
768 if (IS_ERR(keypad
->clk
)) {
769 dev_err(&pdev
->dev
, "failed to get keypad clock\n");
770 error
= PTR_ERR(keypad
->clk
);
774 input_dev
->name
= pdev
->name
;
775 input_dev
->id
.bustype
= BUS_HOST
;
776 input_dev
->open
= pxa27x_keypad_open
;
777 input_dev
->close
= pxa27x_keypad_close
;
778 input_dev
->dev
.parent
= &pdev
->dev
;
780 input_dev
->keycode
= keypad
->keycodes
;
781 input_dev
->keycodesize
= sizeof(keypad
->keycodes
[0]);
782 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
784 input_set_drvdata(input_dev
, keypad
);
786 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
);
787 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
790 error
= pxa27x_keypad_build_keycode(keypad
);
792 error
= pxa27x_keypad_build_keycode_from_dt(keypad
);
794 * Data that we get from DT resides in dynamically
795 * allocated memory so we need to update our pdata
798 pdata
= keypad
->pdata
;
801 dev_err(&pdev
->dev
, "failed to build keycode\n");
805 if ((pdata
->enable_rotary0
&& keypad
->rotary_rel_code
[0] != -1) ||
806 (pdata
->enable_rotary1
&& keypad
->rotary_rel_code
[1] != -1)) {
807 input_dev
->evbit
[0] |= BIT_MASK(EV_REL
);
810 error
= request_irq(irq
, pxa27x_keypad_irq_handler
, 0,
813 dev_err(&pdev
->dev
, "failed to request IRQ\n");
817 /* Register the input device */
818 error
= input_register_device(input_dev
);
820 dev_err(&pdev
->dev
, "failed to register input device\n");
821 goto failed_free_irq
;
824 platform_set_drvdata(pdev
, keypad
);
825 device_init_wakeup(&pdev
->dev
, 1);
830 free_irq(irq
, keypad
);
832 clk_put(keypad
->clk
);
834 iounmap(keypad
->mmio_base
);
836 release_mem_region(res
->start
, resource_size(res
));
838 input_free_device(input_dev
);
843 static int pxa27x_keypad_remove(struct platform_device
*pdev
)
845 struct pxa27x_keypad
*keypad
= platform_get_drvdata(pdev
);
846 struct resource
*res
;
848 free_irq(keypad
->irq
, keypad
);
849 clk_put(keypad
->clk
);
851 input_unregister_device(keypad
->input_dev
);
852 iounmap(keypad
->mmio_base
);
854 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
855 release_mem_region(res
->start
, resource_size(res
));
862 /* work with hotplug and coldplug */
863 MODULE_ALIAS("platform:pxa27x-keypad");
866 static const struct of_device_id pxa27x_keypad_dt_match
[] = {
867 { .compatible
= "marvell,pxa27x-keypad" },
870 MODULE_DEVICE_TABLE(of
, pxa27x_keypad_dt_match
);
873 static struct platform_driver pxa27x_keypad_driver
= {
874 .probe
= pxa27x_keypad_probe
,
875 .remove
= pxa27x_keypad_remove
,
877 .name
= "pxa27x-keypad",
878 .of_match_table
= of_match_ptr(pxa27x_keypad_dt_match
),
879 .owner
= THIS_MODULE
,
880 .pm
= &pxa27x_keypad_pm_ops
,
883 module_platform_driver(pxa27x_keypad_driver
);
885 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
886 MODULE_LICENSE("GPL");