2 * Copyright (c) 2008-2009 Nuvoton technology corporation.
4 * Wan ZongShun <mcuos.com@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation;version 2 of the License.
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/interrupt.h>
16 #include <linux/input.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/clk.h>
20 #include <linux/err.h>
22 #include <linux/slab.h>
24 #include <mach/w90p910_keypad.h>
26 /* Keypad Interface Control Registers */
28 #define KPI_3KCONF 0x04
29 #define KPI_LPCONF 0x08
30 #define KPI_STATUS 0x0C
32 #define IS1KEY (0x01 << 16)
33 #define INTTR (0x01 << 21)
34 #define KEY0R (0x0f << 3)
36 #define DEBOUNCE_BIT 0x08
37 #define KSIZE0 (0x01 << 16)
38 #define KSIZE1 (0x01 << 17)
39 #define KPSEL (0x01 << 19)
40 #define ENKP (0x01 << 18)
42 #define KGET_RAW(n) (((n) & KEY0R) >> 3)
43 #define KGET_COLUMN(n) ((n) & KEY0C)
45 #define W90P910_NUM_ROWS 8
46 #define W90P910_NUM_COLS 8
47 #define W90P910_ROW_SHIFT 3
49 struct w90p910_keypad
{
50 const struct w90p910_keypad_platform_data
*pdata
;
52 struct input_dev
*input_dev
;
53 void __iomem
*mmio_base
;
55 unsigned short keymap
[W90P910_NUM_ROWS
* W90P910_NUM_COLS
];
58 static void w90p910_keypad_scan_matrix(struct w90p910_keypad
*keypad
,
61 struct input_dev
*input_dev
= keypad
->input_dev
;
62 unsigned int row
= KGET_RAW(status
);
63 unsigned int col
= KGET_COLUMN(status
);
64 unsigned int code
= MATRIX_SCAN_CODE(row
, col
, W90P910_ROW_SHIFT
);
65 unsigned int key
= keypad
->keymap
[code
];
67 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
68 input_report_key(input_dev
, key
, 1);
69 input_sync(input_dev
);
71 input_event(input_dev
, EV_MSC
, MSC_SCAN
, code
);
72 input_report_key(input_dev
, key
, 0);
73 input_sync(input_dev
);
76 static irqreturn_t
w90p910_keypad_irq_handler(int irq
, void *dev_id
)
78 struct w90p910_keypad
*keypad
= dev_id
;
79 unsigned int kstatus
, val
;
81 kstatus
= __raw_readl(keypad
->mmio_base
+ KPI_STATUS
);
86 w90p910_keypad_scan_matrix(keypad
, kstatus
);
91 static int w90p910_keypad_open(struct input_dev
*dev
)
93 struct w90p910_keypad
*keypad
= input_get_drvdata(dev
);
94 const struct w90p910_keypad_platform_data
*pdata
= keypad
->pdata
;
95 unsigned int val
, config
;
97 /* Enable unit clock */
98 clk_enable(keypad
->clk
);
100 val
= __raw_readl(keypad
->mmio_base
+ KPI_CONF
);
101 val
|= (KPSEL
| ENKP
);
102 val
&= ~(KSIZE0
| KSIZE1
);
104 config
= pdata
->prescale
| (pdata
->debounce
<< DEBOUNCE_BIT
);
108 __raw_writel(val
, keypad
->mmio_base
+ KPI_CONF
);
113 static void w90p910_keypad_close(struct input_dev
*dev
)
115 struct w90p910_keypad
*keypad
= input_get_drvdata(dev
);
117 /* Disable clock unit */
118 clk_disable(keypad
->clk
);
121 static int __devinit
w90p910_keypad_probe(struct platform_device
*pdev
)
123 const struct w90p910_keypad_platform_data
*pdata
=
124 pdev
->dev
.platform_data
;
125 const struct matrix_keymap_data
*keymap_data
;
126 struct w90p910_keypad
*keypad
;
127 struct input_dev
*input_dev
;
128 struct resource
*res
;
133 dev_err(&pdev
->dev
, "no platform data defined\n");
137 keymap_data
= pdata
->keymap_data
;
139 irq
= platform_get_irq(pdev
, 0);
141 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
145 keypad
= kzalloc(sizeof(struct w90p910_keypad
), GFP_KERNEL
);
146 input_dev
= input_allocate_device();
147 if (!keypad
|| !input_dev
) {
148 dev_err(&pdev
->dev
, "failed to allocate driver data\n");
153 keypad
->pdata
= pdata
;
154 keypad
->input_dev
= input_dev
;
157 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
159 dev_err(&pdev
->dev
, "failed to get I/O memory\n");
164 res
= request_mem_region(res
->start
, resource_size(res
), pdev
->name
);
166 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
171 keypad
->mmio_base
= ioremap(res
->start
, resource_size(res
));
172 if (keypad
->mmio_base
== NULL
) {
173 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
175 goto failed_free_res
;
178 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
179 if (IS_ERR(keypad
->clk
)) {
180 dev_err(&pdev
->dev
, "failed to get keypad clock\n");
181 error
= PTR_ERR(keypad
->clk
);
185 /* set multi-function pin for w90p910 kpi. */
186 mfp_set_groupi(&pdev
->dev
);
188 input_dev
->name
= pdev
->name
;
189 input_dev
->id
.bustype
= BUS_HOST
;
190 input_dev
->open
= w90p910_keypad_open
;
191 input_dev
->close
= w90p910_keypad_close
;
192 input_dev
->dev
.parent
= &pdev
->dev
;
194 error
= matrix_keypad_build_keymap(keymap_data
, NULL
,
195 W90P910_NUM_ROWS
, W90P910_NUM_COLS
,
196 keypad
->keymap
, input_dev
);
198 dev_err(&pdev
->dev
, "failed to build keymap\n");
202 error
= request_irq(keypad
->irq
, w90p910_keypad_irq_handler
,
203 0, pdev
->name
, keypad
);
205 dev_err(&pdev
->dev
, "failed to request IRQ\n");
209 __set_bit(EV_REP
, input_dev
->evbit
);
210 input_set_capability(input_dev
, EV_MSC
, MSC_SCAN
);
211 input_set_drvdata(input_dev
, keypad
);
213 /* Register the input device */
214 error
= input_register_device(input_dev
);
216 dev_err(&pdev
->dev
, "failed to register input device\n");
217 goto failed_free_irq
;
220 platform_set_drvdata(pdev
, keypad
);
226 clk_put(keypad
->clk
);
228 iounmap(keypad
->mmio_base
);
230 release_mem_region(res
->start
, resource_size(res
));
232 input_free_device(input_dev
);
237 static int __devexit
w90p910_keypad_remove(struct platform_device
*pdev
)
239 struct w90p910_keypad
*keypad
= platform_get_drvdata(pdev
);
240 struct resource
*res
;
242 free_irq(keypad
->irq
, pdev
);
244 clk_put(keypad
->clk
);
246 input_unregister_device(keypad
->input_dev
);
248 iounmap(keypad
->mmio_base
);
249 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
250 release_mem_region(res
->start
, resource_size(res
));
252 platform_set_drvdata(pdev
, NULL
);
258 static struct platform_driver w90p910_keypad_driver
= {
259 .probe
= w90p910_keypad_probe
,
260 .remove
= __devexit_p(w90p910_keypad_remove
),
262 .name
= "nuc900-kpi",
263 .owner
= THIS_MODULE
,
266 module_platform_driver(w90p910_keypad_driver
);
268 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
269 MODULE_DESCRIPTION("w90p910 keypad driver");
270 MODULE_LICENSE("GPL");
271 MODULE_ALIAS("platform:nuc900-keypad");