xusb.c 35.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
 * Copyright (c) 2014-2022, NVIDIA CORPORATION.  All rights reserved.
4 5 6 7 8 9 10
 */

#include <linux/delay.h>
#include <linux/io.h>
#include <linux/mailbox_client.h>
#include <linux/module.h>
#include <linux/of.h>
11
#include <linux/of_platform.h>
12
#include <linux/phy/phy.h>
13
#include <linux/phy/tegra/xusb.h>
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#include <linux/platform_device.h>
#include <linux/regulator/consumer.h>
#include <linux/reset.h>
#include <linux/slab.h>
#include <linux/workqueue.h>

#include <soc/tegra/fuse.h>

#include "xusb.h"

static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
					   struct of_phandle_args *args)
{
	struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
	struct phy *phy = NULL;
	unsigned int i;

	if (args->args_count != 0)
		return ERR_PTR(-EINVAL);

	for (i = 0; i < pad->soc->num_lanes; i++) {
		if (!pad->lanes[i])
			continue;

		if (pad->lanes[i]->dev.of_node == args->np) {
			phy = pad->lanes[i];
			break;
		}
	}

	if (phy == NULL)
		phy = ERR_PTR(-ENODEV);

	return phy;
}

static const struct of_device_id tegra_xusb_padctl_of_match[] = {
#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
	{
		.compatible = "nvidia,tegra124-xusb-padctl",
		.data = &tegra124_xusb_padctl_soc,
	},
#endif
#if defined(CONFIG_ARCH_TEGRA_210_SOC)
	{
		.compatible = "nvidia,tegra210-xusb-padctl",
		.data = &tegra210_xusb_padctl_soc,
	},
62 63 64 65 66 67
#endif
#if defined(CONFIG_ARCH_TEGRA_186_SOC)
	{
		.compatible = "nvidia,tegra186-xusb-padctl",
		.data = &tegra186_xusb_padctl_soc,
	},
68 69 70 71 72 73
#endif
#if defined(CONFIG_ARCH_TEGRA_194_SOC)
	{
		.compatible = "nvidia,tegra194-xusb-padctl",
		.data = &tegra194_xusb_padctl_soc,
	},
74 75 76 77 78 79
#endif
#if defined(CONFIG_ARCH_TEGRA_234_SOC)
	{
		.compatible = "nvidia,tegra234-xusb-padctl",
		.data = &tegra234_xusb_padctl_soc,
	},
80 81 82 83 84 85 86 87
#endif
	{ }
};
MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);

static struct device_node *
tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
{
88 89 90 91 92
	struct device_node *pads, *np;

	pads = of_get_child_by_name(padctl->dev->of_node, "pads");
	if (!pads)
		return NULL;
93

94 95
	np = of_get_child_by_name(pads, name);
	of_node_put(pads);
96 97 98 99 100 101 102

	return np;
}

static struct device_node *
tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
{
103
	struct device_node *np, *lanes;
104

105 106
	lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
	if (!lanes)
107 108
		return NULL;

109 110 111 112
	np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
	of_node_put(lanes);

	return np;
113 114 115 116 117 118 119 120 121 122 123 124 125
}

int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
			     struct device_node *np)
{
	struct device *dev = &lane->pad->dev;
	const char *function;
	int err;

	err = of_property_read_string(np, "nvidia,function", &function);
	if (err < 0)
		return err;

126
	err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
127
	if (err < 0) {
128 129
		dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
			function, np);
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
		return err;
	}

	lane->function = err;

	return 0;
}

static void tegra_xusb_lane_destroy(struct phy *phy)
{
	if (phy) {
		struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

		lane->pad->ops->remove(lane);
		phy_destroy(phy);
	}
}

static void tegra_xusb_pad_release(struct device *dev)
{
	struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);

	pad->soc->ops->remove(pad);
}

155
static const struct device_type tegra_xusb_pad_type = {
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	.release = tegra_xusb_pad_release,
};

int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
			struct tegra_xusb_padctl *padctl,
			struct device_node *np)
{
	int err;

	device_initialize(&pad->dev);
	INIT_LIST_HEAD(&pad->list);
	pad->dev.parent = padctl->dev;
	pad->dev.type = &tegra_xusb_pad_type;
	pad->dev.of_node = np;
	pad->padctl = padctl;

	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
	if (err < 0)
		goto unregister;

	err = device_add(&pad->dev);
	if (err < 0)
		goto unregister;

	return 0;

unregister:
	device_unregister(&pad->dev);
	return err;
}

int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
			    const struct phy_ops *ops)
{
	struct device_node *children;
	struct phy *lane;
	unsigned int i;
	int err;

195
	children = of_get_child_by_name(pad->dev.of_node, "lanes");
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
	if (!children)
		return -ENODEV;

	pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
				  GFP_KERNEL);
	if (!pad->lanes) {
		of_node_put(children);
		return -ENOMEM;
	}

	for (i = 0; i < pad->soc->num_lanes; i++) {
		struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
		struct tegra_xusb_lane *lane;

		/* skip disabled lanes */
		if (!np || !of_device_is_available(np)) {
			of_node_put(np);
			continue;
		}

		pad->lanes[i] = phy_create(&pad->dev, np, ops);
		if (IS_ERR(pad->lanes[i])) {
			err = PTR_ERR(pad->lanes[i]);
			of_node_put(np);
			goto remove;
		}

		lane = pad->ops->probe(pad, np, i);
		if (IS_ERR(lane)) {
			phy_destroy(pad->lanes[i]);
			err = PTR_ERR(lane);
			goto remove;
		}

		list_add_tail(&lane->list, &pad->padctl->lanes);
		phy_set_drvdata(pad->lanes[i], lane);
	}

	pad->provider = of_phy_provider_register_full(&pad->dev, children,
						      tegra_xusb_pad_of_xlate);
	if (IS_ERR(pad->provider)) {
		err = PTR_ERR(pad->provider);
		goto remove;
	}

	return 0;

remove:
	while (i--)
		tegra_xusb_lane_destroy(pad->lanes[i]);

	of_node_put(children);

	return err;
}

void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
{
	unsigned int i = pad->soc->num_lanes;

	of_phy_provider_unregister(pad->provider);

	while (i--)
		tegra_xusb_lane_destroy(pad->lanes[i]);

	device_unregister(&pad->dev);
}

static struct tegra_xusb_pad *
tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
		      const struct tegra_xusb_pad_soc *soc)
{
	struct tegra_xusb_pad *pad;
	struct device_node *np;
	int err;

	np = tegra_xusb_find_pad_node(padctl, soc->name);
	if (!np || !of_device_is_available(np))
		return NULL;

	pad = soc->ops->probe(padctl, soc, np);
	if (IS_ERR(pad)) {
		err = PTR_ERR(pad);
		dev_err(padctl->dev, "failed to create pad %s: %d\n",
			soc->name, err);
		return ERR_PTR(err);
	}

	/* XXX move this into ->probe() to avoid string comparison */
	if (strcmp(soc->name, "pcie") == 0)
		padctl->pcie = pad;

	if (strcmp(soc->name, "sata") == 0)
		padctl->sata = pad;

	if (strcmp(soc->name, "usb2") == 0)
		padctl->usb2 = pad;

	if (strcmp(soc->name, "ulpi") == 0)
		padctl->ulpi = pad;

	if (strcmp(soc->name, "hsic") == 0)
		padctl->hsic = pad;

	return pad;
}

static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
{
	struct tegra_xusb_pad *pad, *tmp;

	list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
		list_del(&pad->list);
		tegra_xusb_pad_unregister(pad);
	}
}

static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
{
	mutex_lock(&padctl->lock);
	__tegra_xusb_remove_pads(padctl);
	mutex_unlock(&padctl->lock);
}

static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
{
	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
	const struct tegra_xusb_lane_soc *soc = lane->soc;
	u32 value;

326 327 328 329
	/* skip single function lanes */
	if (soc->num_funcs < 2)
		return;

330 331 332
	if (lane->pad->ops->iddq_enable)
		lane->pad->ops->iddq_enable(lane);

333 334 335 336 337
	/* choose function */
	value = padctl_readl(padctl, soc->offset);
	value &= ~(soc->mask << soc->shift);
	value |= lane->function << soc->shift;
	padctl_writel(padctl, value, soc->offset);
338 339 340

	if (lane->pad->ops->iddq_disable)
		lane->pad->ops->iddq_disable(lane);
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 388 389 390
}

static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
{
	unsigned int i;

	for (i = 0; i < pad->soc->num_lanes; i++) {
		struct tegra_xusb_lane *lane;

		if (pad->lanes[i]) {
			lane = phy_get_drvdata(pad->lanes[i]);
			tegra_xusb_lane_program(lane);
		}
	}
}

static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
{
	struct tegra_xusb_pad *pad;
	unsigned int i;

	mutex_lock(&padctl->lock);

	for (i = 0; i < padctl->soc->num_pads; i++) {
		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
		int err;

		pad = tegra_xusb_pad_create(padctl, soc);
		if (IS_ERR(pad)) {
			err = PTR_ERR(pad);
			dev_err(padctl->dev, "failed to create pad %s: %d\n",
				soc->name, err);
			__tegra_xusb_remove_pads(padctl);
			mutex_unlock(&padctl->lock);
			return err;
		}

		if (!pad)
			continue;

		list_add_tail(&pad->list, &padctl->pads);
	}

	list_for_each_entry(pad, &padctl->pads, list)
		tegra_xusb_pad_program(pad);

	mutex_unlock(&padctl->lock);
	return 0;
}

391
bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
				  const char *function)
{
	const char *func = lane->soc->funcs[lane->function];

	return strcmp(function, func) == 0;
}

struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
					     const char *type,
					     unsigned int index)
{
	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
	char *name;

	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
	if (!name)
		return ERR_PTR(-ENOMEM);

	list_for_each_entry(lane, &padctl->lanes, list) {
		if (strcmp(lane->soc->name, name) == 0) {
			hit = lane;
			break;
		}
	}

	kfree(name);
	return hit;
}

struct tegra_xusb_lane *
tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
			  const struct tegra_xusb_lane_map *map,
			  const char *function)
{
	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);

428
	for (; map->type; map++) {
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
		if (port->index != map->port)
			continue;

		lane = tegra_xusb_find_lane(port->padctl, map->type,
					    map->index);
		if (IS_ERR(lane))
			continue;

		if (!tegra_xusb_lane_check(lane, function))
			continue;

		if (!IS_ERR(match))
			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
				map->type, map->index, match->soc->name);
		else
			match = lane;
	}

	return match;
}

static struct device_node *
tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
			  unsigned int index)
{
454 455
	struct device_node *ports, *np;
	char *name;
456

457 458 459
	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
	if (!ports)
		return NULL;
460

461 462 463
	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
	if (!name) {
		of_node_put(ports);
464
		return NULL;
465
	}
466 467 468
	np = of_get_child_by_name(ports, name);
	kfree(name);
	of_node_put(ports);
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521

	return np;
}

struct tegra_xusb_port *
tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
		     unsigned int index)
{
	struct tegra_xusb_port *port;
	struct device_node *np;

	np = tegra_xusb_find_port_node(padctl, type, index);
	if (!np)
		return NULL;

	list_for_each_entry(port, &padctl->ports, list) {
		if (np == port->dev.of_node) {
			of_node_put(np);
			return port;
		}
	}

	of_node_put(np);

	return NULL;
}

struct tegra_xusb_usb2_port *
tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
{
	struct tegra_xusb_port *port;

	port = tegra_xusb_find_port(padctl, "usb2", index);
	if (port)
		return to_usb2_port(port);

	return NULL;
}

struct tegra_xusb_usb3_port *
tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
{
	struct tegra_xusb_port *port;

	port = tegra_xusb_find_port(padctl, "usb3", index);
	if (port)
		return to_usb3_port(port);

	return NULL;
}

static void tegra_xusb_port_release(struct device *dev)
{
522 523 524 525
	struct tegra_xusb_port *port = to_tegra_xusb_port(dev);

	if (port->ops->release)
		port->ops->release(port);
526 527
}

528
static const struct device_type tegra_xusb_port_type = {
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 556 557 558 559 560 561 562 563 564 565
	.release = tegra_xusb_port_release,
};

static int tegra_xusb_port_init(struct tegra_xusb_port *port,
				struct tegra_xusb_padctl *padctl,
				struct device_node *np,
				const char *name,
				unsigned int index)
{
	int err;

	INIT_LIST_HEAD(&port->list);
	port->padctl = padctl;
	port->index = index;

	device_initialize(&port->dev);
	port->dev.type = &tegra_xusb_port_type;
	port->dev.of_node = of_node_get(np);
	port->dev.parent = padctl->dev;

	err = dev_set_name(&port->dev, "%s-%u", name, index);
	if (err < 0)
		goto unregister;

	err = device_add(&port->dev);
	if (err < 0)
		goto unregister;

	return 0;

unregister:
	device_unregister(&port->dev);
	return err;
}

static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
{
566 567 568
	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
		of_platform_depopulate(&port->dev);
		usb_role_switch_unregister(port->usb_role_sw);
569 570
		cancel_work_sync(&port->usb_phy_work);
		usb_remove_phy(&port->usb_phy);
571
		port->usb_phy.dev->driver = NULL;
572 573
	}

574 575 576
	if (port->ops->remove)
		port->ops->remove(port);

577 578 579
	device_unregister(&port->dev);
}

580 581 582 583 584 585 586
static const char *const modes[] = {
	[USB_DR_MODE_UNKNOWN] = "",
	[USB_DR_MODE_HOST] = "host",
	[USB_DR_MODE_PERIPHERAL] = "peripheral",
	[USB_DR_MODE_OTG] = "otg",
};

587 588 589 590 591 592
static const char * const usb_roles[] = {
	[USB_ROLE_NONE]		= "none",
	[USB_ROLE_HOST]		= "host",
	[USB_ROLE_DEVICE]	= "device",
};

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
static enum usb_phy_events to_usb_phy_event(enum usb_role role)
{
	switch (role) {
	case USB_ROLE_DEVICE:
		return USB_EVENT_VBUS;

	case USB_ROLE_HOST:
		return USB_EVENT_ID;

	default:
		return USB_EVENT_NONE;
	}
}

static void tegra_xusb_usb_phy_work(struct work_struct *work)
{
	struct tegra_xusb_port *port = container_of(work,
						    struct tegra_xusb_port,
						    usb_phy_work);
	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);

	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));

	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
		usb_roles[role]);

	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
}

622 623 624 625 626 627 628
static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
				  enum usb_role role)
{
	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);

	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);

629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	schedule_work(&port->usb_phy_work);

	return 0;
}

static int tegra_xusb_set_peripheral(struct usb_otg *otg,
				     struct usb_gadget *gadget)
{
	struct tegra_xusb_port *port = container_of(otg->usb_phy,
						    struct tegra_xusb_port,
						    usb_phy);

	if (gadget != NULL)
		schedule_work(&port->usb_phy_work);

644 645 646
	return 0;
}

647 648 649 650 651 652 653 654 655 656 657 658 659
static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
{
	struct tegra_xusb_port *port = container_of(otg->usb_phy,
						    struct tegra_xusb_port,
						    usb_phy);

	if (host != NULL)
		schedule_work(&port->usb_phy_work);

	return 0;
}


660 661
static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
{
662
	struct tegra_xusb_lane *lane;
663 664 665
	struct usb_role_switch_desc role_sx_desc = {
		.fwnode = dev_fwnode(&port->dev),
		.set = tegra_xusb_role_sw_set,
666
		.allow_userspace_control = true,
667 668 669 670 671 672 673 674 675 676 677 678
	};
	int err = 0;

	/*
	 * USB role switch driver needs parent driver owner info. This is a
	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
	 * where an optimal solution is possible with changes to USB role
	 * switch driver.
	 */
	port->dev.driver = devm_kzalloc(&port->dev,
					sizeof(struct device_driver),
					GFP_KERNEL);
679 680 681
	if (!port->dev.driver)
		return -ENOMEM;

682 683 684 685 686 687 688 689 690 691 692
	port->dev.driver->owner	 = THIS_MODULE;

	port->usb_role_sw = usb_role_switch_register(&port->dev,
						     &role_sx_desc);
	if (IS_ERR(port->usb_role_sw)) {
		err = PTR_ERR(port->usb_role_sw);
		dev_err(&port->dev, "failed to register USB role switch: %d",
			err);
		return err;
	}

693
	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
694 695
	usb_role_switch_set_drvdata(port->usb_role_sw, port);

696 697 698 699 700 701 702 703 704 705 706 707
	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
					 GFP_KERNEL);
	if (!port->usb_phy.otg)
		return -ENOMEM;

	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);

	/*
	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
	 * reference to retrieve usb-phy details.
	 */
	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
708
	port->usb_phy.dev->driver = port->dev.driver;
709 710 711 712 713 714 715 716 717 718
	port->usb_phy.otg->usb_phy = &port->usb_phy;
	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
	port->usb_phy.otg->set_host = tegra_xusb_set_host;

	err = usb_add_phy_dev(&port->usb_phy);
	if (err < 0) {
		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
		return err;
	}

719 720 721 722 723 724
	/* populate connector entry */
	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);

	return err;
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static void tegra_xusb_parse_usb_role_default_mode(struct tegra_xusb_port *port)
{
	enum usb_role role = USB_ROLE_NONE;
	enum usb_dr_mode mode = usb_get_role_switch_default_mode(&port->dev);

	if (mode == USB_DR_MODE_HOST)
		role = USB_ROLE_HOST;
	else if (mode == USB_DR_MODE_PERIPHERAL)
		role = USB_ROLE_DEVICE;

	if (role != USB_ROLE_NONE) {
		usb_role_switch_set_role(port->usb_role_sw, role);
		dev_dbg(&port->dev, "usb role default mode is %s", modes[mode]);
	}
}

741 742 743 744
static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
{
	struct tegra_xusb_port *port = &usb2->base;
	struct device_node *np = port->dev.of_node;
745
	const char *mode;
746
	int err;
747 748 749

	usb2->internal = of_property_read_bool(np, "nvidia,internal");

750 751 752 753 754 755 756 757 758 759 760 761 762
	if (!of_property_read_string(np, "mode", &mode)) {
		int err = match_string(modes, ARRAY_SIZE(modes), mode);
		if (err < 0) {
			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
				mode);
			usb2->mode = USB_DR_MODE_UNKNOWN;
		} else {
			usb2->mode = err;
		}
	} else {
		usb2->mode = USB_DR_MODE_HOST;
	}

763 764 765 766 767 768 769
	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
	    usb2->mode == USB_DR_MODE_OTG) {
		if (of_property_read_bool(np, "usb-role-switch")) {
			err = tegra_xusb_setup_usb_role_switch(port);
			if (err < 0)
				return err;
770
			tegra_xusb_parse_usb_role_default_mode(port);
771 772 773 774 775 776 777
		} else {
			dev_err(&port->dev, "usb-role-switch not found for %s mode",
				modes[usb2->mode]);
			return -EINVAL;
		}
	}

778
	usb2->supply = regulator_get(&port->dev, "vbus");
779
	return PTR_ERR_OR_ZERO(usb2->supply);
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
}

static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
				    unsigned int index)
{
	struct tegra_xusb_usb2_port *usb2;
	struct device_node *np;
	int err = 0;

	/*
	 * USB2 ports don't require additional properties, but if the port is
	 * marked as disabled there is no reason to register it.
	 */
	np = tegra_xusb_find_port_node(padctl, "usb2", index);
	if (!np || !of_device_is_available(np))
		goto out;

797
	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
798 799 800 801 802 803 804 805 806 807 808 809 810 811
	if (!usb2) {
		err = -ENOMEM;
		goto out;
	}

	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
	if (err < 0)
		goto out;

	usb2->base.ops = padctl->soc->ports.usb2.ops;

	usb2->base.lane = usb2->base.ops->map(&usb2->base);
	if (IS_ERR(usb2->base.lane)) {
		err = PTR_ERR(usb2->base.lane);
812
		tegra_xusb_port_unregister(&usb2->base);
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
		goto out;
	}

	err = tegra_xusb_usb2_port_parse_dt(usb2);
	if (err < 0) {
		tegra_xusb_port_unregister(&usb2->base);
		goto out;
	}

	list_add_tail(&usb2->base.list, &padctl->ports);

out:
	of_node_put(np);
	return err;
}

829 830 831 832 833 834 835
void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
{
	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);

	kfree(usb2);
}

836 837 838 839 840 841 842
void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
{
	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);

	regulator_put(usb2->supply);
}

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
{
	struct tegra_xusb_port *port = &ulpi->base;
	struct device_node *np = port->dev.of_node;

	ulpi->internal = of_property_read_bool(np, "nvidia,internal");

	return 0;
}

static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
				    unsigned int index)
{
	struct tegra_xusb_ulpi_port *ulpi;
	struct device_node *np;
	int err = 0;

	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
	if (!np || !of_device_is_available(np))
		goto out;

864
	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
865 866 867 868 869 870 871 872 873 874 875 876 877 878
	if (!ulpi) {
		err = -ENOMEM;
		goto out;
	}

	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
	if (err < 0)
		goto out;

	ulpi->base.ops = padctl->soc->ports.ulpi.ops;

	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
	if (IS_ERR(ulpi->base.lane)) {
		err = PTR_ERR(ulpi->base.lane);
879
		tegra_xusb_port_unregister(&ulpi->base);
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
		goto out;
	}

	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
	if (err < 0) {
		tegra_xusb_port_unregister(&ulpi->base);
		goto out;
	}

	list_add_tail(&ulpi->base.list, &padctl->ports);

out:
	of_node_put(np);
	return err;
}

896 897 898 899 900 901 902
void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
{
	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);

	kfree(ulpi);
}

903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
{
	/* XXX */
	return 0;
}

static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
				    unsigned int index)
{
	struct tegra_xusb_hsic_port *hsic;
	struct device_node *np;
	int err = 0;

	np = tegra_xusb_find_port_node(padctl, "hsic", index);
	if (!np || !of_device_is_available(np))
		goto out;

920
	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	if (!hsic) {
		err = -ENOMEM;
		goto out;
	}

	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
	if (err < 0)
		goto out;

	hsic->base.ops = padctl->soc->ports.hsic.ops;

	hsic->base.lane = hsic->base.ops->map(&hsic->base);
	if (IS_ERR(hsic->base.lane)) {
		err = PTR_ERR(hsic->base.lane);
		goto out;
	}

	err = tegra_xusb_hsic_port_parse_dt(hsic);
	if (err < 0) {
		tegra_xusb_port_unregister(&hsic->base);
		goto out;
	}

	list_add_tail(&hsic->base.list, &padctl->ports);

out:
	of_node_put(np);
	return err;
}

951 952 953 954 955 956 957
void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
{
	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);

	kfree(hsic);
}

958 959 960 961
static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
{
	struct tegra_xusb_port *port = &usb3->base;
	struct device_node *np = port->dev.of_node;
962
	enum usb_device_speed maximum_speed;
963 964 965 966 967 968 969 970 971 972 973 974 975
	u32 value;
	int err;

	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
	if (err < 0) {
		dev_err(&port->dev, "failed to read port: %d\n", err);
		return err;
	}

	usb3->port = value;

	usb3->internal = of_property_read_bool(np, "nvidia,internal");

976 977 978 979 980 981 982 983 984 985
	if (device_property_present(&port->dev, "maximum-speed")) {
		maximum_speed =  usb_get_maximum_speed(&port->dev);
		if (maximum_speed == USB_SPEED_SUPER)
			usb3->disable_gen2 = true;
		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
			usb3->disable_gen2 = false;
		else
			return -EINVAL;
	}

986
	return 0;
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
}

static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
				    unsigned int index)
{
	struct tegra_xusb_usb3_port *usb3;
	struct device_node *np;
	int err = 0;

	/*
	 * If there is no supplemental configuration in the device tree the
	 * port is unusable. But it is valid to configure only a single port,
	 * hence return 0 instead of an error to allow ports to be optional.
	 */
	np = tegra_xusb_find_port_node(padctl, "usb3", index);
	if (!np || !of_device_is_available(np))
		goto out;

1005
	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	if (!usb3) {
		err = -ENOMEM;
		goto out;
	}

	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
	if (err < 0)
		goto out;

	usb3->base.ops = padctl->soc->ports.usb3.ops;

	usb3->base.lane = usb3->base.ops->map(&usb3->base);
	if (IS_ERR(usb3->base.lane)) {
		err = PTR_ERR(usb3->base.lane);
		goto out;
	}

	err = tegra_xusb_usb3_port_parse_dt(usb3);
	if (err < 0) {
		tegra_xusb_port_unregister(&usb3->base);
		goto out;
	}

	list_add_tail(&usb3->base.list, &padctl->ports);

out:
	of_node_put(np);
	return err;
}

1036 1037 1038 1039 1040 1041 1042
void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
{
	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);

	kfree(usb3);
}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
{
	struct tegra_xusb_port *port, *tmp;

	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
		list_del(&port->list);
		tegra_xusb_port_unregister(port);
	}
}

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
{
	struct device_node *np;
	unsigned int i;

	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
		np = tegra_xusb_find_port_node(padctl, "usb3", i);
		if (!np || !of_device_is_available(np))
			return i;
	}

	return -ENODEV;
}

static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
{
	unsigned int i;
	struct tegra_xusb_usb3_port *usb3;
	struct tegra_xusb_padctl *padctl = usb2->base.padctl;

	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
		usb3 = tegra_xusb_find_usb3_port(padctl, i);
		if (usb3 && usb3->port == usb2->base.index)
			return true;
	}

	return false;
}

static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
{
	int fake;

	/* Disable usb3_port_fake usage by default and assign if needed */
	usb2->usb3_port_fake = -1;

	if ((usb2->mode == USB_DR_MODE_OTG ||
	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
		!tegra_xusb_port_is_companion(usb2)) {
		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
		if (fake < 0) {
			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
			return -ENODEV;
		}

		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
		usb2->usb3_port_fake = fake;
	}

	return 0;
}

1105 1106 1107
static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
{
	struct tegra_xusb_port *port;
1108
	struct tegra_xusb_usb2_port *usb2;
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
	unsigned int i;
	int err = 0;

	mutex_lock(&padctl->lock);

	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
		err = tegra_xusb_add_usb2_port(padctl, i);
		if (err < 0)
			goto remove_ports;
	}

	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
		err = tegra_xusb_add_ulpi_port(padctl, i);
		if (err < 0)
			goto remove_ports;
	}

	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
		err = tegra_xusb_add_hsic_port(padctl, i);
		if (err < 0)
			goto remove_ports;
	}

	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
		err = tegra_xusb_add_usb3_port(padctl, i);
		if (err < 0)
			goto remove_ports;
	}

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
	if (padctl->soc->need_fake_usb3_port) {
		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
			usb2 = tegra_xusb_find_usb2_port(padctl, i);
			if (!usb2)
				continue;

			err = tegra_xusb_update_usb3_fake_port(usb2);
			if (err < 0)
				goto remove_ports;
		}
	}

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	list_for_each_entry(port, &padctl->ports, list) {
		err = port->ops->enable(port);
		if (err < 0)
			dev_err(padctl->dev, "failed to enable port %s: %d\n",
				dev_name(&port->dev), err);
	}

	goto unlock;

remove_ports:
	__tegra_xusb_remove_ports(padctl);
unlock:
	mutex_unlock(&padctl->lock);
	return err;
}

static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
{
	mutex_lock(&padctl->lock);
	__tegra_xusb_remove_ports(padctl);
	mutex_unlock(&padctl->lock);
}

static int tegra_xusb_padctl_probe(struct platform_device *pdev)
{
1175
	struct device_node *np = pdev->dev.of_node;
1176 1177 1178 1179 1180 1181
	const struct tegra_xusb_padctl_soc *soc;
	struct tegra_xusb_padctl *padctl;
	const struct of_device_id *match;
	int err;

	/* for backwards compatibility with old device trees */
1182
	np = of_get_child_by_name(np, "pads");
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
	if (!np) {
		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
		return tegra_xusb_padctl_legacy_probe(pdev);
	}

	of_node_put(np);

	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
	soc = match->data;

	padctl = soc->ops->probe(&pdev->dev, soc);
	if (IS_ERR(padctl))
		return PTR_ERR(padctl);

	platform_set_drvdata(pdev, padctl);
	INIT_LIST_HEAD(&padctl->ports);
	INIT_LIST_HEAD(&padctl->lanes);
	INIT_LIST_HEAD(&padctl->pads);
	mutex_init(&padctl->lock);

1203
	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	if (IS_ERR(padctl->regs)) {
		err = PTR_ERR(padctl->regs);
		goto remove;
	}

	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
	if (IS_ERR(padctl->rst)) {
		err = PTR_ERR(padctl->rst);
		goto remove;
	}

1215 1216 1217 1218 1219 1220 1221
	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
					sizeof(*padctl->supplies), GFP_KERNEL);
	if (!padctl->supplies) {
		err = -ENOMEM;
		goto remove;
	}

1222 1223 1224
	regulator_bulk_set_supply_names(padctl->supplies,
					padctl->soc->supply_names,
					padctl->soc->num_supplies);
1225 1226 1227 1228

	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
				      padctl->supplies);
	if (err < 0) {
1229
		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1230 1231 1232
		goto remove;
	}

1233 1234 1235 1236
	err = reset_control_deassert(padctl->rst);
	if (err < 0)
		goto remove;

1237 1238 1239 1240 1241 1242 1243
	err = regulator_bulk_enable(padctl->soc->num_supplies,
				    padctl->supplies);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
		goto reset;
	}

1244 1245 1246
	err = tegra_xusb_setup_pads(padctl);
	if (err < 0) {
		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1247
		goto power_down;
1248 1249 1250 1251
	}

	err = tegra_xusb_setup_ports(padctl);
	if (err) {
1252 1253 1254 1255 1256 1257 1258
		const char *level = KERN_ERR;

		if (err == -EPROBE_DEFER)
			level = KERN_DEBUG;

		dev_printk(level, &pdev->dev,
			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1259 1260 1261 1262 1263 1264 1265
		goto remove_pads;
	}

	return 0;

remove_pads:
	tegra_xusb_remove_pads(padctl);
1266 1267
power_down:
	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1268 1269 1270
reset:
	reset_control_assert(padctl->rst);
remove:
1271
	platform_set_drvdata(pdev, NULL);
1272 1273 1274 1275
	soc->ops->remove(padctl);
	return err;
}

1276
static void tegra_xusb_padctl_remove(struct platform_device *pdev)
1277 1278 1279 1280 1281 1282 1283
{
	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
	int err;

	tegra_xusb_remove_ports(padctl);
	tegra_xusb_remove_pads(padctl);

1284 1285 1286 1287 1288
	err = regulator_bulk_disable(padctl->soc->num_supplies,
				     padctl->supplies);
	if (err < 0)
		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);

1289 1290 1291 1292 1293 1294 1295
	err = reset_control_assert(padctl->rst);
	if (err < 0)
		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);

	padctl->soc->ops->remove(padctl);
}

1296
static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1297 1298 1299 1300 1301 1302 1303 1304 1305
{
	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);

	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
		return padctl->soc->ops->suspend_noirq(padctl);

	return 0;
}

1306
static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
{
	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);

	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
		return padctl->soc->ops->resume_noirq(padctl);

	return 0;
}

static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
				      tegra_xusb_padctl_resume_noirq)
};

1321 1322 1323 1324
static struct platform_driver tegra_xusb_padctl_driver = {
	.driver = {
		.name = "tegra-xusb-padctl",
		.of_match_table = tegra_xusb_padctl_of_match,
1325
		.pm = &tegra_xusb_padctl_pm_ops,
1326 1327
	},
	.probe = tegra_xusb_padctl_probe,
1328
	.remove_new = tegra_xusb_padctl_remove,
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391
};
module_platform_driver(tegra_xusb_padctl_driver);

struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
{
	struct tegra_xusb_padctl *padctl;
	struct platform_device *pdev;
	struct device_node *np;

	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
	if (!np)
		return ERR_PTR(-EINVAL);

	/*
	 * This is slightly ugly. A better implementation would be to keep a
	 * registry of pad controllers, but since there will almost certainly
	 * only ever be one per SoC that would be a little overkill.
	 */
	pdev = of_find_device_by_node(np);
	if (!pdev) {
		of_node_put(np);
		return ERR_PTR(-ENODEV);
	}

	of_node_put(np);

	padctl = platform_get_drvdata(pdev);
	if (!padctl) {
		put_device(&pdev->dev);
		return ERR_PTR(-EPROBE_DEFER);
	}

	return padctl;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);

void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
{
	if (padctl)
		put_device(padctl->dev);
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);

int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
					unsigned int port)
{
	if (padctl->soc->ops->usb3_save_context)
		return padctl->soc->ops->usb3_save_context(padctl, port);

	return -ENOSYS;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);

int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
				    unsigned int port, bool idle)
{
	if (padctl->soc->ops->hsic_set_idle)
		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);

	return -ENOSYS;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);

1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447
int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
					   enum usb_device_speed speed)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

	if (lane->pad->ops->enable_phy_sleepwalk)
		return lane->pad->ops->enable_phy_sleepwalk(lane, speed);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);

int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

	if (lane->pad->ops->disable_phy_sleepwalk)
		return lane->pad->ops->disable_phy_sleepwalk(lane);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);

int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

	if (lane->pad->ops->enable_phy_wake)
		return lane->pad->ops->enable_phy_wake(lane);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);

int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

	if (lane->pad->ops->disable_phy_wake)
		return lane->pad->ops->disable_phy_wake(lane);

	return -EOPNOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);

bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);

	if (lane->pad->ops->remote_wake_detected)
		return lane->pad->ops->remote_wake_detected(lane);

	return false;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);

1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
					   unsigned int port, bool enable)
{
	if (padctl->soc->ops->usb3_set_lfps_detect)
		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
							      enable);

	return -ENOSYS;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);

1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480
int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
							bool val)
{
	if (padctl->soc->ops->vbus_override)
		return padctl->soc->ops->vbus_override(padctl, val);

	return -ENOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);

int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
{
	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
	struct tegra_xusb_padctl *padctl = lane->pad->padctl;

	if (padctl->soc->ops->utmi_port_reset)
		return padctl->soc->ops->utmi_port_reset(phy);

	return -ENOTSUPP;
}
EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);

1481 1482
void tegra_phy_xusb_utmi_pad_power_on(struct phy *phy)
{
1483 1484 1485 1486 1487 1488 1489 1490
	struct tegra_xusb_lane *lane;
	struct tegra_xusb_padctl *padctl;

	if (!phy)
		return;

	lane = phy_get_drvdata(phy);
	padctl = lane->pad->padctl;
1491 1492 1493 1494 1495 1496 1497 1498

	if (padctl->soc->ops->utmi_pad_power_on)
		padctl->soc->ops->utmi_pad_power_on(phy);
}
EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on);

void tegra_phy_xusb_utmi_pad_power_down(struct phy *phy)
{
1499 1500 1501 1502 1503 1504 1505 1506
	struct tegra_xusb_lane *lane;
	struct tegra_xusb_padctl *padctl;

	if (!phy)
		return;

	lane = phy_get_drvdata(phy);
	padctl = lane->pad->padctl;
1507 1508 1509 1510 1511 1512

	if (padctl->soc->ops->utmi_pad_power_down)
		padctl->soc->ops->utmi_pad_power_down(phy);
}
EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down);

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
				    unsigned int port)
{
	struct tegra_xusb_usb2_port *usb2;
	struct tegra_xusb_usb3_port *usb3;
	int i;

	usb2 = tegra_xusb_find_usb2_port(padctl, port);
	if (!usb2)
		return -EINVAL;

	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
		usb3 = tegra_xusb_find_usb3_port(padctl, i);
		if (usb3 && usb3->port == usb2->base.index)
			return usb3->base.index;
	}

	return -ENODEV;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);

1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
int tegra_xusb_padctl_get_port_number(struct phy *phy)
{
	struct tegra_xusb_lane *lane;

	if (!phy)
		return -ENODEV;

	lane = phy_get_drvdata(phy);

	return lane->index;
}
EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_port_number);

1547 1548 1549
MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
MODULE_LICENSE("GPL v2");