samsung-keypad.c 15 KB

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