pxa930_trkball.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * PXA930 track ball mouse driver
  3. *
  4. * Copyright (C) 2007 Marvell International Ltd.
  5. * 2008-02-28: Yong Yao <yaoyong@marvell.com>
  6. * initial version
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/input.h>
  14. #include <linux/version.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/module.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/delay.h>
  19. #include <linux/io.h>
  20. #include <linux/slab.h>
  21. #include <mach/hardware.h>
  22. #include <mach/pxa930_trkball.h>
  23. /* Trackball Controller Register Definitions */
  24. #define TBCR (0x000C)
  25. #define TBCNTR (0x0010)
  26. #define TBSBC (0x0014)
  27. #define TBCR_TBRST (1 << 1)
  28. #define TBCR_TBSB (1 << 10)
  29. #define TBCR_Y_FLT(n) (((n) & 0xf) << 6)
  30. #define TBCR_X_FLT(n) (((n) & 0xf) << 2)
  31. #define TBCNTR_YM(n) (((n) >> 24) & 0xff)
  32. #define TBCNTR_YP(n) (((n) >> 16) & 0xff)
  33. #define TBCNTR_XM(n) (((n) >> 8) & 0xff)
  34. #define TBCNTR_XP(n) ((n) & 0xff)
  35. #define TBSBC_TBSBC (0x1)
  36. struct pxa930_trkball {
  37. struct pxa930_trkball_platform_data *pdata;
  38. /* Memory Mapped Register */
  39. struct resource *mem;
  40. void __iomem *mmio_base;
  41. struct input_dev *input;
  42. };
  43. static irqreturn_t pxa930_trkball_interrupt(int irq, void *dev_id)
  44. {
  45. struct pxa930_trkball *trkball = dev_id;
  46. struct input_dev *input = trkball->input;
  47. int tbcntr, x, y;
  48. /* According to the spec software must read TBCNTR twice:
  49. * if the read value is the same, the reading is valid
  50. */
  51. tbcntr = __raw_readl(trkball->mmio_base + TBCNTR);
  52. if (tbcntr == __raw_readl(trkball->mmio_base + TBCNTR)) {
  53. x = (TBCNTR_XP(tbcntr) - TBCNTR_XM(tbcntr)) / 2;
  54. y = (TBCNTR_YP(tbcntr) - TBCNTR_YM(tbcntr)) / 2;
  55. input_report_rel(input, REL_X, x);
  56. input_report_rel(input, REL_Y, y);
  57. input_sync(input);
  58. }
  59. __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
  60. __raw_writel(0, trkball->mmio_base + TBSBC);
  61. return IRQ_HANDLED;
  62. }
  63. /* For TBCR, we need to wait for a while to make sure it has been modified. */
  64. static int write_tbcr(struct pxa930_trkball *trkball, int v)
  65. {
  66. int i = 100;
  67. __raw_writel(v, trkball->mmio_base + TBCR);
  68. while (--i) {
  69. if (__raw_readl(trkball->mmio_base + TBCR) == v)
  70. break;
  71. msleep(1);
  72. }
  73. if (i == 0) {
  74. pr_err("%s: timed out writing TBCR(%x)!\n", __func__, v);
  75. return -ETIMEDOUT;
  76. }
  77. return 0;
  78. }
  79. static void pxa930_trkball_config(struct pxa930_trkball *trkball)
  80. {
  81. uint32_t tbcr;
  82. /* According to spec, need to write the filters of x,y to 0xf first! */
  83. tbcr = __raw_readl(trkball->mmio_base + TBCR);
  84. write_tbcr(trkball, tbcr | TBCR_X_FLT(0xf) | TBCR_Y_FLT(0xf));
  85. write_tbcr(trkball, TBCR_X_FLT(trkball->pdata->x_filter) |
  86. TBCR_Y_FLT(trkball->pdata->y_filter));
  87. /* According to spec, set TBCR_TBRST first, before clearing it! */
  88. tbcr = __raw_readl(trkball->mmio_base + TBCR);
  89. write_tbcr(trkball, tbcr | TBCR_TBRST);
  90. write_tbcr(trkball, tbcr & ~TBCR_TBRST);
  91. __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
  92. __raw_writel(0, trkball->mmio_base + TBSBC);
  93. pr_debug("%s: final TBCR=%x!\n", __func__,
  94. __raw_readl(trkball->mmio_base + TBCR));
  95. }
  96. static int pxa930_trkball_open(struct input_dev *dev)
  97. {
  98. struct pxa930_trkball *trkball = input_get_drvdata(dev);
  99. pxa930_trkball_config(trkball);
  100. return 0;
  101. }
  102. static void pxa930_trkball_disable(struct pxa930_trkball *trkball)
  103. {
  104. uint32_t tbcr = __raw_readl(trkball->mmio_base + TBCR);
  105. /* Held in reset, gate the 32-KHz input clock off */
  106. write_tbcr(trkball, tbcr | TBCR_TBRST);
  107. }
  108. static void pxa930_trkball_close(struct input_dev *dev)
  109. {
  110. struct pxa930_trkball *trkball = input_get_drvdata(dev);
  111. pxa930_trkball_disable(trkball);
  112. }
  113. static int __devinit pxa930_trkball_probe(struct platform_device *pdev)
  114. {
  115. struct pxa930_trkball *trkball;
  116. struct input_dev *input;
  117. struct resource *res;
  118. int irq, error;
  119. irq = platform_get_irq(pdev, 0);
  120. if (irq < 0) {
  121. dev_err(&pdev->dev, "failed to get trkball irq\n");
  122. return -ENXIO;
  123. }
  124. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  125. if (!res) {
  126. dev_err(&pdev->dev, "failed to get register memory\n");
  127. return -ENXIO;
  128. }
  129. trkball = kzalloc(sizeof(struct pxa930_trkball), GFP_KERNEL);
  130. if (!trkball)
  131. return -ENOMEM;
  132. trkball->pdata = pdev->dev.platform_data;
  133. if (!trkball->pdata) {
  134. dev_err(&pdev->dev, "no platform data defined\n");
  135. error = -EINVAL;
  136. goto failed;
  137. }
  138. trkball->mmio_base = ioremap_nocache(res->start, resource_size(res));
  139. if (!trkball->mmio_base) {
  140. dev_err(&pdev->dev, "failed to ioremap registers\n");
  141. error = -ENXIO;
  142. goto failed;
  143. }
  144. /* held the module in reset, will be enabled in open() */
  145. pxa930_trkball_disable(trkball);
  146. error = request_irq(irq, pxa930_trkball_interrupt, IRQF_DISABLED,
  147. pdev->name, trkball);
  148. if (error) {
  149. dev_err(&pdev->dev, "failed to request irq: %d\n", error);
  150. goto failed_free_io;
  151. }
  152. platform_set_drvdata(pdev, trkball);
  153. input = input_allocate_device();
  154. if (!input) {
  155. dev_err(&pdev->dev, "failed to allocate input device\n");
  156. error = -ENOMEM;
  157. goto failed_free_irq;
  158. }
  159. input->name = pdev->name;
  160. input->id.bustype = BUS_HOST;
  161. input->open = pxa930_trkball_open;
  162. input->close = pxa930_trkball_close;
  163. input->dev.parent = &pdev->dev;
  164. input_set_drvdata(input, trkball);
  165. trkball->input = input;
  166. input_set_capability(input, EV_REL, REL_X);
  167. input_set_capability(input, EV_REL, REL_Y);
  168. error = input_register_device(input);
  169. if (error) {
  170. dev_err(&pdev->dev, "unable to register input device\n");
  171. goto failed_free_input;
  172. }
  173. return 0;
  174. failed_free_input:
  175. input_free_device(input);
  176. failed_free_irq:
  177. free_irq(irq, trkball);
  178. failed_free_io:
  179. iounmap(trkball->mmio_base);
  180. failed:
  181. kfree(trkball);
  182. return error;
  183. }
  184. static int __devexit pxa930_trkball_remove(struct platform_device *pdev)
  185. {
  186. struct pxa930_trkball *trkball = platform_get_drvdata(pdev);
  187. int irq = platform_get_irq(pdev, 0);
  188. input_unregister_device(trkball->input);
  189. free_irq(irq, trkball);
  190. iounmap(trkball->mmio_base);
  191. kfree(trkball);
  192. return 0;
  193. }
  194. static struct platform_driver pxa930_trkball_driver = {
  195. .driver = {
  196. .name = "pxa930-trkball",
  197. },
  198. .probe = pxa930_trkball_probe,
  199. .remove = __devexit_p(pxa930_trkball_remove),
  200. };
  201. static int __init pxa930_trkball_init(void)
  202. {
  203. return platform_driver_register(&pxa930_trkball_driver);
  204. }
  205. static void __exit pxa930_trkball_exit(void)
  206. {
  207. platform_driver_unregister(&pxa930_trkball_driver);
  208. }
  209. module_init(pxa930_trkball_init);
  210. module_exit(pxa930_trkball_exit);
  211. MODULE_AUTHOR("Yong Yao <yaoyong@marvell.com>");
  212. MODULE_DESCRIPTION("PXA930 Trackball Mouse Driver");
  213. MODULE_LICENSE("GPL");