samsung-keypad.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/slab.h>
  23. #include <linux/sched.h>
  24. #include <plat/keypad.h>
  25. #define SAMSUNG_KEYIFCON 0x00
  26. #define SAMSUNG_KEYIFSTSCLR 0x04
  27. #define SAMSUNG_KEYIFCOL 0x08
  28. #define SAMSUNG_KEYIFROW 0x0c
  29. #define SAMSUNG_KEYIFFC 0x10
  30. /* SAMSUNG_KEYIFCON */
  31. #define SAMSUNG_KEYIFCON_INT_F_EN (1 << 0)
  32. #define SAMSUNG_KEYIFCON_INT_R_EN (1 << 1)
  33. #define SAMSUNG_KEYIFCON_DF_EN (1 << 2)
  34. #define SAMSUNG_KEYIFCON_FC_EN (1 << 3)
  35. #define SAMSUNG_KEYIFCON_WAKEUPEN (1 << 4)
  36. /* SAMSUNG_KEYIFSTSCLR */
  37. #define SAMSUNG_KEYIFSTSCLR_P_INT_MASK (0xff << 0)
  38. #define SAMSUNG_KEYIFSTSCLR_R_INT_MASK (0xff << 8)
  39. #define SAMSUNG_KEYIFSTSCLR_R_INT_OFFSET 8
  40. #define S5PV210_KEYIFSTSCLR_P_INT_MASK (0x3fff << 0)
  41. #define S5PV210_KEYIFSTSCLR_R_INT_MASK (0x3fff << 16)
  42. #define S5PV210_KEYIFSTSCLR_R_INT_OFFSET 16
  43. /* SAMSUNG_KEYIFCOL */
  44. #define SAMSUNG_KEYIFCOL_MASK (0xff << 0)
  45. #define S5PV210_KEYIFCOLEN_MASK (0xff << 8)
  46. /* SAMSUNG_KEYIFROW */
  47. #define SAMSUNG_KEYIFROW_MASK (0xff << 0)
  48. #define S5PV210_KEYIFROW_MASK (0x3fff << 0)
  49. /* SAMSUNG_KEYIFFC */
  50. #define SAMSUNG_KEYIFFC_MASK (0x3ff << 0)
  51. enum samsung_keypad_type {
  52. KEYPAD_TYPE_SAMSUNG,
  53. KEYPAD_TYPE_S5PV210,
  54. };
  55. struct samsung_keypad {
  56. struct input_dev *input_dev;
  57. struct clk *clk;
  58. void __iomem *base;
  59. wait_queue_head_t wait;
  60. bool stopped;
  61. int irq;
  62. unsigned int row_shift;
  63. unsigned int rows;
  64. unsigned int cols;
  65. unsigned int row_state[SAMSUNG_MAX_COLS];
  66. unsigned short keycodes[];
  67. };
  68. static int samsung_keypad_is_s5pv210(struct device *dev)
  69. {
  70. struct platform_device *pdev = to_platform_device(dev);
  71. enum samsung_keypad_type type =
  72. platform_get_device_id(pdev)->driver_data;
  73. return type == KEYPAD_TYPE_S5PV210;
  74. }
  75. static void samsung_keypad_scan(struct samsung_keypad *keypad,
  76. unsigned int *row_state)
  77. {
  78. struct device *dev = keypad->input_dev->dev.parent;
  79. unsigned int col;
  80. unsigned int val;
  81. for (col = 0; col < keypad->cols; col++) {
  82. if (samsung_keypad_is_s5pv210(dev)) {
  83. val = S5PV210_KEYIFCOLEN_MASK;
  84. val &= ~(1 << col) << 8;
  85. } else {
  86. val = SAMSUNG_KEYIFCOL_MASK;
  87. val &= ~(1 << col);
  88. }
  89. writel(val, keypad->base + SAMSUNG_KEYIFCOL);
  90. mdelay(1);
  91. val = readl(keypad->base + SAMSUNG_KEYIFROW);
  92. row_state[col] = ~val & ((1 << keypad->rows) - 1);
  93. }
  94. /* KEYIFCOL reg clear */
  95. writel(0, keypad->base + SAMSUNG_KEYIFCOL);
  96. }
  97. static bool samsung_keypad_report(struct samsung_keypad *keypad,
  98. unsigned int *row_state)
  99. {
  100. struct input_dev *input_dev = keypad->input_dev;
  101. unsigned int changed;
  102. unsigned int pressed;
  103. unsigned int key_down = 0;
  104. unsigned int val;
  105. unsigned int col, row;
  106. for (col = 0; col < keypad->cols; col++) {
  107. changed = row_state[col] ^ keypad->row_state[col];
  108. key_down |= row_state[col];
  109. if (!changed)
  110. continue;
  111. for (row = 0; row < keypad->rows; row++) {
  112. if (!(changed & (1 << row)))
  113. continue;
  114. pressed = row_state[col] & (1 << row);
  115. dev_dbg(&keypad->input_dev->dev,
  116. "key %s, row: %d, col: %d\n",
  117. pressed ? "pressed" : "released", row, col);
  118. val = MATRIX_SCAN_CODE(row, col, keypad->row_shift);
  119. input_event(input_dev, EV_MSC, MSC_SCAN, val);
  120. input_report_key(input_dev,
  121. keypad->keycodes[val], pressed);
  122. }
  123. input_sync(keypad->input_dev);
  124. }
  125. memcpy(keypad->row_state, row_state, sizeof(keypad->row_state));
  126. return key_down;
  127. }
  128. static irqreturn_t samsung_keypad_irq(int irq, void *dev_id)
  129. {
  130. struct samsung_keypad *keypad = dev_id;
  131. unsigned int row_state[SAMSUNG_MAX_COLS];
  132. unsigned int val;
  133. bool key_down;
  134. do {
  135. val = readl(keypad->base + SAMSUNG_KEYIFSTSCLR);
  136. /* Clear interrupt. */
  137. writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
  138. samsung_keypad_scan(keypad, row_state);
  139. key_down = samsung_keypad_report(keypad, row_state);
  140. if (key_down)
  141. wait_event_timeout(keypad->wait, keypad->stopped,
  142. msecs_to_jiffies(50));
  143. } while (key_down && !keypad->stopped);
  144. return IRQ_HANDLED;
  145. }
  146. static void samsung_keypad_start(struct samsung_keypad *keypad)
  147. {
  148. unsigned int val;
  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. }
  159. static void samsung_keypad_stop(struct samsung_keypad *keypad)
  160. {
  161. unsigned int val;
  162. /* Signal IRQ thread to stop polling and disable the handler. */
  163. keypad->stopped = true;
  164. wake_up(&keypad->wait);
  165. disable_irq(keypad->irq);
  166. /* Clear interrupt. */
  167. writel(~0x0, keypad->base + SAMSUNG_KEYIFSTSCLR);
  168. /* Disable interrupt bits. */
  169. val = readl(keypad->base + SAMSUNG_KEYIFCON);
  170. val &= ~(SAMSUNG_KEYIFCON_INT_F_EN | SAMSUNG_KEYIFCON_INT_R_EN);
  171. writel(val, keypad->base + SAMSUNG_KEYIFCON);
  172. clk_disable(keypad->clk);
  173. /*
  174. * Now that chip should not generate interrupts we can safely
  175. * re-enable the handler.
  176. */
  177. enable_irq(keypad->irq);
  178. }
  179. static int samsung_keypad_open(struct input_dev *input_dev)
  180. {
  181. struct samsung_keypad *keypad = input_get_drvdata(input_dev);
  182. samsung_keypad_start(keypad);
  183. return 0;
  184. }
  185. static void samsung_keypad_close(struct input_dev *input_dev)
  186. {
  187. struct samsung_keypad *keypad = input_get_drvdata(input_dev);
  188. samsung_keypad_stop(keypad);
  189. }
  190. static int __devinit samsung_keypad_probe(struct platform_device *pdev)
  191. {
  192. const struct samsung_keypad_platdata *pdata;
  193. const struct matrix_keymap_data *keymap_data;
  194. struct samsung_keypad *keypad;
  195. struct resource *res;
  196. struct input_dev *input_dev;
  197. unsigned int row_shift;
  198. unsigned int keymap_size;
  199. int error;
  200. pdata = pdev->dev.platform_data;
  201. if (!pdata) {
  202. dev_err(&pdev->dev, "no platform data defined\n");
  203. return -EINVAL;
  204. }
  205. keymap_data = pdata->keymap_data;
  206. if (!keymap_data) {
  207. dev_err(&pdev->dev, "no keymap data defined\n");
  208. return -EINVAL;
  209. }
  210. if (!pdata->rows || pdata->rows > SAMSUNG_MAX_ROWS)
  211. return -EINVAL;
  212. if (!pdata->cols || pdata->cols > SAMSUNG_MAX_COLS)
  213. return -EINVAL;
  214. /* initialize the gpio */
  215. if (pdata->cfg_gpio)
  216. pdata->cfg_gpio(pdata->rows, pdata->cols);
  217. row_shift = get_count_order(pdata->cols);
  218. keymap_size = (pdata->rows << row_shift) * sizeof(keypad->keycodes[0]);
  219. keypad = kzalloc(sizeof(*keypad) + keymap_size, GFP_KERNEL);
  220. input_dev = input_allocate_device();
  221. if (!keypad || !input_dev) {
  222. error = -ENOMEM;
  223. goto err_free_mem;
  224. }
  225. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  226. if (!res) {
  227. error = -ENODEV;
  228. goto err_free_mem;
  229. }
  230. keypad->base = ioremap(res->start, resource_size(res));
  231. if (!keypad->base) {
  232. error = -EBUSY;
  233. goto err_free_mem;
  234. }
  235. keypad->clk = clk_get(&pdev->dev, "keypad");
  236. if (IS_ERR(keypad->clk)) {
  237. dev_err(&pdev->dev, "failed to get keypad clk\n");
  238. error = PTR_ERR(keypad->clk);
  239. goto err_unmap_base;
  240. }
  241. keypad->input_dev = input_dev;
  242. keypad->row_shift = row_shift;
  243. keypad->rows = pdata->rows;
  244. keypad->cols = pdata->cols;
  245. init_waitqueue_head(&keypad->wait);
  246. input_dev->name = pdev->name;
  247. input_dev->id.bustype = BUS_HOST;
  248. input_dev->dev.parent = &pdev->dev;
  249. input_set_drvdata(input_dev, keypad);
  250. input_dev->open = samsung_keypad_open;
  251. input_dev->close = samsung_keypad_close;
  252. input_dev->evbit[0] = BIT_MASK(EV_KEY);
  253. if (!pdata->no_autorepeat)
  254. input_dev->evbit[0] |= BIT_MASK(EV_REP);
  255. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  256. input_dev->keycode = keypad->keycodes;
  257. input_dev->keycodesize = sizeof(keypad->keycodes[0]);
  258. input_dev->keycodemax = pdata->rows << row_shift;
  259. matrix_keypad_build_keymap(keymap_data, row_shift,
  260. input_dev->keycode, input_dev->keybit);
  261. keypad->irq = platform_get_irq(pdev, 0);
  262. if (keypad->irq < 0) {
  263. error = keypad->irq;
  264. goto err_put_clk;
  265. }
  266. error = request_threaded_irq(keypad->irq, NULL, samsung_keypad_irq,
  267. IRQF_ONESHOT, dev_name(&pdev->dev), keypad);
  268. if (error) {
  269. dev_err(&pdev->dev, "failed to register keypad interrupt\n");
  270. goto err_put_clk;
  271. }
  272. error = input_register_device(keypad->input_dev);
  273. if (error)
  274. goto err_free_irq;
  275. device_init_wakeup(&pdev->dev, pdata->wakeup);
  276. platform_set_drvdata(pdev, keypad);
  277. return 0;
  278. err_free_irq:
  279. free_irq(keypad->irq, keypad);
  280. err_put_clk:
  281. clk_put(keypad->clk);
  282. err_unmap_base:
  283. iounmap(keypad->base);
  284. err_free_mem:
  285. input_free_device(input_dev);
  286. kfree(keypad);
  287. return error;
  288. }
  289. static int __devexit samsung_keypad_remove(struct platform_device *pdev)
  290. {
  291. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  292. device_init_wakeup(&pdev->dev, 0);
  293. platform_set_drvdata(pdev, NULL);
  294. input_unregister_device(keypad->input_dev);
  295. /*
  296. * It is safe to free IRQ after unregistering device because
  297. * samsung_keypad_close will shut off interrupts.
  298. */
  299. free_irq(keypad->irq, keypad);
  300. clk_put(keypad->clk);
  301. iounmap(keypad->base);
  302. kfree(keypad);
  303. return 0;
  304. }
  305. #ifdef CONFIG_PM
  306. static void samsung_keypad_toggle_wakeup(struct samsung_keypad *keypad,
  307. bool enable)
  308. {
  309. struct device *dev = keypad->input_dev->dev.parent;
  310. unsigned int val;
  311. clk_enable(keypad->clk);
  312. val = readl(keypad->base + SAMSUNG_KEYIFCON);
  313. if (enable) {
  314. val |= SAMSUNG_KEYIFCON_WAKEUPEN;
  315. if (device_may_wakeup(dev))
  316. enable_irq_wake(keypad->irq);
  317. } else {
  318. val &= ~SAMSUNG_KEYIFCON_WAKEUPEN;
  319. if (device_may_wakeup(dev))
  320. disable_irq_wake(keypad->irq);
  321. }
  322. writel(val, keypad->base + SAMSUNG_KEYIFCON);
  323. clk_disable(keypad->clk);
  324. }
  325. static int samsung_keypad_suspend(struct device *dev)
  326. {
  327. struct platform_device *pdev = to_platform_device(dev);
  328. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  329. struct input_dev *input_dev = keypad->input_dev;
  330. mutex_lock(&input_dev->mutex);
  331. if (input_dev->users)
  332. samsung_keypad_stop(keypad);
  333. samsung_keypad_toggle_wakeup(keypad, true);
  334. mutex_unlock(&input_dev->mutex);
  335. return 0;
  336. }
  337. static int samsung_keypad_resume(struct device *dev)
  338. {
  339. struct platform_device *pdev = to_platform_device(dev);
  340. struct samsung_keypad *keypad = platform_get_drvdata(pdev);
  341. struct input_dev *input_dev = keypad->input_dev;
  342. mutex_lock(&input_dev->mutex);
  343. samsung_keypad_toggle_wakeup(keypad, false);
  344. if (input_dev->users)
  345. samsung_keypad_start(keypad);
  346. mutex_unlock(&input_dev->mutex);
  347. return 0;
  348. }
  349. static const struct dev_pm_ops samsung_keypad_pm_ops = {
  350. .suspend = samsung_keypad_suspend,
  351. .resume = samsung_keypad_resume,
  352. };
  353. #endif
  354. static struct platform_device_id samsung_keypad_driver_ids[] = {
  355. {
  356. .name = "samsung-keypad",
  357. .driver_data = KEYPAD_TYPE_SAMSUNG,
  358. }, {
  359. .name = "s5pv210-keypad",
  360. .driver_data = KEYPAD_TYPE_S5PV210,
  361. },
  362. { },
  363. };
  364. MODULE_DEVICE_TABLE(platform, samsung_keypad_driver_ids);
  365. static struct platform_driver samsung_keypad_driver = {
  366. .probe = samsung_keypad_probe,
  367. .remove = __devexit_p(samsung_keypad_remove),
  368. .driver = {
  369. .name = "samsung-keypad",
  370. .owner = THIS_MODULE,
  371. #ifdef CONFIG_PM
  372. .pm = &samsung_keypad_pm_ops,
  373. #endif
  374. },
  375. .id_table = samsung_keypad_driver_ids,
  376. };
  377. static int __init samsung_keypad_init(void)
  378. {
  379. return platform_driver_register(&samsung_keypad_driver);
  380. }
  381. module_init(samsung_keypad_init);
  382. static void __exit samsung_keypad_exit(void)
  383. {
  384. platform_driver_unregister(&samsung_keypad_driver);
  385. }
  386. module_exit(samsung_keypad_exit);
  387. MODULE_DESCRIPTION("Samsung keypad driver");
  388. MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
  389. MODULE_AUTHOR("Donghwa Lee <dh09.lee@samsung.com>");
  390. MODULE_LICENSE("GPL");
  391. MODULE_ALIAS("platform:samsung-keypad");