Linux Audio

Check our new training course

Loading...
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");
v5.4
   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	{ }
  70};
  71MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
  72
  73static struct device_node *
  74tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
  75{
  76	struct device_node *pads, *np;
  77
  78	pads = of_get_child_by_name(padctl->dev->of_node, "pads");
  79	if (!pads)
  80		return NULL;
  81
  82	np = of_get_child_by_name(pads, name);
  83	of_node_put(pads);
  84
  85	return np;
  86}
  87
  88static struct device_node *
  89tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
  90{
  91	struct device_node *np, *lanes;
  92
  93	lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
  94	if (!lanes)
  95		return NULL;
  96
  97	np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
  98	of_node_put(lanes);
  99
 100	return np;
 101}
 102
 
 
 
 
 
 
 
 
 
 
 
 
 
 103int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
 104			     struct device_node *np)
 105{
 106	struct device *dev = &lane->pad->dev;
 107	const char *function;
 108	int err;
 109
 110	err = of_property_read_string(np, "nvidia,function", &function);
 111	if (err < 0)
 112		return err;
 113
 114	err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
 115	if (err < 0) {
 116		dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
 117			function, np);
 118		return err;
 119	}
 120
 121	lane->function = err;
 122
 123	return 0;
 124}
 125
 126static void tegra_xusb_lane_destroy(struct phy *phy)
 127{
 128	if (phy) {
 129		struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
 130
 131		lane->pad->ops->remove(lane);
 132		phy_destroy(phy);
 133	}
 134}
 135
 136static void tegra_xusb_pad_release(struct device *dev)
 137{
 138	struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
 139
 140	pad->soc->ops->remove(pad);
 141}
 142
 143static struct device_type tegra_xusb_pad_type = {
 144	.release = tegra_xusb_pad_release,
 145};
 146
 147int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
 148			struct tegra_xusb_padctl *padctl,
 149			struct device_node *np)
 150{
 151	int err;
 152
 153	device_initialize(&pad->dev);
 154	INIT_LIST_HEAD(&pad->list);
 155	pad->dev.parent = padctl->dev;
 156	pad->dev.type = &tegra_xusb_pad_type;
 157	pad->dev.of_node = np;
 158	pad->padctl = padctl;
 159
 160	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
 161	if (err < 0)
 162		goto unregister;
 163
 164	err = device_add(&pad->dev);
 165	if (err < 0)
 166		goto unregister;
 167
 168	return 0;
 169
 170unregister:
 171	device_unregister(&pad->dev);
 172	return err;
 173}
 174
 175int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
 176			    const struct phy_ops *ops)
 177{
 178	struct device_node *children;
 179	struct phy *lane;
 180	unsigned int i;
 181	int err;
 182
 183	children = of_get_child_by_name(pad->dev.of_node, "lanes");
 184	if (!children)
 185		return -ENODEV;
 186
 187	pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
 188				  GFP_KERNEL);
 189	if (!pad->lanes) {
 190		of_node_put(children);
 191		return -ENOMEM;
 192	}
 193
 194	for (i = 0; i < pad->soc->num_lanes; i++) {
 195		struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
 196		struct tegra_xusb_lane *lane;
 197
 198		/* skip disabled lanes */
 199		if (!np || !of_device_is_available(np)) {
 200			of_node_put(np);
 201			continue;
 202		}
 203
 204		pad->lanes[i] = phy_create(&pad->dev, np, ops);
 205		if (IS_ERR(pad->lanes[i])) {
 206			err = PTR_ERR(pad->lanes[i]);
 207			of_node_put(np);
 208			goto remove;
 209		}
 210
 211		lane = pad->ops->probe(pad, np, i);
 212		if (IS_ERR(lane)) {
 213			phy_destroy(pad->lanes[i]);
 214			err = PTR_ERR(lane);
 215			goto remove;
 216		}
 217
 218		list_add_tail(&lane->list, &pad->padctl->lanes);
 219		phy_set_drvdata(pad->lanes[i], lane);
 220	}
 221
 222	pad->provider = of_phy_provider_register_full(&pad->dev, children,
 223						      tegra_xusb_pad_of_xlate);
 224	if (IS_ERR(pad->provider)) {
 225		err = PTR_ERR(pad->provider);
 226		goto remove;
 227	}
 228
 229	return 0;
 230
 231remove:
 232	while (i--)
 233		tegra_xusb_lane_destroy(pad->lanes[i]);
 234
 235	of_node_put(children);
 236
 237	return err;
 238}
 239
 240void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
 241{
 242	unsigned int i = pad->soc->num_lanes;
 243
 244	of_phy_provider_unregister(pad->provider);
 245
 246	while (i--)
 247		tegra_xusb_lane_destroy(pad->lanes[i]);
 248
 249	device_unregister(&pad->dev);
 250}
 251
 252static struct tegra_xusb_pad *
 253tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
 254		      const struct tegra_xusb_pad_soc *soc)
 255{
 256	struct tegra_xusb_pad *pad;
 257	struct device_node *np;
 258	int err;
 259
 260	np = tegra_xusb_find_pad_node(padctl, soc->name);
 261	if (!np || !of_device_is_available(np))
 262		return NULL;
 263
 264	pad = soc->ops->probe(padctl, soc, np);
 265	if (IS_ERR(pad)) {
 266		err = PTR_ERR(pad);
 267		dev_err(padctl->dev, "failed to create pad %s: %d\n",
 268			soc->name, err);
 269		return ERR_PTR(err);
 270	}
 271
 272	/* XXX move this into ->probe() to avoid string comparison */
 273	if (strcmp(soc->name, "pcie") == 0)
 274		padctl->pcie = pad;
 275
 276	if (strcmp(soc->name, "sata") == 0)
 277		padctl->sata = pad;
 278
 279	if (strcmp(soc->name, "usb2") == 0)
 280		padctl->usb2 = pad;
 281
 282	if (strcmp(soc->name, "ulpi") == 0)
 283		padctl->ulpi = pad;
 284
 285	if (strcmp(soc->name, "hsic") == 0)
 286		padctl->hsic = pad;
 287
 288	return pad;
 289}
 290
 291static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 292{
 293	struct tegra_xusb_pad *pad, *tmp;
 294
 295	list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
 296		list_del(&pad->list);
 297		tegra_xusb_pad_unregister(pad);
 298	}
 299}
 300
 301static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
 302{
 303	mutex_lock(&padctl->lock);
 304	__tegra_xusb_remove_pads(padctl);
 305	mutex_unlock(&padctl->lock);
 306}
 307
 308static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
 309{
 310	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
 311	const struct tegra_xusb_lane_soc *soc = lane->soc;
 312	u32 value;
 313
 314	/* skip single function lanes */
 315	if (soc->num_funcs < 2)
 316		return;
 317
 318	/* choose function */
 319	value = padctl_readl(padctl, soc->offset);
 320	value &= ~(soc->mask << soc->shift);
 321	value |= lane->function << soc->shift;
 322	padctl_writel(padctl, value, soc->offset);
 323}
 324
 325static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
 326{
 327	unsigned int i;
 328
 329	for (i = 0; i < pad->soc->num_lanes; i++) {
 330		struct tegra_xusb_lane *lane;
 331
 332		if (pad->lanes[i]) {
 333			lane = phy_get_drvdata(pad->lanes[i]);
 334			tegra_xusb_lane_program(lane);
 335		}
 336	}
 337}
 338
 339static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
 340{
 341	struct tegra_xusb_pad *pad;
 342	unsigned int i;
 343
 344	mutex_lock(&padctl->lock);
 345
 346	for (i = 0; i < padctl->soc->num_pads; i++) {
 347		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
 348		int err;
 349
 350		pad = tegra_xusb_pad_create(padctl, soc);
 351		if (IS_ERR(pad)) {
 352			err = PTR_ERR(pad);
 353			dev_err(padctl->dev, "failed to create pad %s: %d\n",
 354				soc->name, err);
 355			__tegra_xusb_remove_pads(padctl);
 356			mutex_unlock(&padctl->lock);
 357			return err;
 358		}
 359
 360		if (!pad)
 361			continue;
 362
 363		list_add_tail(&pad->list, &padctl->pads);
 364	}
 365
 366	list_for_each_entry(pad, &padctl->pads, list)
 367		tegra_xusb_pad_program(pad);
 368
 369	mutex_unlock(&padctl->lock);
 370	return 0;
 371}
 372
 373static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
 374				  const char *function)
 375{
 376	const char *func = lane->soc->funcs[lane->function];
 377
 378	return strcmp(function, func) == 0;
 379}
 380
 381struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
 382					     const char *type,
 383					     unsigned int index)
 384{
 385	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
 386	char *name;
 387
 388	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 389	if (!name)
 390		return ERR_PTR(-ENOMEM);
 391
 392	list_for_each_entry(lane, &padctl->lanes, list) {
 393		if (strcmp(lane->soc->name, name) == 0) {
 394			hit = lane;
 395			break;
 396		}
 397	}
 398
 399	kfree(name);
 400	return hit;
 401}
 402
 403struct tegra_xusb_lane *
 404tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
 405			  const struct tegra_xusb_lane_map *map,
 406			  const char *function)
 407{
 408	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
 409
 410	for (; map->type; map++) {
 411		if (port->index != map->port)
 412			continue;
 413
 414		lane = tegra_xusb_find_lane(port->padctl, map->type,
 415					    map->index);
 416		if (IS_ERR(lane))
 417			continue;
 418
 419		if (!tegra_xusb_lane_check(lane, function))
 420			continue;
 421
 422		if (!IS_ERR(match))
 423			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
 424				map->type, map->index, match->soc->name);
 425		else
 426			match = lane;
 427	}
 428
 429	return match;
 430}
 431
 432static struct device_node *
 433tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
 434			  unsigned int index)
 435{
 436	struct device_node *ports, *np;
 437	char *name;
 438
 439	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
 440	if (!ports)
 441		return NULL;
 442
 443	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
 444	if (!name) {
 445		of_node_put(ports);
 446		return ERR_PTR(-ENOMEM);
 447	}
 448	np = of_get_child_by_name(ports, name);
 449	kfree(name);
 450	of_node_put(ports);
 451
 452	return np;
 453}
 454
 455struct tegra_xusb_port *
 456tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
 457		     unsigned int index)
 458{
 459	struct tegra_xusb_port *port;
 460	struct device_node *np;
 461
 462	np = tegra_xusb_find_port_node(padctl, type, index);
 463	if (!np)
 464		return NULL;
 465
 466	list_for_each_entry(port, &padctl->ports, list) {
 467		if (np == port->dev.of_node) {
 468			of_node_put(np);
 469			return port;
 470		}
 471	}
 472
 473	of_node_put(np);
 474
 475	return NULL;
 476}
 477
 478struct tegra_xusb_usb2_port *
 479tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 480{
 481	struct tegra_xusb_port *port;
 482
 483	port = tegra_xusb_find_port(padctl, "usb2", index);
 484	if (port)
 485		return to_usb2_port(port);
 486
 487	return NULL;
 488}
 489
 490struct tegra_xusb_usb3_port *
 491tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
 492{
 493	struct tegra_xusb_port *port;
 494
 495	port = tegra_xusb_find_port(padctl, "usb3", index);
 496	if (port)
 497		return to_usb3_port(port);
 498
 499	return NULL;
 500}
 501
 502static void tegra_xusb_port_release(struct device *dev)
 503{
 504}
 505
 506static struct device_type tegra_xusb_port_type = {
 507	.release = tegra_xusb_port_release,
 508};
 509
 510static int tegra_xusb_port_init(struct tegra_xusb_port *port,
 511				struct tegra_xusb_padctl *padctl,
 512				struct device_node *np,
 513				const char *name,
 514				unsigned int index)
 515{
 516	int err;
 517
 518	INIT_LIST_HEAD(&port->list);
 519	port->padctl = padctl;
 520	port->index = index;
 521
 522	device_initialize(&port->dev);
 523	port->dev.type = &tegra_xusb_port_type;
 524	port->dev.of_node = of_node_get(np);
 525	port->dev.parent = padctl->dev;
 526
 527	err = dev_set_name(&port->dev, "%s-%u", name, index);
 528	if (err < 0)
 529		goto unregister;
 530
 531	err = device_add(&port->dev);
 532	if (err < 0)
 533		goto unregister;
 534
 535	return 0;
 536
 537unregister:
 538	device_unregister(&port->dev);
 539	return err;
 540}
 541
 542static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
 543{
 544	device_unregister(&port->dev);
 545}
 546
 547static const char *const modes[] = {
 548	[USB_DR_MODE_UNKNOWN] = "",
 549	[USB_DR_MODE_HOST] = "host",
 550	[USB_DR_MODE_PERIPHERAL] = "peripheral",
 551	[USB_DR_MODE_OTG] = "otg",
 552};
 553
 554static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
 555{
 556	struct tegra_xusb_port *port = &usb2->base;
 557	struct device_node *np = port->dev.of_node;
 558	const char *mode;
 559
 560	usb2->internal = of_property_read_bool(np, "nvidia,internal");
 561
 562	if (!of_property_read_string(np, "mode", &mode)) {
 563		int err = match_string(modes, ARRAY_SIZE(modes), mode);
 564		if (err < 0) {
 565			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
 566				mode);
 567			usb2->mode = USB_DR_MODE_UNKNOWN;
 568		} else {
 569			usb2->mode = err;
 570		}
 571	} else {
 572		usb2->mode = USB_DR_MODE_HOST;
 573	}
 574
 575	usb2->supply = devm_regulator_get(&port->dev, "vbus");
 576	return PTR_ERR_OR_ZERO(usb2->supply);
 577}
 578
 579static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
 580				    unsigned int index)
 581{
 582	struct tegra_xusb_usb2_port *usb2;
 583	struct device_node *np;
 584	int err = 0;
 585
 586	/*
 587	 * USB2 ports don't require additional properties, but if the port is
 588	 * marked as disabled there is no reason to register it.
 589	 */
 590	np = tegra_xusb_find_port_node(padctl, "usb2", index);
 591	if (!np || !of_device_is_available(np))
 592		goto out;
 593
 594	usb2 = devm_kzalloc(padctl->dev, sizeof(*usb2), GFP_KERNEL);
 595	if (!usb2) {
 596		err = -ENOMEM;
 597		goto out;
 598	}
 599
 600	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
 601	if (err < 0)
 602		goto out;
 603
 604	usb2->base.ops = padctl->soc->ports.usb2.ops;
 605
 606	usb2->base.lane = usb2->base.ops->map(&usb2->base);
 607	if (IS_ERR(usb2->base.lane)) {
 608		err = PTR_ERR(usb2->base.lane);
 609		goto out;
 610	}
 611
 612	err = tegra_xusb_usb2_port_parse_dt(usb2);
 613	if (err < 0) {
 614		tegra_xusb_port_unregister(&usb2->base);
 615		goto out;
 616	}
 617
 618	list_add_tail(&usb2->base.list, &padctl->ports);
 619
 620out:
 621	of_node_put(np);
 622	return err;
 623}
 624
 625static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
 626{
 627	struct tegra_xusb_port *port = &ulpi->base;
 628	struct device_node *np = port->dev.of_node;
 629
 630	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
 631
 632	return 0;
 633}
 634
 635static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
 636				    unsigned int index)
 637{
 638	struct tegra_xusb_ulpi_port *ulpi;
 639	struct device_node *np;
 640	int err = 0;
 641
 642	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
 643	if (!np || !of_device_is_available(np))
 644		goto out;
 645
 646	ulpi = devm_kzalloc(padctl->dev, sizeof(*ulpi), GFP_KERNEL);
 647	if (!ulpi) {
 648		err = -ENOMEM;
 649		goto out;
 650	}
 651
 652	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
 653	if (err < 0)
 654		goto out;
 655
 656	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
 657
 658	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
 659	if (IS_ERR(ulpi->base.lane)) {
 660		err = PTR_ERR(ulpi->base.lane);
 661		goto out;
 662	}
 663
 664	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
 665	if (err < 0) {
 666		tegra_xusb_port_unregister(&ulpi->base);
 667		goto out;
 668	}
 669
 670	list_add_tail(&ulpi->base.list, &padctl->ports);
 671
 672out:
 673	of_node_put(np);
 674	return err;
 675}
 676
 677static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
 678{
 679	/* XXX */
 680	return 0;
 681}
 682
 683static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
 684				    unsigned int index)
 685{
 686	struct tegra_xusb_hsic_port *hsic;
 687	struct device_node *np;
 688	int err = 0;
 689
 690	np = tegra_xusb_find_port_node(padctl, "hsic", index);
 691	if (!np || !of_device_is_available(np))
 692		goto out;
 693
 694	hsic = devm_kzalloc(padctl->dev, sizeof(*hsic), GFP_KERNEL);
 695	if (!hsic) {
 696		err = -ENOMEM;
 697		goto out;
 698	}
 699
 700	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
 701	if (err < 0)
 702		goto out;
 703
 704	hsic->base.ops = padctl->soc->ports.hsic.ops;
 705
 706	hsic->base.lane = hsic->base.ops->map(&hsic->base);
 707	if (IS_ERR(hsic->base.lane)) {
 708		err = PTR_ERR(hsic->base.lane);
 709		goto out;
 710	}
 711
 712	err = tegra_xusb_hsic_port_parse_dt(hsic);
 713	if (err < 0) {
 714		tegra_xusb_port_unregister(&hsic->base);
 715		goto out;
 716	}
 717
 718	list_add_tail(&hsic->base.list, &padctl->ports);
 719
 720out:
 721	of_node_put(np);
 722	return err;
 723}
 724
 725static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
 726{
 727	struct tegra_xusb_port *port = &usb3->base;
 728	struct device_node *np = port->dev.of_node;
 729	u32 value;
 730	int err;
 731
 732	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
 733	if (err < 0) {
 734		dev_err(&port->dev, "failed to read port: %d\n", err);
 735		return err;
 736	}
 737
 738	usb3->port = value;
 739
 740	usb3->internal = of_property_read_bool(np, "nvidia,internal");
 741
 742	usb3->supply = devm_regulator_get(&port->dev, "vbus");
 743	return PTR_ERR_OR_ZERO(usb3->supply);
 744}
 745
 746static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
 747				    unsigned int index)
 748{
 749	struct tegra_xusb_usb3_port *usb3;
 750	struct device_node *np;
 751	int err = 0;
 752
 753	/*
 754	 * If there is no supplemental configuration in the device tree the
 755	 * port is unusable. But it is valid to configure only a single port,
 756	 * hence return 0 instead of an error to allow ports to be optional.
 757	 */
 758	np = tegra_xusb_find_port_node(padctl, "usb3", index);
 759	if (!np || !of_device_is_available(np))
 760		goto out;
 761
 762	usb3 = devm_kzalloc(padctl->dev, sizeof(*usb3), GFP_KERNEL);
 763	if (!usb3) {
 764		err = -ENOMEM;
 765		goto out;
 766	}
 767
 768	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
 769	if (err < 0)
 770		goto out;
 771
 772	usb3->base.ops = padctl->soc->ports.usb3.ops;
 773
 774	usb3->base.lane = usb3->base.ops->map(&usb3->base);
 775	if (IS_ERR(usb3->base.lane)) {
 776		err = PTR_ERR(usb3->base.lane);
 777		goto out;
 778	}
 779
 780	err = tegra_xusb_usb3_port_parse_dt(usb3);
 781	if (err < 0) {
 782		tegra_xusb_port_unregister(&usb3->base);
 783		goto out;
 784	}
 785
 786	list_add_tail(&usb3->base.list, &padctl->ports);
 787
 788out:
 789	of_node_put(np);
 790	return err;
 791}
 792
 793static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
 794{
 795	struct tegra_xusb_port *port, *tmp;
 796
 797	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
 798		list_del(&port->list);
 799		tegra_xusb_port_unregister(port);
 800	}
 801}
 802
 803static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
 804{
 805	struct tegra_xusb_port *port;
 806	unsigned int i;
 807	int err = 0;
 808
 809	mutex_lock(&padctl->lock);
 810
 811	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
 812		err = tegra_xusb_add_usb2_port(padctl, i);
 813		if (err < 0)
 814			goto remove_ports;
 815	}
 816
 817	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
 818		err = tegra_xusb_add_ulpi_port(padctl, i);
 819		if (err < 0)
 820			goto remove_ports;
 821	}
 822
 823	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
 824		err = tegra_xusb_add_hsic_port(padctl, i);
 825		if (err < 0)
 826			goto remove_ports;
 827	}
 828
 829	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
 830		err = tegra_xusb_add_usb3_port(padctl, i);
 831		if (err < 0)
 832			goto remove_ports;
 833	}
 834
 835	list_for_each_entry(port, &padctl->ports, list) {
 836		err = port->ops->enable(port);
 837		if (err < 0)
 838			dev_err(padctl->dev, "failed to enable port %s: %d\n",
 839				dev_name(&port->dev), err);
 840	}
 841
 842	goto unlock;
 843
 844remove_ports:
 845	__tegra_xusb_remove_ports(padctl);
 846unlock:
 847	mutex_unlock(&padctl->lock);
 848	return err;
 849}
 850
 851static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
 852{
 853	mutex_lock(&padctl->lock);
 854	__tegra_xusb_remove_ports(padctl);
 855	mutex_unlock(&padctl->lock);
 856}
 857
 858static int tegra_xusb_padctl_probe(struct platform_device *pdev)
 859{
 860	struct device_node *np = pdev->dev.of_node;
 861	const struct tegra_xusb_padctl_soc *soc;
 862	struct tegra_xusb_padctl *padctl;
 863	const struct of_device_id *match;
 864	struct resource *res;
 865	unsigned int i;
 866	int err;
 867
 868	/* for backwards compatibility with old device trees */
 869	np = of_get_child_by_name(np, "pads");
 870	if (!np) {
 871		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
 872		return tegra_xusb_padctl_legacy_probe(pdev);
 873	}
 874
 875	of_node_put(np);
 876
 877	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
 878	soc = match->data;
 879
 880	padctl = soc->ops->probe(&pdev->dev, soc);
 881	if (IS_ERR(padctl))
 882		return PTR_ERR(padctl);
 883
 884	platform_set_drvdata(pdev, padctl);
 885	INIT_LIST_HEAD(&padctl->ports);
 886	INIT_LIST_HEAD(&padctl->lanes);
 887	INIT_LIST_HEAD(&padctl->pads);
 888	mutex_init(&padctl->lock);
 889
 890	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 891	padctl->regs = devm_ioremap_resource(&pdev->dev, res);
 892	if (IS_ERR(padctl->regs)) {
 893		err = PTR_ERR(padctl->regs);
 894		goto remove;
 895	}
 896
 897	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
 898	if (IS_ERR(padctl->rst)) {
 899		err = PTR_ERR(padctl->rst);
 900		goto remove;
 901	}
 902
 903	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
 904					sizeof(*padctl->supplies), GFP_KERNEL);
 905	if (!padctl->supplies) {
 906		err = -ENOMEM;
 907		goto remove;
 908	}
 909
 910	for (i = 0; i < padctl->soc->num_supplies; i++)
 911		padctl->supplies[i].supply = padctl->soc->supply_names[i];
 912
 913	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
 914				      padctl->supplies);
 915	if (err < 0) {
 916		dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
 917		goto remove;
 918	}
 919
 920	err = reset_control_deassert(padctl->rst);
 921	if (err < 0)
 922		goto remove;
 923
 924	err = regulator_bulk_enable(padctl->soc->num_supplies,
 925				    padctl->supplies);
 926	if (err < 0) {
 927		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
 928		goto reset;
 929	}
 930
 931	err = tegra_xusb_setup_pads(padctl);
 932	if (err < 0) {
 933		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
 934		goto power_down;
 935	}
 936
 937	err = tegra_xusb_setup_ports(padctl);
 938	if (err) {
 939		dev_err(&pdev->dev, "failed to setup XUSB ports: %d\n", err);
 940		goto remove_pads;
 941	}
 942
 943	return 0;
 944
 945remove_pads:
 946	tegra_xusb_remove_pads(padctl);
 947power_down:
 948	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
 949reset:
 950	reset_control_assert(padctl->rst);
 951remove:
 952	soc->ops->remove(padctl);
 953	return err;
 954}
 955
 956static int tegra_xusb_padctl_remove(struct platform_device *pdev)
 957{
 958	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
 959	int err;
 960
 961	tegra_xusb_remove_ports(padctl);
 962	tegra_xusb_remove_pads(padctl);
 963
 964	err = regulator_bulk_disable(padctl->soc->num_supplies,
 965				     padctl->supplies);
 966	if (err < 0)
 967		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
 968
 969	err = reset_control_assert(padctl->rst);
 970	if (err < 0)
 971		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
 972
 973	padctl->soc->ops->remove(padctl);
 974
 975	return err;
 976}
 977
 978static struct platform_driver tegra_xusb_padctl_driver = {
 979	.driver = {
 980		.name = "tegra-xusb-padctl",
 981		.of_match_table = tegra_xusb_padctl_of_match,
 982	},
 983	.probe = tegra_xusb_padctl_probe,
 984	.remove = tegra_xusb_padctl_remove,
 985};
 986module_platform_driver(tegra_xusb_padctl_driver);
 987
 988struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
 989{
 990	struct tegra_xusb_padctl *padctl;
 991	struct platform_device *pdev;
 992	struct device_node *np;
 993
 994	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
 995	if (!np)
 996		return ERR_PTR(-EINVAL);
 997
 998	/*
 999	 * This is slightly ugly. A better implementation would be to keep a
1000	 * registry of pad controllers, but since there will almost certainly
1001	 * only ever be one per SoC that would be a little overkill.
1002	 */
1003	pdev = of_find_device_by_node(np);
1004	if (!pdev) {
1005		of_node_put(np);
1006		return ERR_PTR(-ENODEV);
1007	}
1008
1009	of_node_put(np);
1010
1011	padctl = platform_get_drvdata(pdev);
1012	if (!padctl) {
1013		put_device(&pdev->dev);
1014		return ERR_PTR(-EPROBE_DEFER);
1015	}
1016
1017	return padctl;
1018}
1019EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1020
1021void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1022{
1023	if (padctl)
1024		put_device(padctl->dev);
1025}
1026EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1027
1028int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1029					unsigned int port)
1030{
1031	if (padctl->soc->ops->usb3_save_context)
1032		return padctl->soc->ops->usb3_save_context(padctl, port);
1033
1034	return -ENOSYS;
1035}
1036EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1037
1038int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1039				    unsigned int port, bool idle)
1040{
1041	if (padctl->soc->ops->hsic_set_idle)
1042		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1043
1044	return -ENOSYS;
1045}
1046EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1047
1048int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1049					   unsigned int port, bool enable)
1050{
1051	if (padctl->soc->ops->usb3_set_lfps_detect)
1052		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1053							      enable);
1054
1055	return -ENOSYS;
1056}
1057EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1058
1059MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1060MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1061MODULE_LICENSE("GPL v2");