w90p910_keypad.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2008-2009 Nuvoton technology corporation.
  3. *
  4. * Wan ZongShun <mcuos.com@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation;version 2 of the License.
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/input.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/clk.h>
  19. #include <linux/err.h>
  20. #include <linux/io.h>
  21. #include <mach/w90p910_keypad.h>
  22. /* Keypad Interface Control Registers */
  23. #define KPI_CONF 0x00
  24. #define KPI_3KCONF 0x04
  25. #define KPI_LPCONF 0x08
  26. #define KPI_STATUS 0x0C
  27. #define IS1KEY (0x01 << 16)
  28. #define INTTR (0x01 << 21)
  29. #define KEY0R (0x0f << 3)
  30. #define KEY0C 0x07
  31. #define DEBOUNCE_BIT 0x08
  32. #define KSIZE0 (0x01 << 16)
  33. #define KSIZE1 (0x01 << 17)
  34. #define KPSEL (0x01 << 19)
  35. #define ENKP (0x01 << 18)
  36. #define KGET_RAW(n) (((n) & KEY0R) >> 3)
  37. #define KGET_COLUMN(n) ((n) & KEY0C)
  38. #define W90P910_MAX_KEY_NUM (8 * 8)
  39. #define W90P910_ROW_SHIFT 3
  40. struct w90p910_keypad {
  41. const struct w90p910_keypad_platform_data *pdata;
  42. struct clk *clk;
  43. struct input_dev *input_dev;
  44. void __iomem *mmio_base;
  45. int irq;
  46. unsigned short keymap[W90P910_MAX_KEY_NUM];
  47. };
  48. static void w90p910_keypad_scan_matrix(struct w90p910_keypad *keypad,
  49. unsigned int status)
  50. {
  51. struct input_dev *input_dev = keypad->input_dev;
  52. unsigned int row = KGET_RAW(status);
  53. unsigned int col = KGET_COLUMN(status);
  54. unsigned int code = MATRIX_SCAN_CODE(row, col, W90P910_ROW_SHIFT);
  55. unsigned int key = keypad->keymap[code];
  56. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  57. input_report_key(input_dev, key, 1);
  58. input_sync(input_dev);
  59. input_event(input_dev, EV_MSC, MSC_SCAN, code);
  60. input_report_key(input_dev, key, 0);
  61. input_sync(input_dev);
  62. }
  63. static irqreturn_t w90p910_keypad_irq_handler(int irq, void *dev_id)
  64. {
  65. struct w90p910_keypad *keypad = dev_id;
  66. unsigned int kstatus, val;
  67. kstatus = __raw_readl(keypad->mmio_base + KPI_STATUS);
  68. val = INTTR | IS1KEY;
  69. if (kstatus & val)
  70. w90p910_keypad_scan_matrix(keypad, kstatus);
  71. return IRQ_HANDLED;
  72. }
  73. static int w90p910_keypad_open(struct input_dev *dev)
  74. {
  75. struct w90p910_keypad *keypad = input_get_drvdata(dev);
  76. const struct w90p910_keypad_platform_data *pdata = keypad->pdata;
  77. unsigned int val, config;
  78. /* Enable unit clock */
  79. clk_enable(keypad->clk);
  80. val = __raw_readl(keypad->mmio_base + KPI_CONF);
  81. val |= (KPSEL | ENKP);
  82. val &= ~(KSIZE0 | KSIZE1);
  83. config = pdata->prescale | (pdata->debounce << DEBOUNCE_BIT);
  84. val |= config;
  85. __raw_writel(val, keypad->mmio_base + KPI_CONF);
  86. return 0;
  87. }
  88. static void w90p910_keypad_close(struct input_dev *dev)
  89. {
  90. struct w90p910_keypad *keypad = input_get_drvdata(dev);
  91. /* Disable clock unit */
  92. clk_disable(keypad->clk);
  93. }
  94. static int __devinit w90p910_keypad_probe(struct platform_device *pdev)
  95. {
  96. const struct w90p910_keypad_platform_data *pdata =
  97. pdev->dev.platform_data;
  98. const struct matrix_keymap_data *keymap_data;
  99. struct w90p910_keypad *keypad;
  100. struct input_dev *input_dev;
  101. struct resource *res;
  102. int irq;
  103. int error;
  104. if (!pdata) {
  105. dev_err(&pdev->dev, "no platform data defined\n");
  106. return -EINVAL;
  107. }
  108. keymap_data = pdata->keymap_data;
  109. irq = platform_get_irq(pdev, 0);
  110. if (irq < 0) {
  111. dev_err(&pdev->dev, "failed to get keypad irq\n");
  112. return -ENXIO;
  113. }
  114. keypad = kzalloc(sizeof(struct w90p910_keypad), GFP_KERNEL);
  115. input_dev = input_allocate_device();
  116. if (!keypad || !input_dev) {
  117. dev_err(&pdev->dev, "failed to allocate driver data\n");
  118. error = -ENOMEM;
  119. goto failed_free;
  120. }
  121. keypad->pdata = pdata;
  122. keypad->input_dev = input_dev;
  123. keypad->irq = irq;
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (res == NULL) {
  126. dev_err(&pdev->dev, "failed to get I/O memory\n");
  127. error = -ENXIO;
  128. goto failed_free;
  129. }
  130. res = request_mem_region(res->start, resource_size(res), pdev->name);
  131. if (res == NULL) {
  132. dev_err(&pdev->dev, "failed to request I/O memory\n");
  133. error = -EBUSY;
  134. goto failed_free;
  135. }
  136. keypad->mmio_base = ioremap(res->start, resource_size(res));
  137. if (keypad->mmio_base == NULL) {
  138. dev_err(&pdev->dev, "failed to remap I/O memory\n");
  139. error = -ENXIO;
  140. goto failed_free_res;
  141. }
  142. keypad->clk = clk_get(&pdev->dev, NULL);
  143. if (IS_ERR(keypad->clk)) {
  144. dev_err(&pdev->dev, "failed to get keypad clock\n");
  145. error = PTR_ERR(keypad->clk);
  146. goto failed_free_io;
  147. }
  148. /* set multi-function pin for w90p910 kpi. */
  149. mfp_set_groupi(&pdev->dev);
  150. input_dev->name = pdev->name;
  151. input_dev->id.bustype = BUS_HOST;
  152. input_dev->open = w90p910_keypad_open;
  153. input_dev->close = w90p910_keypad_close;
  154. input_dev->dev.parent = &pdev->dev;
  155. input_dev->keycode = keypad->keymap;
  156. input_dev->keycodesize = sizeof(keypad->keymap[0]);
  157. input_dev->keycodemax = ARRAY_SIZE(keypad->keymap);
  158. input_set_drvdata(input_dev, keypad);
  159. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REP);
  160. input_set_capability(input_dev, EV_MSC, MSC_SCAN);
  161. matrix_keypad_build_keymap(keymap_data, W90P910_ROW_SHIFT,
  162. input_dev->keycode, input_dev->keybit);
  163. error = request_irq(keypad->irq, w90p910_keypad_irq_handler,
  164. IRQF_DISABLED, pdev->name, keypad);
  165. if (error) {
  166. dev_err(&pdev->dev, "failed to request IRQ\n");
  167. goto failed_put_clk;
  168. }
  169. /* Register the input device */
  170. error = input_register_device(input_dev);
  171. if (error) {
  172. dev_err(&pdev->dev, "failed to register input device\n");
  173. goto failed_free_irq;
  174. }
  175. platform_set_drvdata(pdev, keypad);
  176. return 0;
  177. failed_free_irq:
  178. free_irq(irq, pdev);
  179. failed_put_clk:
  180. clk_put(keypad->clk);
  181. failed_free_io:
  182. iounmap(keypad->mmio_base);
  183. failed_free_res:
  184. release_mem_region(res->start, resource_size(res));
  185. failed_free:
  186. input_free_device(input_dev);
  187. kfree(keypad);
  188. return error;
  189. }
  190. static int __devexit w90p910_keypad_remove(struct platform_device *pdev)
  191. {
  192. struct w90p910_keypad *keypad = platform_get_drvdata(pdev);
  193. struct resource *res;
  194. free_irq(keypad->irq, pdev);
  195. clk_put(keypad->clk);
  196. input_unregister_device(keypad->input_dev);
  197. iounmap(keypad->mmio_base);
  198. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  199. release_mem_region(res->start, resource_size(res));
  200. platform_set_drvdata(pdev, NULL);
  201. kfree(keypad);
  202. return 0;
  203. }
  204. static struct platform_driver w90p910_keypad_driver = {
  205. .probe = w90p910_keypad_probe,
  206. .remove = __devexit_p(w90p910_keypad_remove),
  207. .driver = {
  208. .name = "nuc900-keypad",
  209. .owner = THIS_MODULE,
  210. },
  211. };
  212. static int __init w90p910_keypad_init(void)
  213. {
  214. return platform_driver_register(&w90p910_keypad_driver);
  215. }
  216. static void __exit w90p910_keypad_exit(void)
  217. {
  218. platform_driver_unregister(&w90p910_keypad_driver);
  219. }
  220. module_init(w90p910_keypad_init);
  221. module_exit(w90p910_keypad_exit);
  222. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  223. MODULE_DESCRIPTION("w90p910 keypad driver");
  224. MODULE_LICENSE("GPL");
  225. MODULE_ALIAS("platform:nuc900-keypad");