gpiolib-of.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * OF helpers for the GPIO API
  3. *
  4. * Copyright (c) 2007-2008 MontaVista Software, Inc.
  5. *
  6. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/device.h>
  14. #include <linux/err.h>
  15. #include <linux/errno.h>
  16. #include <linux/module.h>
  17. #include <linux/io.h>
  18. #include <linux/gpio/consumer.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_gpio.h>
  22. #include <linux/pinctrl/pinctrl.h>
  23. #include <linux/slab.h>
  24. #include <linux/gpio/machine.h>
  25. #include "gpiolib.h"
  26. /* Private data structure for of_gpiochip_find_and_xlate */
  27. struct gg_data {
  28. enum of_gpio_flags *flags;
  29. struct of_phandle_args gpiospec;
  30. struct gpio_desc *out_gpio;
  31. };
  32. /* Private function for resolving node pointer to gpio_chip */
  33. static int of_gpiochip_find_and_xlate(struct gpio_chip *gc, void *data)
  34. {
  35. struct gg_data *gg_data = data;
  36. int ret;
  37. if ((gc->of_node != gg_data->gpiospec.np) ||
  38. (gc->of_gpio_n_cells != gg_data->gpiospec.args_count) ||
  39. (!gc->of_xlate))
  40. return false;
  41. ret = gc->of_xlate(gc, &gg_data->gpiospec, gg_data->flags);
  42. if (ret < 0) {
  43. /* We've found a gpio chip, but the translation failed.
  44. * Store translation error in out_gpio.
  45. * Return false to keep looking, as more than one gpio chip
  46. * could be registered per of-node.
  47. */
  48. gg_data->out_gpio = ERR_PTR(ret);
  49. return false;
  50. }
  51. gg_data->out_gpio = gpiochip_get_desc(gc, ret);
  52. return true;
  53. }
  54. /**
  55. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  56. * @np: device node to get GPIO from
  57. * @propname: property name containing gpio specifier(s)
  58. * @index: index of the GPIO
  59. * @flags: a flags pointer to fill in
  60. *
  61. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  62. * value on the error condition. If @flags is not NULL the function also fills
  63. * in flags for the GPIO.
  64. */
  65. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  66. const char *propname, int index, enum of_gpio_flags *flags)
  67. {
  68. /* Return -EPROBE_DEFER to support probe() functions to be called
  69. * later when the GPIO actually becomes available
  70. */
  71. struct gg_data gg_data = {
  72. .flags = flags,
  73. .out_gpio = ERR_PTR(-EPROBE_DEFER)
  74. };
  75. int ret;
  76. /* .of_xlate might decide to not fill in the flags, so clear it. */
  77. if (flags)
  78. *flags = 0;
  79. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  80. &gg_data.gpiospec);
  81. if (ret) {
  82. pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
  83. __func__, propname, np->full_name, index);
  84. return ERR_PTR(ret);
  85. }
  86. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  87. of_node_put(gg_data.gpiospec.np);
  88. pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
  89. __func__, propname, np->full_name, index,
  90. PTR_ERR_OR_ZERO(gg_data.out_gpio));
  91. return gg_data.out_gpio;
  92. }
  93. int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
  94. int index, enum of_gpio_flags *flags)
  95. {
  96. struct gpio_desc *desc;
  97. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  98. if (IS_ERR(desc))
  99. return PTR_ERR(desc);
  100. else
  101. return desc_to_gpio(desc);
  102. }
  103. EXPORT_SYMBOL(of_get_named_gpio_flags);
  104. /**
  105. * of_get_gpio_hog() - Get a GPIO hog descriptor, names and flags for GPIO API
  106. * @np: device node to get GPIO from
  107. * @name: GPIO line name
  108. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  109. * of_get_gpio_hog()
  110. * @dflags: gpiod_flags - optional GPIO initialization flags
  111. *
  112. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  113. * value on the error condition.
  114. */
  115. static struct gpio_desc *of_get_gpio_hog(struct device_node *np,
  116. const char **name,
  117. enum gpio_lookup_flags *lflags,
  118. enum gpiod_flags *dflags)
  119. {
  120. struct device_node *chip_np;
  121. enum of_gpio_flags xlate_flags;
  122. struct gpio_desc *desc;
  123. struct gg_data gg_data = {
  124. .flags = &xlate_flags,
  125. };
  126. u32 tmp;
  127. int i, ret;
  128. chip_np = np->parent;
  129. if (!chip_np)
  130. return ERR_PTR(-EINVAL);
  131. xlate_flags = 0;
  132. *lflags = 0;
  133. *dflags = 0;
  134. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  135. if (ret)
  136. return ERR_PTR(ret);
  137. if (tmp > MAX_PHANDLE_ARGS)
  138. return ERR_PTR(-EINVAL);
  139. gg_data.gpiospec.args_count = tmp;
  140. gg_data.gpiospec.np = chip_np;
  141. for (i = 0; i < tmp; i++) {
  142. ret = of_property_read_u32_index(np, "gpios", i,
  143. &gg_data.gpiospec.args[i]);
  144. if (ret)
  145. return ERR_PTR(ret);
  146. }
  147. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  148. if (!gg_data.out_gpio) {
  149. if (np->parent == np)
  150. return ERR_PTR(-ENXIO);
  151. else
  152. return ERR_PTR(-EINVAL);
  153. }
  154. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  155. *lflags |= GPIO_ACTIVE_LOW;
  156. if (of_property_read_bool(np, "input"))
  157. *dflags |= GPIOD_IN;
  158. else if (of_property_read_bool(np, "output-low"))
  159. *dflags |= GPIOD_OUT_LOW;
  160. else if (of_property_read_bool(np, "output-high"))
  161. *dflags |= GPIOD_OUT_HIGH;
  162. else {
  163. pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
  164. desc_to_gpio(gg_data.out_gpio), np->name);
  165. return ERR_PTR(-EINVAL);
  166. }
  167. if (name && of_property_read_string(np, "line-name", name))
  168. *name = np->name;
  169. desc = gg_data.out_gpio;
  170. return desc;
  171. }
  172. /**
  173. * of_gpiochip_scan_hogs - Scan gpio-controller and apply GPIO hog as requested
  174. * @chip: gpio chip to act on
  175. *
  176. * This is only used by of_gpiochip_add to request/set GPIO initial
  177. * configuration.
  178. */
  179. static void of_gpiochip_scan_hogs(struct gpio_chip *chip)
  180. {
  181. struct gpio_desc *desc = NULL;
  182. struct device_node *np;
  183. const char *name;
  184. enum gpio_lookup_flags lflags;
  185. enum gpiod_flags dflags;
  186. for_each_child_of_node(chip->of_node, np) {
  187. if (!of_property_read_bool(np, "gpio-hog"))
  188. continue;
  189. desc = of_get_gpio_hog(np, &name, &lflags, &dflags);
  190. if (IS_ERR(desc))
  191. continue;
  192. if (gpiod_hog(desc, name, lflags, dflags))
  193. continue;
  194. }
  195. }
  196. /**
  197. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  198. * @gc: pointer to the gpio_chip structure
  199. * @np: device node of the GPIO chip
  200. * @gpio_spec: gpio specifier as found in the device tree
  201. * @flags: a flags pointer to fill in
  202. *
  203. * This is simple translation function, suitable for the most 1:1 mapped
  204. * gpio chips. This function performs only one sanity check: whether gpio
  205. * is less than ngpios (that is specified in the gpio_chip).
  206. */
  207. int of_gpio_simple_xlate(struct gpio_chip *gc,
  208. const struct of_phandle_args *gpiospec, u32 *flags)
  209. {
  210. /*
  211. * We're discouraging gpio_cells < 2, since that way you'll have to
  212. * write your own xlate function (that will have to retrieve the GPIO
  213. * number and the flags from a single gpio cell -- this is possible,
  214. * but not recommended).
  215. */
  216. if (gc->of_gpio_n_cells < 2) {
  217. WARN_ON(1);
  218. return -EINVAL;
  219. }
  220. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  221. return -EINVAL;
  222. if (gpiospec->args[0] >= gc->ngpio)
  223. return -EINVAL;
  224. if (flags)
  225. *flags = gpiospec->args[1];
  226. return gpiospec->args[0];
  227. }
  228. EXPORT_SYMBOL(of_gpio_simple_xlate);
  229. /**
  230. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  231. * @np: device node of the GPIO chip
  232. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  233. *
  234. * To use this function you should allocate and fill mm_gc with:
  235. *
  236. * 1) In the gpio_chip structure:
  237. * - all the callbacks
  238. * - of_gpio_n_cells
  239. * - of_xlate callback (optional)
  240. *
  241. * 3) In the of_mm_gpio_chip structure:
  242. * - save_regs callback (optional)
  243. *
  244. * If succeeded, this function will map bank's memory and will
  245. * do all necessary work for you. Then you'll able to use .regs
  246. * to manage GPIOs from the callbacks.
  247. */
  248. int of_mm_gpiochip_add(struct device_node *np,
  249. struct of_mm_gpio_chip *mm_gc)
  250. {
  251. int ret = -ENOMEM;
  252. struct gpio_chip *gc = &mm_gc->gc;
  253. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  254. if (!gc->label)
  255. goto err0;
  256. mm_gc->regs = of_iomap(np, 0);
  257. if (!mm_gc->regs)
  258. goto err1;
  259. gc->base = -1;
  260. if (mm_gc->save_regs)
  261. mm_gc->save_regs(mm_gc);
  262. mm_gc->gc.of_node = np;
  263. ret = gpiochip_add(gc);
  264. if (ret)
  265. goto err2;
  266. return 0;
  267. err2:
  268. iounmap(mm_gc->regs);
  269. err1:
  270. kfree(gc->label);
  271. err0:
  272. pr_err("%s: GPIO chip registration failed with status %d\n",
  273. np->full_name, ret);
  274. return ret;
  275. }
  276. EXPORT_SYMBOL(of_mm_gpiochip_add);
  277. /**
  278. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  279. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  280. */
  281. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  282. {
  283. struct gpio_chip *gc = &mm_gc->gc;
  284. if (!mm_gc)
  285. return;
  286. gpiochip_remove(gc);
  287. iounmap(mm_gc->regs);
  288. kfree(gc->label);
  289. }
  290. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  291. #ifdef CONFIG_PINCTRL
  292. static void of_gpiochip_add_pin_range(struct gpio_chip *chip)
  293. {
  294. struct device_node *np = chip->of_node;
  295. struct of_phandle_args pinspec;
  296. struct pinctrl_dev *pctldev;
  297. int index = 0, ret;
  298. const char *name;
  299. static const char group_names_propname[] = "gpio-ranges-group-names";
  300. struct property *group_names;
  301. if (!np)
  302. return;
  303. group_names = of_find_property(np, group_names_propname, NULL);
  304. for (;; index++) {
  305. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  306. index, &pinspec);
  307. if (ret)
  308. break;
  309. pctldev = of_pinctrl_get(pinspec.np);
  310. if (!pctldev)
  311. break;
  312. if (pinspec.args[2]) {
  313. if (group_names) {
  314. ret = of_property_read_string_index(np,
  315. group_names_propname,
  316. index, &name);
  317. if (strlen(name)) {
  318. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  319. np->full_name);
  320. break;
  321. }
  322. }
  323. /* npins != 0: linear range */
  324. ret = gpiochip_add_pin_range(chip,
  325. pinctrl_dev_get_devname(pctldev),
  326. pinspec.args[0],
  327. pinspec.args[1],
  328. pinspec.args[2]);
  329. if (ret)
  330. break;
  331. } else {
  332. /* npins == 0: special range */
  333. if (pinspec.args[1]) {
  334. pr_err("%s: Illegal gpio-range format.\n",
  335. np->full_name);
  336. break;
  337. }
  338. if (!group_names) {
  339. pr_err("%s: GPIO group range requested but no %s property.\n",
  340. np->full_name, group_names_propname);
  341. break;
  342. }
  343. ret = of_property_read_string_index(np,
  344. group_names_propname,
  345. index, &name);
  346. if (ret)
  347. break;
  348. if (!strlen(name)) {
  349. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  350. np->full_name);
  351. break;
  352. }
  353. ret = gpiochip_add_pingroup_range(chip, pctldev,
  354. pinspec.args[0], name);
  355. if (ret)
  356. break;
  357. }
  358. }
  359. }
  360. #else
  361. static void of_gpiochip_add_pin_range(struct gpio_chip *chip) {}
  362. #endif
  363. void of_gpiochip_add(struct gpio_chip *chip)
  364. {
  365. if ((!chip->of_node) && (chip->dev))
  366. chip->of_node = chip->dev->of_node;
  367. if (!chip->of_node)
  368. return;
  369. if (!chip->of_xlate) {
  370. chip->of_gpio_n_cells = 2;
  371. chip->of_xlate = of_gpio_simple_xlate;
  372. }
  373. of_gpiochip_add_pin_range(chip);
  374. of_node_get(chip->of_node);
  375. of_gpiochip_scan_hogs(chip);
  376. }
  377. void of_gpiochip_remove(struct gpio_chip *chip)
  378. {
  379. gpiochip_remove_pin_ranges(chip);
  380. of_node_put(chip->of_node);
  381. }