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