]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/input/keyboard/imx_keypad.c
ENGR00283508-3 keyboard: imx: support pin sleep mode in suspend
[karo-tx-linux.git] / drivers / input / keyboard / imx_keypad.c
1 /*
2  * Driver for the IMX keypad port.
3  * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
4  * Copyright (C) 2013 Freescale Semiconductor, Inc.
5  *
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.
9  *
10  * <<Power management needs to be implemented>>.
11  */
12
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>
20 #include <linux/io.h>
21 #include <linux/jiffies.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/timer.h>
28
29 /*
30  * Keypad Controller registers (halfword)
31  */
32 #define KPCR            0x00 /* Keypad Control Register */
33
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 */
42
43 #define KDDR            0x04 /* Keypad Data Direction Register */
44 #define KPDR            0x06 /* Keypad Data Register */
45
46 #define MAX_MATRIX_KEY_ROWS     8
47 #define MAX_MATRIX_KEY_COLS     8
48 #define MATRIX_ROW_SHIFT        3
49
50 #define MAX_MATRIX_KEY_NUM      (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
51
52 struct imx_keypad {
53
54         struct clk *clk;
55         struct input_dev *input_dev;
56         void __iomem *mmio_base;
57
58         int                     irq;
59         struct timer_list       check_matrix_timer;
60
61         /*
62          * The matrix is stable only if no changes are detected after
63          * IMX_KEYPAD_SCANS_FOR_STABILITY scans
64          */
65 #define IMX_KEYPAD_SCANS_FOR_STABILITY 3
66         int                     stable_count;
67
68         bool                    enabled;
69
70         /* Masks for enabled rows/cols */
71         unsigned short          rows_en_mask;
72         unsigned short          cols_en_mask;
73
74         unsigned short          keycodes[MAX_MATRIX_KEY_NUM];
75
76         /*
77          * Matrix states:
78          * -stable: achieved after a complete debounce process.
79          * -unstable: used in the debouncing process.
80          */
81         unsigned short          matrix_stable_state[MAX_MATRIX_KEY_COLS];
82         unsigned short          matrix_unstable_state[MAX_MATRIX_KEY_COLS];
83 };
84
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)
88 {
89         int col;
90         unsigned short reg_val;
91
92         for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
93                 if ((keypad->cols_en_mask & (1 << col)) == 0)
94                         continue;
95                 /*
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.
100                  */
101                 reg_val = readw(keypad->mmio_base + KPDR);
102                 reg_val |= 0xff00;
103                 writew(reg_val, keypad->mmio_base + KPDR);
104
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);
108
109                 udelay(2);
110
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);
114
115                 /*
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.
119                  */
120                 reg_val = readw(keypad->mmio_base + KPDR);
121                 reg_val &= ~(1 << (8 + col));
122                 writew(reg_val, keypad->mmio_base + KPDR);
123
124                 /*
125                  * Delay added to avoid propagating the 0 from column to row
126                  * when scanning.
127                  */
128                 udelay(5);
129
130                 /*
131                  * 1s in matrix_volatile_state[col] means key pressures
132                  * throw data from non enabled rows.
133                  */
134                 reg_val = readw(keypad->mmio_base + KPDR);
135                 matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask;
136         }
137
138         /*
139          * Return in standby mode:
140          * 9. write 0s to columns
141          */
142         reg_val = readw(keypad->mmio_base + KPDR);
143         reg_val &= 0x00ff;
144         writew(reg_val, keypad->mmio_base + KPDR);
145 }
146
147 /*
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.
150  */
151 static void imx_keypad_fire_events(struct imx_keypad *keypad,
152                                    unsigned short *matrix_volatile_state)
153 {
154         struct input_dev *input_dev = keypad->input_dev;
155         int row, col;
156
157         for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) {
158                 unsigned short bits_changed;
159                 int code;
160
161                 if ((keypad->cols_en_mask & (1 << col)) == 0)
162                         continue; /* Column is not enabled */
163
164                 bits_changed = keypad->matrix_stable_state[col] ^
165                                                 matrix_volatile_state[col];
166
167                 if (bits_changed == 0)
168                         continue; /* Column does not contain changes */
169
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 */
175
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));
183                 }
184         }
185         input_sync(input_dev);
186 }
187
188 /*
189  * imx_keypad_check_for_events is the timer handler.
190  */
191 static void imx_keypad_check_for_events(unsigned long data)
192 {
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;
197         int i;
198
199         memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state));
200
201         imx_keypad_scan_matrix(keypad, matrix_volatile_state);
202
203         state_changed = false;
204         for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) {
205                 if ((keypad->cols_en_mask & (1 << i)) == 0)
206                         continue;
207
208                 if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) {
209                         state_changed = true;
210                         break;
211                 }
212         }
213
214         /*
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.
218          * else
219          *   Increase the count of number of scans with a stable state.
220          */
221         if (state_changed) {
222                 memcpy(keypad->matrix_unstable_state, matrix_volatile_state,
223                         sizeof(matrix_volatile_state));
224                 keypad->stable_count = 0;
225         } else
226                 keypad->stable_count++;
227
228         /*
229          * If the matrix is not as stable as we want reschedule scan
230          * in the near future.
231          */
232         if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) {
233                 mod_timer(&keypad->check_matrix_timer,
234                           jiffies + msecs_to_jiffies(10));
235                 return;
236         }
237
238         /*
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.
243          */
244         if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) {
245                 imx_keypad_fire_events(keypad, matrix_volatile_state);
246
247                 memcpy(keypad->matrix_stable_state, matrix_volatile_state,
248                         sizeof(matrix_volatile_state));
249         }
250
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;
255                         break;
256                 }
257         }
258
259
260         if (is_zero_matrix) {
261                 /*
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).
265                  */
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);
269
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);
274         } else {
275                 /*
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
279                  * event.
280                  */
281                 mod_timer(&keypad->check_matrix_timer,
282                           jiffies + msecs_to_jiffies(60));
283
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);
287
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);
292         }
293 }
294
295 static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id)
296 {
297         struct imx_keypad *keypad = dev_id;
298         unsigned short reg_val;
299
300         reg_val = readw(keypad->mmio_base + KPSR);
301
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);
307
308         if (keypad->enabled) {
309                 /* The matrix is supposed to be changed */
310                 keypad->stable_count = 0;
311
312                 /* Schedule the scanning procedure near in the future */
313                 mod_timer(&keypad->check_matrix_timer,
314                           jiffies + msecs_to_jiffies(2));
315         }
316
317         return IRQ_HANDLED;
318 }
319
320 static void imx_keypad_config(struct imx_keypad *keypad)
321 {
322         unsigned short reg_val;
323
324         /*
325          * Include enabled rows in interrupt generation (KPCR[7:0])
326          * Configure keypad columns as open-drain (KPCR[15:8])
327          */
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);
332
333         /* Write 0's to KPDR[15:8] (Colums) */
334         reg_val = readw(keypad->mmio_base + KPDR);
335         reg_val &= 0x00ff;
336         writew(reg_val, keypad->mmio_base + KPDR);
337
338         /* Configure columns as output, rows as input (KDDR[15:0]) */
339         writew(0xff00, keypad->mmio_base + KDDR);
340
341         /*
342          * Clear Key Depress and Key Release status bit.
343          * Clear both synchronizer chain.
344          */
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);
349
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);
354 }
355
356 static void imx_keypad_inhibit(struct imx_keypad *keypad)
357 {
358         unsigned short reg_val;
359
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);
365
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);
369 }
370
371 static void imx_keypad_close(struct input_dev *dev)
372 {
373         struct imx_keypad *keypad = input_get_drvdata(dev);
374
375         dev_dbg(&dev->dev, ">%s\n", __func__);
376
377         /* Mark keypad as being inactive */
378         keypad->enabled = false;
379         synchronize_irq(keypad->irq);
380         del_timer_sync(&keypad->check_matrix_timer);
381
382         imx_keypad_inhibit(keypad);
383
384         /* Disable clock unit */
385         clk_disable_unprepare(keypad->clk);
386 }
387
388 static int imx_keypad_open(struct input_dev *dev)
389 {
390         struct imx_keypad *keypad = input_get_drvdata(dev);
391         int error;
392
393         dev_dbg(&dev->dev, ">%s\n", __func__);
394
395         /* Enable the kpp clock */
396         error = clk_prepare_enable(keypad->clk);
397         if (error)
398                 return error;
399
400         /* We became active from now */
401         keypad->enabled = true;
402
403         imx_keypad_config(keypad);
404
405         /* Sanity control, not all the rows must be actived now. */
406         if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) {
407                 dev_err(&dev->dev,
408                         "too many keys pressed, control pins initialisation\n");
409                 goto open_err;
410         }
411
412         return 0;
413
414 open_err:
415         imx_keypad_close(dev);
416         return -EIO;
417 }
418
419 #ifdef CONFIG_OF
420 static struct of_device_id imx_keypad_of_match[] = {
421         { .compatible = "fsl,imx21-kpp", },
422         { /* sentinel */ }
423 };
424 MODULE_DEVICE_TABLE(of, imx_keypad_of_match);
425 #endif
426
427 static int imx_keypad_probe(struct platform_device *pdev)
428 {
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;
434
435         if (!keymap_data && !pdev->dev.of_node) {
436                 dev_err(&pdev->dev, "no keymap defined\n");
437                 return -EINVAL;
438         }
439
440         irq = platform_get_irq(pdev, 0);
441         if (irq < 0) {
442                 dev_err(&pdev->dev, "no irq defined in platform data\n");
443                 return -EINVAL;
444         }
445
446         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
447         if (res == NULL) {
448                 dev_err(&pdev->dev, "no I/O memory defined in platform data\n");
449                 return -EINVAL;
450         }
451
452         input_dev = devm_input_allocate_device(&pdev->dev);
453         if (!input_dev) {
454                 dev_err(&pdev->dev, "failed to allocate the input device\n");
455                 return -ENOMEM;
456         }
457
458         keypad = devm_kzalloc(&pdev->dev, sizeof(struct imx_keypad),
459                              GFP_KERNEL);
460         if (!keypad) {
461                 dev_err(&pdev->dev, "not enough memory for driver data\n");
462                 return -ENOMEM;
463         }
464
465         keypad->input_dev = input_dev;
466         keypad->irq = irq;
467         keypad->stable_count = 0;
468
469         setup_timer(&keypad->check_matrix_timer,
470                     imx_keypad_check_for_events, (unsigned long) keypad);
471
472         keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res);
473         if (IS_ERR(keypad->mmio_base))
474                 return PTR_ERR(keypad->mmio_base);
475
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);
480         }
481
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;
488
489         error = matrix_keypad_build_keymap(keymap_data, NULL,
490                                            MAX_MATRIX_KEY_ROWS,
491                                            MAX_MATRIX_KEY_COLS,
492                                            keypad->keycodes, input_dev);
493         if (error) {
494                 dev_err(&pdev->dev, "failed to build keymap\n");
495                 return error;
496         }
497
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;
505                         }
506                 }
507         }
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);
510
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);
514
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);
519
520         error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0,
521                             pdev->name, keypad);
522         if (error) {
523                 dev_err(&pdev->dev, "failed to request IRQ\n");
524                 return error;
525         }
526
527         /* Register the input device */
528         error = input_register_device(input_dev);
529         if (error) {
530                 dev_err(&pdev->dev, "failed to register input device\n");
531                 return error;
532         }
533
534         platform_set_drvdata(pdev, keypad);
535         device_init_wakeup(&pdev->dev, 1);
536
537         return 0;
538 }
539
540 #ifdef CONFIG_PM_SLEEP
541 static int imx_kbd_suspend(struct device *dev)
542 {
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;
546
547         /* imx kbd can wake up system even clock is disabled */
548         mutex_lock(&input_dev->mutex);
549
550         if (input_dev->users)
551                 clk_disable_unprepare(kbd->clk);
552
553         mutex_unlock(&input_dev->mutex);
554
555         if (device_may_wakeup(&pdev->dev))
556                 enable_irq_wake(kbd->irq);
557         else
558                 pinctrl_pm_select_sleep_state(dev);
559
560         return 0;
561 }
562
563 static int imx_kbd_resume(struct device *dev)
564 {
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;
568         int ret = 0;
569
570         if (device_may_wakeup(&pdev->dev))
571                 disable_irq_wake(kbd->irq);
572         else
573                 pinctrl_pm_select_default_state(dev);
574
575         mutex_lock(&input_dev->mutex);
576
577         if (input_dev->users) {
578                 ret = clk_prepare_enable(kbd->clk);
579                 if (ret)
580                         goto err_clk;
581         }
582
583 err_clk:
584         mutex_unlock(&input_dev->mutex);
585
586         return ret;
587 }
588 #endif
589
590 static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume);
591
592 static struct platform_driver imx_keypad_driver = {
593         .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),
598         },
599         .probe          = imx_keypad_probe,
600 };
601 module_platform_driver(imx_keypad_driver);
602
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");