xref: /openbmc/u-boot/drivers/gpio/gpio-uclass.c (revision 5f7e3104)
1 /*
2  * Copyright (c) 2013 Google, Inc
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <fdtdec.h>
11 #include <malloc.h>
12 #include <asm/gpio.h>
13 #include <linux/ctype.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 /**
18  * gpio_to_device() - Convert global GPIO number to device, number
19  *
20  * Convert the GPIO number to an entry in the list of GPIOs
21  * or GPIO blocks registered with the GPIO controller. Returns
22  * entry on success, NULL on error.
23  *
24  * @gpio:	The numeric representation of the GPIO
25  * @desc:	Returns description (desc->flags will always be 0)
26  * @return 0 if found, -ENOENT if not found
27  */
28 static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
29 {
30 	struct gpio_dev_priv *uc_priv;
31 	struct udevice *dev;
32 	int ret;
33 
34 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
35 	     dev;
36 	     ret = uclass_next_device(&dev)) {
37 		uc_priv = dev_get_uclass_priv(dev);
38 		if (gpio >= uc_priv->gpio_base &&
39 		    gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
40 			desc->dev = dev;
41 			desc->offset = gpio - uc_priv->gpio_base;
42 			desc->flags = 0;
43 			return 0;
44 		}
45 	}
46 
47 	/* No such GPIO */
48 	return ret ? ret : -ENOENT;
49 }
50 
51 int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
52 {
53 	struct gpio_dev_priv *uc_priv = NULL;
54 	struct udevice *dev;
55 	ulong offset;
56 	int numeric;
57 	int ret;
58 
59 	numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
60 	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
61 	     dev;
62 	     ret = uclass_next_device(&dev)) {
63 		int len;
64 
65 		uc_priv = dev_get_uclass_priv(dev);
66 		if (numeric != -1) {
67 			offset = numeric - uc_priv->gpio_base;
68 			/* Allow GPIOs to be numbered from 0 */
69 			if (offset >= 0 && offset < uc_priv->gpio_count)
70 				break;
71 		}
72 
73 		len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;
74 
75 		if (!strncasecmp(name, uc_priv->bank_name, len)) {
76 			if (!strict_strtoul(name + len, 10, &offset))
77 				break;
78 		}
79 	}
80 
81 	if (!dev)
82 		return ret ? ret : -EINVAL;
83 
84 	desc->dev = dev;
85 	desc->offset = offset;
86 
87 	return 0;
88 }
89 
90 int gpio_lookup_name(const char *name, struct udevice **devp,
91 		     unsigned int *offsetp, unsigned int *gpiop)
92 {
93 	struct gpio_desc desc;
94 	int ret;
95 
96 	if (devp)
97 		*devp = NULL;
98 	ret = dm_gpio_lookup_name(name, &desc);
99 	if (ret)
100 		return ret;
101 
102 	if (devp)
103 		*devp = desc.dev;
104 	if (offsetp)
105 		*offsetp = desc.offset;
106 	if (gpiop) {
107 		struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);
108 
109 		*gpiop = uc_priv->gpio_base + desc.offset;
110 	}
111 
112 	return 0;
113 }
114 
115 static int gpio_find_and_xlate(struct gpio_desc *desc,
116 			       struct fdtdec_phandle_args *args)
117 {
118 	struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);
119 
120 	/* Use the first argument as the offset by default */
121 	if (args->args_count > 0)
122 		desc->offset = args->args[0];
123 	else
124 		desc->offset = -1;
125 	desc->flags = 0;
126 
127 	return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
128 }
129 
130 int dm_gpio_request(struct gpio_desc *desc, const char *label)
131 {
132 	struct udevice *dev = desc->dev;
133 	struct gpio_dev_priv *uc_priv;
134 	char *str;
135 	int ret;
136 
137 	uc_priv = dev_get_uclass_priv(dev);
138 	if (uc_priv->name[desc->offset])
139 		return -EBUSY;
140 	str = strdup(label);
141 	if (!str)
142 		return -ENOMEM;
143 	if (gpio_get_ops(dev)->request) {
144 		ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
145 		if (ret) {
146 			free(str);
147 			return ret;
148 		}
149 	}
150 	uc_priv->name[desc->offset] = str;
151 
152 	return 0;
153 }
154 
155 static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
156 {
157 	va_list args;
158 	char buf[40];
159 
160 	va_start(args, fmt);
161 	vscnprintf(buf, sizeof(buf), fmt, args);
162 	va_end(args);
163 	return dm_gpio_request(desc, buf);
164 }
165 
166 /**
167  * gpio_request() - [COMPAT] Request GPIO
168  * gpio:	GPIO number
169  * label:	Name for the requested GPIO
170  *
171  * The label is copied and allocated so the caller does not need to keep
172  * the pointer around.
173  *
174  * This function implements the API that's compatible with current
175  * GPIO API used in U-Boot. The request is forwarded to particular
176  * GPIO driver. Returns 0 on success, negative value on error.
177  */
178 int gpio_request(unsigned gpio, const char *label)
179 {
180 	struct gpio_desc desc;
181 	int ret;
182 
183 	ret = gpio_to_device(gpio, &desc);
184 	if (ret)
185 		return ret;
186 
187 	return dm_gpio_request(&desc, label);
188 }
189 
190 /**
191  * gpio_requestf() - [COMPAT] Request GPIO
192  * @gpio:	GPIO number
193  * @fmt:	Format string for the requested GPIO
194  * @...:	Arguments for the printf() format string
195  *
196  * This function implements the API that's compatible with current
197  * GPIO API used in U-Boot. The request is forwarded to particular
198  * GPIO driver. Returns 0 on success, negative value on error.
199  */
200 int gpio_requestf(unsigned gpio, const char *fmt, ...)
201 {
202 	va_list args;
203 	char buf[40];
204 
205 	va_start(args, fmt);
206 	vscnprintf(buf, sizeof(buf), fmt, args);
207 	va_end(args);
208 	return gpio_request(gpio, buf);
209 }
210 
211 int _dm_gpio_free(struct udevice *dev, uint offset)
212 {
213 	struct gpio_dev_priv *uc_priv;
214 	int ret;
215 
216 	uc_priv = dev_get_uclass_priv(dev);
217 	if (!uc_priv->name[offset])
218 		return -ENXIO;
219 	if (gpio_get_ops(dev)->free) {
220 		ret = gpio_get_ops(dev)->free(dev, offset);
221 		if (ret)
222 			return ret;
223 	}
224 
225 	free(uc_priv->name[offset]);
226 	uc_priv->name[offset] = NULL;
227 
228 	return 0;
229 }
230 
231 /**
232  * gpio_free() - [COMPAT] Relinquish GPIO
233  * gpio:	GPIO number
234  *
235  * This function implements the API that's compatible with current
236  * GPIO API used in U-Boot. The request is forwarded to particular
237  * GPIO driver. Returns 0 on success, negative value on error.
238  */
239 int gpio_free(unsigned gpio)
240 {
241 	struct gpio_desc desc;
242 	int ret;
243 
244 	ret = gpio_to_device(gpio, &desc);
245 	if (ret)
246 		return ret;
247 
248 	return _dm_gpio_free(desc.dev, desc.offset);
249 }
250 
251 static int check_reserved(struct gpio_desc *desc, const char *func)
252 {
253 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc->dev);
254 
255 	if (!uc_priv->name[desc->offset]) {
256 		printf("%s: %s: error: gpio %s%d not reserved\n",
257 		       desc->dev->name, func,
258 		       uc_priv->bank_name ? uc_priv->bank_name : "",
259 		       desc->offset);
260 		return -EBUSY;
261 	}
262 
263 	return 0;
264 }
265 
266 /**
267  * gpio_direction_input() - [COMPAT] Set GPIO direction to input
268  * gpio:	GPIO number
269  *
270  * This function implements the API that's compatible with current
271  * GPIO API used in U-Boot. The request is forwarded to particular
272  * GPIO driver. Returns 0 on success, negative value on error.
273  */
274 int gpio_direction_input(unsigned gpio)
275 {
276 	struct gpio_desc desc;
277 	int ret;
278 
279 	ret = gpio_to_device(gpio, &desc);
280 	if (ret)
281 		return ret;
282 	ret = check_reserved(&desc, "dir_input");
283 	if (ret)
284 		return ret;
285 
286 	return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
287 }
288 
289 /**
290  * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
291  * gpio:	GPIO number
292  * value:	Logical value to be set on the GPIO pin
293  *
294  * This function implements the API that's compatible with current
295  * GPIO API used in U-Boot. The request is forwarded to particular
296  * GPIO driver. Returns 0 on success, negative value on error.
297  */
298 int gpio_direction_output(unsigned gpio, int value)
299 {
300 	struct gpio_desc desc;
301 	int ret;
302 
303 	ret = gpio_to_device(gpio, &desc);
304 	if (ret)
305 		return ret;
306 	ret = check_reserved(&desc, "dir_output");
307 	if (ret)
308 		return ret;
309 
310 	return gpio_get_ops(desc.dev)->direction_output(desc.dev,
311 							desc.offset, value);
312 }
313 
314 int dm_gpio_get_value(struct gpio_desc *desc)
315 {
316 	int value;
317 	int ret;
318 
319 	ret = check_reserved(desc, "get_value");
320 	if (ret)
321 		return ret;
322 
323 	value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);
324 
325 	return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
326 }
327 
328 int dm_gpio_set_value(struct gpio_desc *desc, int value)
329 {
330 	int ret;
331 
332 	ret = check_reserved(desc, "set_value");
333 	if (ret)
334 		return ret;
335 
336 	if (desc->flags & GPIOD_ACTIVE_LOW)
337 		value = !value;
338 	gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
339 	return 0;
340 }
341 
342 int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
343 {
344 	struct udevice *dev = desc->dev;
345 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
346 	int ret;
347 
348 	ret = check_reserved(desc, "set_dir");
349 	if (ret)
350 		return ret;
351 
352 	if (flags & GPIOD_IS_OUT) {
353 		int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;
354 
355 		if (flags & GPIOD_ACTIVE_LOW)
356 			value = !value;
357 		ret = ops->direction_output(dev, desc->offset, value);
358 	} else  if (flags & GPIOD_IS_IN) {
359 		ret = ops->direction_input(dev, desc->offset);
360 	}
361 	if (ret)
362 		return ret;
363 	/*
364 	 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
365 	 * futures
366 	 */
367 	desc->flags = flags;
368 
369 	return 0;
370 }
371 
372 int dm_gpio_set_dir(struct gpio_desc *desc)
373 {
374 	return dm_gpio_set_dir_flags(desc, desc->flags);
375 }
376 
377 /**
378  * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
379  * gpio:	GPIO number
380  *
381  * This function implements the API that's compatible with current
382  * GPIO API used in U-Boot. The request is forwarded to particular
383  * GPIO driver. Returns the value of the GPIO pin, or negative value
384  * on error.
385  */
386 int gpio_get_value(unsigned gpio)
387 {
388 	int ret;
389 
390 	struct gpio_desc desc;
391 
392 	ret = gpio_to_device(gpio, &desc);
393 	if (ret)
394 		return ret;
395 	return dm_gpio_get_value(&desc);
396 }
397 
398 /**
399  * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
400  * gpio:	GPIO number
401  * value:	Logical value to be set on the GPIO pin.
402  *
403  * This function implements the API that's compatible with current
404  * GPIO API used in U-Boot. The request is forwarded to particular
405  * GPIO driver. Returns 0 on success, negative value on error.
406  */
407 int gpio_set_value(unsigned gpio, int value)
408 {
409 	struct gpio_desc desc;
410 	int ret;
411 
412 	ret = gpio_to_device(gpio, &desc);
413 	if (ret)
414 		return ret;
415 	return dm_gpio_set_value(&desc, value);
416 }
417 
418 const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
419 {
420 	struct gpio_dev_priv *priv;
421 
422 	/* Must be called on an active device */
423 	priv = dev_get_uclass_priv(dev);
424 	assert(priv);
425 
426 	*bit_count = priv->gpio_count;
427 	return priv->bank_name;
428 }
429 
430 static const char * const gpio_function[GPIOF_COUNT] = {
431 	"input",
432 	"output",
433 	"unused",
434 	"unknown",
435 	"func",
436 };
437 
438 int get_function(struct udevice *dev, int offset, bool skip_unused,
439 		 const char **namep)
440 {
441 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
442 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
443 
444 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
445 	if (!device_active(dev))
446 		return -ENODEV;
447 	if (offset < 0 || offset >= uc_priv->gpio_count)
448 		return -EINVAL;
449 	if (namep)
450 		*namep = uc_priv->name[offset];
451 	if (skip_unused && !uc_priv->name[offset])
452 		return GPIOF_UNUSED;
453 	if (ops->get_function) {
454 		int ret;
455 
456 		ret = ops->get_function(dev, offset);
457 		if (ret < 0)
458 			return ret;
459 		if (ret >= ARRAY_SIZE(gpio_function))
460 			return -ENODATA;
461 		return ret;
462 	}
463 
464 	return GPIOF_UNKNOWN;
465 }
466 
467 int gpio_get_function(struct udevice *dev, int offset, const char **namep)
468 {
469 	return get_function(dev, offset, true, namep);
470 }
471 
472 int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
473 {
474 	return get_function(dev, offset, false, namep);
475 }
476 
477 int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
478 {
479 	struct dm_gpio_ops *ops = gpio_get_ops(dev);
480 	struct gpio_dev_priv *priv;
481 	char *str = buf;
482 	int func;
483 	int ret;
484 	int len;
485 
486 	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
487 
488 	*buf = 0;
489 	priv = dev_get_uclass_priv(dev);
490 	ret = gpio_get_raw_function(dev, offset, NULL);
491 	if (ret < 0)
492 		return ret;
493 	func = ret;
494 	len = snprintf(str, buffsize, "%s%d: %s",
495 		       priv->bank_name ? priv->bank_name : "",
496 		       offset, gpio_function[func]);
497 	if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
498 	    func == GPIOF_UNUSED) {
499 		const char *label;
500 		bool used;
501 
502 		ret = ops->get_value(dev, offset);
503 		if (ret < 0)
504 			return ret;
505 		used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
506 		snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
507 			 ret,
508 			 used ? 'x' : ' ',
509 			 used ? " " : "",
510 			 label ? label : "");
511 	}
512 
513 	return 0;
514 }
515 
516 int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
517 {
518 	int i, ret;
519 	int gpio;
520 
521 	for (i = 0; i < 32; i++) {
522 		gpio = gpio_num_array[i];
523 		if (gpio == -1)
524 			break;
525 		ret = gpio_requestf(gpio, fmt, i);
526 		if (ret)
527 			goto err;
528 		ret = gpio_direction_input(gpio);
529 		if (ret) {
530 			gpio_free(gpio);
531 			goto err;
532 		}
533 	}
534 
535 	return 0;
536 err:
537 	for (i--; i >= 0; i--)
538 		gpio_free(gpio_num_array[i]);
539 
540 	return ret;
541 }
542 
543 /*
544  * get a number comprised of multiple GPIO values. gpio_num_array points to
545  * the array of gpio pin numbers to scan, terminated by -1.
546  */
547 int gpio_get_values_as_int(const int *gpio_list)
548 {
549 	int gpio;
550 	unsigned bitmask = 1;
551 	unsigned vector = 0;
552 	int ret;
553 
554 	while (bitmask &&
555 	       ((gpio = *gpio_list++) != -1)) {
556 		ret = gpio_get_value(gpio);
557 		if (ret < 0)
558 			return ret;
559 		else if (ret)
560 			vector |= bitmask;
561 		bitmask <<= 1;
562 	}
563 
564 	return vector;
565 }
566 
567 static int _gpio_request_by_name_nodev(const void *blob, int node,
568 				       const char *list_name, int index,
569 				       struct gpio_desc *desc, int flags,
570 				       bool add_index)
571 {
572 	struct fdtdec_phandle_args args;
573 	int ret;
574 
575 	desc->dev = NULL;
576 	desc->offset = 0;
577 	ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
578 					     "#gpio-cells", 0, index, &args);
579 	if (ret) {
580 		debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
581 		goto err;
582 	}
583 
584 	ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
585 					     &desc->dev);
586 	if (ret) {
587 		debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
588 		goto err;
589 	}
590 	ret = gpio_find_and_xlate(desc, &args);
591 	if (ret) {
592 		debug("%s: gpio_find_and_xlate failed\n", __func__);
593 		goto err;
594 	}
595 	ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
596 			       fdt_get_name(blob, node, NULL),
597 			       list_name, index);
598 	if (ret) {
599 		debug("%s: dm_gpio_requestf failed\n", __func__);
600 		goto err;
601 	}
602 	ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
603 	if (ret) {
604 		debug("%s: dm_gpio_set_dir failed\n", __func__);
605 		goto err;
606 	}
607 
608 	return 0;
609 err:
610 	debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
611 	      __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
612 	return ret;
613 }
614 
615 int gpio_request_by_name_nodev(const void *blob, int node,
616 			       const char *list_name, int index,
617 			       struct gpio_desc *desc, int flags)
618 {
619 	return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
620 					   flags, index > 0);
621 }
622 
623 int gpio_request_by_name(struct udevice *dev,  const char *list_name, int index,
624 			 struct gpio_desc *desc, int flags)
625 {
626 	/*
627 	 * This isn't ideal since we don't use dev->name in the debug()
628 	 * calls in gpio_request_by_name(), but we can do this until
629 	 * gpio_request_by_name_nodev() can be dropped.
630 	 */
631 	return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
632 					  list_name, index, desc, flags);
633 }
634 
635 int gpio_request_list_by_name_nodev(const void *blob, int node,
636 				    const char *list_name,
637 				    struct gpio_desc *desc, int max_count,
638 				    int flags)
639 {
640 	int count;
641 	int ret;
642 
643 	for (count = 0; count < max_count; count++) {
644 		ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
645 						  &desc[count], flags, true);
646 		if (ret == -ENOENT)
647 			break;
648 		else if (ret)
649 			goto err;
650 	}
651 
652 	/* We ran out of GPIOs in the list */
653 	return count;
654 
655 err:
656 	gpio_free_list_nodev(desc, count - 1);
657 
658 	return ret;
659 }
660 
661 int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
662 			      struct gpio_desc *desc, int max_count,
663 			      int flags)
664 {
665 	/*
666 	 * This isn't ideal since we don't use dev->name in the debug()
667 	 * calls in gpio_request_by_name(), but we can do this until
668 	 * gpio_request_list_by_name_nodev() can be dropped.
669 	 */
670 	return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
671 					       list_name, desc, max_count,
672 					       flags);
673 }
674 
675 int gpio_get_list_count(struct udevice *dev, const char *list_name)
676 {
677 	int ret;
678 
679 	ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
680 					     list_name, "#gpio-cells", 0, -1,
681 					     NULL);
682 	if (ret) {
683 		debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
684 		      __func__, dev->name, list_name, ret);
685 	}
686 
687 	return ret;
688 }
689 
690 int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
691 {
692 	/* For now, we don't do any checking of dev */
693 	return _dm_gpio_free(desc->dev, desc->offset);
694 }
695 
696 int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
697 {
698 	int i;
699 
700 	/* For now, we don't do any checking of dev */
701 	for (i = 0; i < count; i++)
702 		dm_gpio_free(dev, &desc[i]);
703 
704 	return 0;
705 }
706 
707 int gpio_free_list_nodev(struct gpio_desc *desc, int count)
708 {
709 	return gpio_free_list(NULL, desc, count);
710 }
711 
712 /* We need to renumber the GPIOs when any driver is probed/removed */
713 static int gpio_renumber(struct udevice *removed_dev)
714 {
715 	struct gpio_dev_priv *uc_priv;
716 	struct udevice *dev;
717 	struct uclass *uc;
718 	unsigned base;
719 	int ret;
720 
721 	ret = uclass_get(UCLASS_GPIO, &uc);
722 	if (ret)
723 		return ret;
724 
725 	/* Ensure that we have a base for each bank */
726 	base = 0;
727 	uclass_foreach_dev(dev, uc) {
728 		if (device_active(dev) && dev != removed_dev) {
729 			uc_priv = dev_get_uclass_priv(dev);
730 			uc_priv->gpio_base = base;
731 			base += uc_priv->gpio_count;
732 		}
733 	}
734 
735 	return 0;
736 }
737 
738 int gpio_get_number(struct gpio_desc *desc)
739 {
740 	struct udevice *dev = desc->dev;
741 	struct gpio_dev_priv *uc_priv;
742 
743 	if (!dev)
744 		return -1;
745 	uc_priv = dev->uclass_priv;
746 
747 	return uc_priv->gpio_base + desc->offset;
748 }
749 
750 static int gpio_post_probe(struct udevice *dev)
751 {
752 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
753 
754 	uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
755 	if (!uc_priv->name)
756 		return -ENOMEM;
757 
758 	return gpio_renumber(NULL);
759 }
760 
761 static int gpio_pre_remove(struct udevice *dev)
762 {
763 	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
764 	int i;
765 
766 	for (i = 0; i < uc_priv->gpio_count; i++) {
767 		if (uc_priv->name[i])
768 			free(uc_priv->name[i]);
769 	}
770 	free(uc_priv->name);
771 
772 	return gpio_renumber(dev);
773 }
774 
775 UCLASS_DRIVER(gpio) = {
776 	.id		= UCLASS_GPIO,
777 	.name		= "gpio",
778 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
779 	.post_probe	= gpio_post_probe,
780 	.pre_remove	= gpio_pre_remove,
781 	.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
782 };
783