pxa930_trkball.c 6.2 KB

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