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