14 #include <linux/device.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
24 #include <linux/slab.h>
33 #define KBD_STAT_KPKD (0x1 << 0)
34 #define KBD_STAT_KPKR (0x1 << 1)
35 #define KBD_STAT_KDSC (0x1 << 2)
36 #define KBD_STAT_KRSS (0x1 << 3)
37 #define KBD_STAT_KDIE (0x1 << 8)
38 #define KBD_STAT_KRIE (0x1 << 9)
39 #define KBD_STAT_KPPEN (0x1 << 10)
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)
63 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
84 static void imx_keypad_scan_matrix(
struct imx_keypad *keypad,
85 unsigned short *matrix_volatile_state)
119 reg_val &= ~(1 << (8 + col));
133 matrix_volatile_state[col] = (~reg_val) & keypad->
rows_en_mask;
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;
156 unsigned short bits_changed;
163 matrix_volatile_state[col];
165 if (bits_changed == 0)
171 if ((bits_changed & (1 << row)) == 0)
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",
180 matrix_volatile_state[col] & (1 << row));
183 input_sync(input_dev);
189 static void imx_keypad_check_for_events(
unsigned long data)
194 bool state_changed, is_zero_matrix;
197 memset(matrix_volatile_state, 0,
sizeof(matrix_volatile_state));
199 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
201 state_changed =
false;
207 state_changed =
true;
221 sizeof(matrix_volatile_state));
243 imx_keypad_fire_events(keypad, matrix_volatile_state);
246 sizeof(matrix_volatile_state));
249 is_zero_matrix =
true;
251 if (matrix_volatile_state[i] != 0) {
252 is_zero_matrix =
false;
258 if (is_zero_matrix) {
318 static void imx_keypad_config(
struct imx_keypad *keypad)
354 static void imx_keypad_inhibit(
struct imx_keypad *keypad)
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__);
379 imx_keypad_inhibit(keypad);
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);
390 dev_dbg(&dev->dev,
">%s\n", __func__);
393 error = clk_prepare_enable(keypad->
clk);
400 imx_keypad_config(keypad);
405 "too many keys pressed, control pins initialisation\n");
412 imx_keypad_close(dev);
420 struct input_dev *input_dev;
424 if (keymap_data ==
NULL) {
431 dev_err(&pdev->
dev,
"no irq defined in platform data\n");
437 dev_err(&pdev->
dev,
"no I/O memory defined in platform data\n");
443 dev_err(&pdev->
dev,
"failed to request I/O memory\n");
447 input_dev = input_allocate_device();
449 dev_err(&pdev->
dev,
"failed to allocate the input device\n");
456 dev_err(&pdev->
dev,
"not enough memory for driver data\n");
458 goto failed_free_input;
466 imx_keypad_check_for_events, (
unsigned long) keypad);
470 dev_err(&pdev->
dev,
"failed to remap I/O memory\n");
472 goto failed_free_priv;
476 if (IS_ERR(keypad->
clk)) {
477 dev_err(&pdev->
dev,
"failed to get keypad clock\n");
478 error = PTR_ERR(keypad->
clk);
488 if (keypad->
rows_en_mask > ((1 << MAX_MATRIX_KEY_ROWS) - 1) ||
489 keypad->
cols_en_mask > ((1 << MAX_MATRIX_KEY_COLS) - 1)) {
491 "invalid key data (too many rows or colums)\n");
493 goto failed_clock_put;
499 input_dev->name = pdev->
name;
501 input_dev->dev.parent = &pdev->
dev;
502 input_dev->open = imx_keypad_open;
503 input_dev->close = imx_keypad_close;
510 dev_err(&pdev->
dev,
"failed to build keymap\n");
511 goto failed_clock_put;
516 input_set_drvdata(input_dev, keypad);
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,
526 dev_err(&pdev->
dev,
"failed to request IRQ\n");
527 goto failed_clock_put;
531 error = input_register_device(input_dev);
533 dev_err(&pdev->
dev,
"failed to register input device\n");
534 goto failed_free_irq;
537 platform_set_drvdata(pdev, keypad);
551 input_free_device(input_dev);
559 struct imx_keypad *keypad = platform_get_drvdata(pdev);
564 platform_set_drvdata(pdev,
NULL);
566 input_unregister_device(keypad->
input_dev);
580 #ifdef CONFIG_PM_SLEEP
581 static int imx_kbd_suspend(
struct device *dev)
584 struct imx_keypad *kbd = platform_get_drvdata(pdev);
585 struct input_dev *input_dev = kbd->
input_dev;
590 if (input_dev->users)
591 clk_disable_unprepare(kbd->
clk);
595 if (device_may_wakeup(&pdev->
dev))
596 enable_irq_wake(kbd->
irq);
601 static int imx_kbd_resume(
struct device *dev)
604 struct imx_keypad *kbd = platform_get_drvdata(pdev);
605 struct input_dev *input_dev = kbd->
input_dev;
608 if (device_may_wakeup(&pdev->
dev))
609 disable_irq_wake(kbd->
irq);
613 if (input_dev->users) {
614 ret = clk_prepare_enable(kbd->
clk);
630 .name =
"imx-keypad",
632 .pm = &imx_kbd_pm_ops,
634 .probe = imx_keypad_probe,