blob: fa265fdce2c42d05f8091d524237816a788c3600 [file] [log] [blame]
Thomas Gleixneraf873fc2019-05-28 09:57:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Sundar Iyer1158f0f2010-09-29 19:42:14 -07002/*
3 * Copyright (C) ST-Ericsson SA 2010
4 *
5 * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
6 * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
7 *
Sundar Iyer1158f0f2010-09-29 19:42:14 -07008 * Keypad controller driver for the SKE (Scroll Key Encoder) module used in
9 * the Nomadik 8815 and Ux500 platforms.
10 */
11
12#include <linux/platform_device.h>
13#include <linux/interrupt.h>
14#include <linux/spinlock.h>
15#include <linux/io.h>
16#include <linux/delay.h>
17#include <linux/input.h>
18#include <linux/slab.h>
19#include <linux/clk.h>
Paul Gortmakerd2d84422011-07-03 13:53:48 -040020#include <linux/module.h>
Sundar Iyer1158f0f2010-09-29 19:42:14 -070021
Arnd Bergmanndb298da2012-08-24 15:19:33 +020022#include <linux/platform_data/keypad-nomadik-ske.h>
Sundar Iyer1158f0f2010-09-29 19:42:14 -070023
24/* SKE_CR bits */
25#define SKE_KPMLT (0x1 << 6)
26#define SKE_KPCN (0x7 << 3)
27#define SKE_KPASEN (0x1 << 2)
28#define SKE_KPASON (0x1 << 7)
29
30/* SKE_IMSC bits */
31#define SKE_KPIMA (0x1 << 2)
32
33/* SKE_ICR bits */
34#define SKE_KPICS (0x1 << 3)
35#define SKE_KPICA (0x1 << 2)
36
37/* SKE_RIS bits */
38#define SKE_KPRISA (0x1 << 2)
39
40#define SKE_KEYPAD_ROW_SHIFT 3
Dmitry Torokhov19328112012-05-10 22:37:08 -070041#define SKE_KPD_NUM_ROWS 8
42#define SKE_KPD_NUM_COLS 8
Sundar Iyer1158f0f2010-09-29 19:42:14 -070043
44/* keypad auto scan registers */
45#define SKE_ASR0 0x20
46#define SKE_ASR1 0x24
47#define SKE_ASR2 0x28
48#define SKE_ASR3 0x2C
49
50#define SKE_NUM_ASRX_REGISTERS (4)
Naveen Kumar Gaddipati84b63ad2012-06-25 00:34:36 -070051#define KEY_PRESSED_DELAY 10
Sundar Iyer1158f0f2010-09-29 19:42:14 -070052
53/**
54 * struct ske_keypad - data structure used by keypad driver
55 * @irq: irq no
Geliang Tang2e9776e2015-10-20 09:40:42 -070056 * @reg_base: ske registers base address
Sundar Iyer1158f0f2010-09-29 19:42:14 -070057 * @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 */
62struct ske_keypad {
63 int irq;
64 void __iomem *reg_base;
65 struct input_dev *input;
66 const struct ske_keypad_platform_data *board;
Dmitry Torokhov19328112012-05-10 22:37:08 -070067 unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS];
Sundar Iyer1158f0f2010-09-29 19:42:14 -070068 struct clk *clk;
Ulf Hanssond852f952012-11-03 12:16:55 -070069 struct clk *pclk;
Sundar Iyer1158f0f2010-09-29 19:42:14 -070070 spinlock_t ske_keypad_lock;
71};
72
73static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
74 u8 mask, u8 data)
75{
76 u32 ret;
77
78 spin_lock(&keypad->ske_keypad_lock);
79
80 ret = readl(keypad->reg_base + addr);
81 ret &= ~mask;
82 ret |= data;
83 writel(ret, keypad->reg_base + addr);
84
85 spin_unlock(&keypad->ske_keypad_lock);
86}
87
88/*
89 * ske_keypad_chip_init: init keypad controller configuration
90 *
91 * Enable Multi key press detection, auto scan mode
92 */
Dmitry Torokhovdb3dbd02012-01-22 23:27:29 -080093static int __init ske_keypad_chip_init(struct ske_keypad *keypad)
Sundar Iyer1158f0f2010-09-29 19:42:14 -070094{
95 u32 value;
Naveen Kumar Gaddipati84b63ad2012-06-25 00:34:36 -070096 int timeout = keypad->board->debounce_ms;
Sundar Iyer1158f0f2010-09-29 19:42:14 -070097
98 /* check SKE_RIS to be 0 */
99 while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
100 cpu_relax();
101
Dan Carpenter4d8f7272018-12-21 00:38:30 -0800102 if (timeout == -1)
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700103 return -EINVAL;
104
105 /*
106 * set debounce value
107 * keypad dbounce is configured in DBCR[15:8]
108 * dbounce value in steps of 32/32.768 ms
109 */
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);
116
117 /* enable multi key detection */
118 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
119
120 /*
121 * set up the number of columns
122 * KPCN[5:3] defines no. of keypad columns to be auto scanned
123 */
124 value = (keypad->board->kcol - 1) << 3;
125 ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
126
127 /* clear keypad interrupt for auto(and pending SW) scans */
128 ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
129
130 /* un-mask keypad interrupts */
131 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
132
133 /* enable automatic scan */
134 ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
135
136 return 0;
137}
138
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700139static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col)
140{
141 int row = 0, code, pos;
142 struct input_dev *input = keypad->input;
143 u32 ske_ris;
144 int key_pressed;
145 int num_of_rows;
146
147 /* find out the row */
148 num_of_rows = hweight8(status);
149 do {
150 pos = __ffs(status);
151 row = pos;
152 status &= ~(1 << pos);
153
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;
157
158 input_event(input, EV_MSC, MSC_SCAN, code);
159 input_report_key(input, keypad->keymap[code], key_pressed);
160 input_sync(input);
161 num_of_rows--;
162 } while (num_of_rows);
163}
164
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700165static void ske_keypad_read_data(struct ske_keypad *keypad)
166{
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700167 u8 status;
168 int col = 0;
169 int ske_asr, i;
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700170
171 /*
172 * Read the auto scan registers
173 *
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
177 */
178 for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
179 ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
180 if (!ske_asr)
181 continue;
182
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700183 /* now that ASRx is zero, find out the coloumn x and row y */
184 status = ske_asr & 0xff;
185 if (status) {
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700186 col = i * 2;
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700187 ske_keypad_report(keypad, status, col);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700188 }
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700189 status = (ske_asr & 0xff00) >> 8;
190 if (status) {
191 col = (i * 2) + 1;
192 ske_keypad_report(keypad, status, col);
193 }
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700194 }
195}
196
197static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
198{
199 struct ske_keypad *keypad = dev_id;
Naveen Kumar Gaddipati84b63ad2012-06-25 00:34:36 -0700200 int timeout = keypad->board->debounce_ms;
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700201
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);
205
Naveen Kumar Gaddipati84b63ad2012-06-25 00:34:36 -0700206 while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --timeout)
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700207 cpu_relax();
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700208
Naveen Kumar Gaddipatiaf77c882012-06-25 00:25:41 -0700209 /* SKEx registers are stable and can be read */
210 ske_keypad_read_data(keypad);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700211
Naveen Kumar Gaddipati84b63ad2012-06-25 00:34:36 -0700212 /* wait until raw interrupt is clear */
213 while ((readl(keypad->reg_base + SKE_RIS)) && --timeout)
214 msleep(KEY_PRESSED_DELAY);
215
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700216 /* enable auto scan interrupts */
217 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
218
219 return IRQ_HANDLED;
220}
221
Dmitry Torokhovdb3dbd02012-01-22 23:27:29 -0800222static int __init ske_keypad_probe(struct platform_device *pdev)
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700223{
Jingoo Hanc838cb32013-12-05 19:21:10 -0800224 const struct ske_keypad_platform_data *plat =
225 dev_get_platdata(&pdev->dev);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700226 struct ske_keypad *keypad;
227 struct input_dev *input;
228 struct resource *res;
229 int irq;
230 int error;
231
232 if (!plat) {
233 dev_err(&pdev->dev, "invalid keypad platform data\n");
234 return -EINVAL;
235 }
236
237 irq = platform_get_irq(pdev, 0);
238 if (irq < 0) {
239 dev_err(&pdev->dev, "failed to get keypad irq\n");
240 return -EINVAL;
241 }
242
243 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
244 if (!res) {
245 dev_err(&pdev->dev, "missing platform resources\n");
246 return -EINVAL;
247 }
248
249 keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
250 input = input_allocate_device();
251 if (!keypad || !input) {
252 dev_err(&pdev->dev, "failed to allocate keypad memory\n");
253 error = -ENOMEM;
254 goto err_free_mem;
255 }
256
257 keypad->irq = irq;
258 keypad->board = plat;
259 keypad->input = input;
260 spin_lock_init(&keypad->ske_keypad_lock);
261
262 if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
263 dev_err(&pdev->dev, "failed to request I/O memory\n");
264 error = -EBUSY;
265 goto err_free_mem;
266 }
267
268 keypad->reg_base = ioremap(res->start, resource_size(res));
269 if (!keypad->reg_base) {
270 dev_err(&pdev->dev, "failed to remap I/O memory\n");
271 error = -ENXIO;
272 goto err_free_mem_region;
273 }
274
Ulf Hanssond852f952012-11-03 12:16:55 -0700275 keypad->pclk = clk_get(&pdev->dev, "apb_pclk");
276 if (IS_ERR(keypad->pclk)) {
277 dev_err(&pdev->dev, "failed to get pclk\n");
278 error = PTR_ERR(keypad->pclk);
279 goto err_iounmap;
280 }
281
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700282 keypad->clk = clk_get(&pdev->dev, NULL);
283 if (IS_ERR(keypad->clk)) {
284 dev_err(&pdev->dev, "failed to get clk\n");
285 error = PTR_ERR(keypad->clk);
Ulf Hanssond852f952012-11-03 12:16:55 -0700286 goto err_pclk;
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700287 }
288
289 input->id.bustype = BUS_HOST;
290 input->name = "ux500-ske-keypad";
291 input->dev.parent = &pdev->dev;
292
Dmitry Torokhov19328112012-05-10 22:37:08 -0700293 error = matrix_keypad_build_keymap(plat->keymap_data, NULL,
294 SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS,
295 keypad->keymap, input);
296 if (error) {
297 dev_err(&pdev->dev, "Failed to build keymap\n");
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700298 goto err_clk;
Dmitry Torokhov19328112012-05-10 22:37:08 -0700299 }
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700300
301 input_set_capability(input, EV_MSC, MSC_SCAN);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700302 if (!plat->no_autorepeat)
303 __set_bit(EV_REP, input->evbit);
304
Ulf Hanssond852f952012-11-03 12:16:55 -0700305 error = clk_prepare_enable(keypad->pclk);
306 if (error) {
307 dev_err(&pdev->dev, "Failed to prepare/enable pclk\n");
308 goto err_clk;
309 }
310
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700311 error = clk_prepare_enable(keypad->clk);
312 if (error) {
313 dev_err(&pdev->dev, "Failed to prepare/enable clk\n");
Ulf Hanssond852f952012-11-03 12:16:55 -0700314 goto err_pclk_disable;
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700315 }
316
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700317
318 /* go through board initialization helpers */
319 if (keypad->board->init)
320 keypad->board->init();
321
322 error = ske_keypad_chip_init(keypad);
323 if (error) {
324 dev_err(&pdev->dev, "unable to init keypad hardware\n");
325 goto err_clk_disable;
326 }
327
328 error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
329 IRQF_ONESHOT, "ske-keypad", keypad);
330 if (error) {
331 dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
332 goto err_clk_disable;
333 }
334
335 error = input_register_device(input);
336 if (error) {
337 dev_err(&pdev->dev,
338 "unable to register input device: %d\n", error);
339 goto err_free_irq;
340 }
341
342 if (plat->wakeup_enable)
343 device_init_wakeup(&pdev->dev, true);
344
345 platform_set_drvdata(pdev, keypad);
346
347 return 0;
348
349err_free_irq:
350 free_irq(keypad->irq, keypad);
351err_clk_disable:
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700352 clk_disable_unprepare(keypad->clk);
Ulf Hanssond852f952012-11-03 12:16:55 -0700353err_pclk_disable:
354 clk_disable_unprepare(keypad->pclk);
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700355err_clk:
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700356 clk_put(keypad->clk);
Ulf Hanssond852f952012-11-03 12:16:55 -0700357err_pclk:
358 clk_put(keypad->pclk);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700359err_iounmap:
360 iounmap(keypad->reg_base);
361err_free_mem_region:
362 release_mem_region(res->start, resource_size(res));
363err_free_mem:
364 input_free_device(input);
365 kfree(keypad);
366 return error;
367}
368
Bill Pembertone2619cf2012-11-23 21:50:47 -0800369static int ske_keypad_remove(struct platform_device *pdev)
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700370{
371 struct ske_keypad *keypad = platform_get_drvdata(pdev);
372 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
373
374 free_irq(keypad->irq, keypad);
375
376 input_unregister_device(keypad->input);
377
Ulf Hansson4e99aab2012-11-03 12:16:51 -0700378 clk_disable_unprepare(keypad->clk);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700379 clk_put(keypad->clk);
380
381 if (keypad->board->exit)
382 keypad->board->exit();
383
384 iounmap(keypad->reg_base);
385 release_mem_region(res->start, resource_size(res));
386 kfree(keypad);
387
388 return 0;
389}
390
Dmitry Torokhov89f0f172012-01-22 23:27:54 -0800391#ifdef CONFIG_PM_SLEEP
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700392static int ske_keypad_suspend(struct device *dev)
393{
394 struct platform_device *pdev = to_platform_device(dev);
395 struct ske_keypad *keypad = platform_get_drvdata(pdev);
396 int irq = platform_get_irq(pdev, 0);
397
398 if (device_may_wakeup(dev))
399 enable_irq_wake(irq);
400 else
401 ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
402
403 return 0;
404}
405
406static int ske_keypad_resume(struct device *dev)
407{
408 struct platform_device *pdev = to_platform_device(dev);
409 struct ske_keypad *keypad = platform_get_drvdata(pdev);
410 int irq = platform_get_irq(pdev, 0);
411
412 if (device_may_wakeup(dev))
413 disable_irq_wake(irq);
414 else
415 ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
416
417 return 0;
418}
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700419#endif
420
Dmitry Torokhov89f0f172012-01-22 23:27:54 -0800421static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops,
422 ske_keypad_suspend, ske_keypad_resume);
423
Axel Linaeec0512011-12-11 23:45:26 -0800424static struct platform_driver ske_keypad_driver = {
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700425 .driver = {
426 .name = "nmk-ske-keypad",
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700427 .pm = &ske_keypad_dev_pm_ops,
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700428 },
Bill Pemberton1cb0aa82012-11-23 21:27:39 -0800429 .remove = ske_keypad_remove,
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700430};
Dmitry Torokhovd3d25802012-01-10 15:08:01 -0800431
Sachin Kamat0442ce12013-03-17 21:27:41 -0700432module_platform_driver_probe(ske_keypad_driver, ske_keypad_probe);
Sundar Iyer1158f0f2010-09-29 19:42:14 -0700433
434MODULE_LICENSE("GPL v2");
435MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
436MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
437MODULE_ALIAS("platform:nomadik-ske-keypad");