Adding support for MOXA ART SoC. Testing port of linux-2.6.32.60-moxart.
[linux-3.6.7-moxart.git] / drivers / input / keyboard / imx_keypad.c
blobcdc252612c0b35c453b976355c13b26f6d6767e1
1 /*
2 * Driver for the IMX keypad port.
3 * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * <<Power management needs to be implemented>>.
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/input/matrix_keypad.h>
18 #include <linux/interrupt.h>
19 #include <linux/io.h>
20 #include <linux/jiffies.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/slab.h>
25 #include <linux/timer.h>
28 * Keypad Controller registers (halfword)
30 #define KPCR 0x00 /* Keypad Control Register */
32 #define KPSR 0x02 /* Keypad Status Register */
33 #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
34 #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
35 #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
36 #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
37 #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
38 #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
39 #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
41 #define KDDR 0x04 /* Keypad Data Direction Register */
42 #define KPDR 0x06 /* Keypad Data Register */
44 #define MAX_MATRIX_KEY_ROWS 8
45 #define MAX_MATRIX_KEY_COLS 8
46 #define MATRIX_ROW_SHIFT 3
48 #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
50 struct imx_keypad {
52 struct clk *clk;
53 struct input_dev *input_dev;
54 void __iomem *mmio_base;
56 int irq;
57 struct timer_list check_matrix_timer;
60 * The matrix is stable only if no changes are detected after
61 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
63 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
64 int stable_count;
66 bool enabled;
68 /* Masks for enabled rows/cols */
69 unsigned short rows_en_mask;
70 unsigned short cols_en_mask;
72 unsigned short keycodes[MAX_MATRIX_KEY_NUM];
75 * Matrix states:
76 * -stable: achieved after a complete debounce process.
77 * -unstable: used in the debouncing process.
79 unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS];
80 unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS];
83 /* Scan the matrix and return the new state in *matrix_volatile_state. */
84 static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
85 unsigned short *matrix_volatile_state)
87 int col;
88 unsigned short reg_val;
90 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
91 if ((keypad->cols_en_mask & (1 << col)) == 0)
92 continue;
94 * Discharge keypad capacitance:
95 * 2. write 1s on column data.
96 * 3. configure columns as totem-pole to discharge capacitance.
97 * 4. configure columns as open-drain.
99 reg_val = readw(keypad->mmio_base + KPDR);
100 reg_val |= 0xff00;
101 writew(reg_val, keypad->mmio_base + KPDR);
103 reg_val = readw(keypad->mmio_base + KPCR);
104 reg_val &= ~((keypad->cols_en_mask & 0xff) << 8);
105 writew(reg_val, keypad->mmio_base + KPCR);
107 udelay(2);
109 reg_val = readw(keypad->mmio_base + KPCR);
110 reg_val |= (keypad->cols_en_mask & 0xff) << 8;
111 writew(reg_val, keypad->mmio_base + KPCR);
114 * 5. Write a single column to 0, others to 1.
115 * 6. Sample row inputs and save data.
116 * 7. Repeat steps 2 - 6 for remaining columns.
118 reg_val = readw(keypad->mmio_base + KPDR);
119 reg_val &= ~(1 << (8 + col));
120 writew(reg_val, keypad->mmio_base + KPDR);
123 * Delay added to avoid propagating the 0 from column to row
124 * when scanning.
126 udelay(5);
129 * 1s in matrix_volatile_state[col] means key pressures
130 * throw data from non enabled rows.
132 reg_val = readw(keypad->mmio_base + KPDR);
133 matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
137 * Return in standby mode:
138 * 9. write 0s to columns
140 reg_val = readw(keypad->mmio_base + KPDR);
141 reg_val &= 0x00ff;
142 writew(reg_val, keypad->mmio_base + KPDR);
146 * Compare the new matrix state (volatile) with the stable one stored in
147 * keypad->matrix_stable_state and fire events if changes are detected.
149 static void imx_keypad_fire_events(struct imx_keypad *keypad,
150 unsigned short *matrix_volatile_state)
152 struct input_dev *input_dev = keypad->input_dev;
153 int row, col;
155 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
156 unsigned short bits_changed;
157 int code;
159 if ((keypad->cols_en_mask & (1 << col)) == 0)
160 continue; /* Column is not enabled */
162 bits_changed = keypad->matrix_stable_state[col] ^
163 matrix_volatile_state[col];
165 if (bits_changed == 0)
166 continue; /* Column does not contain changes */
168 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
169 if ((keypad->rows_en_mask & (1 << row)) == 0)
170 continue; /* Row is not enabled */
171 if ((bits_changed & (1 << row)) == 0)
172 continue; /* Row does not contain changes */
174 code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
175 input_event(input_dev, EV_MSC, MSC_SCAN, code);
176 input_report_key(input_dev, keypad->keycodes[code],
177 matrix_volatile_state[col] & (1 << row));
178 dev_dbg(&input_dev->dev, "Event code: %d, val: %d",
179 keypad->keycodes[code],
180 matrix_volatile_state[col] & (1 << row));
183 input_sync(input_dev);
187 * imx_keypad_check_for_events is the timer handler.
189 static void imx_keypad_check_for_events(unsigned long data)
191 struct imx_keypad *keypad = (struct imx_keypad *) data;
192 unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
193 unsigned short reg_val;
194 bool state_changed, is_zero_matrix;
195 int i;
197 memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
199 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
201 state_changed = false;
202 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
203 if ((keypad->cols_en_mask & (1 << i)) == 0)
204 continue;
206 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
207 state_changed = true;
208 break;
213 * If the matrix state is changed from the previous scan
214 * (Re)Begin the debouncing process, saving the new state in
215 * keypad->matrix_unstable_state.
216 * else
217 * Increase the count of number of scans with a stable state.
219 if (state_changed) {
220 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
221 sizeof(matrix_volatile_state));
222 keypad->stable_count = 0;
223 } else
224 keypad->stable_count++;
227 * If the matrix is not as stable as we want reschedule scan
228 * in the near future.
230 if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
231 mod_timer(&keypad->check_matrix_timer,
232 jiffies + msecs_to_jiffies(10));
233 return;
237 * If the matrix state is stable, fire the events and save the new
238 * stable state. Note, if the matrix is kept stable for longer
239 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
240 * events have already been generated.
242 if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
243 imx_keypad_fire_events(keypad, matrix_volatile_state);
245 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
246 sizeof(matrix_volatile_state));
249 is_zero_matrix = true;
250 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
251 if (matrix_volatile_state[i] != 0) {
252 is_zero_matrix = false;
253 break;
258 if (is_zero_matrix) {
260 * All keys have been released. Enable only the KDI
261 * interrupt for future key presses (clear the KDI
262 * status bit and its sync chain before that).
264 reg_val = readw(keypad->mmio_base + KPSR);
265 reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
266 writew(reg_val, keypad->mmio_base + KPSR);
268 reg_val = readw(keypad->mmio_base + KPSR);
269 reg_val |= KBD_STAT_KDIE;
270 reg_val &= ~KBD_STAT_KRIE;
271 writew(reg_val, keypad->mmio_base + KPSR);
272 } else {
274 * Some keys are still pressed. Schedule a rescan in
275 * attempt to detect multiple key presses and enable
276 * the KRI interrupt to react quickly to key release
277 * event.
279 mod_timer(&keypad->check_matrix_timer,
280 jiffies + msecs_to_jiffies(60));
282 reg_val = readw(keypad->mmio_base + KPSR);
283 reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
284 writew(reg_val, keypad->mmio_base + KPSR);
286 reg_val = readw(keypad->mmio_base + KPSR);
287 reg_val |= KBD_STAT_KRIE;
288 reg_val &= ~KBD_STAT_KDIE;
289 writew(reg_val, keypad->mmio_base + KPSR);
293 static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
295 struct imx_keypad *keypad = dev_id;
296 unsigned short reg_val;
298 reg_val = readw(keypad->mmio_base + KPSR);
300 /* Disable both interrupt types */
301 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
302 /* Clear interrupts status bits */
303 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
304 writew(reg_val, keypad->mmio_base + KPSR);
306 if (keypad->enabled) {
307 /* The matrix is supposed to be changed */
308 keypad->stable_count = 0;
310 /* Schedule the scanning procedure near in the future */
311 mod_timer(&keypad->check_matrix_timer,
312 jiffies + msecs_to_jiffies(2));
315 return IRQ_HANDLED;
318 static void imx_keypad_config(struct imx_keypad *keypad)
320 unsigned short reg_val;
323 * Include enabled rows in interrupt generation (KPCR[7:0])
324 * Configure keypad columns as open-drain (KPCR[15:8])
326 reg_val = readw(keypad->mmio_base + KPCR);
327 reg_val |= keypad->rows_en_mask & 0xff; /* rows */
328 reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
329 writew(reg_val, keypad->mmio_base + KPCR);
331 /* Write 0's to KPDR[15:8] (Colums) */
332 reg_val = readw(keypad->mmio_base + KPDR);
333 reg_val &= 0x00ff;
334 writew(reg_val, keypad->mmio_base + KPDR);
336 /* Configure columns as output, rows as input (KDDR[15:0]) */
337 writew(0xff00, keypad->mmio_base + KDDR);
340 * Clear Key Depress and Key Release status bit.
341 * Clear both synchronizer chain.
343 reg_val = readw(keypad->mmio_base + KPSR);
344 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
345 KBD_STAT_KDSC | KBD_STAT_KRSS;
346 writew(reg_val, keypad->mmio_base + KPSR);
348 /* Enable KDI and disable KRI (avoid false release events). */
349 reg_val |= KBD_STAT_KDIE;
350 reg_val &= ~KBD_STAT_KRIE;
351 writew(reg_val, keypad->mmio_base + KPSR);
354 static void imx_keypad_inhibit(struct imx_keypad *keypad)
356 unsigned short reg_val;
358 /* Inhibit KDI and KRI interrupts. */
359 reg_val = readw(keypad->mmio_base + KPSR);
360 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
361 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
362 writew(reg_val, keypad->mmio_base + KPSR);
364 /* Colums as open drain and disable all rows */
365 writew(0xff00, keypad->mmio_base + KPCR);
368 static void imx_keypad_close(struct input_dev *dev)
370 struct imx_keypad *keypad = input_get_drvdata(dev);
372 dev_dbg(&dev->dev, ">%s\n", __func__);
374 /* Mark keypad as being inactive */
375 keypad->enabled = false;
376 synchronize_irq(keypad->irq);
377 del_timer_sync(&keypad->check_matrix_timer);
379 imx_keypad_inhibit(keypad);
381 /* Disable clock unit */
382 clk_disable_unprepare(keypad->clk);
385 static int imx_keypad_open(struct input_dev *dev)
387 struct imx_keypad *keypad = input_get_drvdata(dev);
388 int error;
390 dev_dbg(&dev->dev, ">%s\n", __func__);
392 /* Enable the kpp clock */
393 error = clk_prepare_enable(keypad->clk);
394 if (error)
395 return error;
397 /* We became active from now */
398 keypad->enabled = true;
400 imx_keypad_config(keypad);
402 /* Sanity control, not all the rows must be actived now. */
403 if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
404 dev_err(&dev->dev,
405 "too many keys pressed, control pins initialisation\n");
406 goto open_err;
409 return 0;
411 open_err:
412 imx_keypad_close(dev);
413 return -EIO;
416 static int __devinit imx_keypad_probe(struct platform_device *pdev)
418 const struct matrix_keymap_data *keymap_data = pdev->dev.platform_data;
419 struct imx_keypad *keypad;
420 struct input_dev *input_dev;
421 struct resource *res;
422 int irq, error, i;
424 if (keymap_data == NULL) {
425 dev_err(&pdev->dev, "no keymap defined\n");
426 return -EINVAL;
429 irq = platform_get_irq(pdev, 0);
430 if (irq < 0) {
431 dev_err(&pdev->dev, "no irq defined in platform data\n");
432 return -EINVAL;
435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
436 if (res == NULL) {
437 dev_err(&pdev->dev, "no I/O memory defined in platform data\n");
438 return -EINVAL;
441 res = request_mem_region(res->start, resource_size(res), pdev->name);
442 if (res == NULL) {
443 dev_err(&pdev->dev, "failed to request I/O memory\n");
444 return -EBUSY;
447 input_dev = input_allocate_device();
448 if (!input_dev) {
449 dev_err(&pdev->dev, "failed to allocate the input device\n");
450 error = -ENOMEM;
451 goto failed_rel_mem;
454 keypad = kzalloc(sizeof(struct imx_keypad), GFP_KERNEL);
455 if (!keypad) {
456 dev_err(&pdev->dev, "not enough memory for driver data\n");
457 error = -ENOMEM;
458 goto failed_free_input;
461 keypad->input_dev = input_dev;
462 keypad->irq = irq;
463 keypad->stable_count = 0;
465 setup_timer(&keypad->check_matrix_timer,
466 imx_keypad_check_for_events, (unsigned long) keypad);
468 keypad->mmio_base = ioremap(res->start, resource_size(res));
469 if (keypad->mmio_base == NULL) {
470 dev_err(&pdev->dev, "failed to remap I/O memory\n");
471 error = -ENOMEM;
472 goto failed_free_priv;
475 keypad->clk = clk_get(&pdev->dev, NULL);
476 if (IS_ERR(keypad->clk)) {
477 dev_err(&pdev->dev, "failed to get keypad clock\n");
478 error = PTR_ERR(keypad->clk);
479 goto failed_unmap;
482 /* Search for rows and cols enabled */
483 for (i = 0; i < keymap_data->keymap_size; i++) {
484 keypad->rows_en_mask |= 1 << KEY_ROW(keymap_data->keymap[i]);
485 keypad->cols_en_mask |= 1 << KEY_COL(keymap_data->keymap[i]);
488 if (keypad->rows_en_mask > ((1 << MAX_MATRIX_KEY_ROWS) - 1) ||
489 keypad->cols_en_mask > ((1 << MAX_MATRIX_KEY_COLS) - 1)) {
490 dev_err(&pdev->dev,
491 "invalid key data (too many rows or colums)\n");
492 error = -EINVAL;
493 goto failed_clock_put;
495 dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
496 dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
498 /* Init the Input device */
499 input_dev->name = pdev->name;
500 input_dev->id.bustype = BUS_HOST;
501 input_dev->dev.parent = &pdev->dev;
502 input_dev->open = imx_keypad_open;
503 input_dev->close = imx_keypad_close;
505 error = matrix_keypad_build_keymap(keymap_data, NULL,
506 MAX_MATRIX_KEY_ROWS,
507 MAX_MATRIX_KEY_COLS,
508 keypad->keycodes, input_dev);
509 if (error) {
510 dev_err(&pdev->dev, "failed to build keymap\n");
511 goto failed_clock_put;
514 __set_bit(EV_REP, input_dev->evbit);
515 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
516 input_set_drvdata(input_dev, keypad);
518 /* Ensure that the keypad will stay dormant until opened */
519 clk_prepare_enable(keypad->clk);
520 imx_keypad_inhibit(keypad);
521 clk_disable_unprepare(keypad->clk);
523 error = request_irq(irq, imx_keypad_irq_handler, 0,
524 pdev->name, keypad);
525 if (error) {
526 dev_err(&pdev->dev, "failed to request IRQ\n");
527 goto failed_clock_put;
530 /* Register the input device */
531 error = input_register_device(input_dev);
532 if (error) {
533 dev_err(&pdev->dev, "failed to register input device\n");
534 goto failed_free_irq;
537 platform_set_drvdata(pdev, keypad);
538 device_init_wakeup(&pdev->dev, 1);
540 return 0;
542 failed_free_irq:
543 free_irq(irq, pdev);
544 failed_clock_put:
545 clk_put(keypad->clk);
546 failed_unmap:
547 iounmap(keypad->mmio_base);
548 failed_free_priv:
549 kfree(keypad);
550 failed_free_input:
551 input_free_device(input_dev);
552 failed_rel_mem:
553 release_mem_region(res->start, resource_size(res));
554 return error;
557 static int __devexit imx_keypad_remove(struct platform_device *pdev)
559 struct imx_keypad *keypad = platform_get_drvdata(pdev);
560 struct resource *res;
562 dev_dbg(&pdev->dev, ">%s\n", __func__);
564 platform_set_drvdata(pdev, NULL);
566 input_unregister_device(keypad->input_dev);
568 free_irq(keypad->irq, keypad);
569 clk_put(keypad->clk);
571 iounmap(keypad->mmio_base);
572 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
573 release_mem_region(res->start, resource_size(res));
575 kfree(keypad);
577 return 0;
580 #ifdef CONFIG_PM_SLEEP
581 static int imx_kbd_suspend(struct device *dev)
583 struct platform_device *pdev = to_platform_device(dev);
584 struct imx_keypad *kbd = platform_get_drvdata(pdev);
585 struct input_dev *input_dev = kbd->input_dev;
587 /* imx kbd can wake up system even clock is disabled */
588 mutex_lock(&input_dev->mutex);
590 if (input_dev->users)
591 clk_disable_unprepare(kbd->clk);
593 mutex_unlock(&input_dev->mutex);
595 if (device_may_wakeup(&pdev->dev))
596 enable_irq_wake(kbd->irq);
598 return 0;
601 static int imx_kbd_resume(struct device *dev)
603 struct platform_device *pdev = to_platform_device(dev);
604 struct imx_keypad *kbd = platform_get_drvdata(pdev);
605 struct input_dev *input_dev = kbd->input_dev;
606 int ret = 0;
608 if (device_may_wakeup(&pdev->dev))
609 disable_irq_wake(kbd->irq);
611 mutex_lock(&input_dev->mutex);
613 if (input_dev->users) {
614 ret = clk_prepare_enable(kbd->clk);
615 if (ret)
616 goto err_clk;
619 err_clk:
620 mutex_unlock(&input_dev->mutex);
622 return ret;
624 #endif
626 static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume);
628 static struct platform_driver imx_keypad_driver = {
629 .driver = {
630 .name = "imx-keypad",
631 .owner = THIS_MODULE,
632 .pm = &imx_kbd_pm_ops,
634 .probe = imx_keypad_probe,
635 .remove = __devexit_p(imx_keypad_remove),
637 module_platform_driver(imx_keypad_driver);
639 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
640 MODULE_DESCRIPTION("IMX Keypad Port Driver");
641 MODULE_LICENSE("GPL v2");
642 MODULE_ALIAS("platform:imx-keypad");