Commit | Line | Data |
---|---|---|
5a90e5bc | 1 | /* |
0e5f11aa | 2 | * linux/drivers/input/keyboard/pxa27x_keypad.c |
5a90e5bc RG |
3 | * |
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 | |
2f82af08 | 11 | * on some suggestions by Nicolas Pitre <nico@fluxnic.net>. |
5a90e5bc RG |
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> | |
22d8a73a RK |
26 | #include <linux/clk.h> |
27 | #include <linux/err.h> | |
52ec7752 | 28 | #include <linux/input/matrix_keypad.h> |
5a0e3ad6 | 29 | #include <linux/slab.h> |
5a90e5bc | 30 | |
5a90e5bc RG |
31 | #include <asm/mach/arch.h> |
32 | #include <asm/mach/map.h> | |
33 | ||
a09e64fb | 34 | #include <mach/hardware.h> |
293b2da1 | 35 | #include <linux/platform_data/keypad-pxa27x.h> |
9c60debd EM |
36 | /* |
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 */ | |
44 | ||
45 | /* 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 */ | |
688dad4f SO |
53 | #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 */ | |
d7416f9e | 56 | |
9c60debd EM |
57 | #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 | ||
62059d9e EM |
75 | #define KPDK_DKP (0x1 << 31) |
76 | #define KPDK_DK(n) ((n) & 0xff) | |
77 | ||
9c60debd EM |
78 | #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) | |
5a90e5bc | 93 | |
1814db69 EM |
94 | #define KPASMKP_MKC_MASK (0xff) |
95 | ||
9c60debd EM |
96 | #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off)) |
97 | #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off)) | |
98 | ||
bd96f378 DT |
99 | #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS) |
100 | #define MAX_KEYPAD_KEYS (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM) | |
1814db69 EM |
101 | |
102 | struct pxa27x_keypad { | |
103 | struct pxa27x_keypad_platform_data *pdata; | |
104 | ||
105 | struct clk *clk; | |
106 | struct input_dev *input_dev; | |
9c60debd | 107 | void __iomem *mmio_base; |
1814db69 | 108 | |
39ab9dde EM |
109 | int irq; |
110 | ||
bd96f378 DT |
111 | unsigned short keycodes[MAX_KEYPAD_KEYS]; |
112 | int rotary_rel_code[2]; | |
1814db69 EM |
113 | |
114 | /* state row bits of each column scan */ | |
115 | uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS]; | |
62059d9e EM |
116 | uint32_t direct_key_state; |
117 | ||
118 | unsigned int direct_key_mask; | |
1814db69 EM |
119 | }; |
120 | ||
121 | static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad) | |
122 | { | |
123 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; | |
124 | struct input_dev *input_dev = keypad->input_dev; | |
bd96f378 | 125 | unsigned short keycode; |
1814db69 EM |
126 | int i; |
127 | ||
52ec7752 DT |
128 | for (i = 0; i < pdata->matrix_key_map_size; i++) { |
129 | unsigned int key = pdata->matrix_key_map[i]; | |
130 | unsigned int row = KEY_ROW(key); | |
131 | unsigned int col = KEY_COL(key); | |
bd96f378 DT |
132 | unsigned int scancode = MATRIX_SCAN_CODE(row, col, |
133 | MATRIX_ROW_SHIFT); | |
1814db69 | 134 | |
bd96f378 DT |
135 | keycode = KEY_VAL(key); |
136 | keypad->keycodes[scancode] = keycode; | |
137 | __set_bit(keycode, input_dev->keybit); | |
1814db69 | 138 | } |
471637a5 | 139 | |
bd96f378 DT |
140 | for (i = 0; i < pdata->direct_key_num; i++) { |
141 | keycode = pdata->direct_key_map[i]; | |
142 | keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode; | |
143 | __set_bit(keycode, input_dev->keybit); | |
144 | } | |
62059d9e | 145 | |
471637a5 AO |
146 | if (pdata->enable_rotary0) { |
147 | if (pdata->rotary0_up_key && pdata->rotary0_down_key) { | |
bd96f378 DT |
148 | keycode = pdata->rotary0_up_key; |
149 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode; | |
150 | __set_bit(keycode, input_dev->keybit); | |
151 | ||
152 | keycode = pdata->rotary0_down_key; | |
153 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode; | |
154 | __set_bit(keycode, input_dev->keybit); | |
155 | ||
156 | keypad->rotary_rel_code[0] = -1; | |
157 | } else { | |
158 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; | |
52ec7752 | 159 | __set_bit(pdata->rotary0_rel_code, input_dev->relbit); |
bd96f378 | 160 | } |
471637a5 AO |
161 | } |
162 | ||
163 | if (pdata->enable_rotary1) { | |
164 | if (pdata->rotary1_up_key && pdata->rotary1_down_key) { | |
bd96f378 DT |
165 | keycode = pdata->rotary1_up_key; |
166 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode; | |
167 | __set_bit(keycode, input_dev->keybit); | |
168 | ||
169 | keycode = pdata->rotary1_down_key; | |
170 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode; | |
171 | __set_bit(keycode, input_dev->keybit); | |
172 | ||
173 | keypad->rotary_rel_code[1] = -1; | |
174 | } else { | |
175 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; | |
52ec7752 | 176 | __set_bit(pdata->rotary1_rel_code, input_dev->relbit); |
bd96f378 | 177 | } |
471637a5 | 178 | } |
1814db69 | 179 | |
bd96f378 | 180 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
1814db69 EM |
181 | } |
182 | ||
183 | static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad) | |
184 | { | |
185 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; | |
bd96f378 | 186 | struct input_dev *input_dev = keypad->input_dev; |
1814db69 EM |
187 | int row, col, num_keys_pressed = 0; |
188 | uint32_t new_state[MAX_MATRIX_KEY_COLS]; | |
9c60debd | 189 | uint32_t kpas = keypad_readl(KPAS); |
1814db69 EM |
190 | |
191 | num_keys_pressed = KPAS_MUKP(kpas); | |
192 | ||
193 | memset(new_state, 0, sizeof(new_state)); | |
194 | ||
195 | if (num_keys_pressed == 0) | |
196 | goto scan; | |
197 | ||
198 | if (num_keys_pressed == 1) { | |
199 | col = KPAS_CP(kpas); | |
200 | row = KPAS_RP(kpas); | |
201 | ||
202 | /* if invalid row/col, treat as no key pressed */ | |
203 | if (col >= pdata->matrix_key_cols || | |
204 | row >= pdata->matrix_key_rows) | |
205 | goto scan; | |
206 | ||
207 | new_state[col] = (1 << row); | |
208 | goto scan; | |
209 | } | |
210 | ||
211 | if (num_keys_pressed > 1) { | |
9c60debd EM |
212 | uint32_t kpasmkp0 = keypad_readl(KPASMKP0); |
213 | uint32_t kpasmkp1 = keypad_readl(KPASMKP1); | |
214 | uint32_t kpasmkp2 = keypad_readl(KPASMKP2); | |
215 | uint32_t kpasmkp3 = keypad_readl(KPASMKP3); | |
1814db69 EM |
216 | |
217 | new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK; | |
218 | new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK; | |
219 | new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK; | |
220 | new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK; | |
221 | new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK; | |
222 | new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK; | |
223 | new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK; | |
224 | new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK; | |
225 | } | |
226 | scan: | |
227 | for (col = 0; col < pdata->matrix_key_cols; col++) { | |
228 | uint32_t bits_changed; | |
bd96f378 | 229 | int code; |
1814db69 EM |
230 | |
231 | bits_changed = keypad->matrix_key_state[col] ^ new_state[col]; | |
232 | if (bits_changed == 0) | |
233 | continue; | |
234 | ||
235 | for (row = 0; row < pdata->matrix_key_rows; row++) { | |
236 | if ((bits_changed & (1 << row)) == 0) | |
237 | continue; | |
238 | ||
bd96f378 DT |
239 | code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT); |
240 | input_event(input_dev, EV_MSC, MSC_SCAN, code); | |
241 | input_report_key(input_dev, keypad->keycodes[code], | |
242 | new_state[col] & (1 << row)); | |
1814db69 EM |
243 | } |
244 | } | |
bd96f378 | 245 | input_sync(input_dev); |
1814db69 EM |
246 | memcpy(keypad->matrix_key_state, new_state, sizeof(new_state)); |
247 | } | |
22d8a73a | 248 | |
d7416f9e EM |
249 | #define DEFAULT_KPREC (0x007f007f) |
250 | ||
62059d9e EM |
251 | static inline int rotary_delta(uint32_t kprec) |
252 | { | |
253 | if (kprec & KPREC_OF0) | |
254 | return (kprec & 0xff) + 0x7f; | |
255 | else if (kprec & KPREC_UF0) | |
256 | return (kprec & 0xff) - 0x7f - 0xff; | |
257 | else | |
258 | return (kprec & 0xff) - 0x7f; | |
259 | } | |
260 | ||
261 | static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta) | |
262 | { | |
263 | struct input_dev *dev = keypad->input_dev; | |
264 | ||
265 | if (delta == 0) | |
266 | return; | |
267 | ||
bd96f378 DT |
268 | if (keypad->rotary_rel_code[r] == -1) { |
269 | int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1); | |
270 | unsigned char keycode = keypad->keycodes[code]; | |
62059d9e EM |
271 | |
272 | /* simulate a press-n-release */ | |
bd96f378 | 273 | input_event(dev, EV_MSC, MSC_SCAN, code); |
62059d9e EM |
274 | input_report_key(dev, keycode, 1); |
275 | input_sync(dev); | |
bd96f378 | 276 | input_event(dev, EV_MSC, MSC_SCAN, code); |
62059d9e EM |
277 | input_report_key(dev, keycode, 0); |
278 | input_sync(dev); | |
279 | } else { | |
280 | input_report_rel(dev, keypad->rotary_rel_code[r], delta); | |
281 | input_sync(dev); | |
282 | } | |
283 | } | |
284 | ||
285 | static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad) | |
286 | { | |
287 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; | |
288 | uint32_t kprec; | |
289 | ||
290 | /* read and reset to default count value */ | |
9c60debd EM |
291 | kprec = keypad_readl(KPREC); |
292 | keypad_writel(KPREC, DEFAULT_KPREC); | |
62059d9e EM |
293 | |
294 | if (pdata->enable_rotary0) | |
295 | report_rotary_event(keypad, 0, rotary_delta(kprec)); | |
296 | ||
297 | if (pdata->enable_rotary1) | |
298 | report_rotary_event(keypad, 1, rotary_delta(kprec >> 16)); | |
299 | } | |
300 | ||
301 | static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad) | |
302 | { | |
303 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; | |
bd96f378 | 304 | struct input_dev *input_dev = keypad->input_dev; |
62059d9e EM |
305 | unsigned int new_state; |
306 | uint32_t kpdk, bits_changed; | |
307 | int i; | |
308 | ||
9c60debd | 309 | kpdk = keypad_readl(KPDK); |
62059d9e EM |
310 | |
311 | if (pdata->enable_rotary0 || pdata->enable_rotary1) | |
312 | pxa27x_keypad_scan_rotary(keypad); | |
313 | ||
ee1d8040 CX |
314 | /* |
315 | * The KPDR_DK only output the key pin level, so it relates to board, | |
316 | * and low level may be active. | |
317 | */ | |
318 | if (pdata->direct_key_low_active) | |
319 | new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask; | |
320 | else | |
321 | new_state = KPDK_DK(kpdk) & keypad->direct_key_mask; | |
322 | ||
62059d9e EM |
323 | bits_changed = keypad->direct_key_state ^ new_state; |
324 | ||
325 | if (bits_changed == 0) | |
326 | return; | |
327 | ||
328 | for (i = 0; i < pdata->direct_key_num; i++) { | |
bd96f378 DT |
329 | if (bits_changed & (1 << i)) { |
330 | int code = MAX_MATRIX_KEY_NUM + i; | |
331 | ||
332 | input_event(input_dev, EV_MSC, MSC_SCAN, code); | |
333 | input_report_key(input_dev, keypad->keycodes[code], | |
334 | new_state & (1 << i)); | |
335 | } | |
62059d9e | 336 | } |
bd96f378 | 337 | input_sync(input_dev); |
62059d9e EM |
338 | keypad->direct_key_state = new_state; |
339 | } | |
340 | ||
3587c258 MB |
341 | static void clear_wakeup_event(struct pxa27x_keypad *keypad) |
342 | { | |
343 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; | |
344 | ||
345 | if (pdata->clear_wakeup_event) | |
346 | (pdata->clear_wakeup_event)(); | |
347 | } | |
348 | ||
0e5f11aa | 349 | static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id) |
5a90e5bc | 350 | { |
1814db69 | 351 | struct pxa27x_keypad *keypad = dev_id; |
9c60debd | 352 | unsigned long kpc = keypad_readl(KPC); |
62059d9e | 353 | |
3587c258 MB |
354 | clear_wakeup_event(keypad); |
355 | ||
62059d9e EM |
356 | if (kpc & KPC_DI) |
357 | pxa27x_keypad_scan_direct(keypad); | |
5a90e5bc | 358 | |
1814db69 EM |
359 | if (kpc & KPC_MI) |
360 | pxa27x_keypad_scan_matrix(keypad); | |
5a90e5bc RG |
361 | |
362 | return IRQ_HANDLED; | |
363 | } | |
364 | ||
d7416f9e | 365 | static void pxa27x_keypad_config(struct pxa27x_keypad *keypad) |
5a90e5bc | 366 | { |
d7416f9e | 367 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
62059d9e | 368 | unsigned int mask = 0, direct_key_num = 0; |
d7416f9e | 369 | unsigned long kpc = 0; |
1814db69 | 370 | |
904adede VK |
371 | /* clear pending interrupt bit */ |
372 | keypad_readl(KPC); | |
373 | ||
d7416f9e EM |
374 | /* enable matrix keys with automatic scan */ |
375 | if (pdata->matrix_key_rows && pdata->matrix_key_cols) { | |
376 | kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL; | |
377 | kpc |= KPC_MKRN(pdata->matrix_key_rows) | | |
378 | KPC_MKCN(pdata->matrix_key_cols); | |
379 | } | |
5a90e5bc | 380 | |
62059d9e EM |
381 | /* enable rotary key, debounce interval same as direct keys */ |
382 | if (pdata->enable_rotary0) { | |
383 | mask |= 0x03; | |
384 | direct_key_num = 2; | |
385 | kpc |= KPC_REE0; | |
386 | } | |
387 | ||
388 | if (pdata->enable_rotary1) { | |
389 | mask |= 0x0c; | |
390 | direct_key_num = 4; | |
391 | kpc |= KPC_REE1; | |
392 | } | |
393 | ||
394 | if (pdata->direct_key_num > direct_key_num) | |
395 | direct_key_num = pdata->direct_key_num; | |
396 | ||
fb054bf2 CX |
397 | /* |
398 | * Direct keys usage may not start from KP_DKIN0, check the platfrom | |
399 | * mask data to config the specific. | |
400 | */ | |
401 | if (pdata->direct_key_mask) | |
402 | keypad->direct_key_mask = pdata->direct_key_mask; | |
403 | else | |
404 | keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask; | |
62059d9e EM |
405 | |
406 | /* enable direct key */ | |
407 | if (direct_key_num) | |
408 | kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num); | |
5a90e5bc | 409 | |
9c60debd EM |
410 | keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB); |
411 | keypad_writel(KPREC, DEFAULT_KPREC); | |
76cb44e1 | 412 | keypad_writel(KPKDI, pdata->debounce_interval); |
d7416f9e EM |
413 | } |
414 | ||
415 | static int pxa27x_keypad_open(struct input_dev *dev) | |
416 | { | |
417 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); | |
5a90e5bc RG |
418 | |
419 | /* Enable unit clock */ | |
6ce34a5f | 420 | clk_prepare_enable(keypad->clk); |
d7416f9e | 421 | pxa27x_keypad_config(keypad); |
5a90e5bc RG |
422 | |
423 | return 0; | |
424 | } | |
425 | ||
0e5f11aa | 426 | static void pxa27x_keypad_close(struct input_dev *dev) |
5a90e5bc | 427 | { |
1814db69 EM |
428 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
429 | ||
5a90e5bc | 430 | /* Disable clock unit */ |
6ce34a5f | 431 | clk_disable_unprepare(keypad->clk); |
5a90e5bc RG |
432 | } |
433 | ||
434 | #ifdef CONFIG_PM | |
b0010911 | 435 | static int pxa27x_keypad_suspend(struct device *dev) |
5a90e5bc | 436 | { |
b0010911 | 437 | struct platform_device *pdev = to_platform_device(dev); |
1814db69 | 438 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
5a90e5bc | 439 | |
6ce34a5f CX |
440 | /* |
441 | * If the keypad is used a wake up source, clock can not be disabled. | |
442 | * Or it can not detect the key pressing. | |
443 | */ | |
39ab9dde EM |
444 | if (device_may_wakeup(&pdev->dev)) |
445 | enable_irq_wake(keypad->irq); | |
6ce34a5f CX |
446 | else |
447 | clk_disable_unprepare(keypad->clk); | |
39ab9dde | 448 | |
5a90e5bc RG |
449 | return 0; |
450 | } | |
451 | ||
b0010911 | 452 | static int pxa27x_keypad_resume(struct device *dev) |
5a90e5bc | 453 | { |
b0010911 | 454 | struct platform_device *pdev = to_platform_device(dev); |
1814db69 | 455 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
1814db69 | 456 | struct input_dev *input_dev = keypad->input_dev; |
5a90e5bc | 457 | |
6ce34a5f CX |
458 | /* |
459 | * If the keypad is used as wake up source, the clock is not turned | |
460 | * off. So do not need configure it again. | |
461 | */ | |
462 | if (device_may_wakeup(&pdev->dev)) { | |
39ab9dde | 463 | disable_irq_wake(keypad->irq); |
6ce34a5f CX |
464 | } else { |
465 | mutex_lock(&input_dev->mutex); | |
39ab9dde | 466 | |
6ce34a5f CX |
467 | if (input_dev->users) { |
468 | /* Enable unit clock */ | |
469 | clk_prepare_enable(keypad->clk); | |
470 | pxa27x_keypad_config(keypad); | |
471 | } | |
5a90e5bc | 472 | |
6ce34a5f | 473 | mutex_unlock(&input_dev->mutex); |
5a90e5bc RG |
474 | } |
475 | ||
5a90e5bc RG |
476 | return 0; |
477 | } | |
b0010911 DT |
478 | |
479 | static const struct dev_pm_ops pxa27x_keypad_pm_ops = { | |
480 | .suspend = pxa27x_keypad_suspend, | |
481 | .resume = pxa27x_keypad_resume, | |
482 | }; | |
5a90e5bc RG |
483 | #endif |
484 | ||
0e5f11aa | 485 | static int __devinit pxa27x_keypad_probe(struct platform_device *pdev) |
5a90e5bc | 486 | { |
bd96f378 | 487 | struct pxa27x_keypad_platform_data *pdata = pdev->dev.platform_data; |
1814db69 | 488 | struct pxa27x_keypad *keypad; |
5a90e5bc | 489 | struct input_dev *input_dev; |
9c60debd EM |
490 | struct resource *res; |
491 | int irq, error; | |
5a90e5bc | 492 | |
bd96f378 | 493 | if (pdata == NULL) { |
1814db69 | 494 | dev_err(&pdev->dev, "no platform data defined\n"); |
bd96f378 | 495 | return -EINVAL; |
1814db69 EM |
496 | } |
497 | ||
9c60debd EM |
498 | irq = platform_get_irq(pdev, 0); |
499 | if (irq < 0) { | |
500 | dev_err(&pdev->dev, "failed to get keypad irq\n"); | |
bd96f378 | 501 | return -ENXIO; |
9c60debd EM |
502 | } |
503 | ||
504 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
505 | if (res == NULL) { | |
506 | dev_err(&pdev->dev, "failed to get I/O memory\n"); | |
bd96f378 DT |
507 | return -ENXIO; |
508 | } | |
509 | ||
510 | keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL); | |
511 | input_dev = input_allocate_device(); | |
512 | if (!keypad || !input_dev) { | |
513 | dev_err(&pdev->dev, "failed to allocate memory\n"); | |
514 | error = -ENOMEM; | |
9c60debd EM |
515 | goto failed_free; |
516 | } | |
517 | ||
bd96f378 DT |
518 | keypad->pdata = pdata; |
519 | keypad->input_dev = input_dev; | |
520 | keypad->irq = irq; | |
521 | ||
52ec7752 | 522 | res = request_mem_region(res->start, resource_size(res), pdev->name); |
9c60debd EM |
523 | if (res == NULL) { |
524 | dev_err(&pdev->dev, "failed to request I/O memory\n"); | |
525 | error = -EBUSY; | |
526 | goto failed_free; | |
527 | } | |
528 | ||
52ec7752 | 529 | keypad->mmio_base = ioremap(res->start, resource_size(res)); |
9c60debd EM |
530 | if (keypad->mmio_base == NULL) { |
531 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); | |
532 | error = -ENXIO; | |
533 | goto failed_free_mem; | |
534 | } | |
535 | ||
e0d8b13a | 536 | keypad->clk = clk_get(&pdev->dev, NULL); |
1814db69 EM |
537 | if (IS_ERR(keypad->clk)) { |
538 | dev_err(&pdev->dev, "failed to get keypad clock\n"); | |
539 | error = PTR_ERR(keypad->clk); | |
9c60debd | 540 | goto failed_free_io; |
22d8a73a RK |
541 | } |
542 | ||
9c60debd | 543 | input_dev->name = pdev->name; |
5a90e5bc | 544 | input_dev->id.bustype = BUS_HOST; |
0e5f11aa EM |
545 | input_dev->open = pxa27x_keypad_open; |
546 | input_dev->close = pxa27x_keypad_close; | |
469ba4df | 547 | input_dev->dev.parent = &pdev->dev; |
5a90e5bc | 548 | |
bd96f378 DT |
549 | input_dev->keycode = keypad->keycodes; |
550 | input_dev->keycodesize = sizeof(keypad->keycodes[0]); | |
551 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); | |
552 | ||
1814db69 EM |
553 | input_set_drvdata(input_dev, keypad); |
554 | ||
471637a5 | 555 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
bd96f378 | 556 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
1814db69 EM |
557 | |
558 | pxa27x_keypad_build_keycode(keypad); | |
bd96f378 DT |
559 | |
560 | if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) || | |
561 | (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) { | |
562 | input_dev->evbit[0] |= BIT_MASK(EV_REL); | |
563 | } | |
5a90e5bc | 564 | |
ec4665c4 | 565 | error = request_irq(irq, pxa27x_keypad_irq_handler, 0, |
9c60debd | 566 | pdev->name, keypad); |
5a90e5bc | 567 | if (error) { |
9c60debd | 568 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
bd96f378 | 569 | goto failed_put_clk; |
5a90e5bc RG |
570 | } |
571 | ||
5a90e5bc RG |
572 | /* Register the input device */ |
573 | error = input_register_device(input_dev); | |
9c60debd EM |
574 | if (error) { |
575 | dev_err(&pdev->dev, "failed to register input device\n"); | |
576 | goto failed_free_irq; | |
577 | } | |
5a90e5bc | 578 | |
bd96f378 | 579 | platform_set_drvdata(pdev, keypad); |
39ab9dde EM |
580 | device_init_wakeup(&pdev->dev, 1); |
581 | ||
5a90e5bc RG |
582 | return 0; |
583 | ||
9c60debd EM |
584 | failed_free_irq: |
585 | free_irq(irq, pdev); | |
1814db69 EM |
586 | failed_put_clk: |
587 | clk_put(keypad->clk); | |
9c60debd EM |
588 | failed_free_io: |
589 | iounmap(keypad->mmio_base); | |
590 | failed_free_mem: | |
52ec7752 | 591 | release_mem_region(res->start, resource_size(res)); |
1814db69 | 592 | failed_free: |
bd96f378 | 593 | input_free_device(input_dev); |
1814db69 | 594 | kfree(keypad); |
5a90e5bc RG |
595 | return error; |
596 | } | |
597 | ||
0e5f11aa | 598 | static int __devexit pxa27x_keypad_remove(struct platform_device *pdev) |
5a90e5bc | 599 | { |
1814db69 | 600 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
9c60debd | 601 | struct resource *res; |
5a90e5bc | 602 | |
39ab9dde | 603 | free_irq(keypad->irq, pdev); |
1814db69 EM |
604 | clk_put(keypad->clk); |
605 | ||
606 | input_unregister_device(keypad->input_dev); | |
9c60debd EM |
607 | iounmap(keypad->mmio_base); |
608 | ||
609 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
52ec7752 | 610 | release_mem_region(res->start, resource_size(res)); |
1814db69 EM |
611 | |
612 | platform_set_drvdata(pdev, NULL); | |
613 | kfree(keypad); | |
bd96f378 | 614 | |
5a90e5bc RG |
615 | return 0; |
616 | } | |
617 | ||
d7b5247b KS |
618 | /* work with hotplug and coldplug */ |
619 | MODULE_ALIAS("platform:pxa27x-keypad"); | |
620 | ||
0e5f11aa EM |
621 | static struct platform_driver pxa27x_keypad_driver = { |
622 | .probe = pxa27x_keypad_probe, | |
1cb0aa88 | 623 | .remove = pxa27x_keypad_remove, |
5a90e5bc | 624 | .driver = { |
9c60debd | 625 | .name = "pxa27x-keypad", |
d7b5247b | 626 | .owner = THIS_MODULE, |
b0010911 DT |
627 | #ifdef CONFIG_PM |
628 | .pm = &pxa27x_keypad_pm_ops, | |
629 | #endif | |
5a90e5bc RG |
630 | }, |
631 | }; | |
5146c84f | 632 | module_platform_driver(pxa27x_keypad_driver); |
5a90e5bc | 633 | |
0e5f11aa | 634 | MODULE_DESCRIPTION("PXA27x Keypad Controller Driver"); |
5a90e5bc | 635 | MODULE_LICENSE("GPL"); |