nomadik-ske-keypad.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (C) ST-Ericsson SA 2010
  3. *
  4. * Author: Naveen Kumar G <naveen.gaddipati@stericsson.com> for ST-Ericsson
  5. * Author: Sundar Iyer <sundar.iyer@stericsson.com> for ST-Ericsson
  6. *
  7. * License terms:GNU General Public License (GPL) version 2
  8. *
  9. * Keypad controller driver for the SKE (Scroll Key Encoder) module used in
  10. * the Nomadik 8815 and Ux500 platforms.
  11. */
  12. #include <linux/platform_device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/io.h>
  16. #include <linux/delay.h>
  17. #include <linux/input.h>
  18. #include <linux/slab.h>
  19. #include <linux/clk.h>
  20. #include <plat/ske.h>
  21. /* SKE_CR bits */
  22. #define SKE_KPMLT (0x1 << 6)
  23. #define SKE_KPCN (0x7 << 3)
  24. #define SKE_KPASEN (0x1 << 2)
  25. #define SKE_KPASON (0x1 << 7)
  26. /* SKE_IMSC bits */
  27. #define SKE_KPIMA (0x1 << 2)
  28. /* SKE_ICR bits */
  29. #define SKE_KPICS (0x1 << 3)
  30. #define SKE_KPICA (0x1 << 2)
  31. /* SKE_RIS bits */
  32. #define SKE_KPRISA (0x1 << 2)
  33. #define SKE_KEYPAD_ROW_SHIFT 3
  34. #define SKE_KPD_KEYMAP_SIZE (8 * 8)
  35. /* keypad auto scan registers */
  36. #define SKE_ASR0 0x20
  37. #define SKE_ASR1 0x24
  38. #define SKE_ASR2 0x28
  39. #define SKE_ASR3 0x2C
  40. #define SKE_NUM_ASRX_REGISTERS (4)
  41. /**
  42. * struct ske_keypad - data structure used by keypad driver
  43. * @irq: irq no
  44. * @reg_base: ske regsiters base address
  45. * @input: pointer to input device object
  46. * @board: keypad platform device
  47. * @keymap: matrix scan code table for keycodes
  48. * @clk: clock structure pointer
  49. */
  50. struct ske_keypad {
  51. int irq;
  52. void __iomem *reg_base;
  53. struct input_dev *input;
  54. const struct ske_keypad_platform_data *board;
  55. unsigned short keymap[SKE_KPD_KEYMAP_SIZE];
  56. struct clk *clk;
  57. spinlock_t ske_keypad_lock;
  58. };
  59. static void ske_keypad_set_bits(struct ske_keypad *keypad, u16 addr,
  60. u8 mask, u8 data)
  61. {
  62. u32 ret;
  63. spin_lock(&keypad->ske_keypad_lock);
  64. ret = readl(keypad->reg_base + addr);
  65. ret &= ~mask;
  66. ret |= data;
  67. writel(ret, keypad->reg_base + addr);
  68. spin_unlock(&keypad->ske_keypad_lock);
  69. }
  70. /*
  71. * ske_keypad_chip_init: init keypad controller configuration
  72. *
  73. * Enable Multi key press detection, auto scan mode
  74. */
  75. static int __devinit ske_keypad_chip_init(struct ske_keypad *keypad)
  76. {
  77. u32 value;
  78. int timeout = 50;
  79. /* check SKE_RIS to be 0 */
  80. while ((readl(keypad->reg_base + SKE_RIS) != 0x00000000) && timeout--)
  81. cpu_relax();
  82. if (!timeout)
  83. return -EINVAL;
  84. /*
  85. * set debounce value
  86. * keypad dbounce is configured in DBCR[15:8]
  87. * dbounce value in steps of 32/32.768 ms
  88. */
  89. spin_lock(&keypad->ske_keypad_lock);
  90. value = readl(keypad->reg_base + SKE_DBCR);
  91. value = value & 0xff;
  92. value |= ((keypad->board->debounce_ms * 32000)/32768) << 8;
  93. writel(value, keypad->reg_base + SKE_DBCR);
  94. spin_unlock(&keypad->ske_keypad_lock);
  95. /* enable multi key detection */
  96. ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPMLT);
  97. /*
  98. * set up the number of columns
  99. * KPCN[5:3] defines no. of keypad columns to be auto scanned
  100. */
  101. value = (keypad->board->kcol - 1) << 3;
  102. ske_keypad_set_bits(keypad, SKE_CR, SKE_KPCN, value);
  103. /* clear keypad interrupt for auto(and pending SW) scans */
  104. ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA | SKE_KPICS);
  105. /* un-mask keypad interrupts */
  106. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  107. /* enable automatic scan */
  108. ske_keypad_set_bits(keypad, SKE_CR, 0x0, SKE_KPASEN);
  109. return 0;
  110. }
  111. static void ske_keypad_read_data(struct ske_keypad *keypad)
  112. {
  113. struct input_dev *input = keypad->input;
  114. u16 status;
  115. int col = 0, row = 0, code;
  116. int ske_asr, ske_ris, key_pressed, i;
  117. /*
  118. * Read the auto scan registers
  119. *
  120. * Each SKE_ASRx (x=0 to x=3) contains two row values.
  121. * lower byte contains row value for column 2*x,
  122. * upper byte contains row value for column 2*x + 1
  123. */
  124. for (i = 0; i < SKE_NUM_ASRX_REGISTERS; i++) {
  125. ske_asr = readl(keypad->reg_base + SKE_ASR0 + (4 * i));
  126. if (!ske_asr)
  127. continue;
  128. /* now that ASRx is zero, find out the column x and row y*/
  129. if (ske_asr & 0xff) {
  130. col = i * 2;
  131. status = ske_asr & 0xff;
  132. } else {
  133. col = (i * 2) + 1;
  134. status = (ske_asr & 0xff00) >> 8;
  135. }
  136. /* find out the row */
  137. row = __ffs(status);
  138. code = MATRIX_SCAN_CODE(row, col, SKE_KEYPAD_ROW_SHIFT);
  139. ske_ris = readl(keypad->reg_base + SKE_RIS);
  140. key_pressed = ske_ris & SKE_KPRISA;
  141. input_event(input, EV_MSC, MSC_SCAN, code);
  142. input_report_key(input, keypad->keymap[code], key_pressed);
  143. input_sync(input);
  144. }
  145. }
  146. static irqreturn_t ske_keypad_irq(int irq, void *dev_id)
  147. {
  148. struct ske_keypad *keypad = dev_id;
  149. int retries = 20;
  150. /* disable auto scan interrupt; mask the interrupt generated */
  151. ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
  152. ske_keypad_set_bits(keypad, SKE_ICR, 0x0, SKE_KPICA);
  153. while ((readl(keypad->reg_base + SKE_CR) & SKE_KPASON) && --retries)
  154. msleep(5);
  155. if (retries) {
  156. /* SKEx registers are stable and can be read */
  157. ske_keypad_read_data(keypad);
  158. }
  159. /* enable auto scan interrupts */
  160. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  161. return IRQ_HANDLED;
  162. }
  163. static int __devinit ske_keypad_probe(struct platform_device *pdev)
  164. {
  165. const struct ske_keypad_platform_data *plat = pdev->dev.platform_data;
  166. struct ske_keypad *keypad;
  167. struct input_dev *input;
  168. struct resource *res;
  169. int irq;
  170. int error;
  171. if (!plat) {
  172. dev_err(&pdev->dev, "invalid keypad platform data\n");
  173. return -EINVAL;
  174. }
  175. irq = platform_get_irq(pdev, 0);
  176. if (irq < 0) {
  177. dev_err(&pdev->dev, "failed to get keypad irq\n");
  178. return -EINVAL;
  179. }
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  181. if (!res) {
  182. dev_err(&pdev->dev, "missing platform resources\n");
  183. return -EINVAL;
  184. }
  185. keypad = kzalloc(sizeof(struct ske_keypad), GFP_KERNEL);
  186. input = input_allocate_device();
  187. if (!keypad || !input) {
  188. dev_err(&pdev->dev, "failed to allocate keypad memory\n");
  189. error = -ENOMEM;
  190. goto err_free_mem;
  191. }
  192. keypad->irq = irq;
  193. keypad->board = plat;
  194. keypad->input = input;
  195. spin_lock_init(&keypad->ske_keypad_lock);
  196. if (!request_mem_region(res->start, resource_size(res), pdev->name)) {
  197. dev_err(&pdev->dev, "failed to request I/O memory\n");
  198. error = -EBUSY;
  199. goto err_free_mem;
  200. }
  201. keypad->reg_base = ioremap(res->start, resource_size(res));
  202. if (!keypad->reg_base) {
  203. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  204. error = -ENXIO;
  205. goto err_free_mem_region;
  206. }
  207. keypad->clk = clk_get(&pdev->dev, NULL);
  208. if (IS_ERR(keypad->clk)) {
  209. dev_err(&pdev->dev, "failed to get clk\n");
  210. error = PTR_ERR(keypad->clk);
  211. goto err_iounmap;
  212. }
  213. input->id.bustype = BUS_HOST;
  214. input->name = "ux500-ske-keypad";
  215. input->dev.parent = &pdev->dev;
  216. input->keycode = keypad->keymap;
  217. input->keycodesize = sizeof(keypad->keymap[0]);
  218. input->keycodemax = ARRAY_SIZE(keypad->keymap);
  219. input_set_capability(input, EV_MSC, MSC_SCAN);
  220. __set_bit(EV_KEY, input->evbit);
  221. if (!plat->no_autorepeat)
  222. __set_bit(EV_REP, input->evbit);
  223. matrix_keypad_build_keymap(plat->keymap_data, SKE_KEYPAD_ROW_SHIFT,
  224. input->keycode, input->keybit);
  225. clk_enable(keypad->clk);
  226. /* go through board initialization helpers */
  227. if (keypad->board->init)
  228. keypad->board->init();
  229. error = ske_keypad_chip_init(keypad);
  230. if (error) {
  231. dev_err(&pdev->dev, "unable to init keypad hardware\n");
  232. goto err_clk_disable;
  233. }
  234. error = request_threaded_irq(keypad->irq, NULL, ske_keypad_irq,
  235. IRQF_ONESHOT, "ske-keypad", keypad);
  236. if (error) {
  237. dev_err(&pdev->dev, "allocate irq %d failed\n", keypad->irq);
  238. goto err_clk_disable;
  239. }
  240. error = input_register_device(input);
  241. if (error) {
  242. dev_err(&pdev->dev,
  243. "unable to register input device: %d\n", error);
  244. goto err_free_irq;
  245. }
  246. if (plat->wakeup_enable)
  247. device_init_wakeup(&pdev->dev, true);
  248. platform_set_drvdata(pdev, keypad);
  249. return 0;
  250. err_free_irq:
  251. free_irq(keypad->irq, keypad);
  252. err_clk_disable:
  253. clk_disable(keypad->clk);
  254. clk_put(keypad->clk);
  255. err_iounmap:
  256. iounmap(keypad->reg_base);
  257. err_free_mem_region:
  258. release_mem_region(res->start, resource_size(res));
  259. err_free_mem:
  260. input_free_device(input);
  261. kfree(keypad);
  262. return error;
  263. }
  264. static int __devexit ske_keypad_remove(struct platform_device *pdev)
  265. {
  266. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  267. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  268. free_irq(keypad->irq, keypad);
  269. input_unregister_device(keypad->input);
  270. clk_disable(keypad->clk);
  271. clk_put(keypad->clk);
  272. if (keypad->board->exit)
  273. keypad->board->exit();
  274. iounmap(keypad->reg_base);
  275. release_mem_region(res->start, resource_size(res));
  276. kfree(keypad);
  277. return 0;
  278. }
  279. #ifdef CONFIG_PM
  280. static int ske_keypad_suspend(struct device *dev)
  281. {
  282. struct platform_device *pdev = to_platform_device(dev);
  283. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  284. int irq = platform_get_irq(pdev, 0);
  285. if (device_may_wakeup(dev))
  286. enable_irq_wake(irq);
  287. else
  288. ske_keypad_set_bits(keypad, SKE_IMSC, ~SKE_KPIMA, 0x0);
  289. return 0;
  290. }
  291. static int ske_keypad_resume(struct device *dev)
  292. {
  293. struct platform_device *pdev = to_platform_device(dev);
  294. struct ske_keypad *keypad = platform_get_drvdata(pdev);
  295. int irq = platform_get_irq(pdev, 0);
  296. if (device_may_wakeup(dev))
  297. disable_irq_wake(irq);
  298. else
  299. ske_keypad_set_bits(keypad, SKE_IMSC, 0x0, SKE_KPIMA);
  300. return 0;
  301. }
  302. static const struct dev_pm_ops ske_keypad_dev_pm_ops = {
  303. .suspend = ske_keypad_suspend,
  304. .resume = ske_keypad_resume,
  305. };
  306. #endif
  307. struct platform_driver ske_keypad_driver = {
  308. .driver = {
  309. .name = "nmk-ske-keypad",
  310. .owner = THIS_MODULE,
  311. #ifdef CONFIG_PM
  312. .pm = &ske_keypad_dev_pm_ops,
  313. #endif
  314. },
  315. .probe = ske_keypad_probe,
  316. .remove = __devexit_p(ske_keypad_remove),
  317. };
  318. static int __init ske_keypad_init(void)
  319. {
  320. return platform_driver_probe(&ske_keypad_driver, ske_keypad_probe);
  321. }
  322. module_init(ske_keypad_init);
  323. static void __exit ske_keypad_exit(void)
  324. {
  325. platform_driver_unregister(&ske_keypad_driver);
  326. }
  327. module_exit(ske_keypad_exit);
  328. MODULE_LICENSE("GPL v2");
  329. MODULE_AUTHOR("Naveen Kumar <naveen.gaddipati@stericsson.com> / Sundar Iyer <sundar.iyer@stericsson.com>");
  330. MODULE_DESCRIPTION("Nomadik Scroll-Key-Encoder Keypad Driver");
  331. MODULE_ALIAS("platform:nomadik-ske-keypad");