0200-owrt-GPIO-add-gpio_export_with_name.patch 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. From 7d8a8b0e46a970fba505d967e93123e9729d97b5 Mon Sep 17 00:00:00 2001
  2. From: John Crispin <[email protected]>
  3. Date: Sun, 23 Jun 2013 00:16:22 +0200
  4. Subject: [PATCH] owrt: GPIO: add gpio_export_with_name
  5. http://lists.infradead.org/pipermail/linux-arm-kernel/2012-November/133856.html
  6. Signed-off-by: John Crispin <[email protected]>
  7. ---
  8. Documentation/devicetree/bindings/gpio/gpio.txt | 60 ++++++++++++++++++++
  9. drivers/gpio/gpiolib-of.c | 68 +++++++++++++++++++++++
  10. drivers/gpio/gpiolib.c | 24 +++++---
  11. include/asm-generic/gpio.h | 6 +-
  12. include/linux/gpio.h | 26 ++++++++-
  13. 5 files changed, 172 insertions(+), 12 deletions(-)
  14. --- a/Documentation/devicetree/bindings/gpio/gpio.txt
  15. +++ b/Documentation/devicetree/bindings/gpio/gpio.txt
  16. @@ -112,3 +112,63 @@ where,
  17. The pinctrl node must have "#gpio-range-cells" property to show number of
  18. arguments to pass with phandle from gpio controllers node.
  19. +
  20. +3) gpio-export
  21. +--------------
  22. +
  23. +gpio-export will allow you to automatically export gpio
  24. +
  25. +required properties:
  26. +- compatible: Should be "gpio-export"
  27. +
  28. +in each child node will reprensent a gpio or if no name is specified
  29. +a list of gpio to export
  30. +
  31. +required properties:
  32. +- gpios: gpio to export
  33. +
  34. +optional properties:
  35. + - gpio-export,name: export name
  36. + - gpio-export,output: to set the as output with default value
  37. + if no present gpio as input
  38. + - pio-export,direction_may_change: boolean to allow the direction to be controllable
  39. +
  40. +Example:
  41. +
  42. +
  43. +gpio_export {
  44. + compatible = "gpio-export";
  45. + #size-cells = <0>;
  46. +
  47. + in {
  48. + gpio-export,name = "in";
  49. + gpios = <&pioC 20 0>;
  50. + };
  51. +
  52. + out {
  53. + gpio-export,name = "out";
  54. + gpio-export,output = <1>;
  55. + gpio-export,direction_may_change;
  56. + gpios = <&pioC 21 0>;
  57. + };
  58. +
  59. + in_out {
  60. + gpio-export,name = "in_out";
  61. + gpio-export,direction_may_change;
  62. + gpios = <&pioC 21 0>;
  63. + };
  64. +
  65. + gpios_in {
  66. + gpios = <&pioB 0 0
  67. + &pioB 3 0
  68. + &pioC 4 0>;
  69. + gpio-export,direction_may_change;
  70. + };
  71. +
  72. + gpios_out {
  73. + gpios = <&pioB 1 0
  74. + &pioB 2 0
  75. + &pioC 3 0>;
  76. + gpio-export,output = <1>;
  77. + };
  78. +};
  79. --- a/drivers/gpio/gpiolib-of.c
  80. +++ b/drivers/gpio/gpiolib-of.c
  81. @@ -21,6 +21,8 @@
  82. #include <linux/of_gpio.h>
  83. #include <linux/pinctrl/pinctrl.h>
  84. #include <linux/slab.h>
  85. +#include <linux/init.h>
  86. +#include <linux/platform_device.h>
  87. /* Private data structure for of_gpiochip_find_and_xlate */
  88. struct gg_data {
  89. @@ -253,3 +255,69 @@ void of_gpiochip_remove(struct gpio_chip
  90. if (chip->of_node)
  91. of_node_put(chip->of_node);
  92. }
  93. +
  94. +static struct of_device_id gpio_export_ids[] = {
  95. + { .compatible = "gpio-export" },
  96. + { /* sentinel */ }
  97. +};
  98. +
  99. +static int __init of_gpio_export_probe(struct platform_device *pdev)
  100. +{
  101. + struct device_node *np = pdev->dev.of_node;
  102. + struct device_node *cnp;
  103. + u32 val;
  104. + int nb = 0;
  105. +
  106. + for_each_child_of_node(np, cnp) {
  107. + const char *name = NULL;
  108. + int gpio;
  109. + bool dmc;
  110. + int max_gpio = 1;
  111. + int i;
  112. +
  113. + of_property_read_string(cnp, "gpio-export,name", &name);
  114. +
  115. + if (!name)
  116. + max_gpio = of_gpio_count(cnp);
  117. +
  118. + for (i = 0; i < max_gpio; i++) {
  119. + unsigned flags = 0;
  120. + enum of_gpio_flags of_flags;
  121. +
  122. + gpio = of_get_gpio_flags(cnp, i, &of_flags);
  123. +
  124. + if (of_flags == OF_GPIO_ACTIVE_LOW)
  125. + flags |= GPIOF_ACTIVE_LOW;
  126. +
  127. + if (!of_property_read_u32(cnp, "gpio-export,output", &val))
  128. + flags |= val ? GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  129. + else
  130. + flags |= GPIOF_IN;
  131. +
  132. + if (devm_gpio_request_one(&pdev->dev, gpio, flags, name ? name : of_node_full_name(np)))
  133. + continue;
  134. +
  135. + dmc = of_property_read_bool(cnp, "gpio-export,direction_may_change");
  136. + gpio_export_with_name(gpio, dmc, name);
  137. + nb++;
  138. + }
  139. + }
  140. +
  141. + dev_info(&pdev->dev, "%d gpio(s) exported\n", nb);
  142. +
  143. + return 0;
  144. +}
  145. +
  146. +static struct platform_driver gpio_export_driver = {
  147. + .driver = {
  148. + .name = "gpio-export",
  149. + .owner = THIS_MODULE,
  150. + .of_match_table = of_match_ptr(gpio_export_ids),
  151. + },
  152. +};
  153. +
  154. +static int __init of_gpio_export_init(void)
  155. +{
  156. + return platform_driver_probe(&gpio_export_driver, of_gpio_export_probe);
  157. +}
  158. +device_initcall(of_gpio_export_init);
  159. --- a/drivers/gpio/gpiolib.c
  160. +++ b/drivers/gpio/gpiolib.c
  161. @@ -96,7 +96,7 @@ static int gpiod_get_value(const struct
  162. static void gpiod_set_value(struct gpio_desc *desc, int value);
  163. static int gpiod_cansleep(const struct gpio_desc *desc);
  164. static int gpiod_to_irq(const struct gpio_desc *desc);
  165. -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change);
  166. +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name);
  167. static int gpiod_export_link(struct device *dev, const char *name,
  168. struct gpio_desc *desc);
  169. static int gpiod_sysfs_set_active_low(struct gpio_desc *desc, int value);
  170. @@ -674,7 +674,7 @@ static ssize_t export_store(struct class
  171. status = -ENODEV;
  172. goto done;
  173. }
  174. - status = gpiod_export(desc, true);
  175. + status = gpiod_export(desc, true, NULL);
  176. if (status < 0)
  177. gpiod_free(desc);
  178. else
  179. @@ -736,9 +736,10 @@ static struct class gpio_class = {
  180. /**
  181. - * gpio_export - export a GPIO through sysfs
  182. + * gpio_export_with_name - export a GPIO through sysfs
  183. * @gpio: gpio to make available, already requested
  184. * @direction_may_change: true if userspace may change gpio direction
  185. + * @name: gpio name
  186. * Context: arch_initcall or later
  187. *
  188. * When drivers want to make a GPIO accessible to userspace after they
  189. @@ -750,7 +751,7 @@ static struct class gpio_class = {
  190. *
  191. * Returns zero on success, else an error.
  192. */
  193. -static int gpiod_export(struct gpio_desc *desc, bool direction_may_change)
  194. +static int gpiod_export(struct gpio_desc *desc, bool direction_may_change, const char *name)
  195. {
  196. unsigned long flags;
  197. int status;
  198. @@ -783,6 +784,8 @@ static int gpiod_export(struct gpio_desc
  199. goto fail_unlock;
  200. }
  201. + if (name)
  202. + ioname = name;
  203. if (!desc->chip->direction_input || !desc->chip->direction_output)
  204. direction_may_change = false;
  205. spin_unlock_irqrestore(&gpio_lock, flags);
  206. @@ -829,11 +832,11 @@ fail_unlock:
  207. return status;
  208. }
  209. -int gpio_export(unsigned gpio, bool direction_may_change)
  210. +int gpio_export_with_name(unsigned gpio, bool direction_may_change, const char *name)
  211. {
  212. - return gpiod_export(gpio_to_desc(gpio), direction_may_change);
  213. + return gpiod_export(gpio_to_desc(gpio), direction_may_change, name);
  214. }
  215. -EXPORT_SYMBOL_GPL(gpio_export);
  216. +EXPORT_SYMBOL_GPL(gpio_export_with_name);
  217. static int match_export(struct device *dev, const void *data)
  218. {
  219. @@ -1092,7 +1095,7 @@ static inline void gpiochip_unexport(str
  220. }
  221. static inline int gpiod_export(struct gpio_desc *desc,
  222. - bool direction_may_change)
  223. + bool direction_may_change, const char *name)
  224. {
  225. return -ENOSYS;
  226. }
  227. @@ -1521,6 +1524,9 @@ int gpio_request_one(unsigned gpio, unsi
  228. if (flags & GPIOF_OPEN_SOURCE)
  229. set_bit(FLAG_OPEN_SOURCE, &desc->flags);
  230. + if (flags & GPIOF_ACTIVE_LOW)
  231. + set_bit(FLAG_ACTIVE_LOW, &gpio_desc[gpio].flags);
  232. +
  233. if (flags & GPIOF_DIR_IN)
  234. err = gpiod_direction_input(desc);
  235. else
  236. @@ -1531,7 +1537,7 @@ int gpio_request_one(unsigned gpio, unsi
  237. goto free_gpio;
  238. if (flags & GPIOF_EXPORT) {
  239. - err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE);
  240. + err = gpiod_export(desc, flags & GPIOF_EXPORT_CHANGEABLE, NULL);
  241. if (err)
  242. goto free_gpio;
  243. }
  244. --- a/include/asm-generic/gpio.h
  245. +++ b/include/asm-generic/gpio.h
  246. @@ -202,7 +202,8 @@ extern void gpio_free_array(const struct
  247. * A sysfs interface can be exported by individual drivers if they want,
  248. * but more typically is configured entirely from userspace.
  249. */
  250. -extern int gpio_export(unsigned gpio, bool direction_may_change);
  251. +extern int gpio_export_with_name(unsigned gpio, bool direction_may_change,
  252. + const char *name);
  253. extern int gpio_export_link(struct device *dev, const char *name,
  254. unsigned gpio);
  255. extern int gpio_sysfs_set_active_low(unsigned gpio, int value);
  256. @@ -284,7 +285,8 @@ struct device;
  257. /* sysfs support is only available with gpiolib, where it's optional */
  258. -static inline int gpio_export(unsigned gpio, bool direction_may_change)
  259. +static inline int gpio_export_with_name(unsigned gpio,
  260. + bool direction_may_change, const char *name)
  261. {
  262. return -ENOSYS;
  263. }
  264. --- a/include/linux/gpio.h
  265. +++ b/include/linux/gpio.h
  266. @@ -27,6 +27,9 @@
  267. #define GPIOF_EXPORT_DIR_FIXED (GPIOF_EXPORT)
  268. #define GPIOF_EXPORT_DIR_CHANGEABLE (GPIOF_EXPORT | GPIOF_EXPORT_CHANGEABLE)
  269. +#define GPIOF_ACTIVE_LOW (1 << 6)
  270. +
  271. +
  272. /**
  273. * struct gpio - a structure describing a GPIO with configuration
  274. * @gpio: the GPIO number
  275. @@ -169,7 +172,8 @@ static inline void gpio_set_value_cansle
  276. WARN_ON(1);
  277. }
  278. -static inline int gpio_export(unsigned gpio, bool direction_may_change)
  279. +static inline int gpio_export_with_name(unsigned gpio,
  280. + bool direction_may_change, const char *name)
  281. {
  282. /* GPIO can never have been requested or set as {in,out}put */
  283. WARN_ON(1);
  284. @@ -236,4 +240,24 @@ int devm_gpio_request_one(struct device
  285. unsigned long flags, const char *label);
  286. void devm_gpio_free(struct device *dev, unsigned int gpio);
  287. +/**
  288. + * gpio_export - export a GPIO through sysfs
  289. + * @gpio: gpio to make available, already requested
  290. + * @direction_may_change: true if userspace may change gpio direction
  291. + * Context: arch_initcall or later
  292. + *
  293. + * When drivers want to make a GPIO accessible to userspace after they
  294. + * have requested it -- perhaps while debugging, or as part of their
  295. + * public interface -- they may use this routine. If the GPIO can
  296. + * change direction (some can't) and the caller allows it, userspace
  297. + * will see "direction" sysfs attribute which may be used to change
  298. + * the gpio's direction. A "value" attribute will always be provided.
  299. + *
  300. + * Returns zero on success, else an error.
  301. + */
  302. +static inline int gpio_export(unsigned gpio,bool direction_may_change)
  303. +{
  304. + return gpio_export_with_name(gpio, direction_may_change, NULL);
  305. +}
  306. +
  307. #endif /* __LINUX_GPIO_H */