Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
5a90e5bc | 2 | /* |
0e5f11aa | 3 | * linux/drivers/input/keyboard/pxa27x_keypad.c |
5a90e5bc RG |
4 | * |
5 | * Driver for the pxa27x matrix keyboard controller. | |
6 | * | |
7 | * Created: Feb 22, 2007 | |
8 | * Author: Rodolfo Giometti <giometti@linux.it> | |
9 | * | |
10 | * Based on a previous implementations by Kevin O'Connor | |
11 | * <kevin_at_koconnor.net> and Alex Osborne <bobofdoom@gmail.com> and | |
2f82af08 | 12 | * on some suggestions by Nicolas Pitre <nico@fluxnic.net>. |
5a90e5bc RG |
13 | */ |
14 | ||
15 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/module.h> | |
5a90e5bc RG |
18 | #include <linux/interrupt.h> |
19 | #include <linux/input.h> | |
d0542677 | 20 | #include <linux/io.h> |
5a90e5bc RG |
21 | #include <linux/device.h> |
22 | #include <linux/platform_device.h> | |
22d8a73a RK |
23 | #include <linux/clk.h> |
24 | #include <linux/err.h> | |
52ec7752 | 25 | #include <linux/input/matrix_keypad.h> |
5a0e3ad6 | 26 | #include <linux/slab.h> |
95ba7407 | 27 | #include <linux/of.h> |
5a90e5bc | 28 | |
293b2da1 | 29 | #include <linux/platform_data/keypad-pxa27x.h> |
9c60debd EM |
30 | /* |
31 | * Keypad Controller registers | |
32 | */ | |
33 | #define KPC 0x0000 /* Keypad Control register */ | |
34 | #define KPDK 0x0008 /* Keypad Direct Key register */ | |
35 | #define KPREC 0x0010 /* Keypad Rotary Encoder register */ | |
36 | #define KPMK 0x0018 /* Keypad Matrix Key register */ | |
37 | #define KPAS 0x0020 /* Keypad Automatic Scan register */ | |
38 | ||
39 | /* Keypad Automatic Scan Multiple Key Presser register 0-3 */ | |
40 | #define KPASMKP0 0x0028 | |
41 | #define KPASMKP1 0x0030 | |
42 | #define KPASMKP2 0x0038 | |
43 | #define KPASMKP3 0x0040 | |
44 | #define KPKDI 0x0048 | |
45 | ||
46 | /* bit definitions */ | |
688dad4f SO |
47 | #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */ |
48 | #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */ | |
49 | #define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */ | |
d7416f9e | 50 | |
9c60debd EM |
51 | #define KPC_AS (0x1 << 30) /* Automatic Scan bit */ |
52 | #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */ | |
53 | #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */ | |
54 | #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */ | |
55 | ||
56 | #define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */ | |
57 | #define KPC_MS_ALL (0xff << 13) | |
58 | ||
59 | #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */ | |
60 | #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */ | |
61 | #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */ | |
62 | #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */ | |
63 | #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */ | |
64 | #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */ | |
65 | #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */ | |
66 | #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */ | |
67 | #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */ | |
68 | ||
62059d9e EM |
69 | #define KPDK_DKP (0x1 << 31) |
70 | #define KPDK_DK(n) ((n) & 0xff) | |
71 | ||
9c60debd EM |
72 | #define KPREC_OF1 (0x1 << 31) |
73 | #define kPREC_UF1 (0x1 << 30) | |
74 | #define KPREC_OF0 (0x1 << 15) | |
75 | #define KPREC_UF0 (0x1 << 14) | |
76 | ||
77 | #define KPREC_RECOUNT0(n) ((n) & 0xff) | |
78 | #define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff) | |
79 | ||
80 | #define KPMK_MKP (0x1 << 31) | |
81 | #define KPAS_SO (0x1 << 31) | |
82 | #define KPASMKPx_SO (0x1 << 31) | |
83 | ||
84 | #define KPAS_MUKP(n) (((n) >> 26) & 0x1f) | |
85 | #define KPAS_RP(n) (((n) >> 4) & 0xf) | |
86 | #define KPAS_CP(n) ((n) & 0xf) | |
5a90e5bc | 87 | |
1814db69 EM |
88 | #define KPASMKP_MKC_MASK (0xff) |
89 | ||
9c60debd EM |
90 | #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off)) |
91 | #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off)) | |
92 | ||
bd96f378 DT |
93 | #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS) |
94 | #define MAX_KEYPAD_KEYS (MAX_MATRIX_KEY_NUM + MAX_DIRECT_KEY_NUM) | |
1814db69 EM |
95 | |
96 | struct pxa27x_keypad { | |
9eb52139 | 97 | const struct pxa27x_keypad_platform_data *pdata; |
1814db69 EM |
98 | |
99 | struct clk *clk; | |
100 | struct input_dev *input_dev; | |
9c60debd | 101 | void __iomem *mmio_base; |
1814db69 | 102 | |
39ab9dde EM |
103 | int irq; |
104 | ||
bd96f378 DT |
105 | unsigned short keycodes[MAX_KEYPAD_KEYS]; |
106 | int rotary_rel_code[2]; | |
1814db69 | 107 | |
c5ec1948 CX |
108 | unsigned int row_shift; |
109 | ||
1814db69 EM |
110 | /* state row bits of each column scan */ |
111 | uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS]; | |
62059d9e EM |
112 | uint32_t direct_key_state; |
113 | ||
114 | unsigned int direct_key_mask; | |
1814db69 EM |
115 | }; |
116 | ||
e4156979 | 117 | #ifdef CONFIG_OF |
9eb52139 DT |
118 | static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad, |
119 | struct pxa27x_keypad_platform_data *pdata) | |
e4156979 CX |
120 | { |
121 | struct input_dev *input_dev = keypad->input_dev; | |
122 | struct device *dev = input_dev->dev.parent; | |
e4156979 CX |
123 | u32 rows, cols; |
124 | int error; | |
125 | ||
aef01aad | 126 | error = matrix_keypad_parse_properties(dev, &rows, &cols); |
e4156979 CX |
127 | if (error) |
128 | return error; | |
129 | ||
130 | if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) { | |
131 | dev_err(dev, "rows or cols exceeds maximum value\n"); | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
135 | pdata->matrix_key_rows = rows; | |
136 | pdata->matrix_key_cols = cols; | |
137 | ||
138 | error = matrix_keypad_build_keymap(NULL, NULL, | |
139 | pdata->matrix_key_rows, | |
140 | pdata->matrix_key_cols, | |
141 | keypad->keycodes, input_dev); | |
142 | if (error) | |
143 | return error; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
9eb52139 DT |
148 | static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad, |
149 | struct pxa27x_keypad_platform_data *pdata) | |
e4156979 CX |
150 | { |
151 | struct input_dev *input_dev = keypad->input_dev; | |
152 | struct device *dev = input_dev->dev.parent; | |
e4156979 CX |
153 | struct device_node *np = dev->of_node; |
154 | const __be16 *prop; | |
155 | unsigned short code; | |
156 | unsigned int proplen, size; | |
157 | int i; | |
158 | int error; | |
159 | ||
160 | error = of_property_read_u32(np, "marvell,direct-key-count", | |
161 | &pdata->direct_key_num); | |
162 | if (error) { | |
163 | /* | |
164 | * If do not have marvel,direct-key-count defined, | |
165 | * it means direct key is not supported. | |
166 | */ | |
167 | return error == -EINVAL ? 0 : error; | |
168 | } | |
169 | ||
170 | error = of_property_read_u32(np, "marvell,direct-key-mask", | |
171 | &pdata->direct_key_mask); | |
172 | if (error) { | |
173 | if (error != -EINVAL) | |
174 | return error; | |
175 | ||
176 | /* | |
177 | * If marvell,direct-key-mask is not defined, driver will use | |
178 | * default value. Default value is set when configure the keypad. | |
179 | */ | |
180 | pdata->direct_key_mask = 0; | |
181 | } | |
182 | ||
183 | pdata->direct_key_low_active = of_property_read_bool(np, | |
184 | "marvell,direct-key-low-active"); | |
185 | ||
186 | prop = of_get_property(np, "marvell,direct-key-map", &proplen); | |
187 | if (!prop) | |
188 | return -EINVAL; | |
189 | ||
190 | if (proplen % sizeof(u16)) | |
191 | return -EINVAL; | |
192 | ||
193 | size = proplen / sizeof(u16); | |
194 | ||
195 | /* Only MAX_DIRECT_KEY_NUM is accepted.*/ | |
196 | if (size > MAX_DIRECT_KEY_NUM) | |
197 | return -EINVAL; | |
198 | ||
199 | for (i = 0; i < size; i++) { | |
200 | code = be16_to_cpup(prop + i); | |
201 | keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code; | |
202 | __set_bit(code, input_dev->keybit); | |
203 | } | |
204 | ||
205 | return 0; | |
206 | } | |
207 | ||
9eb52139 DT |
208 | static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad, |
209 | struct pxa27x_keypad_platform_data *pdata) | |
e4156979 CX |
210 | { |
211 | const __be32 *prop; | |
212 | int i, relkey_ret; | |
213 | unsigned int code, proplen; | |
214 | const char *rotaryname[2] = { | |
215 | "marvell,rotary0", "marvell,rotary1"}; | |
216 | const char relkeyname[] = {"marvell,rotary-rel-key"}; | |
217 | struct input_dev *input_dev = keypad->input_dev; | |
218 | struct device *dev = input_dev->dev.parent; | |
e4156979 CX |
219 | struct device_node *np = dev->of_node; |
220 | ||
221 | relkey_ret = of_property_read_u32(np, relkeyname, &code); | |
222 | /* if can read correct rotary key-code, we do not need this. */ | |
223 | if (relkey_ret == 0) { | |
224 | unsigned short relcode; | |
225 | ||
226 | /* rotary0 taks lower half, rotary1 taks upper half. */ | |
227 | relcode = code & 0xffff; | |
228 | pdata->rotary0_rel_code = (code & 0xffff); | |
229 | __set_bit(relcode, input_dev->relbit); | |
230 | ||
231 | relcode = code >> 16; | |
232 | pdata->rotary1_rel_code = relcode; | |
233 | __set_bit(relcode, input_dev->relbit); | |
234 | } | |
235 | ||
236 | for (i = 0; i < 2; i++) { | |
237 | prop = of_get_property(np, rotaryname[i], &proplen); | |
238 | /* | |
239 | * If the prop is not set, it means keypad does not need | |
240 | * initialize the rotaryX. | |
241 | */ | |
242 | if (!prop) | |
243 | continue; | |
244 | ||
245 | code = be32_to_cpup(prop); | |
246 | /* | |
247 | * Not all up/down key code are valid. | |
248 | * Now we depends on direct-rel-code. | |
249 | */ | |
250 | if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) { | |
251 | return relkey_ret; | |
252 | } else { | |
253 | unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1); | |
254 | unsigned short keycode; | |
255 | ||
256 | keycode = code & 0xffff; | |
257 | keypad->keycodes[n] = keycode; | |
258 | __set_bit(keycode, input_dev->keybit); | |
259 | ||
260 | keycode = code >> 16; | |
261 | keypad->keycodes[n + 1] = keycode; | |
262 | __set_bit(keycode, input_dev->keybit); | |
263 | ||
264 | if (i == 0) | |
265 | pdata->rotary0_rel_code = -1; | |
266 | else | |
267 | pdata->rotary1_rel_code = -1; | |
268 | } | |
269 | if (i == 0) | |
270 | pdata->enable_rotary0 = 1; | |
271 | else | |
272 | pdata->enable_rotary1 = 1; | |
273 | } | |
274 | ||
275 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; | |
276 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; | |
277 | ||
278 | return 0; | |
279 | } | |
280 | ||
281 | static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad) | |
282 | { | |
283 | struct input_dev *input_dev = keypad->input_dev; | |
284 | struct device *dev = input_dev->dev.parent; | |
285 | struct device_node *np = dev->of_node; | |
9eb52139 | 286 | struct pxa27x_keypad_platform_data *pdata; |
e4156979 CX |
287 | int error; |
288 | ||
9eb52139 DT |
289 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
290 | if (!pdata) { | |
e4156979 CX |
291 | dev_err(dev, "failed to allocate memory for pdata\n"); |
292 | return -ENOMEM; | |
293 | } | |
294 | ||
9eb52139 | 295 | error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata); |
e4156979 CX |
296 | if (error) { |
297 | dev_err(dev, "failed to parse matrix key\n"); | |
298 | return error; | |
299 | } | |
300 | ||
9eb52139 | 301 | error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata); |
e4156979 CX |
302 | if (error) { |
303 | dev_err(dev, "failed to parse direct key\n"); | |
304 | return error; | |
305 | } | |
306 | ||
9eb52139 | 307 | error = pxa27x_keypad_rotary_parse_dt(keypad, pdata); |
e4156979 CX |
308 | if (error) { |
309 | dev_err(dev, "failed to parse rotary key\n"); | |
310 | return error; | |
311 | } | |
312 | ||
313 | error = of_property_read_u32(np, "marvell,debounce-interval", | |
9eb52139 | 314 | &pdata->debounce_interval); |
e4156979 | 315 | if (error) { |
6d86c471 | 316 | dev_err(dev, "failed to parse debounce-interval\n"); |
e4156979 CX |
317 | return error; |
318 | } | |
319 | ||
320 | /* | |
321 | * The keycodes may not only includes matrix key but also the direct | |
322 | * key or rotary key. | |
323 | */ | |
324 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); | |
325 | ||
9eb52139 | 326 | keypad->pdata = pdata; |
e4156979 CX |
327 | return 0; |
328 | } | |
329 | ||
330 | #else | |
331 | ||
332 | static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad) | |
333 | { | |
334 | dev_info(keypad->input_dev->dev.parent, "missing platform data\n"); | |
335 | ||
336 | return -EINVAL; | |
337 | } | |
338 | ||
339 | #endif | |
340 | ||
0a085a94 | 341 | static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad) |
1814db69 | 342 | { |
9eb52139 | 343 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
1814db69 | 344 | struct input_dev *input_dev = keypad->input_dev; |
bd96f378 | 345 | unsigned short keycode; |
1814db69 | 346 | int i; |
0a085a94 | 347 | int error; |
1814db69 | 348 | |
2b2f5147 | 349 | error = matrix_keypad_build_keymap(pdata->matrix_keymap_data, NULL, |
0a085a94 CX |
350 | pdata->matrix_key_rows, |
351 | pdata->matrix_key_cols, | |
352 | keypad->keycodes, input_dev); | |
353 | if (error) | |
354 | return error; | |
1814db69 | 355 | |
0a085a94 CX |
356 | /* |
357 | * The keycodes may not only include matrix keys but also the direct | |
358 | * or rotary keys. | |
359 | */ | |
360 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); | |
471637a5 | 361 | |
0a085a94 | 362 | /* For direct keys. */ |
bd96f378 DT |
363 | for (i = 0; i < pdata->direct_key_num; i++) { |
364 | keycode = pdata->direct_key_map[i]; | |
365 | keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode; | |
366 | __set_bit(keycode, input_dev->keybit); | |
367 | } | |
62059d9e | 368 | |
471637a5 AO |
369 | if (pdata->enable_rotary0) { |
370 | if (pdata->rotary0_up_key && pdata->rotary0_down_key) { | |
bd96f378 DT |
371 | keycode = pdata->rotary0_up_key; |
372 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode; | |
373 | __set_bit(keycode, input_dev->keybit); | |
374 | ||
375 | keycode = pdata->rotary0_down_key; | |
376 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode; | |
377 | __set_bit(keycode, input_dev->keybit); | |
378 | ||
379 | keypad->rotary_rel_code[0] = -1; | |
380 | } else { | |
381 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; | |
52ec7752 | 382 | __set_bit(pdata->rotary0_rel_code, input_dev->relbit); |
bd96f378 | 383 | } |
471637a5 AO |
384 | } |
385 | ||
386 | if (pdata->enable_rotary1) { | |
387 | if (pdata->rotary1_up_key && pdata->rotary1_down_key) { | |
bd96f378 DT |
388 | keycode = pdata->rotary1_up_key; |
389 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode; | |
390 | __set_bit(keycode, input_dev->keybit); | |
391 | ||
392 | keycode = pdata->rotary1_down_key; | |
393 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode; | |
394 | __set_bit(keycode, input_dev->keybit); | |
395 | ||
396 | keypad->rotary_rel_code[1] = -1; | |
397 | } else { | |
398 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; | |
52ec7752 | 399 | __set_bit(pdata->rotary1_rel_code, input_dev->relbit); |
bd96f378 | 400 | } |
471637a5 | 401 | } |
1814db69 | 402 | |
bd96f378 | 403 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
0a085a94 CX |
404 | |
405 | return 0; | |
1814db69 EM |
406 | } |
407 | ||
408 | static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad) | |
409 | { | |
9eb52139 | 410 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
bd96f378 | 411 | struct input_dev *input_dev = keypad->input_dev; |
1814db69 EM |
412 | int row, col, num_keys_pressed = 0; |
413 | uint32_t new_state[MAX_MATRIX_KEY_COLS]; | |
9c60debd | 414 | uint32_t kpas = keypad_readl(KPAS); |
1814db69 EM |
415 | |
416 | num_keys_pressed = KPAS_MUKP(kpas); | |
417 | ||
418 | memset(new_state, 0, sizeof(new_state)); | |
419 | ||
420 | if (num_keys_pressed == 0) | |
421 | goto scan; | |
422 | ||
423 | if (num_keys_pressed == 1) { | |
424 | col = KPAS_CP(kpas); | |
425 | row = KPAS_RP(kpas); | |
426 | ||
427 | /* if invalid row/col, treat as no key pressed */ | |
428 | if (col >= pdata->matrix_key_cols || | |
429 | row >= pdata->matrix_key_rows) | |
430 | goto scan; | |
431 | ||
432 | new_state[col] = (1 << row); | |
433 | goto scan; | |
434 | } | |
435 | ||
436 | if (num_keys_pressed > 1) { | |
9c60debd EM |
437 | uint32_t kpasmkp0 = keypad_readl(KPASMKP0); |
438 | uint32_t kpasmkp1 = keypad_readl(KPASMKP1); | |
439 | uint32_t kpasmkp2 = keypad_readl(KPASMKP2); | |
440 | uint32_t kpasmkp3 = keypad_readl(KPASMKP3); | |
1814db69 EM |
441 | |
442 | new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK; | |
443 | new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK; | |
444 | new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK; | |
445 | new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK; | |
446 | new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK; | |
447 | new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK; | |
448 | new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK; | |
449 | new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK; | |
450 | } | |
451 | scan: | |
452 | for (col = 0; col < pdata->matrix_key_cols; col++) { | |
453 | uint32_t bits_changed; | |
bd96f378 | 454 | int code; |
1814db69 EM |
455 | |
456 | bits_changed = keypad->matrix_key_state[col] ^ new_state[col]; | |
457 | if (bits_changed == 0) | |
458 | continue; | |
459 | ||
460 | for (row = 0; row < pdata->matrix_key_rows; row++) { | |
461 | if ((bits_changed & (1 << row)) == 0) | |
462 | continue; | |
463 | ||
c5ec1948 CX |
464 | code = MATRIX_SCAN_CODE(row, col, keypad->row_shift); |
465 | ||
bd96f378 DT |
466 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
467 | input_report_key(input_dev, keypad->keycodes[code], | |
468 | new_state[col] & (1 << row)); | |
1814db69 EM |
469 | } |
470 | } | |
bd96f378 | 471 | input_sync(input_dev); |
1814db69 EM |
472 | memcpy(keypad->matrix_key_state, new_state, sizeof(new_state)); |
473 | } | |
22d8a73a | 474 | |
d7416f9e EM |
475 | #define DEFAULT_KPREC (0x007f007f) |
476 | ||
62059d9e EM |
477 | static inline int rotary_delta(uint32_t kprec) |
478 | { | |
479 | if (kprec & KPREC_OF0) | |
480 | return (kprec & 0xff) + 0x7f; | |
481 | else if (kprec & KPREC_UF0) | |
482 | return (kprec & 0xff) - 0x7f - 0xff; | |
483 | else | |
484 | return (kprec & 0xff) - 0x7f; | |
485 | } | |
486 | ||
487 | static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta) | |
488 | { | |
489 | struct input_dev *dev = keypad->input_dev; | |
490 | ||
491 | if (delta == 0) | |
492 | return; | |
493 | ||
bd96f378 DT |
494 | if (keypad->rotary_rel_code[r] == -1) { |
495 | int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1); | |
496 | unsigned char keycode = keypad->keycodes[code]; | |
62059d9e EM |
497 | |
498 | /* simulate a press-n-release */ | |
bd96f378 | 499 | input_event(dev, EV_MSC, MSC_SCAN, code); |
62059d9e EM |
500 | input_report_key(dev, keycode, 1); |
501 | input_sync(dev); | |
bd96f378 | 502 | input_event(dev, EV_MSC, MSC_SCAN, code); |
62059d9e EM |
503 | input_report_key(dev, keycode, 0); |
504 | input_sync(dev); | |
505 | } else { | |
506 | input_report_rel(dev, keypad->rotary_rel_code[r], delta); | |
507 | input_sync(dev); | |
508 | } | |
509 | } | |
510 | ||
511 | static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad) | |
512 | { | |
9eb52139 | 513 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
62059d9e EM |
514 | uint32_t kprec; |
515 | ||
516 | /* read and reset to default count value */ | |
9c60debd EM |
517 | kprec = keypad_readl(KPREC); |
518 | keypad_writel(KPREC, DEFAULT_KPREC); | |
62059d9e EM |
519 | |
520 | if (pdata->enable_rotary0) | |
521 | report_rotary_event(keypad, 0, rotary_delta(kprec)); | |
522 | ||
523 | if (pdata->enable_rotary1) | |
524 | report_rotary_event(keypad, 1, rotary_delta(kprec >> 16)); | |
525 | } | |
526 | ||
527 | static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad) | |
528 | { | |
9eb52139 | 529 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
bd96f378 | 530 | struct input_dev *input_dev = keypad->input_dev; |
62059d9e EM |
531 | unsigned int new_state; |
532 | uint32_t kpdk, bits_changed; | |
533 | int i; | |
534 | ||
9c60debd | 535 | kpdk = keypad_readl(KPDK); |
62059d9e EM |
536 | |
537 | if (pdata->enable_rotary0 || pdata->enable_rotary1) | |
538 | pxa27x_keypad_scan_rotary(keypad); | |
539 | ||
ee1d8040 CX |
540 | /* |
541 | * The KPDR_DK only output the key pin level, so it relates to board, | |
542 | * and low level may be active. | |
543 | */ | |
544 | if (pdata->direct_key_low_active) | |
545 | new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask; | |
546 | else | |
547 | new_state = KPDK_DK(kpdk) & keypad->direct_key_mask; | |
548 | ||
62059d9e EM |
549 | bits_changed = keypad->direct_key_state ^ new_state; |
550 | ||
551 | if (bits_changed == 0) | |
552 | return; | |
553 | ||
554 | for (i = 0; i < pdata->direct_key_num; i++) { | |
bd96f378 DT |
555 | if (bits_changed & (1 << i)) { |
556 | int code = MAX_MATRIX_KEY_NUM + i; | |
557 | ||
558 | input_event(input_dev, EV_MSC, MSC_SCAN, code); | |
559 | input_report_key(input_dev, keypad->keycodes[code], | |
560 | new_state & (1 << i)); | |
561 | } | |
62059d9e | 562 | } |
bd96f378 | 563 | input_sync(input_dev); |
62059d9e EM |
564 | keypad->direct_key_state = new_state; |
565 | } | |
566 | ||
3587c258 MB |
567 | static void clear_wakeup_event(struct pxa27x_keypad *keypad) |
568 | { | |
9eb52139 | 569 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
3587c258 MB |
570 | |
571 | if (pdata->clear_wakeup_event) | |
572 | (pdata->clear_wakeup_event)(); | |
573 | } | |
574 | ||
0e5f11aa | 575 | static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id) |
5a90e5bc | 576 | { |
1814db69 | 577 | struct pxa27x_keypad *keypad = dev_id; |
9c60debd | 578 | unsigned long kpc = keypad_readl(KPC); |
62059d9e | 579 | |
3587c258 MB |
580 | clear_wakeup_event(keypad); |
581 | ||
62059d9e EM |
582 | if (kpc & KPC_DI) |
583 | pxa27x_keypad_scan_direct(keypad); | |
5a90e5bc | 584 | |
1814db69 EM |
585 | if (kpc & KPC_MI) |
586 | pxa27x_keypad_scan_matrix(keypad); | |
5a90e5bc RG |
587 | |
588 | return IRQ_HANDLED; | |
589 | } | |
590 | ||
d7416f9e | 591 | static void pxa27x_keypad_config(struct pxa27x_keypad *keypad) |
5a90e5bc | 592 | { |
9eb52139 | 593 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
62059d9e | 594 | unsigned int mask = 0, direct_key_num = 0; |
d7416f9e | 595 | unsigned long kpc = 0; |
1814db69 | 596 | |
904adede VK |
597 | /* clear pending interrupt bit */ |
598 | keypad_readl(KPC); | |
599 | ||
d7416f9e EM |
600 | /* enable matrix keys with automatic scan */ |
601 | if (pdata->matrix_key_rows && pdata->matrix_key_cols) { | |
602 | kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL; | |
603 | kpc |= KPC_MKRN(pdata->matrix_key_rows) | | |
604 | KPC_MKCN(pdata->matrix_key_cols); | |
605 | } | |
5a90e5bc | 606 | |
62059d9e EM |
607 | /* enable rotary key, debounce interval same as direct keys */ |
608 | if (pdata->enable_rotary0) { | |
609 | mask |= 0x03; | |
610 | direct_key_num = 2; | |
611 | kpc |= KPC_REE0; | |
612 | } | |
613 | ||
614 | if (pdata->enable_rotary1) { | |
615 | mask |= 0x0c; | |
616 | direct_key_num = 4; | |
617 | kpc |= KPC_REE1; | |
618 | } | |
619 | ||
620 | if (pdata->direct_key_num > direct_key_num) | |
621 | direct_key_num = pdata->direct_key_num; | |
622 | ||
fb054bf2 CX |
623 | /* |
624 | * Direct keys usage may not start from KP_DKIN0, check the platfrom | |
625 | * mask data to config the specific. | |
626 | */ | |
627 | if (pdata->direct_key_mask) | |
628 | keypad->direct_key_mask = pdata->direct_key_mask; | |
629 | else | |
630 | keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask; | |
62059d9e EM |
631 | |
632 | /* enable direct key */ | |
633 | if (direct_key_num) | |
634 | kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num); | |
5a90e5bc | 635 | |
9c60debd EM |
636 | keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB); |
637 | keypad_writel(KPREC, DEFAULT_KPREC); | |
76cb44e1 | 638 | keypad_writel(KPKDI, pdata->debounce_interval); |
d7416f9e EM |
639 | } |
640 | ||
641 | static int pxa27x_keypad_open(struct input_dev *dev) | |
642 | { | |
643 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); | |
117b2dc5 | 644 | int ret; |
5a90e5bc | 645 | /* Enable unit clock */ |
117b2dc5 AY |
646 | ret = clk_prepare_enable(keypad->clk); |
647 | if (ret) | |
648 | return ret; | |
649 | ||
d7416f9e | 650 | pxa27x_keypad_config(keypad); |
5a90e5bc RG |
651 | |
652 | return 0; | |
653 | } | |
654 | ||
0e5f11aa | 655 | static void pxa27x_keypad_close(struct input_dev *dev) |
5a90e5bc | 656 | { |
1814db69 EM |
657 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
658 | ||
5a90e5bc | 659 | /* Disable clock unit */ |
6ce34a5f | 660 | clk_disable_unprepare(keypad->clk); |
5a90e5bc RG |
661 | } |
662 | ||
b0010911 | 663 | static int pxa27x_keypad_suspend(struct device *dev) |
5a90e5bc | 664 | { |
b0010911 | 665 | struct platform_device *pdev = to_platform_device(dev); |
1814db69 | 666 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
5a90e5bc | 667 | |
6ce34a5f CX |
668 | /* |
669 | * If the keypad is used a wake up source, clock can not be disabled. | |
670 | * Or it can not detect the key pressing. | |
671 | */ | |
39ab9dde EM |
672 | if (device_may_wakeup(&pdev->dev)) |
673 | enable_irq_wake(keypad->irq); | |
6ce34a5f CX |
674 | else |
675 | clk_disable_unprepare(keypad->clk); | |
39ab9dde | 676 | |
5a90e5bc RG |
677 | return 0; |
678 | } | |
679 | ||
b0010911 | 680 | static int pxa27x_keypad_resume(struct device *dev) |
5a90e5bc | 681 | { |
b0010911 | 682 | struct platform_device *pdev = to_platform_device(dev); |
1814db69 | 683 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
1814db69 | 684 | struct input_dev *input_dev = keypad->input_dev; |
117b2dc5 | 685 | int ret = 0; |
5a90e5bc | 686 | |
6ce34a5f CX |
687 | /* |
688 | * If the keypad is used as wake up source, the clock is not turned | |
689 | * off. So do not need configure it again. | |
690 | */ | |
691 | if (device_may_wakeup(&pdev->dev)) { | |
39ab9dde | 692 | disable_irq_wake(keypad->irq); |
6ce34a5f CX |
693 | } else { |
694 | mutex_lock(&input_dev->mutex); | |
39ab9dde | 695 | |
d69f0a43 | 696 | if (input_device_enabled(input_dev)) { |
6ce34a5f | 697 | /* Enable unit clock */ |
117b2dc5 AY |
698 | ret = clk_prepare_enable(keypad->clk); |
699 | if (!ret) | |
700 | pxa27x_keypad_config(keypad); | |
6ce34a5f | 701 | } |
5a90e5bc | 702 | |
6ce34a5f | 703 | mutex_unlock(&input_dev->mutex); |
5a90e5bc RG |
704 | } |
705 | ||
117b2dc5 | 706 | return ret; |
5a90e5bc | 707 | } |
5a90e5bc | 708 | |
a4b4fa51 JC |
709 | static DEFINE_SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops, |
710 | pxa27x_keypad_suspend, pxa27x_keypad_resume); | |
f9f6def8 DT |
711 | |
712 | ||
5298cc4c | 713 | static int pxa27x_keypad_probe(struct platform_device *pdev) |
5a90e5bc | 714 | { |
9eb52139 DT |
715 | const struct pxa27x_keypad_platform_data *pdata = |
716 | dev_get_platdata(&pdev->dev); | |
e4156979 | 717 | struct device_node *np = pdev->dev.of_node; |
1814db69 | 718 | struct pxa27x_keypad *keypad; |
5a90e5bc | 719 | struct input_dev *input_dev; |
9c60debd | 720 | int irq, error; |
5a90e5bc | 721 | |
e4156979 CX |
722 | /* Driver need build keycode from device tree or pdata */ |
723 | if (!np && !pdata) | |
bd96f378 | 724 | return -EINVAL; |
1814db69 | 725 | |
9c60debd | 726 | irq = platform_get_irq(pdev, 0); |
0bec8b7e | 727 | if (irq < 0) |
bd96f378 | 728 | return -ENXIO; |
9c60debd | 729 | |
38c38072 PG |
730 | keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), |
731 | GFP_KERNEL); | |
732 | if (!keypad) | |
733 | return -ENOMEM; | |
734 | ||
735 | input_dev = devm_input_allocate_device(&pdev->dev); | |
736 | if (!input_dev) | |
737 | return -ENOMEM; | |
9c60debd | 738 | |
bd96f378 DT |
739 | keypad->pdata = pdata; |
740 | keypad->input_dev = input_dev; | |
741 | keypad->irq = irq; | |
742 | ||
198a2cca | 743 | keypad->mmio_base = devm_platform_ioremap_resource(pdev, 0); |
38c38072 PG |
744 | if (IS_ERR(keypad->mmio_base)) |
745 | return PTR_ERR(keypad->mmio_base); | |
9c60debd | 746 | |
38c38072 | 747 | keypad->clk = devm_clk_get(&pdev->dev, NULL); |
1814db69 EM |
748 | if (IS_ERR(keypad->clk)) { |
749 | dev_err(&pdev->dev, "failed to get keypad clock\n"); | |
38c38072 | 750 | return PTR_ERR(keypad->clk); |
22d8a73a RK |
751 | } |
752 | ||
9c60debd | 753 | input_dev->name = pdev->name; |
5a90e5bc | 754 | input_dev->id.bustype = BUS_HOST; |
0e5f11aa EM |
755 | input_dev->open = pxa27x_keypad_open; |
756 | input_dev->close = pxa27x_keypad_close; | |
469ba4df | 757 | input_dev->dev.parent = &pdev->dev; |
5a90e5bc | 758 | |
bd96f378 DT |
759 | input_dev->keycode = keypad->keycodes; |
760 | input_dev->keycodesize = sizeof(keypad->keycodes[0]); | |
761 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); | |
762 | ||
1814db69 EM |
763 | input_set_drvdata(input_dev, keypad); |
764 | ||
471637a5 | 765 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
bd96f378 | 766 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
1814db69 | 767 | |
049d75f7 | 768 | if (pdata) { |
e4156979 | 769 | error = pxa27x_keypad_build_keycode(keypad); |
049d75f7 | 770 | } else { |
e4156979 | 771 | error = pxa27x_keypad_build_keycode_from_dt(keypad); |
049d75f7 MD |
772 | /* |
773 | * Data that we get from DT resides in dynamically | |
774 | * allocated memory so we need to update our pdata | |
775 | * pointer. | |
776 | */ | |
777 | pdata = keypad->pdata; | |
778 | } | |
0a085a94 CX |
779 | if (error) { |
780 | dev_err(&pdev->dev, "failed to build keycode\n"); | |
38c38072 | 781 | return error; |
0a085a94 | 782 | } |
bd96f378 | 783 | |
c5ec1948 CX |
784 | keypad->row_shift = get_count_order(pdata->matrix_key_cols); |
785 | ||
bd96f378 DT |
786 | if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) || |
787 | (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) { | |
788 | input_dev->evbit[0] |= BIT_MASK(EV_REL); | |
789 | } | |
5a90e5bc | 790 | |
38c38072 PG |
791 | error = devm_request_irq(&pdev->dev, irq, pxa27x_keypad_irq_handler, |
792 | 0, pdev->name, keypad); | |
5a90e5bc | 793 | if (error) { |
9c60debd | 794 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
38c38072 | 795 | return error; |
5a90e5bc RG |
796 | } |
797 | ||
5a90e5bc RG |
798 | /* Register the input device */ |
799 | error = input_register_device(input_dev); | |
9c60debd EM |
800 | if (error) { |
801 | dev_err(&pdev->dev, "failed to register input device\n"); | |
38c38072 | 802 | return error; |
9c60debd | 803 | } |
5a90e5bc | 804 | |
bd96f378 | 805 | platform_set_drvdata(pdev, keypad); |
39ab9dde EM |
806 | device_init_wakeup(&pdev->dev, 1); |
807 | ||
5a90e5bc | 808 | return 0; |
5a90e5bc RG |
809 | } |
810 | ||
e4156979 CX |
811 | #ifdef CONFIG_OF |
812 | static const struct of_device_id pxa27x_keypad_dt_match[] = { | |
813 | { .compatible = "marvell,pxa27x-keypad" }, | |
814 | {}, | |
815 | }; | |
816 | MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match); | |
817 | #endif | |
818 | ||
0e5f11aa EM |
819 | static struct platform_driver pxa27x_keypad_driver = { |
820 | .probe = pxa27x_keypad_probe, | |
5a90e5bc | 821 | .driver = { |
9c60debd | 822 | .name = "pxa27x-keypad", |
e4156979 | 823 | .of_match_table = of_match_ptr(pxa27x_keypad_dt_match), |
a4b4fa51 | 824 | .pm = pm_sleep_ptr(&pxa27x_keypad_pm_ops), |
5a90e5bc RG |
825 | }, |
826 | }; | |
5146c84f | 827 | module_platform_driver(pxa27x_keypad_driver); |
5a90e5bc | 828 | |
0e5f11aa | 829 | MODULE_DESCRIPTION("PXA27x Keypad Controller Driver"); |
5a90e5bc | 830 | MODULE_LICENSE("GPL"); |
38c38072 PG |
831 | /* work with hotplug and coldplug */ |
832 | MODULE_ALIAS("platform:pxa27x-keypad"); |