Linux Audio

Check our new training course

Loading...
v5.9
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2014-2016, NVIDIA CORPORATION.  All rights reserved.
   4 */
   5
   6#include <linux/delay.h>
   7#include <linux/io.h>
   8#include <linux/mailbox_client.h>
   9#include <linux/module.h>
  10#include <linux/of.h>
  11#include <linux/of_device.h>
  12#include <linux/phy/phy.h>
  13#include <linux/phy/tegra/xusb.h>
  14#include <linux/platform_device.h>
  15#include <linux/regulator/consumer.h>
  16#include <linux/reset.h>
  17#include <linux/slab.h>
  18#include <linux/workqueue.h>
  19
  20#include <soc/tegra/fuse.h>
  21
  22#include "xusb.h"
  23
  24static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
  25					   struct of_phandle_args *args)
  26{
  27	struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
  28	struct phy *phy = NULL;
  29	unsigned int i;
  30
  31	if (args->args_count != 0)
  32		return ERR_PTR(-EINVAL);
  33
  34	for (i = 0; i < pad->soc->num_lanes; i++) {
  35		if (!pad->lanes[i])
  36			continue;
  37
  38		if (pad->lanes[i]->dev.of_node == args->np) {
  39			phy = pad->lanes[i];
  40			break;
  41		}
  42	}
  43
  44	if (phy == NULL)
  45		phy = ERR_PTR(-ENODEV);
  46
  47	return phy;
  48}
  49
  50static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  51#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
  52	{
  53		.compatible = "nvidia,tegra124-xusb-padctl",
  54		.data = &tegra124_xusb_padctl_soc,
  55	},
  56#endif
  57#if defined(CONFIG_ARCH_TEGRA_210_SOC)
  58	{
  59		.compatible = "nvidia,tegra210-xusb-padctl",
  60		.data = &tegra210_xusb_padctl_soc,
  61	},
  62#endif
  63#if defined(CONFIG_ARCH_TEGRA_186_SOC)
  64	{
  65		.compatible = "nvidia,tegra186-xusb-padctl",
  66		.data = &tegra186_xusb_padctl_soc,
  67	},
  68#endif
  69#if defined(CONFIG_ARCH_TEGRA_194_SOC)
  70	{
  71		.compatible = "nvidia,tegra194-xusb-padctl",
  72		.data = &tegra194_xusb_padctl_soc,
  73	},
  74#endif
  75	{ }
  76};
  77MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  78
  79static struct device_node *
  80tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  81{
  82	struct device_node *pads, *np;
  83
  84	pads = of_get_child_by_name(padctl->dev->of_node, "pads");
  85	if (!pads)
  86		return NULL;
  87
  88	np = of_get_child_by_name(pads, name);
  89	of_node_put(pads);
  90
  91	return np;
  92}
  93
  94static struct device_node *
  95tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
  96{
  97	struct device_node *np, *lanes;
  98
  99	lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
 100	if (!lanes)
 101		return NULL;
 102
 103	np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
 104	of_node_put(lanes);
 105
 106	return np;
 107}
 108
 109int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
 110			     struct device_node *np)
 111{
 112	struct device *dev = &lane->pad->dev;
 113	const char *function;
 114	int err;
 115
 116	err = of_property_read_string(np, "nvidia,function", &function);
 117	if (err < 0)
 118		return err;
 119
 120	err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
 121	if (err < 0) {
 122		dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
 123			function, np);
 124		return err;
 125	}
 126
 127	lane->function = err;
 128
 129	return 0;
 130}
 131
 132static void tegra_xusb_lane_destroy(struct phy *phy)
 133{
 134	if (phy) {
 135		struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
 136
 137		lane->pad->ops->remove(lane);
 138		phy_destroy(phy);
 139	}
 140}
 141
 142static void tegra_xusb_pad_release(struct device *dev)
 143{
 144	struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
 145
 146	pad->soc->ops->remove(pad);
 147}
 148
 149static struct device_type tegra_xusb_pad_type = {
 150	.release = tegra_xusb_pad_release,
 151};
 152
 153int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
 154			struct tegra_xusb_padctl *padctl,
 155			struct device_node *np)
 156{
 157	int err;
 158
 159	device_initialize(&pad->dev);
 160	INIT_LIST_HEAD(&pad->list);
 161	pad->dev.parent = padctl->dev;
 162	pad->dev.type = &tegra_xusb_pad_type;
 163	pad->dev.of_node = np;
 164	pad->padctl = padctl;
 165
 166	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
 167	if (err < 0)
 168		goto unregister;
 169
 170	err = device_add(&pad->dev);
 171	if (err < 0)
 172		goto unregister;
 173
 174	return 0;
 175
 176unregister:
 177	device_unregister(&pad->dev);
 178	return err;
 179}
 180
 181int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
 182			    const struct phy_ops *ops)
 183{
 184	struct device_node *children;
 185	struct phy *lane;
 186	unsigned int i;
 187	int err;
 188
 189	children = of_get_child_by_name(pad->dev.of_node, "lanes");
 190	if (!children)
 191		return -ENODEV;
 192
 193	pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
 194				  GFP_KERNEL);
 195	if (!pad->lanes) {
 196		of_node_put(children);
 197		return -ENOMEM;
 198	}
 199
 200	for (i = 0; i < pad->soc->num_lanes; i++) {
 201		struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
 202		struct tegra_xusb_lane *lane;
 203
 204		/* skip disabled lanes */
 205		if (!np || !of_device_is_available(np)) {
 206			of_node_put(np);
 207			continue;
 208		}
 209
 210		pad->lanes[i] = phy_create(&pad->dev, np, ops);
 211		if (IS_ERR(pad->lanes[i])) {
 212			err = PTR_ERR(pad->lanes[i]);
 213			of_node_put(np);
 214			goto remove;
 215		}
 216
 217		lane = pad->ops->probe(pad, np, i);
 218		if (IS_ERR(lane)) {
 219			phy_destroy(pad->lanes[i]);
 220			err = PTR_ERR(lane);
 221			goto remove;
 222		}
 223
 224		list_add_tail(&lane->list, &pad->padctl->lanes);
 225		phy_set_drvdata(pad->lanes[i], lane);
 226	}
 227
 228	pad->provider = of_phy_provider_register_full(&pad->dev, children,
 229						      tegra_xusb_pad_of_xlate);
 230	if (IS_ERR(pad->provider)) {
 231		err = PTR_ERR(pad->provider);
 232		goto remove;
 233	}
 234
 235	return 0;
 236
 237remove:
 238	while (i--)
 239		tegra_xusb_lane_destroy(pad->lanes[i]);
 240
 241	of_node_put(children);
 242
 243	return err;
 244}
 245
 246void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
 247{
 248	unsigned int i = pad->soc->num_lanes;
 249
 250	of_phy_provider_unregister(pad->provider);
 251
 252	while (i--)
 253		tegra_xusb_lane_destroy(pad->lanes[i]);
 254
 255	device_unregister(&pad->dev);
 256}
 257
 258static struct tegra_xusb_pad *
 259tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
 260		      const struct tegra_xusb_pad_soc *soc)
 261{
 262	struct tegra_xusb_pad *pad;
 263	struct device_node *np;
 264	int err;
 265
 266	np = tegra_xusb_find_pad_node(padctl, soc->name);
 267	if (!np || !of_device_is_available(np))
 268		return NULL;
 269
 270	pad = soc->ops->probe(padctl, soc, np);
 271	if (IS_ERR(pad)) {
 272		err = PTR_ERR(pad);
 273		dev_err(padctl->dev, "failed to create pad %s: %d\n",
 274			soc->name, err);
 275		return ERR_PTR(err);
 276	}
 277
 278	/* XXX move this into ->probe() to avoid string comparison */
 279	if (strcmp(soc->name, "pcie") == 0)
 280		padctl->pcie = pad;
 281
 282	if (strcmp(soc->name, "sata") == 0)
 283		padctl->sata = pad;
 284
 285	if (strcmp(soc->name, "usb2") == 0)
 286		padctl->usb2 = pad;
 287
 288	if (strcmp(soc->name, "ulpi") == 0)
 289		padctl->ulpi = pad;
 290
 291	if (strcmp(soc->name, "hsic") == 0)
 292		padctl->hsic = pad;
 293
 294	return pad;
 295}
 296
 297static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 298{
 299	struct tegra_xusb_pad *pad, *tmp;
 300
 301	list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
 302		list_del(&pad->list);
 303		tegra_xusb_pad_unregister(pad);
 304	}
 305}
 306
 307static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 308{
 309	mutex_lock(&padctl->lock);
 310	__tegra_xusb_remove_pads(padctl);
 311	mutex_unlock(&padctl->lock);
 312}
 313
 314static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
 315{
 316	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
 317	const struct tegra_xusb_lane_soc *soc = lane->soc;
 318	u32 value;
 319
 320	/* skip single function lanes */
 321	if (soc->num_funcs < 2)
 322		return;
 323
 
 
 
 324	/* choose function */
 325	value = padctl_readl(padctl, soc->offset);
 326	value &= ~(soc->mask << soc->shift);
 327	value |= lane->function << soc->shift;
 328	padctl_writel(padctl, value, soc->offset);
 
 
 
 329}
 330
 331static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
 332{
 333	unsigned int i;
 334
 335	for (i = 0; i < pad->soc->num_lanes; i++) {
 336		struct tegra_xusb_lane *lane;
 337
 338		if (pad->lanes[i]) {
 339			lane = phy_get_drvdata(pad->lanes[i]);
 340			tegra_xusb_lane_program(lane);
 341		}
 342	}
 343}
 344
 345static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
 346{
 347	struct tegra_xusb_pad *pad;
 348	unsigned int i;
 349
 350	mutex_lock(&padctl->lock);
 351
 352	for (i = 0; i < padctl->soc->num_pads; i++) {
 353		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
 354		int err;
 355
 356		pad = tegra_xusb_pad_create(padctl, soc);
 357		if (IS_ERR(pad)) {
 358			err = PTR_ERR(pad);
 359			dev_err(padctl->dev, "failed to create pad %s: %d\n",
 360				soc->name, err);
 361			__tegra_xusb_remove_pads(padctl);
 362			mutex_unlock(&padctl->lock);
 363			return err;
 364		}
 365
 366		if (!pad)
 367			continue;
 368
 369		list_add_tail(&pad->list, &padctl->pads);
 370	}
 371
 372	list_for_each_entry(pad, &padctl->pads, list)
 373		tegra_xusb_pad_program(pad);
 374
 375	mutex_unlock(&padctl->lock);
 376	return 0;
 377}
 378
 379static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
 380				  const char *function)
 381{
 382	const char *func = lane->soc->funcs[lane->function];
 383
 384	return strcmp(function, func) == 0;
 385}
 386
 387struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
 388					     const char *type,
 389					     unsigned int index)
 390{
 391	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
 392	char *name;
 393
 394	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 395	if (!name)
 396		return ERR_PTR(-ENOMEM);
 397
 398	list_for_each_entry(lane, &padctl->lanes, list) {
 399		if (strcmp(lane->soc->name, name) == 0) {
 400			hit = lane;
 401			break;
 402		}
 403	}
 404
 405	kfree(name);
 406	return hit;
 407}
 408
 409struct tegra_xusb_lane *
 410tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
 411			  const struct tegra_xusb_lane_map *map,
 412			  const char *function)
 413{
 414	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
 415
 416	for (; map->type; map++) {
 417		if (port->index != map->port)
 418			continue;
 419
 420		lane = tegra_xusb_find_lane(port->padctl, map->type,
 421					    map->index);
 422		if (IS_ERR(lane))
 423			continue;
 424
 425		if (!tegra_xusb_lane_check(lane, function))
 426			continue;
 427
 428		if (!IS_ERR(match))
 429			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
 430				map->type, map->index, match->soc->name);
 431		else
 432			match = lane;
 433	}
 434
 435	return match;
 436}
 437
 438static struct device_node *
 439tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
 440			  unsigned int index)
 441{
 442	struct device_node *ports, *np;
 443	char *name;
 444
 445	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
 446	if (!ports)
 447		return NULL;
 448
 449	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 450	if (!name) {
 451		of_node_put(ports);
 452		return ERR_PTR(-ENOMEM);
 453	}
 454	np = of_get_child_by_name(ports, name);
 455	kfree(name);
 456	of_node_put(ports);
 457
 458	return np;
 459}
 460
 461struct tegra_xusb_port *
 462tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
 463		     unsigned int index)
 464{
 465	struct tegra_xusb_port *port;
 466	struct device_node *np;
 467
 468	np = tegra_xusb_find_port_node(padctl, type, index);
 469	if (!np)
 470		return NULL;
 471
 472	list_for_each_entry(port, &padctl->ports, list) {
 473		if (np == port->dev.of_node) {
 474			of_node_put(np);
 475			return port;
 476		}
 477	}
 478
 479	of_node_put(np);
 480
 481	return NULL;
 482}
 483
 484struct tegra_xusb_usb2_port *
 485tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 486{
 487	struct tegra_xusb_port *port;
 488
 489	port = tegra_xusb_find_port(padctl, "usb2", index);
 490	if (port)
 491		return to_usb2_port(port);
 492
 493	return NULL;
 494}
 495
 496struct tegra_xusb_usb3_port *
 497tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 498{
 499	struct tegra_xusb_port *port;
 500
 501	port = tegra_xusb_find_port(padctl, "usb3", index);
 502	if (port)
 503		return to_usb3_port(port);
 504
 505	return NULL;
 506}
 507
 508static void tegra_xusb_port_release(struct device *dev)
 509{
 510	struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
 511
 512	if (port->ops->release)
 513		port->ops->release(port);
 514}
 515
 516static struct device_type tegra_xusb_port_type = {
 517	.release = tegra_xusb_port_release,
 518};
 519
 520static int tegra_xusb_port_init(struct tegra_xusb_port *port,
 521				struct tegra_xusb_padctl *padctl,
 522				struct device_node *np,
 523				const char *name,
 524				unsigned int index)
 525{
 526	int err;
 527
 528	INIT_LIST_HEAD(&port->list);
 529	port->padctl = padctl;
 530	port->index = index;
 531
 532	device_initialize(&port->dev);
 533	port->dev.type = &tegra_xusb_port_type;
 534	port->dev.of_node = of_node_get(np);
 535	port->dev.parent = padctl->dev;
 536
 537	err = dev_set_name(&port->dev, "%s-%u", name, index);
 538	if (err < 0)
 539		goto unregister;
 540
 541	err = device_add(&port->dev);
 542	if (err < 0)
 543		goto unregister;
 544
 545	return 0;
 546
 547unregister:
 548	device_unregister(&port->dev);
 549	return err;
 550}
 551
 552static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
 553{
 554	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
 555		of_platform_depopulate(&port->dev);
 556		usb_role_switch_unregister(port->usb_role_sw);
 557		cancel_work_sync(&port->usb_phy_work);
 558		usb_remove_phy(&port->usb_phy);
 559	}
 560
 561	if (port->ops->remove)
 562		port->ops->remove(port);
 563
 564	device_unregister(&port->dev);
 565}
 566
 567static const char *const modes[] = {
 568	[USB_DR_MODE_UNKNOWN] = "",
 569	[USB_DR_MODE_HOST] = "host",
 570	[USB_DR_MODE_PERIPHERAL] = "peripheral",
 571	[USB_DR_MODE_OTG] = "otg",
 572};
 573
 574static const char * const usb_roles[] = {
 575	[USB_ROLE_NONE]		= "none",
 576	[USB_ROLE_HOST]		= "host",
 577	[USB_ROLE_DEVICE]	= "device",
 578};
 579
 580static enum usb_phy_events to_usb_phy_event(enum usb_role role)
 581{
 582	switch (role) {
 583	case USB_ROLE_DEVICE:
 584		return USB_EVENT_VBUS;
 585
 586	case USB_ROLE_HOST:
 587		return USB_EVENT_ID;
 588
 589	default:
 590		return USB_EVENT_NONE;
 591	}
 592}
 593
 594static void tegra_xusb_usb_phy_work(struct work_struct *work)
 595{
 596	struct tegra_xusb_port *port = container_of(work,
 597						    struct tegra_xusb_port,
 598						    usb_phy_work);
 599	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
 600
 601	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
 602
 603	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
 604		usb_roles[role]);
 605
 606	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
 607}
 608
 609static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
 610				  enum usb_role role)
 611{
 612	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
 613
 614	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
 615
 616	schedule_work(&port->usb_phy_work);
 617
 618	return 0;
 619}
 620
 621static int tegra_xusb_set_peripheral(struct usb_otg *otg,
 622				     struct usb_gadget *gadget)
 623{
 624	struct tegra_xusb_port *port = container_of(otg->usb_phy,
 625						    struct tegra_xusb_port,
 626						    usb_phy);
 627
 628	if (gadget != NULL)
 629		schedule_work(&port->usb_phy_work);
 630
 631	return 0;
 632}
 633
 634static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
 635{
 636	struct tegra_xusb_port *port = container_of(otg->usb_phy,
 637						    struct tegra_xusb_port,
 638						    usb_phy);
 639
 640	if (host != NULL)
 641		schedule_work(&port->usb_phy_work);
 642
 643	return 0;
 644}
 645
 646
 647static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
 648{
 649	struct tegra_xusb_lane *lane;
 650	struct usb_role_switch_desc role_sx_desc = {
 651		.fwnode = dev_fwnode(&port->dev),
 652		.set = tegra_xusb_role_sw_set,
 653	};
 654	int err = 0;
 655
 656	/*
 657	 * USB role switch driver needs parent driver owner info. This is a
 658	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
 659	 * where an optimal solution is possible with changes to USB role
 660	 * switch driver.
 661	 */
 662	port->dev.driver = devm_kzalloc(&port->dev,
 663					sizeof(struct device_driver),
 664					GFP_KERNEL);
 665	port->dev.driver->owner	 = THIS_MODULE;
 666
 667	port->usb_role_sw = usb_role_switch_register(&port->dev,
 668						     &role_sx_desc);
 669	if (IS_ERR(port->usb_role_sw)) {
 670		err = PTR_ERR(port->usb_role_sw);
 671		dev_err(&port->dev, "failed to register USB role switch: %d",
 672			err);
 673		return err;
 674	}
 675
 676	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
 677	usb_role_switch_set_drvdata(port->usb_role_sw, port);
 678
 679	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
 680					 GFP_KERNEL);
 681	if (!port->usb_phy.otg)
 682		return -ENOMEM;
 683
 684	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
 685
 686	/*
 687	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
 688	 * reference to retrieve usb-phy details.
 689	 */
 690	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
 691	port->usb_phy.dev->driver = port->padctl->dev->driver;
 692	port->usb_phy.otg->usb_phy = &port->usb_phy;
 693	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
 694	port->usb_phy.otg->set_host = tegra_xusb_set_host;
 695
 696	err = usb_add_phy_dev(&port->usb_phy);
 697	if (err < 0) {
 698		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
 699		return err;
 700	}
 701
 702	/* populate connector entry */
 703	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
 704
 705	return err;
 706}
 707
 708static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
 709{
 710	struct tegra_xusb_port *port = &usb2->base;
 711	struct device_node *np = port->dev.of_node;
 712	const char *mode;
 713	int err;
 714
 715	usb2->internal = of_property_read_bool(np, "nvidia,internal");
 716
 717	if (!of_property_read_string(np, "mode", &mode)) {
 718		int err = match_string(modes, ARRAY_SIZE(modes), mode);
 719		if (err < 0) {
 720			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
 721				mode);
 722			usb2->mode = USB_DR_MODE_UNKNOWN;
 723		} else {
 724			usb2->mode = err;
 725		}
 726	} else {
 727		usb2->mode = USB_DR_MODE_HOST;
 728	}
 729
 730	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
 731	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
 732	    usb2->mode == USB_DR_MODE_OTG) {
 733		if (of_property_read_bool(np, "usb-role-switch")) {
 734			err = tegra_xusb_setup_usb_role_switch(port);
 735			if (err < 0)
 736				return err;
 737		} else {
 738			dev_err(&port->dev, "usb-role-switch not found for %s mode",
 739				modes[usb2->mode]);
 740			return -EINVAL;
 741		}
 742	}
 743
 744	usb2->supply = regulator_get(&port->dev, "vbus");
 745	return PTR_ERR_OR_ZERO(usb2->supply);
 746}
 747
 748static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
 749				    unsigned int index)
 750{
 751	struct tegra_xusb_usb2_port *usb2;
 752	struct device_node *np;
 753	int err = 0;
 754
 755	/*
 756	 * USB2 ports don't require additional properties, but if the port is
 757	 * marked as disabled there is no reason to register it.
 758	 */
 759	np = tegra_xusb_find_port_node(padctl, "usb2", index);
 760	if (!np || !of_device_is_available(np))
 761		goto out;
 762
 763	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
 764	if (!usb2) {
 765		err = -ENOMEM;
 766		goto out;
 767	}
 768
 769	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
 770	if (err < 0)
 771		goto out;
 772
 773	usb2->base.ops = padctl->soc->ports.usb2.ops;
 774
 775	usb2->base.lane = usb2->base.ops->map(&usb2->base);
 776	if (IS_ERR(usb2->base.lane)) {
 777		err = PTR_ERR(usb2->base.lane);
 778		goto out;
 779	}
 780
 781	err = tegra_xusb_usb2_port_parse_dt(usb2);
 782	if (err < 0) {
 783		tegra_xusb_port_unregister(&usb2->base);
 784		goto out;
 785	}
 786
 787	list_add_tail(&usb2->base.list, &padctl->ports);
 788
 789out:
 790	of_node_put(np);
 791	return err;
 792}
 793
 794void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
 795{
 796	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
 797
 798	kfree(usb2);
 799}
 800
 801void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
 802{
 803	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
 804
 805	regulator_put(usb2->supply);
 806}
 807
 808static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
 809{
 810	struct tegra_xusb_port *port = &ulpi->base;
 811	struct device_node *np = port->dev.of_node;
 812
 813	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
 814
 815	return 0;
 816}
 817
 818static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
 819				    unsigned int index)
 820{
 821	struct tegra_xusb_ulpi_port *ulpi;
 822	struct device_node *np;
 823	int err = 0;
 824
 825	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
 826	if (!np || !of_device_is_available(np))
 827		goto out;
 828
 829	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
 830	if (!ulpi) {
 831		err = -ENOMEM;
 832		goto out;
 833	}
 834
 835	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
 836	if (err < 0)
 837		goto out;
 838
 839	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
 840
 841	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
 842	if (IS_ERR(ulpi->base.lane)) {
 843		err = PTR_ERR(ulpi->base.lane);
 844		goto out;
 845	}
 846
 847	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
 848	if (err < 0) {
 849		tegra_xusb_port_unregister(&ulpi->base);
 850		goto out;
 851	}
 852
 853	list_add_tail(&ulpi->base.list, &padctl->ports);
 854
 855out:
 856	of_node_put(np);
 857	return err;
 858}
 859
 860void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
 861{
 862	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
 863
 864	kfree(ulpi);
 865}
 866
 867static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
 868{
 869	/* XXX */
 870	return 0;
 871}
 872
 873static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
 874				    unsigned int index)
 875{
 876	struct tegra_xusb_hsic_port *hsic;
 877	struct device_node *np;
 878	int err = 0;
 879
 880	np = tegra_xusb_find_port_node(padctl, "hsic", index);
 881	if (!np || !of_device_is_available(np))
 882		goto out;
 883
 884	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
 885	if (!hsic) {
 886		err = -ENOMEM;
 887		goto out;
 888	}
 889
 890	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
 891	if (err < 0)
 892		goto out;
 893
 894	hsic->base.ops = padctl->soc->ports.hsic.ops;
 895
 896	hsic->base.lane = hsic->base.ops->map(&hsic->base);
 897	if (IS_ERR(hsic->base.lane)) {
 898		err = PTR_ERR(hsic->base.lane);
 899		goto out;
 900	}
 901
 902	err = tegra_xusb_hsic_port_parse_dt(hsic);
 903	if (err < 0) {
 904		tegra_xusb_port_unregister(&hsic->base);
 905		goto out;
 906	}
 907
 908	list_add_tail(&hsic->base.list, &padctl->ports);
 909
 910out:
 911	of_node_put(np);
 912	return err;
 913}
 914
 915void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
 916{
 917	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
 918
 919	kfree(hsic);
 920}
 921
 922static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
 923{
 924	struct tegra_xusb_port *port = &usb3->base;
 925	struct device_node *np = port->dev.of_node;
 926	enum usb_device_speed maximum_speed;
 927	u32 value;
 928	int err;
 929
 930	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
 931	if (err < 0) {
 932		dev_err(&port->dev, "failed to read port: %d\n", err);
 933		return err;
 934	}
 935
 936	usb3->port = value;
 937
 938	usb3->internal = of_property_read_bool(np, "nvidia,internal");
 939
 940	if (device_property_present(&port->dev, "maximum-speed")) {
 941		maximum_speed =  usb_get_maximum_speed(&port->dev);
 942		if (maximum_speed == USB_SPEED_SUPER)
 943			usb3->disable_gen2 = true;
 944		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
 945			usb3->disable_gen2 = false;
 946		else
 947			return -EINVAL;
 948	}
 949
 950	usb3->supply = regulator_get(&port->dev, "vbus");
 951	return PTR_ERR_OR_ZERO(usb3->supply);
 952}
 953
 954static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
 955				    unsigned int index)
 956{
 957	struct tegra_xusb_usb3_port *usb3;
 958	struct device_node *np;
 959	int err = 0;
 960
 961	/*
 962	 * If there is no supplemental configuration in the device tree the
 963	 * port is unusable. But it is valid to configure only a single port,
 964	 * hence return 0 instead of an error to allow ports to be optional.
 965	 */
 966	np = tegra_xusb_find_port_node(padctl, "usb3", index);
 967	if (!np || !of_device_is_available(np))
 968		goto out;
 969
 970	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
 971	if (!usb3) {
 972		err = -ENOMEM;
 973		goto out;
 974	}
 975
 976	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
 977	if (err < 0)
 978		goto out;
 979
 980	usb3->base.ops = padctl->soc->ports.usb3.ops;
 981
 982	usb3->base.lane = usb3->base.ops->map(&usb3->base);
 983	if (IS_ERR(usb3->base.lane)) {
 984		err = PTR_ERR(usb3->base.lane);
 985		goto out;
 986	}
 987
 988	err = tegra_xusb_usb3_port_parse_dt(usb3);
 989	if (err < 0) {
 990		tegra_xusb_port_unregister(&usb3->base);
 991		goto out;
 992	}
 993
 994	list_add_tail(&usb3->base.list, &padctl->ports);
 995
 996out:
 997	of_node_put(np);
 998	return err;
 999}
1000
1001void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1002{
1003	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1004
1005	kfree(usb3);
1006}
1007
1008void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1009{
1010	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1011
1012	regulator_put(usb3->supply);
1013}
1014
1015static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1016{
1017	struct tegra_xusb_port *port, *tmp;
1018
1019	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1020		list_del(&port->list);
1021		tegra_xusb_port_unregister(port);
1022	}
1023}
1024
1025static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1026{
1027	struct device_node *np;
1028	unsigned int i;
1029
1030	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1031		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1032		if (!np || !of_device_is_available(np))
1033			return i;
1034	}
1035
1036	return -ENODEV;
1037}
1038
1039static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1040{
1041	unsigned int i;
1042	struct tegra_xusb_usb3_port *usb3;
1043	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1044
1045	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1046		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1047		if (usb3 && usb3->port == usb2->base.index)
1048			return true;
1049	}
1050
1051	return false;
1052}
1053
1054static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1055{
1056	int fake;
1057
1058	/* Disable usb3_port_fake usage by default and assign if needed */
1059	usb2->usb3_port_fake = -1;
1060
1061	if ((usb2->mode == USB_DR_MODE_OTG ||
1062	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1063		!tegra_xusb_port_is_companion(usb2)) {
1064		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1065		if (fake < 0) {
1066			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1067			return -ENODEV;
1068		}
1069
1070		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1071		usb2->usb3_port_fake = fake;
1072	}
1073
1074	return 0;
1075}
1076
1077static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1078{
1079	struct tegra_xusb_port *port;
1080	struct tegra_xusb_usb2_port *usb2;
1081	unsigned int i;
1082	int err = 0;
1083
1084	mutex_lock(&padctl->lock);
1085
1086	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1087		err = tegra_xusb_add_usb2_port(padctl, i);
1088		if (err < 0)
1089			goto remove_ports;
1090	}
1091
1092	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1093		err = tegra_xusb_add_ulpi_port(padctl, i);
1094		if (err < 0)
1095			goto remove_ports;
1096	}
1097
1098	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1099		err = tegra_xusb_add_hsic_port(padctl, i);
1100		if (err < 0)
1101			goto remove_ports;
1102	}
1103
1104	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1105		err = tegra_xusb_add_usb3_port(padctl, i);
1106		if (err < 0)
1107			goto remove_ports;
1108	}
1109
1110	if (padctl->soc->need_fake_usb3_port) {
1111		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1112			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1113			if (!usb2)
1114				continue;
1115
1116			err = tegra_xusb_update_usb3_fake_port(usb2);
1117			if (err < 0)
1118				goto remove_ports;
1119		}
1120	}
1121
1122	list_for_each_entry(port, &padctl->ports, list) {
1123		err = port->ops->enable(port);
1124		if (err < 0)
1125			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1126				dev_name(&port->dev), err);
1127	}
1128
1129	goto unlock;
1130
1131remove_ports:
1132	__tegra_xusb_remove_ports(padctl);
1133unlock:
1134	mutex_unlock(&padctl->lock);
1135	return err;
1136}
1137
1138static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1139{
1140	mutex_lock(&padctl->lock);
1141	__tegra_xusb_remove_ports(padctl);
1142	mutex_unlock(&padctl->lock);
1143}
1144
1145static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1146{
1147	struct device_node *np = pdev->dev.of_node;
1148	const struct tegra_xusb_padctl_soc *soc;
1149	struct tegra_xusb_padctl *padctl;
1150	const struct of_device_id *match;
1151	struct resource *res;
1152	int err;
1153
1154	/* for backwards compatibility with old device trees */
1155	np = of_get_child_by_name(np, "pads");
1156	if (!np) {
1157		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1158		return tegra_xusb_padctl_legacy_probe(pdev);
1159	}
1160
1161	of_node_put(np);
1162
1163	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1164	soc = match->data;
1165
1166	padctl = soc->ops->probe(&pdev->dev, soc);
1167	if (IS_ERR(padctl))
1168		return PTR_ERR(padctl);
1169
1170	platform_set_drvdata(pdev, padctl);
1171	INIT_LIST_HEAD(&padctl->ports);
1172	INIT_LIST_HEAD(&padctl->lanes);
1173	INIT_LIST_HEAD(&padctl->pads);
1174	mutex_init(&padctl->lock);
1175
1176	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1177	padctl->regs = devm_ioremap_resource(&pdev->dev, res);
1178	if (IS_ERR(padctl->regs)) {
1179		err = PTR_ERR(padctl->regs);
1180		goto remove;
1181	}
1182
1183	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1184	if (IS_ERR(padctl->rst)) {
1185		err = PTR_ERR(padctl->rst);
1186		goto remove;
1187	}
1188
1189	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1190					sizeof(*padctl->supplies), GFP_KERNEL);
1191	if (!padctl->supplies) {
1192		err = -ENOMEM;
1193		goto remove;
1194	}
1195
1196	regulator_bulk_set_supply_names(padctl->supplies,
1197					padctl->soc->supply_names,
1198					padctl->soc->num_supplies);
1199
1200	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1201				      padctl->supplies);
1202	if (err < 0) {
1203		dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1204		goto remove;
1205	}
1206
1207	err = reset_control_deassert(padctl->rst);
1208	if (err < 0)
1209		goto remove;
1210
1211	err = regulator_bulk_enable(padctl->soc->num_supplies,
1212				    padctl->supplies);
1213	if (err < 0) {
1214		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1215		goto reset;
1216	}
1217
1218	err = tegra_xusb_setup_pads(padctl);
1219	if (err < 0) {
1220		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1221		goto power_down;
1222	}
1223
1224	err = tegra_xusb_setup_ports(padctl);
1225	if (err) {
1226		const char *level = KERN_ERR;
1227
1228		if (err == -EPROBE_DEFER)
1229			level = KERN_DEBUG;
1230
1231		dev_printk(level, &pdev->dev,
1232			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1233		goto remove_pads;
1234	}
1235
1236	return 0;
1237
1238remove_pads:
1239	tegra_xusb_remove_pads(padctl);
1240power_down:
1241	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1242reset:
1243	reset_control_assert(padctl->rst);
1244remove:
 
1245	soc->ops->remove(padctl);
1246	return err;
1247}
1248
1249static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1250{
1251	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1252	int err;
1253
1254	tegra_xusb_remove_ports(padctl);
1255	tegra_xusb_remove_pads(padctl);
1256
1257	err = regulator_bulk_disable(padctl->soc->num_supplies,
1258				     padctl->supplies);
1259	if (err < 0)
1260		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1261
1262	err = reset_control_assert(padctl->rst);
1263	if (err < 0)
1264		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1265
1266	padctl->soc->ops->remove(padctl);
1267
1268	return err;
1269}
1270
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1271static struct platform_driver tegra_xusb_padctl_driver = {
1272	.driver = {
1273		.name = "tegra-xusb-padctl",
1274		.of_match_table = tegra_xusb_padctl_of_match,
 
1275	},
1276	.probe = tegra_xusb_padctl_probe,
1277	.remove = tegra_xusb_padctl_remove,
1278};
1279module_platform_driver(tegra_xusb_padctl_driver);
1280
1281struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1282{
1283	struct tegra_xusb_padctl *padctl;
1284	struct platform_device *pdev;
1285	struct device_node *np;
1286
1287	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1288	if (!np)
1289		return ERR_PTR(-EINVAL);
1290
1291	/*
1292	 * This is slightly ugly. A better implementation would be to keep a
1293	 * registry of pad controllers, but since there will almost certainly
1294	 * only ever be one per SoC that would be a little overkill.
1295	 */
1296	pdev = of_find_device_by_node(np);
1297	if (!pdev) {
1298		of_node_put(np);
1299		return ERR_PTR(-ENODEV);
1300	}
1301
1302	of_node_put(np);
1303
1304	padctl = platform_get_drvdata(pdev);
1305	if (!padctl) {
1306		put_device(&pdev->dev);
1307		return ERR_PTR(-EPROBE_DEFER);
1308	}
1309
1310	return padctl;
1311}
1312EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1313
1314void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1315{
1316	if (padctl)
1317		put_device(padctl->dev);
1318}
1319EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1320
1321int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1322					unsigned int port)
1323{
1324	if (padctl->soc->ops->usb3_save_context)
1325		return padctl->soc->ops->usb3_save_context(padctl, port);
1326
1327	return -ENOSYS;
1328}
1329EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1330
1331int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1332				    unsigned int port, bool idle)
1333{
1334	if (padctl->soc->ops->hsic_set_idle)
1335		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1336
1337	return -ENOSYS;
1338}
1339EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1340
1341int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1342					   unsigned int port, bool enable)
1343{
1344	if (padctl->soc->ops->usb3_set_lfps_detect)
1345		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1346							      enable);
1347
1348	return -ENOSYS;
1349}
1350EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1351
1352int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1353							bool val)
1354{
1355	if (padctl->soc->ops->vbus_override)
1356		return padctl->soc->ops->vbus_override(padctl, val);
1357
1358	return -ENOTSUPP;
1359}
1360EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1361
1362int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1363{
1364	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1365	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1366
1367	if (padctl->soc->ops->utmi_port_reset)
1368		return padctl->soc->ops->utmi_port_reset(phy);
1369
1370	return -ENOTSUPP;
1371}
1372EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1373
1374int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1375				    unsigned int port)
1376{
1377	struct tegra_xusb_usb2_port *usb2;
1378	struct tegra_xusb_usb3_port *usb3;
1379	int i;
1380
1381	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1382	if (!usb2)
1383		return -EINVAL;
1384
1385	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1386		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1387		if (usb3 && usb3->port == usb2->base.index)
1388			return usb3->base.index;
1389	}
1390
1391	return -ENODEV;
1392}
1393EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1394
1395MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1396MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1397MODULE_LICENSE("GPL v2");
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * Copyright (c) 2014-2020, NVIDIA CORPORATION.  All rights reserved.
   4 */
   5
   6#include <linux/delay.h>
   7#include <linux/io.h>
   8#include <linux/mailbox_client.h>
   9#include <linux/module.h>
  10#include <linux/of.h>
  11#include <linux/of_device.h>
  12#include <linux/phy/phy.h>
  13#include <linux/phy/tegra/xusb.h>
  14#include <linux/platform_device.h>
  15#include <linux/regulator/consumer.h>
  16#include <linux/reset.h>
  17#include <linux/slab.h>
  18#include <linux/workqueue.h>
  19
  20#include <soc/tegra/fuse.h>
  21
  22#include "xusb.h"
  23
  24static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
  25					   struct of_phandle_args *args)
  26{
  27	struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
  28	struct phy *phy = NULL;
  29	unsigned int i;
  30
  31	if (args->args_count != 0)
  32		return ERR_PTR(-EINVAL);
  33
  34	for (i = 0; i < pad->soc->num_lanes; i++) {
  35		if (!pad->lanes[i])
  36			continue;
  37
  38		if (pad->lanes[i]->dev.of_node == args->np) {
  39			phy = pad->lanes[i];
  40			break;
  41		}
  42	}
  43
  44	if (phy == NULL)
  45		phy = ERR_PTR(-ENODEV);
  46
  47	return phy;
  48}
  49
  50static const struct of_device_id tegra_xusb_padctl_of_match[] = {
  51#if defined(CONFIG_ARCH_TEGRA_124_SOC) || defined(CONFIG_ARCH_TEGRA_132_SOC)
  52	{
  53		.compatible = "nvidia,tegra124-xusb-padctl",
  54		.data = &tegra124_xusb_padctl_soc,
  55	},
  56#endif
  57#if defined(CONFIG_ARCH_TEGRA_210_SOC)
  58	{
  59		.compatible = "nvidia,tegra210-xusb-padctl",
  60		.data = &tegra210_xusb_padctl_soc,
  61	},
  62#endif
  63#if defined(CONFIG_ARCH_TEGRA_186_SOC)
  64	{
  65		.compatible = "nvidia,tegra186-xusb-padctl",
  66		.data = &tegra186_xusb_padctl_soc,
  67	},
  68#endif
  69#if defined(CONFIG_ARCH_TEGRA_194_SOC)
  70	{
  71		.compatible = "nvidia,tegra194-xusb-padctl",
  72		.data = &tegra194_xusb_padctl_soc,
  73	},
  74#endif
  75	{ }
  76};
  77MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  78
  79static struct device_node *
  80tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  81{
  82	struct device_node *pads, *np;
  83
  84	pads = of_get_child_by_name(padctl->dev->of_node, "pads");
  85	if (!pads)
  86		return NULL;
  87
  88	np = of_get_child_by_name(pads, name);
  89	of_node_put(pads);
  90
  91	return np;
  92}
  93
  94static struct device_node *
  95tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
  96{
  97	struct device_node *np, *lanes;
  98
  99	lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
 100	if (!lanes)
 101		return NULL;
 102
 103	np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
 104	of_node_put(lanes);
 105
 106	return np;
 107}
 108
 109int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
 110			     struct device_node *np)
 111{
 112	struct device *dev = &lane->pad->dev;
 113	const char *function;
 114	int err;
 115
 116	err = of_property_read_string(np, "nvidia,function", &function);
 117	if (err < 0)
 118		return err;
 119
 120	err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
 121	if (err < 0) {
 122		dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
 123			function, np);
 124		return err;
 125	}
 126
 127	lane->function = err;
 128
 129	return 0;
 130}
 131
 132static void tegra_xusb_lane_destroy(struct phy *phy)
 133{
 134	if (phy) {
 135		struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
 136
 137		lane->pad->ops->remove(lane);
 138		phy_destroy(phy);
 139	}
 140}
 141
 142static void tegra_xusb_pad_release(struct device *dev)
 143{
 144	struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
 145
 146	pad->soc->ops->remove(pad);
 147}
 148
 149static const struct device_type tegra_xusb_pad_type = {
 150	.release = tegra_xusb_pad_release,
 151};
 152
 153int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
 154			struct tegra_xusb_padctl *padctl,
 155			struct device_node *np)
 156{
 157	int err;
 158
 159	device_initialize(&pad->dev);
 160	INIT_LIST_HEAD(&pad->list);
 161	pad->dev.parent = padctl->dev;
 162	pad->dev.type = &tegra_xusb_pad_type;
 163	pad->dev.of_node = np;
 164	pad->padctl = padctl;
 165
 166	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
 167	if (err < 0)
 168		goto unregister;
 169
 170	err = device_add(&pad->dev);
 171	if (err < 0)
 172		goto unregister;
 173
 174	return 0;
 175
 176unregister:
 177	device_unregister(&pad->dev);
 178	return err;
 179}
 180
 181int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
 182			    const struct phy_ops *ops)
 183{
 184	struct device_node *children;
 185	struct phy *lane;
 186	unsigned int i;
 187	int err;
 188
 189	children = of_get_child_by_name(pad->dev.of_node, "lanes");
 190	if (!children)
 191		return -ENODEV;
 192
 193	pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
 194				  GFP_KERNEL);
 195	if (!pad->lanes) {
 196		of_node_put(children);
 197		return -ENOMEM;
 198	}
 199
 200	for (i = 0; i < pad->soc->num_lanes; i++) {
 201		struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
 202		struct tegra_xusb_lane *lane;
 203
 204		/* skip disabled lanes */
 205		if (!np || !of_device_is_available(np)) {
 206			of_node_put(np);
 207			continue;
 208		}
 209
 210		pad->lanes[i] = phy_create(&pad->dev, np, ops);
 211		if (IS_ERR(pad->lanes[i])) {
 212			err = PTR_ERR(pad->lanes[i]);
 213			of_node_put(np);
 214			goto remove;
 215		}
 216
 217		lane = pad->ops->probe(pad, np, i);
 218		if (IS_ERR(lane)) {
 219			phy_destroy(pad->lanes[i]);
 220			err = PTR_ERR(lane);
 221			goto remove;
 222		}
 223
 224		list_add_tail(&lane->list, &pad->padctl->lanes);
 225		phy_set_drvdata(pad->lanes[i], lane);
 226	}
 227
 228	pad->provider = of_phy_provider_register_full(&pad->dev, children,
 229						      tegra_xusb_pad_of_xlate);
 230	if (IS_ERR(pad->provider)) {
 231		err = PTR_ERR(pad->provider);
 232		goto remove;
 233	}
 234
 235	return 0;
 236
 237remove:
 238	while (i--)
 239		tegra_xusb_lane_destroy(pad->lanes[i]);
 240
 241	of_node_put(children);
 242
 243	return err;
 244}
 245
 246void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
 247{
 248	unsigned int i = pad->soc->num_lanes;
 249
 250	of_phy_provider_unregister(pad->provider);
 251
 252	while (i--)
 253		tegra_xusb_lane_destroy(pad->lanes[i]);
 254
 255	device_unregister(&pad->dev);
 256}
 257
 258static struct tegra_xusb_pad *
 259tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
 260		      const struct tegra_xusb_pad_soc *soc)
 261{
 262	struct tegra_xusb_pad *pad;
 263	struct device_node *np;
 264	int err;
 265
 266	np = tegra_xusb_find_pad_node(padctl, soc->name);
 267	if (!np || !of_device_is_available(np))
 268		return NULL;
 269
 270	pad = soc->ops->probe(padctl, soc, np);
 271	if (IS_ERR(pad)) {
 272		err = PTR_ERR(pad);
 273		dev_err(padctl->dev, "failed to create pad %s: %d\n",
 274			soc->name, err);
 275		return ERR_PTR(err);
 276	}
 277
 278	/* XXX move this into ->probe() to avoid string comparison */
 279	if (strcmp(soc->name, "pcie") == 0)
 280		padctl->pcie = pad;
 281
 282	if (strcmp(soc->name, "sata") == 0)
 283		padctl->sata = pad;
 284
 285	if (strcmp(soc->name, "usb2") == 0)
 286		padctl->usb2 = pad;
 287
 288	if (strcmp(soc->name, "ulpi") == 0)
 289		padctl->ulpi = pad;
 290
 291	if (strcmp(soc->name, "hsic") == 0)
 292		padctl->hsic = pad;
 293
 294	return pad;
 295}
 296
 297static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 298{
 299	struct tegra_xusb_pad *pad, *tmp;
 300
 301	list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
 302		list_del(&pad->list);
 303		tegra_xusb_pad_unregister(pad);
 304	}
 305}
 306
 307static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 308{
 309	mutex_lock(&padctl->lock);
 310	__tegra_xusb_remove_pads(padctl);
 311	mutex_unlock(&padctl->lock);
 312}
 313
 314static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
 315{
 316	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
 317	const struct tegra_xusb_lane_soc *soc = lane->soc;
 318	u32 value;
 319
 320	/* skip single function lanes */
 321	if (soc->num_funcs < 2)
 322		return;
 323
 324	if (lane->pad->ops->iddq_enable)
 325		lane->pad->ops->iddq_enable(lane);
 326
 327	/* choose function */
 328	value = padctl_readl(padctl, soc->offset);
 329	value &= ~(soc->mask << soc->shift);
 330	value |= lane->function << soc->shift;
 331	padctl_writel(padctl, value, soc->offset);
 332
 333	if (lane->pad->ops->iddq_disable)
 334		lane->pad->ops->iddq_disable(lane);
 335}
 336
 337static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
 338{
 339	unsigned int i;
 340
 341	for (i = 0; i < pad->soc->num_lanes; i++) {
 342		struct tegra_xusb_lane *lane;
 343
 344		if (pad->lanes[i]) {
 345			lane = phy_get_drvdata(pad->lanes[i]);
 346			tegra_xusb_lane_program(lane);
 347		}
 348	}
 349}
 350
 351static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
 352{
 353	struct tegra_xusb_pad *pad;
 354	unsigned int i;
 355
 356	mutex_lock(&padctl->lock);
 357
 358	for (i = 0; i < padctl->soc->num_pads; i++) {
 359		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
 360		int err;
 361
 362		pad = tegra_xusb_pad_create(padctl, soc);
 363		if (IS_ERR(pad)) {
 364			err = PTR_ERR(pad);
 365			dev_err(padctl->dev, "failed to create pad %s: %d\n",
 366				soc->name, err);
 367			__tegra_xusb_remove_pads(padctl);
 368			mutex_unlock(&padctl->lock);
 369			return err;
 370		}
 371
 372		if (!pad)
 373			continue;
 374
 375		list_add_tail(&pad->list, &padctl->pads);
 376	}
 377
 378	list_for_each_entry(pad, &padctl->pads, list)
 379		tegra_xusb_pad_program(pad);
 380
 381	mutex_unlock(&padctl->lock);
 382	return 0;
 383}
 384
 385bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
 386				  const char *function)
 387{
 388	const char *func = lane->soc->funcs[lane->function];
 389
 390	return strcmp(function, func) == 0;
 391}
 392
 393struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
 394					     const char *type,
 395					     unsigned int index)
 396{
 397	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
 398	char *name;
 399
 400	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 401	if (!name)
 402		return ERR_PTR(-ENOMEM);
 403
 404	list_for_each_entry(lane, &padctl->lanes, list) {
 405		if (strcmp(lane->soc->name, name) == 0) {
 406			hit = lane;
 407			break;
 408		}
 409	}
 410
 411	kfree(name);
 412	return hit;
 413}
 414
 415struct tegra_xusb_lane *
 416tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
 417			  const struct tegra_xusb_lane_map *map,
 418			  const char *function)
 419{
 420	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
 421
 422	for (; map->type; map++) {
 423		if (port->index != map->port)
 424			continue;
 425
 426		lane = tegra_xusb_find_lane(port->padctl, map->type,
 427					    map->index);
 428		if (IS_ERR(lane))
 429			continue;
 430
 431		if (!tegra_xusb_lane_check(lane, function))
 432			continue;
 433
 434		if (!IS_ERR(match))
 435			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
 436				map->type, map->index, match->soc->name);
 437		else
 438			match = lane;
 439	}
 440
 441	return match;
 442}
 443
 444static struct device_node *
 445tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
 446			  unsigned int index)
 447{
 448	struct device_node *ports, *np;
 449	char *name;
 450
 451	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
 452	if (!ports)
 453		return NULL;
 454
 455	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 456	if (!name) {
 457		of_node_put(ports);
 458		return ERR_PTR(-ENOMEM);
 459	}
 460	np = of_get_child_by_name(ports, name);
 461	kfree(name);
 462	of_node_put(ports);
 463
 464	return np;
 465}
 466
 467struct tegra_xusb_port *
 468tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
 469		     unsigned int index)
 470{
 471	struct tegra_xusb_port *port;
 472	struct device_node *np;
 473
 474	np = tegra_xusb_find_port_node(padctl, type, index);
 475	if (!np)
 476		return NULL;
 477
 478	list_for_each_entry(port, &padctl->ports, list) {
 479		if (np == port->dev.of_node) {
 480			of_node_put(np);
 481			return port;
 482		}
 483	}
 484
 485	of_node_put(np);
 486
 487	return NULL;
 488}
 489
 490struct tegra_xusb_usb2_port *
 491tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 492{
 493	struct tegra_xusb_port *port;
 494
 495	port = tegra_xusb_find_port(padctl, "usb2", index);
 496	if (port)
 497		return to_usb2_port(port);
 498
 499	return NULL;
 500}
 501
 502struct tegra_xusb_usb3_port *
 503tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 504{
 505	struct tegra_xusb_port *port;
 506
 507	port = tegra_xusb_find_port(padctl, "usb3", index);
 508	if (port)
 509		return to_usb3_port(port);
 510
 511	return NULL;
 512}
 513
 514static void tegra_xusb_port_release(struct device *dev)
 515{
 516	struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
 517
 518	if (port->ops->release)
 519		port->ops->release(port);
 520}
 521
 522static const struct device_type tegra_xusb_port_type = {
 523	.release = tegra_xusb_port_release,
 524};
 525
 526static int tegra_xusb_port_init(struct tegra_xusb_port *port,
 527				struct tegra_xusb_padctl *padctl,
 528				struct device_node *np,
 529				const char *name,
 530				unsigned int index)
 531{
 532	int err;
 533
 534	INIT_LIST_HEAD(&port->list);
 535	port->padctl = padctl;
 536	port->index = index;
 537
 538	device_initialize(&port->dev);
 539	port->dev.type = &tegra_xusb_port_type;
 540	port->dev.of_node = of_node_get(np);
 541	port->dev.parent = padctl->dev;
 542
 543	err = dev_set_name(&port->dev, "%s-%u", name, index);
 544	if (err < 0)
 545		goto unregister;
 546
 547	err = device_add(&port->dev);
 548	if (err < 0)
 549		goto unregister;
 550
 551	return 0;
 552
 553unregister:
 554	device_unregister(&port->dev);
 555	return err;
 556}
 557
 558static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
 559{
 560	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
 561		of_platform_depopulate(&port->dev);
 562		usb_role_switch_unregister(port->usb_role_sw);
 563		cancel_work_sync(&port->usb_phy_work);
 564		usb_remove_phy(&port->usb_phy);
 565	}
 566
 567	if (port->ops->remove)
 568		port->ops->remove(port);
 569
 570	device_unregister(&port->dev);
 571}
 572
 573static const char *const modes[] = {
 574	[USB_DR_MODE_UNKNOWN] = "",
 575	[USB_DR_MODE_HOST] = "host",
 576	[USB_DR_MODE_PERIPHERAL] = "peripheral",
 577	[USB_DR_MODE_OTG] = "otg",
 578};
 579
 580static const char * const usb_roles[] = {
 581	[USB_ROLE_NONE]		= "none",
 582	[USB_ROLE_HOST]		= "host",
 583	[USB_ROLE_DEVICE]	= "device",
 584};
 585
 586static enum usb_phy_events to_usb_phy_event(enum usb_role role)
 587{
 588	switch (role) {
 589	case USB_ROLE_DEVICE:
 590		return USB_EVENT_VBUS;
 591
 592	case USB_ROLE_HOST:
 593		return USB_EVENT_ID;
 594
 595	default:
 596		return USB_EVENT_NONE;
 597	}
 598}
 599
 600static void tegra_xusb_usb_phy_work(struct work_struct *work)
 601{
 602	struct tegra_xusb_port *port = container_of(work,
 603						    struct tegra_xusb_port,
 604						    usb_phy_work);
 605	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
 606
 607	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
 608
 609	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
 610		usb_roles[role]);
 611
 612	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
 613}
 614
 615static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
 616				  enum usb_role role)
 617{
 618	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
 619
 620	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
 621
 622	schedule_work(&port->usb_phy_work);
 623
 624	return 0;
 625}
 626
 627static int tegra_xusb_set_peripheral(struct usb_otg *otg,
 628				     struct usb_gadget *gadget)
 629{
 630	struct tegra_xusb_port *port = container_of(otg->usb_phy,
 631						    struct tegra_xusb_port,
 632						    usb_phy);
 633
 634	if (gadget != NULL)
 635		schedule_work(&port->usb_phy_work);
 636
 637	return 0;
 638}
 639
 640static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
 641{
 642	struct tegra_xusb_port *port = container_of(otg->usb_phy,
 643						    struct tegra_xusb_port,
 644						    usb_phy);
 645
 646	if (host != NULL)
 647		schedule_work(&port->usb_phy_work);
 648
 649	return 0;
 650}
 651
 652
 653static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
 654{
 655	struct tegra_xusb_lane *lane;
 656	struct usb_role_switch_desc role_sx_desc = {
 657		.fwnode = dev_fwnode(&port->dev),
 658		.set = tegra_xusb_role_sw_set,
 659	};
 660	int err = 0;
 661
 662	/*
 663	 * USB role switch driver needs parent driver owner info. This is a
 664	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
 665	 * where an optimal solution is possible with changes to USB role
 666	 * switch driver.
 667	 */
 668	port->dev.driver = devm_kzalloc(&port->dev,
 669					sizeof(struct device_driver),
 670					GFP_KERNEL);
 671	port->dev.driver->owner	 = THIS_MODULE;
 672
 673	port->usb_role_sw = usb_role_switch_register(&port->dev,
 674						     &role_sx_desc);
 675	if (IS_ERR(port->usb_role_sw)) {
 676		err = PTR_ERR(port->usb_role_sw);
 677		dev_err(&port->dev, "failed to register USB role switch: %d",
 678			err);
 679		return err;
 680	}
 681
 682	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
 683	usb_role_switch_set_drvdata(port->usb_role_sw, port);
 684
 685	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
 686					 GFP_KERNEL);
 687	if (!port->usb_phy.otg)
 688		return -ENOMEM;
 689
 690	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
 691
 692	/*
 693	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
 694	 * reference to retrieve usb-phy details.
 695	 */
 696	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
 697	port->usb_phy.dev->driver = port->dev.driver;
 698	port->usb_phy.otg->usb_phy = &port->usb_phy;
 699	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
 700	port->usb_phy.otg->set_host = tegra_xusb_set_host;
 701
 702	err = usb_add_phy_dev(&port->usb_phy);
 703	if (err < 0) {
 704		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
 705		return err;
 706	}
 707
 708	/* populate connector entry */
 709	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
 710
 711	return err;
 712}
 713
 714static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
 715{
 716	struct tegra_xusb_port *port = &usb2->base;
 717	struct device_node *np = port->dev.of_node;
 718	const char *mode;
 719	int err;
 720
 721	usb2->internal = of_property_read_bool(np, "nvidia,internal");
 722
 723	if (!of_property_read_string(np, "mode", &mode)) {
 724		int err = match_string(modes, ARRAY_SIZE(modes), mode);
 725		if (err < 0) {
 726			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
 727				mode);
 728			usb2->mode = USB_DR_MODE_UNKNOWN;
 729		} else {
 730			usb2->mode = err;
 731		}
 732	} else {
 733		usb2->mode = USB_DR_MODE_HOST;
 734	}
 735
 736	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
 737	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
 738	    usb2->mode == USB_DR_MODE_OTG) {
 739		if (of_property_read_bool(np, "usb-role-switch")) {
 740			err = tegra_xusb_setup_usb_role_switch(port);
 741			if (err < 0)
 742				return err;
 743		} else {
 744			dev_err(&port->dev, "usb-role-switch not found for %s mode",
 745				modes[usb2->mode]);
 746			return -EINVAL;
 747		}
 748	}
 749
 750	usb2->supply = regulator_get(&port->dev, "vbus");
 751	return PTR_ERR_OR_ZERO(usb2->supply);
 752}
 753
 754static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
 755				    unsigned int index)
 756{
 757	struct tegra_xusb_usb2_port *usb2;
 758	struct device_node *np;
 759	int err = 0;
 760
 761	/*
 762	 * USB2 ports don't require additional properties, but if the port is
 763	 * marked as disabled there is no reason to register it.
 764	 */
 765	np = tegra_xusb_find_port_node(padctl, "usb2", index);
 766	if (!np || !of_device_is_available(np))
 767		goto out;
 768
 769	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
 770	if (!usb2) {
 771		err = -ENOMEM;
 772		goto out;
 773	}
 774
 775	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
 776	if (err < 0)
 777		goto out;
 778
 779	usb2->base.ops = padctl->soc->ports.usb2.ops;
 780
 781	usb2->base.lane = usb2->base.ops->map(&usb2->base);
 782	if (IS_ERR(usb2->base.lane)) {
 783		err = PTR_ERR(usb2->base.lane);
 784		goto out;
 785	}
 786
 787	err = tegra_xusb_usb2_port_parse_dt(usb2);
 788	if (err < 0) {
 789		tegra_xusb_port_unregister(&usb2->base);
 790		goto out;
 791	}
 792
 793	list_add_tail(&usb2->base.list, &padctl->ports);
 794
 795out:
 796	of_node_put(np);
 797	return err;
 798}
 799
 800void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
 801{
 802	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
 803
 804	kfree(usb2);
 805}
 806
 807void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
 808{
 809	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
 810
 811	regulator_put(usb2->supply);
 812}
 813
 814static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
 815{
 816	struct tegra_xusb_port *port = &ulpi->base;
 817	struct device_node *np = port->dev.of_node;
 818
 819	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
 820
 821	return 0;
 822}
 823
 824static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
 825				    unsigned int index)
 826{
 827	struct tegra_xusb_ulpi_port *ulpi;
 828	struct device_node *np;
 829	int err = 0;
 830
 831	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
 832	if (!np || !of_device_is_available(np))
 833		goto out;
 834
 835	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
 836	if (!ulpi) {
 837		err = -ENOMEM;
 838		goto out;
 839	}
 840
 841	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
 842	if (err < 0)
 843		goto out;
 844
 845	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
 846
 847	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
 848	if (IS_ERR(ulpi->base.lane)) {
 849		err = PTR_ERR(ulpi->base.lane);
 850		goto out;
 851	}
 852
 853	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
 854	if (err < 0) {
 855		tegra_xusb_port_unregister(&ulpi->base);
 856		goto out;
 857	}
 858
 859	list_add_tail(&ulpi->base.list, &padctl->ports);
 860
 861out:
 862	of_node_put(np);
 863	return err;
 864}
 865
 866void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
 867{
 868	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
 869
 870	kfree(ulpi);
 871}
 872
 873static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
 874{
 875	/* XXX */
 876	return 0;
 877}
 878
 879static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
 880				    unsigned int index)
 881{
 882	struct tegra_xusb_hsic_port *hsic;
 883	struct device_node *np;
 884	int err = 0;
 885
 886	np = tegra_xusb_find_port_node(padctl, "hsic", index);
 887	if (!np || !of_device_is_available(np))
 888		goto out;
 889
 890	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
 891	if (!hsic) {
 892		err = -ENOMEM;
 893		goto out;
 894	}
 895
 896	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
 897	if (err < 0)
 898		goto out;
 899
 900	hsic->base.ops = padctl->soc->ports.hsic.ops;
 901
 902	hsic->base.lane = hsic->base.ops->map(&hsic->base);
 903	if (IS_ERR(hsic->base.lane)) {
 904		err = PTR_ERR(hsic->base.lane);
 905		goto out;
 906	}
 907
 908	err = tegra_xusb_hsic_port_parse_dt(hsic);
 909	if (err < 0) {
 910		tegra_xusb_port_unregister(&hsic->base);
 911		goto out;
 912	}
 913
 914	list_add_tail(&hsic->base.list, &padctl->ports);
 915
 916out:
 917	of_node_put(np);
 918	return err;
 919}
 920
 921void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
 922{
 923	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
 924
 925	kfree(hsic);
 926}
 927
 928static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
 929{
 930	struct tegra_xusb_port *port = &usb3->base;
 931	struct device_node *np = port->dev.of_node;
 932	enum usb_device_speed maximum_speed;
 933	u32 value;
 934	int err;
 935
 936	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
 937	if (err < 0) {
 938		dev_err(&port->dev, "failed to read port: %d\n", err);
 939		return err;
 940	}
 941
 942	usb3->port = value;
 943
 944	usb3->internal = of_property_read_bool(np, "nvidia,internal");
 945
 946	if (device_property_present(&port->dev, "maximum-speed")) {
 947		maximum_speed =  usb_get_maximum_speed(&port->dev);
 948		if (maximum_speed == USB_SPEED_SUPER)
 949			usb3->disable_gen2 = true;
 950		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
 951			usb3->disable_gen2 = false;
 952		else
 953			return -EINVAL;
 954	}
 955
 956	usb3->supply = regulator_get(&port->dev, "vbus");
 957	return PTR_ERR_OR_ZERO(usb3->supply);
 958}
 959
 960static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
 961				    unsigned int index)
 962{
 963	struct tegra_xusb_usb3_port *usb3;
 964	struct device_node *np;
 965	int err = 0;
 966
 967	/*
 968	 * If there is no supplemental configuration in the device tree the
 969	 * port is unusable. But it is valid to configure only a single port,
 970	 * hence return 0 instead of an error to allow ports to be optional.
 971	 */
 972	np = tegra_xusb_find_port_node(padctl, "usb3", index);
 973	if (!np || !of_device_is_available(np))
 974		goto out;
 975
 976	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
 977	if (!usb3) {
 978		err = -ENOMEM;
 979		goto out;
 980	}
 981
 982	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
 983	if (err < 0)
 984		goto out;
 985
 986	usb3->base.ops = padctl->soc->ports.usb3.ops;
 987
 988	usb3->base.lane = usb3->base.ops->map(&usb3->base);
 989	if (IS_ERR(usb3->base.lane)) {
 990		err = PTR_ERR(usb3->base.lane);
 991		goto out;
 992	}
 993
 994	err = tegra_xusb_usb3_port_parse_dt(usb3);
 995	if (err < 0) {
 996		tegra_xusb_port_unregister(&usb3->base);
 997		goto out;
 998	}
 999
1000	list_add_tail(&usb3->base.list, &padctl->ports);
1001
1002out:
1003	of_node_put(np);
1004	return err;
1005}
1006
1007void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1008{
1009	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1010
1011	kfree(usb3);
1012}
1013
1014void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1015{
1016	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1017
1018	regulator_put(usb3->supply);
1019}
1020
1021static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1022{
1023	struct tegra_xusb_port *port, *tmp;
1024
1025	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1026		list_del(&port->list);
1027		tegra_xusb_port_unregister(port);
1028	}
1029}
1030
1031static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1032{
1033	struct device_node *np;
1034	unsigned int i;
1035
1036	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1037		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1038		if (!np || !of_device_is_available(np))
1039			return i;
1040	}
1041
1042	return -ENODEV;
1043}
1044
1045static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1046{
1047	unsigned int i;
1048	struct tegra_xusb_usb3_port *usb3;
1049	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1050
1051	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1052		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1053		if (usb3 && usb3->port == usb2->base.index)
1054			return true;
1055	}
1056
1057	return false;
1058}
1059
1060static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1061{
1062	int fake;
1063
1064	/* Disable usb3_port_fake usage by default and assign if needed */
1065	usb2->usb3_port_fake = -1;
1066
1067	if ((usb2->mode == USB_DR_MODE_OTG ||
1068	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1069		!tegra_xusb_port_is_companion(usb2)) {
1070		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1071		if (fake < 0) {
1072			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1073			return -ENODEV;
1074		}
1075
1076		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1077		usb2->usb3_port_fake = fake;
1078	}
1079
1080	return 0;
1081}
1082
1083static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1084{
1085	struct tegra_xusb_port *port;
1086	struct tegra_xusb_usb2_port *usb2;
1087	unsigned int i;
1088	int err = 0;
1089
1090	mutex_lock(&padctl->lock);
1091
1092	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1093		err = tegra_xusb_add_usb2_port(padctl, i);
1094		if (err < 0)
1095			goto remove_ports;
1096	}
1097
1098	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1099		err = tegra_xusb_add_ulpi_port(padctl, i);
1100		if (err < 0)
1101			goto remove_ports;
1102	}
1103
1104	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1105		err = tegra_xusb_add_hsic_port(padctl, i);
1106		if (err < 0)
1107			goto remove_ports;
1108	}
1109
1110	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1111		err = tegra_xusb_add_usb3_port(padctl, i);
1112		if (err < 0)
1113			goto remove_ports;
1114	}
1115
1116	if (padctl->soc->need_fake_usb3_port) {
1117		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1118			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1119			if (!usb2)
1120				continue;
1121
1122			err = tegra_xusb_update_usb3_fake_port(usb2);
1123			if (err < 0)
1124				goto remove_ports;
1125		}
1126	}
1127
1128	list_for_each_entry(port, &padctl->ports, list) {
1129		err = port->ops->enable(port);
1130		if (err < 0)
1131			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1132				dev_name(&port->dev), err);
1133	}
1134
1135	goto unlock;
1136
1137remove_ports:
1138	__tegra_xusb_remove_ports(padctl);
1139unlock:
1140	mutex_unlock(&padctl->lock);
1141	return err;
1142}
1143
1144static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1145{
1146	mutex_lock(&padctl->lock);
1147	__tegra_xusb_remove_ports(padctl);
1148	mutex_unlock(&padctl->lock);
1149}
1150
1151static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1152{
1153	struct device_node *np = pdev->dev.of_node;
1154	const struct tegra_xusb_padctl_soc *soc;
1155	struct tegra_xusb_padctl *padctl;
1156	const struct of_device_id *match;
 
1157	int err;
1158
1159	/* for backwards compatibility with old device trees */
1160	np = of_get_child_by_name(np, "pads");
1161	if (!np) {
1162		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1163		return tegra_xusb_padctl_legacy_probe(pdev);
1164	}
1165
1166	of_node_put(np);
1167
1168	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1169	soc = match->data;
1170
1171	padctl = soc->ops->probe(&pdev->dev, soc);
1172	if (IS_ERR(padctl))
1173		return PTR_ERR(padctl);
1174
1175	platform_set_drvdata(pdev, padctl);
1176	INIT_LIST_HEAD(&padctl->ports);
1177	INIT_LIST_HEAD(&padctl->lanes);
1178	INIT_LIST_HEAD(&padctl->pads);
1179	mutex_init(&padctl->lock);
1180
1181	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
 
1182	if (IS_ERR(padctl->regs)) {
1183		err = PTR_ERR(padctl->regs);
1184		goto remove;
1185	}
1186
1187	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1188	if (IS_ERR(padctl->rst)) {
1189		err = PTR_ERR(padctl->rst);
1190		goto remove;
1191	}
1192
1193	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1194					sizeof(*padctl->supplies), GFP_KERNEL);
1195	if (!padctl->supplies) {
1196		err = -ENOMEM;
1197		goto remove;
1198	}
1199
1200	regulator_bulk_set_supply_names(padctl->supplies,
1201					padctl->soc->supply_names,
1202					padctl->soc->num_supplies);
1203
1204	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1205				      padctl->supplies);
1206	if (err < 0) {
1207		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1208		goto remove;
1209	}
1210
1211	err = reset_control_deassert(padctl->rst);
1212	if (err < 0)
1213		goto remove;
1214
1215	err = regulator_bulk_enable(padctl->soc->num_supplies,
1216				    padctl->supplies);
1217	if (err < 0) {
1218		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1219		goto reset;
1220	}
1221
1222	err = tegra_xusb_setup_pads(padctl);
1223	if (err < 0) {
1224		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1225		goto power_down;
1226	}
1227
1228	err = tegra_xusb_setup_ports(padctl);
1229	if (err) {
1230		const char *level = KERN_ERR;
1231
1232		if (err == -EPROBE_DEFER)
1233			level = KERN_DEBUG;
1234
1235		dev_printk(level, &pdev->dev,
1236			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1237		goto remove_pads;
1238	}
1239
1240	return 0;
1241
1242remove_pads:
1243	tegra_xusb_remove_pads(padctl);
1244power_down:
1245	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1246reset:
1247	reset_control_assert(padctl->rst);
1248remove:
1249	platform_set_drvdata(pdev, NULL);
1250	soc->ops->remove(padctl);
1251	return err;
1252}
1253
1254static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1255{
1256	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1257	int err;
1258
1259	tegra_xusb_remove_ports(padctl);
1260	tegra_xusb_remove_pads(padctl);
1261
1262	err = regulator_bulk_disable(padctl->soc->num_supplies,
1263				     padctl->supplies);
1264	if (err < 0)
1265		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1266
1267	err = reset_control_assert(padctl->rst);
1268	if (err < 0)
1269		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1270
1271	padctl->soc->ops->remove(padctl);
1272
1273	return err;
1274}
1275
1276static int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1277{
1278	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1279
1280	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
1281		return padctl->soc->ops->suspend_noirq(padctl);
1282
1283	return 0;
1284}
1285
1286static int tegra_xusb_padctl_resume_noirq(struct device *dev)
1287{
1288	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1289
1290	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
1291		return padctl->soc->ops->resume_noirq(padctl);
1292
1293	return 0;
1294}
1295
1296static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
1297	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
1298				      tegra_xusb_padctl_resume_noirq)
1299};
1300
1301static struct platform_driver tegra_xusb_padctl_driver = {
1302	.driver = {
1303		.name = "tegra-xusb-padctl",
1304		.of_match_table = tegra_xusb_padctl_of_match,
1305		.pm = &tegra_xusb_padctl_pm_ops,
1306	},
1307	.probe = tegra_xusb_padctl_probe,
1308	.remove = tegra_xusb_padctl_remove,
1309};
1310module_platform_driver(tegra_xusb_padctl_driver);
1311
1312struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1313{
1314	struct tegra_xusb_padctl *padctl;
1315	struct platform_device *pdev;
1316	struct device_node *np;
1317
1318	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1319	if (!np)
1320		return ERR_PTR(-EINVAL);
1321
1322	/*
1323	 * This is slightly ugly. A better implementation would be to keep a
1324	 * registry of pad controllers, but since there will almost certainly
1325	 * only ever be one per SoC that would be a little overkill.
1326	 */
1327	pdev = of_find_device_by_node(np);
1328	if (!pdev) {
1329		of_node_put(np);
1330		return ERR_PTR(-ENODEV);
1331	}
1332
1333	of_node_put(np);
1334
1335	padctl = platform_get_drvdata(pdev);
1336	if (!padctl) {
1337		put_device(&pdev->dev);
1338		return ERR_PTR(-EPROBE_DEFER);
1339	}
1340
1341	return padctl;
1342}
1343EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1344
1345void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1346{
1347	if (padctl)
1348		put_device(padctl->dev);
1349}
1350EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1351
1352int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1353					unsigned int port)
1354{
1355	if (padctl->soc->ops->usb3_save_context)
1356		return padctl->soc->ops->usb3_save_context(padctl, port);
1357
1358	return -ENOSYS;
1359}
1360EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1361
1362int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1363				    unsigned int port, bool idle)
1364{
1365	if (padctl->soc->ops->hsic_set_idle)
1366		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1367
1368	return -ENOSYS;
1369}
1370EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1371
1372int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
1373					   enum usb_device_speed speed)
1374{
1375	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1376
1377	if (lane->pad->ops->enable_phy_sleepwalk)
1378		return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
1379
1380	return -EOPNOTSUPP;
1381}
1382EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
1383
1384int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
1385{
1386	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1387
1388	if (lane->pad->ops->disable_phy_sleepwalk)
1389		return lane->pad->ops->disable_phy_sleepwalk(lane);
1390
1391	return -EOPNOTSUPP;
1392}
1393EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
1394
1395int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1396{
1397	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1398
1399	if (lane->pad->ops->enable_phy_wake)
1400		return lane->pad->ops->enable_phy_wake(lane);
1401
1402	return -EOPNOTSUPP;
1403}
1404EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
1405
1406int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1407{
1408	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1409
1410	if (lane->pad->ops->disable_phy_wake)
1411		return lane->pad->ops->disable_phy_wake(lane);
1412
1413	return -EOPNOTSUPP;
1414}
1415EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
1416
1417bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
1418{
1419	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1420
1421	if (lane->pad->ops->remote_wake_detected)
1422		return lane->pad->ops->remote_wake_detected(lane);
1423
1424	return false;
1425}
1426EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
1427
1428int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1429					   unsigned int port, bool enable)
1430{
1431	if (padctl->soc->ops->usb3_set_lfps_detect)
1432		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1433							      enable);
1434
1435	return -ENOSYS;
1436}
1437EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1438
1439int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1440							bool val)
1441{
1442	if (padctl->soc->ops->vbus_override)
1443		return padctl->soc->ops->vbus_override(padctl, val);
1444
1445	return -ENOTSUPP;
1446}
1447EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1448
1449int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1450{
1451	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1452	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1453
1454	if (padctl->soc->ops->utmi_port_reset)
1455		return padctl->soc->ops->utmi_port_reset(phy);
1456
1457	return -ENOTSUPP;
1458}
1459EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1460
1461int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1462				    unsigned int port)
1463{
1464	struct tegra_xusb_usb2_port *usb2;
1465	struct tegra_xusb_usb3_port *usb3;
1466	int i;
1467
1468	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1469	if (!usb2)
1470		return -EINVAL;
1471
1472	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1473		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1474		if (usb3 && usb3->port == usb2->base.index)
1475			return usb3->base.index;
1476	}
1477
1478	return -ENODEV;
1479}
1480EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1481
1482MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1483MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1484MODULE_LICENSE("GPL v2");