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>
25 #define SKE_KPMLT (0x1 << 6)
26 #define SKE_KPCN (0x7 << 3)
27 #define SKE_KPASEN (0x1 << 2)
28 #define SKE_KPASON (0x1 << 7)
31 #define SKE_KPIMA (0x1 << 2)
34 #define SKE_KPICS (0x1 << 3)
35 #define SKE_KPICA (0x1 << 2)
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 */
49 #define SKE_NUM_ASRX_REGISTERS (4)
52 * struct ske_keypad - data structure used by keypad driver
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
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
];
67 spinlock_t ske_keypad_lock
;
70 static void ske_keypad_set_bits(struct ske_keypad
*keypad
, u16 addr
,
75 spin_lock(&keypad
->ske_keypad_lock
);
77 ret
= readl(keypad
->reg_base
+ addr
);
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
)
95 /* check SKE_RIS to be 0 */
96 while ((readl(keypad
->reg_base
+ SKE_RIS
) != 0x00000000) && timeout
--)
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
);
136 static void ske_keypad_read_data(struct ske_keypad
*keypad
)
138 struct input_dev
*input
= keypad
->input
;
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
));
155 /* now that ASRx is zero, find out the column x and row y*/
156 if (ske_asr
& 0xff) {
158 status
= ske_asr
& 0xff;
161 status
= (ske_asr
& 0xff00) >> 8;
164 /* find out the row */
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
);
177 static irqreturn_t
ske_keypad_irq(int irq
, void *dev_id
)
179 struct ske_keypad
*keypad
= dev_id
;
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
)
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
);
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
;
210 dev_err(&pdev
->dev
, "invalid keypad platform data\n");
214 irq
= platform_get_irq(pdev
, 0);
216 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
220 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
222 dev_err(&pdev
->dev
, "missing platform resources\n");
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");
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");
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");
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
);
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
);
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
);
291 dev_err(&pdev
->dev
, "allocate irq %d failed\n", keypad
->irq
);
292 goto err_clk_disable
;
295 error
= input_register_device(input
);
298 "unable to register input device: %d\n", error
);
302 if (plat
->wakeup_enable
)
303 device_init_wakeup(&pdev
->dev
, true);
305 platform_set_drvdata(pdev
, keypad
);
310 free_irq(keypad
->irq
, keypad
);
312 clk_disable(keypad
->clk
);
313 clk_put(keypad
->clk
);
315 iounmap(keypad
->reg_base
);
317 release_mem_region(res
->start
, resource_size(res
));
319 input_free_device(input
);
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
));
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
);
356 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
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
);
370 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
375 static const struct dev_pm_ops ske_keypad_dev_pm_ops
= {
376 .suspend
= ske_keypad_suspend
,
377 .resume
= ske_keypad_resume
,
381 struct platform_driver ske_keypad_driver
= {
383 .name
= "nmk-ske-keypad",
384 .owner
= THIS_MODULE
,
386 .pm
= &ske_keypad_dev_pm_ops
,
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");