blob: 6f1516f5075072ebde372743713e504685b8d990 [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 */
Samuel Ortiz688dad42008-03-20 09:48:14 -040053#define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */
54#define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */
55#define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */
Eric Miaod7416f92008-01-31 00:58:52 -050056
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
Eric Miao39ab9dd2008-06-02 11:20:55 -0400108 int irq;
109
Eric Miao1814db62008-01-31 00:58:37 -0500110 /* matrix key code map */
111 unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM];
112
113 /* state row bits of each column scan */
114 uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS];
Eric Miao62059d92008-01-31 00:59:03 -0500115 uint32_t direct_key_state;
116
117 unsigned int direct_key_mask;
118
119 int rotary_rel_code[2];
120 int rotary_up_key[2];
121 int rotary_down_key[2];
Eric Miao1814db62008-01-31 00:58:37 -0500122};
123
124static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad)
125{
126 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
127 struct input_dev *input_dev = keypad->input_dev;
128 unsigned int *key;
129 int i;
130
131 key = &pdata->matrix_key_map[0];
132 for (i = 0; i < pdata->matrix_key_map_size; i++, key++) {
133 int row = ((*key) >> 28) & 0xf;
134 int col = ((*key) >> 24) & 0xf;
135 int code = (*key) & 0xffffff;
136
137 keypad->matrix_keycodes[(row << 3) + col] = code;
138 set_bit(code, input_dev->keybit);
139 }
Eric Miao62059d92008-01-31 00:59:03 -0500140
Antonio Ospite471637a2008-05-28 14:35:52 -0400141 for (i = 0; i < pdata->direct_key_num; i++)
142 set_bit(pdata->direct_key_map[i], input_dev->keybit);
143
Eric Miao62059d92008-01-31 00:59:03 -0500144 keypad->rotary_up_key[0] = pdata->rotary0_up_key;
145 keypad->rotary_up_key[1] = pdata->rotary1_up_key;
146 keypad->rotary_down_key[0] = pdata->rotary0_down_key;
147 keypad->rotary_down_key[1] = pdata->rotary1_down_key;
148 keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
149 keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
150
Antonio Ospite471637a2008-05-28 14:35:52 -0400151 if (pdata->enable_rotary0) {
152 if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
153 set_bit(pdata->rotary0_up_key, input_dev->keybit);
154 set_bit(pdata->rotary0_down_key, input_dev->keybit);
155 } else
156 set_bit(pdata->rotary0_rel_code, input_dev->relbit);
157 }
Eric Miao62059d92008-01-31 00:59:03 -0500158
Antonio Ospite471637a2008-05-28 14:35:52 -0400159 if (pdata->enable_rotary1) {
160 if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
161 set_bit(pdata->rotary1_up_key, input_dev->keybit);
162 set_bit(pdata->rotary1_down_key, input_dev->keybit);
163 } else
164 set_bit(pdata->rotary1_rel_code, input_dev->relbit);
165 }
Eric Miao1814db62008-01-31 00:58:37 -0500166}
167
168static inline unsigned int lookup_matrix_keycode(
169 struct pxa27x_keypad *keypad, int row, int col)
170{
171 return keypad->matrix_keycodes[(row << 3) + col];
172}
173
174static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
175{
176 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
177 int row, col, num_keys_pressed = 0;
178 uint32_t new_state[MAX_MATRIX_KEY_COLS];
Eric Miao9c60deb2008-01-31 00:59:15 -0500179 uint32_t kpas = keypad_readl(KPAS);
Eric Miao1814db62008-01-31 00:58:37 -0500180
181 num_keys_pressed = KPAS_MUKP(kpas);
182
183 memset(new_state, 0, sizeof(new_state));
184
185 if (num_keys_pressed == 0)
186 goto scan;
187
188 if (num_keys_pressed == 1) {
189 col = KPAS_CP(kpas);
190 row = KPAS_RP(kpas);
191
192 /* if invalid row/col, treat as no key pressed */
193 if (col >= pdata->matrix_key_cols ||
194 row >= pdata->matrix_key_rows)
195 goto scan;
196
197 new_state[col] = (1 << row);
198 goto scan;
199 }
200
201 if (num_keys_pressed > 1) {
Eric Miao9c60deb2008-01-31 00:59:15 -0500202 uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
203 uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
204 uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
205 uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
Eric Miao1814db62008-01-31 00:58:37 -0500206
207 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
208 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
209 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
210 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
211 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
212 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
213 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
214 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
215 }
216scan:
217 for (col = 0; col < pdata->matrix_key_cols; col++) {
218 uint32_t bits_changed;
219
220 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
221 if (bits_changed == 0)
222 continue;
223
224 for (row = 0; row < pdata->matrix_key_rows; row++) {
225 if ((bits_changed & (1 << row)) == 0)
226 continue;
227
228 input_report_key(keypad->input_dev,
229 lookup_matrix_keycode(keypad, row, col),
230 new_state[col] & (1 << row));
231 }
232 }
233 input_sync(keypad->input_dev);
234 memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
235}
Russell King22d8a732007-08-20 10:19:39 +0100236
Eric Miaod7416f92008-01-31 00:58:52 -0500237#define DEFAULT_KPREC (0x007f007f)
238
Eric Miao62059d92008-01-31 00:59:03 -0500239static inline int rotary_delta(uint32_t kprec)
240{
241 if (kprec & KPREC_OF0)
242 return (kprec & 0xff) + 0x7f;
243 else if (kprec & KPREC_UF0)
244 return (kprec & 0xff) - 0x7f - 0xff;
245 else
246 return (kprec & 0xff) - 0x7f;
247}
248
249static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
250{
251 struct input_dev *dev = keypad->input_dev;
252
253 if (delta == 0)
254 return;
255
256 if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
257 int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
258 keypad->rotary_down_key[r];
259
260 /* simulate a press-n-release */
261 input_report_key(dev, keycode, 1);
262 input_sync(dev);
263 input_report_key(dev, keycode, 0);
264 input_sync(dev);
265 } else {
266 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
267 input_sync(dev);
268 }
269}
270
271static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
272{
273 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
274 uint32_t kprec;
275
276 /* read and reset to default count value */
Eric Miao9c60deb2008-01-31 00:59:15 -0500277 kprec = keypad_readl(KPREC);
278 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miao62059d92008-01-31 00:59:03 -0500279
280 if (pdata->enable_rotary0)
281 report_rotary_event(keypad, 0, rotary_delta(kprec));
282
283 if (pdata->enable_rotary1)
284 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
285}
286
287static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
288{
289 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
290 unsigned int new_state;
291 uint32_t kpdk, bits_changed;
292 int i;
293
Eric Miao9c60deb2008-01-31 00:59:15 -0500294 kpdk = keypad_readl(KPDK);
Eric Miao62059d92008-01-31 00:59:03 -0500295
296 if (pdata->enable_rotary0 || pdata->enable_rotary1)
297 pxa27x_keypad_scan_rotary(keypad);
298
299 if (pdata->direct_key_map == NULL)
300 return;
301
302 new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
303 bits_changed = keypad->direct_key_state ^ new_state;
304
305 if (bits_changed == 0)
306 return;
307
308 for (i = 0; i < pdata->direct_key_num; i++) {
309 if (bits_changed & (1 << i))
310 input_report_key(keypad->input_dev,
311 pdata->direct_key_map[i],
312 (new_state & (1 << i)));
313 }
314 input_sync(keypad->input_dev);
315 keypad->direct_key_state = new_state;
316}
317
Eric Miao0e5f11a2008-01-31 00:56:46 -0500318static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400319{
Eric Miao1814db62008-01-31 00:58:37 -0500320 struct pxa27x_keypad *keypad = dev_id;
Eric Miao9c60deb2008-01-31 00:59:15 -0500321 unsigned long kpc = keypad_readl(KPC);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400322
Eric Miao62059d92008-01-31 00:59:03 -0500323 if (kpc & KPC_DI)
324 pxa27x_keypad_scan_direct(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400325
Eric Miao1814db62008-01-31 00:58:37 -0500326 if (kpc & KPC_MI)
327 pxa27x_keypad_scan_matrix(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400328
329 return IRQ_HANDLED;
330}
331
Eric Miaod7416f92008-01-31 00:58:52 -0500332static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
333{
334 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
Eric Miao62059d92008-01-31 00:59:03 -0500335 unsigned int mask = 0, direct_key_num = 0;
Eric Miaod7416f92008-01-31 00:58:52 -0500336 unsigned long kpc = 0;
337
338 /* enable matrix keys with automatic scan */
339 if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
340 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
341 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
342 KPC_MKCN(pdata->matrix_key_cols);
343 }
344
Eric Miao62059d92008-01-31 00:59:03 -0500345 /* enable rotary key, debounce interval same as direct keys */
346 if (pdata->enable_rotary0) {
347 mask |= 0x03;
348 direct_key_num = 2;
349 kpc |= KPC_REE0;
350 }
Eric Miaod7416f92008-01-31 00:58:52 -0500351
Eric Miao62059d92008-01-31 00:59:03 -0500352 if (pdata->enable_rotary1) {
353 mask |= 0x0c;
354 direct_key_num = 4;
355 kpc |= KPC_REE1;
356 }
357
358 if (pdata->direct_key_num > direct_key_num)
359 direct_key_num = pdata->direct_key_num;
360
361 keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
362
363 /* enable direct key */
364 if (direct_key_num)
365 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
366
Eric Miao9c60deb2008-01-31 00:59:15 -0500367 keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
368 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miao76cb44e2008-01-31 00:59:23 -0500369 keypad_writel(KPKDI, pdata->debounce_interval);
Eric Miaod7416f92008-01-31 00:58:52 -0500370}
371
Eric Miao0e5f11a2008-01-31 00:56:46 -0500372static int pxa27x_keypad_open(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400373{
Eric Miao1814db62008-01-31 00:58:37 -0500374 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
375
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400376 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500377 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500378 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400379
380 return 0;
381}
382
Eric Miao0e5f11a2008-01-31 00:56:46 -0500383static void pxa27x_keypad_close(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400384{
Eric Miao1814db62008-01-31 00:58:37 -0500385 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
386
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400387 /* Disable clock unit */
Eric Miao1814db62008-01-31 00:58:37 -0500388 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400389}
390
391#ifdef CONFIG_PM
Eric Miao0e5f11a2008-01-31 00:56:46 -0500392static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400393{
Eric Miao1814db62008-01-31 00:58:37 -0500394 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400395
Eric Miaod7416f92008-01-31 00:58:52 -0500396 clk_disable(keypad->clk);
Eric Miao39ab9dd2008-06-02 11:20:55 -0400397
398 if (device_may_wakeup(&pdev->dev))
399 enable_irq_wake(keypad->irq);
400
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400401 return 0;
402}
403
Eric Miao0e5f11a2008-01-31 00:56:46 -0500404static int pxa27x_keypad_resume(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400405{
Eric Miao1814db62008-01-31 00:58:37 -0500406 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao1814db62008-01-31 00:58:37 -0500407 struct input_dev *input_dev = keypad->input_dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400408
Eric Miao39ab9dd2008-06-02 11:20:55 -0400409 if (device_may_wakeup(&pdev->dev))
410 disable_irq_wake(keypad->irq);
411
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400412 mutex_lock(&input_dev->mutex);
413
414 if (input_dev->users) {
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400415 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500416 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500417 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400418 }
419
420 mutex_unlock(&input_dev->mutex);
421
422 return 0;
423}
424#else
Eric Miao0e5f11a2008-01-31 00:56:46 -0500425#define pxa27x_keypad_suspend NULL
426#define pxa27x_keypad_resume NULL
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400427#endif
428
Eric Miao9c60deb2008-01-31 00:59:15 -0500429#define res_size(res) ((res)->end - (res)->start + 1)
430
Eric Miao0e5f11a2008-01-31 00:56:46 -0500431static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400432{
Eric Miao1814db62008-01-31 00:58:37 -0500433 struct pxa27x_keypad *keypad;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400434 struct input_dev *input_dev;
Eric Miao9c60deb2008-01-31 00:59:15 -0500435 struct resource *res;
436 int irq, error;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400437
Eric Miao1814db62008-01-31 00:58:37 -0500438 keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
439 if (keypad == NULL) {
440 dev_err(&pdev->dev, "failed to allocate driver data\n");
441 return -ENOMEM;
442 }
443
444 keypad->pdata = pdev->dev.platform_data;
445 if (keypad->pdata == NULL) {
446 dev_err(&pdev->dev, "no platform data defined\n");
447 error = -EINVAL;
448 goto failed_free;
449 }
450
Eric Miao9c60deb2008-01-31 00:59:15 -0500451 irq = platform_get_irq(pdev, 0);
452 if (irq < 0) {
453 dev_err(&pdev->dev, "failed to get keypad irq\n");
454 error = -ENXIO;
455 goto failed_free;
456 }
457
458 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
459 if (res == NULL) {
460 dev_err(&pdev->dev, "failed to get I/O memory\n");
461 error = -ENXIO;
462 goto failed_free;
463 }
464
465 res = request_mem_region(res->start, res_size(res), pdev->name);
466 if (res == NULL) {
467 dev_err(&pdev->dev, "failed to request I/O memory\n");
468 error = -EBUSY;
469 goto failed_free;
470 }
471
472 keypad->mmio_base = ioremap(res->start, res_size(res));
473 if (keypad->mmio_base == NULL) {
474 dev_err(&pdev->dev, "failed to remap I/O memory\n");
475 error = -ENXIO;
476 goto failed_free_mem;
477 }
478
Eric Miao1814db62008-01-31 00:58:37 -0500479 keypad->clk = clk_get(&pdev->dev, "KBDCLK");
480 if (IS_ERR(keypad->clk)) {
481 dev_err(&pdev->dev, "failed to get keypad clock\n");
482 error = PTR_ERR(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500483 goto failed_free_io;
Russell King22d8a732007-08-20 10:19:39 +0100484 }
485
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400486 /* Create and register the input driver. */
487 input_dev = input_allocate_device();
488 if (!input_dev) {
Eric Miao1814db62008-01-31 00:58:37 -0500489 dev_err(&pdev->dev, "failed to allocate input device\n");
Russell King22d8a732007-08-20 10:19:39 +0100490 error = -ENOMEM;
Eric Miao1814db62008-01-31 00:58:37 -0500491 goto failed_put_clk;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400492 }
493
Eric Miao9c60deb2008-01-31 00:59:15 -0500494 input_dev->name = pdev->name;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400495 input_dev->id.bustype = BUS_HOST;
Eric Miao0e5f11a2008-01-31 00:56:46 -0500496 input_dev->open = pxa27x_keypad_open;
497 input_dev->close = pxa27x_keypad_close;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400498 input_dev->dev.parent = &pdev->dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400499
Eric Miao1814db62008-01-31 00:58:37 -0500500 keypad->input_dev = input_dev;
501 input_set_drvdata(input_dev, keypad);
502
Antonio Ospite471637a2008-05-28 14:35:52 -0400503 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
504 if ((keypad->pdata->enable_rotary0 &&
505 keypad->pdata->rotary0_rel_code) ||
506 (keypad->pdata->enable_rotary1 &&
507 keypad->pdata->rotary1_rel_code)) {
508 input_dev->evbit[0] |= BIT_MASK(EV_REL);
509 }
Eric Miao1814db62008-01-31 00:58:37 -0500510
511 pxa27x_keypad_build_keycode(keypad);
Eric Miao1814db62008-01-31 00:58:37 -0500512 platform_set_drvdata(pdev, keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400513
Eric Miao9c60deb2008-01-31 00:59:15 -0500514 error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED,
515 pdev->name, keypad);
516 if (error) {
517 dev_err(&pdev->dev, "failed to request IRQ\n");
518 goto failed_free_dev;
519 }
520
Eric Miao39ab9dd2008-06-02 11:20:55 -0400521 keypad->irq = irq;
522
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400523 /* Register the input device */
524 error = input_register_device(input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500525 if (error) {
526 dev_err(&pdev->dev, "failed to register input device\n");
527 goto failed_free_irq;
528 }
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400529
Eric Miao39ab9dd2008-06-02 11:20:55 -0400530 device_init_wakeup(&pdev->dev, 1);
531
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400532 return 0;
533
Eric Miao9c60deb2008-01-31 00:59:15 -0500534failed_free_irq:
535 free_irq(irq, pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400536 platform_set_drvdata(pdev, NULL);
Eric Miao9c60deb2008-01-31 00:59:15 -0500537failed_free_dev:
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400538 input_free_device(input_dev);
Eric Miao1814db62008-01-31 00:58:37 -0500539failed_put_clk:
540 clk_put(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500541failed_free_io:
542 iounmap(keypad->mmio_base);
543failed_free_mem:
544 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500545failed_free:
546 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400547 return error;
548}
549
Eric Miao0e5f11a2008-01-31 00:56:46 -0500550static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400551{
Eric Miao1814db62008-01-31 00:58:37 -0500552 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500553 struct resource *res;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400554
Eric Miao39ab9dd2008-06-02 11:20:55 -0400555 free_irq(keypad->irq, pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400556
Eric Miao1814db62008-01-31 00:58:37 -0500557 clk_disable(keypad->clk);
558 clk_put(keypad->clk);
559
560 input_unregister_device(keypad->input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500561 input_free_device(keypad->input_dev);
562
563 iounmap(keypad->mmio_base);
564
565 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
566 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500567
568 platform_set_drvdata(pdev, NULL);
569 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400570 return 0;
571}
572
Kay Sieversd7b52472008-04-18 00:24:42 -0400573/* work with hotplug and coldplug */
574MODULE_ALIAS("platform:pxa27x-keypad");
575
Eric Miao0e5f11a2008-01-31 00:56:46 -0500576static struct platform_driver pxa27x_keypad_driver = {
577 .probe = pxa27x_keypad_probe,
578 .remove = __devexit_p(pxa27x_keypad_remove),
579 .suspend = pxa27x_keypad_suspend,
580 .resume = pxa27x_keypad_resume,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400581 .driver = {
Eric Miao9c60deb2008-01-31 00:59:15 -0500582 .name = "pxa27x-keypad",
Kay Sieversd7b52472008-04-18 00:24:42 -0400583 .owner = THIS_MODULE,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400584 },
585};
586
Eric Miao0e5f11a2008-01-31 00:56:46 -0500587static int __init pxa27x_keypad_init(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400588{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500589 return platform_driver_register(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400590}
591
Eric Miao0e5f11a2008-01-31 00:56:46 -0500592static void __exit pxa27x_keypad_exit(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400593{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500594 platform_driver_unregister(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400595}
596
Eric Miao0e5f11a2008-01-31 00:56:46 -0500597module_init(pxa27x_keypad_init);
598module_exit(pxa27x_keypad_exit);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400599
Eric Miao0e5f11a2008-01-31 00:56:46 -0500600MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400601MODULE_LICENSE("GPL");