1
2
3
4
5
6
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/interrupt.h>
11#include <linux/input.h>
12#include <linux/device.h>
13#include <linux/platform_device.h>
14#include <linux/clk.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/slab.h>
18
19#include <linux/platform_data/keypad-w90p910.h>
20
21
22#define KPI_CONF 0x00
23#define KPI_3KCONF 0x04
24#define KPI_LPCONF 0x08
25#define KPI_STATUS 0x0C
26
27#define IS1KEY (0x01 << 16)
28#define INTTR (0x01 << 21)
29#define KEY0R (0x0f << 3)
30#define KEY0C 0x07
31#define DEBOUNCE_BIT 0x08
32#define KSIZE0 (0x01 << 16)
33#define KSIZE1 (0x01 << 17)
34#define KPSEL (0x01 << 19)
35#define ENKP (0x01 << 18)
36
37#define KGET_RAW(n) (((n) & KEY0R) >> 3)
38#define KGET_COLUMN(n) ((n) & KEY0C)
39
40#define W90P910_NUM_ROWS 8
41#define W90P910_NUM_COLS 8
42#define W90P910_ROW_SHIFT 3
43
44struct w90p910_keypad {
45 const struct w90p910_keypad_platform_data *pdata;
46 struct clk *clk;
47 struct input_dev *input_dev;
48 void __iomem *mmio_base;
49 int irq;
50 unsigned short keymap[W90P910_NUM_ROWS * W90P910_NUM_COLS];
51};
52
53static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
54 unsigned int status)
55{
56 struct input_dev *input_dev = keypad->input_dev;
57 unsigned int row = KGET_RAW(status);
58 unsigned int col = KGET_COLUMN(status);
59 unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
60 unsigned int key = keypad->keymap[code];
61
62 input_event(input_dev, EV_MSC, MSC_SCAN, code);
63 input_report_key(input_dev, key, 1);
64 input_sync(input_dev);
65
66 input_event(input_dev, EV_MSC, MSC_SCAN, code);
67 input_report_key(input_dev, key, 0);
68 input_sync(input_dev);
69}
70
71static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
72{
73 struct w90p910_keypad *keypad = dev_id;
74 unsigned int kstatus, val;
75
76 kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
77
78 val = INTTR | IS1KEY;
79
80 if (kstatus & val)
81 w90p910_keypad_scan_matrix(keypad, kstatus);
82
83 return IRQ_HANDLED;
84}
85
86static int w90p910_keypad_open(struct input_dev *dev)
87{
88 struct w90p910_keypad *keypad = input_get_drvdata(dev);
89 const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
90 unsigned int val, config;
91
92
93 clk_enable(keypad->clk);
94
95 val = __raw_readl(keypad->mmio_base + KPI_CONF);
96 val |= (KPSEL | ENKP);
97 val &= ~(KSIZE0 | KSIZE1);
98
99 config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
100
101 val |= config;
102
103 __raw_writel(val, keypad->mmio_base + KPI_CONF);
104
105 return 0;
106}
107
108static void w90p910_keypad_close(struct input_dev *dev)
109{
110 struct w90p910_keypad *keypad = input_get_drvdata(dev);
111
112
113 clk_disable(keypad->clk);
114}
115
116static int w90p910_keypad_probe(struct platform_device *pdev)
117{
118 const struct w90p910_keypad_platform_data *pdata =
119 dev_get_platdata(&pdev->dev);
120 const struct matrix_keymap_data *keymap_data;
121 struct w90p910_keypad *keypad;
122 struct input_dev *input_dev;
123 struct resource *res;
124 int irq;
125 int error;
126
127 if (!pdata) {
128 dev_err(&pdev->dev, "no platform data defined\n");
129 return -EINVAL;
130 }
131
132 keymap_data = pdata->keymap_data;
133
134 irq = platform_get_irq(pdev, 0);
135 if (irq < 0) {
136 dev_err(&pdev->dev, "failed to get keypad irq\n");
137 return -ENXIO;
138 }
139
140 keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
141 input_dev = input_allocate_device();
142 if (!keypad || !input_dev) {
143 dev_err(&pdev->dev, "failed to allocate driver data\n");
144 error = -ENOMEM;
145 goto failed_free;
146 }
147
148 keypad->pdata = pdata;
149 keypad->input_dev = input_dev;
150 keypad->irq = irq;
151
152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
153 if (res == NULL) {
154 dev_err(&pdev->dev, "failed to get I/O memory\n");
155 error = -ENXIO;
156 goto failed_free;
157 }
158
159 res = request_mem_region(res->start, resource_size(res), pdev->name);
160 if (res == NULL) {
161 dev_err(&pdev->dev, "failed to request I/O memory\n");
162 error = -EBUSY;
163 goto failed_free;
164 }
165
166 keypad->mmio_base = ioremap(res->start, resource_size(res));
167 if (keypad->mmio_base == NULL) {
168 dev_err(&pdev->dev, "failed to remap I/O memory\n");
169 error = -ENXIO;
170 goto failed_free_res;
171 }
172
173 keypad->clk = clk_get(&pdev->dev, NULL);
174 if (IS_ERR(keypad->clk)) {
175 dev_err(&pdev->dev, "failed to get keypad clock\n");
176 error = PTR_ERR(keypad->clk);
177 goto failed_free_io;
178 }
179
180
181 mfp_set_groupi(&pdev->dev);
182
183 input_dev->name = pdev->name;
184 input_dev->id.bustype = BUS_HOST;
185 input_dev->open = w90p910_keypad_open;
186 input_dev->close = w90p910_keypad_close;
187 input_dev->dev.parent = &pdev->dev;
188
189 error = matrix_keypad_build_keymap(keymap_data, NULL,
190 W90P910_NUM_ROWS, W90P910_NUM_COLS,
191 keypad->keymap, input_dev);
192 if (error) {
193 dev_err(&pdev->dev, "failed to build keymap\n");
194 goto failed_put_clk;
195 }
196
197 error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
198 0, pdev->name, keypad);
199 if (error) {
200 dev_err(&pdev->dev, "failed to request IRQ\n");
201 goto failed_put_clk;
202 }
203
204 __set_bit(EV_REP, input_dev->evbit);
205 input_set_capability(input_dev, EV_MSC, MSC_SCAN);
206 input_set_drvdata(input_dev, keypad);
207
208
209 error = input_register_device(input_dev);
210 if (error) {
211 dev_err(&pdev->dev, "failed to register input device\n");
212 goto failed_free_irq;
213 }
214
215 platform_set_drvdata(pdev, keypad);
216 return 0;
217
218failed_free_irq:
219 free_irq(irq, keypad);
220failed_put_clk:
221 clk_put(keypad->clk);
222failed_free_io:
223 iounmap(keypad->mmio_base);
224failed_free_res:
225 release_mem_region(res->start, resource_size(res));
226failed_free:
227 input_free_device(input_dev);
228 kfree(keypad);
229 return error;
230}
231
232static int w90p910_keypad_remove(struct platform_device *pdev)
233{
234 struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
235 struct resource *res;
236
237 free_irq(keypad->irq, keypad);
238
239 clk_put(keypad->clk);
240
241 input_unregister_device(keypad->input_dev);
242
243 iounmap(keypad->mmio_base);
244 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
245 release_mem_region(res->start, resource_size(res));
246
247 kfree(keypad);
248
249 return 0;
250}
251
252static struct platform_driver w90p910_keypad_driver = {
253 .probe = w90p910_keypad_probe,
254 .remove = w90p910_keypad_remove,
255 .driver = {
256 .name = "nuc900-kpi",
257 },
258};
259module_platform_driver(w90p910_keypad_driver);
260
261MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
262MODULE_DESCRIPTION("w90p910 keypad driver");
263MODULE_LICENSE("GPL");
264MODULE_ALIAS("platform:nuc900-keypad");
265