gpio_mouse.c 4.6 KB

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