gpio_decoder.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2016 Texas Instruments Incorporated - http://www.ti.com/
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation version 2.
  7. *
  8. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  9. * kind, whether express or implied; without even the implied warranty
  10. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * A generic driver to read multiple gpio lines and translate the
  14. * encoded numeric value into an input event.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/input.h>
  19. #include <linux/input-polldev.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/platform_device.h>
  24. struct gpio_decoder {
  25. struct input_polled_dev *poll_dev;
  26. struct gpio_descs *input_gpios;
  27. struct device *dev;
  28. u32 axis;
  29. u32 last_stable;
  30. };
  31. static int gpio_decoder_get_gpios_state(struct gpio_decoder *decoder)
  32. {
  33. struct gpio_descs *gpios = decoder->input_gpios;
  34. unsigned int ret = 0;
  35. int i, val;
  36. for (i = 0; i < gpios->ndescs; i++) {
  37. val = gpiod_get_value_cansleep(gpios->desc[i]);
  38. if (val < 0) {
  39. dev_err(decoder->dev,
  40. "Error reading gpio %d: %d\n",
  41. desc_to_gpio(gpios->desc[i]), val);
  42. return val;
  43. }
  44. val = !!val;
  45. ret = (ret << 1) | val;
  46. }
  47. return ret;
  48. }
  49. static void gpio_decoder_poll_gpios(struct input_polled_dev *poll_dev)
  50. {
  51. struct gpio_decoder *decoder = poll_dev->private;
  52. int state;
  53. state = gpio_decoder_get_gpios_state(decoder);
  54. if (state >= 0 && state != decoder->last_stable) {
  55. input_report_abs(poll_dev->input, decoder->axis, state);
  56. input_sync(poll_dev->input);
  57. decoder->last_stable = state;
  58. }
  59. }
  60. static int gpio_decoder_probe(struct platform_device *pdev)
  61. {
  62. struct device *dev = &pdev->dev;
  63. struct gpio_decoder *decoder;
  64. struct input_polled_dev *poll_dev;
  65. u32 max;
  66. int err;
  67. decoder = devm_kzalloc(dev, sizeof(struct gpio_decoder), GFP_KERNEL);
  68. if (!decoder)
  69. return -ENOMEM;
  70. device_property_read_u32(dev, "linux,axis", &decoder->axis);
  71. decoder->input_gpios = devm_gpiod_get_array(dev, NULL, GPIOD_IN);
  72. if (IS_ERR(decoder->input_gpios)) {
  73. dev_err(dev, "unable to acquire input gpios\n");
  74. return PTR_ERR(decoder->input_gpios);
  75. }
  76. if (decoder->input_gpios->ndescs < 2) {
  77. dev_err(dev, "not enough gpios found\n");
  78. return -EINVAL;
  79. }
  80. if (device_property_read_u32(dev, "decoder-max-value", &max))
  81. max = (1U << decoder->input_gpios->ndescs) - 1;
  82. decoder->dev = dev;
  83. poll_dev = devm_input_allocate_polled_device(decoder->dev);
  84. if (!poll_dev)
  85. return -ENOMEM;
  86. poll_dev->private = decoder;
  87. poll_dev->poll = gpio_decoder_poll_gpios;
  88. decoder->poll_dev = poll_dev;
  89. poll_dev->input->name = pdev->name;
  90. poll_dev->input->id.bustype = BUS_HOST;
  91. input_set_abs_params(poll_dev->input, decoder->axis, 0, max, 0, 0);
  92. err = input_register_polled_device(poll_dev);
  93. if (err) {
  94. dev_err(dev, "failed to register polled device\n");
  95. return err;
  96. }
  97. platform_set_drvdata(pdev, decoder);
  98. return 0;
  99. }
  100. #ifdef CONFIG_OF
  101. static const struct of_device_id gpio_decoder_of_match[] = {
  102. { .compatible = "gpio-decoder", },
  103. { },
  104. };
  105. MODULE_DEVICE_TABLE(of, gpio_decoder_of_match);
  106. #endif
  107. static struct platform_driver gpio_decoder_driver = {
  108. .probe = gpio_decoder_probe,
  109. .driver = {
  110. .name = "gpio-decoder",
  111. .of_match_table = of_match_ptr(gpio_decoder_of_match),
  112. }
  113. };
  114. module_platform_driver(gpio_decoder_driver);
  115. MODULE_DESCRIPTION("GPIO decoder input driver");
  116. MODULE_AUTHOR("Vignesh R <vigneshr@ti.com>");
  117. MODULE_LICENSE("GPL v2");