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