gpio_mouse.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Driver for simulating a mouse on GPIO lines.
  3. *
  4. * Copyright (C) 2007 Atmel Corporation
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/version.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/input-polldev.h>
  15. #include <linux/gpio_mouse.h>
  16. #include <asm/gpio.h>
  17. /*
  18. * Timer function which is run every scan_ms ms when the device is opened.
  19. * The dev input varaible is set to the the input_dev pointer.
  20. */
  21. static void gpio_mouse_scan(struct input_polled_dev *dev)
  22. {
  23. struct gpio_mouse_platform_data *gpio = dev->private;
  24. struct input_dev *input = dev->input;
  25. int x, y;
  26. if (gpio->bleft >= 0)
  27. input_report_key(input, BTN_LEFT,
  28. gpio_get_value(gpio->bleft) ^ gpio->polarity);
  29. if (gpio->bmiddle >= 0)
  30. input_report_key(input, BTN_MIDDLE,
  31. gpio_get_value(gpio->bmiddle) ^ gpio->polarity);
  32. if (gpio->bright >= 0)
  33. input_report_key(input, BTN_RIGHT,
  34. gpio_get_value(gpio->bright) ^ gpio->polarity);
  35. x = (gpio_get_value(gpio->right) ^ gpio->polarity)
  36. - (gpio_get_value(gpio->left) ^ gpio->polarity);
  37. y = (gpio_get_value(gpio->down) ^ gpio->polarity)
  38. - (gpio_get_value(gpio->up) ^ gpio->polarity);
  39. input_report_rel(input, REL_X, x);
  40. input_report_rel(input, REL_Y, y);
  41. input_sync(input);
  42. }
  43. static int __init gpio_mouse_probe(struct platform_device *pdev)
  44. {
  45. struct gpio_mouse_platform_data *pdata = pdev->dev.platform_data;
  46. struct input_polled_dev *input_poll;
  47. struct input_dev *input;
  48. int pin, i;
  49. int error;
  50. if (!pdata) {
  51. dev_err(&pdev->dev, "no platform data\n");
  52. error = -ENXIO;
  53. goto out;
  54. }
  55. if (pdata->scan_ms < 0) {
  56. dev_err(&pdev->dev, "invalid scan time\n");
  57. error = -EINVAL;
  58. goto out;
  59. }
  60. for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) {
  61. pin = pdata->pins[i];
  62. if (pin < 0) {
  63. if (i <= GPIO_MOUSE_PIN_RIGHT) {
  64. /* Mouse direction is required. */
  65. dev_err(&pdev->dev,
  66. "missing GPIO for directions\n");
  67. error = -EINVAL;
  68. goto out_free_gpios;
  69. }
  70. if (i == GPIO_MOUSE_PIN_BLEFT)
  71. dev_dbg(&pdev->dev, "no left button defined\n");
  72. } else {
  73. error = gpio_request(pin, "gpio_mouse");
  74. if (error) {
  75. dev_err(&pdev->dev, "fail %d pin (%d idx)\n",
  76. pin, i);
  77. goto out_free_gpios;
  78. }
  79. gpio_direction_input(pin);
  80. }
  81. }
  82. input_poll = input_allocate_polled_device();
  83. if (!input_poll) {
  84. dev_err(&pdev->dev, "not enough memory for input device\n");
  85. error = -ENOMEM;
  86. goto out_free_gpios;
  87. }
  88. platform_set_drvdata(pdev, input_poll);
  89. /* set input-polldev handlers */
  90. input_poll->private = pdata;
  91. input_poll->poll = gpio_mouse_scan;
  92. input_poll->poll_interval = pdata->scan_ms;
  93. input = input_poll->input;
  94. input->name = pdev->name;
  95. input->id.bustype = BUS_HOST;
  96. input->dev.parent = &pdev->dev;
  97. input_set_capability(input, EV_REL, REL_X);
  98. input_set_capability(input, EV_REL, REL_Y);
  99. if (pdata->bleft >= 0)
  100. input_set_capability(input, EV_KEY, BTN_LEFT);
  101. if (pdata->bmiddle >= 0)
  102. input_set_capability(input, EV_KEY, BTN_MIDDLE);
  103. if (pdata->bright >= 0)
  104. input_set_capability(input, EV_KEY, BTN_RIGHT);
  105. error = input_register_polled_device(input_poll);
  106. if (error) {
  107. dev_err(&pdev->dev, "could not register input device\n");
  108. goto out_free_polldev;
  109. }
  110. dev_dbg(&pdev->dev, "%d ms scan time, buttons: %s%s%s\n",
  111. pdata->scan_ms,
  112. pdata->bleft < 0 ? "" : "left ",
  113. pdata->bmiddle < 0 ? "" : "middle ",
  114. pdata->bright < 0 ? "" : "right");
  115. return 0;
  116. out_free_polldev:
  117. input_free_polled_device(input_poll);
  118. platform_set_drvdata(pdev, NULL);
  119. out_free_gpios:
  120. while (--i >= 0) {
  121. pin = pdata->pins[i];
  122. if (pin)
  123. gpio_free(pin);
  124. }
  125. out:
  126. return error;
  127. }
  128. static int __devexit gpio_mouse_remove(struct platform_device *pdev)
  129. {
  130. struct input_polled_dev *input = platform_get_drvdata(pdev);
  131. struct gpio_mouse_platform_data *pdata = input->private;
  132. int pin, i;
  133. input_unregister_polled_device(input);
  134. input_free_polled_device(input);
  135. for (i = 0; i < GPIO_MOUSE_PIN_MAX; i++) {
  136. pin = pdata->pins[i];
  137. if (pin >= 0)
  138. gpio_free(pin);
  139. }
  140. platform_set_drvdata(pdev, NULL);
  141. return 0;
  142. }
  143. struct platform_driver gpio_mouse_device_driver = {
  144. .remove = __devexit_p(gpio_mouse_remove),
  145. .driver = {
  146. .name = "gpio_mouse",
  147. }
  148. };
  149. static int __init gpio_mouse_init(void)
  150. {
  151. return platform_driver_probe(&gpio_mouse_device_driver,
  152. gpio_mouse_probe);
  153. }
  154. module_init(gpio_mouse_init);
  155. static void __exit gpio_mouse_exit(void)
  156. {
  157. platform_driver_unregister(&gpio_mouse_device_driver);
  158. }
  159. module_exit(gpio_mouse_exit);
  160. MODULE_AUTHOR("Hans-Christian Egtvedt <hcegtvedt@atmel.com>");
  161. MODULE_DESCRIPTION("GPIO mouse driver");
  162. MODULE_LICENSE("GPL");