gpiolib-of.c 17 KB

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