gpiolib-of.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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_parse_own_gpio() - 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_parse_own_gpio()
  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_parse_own_gpio(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 gg_data gg_data = {
  123. .flags = &xlate_flags,
  124. };
  125. u32 tmp;
  126. int i, ret;
  127. chip_np = np->parent;
  128. if (!chip_np)
  129. return ERR_PTR(-EINVAL);
  130. xlate_flags = 0;
  131. *lflags = 0;
  132. *dflags = 0;
  133. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  134. if (ret)
  135. return ERR_PTR(ret);
  136. if (tmp > MAX_PHANDLE_ARGS)
  137. return ERR_PTR(-EINVAL);
  138. gg_data.gpiospec.args_count = tmp;
  139. gg_data.gpiospec.np = chip_np;
  140. for (i = 0; i < tmp; i++) {
  141. ret = of_property_read_u32_index(np, "gpios", i,
  142. &gg_data.gpiospec.args[i]);
  143. if (ret)
  144. return ERR_PTR(ret);
  145. }
  146. gpiochip_find(&gg_data, of_gpiochip_find_and_xlate);
  147. if (!gg_data.out_gpio) {
  148. if (np->parent == np)
  149. return ERR_PTR(-ENXIO);
  150. else
  151. return ERR_PTR(-EINVAL);
  152. }
  153. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  154. *lflags |= GPIO_ACTIVE_LOW;
  155. if (of_property_read_bool(np, "input"))
  156. *dflags |= GPIOD_IN;
  157. else if (of_property_read_bool(np, "output-low"))
  158. *dflags |= GPIOD_OUT_LOW;
  159. else if (of_property_read_bool(np, "output-high"))
  160. *dflags |= GPIOD_OUT_HIGH;
  161. else {
  162. pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
  163. desc_to_gpio(gg_data.out_gpio), np->name);
  164. return ERR_PTR(-EINVAL);
  165. }
  166. if (name && of_property_read_string(np, "line-name", name))
  167. *name = np->name;
  168. return gg_data.out_gpio;
  169. }
  170. /**
  171. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  172. * @chip: gpio chip to act on
  173. *
  174. * This is only used by of_gpiochip_add to request/set GPIO initial
  175. * configuration.
  176. */
  177. static void of_gpiochip_scan_gpios(struct gpio_chip *chip)
  178. {
  179. struct gpio_desc *desc = NULL;
  180. struct device_node *np;
  181. const char *name;
  182. enum gpio_lookup_flags lflags;
  183. enum gpiod_flags dflags;
  184. for_each_child_of_node(chip->of_node, np) {
  185. if (!of_property_read_bool(np, "gpio-hog"))
  186. continue;
  187. desc = of_parse_own_gpio(np, &name, &lflags, &dflags);
  188. if (IS_ERR(desc))
  189. continue;
  190. if (gpiod_hog(desc, name, lflags, dflags))
  191. continue;
  192. }
  193. }
  194. /**
  195. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  196. * @gc: pointer to the gpio_chip structure
  197. * @np: device node of the GPIO chip
  198. * @gpio_spec: gpio specifier as found in the device tree
  199. * @flags: a flags pointer to fill in
  200. *
  201. * This is simple translation function, suitable for the most 1:1 mapped
  202. * gpio chips. This function performs only one sanity check: whether gpio
  203. * is less than ngpios (that is specified in the gpio_chip).
  204. */
  205. int of_gpio_simple_xlate(struct gpio_chip *gc,
  206. const struct of_phandle_args *gpiospec, u32 *flags)
  207. {
  208. /*
  209. * We're discouraging gpio_cells < 2, since that way you'll have to
  210. * write your own xlate function (that will have to retrieve the GPIO
  211. * number and the flags from a single gpio cell -- this is possible,
  212. * but not recommended).
  213. */
  214. if (gc->of_gpio_n_cells < 2) {
  215. WARN_ON(1);
  216. return -EINVAL;
  217. }
  218. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  219. return -EINVAL;
  220. if (gpiospec->args[0] >= gc->ngpio)
  221. return -EINVAL;
  222. if (flags)
  223. *flags = gpiospec->args[1];
  224. return gpiospec->args[0];
  225. }
  226. EXPORT_SYMBOL(of_gpio_simple_xlate);
  227. /**
  228. * of_mm_gpiochip_add - Add memory mapped GPIO chip (bank)
  229. * @np: device node of the GPIO chip
  230. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  231. *
  232. * To use this function you should allocate and fill mm_gc with:
  233. *
  234. * 1) In the gpio_chip structure:
  235. * - all the callbacks
  236. * - of_gpio_n_cells
  237. * - of_xlate callback (optional)
  238. *
  239. * 3) In the of_mm_gpio_chip structure:
  240. * - save_regs callback (optional)
  241. *
  242. * If succeeded, this function will map bank's memory and will
  243. * do all necessary work for you. Then you'll able to use .regs
  244. * to manage GPIOs from the callbacks.
  245. */
  246. int of_mm_gpiochip_add(struct device_node *np,
  247. struct of_mm_gpio_chip *mm_gc)
  248. {
  249. int ret = -ENOMEM;
  250. struct gpio_chip *gc = &mm_gc->gc;
  251. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  252. if (!gc->label)
  253. goto err0;
  254. mm_gc->regs = of_iomap(np, 0);
  255. if (!mm_gc->regs)
  256. goto err1;
  257. gc->base = -1;
  258. if (mm_gc->save_regs)
  259. mm_gc->save_regs(mm_gc);
  260. mm_gc->gc.of_node = np;
  261. ret = gpiochip_add(gc);
  262. if (ret)
  263. goto err2;
  264. return 0;
  265. err2:
  266. iounmap(mm_gc->regs);
  267. err1:
  268. kfree(gc->label);
  269. err0:
  270. pr_err("%s: GPIO chip registration failed with status %d\n",
  271. np->full_name, ret);
  272. return ret;
  273. }
  274. EXPORT_SYMBOL(of_mm_gpiochip_add);
  275. /**
  276. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  277. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  278. */
  279. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  280. {
  281. struct gpio_chip *gc = &mm_gc->gc;
  282. if (!mm_gc)
  283. return;
  284. gpiochip_remove(gc);
  285. iounmap(mm_gc->regs);
  286. kfree(gc->label);
  287. }
  288. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  289. #ifdef CONFIG_PINCTRL
  290. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  291. {
  292. struct device_node *np = chip->of_node;
  293. struct of_phandle_args pinspec;
  294. struct pinctrl_dev *pctldev;
  295. int index = 0, ret;
  296. const char *name;
  297. static const char group_names_propname[] = "gpio-ranges-group-names";
  298. struct property *group_names;
  299. if (!np)
  300. return 0;
  301. group_names = of_find_property(np, group_names_propname, NULL);
  302. for (;; index++) {
  303. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  304. index, &pinspec);
  305. if (ret)
  306. break;
  307. pctldev = of_pinctrl_get(pinspec.np);
  308. if (!pctldev)
  309. return -EPROBE_DEFER;
  310. if (pinspec.args[2]) {
  311. if (group_names) {
  312. of_property_read_string_index(np,
  313. group_names_propname,
  314. index, &name);
  315. if (strlen(name)) {
  316. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  317. np->full_name);
  318. break;
  319. }
  320. }
  321. /* npins != 0: linear range */
  322. ret = gpiochip_add_pin_range(chip,
  323. pinctrl_dev_get_devname(pctldev),
  324. pinspec.args[0],
  325. pinspec.args[1],
  326. pinspec.args[2]);
  327. if (ret)
  328. return ret;
  329. } else {
  330. /* npins == 0: special range */
  331. if (pinspec.args[1]) {
  332. pr_err("%s: Illegal gpio-range format.\n",
  333. np->full_name);
  334. break;
  335. }
  336. if (!group_names) {
  337. pr_err("%s: GPIO group range requested but no %s property.\n",
  338. np->full_name, group_names_propname);
  339. break;
  340. }
  341. ret = of_property_read_string_index(np,
  342. group_names_propname,
  343. index, &name);
  344. if (ret)
  345. break;
  346. if (!strlen(name)) {
  347. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  348. np->full_name);
  349. break;
  350. }
  351. ret = gpiochip_add_pingroup_range(chip, pctldev,
  352. pinspec.args[0], name);
  353. if (ret)
  354. return ret;
  355. }
  356. }
  357. return 0;
  358. }
  359. #else
  360. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  361. #endif
  362. int of_gpiochip_add(struct gpio_chip *chip)
  363. {
  364. int status;
  365. if ((!chip->of_node) && (chip->dev))
  366. chip->of_node = chip->dev->of_node;
  367. if (!chip->of_node)
  368. return 0;
  369. if (!chip->of_xlate) {
  370. chip->of_gpio_n_cells = 2;
  371. chip->of_xlate = of_gpio_simple_xlate;
  372. }
  373. status = of_gpiochip_add_pin_range(chip);
  374. if (status)
  375. return status;
  376. of_node_get(chip->of_node);
  377. of_gpiochip_scan_gpios(chip);
  378. return 0;
  379. }
  380. void of_gpiochip_remove(struct gpio_chip *chip)
  381. {
  382. gpiochip_remove_pin_ranges(chip);
  383. of_node_put(chip->of_node);
  384. }