blob: e9d4e227a009a4cb8a7481864b31f7695970a052 [file] [log] [blame]
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -04001/*
Eric Miao0e5f11a2008-01-31 00:56:46 -05002 * linux/drivers/input/keyboard/pxa27x_keypad.c
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -04003 *
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@cam.org>.
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/init.h>
22#include <linux/interrupt.h>
23#include <linux/input.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
Russell King22d8a732007-08-20 10:19:39 +010026#include <linux/clk.h>
27#include <linux/err.h>
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040028
29#include <asm/mach-types.h>
30#include <asm/mach/arch.h>
31#include <asm/mach/map.h>
32
33#include <asm/arch/hardware.h>
34#include <asm/arch/pxa-regs.h>
35#include <asm/arch/irqs.h>
Eric Miao0e5f11a2008-01-31 00:56:46 -050036#include <asm/arch/pxa27x_keypad.h>
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040037
Eric Miao0e5f11a2008-01-31 00:56:46 -050038#define DRIVER_NAME "pxa27x-keypad"
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040039
Eric Miaod7416f92008-01-31 00:58:52 -050040#define KPC_MKRN(n) ((((n) & 0x7) - 1) << 26) /* matrix key row number */
41#define KPC_MKCN(n) ((((n) & 0x7) - 1) << 23) /* matrix key column number */
42#define KPC_DKN(n) ((((n) & 0x7) - 1) << 6) /* direct key number */
43
Eric Miao1814db62008-01-31 00:58:37 -050044#define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
45#define KPAS_RP(n) (((n) >> 4) & 0xf)
46#define KPAS_CP(n) ((n) & 0xf)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040047
Eric Miao1814db62008-01-31 00:58:37 -050048#define KPASMKP_MKC_MASK (0xff)
49
50#define MAX_MATRIX_KEY_NUM (8 * 8)
51
52struct pxa27x_keypad {
53 struct pxa27x_keypad_platform_data *pdata;
54
55 struct clk *clk;
56 struct input_dev *input_dev;
57
58 /* matrix key code map */
59 unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
60
61 /* state row bits of each column scan */
62 uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
63};
64
65static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
66{
67 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
68 struct input_dev *input_dev = keypad->input_dev;
69 unsigned int *key;
70 int i;
71
72 key = &pdata->matrix_key_map[0];
73 for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
74 int row = ((*key) >> 28) & 0xf;
75 int col = ((*key) >> 24) & 0xf;
76 int code = (*key) & 0xffffff;
77
78 keypad->matrix_keycodes[(row << 3) + col] = code;
79 set_bit(code, input_dev->keybit);
80 }
81}
82
83static inline unsigned int lookup_matrix_keycode(
84 struct pxa27x_keypad *keypad, int row, int col)
85{
86 return keypad->matrix_keycodes[(row << 3) + col];
87}
88
89static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
90{
91 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
92 int row, col, num_keys_pressed = 0;
93 uint32_t new_state[MAX_MATRIX_KEY_COLS];
94 uint32_t kpas = KPAS;
95
96 num_keys_pressed = KPAS_MUKP(kpas);
97
98 memset(new_state, 0, sizeof(new_state));
99
100 if (num_keys_pressed == 0)
101 goto scan;
102
103 if (num_keys_pressed == 1) {
104 col = KPAS_CP(kpas);
105 row = KPAS_RP(kpas);
106
107 /* if invalid row/col, treat as no key pressed */
108 if (col >= pdata->matrix_key_cols ||
109 row >= pdata->matrix_key_rows)
110 goto scan;
111
112 new_state[col] = (1 << row);
113 goto scan;
114 }
115
116 if (num_keys_pressed > 1) {
117 uint32_t kpasmkp0 = KPASMKP0;
118 uint32_t kpasmkp1 = KPASMKP1;
119 uint32_t kpasmkp2 = KPASMKP2;
120 uint32_t kpasmkp3 = KPASMKP3;
121
122 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
123 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
124 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
125 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
126 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
127 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
128 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
129 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
130 }
131scan:
132 for (col = 0; col < pdata->matrix_key_cols; col++) {
133 uint32_t bits_changed;
134
135 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
136 if (bits_changed == 0)
137 continue;
138
139 for (row = 0; row < pdata->matrix_key_rows; row++) {
140 if ((bits_changed & (1 << row)) == 0)
141 continue;
142
143 input_report_key(keypad->input_dev,
144 lookup_matrix_keycode(keypad, row, col),
145 new_state[col] & (1 << row));
146 }
147 }
148 input_sync(keypad->input_dev);
149 memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
150}
Russell King22d8a732007-08-20 10:19:39 +0100151
Eric Miaod7416f92008-01-31 00:58:52 -0500152#define DEFAULT_KPREC (0x007f007f)
153
Eric Miao0e5f11a2008-01-31 00:56:46 -0500154static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400155{
Eric Miao1814db62008-01-31 00:58:37 -0500156 struct pxa27x_keypad *keypad = dev_id;
157 struct input_dev *input_dev = keypad->input_dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400158 unsigned long kpc = KPC;
Eric Miao1814db62008-01-31 00:58:37 -0500159 int rel;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400160
161 if (kpc & KPC_DI) {
162 unsigned long kpdk = KPDK;
163
164 if (!(kpdk & KPDK_DKP)) {
165 /* better luck next time */
166 } else if (kpc & KPC_REE0) {
167 unsigned long kprec = KPREC;
168 KPREC = 0x7f;
169
170 if (kprec & KPREC_OF0)
171 rel = (kprec & 0xff) + 0x7f;
172 else if (kprec & KPREC_UF0)
173 rel = (kprec & 0xff) - 0x7f - 0xff;
174 else
175 rel = (kprec & 0xff) - 0x7f;
176
177 if (rel) {
178 input_report_rel(input_dev, REL_WHEEL, rel);
179 input_sync(input_dev);
180 }
181 }
182 }
183
Eric Miao1814db62008-01-31 00:58:37 -0500184 if (kpc & KPC_MI)
185 pxa27x_keypad_scan_matrix(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400186
187 return IRQ_HANDLED;
188}
189
Eric Miaod7416f92008-01-31 00:58:52 -0500190static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
191{
192 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
193 unsigned long kpc = 0;
194
195 /* enable matrix keys with automatic scan */
196 if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
197 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
198 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
199 KPC_MKCN(pdata->matrix_key_cols);
200 }
201
202 /* FIXME: hardcoded to enable rotary 0 _only_ */
203 kpc |= KPC_DKN(2) | KPC_REE0 | KPC_DI | KPC_DIE;
204
205 KPC = kpc;
206 KPREC = DEFAULT_KPREC;
207}
208
Eric Miao0e5f11a2008-01-31 00:56:46 -0500209static int pxa27x_keypad_open(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400210{
Eric Miao1814db62008-01-31 00:58:37 -0500211 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
212
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400213 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500214 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500215 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400216
217 return 0;
218}
219
Eric Miao0e5f11a2008-01-31 00:56:46 -0500220static void pxa27x_keypad_close(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400221{
Eric Miao1814db62008-01-31 00:58:37 -0500222 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
223
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400224 /* Disable clock unit */
Eric Miao1814db62008-01-31 00:58:37 -0500225 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400226}
227
228#ifdef CONFIG_PM
Eric Miao0e5f11a2008-01-31 00:56:46 -0500229static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400230{
Eric Miao1814db62008-01-31 00:58:37 -0500231 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400232
Eric Miaod7416f92008-01-31 00:58:52 -0500233 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400234 return 0;
235}
236
Eric Miao0e5f11a2008-01-31 00:56:46 -0500237static int pxa27x_keypad_resume(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400238{
Eric Miao1814db62008-01-31 00:58:37 -0500239 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao1814db62008-01-31 00:58:37 -0500240 struct input_dev *input_dev = keypad->input_dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400241
242 mutex_lock(&input_dev->mutex);
243
244 if (input_dev->users) {
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400245 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500246 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500247 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400248 }
249
250 mutex_unlock(&input_dev->mutex);
251
252 return 0;
253}
254#else
Eric Miao0e5f11a2008-01-31 00:56:46 -0500255#define pxa27x_keypad_suspend NULL
256#define pxa27x_keypad_resume NULL
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400257#endif
258
Eric Miao0e5f11a2008-01-31 00:56:46 -0500259static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400260{
Eric Miao1814db62008-01-31 00:58:37 -0500261 struct pxa27x_keypad *keypad;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400262 struct input_dev *input_dev;
Eric Miaod7416f92008-01-31 00:58:52 -0500263 int error;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400264
Eric Miao1814db62008-01-31 00:58:37 -0500265 keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
266 if (keypad == NULL) {
267 dev_err(&pdev->dev, "failed to allocate driver data\n");
268 return -ENOMEM;
269 }
270
271 keypad->pdata = pdev->dev.platform_data;
272 if (keypad->pdata == NULL) {
273 dev_err(&pdev->dev, "no platform data defined\n");
274 error = -EINVAL;
275 goto failed_free;
276 }
277
278 keypad->clk = clk_get(&pdev->dev, "KBDCLK");
279 if (IS_ERR(keypad->clk)) {
280 dev_err(&pdev->dev, "failed to get keypad clock\n");
281 error = PTR_ERR(keypad->clk);
282 goto failed_free;
Russell King22d8a732007-08-20 10:19:39 +0100283 }
284
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400285 /* Create and register the input driver. */
286 input_dev = input_allocate_device();
287 if (!input_dev) {
Eric Miao1814db62008-01-31 00:58:37 -0500288 dev_err(&pdev->dev, "failed to allocate input device\n");
Russell King22d8a732007-08-20 10:19:39 +0100289 error = -ENOMEM;
Eric Miao1814db62008-01-31 00:58:37 -0500290 goto failed_put_clk;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400291 }
292
293 input_dev->name = DRIVER_NAME;
294 input_dev->id.bustype = BUS_HOST;
Eric Miao0e5f11a2008-01-31 00:56:46 -0500295 input_dev->open = pxa27x_keypad_open;
296 input_dev->close = pxa27x_keypad_close;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400297 input_dev->dev.parent = &pdev->dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400298
Eric Miao1814db62008-01-31 00:58:37 -0500299 keypad->input_dev = input_dev;
300 input_set_drvdata(input_dev, keypad);
301
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700302 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
303 BIT_MASK(EV_REL);
304 input_dev->relbit[BIT_WORD(REL_WHEEL)] = BIT_MASK(REL_WHEEL);
Eric Miao1814db62008-01-31 00:58:37 -0500305
306 pxa27x_keypad_build_keycode(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400307
Eric Miao0e5f11a2008-01-31 00:56:46 -0500308 error = request_irq(IRQ_KEYPAD, pxa27x_keypad_irq_handler, IRQF_DISABLED,
Eric Miao1814db62008-01-31 00:58:37 -0500309 DRIVER_NAME, keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400310 if (error) {
311 printk(KERN_ERR "Cannot request keypad IRQ\n");
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400312 goto err_free_dev;
313 }
314
Eric Miao1814db62008-01-31 00:58:37 -0500315 platform_set_drvdata(pdev, keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400316
317 /* Register the input device */
318 error = input_register_device(input_dev);
319 if (error)
320 goto err_free_irq;
321
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400322 return 0;
323
324 err_free_irq:
325 platform_set_drvdata(pdev, NULL);
326 free_irq(IRQ_KEYPAD, pdev);
327 err_free_dev:
328 input_free_device(input_dev);
Eric Miao1814db62008-01-31 00:58:37 -0500329failed_put_clk:
330 clk_put(keypad->clk);
331failed_free:
332 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400333 return error;
334}
335
Eric Miao0e5f11a2008-01-31 00:56:46 -0500336static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400337{
Eric Miao1814db62008-01-31 00:58:37 -0500338 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400339
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400340 free_irq(IRQ_KEYPAD, pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400341
Eric Miao1814db62008-01-31 00:58:37 -0500342 clk_disable(keypad->clk);
343 clk_put(keypad->clk);
344
345 input_unregister_device(keypad->input_dev);
346
347 platform_set_drvdata(pdev, NULL);
348 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400349 return 0;
350}
351
Eric Miao0e5f11a2008-01-31 00:56:46 -0500352static struct platform_driver pxa27x_keypad_driver = {
353 .probe = pxa27x_keypad_probe,
354 .remove = __devexit_p(pxa27x_keypad_remove),
355 .suspend = pxa27x_keypad_suspend,
356 .resume = pxa27x_keypad_resume,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400357 .driver = {
358 .name = DRIVER_NAME,
359 },
360};
361
Eric Miao0e5f11a2008-01-31 00:56:46 -0500362static int __init pxa27x_keypad_init(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400363{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500364 return platform_driver_register(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400365}
366
Eric Miao0e5f11a2008-01-31 00:56:46 -0500367static void __exit pxa27x_keypad_exit(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400368{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500369 platform_driver_unregister(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400370}
371
Eric Miao0e5f11a2008-01-31 00:56:46 -0500372module_init(pxa27x_keypad_init);
373module_exit(pxa27x_keypad_exit);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400374
Eric Miao0e5f11a2008-01-31 00:56:46 -0500375MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400376MODULE_LICENSE("GPL");