blob: ceaf1e0ab5407886cf4c3e391c81ec6be34964d4 [file] [log] [blame]
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -04001/*
Eric Miao0e5f11a2008-01-31 00:56:46 -05002 * linux/drivers/input/keyboard/pxa27x_keypad.c
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -04003 *
4 * Driver for the pxa27x matrix keyboard controller.
5 *
6 * Created: Feb 22, 2007
7 * Author: Rodolfo Giometti <giometti@linux.it>
8 *
9 * Based on a previous implementations by Kevin O'Connor
10 * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and
11 * on some suggestions by Nicolas Pitre <nico@cam.org>.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16 */
17
18
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/input.h>
24#include <linux/device.h>
25#include <linux/platform_device.h>
Russell King22d8a732007-08-20 10:19:39 +010026#include <linux/clk.h>
27#include <linux/err.h>
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040028
29#include <asm/mach-types.h>
30#include <asm/mach/arch.h>
31#include <asm/mach/map.h>
32
33#include <asm/arch/hardware.h>
Eric Miao0e5f11a2008-01-31 00:56:46 -050034#include <asm/arch/pxa27x_keypad.h>
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040035
Eric Miao9c60deb2008-01-31 00:59:15 -050036/*
37 * Keypad Controller registers
38 */
39#define KPC 0x0000 /* Keypad Control register */
40#define KPDK 0x0008 /* Keypad Direct Key register */
41#define KPREC 0x0010 /* Keypad Rotary Encoder register */
42#define KPMK 0x0018 /* Keypad Matrix Key register */
43#define KPAS 0x0020 /* Keypad Automatic Scan register */
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040044
Eric Miao9c60deb2008-01-31 00:59:15 -050045/* Keypad Automatic Scan Multiple Key Presser register 0-3 */
46#define KPASMKP0 0x0028
47#define KPASMKP1 0x0030
48#define KPASMKP2 0x0038
49#define KPASMKP3 0x0040
50#define KPKDI 0x0048
51
52/* bit definitions */
Eric Miaod7416f92008-01-31 00:58:52 -050053#define KPC_MKRN(n) ((((n) & 0x7) - 1) << 26) /* matrix key row number */
54#define KPC_MKCN(n) ((((n) & 0x7) - 1) << 23) /* matrix key column number */
55#define KPC_DKN(n) ((((n) & 0x7) - 1) << 6) /* direct key number */
56
Eric Miao9c60deb2008-01-31 00:59:15 -050057#define KPC_AS (0x1 << 30) /* Automatic Scan bit */
58#define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */
59#define KPC_MI (0x1 << 22) /* Matrix interrupt bit */
60#define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */
61
62#define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */
63#define KPC_MS_ALL (0xff << 13)
64
65#define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */
66#define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */
67#define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */
68#define KPC_DI (0x1 << 5) /* Direct key interrupt bit */
69#define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */
70#define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */
71#define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */
72#define KPC_DE (0x1 << 1) /* Direct Keypad Enable */
73#define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */
74
Eric Miao62059d92008-01-31 00:59:03 -050075#define KPDK_DKP (0x1 << 31)
76#define KPDK_DK(n) ((n) & 0xff)
77
Eric Miao9c60deb2008-01-31 00:59:15 -050078#define KPREC_OF1 (0x1 << 31)
79#define kPREC_UF1 (0x1 << 30)
80#define KPREC_OF0 (0x1 << 15)
81#define KPREC_UF0 (0x1 << 14)
82
83#define KPREC_RECOUNT0(n) ((n) & 0xff)
84#define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff)
85
86#define KPMK_MKP (0x1 << 31)
87#define KPAS_SO (0x1 << 31)
88#define KPASMKPx_SO (0x1 << 31)
89
90#define KPAS_MUKP(n) (((n) >> 26) & 0x1f)
91#define KPAS_RP(n) (((n) >> 4) & 0xf)
92#define KPAS_CP(n) ((n) & 0xf)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -040093
Eric Miao1814db62008-01-31 00:58:37 -050094#define KPASMKP_MKC_MASK (0xff)
95
Eric Miao9c60deb2008-01-31 00:59:15 -050096#define keypad_readl(off) __raw_readl(keypad->mmio_base + (off))
97#define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off))
98
Eric Miao1814db62008-01-31 00:58:37 -050099#define MAX_MATRIX_KEY_NUM (8 * 8)
100
101struct pxa27x_keypad {
102 struct pxa27x_keypad_platform_data *pdata;
103
104 struct clk *clk;
105 struct input_dev *input_dev;
Eric Miao9c60deb2008-01-31 00:59:15 -0500106 void __iomem *mmio_base;
Eric Miao1814db62008-01-31 00:58:37 -0500107
108 /* matrix key code map */
109 unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
110
111 /* state row bits of each column scan */
112 uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
Eric Miao62059d92008-01-31 00:59:03 -0500113 uint32_t direct_key_state;
114
115 unsigned int direct_key_mask;
116
117 int rotary_rel_code[2];
118 int rotary_up_key[2];
119 int rotary_down_key[2];
Eric Miao1814db62008-01-31 00:58:37 -0500120};
121
122static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
123{
124 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
125 struct input_dev *input_dev = keypad->input_dev;
126 unsigned int *key;
127 int i;
128
129 key = &pdata->matrix_key_map[0];
130 for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
131 int row = ((*key) >> 28) & 0xf;
132 int col = ((*key) >> 24) & 0xf;
133 int code = (*key) & 0xffffff;
134
135 keypad->matrix_keycodes[(row << 3) + col] = code;
136 set_bit(code, input_dev->keybit);
137 }
Eric Miao62059d92008-01-31 00:59:03 -0500138
139 keypad->rotary_up_key[0] = pdata->rotary0_up_key;
140 keypad->rotary_up_key[1] = pdata->rotary1_up_key;
141 keypad->rotary_down_key[0] = pdata->rotary0_down_key;
142 keypad->rotary_down_key[1] = pdata->rotary1_down_key;
143 keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
144 keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
145
146 if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
147 set_bit(pdata->rotary0_up_key, input_dev->keybit);
148 set_bit(pdata->rotary0_down_key, input_dev->keybit);
149 } else
150 set_bit(pdata->rotary0_rel_code, input_dev->relbit);
151
152 if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
153 set_bit(pdata->rotary1_up_key, input_dev->keybit);
154 set_bit(pdata->rotary1_down_key, input_dev->keybit);
155 } else
156 set_bit(pdata->rotary1_rel_code, input_dev->relbit);
Eric Miao1814db62008-01-31 00:58:37 -0500157}
158
159static inline unsigned int lookup_matrix_keycode(
160 struct pxa27x_keypad *keypad, int row, int col)
161{
162 return keypad->matrix_keycodes[(row << 3) + col];
163}
164
165static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
166{
167 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
168 int row, col, num_keys_pressed = 0;
169 uint32_t new_state[MAX_MATRIX_KEY_COLS];
Eric Miao9c60deb2008-01-31 00:59:15 -0500170 uint32_t kpas = keypad_readl(KPAS);
Eric Miao1814db62008-01-31 00:58:37 -0500171
172 num_keys_pressed = KPAS_MUKP(kpas);
173
174 memset(new_state, 0, sizeof(new_state));
175
176 if (num_keys_pressed == 0)
177 goto scan;
178
179 if (num_keys_pressed == 1) {
180 col = KPAS_CP(kpas);
181 row = KPAS_RP(kpas);
182
183 /* if invalid row/col, treat as no key pressed */
184 if (col >= pdata->matrix_key_cols ||
185 row >= pdata->matrix_key_rows)
186 goto scan;
187
188 new_state[col] = (1 << row);
189 goto scan;
190 }
191
192 if (num_keys_pressed > 1) {
Eric Miao9c60deb2008-01-31 00:59:15 -0500193 uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
194 uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
195 uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
196 uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
Eric Miao1814db62008-01-31 00:58:37 -0500197
198 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
199 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
200 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
201 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
202 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
203 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
204 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
205 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
206 }
207scan:
208 for (col = 0; col < pdata->matrix_key_cols; col++) {
209 uint32_t bits_changed;
210
211 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
212 if (bits_changed == 0)
213 continue;
214
215 for (row = 0; row < pdata->matrix_key_rows; row++) {
216 if ((bits_changed & (1 << row)) == 0)
217 continue;
218
219 input_report_key(keypad->input_dev,
220 lookup_matrix_keycode(keypad, row, col),
221 new_state[col] & (1 << row));
222 }
223 }
224 input_sync(keypad->input_dev);
225 memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
226}
Russell King22d8a732007-08-20 10:19:39 +0100227
Eric Miaod7416f92008-01-31 00:58:52 -0500228#define DEFAULT_KPREC (0x007f007f)
229
Eric Miao62059d92008-01-31 00:59:03 -0500230static inline int rotary_delta(uint32_t kprec)
231{
232 if (kprec & KPREC_OF0)
233 return (kprec & 0xff) + 0x7f;
234 else if (kprec & KPREC_UF0)
235 return (kprec & 0xff) - 0x7f - 0xff;
236 else
237 return (kprec & 0xff) - 0x7f;
238}
239
240static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
241{
242 struct input_dev *dev = keypad->input_dev;
243
244 if (delta == 0)
245 return;
246
247 if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
248 int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
249 keypad->rotary_down_key[r];
250
251 /* simulate a press-n-release */
252 input_report_key(dev, keycode, 1);
253 input_sync(dev);
254 input_report_key(dev, keycode, 0);
255 input_sync(dev);
256 } else {
257 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
258 input_sync(dev);
259 }
260}
261
262static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
263{
264 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
265 uint32_t kprec;
266
267 /* read and reset to default count value */
Eric Miao9c60deb2008-01-31 00:59:15 -0500268 kprec = keypad_readl(KPREC);
269 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miao62059d92008-01-31 00:59:03 -0500270
271 if (pdata->enable_rotary0)
272 report_rotary_event(keypad, 0, rotary_delta(kprec));
273
274 if (pdata->enable_rotary1)
275 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
276}
277
278static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
279{
280 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
281 unsigned int new_state;
282 uint32_t kpdk, bits_changed;
283 int i;
284
Eric Miao9c60deb2008-01-31 00:59:15 -0500285 kpdk = keypad_readl(KPDK);
Eric Miao62059d92008-01-31 00:59:03 -0500286
287 if (pdata->enable_rotary0 || pdata->enable_rotary1)
288 pxa27x_keypad_scan_rotary(keypad);
289
290 if (pdata->direct_key_map == NULL)
291 return;
292
293 new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
294 bits_changed = keypad->direct_key_state ^ new_state;
295
296 if (bits_changed == 0)
297 return;
298
299 for (i = 0; i < pdata->direct_key_num; i++) {
300 if (bits_changed & (1 << i))
301 input_report_key(keypad->input_dev,
302 pdata->direct_key_map[i],
303 (new_state & (1 << i)));
304 }
305 input_sync(keypad->input_dev);
306 keypad->direct_key_state = new_state;
307}
308
Eric Miao0e5f11a2008-01-31 00:56:46 -0500309static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400310{
Eric Miao1814db62008-01-31 00:58:37 -0500311 struct pxa27x_keypad *keypad = dev_id;
Eric Miao9c60deb2008-01-31 00:59:15 -0500312 unsigned long kpc = keypad_readl(KPC);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400313
Eric Miao62059d92008-01-31 00:59:03 -0500314 if (kpc & KPC_DI)
315 pxa27x_keypad_scan_direct(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400316
Eric Miao1814db62008-01-31 00:58:37 -0500317 if (kpc & KPC_MI)
318 pxa27x_keypad_scan_matrix(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400319
320 return IRQ_HANDLED;
321}
322
Eric Miaod7416f92008-01-31 00:58:52 -0500323static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
324{
325 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
Eric Miao62059d92008-01-31 00:59:03 -0500326 unsigned int mask = 0, direct_key_num = 0;
Eric Miaod7416f92008-01-31 00:58:52 -0500327 unsigned long kpc = 0;
328
329 /* enable matrix keys with automatic scan */
330 if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
331 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
332 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
333 KPC_MKCN(pdata->matrix_key_cols);
334 }
335
Eric Miao62059d92008-01-31 00:59:03 -0500336 /* enable rotary key, debounce interval same as direct keys */
337 if (pdata->enable_rotary0) {
338 mask |= 0x03;
339 direct_key_num = 2;
340 kpc |= KPC_REE0;
341 }
Eric Miaod7416f92008-01-31 00:58:52 -0500342
Eric Miao62059d92008-01-31 00:59:03 -0500343 if (pdata->enable_rotary1) {
344 mask |= 0x0c;
345 direct_key_num = 4;
346 kpc |= KPC_REE1;
347 }
348
349 if (pdata->direct_key_num > direct_key_num)
350 direct_key_num = pdata->direct_key_num;
351
352 keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
353
354 /* enable direct key */
355 if (direct_key_num)
356 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
357
Eric Miao9c60deb2008-01-31 00:59:15 -0500358 keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
359 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miaod7416f92008-01-31 00:58:52 -0500360}
361
Eric Miao0e5f11a2008-01-31 00:56:46 -0500362static int pxa27x_keypad_open(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400363{
Eric Miao1814db62008-01-31 00:58:37 -0500364 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
365
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400366 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500367 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500368 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400369
370 return 0;
371}
372
Eric Miao0e5f11a2008-01-31 00:56:46 -0500373static void pxa27x_keypad_close(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400374{
Eric Miao1814db62008-01-31 00:58:37 -0500375 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
376
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400377 /* Disable clock unit */
Eric Miao1814db62008-01-31 00:58:37 -0500378 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400379}
380
381#ifdef CONFIG_PM
Eric Miao0e5f11a2008-01-31 00:56:46 -0500382static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400383{
Eric Miao1814db62008-01-31 00:58:37 -0500384 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400385
Eric Miaod7416f92008-01-31 00:58:52 -0500386 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400387 return 0;
388}
389
Eric Miao0e5f11a2008-01-31 00:56:46 -0500390static int pxa27x_keypad_resume(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400391{
Eric Miao1814db62008-01-31 00:58:37 -0500392 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao1814db62008-01-31 00:58:37 -0500393 struct input_dev *input_dev = keypad->input_dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400394
395 mutex_lock(&input_dev->mutex);
396
397 if (input_dev->users) {
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400398 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500399 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500400 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400401 }
402
403 mutex_unlock(&input_dev->mutex);
404
405 return 0;
406}
407#else
Eric Miao0e5f11a2008-01-31 00:56:46 -0500408#define pxa27x_keypad_suspend NULL
409#define pxa27x_keypad_resume NULL
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400410#endif
411
Eric Miao9c60deb2008-01-31 00:59:15 -0500412#define res_size(res) ((res)->end - (res)->start + 1)
413
Eric Miao0e5f11a2008-01-31 00:56:46 -0500414static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400415{
Eric Miao1814db62008-01-31 00:58:37 -0500416 struct pxa27x_keypad *keypad;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400417 struct input_dev *input_dev;
Eric Miao9c60deb2008-01-31 00:59:15 -0500418 struct resource *res;
419 int irq, error;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400420
Eric Miao1814db62008-01-31 00:58:37 -0500421 keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
422 if (keypad == NULL) {
423 dev_err(&pdev->dev, "failed to allocate driver data\n");
424 return -ENOMEM;
425 }
426
427 keypad->pdata = pdev->dev.platform_data;
428 if (keypad->pdata == NULL) {
429 dev_err(&pdev->dev, "no platform data defined\n");
430 error = -EINVAL;
431 goto failed_free;
432 }
433
Eric Miao9c60deb2008-01-31 00:59:15 -0500434 irq = platform_get_irq(pdev, 0);
435 if (irq < 0) {
436 dev_err(&pdev->dev, "failed to get keypad irq\n");
437 error = -ENXIO;
438 goto failed_free;
439 }
440
441 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
442 if (res == NULL) {
443 dev_err(&pdev->dev, "failed to get I/O memory\n");
444 error = -ENXIO;
445 goto failed_free;
446 }
447
448 res = request_mem_region(res->start, res_size(res), pdev->name);
449 if (res == NULL) {
450 dev_err(&pdev->dev, "failed to request I/O memory\n");
451 error = -EBUSY;
452 goto failed_free;
453 }
454
455 keypad->mmio_base = ioremap(res->start, res_size(res));
456 if (keypad->mmio_base == NULL) {
457 dev_err(&pdev->dev, "failed to remap I/O memory\n");
458 error = -ENXIO;
459 goto failed_free_mem;
460 }
461
Eric Miao1814db62008-01-31 00:58:37 -0500462 keypad->clk = clk_get(&pdev->dev, "KBDCLK");
463 if (IS_ERR(keypad->clk)) {
464 dev_err(&pdev->dev, "failed to get keypad clock\n");
465 error = PTR_ERR(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500466 goto failed_free_io;
Russell King22d8a732007-08-20 10:19:39 +0100467 }
468
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400469 /* Create and register the input driver. */
470 input_dev = input_allocate_device();
471 if (!input_dev) {
Eric Miao1814db62008-01-31 00:58:37 -0500472 dev_err(&pdev->dev, "failed to allocate input device\n");
Russell King22d8a732007-08-20 10:19:39 +0100473 error = -ENOMEM;
Eric Miao1814db62008-01-31 00:58:37 -0500474 goto failed_put_clk;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400475 }
476
Eric Miao9c60deb2008-01-31 00:59:15 -0500477 input_dev->name = pdev->name;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400478 input_dev->id.bustype = BUS_HOST;
Eric Miao0e5f11a2008-01-31 00:56:46 -0500479 input_dev->open = pxa27x_keypad_open;
480 input_dev->close = pxa27x_keypad_close;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400481 input_dev->dev.parent = &pdev->dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400482
Eric Miao1814db62008-01-31 00:58:37 -0500483 keypad->input_dev = input_dev;
484 input_set_drvdata(input_dev, keypad);
485
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700486 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP) |
487 BIT_MASK(EV_REL);
Eric Miao1814db62008-01-31 00:58:37 -0500488
489 pxa27x_keypad_build_keycode(keypad);
Eric Miao1814db62008-01-31 00:58:37 -0500490 platform_set_drvdata(pdev, keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400491
Eric Miao9c60deb2008-01-31 00:59:15 -0500492 error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED,
493 pdev->name, keypad);
494 if (error) {
495 dev_err(&pdev->dev, "failed to request IRQ\n");
496 goto failed_free_dev;
497 }
498
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400499 /* Register the input device */
500 error = input_register_device(input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500501 if (error) {
502 dev_err(&pdev->dev, "failed to register input device\n");
503 goto failed_free_irq;
504 }
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400505
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400506 return 0;
507
Eric Miao9c60deb2008-01-31 00:59:15 -0500508failed_free_irq:
509 free_irq(irq, pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400510 platform_set_drvdata(pdev, NULL);
Eric Miao9c60deb2008-01-31 00:59:15 -0500511failed_free_dev:
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400512 input_free_device(input_dev);
Eric Miao1814db62008-01-31 00:58:37 -0500513failed_put_clk:
514 clk_put(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500515failed_free_io:
516 iounmap(keypad->mmio_base);
517failed_free_mem:
518 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500519failed_free:
520 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400521 return error;
522}
523
Eric Miao0e5f11a2008-01-31 00:56:46 -0500524static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400525{
Eric Miao1814db62008-01-31 00:58:37 -0500526 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500527 struct resource *res;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400528
Eric Miao9c60deb2008-01-31 00:59:15 -0500529 free_irq(platform_get_irq(pdev, 0), pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400530
Eric Miao1814db62008-01-31 00:58:37 -0500531 clk_disable(keypad->clk);
532 clk_put(keypad->clk);
533
534 input_unregister_device(keypad->input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500535 input_free_device(keypad->input_dev);
536
537 iounmap(keypad->mmio_base);
538
539 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
540 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500541
542 platform_set_drvdata(pdev, NULL);
543 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400544 return 0;
545}
546
Eric Miao0e5f11a2008-01-31 00:56:46 -0500547static struct platform_driver pxa27x_keypad_driver = {
548 .probe = pxa27x_keypad_probe,
549 .remove = __devexit_p(pxa27x_keypad_remove),
550 .suspend = pxa27x_keypad_suspend,
551 .resume = pxa27x_keypad_resume,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400552 .driver = {
Eric Miao9c60deb2008-01-31 00:59:15 -0500553 .name = "pxa27x-keypad",
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400554 },
555};
556
Eric Miao0e5f11a2008-01-31 00:56:46 -0500557static int __init pxa27x_keypad_init(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400558{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500559 return platform_driver_register(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400560}
561
Eric Miao0e5f11a2008-01-31 00:56:46 -0500562static void __exit pxa27x_keypad_exit(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400563{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500564 platform_driver_unregister(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400565}
566
Eric Miao0e5f11a2008-01-31 00:56:46 -0500567module_init(pxa27x_keypad_init);
568module_exit(pxa27x_keypad_exit);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400569
Eric Miao0e5f11a2008-01-31 00:56:46 -0500570MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400571MODULE_LICENSE("GPL");