gpiolib-of.c 12 KB

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