2 * Driver for the Cirrus EP93xx matrix keypad controller.
4 * Copyright (c) 2008 H Hartley Sweeten <hsweeten@visionengravers.com>
6 * Based on the pxa27x matrix keypad controller by Rodolfo Giometti.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 * The 3-key reset is triggered by pressing the 3 keys in
15 * Row 0, Columns 2, 4, and 7 at the same time. This action can
16 * be disabled by setting the EP93XX_KEYPAD_DISABLE_3_KEY flag.
18 * Normal operation for the matrix does not autorepeat the key press.
19 * This action can be enabled by setting the EP93XX_KEYPAD_AUTOREPEAT
23 #include <linux/platform_device.h>
24 #include <linux/interrupt.h>
25 #include <linux/clk.h>
27 #include <linux/input/matrix_keypad.h>
29 #include <mach/hardware.h>
30 #include <mach/ep93xx_keypad.h>
33 * Keypad Interface Register offsets
35 #define KEY_INIT 0x00 /* Key Scan Initialization register */
36 #define KEY_DIAG 0x04 /* Key Scan Diagnostic register */
37 #define KEY_REG 0x08 /* Key Value Capture register */
39 /* Key Scan Initialization Register bit defines */
40 #define KEY_INIT_DBNC_MASK (0x00ff0000)
41 #define KEY_INIT_DBNC_SHIFT (16)
42 #define KEY_INIT_DIS3KY (1<<15)
43 #define KEY_INIT_DIAG (1<<14)
44 #define KEY_INIT_BACK (1<<13)
45 #define KEY_INIT_T2 (1<<12)
46 #define KEY_INIT_PRSCL_MASK (0x000003ff)
47 #define KEY_INIT_PRSCL_SHIFT (0)
49 /* Key Scan Diagnostic Register bit defines */
50 #define KEY_DIAG_MASK (0x0000003f)
51 #define KEY_DIAG_SHIFT (0)
53 /* Key Value Capture Register bit defines */
54 #define KEY_REG_K (1<<15)
55 #define KEY_REG_INT (1<<14)
56 #define KEY_REG_2KEYS (1<<13)
57 #define KEY_REG_1KEY (1<<12)
58 #define KEY_REG_KEY2_MASK (0x00000fc0)
59 #define KEY_REG_KEY2_SHIFT (6)
60 #define KEY_REG_KEY1_MASK (0x0000003f)
61 #define KEY_REG_KEY1_SHIFT (0)
63 #define EP93XX_MATRIX_SIZE (EP93XX_MATRIX_ROWS * EP93XX_MATRIX_COLS)
65 struct ep93xx_keypad
{
66 struct ep93xx_keypad_platform_data
*pdata
;
67 struct input_dev
*input_dev
;
70 void __iomem
*mmio_base
;
72 unsigned short keycodes
[EP93XX_MATRIX_SIZE
];
82 static irqreturn_t
ep93xx_keypad_irq_handler(int irq
, void *dev_id
)
84 struct ep93xx_keypad
*keypad
= dev_id
;
85 struct input_dev
*input_dev
= keypad
->input_dev
;
87 int keycode
, key1
, key2
;
89 status
= __raw_readl(keypad
->mmio_base
+ KEY_REG
);
91 keycode
= (status
& KEY_REG_KEY1_MASK
) >> KEY_REG_KEY1_SHIFT
;
92 key1
= keypad
->keycodes
[keycode
];
94 keycode
= (status
& KEY_REG_KEY2_MASK
) >> KEY_REG_KEY2_SHIFT
;
95 key2
= keypad
->keycodes
[keycode
];
97 if (status
& KEY_REG_2KEYS
) {
98 if (keypad
->key1
&& key1
!= keypad
->key1
&& key2
!= keypad
->key1
)
99 input_report_key(input_dev
, keypad
->key1
, 0);
101 if (keypad
->key2
&& key1
!= keypad
->key2
&& key2
!= keypad
->key2
)
102 input_report_key(input_dev
, keypad
->key2
, 0);
104 input_report_key(input_dev
, key1
, 1);
105 input_report_key(input_dev
, key2
, 1);
110 } else if (status
& KEY_REG_1KEY
) {
111 if (keypad
->key1
&& key1
!= keypad
->key1
)
112 input_report_key(input_dev
, keypad
->key1
, 0);
114 if (keypad
->key2
&& key1
!= keypad
->key2
)
115 input_report_key(input_dev
, keypad
->key2
, 0);
117 input_report_key(input_dev
, key1
, 1);
123 input_report_key(input_dev
, keypad
->key1
, 0);
124 input_report_key(input_dev
, keypad
->key2
, 0);
126 keypad
->key1
= keypad
->key2
= 0;
128 input_sync(input_dev
);
133 static void ep93xx_keypad_config(struct ep93xx_keypad
*keypad
)
135 struct ep93xx_keypad_platform_data
*pdata
= keypad
->pdata
;
136 unsigned int val
= 0;
138 if (pdata
->flags
& EP93XX_KEYPAD_KDIV
)
139 clk_set_rate(keypad
->clk
, EP93XX_KEYTCHCLK_DIV4
);
141 clk_set_rate(keypad
->clk
, EP93XX_KEYTCHCLK_DIV16
);
143 if (pdata
->flags
& EP93XX_KEYPAD_DISABLE_3_KEY
)
144 val
|= KEY_INIT_DIS3KY
;
145 if (pdata
->flags
& EP93XX_KEYPAD_DIAG_MODE
)
146 val
|= KEY_INIT_DIAG
;
147 if (pdata
->flags
& EP93XX_KEYPAD_BACK_DRIVE
)
148 val
|= KEY_INIT_BACK
;
149 if (pdata
->flags
& EP93XX_KEYPAD_TEST_MODE
)
152 val
|= ((pdata
->debounce
<< KEY_INIT_DBNC_SHIFT
) & KEY_INIT_DBNC_MASK
);
154 val
|= ((pdata
->prescale
<< KEY_INIT_PRSCL_SHIFT
) & KEY_INIT_PRSCL_MASK
);
156 __raw_writel(val
, keypad
->mmio_base
+ KEY_INIT
);
159 static int ep93xx_keypad_open(struct input_dev
*pdev
)
161 struct ep93xx_keypad
*keypad
= input_get_drvdata(pdev
);
163 if (!keypad
->enabled
) {
164 ep93xx_keypad_config(keypad
);
165 clk_enable(keypad
->clk
);
166 keypad
->enabled
= true;
172 static void ep93xx_keypad_close(struct input_dev
*pdev
)
174 struct ep93xx_keypad
*keypad
= input_get_drvdata(pdev
);
176 if (keypad
->enabled
) {
177 clk_disable(keypad
->clk
);
178 keypad
->enabled
= false;
185 * NOTE: I don't know if this is correct, or will work on the ep93xx.
187 * None of the existing ep93xx drivers have power management support.
188 * But, this is basically what the pxa27x_keypad driver does.
190 static int ep93xx_keypad_suspend(struct platform_device
*pdev
,
193 struct ep93xx_keypad
*keypad
= platform_get_drvdata(pdev
);
194 struct input_dev
*input_dev
= keypad
->input_dev
;
196 mutex_lock(&input_dev
->mutex
);
198 if (keypad
->enabled
) {
199 clk_disable(keypad
->clk
);
200 keypad
->enabled
= false;
203 mutex_unlock(&input_dev
->mutex
);
205 if (device_may_wakeup(&pdev
->dev
))
206 enable_irq_wake(keypad
->irq
);
211 static int ep93xx_keypad_resume(struct platform_device
*pdev
)
213 struct ep93xx_keypad
*keypad
= platform_get_drvdata(pdev
);
214 struct input_dev
*input_dev
= keypad
->input_dev
;
216 if (device_may_wakeup(&pdev
->dev
))
217 disable_irq_wake(keypad
->irq
);
219 mutex_lock(&input_dev
->mutex
);
221 if (input_dev
->users
) {
222 if (!keypad
->enabled
) {
223 ep93xx_keypad_config(keypad
);
224 clk_enable(keypad
->clk
);
225 keypad
->enabled
= true;
229 mutex_unlock(&input_dev
->mutex
);
233 #else /* !CONFIG_PM */
234 #define ep93xx_keypad_suspend NULL
235 #define ep93xx_keypad_resume NULL
236 #endif /* !CONFIG_PM */
238 static int __devinit
ep93xx_keypad_probe(struct platform_device
*pdev
)
240 struct ep93xx_keypad
*keypad
;
241 const struct matrix_keymap_data
*keymap_data
;
242 struct input_dev
*input_dev
;
243 struct resource
*res
;
246 keypad
= kzalloc(sizeof(struct ep93xx_keypad
), GFP_KERNEL
);
250 keypad
->pdata
= pdev
->dev
.platform_data
;
251 if (!keypad
->pdata
) {
256 keymap_data
= keypad
->pdata
->keymap_data
;
262 keypad
->irq
= platform_get_irq(pdev
, 0);
268 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
274 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
280 keypad
->mmio_base
= ioremap(res
->start
, resource_size(res
));
281 if (keypad
->mmio_base
== NULL
) {
283 goto failed_free_mem
;
286 err
= ep93xx_keypad_acquire_gpio(pdev
);
290 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
291 if (IS_ERR(keypad
->clk
)) {
292 err
= PTR_ERR(keypad
->clk
);
293 goto failed_free_gpio
;
296 input_dev
= input_allocate_device();
302 keypad
->input_dev
= input_dev
;
304 input_dev
->name
= pdev
->name
;
305 input_dev
->id
.bustype
= BUS_HOST
;
306 input_dev
->open
= ep93xx_keypad_open
;
307 input_dev
->close
= ep93xx_keypad_close
;
308 input_dev
->dev
.parent
= &pdev
->dev
;
309 input_dev
->keycode
= keypad
->keycodes
;
310 input_dev
->keycodesize
= sizeof(keypad
->keycodes
[0]);
311 input_dev
->keycodemax
= ARRAY_SIZE(keypad
->keycodes
);
313 input_set_drvdata(input_dev
, keypad
);
315 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
);
316 if (keypad
->pdata
->flags
& EP93XX_KEYPAD_AUTOREPEAT
)
317 input_dev
->evbit
[0] |= BIT_MASK(EV_REP
);
319 matrix_keypad_build_keymap(keymap_data
, 3,
320 input_dev
->keycode
, input_dev
->keybit
);
321 platform_set_drvdata(pdev
, keypad
);
323 err
= request_irq(keypad
->irq
, ep93xx_keypad_irq_handler
,
324 IRQF_DISABLED
, pdev
->name
, keypad
);
326 goto failed_free_dev
;
328 err
= input_register_device(input_dev
);
330 goto failed_free_irq
;
332 device_init_wakeup(&pdev
->dev
, 1);
337 free_irq(keypad
->irq
, pdev
);
338 platform_set_drvdata(pdev
, NULL
);
340 input_free_device(input_dev
);
342 clk_put(keypad
->clk
);
344 ep93xx_keypad_release_gpio(pdev
);
346 iounmap(keypad
->mmio_base
);
348 release_mem_region(res
->start
, resource_size(res
));
354 static int __devexit
ep93xx_keypad_remove(struct platform_device
*pdev
)
356 struct ep93xx_keypad
*keypad
= platform_get_drvdata(pdev
);
357 struct resource
*res
;
359 free_irq(keypad
->irq
, pdev
);
361 platform_set_drvdata(pdev
, NULL
);
364 clk_disable(keypad
->clk
);
365 clk_put(keypad
->clk
);
367 input_unregister_device(keypad
->input_dev
);
369 ep93xx_keypad_release_gpio(pdev
);
371 iounmap(keypad
->mmio_base
);
373 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
374 release_mem_region(res
->start
, resource_size(res
));
381 static struct platform_driver ep93xx_keypad_driver
= {
383 .name
= "ep93xx-keypad",
384 .owner
= THIS_MODULE
,
386 .probe
= ep93xx_keypad_probe
,
387 .remove
= __devexit_p(ep93xx_keypad_remove
),
388 .suspend
= ep93xx_keypad_suspend
,
389 .resume
= ep93xx_keypad_resume
,
392 static int __init
ep93xx_keypad_init(void)
394 return platform_driver_register(&ep93xx_keypad_driver
);
397 static void __exit
ep93xx_keypad_exit(void)
399 platform_driver_unregister(&ep93xx_keypad_driver
);
402 module_init(ep93xx_keypad_init
);
403 module_exit(ep93xx_keypad_exit
);
405 MODULE_LICENSE("GPL");
406 MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
407 MODULE_DESCRIPTION("EP93xx Matrix Keypad Controller");
408 MODULE_ALIAS("platform:ep93xx-keypad");