blob: 0ba69f3fcb52b8ca3d47ce3516be6b2f6bbbf167 [file] [log] [blame]
H Hartley Sweetene06003a2009-04-18 23:43:57 -07001/*
2 * Driver for the Cirrus EP93xx matrix keypad controller.
3 *
4 * Copyright (c) 2008 H Hartley Sweeten <hsweeten@visionengravers.com>
5 *
6 * Based on the pxa27x matrix keypad controller by Rodolfo Giometti.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * NOTE:
13 *
14 * The 3-key reset is triggered by pressing the 3 keys in
15 * Row 0, Columns 2, 4, and 7 at the same time. This action can
16 * be disabled by setting the EP93XX_KEYPAD_DISABLE_3_KEY flag.
17 *
18 * Normal operation for the matrix does not autorepeat the key press.
19 * This action can be enabled by setting the EP93XX_KEYPAD_AUTOREPEAT
20 * flag.
21 */
22
Axel Linb9cc5102011-08-21 12:48:08 -070023#include <linux/module.h>
H Hartley Sweetene06003a2009-04-18 23:43:57 -070024#include <linux/platform_device.h>
25#include <linux/interrupt.h>
H Hartley Sweetene06003a2009-04-18 23:43:57 -070026#include <linux/clk.h>
H Hartley Sweeten60214f02009-12-15 08:39:51 -080027#include <linux/io.h>
28#include <linux/input/matrix_keypad.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
H Hartley Sweetene06003a2009-04-18 23:43:57 -070030
31#include <mach/hardware.h>
H Hartley Sweetene06003a2009-04-18 23:43:57 -070032#include <mach/ep93xx_keypad.h>
33
34/*
35 * Keypad Interface Register offsets
36 */
37#define KEY_INIT 0x00 /* Key Scan Initialization register */
38#define KEY_DIAG 0x04 /* Key Scan Diagnostic register */
39#define KEY_REG 0x08 /* Key Value Capture register */
40
41/* Key Scan Initialization Register bit defines */
42#define KEY_INIT_DBNC_MASK (0x00ff0000)
43#define KEY_INIT_DBNC_SHIFT (16)
44#define KEY_INIT_DIS3KY (1<<15)
45#define KEY_INIT_DIAG (1<<14)
46#define KEY_INIT_BACK (1<<13)
47#define KEY_INIT_T2 (1<<12)
48#define KEY_INIT_PRSCL_MASK (0x000003ff)
49#define KEY_INIT_PRSCL_SHIFT (0)
50
51/* Key Scan Diagnostic Register bit defines */
52#define KEY_DIAG_MASK (0x0000003f)
53#define KEY_DIAG_SHIFT (0)
54
55/* Key Value Capture Register bit defines */
56#define KEY_REG_K (1<<15)
57#define KEY_REG_INT (1<<14)
58#define KEY_REG_2KEYS (1<<13)
59#define KEY_REG_1KEY (1<<12)
60#define KEY_REG_KEY2_MASK (0x00000fc0)
61#define KEY_REG_KEY2_SHIFT (6)
62#define KEY_REG_KEY1_MASK (0x0000003f)
63#define KEY_REG_KEY1_SHIFT (0)
64
H Hartley Sweeten60214f02009-12-15 08:39:51 -080065#define EP93XX_MATRIX_SIZE (EP93XX_MATRIX_ROWS * EP93XX_MATRIX_COLS)
H Hartley Sweetene06003a2009-04-18 23:43:57 -070066
67struct ep93xx_keypad {
68 struct ep93xx_keypad_platform_data *pdata;
H Hartley Sweetene06003a2009-04-18 23:43:57 -070069 struct input_dev *input_dev;
H Hartley Sweeten60214f02009-12-15 08:39:51 -080070 struct clk *clk;
71
H Hartley Sweetene06003a2009-04-18 23:43:57 -070072 void __iomem *mmio_base;
73
H Hartley Sweetena5abd952010-01-13 00:34:12 -080074 unsigned short keycodes[EP93XX_MATRIX_SIZE];
H Hartley Sweetene06003a2009-04-18 23:43:57 -070075
76 int key1;
77 int key2;
78
H Hartley Sweeten60214f02009-12-15 08:39:51 -080079 int irq;
80
81 bool enabled;
H Hartley Sweetene06003a2009-04-18 23:43:57 -070082};
83
H Hartley Sweetene06003a2009-04-18 23:43:57 -070084static irqreturn_t ep93xx_keypad_irq_handler(int irq, void *dev_id)
85{
86 struct ep93xx_keypad *keypad = dev_id;
87 struct input_dev *input_dev = keypad->input_dev;
H Hartley Sweeten60214f02009-12-15 08:39:51 -080088 unsigned int status;
H Hartley Sweetene06003a2009-04-18 23:43:57 -070089 int keycode, key1, key2;
90
H Hartley Sweeten60214f02009-12-15 08:39:51 -080091 status = __raw_readl(keypad->mmio_base + KEY_REG);
92
H Hartley Sweetene06003a2009-04-18 23:43:57 -070093 keycode = (status & KEY_REG_KEY1_MASK) >> KEY_REG_KEY1_SHIFT;
H Hartley Sweetena5abd952010-01-13 00:34:12 -080094 key1 = keypad->keycodes[keycode];
H Hartley Sweetene06003a2009-04-18 23:43:57 -070095
96 keycode = (status & KEY_REG_KEY2_MASK) >> KEY_REG_KEY2_SHIFT;
H Hartley Sweetena5abd952010-01-13 00:34:12 -080097 key2 = keypad->keycodes[keycode];
H Hartley Sweetene06003a2009-04-18 23:43:57 -070098
99 if (status & KEY_REG_2KEYS) {
100 if (keypad->key1 && key1 != keypad->key1 && key2 != keypad->key1)
101 input_report_key(input_dev, keypad->key1, 0);
102
103 if (keypad->key2 && key1 != keypad->key2 && key2 != keypad->key2)
104 input_report_key(input_dev, keypad->key2, 0);
105
106 input_report_key(input_dev, key1, 1);
107 input_report_key(input_dev, key2, 1);
108
109 keypad->key1 = key1;
110 keypad->key2 = key2;
111
112 } else if (status & KEY_REG_1KEY) {
113 if (keypad->key1 && key1 != keypad->key1)
114 input_report_key(input_dev, keypad->key1, 0);
115
116 if (keypad->key2 && key1 != keypad->key2)
117 input_report_key(input_dev, keypad->key2, 0);
118
119 input_report_key(input_dev, key1, 1);
120
121 keypad->key1 = key1;
122 keypad->key2 = 0;
123
124 } else {
125 input_report_key(input_dev, keypad->key1, 0);
126 input_report_key(input_dev, keypad->key2, 0);
127
128 keypad->key1 = keypad->key2 = 0;
129 }
130 input_sync(input_dev);
131
132 return IRQ_HANDLED;
133}
134
135static void ep93xx_keypad_config(struct ep93xx_keypad *keypad)
136{
137 struct ep93xx_keypad_platform_data *pdata = keypad->pdata;
138 unsigned int val = 0;
139
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800140 if (pdata->flags & EP93XX_KEYPAD_KDIV)
141 clk_set_rate(keypad->clk, EP93XX_KEYTCHCLK_DIV4);
142 else
143 clk_set_rate(keypad->clk, EP93XX_KEYTCHCLK_DIV16);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700144
145 if (pdata->flags & EP93XX_KEYPAD_DISABLE_3_KEY)
146 val |= KEY_INIT_DIS3KY;
147 if (pdata->flags & EP93XX_KEYPAD_DIAG_MODE)
148 val |= KEY_INIT_DIAG;
149 if (pdata->flags & EP93XX_KEYPAD_BACK_DRIVE)
150 val |= KEY_INIT_BACK;
151 if (pdata->flags & EP93XX_KEYPAD_TEST_MODE)
152 val |= KEY_INIT_T2;
153
154 val |= ((pdata->debounce << KEY_INIT_DBNC_SHIFT) & KEY_INIT_DBNC_MASK);
155
156 val |= ((pdata->prescale << KEY_INIT_PRSCL_SHIFT) & KEY_INIT_PRSCL_MASK);
157
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800158 __raw_writel(val, keypad->mmio_base + KEY_INIT);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700159}
160
161static int ep93xx_keypad_open(struct input_dev *pdev)
162{
163 struct ep93xx_keypad *keypad = input_get_drvdata(pdev);
164
165 if (!keypad->enabled) {
166 ep93xx_keypad_config(keypad);
167 clk_enable(keypad->clk);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800168 keypad->enabled = true;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700169 }
170
171 return 0;
172}
173
174static void ep93xx_keypad_close(struct input_dev *pdev)
175{
176 struct ep93xx_keypad *keypad = input_get_drvdata(pdev);
177
178 if (keypad->enabled) {
179 clk_disable(keypad->clk);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800180 keypad->enabled = false;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700181 }
182}
183
184
185#ifdef CONFIG_PM
186/*
187 * NOTE: I don't know if this is correct, or will work on the ep93xx.
188 *
189 * None of the existing ep93xx drivers have power management support.
190 * But, this is basically what the pxa27x_keypad driver does.
191 */
192static int ep93xx_keypad_suspend(struct platform_device *pdev,
193 pm_message_t state)
194{
195 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
196 struct input_dev *input_dev = keypad->input_dev;
197
198 mutex_lock(&input_dev->mutex);
199
200 if (keypad->enabled) {
201 clk_disable(keypad->clk);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800202 keypad->enabled = false;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700203 }
204
205 mutex_unlock(&input_dev->mutex);
206
207 if (device_may_wakeup(&pdev->dev))
208 enable_irq_wake(keypad->irq);
209
210 return 0;
211}
212
213static int ep93xx_keypad_resume(struct platform_device *pdev)
214{
215 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
216 struct input_dev *input_dev = keypad->input_dev;
217
218 if (device_may_wakeup(&pdev->dev))
219 disable_irq_wake(keypad->irq);
220
221 mutex_lock(&input_dev->mutex);
222
223 if (input_dev->users) {
224 if (!keypad->enabled) {
225 ep93xx_keypad_config(keypad);
226 clk_enable(keypad->clk);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800227 keypad->enabled = true;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700228 }
229 }
230
231 mutex_unlock(&input_dev->mutex);
232
233 return 0;
234}
235#else /* !CONFIG_PM */
236#define ep93xx_keypad_suspend NULL
237#define ep93xx_keypad_resume NULL
238#endif /* !CONFIG_PM */
239
240static int __devinit ep93xx_keypad_probe(struct platform_device *pdev)
241{
242 struct ep93xx_keypad *keypad;
H Hartley Sweetena5abd952010-01-13 00:34:12 -0800243 const struct matrix_keymap_data *keymap_data;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700244 struct input_dev *input_dev;
245 struct resource *res;
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800246 int err;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700247
248 keypad = kzalloc(sizeof(struct ep93xx_keypad), GFP_KERNEL);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800249 if (!keypad)
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700250 return -ENOMEM;
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800251
252 keypad->pdata = pdev->dev.platform_data;
253 if (!keypad->pdata) {
254 err = -EINVAL;
255 goto failed_free;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700256 }
257
H Hartley Sweetena5abd952010-01-13 00:34:12 -0800258 keymap_data = keypad->pdata->keymap_data;
259 if (!keymap_data) {
260 err = -EINVAL;
261 goto failed_free;
262 }
263
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800264 keypad->irq = platform_get_irq(pdev, 0);
265 if (!keypad->irq) {
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700266 err = -ENXIO;
267 goto failed_free;
268 }
269
270 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
271 if (!res) {
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700272 err = -ENXIO;
273 goto failed_free;
274 }
275
276 res = request_mem_region(res->start, resource_size(res), pdev->name);
277 if (!res) {
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700278 err = -EBUSY;
279 goto failed_free;
280 }
281
282 keypad->mmio_base = ioremap(res->start, resource_size(res));
283 if (keypad->mmio_base == NULL) {
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700284 err = -ENXIO;
285 goto failed_free_mem;
286 }
287
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800288 err = ep93xx_keypad_acquire_gpio(pdev);
289 if (err)
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700290 goto failed_free_io;
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800291
292 keypad->clk = clk_get(&pdev->dev, NULL);
293 if (IS_ERR(keypad->clk)) {
294 err = PTR_ERR(keypad->clk);
295 goto failed_free_gpio;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700296 }
297
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700298 input_dev = input_allocate_device();
299 if (!input_dev) {
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700300 err = -ENOMEM;
301 goto failed_put_clk;
302 }
303
304 keypad->input_dev = input_dev;
305
306 input_dev->name = pdev->name;
307 input_dev->id.bustype = BUS_HOST;
308 input_dev->open = ep93xx_keypad_open;
309 input_dev->close = ep93xx_keypad_close;
310 input_dev->dev.parent = &pdev->dev;
H Hartley Sweetena5abd952010-01-13 00:34:12 -0800311 input_dev->keycode = keypad->keycodes;
312 input_dev->keycodesize = sizeof(keypad->keycodes[0]);
313 input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700314
315 input_set_drvdata(input_dev, keypad);
316
317 input_dev->evbit[0] = BIT_MASK(EV_KEY);
318 if (keypad->pdata->flags & EP93XX_KEYPAD_AUTOREPEAT)
319 input_dev->evbit[0] |= BIT_MASK(EV_REP);
320
H Hartley Sweetena5abd952010-01-13 00:34:12 -0800321 matrix_keypad_build_keymap(keymap_data, 3,
322 input_dev->keycode, input_dev->keybit);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700323 platform_set_drvdata(pdev, keypad);
324
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800325 err = request_irq(keypad->irq, ep93xx_keypad_irq_handler,
Yong Zhangec4665c2011-09-07 14:04:16 -0700326 0, pdev->name, keypad);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800327 if (err)
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700328 goto failed_free_dev;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700329
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700330 err = input_register_device(input_dev);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800331 if (err)
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700332 goto failed_free_irq;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700333
334 device_init_wakeup(&pdev->dev, 1);
335
336 return 0;
337
338failed_free_irq:
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800339 free_irq(keypad->irq, pdev);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700340 platform_set_drvdata(pdev, NULL);
341failed_free_dev:
342 input_free_device(input_dev);
343failed_put_clk:
344 clk_put(keypad->clk);
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800345failed_free_gpio:
346 ep93xx_keypad_release_gpio(pdev);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700347failed_free_io:
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700348 iounmap(keypad->mmio_base);
349failed_free_mem:
350 release_mem_region(res->start, resource_size(res));
351failed_free:
352 kfree(keypad);
353 return err;
354}
355
356static int __devexit ep93xx_keypad_remove(struct platform_device *pdev)
357{
358 struct ep93xx_keypad *keypad = platform_get_drvdata(pdev);
359 struct resource *res;
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700360
361 free_irq(keypad->irq, pdev);
362
363 platform_set_drvdata(pdev, NULL);
364
365 if (keypad->enabled)
366 clk_disable(keypad->clk);
367 clk_put(keypad->clk);
368
369 input_unregister_device(keypad->input_dev);
370
H Hartley Sweeten60214f02009-12-15 08:39:51 -0800371 ep93xx_keypad_release_gpio(pdev);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700372
373 iounmap(keypad->mmio_base);
374
375 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
376 release_mem_region(res->start, resource_size(res));
377
378 kfree(keypad);
379
380 return 0;
381}
382
383static struct platform_driver ep93xx_keypad_driver = {
384 .driver = {
385 .name = "ep93xx-keypad",
386 .owner = THIS_MODULE,
387 },
388 .probe = ep93xx_keypad_probe,
389 .remove = __devexit_p(ep93xx_keypad_remove),
390 .suspend = ep93xx_keypad_suspend,
391 .resume = ep93xx_keypad_resume,
392};
JJ Ding5146c842011-11-29 11:08:39 -0800393module_platform_driver(ep93xx_keypad_driver);
H Hartley Sweetene06003a2009-04-18 23:43:57 -0700394
395MODULE_LICENSE("GPL");
396MODULE_AUTHOR("H Hartley Sweeten <hsweeten@visionengravers.com>");
397MODULE_DESCRIPTION("EP93xx Matrix Keypad Controller");
398MODULE_ALIAS("platform:ep93xx-keypad");