blob: 45767e73f071e3859ffccebaa7a82d1578019530 [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
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
Antonio Ospite471637a2008-05-28 14:35:52 -0400139 for (i = 0; i < pdata->direct_key_num; i++)
140 set_bit(pdata->direct_key_map[i], input_dev->keybit);
141
Eric Miao62059d92008-01-31 00:59:03 -0500142 keypad->rotary_up_key[0] = pdata->rotary0_up_key;
143 keypad->rotary_up_key[1] = pdata->rotary1_up_key;
144 keypad->rotary_down_key[0] = pdata->rotary0_down_key;
145 keypad->rotary_down_key[1] = pdata->rotary1_down_key;
146 keypad->rotary_rel_code[0] = pdata->rotary0_rel_code;
147 keypad->rotary_rel_code[1] = pdata->rotary1_rel_code;
148
Antonio Ospite471637a2008-05-28 14:35:52 -0400149 if (pdata->enable_rotary0) {
150 if (pdata->rotary0_up_key && pdata->rotary0_down_key) {
151 set_bit(pdata->rotary0_up_key, input_dev->keybit);
152 set_bit(pdata->rotary0_down_key, input_dev->keybit);
153 } else
154 set_bit(pdata->rotary0_rel_code, input_dev->relbit);
155 }
Eric Miao62059d92008-01-31 00:59:03 -0500156
Antonio Ospite471637a2008-05-28 14:35:52 -0400157 if (pdata->enable_rotary1) {
158 if (pdata->rotary1_up_key && pdata->rotary1_down_key) {
159 set_bit(pdata->rotary1_up_key, input_dev->keybit);
160 set_bit(pdata->rotary1_down_key, input_dev->keybit);
161 } else
162 set_bit(pdata->rotary1_rel_code, input_dev->relbit);
163 }
Eric Miao1814db62008-01-31 00:58:37 -0500164}
165
166static inline unsigned int lookup_matrix_keycode(
167 struct pxa27x_keypad *keypad, int row, int col)
168{
169 return keypad->matrix_keycodes[(row << 3) + col];
170}
171
172static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad)
173{
174 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
175 int row, col, num_keys_pressed = 0;
176 uint32_t new_state[MAX_MATRIX_KEY_COLS];
Eric Miao9c60deb2008-01-31 00:59:15 -0500177 uint32_t kpas = keypad_readl(KPAS);
Eric Miao1814db62008-01-31 00:58:37 -0500178
179 num_keys_pressed = KPAS_MUKP(kpas);
180
181 memset(new_state, 0, sizeof(new_state));
182
183 if (num_keys_pressed == 0)
184 goto scan;
185
186 if (num_keys_pressed == 1) {
187 col = KPAS_CP(kpas);
188 row = KPAS_RP(kpas);
189
190 /* if invalid row/col, treat as no key pressed */
191 if (col >= pdata->matrix_key_cols ||
192 row >= pdata->matrix_key_rows)
193 goto scan;
194
195 new_state[col] = (1 << row);
196 goto scan;
197 }
198
199 if (num_keys_pressed > 1) {
Eric Miao9c60deb2008-01-31 00:59:15 -0500200 uint32_t kpasmkp0 = keypad_readl(KPASMKP0);
201 uint32_t kpasmkp1 = keypad_readl(KPASMKP1);
202 uint32_t kpasmkp2 = keypad_readl(KPASMKP2);
203 uint32_t kpasmkp3 = keypad_readl(KPASMKP3);
Eric Miao1814db62008-01-31 00:58:37 -0500204
205 new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK;
206 new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK;
207 new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK;
208 new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK;
209 new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK;
210 new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK;
211 new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK;
212 new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK;
213 }
214scan:
215 for (col = 0; col < pdata->matrix_key_cols; col++) {
216 uint32_t bits_changed;
217
218 bits_changed = keypad->matrix_key_state[col] ^ new_state[col];
219 if (bits_changed == 0)
220 continue;
221
222 for (row = 0; row < pdata->matrix_key_rows; row++) {
223 if ((bits_changed & (1 << row)) == 0)
224 continue;
225
226 input_report_key(keypad->input_dev,
227 lookup_matrix_keycode(keypad, row, col),
228 new_state[col] & (1 << row));
229 }
230 }
231 input_sync(keypad->input_dev);
232 memcpy(keypad->matrix_key_state, new_state, sizeof(new_state));
233}
Russell King22d8a732007-08-20 10:19:39 +0100234
Eric Miaod7416f92008-01-31 00:58:52 -0500235#define DEFAULT_KPREC (0x007f007f)
236
Eric Miao62059d92008-01-31 00:59:03 -0500237static inline int rotary_delta(uint32_t kprec)
238{
239 if (kprec & KPREC_OF0)
240 return (kprec & 0xff) + 0x7f;
241 else if (kprec & KPREC_UF0)
242 return (kprec & 0xff) - 0x7f - 0xff;
243 else
244 return (kprec & 0xff) - 0x7f;
245}
246
247static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta)
248{
249 struct input_dev *dev = keypad->input_dev;
250
251 if (delta == 0)
252 return;
253
254 if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) {
255 int keycode = (delta > 0) ? keypad->rotary_up_key[r] :
256 keypad->rotary_down_key[r];
257
258 /* simulate a press-n-release */
259 input_report_key(dev, keycode, 1);
260 input_sync(dev);
261 input_report_key(dev, keycode, 0);
262 input_sync(dev);
263 } else {
264 input_report_rel(dev, keypad->rotary_rel_code[r], delta);
265 input_sync(dev);
266 }
267}
268
269static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad)
270{
271 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
272 uint32_t kprec;
273
274 /* read and reset to default count value */
Eric Miao9c60deb2008-01-31 00:59:15 -0500275 kprec = keypad_readl(KPREC);
276 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miao62059d92008-01-31 00:59:03 -0500277
278 if (pdata->enable_rotary0)
279 report_rotary_event(keypad, 0, rotary_delta(kprec));
280
281 if (pdata->enable_rotary1)
282 report_rotary_event(keypad, 1, rotary_delta(kprec >> 16));
283}
284
285static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad)
286{
287 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
288 unsigned int new_state;
289 uint32_t kpdk, bits_changed;
290 int i;
291
Eric Miao9c60deb2008-01-31 00:59:15 -0500292 kpdk = keypad_readl(KPDK);
Eric Miao62059d92008-01-31 00:59:03 -0500293
294 if (pdata->enable_rotary0 || pdata->enable_rotary1)
295 pxa27x_keypad_scan_rotary(keypad);
296
297 if (pdata->direct_key_map == NULL)
298 return;
299
300 new_state = KPDK_DK(kpdk) & keypad->direct_key_mask;
301 bits_changed = keypad->direct_key_state ^ new_state;
302
303 if (bits_changed == 0)
304 return;
305
306 for (i = 0; i < pdata->direct_key_num; i++) {
307 if (bits_changed & (1 << i))
308 input_report_key(keypad->input_dev,
309 pdata->direct_key_map[i],
310 (new_state & (1 << i)));
311 }
312 input_sync(keypad->input_dev);
313 keypad->direct_key_state = new_state;
314}
315
Eric Miao0e5f11a2008-01-31 00:56:46 -0500316static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400317{
Eric Miao1814db62008-01-31 00:58:37 -0500318 struct pxa27x_keypad *keypad = dev_id;
Eric Miao9c60deb2008-01-31 00:59:15 -0500319 unsigned long kpc = keypad_readl(KPC);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400320
Eric Miao62059d92008-01-31 00:59:03 -0500321 if (kpc & KPC_DI)
322 pxa27x_keypad_scan_direct(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400323
Eric Miao1814db62008-01-31 00:58:37 -0500324 if (kpc & KPC_MI)
325 pxa27x_keypad_scan_matrix(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400326
327 return IRQ_HANDLED;
328}
329
Eric Miaod7416f92008-01-31 00:58:52 -0500330static void pxa27x_keypad_config(struct pxa27x_keypad *keypad)
331{
332 struct pxa27x_keypad_platform_data *pdata = keypad->pdata;
Eric Miao62059d92008-01-31 00:59:03 -0500333 unsigned int mask = 0, direct_key_num = 0;
Eric Miaod7416f92008-01-31 00:58:52 -0500334 unsigned long kpc = 0;
335
336 /* enable matrix keys with automatic scan */
337 if (pdata->matrix_key_rows && pdata->matrix_key_cols) {
338 kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL;
339 kpc |= KPC_MKRN(pdata->matrix_key_rows) |
340 KPC_MKCN(pdata->matrix_key_cols);
341 }
342
Eric Miao62059d92008-01-31 00:59:03 -0500343 /* enable rotary key, debounce interval same as direct keys */
344 if (pdata->enable_rotary0) {
345 mask |= 0x03;
346 direct_key_num = 2;
347 kpc |= KPC_REE0;
348 }
Eric Miaod7416f92008-01-31 00:58:52 -0500349
Eric Miao62059d92008-01-31 00:59:03 -0500350 if (pdata->enable_rotary1) {
351 mask |= 0x0c;
352 direct_key_num = 4;
353 kpc |= KPC_REE1;
354 }
355
356 if (pdata->direct_key_num > direct_key_num)
357 direct_key_num = pdata->direct_key_num;
358
359 keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask;
360
361 /* enable direct key */
362 if (direct_key_num)
363 kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num);
364
Eric Miao9c60deb2008-01-31 00:59:15 -0500365 keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB);
366 keypad_writel(KPREC, DEFAULT_KPREC);
Eric Miao76cb44e2008-01-31 00:59:23 -0500367 keypad_writel(KPKDI, pdata->debounce_interval);
Eric Miaod7416f92008-01-31 00:58:52 -0500368}
369
Eric Miao0e5f11a2008-01-31 00:56:46 -0500370static int pxa27x_keypad_open(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400371{
Eric Miao1814db62008-01-31 00:58:37 -0500372 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
373
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400374 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500375 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500376 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400377
378 return 0;
379}
380
Eric Miao0e5f11a2008-01-31 00:56:46 -0500381static void pxa27x_keypad_close(struct input_dev *dev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400382{
Eric Miao1814db62008-01-31 00:58:37 -0500383 struct pxa27x_keypad *keypad = input_get_drvdata(dev);
384
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400385 /* Disable clock unit */
Eric Miao1814db62008-01-31 00:58:37 -0500386 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400387}
388
389#ifdef CONFIG_PM
Eric Miao0e5f11a2008-01-31 00:56:46 -0500390static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400391{
Eric Miao1814db62008-01-31 00:58:37 -0500392 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400393
Eric Miaod7416f92008-01-31 00:58:52 -0500394 clk_disable(keypad->clk);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400395 return 0;
396}
397
Eric Miao0e5f11a2008-01-31 00:56:46 -0500398static int pxa27x_keypad_resume(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400399{
Eric Miao1814db62008-01-31 00:58:37 -0500400 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao1814db62008-01-31 00:58:37 -0500401 struct input_dev *input_dev = keypad->input_dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400402
403 mutex_lock(&input_dev->mutex);
404
405 if (input_dev->users) {
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400406 /* Enable unit clock */
Eric Miao1814db62008-01-31 00:58:37 -0500407 clk_enable(keypad->clk);
Eric Miaod7416f92008-01-31 00:58:52 -0500408 pxa27x_keypad_config(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400409 }
410
411 mutex_unlock(&input_dev->mutex);
412
413 return 0;
414}
415#else
Eric Miao0e5f11a2008-01-31 00:56:46 -0500416#define pxa27x_keypad_suspend NULL
417#define pxa27x_keypad_resume NULL
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400418#endif
419
Eric Miao9c60deb2008-01-31 00:59:15 -0500420#define res_size(res) ((res)->end - (res)->start + 1)
421
Eric Miao0e5f11a2008-01-31 00:56:46 -0500422static int __devinit pxa27x_keypad_probe(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400423{
Eric Miao1814db62008-01-31 00:58:37 -0500424 struct pxa27x_keypad *keypad;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400425 struct input_dev *input_dev;
Eric Miao9c60deb2008-01-31 00:59:15 -0500426 struct resource *res;
427 int irq, error;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400428
Eric Miao1814db62008-01-31 00:58:37 -0500429 keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL);
430 if (keypad == NULL) {
431 dev_err(&pdev->dev, "failed to allocate driver data\n");
432 return -ENOMEM;
433 }
434
435 keypad->pdata = pdev->dev.platform_data;
436 if (keypad->pdata == NULL) {
437 dev_err(&pdev->dev, "no platform data defined\n");
438 error = -EINVAL;
439 goto failed_free;
440 }
441
Eric Miao9c60deb2008-01-31 00:59:15 -0500442 irq = platform_get_irq(pdev, 0);
443 if (irq < 0) {
444 dev_err(&pdev->dev, "failed to get keypad irq\n");
445 error = -ENXIO;
446 goto failed_free;
447 }
448
449 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
450 if (res == NULL) {
451 dev_err(&pdev->dev, "failed to get I/O memory\n");
452 error = -ENXIO;
453 goto failed_free;
454 }
455
456 res = request_mem_region(res->start, res_size(res), pdev->name);
457 if (res == NULL) {
458 dev_err(&pdev->dev, "failed to request I/O memory\n");
459 error = -EBUSY;
460 goto failed_free;
461 }
462
463 keypad->mmio_base = ioremap(res->start, res_size(res));
464 if (keypad->mmio_base == NULL) {
465 dev_err(&pdev->dev, "failed to remap I/O memory\n");
466 error = -ENXIO;
467 goto failed_free_mem;
468 }
469
Eric Miao1814db62008-01-31 00:58:37 -0500470 keypad->clk = clk_get(&pdev->dev, "KBDCLK");
471 if (IS_ERR(keypad->clk)) {
472 dev_err(&pdev->dev, "failed to get keypad clock\n");
473 error = PTR_ERR(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500474 goto failed_free_io;
Russell King22d8a732007-08-20 10:19:39 +0100475 }
476
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400477 /* Create and register the input driver. */
478 input_dev = input_allocate_device();
479 if (!input_dev) {
Eric Miao1814db62008-01-31 00:58:37 -0500480 dev_err(&pdev->dev, "failed to allocate input device\n");
Russell King22d8a732007-08-20 10:19:39 +0100481 error = -ENOMEM;
Eric Miao1814db62008-01-31 00:58:37 -0500482 goto failed_put_clk;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400483 }
484
Eric Miao9c60deb2008-01-31 00:59:15 -0500485 input_dev->name = pdev->name;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400486 input_dev->id.bustype = BUS_HOST;
Eric Miao0e5f11a2008-01-31 00:56:46 -0500487 input_dev->open = pxa27x_keypad_open;
488 input_dev->close = pxa27x_keypad_close;
Dmitry Torokhov469ba4d2007-04-12 01:34:58 -0400489 input_dev->dev.parent = &pdev->dev;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400490
Eric Miao1814db62008-01-31 00:58:37 -0500491 keypad->input_dev = input_dev;
492 input_set_drvdata(input_dev, keypad);
493
Antonio Ospite471637a2008-05-28 14:35:52 -0400494 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
495 if ((keypad->pdata->enable_rotary0 &&
496 keypad->pdata->rotary0_rel_code) ||
497 (keypad->pdata->enable_rotary1 &&
498 keypad->pdata->rotary1_rel_code)) {
499 input_dev->evbit[0] |= BIT_MASK(EV_REL);
500 }
Eric Miao1814db62008-01-31 00:58:37 -0500501
502 pxa27x_keypad_build_keycode(keypad);
Eric Miao1814db62008-01-31 00:58:37 -0500503 platform_set_drvdata(pdev, keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400504
Eric Miao9c60deb2008-01-31 00:59:15 -0500505 error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED,
506 pdev->name, keypad);
507 if (error) {
508 dev_err(&pdev->dev, "failed to request IRQ\n");
509 goto failed_free_dev;
510 }
511
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400512 /* Register the input device */
513 error = input_register_device(input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500514 if (error) {
515 dev_err(&pdev->dev, "failed to register input device\n");
516 goto failed_free_irq;
517 }
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400518
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400519 return 0;
520
Eric Miao9c60deb2008-01-31 00:59:15 -0500521failed_free_irq:
522 free_irq(irq, pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400523 platform_set_drvdata(pdev, NULL);
Eric Miao9c60deb2008-01-31 00:59:15 -0500524failed_free_dev:
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400525 input_free_device(input_dev);
Eric Miao1814db62008-01-31 00:58:37 -0500526failed_put_clk:
527 clk_put(keypad->clk);
Eric Miao9c60deb2008-01-31 00:59:15 -0500528failed_free_io:
529 iounmap(keypad->mmio_base);
530failed_free_mem:
531 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500532failed_free:
533 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400534 return error;
535}
536
Eric Miao0e5f11a2008-01-31 00:56:46 -0500537static int __devexit pxa27x_keypad_remove(struct platform_device *pdev)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400538{
Eric Miao1814db62008-01-31 00:58:37 -0500539 struct pxa27x_keypad *keypad = platform_get_drvdata(pdev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500540 struct resource *res;
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400541
Eric Miao9c60deb2008-01-31 00:59:15 -0500542 free_irq(platform_get_irq(pdev, 0), pdev);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400543
Eric Miao1814db62008-01-31 00:58:37 -0500544 clk_disable(keypad->clk);
545 clk_put(keypad->clk);
546
547 input_unregister_device(keypad->input_dev);
Eric Miao9c60deb2008-01-31 00:59:15 -0500548 input_free_device(keypad->input_dev);
549
550 iounmap(keypad->mmio_base);
551
552 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
553 release_mem_region(res->start, res_size(res));
Eric Miao1814db62008-01-31 00:58:37 -0500554
555 platform_set_drvdata(pdev, NULL);
556 kfree(keypad);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400557 return 0;
558}
559
Kay Sieversd7b52472008-04-18 00:24:42 -0400560/* work with hotplug and coldplug */
561MODULE_ALIAS("platform:pxa27x-keypad");
562
Eric Miao0e5f11a2008-01-31 00:56:46 -0500563static struct platform_driver pxa27x_keypad_driver = {
564 .probe = pxa27x_keypad_probe,
565 .remove = __devexit_p(pxa27x_keypad_remove),
566 .suspend = pxa27x_keypad_suspend,
567 .resume = pxa27x_keypad_resume,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400568 .driver = {
Eric Miao9c60deb2008-01-31 00:59:15 -0500569 .name = "pxa27x-keypad",
Kay Sieversd7b52472008-04-18 00:24:42 -0400570 .owner = THIS_MODULE,
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400571 },
572};
573
Eric Miao0e5f11a2008-01-31 00:56:46 -0500574static int __init pxa27x_keypad_init(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400575{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500576 return platform_driver_register(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400577}
578
Eric Miao0e5f11a2008-01-31 00:56:46 -0500579static void __exit pxa27x_keypad_exit(void)
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400580{
Eric Miao0e5f11a2008-01-31 00:56:46 -0500581 platform_driver_unregister(&pxa27x_keypad_driver);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400582}
583
Eric Miao0e5f11a2008-01-31 00:56:46 -0500584module_init(pxa27x_keypad_init);
585module_exit(pxa27x_keypad_exit);
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400586
Eric Miao0e5f11a2008-01-31 00:56:46 -0500587MODULE_DESCRIPTION("PXA27x Keypad Controller Driver");
Rodolfo Giometti5a90e5b2007-03-16 00:58:52 -0400588MODULE_LICENSE("GPL");