gpio_mouse.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for simulating a mouse on GPIO lines.
  4. *
  5. * Copyright (C) 2007 Atmel Corporation
  6. * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/input.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/property.h>
  13. #include <linux/of.h>
  14. /**
  15. * struct gpio_mouse
  16. * @scan_ms: the scan interval in milliseconds.
  17. * @up: GPIO line for up value.
  18. * @down: GPIO line for down value.
  19. * @left: GPIO line for left value.
  20. * @right: GPIO line for right value.
  21. * @bleft: GPIO line for left button.
  22. * @bmiddle: GPIO line for middle button.
  23. * @bright: GPIO line for right button.
  24. *
  25. * This struct must be added to the platform_device in the board code.
  26. * It is used by the gpio_mouse driver to setup GPIO lines and to
  27. * calculate mouse movement.
  28. */
  29. struct gpio_mouse {
  30. u32 scan_ms;
  31. struct gpio_desc *up;
  32. struct gpio_desc *down;
  33. struct gpio_desc *left;
  34. struct gpio_desc *right;
  35. struct gpio_desc *bleft;
  36. struct gpio_desc *bmiddle;
  37. struct gpio_desc *bright;
  38. };
  39. /*
  40. * Timer function which is run every scan_ms ms when the device is opened.
  41. * The dev input variable is set to the the input_dev pointer.
  42. */
  43. static void gpio_mouse_scan(struct input_dev *input)
  44. {
  45. struct gpio_mouse *gpio = input_get_drvdata(input);
  46. int x, y;
  47. if (gpio->bleft)
  48. input_report_key(input, BTN_LEFT,
  49. gpiod_get_value(gpio->bleft));
  50. if (gpio->bmiddle)
  51. input_report_key(input, BTN_MIDDLE,
  52. gpiod_get_value(gpio->bmiddle));
  53. if (gpio->bright)
  54. input_report_key(input, BTN_RIGHT,
  55. gpiod_get_value(gpio->bright));
  56. x = gpiod_get_value(gpio->right) - gpiod_get_value(gpio->left);
  57. y = gpiod_get_value(gpio->down) - gpiod_get_value(gpio->up);
  58. input_report_rel(input, REL_X, x);
  59. input_report_rel(input, REL_Y, y);
  60. input_sync(input);
  61. }
  62. static int gpio_mouse_probe(struct platform_device *pdev)
  63. {
  64. struct device *dev = &pdev->dev;
  65. struct gpio_mouse *gmouse;
  66. struct input_dev *input;
  67. int error;
  68. gmouse = devm_kzalloc(dev, sizeof(*gmouse), GFP_KERNEL);
  69. if (!gmouse)
  70. return -ENOMEM;
  71. /* Assign some default scanning time */
  72. error = device_property_read_u32(dev, "scan-interval-ms",
  73. &gmouse->scan_ms);
  74. if (error || gmouse->scan_ms == 0) {
  75. dev_warn(dev, "invalid scan time, set to 50 ms\n");
  76. gmouse->scan_ms = 50;
  77. }
  78. gmouse->up = devm_gpiod_get(dev, "up", GPIOD_IN);
  79. if (IS_ERR(gmouse->up))
  80. return PTR_ERR(gmouse->up);
  81. gmouse->down = devm_gpiod_get(dev, "down", GPIOD_IN);
  82. if (IS_ERR(gmouse->down))
  83. return PTR_ERR(gmouse->down);
  84. gmouse->left = devm_gpiod_get(dev, "left", GPIOD_IN);
  85. if (IS_ERR(gmouse->left))
  86. return PTR_ERR(gmouse->left);
  87. gmouse->right = devm_gpiod_get(dev, "right", GPIOD_IN);
  88. if (IS_ERR(gmouse->right))
  89. return PTR_ERR(gmouse->right);
  90. gmouse->bleft = devm_gpiod_get_optional(dev, "button-left", GPIOD_IN);
  91. if (IS_ERR(gmouse->bleft))
  92. return PTR_ERR(gmouse->bleft);
  93. gmouse->bmiddle = devm_gpiod_get_optional(dev, "button-middle",
  94. GPIOD_IN);
  95. if (IS_ERR(gmouse->bmiddle))
  96. return PTR_ERR(gmouse->bmiddle);
  97. gmouse->bright = devm_gpiod_get_optional(dev, "button-right",
  98. GPIOD_IN);
  99. if (IS_ERR(gmouse->bright))
  100. return PTR_ERR(gmouse->bright);
  101. input = devm_input_allocate_device(dev);
  102. if (!input)
  103. return -ENOMEM;
  104. input->name = pdev->name;
  105. input->id.bustype = BUS_HOST;
  106. input_set_drvdata(input, gmouse);
  107. input_set_capability(input, EV_REL, REL_X);
  108. input_set_capability(input, EV_REL, REL_Y);
  109. if (gmouse->bleft)
  110. input_set_capability(input, EV_KEY, BTN_LEFT);
  111. if (gmouse->bmiddle)
  112. input_set_capability(input, EV_KEY, BTN_MIDDLE);
  113. if (gmouse->bright)
  114. input_set_capability(input, EV_KEY, BTN_RIGHT);
  115. error = input_setup_polling(input, gpio_mouse_scan);
  116. if (error)
  117. return error;
  118. input_set_poll_interval(input, gmouse->scan_ms);
  119. error = input_register_device(input);
  120. if (error) {
  121. dev_err(dev, "could not register input device\n");
  122. return error;
  123. }
  124. dev_dbg(dev, "%d ms scan time, buttons: %s%s%s\n",
  125. gmouse->scan_ms,
  126. gmouse->bleft ? "" : "left ",
  127. gmouse->bmiddle ? "" : "middle ",
  128. gmouse->bright ? "" : "right");
  129. return 0;
  130. }
  131. static const struct of_device_id gpio_mouse_of_match[] = {
  132. { .compatible = "gpio-mouse", },
  133. { },
  134. };
  135. MODULE_DEVICE_TABLE(of, gpio_mouse_of_match);
  136. static struct platform_driver gpio_mouse_device_driver = {
  137. .probe = gpio_mouse_probe,
  138. .driver = {
  139. .name = "gpio_mouse",
  140. .of_match_table = gpio_mouse_of_match,
  141. }
  142. };
  143. module_platform_driver(gpio_mouse_device_driver);
  144. MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
  145. MODULE_DESCRIPTION("GPIO mouse driver");
  146. MODULE_LICENSE("GPL");
  147. MODULE_ALIAS("platform:gpio_mouse"); /* work with hotplug and coldplug */