2
0

800-GPIO-add-named-gpio-exports.patch 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. From cc809a441d8f2924f785eb863dfa6aef47a25b0b Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Tue, 12 Aug 2014 20:49:27 +0200
  4. Subject: [PATCH 30/36] GPIO: add named gpio exports
  5. Signed-off-by: John Crispin <[email protected]>
  6. --- a/drivers/gpio/gpiolib-of.c
  7. +++ b/drivers/gpio/gpiolib-of.c
  8. @@ -21,6 +21,8 @@
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/gpio/machine.h>
  11. +#include <linux/init.h>
  12. +#include <linux/platform_device.h>
  13. #include "gpiolib.h"
  14. #include "gpiolib-of.h"
  15. @@ -1111,3 +1113,74 @@ void of_gpiochip_remove(struct gpio_chip
  16. {
  17. of_node_put(dev_of_node(&chip->gpiodev->dev));
  18. }
  19. +
  20. +#ifdef CONFIG_GPIO_SYSFS
  21. +
  22. +static struct of_device_id gpio_export_ids[] = {
  23. + { .compatible = "gpio-export" },
  24. + { /* sentinel */ }
  25. +};
  26. +
  27. +static int of_gpio_export_probe(struct platform_device *pdev)
  28. +{
  29. + struct device_node *np = pdev->dev.of_node;
  30. + struct device_node *cnp;
  31. + u32 val;
  32. + int nb = 0;
  33. +
  34. + for_each_child_of_node(np, cnp) {
  35. + const char *name = NULL;
  36. + int gpio;
  37. + bool dmc;
  38. + int max_gpio = 1;
  39. + int i;
  40. +
  41. + of_property_read_string(cnp, "gpio-export,name", &name);
  42. +
  43. + if (!name)
  44. + max_gpio = of_gpio_named_count(cnp, "gpios");
  45. +
  46. + for (i = 0; i < max_gpio; i++) {
  47. + struct gpio_desc *desc;
  48. + unsigned flags = 0;
  49. + enum of_gpio_flags of_flags;
  50. +
  51. + desc = of_get_named_gpiod_flags(cnp, "gpios", i, &of_flags);
  52. + if (IS_ERR(desc))
  53. + return PTR_ERR(desc);
  54. + gpio = desc_to_gpio(desc);
  55. +
  56. + if (of_flags & OF_GPIO_ACTIVE_LOW)
  57. + flags |= GPIOF_ACTIVE_LOW;
  58. +
  59. + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
  60. + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  61. + else
  62. + flags |= GPIOF_IN;
  63. +
  64. + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
  65. + continue;
  66. +
  67. + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
  68. + gpio_export_with_name(gpio_to_desc(gpio), dmc, name);
  69. + nb++;
  70. + }
  71. + }
  72. +
  73. + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
  74. +
  75. + return 0;
  76. +}
  77. +
  78. +static struct platform_driver gpio_export_driver = {
  79. + .driver = {
  80. + .name = "gpio-export",
  81. + .owner = THIS_MODULE,
  82. + .of_match_table = of_match_ptr(gpio_export_ids),
  83. + },
  84. + .probe = of_gpio_export_probe,
  85. +};
  86. +
  87. +module_platform_driver(gpio_export_driver);
  88. +
  89. +#endif
  90. --- a/include/linux/gpio/consumer.h
  91. +++ b/include/linux/gpio/consumer.h
  92. @@ -644,7 +644,10 @@ static inline struct gpio_desc *acpi_get
  93. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  94. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  95. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  96. +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
  97. + const char *name);
  98. int gpiod_export_link(struct device *dev, const char *name,
  99. struct gpio_desc *desc);
  100. void gpiod_unexport(struct gpio_desc *desc);
  101. @@ -653,11 +656,25 @@ void gpiod_unexport(struct gpio_desc *de
  102. #include <asm/errno.h>
  103. +static inline int __gpiod_export(struct gpio_desc *desc,
  104. + bool direction_may_change,
  105. + const char *name)
  106. +{
  107. + return -ENOSYS;
  108. +}
  109. +
  110. static inline int gpiod_export(struct gpio_desc *desc,
  111. bool direction_may_change)
  112. {
  113. return -ENOSYS;
  114. }
  115. +
  116. +static inline int gpio_export_with_name(struct gpio_desc *desc,
  117. + bool direction_may_change,
  118. + const char *name)
  119. +{
  120. + return -ENOSYS;
  121. +}
  122. static inline int gpiod_export_link(struct device *dev, const char *name,
  123. struct gpio_desc *desc)
  124. --- a/drivers/gpio/gpiolib-sysfs.c
  125. +++ b/drivers/gpio/gpiolib-sysfs.c
  126. @@ -557,7 +557,7 @@ static struct class gpio_class = {
  127. *
  128. * Returns zero on success, else an error.
  129. */
  130. -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  131. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
  132. {
  133. struct gpio_chip *chip;
  134. struct gpio_device *gdev;
  135. @@ -619,6 +619,8 @@ int gpiod_export(struct gpio_desc *desc,
  136. offset = gpio_chip_hwgpio(desc);
  137. if (chip->names && chip->names[offset])
  138. ioname = chip->names[offset];
  139. + if (name)
  140. + ioname = name;
  141. dev = device_create_with_groups(&gpio_class, &gdev->dev,
  142. MKDEV(0, 0), data, gpio_groups,
  143. @@ -640,8 +642,21 @@ err_unlock:
  144. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  145. return status;
  146. }
  147. +EXPORT_SYMBOL_GPL(__gpiod_export);
  148. +
  149. +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  150. +{
  151. + return __gpiod_export(desc, direction_may_change, NULL);
  152. +}
  153. EXPORT_SYMBOL_GPL(gpiod_export);
  154. +int gpio_export_with_name(struct gpio_desc *desc, bool direction_may_change,
  155. + const char *name)
  156. +{
  157. + return __gpiod_export(desc, direction_may_change, name);
  158. +}
  159. +EXPORT_SYMBOL_GPL(gpio_export_with_name);
  160. +
  161. static int match_export(struct device *dev, const void *desc)
  162. {
  163. struct gpiod_data *data = dev_get_drvdata(dev);