Loading...
1// SPDX-License-Identifier: GPL-2.0
2//
3// Driver for the IMX keypad port.
4// Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
5
6#include <linux/clk.h>
7#include <linux/delay.h>
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/input.h>
11#include <linux/input/matrix_keypad.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/jiffies.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/timer.h>
21
22/*
23 * Keypad Controller registers (halfword)
24 */
25#define KPCR 0x00 /* Keypad Control Register */
26
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 */
35
36#define KDDR 0x04 /* Keypad Data Direction Register */
37#define KPDR 0x06 /* Keypad Data Register */
38
39#define MAX_MATRIX_KEY_ROWS 8
40#define MAX_MATRIX_KEY_COLS 8
41#define MATRIX_ROW_SHIFT 3
42
43#define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
44
45struct imx_keypad {
46
47 struct clk *clk;
48 struct input_dev *input_dev;
49 void __iomem *mmio_base;
50
51 int irq;
52 struct timer_list check_matrix_timer;
53
54 /*
55 * The matrix is stable only if no changes are detected after
56 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
57 */
58#define IMX_KEYPAD_SCANS_FOR_STABILITY 3
59 int stable_count;
60
61 bool enabled;
62
63 /* Masks for enabled rows/cols */
64 unsigned short rows_en_mask;
65 unsigned short cols_en_mask;
66
67 unsigned short keycodes[MAX_MATRIX_KEY_NUM];
68
69 /*
70 * Matrix states:
71 * -stable: achieved after a complete debounce process.
72 * -unstable: used in the debouncing process.
73 */
74 unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS];
75 unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS];
76};
77
78/* Scan the matrix and return the new state in *matrix_volatile_state. */
79static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
80 unsigned short *matrix_volatile_state)
81{
82 int col;
83 unsigned short reg_val;
84
85 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
86 if ((keypad->cols_en_mask & (1 << col)) == 0)
87 continue;
88 /*
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.
93 */
94 reg_val = readw(keypad->mmio_base + KPDR);
95 reg_val |= 0xff00;
96 writew(reg_val, keypad->mmio_base + KPDR);
97
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);
101
102 udelay(2);
103
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);
107
108 /*
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.
112 */
113 reg_val = readw(keypad->mmio_base + KPDR);
114 reg_val &= ~(1 << (8 + col));
115 writew(reg_val, keypad->mmio_base + KPDR);
116
117 /*
118 * Delay added to avoid propagating the 0 from column to row
119 * when scanning.
120 */
121 udelay(5);
122
123 /*
124 * 1s in matrix_volatile_state[col] means key pressures
125 * throw data from non enabled rows.
126 */
127 reg_val = readw(keypad->mmio_base + KPDR);
128 matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
129 }
130
131 /*
132 * Return in standby mode:
133 * 9. write 0s to columns
134 */
135 reg_val = readw(keypad->mmio_base + KPDR);
136 reg_val &= 0x00ff;
137 writew(reg_val, keypad->mmio_base + KPDR);
138}
139
140/*
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.
143 */
144static void imx_keypad_fire_events(struct imx_keypad *keypad,
145 unsigned short *matrix_volatile_state)
146{
147 struct input_dev *input_dev = keypad->input_dev;
148 int row, col;
149
150 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
151 unsigned short bits_changed;
152 int code;
153
154 if ((keypad->cols_en_mask & (1 << col)) == 0)
155 continue; /* Column is not enabled */
156
157 bits_changed = keypad->matrix_stable_state[col] ^
158 matrix_volatile_state[col];
159
160 if (bits_changed == 0)
161 continue; /* Column does not contain changes */
162
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 */
168
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));
176 }
177 }
178 input_sync(input_dev);
179}
180
181/*
182 * imx_keypad_check_for_events is the timer handler.
183 */
184static void imx_keypad_check_for_events(struct timer_list *t)
185{
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;
190 int i;
191
192 memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
193
194 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
195
196 state_changed = false;
197 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
198 if ((keypad->cols_en_mask & (1 << i)) == 0)
199 continue;
200
201 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
202 state_changed = true;
203 break;
204 }
205 }
206
207 /*
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.
211 * else
212 * Increase the count of number of scans with a stable state.
213 */
214 if (state_changed) {
215 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
216 sizeof(matrix_volatile_state));
217 keypad->stable_count = 0;
218 } else
219 keypad->stable_count++;
220
221 /*
222 * If the matrix is not as stable as we want reschedule scan
223 * in the near future.
224 */
225 if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
226 mod_timer(&keypad->check_matrix_timer,
227 jiffies + msecs_to_jiffies(10));
228 return;
229 }
230
231 /*
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.
236 */
237 if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
238 imx_keypad_fire_events(keypad, matrix_volatile_state);
239
240 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
241 sizeof(matrix_volatile_state));
242 }
243
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;
248 break;
249 }
250 }
251
252
253 if (is_zero_matrix) {
254 /*
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).
258 */
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);
262
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);
267 } else {
268 /*
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
272 * event.
273 */
274 mod_timer(&keypad->check_matrix_timer,
275 jiffies + msecs_to_jiffies(60));
276
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);
280
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);
285 }
286}
287
288static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
289{
290 struct imx_keypad *keypad = dev_id;
291 unsigned short reg_val;
292
293 reg_val = readw(keypad->mmio_base + KPSR);
294
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);
300
301 if (keypad->enabled) {
302 /* The matrix is supposed to be changed */
303 keypad->stable_count = 0;
304
305 /* Schedule the scanning procedure near in the future */
306 mod_timer(&keypad->check_matrix_timer,
307 jiffies + msecs_to_jiffies(2));
308 }
309
310 return IRQ_HANDLED;
311}
312
313static void imx_keypad_config(struct imx_keypad *keypad)
314{
315 unsigned short reg_val;
316
317 /*
318 * Include enabled rows in interrupt generation (KPCR[7:0])
319 * Configure keypad columns as open-drain (KPCR[15:8])
320 */
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);
325
326 /* Write 0's to KPDR[15:8] (Colums) */
327 reg_val = readw(keypad->mmio_base + KPDR);
328 reg_val &= 0x00ff;
329 writew(reg_val, keypad->mmio_base + KPDR);
330
331 /* Configure columns as output, rows as input (KDDR[15:0]) */
332 writew(0xff00, keypad->mmio_base + KDDR);
333
334 /*
335 * Clear Key Depress and Key Release status bit.
336 * Clear both synchronizer chain.
337 */
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);
342
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);
347}
348
349static void imx_keypad_inhibit(struct imx_keypad *keypad)
350{
351 unsigned short reg_val;
352
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);
358
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);
362}
363
364static void imx_keypad_close(struct input_dev *dev)
365{
366 struct imx_keypad *keypad = input_get_drvdata(dev);
367
368 dev_dbg(&dev->dev, ">%s\n", __func__);
369
370 /* Mark keypad as being inactive */
371 keypad->enabled = false;
372 synchronize_irq(keypad->irq);
373 del_timer_sync(&keypad->check_matrix_timer);
374
375 imx_keypad_inhibit(keypad);
376
377 /* Disable clock unit */
378 clk_disable_unprepare(keypad->clk);
379}
380
381static int imx_keypad_open(struct input_dev *dev)
382{
383 struct imx_keypad *keypad = input_get_drvdata(dev);
384 int error;
385
386 dev_dbg(&dev->dev, ">%s\n", __func__);
387
388 /* Enable the kpp clock */
389 error = clk_prepare_enable(keypad->clk);
390 if (error)
391 return error;
392
393 /* We became active from now */
394 keypad->enabled = true;
395
396 imx_keypad_config(keypad);
397
398 /* Sanity control, not all the rows must be actived now. */
399 if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
400 dev_err(&dev->dev,
401 "too many keys pressed, control pins initialisation\n");
402 goto open_err;
403 }
404
405 return 0;
406
407open_err:
408 imx_keypad_close(dev);
409 return -EIO;
410}
411
412static const struct of_device_id imx_keypad_of_match[] = {
413 { .compatible = "fsl,imx21-kpp", },
414 { /* sentinel */ }
415};
416MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
417
418static int imx_keypad_probe(struct platform_device *pdev)
419{
420 struct imx_keypad *keypad;
421 struct input_dev *input_dev;
422 int irq, error, i, row, col;
423
424 irq = platform_get_irq(pdev, 0);
425 if (irq < 0)
426 return irq;
427
428 input_dev = devm_input_allocate_device(&pdev->dev);
429 if (!input_dev) {
430 dev_err(&pdev->dev, "failed to allocate the input device\n");
431 return -ENOMEM;
432 }
433
434 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
435 if (!keypad) {
436 dev_err(&pdev->dev, "not enough memory for driver data\n");
437 return -ENOMEM;
438 }
439
440 keypad->input_dev = input_dev;
441 keypad->irq = irq;
442 keypad->stable_count = 0;
443
444 timer_setup(&keypad->check_matrix_timer,
445 imx_keypad_check_for_events, 0);
446
447 keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0);
448 if (IS_ERR(keypad->mmio_base))
449 return PTR_ERR(keypad->mmio_base);
450
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);
455 }
456
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;
463
464 error = matrix_keypad_build_keymap(NULL, NULL,
465 MAX_MATRIX_KEY_ROWS,
466 MAX_MATRIX_KEY_COLS,
467 keypad->keycodes, input_dev);
468 if (error) {
469 dev_err(&pdev->dev, "failed to build keymap\n");
470 return error;
471 }
472
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;
480 }
481 }
482 }
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);
485
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);
489
490 /* Ensure that the keypad will stay dormant until opened */
491 error = clk_prepare_enable(keypad->clk);
492 if (error)
493 return error;
494 imx_keypad_inhibit(keypad);
495 clk_disable_unprepare(keypad->clk);
496
497 error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
498 pdev->name, keypad);
499 if (error) {
500 dev_err(&pdev->dev, "failed to request IRQ\n");
501 return error;
502 }
503
504 /* Register the input device */
505 error = input_register_device(input_dev);
506 if (error) {
507 dev_err(&pdev->dev, "failed to register input device\n");
508 return error;
509 }
510
511 platform_set_drvdata(pdev, keypad);
512 device_init_wakeup(&pdev->dev, 1);
513
514 return 0;
515}
516
517static int __maybe_unused imx_kbd_noirq_suspend(struct device *dev)
518{
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);
523
524 /* imx kbd can wake up system even clock is disabled */
525 mutex_lock(&input_dev->mutex);
526
527 if (input_device_enabled(input_dev))
528 clk_disable_unprepare(kbd->clk);
529
530 mutex_unlock(&input_dev->mutex);
531
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);
538
539 enable_irq_wake(kbd->irq);
540 }
541
542 return 0;
543}
544
545static int __maybe_unused imx_kbd_noirq_resume(struct device *dev)
546{
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;
550 int ret = 0;
551
552 if (device_may_wakeup(&pdev->dev))
553 disable_irq_wake(kbd->irq);
554
555 mutex_lock(&input_dev->mutex);
556
557 if (input_device_enabled(input_dev)) {
558 ret = clk_prepare_enable(kbd->clk);
559 if (ret)
560 goto err_clk;
561 }
562
563err_clk:
564 mutex_unlock(&input_dev->mutex);
565
566 return ret;
567}
568
569static const struct dev_pm_ops imx_kbd_pm_ops = {
570 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend, imx_kbd_noirq_resume)
571};
572
573static struct platform_driver imx_keypad_driver = {
574 .driver = {
575 .name = "imx-keypad",
576 .pm = &imx_kbd_pm_ops,
577 .of_match_table = imx_keypad_of_match,
578 },
579 .probe = imx_keypad_probe,
580};
581module_platform_driver(imx_keypad_driver);
582
583MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
584MODULE_DESCRIPTION("IMX Keypad Port Driver");
585MODULE_LICENSE("GPL v2");
586MODULE_ALIAS("platform:imx-keypad");
1/*
2 * Driver for the IMX keypad port.
3 * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
4 *
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.
8 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/input/matrix_keypad.h>
15#include <linux/interrupt.h>
16#include <linux/io.h>
17#include <linux/jiffies.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/of.h>
21#include <linux/platform_device.h>
22#include <linux/slab.h>
23#include <linux/timer.h>
24
25/*
26 * Keypad Controller registers (halfword)
27 */
28#define KPCR 0x00 /* Keypad Control Register */
29
30#define KPSR 0x02 /* Keypad Status Register */
31#define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */
32#define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */
33#define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/
34#define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/
35#define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */
36#define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */
37#define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */
38
39#define KDDR 0x04 /* Keypad Data Direction Register */
40#define KPDR 0x06 /* Keypad Data Register */
41
42#define MAX_MATRIX_KEY_ROWS 8
43#define MAX_MATRIX_KEY_COLS 8
44#define MATRIX_ROW_SHIFT 3
45
46#define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
47
48struct imx_keypad {
49
50 struct clk *clk;
51 struct input_dev *input_dev;
52 void __iomem *mmio_base;
53
54 int irq;
55 struct timer_list check_matrix_timer;
56
57 /*
58 * The matrix is stable only if no changes are detected after
59 * IMX_KEYPAD_SCANS_FOR_STABILITY scans
60 */
61#define IMX_KEYPAD_SCANS_FOR_STABILITY 3
62 int stable_count;
63
64 bool enabled;
65
66 /* Masks for enabled rows/cols */
67 unsigned short rows_en_mask;
68 unsigned short cols_en_mask;
69
70 unsigned short keycodes[MAX_MATRIX_KEY_NUM];
71
72 /*
73 * Matrix states:
74 * -stable: achieved after a complete debounce process.
75 * -unstable: used in the debouncing process.
76 */
77 unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS];
78 unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS];
79};
80
81/* Scan the matrix and return the new state in *matrix_volatile_state. */
82static void imx_keypad_scan_matrix(struct imx_keypad *keypad,
83 unsigned short *matrix_volatile_state)
84{
85 int col;
86 unsigned short reg_val;
87
88 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
89 if ((keypad->cols_en_mask & (1 << col)) == 0)
90 continue;
91 /*
92 * Discharge keypad capacitance:
93 * 2. write 1s on column data.
94 * 3. configure columns as totem-pole to discharge capacitance.
95 * 4. configure columns as open-drain.
96 */
97 reg_val = readw(keypad->mmio_base + KPDR);
98 reg_val |= 0xff00;
99 writew(reg_val, keypad->mmio_base + KPDR);
100
101 reg_val = readw(keypad->mmio_base + KPCR);
102 reg_val &= ~((keypad->cols_en_mask & 0xff) << 8);
103 writew(reg_val, keypad->mmio_base + KPCR);
104
105 udelay(2);
106
107 reg_val = readw(keypad->mmio_base + KPCR);
108 reg_val |= (keypad->cols_en_mask & 0xff) << 8;
109 writew(reg_val, keypad->mmio_base + KPCR);
110
111 /*
112 * 5. Write a single column to 0, others to 1.
113 * 6. Sample row inputs and save data.
114 * 7. Repeat steps 2 - 6 for remaining columns.
115 */
116 reg_val = readw(keypad->mmio_base + KPDR);
117 reg_val &= ~(1 << (8 + col));
118 writew(reg_val, keypad->mmio_base + KPDR);
119
120 /*
121 * Delay added to avoid propagating the 0 from column to row
122 * when scanning.
123 */
124 udelay(5);
125
126 /*
127 * 1s in matrix_volatile_state[col] means key pressures
128 * throw data from non enabled rows.
129 */
130 reg_val = readw(keypad->mmio_base + KPDR);
131 matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
132 }
133
134 /*
135 * Return in standby mode:
136 * 9. write 0s to columns
137 */
138 reg_val = readw(keypad->mmio_base + KPDR);
139 reg_val &= 0x00ff;
140 writew(reg_val, keypad->mmio_base + KPDR);
141}
142
143/*
144 * Compare the new matrix state (volatile) with the stable one stored in
145 * keypad->matrix_stable_state and fire events if changes are detected.
146 */
147static void imx_keypad_fire_events(struct imx_keypad *keypad,
148 unsigned short *matrix_volatile_state)
149{
150 struct input_dev *input_dev = keypad->input_dev;
151 int row, col;
152
153 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
154 unsigned short bits_changed;
155 int code;
156
157 if ((keypad->cols_en_mask & (1 << col)) == 0)
158 continue; /* Column is not enabled */
159
160 bits_changed = keypad->matrix_stable_state[col] ^
161 matrix_volatile_state[col];
162
163 if (bits_changed == 0)
164 continue; /* Column does not contain changes */
165
166 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
167 if ((keypad->rows_en_mask & (1 << row)) == 0)
168 continue; /* Row is not enabled */
169 if ((bits_changed & (1 << row)) == 0)
170 continue; /* Row does not contain changes */
171
172 code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
173 input_event(input_dev, EV_MSC, MSC_SCAN, code);
174 input_report_key(input_dev, keypad->keycodes[code],
175 matrix_volatile_state[col] & (1 << row));
176 dev_dbg(&input_dev->dev, "Event code: %d, val: %d",
177 keypad->keycodes[code],
178 matrix_volatile_state[col] & (1 << row));
179 }
180 }
181 input_sync(input_dev);
182}
183
184/*
185 * imx_keypad_check_for_events is the timer handler.
186 */
187static void imx_keypad_check_for_events(struct timer_list *t)
188{
189 struct imx_keypad *keypad = from_timer(keypad, t, check_matrix_timer);
190 unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
191 unsigned short reg_val;
192 bool state_changed, is_zero_matrix;
193 int i;
194
195 memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
196
197 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
198
199 state_changed = false;
200 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
201 if ((keypad->cols_en_mask & (1 << i)) == 0)
202 continue;
203
204 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
205 state_changed = true;
206 break;
207 }
208 }
209
210 /*
211 * If the matrix state is changed from the previous scan
212 * (Re)Begin the debouncing process, saving the new state in
213 * keypad->matrix_unstable_state.
214 * else
215 * Increase the count of number of scans with a stable state.
216 */
217 if (state_changed) {
218 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
219 sizeof(matrix_volatile_state));
220 keypad->stable_count = 0;
221 } else
222 keypad->stable_count++;
223
224 /*
225 * If the matrix is not as stable as we want reschedule scan
226 * in the near future.
227 */
228 if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
229 mod_timer(&keypad->check_matrix_timer,
230 jiffies + msecs_to_jiffies(10));
231 return;
232 }
233
234 /*
235 * If the matrix state is stable, fire the events and save the new
236 * stable state. Note, if the matrix is kept stable for longer
237 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
238 * events have already been generated.
239 */
240 if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
241 imx_keypad_fire_events(keypad, matrix_volatile_state);
242
243 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
244 sizeof(matrix_volatile_state));
245 }
246
247 is_zero_matrix = true;
248 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
249 if (matrix_volatile_state[i] != 0) {
250 is_zero_matrix = false;
251 break;
252 }
253 }
254
255
256 if (is_zero_matrix) {
257 /*
258 * All keys have been released. Enable only the KDI
259 * interrupt for future key presses (clear the KDI
260 * status bit and its sync chain before that).
261 */
262 reg_val = readw(keypad->mmio_base + KPSR);
263 reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
264 writew(reg_val, keypad->mmio_base + KPSR);
265
266 reg_val = readw(keypad->mmio_base + KPSR);
267 reg_val |= KBD_STAT_KDIE;
268 reg_val &= ~KBD_STAT_KRIE;
269 writew(reg_val, keypad->mmio_base + KPSR);
270 } else {
271 /*
272 * Some keys are still pressed. Schedule a rescan in
273 * attempt to detect multiple key presses and enable
274 * the KRI interrupt to react quickly to key release
275 * event.
276 */
277 mod_timer(&keypad->check_matrix_timer,
278 jiffies + msecs_to_jiffies(60));
279
280 reg_val = readw(keypad->mmio_base + KPSR);
281 reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
282 writew(reg_val, keypad->mmio_base + KPSR);
283
284 reg_val = readw(keypad->mmio_base + KPSR);
285 reg_val |= KBD_STAT_KRIE;
286 reg_val &= ~KBD_STAT_KDIE;
287 writew(reg_val, keypad->mmio_base + KPSR);
288 }
289}
290
291static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
292{
293 struct imx_keypad *keypad = dev_id;
294 unsigned short reg_val;
295
296 reg_val = readw(keypad->mmio_base + KPSR);
297
298 /* Disable both interrupt types */
299 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
300 /* Clear interrupts status bits */
301 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
302 writew(reg_val, keypad->mmio_base + KPSR);
303
304 if (keypad->enabled) {
305 /* The matrix is supposed to be changed */
306 keypad->stable_count = 0;
307
308 /* Schedule the scanning procedure near in the future */
309 mod_timer(&keypad->check_matrix_timer,
310 jiffies + msecs_to_jiffies(2));
311 }
312
313 return IRQ_HANDLED;
314}
315
316static void imx_keypad_config(struct imx_keypad *keypad)
317{
318 unsigned short reg_val;
319
320 /*
321 * Include enabled rows in interrupt generation (KPCR[7:0])
322 * Configure keypad columns as open-drain (KPCR[15:8])
323 */
324 reg_val = readw(keypad->mmio_base + KPCR);
325 reg_val |= keypad->rows_en_mask & 0xff; /* rows */
326 reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
327 writew(reg_val, keypad->mmio_base + KPCR);
328
329 /* Write 0's to KPDR[15:8] (Colums) */
330 reg_val = readw(keypad->mmio_base + KPDR);
331 reg_val &= 0x00ff;
332 writew(reg_val, keypad->mmio_base + KPDR);
333
334 /* Configure columns as output, rows as input (KDDR[15:0]) */
335 writew(0xff00, keypad->mmio_base + KDDR);
336
337 /*
338 * Clear Key Depress and Key Release status bit.
339 * Clear both synchronizer chain.
340 */
341 reg_val = readw(keypad->mmio_base + KPSR);
342 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
343 KBD_STAT_KDSC | KBD_STAT_KRSS;
344 writew(reg_val, keypad->mmio_base + KPSR);
345
346 /* Enable KDI and disable KRI (avoid false release events). */
347 reg_val |= KBD_STAT_KDIE;
348 reg_val &= ~KBD_STAT_KRIE;
349 writew(reg_val, keypad->mmio_base + KPSR);
350}
351
352static void imx_keypad_inhibit(struct imx_keypad *keypad)
353{
354 unsigned short reg_val;
355
356 /* Inhibit KDI and KRI interrupts. */
357 reg_val = readw(keypad->mmio_base + KPSR);
358 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
359 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
360 writew(reg_val, keypad->mmio_base + KPSR);
361
362 /* Colums as open drain and disable all rows */
363 reg_val = (keypad->cols_en_mask & 0xff) << 8;
364 writew(reg_val, keypad->mmio_base + KPCR);
365}
366
367static void imx_keypad_close(struct input_dev *dev)
368{
369 struct imx_keypad *keypad = input_get_drvdata(dev);
370
371 dev_dbg(&dev->dev, ">%s\n", __func__);
372
373 /* Mark keypad as being inactive */
374 keypad->enabled = false;
375 synchronize_irq(keypad->irq);
376 del_timer_sync(&keypad->check_matrix_timer);
377
378 imx_keypad_inhibit(keypad);
379
380 /* Disable clock unit */
381 clk_disable_unprepare(keypad->clk);
382}
383
384static int imx_keypad_open(struct input_dev *dev)
385{
386 struct imx_keypad *keypad = input_get_drvdata(dev);
387 int error;
388
389 dev_dbg(&dev->dev, ">%s\n", __func__);
390
391 /* Enable the kpp clock */
392 error = clk_prepare_enable(keypad->clk);
393 if (error)
394 return error;
395
396 /* We became active from now */
397 keypad->enabled = true;
398
399 imx_keypad_config(keypad);
400
401 /* Sanity control, not all the rows must be actived now. */
402 if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
403 dev_err(&dev->dev,
404 "too many keys pressed, control pins initialisation\n");
405 goto open_err;
406 }
407
408 return 0;
409
410open_err:
411 imx_keypad_close(dev);
412 return -EIO;
413}
414
415#ifdef CONFIG_OF
416static const struct of_device_id imx_keypad_of_match[] = {
417 { .compatible = "fsl,imx21-kpp", },
418 { /* sentinel */ }
419};
420MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
421#endif
422
423static int imx_keypad_probe(struct platform_device *pdev)
424{
425 const struct matrix_keymap_data *keymap_data =
426 dev_get_platdata(&pdev->dev);
427 struct imx_keypad *keypad;
428 struct input_dev *input_dev;
429 struct resource *res;
430 int irq, error, i, row, col;
431
432 if (!keymap_data && !pdev->dev.of_node) {
433 dev_err(&pdev->dev, "no keymap defined\n");
434 return -EINVAL;
435 }
436
437 irq = platform_get_irq(pdev, 0);
438 if (irq < 0) {
439 dev_err(&pdev->dev, "no irq defined in platform data\n");
440 return irq;
441 }
442
443 input_dev = devm_input_allocate_device(&pdev->dev);
444 if (!input_dev) {
445 dev_err(&pdev->dev, "failed to allocate the input device\n");
446 return -ENOMEM;
447 }
448
449 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
450 if (!keypad) {
451 dev_err(&pdev->dev, "not enough memory for driver data\n");
452 return -ENOMEM;
453 }
454
455 keypad->input_dev = input_dev;
456 keypad->irq = irq;
457 keypad->stable_count = 0;
458
459 timer_setup(&keypad->check_matrix_timer,
460 imx_keypad_check_for_events, 0);
461
462 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
463 keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
464 if (IS_ERR(keypad->mmio_base))
465 return PTR_ERR(keypad->mmio_base);
466
467 keypad->clk = devm_clk_get(&pdev->dev, NULL);
468 if (IS_ERR(keypad->clk)) {
469 dev_err(&pdev->dev, "failed to get keypad clock\n");
470 return PTR_ERR(keypad->clk);
471 }
472
473 /* Init the Input device */
474 input_dev->name = pdev->name;
475 input_dev->id.bustype = BUS_HOST;
476 input_dev->dev.parent = &pdev->dev;
477 input_dev->open = imx_keypad_open;
478 input_dev->close = imx_keypad_close;
479
480 error = matrix_keypad_build_keymap(keymap_data, NULL,
481 MAX_MATRIX_KEY_ROWS,
482 MAX_MATRIX_KEY_COLS,
483 keypad->keycodes, input_dev);
484 if (error) {
485 dev_err(&pdev->dev, "failed to build keymap\n");
486 return error;
487 }
488
489 /* Search for rows and cols enabled */
490 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
491 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
492 i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
493 if (keypad->keycodes[i] != KEY_RESERVED) {
494 keypad->rows_en_mask |= 1 << row;
495 keypad->cols_en_mask |= 1 << col;
496 }
497 }
498 }
499 dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
500 dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
501
502 __set_bit(EV_REP, input_dev->evbit);
503 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
504 input_set_drvdata(input_dev, keypad);
505
506 /* Ensure that the keypad will stay dormant until opened */
507 error = clk_prepare_enable(keypad->clk);
508 if (error)
509 return error;
510 imx_keypad_inhibit(keypad);
511 clk_disable_unprepare(keypad->clk);
512
513 error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
514 pdev->name, keypad);
515 if (error) {
516 dev_err(&pdev->dev, "failed to request IRQ\n");
517 return error;
518 }
519
520 /* Register the input device */
521 error = input_register_device(input_dev);
522 if (error) {
523 dev_err(&pdev->dev, "failed to register input device\n");
524 return error;
525 }
526
527 platform_set_drvdata(pdev, keypad);
528 device_init_wakeup(&pdev->dev, 1);
529
530 return 0;
531}
532
533static int __maybe_unused imx_kbd_suspend(struct device *dev)
534{
535 struct platform_device *pdev = to_platform_device(dev);
536 struct imx_keypad *kbd = platform_get_drvdata(pdev);
537 struct input_dev *input_dev = kbd->input_dev;
538
539 /* imx kbd can wake up system even clock is disabled */
540 mutex_lock(&input_dev->mutex);
541
542 if (input_dev->users)
543 clk_disable_unprepare(kbd->clk);
544
545 mutex_unlock(&input_dev->mutex);
546
547 if (device_may_wakeup(&pdev->dev))
548 enable_irq_wake(kbd->irq);
549
550 return 0;
551}
552
553static int __maybe_unused imx_kbd_resume(struct device *dev)
554{
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;
558 int ret = 0;
559
560 if (device_may_wakeup(&pdev->dev))
561 disable_irq_wake(kbd->irq);
562
563 mutex_lock(&input_dev->mutex);
564
565 if (input_dev->users) {
566 ret = clk_prepare_enable(kbd->clk);
567 if (ret)
568 goto err_clk;
569 }
570
571err_clk:
572 mutex_unlock(&input_dev->mutex);
573
574 return ret;
575}
576
577static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume);
578
579static struct platform_driver imx_keypad_driver = {
580 .driver = {
581 .name = "imx-keypad",
582 .pm = &imx_kbd_pm_ops,
583 .of_match_table = of_match_ptr(imx_keypad_of_match),
584 },
585 .probe = imx_keypad_probe,
586};
587module_platform_driver(imx_keypad_driver);
588
589MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
590MODULE_DESCRIPTION("IMX Keypad Port Driver");
591MODULE_LICENSE("GPL v2");
592MODULE_ALIAS("platform:imx-keypad");