gpiolib-of.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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_set_names() - set up the names of the lines
  192. * @chip: GPIO chip whose lines should be named, if possible
  193. */
  194. static void of_gpiochip_set_names(struct gpio_chip *gc)
  195. {
  196. struct gpio_device *gdev = gc->gpiodev;
  197. struct device_node *np = gc->of_node;
  198. int i;
  199. int nstrings;
  200. nstrings = of_property_count_strings(np, "gpio-line-names");
  201. if (nstrings <= 0)
  202. /* Lines names not present */
  203. return;
  204. /* This is normally not what you want */
  205. if (gdev->ngpio != nstrings)
  206. dev_info(&gdev->dev, "gpio-line-names specifies %d line "
  207. "names but there are %d lines on the chip\n",
  208. nstrings, gdev->ngpio);
  209. /*
  210. * Make sure to not index beyond the end of the number of descriptors
  211. * of the GPIO device.
  212. */
  213. for (i = 0; i < gdev->ngpio; i++) {
  214. const char *name;
  215. int ret;
  216. ret = of_property_read_string_index(np,
  217. "gpio-line-names",
  218. i,
  219. &name);
  220. if (ret) {
  221. if (ret != -ENODATA)
  222. dev_err(&gdev->dev,
  223. "unable to name line %d: %d\n",
  224. i, ret);
  225. break;
  226. }
  227. gdev->descs[i].name = name;
  228. }
  229. }
  230. /**
  231. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  232. * @chip: gpio chip to act on
  233. *
  234. * This is only used by of_gpiochip_add to request/set GPIO initial
  235. * configuration.
  236. * It retures error if it fails otherwise 0 on success.
  237. */
  238. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  239. {
  240. struct gpio_desc *desc = NULL;
  241. struct device_node *np;
  242. const char *name;
  243. enum gpio_lookup_flags lflags;
  244. enum gpiod_flags dflags;
  245. int ret;
  246. for_each_available_child_of_node(chip->of_node, np) {
  247. if (!of_property_read_bool(np, "gpio-hog"))
  248. continue;
  249. desc = of_parse_own_gpio(np, chip, &name, &lflags, &dflags);
  250. if (IS_ERR(desc))
  251. continue;
  252. ret = gpiod_hog(desc, name, lflags, dflags);
  253. if (ret < 0)
  254. return ret;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * of_gpio_simple_xlate - translate gpio_spec to the GPIO number and flags
  260. * @gc: pointer to the gpio_chip structure
  261. * @np: device node of the GPIO chip
  262. * @gpio_spec: gpio specifier as found in the device tree
  263. * @flags: a flags pointer to fill in
  264. *
  265. * This is simple translation function, suitable for the most 1:1 mapped
  266. * gpio chips. This function performs only one sanity check: whether gpio
  267. * is less than ngpios (that is specified in the gpio_chip).
  268. */
  269. int of_gpio_simple_xlate(struct gpio_chip *gc,
  270. const struct of_phandle_args *gpiospec, u32 *flags)
  271. {
  272. /*
  273. * We're discouraging gpio_cells < 2, since that way you'll have to
  274. * write your own xlate function (that will have to retrieve the GPIO
  275. * number and the flags from a single gpio cell -- this is possible,
  276. * but not recommended).
  277. */
  278. if (gc->of_gpio_n_cells < 2) {
  279. WARN_ON(1);
  280. return -EINVAL;
  281. }
  282. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  283. return -EINVAL;
  284. if (gpiospec->args[0] >= gc->ngpio)
  285. return -EINVAL;
  286. if (flags)
  287. *flags = gpiospec->args[1];
  288. return gpiospec->args[0];
  289. }
  290. EXPORT_SYMBOL(of_gpio_simple_xlate);
  291. /**
  292. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  293. * @np: device node of the GPIO chip
  294. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  295. * @data: driver data to store in the struct gpio_chip
  296. *
  297. * To use this function you should allocate and fill mm_gc with:
  298. *
  299. * 1) In the gpio_chip structure:
  300. * - all the callbacks
  301. * - of_gpio_n_cells
  302. * - of_xlate callback (optional)
  303. *
  304. * 3) In the of_mm_gpio_chip structure:
  305. * - save_regs callback (optional)
  306. *
  307. * If succeeded, this function will map bank's memory and will
  308. * do all necessary work for you. Then you'll able to use .regs
  309. * to manage GPIOs from the callbacks.
  310. */
  311. int of_mm_gpiochip_add_data(struct device_node *np,
  312. struct of_mm_gpio_chip *mm_gc,
  313. void *data)
  314. {
  315. int ret = -ENOMEM;
  316. struct gpio_chip *gc = &mm_gc->gc;
  317. gc->label = kstrdup(np->full_name, GFP_KERNEL);
  318. if (!gc->label)
  319. goto err0;
  320. mm_gc->regs = of_iomap(np, 0);
  321. if (!mm_gc->regs)
  322. goto err1;
  323. gc->base = -1;
  324. if (mm_gc->save_regs)
  325. mm_gc->save_regs(mm_gc);
  326. mm_gc->gc.of_node = np;
  327. ret = gpiochip_add_data(gc, data);
  328. if (ret)
  329. goto err2;
  330. return 0;
  331. err2:
  332. iounmap(mm_gc->regs);
  333. err1:
  334. kfree(gc->label);
  335. err0:
  336. pr_err("%s: GPIO chip registration failed with status %d\n",
  337. np->full_name, ret);
  338. return ret;
  339. }
  340. EXPORT_SYMBOL(of_mm_gpiochip_add_data);
  341. /**
  342. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  343. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  344. */
  345. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  346. {
  347. struct gpio_chip *gc = &mm_gc->gc;
  348. if (!mm_gc)
  349. return;
  350. gpiochip_remove(gc);
  351. iounmap(mm_gc->regs);
  352. kfree(gc->label);
  353. }
  354. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  355. #ifdef CONFIG_PINCTRL
  356. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  357. {
  358. struct device_node *np = chip->of_node;
  359. struct of_phandle_args pinspec;
  360. struct pinctrl_dev *pctldev;
  361. int index = 0, ret;
  362. const char *name;
  363. static const char group_names_propname[] = "gpio-ranges-group-names";
  364. struct property *group_names;
  365. if (!np)
  366. return 0;
  367. group_names = of_find_property(np, group_names_propname, NULL);
  368. for (;; index++) {
  369. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  370. index, &pinspec);
  371. if (ret)
  372. break;
  373. pctldev = of_pinctrl_get(pinspec.np);
  374. of_node_put(pinspec.np);
  375. if (!pctldev)
  376. return -EPROBE_DEFER;
  377. if (pinspec.args[2]) {
  378. if (group_names) {
  379. of_property_read_string_index(np,
  380. group_names_propname,
  381. index, &name);
  382. if (strlen(name)) {
  383. pr_err("%s: Group name of numeric GPIO ranges must be the empty string.\n",
  384. np->full_name);
  385. break;
  386. }
  387. }
  388. /* npins != 0: linear range */
  389. ret = gpiochip_add_pin_range(chip,
  390. pinctrl_dev_get_devname(pctldev),
  391. pinspec.args[0],
  392. pinspec.args[1],
  393. pinspec.args[2]);
  394. if (ret)
  395. return ret;
  396. } else {
  397. /* npins == 0: special range */
  398. if (pinspec.args[1]) {
  399. pr_err("%s: Illegal gpio-range format.\n",
  400. np->full_name);
  401. break;
  402. }
  403. if (!group_names) {
  404. pr_err("%s: GPIO group range requested but no %s property.\n",
  405. np->full_name, group_names_propname);
  406. break;
  407. }
  408. ret = of_property_read_string_index(np,
  409. group_names_propname,
  410. index, &name);
  411. if (ret)
  412. break;
  413. if (!strlen(name)) {
  414. pr_err("%s: Group name of GPIO group range cannot be the empty string.\n",
  415. np->full_name);
  416. break;
  417. }
  418. ret = gpiochip_add_pingroup_range(chip, pctldev,
  419. pinspec.args[0], name);
  420. if (ret)
  421. return ret;
  422. }
  423. }
  424. return 0;
  425. }
  426. #else
  427. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  428. #endif
  429. int of_gpiochip_add(struct gpio_chip *chip)
  430. {
  431. int status;
  432. if ((!chip->of_node) && (chip->parent))
  433. chip->of_node = chip->parent->of_node;
  434. if (!chip->of_node)
  435. return 0;
  436. if (!chip->of_xlate) {
  437. chip->of_gpio_n_cells = 2;
  438. chip->of_xlate = of_gpio_simple_xlate;
  439. }
  440. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  441. return -EINVAL;
  442. status = of_gpiochip_add_pin_range(chip);
  443. if (status)
  444. return status;
  445. /* If the chip defines names itself, these take precedence */
  446. if (!chip->names)
  447. of_gpiochip_set_names(chip);
  448. of_node_get(chip->of_node);
  449. return of_gpiochip_scan_gpios(chip);
  450. }
  451. void of_gpiochip_remove(struct gpio_chip *chip)
  452. {
  453. gpiochip_remove_pin_ranges(chip);
  454. of_node_put(chip->of_node);
  455. }