Input: pxa27x_keypad - enable rotary encoders and direct keys
[pandora-kernel.git] / drivers / input / keyboard / pxa27x_keypad.c
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@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>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28
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>
36 #include <asm/arch/pxa27x_keypad.h>
37
38 #define DRIVER_NAME             "pxa27x-keypad"
39
40 #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
44 #define KPDK_DKP        (0x1 << 31)
45 #define KPDK_DK(n)      ((n) & 0xff)
46
47 #define KPAS_MUKP(n)            (((n) >> 26) & 0x1f)
48 #define KPAS_RP(n)              (((n) >> 4) & 0xf)
49 #define KPAS_CP(n)              ((n) & 0xf)
50
51 #define KPASMKP_MKC_MASK        (0xff)
52
53 #define MAX_MATRIX_KEY_NUM      (8 * 8)
54
55 struct pxa27x_keypad {
56         struct pxa27x_keypad_platform_data *pdata;
57
58         struct clk *clk;
59         struct input_dev *input_dev;
60
61         /* matrix key code map */
62         unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
63
64         /* state row bits of each column scan */
65         uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
66         uint32_t direct_key_state;
67
68         unsigned int direct_key_mask;
69
70         int rotary_rel_code[2];
71         int rotary_up_key[2];
72         int rotary_down_key[2];
73 };
74
75 static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
76 {
77         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
78         struct input_dev *input_dev = keypad->input_dev;
79         unsigned int *key;
80         int i;
81
82         key = &pdata->matrix_key_map[0];
83         for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
84                 int row = ((*key) >> 28) & 0xf;
85                 int col = ((*key) >> 24) & 0xf;
86                 int code = (*key) & 0xffffff;
87
88                 keypad->matrix_keycodes[(row << 3) + col] = code;
89                 set_bit(code, input_dev->keybit);
90         }
91
92         keypad->rotary_up_key[0] = pdata->rotary0_up_key;
93         keypad->rotary_up_key[1] = pdata->rotary1_up_key;
94         keypad->rotary_down_key[0] = pdata->rotary0_down_key;
95         keypad->rotary_down_key[1] = pdata->rotary1_down_key;
96         keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
97         keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
98
99         if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
100                 set_bit(pdata->rotary0_up_key, input_dev->keybit);
101                 set_bit(pdata->rotary0_down_key, input_dev->keybit);
102         } else
103                 set_bit(pdata->rotary0_rel_code, input_dev->relbit);
104
105         if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
106                 set_bit(pdata->rotary1_up_key, input_dev->keybit);
107                 set_bit(pdata->rotary1_down_key, input_dev->keybit);
108         } else
109                 set_bit(pdata->rotary1_rel_code, input_dev->relbit);
110 }
111
112 static inline unsigned int lookup_matrix_keycode(
113                 struct pxa27x_keypad *keypad, int row, int col)
114 {
115         return keypad->matrix_keycodes[(row << 3) + col];
116 }
117
118 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
119 {
120         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
121         int row, col, num_keys_pressed = 0;
122         uint32_t new_state[MAX_MATRIX_KEY_COLS];
123         uint32_t kpas = KPAS;
124
125         num_keys_pressed = KPAS_MUKP(kpas);
126
127         memset(new_state, 0, sizeof(new_state));
128
129         if (num_keys_pressed == 0)
130                 goto scan;
131
132         if (num_keys_pressed == 1) {
133                 col = KPAS_CP(kpas);
134                 row = KPAS_RP(kpas);
135
136                 /* if invalid row/col, treat as no key pressed */
137                 if (col >= pdata->matrix_key_cols ||
138                     row >= pdata->matrix_key_rows)
139                         goto scan;
140
141                 new_state[col] = (1 << row);
142                 goto scan;
143         }
144
145         if (num_keys_pressed > 1) {
146                 uint32_t kpasmkp0 = KPASMKP0;
147                 uint32_t kpasmkp1 = KPASMKP1;
148                 uint32_t kpasmkp2 = KPASMKP2;
149                 uint32_t kpasmkp3 = KPASMKP3;
150
151                 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
152                 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
153                 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
154                 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
155                 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
156                 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
157                 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
158                 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
159         }
160 scan:
161         for (col = 0; col < pdata->matrix_key_cols; col++) {
162                 uint32_t bits_changed;
163
164                 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
165                 if (bits_changed == 0)
166                         continue;
167
168                 for (row = 0; row < pdata->matrix_key_rows; row++) {
169                         if ((bits_changed & (1 << row)) == 0)
170                                 continue;
171
172                         input_report_key(keypad->input_dev,
173                                 lookup_matrix_keycode(keypad, row, col),
174                                 new_state[col] & (1 << row));
175                 }
176         }
177         input_sync(keypad->input_dev);
178         memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
179 }
180
181 #define DEFAULT_KPREC   (0x007f007f)
182
183 static inline int rotary_delta(uint32_t kprec)
184 {
185         if (kprec & KPREC_OF0)
186                 return (kprec & 0xff) + 0x7f;
187         else if (kprec & KPREC_UF0)
188                 return (kprec & 0xff) - 0x7f - 0xff;
189         else
190                 return (kprec & 0xff) - 0x7f;
191 }
192
193 static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
194 {
195         struct input_dev *dev = keypad->input_dev;
196
197         if (delta == 0)
198                 return;
199
200         if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
201                 int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
202                                             keypad->rotary_down_key[r];
203
204                 /* simulate a press-n-release */
205                 input_report_key(dev, keycode, 1);
206                 input_sync(dev);
207                 input_report_key(dev, keycode, 0);
208                 input_sync(dev);
209         } else {
210                 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
211                 input_sync(dev);
212         }
213 }
214
215 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
216 {
217         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
218         uint32_t kprec;
219
220         /* read and reset to default count value */
221         kprec = KPREC;
222         KPREC = DEFAULT_KPREC;
223
224         if (pdata->enable_rotary0)
225                 report_rotary_event(keypad, 0, rotary_delta(kprec));
226
227         if (pdata->enable_rotary1)
228                 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
229 }
230
231 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
232 {
233         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
234         unsigned int new_state;
235         uint32_t kpdk, bits_changed;
236         int i;
237
238         kpdk = KPDK;
239
240         if (pdata->enable_rotary0 || pdata->enable_rotary1)
241                 pxa27x_keypad_scan_rotary(keypad);
242
243         if (pdata->direct_key_map == NULL)
244                 return;
245
246         new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
247         bits_changed = keypad->direct_key_state ^ new_state;
248
249         if (bits_changed == 0)
250                 return;
251
252         for (i = 0; i < pdata->direct_key_num; i++) {
253                 if (bits_changed & (1 << i))
254                         input_report_key(keypad->input_dev,
255                                         pdata->direct_key_map[i],
256                                         (new_state & (1 << i)));
257         }
258         input_sync(keypad->input_dev);
259         keypad->direct_key_state = new_state;
260 }
261
262 static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
263 {
264         struct pxa27x_keypad *keypad = dev_id;
265         unsigned long kpc = KPC;
266
267         if (kpc & KPC_DI)
268                 pxa27x_keypad_scan_direct(keypad);
269
270         if (kpc & KPC_MI)
271                 pxa27x_keypad_scan_matrix(keypad);
272
273         return IRQ_HANDLED;
274 }
275
276 static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
277 {
278         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
279         unsigned int mask = 0, direct_key_num = 0;
280         unsigned long kpc = 0;
281
282         /* enable matrix keys with automatic scan */
283         if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
284                 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
285                 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
286                        KPC_MKCN(pdata->matrix_key_cols);
287         }
288
289         /* enable rotary key, debounce interval same as direct keys */
290         if (pdata->enable_rotary0) {
291                 mask |= 0x03;
292                 direct_key_num = 2;
293                 kpc |= KPC_REE0;
294         }
295
296         if (pdata->enable_rotary1) {
297                 mask |= 0x0c;
298                 direct_key_num = 4;
299                 kpc |= KPC_REE1;
300         }
301
302         if (pdata->direct_key_num > direct_key_num)
303                 direct_key_num = pdata->direct_key_num;
304
305         keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
306
307         /* enable direct key */
308         if (direct_key_num)
309                 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
310
311         KPC = kpc | KPC_RE_ZERO_DEB;
312         KPREC = DEFAULT_KPREC;
313 }
314
315 static int pxa27x_keypad_open(struct input_dev *dev)
316 {
317         struct pxa27x_keypad *keypad = input_get_drvdata(dev);
318
319         /* Enable unit clock */
320         clk_enable(keypad->clk);
321         pxa27x_keypad_config(keypad);
322
323         return 0;
324 }
325
326 static void pxa27x_keypad_close(struct input_dev *dev)
327 {
328         struct pxa27x_keypad *keypad = input_get_drvdata(dev);
329
330         /* Disable clock unit */
331         clk_disable(keypad->clk);
332 }
333
334 #ifdef CONFIG_PM
335 static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
336 {
337         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
338
339         clk_disable(keypad->clk);
340         return 0;
341 }
342
343 static int pxa27x_keypad_resume(struct platform_device *pdev)
344 {
345         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
346         struct input_dev *input_dev = keypad->input_dev;
347
348         mutex_lock(&input_dev->mutex);
349
350         if (input_dev->users) {
351                 /* Enable unit clock */
352                 clk_enable(keypad->clk);
353                 pxa27x_keypad_config(keypad);
354         }
355
356         mutex_unlock(&input_dev->mutex);
357
358         return 0;
359 }
360 #else
361 #define pxa27x_keypad_suspend   NULL
362 #define pxa27x_keypad_resume    NULL
363 #endif
364
365 static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
366 {
367         struct pxa27x_keypad *keypad;
368         struct input_dev *input_dev;
369         int error;
370
371         keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
372         if (keypad == NULL) {
373                 dev_err(&pdev->dev, "failed to allocate driver data\n");
374                 return -ENOMEM;
375         }
376
377         keypad->pdata = pdev->dev.platform_data;
378         if (keypad->pdata == NULL) {
379                 dev_err(&pdev->dev, "no platform data defined\n");
380                 error = -EINVAL;
381                 goto failed_free;
382         }
383
384         keypad->clk = clk_get(&pdev->dev, "KBDCLK");
385         if (IS_ERR(keypad->clk)) {
386                 dev_err(&pdev->dev, "failed to get keypad clock\n");
387                 error = PTR_ERR(keypad->clk);
388                 goto failed_free;
389         }
390
391         /* Create and register the input driver. */
392         input_dev = input_allocate_device();
393         if (!input_dev) {
394                 dev_err(&pdev->dev, "failed to allocate input device\n");
395                 error = -ENOMEM;
396                 goto failed_put_clk;
397         }
398
399         input_dev->name = DRIVER_NAME;
400         input_dev->id.bustype = BUS_HOST;
401         input_dev->open = pxa27x_keypad_open;
402         input_dev->close = pxa27x_keypad_close;
403         input_dev->dev.parent = &pdev->dev;
404
405         keypad->input_dev = input_dev;
406         input_set_drvdata(input_dev, keypad);
407
408         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
409                 BIT_MASK(EV_REL);
410
411         pxa27x_keypad_build_keycode(keypad);
412
413         error = request_irq(IRQ_KEYPAD, pxa27x_keypad_irq_handler, IRQF_DISABLED,
414                             DRIVER_NAME, keypad);
415         if (error) {
416                 printk(KERN_ERR "Cannot request keypad IRQ\n");
417                 goto err_free_dev;
418         }
419
420         platform_set_drvdata(pdev, keypad);
421
422         /* Register the input device */
423         error = input_register_device(input_dev);
424         if (error)
425                 goto err_free_irq;
426
427         return 0;
428
429  err_free_irq:
430         platform_set_drvdata(pdev, NULL);
431         free_irq(IRQ_KEYPAD, pdev);
432  err_free_dev:
433         input_free_device(input_dev);
434 failed_put_clk:
435         clk_put(keypad->clk);
436 failed_free:
437         kfree(keypad);
438         return error;
439 }
440
441 static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
442 {
443         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
444
445         free_irq(IRQ_KEYPAD, pdev);
446
447         clk_disable(keypad->clk);
448         clk_put(keypad->clk);
449
450         input_unregister_device(keypad->input_dev);
451
452         platform_set_drvdata(pdev, NULL);
453         kfree(keypad);
454         return 0;
455 }
456
457 static struct platform_driver pxa27x_keypad_driver = {
458         .probe          = pxa27x_keypad_probe,
459         .remove         = __devexit_p(pxa27x_keypad_remove),
460         .suspend        = pxa27x_keypad_suspend,
461         .resume         = pxa27x_keypad_resume,
462         .driver         = {
463                 .name   = DRIVER_NAME,
464         },
465 };
466
467 static int __init pxa27x_keypad_init(void)
468 {
469         return platform_driver_register(&pxa27x_keypad_driver);
470 }
471
472 static void __exit pxa27x_keypad_exit(void)
473 {
474         platform_driver_unregister(&pxa27x_keypad_driver);
475 }
476
477 module_init(pxa27x_keypad_init);
478 module_exit(pxa27x_keypad_exit);
479
480 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
481 MODULE_LICENSE("GPL");