xref: /linux/drivers/phy/tegra/xusb.c (revision 50bd67abe5bacc8fd160461bb3eb2c164c4698c0)
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 int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
722 {
723 	struct tegra_xusb_port *port = &usb2->base;
724 	struct device_node *np = port->dev.of_node;
725 	const char *mode;
726 	int err;
727 
728 	usb2->internal = of_property_read_bool(np, "nvidia,internal");
729 
730 	if (!of_property_read_string(np, "mode", &mode)) {
731 		int err = match_string(modes, ARRAY_SIZE(modes), mode);
732 		if (err < 0) {
733 			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
734 				mode);
735 			usb2->mode = USB_DR_MODE_UNKNOWN;
736 		} else {
737 			usb2->mode = err;
738 		}
739 	} else {
740 		usb2->mode = USB_DR_MODE_HOST;
741 	}
742 
743 	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
744 	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
745 	    usb2->mode == USB_DR_MODE_OTG) {
746 		if (of_property_read_bool(np, "usb-role-switch")) {
747 			err = tegra_xusb_setup_usb_role_switch(port);
748 			if (err < 0)
749 				return err;
750 		} else {
751 			dev_err(&port->dev, "usb-role-switch not found for %s mode",
752 				modes[usb2->mode]);
753 			return -EINVAL;
754 		}
755 	}
756 
757 	usb2->supply = regulator_get(&port->dev, "vbus");
758 	return PTR_ERR_OR_ZERO(usb2->supply);
759 }
760 
761 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
762 				    unsigned int index)
763 {
764 	struct tegra_xusb_usb2_port *usb2;
765 	struct device_node *np;
766 	int err = 0;
767 
768 	/*
769 	 * USB2 ports don't require additional properties, but if the port is
770 	 * marked as disabled there is no reason to register it.
771 	 */
772 	np = tegra_xusb_find_port_node(padctl, "usb2", index);
773 	if (!np || !of_device_is_available(np))
774 		goto out;
775 
776 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
777 	if (!usb2) {
778 		err = -ENOMEM;
779 		goto out;
780 	}
781 
782 	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
783 	if (err < 0)
784 		goto out;
785 
786 	usb2->base.ops = padctl->soc->ports.usb2.ops;
787 
788 	usb2->base.lane = usb2->base.ops->map(&usb2->base);
789 	if (IS_ERR(usb2->base.lane)) {
790 		err = PTR_ERR(usb2->base.lane);
791 		goto out;
792 	}
793 
794 	err = tegra_xusb_usb2_port_parse_dt(usb2);
795 	if (err < 0) {
796 		tegra_xusb_port_unregister(&usb2->base);
797 		goto out;
798 	}
799 
800 	list_add_tail(&usb2->base.list, &padctl->ports);
801 
802 out:
803 	of_node_put(np);
804 	return err;
805 }
806 
807 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
808 {
809 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
810 
811 	kfree(usb2);
812 }
813 
814 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
815 {
816 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
817 
818 	regulator_put(usb2->supply);
819 }
820 
821 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
822 {
823 	struct tegra_xusb_port *port = &ulpi->base;
824 	struct device_node *np = port->dev.of_node;
825 
826 	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
827 
828 	return 0;
829 }
830 
831 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
832 				    unsigned int index)
833 {
834 	struct tegra_xusb_ulpi_port *ulpi;
835 	struct device_node *np;
836 	int err = 0;
837 
838 	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
839 	if (!np || !of_device_is_available(np))
840 		goto out;
841 
842 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
843 	if (!ulpi) {
844 		err = -ENOMEM;
845 		goto out;
846 	}
847 
848 	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
849 	if (err < 0)
850 		goto out;
851 
852 	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
853 
854 	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
855 	if (IS_ERR(ulpi->base.lane)) {
856 		err = PTR_ERR(ulpi->base.lane);
857 		goto out;
858 	}
859 
860 	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
861 	if (err < 0) {
862 		tegra_xusb_port_unregister(&ulpi->base);
863 		goto out;
864 	}
865 
866 	list_add_tail(&ulpi->base.list, &padctl->ports);
867 
868 out:
869 	of_node_put(np);
870 	return err;
871 }
872 
873 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
874 {
875 	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
876 
877 	kfree(ulpi);
878 }
879 
880 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
881 {
882 	/* XXX */
883 	return 0;
884 }
885 
886 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
887 				    unsigned int index)
888 {
889 	struct tegra_xusb_hsic_port *hsic;
890 	struct device_node *np;
891 	int err = 0;
892 
893 	np = tegra_xusb_find_port_node(padctl, "hsic", index);
894 	if (!np || !of_device_is_available(np))
895 		goto out;
896 
897 	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
898 	if (!hsic) {
899 		err = -ENOMEM;
900 		goto out;
901 	}
902 
903 	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
904 	if (err < 0)
905 		goto out;
906 
907 	hsic->base.ops = padctl->soc->ports.hsic.ops;
908 
909 	hsic->base.lane = hsic->base.ops->map(&hsic->base);
910 	if (IS_ERR(hsic->base.lane)) {
911 		err = PTR_ERR(hsic->base.lane);
912 		goto out;
913 	}
914 
915 	err = tegra_xusb_hsic_port_parse_dt(hsic);
916 	if (err < 0) {
917 		tegra_xusb_port_unregister(&hsic->base);
918 		goto out;
919 	}
920 
921 	list_add_tail(&hsic->base.list, &padctl->ports);
922 
923 out:
924 	of_node_put(np);
925 	return err;
926 }
927 
928 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
929 {
930 	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
931 
932 	kfree(hsic);
933 }
934 
935 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
936 {
937 	struct tegra_xusb_port *port = &usb3->base;
938 	struct device_node *np = port->dev.of_node;
939 	enum usb_device_speed maximum_speed;
940 	u32 value;
941 	int err;
942 
943 	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
944 	if (err < 0) {
945 		dev_err(&port->dev, "failed to read port: %d\n", err);
946 		return err;
947 	}
948 
949 	usb3->port = value;
950 
951 	usb3->internal = of_property_read_bool(np, "nvidia,internal");
952 
953 	if (device_property_present(&port->dev, "maximum-speed")) {
954 		maximum_speed =  usb_get_maximum_speed(&port->dev);
955 		if (maximum_speed == USB_SPEED_SUPER)
956 			usb3->disable_gen2 = true;
957 		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
958 			usb3->disable_gen2 = false;
959 		else
960 			return -EINVAL;
961 	}
962 
963 	return 0;
964 }
965 
966 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
967 				    unsigned int index)
968 {
969 	struct tegra_xusb_usb3_port *usb3;
970 	struct device_node *np;
971 	int err = 0;
972 
973 	/*
974 	 * If there is no supplemental configuration in the device tree the
975 	 * port is unusable. But it is valid to configure only a single port,
976 	 * hence return 0 instead of an error to allow ports to be optional.
977 	 */
978 	np = tegra_xusb_find_port_node(padctl, "usb3", index);
979 	if (!np || !of_device_is_available(np))
980 		goto out;
981 
982 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
983 	if (!usb3) {
984 		err = -ENOMEM;
985 		goto out;
986 	}
987 
988 	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
989 	if (err < 0)
990 		goto out;
991 
992 	usb3->base.ops = padctl->soc->ports.usb3.ops;
993 
994 	usb3->base.lane = usb3->base.ops->map(&usb3->base);
995 	if (IS_ERR(usb3->base.lane)) {
996 		err = PTR_ERR(usb3->base.lane);
997 		goto out;
998 	}
999 
1000 	err = tegra_xusb_usb3_port_parse_dt(usb3);
1001 	if (err < 0) {
1002 		tegra_xusb_port_unregister(&usb3->base);
1003 		goto out;
1004 	}
1005 
1006 	list_add_tail(&usb3->base.list, &padctl->ports);
1007 
1008 out:
1009 	of_node_put(np);
1010 	return err;
1011 }
1012 
1013 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1014 {
1015 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1016 
1017 	kfree(usb3);
1018 }
1019 
1020 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1021 {
1022 	struct tegra_xusb_port *port, *tmp;
1023 
1024 	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1025 		list_del(&port->list);
1026 		tegra_xusb_port_unregister(port);
1027 	}
1028 }
1029 
1030 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1031 {
1032 	struct device_node *np;
1033 	unsigned int i;
1034 
1035 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1036 		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1037 		if (!np || !of_device_is_available(np))
1038 			return i;
1039 	}
1040 
1041 	return -ENODEV;
1042 }
1043 
1044 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1045 {
1046 	unsigned int i;
1047 	struct tegra_xusb_usb3_port *usb3;
1048 	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1049 
1050 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1051 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1052 		if (usb3 && usb3->port == usb2->base.index)
1053 			return true;
1054 	}
1055 
1056 	return false;
1057 }
1058 
1059 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1060 {
1061 	int fake;
1062 
1063 	/* Disable usb3_port_fake usage by default and assign if needed */
1064 	usb2->usb3_port_fake = -1;
1065 
1066 	if ((usb2->mode == USB_DR_MODE_OTG ||
1067 	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1068 		!tegra_xusb_port_is_companion(usb2)) {
1069 		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1070 		if (fake < 0) {
1071 			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1072 			return -ENODEV;
1073 		}
1074 
1075 		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1076 		usb2->usb3_port_fake = fake;
1077 	}
1078 
1079 	return 0;
1080 }
1081 
1082 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1083 {
1084 	struct tegra_xusb_port *port;
1085 	struct tegra_xusb_usb2_port *usb2;
1086 	unsigned int i;
1087 	int err = 0;
1088 
1089 	mutex_lock(&padctl->lock);
1090 
1091 	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1092 		err = tegra_xusb_add_usb2_port(padctl, i);
1093 		if (err < 0)
1094 			goto remove_ports;
1095 	}
1096 
1097 	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1098 		err = tegra_xusb_add_ulpi_port(padctl, i);
1099 		if (err < 0)
1100 			goto remove_ports;
1101 	}
1102 
1103 	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1104 		err = tegra_xusb_add_hsic_port(padctl, i);
1105 		if (err < 0)
1106 			goto remove_ports;
1107 	}
1108 
1109 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1110 		err = tegra_xusb_add_usb3_port(padctl, i);
1111 		if (err < 0)
1112 			goto remove_ports;
1113 	}
1114 
1115 	if (padctl->soc->need_fake_usb3_port) {
1116 		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1117 			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1118 			if (!usb2)
1119 				continue;
1120 
1121 			err = tegra_xusb_update_usb3_fake_port(usb2);
1122 			if (err < 0)
1123 				goto remove_ports;
1124 		}
1125 	}
1126 
1127 	list_for_each_entry(port, &padctl->ports, list) {
1128 		err = port->ops->enable(port);
1129 		if (err < 0)
1130 			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1131 				dev_name(&port->dev), err);
1132 	}
1133 
1134 	goto unlock;
1135 
1136 remove_ports:
1137 	__tegra_xusb_remove_ports(padctl);
1138 unlock:
1139 	mutex_unlock(&padctl->lock);
1140 	return err;
1141 }
1142 
1143 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1144 {
1145 	mutex_lock(&padctl->lock);
1146 	__tegra_xusb_remove_ports(padctl);
1147 	mutex_unlock(&padctl->lock);
1148 }
1149 
1150 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1151 {
1152 	struct device_node *np = pdev->dev.of_node;
1153 	const struct tegra_xusb_padctl_soc *soc;
1154 	struct tegra_xusb_padctl *padctl;
1155 	const struct of_device_id *match;
1156 	int err;
1157 
1158 	/* for backwards compatibility with old device trees */
1159 	np = of_get_child_by_name(np, "pads");
1160 	if (!np) {
1161 		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1162 		return tegra_xusb_padctl_legacy_probe(pdev);
1163 	}
1164 
1165 	of_node_put(np);
1166 
1167 	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1168 	soc = match->data;
1169 
1170 	padctl = soc->ops->probe(&pdev->dev, soc);
1171 	if (IS_ERR(padctl))
1172 		return PTR_ERR(padctl);
1173 
1174 	platform_set_drvdata(pdev, padctl);
1175 	INIT_LIST_HEAD(&padctl->ports);
1176 	INIT_LIST_HEAD(&padctl->lanes);
1177 	INIT_LIST_HEAD(&padctl->pads);
1178 	mutex_init(&padctl->lock);
1179 
1180 	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1181 	if (IS_ERR(padctl->regs)) {
1182 		err = PTR_ERR(padctl->regs);
1183 		goto remove;
1184 	}
1185 
1186 	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1187 	if (IS_ERR(padctl->rst)) {
1188 		err = PTR_ERR(padctl->rst);
1189 		goto remove;
1190 	}
1191 
1192 	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1193 					sizeof(*padctl->supplies), GFP_KERNEL);
1194 	if (!padctl->supplies) {
1195 		err = -ENOMEM;
1196 		goto remove;
1197 	}
1198 
1199 	regulator_bulk_set_supply_names(padctl->supplies,
1200 					padctl->soc->supply_names,
1201 					padctl->soc->num_supplies);
1202 
1203 	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1204 				      padctl->supplies);
1205 	if (err < 0) {
1206 		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1207 		goto remove;
1208 	}
1209 
1210 	err = reset_control_deassert(padctl->rst);
1211 	if (err < 0)
1212 		goto remove;
1213 
1214 	err = regulator_bulk_enable(padctl->soc->num_supplies,
1215 				    padctl->supplies);
1216 	if (err < 0) {
1217 		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1218 		goto reset;
1219 	}
1220 
1221 	err = tegra_xusb_setup_pads(padctl);
1222 	if (err < 0) {
1223 		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1224 		goto power_down;
1225 	}
1226 
1227 	err = tegra_xusb_setup_ports(padctl);
1228 	if (err) {
1229 		const char *level = KERN_ERR;
1230 
1231 		if (err == -EPROBE_DEFER)
1232 			level = KERN_DEBUG;
1233 
1234 		dev_printk(level, &pdev->dev,
1235 			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1236 		goto remove_pads;
1237 	}
1238 
1239 	return 0;
1240 
1241 remove_pads:
1242 	tegra_xusb_remove_pads(padctl);
1243 power_down:
1244 	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1245 reset:
1246 	reset_control_assert(padctl->rst);
1247 remove:
1248 	platform_set_drvdata(pdev, NULL);
1249 	soc->ops->remove(padctl);
1250 	return err;
1251 }
1252 
1253 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1254 {
1255 	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1256 	int err;
1257 
1258 	tegra_xusb_remove_ports(padctl);
1259 	tegra_xusb_remove_pads(padctl);
1260 
1261 	err = regulator_bulk_disable(padctl->soc->num_supplies,
1262 				     padctl->supplies);
1263 	if (err < 0)
1264 		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1265 
1266 	err = reset_control_assert(padctl->rst);
1267 	if (err < 0)
1268 		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1269 
1270 	padctl->soc->ops->remove(padctl);
1271 
1272 	return 0;
1273 }
1274 
1275 static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1276 {
1277 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1278 
1279 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
1280 		return padctl->soc->ops->suspend_noirq(padctl);
1281 
1282 	return 0;
1283 }
1284 
1285 static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
1286 {
1287 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1288 
1289 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
1290 		return padctl->soc->ops->resume_noirq(padctl);
1291 
1292 	return 0;
1293 }
1294 
1295 static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
1296 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
1297 				      tegra_xusb_padctl_resume_noirq)
1298 };
1299 
1300 static struct platform_driver tegra_xusb_padctl_driver = {
1301 	.driver = {
1302 		.name = "tegra-xusb-padctl",
1303 		.of_match_table = tegra_xusb_padctl_of_match,
1304 		.pm = &tegra_xusb_padctl_pm_ops,
1305 	},
1306 	.probe = tegra_xusb_padctl_probe,
1307 	.remove = tegra_xusb_padctl_remove,
1308 };
1309 module_platform_driver(tegra_xusb_padctl_driver);
1310 
1311 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1312 {
1313 	struct tegra_xusb_padctl *padctl;
1314 	struct platform_device *pdev;
1315 	struct device_node *np;
1316 
1317 	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1318 	if (!np)
1319 		return ERR_PTR(-EINVAL);
1320 
1321 	/*
1322 	 * This is slightly ugly. A better implementation would be to keep a
1323 	 * registry of pad controllers, but since there will almost certainly
1324 	 * only ever be one per SoC that would be a little overkill.
1325 	 */
1326 	pdev = of_find_device_by_node(np);
1327 	if (!pdev) {
1328 		of_node_put(np);
1329 		return ERR_PTR(-ENODEV);
1330 	}
1331 
1332 	of_node_put(np);
1333 
1334 	padctl = platform_get_drvdata(pdev);
1335 	if (!padctl) {
1336 		put_device(&pdev->dev);
1337 		return ERR_PTR(-EPROBE_DEFER);
1338 	}
1339 
1340 	return padctl;
1341 }
1342 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1343 
1344 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1345 {
1346 	if (padctl)
1347 		put_device(padctl->dev);
1348 }
1349 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1350 
1351 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1352 					unsigned int port)
1353 {
1354 	if (padctl->soc->ops->usb3_save_context)
1355 		return padctl->soc->ops->usb3_save_context(padctl, port);
1356 
1357 	return -ENOSYS;
1358 }
1359 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1360 
1361 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1362 				    unsigned int port, bool idle)
1363 {
1364 	if (padctl->soc->ops->hsic_set_idle)
1365 		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1366 
1367 	return -ENOSYS;
1368 }
1369 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1370 
1371 int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
1372 					   enum usb_device_speed speed)
1373 {
1374 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1375 
1376 	if (lane->pad->ops->enable_phy_sleepwalk)
1377 		return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
1378 
1379 	return -EOPNOTSUPP;
1380 }
1381 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
1382 
1383 int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
1384 {
1385 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1386 
1387 	if (lane->pad->ops->disable_phy_sleepwalk)
1388 		return lane->pad->ops->disable_phy_sleepwalk(lane);
1389 
1390 	return -EOPNOTSUPP;
1391 }
1392 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
1393 
1394 int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1395 {
1396 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1397 
1398 	if (lane->pad->ops->enable_phy_wake)
1399 		return lane->pad->ops->enable_phy_wake(lane);
1400 
1401 	return -EOPNOTSUPP;
1402 }
1403 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
1404 
1405 int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1406 {
1407 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1408 
1409 	if (lane->pad->ops->disable_phy_wake)
1410 		return lane->pad->ops->disable_phy_wake(lane);
1411 
1412 	return -EOPNOTSUPP;
1413 }
1414 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
1415 
1416 bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
1417 {
1418 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1419 
1420 	if (lane->pad->ops->remote_wake_detected)
1421 		return lane->pad->ops->remote_wake_detected(lane);
1422 
1423 	return false;
1424 }
1425 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
1426 
1427 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1428 					   unsigned int port, bool enable)
1429 {
1430 	if (padctl->soc->ops->usb3_set_lfps_detect)
1431 		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1432 							      enable);
1433 
1434 	return -ENOSYS;
1435 }
1436 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1437 
1438 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1439 							bool val)
1440 {
1441 	if (padctl->soc->ops->vbus_override)
1442 		return padctl->soc->ops->vbus_override(padctl, val);
1443 
1444 	return -ENOTSUPP;
1445 }
1446 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1447 
1448 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1449 {
1450 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1451 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1452 
1453 	if (padctl->soc->ops->utmi_port_reset)
1454 		return padctl->soc->ops->utmi_port_reset(phy);
1455 
1456 	return -ENOTSUPP;
1457 }
1458 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1459 
1460 void tegra_phy_xusb_utmi_pad_power_on(struct phy *phy)
1461 {
1462 	struct tegra_xusb_lane *lane;
1463 	struct tegra_xusb_padctl *padctl;
1464 
1465 	if (!phy)
1466 		return;
1467 
1468 	lane = phy_get_drvdata(phy);
1469 	padctl = lane->pad->padctl;
1470 
1471 	if (padctl->soc->ops->utmi_pad_power_on)
1472 		padctl->soc->ops->utmi_pad_power_on(phy);
1473 }
1474 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on);
1475 
1476 void tegra_phy_xusb_utmi_pad_power_down(struct phy *phy)
1477 {
1478 	struct tegra_xusb_lane *lane;
1479 	struct tegra_xusb_padctl *padctl;
1480 
1481 	if (!phy)
1482 		return;
1483 
1484 	lane = phy_get_drvdata(phy);
1485 	padctl = lane->pad->padctl;
1486 
1487 	if (padctl->soc->ops->utmi_pad_power_down)
1488 		padctl->soc->ops->utmi_pad_power_down(phy);
1489 }
1490 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down);
1491 
1492 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1493 				    unsigned int port)
1494 {
1495 	struct tegra_xusb_usb2_port *usb2;
1496 	struct tegra_xusb_usb3_port *usb3;
1497 	int i;
1498 
1499 	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1500 	if (!usb2)
1501 		return -EINVAL;
1502 
1503 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1504 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1505 		if (usb3 && usb3->port == usb2->base.index)
1506 			return usb3->base.index;
1507 	}
1508 
1509 	return -ENODEV;
1510 }
1511 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1512 
1513 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1514 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1515 MODULE_LICENSE("GPL v2");
1516