Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 1 | /* |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 2 | * linux/drivers/input/keyboard/pxa27x_keypad.c |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 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 |
| 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 King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 26 | #include <linux/clk.h> |
| 27 | #include <linux/err.h> |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 28 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 29 | #include <asm/mach/arch.h> |
| 30 | #include <asm/mach/map.h> |
| 31 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame^] | 32 | #include <mach/hardware.h> |
| 33 | #include <mach/pxa27x_keypad.h> |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 34 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 35 | /* |
| 36 | * Keypad Controller registers |
| 37 | */ |
| 38 | #define KPC 0x0000 /* Keypad Control register */ |
| 39 | #define KPDK 0x0008 /* Keypad Direct Key register */ |
| 40 | #define KPREC 0x0010 /* Keypad Rotary Encoder register */ |
| 41 | #define KPMK 0x0018 /* Keypad Matrix Key register */ |
| 42 | #define KPAS 0x0020 /* Keypad Automatic Scan register */ |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 43 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 44 | /* Keypad Automatic Scan Multiple Key Presser register 0-3 */ |
| 45 | #define KPASMKP0 0x0028 |
| 46 | #define KPASMKP1 0x0030 |
| 47 | #define KPASMKP2 0x0038 |
| 48 | #define KPASMKP3 0x0040 |
| 49 | #define KPKDI 0x0048 |
| 50 | |
| 51 | /* bit definitions */ |
Samuel Ortiz | 688dad4 | 2008-03-20 09:48:14 -0400 | [diff] [blame] | 52 | #define KPC_MKRN(n) ((((n) - 1) & 0x7) << 26) /* matrix key row number */ |
| 53 | #define KPC_MKCN(n) ((((n) - 1) & 0x7) << 23) /* matrix key column number */ |
| 54 | #define KPC_DKN(n) ((((n) - 1) & 0x7) << 6) /* direct key number */ |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 55 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 56 | #define KPC_AS (0x1 << 30) /* Automatic Scan bit */ |
| 57 | #define KPC_ASACT (0x1 << 29) /* Automatic Scan on Activity */ |
| 58 | #define KPC_MI (0x1 << 22) /* Matrix interrupt bit */ |
| 59 | #define KPC_IMKP (0x1 << 21) /* Ignore Multiple Key Press */ |
| 60 | |
| 61 | #define KPC_MS(n) (0x1 << (13 + (n))) /* Matrix scan line 'n' */ |
| 62 | #define KPC_MS_ALL (0xff << 13) |
| 63 | |
| 64 | #define KPC_ME (0x1 << 12) /* Matrix Keypad Enable */ |
| 65 | #define KPC_MIE (0x1 << 11) /* Matrix Interrupt Enable */ |
| 66 | #define KPC_DK_DEB_SEL (0x1 << 9) /* Direct Keypad Debounce Select */ |
| 67 | #define KPC_DI (0x1 << 5) /* Direct key interrupt bit */ |
| 68 | #define KPC_RE_ZERO_DEB (0x1 << 4) /* Rotary Encoder Zero Debounce */ |
| 69 | #define KPC_REE1 (0x1 << 3) /* Rotary Encoder1 Enable */ |
| 70 | #define KPC_REE0 (0x1 << 2) /* Rotary Encoder0 Enable */ |
| 71 | #define KPC_DE (0x1 << 1) /* Direct Keypad Enable */ |
| 72 | #define KPC_DIE (0x1 << 0) /* Direct Keypad interrupt Enable */ |
| 73 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 74 | #define KPDK_DKP (0x1 << 31) |
| 75 | #define KPDK_DK(n) ((n) & 0xff) |
| 76 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 77 | #define KPREC_OF1 (0x1 << 31) |
| 78 | #define kPREC_UF1 (0x1 << 30) |
| 79 | #define KPREC_OF0 (0x1 << 15) |
| 80 | #define KPREC_UF0 (0x1 << 14) |
| 81 | |
| 82 | #define KPREC_RECOUNT0(n) ((n) & 0xff) |
| 83 | #define KPREC_RECOUNT1(n) (((n) >> 16) & 0xff) |
| 84 | |
| 85 | #define KPMK_MKP (0x1 << 31) |
| 86 | #define KPAS_SO (0x1 << 31) |
| 87 | #define KPASMKPx_SO (0x1 << 31) |
| 88 | |
| 89 | #define KPAS_MUKP(n) (((n) >> 26) & 0x1f) |
| 90 | #define KPAS_RP(n) (((n) >> 4) & 0xf) |
| 91 | #define KPAS_CP(n) ((n) & 0xf) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 92 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 93 | #define KPASMKP_MKC_MASK (0xff) |
| 94 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 95 | #define keypad_readl(off) __raw_readl(keypad->mmio_base + (off)) |
| 96 | #define keypad_writel(off, v) __raw_writel((v), keypad->mmio_base + (off)) |
| 97 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 98 | #define MAX_MATRIX_KEY_NUM (8 * 8) |
| 99 | |
| 100 | struct pxa27x_keypad { |
| 101 | struct pxa27x_keypad_platform_data *pdata; |
| 102 | |
| 103 | struct clk *clk; |
| 104 | struct input_dev *input_dev; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 105 | void __iomem *mmio_base; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 106 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 107 | int irq; |
| 108 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 109 | /* matrix key code map */ |
| 110 | unsigned int matrix_keycodes[MAX_MATRIX_KEY_NUM]; |
| 111 | |
| 112 | /* state row bits of each column scan */ |
| 113 | uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS]; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 114 | uint32_t direct_key_state; |
| 115 | |
| 116 | unsigned int direct_key_mask; |
| 117 | |
| 118 | int rotary_rel_code[2]; |
| 119 | int rotary_up_key[2]; |
| 120 | int rotary_down_key[2]; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
| 123 | static void pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad) |
| 124 | { |
| 125 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
| 126 | struct input_dev *input_dev = keypad->input_dev; |
| 127 | unsigned int *key; |
| 128 | int i; |
| 129 | |
| 130 | key = &pdata->matrix_key_map[0]; |
| 131 | for (i = 0; i < pdata->matrix_key_map_size; i++, key++) { |
| 132 | int row = ((*key) >> 28) & 0xf; |
| 133 | int col = ((*key) >> 24) & 0xf; |
| 134 | int code = (*key) & 0xffffff; |
| 135 | |
| 136 | keypad->matrix_keycodes[(row << 3) + col] = code; |
| 137 | set_bit(code, input_dev->keybit); |
| 138 | } |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 139 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 140 | for (i = 0; i < pdata->direct_key_num; i++) |
| 141 | set_bit(pdata->direct_key_map[i], input_dev->keybit); |
| 142 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 143 | keypad->rotary_up_key[0] = pdata->rotary0_up_key; |
| 144 | keypad->rotary_up_key[1] = pdata->rotary1_up_key; |
| 145 | keypad->rotary_down_key[0] = pdata->rotary0_down_key; |
| 146 | keypad->rotary_down_key[1] = pdata->rotary1_down_key; |
| 147 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; |
| 148 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; |
| 149 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 150 | if (pdata->enable_rotary0) { |
| 151 | if (pdata->rotary0_up_key && pdata->rotary0_down_key) { |
| 152 | set_bit(pdata->rotary0_up_key, input_dev->keybit); |
| 153 | set_bit(pdata->rotary0_down_key, input_dev->keybit); |
| 154 | } else |
| 155 | set_bit(pdata->rotary0_rel_code, input_dev->relbit); |
| 156 | } |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 157 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 158 | if (pdata->enable_rotary1) { |
| 159 | if (pdata->rotary1_up_key && pdata->rotary1_down_key) { |
| 160 | set_bit(pdata->rotary1_up_key, input_dev->keybit); |
| 161 | set_bit(pdata->rotary1_down_key, input_dev->keybit); |
| 162 | } else |
| 163 | set_bit(pdata->rotary1_rel_code, input_dev->relbit); |
| 164 | } |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline unsigned int lookup_matrix_keycode( |
| 168 | struct pxa27x_keypad *keypad, int row, int col) |
| 169 | { |
| 170 | return keypad->matrix_keycodes[(row << 3) + col]; |
| 171 | } |
| 172 | |
| 173 | static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad) |
| 174 | { |
| 175 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
| 176 | int row, col, num_keys_pressed = 0; |
| 177 | uint32_t new_state[MAX_MATRIX_KEY_COLS]; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 178 | uint32_t kpas = keypad_readl(KPAS); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 179 | |
| 180 | num_keys_pressed = KPAS_MUKP(kpas); |
| 181 | |
| 182 | memset(new_state, 0, sizeof(new_state)); |
| 183 | |
| 184 | if (num_keys_pressed == 0) |
| 185 | goto scan; |
| 186 | |
| 187 | if (num_keys_pressed == 1) { |
| 188 | col = KPAS_CP(kpas); |
| 189 | row = KPAS_RP(kpas); |
| 190 | |
| 191 | /* if invalid row/col, treat as no key pressed */ |
| 192 | if (col >= pdata->matrix_key_cols || |
| 193 | row >= pdata->matrix_key_rows) |
| 194 | goto scan; |
| 195 | |
| 196 | new_state[col] = (1 << row); |
| 197 | goto scan; |
| 198 | } |
| 199 | |
| 200 | if (num_keys_pressed > 1) { |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 201 | uint32_t kpasmkp0 = keypad_readl(KPASMKP0); |
| 202 | uint32_t kpasmkp1 = keypad_readl(KPASMKP1); |
| 203 | uint32_t kpasmkp2 = keypad_readl(KPASMKP2); |
| 204 | uint32_t kpasmkp3 = keypad_readl(KPASMKP3); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 205 | |
| 206 | new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK; |
| 207 | new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK; |
| 208 | new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK; |
| 209 | new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK; |
| 210 | new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK; |
| 211 | new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK; |
| 212 | new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK; |
| 213 | new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK; |
| 214 | } |
| 215 | scan: |
| 216 | for (col = 0; col < pdata->matrix_key_cols; col++) { |
| 217 | uint32_t bits_changed; |
| 218 | |
| 219 | bits_changed = keypad->matrix_key_state[col] ^ new_state[col]; |
| 220 | if (bits_changed == 0) |
| 221 | continue; |
| 222 | |
| 223 | for (row = 0; row < pdata->matrix_key_rows; row++) { |
| 224 | if ((bits_changed & (1 << row)) == 0) |
| 225 | continue; |
| 226 | |
| 227 | input_report_key(keypad->input_dev, |
| 228 | lookup_matrix_keycode(keypad, row, col), |
| 229 | new_state[col] & (1 << row)); |
| 230 | } |
| 231 | } |
| 232 | input_sync(keypad->input_dev); |
| 233 | memcpy(keypad->matrix_key_state, new_state, sizeof(new_state)); |
| 234 | } |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 235 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 236 | #define DEFAULT_KPREC (0x007f007f) |
| 237 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 238 | static inline int rotary_delta(uint32_t kprec) |
| 239 | { |
| 240 | if (kprec & KPREC_OF0) |
| 241 | return (kprec & 0xff) + 0x7f; |
| 242 | else if (kprec & KPREC_UF0) |
| 243 | return (kprec & 0xff) - 0x7f - 0xff; |
| 244 | else |
| 245 | return (kprec & 0xff) - 0x7f; |
| 246 | } |
| 247 | |
| 248 | static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta) |
| 249 | { |
| 250 | struct input_dev *dev = keypad->input_dev; |
| 251 | |
| 252 | if (delta == 0) |
| 253 | return; |
| 254 | |
| 255 | if (keypad->rotary_up_key[r] && keypad->rotary_down_key[r]) { |
| 256 | int keycode = (delta > 0) ? keypad->rotary_up_key[r] : |
| 257 | keypad->rotary_down_key[r]; |
| 258 | |
| 259 | /* simulate a press-n-release */ |
| 260 | input_report_key(dev, keycode, 1); |
| 261 | input_sync(dev); |
| 262 | input_report_key(dev, keycode, 0); |
| 263 | input_sync(dev); |
| 264 | } else { |
| 265 | input_report_rel(dev, keypad->rotary_rel_code[r], delta); |
| 266 | input_sync(dev); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad) |
| 271 | { |
| 272 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
| 273 | uint32_t kprec; |
| 274 | |
| 275 | /* read and reset to default count value */ |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 276 | kprec = keypad_readl(KPREC); |
| 277 | keypad_writel(KPREC, DEFAULT_KPREC); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 278 | |
| 279 | if (pdata->enable_rotary0) |
| 280 | report_rotary_event(keypad, 0, rotary_delta(kprec)); |
| 281 | |
| 282 | if (pdata->enable_rotary1) |
| 283 | report_rotary_event(keypad, 1, rotary_delta(kprec >> 16)); |
| 284 | } |
| 285 | |
| 286 | static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad) |
| 287 | { |
| 288 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
| 289 | unsigned int new_state; |
| 290 | uint32_t kpdk, bits_changed; |
| 291 | int i; |
| 292 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 293 | kpdk = keypad_readl(KPDK); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 294 | |
| 295 | if (pdata->enable_rotary0 || pdata->enable_rotary1) |
| 296 | pxa27x_keypad_scan_rotary(keypad); |
| 297 | |
| 298 | if (pdata->direct_key_map == NULL) |
| 299 | return; |
| 300 | |
| 301 | new_state = KPDK_DK(kpdk) & keypad->direct_key_mask; |
| 302 | bits_changed = keypad->direct_key_state ^ new_state; |
| 303 | |
| 304 | if (bits_changed == 0) |
| 305 | return; |
| 306 | |
| 307 | for (i = 0; i < pdata->direct_key_num; i++) { |
| 308 | if (bits_changed & (1 << i)) |
| 309 | input_report_key(keypad->input_dev, |
| 310 | pdata->direct_key_map[i], |
| 311 | (new_state & (1 << i))); |
| 312 | } |
| 313 | input_sync(keypad->input_dev); |
| 314 | keypad->direct_key_state = new_state; |
| 315 | } |
| 316 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 317 | static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 318 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 319 | struct pxa27x_keypad *keypad = dev_id; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 320 | unsigned long kpc = keypad_readl(KPC); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 321 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 322 | if (kpc & KPC_DI) |
| 323 | pxa27x_keypad_scan_direct(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 324 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 325 | if (kpc & KPC_MI) |
| 326 | pxa27x_keypad_scan_matrix(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 327 | |
| 328 | return IRQ_HANDLED; |
| 329 | } |
| 330 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 331 | static void pxa27x_keypad_config(struct pxa27x_keypad *keypad) |
| 332 | { |
| 333 | struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 334 | unsigned int mask = 0, direct_key_num = 0; |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 335 | unsigned long kpc = 0; |
| 336 | |
| 337 | /* enable matrix keys with automatic scan */ |
| 338 | if (pdata->matrix_key_rows && pdata->matrix_key_cols) { |
| 339 | kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL; |
| 340 | kpc |= KPC_MKRN(pdata->matrix_key_rows) | |
| 341 | KPC_MKCN(pdata->matrix_key_cols); |
| 342 | } |
| 343 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 344 | /* enable rotary key, debounce interval same as direct keys */ |
| 345 | if (pdata->enable_rotary0) { |
| 346 | mask |= 0x03; |
| 347 | direct_key_num = 2; |
| 348 | kpc |= KPC_REE0; |
| 349 | } |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 350 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 351 | if (pdata->enable_rotary1) { |
| 352 | mask |= 0x0c; |
| 353 | direct_key_num = 4; |
| 354 | kpc |= KPC_REE1; |
| 355 | } |
| 356 | |
| 357 | if (pdata->direct_key_num > direct_key_num) |
| 358 | direct_key_num = pdata->direct_key_num; |
| 359 | |
| 360 | keypad->direct_key_mask = ((2 << direct_key_num) - 1) & ~mask; |
| 361 | |
| 362 | /* enable direct key */ |
| 363 | if (direct_key_num) |
| 364 | kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num); |
| 365 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 366 | keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB); |
| 367 | keypad_writel(KPREC, DEFAULT_KPREC); |
Eric Miao | 76cb44e | 2008-01-31 00:59:23 -0500 | [diff] [blame] | 368 | keypad_writel(KPKDI, pdata->debounce_interval); |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 369 | } |
| 370 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 371 | static int pxa27x_keypad_open(struct input_dev *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 372 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 373 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
| 374 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 375 | /* Enable unit clock */ |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 376 | clk_enable(keypad->clk); |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 377 | pxa27x_keypad_config(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 382 | static void pxa27x_keypad_close(struct input_dev *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 383 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 384 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
| 385 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 386 | /* Disable clock unit */ |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 387 | clk_disable(keypad->clk); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | #ifdef CONFIG_PM |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 391 | static int pxa27x_keypad_suspend(struct platform_device *pdev, pm_message_t state) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 392 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 393 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 394 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 395 | clk_disable(keypad->clk); |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 396 | |
| 397 | if (device_may_wakeup(&pdev->dev)) |
| 398 | enable_irq_wake(keypad->irq); |
| 399 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 400 | return 0; |
| 401 | } |
| 402 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 403 | static int pxa27x_keypad_resume(struct platform_device *pdev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 404 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 405 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 406 | struct input_dev *input_dev = keypad->input_dev; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 407 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 408 | if (device_may_wakeup(&pdev->dev)) |
| 409 | disable_irq_wake(keypad->irq); |
| 410 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 411 | mutex_lock(&input_dev->mutex); |
| 412 | |
| 413 | if (input_dev->users) { |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 414 | /* Enable unit clock */ |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 415 | clk_enable(keypad->clk); |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 416 | pxa27x_keypad_config(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | mutex_unlock(&input_dev->mutex); |
| 420 | |
| 421 | return 0; |
| 422 | } |
| 423 | #else |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 424 | #define pxa27x_keypad_suspend NULL |
| 425 | #define pxa27x_keypad_resume NULL |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 426 | #endif |
| 427 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 428 | #define res_size(res) ((res)->end - (res)->start + 1) |
| 429 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 430 | static int __devinit pxa27x_keypad_probe(struct platform_device *pdev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 431 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 432 | struct pxa27x_keypad *keypad; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 433 | struct input_dev *input_dev; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 434 | struct resource *res; |
| 435 | int irq, error; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 436 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 437 | keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL); |
| 438 | if (keypad == NULL) { |
| 439 | dev_err(&pdev->dev, "failed to allocate driver data\n"); |
| 440 | return -ENOMEM; |
| 441 | } |
| 442 | |
| 443 | keypad->pdata = pdev->dev.platform_data; |
| 444 | if (keypad->pdata == NULL) { |
| 445 | dev_err(&pdev->dev, "no platform data defined\n"); |
| 446 | error = -EINVAL; |
| 447 | goto failed_free; |
| 448 | } |
| 449 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 450 | irq = platform_get_irq(pdev, 0); |
| 451 | if (irq < 0) { |
| 452 | dev_err(&pdev->dev, "failed to get keypad irq\n"); |
| 453 | error = -ENXIO; |
| 454 | goto failed_free; |
| 455 | } |
| 456 | |
| 457 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 458 | if (res == NULL) { |
| 459 | dev_err(&pdev->dev, "failed to get I/O memory\n"); |
| 460 | error = -ENXIO; |
| 461 | goto failed_free; |
| 462 | } |
| 463 | |
| 464 | res = request_mem_region(res->start, res_size(res), pdev->name); |
| 465 | if (res == NULL) { |
| 466 | dev_err(&pdev->dev, "failed to request I/O memory\n"); |
| 467 | error = -EBUSY; |
| 468 | goto failed_free; |
| 469 | } |
| 470 | |
| 471 | keypad->mmio_base = ioremap(res->start, res_size(res)); |
| 472 | if (keypad->mmio_base == NULL) { |
| 473 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); |
| 474 | error = -ENXIO; |
| 475 | goto failed_free_mem; |
| 476 | } |
| 477 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 478 | keypad->clk = clk_get(&pdev->dev, "KBDCLK"); |
| 479 | if (IS_ERR(keypad->clk)) { |
| 480 | dev_err(&pdev->dev, "failed to get keypad clock\n"); |
| 481 | error = PTR_ERR(keypad->clk); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 482 | goto failed_free_io; |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 483 | } |
| 484 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 485 | /* Create and register the input driver. */ |
| 486 | input_dev = input_allocate_device(); |
| 487 | if (!input_dev) { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 488 | dev_err(&pdev->dev, "failed to allocate input device\n"); |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 489 | error = -ENOMEM; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 490 | goto failed_put_clk; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 493 | input_dev->name = pdev->name; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 494 | input_dev->id.bustype = BUS_HOST; |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 495 | input_dev->open = pxa27x_keypad_open; |
| 496 | input_dev->close = pxa27x_keypad_close; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 497 | input_dev->dev.parent = &pdev->dev; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 498 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 499 | keypad->input_dev = input_dev; |
| 500 | input_set_drvdata(input_dev, keypad); |
| 501 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 502 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
| 503 | if ((keypad->pdata->enable_rotary0 && |
| 504 | keypad->pdata->rotary0_rel_code) || |
| 505 | (keypad->pdata->enable_rotary1 && |
| 506 | keypad->pdata->rotary1_rel_code)) { |
| 507 | input_dev->evbit[0] |= BIT_MASK(EV_REL); |
| 508 | } |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 509 | |
| 510 | pxa27x_keypad_build_keycode(keypad); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 511 | platform_set_drvdata(pdev, keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 512 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 513 | error = request_irq(irq, pxa27x_keypad_irq_handler, IRQF_DISABLED, |
| 514 | pdev->name, keypad); |
| 515 | if (error) { |
| 516 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
| 517 | goto failed_free_dev; |
| 518 | } |
| 519 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 520 | keypad->irq = irq; |
| 521 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 522 | /* Register the input device */ |
| 523 | error = input_register_device(input_dev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 524 | if (error) { |
| 525 | dev_err(&pdev->dev, "failed to register input device\n"); |
| 526 | goto failed_free_irq; |
| 527 | } |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 528 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 529 | device_init_wakeup(&pdev->dev, 1); |
| 530 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 531 | return 0; |
| 532 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 533 | failed_free_irq: |
| 534 | free_irq(irq, pdev); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 535 | platform_set_drvdata(pdev, NULL); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 536 | failed_free_dev: |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 537 | input_free_device(input_dev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 538 | failed_put_clk: |
| 539 | clk_put(keypad->clk); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 540 | failed_free_io: |
| 541 | iounmap(keypad->mmio_base); |
| 542 | failed_free_mem: |
| 543 | release_mem_region(res->start, res_size(res)); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 544 | failed_free: |
| 545 | kfree(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 546 | return error; |
| 547 | } |
| 548 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 549 | static int __devexit pxa27x_keypad_remove(struct platform_device *pdev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 550 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 551 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 552 | struct resource *res; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 553 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 554 | free_irq(keypad->irq, pdev); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 555 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 556 | clk_disable(keypad->clk); |
| 557 | clk_put(keypad->clk); |
| 558 | |
| 559 | input_unregister_device(keypad->input_dev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 560 | input_free_device(keypad->input_dev); |
| 561 | |
| 562 | iounmap(keypad->mmio_base); |
| 563 | |
| 564 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 565 | release_mem_region(res->start, res_size(res)); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 566 | |
| 567 | platform_set_drvdata(pdev, NULL); |
| 568 | kfree(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 572 | /* work with hotplug and coldplug */ |
| 573 | MODULE_ALIAS("platform:pxa27x-keypad"); |
| 574 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 575 | static struct platform_driver pxa27x_keypad_driver = { |
| 576 | .probe = pxa27x_keypad_probe, |
| 577 | .remove = __devexit_p(pxa27x_keypad_remove), |
| 578 | .suspend = pxa27x_keypad_suspend, |
| 579 | .resume = pxa27x_keypad_resume, |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 580 | .driver = { |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 581 | .name = "pxa27x-keypad", |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 582 | .owner = THIS_MODULE, |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 583 | }, |
| 584 | }; |
| 585 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 586 | static int __init pxa27x_keypad_init(void) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 587 | { |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 588 | return platform_driver_register(&pxa27x_keypad_driver); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 591 | static void __exit pxa27x_keypad_exit(void) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 592 | { |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 593 | platform_driver_unregister(&pxa27x_keypad_driver); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 594 | } |
| 595 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 596 | module_init(pxa27x_keypad_init); |
| 597 | module_exit(pxa27x_keypad_exit); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 598 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 599 | MODULE_DESCRIPTION("PXA27x Keypad Controller Driver"); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 600 | MODULE_LICENSE("GPL"); |