gpiolib-of.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
  27. {
  28. struct of_phandle_args *gpiospec = data;
  29. return chip->gpiodev->dev.of_node == gpiospec->np &&
  30. chip->of_xlate(chip, gpiospec, NULL) >= 0;
  31. }
  32. static struct gpio_chip *of_find_gpiochip_by_xlate(
  33. struct of_phandle_args *gpiospec)
  34. {
  35. return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
  36. }
  37. static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
  38. struct of_phandle_args *gpiospec,
  39. enum of_gpio_flags *flags)
  40. {
  41. int ret;
  42. if (chip->of_gpio_n_cells != gpiospec->args_count)
  43. return ERR_PTR(-EINVAL);
  44. ret = chip->of_xlate(chip, gpiospec, flags);
  45. if (ret < 0)
  46. return ERR_PTR(ret);
  47. return gpiochip_get_desc(chip, ret);
  48. }
  49. /**
  50. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  51. * @np: device node to get GPIO from
  52. * @propname: property name containing gpio specifier(s)
  53. * @index: index of the GPIO
  54. * @flags: a flags pointer to fill in
  55. *
  56. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  57. * value on the error condition. If @flags is not NULL the function also fills
  58. * in flags for the GPIO.
  59. */
  60. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  61. const char *propname, int index, enum of_gpio_flags *flags)
  62. {
  63. struct of_phandle_args gpiospec;
  64. struct gpio_chip *chip;
  65. struct gpio_desc *desc;
  66. int ret;
  67. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  68. &gpiospec);
  69. if (ret) {
  70. pr_debug("%s: can't parse '%s' property of node '%s[%d]'\n",
  71. __func__, propname, np->full_name, index);
  72. return ERR_PTR(ret);
  73. }
  74. chip = of_find_gpiochip_by_xlate(&gpiospec);
  75. if (!chip) {
  76. desc = ERR_PTR(-EPROBE_DEFER);
  77. goto out;
  78. }
  79. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
  80. if (IS_ERR(desc))
  81. goto out;
  82. pr_debug("%s: parsed '%s' property of node '%s[%d]' - status (%d)\n",
  83. __func__, propname, np->full_name, index,
  84. PTR_ERR_OR_ZERO(desc));
  85. out:
  86. of_node_put(gpiospec.np);
  87. return desc;
  88. }
  89. int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
  90. int index, enum of_gpio_flags *flags)
  91. {
  92. struct gpio_desc *desc;
  93. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  94. if (IS_ERR(desc))
  95. return PTR_ERR(desc);
  96. else
  97. return desc_to_gpio(desc);
  98. }
  99. EXPORT_SYMBOL(of_get_named_gpio_flags);
  100. struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  101. unsigned int idx,
  102. enum gpio_lookup_flags *flags)
  103. {
  104. char prop_name[32]; /* 32 is max size of property name */
  105. enum of_gpio_flags of_flags;
  106. struct gpio_desc *desc;
  107. unsigned int i;
  108. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  109. if (con_id)
  110. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  111. gpio_suffixes[i]);
  112. else
  113. snprintf(prop_name, sizeof(prop_name), "%s",
  114. gpio_suffixes[i]);
  115. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  116. &of_flags);
  117. if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
  118. break;
  119. }
  120. if (IS_ERR(desc))
  121. return desc;
  122. if (of_flags & OF_GPIO_ACTIVE_LOW)
  123. *flags |= GPIO_ACTIVE_LOW;
  124. if (of_flags & OF_GPIO_SINGLE_ENDED) {
  125. if (of_flags & OF_GPIO_ACTIVE_LOW)
  126. *flags |= GPIO_OPEN_DRAIN;
  127. else
  128. *flags |= GPIO_OPEN_SOURCE;
  129. }
  130. return desc;
  131. }
  132. /**
  133. * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  134. * @np: device node to get GPIO from
  135. * @chip: GPIO chip whose hog is parsed
  136. * @name: GPIO line name
  137. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  138. * of_parse_own_gpio()
  139. * @dflags: gpiod_flags - optional GPIO initialization flags
  140. *
  141. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  142. * value on the error condition.
  143. */
  144. static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
  145. struct gpio_chip *chip,
  146. const char **name,
  147. enum gpio_lookup_flags *lflags,
  148. enum gpiod_flags *dflags)
  149. {
  150. struct device_node *chip_np;
  151. enum of_gpio_flags xlate_flags;
  152. struct of_phandle_args gpiospec;
  153. struct gpio_desc *desc;
  154. u32 tmp;
  155. int ret;
  156. chip_np = chip->of_node;
  157. if (!chip_np)
  158. return ERR_PTR(-EINVAL);
  159. xlate_flags = 0;
  160. *lflags = 0;
  161. *dflags = 0;
  162. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  163. if (ret)
  164. return ERR_PTR(ret);
  165. gpiospec.np = chip_np;
  166. gpiospec.args_count = tmp;
  167. ret = of_property_read_u32_array(np, "gpios", gpiospec.args, tmp);
  168. if (ret)
  169. return ERR_PTR(ret);
  170. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
  171. if (IS_ERR(desc))
  172. return desc;
  173. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  174. *lflags |= GPIO_ACTIVE_LOW;
  175. if (of_property_read_bool(np, "input"))
  176. *dflags |= GPIOD_IN;
  177. else if (of_property_read_bool(np, "output-low"))
  178. *dflags |= GPIOD_OUT_LOW;
  179. else if (of_property_read_bool(np, "output-high"))
  180. *dflags |= GPIOD_OUT_HIGH;
  181. else {
  182. pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
  183. desc_to_gpio(desc), np->name);
  184. return ERR_PTR(-EINVAL);
  185. }
  186. if (name && of_property_read_string(np, "line-name", name))
  187. *name = np->name;
  188. return desc;
  189. }
  190. /**
  191. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  192. * @chip: gpio chip to act on
  193. *
  194. * This is only used by of_gpiochip_add to request/set GPIO initial
  195. * configuration.
  196. * It retures error if it fails otherwise 0 on success.
  197. */
  198. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  199. {
  200. struct gpio_desc *desc = NULL;
  201. struct device_node *np;
  202. const char *name;
  203. enum gpio_lookup_flags lflags;
  204. enum gpiod_flags dflags;
  205. int ret;
  206. for_each_available_child_of_node(chip->of_node, np) {
  207. if (!of_property_read_bool(np, "gpio-hog"))
  208. continue;
  209. desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
  210. if (IS_ERR(desc))
  211. continue;
  212. ret = gpiod_hog(desc, name, lflags, dflags);
  213. if (ret < 0) {
  214. of_node_put(np);
  215. return ret;
  216. }
  217. }
  218. return 0;
  219. }
  220. /**
  221. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  222. * @gc: pointer to the gpio_chip structure
  223. * @np: device node of the GPIO chip
  224. * @gpio_spec: gpio specifier as found in the device tree
  225. * @flags: a flags pointer to fill in
  226. *
  227. * This is simple translation function, suitable for the most 1:1 mapped
  228. * gpio chips. This function performs only one sanity check: whether gpio
  229. * is less than ngpios (that is specified in the gpio_chip).
  230. */
  231. int of_gpio_simple_xlate(struct gpio_chip *gc,
  232. const struct of_phandle_args *gpiospec, u32 *flags)
  233. {
  234. /*
  235. * We're discouraging gpio_cells < 2, since that way you'll have to
  236. * write your own xlate function (that will have to retrieve the GPIO
  237. * number and the flags from a single gpio cell -- this is possible,
  238. * but not recommended).
  239. */
  240. if (gc->of_gpio_n_cells < 2) {
  241. WARN_ON(1);
  242. return -EINVAL;
  243. }
  244. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  245. return -EINVAL;
  246. if (gpiospec->args[0] >= gc->ngpio)
  247. return -EINVAL;
  248. if (flags)
  249. *flags = gpiospec->args[1];
  250. return gpiospec->args[0];
  251. }
  252. EXPORT_SYMBOL(of_gpio_simple_xlate);
  253. /**
  254. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  255. * @np: device node of the GPIO chip
  256. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  257. * @data: driver data to store in the struct gpio_chip
  258. *
  259. * To use this function you should allocate and fill mm_gc with:
  260. *
  261. * 1) In the gpio_chip structure:
  262. * - all the callbacks
  263. * - of_gpio_n_cells
  264. * - of_xlate callback (optional)
  265. *
  266. * 3) In the of_mm_gpio_chip structure:
  267. * - save_regs callback (optional)
  268. *
  269. * If succeeded, this function will map bank's memory and will
  270. * do all necessary work for you. Then you'll able to use .regs
  271. * to manage GPIOs from the callbacks.
  272. */
  273. int of_mm_gpiochip_add_data(struct device_node *np,
  274. struct of_mm_gpio_chip *mm_gc,
  275. void *data)
  276. {
  277. int ret = -ENOMEM;
  278. struct gpio_chip *gc = &mm_gc->gc;
  279. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  280. if (!gc->label)
  281. goto err0;
  282. mm_gc->regs = of_iomap(np, 0);
  283. if (!mm_gc->regs)
  284. goto err1;
  285. gc->base = -1;
  286. if (mm_gc->save_regs)
  287. mm_gc->save_regs(mm_gc);
  288. mm_gc->gc.of_node = np;
  289. ret = gpiochip_add_data(gc, data);
  290. if (ret)
  291. goto err2;
  292. return 0;
  293. err2:
  294. iounmap(mm_gc->regs);
  295. err1:
  296. kfree(gc->label);
  297. err0:
  298. pr_err("%s: GPIO chip registration failed with status %d\n",
  299. np->full_name, ret);
  300. return ret;
  301. }
  302. EXPORT_SYMBOL(of_mm_gpiochip_add_data);
  303. /**
  304. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  305. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  306. */
  307. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  308. {
  309. struct gpio_chip *gc = &mm_gc->gc;
  310. if (!mm_gc)
  311. return;
  312. gpiochip_remove(gc);
  313. iounmap(mm_gc->regs);
  314. kfree(gc->label);
  315. }
  316. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  317. #ifdef CONFIG_PINCTRL
  318. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  319. {
  320. struct device_node *np = chip->of_node;
  321. struct of_phandle_args pinspec;
  322. struct pinctrl_dev *pctldev;
  323. int index = 0, ret;
  324. const char *name;
  325. static const char group_names_propname[] = "gpio-ranges-group-names";
  326. struct property *group_names;
  327. if (!np)
  328. return 0;
  329. group_names = of_find_property(np, group_names_propname, NULL);
  330. for (;; index++) {
  331. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  332. index, &pinspec);
  333. if (ret)
  334. break;
  335. pctldev = of_pinctrl_get(pinspec.np);
  336. of_node_put(pinspec.np);
  337. if (!pctldev)
  338. return -EPROBE_DEFER;
  339. if (pinspec.args[2]) {
  340. if (group_names) {
  341. of_property_read_string_index(np,
  342. group_names_propname,
  343. index, &name);
  344. if (strlen(name)) {
  345. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  346. np->full_name);
  347. break;
  348. }
  349. }
  350. /* npins != 0: linear range */
  351. ret = gpiochip_add_pin_range(chip,
  352. pinctrl_dev_get_devname(pctldev),
  353. pinspec.args[0],
  354. pinspec.args[1],
  355. pinspec.args[2]);
  356. if (ret)
  357. return ret;
  358. } else {
  359. /* npins == 0: special range */
  360. if (pinspec.args[1]) {
  361. pr_err("%s: Illegal gpio-range format.\n",
  362. np->full_name);
  363. break;
  364. }
  365. if (!group_names) {
  366. pr_err("%s: GPIO group range requested but no %s property.\n",
  367. np->full_name, group_names_propname);
  368. break;
  369. }
  370. ret = of_property_read_string_index(np,
  371. group_names_propname,
  372. index, &name);
  373. if (ret)
  374. break;
  375. if (!strlen(name)) {
  376. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  377. np->full_name);
  378. break;
  379. }
  380. ret = gpiochip_add_pingroup_range(chip, pctldev,
  381. pinspec.args[0], name);
  382. if (ret)
  383. return ret;
  384. }
  385. }
  386. return 0;
  387. }
  388. #else
  389. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  390. #endif
  391. int of_gpiochip_add(struct gpio_chip *chip)
  392. {
  393. int status;
  394. if ((!chip->of_node) && (chip->parent))
  395. chip->of_node = chip->parent->of_node;
  396. if (!chip->of_node)
  397. return 0;
  398. if (!chip->of_xlate) {
  399. chip->of_gpio_n_cells = 2;
  400. chip->of_xlate = of_gpio_simple_xlate;
  401. }
  402. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  403. return -EINVAL;
  404. status = of_gpiochip_add_pin_range(chip);
  405. if (status)
  406. return status;
  407. /* If the chip defines names itself, these take precedence */
  408. if (!chip->names)
  409. devprop_gpiochip_set_names(chip);
  410. of_node_get(chip->of_node);
  411. return of_gpiochip_scan_gpios(chip);
  412. }
  413. void of_gpiochip_remove(struct gpio_chip *chip)
  414. {
  415. gpiochip_remove_pin_ranges(chip);
  416. of_node_put(chip->of_node);
  417. }