1 // SPDX-License-Identifier: GPL-2.0
3 // Driver for the IMX keypad port.
4 // Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
7 #include <linux/delay.h>
8 #include <linux/device.h>
10 #include <linux/input/matrix_keypad.h>
11 #include <linux/interrupt.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/timer.h>
22 * Keypad Controller registers (halfword)
24 #define KPCR 0x00 /* Keypad Control Register */
26 #define KPSR 0x02 /* Keypad Status Register */
27 #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
28 #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
29 #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
30 #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
31 #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
32 #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
33 #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
35 #define KDDR 0x04 /* Keypad Data Direction Register */
36 #define KPDR 0x06 /* Keypad Data Register */
38 #define MAX_MATRIX_KEY_ROWS 8
39 #define MAX_MATRIX_KEY_COLS 8
40 #define MATRIX_ROW_SHIFT 3
42 #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
47 struct input_dev
*input_dev
;
48 void __iomem
*mmio_base
;
51 struct timer_list check_matrix_timer
;
54 * The matrix is stable only if no changes are detected after
55 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
57 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
62 /* Masks for enabled rows/cols */
63 unsigned short rows_en_mask
;
64 unsigned short cols_en_mask
;
66 unsigned short keycodes
[MAX_MATRIX_KEY_NUM
];
70 * -stable: achieved after a complete debounce process.
71 * -unstable: used in the debouncing process.
73 unsigned short matrix_stable_state
[MAX_MATRIX_KEY_COLS
];
74 unsigned short matrix_unstable_state
[MAX_MATRIX_KEY_COLS
];
77 /* Scan the matrix and return the new state in *matrix_volatile_state. */
78 static void imx_keypad_scan_matrix(struct imx_keypad
*keypad
,
79 unsigned short *matrix_volatile_state
)
82 unsigned short reg_val
;
84 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
85 if ((keypad
->cols_en_mask
& (1 << col
)) == 0)
88 * Discharge keypad capacitance:
89 * 2. write 1s on column data.
90 * 3. configure columns as totem-pole to discharge capacitance.
91 * 4. configure columns as open-drain.
93 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
95 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
97 reg_val
= readw(keypad
->mmio_base
+ KPCR
);
98 reg_val
&= ~((keypad
->cols_en_mask
& 0xff) << 8);
99 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
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
);
108 * 5. Write a single column to 0, others to 1.
109 * 6. Sample row inputs and save data.
110 * 7. Repeat steps 2 - 6 for remaining columns.
112 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
113 reg_val
&= ~(1 << (8 + col
));
114 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
117 * Delay added to avoid propagating the 0 from column to row
123 * 1s in matrix_volatile_state[col] means key pressures
124 * throw data from non enabled rows.
126 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
127 matrix_volatile_state
[col
] = (~reg_val
) & keypad
->rows_en_mask
;
131 * Return in standby mode:
132 * 9. write 0s to columns
134 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
136 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
140 * Compare the new matrix state (volatile) with the stable one stored in
141 * keypad->matrix_stable_state and fire events if changes are detected.
143 static void imx_keypad_fire_events(struct imx_keypad
*keypad
,
144 unsigned short *matrix_volatile_state
)
146 struct input_dev
*input_dev
= keypad
->input_dev
;
149 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
150 unsigned short bits_changed
;
153 if ((keypad
->cols_en_mask
& (1 << col
)) == 0)
154 continue; /* Column is not enabled */
156 bits_changed
= keypad
->matrix_stable_state
[col
] ^
157 matrix_volatile_state
[col
];
159 if (bits_changed
== 0)
160 continue; /* Column does not contain changes */
162 for (row
= 0; row
< MAX_MATRIX_KEY_ROWS
; row
++) {
163 if ((keypad
->rows_en_mask
& (1 << row
)) == 0)
164 continue; /* Row is not enabled */
165 if ((bits_changed
& (1 << row
)) == 0)
166 continue; /* Row does not contain changes */
168 code
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
169 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
170 input_report_key(input_dev
, keypad
->keycodes
[code
],
171 matrix_volatile_state
[col
] & (1 << row
));
172 dev_dbg(&input_dev
->dev
, "Event code: %d, val: %d",
173 keypad
->keycodes
[code
],
174 matrix_volatile_state
[col
] & (1 << row
));
177 input_sync(input_dev
);
181 * imx_keypad_check_for_events is the timer handler.
183 static void imx_keypad_check_for_events(struct timer_list
*t
)
185 struct imx_keypad
*keypad
= from_timer(keypad
, t
, check_matrix_timer
);
186 unsigned short matrix_volatile_state
[MAX_MATRIX_KEY_COLS
];
187 unsigned short reg_val
;
188 bool state_changed
, is_zero_matrix
;
191 memset(matrix_volatile_state
, 0, sizeof(matrix_volatile_state
));
193 imx_keypad_scan_matrix(keypad
, matrix_volatile_state
);
195 state_changed
= false;
196 for (i
= 0; i
< MAX_MATRIX_KEY_COLS
; i
++) {
197 if ((keypad
->cols_en_mask
& (1 << i
)) == 0)
200 if (keypad
->matrix_unstable_state
[i
] ^ matrix_volatile_state
[i
]) {
201 state_changed
= true;
207 * If the matrix state is changed from the previous scan
208 * (Re)Begin the debouncing process, saving the new state in
209 * keypad->matrix_unstable_state.
211 * Increase the count of number of scans with a stable state.
214 memcpy(keypad
->matrix_unstable_state
, matrix_volatile_state
,
215 sizeof(matrix_volatile_state
));
216 keypad
->stable_count
= 0;
218 keypad
->stable_count
++;
221 * If the matrix is not as stable as we want reschedule scan
222 * in the near future.
224 if (keypad
->stable_count
< IMX_KEYPAD_SCANS_FOR_STABILITY
) {
225 mod_timer(&keypad
->check_matrix_timer
,
226 jiffies
+ msecs_to_jiffies(10));
231 * If the matrix state is stable, fire the events and save the new
232 * stable state. Note, if the matrix is kept stable for longer
233 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
234 * events have already been generated.
236 if (keypad
->stable_count
== IMX_KEYPAD_SCANS_FOR_STABILITY
) {
237 imx_keypad_fire_events(keypad
, matrix_volatile_state
);
239 memcpy(keypad
->matrix_stable_state
, matrix_volatile_state
,
240 sizeof(matrix_volatile_state
));
243 is_zero_matrix
= true;
244 for (i
= 0; i
< MAX_MATRIX_KEY_COLS
; i
++) {
245 if (matrix_volatile_state
[i
] != 0) {
246 is_zero_matrix
= false;
252 if (is_zero_matrix
) {
254 * All keys have been released. Enable only the KDI
255 * interrupt for future key presses (clear the KDI
256 * status bit and its sync chain before that).
258 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
259 reg_val
|= KBD_STAT_KPKD
| KBD_STAT_KDSC
;
260 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
262 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
263 reg_val
|= KBD_STAT_KDIE
;
264 reg_val
&= ~KBD_STAT_KRIE
;
265 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
268 * Some keys are still pressed. Schedule a rescan in
269 * attempt to detect multiple key presses and enable
270 * the KRI interrupt to react quickly to key release
273 mod_timer(&keypad
->check_matrix_timer
,
274 jiffies
+ msecs_to_jiffies(60));
276 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
277 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KRSS
;
278 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
280 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
281 reg_val
|= KBD_STAT_KRIE
;
282 reg_val
&= ~KBD_STAT_KDIE
;
283 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
287 static irqreturn_t
imx_keypad_irq_handler(int irq
, void *dev_id
)
289 struct imx_keypad
*keypad
= dev_id
;
290 unsigned short reg_val
;
292 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
294 /* Disable both interrupt types */
295 reg_val
&= ~(KBD_STAT_KRIE
| KBD_STAT_KDIE
);
296 /* Clear interrupts status bits */
297 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
;
298 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
300 if (keypad
->enabled
) {
301 /* The matrix is supposed to be changed */
302 keypad
->stable_count
= 0;
304 /* Schedule the scanning procedure near in the future */
305 mod_timer(&keypad
->check_matrix_timer
,
306 jiffies
+ msecs_to_jiffies(2));
312 static void imx_keypad_config(struct imx_keypad
*keypad
)
314 unsigned short reg_val
;
317 * Include enabled rows in interrupt generation (KPCR[7:0])
318 * Configure keypad columns as open-drain (KPCR[15:8])
320 reg_val
= readw(keypad
->mmio_base
+ KPCR
);
321 reg_val
|= keypad
->rows_en_mask
& 0xff; /* rows */
322 reg_val
|= (keypad
->cols_en_mask
& 0xff) << 8; /* cols */
323 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
325 /* Write 0's to KPDR[15:8] (Colums) */
326 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
328 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
330 /* Configure columns as output, rows as input (KDDR[15:0]) */
331 writew(0xff00, keypad
->mmio_base
+ KDDR
);
334 * Clear Key Depress and Key Release status bit.
335 * Clear both synchronizer chain.
337 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
338 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
|
339 KBD_STAT_KDSC
| KBD_STAT_KRSS
;
340 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
342 /* Enable KDI and disable KRI (avoid false release events). */
343 reg_val
|= KBD_STAT_KDIE
;
344 reg_val
&= ~KBD_STAT_KRIE
;
345 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
348 static void imx_keypad_inhibit(struct imx_keypad
*keypad
)
350 unsigned short reg_val
;
352 /* Inhibit KDI and KRI interrupts. */
353 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
354 reg_val
&= ~(KBD_STAT_KRIE
| KBD_STAT_KDIE
);
355 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
;
356 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
358 /* Colums as open drain and disable all rows */
359 reg_val
= (keypad
->cols_en_mask
& 0xff) << 8;
360 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
363 static void imx_keypad_close(struct input_dev
*dev
)
365 struct imx_keypad
*keypad
= input_get_drvdata(dev
);
367 dev_dbg(&dev
->dev
, ">%s\n", __func__
);
369 /* Mark keypad as being inactive */
370 keypad
->enabled
= false;
371 synchronize_irq(keypad
->irq
);
372 del_timer_sync(&keypad
->check_matrix_timer
);
374 imx_keypad_inhibit(keypad
);
376 /* Disable clock unit */
377 clk_disable_unprepare(keypad
->clk
);
380 static int imx_keypad_open(struct input_dev
*dev
)
382 struct imx_keypad
*keypad
= input_get_drvdata(dev
);
385 dev_dbg(&dev
->dev
, ">%s\n", __func__
);
387 /* Enable the kpp clock */
388 error
= clk_prepare_enable(keypad
->clk
);
392 /* We became active from now */
393 keypad
->enabled
= true;
395 imx_keypad_config(keypad
);
397 /* Sanity control, not all the rows must be actived now. */
398 if ((readw(keypad
->mmio_base
+ KPDR
) & keypad
->rows_en_mask
) == 0) {
400 "too many keys pressed, control pins initialisation\n");
407 imx_keypad_close(dev
);
412 static const struct of_device_id imx_keypad_of_match
[] = {
413 { .compatible
= "fsl,imx21-kpp", },
416 MODULE_DEVICE_TABLE(of
, imx_keypad_of_match
);
419 static int imx_keypad_probe(struct platform_device
*pdev
)
421 const struct matrix_keymap_data
*keymap_data
=
422 dev_get_platdata(&pdev
->dev
);
423 struct imx_keypad
*keypad
;
424 struct input_dev
*input_dev
;
425 int irq
, error
, i
, row
, col
;
427 if (!keymap_data
&& !pdev
->dev
.of_node
) {
428 dev_err(&pdev
->dev
, "no keymap defined\n");
432 irq
= platform_get_irq(pdev
, 0);
436 input_dev
= devm_input_allocate_device(&pdev
->dev
);
438 dev_err(&pdev
->dev
, "failed to allocate the input device\n");
442 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(*keypad
), GFP_KERNEL
);
444 dev_err(&pdev
->dev
, "not enough memory for driver data\n");
448 keypad
->input_dev
= input_dev
;
450 keypad
->stable_count
= 0;
452 timer_setup(&keypad
->check_matrix_timer
,
453 imx_keypad_check_for_events
, 0);
455 keypad
->mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
456 if (IS_ERR(keypad
->mmio_base
))
457 return PTR_ERR(keypad
->mmio_base
);
459 keypad
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
460 if (IS_ERR(keypad
->clk
)) {
461 dev_err(&pdev
->dev
, "failed to get keypad clock\n");
462 return PTR_ERR(keypad
->clk
);
465 /* Init the Input device */
466 input_dev
->name
= pdev
->name
;
467 input_dev
->id
.bustype
= BUS_HOST
;
468 input_dev
->dev
.parent
= &pdev
->dev
;
469 input_dev
->open
= imx_keypad_open
;
470 input_dev
->close
= imx_keypad_close
;
472 error
= matrix_keypad_build_keymap(keymap_data
, NULL
,
475 keypad
->keycodes
, input_dev
);
477 dev_err(&pdev
->dev
, "failed to build keymap\n");
481 /* Search for rows and cols enabled */
482 for (row
= 0; row
< MAX_MATRIX_KEY_ROWS
; row
++) {
483 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
484 i
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
485 if (keypad
->keycodes
[i
] != KEY_RESERVED
) {
486 keypad
->rows_en_mask
|= 1 << row
;
487 keypad
->cols_en_mask
|= 1 << col
;
491 dev_dbg(&pdev
->dev
, "enabled rows mask: %x\n", keypad
->rows_en_mask
);
492 dev_dbg(&pdev
->dev
, "enabled cols mask: %x\n", keypad
->cols_en_mask
);
494 __set_bit(EV_REP
, input_dev
->evbit
);
495 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
496 input_set_drvdata(input_dev
, keypad
);
498 /* Ensure that the keypad will stay dormant until opened */
499 error
= clk_prepare_enable(keypad
->clk
);
502 imx_keypad_inhibit(keypad
);
503 clk_disable_unprepare(keypad
->clk
);
505 error
= devm_request_irq(&pdev
->dev
, irq
, imx_keypad_irq_handler
, 0,
508 dev_err(&pdev
->dev
, "failed to request IRQ\n");
512 /* Register the input device */
513 error
= input_register_device(input_dev
);
515 dev_err(&pdev
->dev
, "failed to register input device\n");
519 platform_set_drvdata(pdev
, keypad
);
520 device_init_wakeup(&pdev
->dev
, 1);
525 static int __maybe_unused
imx_kbd_noirq_suspend(struct device
*dev
)
527 struct platform_device
*pdev
= to_platform_device(dev
);
528 struct imx_keypad
*kbd
= platform_get_drvdata(pdev
);
529 struct input_dev
*input_dev
= kbd
->input_dev
;
530 unsigned short reg_val
= readw(kbd
->mmio_base
+ KPSR
);
532 /* imx kbd can wake up system even clock is disabled */
533 mutex_lock(&input_dev
->mutex
);
535 if (input_device_enabled(input_dev
))
536 clk_disable_unprepare(kbd
->clk
);
538 mutex_unlock(&input_dev
->mutex
);
540 if (device_may_wakeup(&pdev
->dev
)) {
541 if (reg_val
& KBD_STAT_KPKD
)
542 reg_val
|= KBD_STAT_KRIE
;
543 if (reg_val
& KBD_STAT_KPKR
)
544 reg_val
|= KBD_STAT_KDIE
;
545 writew(reg_val
, kbd
->mmio_base
+ KPSR
);
547 enable_irq_wake(kbd
->irq
);
553 static int __maybe_unused
imx_kbd_noirq_resume(struct device
*dev
)
555 struct platform_device
*pdev
= to_platform_device(dev
);
556 struct imx_keypad
*kbd
= platform_get_drvdata(pdev
);
557 struct input_dev
*input_dev
= kbd
->input_dev
;
560 if (device_may_wakeup(&pdev
->dev
))
561 disable_irq_wake(kbd
->irq
);
563 mutex_lock(&input_dev
->mutex
);
565 if (input_device_enabled(input_dev
)) {
566 ret
= clk_prepare_enable(kbd
->clk
);
572 mutex_unlock(&input_dev
->mutex
);
577 static const struct dev_pm_ops imx_kbd_pm_ops
= {
578 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend
, imx_kbd_noirq_resume
)
581 static struct platform_driver imx_keypad_driver
= {
583 .name
= "imx-keypad",
584 .pm
= &imx_kbd_pm_ops
,
585 .of_match_table
= of_match_ptr(imx_keypad_of_match
),
587 .probe
= imx_keypad_probe
,
589 module_platform_driver(imx_keypad_driver
);
591 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
592 MODULE_DESCRIPTION("IMX Keypad Port Driver");
593 MODULE_LICENSE("GPL v2");
594 MODULE_ALIAS("platform:imx-keypad");