gpio_decoder.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  4. *
  5. * A generic driver to read multiple gpio lines and translate the
  6. * encoded numeric value into an input event.
  7. */
  8. #include <linux/device.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/input.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. struct gpio_decoder {
  16. struct gpio_descs *input_gpios;
  17. struct device *dev;
  18. u32 axis;
  19. u32 last_stable;
  20. };
  21. static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
  22. {
  23. struct gpio_descs *gpios = decoder->input_gpios;
  24. unsigned int ret = 0;
  25. int i, val;
  26. for (i = 0; i < gpios->ndescs; i++) {
  27. val = gpiod_get_value_cansleep(gpios->desc[i]);
  28. if (val < 0) {
  29. dev_err(decoder->dev,
  30. "Error reading gpio %d: %d\n",
  31. desc_to_gpio(gpios->desc[i]), val);
  32. return val;
  33. }
  34. val = !!val;
  35. ret = (ret << 1) | val;
  36. }
  37. return ret;
  38. }
  39. static void gpio_decoder_poll_gpios(struct input_dev *input)
  40. {
  41. struct gpio_decoder *decoder = input_get_drvdata(input);
  42. int state;
  43. state = gpio_decoder_get_gpios_state(decoder);
  44. if (state >= 0 && state != decoder->last_stable) {
  45. input_report_abs(input, decoder->axis, state);
  46. input_sync(input);
  47. decoder->last_stable = state;
  48. }
  49. }
  50. static int gpio_decoder_probe(struct platform_device *pdev)
  51. {
  52. struct device *dev = &pdev->dev;
  53. struct gpio_decoder *decoder;
  54. struct input_dev *input;
  55. u32 max;
  56. int err;
  57. decoder = devm_kzalloc(dev, sizeof(*decoder), GFP_KERNEL);
  58. if (!decoder)
  59. return -ENOMEM;
  60. decoder->dev = dev;
  61. device_property_read_u32(dev, "linux,axis", &decoder->axis);
  62. decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
  63. if (IS_ERR(decoder->input_gpios)) {
  64. dev_err(dev, "unable to acquire input gpios\n");
  65. return PTR_ERR(decoder->input_gpios);
  66. }
  67. if (decoder->input_gpios->ndescs < 2) {
  68. dev_err(dev, "not enough gpios found\n");
  69. return -EINVAL;
  70. }
  71. if (device_property_read_u32(dev, "decoder-max-value", &max))
  72. max = (1U << decoder->input_gpios->ndescs) - 1;
  73. input = devm_input_allocate_device(dev);
  74. if (!input)
  75. return -ENOMEM;
  76. input_set_drvdata(input, decoder);
  77. input->name = pdev->name;
  78. input->id.bustype = BUS_HOST;
  79. input_set_abs_params(input, decoder->axis, 0, max, 0, 0);
  80. err = input_setup_polling(input, gpio_decoder_poll_gpios);
  81. if (err) {
  82. dev_err(dev, "failed to set up polling\n");
  83. return err;
  84. }
  85. err = input_register_device(input);
  86. if (err) {
  87. dev_err(dev, "failed to register input device\n");
  88. return err;
  89. }
  90. return 0;
  91. }
  92. #ifdef CONFIG_OF
  93. static const struct of_device_id gpio_decoder_of_match[] = {
  94. { .compatible = "gpio-decoder", },
  95. { },
  96. };
  97. MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
  98. #endif
  99. static struct platform_driver gpio_decoder_driver = {
  100. .probe = gpio_decoder_probe,
  101. .driver = {
  102. .name = "gpio-decoder",
  103. .of_match_table = of_match_ptr(gpio_decoder_of_match),
  104. }
  105. };
  106. module_platform_driver(gpio_decoder_driver);
  107. MODULE_DESCRIPTION("GPIO decoder input driver");
  108. MODULE_AUTHOR("Vignesh R <[email protected]>");
  109. MODULE_LICENSE("GPL v2");