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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. From cc809a441d8f2924f785eb863dfa6aef47a25b0b Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  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 <blogic@openwrt.org>
  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. @@ -22,6 +22,8 @@
  15. #include <linux/of_gpio.h>
  16. #include <linux/pinctrl/pinctrl.h>
  17. #include <linux/slab.h>
  18. +#include <linux/init.h>
  19. +#include <linux/platform_device.h>
  20. #include "gpiolib.h"
  21. @@ -316,3 +318,69 @@ void of_gpiochip_remove(struct gpio_chip
  22. gpiochip_remove_pin_ranges(chip);
  23. of_node_put(chip->of_node);
  24. }
  25. +
  26. +static struct of_device_id gpio_export_ids[] = {
  27. + { .compatible = "gpio-export" },
  28. + { /* sentinel */ }
  29. +};
  30. +
  31. +static int __init of_gpio_export_probe(struct platform_device *pdev)
  32. +{
  33. + struct device_node *np = pdev->dev.of_node;
  34. + struct device_node *cnp;
  35. + u32 val;
  36. + int nb = 0;
  37. +
  38. + for_each_child_of_node(np, cnp) {
  39. + const char *name = NULL;
  40. + int gpio;
  41. + bool dmc;
  42. + int max_gpio = 1;
  43. + int i;
  44. +
  45. + of_property_read_string(cnp, "gpio-export,name", &name);
  46. +
  47. + if (!name)
  48. + max_gpio = of_gpio_count(cnp);
  49. +
  50. + for (i = 0; i < max_gpio; i++) {
  51. + unsigned flags = 0;
  52. + enum of_gpio_flags of_flags;
  53. +
  54. + gpio = of_get_gpio_flags(cnp, i, &of_flags);
  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, 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. +};
  85. +
  86. +static int __init of_gpio_export_init(void)
  87. +{
  88. + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
  89. +}
  90. +device_initcall(of_gpio_export_init);
  91. --- a/include/asm-generic/gpio.h
  92. +++ b/include/asm-generic/gpio.h
  93. @@ -123,6 +123,12 @@ static inline int gpio_export(unsigned g
  94. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  95. }
  96. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  97. +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
  98. +{
  99. + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
  100. +}
  101. +
  102. static inline int gpio_export_link(struct device *dev, const char *name,
  103. unsigned gpio)
  104. {
  105. --- a/include/linux/gpio/consumer.h
  106. +++ b/include/linux/gpio/consumer.h
  107. @@ -323,6 +323,7 @@ static inline int desc_to_gpio(const str
  108. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  109. +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  110. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  111. int gpiod_export_link(struct device *dev, const char *name,
  112. struct gpio_desc *desc);
  113. @@ -331,6 +332,13 @@ void gpiod_unexport(struct gpio_desc *de
  114. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  115. +static inline int _gpiod_export(struct gpio_desc *desc,
  116. + bool direction_may_change,
  117. + const char *name)
  118. +{
  119. + return -ENOSYS;
  120. +}
  121. +
  122. static inline int gpiod_export(struct gpio_desc *desc,
  123. bool direction_may_change)
  124. {
  125. --- a/drivers/gpio/gpiolib-sysfs.c
  126. +++ b/drivers/gpio/gpiolib-sysfs.c
  127. @@ -517,7 +517,7 @@ static struct class gpio_class = {
  128. *
  129. * Returns zero on success, else an error.
  130. */
  131. -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  132. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
  133. {
  134. struct gpio_chip *chip;
  135. unsigned long flags;
  136. @@ -566,6 +566,8 @@ int gpiod_export(struct gpio_desc *desc,
  137. offset = gpio_chip_hwgpio(desc);
  138. if (desc->chip->names && desc->chip->names[offset])
  139. ioname = desc->chip->names[offset];
  140. + if (name)
  141. + ioname = name;
  142. dev = device_create_with_groups(&gpio_class, desc->chip->dev,
  143. MKDEV(0, 0), desc, gpio_groups,
  144. @@ -602,6 +604,12 @@ fail_unlock:
  145. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  146. return status;
  147. }
  148. +EXPORT_SYMBOL_GPL(__gpiod_export);
  149. +
  150. +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  151. +{
  152. + return __gpiod_export(desc, direction_may_change, NULL);
  153. +}
  154. EXPORT_SYMBOL_GPL(gpiod_export);
  155. static int match_export(struct device *dev, const void *data)