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.h>
11 #include <linux/input/matrix_keypad.h>
12 #include <linux/interrupt.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
18 #include <linux/platform_device.h>
19 #include <linux/slab.h>
20 #include <linux/timer.h>
23 * Keypad Controller registers (halfword)
25 #define KPCR 0x00 /* Keypad Control Register */
27 #define KPSR 0x02 /* Keypad Status Register */
28 #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
29 #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
30 #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
31 #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
32 #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
33 #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
34 #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
36 #define KDDR 0x04 /* Keypad Data Direction Register */
37 #define KPDR 0x06 /* Keypad Data Register */
39 #define MAX_MATRIX_KEY_ROWS 8
40 #define MAX_MATRIX_KEY_COLS 8
41 #define MATRIX_ROW_SHIFT 3
43 #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
48 struct input_dev
*input_dev
;
49 void __iomem
*mmio_base
;
52 struct timer_list check_matrix_timer
;
55 * The matrix is stable only if no changes are detected after
56 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
58 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
63 /* Masks for enabled rows/cols */
64 unsigned short rows_en_mask
;
65 unsigned short cols_en_mask
;
67 unsigned short keycodes
[MAX_MATRIX_KEY_NUM
];
71 * -stable: achieved after a complete debounce process.
72 * -unstable: used in the debouncing process.
74 unsigned short matrix_stable_state
[MAX_MATRIX_KEY_COLS
];
75 unsigned short matrix_unstable_state
[MAX_MATRIX_KEY_COLS
];
78 /* Scan the matrix and return the new state in *matrix_volatile_state. */
79 static void imx_keypad_scan_matrix(struct imx_keypad
*keypad
,
80 unsigned short *matrix_volatile_state
)
83 unsigned short reg_val
;
85 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
86 if ((keypad
->cols_en_mask
& (1 << col
)) == 0)
89 * Discharge keypad capacitance:
90 * 2. write 1s on column data.
91 * 3. configure columns as totem-pole to discharge capacitance.
92 * 4. configure columns as open-drain.
94 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
96 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
98 reg_val
= readw(keypad
->mmio_base
+ KPCR
);
99 reg_val
&= ~((keypad
->cols_en_mask
& 0xff) << 8);
100 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
104 reg_val
= readw(keypad
->mmio_base
+ KPCR
);
105 reg_val
|= (keypad
->cols_en_mask
& 0xff) << 8;
106 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
109 * 5. Write a single column to 0, others to 1.
110 * 6. Sample row inputs and save data.
111 * 7. Repeat steps 2 - 6 for remaining columns.
113 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
114 reg_val
&= ~(1 << (8 + col
));
115 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
118 * Delay added to avoid propagating the 0 from column to row
124 * 1s in matrix_volatile_state[col] means key pressures
125 * throw data from non enabled rows.
127 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
128 matrix_volatile_state
[col
] = (~reg_val
) & keypad
->rows_en_mask
;
132 * Return in standby mode:
133 * 9. write 0s to columns
135 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
137 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
141 * Compare the new matrix state (volatile) with the stable one stored in
142 * keypad->matrix_stable_state and fire events if changes are detected.
144 static void imx_keypad_fire_events(struct imx_keypad
*keypad
,
145 unsigned short *matrix_volatile_state
)
147 struct input_dev
*input_dev
= keypad
->input_dev
;
150 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
151 unsigned short bits_changed
;
154 if ((keypad
->cols_en_mask
& (1 << col
)) == 0)
155 continue; /* Column is not enabled */
157 bits_changed
= keypad
->matrix_stable_state
[col
] ^
158 matrix_volatile_state
[col
];
160 if (bits_changed
== 0)
161 continue; /* Column does not contain changes */
163 for (row
= 0; row
< MAX_MATRIX_KEY_ROWS
; row
++) {
164 if ((keypad
->rows_en_mask
& (1 << row
)) == 0)
165 continue; /* Row is not enabled */
166 if ((bits_changed
& (1 << row
)) == 0)
167 continue; /* Row does not contain changes */
169 code
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
170 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
171 input_report_key(input_dev
, keypad
->keycodes
[code
],
172 matrix_volatile_state
[col
] & (1 << row
));
173 dev_dbg(&input_dev
->dev
, "Event code: %d, val: %d",
174 keypad
->keycodes
[code
],
175 matrix_volatile_state
[col
] & (1 << row
));
178 input_sync(input_dev
);
182 * imx_keypad_check_for_events is the timer handler.
184 static void imx_keypad_check_for_events(struct timer_list
*t
)
186 struct imx_keypad
*keypad
= from_timer(keypad
, t
, check_matrix_timer
);
187 unsigned short matrix_volatile_state
[MAX_MATRIX_KEY_COLS
];
188 unsigned short reg_val
;
189 bool state_changed
, is_zero_matrix
;
192 memset(matrix_volatile_state
, 0, sizeof(matrix_volatile_state
));
194 imx_keypad_scan_matrix(keypad
, matrix_volatile_state
);
196 state_changed
= false;
197 for (i
= 0; i
< MAX_MATRIX_KEY_COLS
; i
++) {
198 if ((keypad
->cols_en_mask
& (1 << i
)) == 0)
201 if (keypad
->matrix_unstable_state
[i
] ^ matrix_volatile_state
[i
]) {
202 state_changed
= true;
208 * If the matrix state is changed from the previous scan
209 * (Re)Begin the debouncing process, saving the new state in
210 * keypad->matrix_unstable_state.
212 * Increase the count of number of scans with a stable state.
215 memcpy(keypad
->matrix_unstable_state
, matrix_volatile_state
,
216 sizeof(matrix_volatile_state
));
217 keypad
->stable_count
= 0;
219 keypad
->stable_count
++;
222 * If the matrix is not as stable as we want reschedule scan
223 * in the near future.
225 if (keypad
->stable_count
< IMX_KEYPAD_SCANS_FOR_STABILITY
) {
226 mod_timer(&keypad
->check_matrix_timer
,
227 jiffies
+ msecs_to_jiffies(10));
232 * If the matrix state is stable, fire the events and save the new
233 * stable state. Note, if the matrix is kept stable for longer
234 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
235 * events have already been generated.
237 if (keypad
->stable_count
== IMX_KEYPAD_SCANS_FOR_STABILITY
) {
238 imx_keypad_fire_events(keypad
, matrix_volatile_state
);
240 memcpy(keypad
->matrix_stable_state
, matrix_volatile_state
,
241 sizeof(matrix_volatile_state
));
244 is_zero_matrix
= true;
245 for (i
= 0; i
< MAX_MATRIX_KEY_COLS
; i
++) {
246 if (matrix_volatile_state
[i
] != 0) {
247 is_zero_matrix
= false;
253 if (is_zero_matrix
) {
255 * All keys have been released. Enable only the KDI
256 * interrupt for future key presses (clear the KDI
257 * status bit and its sync chain before that).
259 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
260 reg_val
|= KBD_STAT_KPKD
| KBD_STAT_KDSC
;
261 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
263 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
264 reg_val
|= KBD_STAT_KDIE
;
265 reg_val
&= ~KBD_STAT_KRIE
;
266 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
269 * Some keys are still pressed. Schedule a rescan in
270 * attempt to detect multiple key presses and enable
271 * the KRI interrupt to react quickly to key release
274 mod_timer(&keypad
->check_matrix_timer
,
275 jiffies
+ msecs_to_jiffies(60));
277 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
278 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KRSS
;
279 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
281 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
282 reg_val
|= KBD_STAT_KRIE
;
283 reg_val
&= ~KBD_STAT_KDIE
;
284 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
288 static irqreturn_t
imx_keypad_irq_handler(int irq
, void *dev_id
)
290 struct imx_keypad
*keypad
= dev_id
;
291 unsigned short reg_val
;
293 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
295 /* Disable both interrupt types */
296 reg_val
&= ~(KBD_STAT_KRIE
| KBD_STAT_KDIE
);
297 /* Clear interrupts status bits */
298 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
;
299 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
301 if (keypad
->enabled
) {
302 /* The matrix is supposed to be changed */
303 keypad
->stable_count
= 0;
305 /* Schedule the scanning procedure near in the future */
306 mod_timer(&keypad
->check_matrix_timer
,
307 jiffies
+ msecs_to_jiffies(2));
313 static void imx_keypad_config(struct imx_keypad
*keypad
)
315 unsigned short reg_val
;
318 * Include enabled rows in interrupt generation (KPCR[7:0])
319 * Configure keypad columns as open-drain (KPCR[15:8])
321 reg_val
= readw(keypad
->mmio_base
+ KPCR
);
322 reg_val
|= keypad
->rows_en_mask
& 0xff; /* rows */
323 reg_val
|= (keypad
->cols_en_mask
& 0xff) << 8; /* cols */
324 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
326 /* Write 0's to KPDR[15:8] (Colums) */
327 reg_val
= readw(keypad
->mmio_base
+ KPDR
);
329 writew(reg_val
, keypad
->mmio_base
+ KPDR
);
331 /* Configure columns as output, rows as input (KDDR[15:0]) */
332 writew(0xff00, keypad
->mmio_base
+ KDDR
);
335 * Clear Key Depress and Key Release status bit.
336 * Clear both synchronizer chain.
338 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
339 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
|
340 KBD_STAT_KDSC
| KBD_STAT_KRSS
;
341 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
343 /* Enable KDI and disable KRI (avoid false release events). */
344 reg_val
|= KBD_STAT_KDIE
;
345 reg_val
&= ~KBD_STAT_KRIE
;
346 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
349 static void imx_keypad_inhibit(struct imx_keypad
*keypad
)
351 unsigned short reg_val
;
353 /* Inhibit KDI and KRI interrupts. */
354 reg_val
= readw(keypad
->mmio_base
+ KPSR
);
355 reg_val
&= ~(KBD_STAT_KRIE
| KBD_STAT_KDIE
);
356 reg_val
|= KBD_STAT_KPKR
| KBD_STAT_KPKD
;
357 writew(reg_val
, keypad
->mmio_base
+ KPSR
);
359 /* Colums as open drain and disable all rows */
360 reg_val
= (keypad
->cols_en_mask
& 0xff) << 8;
361 writew(reg_val
, keypad
->mmio_base
+ KPCR
);
364 static void imx_keypad_close(struct input_dev
*dev
)
366 struct imx_keypad
*keypad
= input_get_drvdata(dev
);
368 dev_dbg(&dev
->dev
, ">%s\n", __func__
);
370 /* Mark keypad as being inactive */
371 keypad
->enabled
= false;
372 synchronize_irq(keypad
->irq
);
373 del_timer_sync(&keypad
->check_matrix_timer
);
375 imx_keypad_inhibit(keypad
);
377 /* Disable clock unit */
378 clk_disable_unprepare(keypad
->clk
);
381 static int imx_keypad_open(struct input_dev
*dev
)
383 struct imx_keypad
*keypad
= input_get_drvdata(dev
);
386 dev_dbg(&dev
->dev
, ">%s\n", __func__
);
388 /* Enable the kpp clock */
389 error
= clk_prepare_enable(keypad
->clk
);
393 /* We became active from now */
394 keypad
->enabled
= true;
396 imx_keypad_config(keypad
);
398 /* Sanity control, not all the rows must be actived now. */
399 if ((readw(keypad
->mmio_base
+ KPDR
) & keypad
->rows_en_mask
) == 0) {
401 "too many keys pressed, control pins initialisation\n");
408 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
);
418 static int imx_keypad_probe(struct platform_device
*pdev
)
420 struct imx_keypad
*keypad
;
421 struct input_dev
*input_dev
;
422 int irq
, error
, i
, row
, col
;
424 irq
= platform_get_irq(pdev
, 0);
428 input_dev
= devm_input_allocate_device(&pdev
->dev
);
430 dev_err(&pdev
->dev
, "failed to allocate the input device\n");
434 keypad
= devm_kzalloc(&pdev
->dev
, sizeof(*keypad
), GFP_KERNEL
);
436 dev_err(&pdev
->dev
, "not enough memory for driver data\n");
440 keypad
->input_dev
= input_dev
;
442 keypad
->stable_count
= 0;
444 timer_setup(&keypad
->check_matrix_timer
,
445 imx_keypad_check_for_events
, 0);
447 keypad
->mmio_base
= devm_platform_ioremap_resource(pdev
, 0);
448 if (IS_ERR(keypad
->mmio_base
))
449 return PTR_ERR(keypad
->mmio_base
);
451 keypad
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
452 if (IS_ERR(keypad
->clk
)) {
453 dev_err(&pdev
->dev
, "failed to get keypad clock\n");
454 return PTR_ERR(keypad
->clk
);
457 /* Init the Input device */
458 input_dev
->name
= pdev
->name
;
459 input_dev
->id
.bustype
= BUS_HOST
;
460 input_dev
->dev
.parent
= &pdev
->dev
;
461 input_dev
->open
= imx_keypad_open
;
462 input_dev
->close
= imx_keypad_close
;
464 error
= matrix_keypad_build_keymap(NULL
, NULL
,
467 keypad
->keycodes
, input_dev
);
469 dev_err(&pdev
->dev
, "failed to build keymap\n");
473 /* Search for rows and cols enabled */
474 for (row
= 0; row
< MAX_MATRIX_KEY_ROWS
; row
++) {
475 for (col
= 0; col
< MAX_MATRIX_KEY_COLS
; col
++) {
476 i
= MATRIX_SCAN_CODE(row
, col
, MATRIX_ROW_SHIFT
);
477 if (keypad
->keycodes
[i
] != KEY_RESERVED
) {
478 keypad
->rows_en_mask
|= 1 << row
;
479 keypad
->cols_en_mask
|= 1 << col
;
483 dev_dbg(&pdev
->dev
, "enabled rows mask: %x\n", keypad
->rows_en_mask
);
484 dev_dbg(&pdev
->dev
, "enabled cols mask: %x\n", keypad
->cols_en_mask
);
486 __set_bit(EV_REP
, input_dev
->evbit
);
487 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
488 input_set_drvdata(input_dev
, keypad
);
490 /* Ensure that the keypad will stay dormant until opened */
491 error
= clk_prepare_enable(keypad
->clk
);
494 imx_keypad_inhibit(keypad
);
495 clk_disable_unprepare(keypad
->clk
);
497 error
= devm_request_irq(&pdev
->dev
, irq
, imx_keypad_irq_handler
, 0,
500 dev_err(&pdev
->dev
, "failed to request IRQ\n");
504 /* Register the input device */
505 error
= input_register_device(input_dev
);
507 dev_err(&pdev
->dev
, "failed to register input device\n");
511 platform_set_drvdata(pdev
, keypad
);
512 device_init_wakeup(&pdev
->dev
, 1);
517 static int __maybe_unused
imx_kbd_noirq_suspend(struct device
*dev
)
519 struct platform_device
*pdev
= to_platform_device(dev
);
520 struct imx_keypad
*kbd
= platform_get_drvdata(pdev
);
521 struct input_dev
*input_dev
= kbd
->input_dev
;
522 unsigned short reg_val
= readw(kbd
->mmio_base
+ KPSR
);
524 /* imx kbd can wake up system even clock is disabled */
525 mutex_lock(&input_dev
->mutex
);
527 if (input_device_enabled(input_dev
))
528 clk_disable_unprepare(kbd
->clk
);
530 mutex_unlock(&input_dev
->mutex
);
532 if (device_may_wakeup(&pdev
->dev
)) {
533 if (reg_val
& KBD_STAT_KPKD
)
534 reg_val
|= KBD_STAT_KRIE
;
535 if (reg_val
& KBD_STAT_KPKR
)
536 reg_val
|= KBD_STAT_KDIE
;
537 writew(reg_val
, kbd
->mmio_base
+ KPSR
);
539 enable_irq_wake(kbd
->irq
);
545 static int __maybe_unused
imx_kbd_noirq_resume(struct device
*dev
)
547 struct platform_device
*pdev
= to_platform_device(dev
);
548 struct imx_keypad
*kbd
= platform_get_drvdata(pdev
);
549 struct input_dev
*input_dev
= kbd
->input_dev
;
552 if (device_may_wakeup(&pdev
->dev
))
553 disable_irq_wake(kbd
->irq
);
555 mutex_lock(&input_dev
->mutex
);
557 if (input_device_enabled(input_dev
)) {
558 ret
= clk_prepare_enable(kbd
->clk
);
564 mutex_unlock(&input_dev
->mutex
);
569 static const struct dev_pm_ops imx_kbd_pm_ops
= {
570 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend
, imx_kbd_noirq_resume
)
573 static struct platform_driver imx_keypad_driver
= {
575 .name
= "imx-keypad",
576 .pm
= &imx_kbd_pm_ops
,
577 .of_match_table
= imx_keypad_of_match
,
579 .probe
= imx_keypad_probe
,
581 module_platform_driver(imx_keypad_driver
);
583 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
584 MODULE_DESCRIPTION("IMX Keypad Port Driver");
585 MODULE_LICENSE("GPL v2");
586 MODULE_ALIAS("platform:imx-keypad");