]> Pileus Git - ~andy/linux/blob - drivers/input/keyboard/pxa27x_keypad.c
Input: pxa27x-keypad - convert to using SIMPLE_DEV_PM_OPS
[~andy/linux] / drivers / input / keyboard / pxa27x_keypad.c
1 /*
2  * linux/drivers/input/keyboard/pxa27x_keypad.c
3  *
4  * Driver for the pxa27x matrix keyboard controller.
5  *
6  * Created:     Feb 22, 2007
7  * Author:      Rodolfo Giometti <giometti@linux.it>
8  *
9  * Based on a previous implementations by Kevin O'Connor
10  * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
11  * on some suggestions by Nicolas Pitre <nico@fluxnic.net>.
12  *
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License version 2 as
15  * published by the Free Software Foundation.
16  */
17
18
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/input.h>
24 #include <linux/device.h>
25 #include <linux/platform_device.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/input/matrix_keypad.h>
29 #include <linux/slab.h>
30
31 #include <asm/mach/arch.h>
32 #include <asm/mach/map.h>
33
34 #include <mach/hardware.h>
35 #include <linux/platform_data/keypad-pxa27x.h>
36 /*
37  * Keypad Controller registers
38  */
39 #define KPC             0x0000 /* Keypad Control register */
40 #define KPDK            0x0008 /* Keypad Direct Key register */
41 #define KPREC           0x0010 /* Keypad Rotary Encoder register */
42 #define KPMK            0x0018 /* Keypad Matrix Key register */
43 #define KPAS            0x0020 /* Keypad Automatic Scan register */
44
45 /* Keypad Automatic Scan Multiple Key Presser register 0-3 */
46 #define KPASMKP0        0x0028
47 #define KPASMKP1        0x0030
48 #define KPASMKP2        0x0038
49 #define KPASMKP3        0x0040
50 #define KPKDI           0x0048
51
52 /* bit definitions */
53 #define KPC_MKRN(n)     ((((n) - 1) & 0x7) << 26) /* matrix key row number */
54 #define KPC_MKCN(n)     ((((n) - 1) & 0x7) << 23) /* matrix key column number */
55 #define KPC_DKN(n)      ((((n) - 1) & 0x7) << 6)  /* direct key number */
56
57 #define KPC_AS          (0x1 << 30)  /* Automatic Scan bit */
58 #define KPC_ASACT       (0x1 << 29)  /* Automatic Scan on Activity */
59 #define KPC_MI          (0x1 << 22)  /* Matrix interrupt bit */
60 #define KPC_IMKP        (0x1 << 21)  /* Ignore Multiple Key Press */
61
62 #define KPC_MS(n)       (0x1 << (13 + (n)))     /* Matrix scan line 'n' */
63 #define KPC_MS_ALL      (0xff << 13)
64
65 #define KPC_ME          (0x1 << 12)  /* Matrix Keypad Enable */
66 #define KPC_MIE         (0x1 << 11)  /* Matrix Interrupt Enable */
67 #define KPC_DK_DEB_SEL  (0x1 <<  9)  /* Direct Keypad Debounce Select */
68 #define KPC_DI          (0x1 <<  5)  /* Direct key interrupt bit */
69 #define KPC_RE_ZERO_DEB (0x1 <<  4)  /* Rotary Encoder Zero Debounce */
70 #define KPC_REE1        (0x1 <<  3)  /* Rotary Encoder1 Enable */
71 #define KPC_REE0        (0x1 <<  2)  /* Rotary Encoder0 Enable */
72 #define KPC_DE          (0x1 <<  1)  /* Direct Keypad Enable */
73 #define KPC_DIE         (0x1 <<  0)  /* Direct Keypad interrupt Enable */
74
75 #define KPDK_DKP        (0x1 << 31)
76 #define KPDK_DK(n)      ((n) & 0xff)
77
78 #define KPREC_OF1       (0x1 << 31)
79 #define kPREC_UF1       (0x1 << 30)
80 #define KPREC_OF0       (0x1 << 15)
81 #define KPREC_UF0       (0x1 << 14)
82
83 #define KPREC_RECOUNT0(n)       ((n) & 0xff)
84 #define KPREC_RECOUNT1(n)       (((n) >> 16) & 0xff)
85
86 #define KPMK_MKP        (0x1 << 31)
87 #define KPAS_SO         (0x1 << 31)
88 #define KPASMKPx_SO     (0x1 << 31)
89
90 #define KPAS_MUKP(n)    (((n) >> 26) & 0x1f)
91 #define KPAS_RP(n)      (((n) >> 4) & 0xf)
92 #define KPAS_CP(n)      ((n) & 0xf)
93
94 #define KPASMKP_MKC_MASK        (0xff)
95
96 #define keypad_readl(off)       __raw_readl(keypad->mmio_base + (off))
97 #define keypad_writel(off, v)   __raw_writel((v), keypad->mmio_base + (off))
98
99 #define MAX_MATRIX_KEY_NUM      (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS)
100 #define MAX_KEYPAD_KEYS         (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM)
101
102 struct pxa27x_keypad {
103         struct pxa27x_keypad_platform_data *pdata;
104
105         struct clk *clk;
106         struct input_dev *input_dev;
107         void __iomem *mmio_base;
108
109         int irq;
110
111         unsigned short keycodes[MAX_KEYPAD_KEYS];
112         int rotary_rel_code[2];
113
114         /* state row bits of each column scan */
115         uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
116         uint32_t direct_key_state;
117
118         unsigned int direct_key_mask;
119 };
120
121 #ifdef CONFIG_OF
122 static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad)
123 {
124         struct input_dev *input_dev = keypad->input_dev;
125         struct device *dev = input_dev->dev.parent;
126         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
127         u32 rows, cols;
128         int error;
129
130         error = matrix_keypad_parse_of_params(dev, &rows, &cols);
131         if (error)
132                 return error;
133
134         if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) {
135                 dev_err(dev, "rows or cols exceeds maximum value\n");
136                 return -EINVAL;
137         }
138
139         pdata->matrix_key_rows = rows;
140         pdata->matrix_key_cols = cols;
141
142         error = matrix_keypad_build_keymap(NULL, NULL,
143                                            pdata->matrix_key_rows,
144                                            pdata->matrix_key_cols,
145                                            keypad->keycodes, input_dev);
146         if (error)
147                 return error;
148
149         return 0;
150 }
151
152 static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad)
153 {
154         struct input_dev *input_dev = keypad->input_dev;
155         struct device *dev = input_dev->dev.parent;
156         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
157         struct device_node *np = dev->of_node;
158         const __be16 *prop;
159         unsigned short code;
160         unsigned int proplen, size;
161         int i;
162         int error;
163
164         error = of_property_read_u32(np, "marvell,direct-key-count",
165                                      &pdata->direct_key_num);
166         if (error) {
167                 /*
168                  * If do not have marvel,direct-key-count defined,
169                  * it means direct key is not supported.
170                  */
171                 return error == -EINVAL ? 0 : error;
172         }
173
174         error = of_property_read_u32(np, "marvell,direct-key-mask",
175                                      &pdata->direct_key_mask);
176         if (error) {
177                 if (error != -EINVAL)
178                         return error;
179
180                 /*
181                  * If marvell,direct-key-mask is not defined, driver will use
182                  * default value. Default value is set when configure the keypad.
183                  */
184                 pdata->direct_key_mask = 0;
185         }
186
187         pdata->direct_key_low_active = of_property_read_bool(np,
188                                         "marvell,direct-key-low-active");
189
190         prop = of_get_property(np, "marvell,direct-key-map", &proplen);
191         if (!prop)
192                 return -EINVAL;
193
194         if (proplen % sizeof(u16))
195                 return -EINVAL;
196
197         size = proplen / sizeof(u16);
198
199         /* Only MAX_DIRECT_KEY_NUM is accepted.*/
200         if (size > MAX_DIRECT_KEY_NUM)
201                 return -EINVAL;
202
203         for (i = 0; i < size; i++) {
204                 code = be16_to_cpup(prop + i);
205                 keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code;
206                 __set_bit(code, input_dev->keybit);
207         }
208
209         return 0;
210 }
211
212 static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad)
213 {
214         const __be32 *prop;
215         int i, relkey_ret;
216         unsigned int code, proplen;
217         const char *rotaryname[2] = {
218                         "marvell,rotary0", "marvell,rotary1"};
219         const char relkeyname[] = {"marvell,rotary-rel-key"};
220         struct input_dev *input_dev = keypad->input_dev;
221         struct device *dev = input_dev->dev.parent;
222         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
223         struct device_node *np = dev->of_node;
224
225         relkey_ret = of_property_read_u32(np, relkeyname, &code);
226         /* if can read correct rotary key-code, we do not need this. */
227         if (relkey_ret == 0) {
228                 unsigned short relcode;
229
230                 /* rotary0 taks lower half, rotary1 taks upper half. */
231                 relcode = code & 0xffff;
232                 pdata->rotary0_rel_code = (code & 0xffff);
233                 __set_bit(relcode, input_dev->relbit);
234
235                 relcode = code >> 16;
236                 pdata->rotary1_rel_code = relcode;
237                 __set_bit(relcode, input_dev->relbit);
238         }
239
240         for (i = 0; i < 2; i++) {
241                 prop = of_get_property(np, rotaryname[i], &proplen);
242                 /*
243                  * If the prop is not set, it means keypad does not need
244                  * initialize the rotaryX.
245                  */
246                 if (!prop)
247                         continue;
248
249                 code = be32_to_cpup(prop);
250                 /*
251                  * Not all up/down key code are valid.
252                  * Now we depends on direct-rel-code.
253                  */
254                 if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) {
255                         return relkey_ret;
256                 } else {
257                         unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1);
258                         unsigned short keycode;
259
260                         keycode = code & 0xffff;
261                         keypad->keycodes[n] = keycode;
262                         __set_bit(keycode, input_dev->keybit);
263
264                         keycode = code >> 16;
265                         keypad->keycodes[n + 1] = keycode;
266                         __set_bit(keycode, input_dev->keybit);
267
268                         if (i == 0)
269                                 pdata->rotary0_rel_code = -1;
270                         else
271                                 pdata->rotary1_rel_code = -1;
272                 }
273                 if (i == 0)
274                         pdata->enable_rotary0 = 1;
275                 else
276                         pdata->enable_rotary1 = 1;
277         }
278
279         keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
280         keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
281
282         return 0;
283 }
284
285 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
286 {
287         struct input_dev *input_dev = keypad->input_dev;
288         struct device *dev = input_dev->dev.parent;
289         struct device_node *np = dev->of_node;
290         int error;
291
292         keypad->pdata = devm_kzalloc(dev, sizeof(*keypad->pdata),
293                                      GFP_KERNEL);
294         if (!keypad->pdata) {
295                 dev_err(dev, "failed to allocate memory for pdata\n");
296                 return -ENOMEM;
297         }
298
299         error = pxa27x_keypad_matrix_key_parse_dt(keypad);
300         if (error) {
301                 dev_err(dev, "failed to parse matrix key\n");
302                 return error;
303         }
304
305         error = pxa27x_keypad_direct_key_parse_dt(keypad);
306         if (error) {
307                 dev_err(dev, "failed to parse direct key\n");
308                 return error;
309         }
310
311         error = pxa27x_keypad_rotary_parse_dt(keypad);
312         if (error) {
313                 dev_err(dev, "failed to parse rotary key\n");
314                 return error;
315         }
316
317         error = of_property_read_u32(np, "marvell,debounce-interval",
318                                     &keypad->pdata->debounce_interval);
319         if (error) {
320                 dev_err(dev, "failed to parse debpunce-interval\n");
321                 return error;
322         }
323
324         /*
325          * The keycodes may not only includes matrix key but also the direct
326          * key or rotary key.
327          */
328         input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
329
330         return 0;
331 }
332
333 #else
334
335 static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad)
336 {
337         dev_info(keypad->input_dev->dev.parent, "missing platform data\n");
338
339         return -EINVAL;
340 }
341
342 #endif
343
344 static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
345 {
346         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
347         struct input_dev *input_dev = keypad->input_dev;
348         const struct matrix_keymap_data *keymap_data =
349                                 pdata ? pdata->matrix_keymap_data : NULL;
350         unsigned short keycode;
351         int i;
352         int error;
353
354         error = matrix_keypad_build_keymap(keymap_data, NULL,
355                                            pdata->matrix_key_rows,
356                                            pdata->matrix_key_cols,
357                                            keypad->keycodes, input_dev);
358         if (error)
359                 return error;
360
361         /*
362          * The keycodes may not only include matrix keys but also the direct
363          * or rotary keys.
364          */
365         input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
366
367         /* For direct keys. */
368         for (i = 0; i < pdata->direct_key_num; i++) {
369                 keycode = pdata->direct_key_map[i];
370                 keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode;
371                 __set_bit(keycode, input_dev->keybit);
372         }
373
374         if (pdata->enable_rotary0) {
375                 if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
376                         keycode = pdata->rotary0_up_key;
377                         keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode;
378                         __set_bit(keycode, input_dev->keybit);
379
380                         keycode = pdata->rotary0_down_key;
381                         keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode;
382                         __set_bit(keycode, input_dev->keybit);
383
384                         keypad->rotary_rel_code[0] = -1;
385                 } else {
386                         keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
387                         __set_bit(pdata->rotary0_rel_code, input_dev->relbit);
388                 }
389         }
390
391         if (pdata->enable_rotary1) {
392                 if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
393                         keycode = pdata->rotary1_up_key;
394                         keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode;
395                         __set_bit(keycode, input_dev->keybit);
396
397                         keycode = pdata->rotary1_down_key;
398                         keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode;
399                         __set_bit(keycode, input_dev->keybit);
400
401                         keypad->rotary_rel_code[1] = -1;
402                 } else {
403                         keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
404                         __set_bit(pdata->rotary1_rel_code, input_dev->relbit);
405                 }
406         }
407
408         __clear_bit(KEY_RESERVED, input_dev->keybit);
409
410         return 0;
411 }
412
413 static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
414 {
415         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
416         struct input_dev *input_dev = keypad->input_dev;
417         int row, col, num_keys_pressed = 0;
418         uint32_t new_state[MAX_MATRIX_KEY_COLS];
419         uint32_t kpas = keypad_readl(KPAS);
420
421         num_keys_pressed = KPAS_MUKP(kpas);
422
423         memset(new_state, 0, sizeof(new_state));
424
425         if (num_keys_pressed == 0)
426                 goto scan;
427
428         if (num_keys_pressed == 1) {
429                 col = KPAS_CP(kpas);
430                 row = KPAS_RP(kpas);
431
432                 /* if invalid row/col, treat as no key pressed */
433                 if (col >= pdata->matrix_key_cols ||
434                     row >= pdata->matrix_key_rows)
435                         goto scan;
436
437                 new_state[col] = (1 << row);
438                 goto scan;
439         }
440
441         if (num_keys_pressed > 1) {
442                 uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
443                 uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
444                 uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
445                 uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
446
447                 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
448                 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
449                 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
450                 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
451                 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
452                 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
453                 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
454                 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
455         }
456 scan:
457         for (col = 0; col < pdata->matrix_key_cols; col++) {
458                 uint32_t bits_changed;
459                 int code;
460
461                 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
462                 if (bits_changed == 0)
463                         continue;
464
465                 for (row = 0; row < pdata->matrix_key_rows; row++) {
466                         if ((bits_changed & (1 << row)) == 0)
467                                 continue;
468
469                         code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT);
470                         input_event(input_dev, EV_MSC, MSC_SCAN, code);
471                         input_report_key(input_dev, keypad->keycodes[code],
472                                          new_state[col] & (1 << row));
473                 }
474         }
475         input_sync(input_dev);
476         memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
477 }
478
479 #define DEFAULT_KPREC   (0x007f007f)
480
481 static inline int rotary_delta(uint32_t kprec)
482 {
483         if (kprec & KPREC_OF0)
484                 return (kprec & 0xff) + 0x7f;
485         else if (kprec & KPREC_UF0)
486                 return (kprec & 0xff) - 0x7f - 0xff;
487         else
488                 return (kprec & 0xff) - 0x7f;
489 }
490
491 static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
492 {
493         struct input_dev *dev = keypad->input_dev;
494
495         if (delta == 0)
496                 return;
497
498         if (keypad->rotary_rel_code[r] == -1) {
499                 int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1);
500                 unsigned char keycode = keypad->keycodes[code];
501
502                 /* simulate a press-n-release */
503                 input_event(dev, EV_MSC, MSC_SCAN, code);
504                 input_report_key(dev, keycode, 1);
505                 input_sync(dev);
506                 input_event(dev, EV_MSC, MSC_SCAN, code);
507                 input_report_key(dev, keycode, 0);
508                 input_sync(dev);
509         } else {
510                 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
511                 input_sync(dev);
512         }
513 }
514
515 static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
516 {
517         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
518         uint32_t kprec;
519
520         /* read and reset to default count value */
521         kprec = keypad_readl(KPREC);
522         keypad_writel(KPREC, DEFAULT_KPREC);
523
524         if (pdata->enable_rotary0)
525                 report_rotary_event(keypad, 0, rotary_delta(kprec));
526
527         if (pdata->enable_rotary1)
528                 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
529 }
530
531 static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
532 {
533         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
534         struct input_dev *input_dev = keypad->input_dev;
535         unsigned int new_state;
536         uint32_t kpdk, bits_changed;
537         int i;
538
539         kpdk = keypad_readl(KPDK);
540
541         if (pdata->enable_rotary0 || pdata->enable_rotary1)
542                 pxa27x_keypad_scan_rotary(keypad);
543
544         /*
545          * The KPDR_DK only output the key pin level, so it relates to board,
546          * and low level may be active.
547          */
548         if (pdata->direct_key_low_active)
549                 new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask;
550         else
551                 new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
552
553         bits_changed = keypad->direct_key_state ^ new_state;
554
555         if (bits_changed == 0)
556                 return;
557
558         for (i = 0; i < pdata->direct_key_num; i++) {
559                 if (bits_changed & (1 << i)) {
560                         int code = MAX_MATRIX_KEY_NUM + i;
561
562                         input_event(input_dev, EV_MSC, MSC_SCAN, code);
563                         input_report_key(input_dev, keypad->keycodes[code],
564                                          new_state & (1 << i));
565                 }
566         }
567         input_sync(input_dev);
568         keypad->direct_key_state = new_state;
569 }
570
571 static void clear_wakeup_event(struct pxa27x_keypad *keypad)
572 {
573         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
574
575         if (pdata->clear_wakeup_event)
576                 (pdata->clear_wakeup_event)();
577 }
578
579 static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
580 {
581         struct pxa27x_keypad *keypad = dev_id;
582         unsigned long kpc = keypad_readl(KPC);
583
584         clear_wakeup_event(keypad);
585
586         if (kpc & KPC_DI)
587                 pxa27x_keypad_scan_direct(keypad);
588
589         if (kpc & KPC_MI)
590                 pxa27x_keypad_scan_matrix(keypad);
591
592         return IRQ_HANDLED;
593 }
594
595 static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
596 {
597         struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
598         unsigned int mask = 0, direct_key_num = 0;
599         unsigned long kpc = 0;
600
601         /* clear pending interrupt bit */
602         keypad_readl(KPC);
603
604         /* enable matrix keys with automatic scan */
605         if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
606                 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
607                 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
608                        KPC_MKCN(pdata->matrix_key_cols);
609         }
610
611         /* enable rotary key, debounce interval same as direct keys */
612         if (pdata->enable_rotary0) {
613                 mask |= 0x03;
614                 direct_key_num = 2;
615                 kpc |= KPC_REE0;
616         }
617
618         if (pdata->enable_rotary1) {
619                 mask |= 0x0c;
620                 direct_key_num = 4;
621                 kpc |= KPC_REE1;
622         }
623
624         if (pdata->direct_key_num > direct_key_num)
625                 direct_key_num = pdata->direct_key_num;
626
627         /*
628          * Direct keys usage may not start from KP_DKIN0, check the platfrom
629          * mask data to config the specific.
630          */
631         if (pdata->direct_key_mask)
632                 keypad->direct_key_mask = pdata->direct_key_mask;
633         else
634                 keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask;
635
636         /* enable direct key */
637         if (direct_key_num)
638                 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
639
640         keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
641         keypad_writel(KPREC, DEFAULT_KPREC);
642         keypad_writel(KPKDI, pdata->debounce_interval);
643 }
644
645 static int pxa27x_keypad_open(struct input_dev *dev)
646 {
647         struct pxa27x_keypad *keypad = input_get_drvdata(dev);
648
649         /* Enable unit clock */
650         clk_prepare_enable(keypad->clk);
651         pxa27x_keypad_config(keypad);
652
653         return 0;
654 }
655
656 static void pxa27x_keypad_close(struct input_dev *dev)
657 {
658         struct pxa27x_keypad *keypad = input_get_drvdata(dev);
659
660         /* Disable clock unit */
661         clk_disable_unprepare(keypad->clk);
662 }
663
664 #ifdef CONFIG_PM_SLEEP
665 static int pxa27x_keypad_suspend(struct device *dev)
666 {
667         struct platform_device *pdev = to_platform_device(dev);
668         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
669
670         /*
671          * If the keypad is used a wake up source, clock can not be disabled.
672          * Or it can not detect the key pressing.
673          */
674         if (device_may_wakeup(&pdev->dev))
675                 enable_irq_wake(keypad->irq);
676         else
677                 clk_disable_unprepare(keypad->clk);
678
679         return 0;
680 }
681
682 static int pxa27x_keypad_resume(struct device *dev)
683 {
684         struct platform_device *pdev = to_platform_device(dev);
685         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
686         struct input_dev *input_dev = keypad->input_dev;
687
688         /*
689          * If the keypad is used as wake up source, the clock is not turned
690          * off. So do not need configure it again.
691          */
692         if (device_may_wakeup(&pdev->dev)) {
693                 disable_irq_wake(keypad->irq);
694         } else {
695                 mutex_lock(&input_dev->mutex);
696
697                 if (input_dev->users) {
698                         /* Enable unit clock */
699                         clk_prepare_enable(keypad->clk);
700                         pxa27x_keypad_config(keypad);
701                 }
702
703                 mutex_unlock(&input_dev->mutex);
704         }
705
706         return 0;
707 }
708 #endif
709
710 static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops,
711                          pxa27x_keypad_suspend, pxa27x_keypad_resume);
712
713
714 static int pxa27x_keypad_probe(struct platform_device *pdev)
715 {
716         struct pxa27x_keypad_platform_data *pdata = pdev->dev.platform_data;
717         struct device_node *np = pdev->dev.of_node;
718         struct pxa27x_keypad *keypad;
719         struct input_dev *input_dev;
720         struct resource *res;
721         int irq, error;
722
723         /* Driver need build keycode from device tree or pdata */
724         if (!np && !pdata)
725                 return -EINVAL;
726
727         irq = platform_get_irq(pdev, 0);
728         if (irq < 0) {
729                 dev_err(&pdev->dev, "failed to get keypad irq\n");
730                 return -ENXIO;
731         }
732
733         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
734         if (res == NULL) {
735                 dev_err(&pdev->dev, "failed to get I/O memory\n");
736                 return -ENXIO;
737         }
738
739         keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
740         input_dev = input_allocate_device();
741         if (!keypad || !input_dev) {
742                 dev_err(&pdev->dev, "failed to allocate memory\n");
743                 error = -ENOMEM;
744                 goto failed_free;
745         }
746
747         keypad->pdata = pdata;
748         keypad->input_dev = input_dev;
749         keypad->irq = irq;
750
751         res = request_mem_region(res->start, resource_size(res), pdev->name);
752         if (res == NULL) {
753                 dev_err(&pdev->dev, "failed to request I/O memory\n");
754                 error = -EBUSY;
755                 goto failed_free;
756         }
757
758         keypad->mmio_base = ioremap(res->start, resource_size(res));
759         if (keypad->mmio_base == NULL) {
760                 dev_err(&pdev->dev, "failed to remap I/O memory\n");
761                 error = -ENXIO;
762                 goto failed_free_mem;
763         }
764
765         keypad->clk = clk_get(&pdev->dev, NULL);
766         if (IS_ERR(keypad->clk)) {
767                 dev_err(&pdev->dev, "failed to get keypad clock\n");
768                 error = PTR_ERR(keypad->clk);
769                 goto failed_free_io;
770         }
771
772         input_dev->name = pdev->name;
773         input_dev->id.bustype = BUS_HOST;
774         input_dev->open = pxa27x_keypad_open;
775         input_dev->close = pxa27x_keypad_close;
776         input_dev->dev.parent = &pdev->dev;
777
778         input_dev->keycode = keypad->keycodes;
779         input_dev->keycodesize = sizeof(keypad->keycodes[0]);
780         input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
781
782         input_set_drvdata(input_dev, keypad);
783
784         input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
785         input_set_capability(input_dev, EV_MSC, MSC_SCAN);
786
787         if (pdata)
788                 error = pxa27x_keypad_build_keycode(keypad);
789         else
790                 error = pxa27x_keypad_build_keycode_from_dt(keypad);
791         if (error) {
792                 dev_err(&pdev->dev, "failed to build keycode\n");
793                 goto failed_put_clk;
794         }
795
796         /* If device tree is supported, pdata will be allocated. */
797         pdata = keypad->pdata;
798
799         if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) ||
800             (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) {
801                 input_dev->evbit[0] |= BIT_MASK(EV_REL);
802         }
803
804         error = request_irq(irq, pxa27x_keypad_irq_handler, 0,
805                             pdev->name, keypad);
806         if (error) {
807                 dev_err(&pdev->dev, "failed to request IRQ\n");
808                 goto failed_put_clk;
809         }
810
811         /* Register the input device */
812         error = input_register_device(input_dev);
813         if (error) {
814                 dev_err(&pdev->dev, "failed to register input device\n");
815                 goto failed_free_irq;
816         }
817
818         platform_set_drvdata(pdev, keypad);
819         device_init_wakeup(&pdev->dev, 1);
820
821         return 0;
822
823 failed_free_irq:
824         free_irq(irq, keypad);
825 failed_put_clk:
826         clk_put(keypad->clk);
827 failed_free_io:
828         iounmap(keypad->mmio_base);
829 failed_free_mem:
830         release_mem_region(res->start, resource_size(res));
831 failed_free:
832         input_free_device(input_dev);
833         kfree(keypad);
834         return error;
835 }
836
837 static int pxa27x_keypad_remove(struct platform_device *pdev)
838 {
839         struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
840         struct resource *res;
841
842         free_irq(keypad->irq, keypad);
843         clk_put(keypad->clk);
844
845         input_unregister_device(keypad->input_dev);
846         iounmap(keypad->mmio_base);
847
848         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
849         release_mem_region(res->start, resource_size(res));
850
851         kfree(keypad);
852
853         return 0;
854 }
855
856 /* work with hotplug and coldplug */
857 MODULE_ALIAS("platform:pxa27x-keypad");
858
859 #ifdef CONFIG_OF
860 static const struct of_device_id pxa27x_keypad_dt_match[] = {
861         { .compatible = "marvell,pxa27x-keypad" },
862         {},
863 };
864 MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match);
865 #endif
866
867 static struct platform_driver pxa27x_keypad_driver = {
868         .probe          = pxa27x_keypad_probe,
869         .remove         = pxa27x_keypad_remove,
870         .driver         = {
871                 .name   = "pxa27x-keypad",
872                 .of_match_table = of_match_ptr(pxa27x_keypad_dt_match),
873                 .owner  = THIS_MODULE,
874                 .pm     = &pxa27x_keypad_pm_ops,
875         },
876 };
877 module_platform_driver(pxa27x_keypad_driver);
878
879 MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
880 MODULE_LICENSE("GPL");