MIPS: Yosemite, Emma: Fix off-by-two in arcs_cmdline buffer size check
[linux-2.6/linux-mips.git] / drivers / input / keyboard / nomadik-ske-keypad.c
blob6e0f230913608a97ad3b8fa85449ef97a33da7d8
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>
22 #include <plat/ske.h>
24 /* SKE_CR bits */
25 #define SKE_KPMLT (0x1 << 6)
26 #define SKE_KPCN (0x7 << 3)
27 #define SKE_KPASEN (0x1 << 2)
28 #define SKE_KPASON (0x1 << 7)
30 /* SKE_IMSC bits */
31 #define SKE_KPIMA (0x1 << 2)
33 /* SKE_ICR bits */
34 #define SKE_KPICS (0x1 << 3)
35 #define SKE_KPICA (0x1 << 2)
37 /* SKE_RIS bits */
38 #define SKE_KPRISA (0x1 << 2)
40 #define SKE_KEYPAD_ROW_SHIFT 3
41 #define SKE_KPD_KEYMAP_SIZE (8 * 8)
43 /* keypad auto scan registers */
44 #define SKE_ASR0 0x20
45 #define SKE_ASR1 0x24
46 #define SKE_ASR2 0x28
47 #define SKE_ASR3 0x2C
49 #define SKE_NUM_ASRX_REGISTERS (4)
51 /**
52 * struct ske_keypad - data structure used by keypad driver
53 * @irq: irq no
54 * @reg_base: ske regsiters base address
55 * @input: pointer to input device object
56 * @board: keypad platform device
57 * @keymap: matrix scan code table for keycodes
58 * @clk: clock structure pointer
60 struct ske_keypad {
61 int irq;
62 void __iomem *reg_base;
63 struct input_dev *input;
64 const struct ske_keypad_platform_data *board;
65 unsigned short keymap[SKE_KPD_KEYMAP_SIZE];
66 struct clk *clk;
67 spinlock_t ske_keypad_lock;
70 static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
71 u8 mask, u8 data)
73 u32 ret;
75 spin_lock(&keypad->ske_keypad_lock);
77 ret = readl(keypad->reg_base + addr);
78 ret &= ~mask;
79 ret |= data;
80 writel(ret, keypad->reg_base + addr);
82 spin_unlock(&keypad->ske_keypad_lock);
86 * ske_keypad_chip_init: init keypad controller configuration
88 * Enable Multi key press detection, auto scan mode
90 static int __devinit ske_keypad_chip_init(struct ske_keypad *keypad)
92 u32 value;
93 int timeout = 50;
95 /* check SKE_RIS to be 0 */
96 while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
97 cpu_relax();
99 if (!timeout)
100 return -EINVAL;
103 * set debounce value
104 * keypad dbounce is configured in DBCR[15:8]
105 * dbounce value in steps of 32/32.768 ms
107 spin_lock(&keypad->ske_keypad_lock);
108 value = readl(keypad->reg_base + SKE_DBCR);
109 value = value & 0xff;
110 value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
111 writel(value, keypad->reg_base + SKE_DBCR);
112 spin_unlock(&keypad->ske_keypad_lock);
114 /* enable multi key detection */
115 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
118 * set up the number of columns
119 * KPCN[5:3] defines no. of keypad columns to be auto scanned
121 value = (keypad->board->kcol - 1) << 3;
122 ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
124 /* clear keypad interrupt for auto(and pending SW) scans */
125 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
127 /* un-mask keypad interrupts */
128 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
130 /* enable automatic scan */
131 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
133 return 0;
136 static void ske_keypad_read_data(struct ske_keypad *keypad)
138 struct input_dev *input = keypad->input;
139 u16 status;
140 int col = 0, row = 0, code;
141 int ske_asr, ske_ris, key_pressed, i;
144 * Read the auto scan registers
146 * Each SKE_ASRx (x=0 to x=3) contains two row values.
147 * lower byte contains row value for column 2*x,
148 * upper byte contains row value for column 2*x + 1
150 for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
151 ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
152 if (!ske_asr)
153 continue;
155 /* now that ASRx is zero, find out the column x and row y*/
156 if (ske_asr & 0xff) {
157 col = i * 2;
158 status = ske_asr & 0xff;
159 } else {
160 col = (i * 2) + 1;
161 status = (ske_asr & 0xff00) >> 8;
164 /* find out the row */
165 row = __ffs(status);
167 code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
168 ske_ris = readl(keypad->reg_base + SKE_RIS);
169 key_pressed = ske_ris & SKE_KPRISA;
171 input_event(input, EV_MSC, MSC_SCAN, code);
172 input_report_key(input, keypad->keymap[code], key_pressed);
173 input_sync(input);
177 static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
179 struct ske_keypad *keypad = dev_id;
180 int retries = 20;
182 /* disable auto scan interrupt; mask the interrupt generated */
183 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
184 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
186 while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --retries)
187 msleep(5);
189 if (retries) {
190 /* SKEx registers are stable and can be read */
191 ske_keypad_read_data(keypad);
194 /* enable auto scan interrupts */
195 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
197 return IRQ_HANDLED;
200 static int __devinit ske_keypad_probe(struct platform_device *pdev)
202 const struct ske_keypad_platform_data *plat = pdev->dev.platform_data;
203 struct ske_keypad *keypad;
204 struct input_dev *input;
205 struct resource *res;
206 int irq;
207 int error;
209 if (!plat) {
210 dev_err(&pdev->dev, "invalid keypad platform data\n");
211 return -EINVAL;
214 irq = platform_get_irq(pdev, 0);
215 if (irq < 0) {
216 dev_err(&pdev->dev, "failed to get keypad irq\n");
217 return -EINVAL;
220 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
221 if (!res) {
222 dev_err(&pdev->dev, "missing platform resources\n");
223 return -EINVAL;
226 keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
227 input = input_allocate_device();
228 if (!keypad || !input) {
229 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
230 error = -ENOMEM;
231 goto err_free_mem;
234 keypad->irq = irq;
235 keypad->board = plat;
236 keypad->input = input;
237 spin_lock_init(&keypad->ske_keypad_lock);
239 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
240 dev_err(&pdev->dev, "failed to request I/O memory\n");
241 error = -EBUSY;
242 goto err_free_mem;
245 keypad->reg_base = ioremap(res->start, resource_size(res));
246 if (!keypad->reg_base) {
247 dev_err(&pdev->dev, "failed to remap I/O memory\n");
248 error = -ENXIO;
249 goto err_free_mem_region;
252 keypad->clk = clk_get(&pdev->dev, NULL);
253 if (IS_ERR(keypad->clk)) {
254 dev_err(&pdev->dev, "failed to get clk\n");
255 error = PTR_ERR(keypad->clk);
256 goto err_iounmap;
259 input->id.bustype = BUS_HOST;
260 input->name = "ux500-ske-keypad";
261 input->dev.parent = &pdev->dev;
263 input->keycode = keypad->keymap;
264 input->keycodesize = sizeof(keypad->keymap[0]);
265 input->keycodemax = ARRAY_SIZE(keypad->keymap);
267 input_set_capability(input, EV_MSC, MSC_SCAN);
269 __set_bit(EV_KEY, input->evbit);
270 if (!plat->no_autorepeat)
271 __set_bit(EV_REP, input->evbit);
273 matrix_keypad_build_keymap(plat->keymap_data, SKE_KEYPAD_ROW_SHIFT,
274 input->keycode, input->keybit);
276 clk_enable(keypad->clk);
278 /* go through board initialization helpers */
279 if (keypad->board->init)
280 keypad->board->init();
282 error = ske_keypad_chip_init(keypad);
283 if (error) {
284 dev_err(&pdev->dev, "unable to init keypad hardware\n");
285 goto err_clk_disable;
288 error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
289 IRQF_ONESHOT, "ske-keypad", keypad);
290 if (error) {
291 dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
292 goto err_clk_disable;
295 error = input_register_device(input);
296 if (error) {
297 dev_err(&pdev->dev,
298 "unable to register input device: %d\n", error);
299 goto err_free_irq;
302 if (plat->wakeup_enable)
303 device_init_wakeup(&pdev->dev, true);
305 platform_set_drvdata(pdev, keypad);
307 return 0;
309 err_free_irq:
310 free_irq(keypad->irq, keypad);
311 err_clk_disable:
312 clk_disable(keypad->clk);
313 clk_put(keypad->clk);
314 err_iounmap:
315 iounmap(keypad->reg_base);
316 err_free_mem_region:
317 release_mem_region(res->start, resource_size(res));
318 err_free_mem:
319 input_free_device(input);
320 kfree(keypad);
321 return error;
324 static int __devexit ske_keypad_remove(struct platform_device *pdev)
326 struct ske_keypad *keypad = platform_get_drvdata(pdev);
327 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
329 free_irq(keypad->irq, keypad);
331 input_unregister_device(keypad->input);
333 clk_disable(keypad->clk);
334 clk_put(keypad->clk);
336 if (keypad->board->exit)
337 keypad->board->exit();
339 iounmap(keypad->reg_base);
340 release_mem_region(res->start, resource_size(res));
341 kfree(keypad);
343 return 0;
346 #ifdef CONFIG_PM
347 static int ske_keypad_suspend(struct device *dev)
349 struct platform_device *pdev = to_platform_device(dev);
350 struct ske_keypad *keypad = platform_get_drvdata(pdev);
351 int irq = platform_get_irq(pdev, 0);
353 if (device_may_wakeup(dev))
354 enable_irq_wake(irq);
355 else
356 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
358 return 0;
361 static int ske_keypad_resume(struct device *dev)
363 struct platform_device *pdev = to_platform_device(dev);
364 struct ske_keypad *keypad = platform_get_drvdata(pdev);
365 int irq = platform_get_irq(pdev, 0);
367 if (device_may_wakeup(dev))
368 disable_irq_wake(irq);
369 else
370 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
372 return 0;
375 static const struct dev_pm_ops ske_keypad_dev_pm_ops = {
376 .suspend = ske_keypad_suspend,
377 .resume = ske_keypad_resume,
379 #endif
381 struct platform_driver ske_keypad_driver = {
382 .driver = {
383 .name = "nmk-ske-keypad",
384 .owner = THIS_MODULE,
385 #ifdef CONFIG_PM
386 .pm = &ske_keypad_dev_pm_ops,
387 #endif
389 .probe = ske_keypad_probe,
390 .remove = __devexit_p(ske_keypad_remove),
393 static int __init ske_keypad_init(void)
395 return platform_driver_probe(&ske_keypad_driver, ske_keypad_probe);
397 module_init(ske_keypad_init);
399 static void __exit ske_keypad_exit(void)
401 platform_driver_unregister(&ske_keypad_driver);
403 module_exit(ske_keypad_exit);
405 MODULE_LICENSE("GPL v2");
406 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
407 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
408 MODULE_ALIAS("platform:nomadik-ske-keypad");