nomadik-ske-keypad.c 9.8 KB

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