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