2 * Driver for the IMX keypad port.
3 * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
4 * Copyright (C) 2013 Freescale Semiconductor, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * <<Power management needs to be implemented>>.
13 #include <linux/clk.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/input/matrix_keypad.h>
19 #include <linux/interrupt.h>
21 #include <linux/jiffies.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/timer.h>
30 * Keypad Controller registers (halfword)
32 #define KPCR 0x00 /* Keypad Control Register */
34 #define KPSR 0x02 /* Keypad Status Register */
35 #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
36 #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
37 #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
38 #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
39 #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
40 #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
41 #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
43 #define KDDR 0x04 /* Keypad Data Direction Register */
44 #define KPDR 0x06 /* Keypad Data Register */
46 #define MAX_MATRIX_KEY_ROWS 8
47 #define MAX_MATRIX_KEY_COLS 8
48 #define MATRIX_ROW_SHIFT 3
50 #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
55 struct input_dev *input_dev;
56 void __iomem *mmio_base;
59 struct timer_list check_matrix_timer;
62 * The matrix is stable only if no changes are detected after
63 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
65 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
70 /* Masks for enabled rows/cols */
71 unsigned short rows_en_mask;
72 unsigned short cols_en_mask;
74 unsigned short keycodes[MAX_MATRIX_KEY_NUM];
78 * -stable: achieved after a complete debounce process.
79 * -unstable: used in the debouncing process.
81 unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS];
82 unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS];
85 /* Scan the matrix and return the new state in *matrix_volatile_state. */
86 static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
87 unsigned short *matrix_volatile_state)
90 unsigned short reg_val;
92 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
93 if ((keypad->cols_en_mask & (1 << col)) == 0)
96 * Discharge keypad capacitance:
97 * 2. write 1s on column data.
98 * 3. configure columns as totem-pole to discharge capacitance.
99 * 4. configure columns as open-drain.
101 reg_val = readw(keypad->mmio_base + KPDR);
103 writew(reg_val, keypad->mmio_base + KPDR);
105 reg_val = readw(keypad->mmio_base + KPCR);
106 reg_val &= ~((keypad->cols_en_mask & 0xff) << 8);
107 writew(reg_val, keypad->mmio_base + KPCR);
111 reg_val = readw(keypad->mmio_base + KPCR);
112 reg_val |= (keypad->cols_en_mask & 0xff) << 8;
113 writew(reg_val, keypad->mmio_base + KPCR);
116 * 5. Write a single column to 0, others to 1.
117 * 6. Sample row inputs and save data.
118 * 7. Repeat steps 2 - 6 for remaining columns.
120 reg_val = readw(keypad->mmio_base + KPDR);
121 reg_val &= ~(1 << (8 + col));
122 writew(reg_val, keypad->mmio_base + KPDR);
125 * Delay added to avoid propagating the 0 from column to row
131 * 1s in matrix_volatile_state[col] means key pressures
132 * throw data from non enabled rows.
134 reg_val = readw(keypad->mmio_base + KPDR);
135 matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
139 * Return in standby mode:
140 * 9. write 0s to columns
142 reg_val = readw(keypad->mmio_base + KPDR);
144 writew(reg_val, keypad->mmio_base + KPDR);
148 * Compare the new matrix state (volatile) with the stable one stored in
149 * keypad->matrix_stable_state and fire events if changes are detected.
151 static void imx_keypad_fire_events(struct imx_keypad *keypad,
152 unsigned short *matrix_volatile_state)
154 struct input_dev *input_dev = keypad->input_dev;
157 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
158 unsigned short bits_changed;
161 if ((keypad->cols_en_mask & (1 << col)) == 0)
162 continue; /* Column is not enabled */
164 bits_changed = keypad->matrix_stable_state[col] ^
165 matrix_volatile_state[col];
167 if (bits_changed == 0)
168 continue; /* Column does not contain changes */
170 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
171 if ((keypad->rows_en_mask & (1 << row)) == 0)
172 continue; /* Row is not enabled */
173 if ((bits_changed & (1 << row)) == 0)
174 continue; /* Row does not contain changes */
176 code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
177 input_event(input_dev, EV_MSC, MSC_SCAN, code);
178 input_report_key(input_dev, keypad->keycodes[code],
179 matrix_volatile_state[col] & (1 << row));
180 dev_dbg(&input_dev->dev, "Event code: %d, val: %d",
181 keypad->keycodes[code],
182 matrix_volatile_state[col] & (1 << row));
185 input_sync(input_dev);
189 * imx_keypad_check_for_events is the timer handler.
191 static void imx_keypad_check_for_events(unsigned long data)
193 struct imx_keypad *keypad = (struct imx_keypad *) data;
194 unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
195 unsigned short reg_val;
196 bool state_changed, is_zero_matrix;
199 memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
201 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
203 state_changed = false;
204 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
205 if ((keypad->cols_en_mask & (1 << i)) == 0)
208 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
209 state_changed = true;
215 * If the matrix state is changed from the previous scan
216 * (Re)Begin the debouncing process, saving the new state in
217 * keypad->matrix_unstable_state.
219 * Increase the count of number of scans with a stable state.
222 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
223 sizeof(matrix_volatile_state));
224 keypad->stable_count = 0;
226 keypad->stable_count++;
229 * If the matrix is not as stable as we want reschedule scan
230 * in the near future.
232 if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
233 mod_timer(&keypad->check_matrix_timer,
234 jiffies + msecs_to_jiffies(10));
239 * If the matrix state is stable, fire the events and save the new
240 * stable state. Note, if the matrix is kept stable for longer
241 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
242 * events have already been generated.
244 if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
245 imx_keypad_fire_events(keypad, matrix_volatile_state);
247 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
248 sizeof(matrix_volatile_state));
251 is_zero_matrix = true;
252 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
253 if (matrix_volatile_state[i] != 0) {
254 is_zero_matrix = false;
260 if (is_zero_matrix) {
262 * All keys have been released. Enable only the KDI
263 * interrupt for future key presses (clear the KDI
264 * status bit and its sync chain before that).
266 reg_val = readw(keypad->mmio_base + KPSR);
267 reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
268 writew(reg_val, keypad->mmio_base + KPSR);
270 reg_val = readw(keypad->mmio_base + KPSR);
271 reg_val |= KBD_STAT_KDIE;
272 reg_val &= ~KBD_STAT_KRIE;
273 writew(reg_val, keypad->mmio_base + KPSR);
276 * Some keys are still pressed. Schedule a rescan in
277 * attempt to detect multiple key presses and enable
278 * the KRI interrupt to react quickly to key release
281 mod_timer(&keypad->check_matrix_timer,
282 jiffies + msecs_to_jiffies(60));
284 reg_val = readw(keypad->mmio_base + KPSR);
285 reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
286 writew(reg_val, keypad->mmio_base + KPSR);
288 reg_val = readw(keypad->mmio_base + KPSR);
289 reg_val |= KBD_STAT_KRIE;
290 reg_val &= ~KBD_STAT_KDIE;
291 writew(reg_val, keypad->mmio_base + KPSR);
295 static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
297 struct imx_keypad *keypad = dev_id;
298 unsigned short reg_val;
300 reg_val = readw(keypad->mmio_base + KPSR);
302 /* Disable both interrupt types */
303 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
304 /* Clear interrupts status bits */
305 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
306 writew(reg_val, keypad->mmio_base + KPSR);
308 if (keypad->enabled) {
309 /* The matrix is supposed to be changed */
310 keypad->stable_count = 0;
312 /* Schedule the scanning procedure near in the future */
313 mod_timer(&keypad->check_matrix_timer,
314 jiffies + msecs_to_jiffies(2));
320 static void imx_keypad_config(struct imx_keypad *keypad)
322 unsigned short reg_val;
325 * Include enabled rows in interrupt generation (KPCR[7:0])
326 * Configure keypad columns as open-drain (KPCR[15:8])
328 reg_val = readw(keypad->mmio_base + KPCR);
329 reg_val |= keypad->rows_en_mask & 0xff; /* rows */
330 reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
331 writew(reg_val, keypad->mmio_base + KPCR);
333 /* Write 0's to KPDR[15:8] (Colums) */
334 reg_val = readw(keypad->mmio_base + KPDR);
336 writew(reg_val, keypad->mmio_base + KPDR);
338 /* Configure columns as output, rows as input (KDDR[15:0]) */
339 writew(0xff00, keypad->mmio_base + KDDR);
342 * Clear Key Depress and Key Release status bit.
343 * Clear both synchronizer chain.
345 reg_val = readw(keypad->mmio_base + KPSR);
346 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
347 KBD_STAT_KDSC | KBD_STAT_KRSS;
348 writew(reg_val, keypad->mmio_base + KPSR);
350 /* Enable KDI and disable KRI (avoid false release events). */
351 reg_val |= KBD_STAT_KDIE;
352 reg_val &= ~KBD_STAT_KRIE;
353 writew(reg_val, keypad->mmio_base + KPSR);
356 static void imx_keypad_inhibit(struct imx_keypad *keypad)
358 unsigned short reg_val;
360 /* Inhibit KDI and KRI interrupts. */
361 reg_val = readw(keypad->mmio_base + KPSR);
362 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
363 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
364 writew(reg_val, keypad->mmio_base + KPSR);
366 /* Colums as open drain and disable all rows */
367 reg_val = (keypad->cols_en_mask & 0xff) << 8;
368 writew(reg_val, keypad->mmio_base + KPCR);
371 static void imx_keypad_close(struct input_dev *dev)
373 struct imx_keypad *keypad = input_get_drvdata(dev);
375 dev_dbg(&dev->dev, ">%s\n", __func__);
377 /* Mark keypad as being inactive */
378 keypad->enabled = false;
379 synchronize_irq(keypad->irq);
380 del_timer_sync(&keypad->check_matrix_timer);
382 imx_keypad_inhibit(keypad);
384 /* Disable clock unit */
385 clk_disable_unprepare(keypad->clk);
388 static int imx_keypad_open(struct input_dev *dev)
390 struct imx_keypad *keypad = input_get_drvdata(dev);
393 dev_dbg(&dev->dev, ">%s\n", __func__);
395 /* Enable the kpp clock */
396 error = clk_prepare_enable(keypad->clk);
400 /* We became active from now */
401 keypad->enabled = true;
403 imx_keypad_config(keypad);
405 /* Sanity control, not all the rows must be actived now. */
406 if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
408 "too many keys pressed, control pins initialisation\n");
415 imx_keypad_close(dev);
420 static struct of_device_id imx_keypad_of_match[] = {
421 { .compatible = "fsl,imx21-kpp", },
424 MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
427 static int imx_keypad_probe(struct platform_device *pdev)
429 const struct matrix_keymap_data *keymap_data = pdev->dev.platform_data;
430 struct imx_keypad *keypad;
431 struct input_dev *input_dev;
432 struct resource *res;
433 int irq, error, i, row, col;
435 if (!keymap_data && !pdev->dev.of_node) {
436 dev_err(&pdev->dev, "no keymap defined\n");
440 irq = platform_get_irq(pdev, 0);
442 dev_err(&pdev->dev, "no irq defined in platform data\n");
446 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
448 dev_err(&pdev->dev, "no I/O memory defined in platform data\n");
452 input_dev = devm_input_allocate_device(&pdev->dev);
454 dev_err(&pdev->dev, "failed to allocate the input device\n");
458 keypad = devm_kzalloc(&pdev->dev, sizeof(struct imx_keypad),
461 dev_err(&pdev->dev, "not enough memory for driver data\n");
465 keypad->input_dev = input_dev;
467 keypad->stable_count = 0;
469 setup_timer(&keypad->check_matrix_timer,
470 imx_keypad_check_for_events, (unsigned long) keypad);
472 keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
473 if (IS_ERR(keypad->mmio_base))
474 return PTR_ERR(keypad->mmio_base);
476 keypad->clk = devm_clk_get(&pdev->dev, NULL);
477 if (IS_ERR(keypad->clk)) {
478 dev_err(&pdev->dev, "failed to get keypad clock\n");
479 return PTR_ERR(keypad->clk);
482 /* Init the Input device */
483 input_dev->name = pdev->name;
484 input_dev->id.bustype = BUS_HOST;
485 input_dev->dev.parent = &pdev->dev;
486 input_dev->open = imx_keypad_open;
487 input_dev->close = imx_keypad_close;
489 error = matrix_keypad_build_keymap(keymap_data, NULL,
492 keypad->keycodes, input_dev);
494 dev_err(&pdev->dev, "failed to build keymap\n");
498 /* Search for rows and cols enabled */
499 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
500 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
501 i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
502 if (keypad->keycodes[i] != KEY_RESERVED) {
503 keypad->rows_en_mask |= 1 << row;
504 keypad->cols_en_mask |= 1 << col;
508 dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
509 dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
511 __set_bit(EV_REP, input_dev->evbit);
512 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
513 input_set_drvdata(input_dev, keypad);
515 /* Ensure that the keypad will stay dormant until opened */
516 clk_prepare_enable(keypad->clk);
517 imx_keypad_inhibit(keypad);
518 clk_disable_unprepare(keypad->clk);
520 error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
523 dev_err(&pdev->dev, "failed to request IRQ\n");
527 /* Register the input device */
528 error = input_register_device(input_dev);
530 dev_err(&pdev->dev, "failed to register input device\n");
534 platform_set_drvdata(pdev, keypad);
535 device_init_wakeup(&pdev->dev, 1);
540 #ifdef CONFIG_PM_SLEEP
541 static int imx_kbd_suspend(struct device *dev)
543 struct platform_device *pdev = to_platform_device(dev);
544 struct imx_keypad *kbd = platform_get_drvdata(pdev);
545 struct input_dev *input_dev = kbd->input_dev;
547 /* imx kbd can wake up system even clock is disabled */
548 mutex_lock(&input_dev->mutex);
550 if (input_dev->users)
551 clk_disable_unprepare(kbd->clk);
553 mutex_unlock(&input_dev->mutex);
555 if (device_may_wakeup(&pdev->dev))
556 enable_irq_wake(kbd->irq);
558 pinctrl_pm_select_sleep_state(dev);
563 static int imx_kbd_resume(struct device *dev)
565 struct platform_device *pdev = to_platform_device(dev);
566 struct imx_keypad *kbd = platform_get_drvdata(pdev);
567 struct input_dev *input_dev = kbd->input_dev;
570 if (device_may_wakeup(&pdev->dev))
571 disable_irq_wake(kbd->irq);
573 pinctrl_pm_select_default_state(dev);
575 mutex_lock(&input_dev->mutex);
577 if (input_dev->users) {
578 ret = clk_prepare_enable(kbd->clk);
584 mutex_unlock(&input_dev->mutex);
590 static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume);
592 static struct platform_driver imx_keypad_driver = {
594 .name = "imx-keypad",
595 .owner = THIS_MODULE,
596 .pm = &imx_kbd_pm_ops,
597 .of_match_table = of_match_ptr(imx_keypad_of_match),
599 .probe = imx_keypad_probe,
601 module_platform_driver(imx_keypad_driver);
603 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
604 MODULE_DESCRIPTION("IMX Keypad Port Driver");
605 MODULE_LICENSE("GPL v2");
606 MODULE_ALIAS("platform:imx-keypad");