gpiolib-of.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. static void of_gpio_flags_quirks(struct device_node *np,
  50. enum of_gpio_flags *flags)
  51. {
  52. /*
  53. * Some GPIO fixed regulator quirks.
  54. * Note that active low is the default.
  55. */
  56. if (IS_ENABLED(CONFIG_REGULATOR) &&
  57. (of_device_is_compatible(np, "reg-fixed-voltage") ||
  58. of_device_is_compatible(np, "regulator-gpio"))) {
  59. /*
  60. * The regulator GPIO handles are specified such that the
  61. * presence or absence of "enable-active-high" solely controls
  62. * the polarity of the GPIO line. Any phandle flags must
  63. * be actively ignored.
  64. */
  65. if (*flags & OF_GPIO_ACTIVE_LOW) {
  66. pr_warn("%s GPIO handle specifies active low - ignored\n",
  67. of_node_full_name(np));
  68. *flags &= ~OF_GPIO_ACTIVE_LOW;
  69. }
  70. if (!of_property_read_bool(np, "enable-active-high"))
  71. *flags |= OF_GPIO_ACTIVE_LOW;
  72. }
  73. /*
  74. * Legacy open drain handling for fixed voltage regulators.
  75. */
  76. if (IS_ENABLED(CONFIG_REGULATOR) &&
  77. of_device_is_compatible(np, "reg-fixed-voltage") &&
  78. of_property_read_bool(np, "gpio-open-drain")) {
  79. *flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
  80. pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
  81. of_node_full_name(np));
  82. }
  83. }
  84. /**
  85. * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
  86. * @np: device node to get GPIO from
  87. * @propname: property name containing gpio specifier(s)
  88. * @index: index of the GPIO
  89. * @flags: a flags pointer to fill in
  90. *
  91. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  92. * value on the error condition. If @flags is not NULL the function also fills
  93. * in flags for the GPIO.
  94. */
  95. struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
  96. const char *propname, int index, enum of_gpio_flags *flags)
  97. {
  98. struct of_phandle_args gpiospec;
  99. struct gpio_chip *chip;
  100. struct gpio_desc *desc;
  101. int ret;
  102. ret = of_parse_phandle_with_args(np, propname, "#gpio-cells", index,
  103. &gpiospec);
  104. if (ret) {
  105. pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
  106. __func__, propname, np, index);
  107. return ERR_PTR(ret);
  108. }
  109. chip = of_find_gpiochip_by_xlate(&gpiospec);
  110. if (!chip) {
  111. desc = ERR_PTR(-EPROBE_DEFER);
  112. goto out;
  113. }
  114. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
  115. if (IS_ERR(desc))
  116. goto out;
  117. if (flags)
  118. of_gpio_flags_quirks(np, flags);
  119. pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
  120. __func__, propname, np, index,
  121. PTR_ERR_OR_ZERO(desc));
  122. out:
  123. of_node_put(gpiospec.np);
  124. return desc;
  125. }
  126. int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
  127. int index, enum of_gpio_flags *flags)
  128. {
  129. struct gpio_desc *desc;
  130. desc = of_get_named_gpiod_flags(np, list_name, index, flags);
  131. if (IS_ERR(desc))
  132. return PTR_ERR(desc);
  133. else
  134. return desc_to_gpio(desc);
  135. }
  136. EXPORT_SYMBOL(of_get_named_gpio_flags);
  137. /*
  138. * The SPI GPIO bindings happened before we managed to establish that GPIO
  139. * properties should be named "foo-gpios" so we have this special kludge for
  140. * them.
  141. */
  142. static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
  143. enum of_gpio_flags *of_flags)
  144. {
  145. char prop_name[32]; /* 32 is max size of property name */
  146. struct device_node *np = dev->of_node;
  147. struct gpio_desc *desc;
  148. /*
  149. * Hopefully the compiler stubs the rest of the function if this
  150. * is false.
  151. */
  152. if (!IS_ENABLED(CONFIG_SPI_MASTER))
  153. return ERR_PTR(-ENOENT);
  154. /* Allow this specifically for "spi-gpio" devices */
  155. if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
  156. return ERR_PTR(-ENOENT);
  157. /* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
  158. snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
  159. desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
  160. return desc;
  161. }
  162. /*
  163. * Some regulator bindings happened before we managed to establish that GPIO
  164. * properties should be named "foo-gpios" so we have this special kludge for
  165. * them.
  166. */
  167. static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
  168. enum of_gpio_flags *of_flags)
  169. {
  170. /* These are the connection IDs we accept as legacy GPIO phandles */
  171. const char *whitelist[] = {
  172. "wlf,ldoena", /* Arizona */
  173. "wlf,ldo1ena", /* WM8994 */
  174. "wlf,ldo2ena", /* WM8994 */
  175. };
  176. struct device_node *np = dev->of_node;
  177. struct gpio_desc *desc;
  178. int i;
  179. if (!IS_ENABLED(CONFIG_REGULATOR))
  180. return ERR_PTR(-ENOENT);
  181. if (!con_id)
  182. return ERR_PTR(-ENOENT);
  183. for (i = 0; i < ARRAY_SIZE(whitelist); i++)
  184. if (!strcmp(con_id, whitelist[i]))
  185. break;
  186. if (i == ARRAY_SIZE(whitelist))
  187. return ERR_PTR(-ENOENT);
  188. desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
  189. return desc;
  190. }
  191. struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
  192. unsigned int idx,
  193. enum gpio_lookup_flags *flags)
  194. {
  195. char prop_name[32]; /* 32 is max size of property name */
  196. enum of_gpio_flags of_flags;
  197. struct gpio_desc *desc;
  198. unsigned int i;
  199. /* Try GPIO property "foo-gpios" and "foo-gpio" */
  200. for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
  201. if (con_id)
  202. snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
  203. gpio_suffixes[i]);
  204. else
  205. snprintf(prop_name, sizeof(prop_name), "%s",
  206. gpio_suffixes[i]);
  207. desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
  208. &of_flags);
  209. /*
  210. * -EPROBE_DEFER in our case means that we found a
  211. * valid GPIO property, but no controller has been
  212. * registered so far.
  213. *
  214. * This means we don't need to look any further for
  215. * alternate name conventions, and we should really
  216. * preserve the return code for our user to be able to
  217. * retry probing later.
  218. */
  219. if (IS_ERR(desc) && PTR_ERR(desc) == -EPROBE_DEFER)
  220. return desc;
  221. if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
  222. break;
  223. }
  224. /* Special handling for SPI GPIOs if used */
  225. if (IS_ERR(desc))
  226. desc = of_find_spi_gpio(dev, con_id, &of_flags);
  227. /* Special handling for regulator GPIOs if used */
  228. if (IS_ERR(desc) && PTR_ERR(desc) != -EPROBE_DEFER)
  229. desc = of_find_regulator_gpio(dev, con_id, &of_flags);
  230. if (IS_ERR(desc))
  231. return desc;
  232. if (of_flags & OF_GPIO_ACTIVE_LOW)
  233. *flags |= GPIO_ACTIVE_LOW;
  234. if (of_flags & OF_GPIO_SINGLE_ENDED) {
  235. if (of_flags & OF_GPIO_OPEN_DRAIN)
  236. *flags |= GPIO_OPEN_DRAIN;
  237. else
  238. *flags |= GPIO_OPEN_SOURCE;
  239. }
  240. if (of_flags & OF_GPIO_TRANSITORY)
  241. *flags |= GPIO_TRANSITORY;
  242. return desc;
  243. }
  244. /**
  245. * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  246. * @np: device node to get GPIO from
  247. * @chip: GPIO chip whose hog is parsed
  248. * @idx: Index of the GPIO to parse
  249. * @name: GPIO line name
  250. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  251. * of_parse_own_gpio()
  252. * @dflags: gpiod_flags - optional GPIO initialization flags
  253. *
  254. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  255. * value on the error condition.
  256. */
  257. static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
  258. struct gpio_chip *chip,
  259. unsigned int idx, const char **name,
  260. enum gpio_lookup_flags *lflags,
  261. enum gpiod_flags *dflags)
  262. {
  263. struct device_node *chip_np;
  264. enum of_gpio_flags xlate_flags;
  265. struct of_phandle_args gpiospec;
  266. struct gpio_desc *desc;
  267. unsigned int i;
  268. u32 tmp;
  269. int ret;
  270. chip_np = chip->of_node;
  271. if (!chip_np)
  272. return ERR_PTR(-EINVAL);
  273. xlate_flags = 0;
  274. *lflags = 0;
  275. *dflags = 0;
  276. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  277. if (ret)
  278. return ERR_PTR(ret);
  279. gpiospec.np = chip_np;
  280. gpiospec.args_count = tmp;
  281. for (i = 0; i < tmp; i++) {
  282. ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
  283. &gpiospec.args[i]);
  284. if (ret)
  285. return ERR_PTR(ret);
  286. }
  287. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
  288. if (IS_ERR(desc))
  289. return desc;
  290. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  291. *lflags |= GPIO_ACTIVE_LOW;
  292. if (xlate_flags & OF_GPIO_TRANSITORY)
  293. *lflags |= GPIO_TRANSITORY;
  294. if (of_property_read_bool(np, "input"))
  295. *dflags |= GPIOD_IN;
  296. else if (of_property_read_bool(np, "output-low"))
  297. *dflags |= GPIOD_OUT_LOW;
  298. else if (of_property_read_bool(np, "output-high"))
  299. *dflags |= GPIOD_OUT_HIGH;
  300. else {
  301. pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
  302. desc_to_gpio(desc), np->name);
  303. return ERR_PTR(-EINVAL);
  304. }
  305. if (name && of_property_read_string(np, "line-name", name))
  306. *name = np->name;
  307. return desc;
  308. }
  309. /**
  310. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  311. * @chip: gpio chip to act on
  312. *
  313. * This is only used by of_gpiochip_add to request/set GPIO initial
  314. * configuration.
  315. * It returns error if it fails otherwise 0 on success.
  316. */
  317. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  318. {
  319. struct gpio_desc *desc = NULL;
  320. struct device_node *np;
  321. const char *name;
  322. enum gpio_lookup_flags lflags;
  323. enum gpiod_flags dflags;
  324. unsigned int i;
  325. int ret;
  326. for_each_available_child_of_node(chip->of_node, np) {
  327. if (!of_property_read_bool(np, "gpio-hog"))
  328. continue;
  329. for (i = 0;; i++) {
  330. desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
  331. &dflags);
  332. if (IS_ERR(desc))
  333. break;
  334. ret = gpiod_hog(desc, name, lflags, dflags);
  335. if (ret < 0) {
  336. of_node_put(np);
  337. return ret;
  338. }
  339. }
  340. }
  341. return 0;
  342. }
  343. /**
  344. * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
  345. * @gc: pointer to the gpio_chip structure
  346. * @gpiospec: GPIO specifier as found in the device tree
  347. * @flags: a flags pointer to fill in
  348. *
  349. * This is simple translation function, suitable for the most 1:1 mapped
  350. * GPIO chips. This function performs only one sanity check: whether GPIO
  351. * is less than ngpios (that is specified in the gpio_chip).
  352. */
  353. int of_gpio_simple_xlate(struct gpio_chip *gc,
  354. const struct of_phandle_args *gpiospec, u32 *flags)
  355. {
  356. /*
  357. * We're discouraging gpio_cells < 2, since that way you'll have to
  358. * write your own xlate function (that will have to retrieve the GPIO
  359. * number and the flags from a single gpio cell -- this is possible,
  360. * but not recommended).
  361. */
  362. if (gc->of_gpio_n_cells < 2) {
  363. WARN_ON(1);
  364. return -EINVAL;
  365. }
  366. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  367. return -EINVAL;
  368. if (gpiospec->args[0] >= gc->ngpio)
  369. return -EINVAL;
  370. if (flags)
  371. *flags = gpiospec->args[1];
  372. return gpiospec->args[0];
  373. }
  374. EXPORT_SYMBOL(of_gpio_simple_xlate);
  375. /**
  376. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  377. * @np: device node of the GPIO chip
  378. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  379. * @data: driver data to store in the struct gpio_chip
  380. *
  381. * To use this function you should allocate and fill mm_gc with:
  382. *
  383. * 1) In the gpio_chip structure:
  384. * - all the callbacks
  385. * - of_gpio_n_cells
  386. * - of_xlate callback (optional)
  387. *
  388. * 3) In the of_mm_gpio_chip structure:
  389. * - save_regs callback (optional)
  390. *
  391. * If succeeded, this function will map bank's memory and will
  392. * do all necessary work for you. Then you'll able to use .regs
  393. * to manage GPIOs from the callbacks.
  394. */
  395. int of_mm_gpiochip_add_data(struct device_node *np,
  396. struct of_mm_gpio_chip *mm_gc,
  397. void *data)
  398. {
  399. int ret = -ENOMEM;
  400. struct gpio_chip *gc = &mm_gc->gc;
  401. gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
  402. if (!gc->label)
  403. goto err0;
  404. mm_gc->regs = of_iomap(np, 0);
  405. if (!mm_gc->regs)
  406. goto err1;
  407. gc->base = -1;
  408. if (mm_gc->save_regs)
  409. mm_gc->save_regs(mm_gc);
  410. mm_gc->gc.of_node = np;
  411. ret = gpiochip_add_data(gc, data);
  412. if (ret)
  413. goto err2;
  414. return 0;
  415. err2:
  416. iounmap(mm_gc->regs);
  417. err1:
  418. kfree(gc->label);
  419. err0:
  420. pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
  421. return ret;
  422. }
  423. EXPORT_SYMBOL(of_mm_gpiochip_add_data);
  424. /**
  425. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  426. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  427. */
  428. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  429. {
  430. struct gpio_chip *gc = &mm_gc->gc;
  431. if (!mm_gc)
  432. return;
  433. gpiochip_remove(gc);
  434. iounmap(mm_gc->regs);
  435. kfree(gc->label);
  436. }
  437. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  438. #ifdef CONFIG_PINCTRL
  439. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  440. {
  441. struct device_node *np = chip->of_node;
  442. struct of_phandle_args pinspec;
  443. struct pinctrl_dev *pctldev;
  444. int index = 0, ret;
  445. const char *name;
  446. static const char group_names_propname[] = "gpio-ranges-group-names";
  447. struct property *group_names;
  448. if (!np)
  449. return 0;
  450. group_names = of_find_property(np, group_names_propname, NULL);
  451. for (;; index++) {
  452. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  453. index, &pinspec);
  454. if (ret)
  455. break;
  456. pctldev = of_pinctrl_get(pinspec.np);
  457. of_node_put(pinspec.np);
  458. if (!pctldev)
  459. return -EPROBE_DEFER;
  460. if (pinspec.args[2]) {
  461. if (group_names) {
  462. of_property_read_string_index(np,
  463. group_names_propname,
  464. index, &name);
  465. if (strlen(name)) {
  466. pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
  467. np);
  468. break;
  469. }
  470. }
  471. /* npins != 0: linear range */
  472. ret = gpiochip_add_pin_range(chip,
  473. pinctrl_dev_get_devname(pctldev),
  474. pinspec.args[0],
  475. pinspec.args[1],
  476. pinspec.args[2]);
  477. if (ret)
  478. return ret;
  479. } else {
  480. /* npins == 0: special range */
  481. if (pinspec.args[1]) {
  482. pr_err("%pOF: Illegal gpio-range format.\n",
  483. np);
  484. break;
  485. }
  486. if (!group_names) {
  487. pr_err("%pOF: GPIO group range requested but no %s property.\n",
  488. np, group_names_propname);
  489. break;
  490. }
  491. ret = of_property_read_string_index(np,
  492. group_names_propname,
  493. index, &name);
  494. if (ret)
  495. break;
  496. if (!strlen(name)) {
  497. pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
  498. np);
  499. break;
  500. }
  501. ret = gpiochip_add_pingroup_range(chip, pctldev,
  502. pinspec.args[0], name);
  503. if (ret)
  504. return ret;
  505. }
  506. }
  507. return 0;
  508. }
  509. #else
  510. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  511. #endif
  512. int of_gpiochip_add(struct gpio_chip *chip)
  513. {
  514. int status;
  515. if ((!chip->of_node) && (chip->parent))
  516. chip->of_node = chip->parent->of_node;
  517. if (!chip->of_node)
  518. return 0;
  519. if (!chip->of_xlate) {
  520. chip->of_gpio_n_cells = 2;
  521. chip->of_xlate = of_gpio_simple_xlate;
  522. }
  523. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  524. return -EINVAL;
  525. status = of_gpiochip_add_pin_range(chip);
  526. if (status)
  527. return status;
  528. /* If the chip defines names itself, these take precedence */
  529. if (!chip->names)
  530. devprop_gpiochip_set_names(chip,
  531. of_fwnode_handle(chip->of_node));
  532. of_node_get(chip->of_node);
  533. return of_gpiochip_scan_gpios(chip);
  534. }
  535. void of_gpiochip_remove(struct gpio_chip *chip)
  536. {
  537. gpiochip_remove_pin_ranges(chip);
  538. of_node_put(chip->of_node);
  539. }