samsung-keypad.c 15 KB

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