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