pxa930_trkball.c 6.0 KB

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