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
45 struct 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. */
imx_keypad_scan_matrix(struct imx_keypad * keypad,unsigned short * matrix_volatile_state)79 static 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 */
imx_keypad_fire_events(struct imx_keypad * keypad,unsigned short * matrix_volatile_state)144 static 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 */
imx_keypad_check_for_events(struct timer_list * t)184 static void imx_keypad_check_for_events(struct timer_list *t)
185 {
186 struct imx_keypad *keypad = timer_container_of(keypad, t,
187 check_matrix_timer);
188 unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS];
189 unsigned short reg_val;
190 bool state_changed, is_zero_matrix;
191 int i;
192
193 memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
194
195 imx_keypad_scan_matrix(keypad, matrix_volatile_state);
196
197 state_changed = false;
198 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
199 if ((keypad->cols_en_mask & (1 << i)) == 0)
200 continue;
201
202 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
203 state_changed = true;
204 break;
205 }
206 }
207
208 /*
209 * If the matrix state is changed from the previous scan
210 * (Re)Begin the debouncing process, saving the new state in
211 * keypad->matrix_unstable_state.
212 * else
213 * Increase the count of number of scans with a stable state.
214 */
215 if (state_changed) {
216 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
217 sizeof(matrix_volatile_state));
218 keypad->stable_count = 0;
219 } else
220 keypad->stable_count++;
221
222 /*
223 * If the matrix is not as stable as we want reschedule scan
224 * in the near future.
225 */
226 if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
227 mod_timer(&keypad->check_matrix_timer,
228 jiffies + msecs_to_jiffies(10));
229 return;
230 }
231
232 /*
233 * If the matrix state is stable, fire the events and save the new
234 * stable state. Note, if the matrix is kept stable for longer
235 * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all
236 * events have already been generated.
237 */
238 if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
239 imx_keypad_fire_events(keypad, matrix_volatile_state);
240
241 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
242 sizeof(matrix_volatile_state));
243 }
244
245 is_zero_matrix = true;
246 for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
247 if (matrix_volatile_state[i] != 0) {
248 is_zero_matrix = false;
249 break;
250 }
251 }
252
253
254 if (is_zero_matrix) {
255 /*
256 * All keys have been released. Enable only the KDI
257 * interrupt for future key presses (clear the KDI
258 * status bit and its sync chain before that).
259 */
260 reg_val = readw(keypad->mmio_base + KPSR);
261 reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC;
262 writew(reg_val, keypad->mmio_base + KPSR);
263
264 reg_val = readw(keypad->mmio_base + KPSR);
265 reg_val |= KBD_STAT_KDIE;
266 reg_val &= ~KBD_STAT_KRIE;
267 writew(reg_val, keypad->mmio_base + KPSR);
268 } else {
269 /*
270 * Some keys are still pressed. Schedule a rescan in
271 * attempt to detect multiple key presses and enable
272 * the KRI interrupt to react quickly to key release
273 * event.
274 */
275 mod_timer(&keypad->check_matrix_timer,
276 jiffies + msecs_to_jiffies(60));
277
278 reg_val = readw(keypad->mmio_base + KPSR);
279 reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS;
280 writew(reg_val, keypad->mmio_base + KPSR);
281
282 reg_val = readw(keypad->mmio_base + KPSR);
283 reg_val |= KBD_STAT_KRIE;
284 reg_val &= ~KBD_STAT_KDIE;
285 writew(reg_val, keypad->mmio_base + KPSR);
286 }
287 }
288
imx_keypad_irq_handler(int irq,void * dev_id)289 static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
290 {
291 struct imx_keypad *keypad = dev_id;
292 unsigned short reg_val;
293
294 reg_val = readw(keypad->mmio_base + KPSR);
295
296 /* Disable both interrupt types */
297 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
298 /* Clear interrupts status bits */
299 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
300 writew(reg_val, keypad->mmio_base + KPSR);
301
302 if (keypad->enabled) {
303 /* The matrix is supposed to be changed */
304 keypad->stable_count = 0;
305
306 /* Schedule the scanning procedure near in the future */
307 mod_timer(&keypad->check_matrix_timer,
308 jiffies + msecs_to_jiffies(2));
309 }
310
311 return IRQ_HANDLED;
312 }
313
imx_keypad_config(struct imx_keypad * keypad)314 static void imx_keypad_config(struct imx_keypad *keypad)
315 {
316 unsigned short reg_val;
317
318 /*
319 * Include enabled rows in interrupt generation (KPCR[7:0])
320 * Configure keypad columns as open-drain (KPCR[15:8])
321 */
322 reg_val = readw(keypad->mmio_base + KPCR);
323 reg_val |= keypad->rows_en_mask & 0xff; /* rows */
324 reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */
325 writew(reg_val, keypad->mmio_base + KPCR);
326
327 /* Write 0's to KPDR[15:8] (Colums) */
328 reg_val = readw(keypad->mmio_base + KPDR);
329 reg_val &= 0x00ff;
330 writew(reg_val, keypad->mmio_base + KPDR);
331
332 /* Configure columns as output, rows as input (KDDR[15:0]) */
333 writew(0xff00, keypad->mmio_base + KDDR);
334
335 /*
336 * Clear Key Depress and Key Release status bit.
337 * Clear both synchronizer chain.
338 */
339 reg_val = readw(keypad->mmio_base + KPSR);
340 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD |
341 KBD_STAT_KDSC | KBD_STAT_KRSS;
342 writew(reg_val, keypad->mmio_base + KPSR);
343
344 /* Enable KDI and disable KRI (avoid false release events). */
345 reg_val |= KBD_STAT_KDIE;
346 reg_val &= ~KBD_STAT_KRIE;
347 writew(reg_val, keypad->mmio_base + KPSR);
348 }
349
imx_keypad_inhibit(struct imx_keypad * keypad)350 static void imx_keypad_inhibit(struct imx_keypad *keypad)
351 {
352 unsigned short reg_val;
353
354 /* Inhibit KDI and KRI interrupts. */
355 reg_val = readw(keypad->mmio_base + KPSR);
356 reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE);
357 reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD;
358 writew(reg_val, keypad->mmio_base + KPSR);
359
360 /* Colums as open drain and disable all rows */
361 reg_val = (keypad->cols_en_mask & 0xff) << 8;
362 writew(reg_val, keypad->mmio_base + KPCR);
363 }
364
imx_keypad_close(struct input_dev * dev)365 static void imx_keypad_close(struct input_dev *dev)
366 {
367 struct imx_keypad *keypad = input_get_drvdata(dev);
368
369 dev_dbg(&dev->dev, ">%s\n", __func__);
370
371 /* Mark keypad as being inactive */
372 keypad->enabled = false;
373 synchronize_irq(keypad->irq);
374 timer_delete_sync(&keypad->check_matrix_timer);
375
376 imx_keypad_inhibit(keypad);
377
378 /* Disable clock unit */
379 clk_disable_unprepare(keypad->clk);
380 }
381
imx_keypad_open(struct input_dev * dev)382 static int imx_keypad_open(struct input_dev *dev)
383 {
384 struct imx_keypad *keypad = input_get_drvdata(dev);
385 int error;
386
387 dev_dbg(&dev->dev, ">%s\n", __func__);
388
389 /* Enable the kpp clock */
390 error = clk_prepare_enable(keypad->clk);
391 if (error)
392 return error;
393
394 /* We became active from now */
395 keypad->enabled = true;
396
397 imx_keypad_config(keypad);
398
399 /* Sanity control, not all the rows must be actived now. */
400 if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
401 dev_err(&dev->dev,
402 "too many keys pressed, control pins initialisation\n");
403 goto open_err;
404 }
405
406 return 0;
407
408 open_err:
409 imx_keypad_close(dev);
410 return -EIO;
411 }
412
413 static const struct of_device_id imx_keypad_of_match[] = {
414 { .compatible = "fsl,imx21-kpp", },
415 { /* sentinel */ }
416 };
417 MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
418
imx_keypad_probe(struct platform_device * pdev)419 static int imx_keypad_probe(struct platform_device *pdev)
420 {
421 struct imx_keypad *keypad;
422 struct input_dev *input_dev;
423 int irq, error, i, row, col;
424
425 irq = platform_get_irq(pdev, 0);
426 if (irq < 0)
427 return irq;
428
429 input_dev = devm_input_allocate_device(&pdev->dev);
430 if (!input_dev) {
431 dev_err(&pdev->dev, "failed to allocate the input device\n");
432 return -ENOMEM;
433 }
434
435 keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL);
436 if (!keypad) {
437 dev_err(&pdev->dev, "not enough memory for driver data\n");
438 return -ENOMEM;
439 }
440
441 keypad->input_dev = input_dev;
442 keypad->irq = irq;
443 keypad->stable_count = 0;
444
445 timer_setup(&keypad->check_matrix_timer,
446 imx_keypad_check_for_events, 0);
447
448 keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0);
449 if (IS_ERR(keypad->mmio_base))
450 return PTR_ERR(keypad->mmio_base);
451
452 keypad->clk = devm_clk_get(&pdev->dev, NULL);
453 if (IS_ERR(keypad->clk)) {
454 dev_err(&pdev->dev, "failed to get keypad clock\n");
455 return PTR_ERR(keypad->clk);
456 }
457
458 /* Init the Input device */
459 input_dev->name = pdev->name;
460 input_dev->id.bustype = BUS_HOST;
461 input_dev->dev.parent = &pdev->dev;
462 input_dev->open = imx_keypad_open;
463 input_dev->close = imx_keypad_close;
464
465 error = matrix_keypad_build_keymap(NULL, NULL,
466 MAX_MATRIX_KEY_ROWS,
467 MAX_MATRIX_KEY_COLS,
468 keypad->keycodes, input_dev);
469 if (error) {
470 dev_err(&pdev->dev, "failed to build keymap\n");
471 return error;
472 }
473
474 /* Search for rows and cols enabled */
475 for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) {
476 for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
477 i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
478 if (keypad->keycodes[i] != KEY_RESERVED) {
479 keypad->rows_en_mask |= 1 << row;
480 keypad->cols_en_mask |= 1 << col;
481 }
482 }
483 }
484 dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask);
485 dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask);
486
487 __set_bit(EV_REP, input_dev->evbit);
488 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
489 input_set_drvdata(input_dev, keypad);
490
491 /* Ensure that the keypad will stay dormant until opened */
492 error = clk_prepare_enable(keypad->clk);
493 if (error)
494 return error;
495 imx_keypad_inhibit(keypad);
496 clk_disable_unprepare(keypad->clk);
497
498 error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
499 pdev->name, keypad);
500 if (error) {
501 dev_err(&pdev->dev, "failed to request IRQ\n");
502 return error;
503 }
504
505 /* Register the input device */
506 error = input_register_device(input_dev);
507 if (error) {
508 dev_err(&pdev->dev, "failed to register input device\n");
509 return error;
510 }
511
512 platform_set_drvdata(pdev, keypad);
513 device_init_wakeup(&pdev->dev, 1);
514
515 return 0;
516 }
517
imx_kbd_noirq_suspend(struct device * dev)518 static int __maybe_unused imx_kbd_noirq_suspend(struct device *dev)
519 {
520 struct platform_device *pdev = to_platform_device(dev);
521 struct imx_keypad *kbd = platform_get_drvdata(pdev);
522 struct input_dev *input_dev = kbd->input_dev;
523 unsigned short reg_val = readw(kbd->mmio_base + KPSR);
524
525 scoped_guard(mutex, &input_dev->mutex) {
526 /* imx kbd can wake up system even clock is disabled */
527 if (input_device_enabled(input_dev))
528 clk_disable_unprepare(kbd->clk);
529 }
530
531 if (device_may_wakeup(&pdev->dev)) {
532 if (reg_val & KBD_STAT_KPKD)
533 reg_val |= KBD_STAT_KRIE;
534 if (reg_val & KBD_STAT_KPKR)
535 reg_val |= KBD_STAT_KDIE;
536 writew(reg_val, kbd->mmio_base + KPSR);
537
538 enable_irq_wake(kbd->irq);
539 }
540
541 return 0;
542 }
543
imx_kbd_noirq_resume(struct device * dev)544 static int __maybe_unused imx_kbd_noirq_resume(struct device *dev)
545 {
546 struct platform_device *pdev = to_platform_device(dev);
547 struct imx_keypad *kbd = platform_get_drvdata(pdev);
548 struct input_dev *input_dev = kbd->input_dev;
549 int error;
550
551 if (device_may_wakeup(&pdev->dev))
552 disable_irq_wake(kbd->irq);
553
554 guard(mutex)(&input_dev->mutex);
555
556 if (input_device_enabled(input_dev)) {
557 error = clk_prepare_enable(kbd->clk);
558 if (error)
559 return error;
560 }
561
562 return 0;
563 }
564
565 static const struct dev_pm_ops imx_kbd_pm_ops = {
566 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx_kbd_noirq_suspend, imx_kbd_noirq_resume)
567 };
568
569 static struct platform_driver imx_keypad_driver = {
570 .driver = {
571 .name = "imx-keypad",
572 .pm = &imx_kbd_pm_ops,
573 .of_match_table = imx_keypad_of_match,
574 },
575 .probe = imx_keypad_probe,
576 };
577 module_platform_driver(imx_keypad_driver);
578
579 MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>");
580 MODULE_DESCRIPTION("IMX Keypad Port Driver");
581 MODULE_LICENSE("GPL v2");
582 MODULE_ALIAS("platform:imx-keypad");
583