Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the IMX keypad port. |
| 3 | * Copyright (C) 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * <<Power management needs to be implemented>>. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/device.h> |
| 15 | #include <linux/err.h> |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 16 | #include <linux/input/matrix_keypad.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/jiffies.h> |
| 20 | #include <linux/kernel.h> |
| 21 | #include <linux/module.h> |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 22 | #include <linux/of.h> |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 23 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 25 | #include <linux/timer.h> |
| 26 | |
| 27 | /* |
| 28 | * Keypad Controller registers (halfword) |
| 29 | */ |
| 30 | #define KPCR 0x00 /* Keypad Control Register */ |
| 31 | |
| 32 | #define KPSR 0x02 /* Keypad Status Register */ |
| 33 | #define KBD_STAT_KPKD (0x1 << 0) /* Key Press Interrupt Status bit (w1c) */ |
| 34 | #define KBD_STAT_KPKR (0x1 << 1) /* Key Release Interrupt Status bit (w1c) */ |
| 35 | #define KBD_STAT_KDSC (0x1 << 2) /* Key Depress Synch Chain Status bit (w1c)*/ |
| 36 | #define KBD_STAT_KRSS (0x1 << 3) /* Key Release Synch Status bit (w1c)*/ |
| 37 | #define KBD_STAT_KDIE (0x1 << 8) /* Key Depress Interrupt Enable Status bit */ |
| 38 | #define KBD_STAT_KRIE (0x1 << 9) /* Key Release Interrupt Enable */ |
| 39 | #define KBD_STAT_KPPEN (0x1 << 10) /* Keypad Clock Enable */ |
| 40 | |
| 41 | #define KDDR 0x04 /* Keypad Data Direction Register */ |
| 42 | #define KPDR 0x06 /* Keypad Data Register */ |
| 43 | |
| 44 | #define MAX_MATRIX_KEY_ROWS 8 |
| 45 | #define MAX_MATRIX_KEY_COLS 8 |
| 46 | #define MATRIX_ROW_SHIFT 3 |
| 47 | |
| 48 | #define MAX_MATRIX_KEY_NUM (MAX_MATRIX_KEY_ROWS * MAX_MATRIX_KEY_COLS) |
| 49 | |
| 50 | struct imx_keypad { |
| 51 | |
| 52 | struct clk *clk; |
| 53 | struct input_dev *input_dev; |
| 54 | void __iomem *mmio_base; |
| 55 | |
| 56 | int irq; |
| 57 | struct timer_list check_matrix_timer; |
| 58 | |
| 59 | /* |
| 60 | * The matrix is stable only if no changes are detected after |
| 61 | * IMX_KEYPAD_SCANS_FOR_STABILITY scans |
| 62 | */ |
| 63 | #define IMX_KEYPAD_SCANS_FOR_STABILITY 3 |
| 64 | int stable_count; |
| 65 | |
| 66 | bool enabled; |
| 67 | |
| 68 | /* Masks for enabled rows/cols */ |
| 69 | unsigned short rows_en_mask; |
| 70 | unsigned short cols_en_mask; |
| 71 | |
| 72 | unsigned short keycodes[MAX_MATRIX_KEY_NUM]; |
| 73 | |
| 74 | /* |
| 75 | * Matrix states: |
| 76 | * -stable: achieved after a complete debounce process. |
| 77 | * -unstable: used in the debouncing process. |
| 78 | */ |
| 79 | unsigned short matrix_stable_state[MAX_MATRIX_KEY_COLS]; |
| 80 | unsigned short matrix_unstable_state[MAX_MATRIX_KEY_COLS]; |
| 81 | }; |
| 82 | |
| 83 | /* Scan the matrix and return the new state in *matrix_volatile_state. */ |
| 84 | static void imx_keypad_scan_matrix(struct imx_keypad *keypad, |
| 85 | unsigned short *matrix_volatile_state) |
| 86 | { |
| 87 | int col; |
| 88 | unsigned short reg_val; |
| 89 | |
| 90 | for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) { |
| 91 | if ((keypad->cols_en_mask & (1 << col)) == 0) |
| 92 | continue; |
| 93 | /* |
| 94 | * Discharge keypad capacitance: |
| 95 | * 2. write 1s on column data. |
| 96 | * 3. configure columns as totem-pole to discharge capacitance. |
| 97 | * 4. configure columns as open-drain. |
| 98 | */ |
| 99 | reg_val = readw(keypad->mmio_base + KPDR); |
| 100 | reg_val |= 0xff00; |
| 101 | writew(reg_val, keypad->mmio_base + KPDR); |
| 102 | |
| 103 | reg_val = readw(keypad->mmio_base + KPCR); |
| 104 | reg_val &= ~((keypad->cols_en_mask & 0xff) << 8); |
| 105 | writew(reg_val, keypad->mmio_base + KPCR); |
| 106 | |
| 107 | udelay(2); |
| 108 | |
| 109 | reg_val = readw(keypad->mmio_base + KPCR); |
| 110 | reg_val |= (keypad->cols_en_mask & 0xff) << 8; |
| 111 | writew(reg_val, keypad->mmio_base + KPCR); |
| 112 | |
| 113 | /* |
| 114 | * 5. Write a single column to 0, others to 1. |
| 115 | * 6. Sample row inputs and save data. |
| 116 | * 7. Repeat steps 2 - 6 for remaining columns. |
| 117 | */ |
| 118 | reg_val = readw(keypad->mmio_base + KPDR); |
| 119 | reg_val &= ~(1 << (8 + col)); |
| 120 | writew(reg_val, keypad->mmio_base + KPDR); |
| 121 | |
| 122 | /* |
| 123 | * Delay added to avoid propagating the 0 from column to row |
| 124 | * when scanning. |
| 125 | */ |
| 126 | udelay(5); |
| 127 | |
| 128 | /* |
| 129 | * 1s in matrix_volatile_state[col] means key pressures |
| 130 | * throw data from non enabled rows. |
| 131 | */ |
| 132 | reg_val = readw(keypad->mmio_base + KPDR); |
| 133 | matrix_volatile_state[col] = (~reg_val) & keypad->rows_en_mask; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Return in standby mode: |
| 138 | * 9. write 0s to columns |
| 139 | */ |
| 140 | reg_val = readw(keypad->mmio_base + KPDR); |
| 141 | reg_val &= 0x00ff; |
| 142 | writew(reg_val, keypad->mmio_base + KPDR); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Compare the new matrix state (volatile) with the stable one stored in |
| 147 | * keypad->matrix_stable_state and fire events if changes are detected. |
| 148 | */ |
| 149 | static void imx_keypad_fire_events(struct imx_keypad *keypad, |
| 150 | unsigned short *matrix_volatile_state) |
| 151 | { |
| 152 | struct input_dev *input_dev = keypad->input_dev; |
| 153 | int row, col; |
| 154 | |
| 155 | for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) { |
| 156 | unsigned short bits_changed; |
| 157 | int code; |
| 158 | |
| 159 | if ((keypad->cols_en_mask & (1 << col)) == 0) |
| 160 | continue; /* Column is not enabled */ |
| 161 | |
| 162 | bits_changed = keypad->matrix_stable_state[col] ^ |
| 163 | matrix_volatile_state[col]; |
| 164 | |
| 165 | if (bits_changed == 0) |
| 166 | continue; /* Column does not contain changes */ |
| 167 | |
| 168 | for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) { |
| 169 | if ((keypad->rows_en_mask & (1 << row)) == 0) |
| 170 | continue; /* Row is not enabled */ |
| 171 | if ((bits_changed & (1 << row)) == 0) |
| 172 | continue; /* Row does not contain changes */ |
| 173 | |
| 174 | code = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT); |
| 175 | input_event(input_dev, EV_MSC, MSC_SCAN, code); |
| 176 | input_report_key(input_dev, keypad->keycodes[code], |
| 177 | matrix_volatile_state[col] & (1 << row)); |
| 178 | dev_dbg(&input_dev->dev, "Event code: %d, val: %d", |
| 179 | keypad->keycodes[code], |
| 180 | matrix_volatile_state[col] & (1 << row)); |
| 181 | } |
| 182 | } |
| 183 | input_sync(input_dev); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * imx_keypad_check_for_events is the timer handler. |
| 188 | */ |
| 189 | static void imx_keypad_check_for_events(unsigned long data) |
| 190 | { |
| 191 | struct imx_keypad *keypad = (struct imx_keypad *) data; |
| 192 | unsigned short matrix_volatile_state[MAX_MATRIX_KEY_COLS]; |
| 193 | unsigned short reg_val; |
| 194 | bool state_changed, is_zero_matrix; |
| 195 | int i; |
| 196 | |
| 197 | memset(matrix_volatile_state, 0, sizeof(matrix_volatile_state)); |
| 198 | |
| 199 | imx_keypad_scan_matrix(keypad, matrix_volatile_state); |
| 200 | |
| 201 | state_changed = false; |
| 202 | for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) { |
| 203 | if ((keypad->cols_en_mask & (1 << i)) == 0) |
| 204 | continue; |
| 205 | |
| 206 | if (keypad->matrix_unstable_state[i] ^ matrix_volatile_state[i]) { |
| 207 | state_changed = true; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * If the matrix state is changed from the previous scan |
| 214 | * (Re)Begin the debouncing process, saving the new state in |
| 215 | * keypad->matrix_unstable_state. |
| 216 | * else |
| 217 | * Increase the count of number of scans with a stable state. |
| 218 | */ |
| 219 | if (state_changed) { |
| 220 | memcpy(keypad->matrix_unstable_state, matrix_volatile_state, |
| 221 | sizeof(matrix_volatile_state)); |
| 222 | keypad->stable_count = 0; |
| 223 | } else |
| 224 | keypad->stable_count++; |
| 225 | |
| 226 | /* |
| 227 | * If the matrix is not as stable as we want reschedule scan |
| 228 | * in the near future. |
| 229 | */ |
| 230 | if (keypad->stable_count < IMX_KEYPAD_SCANS_FOR_STABILITY) { |
| 231 | mod_timer(&keypad->check_matrix_timer, |
| 232 | jiffies + msecs_to_jiffies(10)); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * If the matrix state is stable, fire the events and save the new |
| 238 | * stable state. Note, if the matrix is kept stable for longer |
| 239 | * (keypad->stable_count > IMX_KEYPAD_SCANS_FOR_STABILITY) all |
| 240 | * events have already been generated. |
| 241 | */ |
| 242 | if (keypad->stable_count == IMX_KEYPAD_SCANS_FOR_STABILITY) { |
| 243 | imx_keypad_fire_events(keypad, matrix_volatile_state); |
| 244 | |
| 245 | memcpy(keypad->matrix_stable_state, matrix_volatile_state, |
| 246 | sizeof(matrix_volatile_state)); |
| 247 | } |
| 248 | |
| 249 | is_zero_matrix = true; |
| 250 | for (i = 0; i < MAX_MATRIX_KEY_COLS; i++) { |
| 251 | if (matrix_volatile_state[i] != 0) { |
| 252 | is_zero_matrix = false; |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | if (is_zero_matrix) { |
| 259 | /* |
| 260 | * All keys have been released. Enable only the KDI |
| 261 | * interrupt for future key presses (clear the KDI |
| 262 | * status bit and its sync chain before that). |
| 263 | */ |
| 264 | reg_val = readw(keypad->mmio_base + KPSR); |
| 265 | reg_val |= KBD_STAT_KPKD | KBD_STAT_KDSC; |
| 266 | writew(reg_val, keypad->mmio_base + KPSR); |
| 267 | |
| 268 | reg_val = readw(keypad->mmio_base + KPSR); |
| 269 | reg_val |= KBD_STAT_KDIE; |
| 270 | reg_val &= ~KBD_STAT_KRIE; |
| 271 | writew(reg_val, keypad->mmio_base + KPSR); |
| 272 | } else { |
| 273 | /* |
| 274 | * Some keys are still pressed. Schedule a rescan in |
| 275 | * attempt to detect multiple key presses and enable |
| 276 | * the KRI interrupt to react quickly to key release |
| 277 | * event. |
| 278 | */ |
| 279 | mod_timer(&keypad->check_matrix_timer, |
| 280 | jiffies + msecs_to_jiffies(60)); |
| 281 | |
| 282 | reg_val = readw(keypad->mmio_base + KPSR); |
| 283 | reg_val |= KBD_STAT_KPKR | KBD_STAT_KRSS; |
| 284 | writew(reg_val, keypad->mmio_base + KPSR); |
| 285 | |
| 286 | reg_val = readw(keypad->mmio_base + KPSR); |
| 287 | reg_val |= KBD_STAT_KRIE; |
| 288 | reg_val &= ~KBD_STAT_KDIE; |
| 289 | writew(reg_val, keypad->mmio_base + KPSR); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static irqreturn_t imx_keypad_irq_handler(int irq, void *dev_id) |
| 294 | { |
| 295 | struct imx_keypad *keypad = dev_id; |
| 296 | unsigned short reg_val; |
| 297 | |
| 298 | reg_val = readw(keypad->mmio_base + KPSR); |
| 299 | |
| 300 | /* Disable both interrupt types */ |
| 301 | reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE); |
| 302 | /* Clear interrupts status bits */ |
| 303 | reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD; |
| 304 | writew(reg_val, keypad->mmio_base + KPSR); |
| 305 | |
| 306 | if (keypad->enabled) { |
| 307 | /* The matrix is supposed to be changed */ |
| 308 | keypad->stable_count = 0; |
| 309 | |
| 310 | /* Schedule the scanning procedure near in the future */ |
| 311 | mod_timer(&keypad->check_matrix_timer, |
| 312 | jiffies + msecs_to_jiffies(2)); |
| 313 | } |
| 314 | |
| 315 | return IRQ_HANDLED; |
| 316 | } |
| 317 | |
| 318 | static void imx_keypad_config(struct imx_keypad *keypad) |
| 319 | { |
| 320 | unsigned short reg_val; |
| 321 | |
| 322 | /* |
| 323 | * Include enabled rows in interrupt generation (KPCR[7:0]) |
| 324 | * Configure keypad columns as open-drain (KPCR[15:8]) |
| 325 | */ |
| 326 | reg_val = readw(keypad->mmio_base + KPCR); |
| 327 | reg_val |= keypad->rows_en_mask & 0xff; /* rows */ |
| 328 | reg_val |= (keypad->cols_en_mask & 0xff) << 8; /* cols */ |
| 329 | writew(reg_val, keypad->mmio_base + KPCR); |
| 330 | |
| 331 | /* Write 0's to KPDR[15:8] (Colums) */ |
| 332 | reg_val = readw(keypad->mmio_base + KPDR); |
| 333 | reg_val &= 0x00ff; |
| 334 | writew(reg_val, keypad->mmio_base + KPDR); |
| 335 | |
| 336 | /* Configure columns as output, rows as input (KDDR[15:0]) */ |
| 337 | writew(0xff00, keypad->mmio_base + KDDR); |
| 338 | |
| 339 | /* |
| 340 | * Clear Key Depress and Key Release status bit. |
| 341 | * Clear both synchronizer chain. |
| 342 | */ |
| 343 | reg_val = readw(keypad->mmio_base + KPSR); |
| 344 | reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD | |
| 345 | KBD_STAT_KDSC | KBD_STAT_KRSS; |
| 346 | writew(reg_val, keypad->mmio_base + KPSR); |
| 347 | |
| 348 | /* Enable KDI and disable KRI (avoid false release events). */ |
| 349 | reg_val |= KBD_STAT_KDIE; |
| 350 | reg_val &= ~KBD_STAT_KRIE; |
| 351 | writew(reg_val, keypad->mmio_base + KPSR); |
| 352 | } |
| 353 | |
| 354 | static void imx_keypad_inhibit(struct imx_keypad *keypad) |
| 355 | { |
| 356 | unsigned short reg_val; |
| 357 | |
| 358 | /* Inhibit KDI and KRI interrupts. */ |
| 359 | reg_val = readw(keypad->mmio_base + KPSR); |
| 360 | reg_val &= ~(KBD_STAT_KRIE | KBD_STAT_KDIE); |
Michael Grzeschik | f35dd69 | 2012-08-21 21:57:15 -0700 | [diff] [blame] | 361 | reg_val |= KBD_STAT_KPKR | KBD_STAT_KPKD; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 362 | writew(reg_val, keypad->mmio_base + KPSR); |
| 363 | |
| 364 | /* Colums as open drain and disable all rows */ |
Andreas Pretzsch | 92aab96 | 2012-11-24 23:31:38 -0800 | [diff] [blame] | 365 | reg_val = (keypad->cols_en_mask & 0xff) << 8; |
| 366 | writew(reg_val, keypad->mmio_base + KPCR); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | static void imx_keypad_close(struct input_dev *dev) |
| 370 | { |
| 371 | struct imx_keypad *keypad = input_get_drvdata(dev); |
| 372 | |
| 373 | dev_dbg(&dev->dev, ">%s\n", __func__); |
| 374 | |
| 375 | /* Mark keypad as being inactive */ |
| 376 | keypad->enabled = false; |
| 377 | synchronize_irq(keypad->irq); |
| 378 | del_timer_sync(&keypad->check_matrix_timer); |
| 379 | |
| 380 | imx_keypad_inhibit(keypad); |
| 381 | |
| 382 | /* Disable clock unit */ |
Fabio Estevam | a1e636e | 2012-07-06 11:26:05 -0700 | [diff] [blame] | 383 | clk_disable_unprepare(keypad->clk); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | static int imx_keypad_open(struct input_dev *dev) |
| 387 | { |
| 388 | struct imx_keypad *keypad = input_get_drvdata(dev); |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 389 | int error; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 390 | |
| 391 | dev_dbg(&dev->dev, ">%s\n", __func__); |
| 392 | |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 393 | /* Enable the kpp clock */ |
| 394 | error = clk_prepare_enable(keypad->clk); |
| 395 | if (error) |
| 396 | return error; |
| 397 | |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 398 | /* We became active from now */ |
| 399 | keypad->enabled = true; |
| 400 | |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 401 | imx_keypad_config(keypad); |
| 402 | |
| 403 | /* Sanity control, not all the rows must be actived now. */ |
| 404 | if ((readw(keypad->mmio_base + KPDR) & keypad->rows_en_mask) == 0) { |
| 405 | dev_err(&dev->dev, |
| 406 | "too many keys pressed, control pins initialisation\n"); |
| 407 | goto open_err; |
| 408 | } |
| 409 | |
| 410 | return 0; |
| 411 | |
| 412 | open_err: |
| 413 | imx_keypad_close(dev); |
| 414 | return -EIO; |
| 415 | } |
| 416 | |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 417 | #ifdef CONFIG_OF |
Jingoo Han | 27ec39c | 2014-05-07 12:59:47 -0700 | [diff] [blame] | 418 | static const struct of_device_id imx_keypad_of_match[] = { |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 419 | { .compatible = "fsl,imx21-kpp", }, |
| 420 | { /* sentinel */ } |
| 421 | }; |
| 422 | MODULE_DEVICE_TABLE(of, imx_keypad_of_match); |
| 423 | #endif |
| 424 | |
Bill Pemberton | 5298cc4 | 2012-11-23 21:38:25 -0800 | [diff] [blame] | 425 | static int imx_keypad_probe(struct platform_device *pdev) |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 426 | { |
Jingoo Han | c838cb3 | 2013-12-05 19:21:10 -0800 | [diff] [blame] | 427 | const struct matrix_keymap_data *keymap_data = |
| 428 | dev_get_platdata(&pdev->dev); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 429 | struct imx_keypad *keypad; |
| 430 | struct input_dev *input_dev; |
| 431 | struct resource *res; |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 432 | int irq, error, i, row, col; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 433 | |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 434 | if (!keymap_data && !pdev->dev.of_node) { |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 435 | dev_err(&pdev->dev, "no keymap defined\n"); |
| 436 | return -EINVAL; |
| 437 | } |
| 438 | |
| 439 | irq = platform_get_irq(pdev, 0); |
| 440 | if (irq < 0) { |
| 441 | dev_err(&pdev->dev, "no irq defined in platform data\n"); |
Fabio Estevam | 7fb45ed | 2014-02-17 11:31:36 -0800 | [diff] [blame] | 442 | return irq; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 443 | } |
| 444 | |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 445 | input_dev = devm_input_allocate_device(&pdev->dev); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 446 | if (!input_dev) { |
| 447 | dev_err(&pdev->dev, "failed to allocate the input device\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 448 | return -ENOMEM; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 449 | } |
| 450 | |
Fabio Estevam | 061a5ad | 2015-01-04 14:28:46 -0800 | [diff] [blame] | 451 | keypad = devm_kzalloc(&pdev->dev, sizeof(*keypad), GFP_KERNEL); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 452 | if (!keypad) { |
| 453 | dev_err(&pdev->dev, "not enough memory for driver data\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 454 | return -ENOMEM; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | keypad->input_dev = input_dev; |
| 458 | keypad->irq = irq; |
| 459 | keypad->stable_count = 0; |
| 460 | |
| 461 | setup_timer(&keypad->check_matrix_timer, |
| 462 | imx_keypad_check_for_events, (unsigned long) keypad); |
| 463 | |
Julia Lawall | ef0aca7 | 2013-08-15 00:08:17 -0700 | [diff] [blame] | 464 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 465 | keypad->mmio_base = devm_ioremap_resource(&pdev->dev, res); |
| 466 | if (IS_ERR(keypad->mmio_base)) |
| 467 | return PTR_ERR(keypad->mmio_base); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 468 | |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 469 | keypad->clk = devm_clk_get(&pdev->dev, NULL); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 470 | if (IS_ERR(keypad->clk)) { |
| 471 | dev_err(&pdev->dev, "failed to get keypad clock\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 472 | return PTR_ERR(keypad->clk); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 475 | /* Init the Input device */ |
| 476 | input_dev->name = pdev->name; |
| 477 | input_dev->id.bustype = BUS_HOST; |
| 478 | input_dev->dev.parent = &pdev->dev; |
| 479 | input_dev->open = imx_keypad_open; |
| 480 | input_dev->close = imx_keypad_close; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 481 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 482 | error = matrix_keypad_build_keymap(keymap_data, NULL, |
| 483 | MAX_MATRIX_KEY_ROWS, |
| 484 | MAX_MATRIX_KEY_COLS, |
| 485 | keypad->keycodes, input_dev); |
| 486 | if (error) { |
| 487 | dev_err(&pdev->dev, "failed to build keymap\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 488 | return error; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 489 | } |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 490 | |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 491 | /* Search for rows and cols enabled */ |
| 492 | for (row = 0; row < MAX_MATRIX_KEY_ROWS; row++) { |
| 493 | for (col = 0; col < MAX_MATRIX_KEY_COLS; col++) { |
| 494 | i = MATRIX_SCAN_CODE(row, col, MATRIX_ROW_SHIFT); |
| 495 | if (keypad->keycodes[i] != KEY_RESERVED) { |
| 496 | keypad->rows_en_mask |= 1 << row; |
| 497 | keypad->cols_en_mask |= 1 << col; |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | dev_dbg(&pdev->dev, "enabled rows mask: %x\n", keypad->rows_en_mask); |
| 502 | dev_dbg(&pdev->dev, "enabled cols mask: %x\n", keypad->cols_en_mask); |
| 503 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 504 | __set_bit(EV_REP, input_dev->evbit); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 505 | input_set_capability(input_dev, EV_MSC, MSC_SCAN); |
| 506 | input_set_drvdata(input_dev, keypad); |
| 507 | |
| 508 | /* Ensure that the keypad will stay dormant until opened */ |
Fabio Estevam | 609455f | 2012-09-04 20:27:38 -0700 | [diff] [blame] | 509 | clk_prepare_enable(keypad->clk); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 510 | imx_keypad_inhibit(keypad); |
Fabio Estevam | 609455f | 2012-09-04 20:27:38 -0700 | [diff] [blame] | 511 | clk_disable_unprepare(keypad->clk); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 512 | |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 513 | error = devm_request_irq(&pdev->dev, irq, imx_keypad_irq_handler, 0, |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 514 | pdev->name, keypad); |
| 515 | if (error) { |
| 516 | dev_err(&pdev->dev, "failed to request IRQ\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 517 | return error; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | /* Register the input device */ |
| 521 | error = input_register_device(input_dev); |
| 522 | if (error) { |
| 523 | dev_err(&pdev->dev, "failed to register input device\n"); |
Fabio Estevam | da5bce1 | 2013-03-31 00:38:21 -0700 | [diff] [blame] | 524 | return error; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | platform_set_drvdata(pdev, keypad); |
| 528 | device_init_wakeup(&pdev->dev, 1); |
| 529 | |
| 530 | return 0; |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 531 | } |
| 532 | |
Fabio Estevam | e179d5f | 2014-07-31 11:57:08 -0700 | [diff] [blame] | 533 | static int __maybe_unused imx_kbd_suspend(struct device *dev) |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 534 | { |
| 535 | struct platform_device *pdev = to_platform_device(dev); |
| 536 | struct imx_keypad *kbd = platform_get_drvdata(pdev); |
| 537 | struct input_dev *input_dev = kbd->input_dev; |
| 538 | |
| 539 | /* imx kbd can wake up system even clock is disabled */ |
| 540 | mutex_lock(&input_dev->mutex); |
| 541 | |
| 542 | if (input_dev->users) |
Fabio Estevam | a1e636e | 2012-07-06 11:26:05 -0700 | [diff] [blame] | 543 | clk_disable_unprepare(kbd->clk); |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 544 | |
| 545 | mutex_unlock(&input_dev->mutex); |
| 546 | |
| 547 | if (device_may_wakeup(&pdev->dev)) |
| 548 | enable_irq_wake(kbd->irq); |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | |
Fabio Estevam | e179d5f | 2014-07-31 11:57:08 -0700 | [diff] [blame] | 553 | static int __maybe_unused imx_kbd_resume(struct device *dev) |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 554 | { |
| 555 | struct platform_device *pdev = to_platform_device(dev); |
| 556 | struct imx_keypad *kbd = platform_get_drvdata(pdev); |
| 557 | struct input_dev *input_dev = kbd->input_dev; |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 558 | int ret = 0; |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 559 | |
| 560 | if (device_may_wakeup(&pdev->dev)) |
| 561 | disable_irq_wake(kbd->irq); |
| 562 | |
| 563 | mutex_lock(&input_dev->mutex); |
| 564 | |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 565 | if (input_dev->users) { |
| 566 | ret = clk_prepare_enable(kbd->clk); |
| 567 | if (ret) |
| 568 | goto err_clk; |
| 569 | } |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 570 | |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 571 | err_clk: |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 572 | mutex_unlock(&input_dev->mutex); |
| 573 | |
Fabio Estevam | 333fbe8 | 2012-07-06 11:31:14 -0700 | [diff] [blame] | 574 | return ret; |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 575 | } |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 576 | |
| 577 | static SIMPLE_DEV_PM_OPS(imx_kbd_pm_ops, imx_kbd_suspend, imx_kbd_resume); |
| 578 | |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 579 | static struct platform_driver imx_keypad_driver = { |
| 580 | .driver = { |
| 581 | .name = "imx-keypad", |
Hui Wang | 81e8f2b | 2011-10-12 21:11:16 -0700 | [diff] [blame] | 582 | .pm = &imx_kbd_pm_ops, |
Liu Ying | 0e14235 | 2013-01-03 12:25:46 -0800 | [diff] [blame] | 583 | .of_match_table = of_match_ptr(imx_keypad_of_match), |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 584 | }, |
| 585 | .probe = imx_keypad_probe, |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 586 | }; |
JJ Ding | 5146c84 | 2011-11-29 11:08:39 -0800 | [diff] [blame] | 587 | module_platform_driver(imx_keypad_driver); |
Alberto Panizzo | 76cdc08 | 2010-01-31 17:52:07 -0800 | [diff] [blame] | 588 | |
| 589 | MODULE_AUTHOR("Alberto Panizzo <maramaopercheseimorto@gmail.com>"); |
| 590 | MODULE_DESCRIPTION("IMX Keypad Port Driver"); |
| 591 | MODULE_LICENSE("GPL v2"); |
| 592 | MODULE_ALIAS("platform:imx-keypad"); |