Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / input / keyboard / nomadik-ske-keypad.c
blobfcdec5e2b2977269b8718e84bf6da0991b48bb9c
1 /*
2 * Copyright (C) ST-Ericsson SA 2010
4 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
5 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 * License terms:GNU General Public License (GPL) version 2
9 * Keypad controller driver for the SKE (Scroll Key Encoder) module used in
10 * the Nomadik 8815 and Ux500 platforms.
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>
21 #include <linux/module.h>
23 #include <plat/ske.h>
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)
31 /* SKE_IMSC bits */
32 #define SKE_KPIMA (0x1 << 2)
34 /* SKE_ICR bits */
35 #define SKE_KPICS (0x1 << 3)
36 #define SKE_KPICA (0x1 << 2)
38 /* SKE_RIS bits */
39 #define SKE_KPRISA (0x1 << 2)
41 #define SKE_KEYPAD_ROW_SHIFT 3
42 #define SKE_KPD_KEYMAP_SIZE (8 * 8)
44 /* keypad auto scan registers */
45 #define SKE_ASR0 0x20
46 #define SKE_ASR1 0x24
47 #define SKE_ASR2 0x28
48 #define SKE_ASR3 0x2C
50 #define SKE_NUM_ASRX_REGISTERS (4)
52 /**
53 * struct ske_keypad - data structure used by keypad driver
54 * @irq: irq no
55 * @reg_base: ske regsiters base address
56 * @input: pointer to input device object
57 * @board: keypad platform device
58 * @keymap: matrix scan code table for keycodes
59 * @clk: clock structure pointer
61 struct ske_keypad {
62 int irq;
63 void __iomem *reg_base;
64 struct input_dev *input;
65 const struct ske_keypad_platform_data *board;
66 unsigned short keymap[SKE_KPD_KEYMAP_SIZE];
67 struct clk *clk;
68 spinlock_t ske_keypad_lock;
71 static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
72 u8 mask, u8 data)
74 u32 ret;
76 spin_lock(&keypad->ske_keypad_lock);
78 ret = readl(keypad->reg_base + addr);
79 ret &= ~mask;
80 ret |= data;
81 writel(ret, keypad->reg_base + addr);
83 spin_unlock(&keypad->ske_keypad_lock);
87 * ske_keypad_chip_init: init keypad controller configuration
89 * Enable Multi key press detection, auto scan mode
91 static int __devinit ske_keypad_chip_init(struct ske_keypad *keypad)
93 u32 value;
94 int timeout = 50;
96 /* check SKE_RIS to be 0 */
97 while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
98 cpu_relax();
100 if (!timeout)
101 return -EINVAL;
104 * set debounce value
105 * keypad dbounce is configured in DBCR[15:8]
106 * dbounce value in steps of 32/32.768 ms
108 spin_lock(&keypad->ske_keypad_lock);
109 value = readl(keypad->reg_base + SKE_DBCR);
110 value = value & 0xff;
111 value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
112 writel(value, keypad->reg_base + SKE_DBCR);
113 spin_unlock(&keypad->ske_keypad_lock);
115 /* enable multi key detection */
116 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
119 * set up the number of columns
120 * KPCN[5:3] defines no. of keypad columns to be auto scanned
122 value = (keypad->board->kcol - 1) << 3;
123 ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
125 /* clear keypad interrupt for auto(and pending SW) scans */
126 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
128 /* un-mask keypad interrupts */
129 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
131 /* enable automatic scan */
132 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
134 return 0;
137 static void ske_keypad_read_data(struct ske_keypad *keypad)
139 struct input_dev *input = keypad->input;
140 u16 status;
141 int col = 0, row = 0, code;
142 int ske_asr, ske_ris, key_pressed, i;
145 * Read the auto scan registers
147 * Each SKE_ASRx (x=0 to x=3) contains two row values.
148 * lower byte contains row value for column 2*x,
149 * upper byte contains row value for column 2*x + 1
151 for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
152 ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
153 if (!ske_asr)
154 continue;
156 /* now that ASRx is zero, find out the column x and row y*/
157 if (ske_asr & 0xff) {
158 col = i * 2;
159 status = ske_asr & 0xff;
160 } else {
161 col = (i * 2) + 1;
162 status = (ske_asr & 0xff00) >> 8;
165 /* find out the row */
166 row = __ffs(status);
168 code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
169 ske_ris = readl(keypad->reg_base + SKE_RIS);
170 key_pressed = ske_ris & SKE_KPRISA;
172 input_event(input, EV_MSC, MSC_SCAN, code);
173 input_report_key(input, keypad->keymap[code], key_pressed);
174 input_sync(input);
178 static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
180 struct ske_keypad *keypad = dev_id;
181 int retries = 20;
183 /* disable auto scan interrupt; mask the interrupt generated */
184 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
185 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
187 while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --retries)
188 msleep(5);
190 if (retries) {
191 /* SKEx registers are stable and can be read */
192 ske_keypad_read_data(keypad);
195 /* enable auto scan interrupts */
196 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
198 return IRQ_HANDLED;
201 static int __devinit ske_keypad_probe(struct platform_device *pdev)
203 const struct ske_keypad_platform_data *plat = pdev->dev.platform_data;
204 struct ske_keypad *keypad;
205 struct input_dev *input;
206 struct resource *res;
207 int irq;
208 int error;
210 if (!plat) {
211 dev_err(&pdev->dev, "invalid keypad platform data\n");
212 return -EINVAL;
215 irq = platform_get_irq(pdev, 0);
216 if (irq < 0) {
217 dev_err(&pdev->dev, "failed to get keypad irq\n");
218 return -EINVAL;
221 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
222 if (!res) {
223 dev_err(&pdev->dev, "missing platform resources\n");
224 return -EINVAL;
227 keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
228 input = input_allocate_device();
229 if (!keypad || !input) {
230 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
231 error = -ENOMEM;
232 goto err_free_mem;
235 keypad->irq = irq;
236 keypad->board = plat;
237 keypad->input = input;
238 spin_lock_init(&keypad->ske_keypad_lock);
240 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
241 dev_err(&pdev->dev, "failed to request I/O memory\n");
242 error = -EBUSY;
243 goto err_free_mem;
246 keypad->reg_base = ioremap(res->start, resource_size(res));
247 if (!keypad->reg_base) {
248 dev_err(&pdev->dev, "failed to remap I/O memory\n");
249 error = -ENXIO;
250 goto err_free_mem_region;
253 keypad->clk = clk_get(&pdev->dev, NULL);
254 if (IS_ERR(keypad->clk)) {
255 dev_err(&pdev->dev, "failed to get clk\n");
256 error = PTR_ERR(keypad->clk);
257 goto err_iounmap;
260 input->id.bustype = BUS_HOST;
261 input->name = "ux500-ske-keypad";
262 input->dev.parent = &pdev->dev;
264 input->keycode = keypad->keymap;
265 input->keycodesize = sizeof(keypad->keymap[0]);
266 input->keycodemax = ARRAY_SIZE(keypad->keymap);
268 input_set_capability(input, EV_MSC, MSC_SCAN);
270 __set_bit(EV_KEY, input->evbit);
271 if (!plat->no_autorepeat)
272 __set_bit(EV_REP, input->evbit);
274 matrix_keypad_build_keymap(plat->keymap_data, SKE_KEYPAD_ROW_SHIFT,
275 input->keycode, input->keybit);
277 clk_enable(keypad->clk);
279 /* go through board initialization helpers */
280 if (keypad->board->init)
281 keypad->board->init();
283 error = ske_keypad_chip_init(keypad);
284 if (error) {
285 dev_err(&pdev->dev, "unable to init keypad hardware\n");
286 goto err_clk_disable;
289 error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
290 IRQF_ONESHOT, "ske-keypad", keypad);
291 if (error) {
292 dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
293 goto err_clk_disable;
296 error = input_register_device(input);
297 if (error) {
298 dev_err(&pdev->dev,
299 "unable to register input device: %d\n", error);
300 goto err_free_irq;
303 if (plat->wakeup_enable)
304 device_init_wakeup(&pdev->dev, true);
306 platform_set_drvdata(pdev, keypad);
308 return 0;
310 err_free_irq:
311 free_irq(keypad->irq, keypad);
312 err_clk_disable:
313 clk_disable(keypad->clk);
314 clk_put(keypad->clk);
315 err_iounmap:
316 iounmap(keypad->reg_base);
317 err_free_mem_region:
318 release_mem_region(res->start, resource_size(res));
319 err_free_mem:
320 input_free_device(input);
321 kfree(keypad);
322 return error;
325 static int __devexit ske_keypad_remove(struct platform_device *pdev)
327 struct ske_keypad *keypad = platform_get_drvdata(pdev);
328 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
330 free_irq(keypad->irq, keypad);
332 input_unregister_device(keypad->input);
334 clk_disable(keypad->clk);
335 clk_put(keypad->clk);
337 if (keypad->board->exit)
338 keypad->board->exit();
340 iounmap(keypad->reg_base);
341 release_mem_region(res->start, resource_size(res));
342 kfree(keypad);
344 return 0;
347 #ifdef CONFIG_PM
348 static int ske_keypad_suspend(struct device *dev)
350 struct platform_device *pdev = to_platform_device(dev);
351 struct ske_keypad *keypad = platform_get_drvdata(pdev);
352 int irq = platform_get_irq(pdev, 0);
354 if (device_may_wakeup(dev))
355 enable_irq_wake(irq);
356 else
357 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
359 return 0;
362 static int ske_keypad_resume(struct device *dev)
364 struct platform_device *pdev = to_platform_device(dev);
365 struct ske_keypad *keypad = platform_get_drvdata(pdev);
366 int irq = platform_get_irq(pdev, 0);
368 if (device_may_wakeup(dev))
369 disable_irq_wake(irq);
370 else
371 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
373 return 0;
376 static const struct dev_pm_ops ske_keypad_dev_pm_ops = {
377 .suspend = ske_keypad_suspend,
378 .resume = ske_keypad_resume,
380 #endif
382 struct platform_driver ske_keypad_driver = {
383 .driver = {
384 .name = "nmk-ske-keypad",
385 .owner = THIS_MODULE,
386 #ifdef CONFIG_PM
387 .pm = &ske_keypad_dev_pm_ops,
388 #endif
390 .probe = ske_keypad_probe,
391 .remove = __devexit_p(ske_keypad_remove),
394 static int __init ske_keypad_init(void)
396 return platform_driver_probe(&ske_keypad_driver, ske_keypad_probe);
398 module_init(ske_keypad_init);
400 static void __exit ske_keypad_exit(void)
402 platform_driver_unregister(&ske_keypad_driver);
404 module_exit(ske_keypad_exit);
406 MODULE_LICENSE("GPL v2");
407 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
408 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
409 MODULE_ALIAS("platform:nomadik-ske-keypad");