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>
23 #include <mach/w90p910_keypad.h>
25 /* Keypad Interface Control Registers */
27 #define KPI_3KCONF 0x04
28 #define KPI_LPCONF 0x08
29 #define KPI_STATUS 0x0C
31 #define IS1KEY (0x01 << 16)
32 #define INTTR (0x01 << 21)
33 #define KEY0R (0x0f << 3)
35 #define DEBOUNCE_BIT 0x08
36 #define KSIZE0 (0x01 << 16)
37 #define KSIZE1 (0x01 << 17)
38 #define KPSEL (0x01 << 19)
39 #define ENKP (0x01 << 18)
41 #define KGET_RAW(n) (((n) & KEY0R) >> 3)
42 #define KGET_COLUMN(n) ((n) & KEY0C)
44 #define W90P910_MAX_KEY_NUM (8 * 8)
45 #define W90P910_ROW_SHIFT 3
47 struct w90p910_keypad {
48 const struct w90p910_keypad_platform_data *pdata;
50 struct input_dev *input_dev;
51 void __iomem *mmio_base;
53 unsigned short keymap[W90P910_MAX_KEY_NUM];
56 static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
59 struct input_dev *input_dev = keypad->input_dev;
60 unsigned int row = KGET_RAW(status);
61 unsigned int col = KGET_COLUMN(status);
62 unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
63 unsigned int key = keypad->keymap[code];
65 input_event(input_dev, EV_MSC, MSC_SCAN, code);
66 input_report_key(input_dev, key, 1);
67 input_sync(input_dev);
69 input_event(input_dev, EV_MSC, MSC_SCAN, code);
70 input_report_key(input_dev, key, 0);
71 input_sync(input_dev);
74 static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
76 struct w90p910_keypad *keypad = dev_id;
77 unsigned int kstatus, val;
79 kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
84 w90p910_keypad_scan_matrix(keypad, kstatus);
89 static int w90p910_keypad_open(struct input_dev *dev)
91 struct w90p910_keypad *keypad = input_get_drvdata(dev);
92 const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
93 unsigned int val, config;
95 /* Enable unit clock */
96 clk_enable(keypad->clk);
98 val = __raw_readl(keypad->mmio_base + KPI_CONF);
99 val |= (KPSEL | ENKP);
100 val &= ~(KSIZE0 | KSIZE1);
102 config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
106 __raw_writel(val, keypad->mmio_base + KPI_CONF);
111 static void w90p910_keypad_close(struct input_dev *dev)
113 struct w90p910_keypad *keypad = input_get_drvdata(dev);
115 /* Disable clock unit */
116 clk_disable(keypad->clk);
119 static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
121 const struct w90p910_keypad_platform_data *pdata =
122 pdev->dev.platform_data;
123 const struct matrix_keymap_data *keymap_data = pdata->keymap_data;
124 struct w90p910_keypad *keypad;
125 struct input_dev *input_dev;
126 struct resource *res;
132 dev_err(&pdev->dev, "no platform data defined\n");
136 irq = platform_get_irq(pdev, 0);
138 dev_err(&pdev->dev, "failed to get keypad irq\n");
142 keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
143 input_dev = input_allocate_device();
144 if (!keypad || !input_dev) {
145 dev_err(&pdev->dev, "failed to allocate driver data\n");
150 keypad->pdata = pdata;
151 keypad->input_dev = input_dev;
154 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
156 dev_err(&pdev->dev, "failed to get I/O memory\n");
161 res = request_mem_region(res->start, resource_size(res), pdev->name);
163 dev_err(&pdev->dev, "failed to request I/O memory\n");
168 keypad->mmio_base = ioremap(res->start, resource_size(res));
169 if (keypad->mmio_base == NULL) {
170 dev_err(&pdev->dev, "failed to remap I/O memory\n");
172 goto failed_free_res;
175 keypad->clk = clk_get(&pdev->dev, NULL);
176 if (IS_ERR(keypad->clk)) {
177 dev_err(&pdev->dev, "failed to get keypad clock\n");
178 error = PTR_ERR(keypad->clk);
182 /* set multi-function pin for w90p910 kpi. */
183 mfp_set_groupi(&pdev->dev);
185 input_dev->name = pdev->name;
186 input_dev->id.bustype = BUS_HOST;
187 input_dev->open = w90p910_keypad_open;
188 input_dev->close = w90p910_keypad_close;
189 input_dev->dev.parent = &pdev->dev;
191 input_dev->keycode = keypad->keymap;
192 input_dev->keycodesize = sizeof(keypad->keymap[0]);
193 input_dev->keycodemax = ARRAY_SIZE(keypad->keymap);
195 input_set_drvdata(input_dev, keypad);
197 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
198 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
200 for (i = 0; i < keymap_data->keymap_size; i++) {
201 unsigned int key = keymap_data->keymap[i];
202 unsigned int row = KEY_ROW(key);
203 unsigned int col = KEY_COL(key);
204 unsigned short keycode = KEY_VAL(key);
205 unsigned int scancode = MATRIX_SCAN_CODE(row, col,
208 keypad->keymap[scancode] = keycode;
209 __set_bit(keycode, input_dev->keybit);
211 __clear_bit(KEY_RESERVED, input_dev->keybit);
214 error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
215 IRQF_DISABLED, pdev->name, keypad);
217 dev_err(&pdev->dev, "failed to request IRQ\n");
221 /* Register the input device */
222 error = input_register_device(input_dev);
224 dev_err(&pdev->dev, "failed to register input device\n");
225 goto failed_free_irq;
228 platform_set_drvdata(pdev, keypad);
234 clk_put(keypad->clk);
236 iounmap(keypad->mmio_base);
238 release_mem_region(res->start, resource_size(res));
240 input_free_device(input_dev);
245 static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
247 struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
248 struct resource *res;
250 free_irq(keypad->irq, pdev);
252 clk_put(keypad->clk);
254 input_unregister_device(keypad->input_dev);
256 iounmap(keypad->mmio_base);
257 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
258 release_mem_region(res->start, resource_size(res));
260 platform_set_drvdata(pdev, NULL);
266 static struct platform_driver w90p910_keypad_driver = {
267 .probe = w90p910_keypad_probe,
268 .remove = __devexit_p(w90p910_keypad_remove),
270 .name = "w90p910-keypad",
271 .owner = THIS_MODULE,
275 static int __init w90p910_keypad_init(void)
277 return platform_driver_register(&w90p910_keypad_driver);
280 static void __exit w90p910_keypad_exit(void)
282 platform_driver_unregister(&w90p910_keypad_driver);
285 module_init(w90p910_keypad_init);
286 module_exit(w90p910_keypad_exit);
288 MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
289 MODULE_DESCRIPTION("w90p910 keypad driver!");
290 MODULE_LICENSE("GPL");
291 MODULE_ALIAS("platform:w90p910-keypad");