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>
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>
26 #define SKE_KPMLT (0x1 << 6)
27 #define SKE_KPCN (0x7 << 3)
28 #define SKE_KPASEN (0x1 << 2)
29 #define SKE_KPASON (0x1 << 7)
32 #define SKE_KPIMA (0x1 << 2)
35 #define SKE_KPICS (0x1 << 3)
36 #define SKE_KPICA (0x1 << 2)
39 #define SKE_KPRISA (0x1 << 2)
41 #define SKE_KEYPAD_ROW_SHIFT 3
42 #define SKE_KPD_NUM_ROWS 8
43 #define SKE_KPD_NUM_COLS 8
45 /* keypad auto scan registers */
51 #define SKE_NUM_ASRX_REGISTERS (4)
54 * struct ske_keypad - data structure used by keypad driver
56 * @reg_base: ske regsiters base address
57 * @input: pointer to input device object
58 * @board: keypad platform device
59 * @keymap: matrix scan code table for keycodes
60 * @clk: clock structure pointer
64 void __iomem
*reg_base
;
65 struct input_dev
*input
;
66 const struct ske_keypad_platform_data
*board
;
67 unsigned short keymap
[SKE_KPD_NUM_ROWS
* SKE_KPD_NUM_COLS
];
69 spinlock_t ske_keypad_lock
;
72 static void ske_keypad_set_bits(struct ske_keypad
*keypad
, u16 addr
,
77 spin_lock(&keypad
->ske_keypad_lock
);
79 ret
= readl(keypad
->reg_base
+ addr
);
82 writel(ret
, keypad
->reg_base
+ addr
);
84 spin_unlock(&keypad
->ske_keypad_lock
);
88 * ske_keypad_chip_init: init keypad controller configuration
90 * Enable Multi key press detection, auto scan mode
92 static int __init
ske_keypad_chip_init(struct ske_keypad
*keypad
)
97 /* check SKE_RIS to be 0 */
98 while ((readl(keypad
->reg_base
+ SKE_RIS
) != 0x00000000) && timeout
--)
106 * keypad dbounce is configured in DBCR[15:8]
107 * dbounce value in steps of 32/32.768 ms
109 spin_lock(&keypad
->ske_keypad_lock
);
110 value
= readl(keypad
->reg_base
+ SKE_DBCR
);
111 value
= value
& 0xff;
112 value
|= ((keypad
->board
->debounce_ms
* 32000)/32768) << 8;
113 writel(value
, keypad
->reg_base
+ SKE_DBCR
);
114 spin_unlock(&keypad
->ske_keypad_lock
);
116 /* enable multi key detection */
117 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPMLT
);
120 * set up the number of columns
121 * KPCN[5:3] defines no. of keypad columns to be auto scanned
123 value
= (keypad
->board
->kcol
- 1) << 3;
124 ske_keypad_set_bits(keypad
, SKE_CR
, SKE_KPCN
, value
);
126 /* clear keypad interrupt for auto(and pending SW) scans */
127 ske_keypad_set_bits(keypad
, SKE_ICR
, 0x0, SKE_KPICA
| SKE_KPICS
);
129 /* un-mask keypad interrupts */
130 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
132 /* enable automatic scan */
133 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPASEN
);
138 static void ske_keypad_read_data(struct ske_keypad
*keypad
)
140 struct input_dev
*input
= keypad
->input
;
142 int col
= 0, row
= 0, code
;
143 int ske_asr
, ske_ris
, key_pressed
, i
;
146 * Read the auto scan registers
148 * Each SKE_ASRx (x=0 to x=3) contains two row values.
149 * lower byte contains row value for column 2*x,
150 * upper byte contains row value for column 2*x + 1
152 for (i
= 0; i
< SKE_NUM_ASRX_REGISTERS
; i
++) {
153 ske_asr
= readl(keypad
->reg_base
+ SKE_ASR0
+ (4 * i
));
157 /* now that ASRx is zero, find out the column x and row y*/
158 if (ske_asr
& 0xff) {
160 status
= ske_asr
& 0xff;
163 status
= (ske_asr
& 0xff00) >> 8;
166 /* find out the row */
169 code
= MATRIX_SCAN_CODE(row
, col
, SKE_KEYPAD_ROW_SHIFT
);
170 ske_ris
= readl(keypad
->reg_base
+ SKE_RIS
);
171 key_pressed
= ske_ris
& SKE_KPRISA
;
173 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
174 input_report_key(input
, keypad
->keymap
[code
], key_pressed
);
179 static irqreturn_t
ske_keypad_irq(int irq
, void *dev_id
)
181 struct ske_keypad
*keypad
= dev_id
;
184 /* disable auto scan interrupt; mask the interrupt generated */
185 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
186 ske_keypad_set_bits(keypad
, SKE_ICR
, 0x0, SKE_KPICA
);
188 while ((readl(keypad
->reg_base
+ SKE_CR
) & SKE_KPASON
) && --retries
)
192 /* SKEx registers are stable and can be read */
193 ske_keypad_read_data(keypad
);
196 /* enable auto scan interrupts */
197 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
202 static int __init
ske_keypad_probe(struct platform_device
*pdev
)
204 const struct ske_keypad_platform_data
*plat
= pdev
->dev
.platform_data
;
205 struct ske_keypad
*keypad
;
206 struct input_dev
*input
;
207 struct resource
*res
;
212 dev_err(&pdev
->dev
, "invalid keypad platform data\n");
216 irq
= platform_get_irq(pdev
, 0);
218 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
222 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
224 dev_err(&pdev
->dev
, "missing platform resources\n");
228 keypad
= kzalloc(sizeof(struct ske_keypad
), GFP_KERNEL
);
229 input
= input_allocate_device();
230 if (!keypad
|| !input
) {
231 dev_err(&pdev
->dev
, "failed to allocate keypad memory\n");
237 keypad
->board
= plat
;
238 keypad
->input
= input
;
239 spin_lock_init(&keypad
->ske_keypad_lock
);
241 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
242 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
247 keypad
->reg_base
= ioremap(res
->start
, resource_size(res
));
248 if (!keypad
->reg_base
) {
249 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
251 goto err_free_mem_region
;
254 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
255 if (IS_ERR(keypad
->clk
)) {
256 dev_err(&pdev
->dev
, "failed to get clk\n");
257 error
= PTR_ERR(keypad
->clk
);
261 input
->id
.bustype
= BUS_HOST
;
262 input
->name
= "ux500-ske-keypad";
263 input
->dev
.parent
= &pdev
->dev
;
265 error
= matrix_keypad_build_keymap(plat
->keymap_data
, NULL
,
266 SKE_KPD_NUM_ROWS
, SKE_KPD_NUM_COLS
,
267 keypad
->keymap
, input
);
269 dev_err(&pdev
->dev
, "Failed to build keymap\n");
273 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
274 if (!plat
->no_autorepeat
)
275 __set_bit(EV_REP
, input
->evbit
);
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
);
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
);
292 dev_err(&pdev
->dev
, "allocate irq %d failed\n", keypad
->irq
);
293 goto err_clk_disable
;
296 error
= input_register_device(input
);
299 "unable to register input device: %d\n", error
);
303 if (plat
->wakeup_enable
)
304 device_init_wakeup(&pdev
->dev
, true);
306 platform_set_drvdata(pdev
, keypad
);
311 free_irq(keypad
->irq
, keypad
);
313 clk_disable(keypad
->clk
);
314 clk_put(keypad
->clk
);
316 iounmap(keypad
->reg_base
);
318 release_mem_region(res
->start
, resource_size(res
));
320 input_free_device(input
);
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
));
347 #ifdef CONFIG_PM_SLEEP
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
);
357 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
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
);
371 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
377 static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops
,
378 ske_keypad_suspend
, ske_keypad_resume
);
380 static struct platform_driver ske_keypad_driver
= {
382 .name
= "nmk-ske-keypad",
383 .owner
= THIS_MODULE
,
384 .pm
= &ske_keypad_dev_pm_ops
,
386 .remove
= __devexit_p(ske_keypad_remove
),
389 static int __init
ske_keypad_init(void)
391 return platform_driver_probe(&ske_keypad_driver
, ske_keypad_probe
);
393 module_init(ske_keypad_init
);
395 static void __exit
ske_keypad_exit(void)
397 platform_driver_unregister(&ske_keypad_driver
);
399 module_exit(ske_keypad_exit
);
401 MODULE_LICENSE("GPL v2");
402 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
403 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
404 MODULE_ALIAS("platform:nomadik-ske-keypad");