gpio_mouse.c 4.4 KB

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