nomadik-ske-keypad.c 9.8 KB

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