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

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