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
61 * @pclk: clock structure pointer
62 * @ske_keypad_lock: spinlock protecting the keypad read/writes
66 void __iomem *reg_base;
67 struct input_dev *input;
68 const struct ske_keypad_platform_data *board;
69 unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS];
72 spinlock_t ske_keypad_lock;
75 static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
80 spin_lock(&keypad->ske_keypad_lock);
82 ret = readl(keypad->reg_base + addr);
85 writel(ret, keypad->reg_base + addr);
87 spin_unlock(&keypad->ske_keypad_lock);
91 * ske_keypad_chip_init: init keypad controller configuration
93 * Enable Multi key press detection, auto scan mode
95 static int __init ske_keypad_chip_init(struct ske_keypad *keypad)
98 int timeout = keypad->board->debounce_ms;
100 /* check SKE_RIS to be 0 */
101 while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
109 * keypad dbounce is configured in DBCR[15:8]
110 * dbounce value in steps of 32/32.768 ms
112 spin_lock(&keypad->ske_keypad_lock);
113 value = readl(keypad->reg_base + SKE_DBCR);
114 value = value & 0xff;
115 value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
116 writel(value, keypad->reg_base + SKE_DBCR);
117 spin_unlock(&keypad->ske_keypad_lock);
119 /* enable multi key detection */
120 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
123 * set up the number of columns
124 * KPCN[5:3] defines no. of keypad columns to be auto scanned
126 value = (keypad->board->kcol - 1) << 3;
127 ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
129 /* clear keypad interrupt for auto(and pending SW) scans */
130 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
132 /* un-mask keypad interrupts */
133 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
135 /* enable automatic scan */
136 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
141 static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col)
143 int row = 0, code, pos;
144 struct input_dev *input = keypad->input;
149 /* find out the row */
150 num_of_rows = hweight8(status);
154 status &= ~(1 << pos);
156 code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
157 ske_ris = readl(keypad->reg_base + SKE_RIS);
158 key_pressed = ske_ris & SKE_KPRISA;
160 input_event(input, EV_MSC, MSC_SCAN, code);
161 input_report_key(input, keypad->keymap[code], key_pressed);
164 } while (num_of_rows);
167 static void ske_keypad_read_data(struct ske_keypad *keypad)
174 * Read the auto scan registers
176 * Each SKE_ASRx (x=0 to x=3) contains two row values.
177 * lower byte contains row value for column 2*x,
178 * upper byte contains row value for column 2*x + 1
180 for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
181 ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
185 /* now that ASRx is zero, find out the coloumn x and row y */
186 status = ske_asr & 0xff;
189 ske_keypad_report(keypad, status, col);
191 status = (ske_asr & 0xff00) >> 8;
194 ske_keypad_report(keypad, status, col);
199 static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
201 struct ske_keypad *keypad = dev_id;
202 int timeout = keypad->board->debounce_ms;
204 /* disable auto scan interrupt; mask the interrupt generated */
205 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
206 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
208 while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --timeout)
211 /* SKEx registers are stable and can be read */
212 ske_keypad_read_data(keypad);
214 /* wait until raw interrupt is clear */
215 while ((readl(keypad->reg_base + SKE_RIS)) && --timeout)
216 msleep(KEY_PRESSED_DELAY);
218 /* enable auto scan interrupts */
219 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
224 static void ske_keypad_board_exit(void *data)
226 struct ske_keypad *keypad = data;
228 keypad->board->exit();
231 static int __init ske_keypad_probe(struct platform_device *pdev)
233 const struct ske_keypad_platform_data *plat =
234 dev_get_platdata(&pdev->dev);
235 struct device *dev = &pdev->dev;
236 struct ske_keypad *keypad;
237 struct input_dev *input;
242 dev_err(&pdev->dev, "invalid keypad platform data\n");
246 irq = platform_get_irq(pdev, 0);
250 keypad = devm_kzalloc(dev, sizeof(struct ske_keypad),
252 input = devm_input_allocate_device(dev);
253 if (!keypad || !input) {
254 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 keypad->reg_base = devm_platform_ioremap_resource(pdev, 0);
264 if (IS_ERR(keypad->reg_base))
265 return PTR_ERR(keypad->reg_base);
267 keypad->pclk = devm_clk_get_enabled(dev, "apb_pclk");
268 if (IS_ERR(keypad->pclk)) {
269 dev_err(&pdev->dev, "failed to get pclk\n");
270 return PTR_ERR(keypad->pclk);
273 keypad->clk = devm_clk_get_enabled(dev, NULL);
274 if (IS_ERR(keypad->clk)) {
275 dev_err(&pdev->dev, "failed to get clk\n");
276 return PTR_ERR(keypad->clk);
279 input->id.bustype = BUS_HOST;
280 input->name = "ux500-ske-keypad";
281 input->dev.parent = &pdev->dev;
283 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
284 SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS,
285 keypad->keymap, input);
287 dev_err(&pdev->dev, "Failed to build keymap\n");
291 input_set_capability(input, EV_MSC, MSC_SCAN);
292 if (!plat->no_autorepeat)
293 __set_bit(EV_REP, input->evbit);
295 /* go through board initialization helpers */
296 if (keypad->board->init)
297 keypad->board->init();
299 if (keypad->board->exit) {
300 error = devm_add_action_or_reset(dev, ske_keypad_board_exit,
306 error = ske_keypad_chip_init(keypad);
308 dev_err(&pdev->dev, "unable to init keypad hardware\n");
312 error = devm_request_threaded_irq(dev, keypad->irq,
313 NULL, ske_keypad_irq,
314 IRQF_ONESHOT, "ske-keypad", keypad);
316 dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
320 error = input_register_device(input);
323 "unable to register input device: %d\n", error);
327 if (plat->wakeup_enable)
328 device_init_wakeup(&pdev->dev, true);
330 platform_set_drvdata(pdev, keypad);
335 static int ske_keypad_suspend(struct device *dev)
337 struct platform_device *pdev = to_platform_device(dev);
338 struct ske_keypad *keypad = platform_get_drvdata(pdev);
339 int irq = platform_get_irq(pdev, 0);
341 if (device_may_wakeup(dev))
342 enable_irq_wake(irq);
344 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
349 static int ske_keypad_resume(struct device *dev)
351 struct platform_device *pdev = to_platform_device(dev);
352 struct ske_keypad *keypad = platform_get_drvdata(pdev);
353 int irq = platform_get_irq(pdev, 0);
355 if (device_may_wakeup(dev))
356 disable_irq_wake(irq);
358 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
363 static DEFINE_SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops,
364 ske_keypad_suspend, ske_keypad_resume);
366 static struct platform_driver ske_keypad_driver = {
368 .name = "nmk-ske-keypad",
369 .pm = pm_sleep_ptr(&ske_keypad_dev_pm_ops),
373 module_platform_driver_probe(ske_keypad_driver, ske_keypad_probe);
375 MODULE_LICENSE("GPL v2");
376 MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
377 MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
378 MODULE_ALIAS("platform:nomadik-ske-keypad");