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