samsung-keypad.c 15 KB

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