1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Samsung keypad driver 4 * 5 * Copyright (C) 2010 Samsung Electronics Co.Ltd 6 * Author: Joonyoung Shim <jy0922.shim@samsung.com> 7 * Author: Donghwa Lee <dh09.lee@samsung.com> 8 */ 9 10 #include <linux/bits.h> 11 #include <linux/clk.h> 12 #include <linux/delay.h> 13 #include <linux/err.h> 14 #include <linux/input.h> 15 #include <linux/interrupt.h> 16 #include <linux/io.h> 17 #include <linux/module.h> 18 #include <linux/platform_device.h> 19 #include <linux/pm.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/slab.h> 22 #include <linux/of.h> 23 #include <linux/sched.h> 24 #include <linux/input/samsung-keypad.h> 25 26 #define SAMSUNG_KEYIFCON 0x00 27 #define SAMSUNG_KEYIFSTSCLR 0x04 28 #define SAMSUNG_KEYIFCOL 0x08 29 #define SAMSUNG_KEYIFROW 0x0c 30 #define SAMSUNG_KEYIFFC 0x10 31 32 /* SAMSUNG_KEYIFCON */ 33 #define SAMSUNG_KEYIFCON_INT_F_EN BIT(0) 34 #define SAMSUNG_KEYIFCON_INT_R_EN BIT(1) 35 #define SAMSUNG_KEYIFCON_DF_EN BIT(2) 36 #define SAMSUNG_KEYIFCON_FC_EN BIT(3) 37 #define SAMSUNG_KEYIFCON_WAKEUPEN BIT(4) 38 39 /* SAMSUNG_KEYIFSTSCLR */ 40 #define SAMSUNG_KEYIFSTSCLR_P_INT_MASK (0xff << 0) 41 #define SAMSUNG_KEYIFSTSCLR_R_INT_MASK (0xff << 8) 42 #define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET 8 43 #define S5PV210_KEYIFSTSCLR_P_INT_MASK (0x3fff << 0) 44 #define S5PV210_KEYIFSTSCLR_R_INT_MASK (0x3fff << 16) 45 #define S5PV210_KEYIFSTSCLR_R_INT_OFFSET 16 46 47 /* SAMSUNG_KEYIFCOL */ 48 #define SAMSUNG_KEYIFCOL_MASK 0xff 49 50 /* SAMSUNG_KEYIFROW */ 51 #define SAMSUNG_KEYIFROW_MASK (0xff << 0) 52 #define S5PV210_KEYIFROW_MASK (0x3fff << 0) 53 54 /* SAMSUNG_KEYIFFC */ 55 #define SAMSUNG_KEYIFFC_MASK (0x3ff << 0) 56 57 struct samsung_chip_info { 58 unsigned int column_shift; 59 }; 60 61 struct samsung_keypad { 62 const struct samsung_chip_info *chip; 63 struct input_dev *input_dev; 64 struct platform_device *pdev; 65 struct clk *clk; 66 void __iomem *base; 67 wait_queue_head_t wait; 68 bool stopped; 69 bool wake_enabled; 70 int irq; 71 unsigned int row_shift; 72 unsigned int rows; 73 unsigned int cols; 74 unsigned int row_state[SAMSUNG_MAX_COLS]; 75 unsigned short keycodes[]; 76 }; 77 78 static void samsung_keypad_scan(struct samsung_keypad *keypad, 79 unsigned int *row_state) 80 { 81 unsigned int col; 82 unsigned int val; 83 84 for (col = 0; col < keypad->cols; col++) { 85 val = SAMSUNG_KEYIFCOL_MASK & ~BIT(col); 86 val <<= keypad->chip->column_shift; 87 88 writel(val, keypad->base + SAMSUNG_KEYIFCOL); 89 mdelay(1); 90 91 val = readl(keypad->base + SAMSUNG_KEYIFROW); 92 row_state[col] = ~val & GENMASK(keypad->rows - 1, 0); 93 } 94 95 /* KEYIFCOL reg clear */ 96 writel(0, keypad->base + SAMSUNG_KEYIFCOL); 97 } 98 99 static bool samsung_keypad_report(struct samsung_keypad *keypad, 100 unsigned int *row_state) 101 { 102 struct input_dev *input_dev = keypad->input_dev; 103 unsigned int changed; 104 unsigned int pressed; 105 unsigned int key_down = 0; 106 unsigned int val; 107 unsigned int col, row; 108 109 for (col = 0; col < keypad->cols; col++) { 110 changed = row_state[col] ^ keypad->row_state[col]; 111 key_down |= row_state[col]; 112 if (!changed) 113 continue; 114 115 for (row = 0; row < keypad->rows; row++) { 116 if (!(changed & BIT(row))) 117 continue; 118 119 pressed = row_state[col] & BIT(row); 120 121 dev_dbg(&keypad->input_dev->dev, 122 "key %s, row: %d, col: %d\n", 123 pressed ? "pressed" : "released", row, col); 124 125 val = MATRIX_SCAN_CODE(row, col, keypad->row_shift); 126 127 input_event(input_dev, EV_MSC, MSC_SCAN, val); 128 input_report_key(input_dev, 129 keypad->keycodes[val], pressed); 130 } 131 input_sync(keypad->input_dev); 132 } 133 134 memcpy(keypad->row_state, row_state, sizeof(keypad->row_state)); 135 136 return key_down; 137 } 138 139 static irqreturn_t samsung_keypad_irq(int irq, void *dev_id) 140 { 141 struct samsung_keypad *keypad = dev_id; 142 unsigned int row_state[SAMSUNG_MAX_COLS]; 143 bool key_down; 144 145 guard(pm_runtime_active)(&keypad->pdev->dev); 146 147 do { 148 readl(keypad->base + SAMSUNG_KEYIFSTSCLR); 149 /* Clear interrupt. */ 150 writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR); 151 152 samsung_keypad_scan(keypad, row_state); 153 154 key_down = samsung_keypad_report(keypad, row_state); 155 if (key_down) 156 wait_event_timeout(keypad->wait, keypad->stopped, 157 msecs_to_jiffies(50)); 158 159 } while (key_down && !keypad->stopped); 160 161 return IRQ_HANDLED; 162 } 163 164 static void samsung_keypad_start(struct samsung_keypad *keypad) 165 { 166 unsigned int val; 167 168 guard(pm_runtime_active)(&keypad->pdev->dev); 169 170 /* Tell IRQ thread that it may poll the device. */ 171 keypad->stopped = false; 172 173 clk_enable(keypad->clk); 174 175 /* Enable interrupt bits. */ 176 val = readl(keypad->base + SAMSUNG_KEYIFCON); 177 val |= SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN; 178 writel(val, keypad->base + SAMSUNG_KEYIFCON); 179 180 /* KEYIFCOL reg clear. */ 181 writel(0, keypad->base + SAMSUNG_KEYIFCOL); 182 183 enable_irq(keypad->irq); 184 } 185 186 static void samsung_keypad_stop(struct samsung_keypad *keypad) 187 { 188 unsigned int val; 189 190 guard(pm_runtime_active)(&keypad->pdev->dev); 191 192 /* Signal IRQ thread to stop polling and disable the handler. */ 193 keypad->stopped = true; 194 wake_up(&keypad->wait); 195 disable_irq(keypad->irq); 196 197 /* Clear interrupt. */ 198 writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR); 199 200 /* Disable interrupt bits. */ 201 val = readl(keypad->base + SAMSUNG_KEYIFCON); 202 val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN); 203 writel(val, keypad->base + SAMSUNG_KEYIFCON); 204 205 clk_disable(keypad->clk); 206 } 207 208 static int samsung_keypad_open(struct input_dev *input_dev) 209 { 210 struct samsung_keypad *keypad = input_get_drvdata(input_dev); 211 212 samsung_keypad_start(keypad); 213 214 return 0; 215 } 216 217 static void samsung_keypad_close(struct input_dev *input_dev) 218 { 219 struct samsung_keypad *keypad = input_get_drvdata(input_dev); 220 221 samsung_keypad_stop(keypad); 222 } 223 224 #ifdef CONFIG_OF 225 static struct samsung_keypad_platdata * 226 samsung_keypad_parse_dt(struct device *dev) 227 { 228 struct samsung_keypad_platdata *pdata; 229 struct matrix_keymap_data *keymap_data; 230 uint32_t *keymap, num_rows = 0, num_cols = 0; 231 struct device_node *np = dev->of_node, *key_np; 232 unsigned int key_count; 233 234 if (!np) { 235 dev_err(dev, "missing device tree data\n"); 236 return ERR_PTR(-EINVAL); 237 } 238 239 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); 240 if (!pdata) { 241 dev_err(dev, "could not allocate memory for platform data\n"); 242 return ERR_PTR(-ENOMEM); 243 } 244 245 of_property_read_u32(np, "samsung,keypad-num-rows", &num_rows); 246 of_property_read_u32(np, "samsung,keypad-num-columns", &num_cols); 247 if (!num_rows || !num_cols) { 248 dev_err(dev, "number of keypad rows/columns not specified\n"); 249 return ERR_PTR(-EINVAL); 250 } 251 pdata->rows = num_rows; 252 pdata->cols = num_cols; 253 254 keymap_data = devm_kzalloc(dev, sizeof(*keymap_data), GFP_KERNEL); 255 if (!keymap_data) { 256 dev_err(dev, "could not allocate memory for keymap data\n"); 257 return ERR_PTR(-ENOMEM); 258 } 259 pdata->keymap_data = keymap_data; 260 261 key_count = of_get_child_count(np); 262 keymap_data->keymap_size = key_count; 263 keymap = devm_kcalloc(dev, key_count, sizeof(uint32_t), GFP_KERNEL); 264 if (!keymap) { 265 dev_err(dev, "could not allocate memory for keymap\n"); 266 return ERR_PTR(-ENOMEM); 267 } 268 keymap_data->keymap = keymap; 269 270 for_each_child_of_node(np, key_np) { 271 u32 row, col, key_code; 272 of_property_read_u32(key_np, "keypad,row", &row); 273 of_property_read_u32(key_np, "keypad,column", &col); 274 of_property_read_u32(key_np, "linux,code", &key_code); 275 *keymap++ = KEY(row, col, key_code); 276 } 277 278 pdata->no_autorepeat = of_property_read_bool(np, "linux,input-no-autorepeat"); 279 280 pdata->wakeup = of_property_read_bool(np, "wakeup-source") || 281 /* legacy name */ 282 of_property_read_bool(np, "linux,input-wakeup"); 283 284 285 return pdata; 286 } 287 #else 288 static struct samsung_keypad_platdata * 289 samsung_keypad_parse_dt(struct device *dev) 290 { 291 dev_err(dev, "no platform data defined\n"); 292 293 return ERR_PTR(-EINVAL); 294 } 295 #endif 296 297 static int samsung_keypad_probe(struct platform_device *pdev) 298 { 299 const struct samsung_keypad_platdata *pdata; 300 const struct matrix_keymap_data *keymap_data; 301 const struct platform_device_id *id; 302 struct samsung_keypad *keypad; 303 struct resource *res; 304 struct input_dev *input_dev; 305 unsigned int row_shift; 306 int error; 307 308 pdata = dev_get_platdata(&pdev->dev); 309 if (!pdata) { 310 pdata = samsung_keypad_parse_dt(&pdev->dev); 311 if (IS_ERR(pdata)) 312 return PTR_ERR(pdata); 313 } 314 315 keymap_data = pdata->keymap_data; 316 if (!keymap_data) { 317 dev_err(&pdev->dev, "no keymap data defined\n"); 318 return -EINVAL; 319 } 320 321 if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS) 322 return -EINVAL; 323 324 if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS) 325 return -EINVAL; 326 327 /* initialize the gpio */ 328 if (pdata->cfg_gpio) 329 pdata->cfg_gpio(pdata->rows, pdata->cols); 330 331 row_shift = get_count_order(pdata->cols); 332 333 keypad = devm_kzalloc(&pdev->dev, 334 struct_size(keypad, keycodes, 335 pdata->rows << row_shift), 336 GFP_KERNEL); 337 if (!keypad) 338 return -ENOMEM; 339 340 input_dev = devm_input_allocate_device(&pdev->dev); 341 if (!input_dev) 342 return -ENOMEM; 343 344 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 345 if (!res) 346 return -ENODEV; 347 348 keypad->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 349 if (!keypad->base) 350 return -EBUSY; 351 352 keypad->clk = devm_clk_get_prepared(&pdev->dev, "keypad"); 353 if (IS_ERR(keypad->clk)) { 354 dev_err(&pdev->dev, "failed to get keypad clk\n"); 355 return PTR_ERR(keypad->clk); 356 } 357 358 keypad->input_dev = input_dev; 359 keypad->pdev = pdev; 360 keypad->row_shift = row_shift; 361 keypad->rows = pdata->rows; 362 keypad->cols = pdata->cols; 363 keypad->stopped = true; 364 init_waitqueue_head(&keypad->wait); 365 366 keypad->chip = device_get_match_data(&pdev->dev); 367 if (!keypad->chip) { 368 id = platform_get_device_id(pdev); 369 if (id) 370 keypad->chip = (const void *)id->driver_data; 371 } 372 373 if (!keypad->chip) { 374 dev_err(&pdev->dev, "Unable to determine chip type"); 375 return -EINVAL; 376 } 377 378 input_dev->name = pdev->name; 379 input_dev->id.bustype = BUS_HOST; 380 381 input_dev->open = samsung_keypad_open; 382 input_dev->close = samsung_keypad_close; 383 384 error = matrix_keypad_build_keymap(keymap_data, NULL, 385 pdata->rows, pdata->cols, 386 keypad->keycodes, input_dev); 387 if (error) { 388 dev_err(&pdev->dev, "failed to build keymap\n"); 389 return error; 390 } 391 392 input_set_capability(input_dev, EV_MSC, MSC_SCAN); 393 if (!pdata->no_autorepeat) 394 __set_bit(EV_REP, input_dev->evbit); 395 396 input_set_drvdata(input_dev, keypad); 397 398 keypad->irq = platform_get_irq(pdev, 0); 399 if (keypad->irq < 0) { 400 error = keypad->irq; 401 return error; 402 } 403 404 error = devm_request_threaded_irq(&pdev->dev, keypad->irq, NULL, 405 samsung_keypad_irq, 406 IRQF_ONESHOT | IRQF_NO_AUTOEN, 407 dev_name(&pdev->dev), keypad); 408 if (error) { 409 dev_err(&pdev->dev, "failed to register keypad interrupt\n"); 410 return error; 411 } 412 413 device_init_wakeup(&pdev->dev, pdata->wakeup); 414 platform_set_drvdata(pdev, keypad); 415 416 error = devm_pm_runtime_enable(&pdev->dev); 417 if (error) 418 return error; 419 420 error = input_register_device(keypad->input_dev); 421 if (error) 422 return error; 423 424 if (pdev->dev.of_node) { 425 devm_kfree(&pdev->dev, (void *)pdata->keymap_data->keymap); 426 devm_kfree(&pdev->dev, (void *)pdata->keymap_data); 427 devm_kfree(&pdev->dev, (void *)pdata); 428 } 429 return 0; 430 } 431 432 static int samsung_keypad_runtime_suspend(struct device *dev) 433 { 434 struct platform_device *pdev = to_platform_device(dev); 435 struct samsung_keypad *keypad = platform_get_drvdata(pdev); 436 unsigned int val; 437 int error; 438 439 if (keypad->stopped) 440 return 0; 441 442 /* This may fail on some SoCs due to lack of controller support */ 443 error = enable_irq_wake(keypad->irq); 444 if (!error) 445 keypad->wake_enabled = true; 446 447 val = readl(keypad->base + SAMSUNG_KEYIFCON); 448 val |= SAMSUNG_KEYIFCON_WAKEUPEN; 449 writel(val, keypad->base + SAMSUNG_KEYIFCON); 450 451 clk_disable(keypad->clk); 452 453 return 0; 454 } 455 456 static int samsung_keypad_runtime_resume(struct device *dev) 457 { 458 struct platform_device *pdev = to_platform_device(dev); 459 struct samsung_keypad *keypad = platform_get_drvdata(pdev); 460 unsigned int val; 461 462 if (keypad->stopped) 463 return 0; 464 465 clk_enable(keypad->clk); 466 467 val = readl(keypad->base + SAMSUNG_KEYIFCON); 468 val &= ~SAMSUNG_KEYIFCON_WAKEUPEN; 469 writel(val, keypad->base + SAMSUNG_KEYIFCON); 470 471 if (keypad->wake_enabled) 472 disable_irq_wake(keypad->irq); 473 474 return 0; 475 } 476 477 static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad, 478 bool enable) 479 { 480 unsigned int val; 481 482 clk_enable(keypad->clk); 483 484 val = readl(keypad->base + SAMSUNG_KEYIFCON); 485 if (enable) { 486 enable_irq_wake(keypad->irq); 487 val |= SAMSUNG_KEYIFCON_WAKEUPEN; 488 writel(val, keypad->base + SAMSUNG_KEYIFCON); 489 } else { 490 val &= ~SAMSUNG_KEYIFCON_WAKEUPEN; 491 writel(val, keypad->base + SAMSUNG_KEYIFCON); 492 disable_irq_wake(keypad->irq); 493 494 if (!input_device_enabled(keypad->input_dev)) 495 writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR); 496 } 497 498 clk_disable(keypad->clk); 499 } 500 501 static int samsung_keypad_suspend(struct device *dev) 502 { 503 struct platform_device *pdev = to_platform_device(dev); 504 struct samsung_keypad *keypad = platform_get_drvdata(pdev); 505 struct input_dev *input_dev = keypad->input_dev; 506 507 guard(mutex)(&input_dev->mutex); 508 509 if (input_device_enabled(input_dev)) 510 samsung_keypad_stop(keypad); 511 512 if (device_may_wakeup(dev)) 513 samsung_keypad_toggle_wakeup(keypad, true); 514 515 return 0; 516 } 517 518 static int samsung_keypad_resume(struct device *dev) 519 { 520 struct platform_device *pdev = to_platform_device(dev); 521 struct samsung_keypad *keypad = platform_get_drvdata(pdev); 522 struct input_dev *input_dev = keypad->input_dev; 523 524 guard(mutex)(&input_dev->mutex); 525 526 if (device_may_wakeup(dev)) 527 samsung_keypad_toggle_wakeup(keypad, false); 528 529 if (input_device_enabled(input_dev)) 530 samsung_keypad_start(keypad); 531 532 return 0; 533 } 534 535 static const struct dev_pm_ops samsung_keypad_pm_ops = { 536 SYSTEM_SLEEP_PM_OPS(samsung_keypad_suspend, samsung_keypad_resume) 537 RUNTIME_PM_OPS(samsung_keypad_runtime_suspend, 538 samsung_keypad_runtime_resume, NULL) 539 }; 540 541 static const struct samsung_chip_info samsung_s3c6410_chip_info = { 542 .column_shift = 0, 543 }; 544 545 static const struct samsung_chip_info samsung_s5pv210_chip_info = { 546 .column_shift = 8, 547 }; 548 549 #ifdef CONFIG_OF 550 static const struct of_device_id samsung_keypad_dt_match[] = { 551 { 552 .compatible = "samsung,s3c6410-keypad", 553 .data = &samsung_s3c6410_chip_info, 554 }, { 555 .compatible = "samsung,s5pv210-keypad", 556 .data = &samsung_s5pv210_chip_info, 557 }, 558 { } 559 }; 560 MODULE_DEVICE_TABLE(of, samsung_keypad_dt_match); 561 #endif 562 563 static const struct platform_device_id samsung_keypad_driver_ids[] = { 564 { 565 .name = "samsung-keypad", 566 .driver_data = (kernel_ulong_t)&samsung_s3c6410_chip_info, 567 }, { 568 .name = "s5pv210-keypad", 569 .driver_data = (kernel_ulong_t)&samsung_s5pv210_chip_info, 570 }, 571 { } 572 }; 573 MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids); 574 575 static struct platform_driver samsung_keypad_driver = { 576 .probe = samsung_keypad_probe, 577 .driver = { 578 .name = "samsung-keypad", 579 .of_match_table = of_match_ptr(samsung_keypad_dt_match), 580 .pm = pm_ptr(&samsung_keypad_pm_ops), 581 }, 582 .id_table = samsung_keypad_driver_ids, 583 }; 584 module_platform_driver(samsung_keypad_driver); 585 586 MODULE_DESCRIPTION("Samsung keypad driver"); 587 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>"); 588 MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>"); 589 MODULE_LICENSE("GPL"); 590