blob: 085ede4d972d7265652aefd060cefa8aafaa8937 [file] [log] [blame]
Wan ZongShun4a152352009-08-09 21:22:22 -07001/*
2 * Copyright (c) 2008-2009 Nuvoton technology corporation.
3 *
4 * Wan ZongShun <mcuos.com@gmail.com>
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 as published by
8 * the Free Software Foundation;version 2 of the License.
9 *
10 */
11
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>
21#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Wan ZongShun4a152352009-08-09 21:22:22 -070023
24#include <mach/w90p910_keypad.h>
25
26/* Keypad Interface Control Registers */
27#define KPI_CONF 0x00
28#define KPI_3KCONF 0x04
29#define KPI_LPCONF 0x08
30#define KPI_STATUS 0x0C
31
32#define IS1KEY (0x01 << 16)
33#define INTTR (0x01 << 21)
34#define KEY0R (0x0f << 3)
35#define KEY0C 0x07
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)
41
42#define KGET_RAW(n) (((n) & KEY0R) >> 3)
43#define KGET_COLUMN(n) ((n) & KEY0C)
44
Dmitry Torokhov19328112012-05-10 22:37:08 -070045#define W90P910_NUM_ROWS 8
46#define W90P910_NUM_COLS 8
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070047#define W90P910_ROW_SHIFT 3
Wan ZongShun4a152352009-08-09 21:22:22 -070048
49struct w90p910_keypad {
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070050 const struct w90p910_keypad_platform_data *pdata;
Wan ZongShun4a152352009-08-09 21:22:22 -070051 struct clk *clk;
52 struct input_dev *input_dev;
53 void __iomem *mmio_base;
54 int irq;
Dmitry Torokhov19328112012-05-10 22:37:08 -070055 unsigned short keymap[W90P910_NUM_ROWS * W90P910_NUM_COLS];
Wan ZongShun4a152352009-08-09 21:22:22 -070056};
57
Wan ZongShun4a152352009-08-09 21:22:22 -070058static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
59 unsigned int status)
60{
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070061 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];
Wan ZongShun4a152352009-08-09 21:22:22 -070066
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070067 input_event(input_dev, EV_MSC, MSC_SCAN, code);
68 input_report_key(input_dev, key, 1);
69 input_sync(input_dev);
Wan ZongShun4a152352009-08-09 21:22:22 -070070
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070071 input_event(input_dev, EV_MSC, MSC_SCAN, code);
72 input_report_key(input_dev, key, 0);
73 input_sync(input_dev);
Wan ZongShun4a152352009-08-09 21:22:22 -070074}
75
76static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
77{
78 struct w90p910_keypad *keypad = dev_id;
79 unsigned int kstatus, val;
80
81 kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
82
83 val = INTTR | IS1KEY;
84
85 if (kstatus & val)
86 w90p910_keypad_scan_matrix(keypad, kstatus);
87
88 return IRQ_HANDLED;
89}
90
91static int w90p910_keypad_open(struct input_dev *dev)
92{
93 struct w90p910_keypad *keypad = input_get_drvdata(dev);
Dmitry Torokhov09113ae2009-08-09 21:22:22 -070094 const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
Wan ZongShun4a152352009-08-09 21:22:22 -070095 unsigned int val, config;
96
97 /* Enable unit clock */
98 clk_enable(keypad->clk);
99
100 val = __raw_readl(keypad->mmio_base + KPI_CONF);
101 val |= (KPSEL | ENKP);
102 val &= ~(KSIZE0 | KSIZE1);
103
104 config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
105
106 val |= config;
107
108 __raw_writel(val, keypad->mmio_base + KPI_CONF);
109
110 return 0;
111}
112
113static void w90p910_keypad_close(struct input_dev *dev)
114{
115 struct w90p910_keypad *keypad = input_get_drvdata(dev);
116
117 /* Disable clock unit */
118 clk_disable(keypad->clk);
119}
120
121static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
122{
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700123 const struct w90p910_keypad_platform_data *pdata =
124 pdev->dev.platform_data;
Julia Lawall903b9122009-08-30 11:30:48 -0700125 const struct matrix_keymap_data *keymap_data;
Wan ZongShun4a152352009-08-09 21:22:22 -0700126 struct w90p910_keypad *keypad;
127 struct input_dev *input_dev;
128 struct resource *res;
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700129 int irq;
130 int error;
Wan ZongShun4a152352009-08-09 21:22:22 -0700131
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700132 if (!pdata) {
Wan ZongShun4a152352009-08-09 21:22:22 -0700133 dev_err(&pdev->dev, "no platform data defined\n");
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700134 return -EINVAL;
Wan ZongShun4a152352009-08-09 21:22:22 -0700135 }
136
Julia Lawall903b9122009-08-30 11:30:48 -0700137 keymap_data = pdata->keymap_data;
138
Wan ZongShun4a152352009-08-09 21:22:22 -0700139 irq = platform_get_irq(pdev, 0);
140 if (irq < 0) {
141 dev_err(&pdev->dev, "failed to get keypad irq\n");
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700142 return -ENXIO;
143 }
144
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");
149 error = -ENOMEM;
Wan ZongShun4a152352009-08-09 21:22:22 -0700150 goto failed_free;
151 }
152
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700153 keypad->pdata = pdata;
154 keypad->input_dev = input_dev;
155 keypad->irq = irq;
156
Wan ZongShun4a152352009-08-09 21:22:22 -0700157 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
158 if (res == NULL) {
159 dev_err(&pdev->dev, "failed to get I/O memory\n");
160 error = -ENXIO;
161 goto failed_free;
162 }
163
164 res = request_mem_region(res->start, resource_size(res), pdev->name);
165 if (res == NULL) {
166 dev_err(&pdev->dev, "failed to request I/O memory\n");
167 error = -EBUSY;
168 goto failed_free;
169 }
170
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");
174 error = -ENXIO;
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700175 goto failed_free_res;
Wan ZongShun4a152352009-08-09 21:22:22 -0700176 }
177
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);
182 goto failed_free_io;
183 }
184
Wan ZongShun4a152352009-08-09 21:22:22 -0700185 /* set multi-function pin for w90p910 kpi. */
186 mfp_set_groupi(&pdev->dev);
187
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;
Wan ZongShun4a152352009-08-09 21:22:22 -0700193
Dmitry Torokhov19328112012-05-10 22:37:08 -0700194 error = matrix_keypad_build_keymap(keymap_data, NULL,
195 W90P910_NUM_ROWS, W90P910_NUM_COLS,
196 keypad->keymap, input_dev);
197 if (error) {
198 dev_err(&pdev->dev, "failed to build keymap\n");
199 goto failed_put_clk;
200 }
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700201
202 error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
Yong Zhangec4665c2011-09-07 14:04:16 -0700203 0, pdev->name, keypad);
Wan ZongShun4a152352009-08-09 21:22:22 -0700204 if (error) {
205 dev_err(&pdev->dev, "failed to request IRQ\n");
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700206 goto failed_put_clk;
Wan ZongShun4a152352009-08-09 21:22:22 -0700207 }
208
Dmitry Torokhov19328112012-05-10 22:37:08 -0700209 __set_bit(EV_REP, input_dev->evbit);
210 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
211 input_set_drvdata(input_dev, keypad);
212
Wan ZongShun4a152352009-08-09 21:22:22 -0700213 /* Register the input device */
214 error = input_register_device(input_dev);
215 if (error) {
216 dev_err(&pdev->dev, "failed to register input device\n");
217 goto failed_free_irq;
218 }
219
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700220 platform_set_drvdata(pdev, keypad);
Wan ZongShun4a152352009-08-09 21:22:22 -0700221 return 0;
222
223failed_free_irq:
224 free_irq(irq, pdev);
Wan ZongShun4a152352009-08-09 21:22:22 -0700225failed_put_clk:
226 clk_put(keypad->clk);
227failed_free_io:
228 iounmap(keypad->mmio_base);
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700229failed_free_res:
Wan ZongShun4a152352009-08-09 21:22:22 -0700230 release_mem_region(res->start, resource_size(res));
231failed_free:
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700232 input_free_device(input_dev);
Wan ZongShun4a152352009-08-09 21:22:22 -0700233 kfree(keypad);
234 return error;
235}
236
237static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
238{
239 struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
240 struct resource *res;
241
242 free_irq(keypad->irq, pdev);
243
244 clk_put(keypad->clk);
245
246 input_unregister_device(keypad->input_dev);
247
248 iounmap(keypad->mmio_base);
Wan ZongShun4a152352009-08-09 21:22:22 -0700249 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
250 release_mem_region(res->start, resource_size(res));
251
252 platform_set_drvdata(pdev, NULL);
253 kfree(keypad);
Dmitry Torokhov09113ae2009-08-09 21:22:22 -0700254
Wan ZongShun4a152352009-08-09 21:22:22 -0700255 return 0;
256}
257
258static struct platform_driver w90p910_keypad_driver = {
259 .probe = w90p910_keypad_probe,
260 .remove = __devexit_p(w90p910_keypad_remove),
261 .driver = {
Wan ZongShun1afaab92010-07-18 22:23:19 -0700262 .name = "nuc900-kpi",
Wan ZongShun4a152352009-08-09 21:22:22 -0700263 .owner = THIS_MODULE,
264 },
265};
JJ Ding5146c842011-11-29 11:08:39 -0800266module_platform_driver(w90p910_keypad_driver);
Wan ZongShun4a152352009-08-09 21:22:22 -0700267
268MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
Wan ZongShunfb962e12009-08-20 21:41:03 -0700269MODULE_DESCRIPTION("w90p910 keypad driver");
Wan ZongShun4a152352009-08-09 21:22:22 -0700270MODULE_LICENSE("GPL");
Wan ZongShunfb962e12009-08-20 21:41:03 -0700271MODULE_ALIAS("platform:nuc900-keypad");