xref: /linux/drivers/phy/tegra/xusb.c (revision 202779456dc5b75d07b214064161ef6a2421e8be)
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_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 
24 static 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 
50 static 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 };
83 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
84 
85 static struct device_node *
86 tegra_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 
100 static struct device_node *
101 tegra_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 
115 int 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 
138 static 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 
148 static 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 
155 static const struct device_type tegra_xusb_pad_type = {
156 	.release = tegra_xusb_pad_release,
157 };
158 
159 int 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 
182 unregister:
183 	device_unregister(&pad->dev);
184 	return err;
185 }
186 
187 int 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 
243 remove:
244 	while (i--)
245 		tegra_xusb_lane_destroy(pad->lanes[i]);
246 
247 	of_node_put(children);
248 
249 	return err;
250 }
251 
252 void 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 
264 static struct tegra_xusb_pad *
265 tegra_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 
303 static 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 
313 static 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 
320 static 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 
343 static 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 
357 static 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 
391 bool 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 
399 struct 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 
421 struct tegra_xusb_lane *
422 tegra_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 
450 static struct device_node *
451 tegra_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 
473 struct tegra_xusb_port *
474 tegra_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 
496 struct tegra_xusb_usb2_port *
497 tegra_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 
508 struct tegra_xusb_usb3_port *
509 tegra_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 
520 static 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 
528 static const struct device_type tegra_xusb_port_type = {
529 	.release = tegra_xusb_port_release,
530 };
531 
532 static 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 
559 unregister:
560 	device_unregister(&port->dev);
561 	return err;
562 }
563 
564 static 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 	}
572 
573 	if (port->ops->remove)
574 		port->ops->remove(port);
575 
576 	device_unregister(&port->dev);
577 }
578 
579 static const char *const modes[] = {
580 	[USB_DR_MODE_UNKNOWN] = "",
581 	[USB_DR_MODE_HOST] = "host",
582 	[USB_DR_MODE_PERIPHERAL] = "peripheral",
583 	[USB_DR_MODE_OTG] = "otg",
584 };
585 
586 static const char * const usb_roles[] = {
587 	[USB_ROLE_NONE]		= "none",
588 	[USB_ROLE_HOST]		= "host",
589 	[USB_ROLE_DEVICE]	= "device",
590 };
591 
592 static enum usb_phy_events to_usb_phy_event(enum usb_role role)
593 {
594 	switch (role) {
595 	case USB_ROLE_DEVICE:
596 		return USB_EVENT_VBUS;
597 
598 	case USB_ROLE_HOST:
599 		return USB_EVENT_ID;
600 
601 	default:
602 		return USB_EVENT_NONE;
603 	}
604 }
605 
606 static void tegra_xusb_usb_phy_work(struct work_struct *work)
607 {
608 	struct tegra_xusb_port *port = container_of(work,
609 						    struct tegra_xusb_port,
610 						    usb_phy_work);
611 	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
612 
613 	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
614 
615 	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
616 		usb_roles[role]);
617 
618 	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
619 }
620 
621 static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
622 				  enum usb_role role)
623 {
624 	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
625 
626 	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
627 
628 	schedule_work(&port->usb_phy_work);
629 
630 	return 0;
631 }
632 
633 static int tegra_xusb_set_peripheral(struct usb_otg *otg,
634 				     struct usb_gadget *gadget)
635 {
636 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
637 						    struct tegra_xusb_port,
638 						    usb_phy);
639 
640 	if (gadget != NULL)
641 		schedule_work(&port->usb_phy_work);
642 
643 	return 0;
644 }
645 
646 static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
647 {
648 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
649 						    struct tegra_xusb_port,
650 						    usb_phy);
651 
652 	if (host != NULL)
653 		schedule_work(&port->usb_phy_work);
654 
655 	return 0;
656 }
657 
658 
659 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
660 {
661 	struct tegra_xusb_lane *lane;
662 	struct usb_role_switch_desc role_sx_desc = {
663 		.fwnode = dev_fwnode(&port->dev),
664 		.set = tegra_xusb_role_sw_set,
665 		.allow_userspace_control = true,
666 	};
667 	int err = 0;
668 
669 	/*
670 	 * USB role switch driver needs parent driver owner info. This is a
671 	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
672 	 * where an optimal solution is possible with changes to USB role
673 	 * switch driver.
674 	 */
675 	port->dev.driver = devm_kzalloc(&port->dev,
676 					sizeof(struct device_driver),
677 					GFP_KERNEL);
678 	port->dev.driver->owner	 = THIS_MODULE;
679 
680 	port->usb_role_sw = usb_role_switch_register(&port->dev,
681 						     &role_sx_desc);
682 	if (IS_ERR(port->usb_role_sw)) {
683 		err = PTR_ERR(port->usb_role_sw);
684 		dev_err(&port->dev, "failed to register USB role switch: %d",
685 			err);
686 		return err;
687 	}
688 
689 	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
690 	usb_role_switch_set_drvdata(port->usb_role_sw, port);
691 
692 	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
693 					 GFP_KERNEL);
694 	if (!port->usb_phy.otg)
695 		return -ENOMEM;
696 
697 	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
698 
699 	/*
700 	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
701 	 * reference to retrieve usb-phy details.
702 	 */
703 	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
704 	port->usb_phy.dev->driver = port->dev.driver;
705 	port->usb_phy.otg->usb_phy = &port->usb_phy;
706 	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
707 	port->usb_phy.otg->set_host = tegra_xusb_set_host;
708 
709 	err = usb_add_phy_dev(&port->usb_phy);
710 	if (err < 0) {
711 		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
712 		return err;
713 	}
714 
715 	/* populate connector entry */
716 	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
717 
718 	return err;
719 }
720 
721 static void tegra_xusb_parse_usb_role_default_mode(struct tegra_xusb_port *port)
722 {
723 	enum usb_role role = USB_ROLE_NONE;
724 	enum usb_dr_mode mode = usb_get_role_switch_default_mode(&port->dev);
725 
726 	if (mode == USB_DR_MODE_HOST)
727 		role = USB_ROLE_HOST;
728 	else if (mode == USB_DR_MODE_PERIPHERAL)
729 		role = USB_ROLE_DEVICE;
730 
731 	if (role != USB_ROLE_NONE) {
732 		usb_role_switch_set_role(port->usb_role_sw, role);
733 		dev_dbg(&port->dev, "usb role default mode is %s", modes[mode]);
734 	}
735 }
736 
737 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
738 {
739 	struct tegra_xusb_port *port = &usb2->base;
740 	struct device_node *np = port->dev.of_node;
741 	const char *mode;
742 	int err;
743 
744 	usb2->internal = of_property_read_bool(np, "nvidia,internal");
745 
746 	if (!of_property_read_string(np, "mode", &mode)) {
747 		int err = match_string(modes, ARRAY_SIZE(modes), mode);
748 		if (err < 0) {
749 			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
750 				mode);
751 			usb2->mode = USB_DR_MODE_UNKNOWN;
752 		} else {
753 			usb2->mode = err;
754 		}
755 	} else {
756 		usb2->mode = USB_DR_MODE_HOST;
757 	}
758 
759 	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
760 	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
761 	    usb2->mode == USB_DR_MODE_OTG) {
762 		if (of_property_read_bool(np, "usb-role-switch")) {
763 			err = tegra_xusb_setup_usb_role_switch(port);
764 			if (err < 0)
765 				return err;
766 			tegra_xusb_parse_usb_role_default_mode(port);
767 		} else {
768 			dev_err(&port->dev, "usb-role-switch not found for %s mode",
769 				modes[usb2->mode]);
770 			return -EINVAL;
771 		}
772 	}
773 
774 	usb2->supply = regulator_get(&port->dev, "vbus");
775 	return PTR_ERR_OR_ZERO(usb2->supply);
776 }
777 
778 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
779 				    unsigned int index)
780 {
781 	struct tegra_xusb_usb2_port *usb2;
782 	struct device_node *np;
783 	int err = 0;
784 
785 	/*
786 	 * USB2 ports don't require additional properties, but if the port is
787 	 * marked as disabled there is no reason to register it.
788 	 */
789 	np = tegra_xusb_find_port_node(padctl, "usb2", index);
790 	if (!np || !of_device_is_available(np))
791 		goto out;
792 
793 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
794 	if (!usb2) {
795 		err = -ENOMEM;
796 		goto out;
797 	}
798 
799 	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
800 	if (err < 0)
801 		goto out;
802 
803 	usb2->base.ops = padctl->soc->ports.usb2.ops;
804 
805 	usb2->base.lane = usb2->base.ops->map(&usb2->base);
806 	if (IS_ERR(usb2->base.lane)) {
807 		err = PTR_ERR(usb2->base.lane);
808 		goto out;
809 	}
810 
811 	err = tegra_xusb_usb2_port_parse_dt(usb2);
812 	if (err < 0) {
813 		tegra_xusb_port_unregister(&usb2->base);
814 		goto out;
815 	}
816 
817 	list_add_tail(&usb2->base.list, &padctl->ports);
818 
819 out:
820 	of_node_put(np);
821 	return err;
822 }
823 
824 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
825 {
826 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
827 
828 	kfree(usb2);
829 }
830 
831 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
832 {
833 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
834 
835 	regulator_put(usb2->supply);
836 }
837 
838 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
839 {
840 	struct tegra_xusb_port *port = &ulpi->base;
841 	struct device_node *np = port->dev.of_node;
842 
843 	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
844 
845 	return 0;
846 }
847 
848 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
849 				    unsigned int index)
850 {
851 	struct tegra_xusb_ulpi_port *ulpi;
852 	struct device_node *np;
853 	int err = 0;
854 
855 	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
856 	if (!np || !of_device_is_available(np))
857 		goto out;
858 
859 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
860 	if (!ulpi) {
861 		err = -ENOMEM;
862 		goto out;
863 	}
864 
865 	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
866 	if (err < 0)
867 		goto out;
868 
869 	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
870 
871 	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
872 	if (IS_ERR(ulpi->base.lane)) {
873 		err = PTR_ERR(ulpi->base.lane);
874 		goto out;
875 	}
876 
877 	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
878 	if (err < 0) {
879 		tegra_xusb_port_unregister(&ulpi->base);
880 		goto out;
881 	}
882 
883 	list_add_tail(&ulpi->base.list, &padctl->ports);
884 
885 out:
886 	of_node_put(np);
887 	return err;
888 }
889 
890 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
891 {
892 	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
893 
894 	kfree(ulpi);
895 }
896 
897 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
898 {
899 	/* XXX */
900 	return 0;
901 }
902 
903 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
904 				    unsigned int index)
905 {
906 	struct tegra_xusb_hsic_port *hsic;
907 	struct device_node *np;
908 	int err = 0;
909 
910 	np = tegra_xusb_find_port_node(padctl, "hsic", index);
911 	if (!np || !of_device_is_available(np))
912 		goto out;
913 
914 	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
915 	if (!hsic) {
916 		err = -ENOMEM;
917 		goto out;
918 	}
919 
920 	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
921 	if (err < 0)
922 		goto out;
923 
924 	hsic->base.ops = padctl->soc->ports.hsic.ops;
925 
926 	hsic->base.lane = hsic->base.ops->map(&hsic->base);
927 	if (IS_ERR(hsic->base.lane)) {
928 		err = PTR_ERR(hsic->base.lane);
929 		goto out;
930 	}
931 
932 	err = tegra_xusb_hsic_port_parse_dt(hsic);
933 	if (err < 0) {
934 		tegra_xusb_port_unregister(&hsic->base);
935 		goto out;
936 	}
937 
938 	list_add_tail(&hsic->base.list, &padctl->ports);
939 
940 out:
941 	of_node_put(np);
942 	return err;
943 }
944 
945 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
946 {
947 	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
948 
949 	kfree(hsic);
950 }
951 
952 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
953 {
954 	struct tegra_xusb_port *port = &usb3->base;
955 	struct device_node *np = port->dev.of_node;
956 	enum usb_device_speed maximum_speed;
957 	u32 value;
958 	int err;
959 
960 	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
961 	if (err < 0) {
962 		dev_err(&port->dev, "failed to read port: %d\n", err);
963 		return err;
964 	}
965 
966 	usb3->port = value;
967 
968 	usb3->internal = of_property_read_bool(np, "nvidia,internal");
969 
970 	if (device_property_present(&port->dev, "maximum-speed")) {
971 		maximum_speed =  usb_get_maximum_speed(&port->dev);
972 		if (maximum_speed == USB_SPEED_SUPER)
973 			usb3->disable_gen2 = true;
974 		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
975 			usb3->disable_gen2 = false;
976 		else
977 			return -EINVAL;
978 	}
979 
980 	return 0;
981 }
982 
983 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
984 				    unsigned int index)
985 {
986 	struct tegra_xusb_usb3_port *usb3;
987 	struct device_node *np;
988 	int err = 0;
989 
990 	/*
991 	 * If there is no supplemental configuration in the device tree the
992 	 * port is unusable. But it is valid to configure only a single port,
993 	 * hence return 0 instead of an error to allow ports to be optional.
994 	 */
995 	np = tegra_xusb_find_port_node(padctl, "usb3", index);
996 	if (!np || !of_device_is_available(np))
997 		goto out;
998 
999 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
1000 	if (!usb3) {
1001 		err = -ENOMEM;
1002 		goto out;
1003 	}
1004 
1005 	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
1006 	if (err < 0)
1007 		goto out;
1008 
1009 	usb3->base.ops = padctl->soc->ports.usb3.ops;
1010 
1011 	usb3->base.lane = usb3->base.ops->map(&usb3->base);
1012 	if (IS_ERR(usb3->base.lane)) {
1013 		err = PTR_ERR(usb3->base.lane);
1014 		goto out;
1015 	}
1016 
1017 	err = tegra_xusb_usb3_port_parse_dt(usb3);
1018 	if (err < 0) {
1019 		tegra_xusb_port_unregister(&usb3->base);
1020 		goto out;
1021 	}
1022 
1023 	list_add_tail(&usb3->base.list, &padctl->ports);
1024 
1025 out:
1026 	of_node_put(np);
1027 	return err;
1028 }
1029 
1030 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1031 {
1032 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1033 
1034 	kfree(usb3);
1035 }
1036 
1037 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1038 {
1039 	struct tegra_xusb_port *port, *tmp;
1040 
1041 	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1042 		list_del(&port->list);
1043 		tegra_xusb_port_unregister(port);
1044 	}
1045 }
1046 
1047 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1048 {
1049 	struct device_node *np;
1050 	unsigned int i;
1051 
1052 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1053 		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1054 		if (!np || !of_device_is_available(np))
1055 			return i;
1056 	}
1057 
1058 	return -ENODEV;
1059 }
1060 
1061 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1062 {
1063 	unsigned int i;
1064 	struct tegra_xusb_usb3_port *usb3;
1065 	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1066 
1067 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1068 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1069 		if (usb3 && usb3->port == usb2->base.index)
1070 			return true;
1071 	}
1072 
1073 	return false;
1074 }
1075 
1076 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1077 {
1078 	int fake;
1079 
1080 	/* Disable usb3_port_fake usage by default and assign if needed */
1081 	usb2->usb3_port_fake = -1;
1082 
1083 	if ((usb2->mode == USB_DR_MODE_OTG ||
1084 	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1085 		!tegra_xusb_port_is_companion(usb2)) {
1086 		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1087 		if (fake < 0) {
1088 			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1089 			return -ENODEV;
1090 		}
1091 
1092 		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1093 		usb2->usb3_port_fake = fake;
1094 	}
1095 
1096 	return 0;
1097 }
1098 
1099 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1100 {
1101 	struct tegra_xusb_port *port;
1102 	struct tegra_xusb_usb2_port *usb2;
1103 	unsigned int i;
1104 	int err = 0;
1105 
1106 	mutex_lock(&padctl->lock);
1107 
1108 	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1109 		err = tegra_xusb_add_usb2_port(padctl, i);
1110 		if (err < 0)
1111 			goto remove_ports;
1112 	}
1113 
1114 	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1115 		err = tegra_xusb_add_ulpi_port(padctl, i);
1116 		if (err < 0)
1117 			goto remove_ports;
1118 	}
1119 
1120 	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1121 		err = tegra_xusb_add_hsic_port(padctl, i);
1122 		if (err < 0)
1123 			goto remove_ports;
1124 	}
1125 
1126 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1127 		err = tegra_xusb_add_usb3_port(padctl, i);
1128 		if (err < 0)
1129 			goto remove_ports;
1130 	}
1131 
1132 	if (padctl->soc->need_fake_usb3_port) {
1133 		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1134 			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1135 			if (!usb2)
1136 				continue;
1137 
1138 			err = tegra_xusb_update_usb3_fake_port(usb2);
1139 			if (err < 0)
1140 				goto remove_ports;
1141 		}
1142 	}
1143 
1144 	list_for_each_entry(port, &padctl->ports, list) {
1145 		err = port->ops->enable(port);
1146 		if (err < 0)
1147 			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1148 				dev_name(&port->dev), err);
1149 	}
1150 
1151 	goto unlock;
1152 
1153 remove_ports:
1154 	__tegra_xusb_remove_ports(padctl);
1155 unlock:
1156 	mutex_unlock(&padctl->lock);
1157 	return err;
1158 }
1159 
1160 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1161 {
1162 	mutex_lock(&padctl->lock);
1163 	__tegra_xusb_remove_ports(padctl);
1164 	mutex_unlock(&padctl->lock);
1165 }
1166 
1167 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1168 {
1169 	struct device_node *np = pdev->dev.of_node;
1170 	const struct tegra_xusb_padctl_soc *soc;
1171 	struct tegra_xusb_padctl *padctl;
1172 	const struct of_device_id *match;
1173 	int err;
1174 
1175 	/* for backwards compatibility with old device trees */
1176 	np = of_get_child_by_name(np, "pads");
1177 	if (!np) {
1178 		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1179 		return tegra_xusb_padctl_legacy_probe(pdev);
1180 	}
1181 
1182 	of_node_put(np);
1183 
1184 	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1185 	soc = match->data;
1186 
1187 	padctl = soc->ops->probe(&pdev->dev, soc);
1188 	if (IS_ERR(padctl))
1189 		return PTR_ERR(padctl);
1190 
1191 	platform_set_drvdata(pdev, padctl);
1192 	INIT_LIST_HEAD(&padctl->ports);
1193 	INIT_LIST_HEAD(&padctl->lanes);
1194 	INIT_LIST_HEAD(&padctl->pads);
1195 	mutex_init(&padctl->lock);
1196 
1197 	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1198 	if (IS_ERR(padctl->regs)) {
1199 		err = PTR_ERR(padctl->regs);
1200 		goto remove;
1201 	}
1202 
1203 	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1204 	if (IS_ERR(padctl->rst)) {
1205 		err = PTR_ERR(padctl->rst);
1206 		goto remove;
1207 	}
1208 
1209 	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1210 					sizeof(*padctl->supplies), GFP_KERNEL);
1211 	if (!padctl->supplies) {
1212 		err = -ENOMEM;
1213 		goto remove;
1214 	}
1215 
1216 	regulator_bulk_set_supply_names(padctl->supplies,
1217 					padctl->soc->supply_names,
1218 					padctl->soc->num_supplies);
1219 
1220 	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1221 				      padctl->supplies);
1222 	if (err < 0) {
1223 		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1224 		goto remove;
1225 	}
1226 
1227 	err = reset_control_deassert(padctl->rst);
1228 	if (err < 0)
1229 		goto remove;
1230 
1231 	err = regulator_bulk_enable(padctl->soc->num_supplies,
1232 				    padctl->supplies);
1233 	if (err < 0) {
1234 		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1235 		goto reset;
1236 	}
1237 
1238 	err = tegra_xusb_setup_pads(padctl);
1239 	if (err < 0) {
1240 		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1241 		goto power_down;
1242 	}
1243 
1244 	err = tegra_xusb_setup_ports(padctl);
1245 	if (err) {
1246 		const char *level = KERN_ERR;
1247 
1248 		if (err == -EPROBE_DEFER)
1249 			level = KERN_DEBUG;
1250 
1251 		dev_printk(level, &pdev->dev,
1252 			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1253 		goto remove_pads;
1254 	}
1255 
1256 	return 0;
1257 
1258 remove_pads:
1259 	tegra_xusb_remove_pads(padctl);
1260 power_down:
1261 	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1262 reset:
1263 	reset_control_assert(padctl->rst);
1264 remove:
1265 	platform_set_drvdata(pdev, NULL);
1266 	soc->ops->remove(padctl);
1267 	return err;
1268 }
1269 
1270 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1271 {
1272 	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1273 	int err;
1274 
1275 	tegra_xusb_remove_ports(padctl);
1276 	tegra_xusb_remove_pads(padctl);
1277 
1278 	err = regulator_bulk_disable(padctl->soc->num_supplies,
1279 				     padctl->supplies);
1280 	if (err < 0)
1281 		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1282 
1283 	err = reset_control_assert(padctl->rst);
1284 	if (err < 0)
1285 		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1286 
1287 	padctl->soc->ops->remove(padctl);
1288 
1289 	return 0;
1290 }
1291 
1292 static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1293 {
1294 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1295 
1296 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
1297 		return padctl->soc->ops->suspend_noirq(padctl);
1298 
1299 	return 0;
1300 }
1301 
1302 static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
1303 {
1304 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1305 
1306 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
1307 		return padctl->soc->ops->resume_noirq(padctl);
1308 
1309 	return 0;
1310 }
1311 
1312 static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
1313 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
1314 				      tegra_xusb_padctl_resume_noirq)
1315 };
1316 
1317 static struct platform_driver tegra_xusb_padctl_driver = {
1318 	.driver = {
1319 		.name = "tegra-xusb-padctl",
1320 		.of_match_table = tegra_xusb_padctl_of_match,
1321 		.pm = &tegra_xusb_padctl_pm_ops,
1322 	},
1323 	.probe = tegra_xusb_padctl_probe,
1324 	.remove = tegra_xusb_padctl_remove,
1325 };
1326 module_platform_driver(tegra_xusb_padctl_driver);
1327 
1328 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1329 {
1330 	struct tegra_xusb_padctl *padctl;
1331 	struct platform_device *pdev;
1332 	struct device_node *np;
1333 
1334 	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1335 	if (!np)
1336 		return ERR_PTR(-EINVAL);
1337 
1338 	/*
1339 	 * This is slightly ugly. A better implementation would be to keep a
1340 	 * registry of pad controllers, but since there will almost certainly
1341 	 * only ever be one per SoC that would be a little overkill.
1342 	 */
1343 	pdev = of_find_device_by_node(np);
1344 	if (!pdev) {
1345 		of_node_put(np);
1346 		return ERR_PTR(-ENODEV);
1347 	}
1348 
1349 	of_node_put(np);
1350 
1351 	padctl = platform_get_drvdata(pdev);
1352 	if (!padctl) {
1353 		put_device(&pdev->dev);
1354 		return ERR_PTR(-EPROBE_DEFER);
1355 	}
1356 
1357 	return padctl;
1358 }
1359 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1360 
1361 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1362 {
1363 	if (padctl)
1364 		put_device(padctl->dev);
1365 }
1366 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1367 
1368 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1369 					unsigned int port)
1370 {
1371 	if (padctl->soc->ops->usb3_save_context)
1372 		return padctl->soc->ops->usb3_save_context(padctl, port);
1373 
1374 	return -ENOSYS;
1375 }
1376 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1377 
1378 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1379 				    unsigned int port, bool idle)
1380 {
1381 	if (padctl->soc->ops->hsic_set_idle)
1382 		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1383 
1384 	return -ENOSYS;
1385 }
1386 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1387 
1388 int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
1389 					   enum usb_device_speed speed)
1390 {
1391 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1392 
1393 	if (lane->pad->ops->enable_phy_sleepwalk)
1394 		return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
1395 
1396 	return -EOPNOTSUPP;
1397 }
1398 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
1399 
1400 int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
1401 {
1402 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1403 
1404 	if (lane->pad->ops->disable_phy_sleepwalk)
1405 		return lane->pad->ops->disable_phy_sleepwalk(lane);
1406 
1407 	return -EOPNOTSUPP;
1408 }
1409 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
1410 
1411 int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1412 {
1413 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1414 
1415 	if (lane->pad->ops->enable_phy_wake)
1416 		return lane->pad->ops->enable_phy_wake(lane);
1417 
1418 	return -EOPNOTSUPP;
1419 }
1420 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
1421 
1422 int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1423 {
1424 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1425 
1426 	if (lane->pad->ops->disable_phy_wake)
1427 		return lane->pad->ops->disable_phy_wake(lane);
1428 
1429 	return -EOPNOTSUPP;
1430 }
1431 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
1432 
1433 bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
1434 {
1435 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1436 
1437 	if (lane->pad->ops->remote_wake_detected)
1438 		return lane->pad->ops->remote_wake_detected(lane);
1439 
1440 	return false;
1441 }
1442 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
1443 
1444 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1445 					   unsigned int port, bool enable)
1446 {
1447 	if (padctl->soc->ops->usb3_set_lfps_detect)
1448 		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1449 							      enable);
1450 
1451 	return -ENOSYS;
1452 }
1453 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1454 
1455 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1456 							bool val)
1457 {
1458 	if (padctl->soc->ops->vbus_override)
1459 		return padctl->soc->ops->vbus_override(padctl, val);
1460 
1461 	return -ENOTSUPP;
1462 }
1463 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1464 
1465 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1466 {
1467 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1468 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1469 
1470 	if (padctl->soc->ops->utmi_port_reset)
1471 		return padctl->soc->ops->utmi_port_reset(phy);
1472 
1473 	return -ENOTSUPP;
1474 }
1475 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1476 
1477 void tegra_phy_xusb_utmi_pad_power_on(struct phy *phy)
1478 {
1479 	struct tegra_xusb_lane *lane;
1480 	struct tegra_xusb_padctl *padctl;
1481 
1482 	if (!phy)
1483 		return;
1484 
1485 	lane = phy_get_drvdata(phy);
1486 	padctl = lane->pad->padctl;
1487 
1488 	if (padctl->soc->ops->utmi_pad_power_on)
1489 		padctl->soc->ops->utmi_pad_power_on(phy);
1490 }
1491 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on);
1492 
1493 void tegra_phy_xusb_utmi_pad_power_down(struct phy *phy)
1494 {
1495 	struct tegra_xusb_lane *lane;
1496 	struct tegra_xusb_padctl *padctl;
1497 
1498 	if (!phy)
1499 		return;
1500 
1501 	lane = phy_get_drvdata(phy);
1502 	padctl = lane->pad->padctl;
1503 
1504 	if (padctl->soc->ops->utmi_pad_power_down)
1505 		padctl->soc->ops->utmi_pad_power_down(phy);
1506 }
1507 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down);
1508 
1509 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1510 				    unsigned int port)
1511 {
1512 	struct tegra_xusb_usb2_port *usb2;
1513 	struct tegra_xusb_usb3_port *usb3;
1514 	int i;
1515 
1516 	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1517 	if (!usb2)
1518 		return -EINVAL;
1519 
1520 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1521 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1522 		if (usb3 && usb3->port == usb2->base.index)
1523 			return usb3->base.index;
1524 	}
1525 
1526 	return -ENODEV;
1527 }
1528 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1529 
1530 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1531 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1532 MODULE_LICENSE("GPL v2");
1533