Linux Audio

Check our new training course

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