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/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/input.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>
31 #include <asm/mach/arch.h>
32 #include <asm/mach/map.h>
34 #include <mach/hardware.h>
35 #include <mach/pxa27x_keypad.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 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
;
121 static void pxa27x_keypad_build_keycode(struct pxa27x_keypad
*keypad
)
123 struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
124 struct input_dev
*input_dev
= keypad
->input_dev
;
125 unsigned short keycode
;
128 for (i
= 0; i
< pdata
->matrix_key_map_size
; i
++) {
129 unsigned int key
= pdata
->matrix_key_map
[i
];
130 unsigned int row
= KEY_ROW(key
);
131 unsigned int col
= KEY_COL(key
);
132 unsigned int scancode
= MATRIX_SCAN_CODE(row
, col
,
135 keycode
= KEY_VAL(key
);
136 keypad
->keycodes
[scancode
] = keycode
;
137 __set_bit(keycode
, input_dev
->keybit
);
140 for (i
= 0; i
< pdata
->direct_key_num
; i
++) {
141 keycode
= pdata
->direct_key_map
[i
];
142 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ i
] = keycode
;
143 __set_bit(keycode
, input_dev
->keybit
);
146 if (pdata
->enable_rotary0
) {
147 if (pdata
->rotary0_up_key
&& pdata
->rotary0_down_key
) {
148 keycode
= pdata
->rotary0_up_key
;
149 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 0] = keycode
;
150 __set_bit(keycode
, input_dev
->keybit
);
152 keycode
= pdata
->rotary0_down_key
;
153 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 1] = keycode
;
154 __set_bit(keycode
, input_dev
->keybit
);
156 keypad
->rotary_rel_code
[0] = -1;
158 keypad
->rotary_rel_code
[0] = pdata
->rotary0_rel_code
;
159 __set_bit(pdata
->rotary0_rel_code
, input_dev
->relbit
);
163 if (pdata
->enable_rotary1
) {
164 if (pdata
->rotary1_up_key
&& pdata
->rotary1_down_key
) {
165 keycode
= pdata
->rotary1_up_key
;
166 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 2] = keycode
;
167 __set_bit(keycode
, input_dev
->keybit
);
169 keycode
= pdata
->rotary1_down_key
;
170 keypad
->keycodes
[MAX_MATRIX_KEY_NUM
+ 3] = keycode
;
171 __set_bit(keycode
, input_dev
->keybit
);
173 keypad
->rotary_rel_code
[1] = -1;
175 keypad
->rotary_rel_code
[1] = pdata
->rotary1_rel_code
;
176 __set_bit(pdata
->rotary1_rel_code
, input_dev
->relbit
);
180 __clear_bit(KEY_RESERVED
, input_dev
->keybit
);
183 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad
*keypad
)
185 struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
186 struct input_dev
*input_dev
= keypad
->input_dev
;
187 int row
, col
, num_keys_pressed
= 0;
188 uint32_t new_state
[MAX_MATRIX_KEY_COLS
];
189 uint32_t kpas
= keypad_readl(KPAS
);
191 num_keys_pressed
= KPAS_MUKP(kpas
);
193 memset(new_state
, 0, sizeof(new_state
));
195 if (num_keys_pressed
== 0)
198 if (num_keys_pressed
== 1) {
202 /* if invalid row/col, treat as no key pressed */
203 if (col
>= pdata
->matrix_key_cols
||
204 row
>= pdata
->matrix_key_rows
)
207 new_state
[col
] = (1 << row
);
211 if (num_keys_pressed
> 1) {
212 uint32_t kpasmkp0
= keypad_readl(KPASMKP0
);
213 uint32_t kpasmkp1
= keypad_readl(KPASMKP1
);
214 uint32_t kpasmkp2
= keypad_readl(KPASMKP2
);
215 uint32_t kpasmkp3
= keypad_readl(KPASMKP3
);
217 new_state
[0] = kpasmkp0
& KPASMKP_MKC_MASK
;
218 new_state
[1] = (kpasmkp0
>> 16) & KPASMKP_MKC_MASK
;
219 new_state
[2] = kpasmkp1
& KPASMKP_MKC_MASK
;
220 new_state
[3] = (kpasmkp1
>> 16) & KPASMKP_MKC_MASK
;
221 new_state
[4] = kpasmkp2
& KPASMKP_MKC_MASK
;
222 new_state
[5] = (kpasmkp2
>> 16) & KPASMKP_MKC_MASK
;
223 new_state
[6] = kpasmkp3
& KPASMKP_MKC_MASK
;
224 new_state
[7] = (kpasmkp3
>> 16) & KPASMKP_MKC_MASK
;
227 for (col
= 0; col
< pdata
->matrix_key_cols
; col
++) {
228 uint32_t bits_changed
;
231 bits_changed
= keypad
->matrix_key_state
[col
] ^ new_state
[col
];
232 if (bits_changed
== 0)
235 for (row
= 0; row
< pdata
->matrix_key_rows
; row
++) {
236 if ((bits_changed
& (1 << row
)) == 0)
239 code
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
240 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
241 input_report_key(input_dev
, keypad
->keycodes
[code
],
242 new_state
[col
] & (1 << row
));
245 input_sync(input_dev
);
246 memcpy(keypad
->matrix_key_state
, new_state
, sizeof(new_state
));
249 #define DEFAULT_KPREC (0x007f007f)
251 static inline int rotary_delta(uint32_t kprec
)
253 if (kprec
& KPREC_OF0
)
254 return (kprec
& 0xff) + 0x7f;
255 else if (kprec
& KPREC_UF0
)
256 return (kprec
& 0xff) - 0x7f - 0xff;
258 return (kprec
& 0xff) - 0x7f;
261 static void report_rotary_event(struct pxa27x_keypad
*keypad
, int r
, int delta
)
263 struct input_dev
*dev
= keypad
->input_dev
;
268 if (keypad
->rotary_rel_code
[r
] == -1) {
269 int code
= MAX_MATRIX_KEY_NUM
+ 2 * r
+ (delta
> 0 ? 0 : 1);
270 unsigned char keycode
= keypad
->keycodes
[code
];
272 /* simulate a press-n-release */
273 input_event(dev
, EV_MSC
, MSC_SCAN
, code
);
274 input_report_key(dev
, keycode
, 1);
276 input_event(dev
, EV_MSC
, MSC_SCAN
, code
);
277 input_report_key(dev
, keycode
, 0);
280 input_report_rel(dev
, keypad
->rotary_rel_code
[r
], delta
);
285 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad
*keypad
)
287 struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
290 /* read and reset to default count value */
291 kprec
= keypad_readl(KPREC
);
292 keypad_writel(KPREC
, DEFAULT_KPREC
);
294 if (pdata
->enable_rotary0
)
295 report_rotary_event(keypad
, 0, rotary_delta(kprec
));
297 if (pdata
->enable_rotary1
)
298 report_rotary_event(keypad
, 1, rotary_delta(kprec
>> 16));
301 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad
*keypad
)
303 struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
304 struct input_dev
*input_dev
= keypad
->input_dev
;
305 unsigned int new_state
;
306 uint32_t kpdk
, bits_changed
;
309 kpdk
= keypad_readl(KPDK
);
311 if (pdata
->enable_rotary0
|| pdata
->enable_rotary1
)
312 pxa27x_keypad_scan_rotary(keypad
);
314 new_state
= KPDK_DK(kpdk
) & keypad
->direct_key_mask
;
315 bits_changed
= keypad
->direct_key_state
^ new_state
;
317 if (bits_changed
== 0)
320 for (i
= 0; i
< pdata
->direct_key_num
; i
++) {
321 if (bits_changed
& (1 << i
)) {
322 int code
= MAX_MATRIX_KEY_NUM
+ i
;
324 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
325 input_report_key(input_dev
, keypad
->keycodes
[code
],
326 new_state
& (1 << i
));
329 input_sync(input_dev
);
330 keypad
->direct_key_state
= new_state
;
333 static irqreturn_t
pxa27x_keypad_irq_handler(int irq
, void *dev_id
)
335 struct pxa27x_keypad
*keypad
= dev_id
;
336 unsigned long kpc
= keypad_readl(KPC
);
339 pxa27x_keypad_scan_direct(keypad
);
342 pxa27x_keypad_scan_matrix(keypad
);
347 static void pxa27x_keypad_config(struct pxa27x_keypad
*keypad
)
349 struct pxa27x_keypad_platform_data
*pdata
= keypad
->pdata
;
350 unsigned int mask
= 0, direct_key_num
= 0;
351 unsigned long kpc
= 0;
353 /* enable matrix keys with automatic scan */
354 if (pdata
->matrix_key_rows
&& pdata
->matrix_key_cols
) {
355 kpc
|= KPC_ASACT
| KPC_MIE
| KPC_ME
| KPC_MS_ALL
;
356 kpc
|= KPC_MKRN(pdata
->matrix_key_rows
) |
357 KPC_MKCN(pdata
->matrix_key_cols
);
360 /* enable rotary key, debounce interval same as direct keys */
361 if (pdata
->enable_rotary0
) {
367 if (pdata
->enable_rotary1
) {
373 if (pdata
->direct_key_num
> direct_key_num
)
374 direct_key_num
= pdata
->direct_key_num
;
376 keypad
->direct_key_mask
= ((2 << direct_key_num
) - 1) & ~mask
;
378 /* enable direct key */
380 kpc
|= KPC_DE
| KPC_DIE
| KPC_DKN(direct_key_num
);
382 keypad_writel(KPC
, kpc
| KPC_RE_ZERO_DEB
);
383 keypad_writel(KPREC
, DEFAULT_KPREC
);
384 keypad_writel(KPKDI
, pdata
->debounce_interval
);
387 static int pxa27x_keypad_open(struct input_dev
*dev
)
389 struct pxa27x_keypad
*keypad
= input_get_drvdata(dev
);
391 /* Enable unit clock */
392 clk_enable(keypad
->clk
);
393 pxa27x_keypad_config(keypad
);
398 static void pxa27x_keypad_close(struct input_dev
*dev
)
400 struct pxa27x_keypad
*keypad
= input_get_drvdata(dev
);
402 /* Disable clock unit */
403 clk_disable(keypad
->clk
);
407 static int pxa27x_keypad_suspend(struct device
*dev
)
409 struct platform_device
*pdev
= to_platform_device(dev
);
410 struct pxa27x_keypad
*keypad
= platform_get_drvdata(pdev
);
412 clk_disable(keypad
->clk
);
414 if (device_may_wakeup(&pdev
->dev
))
415 enable_irq_wake(keypad
->irq
);
420 static int pxa27x_keypad_resume(struct device
*dev
)
422 struct platform_device
*pdev
= to_platform_device(dev
);
423 struct pxa27x_keypad
*keypad
= platform_get_drvdata(pdev
);
424 struct input_dev
*input_dev
= keypad
->input_dev
;
426 if (device_may_wakeup(&pdev
->dev
))
427 disable_irq_wake(keypad
->irq
);
429 mutex_lock(&input_dev
->mutex
);
431 if (input_dev
->users
) {
432 /* Enable unit clock */
433 clk_enable(keypad
->clk
);
434 pxa27x_keypad_config(keypad
);
437 mutex_unlock(&input_dev
->mutex
);
442 static const struct dev_pm_ops pxa27x_keypad_pm_ops
= {
443 .suspend
= pxa27x_keypad_suspend
,
444 .resume
= pxa27x_keypad_resume
,
448 static int __devinit
pxa27x_keypad_probe(struct platform_device
*pdev
)
450 struct pxa27x_keypad_platform_data
*pdata
= pdev
->dev
.platform_data
;
451 struct pxa27x_keypad
*keypad
;
452 struct input_dev
*input_dev
;
453 struct resource
*res
;
457 dev_err(&pdev
->dev
, "no platform data defined\n");
461 irq
= platform_get_irq(pdev
, 0);
463 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
467 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
469 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
473 keypad
= kzalloc(sizeof(struct pxa27x_keypad
), GFP_KERNEL
);
474 input_dev
= input_allocate_device();
475 if (!keypad
|| !input_dev
) {
476 dev_err(&pdev
->dev
, "failed to allocate memory\n");
481 keypad
->pdata
= pdata
;
482 keypad
->input_dev
= input_dev
;
485 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
487 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
492 keypad
->mmio_base
= ioremap(res
->start
, resource_size(res
));
493 if (keypad
->mmio_base
== NULL
) {
494 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
496 goto failed_free_mem
;
499 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
500 if (IS_ERR(keypad
->clk
)) {
501 dev_err(&pdev
->dev
, "failed to get keypad clock\n");
502 error
= PTR_ERR(keypad
->clk
);
506 input_dev
->name
= pdev
->name
;
507 input_dev
->id
.bustype
= BUS_HOST
;
508 input_dev
->open
= pxa27x_keypad_open
;
509 input_dev
->close
= pxa27x_keypad_close
;
510 input_dev
->dev
.parent
= &pdev
->dev
;
512 input_dev
->keycode
= keypad
->keycodes
;
513 input_dev
->keycodesize
= sizeof(keypad
->keycodes
[0]);
514 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
516 input_set_drvdata(input_dev
, keypad
);
518 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REP
);
519 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
521 pxa27x_keypad_build_keycode(keypad
);
523 if ((pdata
->enable_rotary0
&& keypad
->rotary_rel_code
[0] != -1) ||
524 (pdata
->enable_rotary1
&& keypad
->rotary_rel_code
[1] != -1)) {
525 input_dev
->evbit
[0] |= BIT_MASK(EV_REL
);
528 error
= request_irq(irq
, pxa27x_keypad_irq_handler
, IRQF_DISABLED
,
531 dev_err(&pdev
->dev
, "failed to request IRQ\n");
535 /* Register the input device */
536 error
= input_register_device(input_dev
);
538 dev_err(&pdev
->dev
, "failed to register input device\n");
539 goto failed_free_irq
;
542 platform_set_drvdata(pdev
, keypad
);
543 device_init_wakeup(&pdev
->dev
, 1);
550 clk_put(keypad
->clk
);
552 iounmap(keypad
->mmio_base
);
554 release_mem_region(res
->start
, resource_size(res
));
556 input_free_device(input_dev
);
561 static int __devexit
pxa27x_keypad_remove(struct platform_device
*pdev
)
563 struct pxa27x_keypad
*keypad
= platform_get_drvdata(pdev
);
564 struct resource
*res
;
566 free_irq(keypad
->irq
, pdev
);
567 clk_put(keypad
->clk
);
569 input_unregister_device(keypad
->input_dev
);
570 input_free_device(keypad
->input_dev
);
572 iounmap(keypad
->mmio_base
);
574 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
575 release_mem_region(res
->start
, resource_size(res
));
577 platform_set_drvdata(pdev
, NULL
);
583 /* work with hotplug and coldplug */
584 MODULE_ALIAS("platform:pxa27x-keypad");
586 static struct platform_driver pxa27x_keypad_driver
= {
587 .probe
= pxa27x_keypad_probe
,
588 .remove
= __devexit_p(pxa27x_keypad_remove
),
590 .name
= "pxa27x-keypad",
591 .owner
= THIS_MODULE
,
593 .pm
= &pxa27x_keypad_pm_ops
,
598 static int __init
pxa27x_keypad_init(void)
600 return platform_driver_register(&pxa27x_keypad_driver
);
603 static void __exit
pxa27x_keypad_exit(void)
605 platform_driver_unregister(&pxa27x_keypad_driver
);
608 module_init(pxa27x_keypad_init
);
609 module_exit(pxa27x_keypad_exit
);
611 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
612 MODULE_LICENSE("GPL");