Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) ST-Ericsson SA 2010 |
| 3 | * |
| 4 | * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson |
| 5 | * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson |
| 6 | * |
| 7 | * License terms:GNU General Public License (GPL) version 2 |
| 8 | * |
| 9 | * Keypad controller driver for the SKE (Scroll Key Encoder) module used in |
| 10 | * the Nomadik 8815 and Ux500 platforms. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/platform_device.h> |
| 14 | #include <linux/interrupt.h> |
| 15 | #include <linux/spinlock.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/input.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/clk.h> |
Paul Gortmaker | d2d8442 | 2011-07-03 13:53:48 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 22 | |
| 23 | #include <plat/ske.h> |
| 24 | |
| 25 | /* SKE_CR bits */ |
| 26 | #define SKE_KPMLT (0x1 << 6) |
| 27 | #define SKE_KPCN (0x7 << 3) |
| 28 | #define SKE_KPASEN (0x1 << 2) |
| 29 | #define SKE_KPASON (0x1 << 7) |
| 30 | |
| 31 | /* SKE_IMSC bits */ |
| 32 | #define SKE_KPIMA (0x1 << 2) |
| 33 | |
| 34 | /* SKE_ICR bits */ |
| 35 | #define SKE_KPICS (0x1 << 3) |
| 36 | #define SKE_KPICA (0x1 << 2) |
| 37 | |
| 38 | /* SKE_RIS bits */ |
| 39 | #define SKE_KPRISA (0x1 << 2) |
| 40 | |
| 41 | #define SKE_KEYPAD_ROW_SHIFT 3 |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 42 | #define SKE_KPD_NUM_ROWS 8 |
| 43 | #define SKE_KPD_NUM_COLS 8 |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 44 | |
| 45 | /* keypad auto scan registers */ |
| 46 | #define SKE_ASR0 0x20 |
| 47 | #define SKE_ASR1 0x24 |
| 48 | #define SKE_ASR2 0x28 |
| 49 | #define SKE_ASR3 0x2C |
| 50 | |
| 51 | #define SKE_NUM_ASRX_REGISTERS (4) |
| 52 | |
| 53 | /** |
| 54 | * struct ske_keypad - data structure used by keypad driver |
| 55 | * @irq: irq no |
| 56 | * @reg_base: ske regsiters base address |
| 57 | * @input: pointer to input device object |
| 58 | * @board: keypad platform device |
| 59 | * @keymap: matrix scan code table for keycodes |
| 60 | * @clk: clock structure pointer |
| 61 | */ |
| 62 | struct ske_keypad { |
| 63 | int irq; |
| 64 | void __iomem *reg_base; |
| 65 | struct input_dev *input; |
| 66 | const struct ske_keypad_platform_data *board; |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 67 | unsigned short keymap[SKE_KPD_NUM_ROWS * SKE_KPD_NUM_COLS]; |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 68 | struct clk *clk; |
| 69 | spinlock_t ske_keypad_lock; |
| 70 | }; |
| 71 | |
| 72 | static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr, |
| 73 | u8 mask, u8 data) |
| 74 | { |
| 75 | u32 ret; |
| 76 | |
| 77 | spin_lock(&keypad->ske_keypad_lock); |
| 78 | |
| 79 | ret = readl(keypad->reg_base + addr); |
| 80 | ret &= ~mask; |
| 81 | ret |= data; |
| 82 | writel(ret, keypad->reg_base + addr); |
| 83 | |
| 84 | spin_unlock(&keypad->ske_keypad_lock); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * ske_keypad_chip_init: init keypad controller configuration |
| 89 | * |
| 90 | * Enable Multi key press detection, auto scan mode |
| 91 | */ |
Dmitry Torokhov | db3dbd0 | 2012-01-22 23:27:29 -0800 | [diff] [blame] | 92 | static int __init ske_keypad_chip_init(struct ske_keypad *keypad) |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 93 | { |
| 94 | u32 value; |
| 95 | int timeout = 50; |
| 96 | |
| 97 | /* check SKE_RIS to be 0 */ |
| 98 | while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--) |
| 99 | cpu_relax(); |
| 100 | |
| 101 | if (!timeout) |
| 102 | return -EINVAL; |
| 103 | |
| 104 | /* |
| 105 | * set debounce value |
| 106 | * keypad dbounce is configured in DBCR[15:8] |
| 107 | * dbounce value in steps of 32/32.768 ms |
| 108 | */ |
| 109 | spin_lock(&keypad->ske_keypad_lock); |
| 110 | value = readl(keypad->reg_base + SKE_DBCR); |
| 111 | value = value & 0xff; |
| 112 | value |= ((keypad->board->debounce_ms * 32000)/32768) << 8; |
| 113 | writel(value, keypad->reg_base + SKE_DBCR); |
| 114 | spin_unlock(&keypad->ske_keypad_lock); |
| 115 | |
| 116 | /* enable multi key detection */ |
| 117 | ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT); |
| 118 | |
| 119 | /* |
| 120 | * set up the number of columns |
| 121 | * KPCN[5:3] defines no. of keypad columns to be auto scanned |
| 122 | */ |
| 123 | value = (keypad->board->kcol - 1) << 3; |
| 124 | ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value); |
| 125 | |
| 126 | /* clear keypad interrupt for auto(and pending SW) scans */ |
| 127 | ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS); |
| 128 | |
| 129 | /* un-mask keypad interrupts */ |
| 130 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); |
| 131 | |
| 132 | /* enable automatic scan */ |
| 133 | ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 138 | static void ske_keypad_report(struct ske_keypad *keypad, u8 status, int col) |
| 139 | { |
| 140 | int row = 0, code, pos; |
| 141 | struct input_dev *input = keypad->input; |
| 142 | u32 ske_ris; |
| 143 | int key_pressed; |
| 144 | int num_of_rows; |
| 145 | |
| 146 | /* find out the row */ |
| 147 | num_of_rows = hweight8(status); |
| 148 | do { |
| 149 | pos = __ffs(status); |
| 150 | row = pos; |
| 151 | status &= ~(1 << pos); |
| 152 | |
| 153 | code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT); |
| 154 | ske_ris = readl(keypad->reg_base + SKE_RIS); |
| 155 | key_pressed = ske_ris & SKE_KPRISA; |
| 156 | |
| 157 | input_event(input, EV_MSC, MSC_SCAN, code); |
| 158 | input_report_key(input, keypad->keymap[code], key_pressed); |
| 159 | input_sync(input); |
| 160 | num_of_rows--; |
| 161 | } while (num_of_rows); |
| 162 | } |
| 163 | |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 164 | static void ske_keypad_read_data(struct ske_keypad *keypad) |
| 165 | { |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 166 | u8 status; |
| 167 | int col = 0; |
| 168 | int ske_asr, i; |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 169 | |
| 170 | /* |
| 171 | * Read the auto scan registers |
| 172 | * |
| 173 | * Each SKE_ASRx (x=0 to x=3) contains two row values. |
| 174 | * lower byte contains row value for column 2*x, |
| 175 | * upper byte contains row value for column 2*x + 1 |
| 176 | */ |
| 177 | for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) { |
| 178 | ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i)); |
| 179 | if (!ske_asr) |
| 180 | continue; |
| 181 | |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 182 | /* now that ASRx is zero, find out the coloumn x and row y */ |
| 183 | status = ske_asr & 0xff; |
| 184 | if (status) { |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 185 | col = i * 2; |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 186 | ske_keypad_report(keypad, status, col); |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 187 | } |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 188 | status = (ske_asr & 0xff00) >> 8; |
| 189 | if (status) { |
| 190 | col = (i * 2) + 1; |
| 191 | ske_keypad_report(keypad, status, col); |
| 192 | } |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | static irqreturn_t ske_keypad_irq(int irq, void *dev_id) |
| 197 | { |
| 198 | struct ske_keypad *keypad = dev_id; |
| 199 | int retries = 20; |
| 200 | |
| 201 | /* disable auto scan interrupt; mask the interrupt generated */ |
| 202 | ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0); |
| 203 | ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA); |
| 204 | |
| 205 | while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --retries) |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 206 | cpu_relax(); |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 207 | |
Naveen Kumar Gaddipati | af77c88 | 2012-06-25 00:25:41 -0700 | [diff] [blame^] | 208 | /* SKEx registers are stable and can be read */ |
| 209 | ske_keypad_read_data(keypad); |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 210 | |
| 211 | /* enable auto scan interrupts */ |
| 212 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); |
| 213 | |
| 214 | return IRQ_HANDLED; |
| 215 | } |
| 216 | |
Dmitry Torokhov | db3dbd0 | 2012-01-22 23:27:29 -0800 | [diff] [blame] | 217 | static int __init ske_keypad_probe(struct platform_device *pdev) |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 218 | { |
| 219 | const struct ske_keypad_platform_data *plat = pdev->dev.platform_data; |
| 220 | struct ske_keypad *keypad; |
| 221 | struct input_dev *input; |
| 222 | struct resource *res; |
| 223 | int irq; |
| 224 | int error; |
| 225 | |
| 226 | if (!plat) { |
| 227 | dev_err(&pdev->dev, "invalid keypad platform data\n"); |
| 228 | return -EINVAL; |
| 229 | } |
| 230 | |
| 231 | irq = platform_get_irq(pdev, 0); |
| 232 | if (irq < 0) { |
| 233 | dev_err(&pdev->dev, "failed to get keypad irq\n"); |
| 234 | return -EINVAL; |
| 235 | } |
| 236 | |
| 237 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 238 | if (!res) { |
| 239 | dev_err(&pdev->dev, "missing platform resources\n"); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL); |
| 244 | input = input_allocate_device(); |
| 245 | if (!keypad || !input) { |
| 246 | dev_err(&pdev->dev, "failed to allocate keypad memory\n"); |
| 247 | error = -ENOMEM; |
| 248 | goto err_free_mem; |
| 249 | } |
| 250 | |
| 251 | keypad->irq = irq; |
| 252 | keypad->board = plat; |
| 253 | keypad->input = input; |
| 254 | spin_lock_init(&keypad->ske_keypad_lock); |
| 255 | |
| 256 | if (!request_mem_region(res->start, resource_size(res), pdev->name)) { |
| 257 | dev_err(&pdev->dev, "failed to request I/O memory\n"); |
| 258 | error = -EBUSY; |
| 259 | goto err_free_mem; |
| 260 | } |
| 261 | |
| 262 | keypad->reg_base = ioremap(res->start, resource_size(res)); |
| 263 | if (!keypad->reg_base) { |
| 264 | dev_err(&pdev->dev, "failed to remap I/O memory\n"); |
| 265 | error = -ENXIO; |
| 266 | goto err_free_mem_region; |
| 267 | } |
| 268 | |
| 269 | keypad->clk = clk_get(&pdev->dev, NULL); |
| 270 | if (IS_ERR(keypad->clk)) { |
| 271 | dev_err(&pdev->dev, "failed to get clk\n"); |
| 272 | error = PTR_ERR(keypad->clk); |
| 273 | goto err_iounmap; |
| 274 | } |
| 275 | |
| 276 | input->id.bustype = BUS_HOST; |
| 277 | input->name = "ux500-ske-keypad"; |
| 278 | input->dev.parent = &pdev->dev; |
| 279 | |
Dmitry Torokhov | 1932811 | 2012-05-10 22:37:08 -0700 | [diff] [blame] | 280 | error = matrix_keypad_build_keymap(plat->keymap_data, NULL, |
| 281 | SKE_KPD_NUM_ROWS, SKE_KPD_NUM_COLS, |
| 282 | keypad->keymap, input); |
| 283 | if (error) { |
| 284 | dev_err(&pdev->dev, "Failed to build keymap\n"); |
| 285 | goto err_iounmap; |
| 286 | } |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 287 | |
| 288 | input_set_capability(input, EV_MSC, MSC_SCAN); |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 289 | if (!plat->no_autorepeat) |
| 290 | __set_bit(EV_REP, input->evbit); |
| 291 | |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 292 | clk_enable(keypad->clk); |
| 293 | |
| 294 | /* go through board initialization helpers */ |
| 295 | if (keypad->board->init) |
| 296 | keypad->board->init(); |
| 297 | |
| 298 | error = ske_keypad_chip_init(keypad); |
| 299 | if (error) { |
| 300 | dev_err(&pdev->dev, "unable to init keypad hardware\n"); |
| 301 | goto err_clk_disable; |
| 302 | } |
| 303 | |
| 304 | error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq, |
| 305 | IRQF_ONESHOT, "ske-keypad", keypad); |
| 306 | if (error) { |
| 307 | dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq); |
| 308 | goto err_clk_disable; |
| 309 | } |
| 310 | |
| 311 | error = input_register_device(input); |
| 312 | if (error) { |
| 313 | dev_err(&pdev->dev, |
| 314 | "unable to register input device: %d\n", error); |
| 315 | goto err_free_irq; |
| 316 | } |
| 317 | |
| 318 | if (plat->wakeup_enable) |
| 319 | device_init_wakeup(&pdev->dev, true); |
| 320 | |
| 321 | platform_set_drvdata(pdev, keypad); |
| 322 | |
| 323 | return 0; |
| 324 | |
| 325 | err_free_irq: |
| 326 | free_irq(keypad->irq, keypad); |
| 327 | err_clk_disable: |
| 328 | clk_disable(keypad->clk); |
| 329 | clk_put(keypad->clk); |
| 330 | err_iounmap: |
| 331 | iounmap(keypad->reg_base); |
| 332 | err_free_mem_region: |
| 333 | release_mem_region(res->start, resource_size(res)); |
| 334 | err_free_mem: |
| 335 | input_free_device(input); |
| 336 | kfree(keypad); |
| 337 | return error; |
| 338 | } |
| 339 | |
| 340 | static int __devexit ske_keypad_remove(struct platform_device *pdev) |
| 341 | { |
| 342 | struct ske_keypad *keypad = platform_get_drvdata(pdev); |
| 343 | struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 344 | |
| 345 | free_irq(keypad->irq, keypad); |
| 346 | |
| 347 | input_unregister_device(keypad->input); |
| 348 | |
| 349 | clk_disable(keypad->clk); |
| 350 | clk_put(keypad->clk); |
| 351 | |
| 352 | if (keypad->board->exit) |
| 353 | keypad->board->exit(); |
| 354 | |
| 355 | iounmap(keypad->reg_base); |
| 356 | release_mem_region(res->start, resource_size(res)); |
| 357 | kfree(keypad); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Dmitry Torokhov | 89f0f17 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 362 | #ifdef CONFIG_PM_SLEEP |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 363 | static int ske_keypad_suspend(struct device *dev) |
| 364 | { |
| 365 | struct platform_device *pdev = to_platform_device(dev); |
| 366 | struct ske_keypad *keypad = platform_get_drvdata(pdev); |
| 367 | int irq = platform_get_irq(pdev, 0); |
| 368 | |
| 369 | if (device_may_wakeup(dev)) |
| 370 | enable_irq_wake(irq); |
| 371 | else |
| 372 | ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int ske_keypad_resume(struct device *dev) |
| 378 | { |
| 379 | struct platform_device *pdev = to_platform_device(dev); |
| 380 | struct ske_keypad *keypad = platform_get_drvdata(pdev); |
| 381 | int irq = platform_get_irq(pdev, 0); |
| 382 | |
| 383 | if (device_may_wakeup(dev)) |
| 384 | disable_irq_wake(irq); |
| 385 | else |
| 386 | ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA); |
| 387 | |
| 388 | return 0; |
| 389 | } |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 390 | #endif |
| 391 | |
Dmitry Torokhov | 89f0f17 | 2012-01-22 23:27:54 -0800 | [diff] [blame] | 392 | static SIMPLE_DEV_PM_OPS(ske_keypad_dev_pm_ops, |
| 393 | ske_keypad_suspend, ske_keypad_resume); |
| 394 | |
Axel Lin | aeec051 | 2011-12-11 23:45:26 -0800 | [diff] [blame] | 395 | static struct platform_driver ske_keypad_driver = { |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 396 | .driver = { |
| 397 | .name = "nmk-ske-keypad", |
| 398 | .owner = THIS_MODULE, |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 399 | .pm = &ske_keypad_dev_pm_ops, |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 400 | }, |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 401 | .remove = __devexit_p(ske_keypad_remove), |
| 402 | }; |
Dmitry Torokhov | d3d2580 | 2012-01-10 15:08:01 -0800 | [diff] [blame] | 403 | |
| 404 | static int __init ske_keypad_init(void) |
| 405 | { |
| 406 | return platform_driver_probe(&ske_keypad_driver, ske_keypad_probe); |
| 407 | } |
| 408 | module_init(ske_keypad_init); |
| 409 | |
| 410 | static void __exit ske_keypad_exit(void) |
| 411 | { |
| 412 | platform_driver_unregister(&ske_keypad_driver); |
| 413 | } |
| 414 | module_exit(ske_keypad_exit); |
Sundar Iyer | 1158f0f | 2010-09-29 19:42:14 -0700 | [diff] [blame] | 415 | |
| 416 | MODULE_LICENSE("GPL v2"); |
| 417 | MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>"); |
| 418 | MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver"); |
| 419 | MODULE_ALIAS("platform:nomadik-ske-keypad"); |