143-gpio-fix-device-tree-gpio-hogs-on-dual-role-gpio-pin.patch 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. From e058fa1969019c2f6705c53c4130e364a877d4e6 Mon Sep 17 00:00:00 2001
  2. From: Jonas Gorski <[email protected]>
  3. Date: Sun, 26 Nov 2017 12:07:31 +0100
  4. Subject: [PATCH] gpio: fix device tree gpio hogs on dual role gpio/pincontrol
  5. controllers
  6. For dual role gpio and pincontrol controller, the device registration
  7. path is often:
  8. pinctrl_register(...);
  9. gpiochip_add_data(...);
  10. gpiochip_add_pin_range(...);
  11. If the device tree node has any gpio-hogs, the code will try to apply them
  12. in the gpiochip_add_data step, but fail as they cannot be requested, as the
  13. ranges are missing. But we also cannot first add the pinranges, as the
  14. appropriate data structures are only initialized in gpiochip_add_data.
  15. To fix this, defer gpio-hogs to the time pin ranges get added instead of
  16. directly at chip request time, if the gpio-chip has a request method.
  17. Signed-off-by: Jonas Gorski <[email protected]>
  18. ---
  19. drivers/gpio/gpiolib-of.c | 20 +++++++++++++++-----
  20. drivers/gpio/gpiolib.c | 5 +++--
  21. drivers/gpio/gpiolib.h | 8 ++++++++
  22. 3 files changed, 26 insertions(+), 7 deletions(-)
  23. --- a/drivers/gpio/gpiolib-of.c
  24. +++ b/drivers/gpio/gpiolib-of.c
  25. @@ -646,23 +646,30 @@ static struct gpio_desc *of_parse_own_gp
  26. * of_gpiochip_add_hog - Add all hogs in a hog device node
  27. * @chip: gpio chip to act on
  28. * @hog: device node describing the hogs
  29. + * @start: first gpio to check
  30. + * @num: number of gpios to check
  31. *
  32. * Returns error if it fails otherwise 0 on success.
  33. */
  34. -static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog)
  35. +static int of_gpiochip_add_hog(struct gpio_chip *chip, struct device_node *hog,
  36. + unsigned int start, unsigned int num)
  37. {
  38. enum gpiod_flags dflags;
  39. struct gpio_desc *desc;
  40. unsigned long lflags;
  41. const char *name;
  42. unsigned int i;
  43. - int ret;
  44. + int ret, hwgpio;
  45. for (i = 0;; i++) {
  46. desc = of_parse_own_gpio(hog, chip, i, &name, &lflags, &dflags);
  47. if (IS_ERR(desc))
  48. break;
  49. + hwgpio = gpio_chip_hwgpio(desc);
  50. + if (hwgpio < start || hwgpio >= (start + num))
  51. + continue;
  52. +
  53. ret = gpiod_hog(desc, name, lflags, dflags);
  54. if (ret < 0)
  55. return ret;
  56. @@ -678,12 +685,15 @@ static int of_gpiochip_add_hog(struct gp
  57. /**
  58. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  59. * @chip: gpio chip to act on
  60. + * @start: first gpio to check
  61. + * @num: number of gpios to check
  62. *
  63. - * This is only used by of_gpiochip_add to request/set GPIO initial
  64. - * configuration.
  65. + * This is used by of_gpiochip_add, gpiochip_add_pingroup_range and
  66. + * gpiochip_add_pin_range to request/set GPIO initial configuration.
  67. * It returns error if it fails otherwise 0 on success.
  68. */
  69. -static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  70. +int of_gpiochip_scan_gpios(struct gpio_chip *chip, unsigned int start,
  71. + unsigned int num)
  72. {
  73. struct device_node *np;
  74. int ret;
  75. @@ -692,7 +702,7 @@ static int of_gpiochip_scan_gpios(struct
  76. if (!of_property_read_bool(np, "gpio-hog"))
  77. continue;
  78. - ret = of_gpiochip_add_hog(chip, np);
  79. + ret = of_gpiochip_add_hog(chip, np, start, num);
  80. if (ret < 0) {
  81. of_node_put(np);
  82. return ret;
  83. @@ -758,7 +768,7 @@ static int of_gpio_notify(struct notifie
  84. if (chip == NULL)
  85. return NOTIFY_OK; /* not for us */
  86. - ret = of_gpiochip_add_hog(chip, rd->dn);
  87. + ret = of_gpiochip_add_hog(chip, rd->dn, 0, chip->ngpio);
  88. if (ret < 0) {
  89. pr_err("%s: failed to add hogs for %pOF\n", __func__,
  90. rd->dn);
  91. @@ -1030,9 +1040,11 @@ int of_gpiochip_add(struct gpio_chip *ch
  92. of_node_get(chip->of_node);
  93. - ret = of_gpiochip_scan_gpios(chip);
  94. - if (ret)
  95. - of_node_put(chip->of_node);
  96. + if (!chip->request) {
  97. + ret = of_gpiochip_scan_gpios(chip, 0, chip->ngpio);
  98. + if (ret)
  99. + of_node_put(chip->of_node);
  100. + }
  101. return ret;
  102. }
  103. --- a/drivers/gpio/gpiolib.c
  104. +++ b/drivers/gpio/gpiolib.c
  105. @@ -1893,7 +1893,8 @@ int gpiochip_add_pingroup_range(struct g
  106. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  107. - return 0;
  108. + return of_gpiochip_scan_gpios(gc, gpio_offset,
  109. + pin_range->range.npins);
  110. }
  111. EXPORT_SYMBOL_GPL(gpiochip_add_pingroup_range);
  112. @@ -1950,7 +1951,7 @@ int gpiochip_add_pin_range(struct gpio_c
  113. list_add_tail(&pin_range->node, &gdev->pin_ranges);
  114. - return 0;
  115. + return of_gpiochip_scan_gpios(gc, gpio_offset, npins);
  116. }
  117. EXPORT_SYMBOL_GPL(gpiochip_add_pin_range);
  118. --- a/drivers/gpio/gpiolib-of.h
  119. +++ b/drivers/gpio/gpiolib-of.h
  120. @@ -13,6 +13,8 @@ struct gpio_desc *of_find_gpio(struct de
  121. unsigned long *lookupflags);
  122. int of_gpiochip_add(struct gpio_chip *gc);
  123. void of_gpiochip_remove(struct gpio_chip *gc);
  124. +int of_gpiochip_scan_gpios(struct gpio_chip *chip, unsigned int start,
  125. + unsigned int num);
  126. int of_gpio_get_count(struct device *dev, const char *con_id);
  127. bool of_gpio_need_valid_mask(const struct gpio_chip *gc);
  128. #else
  129. @@ -25,6 +27,12 @@ static inline struct gpio_desc *of_find_
  130. }
  131. static inline int of_gpiochip_add(struct gpio_chip *gc) { return 0; }
  132. static inline void of_gpiochip_remove(struct gpio_chip *gc) { }
  133. +static inline int of_gpiochip_scan_gpios(struct gpio_chip *chip,
  134. + unsigned int start,
  135. + unsigned int num)
  136. +{
  137. + return 0;
  138. +}
  139. static inline int of_gpio_get_count(struct device *dev, const char *con_id)
  140. {
  141. return 0;