• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gpiolib-of.h"
25 
26 /**
27  * of_gpio_spi_cs_get_count() - special GPIO counting for SPI
28  * Some elder GPIO controllers need special quirks. Currently we handle
29  * the Freescale GPIO controller with bindings that doesn't use the
30  * established "cs-gpios" for chip selects but instead rely on
31  * "gpios" for the chip select lines. If we detect this, we redirect
32  * the counting of "cs-gpios" to count "gpios" transparent to the
33  * driver.
34  */
of_gpio_spi_cs_get_count(struct device * dev,const char * con_id)35 int of_gpio_spi_cs_get_count(struct device *dev, const char *con_id)
36 {
37 	struct device_node *np = dev->of_node;
38 
39 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
40 		return 0;
41 	if (!con_id || strcmp(con_id, "cs"))
42 		return 0;
43 	if (!of_device_is_compatible(np, "fsl,spi") &&
44 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
45 		return 0;
46 	return of_gpio_named_count(np, "gpios");
47 }
48 
49 /*
50  * This is used by external users of of_gpio_count() from <linux/of_gpio.h>
51  *
52  * FIXME: get rid of those external users by converting them to GPIO
53  * descriptors and let them all use gpiod_get_count()
54  */
of_gpio_get_count(struct device * dev,const char * con_id)55 int of_gpio_get_count(struct device *dev, const char *con_id)
56 {
57 	int ret;
58 	char propname[32];
59 	unsigned int i;
60 
61 	ret = of_gpio_spi_cs_get_count(dev, con_id);
62 	if (ret > 0)
63 		return ret;
64 
65 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
66 		if (con_id)
67 			snprintf(propname, sizeof(propname), "%s-%s",
68 				 con_id, gpio_suffixes[i]);
69 		else
70 			snprintf(propname, sizeof(propname), "%s",
71 				 gpio_suffixes[i]);
72 
73 		ret = of_gpio_named_count(dev->of_node, propname);
74 		if (ret > 0)
75 			break;
76 	}
77 	return ret ? ret : -ENOENT;
78 }
79 
of_gpiochip_match_node_and_xlate(struct gpio_chip * chip,void * data)80 static int of_gpiochip_match_node_and_xlate(struct gpio_chip *chip, void *data)
81 {
82 	struct of_phandle_args *gpiospec = data;
83 
84 	return chip->gpiodev->dev.of_node == gpiospec->np &&
85 				chip->of_xlate &&
86 				chip->of_xlate(chip, gpiospec, NULL) >= 0;
87 }
88 
of_find_gpiochip_by_xlate(struct of_phandle_args * gpiospec)89 static struct gpio_chip *of_find_gpiochip_by_xlate(
90 					struct of_phandle_args *gpiospec)
91 {
92 	return gpiochip_find(gpiospec, of_gpiochip_match_node_and_xlate);
93 }
94 
of_xlate_and_get_gpiod_flags(struct gpio_chip * chip,struct of_phandle_args * gpiospec,enum of_gpio_flags * flags)95 static struct gpio_desc *of_xlate_and_get_gpiod_flags(struct gpio_chip *chip,
96 					struct of_phandle_args *gpiospec,
97 					enum of_gpio_flags *flags)
98 {
99 	int ret;
100 
101 	if (chip->of_gpio_n_cells != gpiospec->args_count)
102 		return ERR_PTR(-EINVAL);
103 
104 	ret = chip->of_xlate(chip, gpiospec, flags);
105 	if (ret < 0)
106 		return ERR_PTR(ret);
107 
108 	return gpiochip_get_desc(chip, ret);
109 }
110 
111 /**
112  * of_gpio_need_valid_mask() - figure out if the OF GPIO driver needs
113  * to set the .valid_mask
114  * @dev: the device for the GPIO provider
115  * @return: true if the valid mask needs to be set
116  */
of_gpio_need_valid_mask(const struct gpio_chip * gc)117 bool of_gpio_need_valid_mask(const struct gpio_chip *gc)
118 {
119 	int size;
120 	struct device_node *np = gc->of_node;
121 
122 	size = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
123 	if (size > 0 && size % 2 == 0)
124 		return true;
125 	return false;
126 }
127 
of_gpio_flags_quirks(struct device_node * np,const char * propname,enum of_gpio_flags * flags,int index)128 static void of_gpio_flags_quirks(struct device_node *np,
129 				 const char *propname,
130 				 enum of_gpio_flags *flags,
131 				 int index)
132 {
133 	/*
134 	 * Handle MMC "cd-inverted" and "wp-inverted" semantics.
135 	 */
136 	if (IS_ENABLED(CONFIG_MMC)) {
137 		/*
138 		 * Active low is the default according to the
139 		 * SDHCI specification and the device tree
140 		 * bindings. However the code in the current
141 		 * kernel was written such that the phandle
142 		 * flags were always respected, and "cd-inverted"
143 		 * would invert the flag from the device phandle.
144 		 */
145 		if (!strcmp(propname, "cd-gpios")) {
146 			if (of_property_read_bool(np, "cd-inverted"))
147 				*flags ^= OF_GPIO_ACTIVE_LOW;
148 		}
149 	}
150 	/*
151 	 * Some GPIO fixed regulator quirks.
152 	 * Note that active low is the default.
153 	 */
154 	if (IS_ENABLED(CONFIG_REGULATOR) &&
155 	    (of_device_is_compatible(np, "regulator-fixed") ||
156 	     of_device_is_compatible(np, "reg-fixed-voltage") ||
157 	     (!(strcmp(propname, "enable-gpio") &&
158 		strcmp(propname, "enable-gpios")) &&
159 	      of_device_is_compatible(np, "regulator-gpio")))) {
160 		/*
161 		 * The regulator GPIO handles are specified such that the
162 		 * presence or absence of "enable-active-high" solely controls
163 		 * the polarity of the GPIO line. Any phandle flags must
164 		 * be actively ignored.
165 		 */
166 		if (*flags & OF_GPIO_ACTIVE_LOW) {
167 			pr_warn("%s GPIO handle specifies active low - ignored\n",
168 				of_node_full_name(np));
169 			*flags &= ~OF_GPIO_ACTIVE_LOW;
170 		}
171 		if (!of_property_read_bool(np, "enable-active-high"))
172 			*flags |= OF_GPIO_ACTIVE_LOW;
173 	}
174 	/*
175 	 * Legacy open drain handling for fixed voltage regulators.
176 	 */
177 	if (IS_ENABLED(CONFIG_REGULATOR) &&
178 	    of_device_is_compatible(np, "reg-fixed-voltage") &&
179 	    of_property_read_bool(np, "gpio-open-drain")) {
180 		*flags |= (OF_GPIO_SINGLE_ENDED | OF_GPIO_OPEN_DRAIN);
181 		pr_info("%s uses legacy open drain flag - update the DTS if you can\n",
182 			of_node_full_name(np));
183 	}
184 
185 	/*
186 	 * Legacy handling of SPI active high chip select. If we have a
187 	 * property named "cs-gpios" we need to inspect the child node
188 	 * to determine if the flags should have inverted semantics.
189 	 */
190 	if (IS_ENABLED(CONFIG_SPI_MASTER) && !strcmp(propname, "cs-gpios") &&
191 	    of_property_read_bool(np, "cs-gpios")) {
192 		struct device_node *child;
193 		u32 cs;
194 		int ret;
195 
196 		for_each_child_of_node(np, child) {
197 			ret = of_property_read_u32(child, "reg", &cs);
198 			if (ret)
199 				continue;
200 			if (cs == index) {
201 				/*
202 				 * SPI children have active low chip selects
203 				 * by default. This can be specified negatively
204 				 * by just omitting "spi-cs-high" in the
205 				 * device node, or actively by tagging on
206 				 * GPIO_ACTIVE_LOW as flag in the device
207 				 * tree. If the line is simultaneously
208 				 * tagged as active low in the device tree
209 				 * and has the "spi-cs-high" set, we get a
210 				 * conflict and the "spi-cs-high" flag will
211 				 * take precedence.
212 				 */
213 				if (of_property_read_bool(child, "spi-cs-high")) {
214 					if (*flags & OF_GPIO_ACTIVE_LOW) {
215 						pr_warn("%s GPIO handle specifies active low - ignored\n",
216 							of_node_full_name(child));
217 						*flags &= ~OF_GPIO_ACTIVE_LOW;
218 					}
219 				} else {
220 					if (!(*flags & OF_GPIO_ACTIVE_LOW))
221 						pr_info("%s enforce active low on chipselect handle\n",
222 							of_node_full_name(child));
223 					*flags |= OF_GPIO_ACTIVE_LOW;
224 				}
225 				of_node_put(child);
226 				break;
227 			}
228 		}
229 	}
230 
231 	/* Legacy handling of stmmac's active-low PHY reset line */
232 	if (IS_ENABLED(CONFIG_STMMAC_ETH) &&
233 	    !strcmp(propname, "snps,reset-gpio") &&
234 	    of_property_read_bool(np, "snps,reset-active-low"))
235 		*flags |= OF_GPIO_ACTIVE_LOW;
236 }
237 
238 /**
239  * of_get_named_gpiod_flags() - Get a GPIO descriptor and flags for GPIO API
240  * @np:		device node to get GPIO from
241  * @propname:	property name containing gpio specifier(s)
242  * @index:	index of the GPIO
243  * @flags:	a flags pointer to fill in
244  *
245  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
246  * value on the error condition. If @flags is not NULL the function also fills
247  * in flags for the GPIO.
248  */
of_get_named_gpiod_flags(struct device_node * np,const char * propname,int index,enum of_gpio_flags * flags)249 static struct gpio_desc *of_get_named_gpiod_flags(struct device_node *np,
250 		     const char *propname, int index, enum of_gpio_flags *flags)
251 {
252 	struct of_phandle_args gpiospec;
253 	struct gpio_chip *chip;
254 	struct gpio_desc *desc;
255 	int ret;
256 
257 	ret = of_parse_phandle_with_args_map(np, propname, "gpio", index,
258 					     &gpiospec);
259 	if (ret) {
260 		pr_debug("%s: can't parse '%s' property of node '%pOF[%d]'\n",
261 			__func__, propname, np, index);
262 		return ERR_PTR(ret);
263 	}
264 
265 	chip = of_find_gpiochip_by_xlate(&gpiospec);
266 	if (!chip) {
267 		desc = ERR_PTR(-EPROBE_DEFER);
268 		goto out;
269 	}
270 
271 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, flags);
272 	if (IS_ERR(desc))
273 		goto out;
274 
275 	if (flags)
276 		of_gpio_flags_quirks(np, propname, flags, index);
277 
278 	pr_debug("%s: parsed '%s' property of node '%pOF[%d]' - status (%d)\n",
279 		 __func__, propname, np, index,
280 		 PTR_ERR_OR_ZERO(desc));
281 
282 out:
283 	of_node_put(gpiospec.np);
284 
285 	return desc;
286 }
287 
of_get_named_gpio_flags(struct device_node * np,const char * list_name,int index,enum of_gpio_flags * flags)288 int of_get_named_gpio_flags(struct device_node *np, const char *list_name,
289 			    int index, enum of_gpio_flags *flags)
290 {
291 	struct gpio_desc *desc;
292 
293 	desc = of_get_named_gpiod_flags(np, list_name, index, flags);
294 
295 	if (IS_ERR(desc))
296 		return PTR_ERR(desc);
297 	else
298 		return desc_to_gpio(desc);
299 }
300 EXPORT_SYMBOL_GPL(of_get_named_gpio_flags);
301 
302 /**
303  * gpiod_get_from_of_node() - obtain a GPIO from an OF node
304  * @node:	handle of the OF node
305  * @propname:	name of the DT property representing the GPIO
306  * @index:	index of the GPIO to obtain for the consumer
307  * @dflags:	GPIO initialization flags
308  * @label:	label to attach to the requested GPIO
309  *
310  * Returns:
311  * On successful request the GPIO pin is configured in accordance with
312  * provided @dflags.
313  *
314  * In case of error an ERR_PTR() is returned.
315  */
gpiod_get_from_of_node(struct device_node * node,const char * propname,int index,enum gpiod_flags dflags,const char * label)316 struct gpio_desc *gpiod_get_from_of_node(struct device_node *node,
317 					 const char *propname, int index,
318 					 enum gpiod_flags dflags,
319 					 const char *label)
320 {
321 	unsigned long lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
322 	struct gpio_desc *desc;
323 	enum of_gpio_flags flags;
324 	bool active_low = false;
325 	bool single_ended = false;
326 	bool open_drain = false;
327 	bool transitory = false;
328 	int ret;
329 
330 	desc = of_get_named_gpiod_flags(node, propname,
331 					index, &flags);
332 
333 	if (!desc || IS_ERR(desc)) {
334 		return desc;
335 	}
336 
337 	active_low = flags & OF_GPIO_ACTIVE_LOW;
338 	single_ended = flags & OF_GPIO_SINGLE_ENDED;
339 	open_drain = flags & OF_GPIO_OPEN_DRAIN;
340 	transitory = flags & OF_GPIO_TRANSITORY;
341 
342 	ret = gpiod_request(desc, label);
343 	if (ret == -EBUSY && (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE))
344 		return desc;
345 	if (ret)
346 		return ERR_PTR(ret);
347 
348 	if (active_low)
349 		lflags |= GPIO_ACTIVE_LOW;
350 
351 	if (single_ended) {
352 		if (open_drain)
353 			lflags |= GPIO_OPEN_DRAIN;
354 		else
355 			lflags |= GPIO_OPEN_SOURCE;
356 	}
357 
358 	if (transitory)
359 		lflags |= GPIO_TRANSITORY;
360 
361 	ret = gpiod_configure_flags(desc, propname, lflags, dflags);
362 	if (ret < 0) {
363 		gpiod_put(desc);
364 		return ERR_PTR(ret);
365 	}
366 
367 	return desc;
368 }
369 EXPORT_SYMBOL_GPL(gpiod_get_from_of_node);
370 
371 /*
372  * The SPI GPIO bindings happened before we managed to establish that GPIO
373  * properties should be named "foo-gpios" so we have this special kludge for
374  * them.
375  */
of_find_spi_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)376 static struct gpio_desc *of_find_spi_gpio(struct device *dev, const char *con_id,
377 					  enum of_gpio_flags *of_flags)
378 {
379 	char prop_name[32]; /* 32 is max size of property name */
380 	struct device_node *np = dev->of_node;
381 	struct gpio_desc *desc;
382 
383 	/*
384 	 * Hopefully the compiler stubs the rest of the function if this
385 	 * is false.
386 	 */
387 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
388 		return ERR_PTR(-ENOENT);
389 
390 	/* Allow this specifically for "spi-gpio" devices */
391 	if (!of_device_is_compatible(np, "spi-gpio") || !con_id)
392 		return ERR_PTR(-ENOENT);
393 
394 	/* Will be "gpio-sck", "gpio-mosi" or "gpio-miso" */
395 	snprintf(prop_name, sizeof(prop_name), "%s-%s", "gpio", con_id);
396 
397 	desc = of_get_named_gpiod_flags(np, prop_name, 0, of_flags);
398 	return desc;
399 }
400 
401 /*
402  * The old Freescale bindings use simply "gpios" as name for the chip select
403  * lines rather than "cs-gpios" like all other SPI hardware. Account for this
404  * with a special quirk.
405  */
of_find_spi_cs_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)406 static struct gpio_desc *of_find_spi_cs_gpio(struct device *dev,
407 					     const char *con_id,
408 					     unsigned int idx,
409 					     unsigned long *flags)
410 {
411 	struct device_node *np = dev->of_node;
412 
413 	if (!IS_ENABLED(CONFIG_SPI_MASTER))
414 		return ERR_PTR(-ENOENT);
415 
416 	/* Allow this specifically for Freescale devices */
417 	if (!of_device_is_compatible(np, "fsl,spi") &&
418 	    !of_device_is_compatible(np, "aeroflexgaisler,spictrl"))
419 		return ERR_PTR(-ENOENT);
420 	/* Allow only if asking for "cs-gpios" */
421 	if (!con_id || strcmp(con_id, "cs"))
422 		return ERR_PTR(-ENOENT);
423 
424 	/*
425 	 * While all other SPI controllers use "cs-gpios" the Freescale
426 	 * uses just "gpios" so translate to that when "cs-gpios" is
427 	 * requested.
428 	 */
429 	return of_find_gpio(dev, NULL, idx, flags);
430 }
431 
432 /*
433  * Some regulator bindings happened before we managed to establish that GPIO
434  * properties should be named "foo-gpios" so we have this special kludge for
435  * them.
436  */
of_find_regulator_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)437 static struct gpio_desc *of_find_regulator_gpio(struct device *dev, const char *con_id,
438 						enum of_gpio_flags *of_flags)
439 {
440 	/* These are the connection IDs we accept as legacy GPIO phandles */
441 	const char *whitelist[] = {
442 		"wlf,ldoena", /* Arizona */
443 		"wlf,ldo1ena", /* WM8994 */
444 		"wlf,ldo2ena", /* WM8994 */
445 	};
446 	struct device_node *np = dev->of_node;
447 	struct gpio_desc *desc;
448 	int i;
449 
450 	if (!IS_ENABLED(CONFIG_REGULATOR))
451 		return ERR_PTR(-ENOENT);
452 
453 	if (!con_id)
454 		return ERR_PTR(-ENOENT);
455 
456 	i = match_string(whitelist, ARRAY_SIZE(whitelist), con_id);
457 	if (i < 0)
458 		return ERR_PTR(-ENOENT);
459 
460 	desc = of_get_named_gpiod_flags(np, con_id, 0, of_flags);
461 	return desc;
462 }
463 
of_find_arizona_gpio(struct device * dev,const char * con_id,enum of_gpio_flags * of_flags)464 static struct gpio_desc *of_find_arizona_gpio(struct device *dev,
465 					      const char *con_id,
466 					      enum of_gpio_flags *of_flags)
467 {
468 	if (!IS_ENABLED(CONFIG_MFD_ARIZONA))
469 		return ERR_PTR(-ENOENT);
470 
471 	if (!con_id || strcmp(con_id, "wlf,reset"))
472 		return ERR_PTR(-ENOENT);
473 
474 	return of_get_named_gpiod_flags(dev->of_node, con_id, 0, of_flags);
475 }
476 
of_find_gpio(struct device * dev,const char * con_id,unsigned int idx,unsigned long * flags)477 struct gpio_desc *of_find_gpio(struct device *dev, const char *con_id,
478 			       unsigned int idx, unsigned long *flags)
479 {
480 	char prop_name[32]; /* 32 is max size of property name */
481 	enum of_gpio_flags of_flags;
482 	struct gpio_desc *desc;
483 	unsigned int i;
484 
485 	/* Try GPIO property "foo-gpios" and "foo-gpio" */
486 	for (i = 0; i < ARRAY_SIZE(gpio_suffixes); i++) {
487 		if (con_id)
488 			snprintf(prop_name, sizeof(prop_name), "%s-%s", con_id,
489 				 gpio_suffixes[i]);
490 		else
491 			snprintf(prop_name, sizeof(prop_name), "%s",
492 				 gpio_suffixes[i]);
493 
494 		desc = of_get_named_gpiod_flags(dev->of_node, prop_name, idx,
495 						&of_flags);
496 
497 		if (!IS_ERR(desc) || PTR_ERR(desc) != -ENOENT)
498 			break;
499 	}
500 
501 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
502 		/* Special handling for SPI GPIOs if used */
503 		desc = of_find_spi_gpio(dev, con_id, &of_flags);
504 	}
505 
506 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
507 		/* This quirk looks up flags and all */
508 		desc = of_find_spi_cs_gpio(dev, con_id, idx, flags);
509 		if (!IS_ERR(desc))
510 			return desc;
511 	}
512 
513 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT) {
514 		/* Special handling for regulator GPIOs if used */
515 		desc = of_find_regulator_gpio(dev, con_id, &of_flags);
516 	}
517 
518 	if (IS_ERR(desc) && PTR_ERR(desc) == -ENOENT)
519 		desc = of_find_arizona_gpio(dev, con_id, &of_flags);
520 
521 	if (IS_ERR(desc))
522 		return desc;
523 
524 	if (of_flags & OF_GPIO_ACTIVE_LOW)
525 		*flags |= GPIO_ACTIVE_LOW;
526 
527 	if (of_flags & OF_GPIO_SINGLE_ENDED) {
528 		if (of_flags & OF_GPIO_OPEN_DRAIN)
529 			*flags |= GPIO_OPEN_DRAIN;
530 		else
531 			*flags |= GPIO_OPEN_SOURCE;
532 	}
533 
534 	if (of_flags & OF_GPIO_TRANSITORY)
535 		*flags |= GPIO_TRANSITORY;
536 
537 	if (of_flags & OF_GPIO_PULL_UP)
538 		*flags |= GPIO_PULL_UP;
539 	if (of_flags & OF_GPIO_PULL_DOWN)
540 		*flags |= GPIO_PULL_DOWN;
541 
542 	return desc;
543 }
544 
545 /**
546  * of_parse_own_gpio() - Get a GPIO hog descriptor, names and flags for GPIO API
547  * @np:		device node to get GPIO from
548  * @chip:	GPIO chip whose hog is parsed
549  * @idx:	Index of the GPIO to parse
550  * @name:	GPIO line name
551  * @lflags:	bitmask of gpio_lookup_flags GPIO_* values - returned from
552  *		of_find_gpio() or of_parse_own_gpio()
553  * @dflags:	gpiod_flags - optional GPIO initialization flags
554  *
555  * Returns GPIO descriptor to use with Linux GPIO API, or one of the errno
556  * value on the error condition.
557  */
of_parse_own_gpio(struct device_node * np,struct gpio_chip * chip,unsigned int idx,const char ** name,unsigned long * lflags,enum gpiod_flags * dflags)558 static struct gpio_desc *of_parse_own_gpio(struct device_node *np,
559 					   struct gpio_chip *chip,
560 					   unsigned int idx, const char **name,
561 					   unsigned long *lflags,
562 					   enum gpiod_flags *dflags)
563 {
564 	struct device_node *chip_np;
565 	enum of_gpio_flags xlate_flags;
566 	struct of_phandle_args gpiospec;
567 	struct gpio_desc *desc;
568 	unsigned int i;
569 	u32 tmp;
570 	int ret;
571 
572 	chip_np = chip->of_node;
573 	if (!chip_np)
574 		return ERR_PTR(-EINVAL);
575 
576 	xlate_flags = 0;
577 	*lflags = GPIO_LOOKUP_FLAGS_DEFAULT;
578 	*dflags = 0;
579 
580 	ret = of_property_read_u32(chip_np, "#gpio-cells", &tmp);
581 	if (ret)
582 		return ERR_PTR(ret);
583 
584 	gpiospec.np = chip_np;
585 	gpiospec.args_count = tmp;
586 
587 	for (i = 0; i < tmp; i++) {
588 		ret = of_property_read_u32_index(np, "gpios", idx * tmp + i,
589 						 &gpiospec.args[i]);
590 		if (ret)
591 			return ERR_PTR(ret);
592 	}
593 
594 	desc = of_xlate_and_get_gpiod_flags(chip, &gpiospec, &xlate_flags);
595 	if (IS_ERR(desc))
596 		return desc;
597 
598 	if (xlate_flags & OF_GPIO_ACTIVE_LOW)
599 		*lflags |= GPIO_ACTIVE_LOW;
600 	if (xlate_flags & OF_GPIO_TRANSITORY)
601 		*lflags |= GPIO_TRANSITORY;
602 
603 	if (of_property_read_bool(np, "input"))
604 		*dflags |= GPIOD_IN;
605 	else if (of_property_read_bool(np, "output-low"))
606 		*dflags |= GPIOD_OUT_LOW;
607 	else if (of_property_read_bool(np, "output-high"))
608 		*dflags |= GPIOD_OUT_HIGH;
609 	else {
610 		pr_warn("GPIO line %d (%pOFn): no hogging state specified, bailing out\n",
611 			desc_to_gpio(desc), np);
612 		return ERR_PTR(-EINVAL);
613 	}
614 
615 	if (name && of_property_read_string(np, "line-name", name))
616 		*name = np->name;
617 
618 	return desc;
619 }
620 
621 /**
622  * of_gpiochip_scan_gpios - Scan gpio-controller for gpio definitions
623  * @chip:	gpio chip to act on
624  *
625  * This is only used by of_gpiochip_add to request/set GPIO initial
626  * configuration.
627  * It returns error if it fails otherwise 0 on success.
628  */
of_gpiochip_scan_gpios(struct gpio_chip * chip)629 static int of_gpiochip_scan_gpios(struct gpio_chip *chip)
630 {
631 	struct gpio_desc *desc = NULL;
632 	struct device_node *np;
633 	const char *name;
634 	unsigned long lflags;
635 	enum gpiod_flags dflags;
636 	unsigned int i;
637 	int ret;
638 
639 	for_each_available_child_of_node(chip->of_node, np) {
640 		if (!of_property_read_bool(np, "gpio-hog"))
641 			continue;
642 
643 		for (i = 0;; i++) {
644 			desc = of_parse_own_gpio(np, chip, i, &name, &lflags,
645 						 &dflags);
646 			if (IS_ERR(desc))
647 				break;
648 
649 			ret = gpiod_hog(desc, name, lflags, dflags);
650 			if (ret < 0) {
651 				of_node_put(np);
652 				return ret;
653 			}
654 		}
655 	}
656 
657 	return 0;
658 }
659 
660 /**
661  * of_gpio_simple_xlate - translate gpiospec to the GPIO number and flags
662  * @gc:		pointer to the gpio_chip structure
663  * @gpiospec:	GPIO specifier as found in the device tree
664  * @flags:	a flags pointer to fill in
665  *
666  * This is simple translation function, suitable for the most 1:1 mapped
667  * GPIO chips. This function performs only one sanity check: whether GPIO
668  * is less than ngpios (that is specified in the gpio_chip).
669  */
of_gpio_simple_xlate(struct gpio_chip * gc,const struct of_phandle_args * gpiospec,u32 * flags)670 static int of_gpio_simple_xlate(struct gpio_chip *gc,
671 				const struct of_phandle_args *gpiospec,
672 				u32 *flags)
673 {
674 	/*
675 	 * We're discouraging gpio_cells < 2, since that way you'll have to
676 	 * write your own xlate function (that will have to retrieve the GPIO
677 	 * number and the flags from a single gpio cell -- this is possible,
678 	 * but not recommended).
679 	 */
680 	if (gc->of_gpio_n_cells < 2) {
681 		WARN_ON(1);
682 		return -EINVAL;
683 	}
684 
685 	if (WARN_ON(gpiospec->args_count < gc->of_gpio_n_cells))
686 		return -EINVAL;
687 
688 	if (gpiospec->args[0] >= gc->ngpio)
689 		return -EINVAL;
690 
691 	if (flags)
692 		*flags = gpiospec->args[1];
693 
694 	return gpiospec->args[0];
695 }
696 
697 /**
698  * of_mm_gpiochip_add_data - Add memory mapped GPIO chip (bank)
699  * @np:		device node of the GPIO chip
700  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
701  * @data:	driver data to store in the struct gpio_chip
702  *
703  * To use this function you should allocate and fill mm_gc with:
704  *
705  * 1) In the gpio_chip structure:
706  *    - all the callbacks
707  *    - of_gpio_n_cells
708  *    - of_xlate callback (optional)
709  *
710  * 3) In the of_mm_gpio_chip structure:
711  *    - save_regs callback (optional)
712  *
713  * If succeeded, this function will map bank's memory and will
714  * do all necessary work for you. Then you'll able to use .regs
715  * to manage GPIOs from the callbacks.
716  */
of_mm_gpiochip_add_data(struct device_node * np,struct of_mm_gpio_chip * mm_gc,void * data)717 int of_mm_gpiochip_add_data(struct device_node *np,
718 			    struct of_mm_gpio_chip *mm_gc,
719 			    void *data)
720 {
721 	int ret = -ENOMEM;
722 	struct gpio_chip *gc = &mm_gc->gc;
723 
724 	gc->label = kasprintf(GFP_KERNEL, "%pOF", np);
725 	if (!gc->label)
726 		goto err0;
727 
728 	mm_gc->regs = of_iomap(np, 0);
729 	if (!mm_gc->regs)
730 		goto err1;
731 
732 	gc->base = -1;
733 
734 	if (mm_gc->save_regs)
735 		mm_gc->save_regs(mm_gc);
736 
737 	of_node_put(mm_gc->gc.of_node);
738 	mm_gc->gc.of_node = of_node_get(np);
739 
740 	ret = gpiochip_add_data(gc, data);
741 	if (ret)
742 		goto err2;
743 
744 	return 0;
745 err2:
746 	of_node_put(np);
747 	iounmap(mm_gc->regs);
748 err1:
749 	kfree(gc->label);
750 err0:
751 	pr_err("%pOF: GPIO chip registration failed with status %d\n", np, ret);
752 	return ret;
753 }
754 EXPORT_SYMBOL_GPL(of_mm_gpiochip_add_data);
755 
756 /**
757  * of_mm_gpiochip_remove - Remove memory mapped GPIO chip (bank)
758  * @mm_gc:	pointer to the of_mm_gpio_chip allocated structure
759  */
of_mm_gpiochip_remove(struct of_mm_gpio_chip * mm_gc)760 void of_mm_gpiochip_remove(struct of_mm_gpio_chip *mm_gc)
761 {
762 	struct gpio_chip *gc = &mm_gc->gc;
763 
764 	if (!mm_gc)
765 		return;
766 
767 	gpiochip_remove(gc);
768 	iounmap(mm_gc->regs);
769 	kfree(gc->label);
770 }
771 EXPORT_SYMBOL_GPL(of_mm_gpiochip_remove);
772 
of_gpiochip_init_valid_mask(struct gpio_chip * chip)773 static void of_gpiochip_init_valid_mask(struct gpio_chip *chip)
774 {
775 	int len, i;
776 	u32 start, count;
777 	struct device_node *np = chip->of_node;
778 
779 	len = of_property_count_u32_elems(np,  "gpio-reserved-ranges");
780 	if (len < 0 || len % 2 != 0)
781 		return;
782 
783 	for (i = 0; i < len; i += 2) {
784 		of_property_read_u32_index(np, "gpio-reserved-ranges",
785 					   i, &start);
786 		of_property_read_u32_index(np, "gpio-reserved-ranges",
787 					   i + 1, &count);
788 		if (start >= chip->ngpio || start + count > chip->ngpio)
789 			continue;
790 
791 		bitmap_clear(chip->valid_mask, start, count);
792 	}
793 };
794 
795 #ifdef CONFIG_PINCTRL
of_gpiochip_add_pin_range(struct gpio_chip * chip)796 static int of_gpiochip_add_pin_range(struct gpio_chip *chip)
797 {
798 	struct device_node *np = chip->of_node;
799 	struct of_phandle_args pinspec;
800 	struct pinctrl_dev *pctldev;
801 	int index = 0, ret;
802 	const char *name;
803 	static const char group_names_propname[] = "gpio-ranges-group-names";
804 	struct property *group_names;
805 
806 	if (!np)
807 		return 0;
808 
809 	group_names = of_find_property(np, group_names_propname, NULL);
810 
811 	for (;; index++) {
812 		ret = of_parse_phandle_with_fixed_args(np, "gpio-ranges", 3,
813 				index, &pinspec);
814 		if (ret)
815 			break;
816 
817 		pctldev = of_pinctrl_get(pinspec.np);
818 		of_node_put(pinspec.np);
819 		if (!pctldev)
820 			return -EPROBE_DEFER;
821 
822 		if (pinspec.args[2]) {
823 			if (group_names) {
824 				of_property_read_string_index(np,
825 						group_names_propname,
826 						index, &name);
827 				if (strlen(name)) {
828 					pr_err("%pOF: Group name of numeric GPIO ranges must be the empty string.\n",
829 						np);
830 					break;
831 				}
832 			}
833 			/* npins != 0: linear range */
834 			ret = gpiochip_add_pin_range(chip,
835 					pinctrl_dev_get_devname(pctldev),
836 					pinspec.args[0],
837 					pinspec.args[1],
838 					pinspec.args[2]);
839 			if (ret)
840 				return ret;
841 		} else {
842 			/* npins == 0: special range */
843 			if (pinspec.args[1]) {
844 				pr_err("%pOF: Illegal gpio-range format.\n",
845 					np);
846 				break;
847 			}
848 
849 			if (!group_names) {
850 				pr_err("%pOF: GPIO group range requested but no %s property.\n",
851 					np, group_names_propname);
852 				break;
853 			}
854 
855 			ret = of_property_read_string_index(np,
856 						group_names_propname,
857 						index, &name);
858 			if (ret)
859 				break;
860 
861 			if (!strlen(name)) {
862 				pr_err("%pOF: Group name of GPIO group range cannot be the empty string.\n",
863 				np);
864 				break;
865 			}
866 
867 			ret = gpiochip_add_pingroup_range(chip, pctldev,
868 						pinspec.args[0], name);
869 			if (ret)
870 				return ret;
871 		}
872 	}
873 
874 	return 0;
875 }
876 
877 #else
of_gpiochip_add_pin_range(struct gpio_chip * chip)878 static int of_gpiochip_add_pin_range(struct gpio_chip *chip) { return 0; }
879 #endif
880 
of_gpiochip_add(struct gpio_chip * chip)881 int of_gpiochip_add(struct gpio_chip *chip)
882 {
883 	int ret;
884 
885 	if (!chip->of_node)
886 		return 0;
887 
888 	if (!chip->of_xlate) {
889 		chip->of_gpio_n_cells = 2;
890 		chip->of_xlate = of_gpio_simple_xlate;
891 	}
892 
893 	if (chip->of_gpio_n_cells > MAX_PHANDLE_ARGS)
894 		return -EINVAL;
895 
896 	of_gpiochip_init_valid_mask(chip);
897 
898 	ret = of_gpiochip_add_pin_range(chip);
899 	if (ret)
900 		return ret;
901 
902 	/* If the chip defines names itself, these take precedence */
903 	if (!chip->names)
904 		devprop_gpiochip_set_names(chip,
905 					   of_fwnode_handle(chip->of_node));
906 
907 	of_node_get(chip->of_node);
908 
909 	ret = of_gpiochip_scan_gpios(chip);
910 	if (ret)
911 		of_node_put(chip->of_node);
912 
913 	return ret;
914 }
915 
of_gpiochip_remove(struct gpio_chip * chip)916 void of_gpiochip_remove(struct gpio_chip *chip)
917 {
918 	of_node_put(chip->of_node);
919 }
920