2
0

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

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