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