1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5 
6 #include <common.h>
7 #include <dm.h>
8 #include <log.h>
9 #include <dm/devres.h>
10 #include <dm/device_compat.h>
11 #include <dm/device-internal.h>
12 #include <dm/lists.h>
13 #include <dm/uclass-internal.h>
14 #include <dt-bindings/gpio/gpio.h>
15 #include <errno.h>
16 #include <fdtdec.h>
17 #include <malloc.h>
18 #include <acpi/acpi_device.h>
19 #include <asm/global_data.h>
20 #include <asm/gpio.h>
21 #include <dm/device_compat.h>
22 #include <linux/bug.h>
23 #include <linux/ctype.h>
24 
25 DECLARE_GLOBAL_DATA_PTR;
26 
27 /**
28  * gpio_desc_init() - Initialize the GPIO descriptor
29  *
30  * @desc:	GPIO descriptor to initialize
31  * @dev:	GPIO device
32  * @offset:	Offset of device GPIO
33  */
gpio_desc_init(struct gpio_desc * desc,struct udevice * dev,uint offset)34 static void gpio_desc_init(struct gpio_desc *desc,
35 			   struct udevice *dev,
36 			   uint offset)
37 {
38 	desc->dev = dev;
39 	desc->offset = offset;
40 	desc->flags = 0;
41 }
42 
43 /**
44  * gpio_to_device() - Convert global GPIO number to device, number
45  *
46  * Convert the GPIO number to an entry in the list of GPIOs
47  * or GPIO blocks registered with the GPIO controller. Returns
48  * entry on success, NULL on error.
49  *
50  * @gpio:	The numeric representation of the GPIO
51  * @desc:	Returns description (desc->flags will always be 0)
52  * @return 0 if found, -ENOENT if not found
53  */
gpio_to_device(unsigned int gpio,struct gpio_desc * desc)54 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
55 {
56 	struct gpio_dev_priv *uc_priv;
57 	struct udevice *dev;
58 	int ret;
59 
60 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
61 	     dev;
62 	     ret = uclass_next_device(&dev)) {
63 		uc_priv = dev_get_uclass_priv(dev);
64 		if (gpio >= uc_priv->gpio_base &&
65 		    gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
66 			gpio_desc_init(desc, dev, gpio - uc_priv->gpio_base);
67 			return 0;
68 		}
69 	}
70 
71 	/* No such GPIO */
72 	return ret ? ret : -ENOENT;
73 }
74 
75 #if CONFIG_IS_ENABLED(DM_GPIO_LOOKUP_LABEL)
76 /**
77  * dm_gpio_lookup_label() - look for name in gpio device
78  *
79  * search in uc_priv, if there is a gpio with labelname same
80  * as name.
81  *
82  * @name:	name which is searched
83  * @uc_priv:	gpio_dev_priv pointer.
84  * @offset:	gpio offset within the device
85  * @return:	0 if found, -ENOENT if not.
86  */
dm_gpio_lookup_label(const char * name,struct gpio_dev_priv * uc_priv,ulong * offset)87 static int dm_gpio_lookup_label(const char *name,
88 				struct gpio_dev_priv *uc_priv, ulong *offset)
89 {
90 	int len;
91 	int i;
92 
93 	*offset = -1;
94 	len = strlen(name);
95 	for (i = 0; i < uc_priv->gpio_count; i++) {
96 		if (!uc_priv->name[i])
97 			continue;
98 		if (!strncmp(name, uc_priv->name[i], len)) {
99 			*offset = i;
100 			return 0;
101 		}
102 	}
103 	return -ENOENT;
104 }
105 #else
106 static int
dm_gpio_lookup_label(const char * name,struct gpio_dev_priv * uc_priv,ulong * offset)107 dm_gpio_lookup_label(const char *name, struct gpio_dev_priv *uc_priv,
108 		     ulong *offset)
109 {
110 	return -ENOENT;
111 }
112 #endif
113 
dm_gpio_lookup_name(const char * name,struct gpio_desc * desc)114 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
115 {
116 	struct gpio_dev_priv *uc_priv = NULL;
117 	struct udevice *dev;
118 	ulong offset;
119 	int numeric;
120 	int ret;
121 
122 	numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
123 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
124 	     dev;
125 	     ret = uclass_next_device(&dev)) {
126 		int len;
127 
128 		uc_priv = dev_get_uclass_priv(dev);
129 		if (numeric != -1) {
130 			offset = numeric - uc_priv->gpio_base;
131 			/* Allow GPIOs to be numbered from 0 */
132 			if (offset < uc_priv->gpio_count)
133 				break;
134 		}
135 
136 		len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
137 
138 		if (!strncasecmp(name, uc_priv->bank_name, len)) {
139 			if (!strict_strtoul(name + len, 10, &offset))
140 				break;
141 		}
142 
143 		/*
144 		 * if we did not found a gpio through its bank
145 		 * name, we search for a valid gpio label.
146 		 */
147 		if (!dm_gpio_lookup_label(name, uc_priv, &offset))
148 			break;
149 	}
150 
151 	if (!dev)
152 		return ret ? ret : -EINVAL;
153 
154 	gpio_desc_init(desc, dev, offset);
155 
156 	return 0;
157 }
158 
gpio_lookup_name(const char * name,struct udevice ** devp,unsigned int * offsetp,unsigned int * gpiop)159 int gpio_lookup_name(const char *name, struct udevice **devp,
160 		     unsigned int *offsetp, unsigned int *gpiop)
161 {
162 	struct gpio_desc desc;
163 	int ret;
164 
165 	if (devp)
166 		*devp = NULL;
167 	ret = dm_gpio_lookup_name(name, &desc);
168 	if (ret)
169 		return ret;
170 
171 	if (devp)
172 		*devp = desc.dev;
173 	if (offsetp)
174 		*offsetp = desc.offset;
175 	if (gpiop) {
176 		struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
177 
178 		*gpiop = uc_priv->gpio_base + desc.offset;
179 	}
180 
181 	return 0;
182 }
183 
gpio_xlate_offs_flags(struct udevice * dev,struct gpio_desc * desc,struct ofnode_phandle_args * args)184 int gpio_xlate_offs_flags(struct udevice *dev, struct gpio_desc *desc,
185 			  struct ofnode_phandle_args *args)
186 {
187 	if (args->args_count < 1)
188 		return -EINVAL;
189 
190 	desc->offset = args->args[0];
191 
192 	if (args->args_count < 2)
193 		return 0;
194 
195 	desc->flags = 0;
196 	if (args->args[1] & GPIO_ACTIVE_LOW)
197 		desc->flags |= GPIOD_ACTIVE_LOW;
198 
199 	/*
200 	 * need to test 2 bits for gpio output binding:
201 	 * OPEN_DRAIN (0x6) = SINGLE_ENDED (0x2) | LINE_OPEN_DRAIN (0x4)
202 	 * OPEN_SOURCE (0x2) = SINGLE_ENDED (0x2) | LINE_OPEN_SOURCE (0x0)
203 	 */
204 	if (args->args[1] & GPIO_SINGLE_ENDED) {
205 		if (args->args[1] & GPIO_LINE_OPEN_DRAIN)
206 			desc->flags |= GPIOD_OPEN_DRAIN;
207 		else
208 			desc->flags |= GPIOD_OPEN_SOURCE;
209 	}
210 
211 	if (args->args[1] & GPIO_PULL_UP)
212 		desc->flags |= GPIOD_PULL_UP;
213 
214 	if (args->args[1] & GPIO_PULL_DOWN)
215 		desc->flags |= GPIOD_PULL_DOWN;
216 
217 	return 0;
218 }
219 
gpio_find_and_xlate(struct gpio_desc * desc,struct ofnode_phandle_args * args)220 static int gpio_find_and_xlate(struct gpio_desc *desc,
221 			       struct ofnode_phandle_args *args)
222 {
223 	struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
224 
225 	if (ops->xlate)
226 		return ops->xlate(desc->dev, desc, args);
227 	else
228 		return gpio_xlate_offs_flags(desc->dev, desc, args);
229 }
230 
231 #if defined(CONFIG_GPIO_HOG)
232 
233 struct gpio_hog_priv {
234 	struct gpio_desc gpiod;
235 };
236 
237 struct gpio_hog_data {
238 	int gpiod_flags;
239 	int value;
240 	u32 val[2];
241 };
242 
gpio_hog_of_to_plat(struct udevice * dev)243 static int gpio_hog_of_to_plat(struct udevice *dev)
244 {
245 	struct gpio_hog_data *plat = dev_get_plat(dev);
246 	const char *nodename;
247 	int ret;
248 
249 	plat->value = 0;
250 	if (dev_read_bool(dev, "input")) {
251 		plat->gpiod_flags = GPIOD_IS_IN;
252 	} else if (dev_read_bool(dev, "output-high")) {
253 		plat->value = 1;
254 		plat->gpiod_flags = GPIOD_IS_OUT;
255 	} else if (dev_read_bool(dev, "output-low")) {
256 		plat->gpiod_flags = GPIOD_IS_OUT;
257 	} else {
258 		printf("%s: missing gpio-hog state.\n", __func__);
259 		return -EINVAL;
260 	}
261 	ret = dev_read_u32_array(dev, "gpios", plat->val, 2);
262 	if (ret) {
263 		printf("%s: wrong gpios property, 2 values needed %d\n",
264 		       __func__, ret);
265 		return ret;
266 	}
267 	nodename = dev_read_string(dev, "line-name");
268 	if (nodename)
269 		device_set_name(dev, nodename);
270 
271 	return 0;
272 }
273 
gpio_hog_probe(struct udevice * dev)274 static int gpio_hog_probe(struct udevice *dev)
275 {
276 	struct gpio_hog_data *plat = dev_get_plat(dev);
277 	struct gpio_hog_priv *priv = dev_get_priv(dev);
278 	int ret;
279 
280 	ret = gpio_dev_request_index(dev->parent, dev->name, "gpio-hog",
281 				     plat->val[0], plat->gpiod_flags,
282 				     plat->val[1], &priv->gpiod);
283 	if (ret < 0) {
284 		debug("%s: node %s could not get gpio.\n", __func__,
285 		      dev->name);
286 		return ret;
287 	}
288 
289 	if (plat->gpiod_flags == GPIOD_IS_OUT) {
290 		ret = dm_gpio_set_value(&priv->gpiod, plat->value);
291 		if (ret < 0) {
292 			debug("%s: node %s could not set gpio.\n", __func__,
293 			      dev->name);
294 			return ret;
295 		}
296 	}
297 
298 	return 0;
299 }
300 
gpio_hog_probe_all(void)301 int gpio_hog_probe_all(void)
302 {
303 	struct udevice *dev;
304 	int ret;
305 	int retval = 0;
306 
307 	for (uclass_first_device(UCLASS_NOP, &dev);
308 	     dev;
309 	     uclass_find_next_device(&dev)) {
310 		if (dev->driver == DM_DRIVER_GET(gpio_hog)) {
311 			ret = device_probe(dev);
312 			if (ret) {
313 				printf("Failed to probe device %s err: %d\n",
314 				       dev->name, ret);
315 				retval = ret;
316 			}
317 		}
318 	}
319 
320 	return retval;
321 }
322 
gpio_hog_lookup_name(const char * name,struct gpio_desc ** desc)323 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
324 {
325 	struct udevice *dev;
326 
327 	*desc = NULL;
328 	gpio_hog_probe_all();
329 	if (!uclass_get_device_by_name(UCLASS_NOP, name, &dev)) {
330 		struct gpio_hog_priv *priv = dev_get_priv(dev);
331 
332 		*desc = &priv->gpiod;
333 		return 0;
334 	}
335 
336 	return -ENODEV;
337 }
338 
339 U_BOOT_DRIVER(gpio_hog) = {
340 	.name	= "gpio_hog",
341 	.id	= UCLASS_NOP,
342 	.of_to_plat = gpio_hog_of_to_plat,
343 	.probe = gpio_hog_probe,
344 	.priv_auto	= sizeof(struct gpio_hog_priv),
345 	.plat_auto	= sizeof(struct gpio_hog_data),
346 };
347 #else
gpio_hog_lookup_name(const char * name,struct gpio_desc ** desc)348 int gpio_hog_lookup_name(const char *name, struct gpio_desc **desc)
349 {
350 	return 0;
351 }
352 #endif
353 
dm_gpio_request(struct gpio_desc * desc,const char * label)354 int dm_gpio_request(struct gpio_desc *desc, const char *label)
355 {
356 	struct udevice *dev = desc->dev;
357 	struct gpio_dev_priv *uc_priv;
358 	char *str;
359 	int ret;
360 
361 	uc_priv = dev_get_uclass_priv(dev);
362 	if (uc_priv->name[desc->offset])
363 		return -EBUSY;
364 	str = strdup(label);
365 	if (!str)
366 		return -ENOMEM;
367 	if (gpio_get_ops(dev)->request) {
368 		ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
369 		if (ret) {
370 			free(str);
371 			return ret;
372 		}
373 	}
374 	uc_priv->name[desc->offset] = str;
375 
376 	return 0;
377 }
378 
dm_gpio_requestf(struct gpio_desc * desc,const char * fmt,...)379 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
380 {
381 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
382 	va_list args;
383 	char buf[40];
384 
385 	va_start(args, fmt);
386 	vscnprintf(buf, sizeof(buf), fmt, args);
387 	va_end(args);
388 	return dm_gpio_request(desc, buf);
389 #else
390 	return dm_gpio_request(desc, fmt);
391 #endif
392 }
393 
394 /**
395  * gpio_request() - [COMPAT] Request GPIO
396  * gpio:	GPIO number
397  * label:	Name for the requested GPIO
398  *
399  * The label is copied and allocated so the caller does not need to keep
400  * the pointer around.
401  *
402  * This function implements the API that's compatible with current
403  * GPIO API used in U-Boot. The request is forwarded to particular
404  * GPIO driver. Returns 0 on success, negative value on error.
405  */
gpio_request(unsigned gpio,const char * label)406 int gpio_request(unsigned gpio, const char *label)
407 {
408 	struct gpio_desc desc;
409 	int ret;
410 
411 	ret = gpio_to_device(gpio, &desc);
412 	if (ret)
413 		return ret;
414 
415 	return dm_gpio_request(&desc, label);
416 }
417 
418 /**
419  * gpio_requestf() - [COMPAT] Request GPIO
420  * @gpio:	GPIO number
421  * @fmt:	Format string for the requested GPIO
422  * @...:	Arguments for the printf() format string
423  *
424  * This function implements the API that's compatible with current
425  * GPIO API used in U-Boot. The request is forwarded to particular
426  * GPIO driver. Returns 0 on success, negative value on error.
427  */
gpio_requestf(unsigned gpio,const char * fmt,...)428 int gpio_requestf(unsigned gpio, const char *fmt, ...)
429 {
430 #if !defined(CONFIG_SPL_BUILD) || !CONFIG_IS_ENABLED(USE_TINY_PRINTF)
431 	va_list args;
432 	char buf[40];
433 
434 	va_start(args, fmt);
435 	vscnprintf(buf, sizeof(buf), fmt, args);
436 	va_end(args);
437 	return gpio_request(gpio, buf);
438 #else
439 	return gpio_request(gpio, fmt);
440 #endif
441 }
442 
_dm_gpio_free(struct udevice * dev,uint offset)443 int _dm_gpio_free(struct udevice *dev, uint offset)
444 {
445 	struct gpio_dev_priv *uc_priv;
446 	int ret;
447 
448 	uc_priv = dev_get_uclass_priv(dev);
449 	if (!uc_priv->name[offset])
450 		return -ENXIO;
451 	if (gpio_get_ops(dev)->rfree) {
452 		ret = gpio_get_ops(dev)->rfree(dev, offset);
453 		if (ret)
454 			return ret;
455 	}
456 
457 	free(uc_priv->name[offset]);
458 	uc_priv->name[offset] = NULL;
459 
460 	return 0;
461 }
462 
463 /**
464  * gpio_free() - [COMPAT] Relinquish GPIO
465  * gpio:	GPIO number
466  *
467  * This function implements the API that's compatible with current
468  * GPIO API used in U-Boot. The request is forwarded to particular
469  * GPIO driver. Returns 0 on success, negative value on error.
470  */
gpio_free(unsigned gpio)471 int gpio_free(unsigned gpio)
472 {
473 	struct gpio_desc desc;
474 	int ret;
475 
476 	ret = gpio_to_device(gpio, &desc);
477 	if (ret)
478 		return ret;
479 
480 	return _dm_gpio_free(desc.dev, desc.offset);
481 }
482 
check_reserved(const struct gpio_desc * desc,const char * func)483 static int check_reserved(const struct gpio_desc *desc, const char *func)
484 {
485 	struct gpio_dev_priv *uc_priv;
486 
487 	if (!dm_gpio_is_valid(desc))
488 		return -ENOENT;
489 
490 	uc_priv = dev_get_uclass_priv(desc->dev);
491 	if (!uc_priv->name[desc->offset]) {
492 		printf("%s: %s: error: gpio %s%d not reserved\n",
493 		       desc->dev->name, func,
494 		       uc_priv->bank_name ? uc_priv->bank_name : "",
495 		       desc->offset);
496 		return -EBUSY;
497 	}
498 
499 	return 0;
500 }
501 
502 /**
503  * gpio_direction_input() - [COMPAT] Set GPIO direction to input
504  * gpio:	GPIO number
505  *
506  * This function implements the API that's compatible with current
507  * GPIO API used in U-Boot. The request is forwarded to particular
508  * GPIO driver. Returns 0 on success, negative value on error.
509  */
gpio_direction_input(unsigned gpio)510 int gpio_direction_input(unsigned gpio)
511 {
512 	struct gpio_desc desc;
513 	int ret;
514 
515 	ret = gpio_to_device(gpio, &desc);
516 	if (ret)
517 		return ret;
518 	ret = check_reserved(&desc, "dir_input");
519 	if (ret)
520 		return ret;
521 
522 	return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
523 }
524 
525 /**
526  * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
527  * gpio:	GPIO number
528  * value:	Logical value to be set on the GPIO pin
529  *
530  * This function implements the API that's compatible with current
531  * GPIO API used in U-Boot. The request is forwarded to particular
532  * GPIO driver. Returns 0 on success, negative value on error.
533  */
gpio_direction_output(unsigned gpio,int value)534 int gpio_direction_output(unsigned gpio, int value)
535 {
536 	struct gpio_desc desc;
537 	int ret;
538 
539 	ret = gpio_to_device(gpio, &desc);
540 	if (ret)
541 		return ret;
542 	ret = check_reserved(&desc, "dir_output");
543 	if (ret)
544 		return ret;
545 
546 	return gpio_get_ops(desc.dev)->direction_output(desc.dev,
547 							desc.offset, value);
548 }
549 
_gpio_get_value(const struct gpio_desc * desc)550 static int _gpio_get_value(const struct gpio_desc *desc)
551 {
552 	int value;
553 
554 	value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
555 
556 	return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
557 }
558 
dm_gpio_get_value(const struct gpio_desc * desc)559 int dm_gpio_get_value(const struct gpio_desc *desc)
560 {
561 	int ret;
562 
563 	ret = check_reserved(desc, "get_value");
564 	if (ret)
565 		return ret;
566 
567 	return _gpio_get_value(desc);
568 }
569 
dm_gpio_set_value(const struct gpio_desc * desc,int value)570 int dm_gpio_set_value(const struct gpio_desc *desc, int value)
571 {
572 	int ret;
573 
574 	ret = check_reserved(desc, "set_value");
575 	if (ret)
576 		return ret;
577 
578 	if (desc->flags & GPIOD_ACTIVE_LOW)
579 		value = !value;
580 
581 	/*
582 	 * Emulate open drain by not actively driving the line high or
583 	 * Emulate open source by not actively driving the line low
584 	 */
585 	if ((desc->flags & GPIOD_OPEN_DRAIN && value) ||
586 	    (desc->flags & GPIOD_OPEN_SOURCE && !value))
587 		return gpio_get_ops(desc->dev)->direction_input(desc->dev,
588 								desc->offset);
589 	else if (desc->flags & GPIOD_OPEN_DRAIN ||
590 		 desc->flags & GPIOD_OPEN_SOURCE)
591 		return gpio_get_ops(desc->dev)->direction_output(desc->dev,
592 								desc->offset,
593 								value);
594 
595 	gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
596 	return 0;
597 }
598 
599 /* check dir flags invalid configuration */
check_dir_flags(ulong flags)600 static int check_dir_flags(ulong flags)
601 {
602 	if ((flags & GPIOD_IS_OUT) && (flags & GPIOD_IS_IN)) {
603 		log_debug("%s: flags 0x%lx has GPIOD_IS_OUT and GPIOD_IS_IN\n",
604 			  __func__, flags);
605 		return -EINVAL;
606 	}
607 
608 	if ((flags & GPIOD_PULL_UP) && (flags & GPIOD_PULL_DOWN)) {
609 		log_debug("%s: flags 0x%lx has GPIOD_PULL_UP and GPIOD_PULL_DOWN\n",
610 			  __func__, flags);
611 		return -EINVAL;
612 	}
613 
614 	if ((flags & GPIOD_OPEN_DRAIN) && (flags & GPIOD_OPEN_SOURCE)) {
615 		log_debug("%s: flags 0x%lx has GPIOD_OPEN_DRAIN and GPIOD_OPEN_SOURCE\n",
616 			  __func__, flags);
617 		return -EINVAL;
618 	}
619 
620 	return 0;
621 }
622 
_dm_gpio_set_dir_flags(struct gpio_desc * desc,ulong flags)623 static int _dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
624 {
625 	struct udevice *dev = desc->dev;
626 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
627 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
628 	int ret = 0;
629 
630 	ret = check_dir_flags(flags);
631 	if (ret) {
632 		dev_dbg(dev,
633 			"%s error: set_dir_flags for gpio %s%d has invalid dir flags 0x%lx\n",
634 			desc->dev->name,
635 			uc_priv->bank_name ? uc_priv->bank_name : "",
636 			desc->offset, flags);
637 
638 		return ret;
639 	}
640 
641 	/* GPIOD_ are directly managed by driver in set_dir_flags*/
642 	if (ops->set_dir_flags) {
643 		ret = ops->set_dir_flags(dev, desc->offset, flags);
644 	} else {
645 		if (flags & GPIOD_IS_OUT) {
646 			ret = ops->direction_output(dev, desc->offset,
647 						    GPIOD_FLAGS_OUTPUT(flags));
648 		} else if (flags & GPIOD_IS_IN) {
649 			ret = ops->direction_input(dev, desc->offset);
650 		}
651 	}
652 
653 	/* save the flags also in descriptor */
654 	if (!ret)
655 		desc->flags = flags;
656 
657 	return ret;
658 }
659 
dm_gpio_set_dir_flags(struct gpio_desc * desc,ulong flags)660 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
661 {
662 	int ret;
663 
664 	ret = check_reserved(desc, "set_dir_flags");
665 	if (ret)
666 		return ret;
667 
668 	/* combine the requested flags (for IN/OUT) and the descriptor flags */
669 	flags |= desc->flags;
670 	ret = _dm_gpio_set_dir_flags(desc, flags);
671 
672 	return ret;
673 }
674 
dm_gpio_set_dir(struct gpio_desc * desc)675 int dm_gpio_set_dir(struct gpio_desc *desc)
676 {
677 	int ret;
678 
679 	ret = check_reserved(desc, "set_dir");
680 	if (ret)
681 		return ret;
682 
683 	return _dm_gpio_set_dir_flags(desc, desc->flags);
684 }
685 
dm_gpio_get_dir_flags(struct gpio_desc * desc,ulong * flags)686 int dm_gpio_get_dir_flags(struct gpio_desc *desc, ulong *flags)
687 {
688 	struct udevice *dev = desc->dev;
689 	int ret, value;
690 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
691 	ulong dir_flags;
692 
693 	ret = check_reserved(desc, "get_dir_flags");
694 	if (ret)
695 		return ret;
696 
697 	/* GPIOD_ are directly provided by driver except GPIOD_ACTIVE_LOW */
698 	if (ops->get_dir_flags) {
699 		ret = ops->get_dir_flags(dev, desc->offset, &dir_flags);
700 		if (ret)
701 			return ret;
702 
703 		/* GPIOD_ACTIVE_LOW is saved in desc->flags */
704 		value = dir_flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
705 		if (desc->flags & GPIOD_ACTIVE_LOW)
706 			value = !value;
707 		dir_flags &= ~(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT_ACTIVE);
708 		dir_flags |= (desc->flags & GPIOD_ACTIVE_LOW);
709 		if (value)
710 			dir_flags |= GPIOD_IS_OUT_ACTIVE;
711 	} else {
712 		dir_flags = desc->flags;
713 		/* only GPIOD_IS_OUT_ACTIVE is provided by uclass */
714 		dir_flags &= ~GPIOD_IS_OUT_ACTIVE;
715 		if ((desc->flags & GPIOD_IS_OUT) && _gpio_get_value(desc))
716 			dir_flags |= GPIOD_IS_OUT_ACTIVE;
717 	}
718 	*flags = dir_flags;
719 
720 	return 0;
721 }
722 
723 /**
724  * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
725  * gpio:	GPIO number
726  *
727  * This function implements the API that's compatible with current
728  * GPIO API used in U-Boot. The request is forwarded to particular
729  * GPIO driver. Returns the value of the GPIO pin, or negative value
730  * on error.
731  */
gpio_get_value(unsigned gpio)732 int gpio_get_value(unsigned gpio)
733 {
734 	int ret;
735 
736 	struct gpio_desc desc;
737 
738 	ret = gpio_to_device(gpio, &desc);
739 	if (ret)
740 		return ret;
741 	return dm_gpio_get_value(&desc);
742 }
743 
744 /**
745  * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
746  * gpio:	GPIO number
747  * value:	Logical value to be set on the GPIO pin.
748  *
749  * This function implements the API that's compatible with current
750  * GPIO API used in U-Boot. The request is forwarded to particular
751  * GPIO driver. Returns 0 on success, negative value on error.
752  */
gpio_set_value(unsigned gpio,int value)753 int gpio_set_value(unsigned gpio, int value)
754 {
755 	struct gpio_desc desc;
756 	int ret;
757 
758 	ret = gpio_to_device(gpio, &desc);
759 	if (ret)
760 		return ret;
761 	return dm_gpio_set_value(&desc, value);
762 }
763 
gpio_get_bank_info(struct udevice * dev,int * bit_count)764 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
765 {
766 	struct gpio_dev_priv *priv;
767 
768 	/* Must be called on an active device */
769 	priv = dev_get_uclass_priv(dev);
770 	assert(priv);
771 
772 	*bit_count = priv->gpio_count;
773 	return priv->bank_name;
774 }
775 
776 static const char * const gpio_function[GPIOF_COUNT] = {
777 	"input",
778 	"output",
779 	"unused",
780 	"unknown",
781 	"func",
782 };
783 
get_function(struct udevice * dev,int offset,bool skip_unused,const char ** namep)784 static int get_function(struct udevice *dev, int offset, bool skip_unused,
785 			const char **namep)
786 {
787 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
788 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
789 
790 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
791 	if (!device_active(dev))
792 		return -ENODEV;
793 	if (offset < 0 || offset >= uc_priv->gpio_count)
794 		return -EINVAL;
795 	if (namep)
796 		*namep = uc_priv->name[offset];
797 	if (skip_unused && !uc_priv->name[offset])
798 		return GPIOF_UNUSED;
799 	if (ops->get_function) {
800 		int ret;
801 
802 		ret = ops->get_function(dev, offset);
803 		if (ret < 0)
804 			return ret;
805 		if (ret >= ARRAY_SIZE(gpio_function))
806 			return -ENODATA;
807 		return ret;
808 	}
809 
810 	return GPIOF_UNKNOWN;
811 }
812 
gpio_get_function(struct udevice * dev,int offset,const char ** namep)813 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
814 {
815 	return get_function(dev, offset, true, namep);
816 }
817 
gpio_get_raw_function(struct udevice * dev,int offset,const char ** namep)818 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
819 {
820 	return get_function(dev, offset, false, namep);
821 }
822 
gpio_get_status(struct udevice * dev,int offset,char * buf,int buffsize)823 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
824 {
825 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
826 	struct gpio_dev_priv *priv;
827 	char *str = buf;
828 	int func;
829 	int ret;
830 	int len;
831 
832 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
833 
834 	*buf = 0;
835 	priv = dev_get_uclass_priv(dev);
836 	ret = gpio_get_raw_function(dev, offset, NULL);
837 	if (ret < 0)
838 		return ret;
839 	func = ret;
840 	len = snprintf(str, buffsize, "%s%d: %s",
841 		       priv->bank_name ? priv->bank_name : "",
842 		       offset, gpio_function[func]);
843 	if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
844 	    func == GPIOF_UNUSED) {
845 		const char *label;
846 		bool used;
847 
848 		ret = ops->get_value(dev, offset);
849 		if (ret < 0)
850 			return ret;
851 		used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
852 		snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
853 			 ret,
854 			 used ? 'x' : ' ',
855 			 used ? " " : "",
856 			 label ? label : "");
857 	}
858 
859 	return 0;
860 }
861 
862 #if CONFIG_IS_ENABLED(ACPIGEN)
gpio_get_acpi(const struct gpio_desc * desc,struct acpi_gpio * gpio)863 int gpio_get_acpi(const struct gpio_desc *desc, struct acpi_gpio *gpio)
864 {
865 	struct dm_gpio_ops *ops;
866 
867 	memset(gpio, '\0', sizeof(*gpio));
868 	if (!dm_gpio_is_valid(desc)) {
869 		/* Indicate that the GPIO is not valid */
870 		gpio->pin_count = 0;
871 		gpio->pins[0] = 0;
872 		return -EINVAL;
873 	}
874 
875 	ops = gpio_get_ops(desc->dev);
876 	if (!ops->get_acpi)
877 		return -ENOSYS;
878 
879 	return ops->get_acpi(desc, gpio);
880 }
881 #endif
882 
gpio_claim_vector(const int * gpio_num_array,const char * fmt)883 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
884 {
885 	int i, ret;
886 	int gpio;
887 
888 	for (i = 0; i < 32; i++) {
889 		gpio = gpio_num_array[i];
890 		if (gpio == -1)
891 			break;
892 		ret = gpio_requestf(gpio, fmt, i);
893 		if (ret)
894 			goto err;
895 		ret = gpio_direction_input(gpio);
896 		if (ret) {
897 			gpio_free(gpio);
898 			goto err;
899 		}
900 	}
901 
902 	return 0;
903 err:
904 	for (i--; i >= 0; i--)
905 		gpio_free(gpio_num_array[i]);
906 
907 	return ret;
908 }
909 
910 /*
911  * get a number comprised of multiple GPIO values. gpio_num_array points to
912  * the array of gpio pin numbers to scan, terminated by -1.
913  */
gpio_get_values_as_int(const int * gpio_list)914 int gpio_get_values_as_int(const int *gpio_list)
915 {
916 	int gpio;
917 	unsigned bitmask = 1;
918 	unsigned vector = 0;
919 	int ret;
920 
921 	while (bitmask &&
922 	       ((gpio = *gpio_list++) != -1)) {
923 		ret = gpio_get_value(gpio);
924 		if (ret < 0)
925 			return ret;
926 		else if (ret)
927 			vector |= bitmask;
928 		bitmask <<= 1;
929 	}
930 
931 	return vector;
932 }
933 
dm_gpio_get_values_as_int(const struct gpio_desc * desc_list,int count)934 int dm_gpio_get_values_as_int(const struct gpio_desc *desc_list, int count)
935 {
936 	unsigned bitmask = 1;
937 	unsigned vector = 0;
938 	int ret, i;
939 
940 	for (i = 0; i < count; i++) {
941 		ret = dm_gpio_get_value(&desc_list[i]);
942 		if (ret < 0)
943 			return ret;
944 		else if (ret)
945 			vector |= bitmask;
946 		bitmask <<= 1;
947 	}
948 
949 	return vector;
950 }
951 
952 /**
953  * gpio_request_tail: common work for requesting a gpio.
954  *
955  * ret:		return value from previous work in function which calls
956  *		this function.
957  *		This seems bogus (why calling this function instead not
958  *		calling it and end caller function instead?).
959  *		Because on error in caller function we want to set some
960  *		default values in gpio desc and have a common error
961  *		debug message, which provides this function.
962  * nodename:	Name of node for which gpio gets requested
963  *		used for gpio label name.
964  * args:	pointer to output arguments structure
965  * list_name:	Name of GPIO list
966  *		used for gpio label name.
967  * index:	gpio index in gpio list
968  *		used for gpio label name.
969  * desc:	pointer to gpio descriptor, filled from this
970  *		function.
971  * flags:	gpio flags to use.
972  * add_index:	should index added to gpio label name
973  * gpio_dev:	pointer to gpio device from which the gpio
974  *		will be requested. If NULL try to get the
975  *		gpio device with uclass_get_device_by_ofnode()
976  *
977  * return:	In error case this function sets default values in
978  *		gpio descriptor, also emmits a debug message.
979  *		On success it returns 0 else the error code from
980  *		function calls, or the error code passed through
981  *		ret to this function.
982  *
983  */
gpio_request_tail(int ret,const char * nodename,struct ofnode_phandle_args * args,const char * list_name,int index,struct gpio_desc * desc,int flags,bool add_index,struct udevice * gpio_dev)984 static int gpio_request_tail(int ret, const char *nodename,
985 			     struct ofnode_phandle_args *args,
986 			     const char *list_name, int index,
987 			     struct gpio_desc *desc, int flags,
988 			     bool add_index, struct udevice *gpio_dev)
989 {
990 	gpio_desc_init(desc, gpio_dev, 0);
991 	if (ret)
992 		goto err;
993 
994 	if (!desc->dev) {
995 		ret = uclass_get_device_by_ofnode(UCLASS_GPIO, args->node,
996 						  &desc->dev);
997 		if (ret) {
998 			debug("%s: uclass_get_device_by_ofnode failed\n",
999 			      __func__);
1000 			goto err;
1001 		}
1002 	}
1003 	ret = gpio_find_and_xlate(desc, args);
1004 	if (ret) {
1005 		debug("%s: gpio_find_and_xlate failed\n", __func__);
1006 		goto err;
1007 	}
1008 	ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
1009 			       nodename, list_name, index);
1010 	if (ret) {
1011 		debug("%s: dm_gpio_requestf failed\n", __func__);
1012 		goto err;
1013 	}
1014 	ret = dm_gpio_set_dir_flags(desc, flags);
1015 	if (ret) {
1016 		debug("%s: dm_gpio_set_dir failed\n", __func__);
1017 		goto err;
1018 	}
1019 
1020 	return 0;
1021 err:
1022 	debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
1023 	      __func__, nodename, list_name, index, ret);
1024 	return ret;
1025 }
1026 
_gpio_request_by_name_nodev(ofnode node,const char * list_name,int index,struct gpio_desc * desc,int flags,bool add_index)1027 static int _gpio_request_by_name_nodev(ofnode node, const char *list_name,
1028 				       int index, struct gpio_desc *desc,
1029 				       int flags, bool add_index)
1030 {
1031 	struct ofnode_phandle_args args;
1032 	int ret;
1033 
1034 	ret = ofnode_parse_phandle_with_args(node, list_name, "#gpio-cells", 0,
1035 					     index, &args);
1036 
1037 	return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1038 				 index, desc, flags, add_index, NULL);
1039 }
1040 
gpio_request_by_name_nodev(ofnode node,const char * list_name,int index,struct gpio_desc * desc,int flags)1041 int gpio_request_by_name_nodev(ofnode node, const char *list_name, int index,
1042 			       struct gpio_desc *desc, int flags)
1043 {
1044 	return _gpio_request_by_name_nodev(node, list_name, index, desc, flags,
1045 					   index > 0);
1046 }
1047 
gpio_request_by_name(struct udevice * dev,const char * list_name,int index,struct gpio_desc * desc,int flags)1048 int gpio_request_by_name(struct udevice *dev, const char *list_name, int index,
1049 			 struct gpio_desc *desc, int flags)
1050 {
1051 	struct ofnode_phandle_args args;
1052 	ofnode node;
1053 	int ret;
1054 
1055 	ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0,
1056 					 index, &args);
1057 	node = dev_ofnode(dev);
1058 	return gpio_request_tail(ret, ofnode_get_name(node), &args, list_name,
1059 				 index, desc, flags, index > 0, NULL);
1060 }
1061 
gpio_request_list_by_name_nodev(ofnode node,const char * list_name,struct gpio_desc * desc,int max_count,int flags)1062 int gpio_request_list_by_name_nodev(ofnode node, const char *list_name,
1063 				    struct gpio_desc *desc, int max_count,
1064 				    int flags)
1065 {
1066 	int count;
1067 	int ret;
1068 
1069 	for (count = 0; count < max_count; count++) {
1070 		ret = _gpio_request_by_name_nodev(node, list_name, count,
1071 						  &desc[count], flags, true);
1072 		if (ret == -ENOENT)
1073 			break;
1074 		else if (ret)
1075 			goto err;
1076 	}
1077 
1078 	/* We ran out of GPIOs in the list */
1079 	return count;
1080 
1081 err:
1082 	gpio_free_list_nodev(desc, count - 1);
1083 
1084 	return ret;
1085 }
1086 
gpio_request_list_by_name(struct udevice * dev,const char * list_name,struct gpio_desc * desc,int max_count,int flags)1087 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
1088 			      struct gpio_desc *desc, int max_count,
1089 			      int flags)
1090 {
1091 	/*
1092 	 * This isn't ideal since we don't use dev->name in the debug()
1093 	 * calls in gpio_request_by_name(), but we can do this until
1094 	 * gpio_request_list_by_name_nodev() can be dropped.
1095 	 */
1096 	return gpio_request_list_by_name_nodev(dev_ofnode(dev), list_name, desc,
1097 					       max_count, flags);
1098 }
1099 
gpio_get_list_count(struct udevice * dev,const char * list_name)1100 int gpio_get_list_count(struct udevice *dev, const char *list_name)
1101 {
1102 	int ret;
1103 
1104 	ret = dev_read_phandle_with_args(dev, list_name, "#gpio-cells", 0, -1,
1105 					 NULL);
1106 	if (ret) {
1107 		debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
1108 		      __func__, dev->name, list_name, ret);
1109 	}
1110 
1111 	return ret;
1112 }
1113 
dm_gpio_free(struct udevice * dev,struct gpio_desc * desc)1114 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
1115 {
1116 	/* For now, we don't do any checking of dev */
1117 	return _dm_gpio_free(desc->dev, desc->offset);
1118 }
1119 
gpio_free_list(struct udevice * dev,struct gpio_desc * desc,int count)1120 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
1121 {
1122 	int i;
1123 
1124 	/* For now, we don't do any checking of dev */
1125 	for (i = 0; i < count; i++)
1126 		dm_gpio_free(dev, &desc[i]);
1127 
1128 	return 0;
1129 }
1130 
gpio_free_list_nodev(struct gpio_desc * desc,int count)1131 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
1132 {
1133 	return gpio_free_list(NULL, desc, count);
1134 }
1135 
1136 /* We need to renumber the GPIOs when any driver is probed/removed */
gpio_renumber(struct udevice * removed_dev)1137 static int gpio_renumber(struct udevice *removed_dev)
1138 {
1139 	struct gpio_dev_priv *uc_priv;
1140 	struct udevice *dev;
1141 	struct uclass *uc;
1142 	unsigned base;
1143 	int ret;
1144 
1145 	ret = uclass_get(UCLASS_GPIO, &uc);
1146 	if (ret)
1147 		return ret;
1148 
1149 	/* Ensure that we have a base for each bank */
1150 	base = 0;
1151 	uclass_foreach_dev(dev, uc) {
1152 		if (device_active(dev) && dev != removed_dev) {
1153 			uc_priv = dev_get_uclass_priv(dev);
1154 			uc_priv->gpio_base = base;
1155 			base += uc_priv->gpio_count;
1156 		}
1157 	}
1158 
1159 	return 0;
1160 }
1161 
gpio_get_number(const struct gpio_desc * desc)1162 int gpio_get_number(const struct gpio_desc *desc)
1163 {
1164 	struct udevice *dev = desc->dev;
1165 	struct gpio_dev_priv *uc_priv;
1166 
1167 	if (!dev)
1168 		return -1;
1169 	uc_priv = dev_get_uclass_priv(dev);
1170 
1171 	return uc_priv->gpio_base + desc->offset;
1172 }
1173 
gpio_post_probe(struct udevice * dev)1174 static int gpio_post_probe(struct udevice *dev)
1175 {
1176 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1177 
1178 	uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
1179 	if (!uc_priv->name)
1180 		return -ENOMEM;
1181 
1182 	return gpio_renumber(NULL);
1183 }
1184 
gpio_pre_remove(struct udevice * dev)1185 static int gpio_pre_remove(struct udevice *dev)
1186 {
1187 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
1188 	int i;
1189 
1190 	for (i = 0; i < uc_priv->gpio_count; i++) {
1191 		if (uc_priv->name[i])
1192 			free(uc_priv->name[i]);
1193 	}
1194 	free(uc_priv->name);
1195 
1196 	return gpio_renumber(dev);
1197 }
1198 
gpio_dev_request_index(struct udevice * dev,const char * nodename,char * list_name,int index,int flags,int dtflags,struct gpio_desc * desc)1199 int gpio_dev_request_index(struct udevice *dev, const char *nodename,
1200 			   char *list_name, int index, int flags,
1201 			   int dtflags, struct gpio_desc *desc)
1202 {
1203 	struct ofnode_phandle_args args;
1204 
1205 	args.node =  ofnode_null();
1206 	args.args_count = 2;
1207 	args.args[0] = index;
1208 	args.args[1] = dtflags;
1209 
1210 	return gpio_request_tail(0, nodename, &args, list_name, index, desc,
1211 				 flags, 0, dev);
1212 }
1213 
devm_gpiod_release(struct udevice * dev,void * res)1214 static void devm_gpiod_release(struct udevice *dev, void *res)
1215 {
1216 	dm_gpio_free(dev, res);
1217 }
1218 
devm_gpiod_match(struct udevice * dev,void * res,void * data)1219 static int devm_gpiod_match(struct udevice *dev, void *res, void *data)
1220 {
1221 	return res == data;
1222 }
1223 
devm_gpiod_get_index(struct udevice * dev,const char * id,unsigned int index,int flags)1224 struct gpio_desc *devm_gpiod_get_index(struct udevice *dev, const char *id,
1225 				       unsigned int index, int flags)
1226 {
1227 	int rc;
1228 	struct gpio_desc *desc;
1229 	char *propname;
1230 	static const char suffix[] = "-gpios";
1231 
1232 	propname = malloc(strlen(id) + sizeof(suffix));
1233 	if (!propname) {
1234 		rc = -ENOMEM;
1235 		goto end;
1236 	}
1237 
1238 	strcpy(propname, id);
1239 	strcat(propname, suffix);
1240 
1241 	desc = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc),
1242 			    __GFP_ZERO);
1243 	if (unlikely(!desc)) {
1244 		rc = -ENOMEM;
1245 		goto end;
1246 	}
1247 
1248 	rc = gpio_request_by_name(dev, propname, index, desc, flags);
1249 
1250 end:
1251 	if (propname)
1252 		free(propname);
1253 
1254 	if (rc)
1255 		return ERR_PTR(rc);
1256 
1257 	devres_add(dev, desc);
1258 
1259 	return desc;
1260 }
1261 
devm_gpiod_get_index_optional(struct udevice * dev,const char * id,unsigned int index,int flags)1262 struct gpio_desc *devm_gpiod_get_index_optional(struct udevice *dev,
1263 						const char *id,
1264 						unsigned int index,
1265 						int flags)
1266 {
1267 	struct gpio_desc *desc = devm_gpiod_get_index(dev, id, index, flags);
1268 
1269 	if (IS_ERR(desc))
1270 		return NULL;
1271 
1272 	return desc;
1273 }
1274 
devm_gpiod_put(struct udevice * dev,struct gpio_desc * desc)1275 void devm_gpiod_put(struct udevice *dev, struct gpio_desc *desc)
1276 {
1277 	int rc;
1278 
1279 	rc = devres_release(dev, devm_gpiod_release, devm_gpiod_match, desc);
1280 	WARN_ON(rc);
1281 }
1282 
gpio_post_bind(struct udevice * dev)1283 static int gpio_post_bind(struct udevice *dev)
1284 {
1285 	struct udevice *child;
1286 	ofnode node;
1287 
1288 #if defined(CONFIG_NEEDS_MANUAL_RELOC)
1289 	struct dm_gpio_ops *ops = (struct dm_gpio_ops *)device_get_ops(dev);
1290 	static int reloc_done;
1291 
1292 	if (!reloc_done) {
1293 		if (ops->request)
1294 			ops->request += gd->reloc_off;
1295 		if (ops->rfree)
1296 			ops->rfree += gd->reloc_off;
1297 		if (ops->direction_input)
1298 			ops->direction_input += gd->reloc_off;
1299 		if (ops->direction_output)
1300 			ops->direction_output += gd->reloc_off;
1301 		if (ops->get_value)
1302 			ops->get_value += gd->reloc_off;
1303 		if (ops->set_value)
1304 			ops->set_value += gd->reloc_off;
1305 		if (ops->get_function)
1306 			ops->get_function += gd->reloc_off;
1307 		if (ops->xlate)
1308 			ops->xlate += gd->reloc_off;
1309 		if (ops->set_dir_flags)
1310 			ops->set_dir_flags += gd->reloc_off;
1311 		if (ops->get_dir_flags)
1312 			ops->get_dir_flags += gd->reloc_off;
1313 
1314 		reloc_done++;
1315 	}
1316 #endif
1317 
1318 	if (IS_ENABLED(CONFIG_GPIO_HOG)) {
1319 		dev_for_each_subnode(node, dev) {
1320 			if (ofnode_read_bool(node, "gpio-hog")) {
1321 				const char *name = ofnode_get_name(node);
1322 				int ret;
1323 
1324 				ret = device_bind_driver_to_node(dev,
1325 								 "gpio_hog",
1326 								 name, node,
1327 								 &child);
1328 				if (ret)
1329 					return ret;
1330 			}
1331 		}
1332 	}
1333 	return 0;
1334 }
1335 
1336 UCLASS_DRIVER(gpio) = {
1337 	.id		= UCLASS_GPIO,
1338 	.name		= "gpio",
1339 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
1340 	.post_probe	= gpio_post_probe,
1341 	.post_bind	= gpio_post_bind,
1342 	.pre_remove	= gpio_pre_remove,
1343 	.per_device_auto	= sizeof(struct gpio_dev_priv),
1344 };
1345