gpiolib-of.c 17 KB

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