2
0

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. @@ -1030,3 +1032,72 @@ 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. + unsigned flags = 0;
  49. + enum of_gpio_flags of_flags;
  50. +
  51. + gpio = of_get_gpio_flags(cnp, i, &of_flags);
  52. + if (!gpio_is_valid(gpio))
  53. + return gpio;
  54. +
  55. + if (of_flags == OF_GPIO_ACTIVE_LOW)
  56. + flags |= GPIOF_ACTIVE_LOW;
  57. +
  58. + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
  59. + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  60. + else
  61. + flags |= GPIOF_IN;
  62. +
  63. + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
  64. + continue;
  65. +
  66. + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
  67. + gpio_export_with_name(gpio, dmc, name);
  68. + nb++;
  69. + }
  70. + }
  71. +
  72. + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
  73. +
  74. + return 0;
  75. +}
  76. +
  77. +static struct platform_driver gpio_export_driver = {
  78. + .driver = {
  79. + .name = "gpio-export",
  80. + .owner = THIS_MODULE,
  81. + .of_match_table = of_match_ptr(gpio_export_ids),
  82. + },
  83. + .probe = of_gpio_export_probe,
  84. +};
  85. +
  86. +module_platform_driver(gpio_export_driver);
  87. +
  88. +#endif
  89. --- a/include/asm-generic/gpio.h
  90. +++ b/include/asm-generic/gpio.h
  91. @@ -125,6 +125,12 @@ static inline int gpio_export(unsigned g
  92. return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  93. }
  94. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  95. +static inline int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
  96. +{
  97. + return __gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
  98. +}
  99. +
  100. static inline int gpio_export_link(struct device *dev, const char *name,
  101. unsigned gpio)
  102. {
  103. --- a/include/linux/gpio/consumer.h
  104. +++ b/include/linux/gpio/consumer.h
  105. @@ -715,6 +715,7 @@ static inline struct gpio_desc *acpi_get
  106. #if IS_ENABLED(CONFIG_GPIOLIB) && IS_ENABLED(CONFIG_GPIO_SYSFS)
  107. +int _gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  108. int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  109. int gpiod_export_link(struct device *dev, const char *name,
  110. struct gpio_desc *desc);
  111. @@ -722,6 +723,13 @@ void gpiod_unexport(struct gpio_desc *de
  112. #else /* CONFIG_GPIOLIB && CONFIG_GPIO_SYSFS */
  113. +static inline int _gpiod_export(struct gpio_desc *desc,
  114. + bool direction_may_change,
  115. + const char *name)
  116. +{
  117. + return -ENOSYS;
  118. +}
  119. +
  120. static inline int gpiod_export(struct gpio_desc *desc,
  121. bool direction_may_change)
  122. {
  123. --- a/drivers/gpio/gpiolib-sysfs.c
  124. +++ b/drivers/gpio/gpiolib-sysfs.c
  125. @@ -544,7 +544,7 @@ static struct class gpio_class = {
  126. *
  127. * Returns zero on success, else an error.
  128. */
  129. -int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  130. +int __gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
  131. {
  132. struct gpio_chip *chip;
  133. struct gpio_device *gdev;
  134. @@ -606,6 +606,8 @@ int gpiod_export(struct gpio_desc *desc,
  135. offset = gpio_chip_hwgpio(desc);
  136. if (chip->names && chip->names[offset])
  137. ioname = chip->names[offset];
  138. + if (name)
  139. + ioname = name;
  140. dev = device_create_with_groups(&gpio_class, &gdev->dev,
  141. MKDEV(0, 0), data, gpio_groups,
  142. @@ -627,6 +629,12 @@ err_unlock:
  143. gpiod_dbg(desc, "%s: status %d\n", __func__, status);
  144. return status;
  145. }
  146. +EXPORT_SYMBOL_GPL(__gpiod_export);
  147. +
  148. +int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  149. +{
  150. + return __gpiod_export(desc, direction_may_change, NULL);
  151. +}
  152. EXPORT_SYMBOL_GPL(gpiod_export);
  153. static int match_export(struct device *dev, const void *desc)