Commit | Line | Data |
---|---|---|
1158f0f1 SI |
1 | /* |
2 | * Copyright (C) ST-Ericsson SA 2010 | |
3 | * | |
4 | * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson | |
5 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson | |
6 | * | |
7 | * License terms:GNU General Public License (GPL) version 2 | |
8 | * | |
9 | * Keypad controller driver for the SKE (Scroll Key Encoder) module used in | |
10 | * the Nomadik 8815 and Ux500 platforms. | |
11 | */ | |
12 | ||
13 | #include <linux/platform_device.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/spinlock.h> | |
16 | #include <linux/io.h> | |
17 | #include <linux/delay.h> | |
18 | #include <linux/input.h> | |
19 | #include <linux/slab.h> | |
20 | #include <linux/clk.h> | |
d2d8442d | 21 | #include <linux/module.h> |
1158f0f1 | 22 | |
db298da2 | 23 | #include <linux/platform_data/keypad-nomadik-ske.h> |
1158f0f1 SI |
24 | |
25 | /* SKE_CR bits */ | |
26 | #define SKE_KPMLT (0x1 << 6) | |
27 | #define SKE_KPCN (0x7 << 3) | |
28 | #define SKE_KPASEN (0x1 << 2) | |
29 | #define SKE_KPASON (0x1 << 7) | |
30 | ||
31 | /* SKE_IMSC bits */ | |
32 | #define SKE_KPIMA (0x1 << 2) | |
33 | ||
34 | /* SKE_ICR bits */ | |
35 | #define SKE_KPICS (0x1 << 3) | |
36 | #define SKE_KPICA (0x1 << 2) | |
37 | ||
38 | /* SKE_RIS bits */ | |
39 | #define SKE_KPRISA (0x1 << 2) | |
40 | ||
41 | #define SKE_KEYPAD_ROW_SHIFT 3 | |
1932811f DT |
42 | #define SKE_KPD_NUM_ROWS 8 |
43 | #define SKE_KPD_NUM_COLS 8 | |
1158f0f1 SI |
44 | |
45 | /* keypad auto scan registers */ | |
46 | #define SKE_ASR0 0x20 | |
47 | #define SKE_ASR1 0x24 | |
48 | #define SKE_ASR2 0x28 | |
49 | #define SKE_ASR3 0x2C | |
50 | ||
51 | #define SKE_NUM_ASRX_REGISTERS (4) | |
84b63ad8 | 52 | #define KEY_PRESSED_DELAY 10 |
1158f0f1 SI |
53 | |
54 | /** | |
55 | * struct ske_keypad - data structure used by keypad driver | |
56 | * @irq: irq no | |
57 | * @reg_base: ske regsiters base address | |
58 | * @input: pointer to input device object | |
59 | * @board: keypad platform device | |
60 | * @keymap: matrix scan code table for keycodes | |
61 | * @clk: clock structure pointer | |
62 | */ | |
63 | struct ske_keypad { | |
64 | int irq; | |
65 | void __iomem *reg_base; | |
66 | struct input_dev *input; | |
67 | const struct ske_keypad_platform_data *board; | |
1932811f | 68 | unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS]; |
1158f0f1 | 69 | struct clk *clk; |
d852f959 | 70 | struct clk *pclk; |
1158f0f1 SI |
71 | spinlock_t ske_keypad_lock; |
72 | }; | |
73 | ||
74 | static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr, | |
75 | u8 mask, u8 data) | |
76 | { | |
77 | u32 ret; | |
78 | ||
79 | spin_lock(&keypad->ske_keypad_lock); | |
80 | ||
81 | ret = readl(keypad->reg_base + addr); | |
82 | ret &= ~mask; | |
83 | ret |= data; | |
84 | writel(ret, keypad->reg_base + addr); | |
85 | ||
86 | spin_unlock(&keypad->ske_keypad_lock); | |
87 | } | |
88 | ||
89 | /* | |
90 | * ske_keypad_chip_init: init keypad controller configuration | |
91 | * | |
92 | * Enable Multi key press detection, auto scan mode | |
93 | */ | |
db3dbd09 | 94 | static int __init ske_keypad_chip_init(struct ske_keypad *keypad) |
1158f0f1 SI |
95 | { |
96 | u32 value; | |
84b63ad8 | 97 | int timeout = keypad->board->debounce_ms; |
1158f0f1 SI |
98 | |
99 | /* check SKE_RIS to be 0 */ | |
100 | while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--) | |
101 | cpu_relax(); | |
102 | ||
103 | if (!timeout) | |
104 | return -EINVAL; | |
105 | ||
106 | /* | |
107 | * set debounce value | |
108 | * keypad dbounce is configured in DBCR[15:8] | |
109 | * dbounce value in steps of 32/32.768 ms | |
110 | */ | |
111 | spin_lock(&keypad->ske_keypad_lock); | |
112 | value = readl(keypad->reg_base + SKE_DBCR); | |
113 | value = value & 0xff; | |
114 | value |= ((keypad->board->debounce_ms * 32000)/32768) << 8; | |
115 | writel(value, keypad->reg_base + SKE_DBCR); | |
116 | spin_unlock(&keypad->ske_keypad_lock); | |
117 | ||
118 | /* enable multi key detection */ | |
119 | ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT); | |
120 | ||
121 | /* | |
122 | * set up the number of columns | |
123 | * KPCN[5:3] defines no. of keypad columns to be auto scanned | |
124 | */ | |
125 | value = (keypad->board->kcol - 1) << 3; | |
126 | ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value); | |
127 | ||
128 | /* clear keypad interrupt for auto(and pending SW) scans */ | |
129 | ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS); | |
130 | ||
131 | /* un-mask keypad interrupts */ | |
132 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); | |
133 | ||
134 | /* enable automatic scan */ | |
135 | ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN); | |
136 | ||
137 | return 0; | |
138 | } | |
139 | ||
af77c88b | 140 | static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col) |
1158f0f1 | 141 | { |
af77c88b | 142 | int row = 0, code, pos; |
1158f0f1 | 143 | struct input_dev *input = keypad->input; |
af77c88b NKG |
144 | u32 ske_ris; |
145 | int key_pressed; | |
146 | int num_of_rows; | |
147 | ||
148 | /* find out the row */ | |
149 | num_of_rows = hweight8(status); | |
150 | do { | |
151 | pos = __ffs(status); | |
152 | row = pos; | |
153 | status &= ~(1 << pos); | |
154 | ||
155 | code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT); | |
156 | ske_ris = readl(keypad->reg_base + SKE_RIS); | |
157 | key_pressed = ske_ris & SKE_KPRISA; | |
158 | ||
159 | input_event(input, EV_MSC, MSC_SCAN, code); | |
160 | input_report_key(input, keypad->keymap[code], key_pressed); | |
161 | input_sync(input); | |
162 | num_of_rows--; | |
163 | } while (num_of_rows); | |
164 | } | |
165 | ||
166 | static void ske_keypad_read_data(struct ske_keypad *keypad) | |
167 | { | |
168 | u8 status; | |
169 | int col = 0; | |
170 | int ske_asr, i; | |
1158f0f1 SI |
171 | |
172 | /* | |
173 | * Read the auto scan registers | |
174 | * | |
175 | * Each SKE_ASRx (x=0 to x=3) contains two row values. | |
176 | * lower byte contains row value for column 2*x, | |
177 | * upper byte contains row value for column 2*x + 1 | |
178 | */ | |
179 | for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) { | |
180 | ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i)); | |
181 | if (!ske_asr) | |
182 | continue; | |
183 | ||
af77c88b NKG |
184 | /* now that ASRx is zero, find out the coloumn x and row y */ |
185 | status = ske_asr & 0xff; | |
186 | if (status) { | |
1158f0f1 | 187 | col = i * 2; |
af77c88b NKG |
188 | ske_keypad_report(keypad, status, col); |
189 | } | |
190 | status = (ske_asr & 0xff00) >> 8; | |
191 | if (status) { | |
1158f0f1 | 192 | col = (i * 2) + 1; |
af77c88b | 193 | ske_keypad_report(keypad, status, col); |
1158f0f1 | 194 | } |
1158f0f1 SI |
195 | } |
196 | } | |
197 | ||
198 | static irqreturn_t ske_keypad_irq(int irq, void *dev_id) | |
199 | { | |
200 | struct ske_keypad *keypad = dev_id; | |
84b63ad8 | 201 | int timeout = keypad->board->debounce_ms; |
1158f0f1 SI |
202 | |
203 | /* disable auto scan interrupt; mask the interrupt generated */ | |
204 | ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0); | |
205 | ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA); | |
206 | ||
84b63ad8 | 207 | while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --timeout) |
af77c88b | 208 | cpu_relax(); |
1158f0f1 | 209 | |
af77c88b NKG |
210 | /* SKEx registers are stable and can be read */ |
211 | ske_keypad_read_data(keypad); | |
1158f0f1 | 212 | |
84b63ad8 NKG |
213 | /* wait until raw interrupt is clear */ |
214 | while ((readl(keypad->reg_base + SKE_RIS)) && --timeout) | |
215 | msleep(KEY_PRESSED_DELAY); | |
216 | ||
1158f0f1 SI |
217 | /* enable auto scan interrupts */ |
218 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); | |
219 | ||
220 | return IRQ_HANDLED; | |
221 | } | |
222 | ||
db3dbd09 | 223 | static int __init ske_keypad_probe(struct platform_device *pdev) |
1158f0f1 SI |
224 | { |
225 | const struct ske_keypad_platform_data *plat = pdev->dev.platform_data; | |
226 | struct ske_keypad *keypad; | |
227 | struct input_dev *input; | |
228 | struct resource *res; | |
229 | int irq; | |
230 | int error; | |
231 | ||
232 | if (!plat) { | |
233 | dev_err(&pdev->dev, "invalid keypad platform data\n"); | |
234 | return -EINVAL; | |
235 | } | |
236 | ||
237 | irq = platform_get_irq(pdev, 0); | |
238 | if (irq < 0) { | |
239 | dev_err(&pdev->dev, "failed to get keypad irq\n"); | |
240 | return -EINVAL; | |
241 | } | |
242 | ||
243 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
244 | if (!res) { | |
245 | dev_err(&pdev->dev, "missing platform resources\n"); | |
246 | return -EINVAL; | |
247 | } | |
248 | ||
249 | keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL); | |
250 | input = input_allocate_device(); | |
251 | if (!keypad || !input) { | |
252 | dev_err(&pdev->dev, "failed to allocate keypad memory\n"); | |
253 | error = -ENOMEM; | |
254 | goto err_free_mem; | |
255 | } | |
256 | ||
257 | keypad->irq = irq; | |
258 | keypad->board = plat; | |
259 | keypad->input = input; | |
260 | spin_lock_init(&keypad->ske_keypad_lock); | |
261 | ||
262 | if (!request_mem_region(res->start, resource_size(res), pdev->name)) { | |
263 | dev_err(&pdev->dev, "failed to request I/O memory\n"); | |
264 | error = -EBUSY; | |
265 | goto err_free_mem; | |
266 | } | |
267 | ||
268 | keypad->reg_base = ioremap(res->start, resource_size(res)); | |
269 | if (!keypad->reg_base) { | |
270 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); | |
271 | error = -ENXIO; | |
272 | goto err_free_mem_region; | |
273 | } | |
274 | ||
d852f959 UH |
275 | keypad->pclk = clk_get(&pdev->dev, "apb_pclk"); |
276 | if (IS_ERR(keypad->pclk)) { | |
277 | dev_err(&pdev->dev, "failed to get pclk\n"); | |
278 | error = PTR_ERR(keypad->pclk); | |
279 | goto err_iounmap; | |
280 | } | |
281 | ||
1158f0f1 SI |
282 | keypad->clk = clk_get(&pdev->dev, NULL); |
283 | if (IS_ERR(keypad->clk)) { | |
284 | dev_err(&pdev->dev, "failed to get clk\n"); | |
285 | error = PTR_ERR(keypad->clk); | |
d852f959 | 286 | goto err_pclk; |
1158f0f1 SI |
287 | } |
288 | ||
289 | input->id.bustype = BUS_HOST; | |
290 | input->name = "ux500-ske-keypad"; | |
291 | input->dev.parent = &pdev->dev; | |
292 | ||
1932811f DT |
293 | error = matrix_keypad_build_keymap(plat->keymap_data, NULL, |
294 | SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS, | |
295 | keypad->keymap, input); | |
296 | if (error) { | |
297 | dev_err(&pdev->dev, "Failed to build keymap\n"); | |
4e99aab7 | 298 | goto err_clk; |
1932811f | 299 | } |
1158f0f1 SI |
300 | |
301 | input_set_capability(input, EV_MSC, MSC_SCAN); | |
1158f0f1 SI |
302 | if (!plat->no_autorepeat) |
303 | __set_bit(EV_REP, input->evbit); | |
304 | ||
d852f959 UH |
305 | error = clk_prepare_enable(keypad->pclk); |
306 | if (error) { | |
307 | dev_err(&pdev->dev, "Failed to prepare/enable pclk\n"); | |
308 | goto err_clk; | |
309 | } | |
310 | ||
4e99aab7 UH |
311 | error = clk_prepare_enable(keypad->clk); |
312 | if (error) { | |
313 | dev_err(&pdev->dev, "Failed to prepare/enable clk\n"); | |
d852f959 | 314 | goto err_pclk_disable; |
4e99aab7 UH |
315 | } |
316 | ||
1158f0f1 SI |
317 | |
318 | /* go through board initialization helpers */ | |
319 | if (keypad->board->init) | |
320 | keypad->board->init(); | |
321 | ||
322 | error = ske_keypad_chip_init(keypad); | |
323 | if (error) { | |
324 | dev_err(&pdev->dev, "unable to init keypad hardware\n"); | |
325 | goto err_clk_disable; | |
326 | } | |
327 | ||
328 | error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq, | |
329 | IRQF_ONESHOT, "ske-keypad", keypad); | |
330 | if (error) { | |
331 | dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq); | |
332 | goto err_clk_disable; | |
333 | } | |
334 | ||
335 | error = input_register_device(input); | |
336 | if (error) { | |
337 | dev_err(&pdev->dev, | |
338 | "unable to register input device: %d\n", error); | |
339 | goto err_free_irq; | |
340 | } | |
341 | ||
342 | if (plat->wakeup_enable) | |
343 | device_init_wakeup(&pdev->dev, true); | |
344 | ||
345 | platform_set_drvdata(pdev, keypad); | |
346 | ||
347 | return 0; | |
348 | ||
349 | err_free_irq: | |
350 | free_irq(keypad->irq, keypad); | |
351 | err_clk_disable: | |
4e99aab7 | 352 | clk_disable_unprepare(keypad->clk); |
d852f959 UH |
353 | err_pclk_disable: |
354 | clk_disable_unprepare(keypad->pclk); | |
4e99aab7 | 355 | err_clk: |
1158f0f1 | 356 | clk_put(keypad->clk); |
d852f959 UH |
357 | err_pclk: |
358 | clk_put(keypad->pclk); | |
1158f0f1 SI |
359 | err_iounmap: |
360 | iounmap(keypad->reg_base); | |
361 | err_free_mem_region: | |
362 | release_mem_region(res->start, resource_size(res)); | |
363 | err_free_mem: | |
364 | input_free_device(input); | |
365 | kfree(keypad); | |
366 | return error; | |
367 | } | |
368 | ||
369 | static int __devexit ske_keypad_remove(struct platform_device *pdev) | |
370 | { | |
371 | struct ske_keypad *keypad = platform_get_drvdata(pdev); | |
372 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
373 | ||
374 | free_irq(keypad->irq, keypad); | |
375 | ||
376 | input_unregister_device(keypad->input); | |
377 | ||
4e99aab7 | 378 | clk_disable_unprepare(keypad->clk); |
1158f0f1 SI |
379 | clk_put(keypad->clk); |
380 | ||
381 | if (keypad->board->exit) | |
382 | keypad->board->exit(); | |
383 | ||
384 | iounmap(keypad->reg_base); | |
385 | release_mem_region(res->start, resource_size(res)); | |
386 | kfree(keypad); | |
387 | ||
388 | return 0; | |
389 | } | |
390 | ||
89f0f170 | 391 | #ifdef CONFIG_PM_SLEEP |
1158f0f1 SI |
392 | static int ske_keypad_suspend(struct device *dev) |
393 | { | |
394 | struct platform_device *pdev = to_platform_device(dev); | |
395 | struct ske_keypad *keypad = platform_get_drvdata(pdev); | |
396 | int irq = platform_get_irq(pdev, 0); | |
397 | ||
398 | if (device_may_wakeup(dev)) | |
399 | enable_irq_wake(irq); | |
400 | else | |
401 | ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0); | |
402 | ||
403 | return 0; | |
404 | } | |
405 | ||
406 | static int ske_keypad_resume(struct device *dev) | |
407 | { | |
408 | struct platform_device *pdev = to_platform_device(dev); | |
409 | struct ske_keypad *keypad = platform_get_drvdata(pdev); | |
410 | int irq = platform_get_irq(pdev, 0); | |
411 | ||
412 | if (device_may_wakeup(dev)) | |
413 | disable_irq_wake(irq); | |
414 | else | |
415 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); | |
416 | ||
417 | return 0; | |
418 | } | |
1158f0f1 SI |
419 | #endif |
420 | ||
89f0f170 DT |
421 | static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops, |
422 | ske_keypad_suspend, ske_keypad_resume); | |
423 | ||
aeec0517 | 424 | static struct platform_driver ske_keypad_driver = { |
1158f0f1 SI |
425 | .driver = { |
426 | .name = "nmk-ske-keypad", | |
427 | .owner = THIS_MODULE, | |
1158f0f1 | 428 | .pm = &ske_keypad_dev_pm_ops, |
1158f0f1 | 429 | }, |
1cb0aa88 | 430 | .remove = ske_keypad_remove, |
1158f0f1 | 431 | }; |
d3d25808 DT |
432 | |
433 | static int __init ske_keypad_init(void) | |
434 | { | |
435 | return platform_driver_probe(&ske_keypad_driver, ske_keypad_probe); | |
436 | } | |
437 | module_init(ske_keypad_init); | |
438 | ||
439 | static void __exit ske_keypad_exit(void) | |
440 | { | |
441 | platform_driver_unregister(&ske_keypad_driver); | |
442 | } | |
443 | module_exit(ske_keypad_exit); | |
1158f0f1 SI |
444 | |
445 | MODULE_LICENSE("GPL v2"); | |
446 | MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>"); | |
447 | MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver"); | |
448 | MODULE_ALIAS("platform:nomadik-ske-keypad"); |