nomadik-ske-keypad.c 10 KB

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