gpio-uclass.c 18.1 KB
Newer Older
S
Simon Glass 已提交
1 2 3 4 5 6 7 8 9
/*
 * Copyright (c) 2013 Google, Inc
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

#include <common.h>
#include <dm.h>
#include <errno.h>
10
#include <fdtdec.h>
11
#include <malloc.h>
S
Simon Glass 已提交
12
#include <asm/gpio.h>
13
#include <linux/bug.h>
S
Simon Glass 已提交
14
#include <linux/ctype.h>
S
Simon Glass 已提交
15

16 17
DECLARE_GLOBAL_DATA_PTR;

S
Simon Glass 已提交
18 19 20 21 22 23
/**
 * gpio_to_device() - Convert global GPIO number to device, number
 *
 * Convert the GPIO number to an entry in the list of GPIOs
 * or GPIO blocks registered with the GPIO controller. Returns
 * entry on success, NULL on error.
24 25 26 27
 *
 * @gpio:	The numeric representation of the GPIO
 * @desc:	Returns description (desc->flags will always be 0)
 * @return 0 if found, -ENOENT if not found
S
Simon Glass 已提交
28
 */
29
static int gpio_to_device(unsigned int gpio, struct gpio_desc *desc)
S
Simon Glass 已提交
30 31
{
	struct gpio_dev_priv *uc_priv;
32
	struct udevice *dev;
S
Simon Glass 已提交
33 34 35 36 37
	int ret;

	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
	     dev;
	     ret = uclass_next_device(&dev)) {
38
		uc_priv = dev_get_uclass_priv(dev);
S
Simon Glass 已提交
39 40
		if (gpio >= uc_priv->gpio_base &&
		    gpio < uc_priv->gpio_base + uc_priv->gpio_count) {
41 42 43
			desc->dev = dev;
			desc->offset = gpio - uc_priv->gpio_base;
			desc->flags = 0;
S
Simon Glass 已提交
44 45 46 47 48
			return 0;
		}
	}

	/* No such GPIO */
49
	return ret ? ret : -ENOENT;
S
Simon Glass 已提交
50 51
}

52
int dm_gpio_lookup_name(const char *name, struct gpio_desc *desc)
S
Simon Glass 已提交
53
{
S
Simon Glass 已提交
54
	struct gpio_dev_priv *uc_priv = NULL;
55
	struct udevice *dev;
S
Simon Glass 已提交
56 57
	ulong offset;
	int numeric;
S
Simon Glass 已提交
58 59
	int ret;

S
Simon Glass 已提交
60
	numeric = isdigit(*name) ? simple_strtoul(name, NULL, 10) : -1;
S
Simon Glass 已提交
61 62 63 64 65
	for (ret = uclass_first_device(UCLASS_GPIO, &dev);
	     dev;
	     ret = uclass_next_device(&dev)) {
		int len;

66
		uc_priv = dev_get_uclass_priv(dev);
S
Simon Glass 已提交
67 68 69 70 71 72 73
		if (numeric != -1) {
			offset = numeric - uc_priv->gpio_base;
			/* Allow GPIOs to be numbered from 0 */
			if (offset >= 0 && offset < uc_priv->gpio_count)
				break;
		}

S
Simon Glass 已提交
74 75
		len = uc_priv->bank_name ? strlen(uc_priv->bank_name) : 0;

76
		if (!strncasecmp(name, uc_priv->bank_name, len)) {
S
Simon Glass 已提交
77 78
			if (!strict_strtoul(name + len, 10, &offset))
				break;
S
Simon Glass 已提交
79 80 81
		}
	}

S
Simon Glass 已提交
82 83 84
	if (!dev)
		return ret ? ret : -EINVAL;

85 86 87 88 89 90 91 92 93 94 95 96
	desc->dev = dev;
	desc->offset = offset;

	return 0;
}

int gpio_lookup_name(const char *name, struct udevice **devp,
		     unsigned int *offsetp, unsigned int *gpiop)
{
	struct gpio_desc desc;
	int ret;

S
Simon Glass 已提交
97
	if (devp)
98 99 100 101 102 103 104
		*devp = NULL;
	ret = dm_gpio_lookup_name(name, &desc);
	if (ret)
		return ret;

	if (devp)
		*devp = desc.dev;
S
Simon Glass 已提交
105
	if (offsetp)
106 107 108 109 110 111
		*offsetp = desc.offset;
	if (gpiop) {
		struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(desc.dev);

		*gpiop = uc_priv->gpio_base + desc.offset;
	}
S
Simon Glass 已提交
112 113

	return 0;
S
Simon Glass 已提交
114 115
}

116 117
static int gpio_find_and_xlate(struct gpio_desc *desc,
			       struct fdtdec_phandle_args *args)
118 119 120 121 122 123 124 125 126 127 128 129 130
{
	struct dm_gpio_ops *ops = gpio_get_ops(desc->dev);

	/* Use the first argument as the offset by default */
	if (args->args_count > 0)
		desc->offset = args->args[0];
	else
		desc->offset = -1;
	desc->flags = 0;

	return ops->xlate ? ops->xlate(desc->dev, desc, args) : 0;
}

131
int dm_gpio_request(struct gpio_desc *desc, const char *label)
132 133 134 135 136 137
{
	struct udevice *dev = desc->dev;
	struct gpio_dev_priv *uc_priv;
	char *str;
	int ret;

138
	uc_priv = dev_get_uclass_priv(dev);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	if (uc_priv->name[desc->offset])
		return -EBUSY;
	str = strdup(label);
	if (!str)
		return -ENOMEM;
	if (gpio_get_ops(dev)->request) {
		ret = gpio_get_ops(dev)->request(dev, desc->offset, label);
		if (ret) {
			free(str);
			return ret;
		}
	}
	uc_priv->name[desc->offset] = str;

	return 0;
}

156 157
static int dm_gpio_requestf(struct gpio_desc *desc, const char *fmt, ...)
{
158
#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
159 160 161 162 163 164 165
	va_list args;
	char buf[40];

	va_start(args, fmt);
	vscnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	return dm_gpio_request(desc, buf);
166 167 168
#else
	return dm_gpio_request(desc, fmt);
#endif
169 170
}

S
Simon Glass 已提交
171 172 173 174 175
/**
 * gpio_request() - [COMPAT] Request GPIO
 * gpio:	GPIO number
 * label:	Name for the requested GPIO
 *
176 177 178
 * The label is copied and allocated so the caller does not need to keep
 * the pointer around.
 *
S
Simon Glass 已提交
179 180 181 182 183 184
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_request(unsigned gpio, const char *label)
{
185
	struct gpio_desc desc;
S
Simon Glass 已提交
186 187
	int ret;

188
	ret = gpio_to_device(gpio, &desc);
S
Simon Glass 已提交
189 190 191
	if (ret)
		return ret;

192
	return dm_gpio_request(&desc, label);
S
Simon Glass 已提交
193 194
}

195 196 197 198 199 200 201 202 203 204 205 206
/**
 * gpio_requestf() - [COMPAT] Request GPIO
 * @gpio:	GPIO number
 * @fmt:	Format string for the requested GPIO
 * @...:	Arguments for the printf() format string
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_requestf(unsigned gpio, const char *fmt, ...)
{
207
#if !defined(CONFIG_SPL_BUILD) || !defined(CONFIG_USE_TINY_PRINTF)
208 209 210 211 212 213 214
	va_list args;
	char buf[40];

	va_start(args, fmt);
	vscnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);
	return gpio_request(gpio, buf);
215 216 217
#else
	return gpio_request(gpio, fmt);
#endif
218 219
}

220
int _dm_gpio_free(struct udevice *dev, uint offset)
S
Simon Glass 已提交
221
{
222
	struct gpio_dev_priv *uc_priv;
S
Simon Glass 已提交
223 224
	int ret;

225
	uc_priv = dev_get_uclass_priv(dev);
226 227 228 229 230 231 232 233 234 235 236 237 238 239
	if (!uc_priv->name[offset])
		return -ENXIO;
	if (gpio_get_ops(dev)->free) {
		ret = gpio_get_ops(dev)->free(dev, offset);
		if (ret)
			return ret;
	}

	free(uc_priv->name[offset]);
	uc_priv->name[offset] = NULL;

	return 0;
}

240 241 242 243 244 245 246 247 248
/**
 * gpio_free() - [COMPAT] Relinquish GPIO
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_free(unsigned gpio)
249
{
250 251 252 253 254 255 256 257 258
	struct gpio_desc desc;
	int ret;

	ret = gpio_to_device(gpio, &desc);
	if (ret)
		return ret;

	return _dm_gpio_free(desc.dev, desc.offset);
}
259

260 261
static int check_reserved(struct gpio_desc *desc, const char *func)
{
262 263 264 265
	struct gpio_dev_priv *uc_priv;

	if (!dm_gpio_is_valid(desc))
		return -ENOENT;
266

267
	uc_priv = dev_get_uclass_priv(desc->dev);
268
	if (!uc_priv->name[desc->offset]) {
269
		printf("%s: %s: error: gpio %s%d not reserved\n",
270 271 272
		       desc->dev->name, func,
		       uc_priv->bank_name ? uc_priv->bank_name : "",
		       desc->offset);
273 274 275 276
		return -EBUSY;
	}

	return 0;
S
Simon Glass 已提交
277 278 279 280 281 282 283 284 285 286 287 288
}

/**
 * gpio_direction_input() - [COMPAT] Set GPIO direction to input
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_direction_input(unsigned gpio)
{
289
	struct gpio_desc desc;
S
Simon Glass 已提交
290 291
	int ret;

292 293 294 295
	ret = gpio_to_device(gpio, &desc);
	if (ret)
		return ret;
	ret = check_reserved(&desc, "dir_input");
S
Simon Glass 已提交
296 297 298
	if (ret)
		return ret;

299
	return gpio_get_ops(desc.dev)->direction_input(desc.dev, desc.offset);
S
Simon Glass 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312
}

/**
 * gpio_direction_output() - [COMPAT] Set GPIO direction to output and set value
 * gpio:	GPIO number
 * value:	Logical value to be set on the GPIO pin
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_direction_output(unsigned gpio, int value)
{
313
	struct gpio_desc desc;
S
Simon Glass 已提交
314 315
	int ret;

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
	ret = gpio_to_device(gpio, &desc);
	if (ret)
		return ret;
	ret = check_reserved(&desc, "dir_output");
	if (ret)
		return ret;

	return gpio_get_ops(desc.dev)->direction_output(desc.dev,
							desc.offset, value);
}

int dm_gpio_get_value(struct gpio_desc *desc)
{
	int value;
	int ret;

	ret = check_reserved(desc, "get_value");
S
Simon Glass 已提交
333 334 335
	if (ret)
		return ret;

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	value = gpio_get_ops(desc->dev)->get_value(desc->dev, desc->offset);

	return desc->flags & GPIOD_ACTIVE_LOW ? !value : value;
}

int dm_gpio_set_value(struct gpio_desc *desc, int value)
{
	int ret;

	ret = check_reserved(desc, "set_value");
	if (ret)
		return ret;

	if (desc->flags & GPIOD_ACTIVE_LOW)
		value = !value;
	gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value);
	return 0;
}

int dm_gpio_set_dir_flags(struct gpio_desc *desc, ulong flags)
{
	struct udevice *dev = desc->dev;
	struct dm_gpio_ops *ops = gpio_get_ops(dev);
	int ret;

	ret = check_reserved(desc, "set_dir");
	if (ret)
		return ret;

	if (flags & GPIOD_IS_OUT) {
		int value = flags & GPIOD_IS_OUT_ACTIVE ? 1 : 0;

		if (flags & GPIOD_ACTIVE_LOW)
			value = !value;
		ret = ops->direction_output(dev, desc->offset, value);
	} else  if (flags & GPIOD_IS_IN) {
		ret = ops->direction_input(dev, desc->offset);
	}
	if (ret)
		return ret;
	/*
	 * Update desc->flags here, so that GPIO_ACTIVE_LOW is honoured in
	 * futures
	 */
	desc->flags = flags;

	return 0;
}

int dm_gpio_set_dir(struct gpio_desc *desc)
{
	return dm_gpio_set_dir_flags(desc, desc->flags);
S
Simon Glass 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
}

/**
 * gpio_get_value() - [COMPAT] Sample GPIO pin and return it's value
 * gpio:	GPIO number
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns the value of the GPIO pin, or negative value
 * on error.
 */
int gpio_get_value(unsigned gpio)
{
	int ret;

403 404 405
	struct gpio_desc desc;

	ret = gpio_to_device(gpio, &desc);
S
Simon Glass 已提交
406 407
	if (ret)
		return ret;
408
	return dm_gpio_get_value(&desc);
S
Simon Glass 已提交
409 410 411 412 413 414 415 416 417 418 419 420 421
}

/**
 * gpio_set_value() - [COMPAT] Configure logical value on GPIO pin
 * gpio:	GPIO number
 * value:	Logical value to be set on the GPIO pin.
 *
 * This function implements the API that's compatible with current
 * GPIO API used in U-Boot. The request is forwarded to particular
 * GPIO driver. Returns 0 on success, negative value on error.
 */
int gpio_set_value(unsigned gpio, int value)
{
422
	struct gpio_desc desc;
S
Simon Glass 已提交
423 424
	int ret;

425
	ret = gpio_to_device(gpio, &desc);
S
Simon Glass 已提交
426 427
	if (ret)
		return ret;
428
	return dm_gpio_set_value(&desc, value);
S
Simon Glass 已提交
429 430
}

431
const char *gpio_get_bank_info(struct udevice *dev, int *bit_count)
S
Simon Glass 已提交
432 433 434 435
{
	struct gpio_dev_priv *priv;

	/* Must be called on an active device */
436
	priv = dev_get_uclass_priv(dev);
S
Simon Glass 已提交
437 438 439 440 441 442
	assert(priv);

	*bit_count = priv->gpio_count;
	return priv->bank_name;
}

443 444 445 446 447 448 449 450 451 452 453
static const char * const gpio_function[GPIOF_COUNT] = {
	"input",
	"output",
	"unused",
	"unknown",
	"func",
};

int get_function(struct udevice *dev, int offset, bool skip_unused,
		 const char **namep)
{
454
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
	struct dm_gpio_ops *ops = gpio_get_ops(dev);

	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));
	if (!device_active(dev))
		return -ENODEV;
	if (offset < 0 || offset >= uc_priv->gpio_count)
		return -EINVAL;
	if (namep)
		*namep = uc_priv->name[offset];
	if (skip_unused && !uc_priv->name[offset])
		return GPIOF_UNUSED;
	if (ops->get_function) {
		int ret;

		ret = ops->get_function(dev, offset);
		if (ret < 0)
			return ret;
		if (ret >= ARRAY_SIZE(gpio_function))
			return -ENODATA;
		return ret;
	}

	return GPIOF_UNKNOWN;
}

int gpio_get_function(struct udevice *dev, int offset, const char **namep)
{
	return get_function(dev, offset, true, namep);
}

int gpio_get_raw_function(struct udevice *dev, int offset, const char **namep)
{
	return get_function(dev, offset, false, namep);
}

490 491 492 493 494 495 496 497 498 499 500 501
int gpio_get_status(struct udevice *dev, int offset, char *buf, int buffsize)
{
	struct dm_gpio_ops *ops = gpio_get_ops(dev);
	struct gpio_dev_priv *priv;
	char *str = buf;
	int func;
	int ret;
	int len;

	BUILD_BUG_ON(GPIOF_COUNT != ARRAY_SIZE(gpio_function));

	*buf = 0;
502
	priv = dev_get_uclass_priv(dev);
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	ret = gpio_get_raw_function(dev, offset, NULL);
	if (ret < 0)
		return ret;
	func = ret;
	len = snprintf(str, buffsize, "%s%d: %s",
		       priv->bank_name ? priv->bank_name : "",
		       offset, gpio_function[func]);
	if (func == GPIOF_INPUT || func == GPIOF_OUTPUT ||
	    func == GPIOF_UNUSED) {
		const char *label;
		bool used;

		ret = ops->get_value(dev, offset);
		if (ret < 0)
			return ret;
		used = gpio_get_function(dev, offset, &label) != GPIOF_UNUSED;
		snprintf(str + len, buffsize - len, ": %d [%c]%s%s",
			 ret,
			 used ? 'x' : ' ',
			 used ? " " : "",
			 label ? label : "");
	}

	return 0;
}

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
int gpio_claim_vector(const int *gpio_num_array, const char *fmt)
{
	int i, ret;
	int gpio;

	for (i = 0; i < 32; i++) {
		gpio = gpio_num_array[i];
		if (gpio == -1)
			break;
		ret = gpio_requestf(gpio, fmt, i);
		if (ret)
			goto err;
		ret = gpio_direction_input(gpio);
		if (ret) {
			gpio_free(gpio);
			goto err;
		}
	}

	return 0;
err:
	for (i--; i >= 0; i--)
		gpio_free(gpio_num_array[i]);

	return ret;
}

556 557 558 559
/*
 * get a number comprised of multiple GPIO values. gpio_num_array points to
 * the array of gpio pin numbers to scan, terminated by -1.
 */
560
int gpio_get_values_as_int(const int *gpio_list)
561 562 563 564
{
	int gpio;
	unsigned bitmask = 1;
	unsigned vector = 0;
565
	int ret;
566 567

	while (bitmask &&
568 569 570 571 572
	       ((gpio = *gpio_list++) != -1)) {
		ret = gpio_get_value(gpio);
		if (ret < 0)
			return ret;
		else if (ret)
573 574 575
			vector |= bitmask;
		bitmask <<= 1;
	}
576

577 578 579
	return vector;
}

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
int dm_gpio_get_values_as_int(struct gpio_desc *desc_list, int count)
{
	unsigned bitmask = 1;
	unsigned vector = 0;
	int ret, i;

	for (i = 0; i < count; i++) {
		ret = dm_gpio_get_value(&desc_list[i]);
		if (ret < 0)
			return ret;
		else if (ret)
			vector |= bitmask;
		bitmask <<= 1;
	}

	return vector;
}

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
static int _gpio_request_by_name_nodev(const void *blob, int node,
				       const char *list_name, int index,
				       struct gpio_desc *desc, int flags,
				       bool add_index)
{
	struct fdtdec_phandle_args args;
	int ret;

	desc->dev = NULL;
	desc->offset = 0;
	ret = fdtdec_parse_phandle_with_args(blob, node, list_name,
					     "#gpio-cells", 0, index, &args);
	if (ret) {
		debug("%s: fdtdec_parse_phandle_with_args failed\n", __func__);
		goto err;
	}

	ret = uclass_get_device_by_of_offset(UCLASS_GPIO, args.node,
					     &desc->dev);
	if (ret) {
		debug("%s: uclass_get_device_by_of_offset failed\n", __func__);
		goto err;
	}
	ret = gpio_find_and_xlate(desc, &args);
	if (ret) {
		debug("%s: gpio_find_and_xlate failed\n", __func__);
		goto err;
	}
	ret = dm_gpio_requestf(desc, add_index ? "%s.%s%d" : "%s.%s",
			       fdt_get_name(blob, node, NULL),
			       list_name, index);
	if (ret) {
		debug("%s: dm_gpio_requestf failed\n", __func__);
		goto err;
	}
	ret = dm_gpio_set_dir_flags(desc, flags | desc->flags);
	if (ret) {
		debug("%s: dm_gpio_set_dir failed\n", __func__);
		goto err;
	}

	return 0;
err:
	debug("%s: Node '%s', property '%s', failed to request GPIO index %d: %d\n",
	      __func__, fdt_get_name(blob, node, NULL), list_name, index, ret);
	return ret;
}

int gpio_request_by_name_nodev(const void *blob, int node,
			       const char *list_name, int index,
			       struct gpio_desc *desc, int flags)
{
	return _gpio_request_by_name_nodev(blob, node, list_name, index, desc,
					   flags, index > 0);
}

int gpio_request_by_name(struct udevice *dev,  const char *list_name, int index,
			 struct gpio_desc *desc, int flags)
{
	/*
	 * This isn't ideal since we don't use dev->name in the debug()
	 * calls in gpio_request_by_name(), but we can do this until
	 * gpio_request_by_name_nodev() can be dropped.
	 */
	return gpio_request_by_name_nodev(gd->fdt_blob, dev->of_offset,
					  list_name, index, desc, flags);
}

int gpio_request_list_by_name_nodev(const void *blob, int node,
				    const char *list_name,
				    struct gpio_desc *desc, int max_count,
				    int flags)
{
	int count;
	int ret;

674
	for (count = 0; count < max_count; count++) {
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
		ret = _gpio_request_by_name_nodev(blob, node, list_name, count,
						  &desc[count], flags, true);
		if (ret == -ENOENT)
			break;
		else if (ret)
			goto err;
	}

	/* We ran out of GPIOs in the list */
	return count;

err:
	gpio_free_list_nodev(desc, count - 1);

	return ret;
}

int gpio_request_list_by_name(struct udevice *dev, const char *list_name,
			      struct gpio_desc *desc, int max_count,
			      int flags)
{
	/*
	 * This isn't ideal since we don't use dev->name in the debug()
	 * calls in gpio_request_by_name(), but we can do this until
	 * gpio_request_list_by_name_nodev() can be dropped.
	 */
	return gpio_request_list_by_name_nodev(gd->fdt_blob, dev->of_offset,
					       list_name, desc, max_count,
					       flags);
}

int gpio_get_list_count(struct udevice *dev, const char *list_name)
{
	int ret;

	ret = fdtdec_parse_phandle_with_args(gd->fdt_blob, dev->of_offset,
					     list_name, "#gpio-cells", 0, -1,
					     NULL);
	if (ret) {
		debug("%s: Node '%s', property '%s', GPIO count failed: %d\n",
		      __func__, dev->name, list_name, ret);
	}

	return ret;
}

int dm_gpio_free(struct udevice *dev, struct gpio_desc *desc)
{
	/* For now, we don't do any checking of dev */
	return _dm_gpio_free(desc->dev, desc->offset);
}

int gpio_free_list(struct udevice *dev, struct gpio_desc *desc, int count)
{
	int i;

	/* For now, we don't do any checking of dev */
	for (i = 0; i < count; i++)
		dm_gpio_free(dev, &desc[i]);

	return 0;
}

int gpio_free_list_nodev(struct gpio_desc *desc, int count)
{
	return gpio_free_list(NULL, desc, count);
}

S
Simon Glass 已提交
743
/* We need to renumber the GPIOs when any driver is probed/removed */
744
static int gpio_renumber(struct udevice *removed_dev)
S
Simon Glass 已提交
745 746
{
	struct gpio_dev_priv *uc_priv;
747
	struct udevice *dev;
S
Simon Glass 已提交
748 749 750 751 752 753 754 755 756 757 758
	struct uclass *uc;
	unsigned base;
	int ret;

	ret = uclass_get(UCLASS_GPIO, &uc);
	if (ret)
		return ret;

	/* Ensure that we have a base for each bank */
	base = 0;
	uclass_foreach_dev(dev, uc) {
759
		if (device_active(dev) && dev != removed_dev) {
760
			uc_priv = dev_get_uclass_priv(dev);
S
Simon Glass 已提交
761 762 763 764 765 766 767 768
			uc_priv->gpio_base = base;
			base += uc_priv->gpio_count;
		}
	}

	return 0;
}

769 770 771 772 773 774 775 776 777 778 779 780
int gpio_get_number(struct gpio_desc *desc)
{
	struct udevice *dev = desc->dev;
	struct gpio_dev_priv *uc_priv;

	if (!dev)
		return -1;
	uc_priv = dev->uclass_priv;

	return uc_priv->gpio_base + desc->offset;
}

781
static int gpio_post_probe(struct udevice *dev)
S
Simon Glass 已提交
782
{
783
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
784 785 786 787 788 789

	uc_priv->name = calloc(uc_priv->gpio_count, sizeof(char *));
	if (!uc_priv->name)
		return -ENOMEM;

	return gpio_renumber(NULL);
S
Simon Glass 已提交
790 791
}

792
static int gpio_pre_remove(struct udevice *dev)
S
Simon Glass 已提交
793
{
794
	struct gpio_dev_priv *uc_priv = dev_get_uclass_priv(dev);
795 796 797 798 799 800 801 802 803
	int i;

	for (i = 0; i < uc_priv->gpio_count; i++) {
		if (uc_priv->name[i])
			free(uc_priv->name[i]);
	}
	free(uc_priv->name);

	return gpio_renumber(dev);
S
Simon Glass 已提交
804 805 806 807 808
}

UCLASS_DRIVER(gpio) = {
	.id		= UCLASS_GPIO,
	.name		= "gpio",
809
	.flags		= DM_UC_FLAG_SEQ_ALIAS,
S
Simon Glass 已提交
810 811 812 813
	.post_probe	= gpio_post_probe,
	.pre_remove	= gpio_pre_remove,
	.per_device_auto_alloc_size = sizeof(struct gpio_dev_priv),
};