pxa930_trkball.c 6.3 KB

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