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>
23 #include <linux/platform_data/keypad-nomadik-ske.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)
52 #define KEY_PRESSED_DELAY 10
55 * struct ske_keypad - data structure used by keypad driver
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
65 void __iomem
*reg_base
;
66 struct input_dev
*input
;
67 const struct ske_keypad_platform_data
*board
;
68 unsigned short keymap
[SKE_KPD_NUM_ROWS
* SKE_KPD_NUM_COLS
];
71 spinlock_t ske_keypad_lock
;
74 static void ske_keypad_set_bits(struct ske_keypad
*keypad
, u16 addr
,
79 spin_lock(&keypad
->ske_keypad_lock
);
81 ret
= readl(keypad
->reg_base
+ addr
);
84 writel(ret
, keypad
->reg_base
+ addr
);
86 spin_unlock(&keypad
->ske_keypad_lock
);
90 * ske_keypad_chip_init: init keypad controller configuration
92 * Enable Multi key press detection, auto scan mode
94 static int __init
ske_keypad_chip_init(struct ske_keypad
*keypad
)
97 int timeout
= keypad
->board
->debounce_ms
;
99 /* check SKE_RIS to be 0 */
100 while ((readl(keypad
->reg_base
+ SKE_RIS
) != 0x00000000) && timeout
--)
108 * keypad dbounce is configured in DBCR[15:8]
109 * dbounce value in steps of 32/32.768 ms
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
);
118 /* enable multi key detection */
119 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPMLT
);
122 * set up the number of columns
123 * KPCN[5:3] defines no. of keypad columns to be auto scanned
125 value
= (keypad
->board
->kcol
- 1) << 3;
126 ske_keypad_set_bits(keypad
, SKE_CR
, SKE_KPCN
, value
);
128 /* clear keypad interrupt for auto(and pending SW) scans */
129 ske_keypad_set_bits(keypad
, SKE_ICR
, 0x0, SKE_KPICA
| SKE_KPICS
);
131 /* un-mask keypad interrupts */
132 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
134 /* enable automatic scan */
135 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPASEN
);
140 static void ske_keypad_report(struct ske_keypad
*keypad
, u8 status
, int col
)
142 int row
= 0, code
, pos
;
143 struct input_dev
*input
= keypad
->input
;
148 /* find out the row */
149 num_of_rows
= hweight8(status
);
153 status
&= ~(1 << pos
);
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
;
159 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
160 input_report_key(input
, keypad
->keymap
[code
], key_pressed
);
163 } while (num_of_rows
);
166 static void ske_keypad_read_data(struct ske_keypad
*keypad
)
173 * Read the auto scan registers
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
179 for (i
= 0; i
< SKE_NUM_ASRX_REGISTERS
; i
++) {
180 ske_asr
= readl(keypad
->reg_base
+ SKE_ASR0
+ (4 * i
));
184 /* now that ASRx is zero, find out the coloumn x and row y */
185 status
= ske_asr
& 0xff;
188 ske_keypad_report(keypad
, status
, col
);
190 status
= (ske_asr
& 0xff00) >> 8;
193 ske_keypad_report(keypad
, status
, col
);
198 static irqreturn_t
ske_keypad_irq(int irq
, void *dev_id
)
200 struct ske_keypad
*keypad
= dev_id
;
201 int timeout
= keypad
->board
->debounce_ms
;
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
);
207 while ((readl(keypad
->reg_base
+ SKE_CR
) & SKE_KPASON
) && --timeout
)
210 /* SKEx registers are stable and can be read */
211 ske_keypad_read_data(keypad
);
213 /* wait until raw interrupt is clear */
214 while ((readl(keypad
->reg_base
+ SKE_RIS
)) && --timeout
)
215 msleep(KEY_PRESSED_DELAY
);
217 /* enable auto scan interrupts */
218 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
223 static int __init
ske_keypad_probe(struct platform_device
*pdev
)
225 const struct ske_keypad_platform_data
*plat
=
226 dev_get_platdata(&pdev
->dev
);
227 struct ske_keypad
*keypad
;
228 struct input_dev
*input
;
229 struct resource
*res
;
234 dev_err(&pdev
->dev
, "invalid keypad platform data\n");
238 irq
= platform_get_irq(pdev
, 0);
240 dev_err(&pdev
->dev
, "failed to get keypad irq\n");
244 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
246 dev_err(&pdev
->dev
, "missing platform resources\n");
250 keypad
= kzalloc(sizeof(struct ske_keypad
), GFP_KERNEL
);
251 input
= input_allocate_device();
252 if (!keypad
|| !input
) {
253 dev_err(&pdev
->dev
, "failed to allocate keypad memory\n");
259 keypad
->board
= plat
;
260 keypad
->input
= input
;
261 spin_lock_init(&keypad
->ske_keypad_lock
);
263 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
264 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
269 keypad
->reg_base
= ioremap(res
->start
, resource_size(res
));
270 if (!keypad
->reg_base
) {
271 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
273 goto err_free_mem_region
;
276 keypad
->pclk
= clk_get(&pdev
->dev
, "apb_pclk");
277 if (IS_ERR(keypad
->pclk
)) {
278 dev_err(&pdev
->dev
, "failed to get pclk\n");
279 error
= PTR_ERR(keypad
->pclk
);
283 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
284 if (IS_ERR(keypad
->clk
)) {
285 dev_err(&pdev
->dev
, "failed to get clk\n");
286 error
= PTR_ERR(keypad
->clk
);
290 input
->id
.bustype
= BUS_HOST
;
291 input
->name
= "ux500-ske-keypad";
292 input
->dev
.parent
= &pdev
->dev
;
294 error
= matrix_keypad_build_keymap(plat
->keymap_data
, NULL
,
295 SKE_KPD_NUM_ROWS
, SKE_KPD_NUM_COLS
,
296 keypad
->keymap
, input
);
298 dev_err(&pdev
->dev
, "Failed to build keymap\n");
302 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
303 if (!plat
->no_autorepeat
)
304 __set_bit(EV_REP
, input
->evbit
);
306 error
= clk_prepare_enable(keypad
->pclk
);
308 dev_err(&pdev
->dev
, "Failed to prepare/enable pclk\n");
312 error
= clk_prepare_enable(keypad
->clk
);
314 dev_err(&pdev
->dev
, "Failed to prepare/enable clk\n");
315 goto err_pclk_disable
;
319 /* go through board initialization helpers */
320 if (keypad
->board
->init
)
321 keypad
->board
->init();
323 error
= ske_keypad_chip_init(keypad
);
325 dev_err(&pdev
->dev
, "unable to init keypad hardware\n");
326 goto err_clk_disable
;
329 error
= request_threaded_irq(keypad
->irq
, NULL
, ske_keypad_irq
,
330 IRQF_ONESHOT
, "ske-keypad", keypad
);
332 dev_err(&pdev
->dev
, "allocate irq %d failed\n", keypad
->irq
);
333 goto err_clk_disable
;
336 error
= input_register_device(input
);
339 "unable to register input device: %d\n", error
);
343 if (plat
->wakeup_enable
)
344 device_init_wakeup(&pdev
->dev
, true);
346 platform_set_drvdata(pdev
, keypad
);
351 free_irq(keypad
->irq
, keypad
);
353 clk_disable_unprepare(keypad
->clk
);
355 clk_disable_unprepare(keypad
->pclk
);
357 clk_put(keypad
->clk
);
359 clk_put(keypad
->pclk
);
361 iounmap(keypad
->reg_base
);
363 release_mem_region(res
->start
, resource_size(res
));
365 input_free_device(input
);
370 static int ske_keypad_remove(struct platform_device
*pdev
)
372 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
373 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
375 free_irq(keypad
->irq
, keypad
);
377 input_unregister_device(keypad
->input
);
379 clk_disable_unprepare(keypad
->clk
);
380 clk_put(keypad
->clk
);
382 if (keypad
->board
->exit
)
383 keypad
->board
->exit();
385 iounmap(keypad
->reg_base
);
386 release_mem_region(res
->start
, resource_size(res
));
392 #ifdef CONFIG_PM_SLEEP
393 static int ske_keypad_suspend(struct device
*dev
)
395 struct platform_device
*pdev
= to_platform_device(dev
);
396 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
397 int irq
= platform_get_irq(pdev
, 0);
399 if (device_may_wakeup(dev
))
400 enable_irq_wake(irq
);
402 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
407 static int ske_keypad_resume(struct device
*dev
)
409 struct platform_device
*pdev
= to_platform_device(dev
);
410 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
411 int irq
= platform_get_irq(pdev
, 0);
413 if (device_may_wakeup(dev
))
414 disable_irq_wake(irq
);
416 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
422 static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops
,
423 ske_keypad_suspend
, ske_keypad_resume
);
425 static struct platform_driver ske_keypad_driver
= {
427 .name
= "nmk-ske-keypad",
428 .owner
= THIS_MODULE
,
429 .pm
= &ske_keypad_dev_pm_ops
,
431 .remove
= ske_keypad_remove
,
434 module_platform_driver_probe(ske_keypad_driver
, ske_keypad_probe
);
436 MODULE_LICENSE("GPL v2");
437 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
438 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
439 MODULE_ALIAS("platform:nomadik-ske-keypad");