gpiolib-of.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. if (!IS_ERR(desc) || (PTR_ERR(desc) != -ENOENT))
  210. break;
  211. }
  212. /* Special handling for SPI GPIOs if used */
  213. if (IS_ERR(desc))
  214. desc = of_find_spi_gpio(dev, con_id, &of_flags);
  215. /* Special handling for regulator GPIOs if used */
  216. if (IS_ERR(desc))
  217. desc = of_find_regulator_gpio(dev, con_id, &of_flags);
  218. if (IS_ERR(desc))
  219. return desc;
  220. if (of_flags & OF_GPIO_ACTIVE_LOW)
  221. *flags |= GPIO_ACTIVE_LOW;
  222. if (of_flags & OF_GPIO_SINGLE_ENDED) {
  223. if (of_flags & OF_GPIO_OPEN_DRAIN)
  224. *flags |= GPIO_OPEN_DRAIN;
  225. else
  226. *flags |= GPIO_OPEN_SOURCE;
  227. }
  228. if (of_flags & OF_GPIO_TRANSITORY)
  229. *flags |= GPIO_TRANSITORY;
  230. return desc;
  231. }
  232. /**
  233. * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
  234. * @np: device node to get GPIO from
  235. * @chip: GPIO chip whose hog is parsed
  236. * @idx: Index of the GPIO to parse
  237. * @name: GPIO line name
  238. * @lflags: gpio_lookup_flags - returned from of_find_gpio() or
  239. * of_parse_own_gpio()
  240. * @dflags: gpiod_flags - optional GPIO initialization flags
  241. *
  242. * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
  243. * value on the error condition.
  244. */
  245. static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
  246. struct gpio_chip *chip,
  247. unsigned int idx, const char **name,
  248. enum gpio_lookup_flags *lflags,
  249. enum gpiod_flags *dflags)
  250. {
  251. struct device_node *chip_np;
  252. enum of_gpio_flags xlate_flags;
  253. struct of_phandle_args gpiospec;
  254. struct gpio_desc *desc;
  255. unsigned int i;
  256. u32 tmp;
  257. int ret;
  258. chip_np = chip->of_node;
  259. if (!chip_np)
  260. return ERR_PTR(-EINVAL);
  261. xlate_flags = 0;
  262. *lflags = 0;
  263. *dflags = 0;
  264. ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
  265. if (ret)
  266. return ERR_PTR(ret);
  267. gpiospec.np = chip_np;
  268. gpiospec.args_count = tmp;
  269. for (i = 0; i < tmp; i++) {
  270. ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
  271. &gpiospec.args[i]);
  272. if (ret)
  273. return ERR_PTR(ret);
  274. }
  275. desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
  276. if (IS_ERR(desc))
  277. return desc;
  278. if (xlate_flags & OF_GPIO_ACTIVE_LOW)
  279. *lflags |= GPIO_ACTIVE_LOW;
  280. if (xlate_flags & OF_GPIO_TRANSITORY)
  281. *lflags |= GPIO_TRANSITORY;
  282. if (of_property_read_bool(np, "input"))
  283. *dflags |= GPIOD_IN;
  284. else if (of_property_read_bool(np, "output-low"))
  285. *dflags |= GPIOD_OUT_LOW;
  286. else if (of_property_read_bool(np, "output-high"))
  287. *dflags |= GPIOD_OUT_HIGH;
  288. else {
  289. pr_warn("GPIO line %d (%s): no hogging state specified, bailing out\n",
  290. desc_to_gpio(desc), np->name);
  291. return ERR_PTR(-EINVAL);
  292. }
  293. if (name && of_property_read_string(np, "line-name", name))
  294. *name = np->name;
  295. return desc;
  296. }
  297. /**
  298. * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
  299. * @chip: gpio chip to act on
  300. *
  301. * This is only used by of_gpiochip_add to request/set GPIO initial
  302. * configuration.
  303. * It returns error if it fails otherwise 0 on success.
  304. */
  305. static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
  306. {
  307. struct gpio_desc *desc = NULL;
  308. struct device_node *np;
  309. const char *name;
  310. enum gpio_lookup_flags lflags;
  311. enum gpiod_flags dflags;
  312. unsigned int i;
  313. int ret;
  314. for_each_available_child_of_node(chip->of_node, np) {
  315. if (!of_property_read_bool(np, "gpio-hog"))
  316. continue;
  317. for (i = 0;; i++) {
  318. desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
  319. &dflags);
  320. if (IS_ERR(desc))
  321. break;
  322. ret = gpiod_hog(desc, name, lflags, dflags);
  323. if (ret < 0) {
  324. of_node_put(np);
  325. return ret;
  326. }
  327. }
  328. }
  329. return 0;
  330. }
  331. /**
  332. * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
  333. * @gc: pointer to the gpio_chip structure
  334. * @gpiospec: GPIO specifier as found in the device tree
  335. * @flags: a flags pointer to fill in
  336. *
  337. * This is simple translation function, suitable for the most 1:1 mapped
  338. * GPIO chips. This function performs only one sanity check: whether GPIO
  339. * is less than ngpios (that is specified in the gpio_chip).
  340. */
  341. int of_gpio_simple_xlate(struct gpio_chip *gc,
  342. const struct of_phandle_args *gpiospec, u32 *flags)
  343. {
  344. /*
  345. * We're discouraging gpio_cells < 2, since that way you'll have to
  346. * write your own xlate function (that will have to retrieve the GPIO
  347. * number and the flags from a single gpio cell -- this is possible,
  348. * but not recommended).
  349. */
  350. if (gc->of_gpio_n_cells < 2) {
  351. WARN_ON(1);
  352. return -EINVAL;
  353. }
  354. if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
  355. return -EINVAL;
  356. if (gpiospec->args[0] >= gc->ngpio)
  357. return -EINVAL;
  358. if (flags)
  359. *flags = gpiospec->args[1];
  360. return gpiospec->args[0];
  361. }
  362. EXPORT_SYMBOL(of_gpio_simple_xlate);
  363. /**
  364. * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
  365. * @np: device node of the GPIO chip
  366. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  367. * @data: driver data to store in the struct gpio_chip
  368. *
  369. * To use this function you should allocate and fill mm_gc with:
  370. *
  371. * 1) In the gpio_chip structure:
  372. * - all the callbacks
  373. * - of_gpio_n_cells
  374. * - of_xlate callback (optional)
  375. *
  376. * 3) In the of_mm_gpio_chip structure:
  377. * - save_regs callback (optional)
  378. *
  379. * If succeeded, this function will map bank's memory and will
  380. * do all necessary work for you. Then you'll able to use .regs
  381. * to manage GPIOs from the callbacks.
  382. */
  383. int of_mm_gpiochip_add_data(struct device_node *np,
  384. struct of_mm_gpio_chip *mm_gc,
  385. void *data)
  386. {
  387. int ret = -ENOMEM;
  388. struct gpio_chip *gc = &mm_gc->gc;
  389. gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
  390. if (!gc->label)
  391. goto err0;
  392. mm_gc->regs = of_iomap(np, 0);
  393. if (!mm_gc->regs)
  394. goto err1;
  395. gc->base = -1;
  396. if (mm_gc->save_regs)
  397. mm_gc->save_regs(mm_gc);
  398. mm_gc->gc.of_node = np;
  399. ret = gpiochip_add_data(gc, data);
  400. if (ret)
  401. goto err2;
  402. return 0;
  403. err2:
  404. iounmap(mm_gc->regs);
  405. err1:
  406. kfree(gc->label);
  407. err0:
  408. pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
  409. return ret;
  410. }
  411. EXPORT_SYMBOL(of_mm_gpiochip_add_data);
  412. /**
  413. * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
  414. * @mm_gc: pointer to the of_mm_gpio_chip allocated structure
  415. */
  416. void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
  417. {
  418. struct gpio_chip *gc = &mm_gc->gc;
  419. if (!mm_gc)
  420. return;
  421. gpiochip_remove(gc);
  422. iounmap(mm_gc->regs);
  423. kfree(gc->label);
  424. }
  425. EXPORT_SYMBOL(of_mm_gpiochip_remove);
  426. #ifdef CONFIG_PINCTRL
  427. static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
  428. {
  429. struct device_node *np = chip->of_node;
  430. struct of_phandle_args pinspec;
  431. struct pinctrl_dev *pctldev;
  432. int index = 0, ret;
  433. const char *name;
  434. static const char group_names_propname[] = "gpio-ranges-group-names";
  435. struct property *group_names;
  436. if (!np)
  437. return 0;
  438. group_names = of_find_property(np, group_names_propname, NULL);
  439. for (;; index++) {
  440. ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
  441. index, &pinspec);
  442. if (ret)
  443. break;
  444. pctldev = of_pinctrl_get(pinspec.np);
  445. of_node_put(pinspec.np);
  446. if (!pctldev)
  447. return -EPROBE_DEFER;
  448. if (pinspec.args[2]) {
  449. if (group_names) {
  450. of_property_read_string_index(np,
  451. group_names_propname,
  452. index, &name);
  453. if (strlen(name)) {
  454. pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
  455. np);
  456. break;
  457. }
  458. }
  459. /* npins != 0: linear range */
  460. ret = gpiochip_add_pin_range(chip,
  461. pinctrl_dev_get_devname(pctldev),
  462. pinspec.args[0],
  463. pinspec.args[1],
  464. pinspec.args[2]);
  465. if (ret)
  466. return ret;
  467. } else {
  468. /* npins == 0: special range */
  469. if (pinspec.args[1]) {
  470. pr_err("%pOF: Illegal gpio-range format.\n",
  471. np);
  472. break;
  473. }
  474. if (!group_names) {
  475. pr_err("%pOF: GPIO group range requested but no %s property.\n",
  476. np, group_names_propname);
  477. break;
  478. }
  479. ret = of_property_read_string_index(np,
  480. group_names_propname,
  481. index, &name);
  482. if (ret)
  483. break;
  484. if (!strlen(name)) {
  485. pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
  486. np);
  487. break;
  488. }
  489. ret = gpiochip_add_pingroup_range(chip, pctldev,
  490. pinspec.args[0], name);
  491. if (ret)
  492. return ret;
  493. }
  494. }
  495. return 0;
  496. }
  497. #else
  498. static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
  499. #endif
  500. int of_gpiochip_add(struct gpio_chip *chip)
  501. {
  502. int status;
  503. if ((!chip->of_node) && (chip->parent))
  504. chip->of_node = chip->parent->of_node;
  505. if (!chip->of_node)
  506. return 0;
  507. if (!chip->of_xlate) {
  508. chip->of_gpio_n_cells = 2;
  509. chip->of_xlate = of_gpio_simple_xlate;
  510. }
  511. if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
  512. return -EINVAL;
  513. status = of_gpiochip_add_pin_range(chip);
  514. if (status)
  515. return status;
  516. /* If the chip defines names itself, these take precedence */
  517. if (!chip->names)
  518. devprop_gpiochip_set_names(chip,
  519. of_fwnode_handle(chip->of_node));
  520. of_node_get(chip->of_node);
  521. return of_gpiochip_scan_gpios(chip);
  522. }
  523. void of_gpiochip_remove(struct gpio_chip *chip)
  524. {
  525. gpiochip_remove_pin_ranges(chip);
  526. of_node_put(chip->of_node);
  527. }