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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. @@ -19,6 +19,8 @@
  9. #include <linux/pinctrl/pinctrl.h>
  10. #include <linux/slab.h>
  11. #include <linux/gpio/machine.h>
  12. +#include <linux/init.h>
  13. +#include <linux/platform_device.h>
  14. #include "gpiolib.h"
  15. #include "gpiolib-of.h"
  16. @@ -1059,3 +1061,100 @@ void of_gpio_dev_init(struct gpio_chip *
  17. else
  18. gc->of_node = gdev->dev.of_node;
  19. }
  20. +
  21. +#ifdef CONFIG_GPIO_SYSFS
  22. +
  23. +static struct of_device_id gpio_export_ids[] = {
  24. + { .compatible = "gpio-export" },
  25. + { /* sentinel */ }
  26. +};
  27. +
  28. +static int of_gpio_export_probe(struct platform_device *pdev)
  29. +{
  30. + struct device_node *np = pdev->dev.of_node;
  31. + struct device_node *cnp;
  32. + u32 val;
  33. + int nb = 0;
  34. +
  35. + for_each_child_of_node(np, cnp) {
  36. + const char *name = NULL;
  37. + int gpio;
  38. + bool dmc;
  39. + int max_gpio = 1;
  40. + int i;
  41. +
  42. + of_property_read_string(cnp, "gpio-export,name", &name);
  43. +
  44. + if (!name)
  45. + max_gpio = of_gpio_count(cnp);
  46. +
  47. + for (i = 0; i < max_gpio; i++) {
  48. + struct gpio_desc *desc;
  49. + unsigned flags = 0;
  50. + enum of_gpio_flags of_flags;
  51. +
  52. + gpio = of_get_gpio_flags(cnp, i, &of_flags);
  53. + if (!gpio_is_valid(gpio))
  54. + return gpio;
  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. + if (of_flags & OF_GPIO_SINGLE_ENDED) {
  61. + /*
  62. + * As gpiod_direction_output_raw() is used, we
  63. + * need to emulate open drain or open source here.
  64. + */
  65. + if (of_flags & OF_GPIO_OPEN_DRAIN) {
  66. + flags |= GPIOF_OPEN_DRAIN;
  67. + flags |= val ? GPIOF_IN : GPIOF_OUT_INIT_LOW;
  68. + } else {
  69. + flags |= GPIOF_OPEN_SOURCE;
  70. + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_IN;
  71. + }
  72. + } else {
  73. + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  74. + }
  75. + } else {
  76. + flags |= GPIOF_IN;
  77. + }
  78. +
  79. + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
  80. + continue;
  81. +
  82. + /*
  83. + * When emulating open-source or open-drain functionalities by not
  84. + * actively driving the line (setting mode to input) we still need to
  85. + * set the IS_OUT flag or otherwise we won't be able to set the line
  86. + * value anymore.
  87. + */
  88. + if ((flags & GPIOF_IN) &&
  89. + ((flags & GPIOF_OPEN_DRAIN) || (flags & GPIOF_OPEN_SOURCE))) {
  90. + desc = gpio_to_desc(gpio);
  91. + set_bit(FLAG_IS_OUT, &desc->flags);
  92. + }
  93. +
  94. + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
  95. + gpio_export_with_name(gpio, dmc, name);
  96. + nb++;
  97. + }
  98. + }
  99. +
  100. + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
  101. +
  102. + return 0;
  103. +}
  104. +
  105. +static struct platform_driver gpio_export_driver = {
  106. + .driver = {
  107. + .name = "gpio-export",
  108. + .owner = THIS_MODULE,
  109. + .of_match_table = of_match_ptr(gpio_export_ids),
  110. + },
  111. + .probe = of_gpio_export_probe,
  112. +};
  113. +
  114. +module_platform_driver(gpio_export_driver);
  115. +
  116. +#endif
  117. --- a/include/asm-generic/gpio.h
  118. +++ b/include/asm-generic/gpio.h
  119. @@ -125,6 +125,12 @@ static inline int gpio_export(unsigned g
  120. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  121. }
  122. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  123. +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
  124. +{
  125. + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
  126. +}
  127. +
  128. static inline int gpio_export_link(struct device *dev, const char *name,
  129. unsigned gpio)
  130. {
  131. --- a/include/linux/gpio/consumer.h
  132. +++ b/include/linux/gpio/consumer.h
  133. @@ -712,6 +712,7 @@ static inline void devm_acpi_dev_remove_
  134. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  135. +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  136. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  137. int gpiod_export_link(struct device *dev, const char *name,
  138. struct gpio_desc *desc);
  139. @@ -719,6 +720,13 @@ void gpiod_unexport(struct gpio_desc *de
  140. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  141. +static inline int _gpiod_export(struct gpio_desc *desc,
  142. + bool direction_may_change,
  143. + const char *name)
  144. +{
  145. + return -ENOSYS;
  146. +}
  147. +
  148. static inline int gpiod_export(struct gpio_desc *desc,
  149. bool direction_may_change)
  150. {
  151. --- a/drivers/gpio/gpiolib-sysfs.c
  152. +++ b/drivers/gpio/gpiolib-sysfs.c
  153. @@ -564,7 +564,7 @@ static struct class gpio_class = {
  154. *
  155. * Returns zero on success, else an error.
  156. */
  157. -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  158. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
  159. {
  160. struct gpio_chip *chip;
  161. struct gpio_device *gdev;
  162. @@ -626,6 +626,8 @@ int gpiod_export(struct gpio_desc *desc,
  163. offset = gpio_chip_hwgpio(desc);
  164. if (chip->names && chip->names[offset])
  165. ioname = chip->names[offset];
  166. + if (name)
  167. + ioname = name;
  168. dev = device_create_with_groups(&gpio_class, &gdev->dev,
  169. MKDEV(0, 0), data, gpio_groups,
  170. @@ -647,6 +649,12 @@ err_unlock:
  171. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  172. return status;
  173. }
  174. +EXPORT_SYMBOL_GPL(__gpiod_export);
  175. +
  176. +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  177. +{
  178. + return __gpiod_export(desc, direction_may_change, NULL);
  179. +}
  180. EXPORT_SYMBOL_GPL(gpiod_export);
  181. static int match_export(struct device *dev, const void *desc)