1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) ST-Ericsson SA 2010
5 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
8 * Keypad controller driver for the SKE (Scroll Key Encoder) module used in
9 * the Nomadik 8815 and Ux500 platforms.
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/spinlock.h>
16 #include <linux/delay.h>
17 #include <linux/input.h>
18 #include <linux/slab.h>
19 #include <linux/clk.h>
20 #include <linux/module.h>
22 #include <linux/platform_data/keypad-nomadik-ske.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_NUM_ROWS 8
42 #define SKE_KPD_NUM_COLS 8
44 /* keypad auto scan registers */
50 #define SKE_NUM_ASRX_REGISTERS (4)
51 #define KEY_PRESSED_DELAY 10
54 * struct ske_keypad - data structure used by keypad driver
56 * @reg_base: ske registers 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
];
70 spinlock_t ske_keypad_lock
;
73 static void ske_keypad_set_bits(struct ske_keypad
*keypad
, u16 addr
,
78 spin_lock(&keypad
->ske_keypad_lock
);
80 ret
= readl(keypad
->reg_base
+ addr
);
83 writel(ret
, keypad
->reg_base
+ addr
);
85 spin_unlock(&keypad
->ske_keypad_lock
);
89 * ske_keypad_chip_init: init keypad controller configuration
91 * Enable Multi key press detection, auto scan mode
93 static int __init
ske_keypad_chip_init(struct ske_keypad
*keypad
)
96 int timeout
= keypad
->board
->debounce_ms
;
98 /* check SKE_RIS to be 0 */
99 while ((readl(keypad
->reg_base
+ SKE_RIS
) != 0x00000000) && timeout
--)
107 * keypad dbounce is configured in DBCR[15:8]
108 * dbounce value in steps of 32/32.768 ms
110 spin_lock(&keypad
->ske_keypad_lock
);
111 value
= readl(keypad
->reg_base
+ SKE_DBCR
);
112 value
= value
& 0xff;
113 value
|= ((keypad
->board
->debounce_ms
* 32000)/32768) << 8;
114 writel(value
, keypad
->reg_base
+ SKE_DBCR
);
115 spin_unlock(&keypad
->ske_keypad_lock
);
117 /* enable multi key detection */
118 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPMLT
);
121 * set up the number of columns
122 * KPCN[5:3] defines no. of keypad columns to be auto scanned
124 value
= (keypad
->board
->kcol
- 1) << 3;
125 ske_keypad_set_bits(keypad
, SKE_CR
, SKE_KPCN
, value
);
127 /* clear keypad interrupt for auto(and pending SW) scans */
128 ske_keypad_set_bits(keypad
, SKE_ICR
, 0x0, SKE_KPICA
| SKE_KPICS
);
130 /* un-mask keypad interrupts */
131 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
133 /* enable automatic scan */
134 ske_keypad_set_bits(keypad
, SKE_CR
, 0x0, SKE_KPASEN
);
139 static void ske_keypad_report(struct ske_keypad
*keypad
, u8 status
, int col
)
141 int row
= 0, code
, pos
;
142 struct input_dev
*input
= keypad
->input
;
147 /* find out the row */
148 num_of_rows
= hweight8(status
);
152 status
&= ~(1 << pos
);
154 code
= MATRIX_SCAN_CODE(row
, col
, SKE_KEYPAD_ROW_SHIFT
);
155 ske_ris
= readl(keypad
->reg_base
+ SKE_RIS
);
156 key_pressed
= ske_ris
& SKE_KPRISA
;
158 input_event(input
, EV_MSC
, MSC_SCAN
, code
);
159 input_report_key(input
, keypad
->keymap
[code
], key_pressed
);
162 } while (num_of_rows
);
165 static void ske_keypad_read_data(struct ske_keypad
*keypad
)
172 * Read the auto scan registers
174 * Each SKE_ASRx (x=0 to x=3) contains two row values.
175 * lower byte contains row value for column 2*x,
176 * upper byte contains row value for column 2*x + 1
178 for (i
= 0; i
< SKE_NUM_ASRX_REGISTERS
; i
++) {
179 ske_asr
= readl(keypad
->reg_base
+ SKE_ASR0
+ (4 * i
));
183 /* now that ASRx is zero, find out the coloumn x and row y */
184 status
= ske_asr
& 0xff;
187 ske_keypad_report(keypad
, status
, col
);
189 status
= (ske_asr
& 0xff00) >> 8;
192 ske_keypad_report(keypad
, status
, col
);
197 static irqreturn_t
ske_keypad_irq(int irq
, void *dev_id
)
199 struct ske_keypad
*keypad
= dev_id
;
200 int timeout
= keypad
->board
->debounce_ms
;
202 /* disable auto scan interrupt; mask the interrupt generated */
203 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
204 ske_keypad_set_bits(keypad
, SKE_ICR
, 0x0, SKE_KPICA
);
206 while ((readl(keypad
->reg_base
+ SKE_CR
) & SKE_KPASON
) && --timeout
)
209 /* SKEx registers are stable and can be read */
210 ske_keypad_read_data(keypad
);
212 /* wait until raw interrupt is clear */
213 while ((readl(keypad
->reg_base
+ SKE_RIS
)) && --timeout
)
214 msleep(KEY_PRESSED_DELAY
);
216 /* enable auto scan interrupts */
217 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
222 static int __init
ske_keypad_probe(struct platform_device
*pdev
)
224 const struct ske_keypad_platform_data
*plat
=
225 dev_get_platdata(&pdev
->dev
);
226 struct ske_keypad
*keypad
;
227 struct input_dev
*input
;
228 struct resource
*res
;
233 dev_err(&pdev
->dev
, "invalid keypad platform data\n");
237 irq
= platform_get_irq(pdev
, 0);
241 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
243 dev_err(&pdev
->dev
, "missing platform resources\n");
247 keypad
= kzalloc(sizeof(struct ske_keypad
), GFP_KERNEL
);
248 input
= input_allocate_device();
249 if (!keypad
|| !input
) {
250 dev_err(&pdev
->dev
, "failed to allocate keypad memory\n");
256 keypad
->board
= plat
;
257 keypad
->input
= input
;
258 spin_lock_init(&keypad
->ske_keypad_lock
);
260 if (!request_mem_region(res
->start
, resource_size(res
), pdev
->name
)) {
261 dev_err(&pdev
->dev
, "failed to request I/O memory\n");
266 keypad
->reg_base
= ioremap(res
->start
, resource_size(res
));
267 if (!keypad
->reg_base
) {
268 dev_err(&pdev
->dev
, "failed to remap I/O memory\n");
270 goto err_free_mem_region
;
273 keypad
->pclk
= clk_get(&pdev
->dev
, "apb_pclk");
274 if (IS_ERR(keypad
->pclk
)) {
275 dev_err(&pdev
->dev
, "failed to get pclk\n");
276 error
= PTR_ERR(keypad
->pclk
);
280 keypad
->clk
= clk_get(&pdev
->dev
, NULL
);
281 if (IS_ERR(keypad
->clk
)) {
282 dev_err(&pdev
->dev
, "failed to get clk\n");
283 error
= PTR_ERR(keypad
->clk
);
287 input
->id
.bustype
= BUS_HOST
;
288 input
->name
= "ux500-ske-keypad";
289 input
->dev
.parent
= &pdev
->dev
;
291 error
= matrix_keypad_build_keymap(plat
->keymap_data
, NULL
,
292 SKE_KPD_NUM_ROWS
, SKE_KPD_NUM_COLS
,
293 keypad
->keymap
, input
);
295 dev_err(&pdev
->dev
, "Failed to build keymap\n");
299 input_set_capability(input
, EV_MSC
, MSC_SCAN
);
300 if (!plat
->no_autorepeat
)
301 __set_bit(EV_REP
, input
->evbit
);
303 error
= clk_prepare_enable(keypad
->pclk
);
305 dev_err(&pdev
->dev
, "Failed to prepare/enable pclk\n");
309 error
= clk_prepare_enable(keypad
->clk
);
311 dev_err(&pdev
->dev
, "Failed to prepare/enable clk\n");
312 goto err_pclk_disable
;
316 /* go through board initialization helpers */
317 if (keypad
->board
->init
)
318 keypad
->board
->init();
320 error
= ske_keypad_chip_init(keypad
);
322 dev_err(&pdev
->dev
, "unable to init keypad hardware\n");
323 goto err_clk_disable
;
326 error
= request_threaded_irq(keypad
->irq
, NULL
, ske_keypad_irq
,
327 IRQF_ONESHOT
, "ske-keypad", keypad
);
329 dev_err(&pdev
->dev
, "allocate irq %d failed\n", keypad
->irq
);
330 goto err_clk_disable
;
333 error
= input_register_device(input
);
336 "unable to register input device: %d\n", error
);
340 if (plat
->wakeup_enable
)
341 device_init_wakeup(&pdev
->dev
, true);
343 platform_set_drvdata(pdev
, keypad
);
348 free_irq(keypad
->irq
, keypad
);
350 clk_disable_unprepare(keypad
->clk
);
352 clk_disable_unprepare(keypad
->pclk
);
354 clk_put(keypad
->clk
);
356 clk_put(keypad
->pclk
);
358 iounmap(keypad
->reg_base
);
360 release_mem_region(res
->start
, resource_size(res
));
362 input_free_device(input
);
367 static int ske_keypad_remove(struct platform_device
*pdev
)
369 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
370 struct resource
*res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
372 free_irq(keypad
->irq
, keypad
);
374 input_unregister_device(keypad
->input
);
376 clk_disable_unprepare(keypad
->clk
);
377 clk_put(keypad
->clk
);
379 if (keypad
->board
->exit
)
380 keypad
->board
->exit();
382 iounmap(keypad
->reg_base
);
383 release_mem_region(res
->start
, resource_size(res
));
389 #ifdef CONFIG_PM_SLEEP
390 static int ske_keypad_suspend(struct device
*dev
)
392 struct platform_device
*pdev
= to_platform_device(dev
);
393 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
394 int irq
= platform_get_irq(pdev
, 0);
396 if (device_may_wakeup(dev
))
397 enable_irq_wake(irq
);
399 ske_keypad_set_bits(keypad
, SKE_IMSC
, ~SKE_KPIMA
, 0x0);
404 static int ske_keypad_resume(struct device
*dev
)
406 struct platform_device
*pdev
= to_platform_device(dev
);
407 struct ske_keypad
*keypad
= platform_get_drvdata(pdev
);
408 int irq
= platform_get_irq(pdev
, 0);
410 if (device_may_wakeup(dev
))
411 disable_irq_wake(irq
);
413 ske_keypad_set_bits(keypad
, SKE_IMSC
, 0x0, SKE_KPIMA
);
419 static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops
,
420 ske_keypad_suspend
, ske_keypad_resume
);
422 static struct platform_driver ske_keypad_driver
= {
424 .name
= "nmk-ske-keypad",
425 .pm
= &ske_keypad_dev_pm_ops
,
427 .remove
= ske_keypad_remove
,
430 module_platform_driver_probe(ske_keypad_driver
, ske_keypad_probe
);
432 MODULE_LICENSE("GPL v2");
433 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
434 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
435 MODULE_ALIAS("platform:nomadik-ske-keypad");