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 |
Nicolas Pitre | 2f82af0 | 2009-09-14 03:25:28 -0400 | [diff] [blame] | 11 | * on some suggestions by Nicolas Pitre <nico@fluxnic.net>. |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 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> |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 21 | #include <linux/interrupt.h> |
| 22 | #include <linux/input.h> |
| 23 | #include <linux/device.h> |
| 24 | #include <linux/platform_device.h> |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 25 | #include <linux/clk.h> |
| 26 | #include <linux/err.h> |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 27 | #include <linux/input/matrix_keypad.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Sachin Kamat | 95ba740 | 2013-10-06 00:55:51 -0700 | [diff] [blame] | 29 | #include <linux/of.h> |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 30 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 31 | #include <asm/mach/arch.h> |
| 32 | #include <asm/mach/map.h> |
| 33 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 34 | #include <mach/hardware.h> |
Arnd Bergmann | 293b2da | 2012-08-24 15:16:48 +0200 | [diff] [blame] | 35 | #include <linux/platform_data/keypad-pxa27x.h> |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 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 */ |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 44 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 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 */ |
Samuel Ortiz | 688dad4 | 2008-03-20 09:48:14 -0400 | [diff] [blame] | 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 */ |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 56 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 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 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 75 | #define KPDK_DKP (0x1 << 31) |
| 76 | #define KPDK_DK(n) ((n) & 0xff) |
| 77 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 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) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 93 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 94 | #define KPASMKP_MKC_MASK (0xff) |
| 95 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 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 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 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) |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 101 | |
| 102 | struct pxa27x_keypad { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 103 | const struct pxa27x_keypad_platform_data *pdata; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 104 | |
| 105 | struct clk *clk; |
| 106 | struct input_dev *input_dev; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 107 | void __iomem *mmio_base; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 108 | |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 109 | int irq; |
| 110 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 111 | unsigned short keycodes[MAX_KEYPAD_KEYS]; |
| 112 | int rotary_rel_code[2]; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 113 | |
Chao Xie | c5ec194 | 2014-05-12 19:20:48 -0700 | [diff] [blame] | 114 | unsigned int row_shift; |
| 115 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 116 | /* state row bits of each column scan */ |
| 117 | uint32_t matrix_key_state[MAX_MATRIX_KEY_COLS]; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 118 | uint32_t direct_key_state; |
| 119 | |
| 120 | unsigned int direct_key_mask; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 121 | }; |
| 122 | |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 123 | #ifdef CONFIG_OF |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 124 | static int pxa27x_keypad_matrix_key_parse_dt(struct pxa27x_keypad *keypad, |
| 125 | struct pxa27x_keypad_platform_data *pdata) |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 126 | { |
| 127 | struct input_dev *input_dev = keypad->input_dev; |
| 128 | struct device *dev = input_dev->dev.parent; |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 129 | u32 rows, cols; |
| 130 | int error; |
| 131 | |
| 132 | error = matrix_keypad_parse_of_params(dev, &rows, &cols); |
| 133 | if (error) |
| 134 | return error; |
| 135 | |
| 136 | if (rows > MAX_MATRIX_KEY_ROWS || cols > MAX_MATRIX_KEY_COLS) { |
| 137 | dev_err(dev, "rows or cols exceeds maximum value\n"); |
| 138 | return -EINVAL; |
| 139 | } |
| 140 | |
| 141 | pdata->matrix_key_rows = rows; |
| 142 | pdata->matrix_key_cols = cols; |
| 143 | |
| 144 | error = matrix_keypad_build_keymap(NULL, NULL, |
| 145 | pdata->matrix_key_rows, |
| 146 | pdata->matrix_key_cols, |
| 147 | keypad->keycodes, input_dev); |
| 148 | if (error) |
| 149 | return error; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 154 | static int pxa27x_keypad_direct_key_parse_dt(struct pxa27x_keypad *keypad, |
| 155 | struct pxa27x_keypad_platform_data *pdata) |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 156 | { |
| 157 | struct input_dev *input_dev = keypad->input_dev; |
| 158 | struct device *dev = input_dev->dev.parent; |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 159 | struct device_node *np = dev->of_node; |
| 160 | const __be16 *prop; |
| 161 | unsigned short code; |
| 162 | unsigned int proplen, size; |
| 163 | int i; |
| 164 | int error; |
| 165 | |
| 166 | error = of_property_read_u32(np, "marvell,direct-key-count", |
| 167 | &pdata->direct_key_num); |
| 168 | if (error) { |
| 169 | /* |
| 170 | * If do not have marvel,direct-key-count defined, |
| 171 | * it means direct key is not supported. |
| 172 | */ |
| 173 | return error == -EINVAL ? 0 : error; |
| 174 | } |
| 175 | |
| 176 | error = of_property_read_u32(np, "marvell,direct-key-mask", |
| 177 | &pdata->direct_key_mask); |
| 178 | if (error) { |
| 179 | if (error != -EINVAL) |
| 180 | return error; |
| 181 | |
| 182 | /* |
| 183 | * If marvell,direct-key-mask is not defined, driver will use |
| 184 | * default value. Default value is set when configure the keypad. |
| 185 | */ |
| 186 | pdata->direct_key_mask = 0; |
| 187 | } |
| 188 | |
| 189 | pdata->direct_key_low_active = of_property_read_bool(np, |
| 190 | "marvell,direct-key-low-active"); |
| 191 | |
| 192 | prop = of_get_property(np, "marvell,direct-key-map", &proplen); |
| 193 | if (!prop) |
| 194 | return -EINVAL; |
| 195 | |
| 196 | if (proplen % sizeof(u16)) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | size = proplen / sizeof(u16); |
| 200 | |
| 201 | /* Only MAX_DIRECT_KEY_NUM is accepted.*/ |
| 202 | if (size > MAX_DIRECT_KEY_NUM) |
| 203 | return -EINVAL; |
| 204 | |
| 205 | for (i = 0; i < size; i++) { |
| 206 | code = be16_to_cpup(prop + i); |
| 207 | keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = code; |
| 208 | __set_bit(code, input_dev->keybit); |
| 209 | } |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 214 | static int pxa27x_keypad_rotary_parse_dt(struct pxa27x_keypad *keypad, |
| 215 | struct pxa27x_keypad_platform_data *pdata) |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 216 | { |
| 217 | const __be32 *prop; |
| 218 | int i, relkey_ret; |
| 219 | unsigned int code, proplen; |
| 220 | const char *rotaryname[2] = { |
| 221 | "marvell,rotary0", "marvell,rotary1"}; |
| 222 | const char relkeyname[] = {"marvell,rotary-rel-key"}; |
| 223 | struct input_dev *input_dev = keypad->input_dev; |
| 224 | struct device *dev = input_dev->dev.parent; |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 225 | struct device_node *np = dev->of_node; |
| 226 | |
| 227 | relkey_ret = of_property_read_u32(np, relkeyname, &code); |
| 228 | /* if can read correct rotary key-code, we do not need this. */ |
| 229 | if (relkey_ret == 0) { |
| 230 | unsigned short relcode; |
| 231 | |
| 232 | /* rotary0 taks lower half, rotary1 taks upper half. */ |
| 233 | relcode = code & 0xffff; |
| 234 | pdata->rotary0_rel_code = (code & 0xffff); |
| 235 | __set_bit(relcode, input_dev->relbit); |
| 236 | |
| 237 | relcode = code >> 16; |
| 238 | pdata->rotary1_rel_code = relcode; |
| 239 | __set_bit(relcode, input_dev->relbit); |
| 240 | } |
| 241 | |
| 242 | for (i = 0; i < 2; i++) { |
| 243 | prop = of_get_property(np, rotaryname[i], &proplen); |
| 244 | /* |
| 245 | * If the prop is not set, it means keypad does not need |
| 246 | * initialize the rotaryX. |
| 247 | */ |
| 248 | if (!prop) |
| 249 | continue; |
| 250 | |
| 251 | code = be32_to_cpup(prop); |
| 252 | /* |
| 253 | * Not all up/down key code are valid. |
| 254 | * Now we depends on direct-rel-code. |
| 255 | */ |
| 256 | if ((!(code & 0xffff) || !(code >> 16)) && relkey_ret) { |
| 257 | return relkey_ret; |
| 258 | } else { |
| 259 | unsigned int n = MAX_MATRIX_KEY_NUM + (i << 1); |
| 260 | unsigned short keycode; |
| 261 | |
| 262 | keycode = code & 0xffff; |
| 263 | keypad->keycodes[n] = keycode; |
| 264 | __set_bit(keycode, input_dev->keybit); |
| 265 | |
| 266 | keycode = code >> 16; |
| 267 | keypad->keycodes[n + 1] = keycode; |
| 268 | __set_bit(keycode, input_dev->keybit); |
| 269 | |
| 270 | if (i == 0) |
| 271 | pdata->rotary0_rel_code = -1; |
| 272 | else |
| 273 | pdata->rotary1_rel_code = -1; |
| 274 | } |
| 275 | if (i == 0) |
| 276 | pdata->enable_rotary0 = 1; |
| 277 | else |
| 278 | pdata->enable_rotary1 = 1; |
| 279 | } |
| 280 | |
| 281 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; |
| 282 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad) |
| 288 | { |
| 289 | struct input_dev *input_dev = keypad->input_dev; |
| 290 | struct device *dev = input_dev->dev.parent; |
| 291 | struct device_node *np = dev->of_node; |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 292 | struct pxa27x_keypad_platform_data *pdata; |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 293 | int error; |
| 294 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 295 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 296 | if (!pdata) { |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 297 | dev_err(dev, "failed to allocate memory for pdata\n"); |
| 298 | return -ENOMEM; |
| 299 | } |
| 300 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 301 | error = pxa27x_keypad_matrix_key_parse_dt(keypad, pdata); |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 302 | if (error) { |
| 303 | dev_err(dev, "failed to parse matrix key\n"); |
| 304 | return error; |
| 305 | } |
| 306 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 307 | error = pxa27x_keypad_direct_key_parse_dt(keypad, pdata); |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 308 | if (error) { |
| 309 | dev_err(dev, "failed to parse direct key\n"); |
| 310 | return error; |
| 311 | } |
| 312 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 313 | error = pxa27x_keypad_rotary_parse_dt(keypad, pdata); |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 314 | if (error) { |
| 315 | dev_err(dev, "failed to parse rotary key\n"); |
| 316 | return error; |
| 317 | } |
| 318 | |
| 319 | error = of_property_read_u32(np, "marvell,debounce-interval", |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 320 | &pdata->debounce_interval); |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 321 | if (error) { |
| 322 | dev_err(dev, "failed to parse debpunce-interval\n"); |
| 323 | return error; |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * The keycodes may not only includes matrix key but also the direct |
| 328 | * key or rotary key. |
| 329 | */ |
| 330 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); |
| 331 | |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 332 | keypad->pdata = pdata; |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | #else |
| 337 | |
| 338 | static int pxa27x_keypad_build_keycode_from_dt(struct pxa27x_keypad *keypad) |
| 339 | { |
| 340 | dev_info(keypad->input_dev->dev.parent, "missing platform data\n"); |
| 341 | |
| 342 | return -EINVAL; |
| 343 | } |
| 344 | |
| 345 | #endif |
| 346 | |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 347 | static int pxa27x_keypad_build_keycode(struct pxa27x_keypad *keypad) |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 348 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 349 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 350 | struct input_dev *input_dev = keypad->input_dev; |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 351 | const struct matrix_keymap_data *keymap_data = |
| 352 | pdata ? pdata->matrix_keymap_data : NULL; |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 353 | unsigned short keycode; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 354 | int i; |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 355 | int error; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 356 | |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 357 | error = matrix_keypad_build_keymap(keymap_data, NULL, |
| 358 | pdata->matrix_key_rows, |
| 359 | pdata->matrix_key_cols, |
| 360 | keypad->keycodes, input_dev); |
| 361 | if (error) |
| 362 | return error; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 363 | |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 364 | /* |
| 365 | * The keycodes may not only include matrix keys but also the direct |
| 366 | * or rotary keys. |
| 367 | */ |
| 368 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 369 | |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 370 | /* For direct keys. */ |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 371 | for (i = 0; i < pdata->direct_key_num; i++) { |
| 372 | keycode = pdata->direct_key_map[i]; |
| 373 | keypad->keycodes[MAX_MATRIX_KEY_NUM + i] = keycode; |
| 374 | __set_bit(keycode, input_dev->keybit); |
| 375 | } |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 376 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 377 | if (pdata->enable_rotary0) { |
| 378 | if (pdata->rotary0_up_key && pdata->rotary0_down_key) { |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 379 | keycode = pdata->rotary0_up_key; |
| 380 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 0] = keycode; |
| 381 | __set_bit(keycode, input_dev->keybit); |
| 382 | |
| 383 | keycode = pdata->rotary0_down_key; |
| 384 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 1] = keycode; |
| 385 | __set_bit(keycode, input_dev->keybit); |
| 386 | |
| 387 | keypad->rotary_rel_code[0] = -1; |
| 388 | } else { |
| 389 | keypad->rotary_rel_code[0] = pdata->rotary0_rel_code; |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 390 | __set_bit(pdata->rotary0_rel_code, input_dev->relbit); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 391 | } |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 392 | } |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 393 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 394 | if (pdata->enable_rotary1) { |
| 395 | if (pdata->rotary1_up_key && pdata->rotary1_down_key) { |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 396 | keycode = pdata->rotary1_up_key; |
| 397 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 2] = keycode; |
| 398 | __set_bit(keycode, input_dev->keybit); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 399 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 400 | keycode = pdata->rotary1_down_key; |
| 401 | keypad->keycodes[MAX_MATRIX_KEY_NUM + 3] = keycode; |
| 402 | __set_bit(keycode, input_dev->keybit); |
| 403 | |
| 404 | keypad->rotary_rel_code[1] = -1; |
| 405 | } else { |
| 406 | keypad->rotary_rel_code[1] = pdata->rotary1_rel_code; |
| 407 | __set_bit(pdata->rotary1_rel_code, input_dev->relbit); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | __clear_bit(KEY_RESERVED, input_dev->keybit); |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 412 | |
| 413 | return 0; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static void pxa27x_keypad_scan_matrix(struct pxa27x_keypad *keypad) |
| 417 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 418 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 419 | struct input_dev *input_dev = keypad->input_dev; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 420 | int row, col, num_keys_pressed = 0; |
| 421 | uint32_t new_state[MAX_MATRIX_KEY_COLS]; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 422 | uint32_t kpas = keypad_readl(KPAS); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 423 | |
| 424 | num_keys_pressed = KPAS_MUKP(kpas); |
| 425 | |
| 426 | memset(new_state, 0, sizeof(new_state)); |
| 427 | |
| 428 | if (num_keys_pressed == 0) |
| 429 | goto scan; |
| 430 | |
| 431 | if (num_keys_pressed == 1) { |
| 432 | col = KPAS_CP(kpas); |
| 433 | row = KPAS_RP(kpas); |
| 434 | |
| 435 | /* if invalid row/col, treat as no key pressed */ |
| 436 | if (col >= pdata->matrix_key_cols || |
| 437 | row >= pdata->matrix_key_rows) |
| 438 | goto scan; |
| 439 | |
| 440 | new_state[col] = (1 << row); |
| 441 | goto scan; |
| 442 | } |
| 443 | |
| 444 | if (num_keys_pressed > 1) { |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 445 | uint32_t kpasmkp0 = keypad_readl(KPASMKP0); |
| 446 | uint32_t kpasmkp1 = keypad_readl(KPASMKP1); |
| 447 | uint32_t kpasmkp2 = keypad_readl(KPASMKP2); |
| 448 | uint32_t kpasmkp3 = keypad_readl(KPASMKP3); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 449 | |
| 450 | new_state[0] = kpasmkp0 & KPASMKP_MKC_MASK; |
| 451 | new_state[1] = (kpasmkp0 >> 16) & KPASMKP_MKC_MASK; |
| 452 | new_state[2] = kpasmkp1 & KPASMKP_MKC_MASK; |
| 453 | new_state[3] = (kpasmkp1 >> 16) & KPASMKP_MKC_MASK; |
| 454 | new_state[4] = kpasmkp2 & KPASMKP_MKC_MASK; |
| 455 | new_state[5] = (kpasmkp2 >> 16) & KPASMKP_MKC_MASK; |
| 456 | new_state[6] = kpasmkp3 & KPASMKP_MKC_MASK; |
| 457 | new_state[7] = (kpasmkp3 >> 16) & KPASMKP_MKC_MASK; |
| 458 | } |
| 459 | scan: |
| 460 | for (col = 0; col < pdata->matrix_key_cols; col++) { |
| 461 | uint32_t bits_changed; |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 462 | int code; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 463 | |
| 464 | bits_changed = keypad->matrix_key_state[col] ^ new_state[col]; |
| 465 | if (bits_changed == 0) |
| 466 | continue; |
| 467 | |
| 468 | for (row = 0; row < pdata->matrix_key_rows; row++) { |
| 469 | if ((bits_changed & (1 << row)) == 0) |
| 470 | continue; |
| 471 | |
Chao Xie | c5ec194 | 2014-05-12 19:20:48 -0700 | [diff] [blame] | 472 | code = MATRIX_SCAN_CODE(row, col, keypad->row_shift); |
| 473 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 474 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 475 | input_report_key(input_dev, keypad->keycodes[code], |
| 476 | new_state[col] & (1 << row)); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 477 | } |
| 478 | } |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 479 | input_sync(input_dev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 480 | memcpy(keypad->matrix_key_state, new_state, sizeof(new_state)); |
| 481 | } |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 482 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 483 | #define DEFAULT_KPREC (0x007f007f) |
| 484 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 485 | static inline int rotary_delta(uint32_t kprec) |
| 486 | { |
| 487 | if (kprec & KPREC_OF0) |
| 488 | return (kprec & 0xff) + 0x7f; |
| 489 | else if (kprec & KPREC_UF0) |
| 490 | return (kprec & 0xff) - 0x7f - 0xff; |
| 491 | else |
| 492 | return (kprec & 0xff) - 0x7f; |
| 493 | } |
| 494 | |
| 495 | static void report_rotary_event(struct pxa27x_keypad *keypad, int r, int delta) |
| 496 | { |
| 497 | struct input_dev *dev = keypad->input_dev; |
| 498 | |
| 499 | if (delta == 0) |
| 500 | return; |
| 501 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 502 | if (keypad->rotary_rel_code[r] == -1) { |
| 503 | int code = MAX_MATRIX_KEY_NUM + 2 * r + (delta > 0 ? 0 : 1); |
| 504 | unsigned char keycode = keypad->keycodes[code]; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 505 | |
| 506 | /* simulate a press-n-release */ |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 507 | input_event(dev, EV_MSC, MSC_SCAN, code); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 508 | input_report_key(dev, keycode, 1); |
| 509 | input_sync(dev); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 510 | input_event(dev, EV_MSC, MSC_SCAN, code); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 511 | input_report_key(dev, keycode, 0); |
| 512 | input_sync(dev); |
| 513 | } else { |
| 514 | input_report_rel(dev, keypad->rotary_rel_code[r], delta); |
| 515 | input_sync(dev); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | static void pxa27x_keypad_scan_rotary(struct pxa27x_keypad *keypad) |
| 520 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 521 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 522 | uint32_t kprec; |
| 523 | |
| 524 | /* read and reset to default count value */ |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 525 | kprec = keypad_readl(KPREC); |
| 526 | keypad_writel(KPREC, DEFAULT_KPREC); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 527 | |
| 528 | if (pdata->enable_rotary0) |
| 529 | report_rotary_event(keypad, 0, rotary_delta(kprec)); |
| 530 | |
| 531 | if (pdata->enable_rotary1) |
| 532 | report_rotary_event(keypad, 1, rotary_delta(kprec >> 16)); |
| 533 | } |
| 534 | |
| 535 | static void pxa27x_keypad_scan_direct(struct pxa27x_keypad *keypad) |
| 536 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 537 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 538 | struct input_dev *input_dev = keypad->input_dev; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 539 | unsigned int new_state; |
| 540 | uint32_t kpdk, bits_changed; |
| 541 | int i; |
| 542 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 543 | kpdk = keypad_readl(KPDK); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 544 | |
| 545 | if (pdata->enable_rotary0 || pdata->enable_rotary1) |
| 546 | pxa27x_keypad_scan_rotary(keypad); |
| 547 | |
Chao Xie | ee1d804 | 2012-04-01 10:08:03 +0800 | [diff] [blame] | 548 | /* |
| 549 | * The KPDR_DK only output the key pin level, so it relates to board, |
| 550 | * and low level may be active. |
| 551 | */ |
| 552 | if (pdata->direct_key_low_active) |
| 553 | new_state = ~KPDK_DK(kpdk) & keypad->direct_key_mask; |
| 554 | else |
| 555 | new_state = KPDK_DK(kpdk) & keypad->direct_key_mask; |
| 556 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 557 | bits_changed = keypad->direct_key_state ^ new_state; |
| 558 | |
| 559 | if (bits_changed == 0) |
| 560 | return; |
| 561 | |
| 562 | for (i = 0; i < pdata->direct_key_num; i++) { |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 563 | if (bits_changed & (1 << i)) { |
| 564 | int code = MAX_MATRIX_KEY_NUM + i; |
| 565 | |
| 566 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 567 | input_report_key(input_dev, keypad->keycodes[code], |
| 568 | new_state & (1 << i)); |
| 569 | } |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 570 | } |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 571 | input_sync(input_dev); |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 572 | keypad->direct_key_state = new_state; |
| 573 | } |
| 574 | |
Mark F. Brown | 3587c25 | 2010-09-03 18:28:09 -0400 | [diff] [blame] | 575 | static void clear_wakeup_event(struct pxa27x_keypad *keypad) |
| 576 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 577 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Mark F. Brown | 3587c25 | 2010-09-03 18:28:09 -0400 | [diff] [blame] | 578 | |
| 579 | if (pdata->clear_wakeup_event) |
| 580 | (pdata->clear_wakeup_event)(); |
| 581 | } |
| 582 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 583 | static irqreturn_t pxa27x_keypad_irq_handler(int irq, void *dev_id) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 584 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 585 | struct pxa27x_keypad *keypad = dev_id; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 586 | unsigned long kpc = keypad_readl(KPC); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 587 | |
Mark F. Brown | 3587c25 | 2010-09-03 18:28:09 -0400 | [diff] [blame] | 588 | clear_wakeup_event(keypad); |
| 589 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 590 | if (kpc & KPC_DI) |
| 591 | pxa27x_keypad_scan_direct(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 592 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 593 | if (kpc & KPC_MI) |
| 594 | pxa27x_keypad_scan_matrix(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 595 | |
| 596 | return IRQ_HANDLED; |
| 597 | } |
| 598 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 599 | static void pxa27x_keypad_config(struct pxa27x_keypad *keypad) |
| 600 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 601 | const struct pxa27x_keypad_platform_data *pdata = keypad->pdata; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 602 | unsigned int mask = 0, direct_key_num = 0; |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 603 | unsigned long kpc = 0; |
| 604 | |
Vasily Khoruzhick | 904aded | 2012-10-29 23:45:09 -0700 | [diff] [blame] | 605 | /* clear pending interrupt bit */ |
| 606 | keypad_readl(KPC); |
| 607 | |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 608 | /* enable matrix keys with automatic scan */ |
| 609 | if (pdata->matrix_key_rows && pdata->matrix_key_cols) { |
| 610 | kpc |= KPC_ASACT | KPC_MIE | KPC_ME | KPC_MS_ALL; |
| 611 | kpc |= KPC_MKRN(pdata->matrix_key_rows) | |
| 612 | KPC_MKCN(pdata->matrix_key_cols); |
| 613 | } |
| 614 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 615 | /* enable rotary key, debounce interval same as direct keys */ |
| 616 | if (pdata->enable_rotary0) { |
| 617 | mask |= 0x03; |
| 618 | direct_key_num = 2; |
| 619 | kpc |= KPC_REE0; |
| 620 | } |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 621 | |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 622 | if (pdata->enable_rotary1) { |
| 623 | mask |= 0x0c; |
| 624 | direct_key_num = 4; |
| 625 | kpc |= KPC_REE1; |
| 626 | } |
| 627 | |
| 628 | if (pdata->direct_key_num > direct_key_num) |
| 629 | direct_key_num = pdata->direct_key_num; |
| 630 | |
Chao Xie | fb054bf | 2012-04-01 10:08:04 +0800 | [diff] [blame] | 631 | /* |
| 632 | * Direct keys usage may not start from KP_DKIN0, check the platfrom |
| 633 | * mask data to config the specific. |
| 634 | */ |
| 635 | if (pdata->direct_key_mask) |
| 636 | keypad->direct_key_mask = pdata->direct_key_mask; |
| 637 | else |
| 638 | keypad->direct_key_mask = ((1 << direct_key_num) - 1) & ~mask; |
Eric Miao | 62059d9 | 2008-01-31 00:59:03 -0500 | [diff] [blame] | 639 | |
| 640 | /* enable direct key */ |
| 641 | if (direct_key_num) |
| 642 | kpc |= KPC_DE | KPC_DIE | KPC_DKN(direct_key_num); |
| 643 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 644 | keypad_writel(KPC, kpc | KPC_RE_ZERO_DEB); |
| 645 | keypad_writel(KPREC, DEFAULT_KPREC); |
Eric Miao | 76cb44e | 2008-01-31 00:59:23 -0500 | [diff] [blame] | 646 | keypad_writel(KPKDI, pdata->debounce_interval); |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 647 | } |
| 648 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 649 | static int pxa27x_keypad_open(struct input_dev *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 650 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 651 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
| 652 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 653 | /* Enable unit clock */ |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 654 | clk_prepare_enable(keypad->clk); |
Eric Miao | d7416f9 | 2008-01-31 00:58:52 -0500 | [diff] [blame] | 655 | pxa27x_keypad_config(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 656 | |
| 657 | return 0; |
| 658 | } |
| 659 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 660 | static void pxa27x_keypad_close(struct input_dev *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 661 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 662 | struct pxa27x_keypad *keypad = input_get_drvdata(dev); |
| 663 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 664 | /* Disable clock unit */ |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 665 | clk_disable_unprepare(keypad->clk); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Dmitry Torokhov | f9f6def | 2013-06-10 22:10:07 -0700 | [diff] [blame] | 668 | #ifdef CONFIG_PM_SLEEP |
Dmitry Torokhov | b001091 | 2009-07-24 22:01:43 -0700 | [diff] [blame] | 669 | static int pxa27x_keypad_suspend(struct device *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 670 | { |
Dmitry Torokhov | b001091 | 2009-07-24 22:01:43 -0700 | [diff] [blame] | 671 | struct platform_device *pdev = to_platform_device(dev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 672 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 673 | |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 674 | /* |
| 675 | * If the keypad is used a wake up source, clock can not be disabled. |
| 676 | * Or it can not detect the key pressing. |
| 677 | */ |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 678 | if (device_may_wakeup(&pdev->dev)) |
| 679 | enable_irq_wake(keypad->irq); |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 680 | else |
| 681 | clk_disable_unprepare(keypad->clk); |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 682 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 683 | return 0; |
| 684 | } |
| 685 | |
Dmitry Torokhov | b001091 | 2009-07-24 22:01:43 -0700 | [diff] [blame] | 686 | static int pxa27x_keypad_resume(struct device *dev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 687 | { |
Dmitry Torokhov | b001091 | 2009-07-24 22:01:43 -0700 | [diff] [blame] | 688 | struct platform_device *pdev = to_platform_device(dev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 689 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 690 | struct input_dev *input_dev = keypad->input_dev; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 691 | |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 692 | /* |
| 693 | * If the keypad is used as wake up source, the clock is not turned |
| 694 | * off. So do not need configure it again. |
| 695 | */ |
| 696 | if (device_may_wakeup(&pdev->dev)) { |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 697 | disable_irq_wake(keypad->irq); |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 698 | } else { |
| 699 | mutex_lock(&input_dev->mutex); |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 700 | |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 701 | if (input_dev->users) { |
| 702 | /* Enable unit clock */ |
| 703 | clk_prepare_enable(keypad->clk); |
| 704 | pxa27x_keypad_config(keypad); |
| 705 | } |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 706 | |
Chao Xie | 6ce34a5 | 2012-04-01 10:08:01 +0800 | [diff] [blame] | 707 | mutex_unlock(&input_dev->mutex); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 708 | } |
| 709 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 710 | return 0; |
| 711 | } |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 712 | #endif |
| 713 | |
Dmitry Torokhov | f9f6def | 2013-06-10 22:10:07 -0700 | [diff] [blame] | 714 | static SIMPLE_DEV_PM_OPS(pxa27x_keypad_pm_ops, |
| 715 | pxa27x_keypad_suspend, pxa27x_keypad_resume); |
| 716 | |
| 717 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 718 | static int pxa27x_keypad_probe(struct platform_device *pdev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 719 | { |
Dmitry Torokhov | 9eb5213 | 2013-06-10 22:16:29 -0700 | [diff] [blame] | 720 | const struct pxa27x_keypad_platform_data *pdata = |
| 721 | dev_get_platdata(&pdev->dev); |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 722 | struct device_node *np = pdev->dev.of_node; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 723 | struct pxa27x_keypad *keypad; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 724 | struct input_dev *input_dev; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 725 | struct resource *res; |
| 726 | int irq, error; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 727 | |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 728 | /* Driver need build keycode from device tree or pdata */ |
| 729 | if (!np && !pdata) |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 730 | return -EINVAL; |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 731 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 732 | irq = platform_get_irq(pdev, 0); |
| 733 | if (irq < 0) { |
| 734 | dev_err(&pdev->dev, "failed to get keypad irq\n"); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 735 | return -ENXIO; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 739 | if (res == NULL) { |
| 740 | dev_err(&pdev->dev, "failed to get I/O memory\n"); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 741 | return -ENXIO; |
| 742 | } |
| 743 | |
| 744 | keypad = kzalloc(sizeof(struct pxa27x_keypad), GFP_KERNEL); |
| 745 | input_dev = input_allocate_device(); |
| 746 | if (!keypad || !input_dev) { |
| 747 | dev_err(&pdev->dev, "failed to allocate memory\n"); |
| 748 | error = -ENOMEM; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 749 | goto failed_free; |
| 750 | } |
| 751 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 752 | keypad->pdata = pdata; |
| 753 | keypad->input_dev = input_dev; |
| 754 | keypad->irq = irq; |
| 755 | |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 756 | res = request_mem_region(res->start, resource_size(res), pdev->name); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 757 | if (res == NULL) { |
| 758 | dev_err(&pdev->dev, "failed to request I/O memory\n"); |
| 759 | error = -EBUSY; |
| 760 | goto failed_free; |
| 761 | } |
| 762 | |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 763 | keypad->mmio_base = ioremap(res->start, resource_size(res)); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 764 | if (keypad->mmio_base == NULL) { |
| 765 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); |
| 766 | error = -ENXIO; |
| 767 | goto failed_free_mem; |
| 768 | } |
| 769 | |
Russell King | e0d8b13 | 2008-11-11 17:52:32 +0000 | [diff] [blame] | 770 | keypad->clk = clk_get(&pdev->dev, NULL); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 771 | if (IS_ERR(keypad->clk)) { |
| 772 | dev_err(&pdev->dev, "failed to get keypad clock\n"); |
| 773 | error = PTR_ERR(keypad->clk); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 774 | goto failed_free_io; |
Russell King | 22d8a73 | 2007-08-20 10:19:39 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 777 | input_dev->name = pdev->name; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 778 | input_dev->id.bustype = BUS_HOST; |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 779 | input_dev->open = pxa27x_keypad_open; |
| 780 | input_dev->close = pxa27x_keypad_close; |
Dmitry Torokhov | 469ba4d | 2007-04-12 01:34:58 -0400 | [diff] [blame] | 781 | input_dev->dev.parent = &pdev->dev; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 782 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 783 | input_dev->keycode = keypad->keycodes; |
| 784 | input_dev->keycodesize = sizeof(keypad->keycodes[0]); |
| 785 | input_dev->keycodemax = ARRAY_SIZE(keypad->keycodes); |
| 786 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 787 | input_set_drvdata(input_dev, keypad); |
| 788 | |
Antonio Ospite | 471637a | 2008-05-28 14:35:52 -0400 | [diff] [blame] | 789 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 790 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 791 | |
Mike Dunn | 049d75f | 2013-09-18 08:07:41 -0700 | [diff] [blame] | 792 | if (pdata) { |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 793 | error = pxa27x_keypad_build_keycode(keypad); |
Mike Dunn | 049d75f | 2013-09-18 08:07:41 -0700 | [diff] [blame] | 794 | } else { |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 795 | error = pxa27x_keypad_build_keycode_from_dt(keypad); |
Mike Dunn | 049d75f | 2013-09-18 08:07:41 -0700 | [diff] [blame] | 796 | /* |
| 797 | * Data that we get from DT resides in dynamically |
| 798 | * allocated memory so we need to update our pdata |
| 799 | * pointer. |
| 800 | */ |
| 801 | pdata = keypad->pdata; |
| 802 | } |
Chao Xie | 0a085a9 | 2013-05-05 20:24:58 -0700 | [diff] [blame] | 803 | if (error) { |
| 804 | dev_err(&pdev->dev, "failed to build keycode\n"); |
| 805 | goto failed_put_clk; |
| 806 | } |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 807 | |
Chao Xie | c5ec194 | 2014-05-12 19:20:48 -0700 | [diff] [blame] | 808 | keypad->row_shift = get_count_order(pdata->matrix_key_cols); |
| 809 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 810 | if ((pdata->enable_rotary0 && keypad->rotary_rel_code[0] != -1) || |
| 811 | (pdata->enable_rotary1 && keypad->rotary_rel_code[1] != -1)) { |
| 812 | input_dev->evbit[0] |= BIT_MASK(EV_REL); |
| 813 | } |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 814 | |
Yong Zhang | ec4665c | 2011-09-07 14:04:16 -0700 | [diff] [blame] | 815 | error = request_irq(irq, pxa27x_keypad_irq_handler, 0, |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 816 | pdev->name, keypad); |
| 817 | if (error) { |
| 818 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 819 | goto failed_put_clk; |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 820 | } |
| 821 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 822 | /* Register the input device */ |
| 823 | error = input_register_device(input_dev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 824 | if (error) { |
| 825 | dev_err(&pdev->dev, "failed to register input device\n"); |
| 826 | goto failed_free_irq; |
| 827 | } |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 828 | |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 829 | platform_set_drvdata(pdev, keypad); |
Eric Miao | 39ab9dd | 2008-06-02 11:20:55 -0400 | [diff] [blame] | 830 | device_init_wakeup(&pdev->dev, 1); |
| 831 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 832 | return 0; |
| 833 | |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 834 | failed_free_irq: |
Lars-Peter Clausen | 8c7f5f5 | 2013-05-23 09:30:04 -0700 | [diff] [blame] | 835 | free_irq(irq, keypad); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 836 | failed_put_clk: |
| 837 | clk_put(keypad->clk); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 838 | failed_free_io: |
| 839 | iounmap(keypad->mmio_base); |
| 840 | failed_free_mem: |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 841 | release_mem_region(res->start, resource_size(res)); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 842 | failed_free: |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 843 | input_free_device(input_dev); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 844 | kfree(keypad); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 845 | return error; |
| 846 | } |
| 847 | |
Bill Pemberton | e2619cf | 2012-11-23 21:50:47 -0800 | [diff] [blame] | 848 | static int pxa27x_keypad_remove(struct platform_device *pdev) |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 849 | { |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 850 | struct pxa27x_keypad *keypad = platform_get_drvdata(pdev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 851 | struct resource *res; |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 852 | |
Lars-Peter Clausen | 8c7f5f5 | 2013-05-23 09:30:04 -0700 | [diff] [blame] | 853 | free_irq(keypad->irq, keypad); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 854 | clk_put(keypad->clk); |
| 855 | |
| 856 | input_unregister_device(keypad->input_dev); |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 857 | iounmap(keypad->mmio_base); |
| 858 | |
| 859 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Dmitry Torokhov | 52ec775 | 2009-07-22 21:51:40 -0700 | [diff] [blame] | 860 | release_mem_region(res->start, resource_size(res)); |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 861 | |
Eric Miao | 1814db6 | 2008-01-31 00:58:37 -0500 | [diff] [blame] | 862 | kfree(keypad); |
Dmitry Torokhov | bd96f37 | 2009-09-04 23:46:18 -0700 | [diff] [blame] | 863 | |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 864 | return 0; |
| 865 | } |
| 866 | |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 867 | /* work with hotplug and coldplug */ |
| 868 | MODULE_ALIAS("platform:pxa27x-keypad"); |
| 869 | |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 870 | #ifdef CONFIG_OF |
| 871 | static const struct of_device_id pxa27x_keypad_dt_match[] = { |
| 872 | { .compatible = "marvell,pxa27x-keypad" }, |
| 873 | {}, |
| 874 | }; |
| 875 | MODULE_DEVICE_TABLE(of, pxa27x_keypad_dt_match); |
| 876 | #endif |
| 877 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 878 | static struct platform_driver pxa27x_keypad_driver = { |
| 879 | .probe = pxa27x_keypad_probe, |
Bill Pemberton | 1cb0aa8 | 2012-11-23 21:27:39 -0800 | [diff] [blame] | 880 | .remove = pxa27x_keypad_remove, |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 881 | .driver = { |
Eric Miao | 9c60deb | 2008-01-31 00:59:15 -0500 | [diff] [blame] | 882 | .name = "pxa27x-keypad", |
Chao Xie | e415697 | 2013-05-05 20:25:10 -0700 | [diff] [blame] | 883 | .of_match_table = of_match_ptr(pxa27x_keypad_dt_match), |
Kay Sievers | d7b5247 | 2008-04-18 00:24:42 -0400 | [diff] [blame] | 884 | .owner = THIS_MODULE, |
Dmitry Torokhov | b001091 | 2009-07-24 22:01:43 -0700 | [diff] [blame] | 885 | .pm = &pxa27x_keypad_pm_ops, |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 886 | }, |
| 887 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 888 | module_platform_driver(pxa27x_keypad_driver); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 889 | |
Eric Miao | 0e5f11a | 2008-01-31 00:56:46 -0500 | [diff] [blame] | 890 | MODULE_DESCRIPTION("PXA27x Keypad Controller Driver"); |
Rodolfo Giometti | 5a90e5b | 2007-03-16 00:58:52 -0400 | [diff] [blame] | 891 | MODULE_LICENSE("GPL"); |