xref: /linux/drivers/phy/tegra/xusb.c (revision 52990390f91c1c39ca742fc8f390b29891d95127)
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 		tegra_xusb_port_unregister(&usb2->base);
809 		goto out;
810 	}
811 
812 	err = tegra_xusb_usb2_port_parse_dt(usb2);
813 	if (err < 0) {
814 		tegra_xusb_port_unregister(&usb2->base);
815 		goto out;
816 	}
817 
818 	list_add_tail(&usb2->base.list, &padctl->ports);
819 
820 out:
821 	of_node_put(np);
822 	return err;
823 }
824 
825 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
826 {
827 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
828 
829 	kfree(usb2);
830 }
831 
832 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
833 {
834 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
835 
836 	regulator_put(usb2->supply);
837 }
838 
839 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
840 {
841 	struct tegra_xusb_port *port = &ulpi->base;
842 	struct device_node *np = port->dev.of_node;
843 
844 	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
845 
846 	return 0;
847 }
848 
849 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
850 				    unsigned int index)
851 {
852 	struct tegra_xusb_ulpi_port *ulpi;
853 	struct device_node *np;
854 	int err = 0;
855 
856 	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
857 	if (!np || !of_device_is_available(np))
858 		goto out;
859 
860 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
861 	if (!ulpi) {
862 		err = -ENOMEM;
863 		goto out;
864 	}
865 
866 	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
867 	if (err < 0)
868 		goto out;
869 
870 	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
871 
872 	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
873 	if (IS_ERR(ulpi->base.lane)) {
874 		err = PTR_ERR(ulpi->base.lane);
875 		tegra_xusb_port_unregister(&ulpi->base);
876 		goto out;
877 	}
878 
879 	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
880 	if (err < 0) {
881 		tegra_xusb_port_unregister(&ulpi->base);
882 		goto out;
883 	}
884 
885 	list_add_tail(&ulpi->base.list, &padctl->ports);
886 
887 out:
888 	of_node_put(np);
889 	return err;
890 }
891 
892 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
893 {
894 	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
895 
896 	kfree(ulpi);
897 }
898 
899 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
900 {
901 	/* XXX */
902 	return 0;
903 }
904 
905 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
906 				    unsigned int index)
907 {
908 	struct tegra_xusb_hsic_port *hsic;
909 	struct device_node *np;
910 	int err = 0;
911 
912 	np = tegra_xusb_find_port_node(padctl, "hsic", index);
913 	if (!np || !of_device_is_available(np))
914 		goto out;
915 
916 	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
917 	if (!hsic) {
918 		err = -ENOMEM;
919 		goto out;
920 	}
921 
922 	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
923 	if (err < 0)
924 		goto out;
925 
926 	hsic->base.ops = padctl->soc->ports.hsic.ops;
927 
928 	hsic->base.lane = hsic->base.ops->map(&hsic->base);
929 	if (IS_ERR(hsic->base.lane)) {
930 		err = PTR_ERR(hsic->base.lane);
931 		goto out;
932 	}
933 
934 	err = tegra_xusb_hsic_port_parse_dt(hsic);
935 	if (err < 0) {
936 		tegra_xusb_port_unregister(&hsic->base);
937 		goto out;
938 	}
939 
940 	list_add_tail(&hsic->base.list, &padctl->ports);
941 
942 out:
943 	of_node_put(np);
944 	return err;
945 }
946 
947 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
948 {
949 	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
950 
951 	kfree(hsic);
952 }
953 
954 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
955 {
956 	struct tegra_xusb_port *port = &usb3->base;
957 	struct device_node *np = port->dev.of_node;
958 	enum usb_device_speed maximum_speed;
959 	u32 value;
960 	int err;
961 
962 	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
963 	if (err < 0) {
964 		dev_err(&port->dev, "failed to read port: %d\n", err);
965 		return err;
966 	}
967 
968 	usb3->port = value;
969 
970 	usb3->internal = of_property_read_bool(np, "nvidia,internal");
971 
972 	if (device_property_present(&port->dev, "maximum-speed")) {
973 		maximum_speed =  usb_get_maximum_speed(&port->dev);
974 		if (maximum_speed == USB_SPEED_SUPER)
975 			usb3->disable_gen2 = true;
976 		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
977 			usb3->disable_gen2 = false;
978 		else
979 			return -EINVAL;
980 	}
981 
982 	return 0;
983 }
984 
985 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
986 				    unsigned int index)
987 {
988 	struct tegra_xusb_usb3_port *usb3;
989 	struct device_node *np;
990 	int err = 0;
991 
992 	/*
993 	 * If there is no supplemental configuration in the device tree the
994 	 * port is unusable. But it is valid to configure only a single port,
995 	 * hence return 0 instead of an error to allow ports to be optional.
996 	 */
997 	np = tegra_xusb_find_port_node(padctl, "usb3", index);
998 	if (!np || !of_device_is_available(np))
999 		goto out;
1000 
1001 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
1002 	if (!usb3) {
1003 		err = -ENOMEM;
1004 		goto out;
1005 	}
1006 
1007 	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
1008 	if (err < 0)
1009 		goto out;
1010 
1011 	usb3->base.ops = padctl->soc->ports.usb3.ops;
1012 
1013 	usb3->base.lane = usb3->base.ops->map(&usb3->base);
1014 	if (IS_ERR(usb3->base.lane)) {
1015 		err = PTR_ERR(usb3->base.lane);
1016 		goto out;
1017 	}
1018 
1019 	err = tegra_xusb_usb3_port_parse_dt(usb3);
1020 	if (err < 0) {
1021 		tegra_xusb_port_unregister(&usb3->base);
1022 		goto out;
1023 	}
1024 
1025 	list_add_tail(&usb3->base.list, &padctl->ports);
1026 
1027 out:
1028 	of_node_put(np);
1029 	return err;
1030 }
1031 
1032 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1033 {
1034 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1035 
1036 	kfree(usb3);
1037 }
1038 
1039 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1040 {
1041 	struct tegra_xusb_port *port, *tmp;
1042 
1043 	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1044 		list_del(&port->list);
1045 		tegra_xusb_port_unregister(port);
1046 	}
1047 }
1048 
1049 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1050 {
1051 	struct device_node *np;
1052 	unsigned int i;
1053 
1054 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1055 		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1056 		if (!np || !of_device_is_available(np))
1057 			return i;
1058 	}
1059 
1060 	return -ENODEV;
1061 }
1062 
1063 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1064 {
1065 	unsigned int i;
1066 	struct tegra_xusb_usb3_port *usb3;
1067 	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1068 
1069 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1070 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1071 		if (usb3 && usb3->port == usb2->base.index)
1072 			return true;
1073 	}
1074 
1075 	return false;
1076 }
1077 
1078 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1079 {
1080 	int fake;
1081 
1082 	/* Disable usb3_port_fake usage by default and assign if needed */
1083 	usb2->usb3_port_fake = -1;
1084 
1085 	if ((usb2->mode == USB_DR_MODE_OTG ||
1086 	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1087 		!tegra_xusb_port_is_companion(usb2)) {
1088 		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1089 		if (fake < 0) {
1090 			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1091 			return -ENODEV;
1092 		}
1093 
1094 		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1095 		usb2->usb3_port_fake = fake;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1102 {
1103 	struct tegra_xusb_port *port;
1104 	struct tegra_xusb_usb2_port *usb2;
1105 	unsigned int i;
1106 	int err = 0;
1107 
1108 	mutex_lock(&padctl->lock);
1109 
1110 	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1111 		err = tegra_xusb_add_usb2_port(padctl, i);
1112 		if (err < 0)
1113 			goto remove_ports;
1114 	}
1115 
1116 	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1117 		err = tegra_xusb_add_ulpi_port(padctl, i);
1118 		if (err < 0)
1119 			goto remove_ports;
1120 	}
1121 
1122 	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1123 		err = tegra_xusb_add_hsic_port(padctl, i);
1124 		if (err < 0)
1125 			goto remove_ports;
1126 	}
1127 
1128 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1129 		err = tegra_xusb_add_usb3_port(padctl, i);
1130 		if (err < 0)
1131 			goto remove_ports;
1132 	}
1133 
1134 	if (padctl->soc->need_fake_usb3_port) {
1135 		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1136 			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1137 			if (!usb2)
1138 				continue;
1139 
1140 			err = tegra_xusb_update_usb3_fake_port(usb2);
1141 			if (err < 0)
1142 				goto remove_ports;
1143 		}
1144 	}
1145 
1146 	list_for_each_entry(port, &padctl->ports, list) {
1147 		err = port->ops->enable(port);
1148 		if (err < 0)
1149 			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1150 				dev_name(&port->dev), err);
1151 	}
1152 
1153 	goto unlock;
1154 
1155 remove_ports:
1156 	__tegra_xusb_remove_ports(padctl);
1157 unlock:
1158 	mutex_unlock(&padctl->lock);
1159 	return err;
1160 }
1161 
1162 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1163 {
1164 	mutex_lock(&padctl->lock);
1165 	__tegra_xusb_remove_ports(padctl);
1166 	mutex_unlock(&padctl->lock);
1167 }
1168 
1169 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1170 {
1171 	struct device_node *np = pdev->dev.of_node;
1172 	const struct tegra_xusb_padctl_soc *soc;
1173 	struct tegra_xusb_padctl *padctl;
1174 	const struct of_device_id *match;
1175 	int err;
1176 
1177 	/* for backwards compatibility with old device trees */
1178 	np = of_get_child_by_name(np, "pads");
1179 	if (!np) {
1180 		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1181 		return tegra_xusb_padctl_legacy_probe(pdev);
1182 	}
1183 
1184 	of_node_put(np);
1185 
1186 	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1187 	soc = match->data;
1188 
1189 	padctl = soc->ops->probe(&pdev->dev, soc);
1190 	if (IS_ERR(padctl))
1191 		return PTR_ERR(padctl);
1192 
1193 	platform_set_drvdata(pdev, padctl);
1194 	INIT_LIST_HEAD(&padctl->ports);
1195 	INIT_LIST_HEAD(&padctl->lanes);
1196 	INIT_LIST_HEAD(&padctl->pads);
1197 	mutex_init(&padctl->lock);
1198 
1199 	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1200 	if (IS_ERR(padctl->regs)) {
1201 		err = PTR_ERR(padctl->regs);
1202 		goto remove;
1203 	}
1204 
1205 	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1206 	if (IS_ERR(padctl->rst)) {
1207 		err = PTR_ERR(padctl->rst);
1208 		goto remove;
1209 	}
1210 
1211 	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1212 					sizeof(*padctl->supplies), GFP_KERNEL);
1213 	if (!padctl->supplies) {
1214 		err = -ENOMEM;
1215 		goto remove;
1216 	}
1217 
1218 	regulator_bulk_set_supply_names(padctl->supplies,
1219 					padctl->soc->supply_names,
1220 					padctl->soc->num_supplies);
1221 
1222 	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1223 				      padctl->supplies);
1224 	if (err < 0) {
1225 		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1226 		goto remove;
1227 	}
1228 
1229 	err = reset_control_deassert(padctl->rst);
1230 	if (err < 0)
1231 		goto remove;
1232 
1233 	err = regulator_bulk_enable(padctl->soc->num_supplies,
1234 				    padctl->supplies);
1235 	if (err < 0) {
1236 		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1237 		goto reset;
1238 	}
1239 
1240 	err = tegra_xusb_setup_pads(padctl);
1241 	if (err < 0) {
1242 		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1243 		goto power_down;
1244 	}
1245 
1246 	err = tegra_xusb_setup_ports(padctl);
1247 	if (err) {
1248 		const char *level = KERN_ERR;
1249 
1250 		if (err == -EPROBE_DEFER)
1251 			level = KERN_DEBUG;
1252 
1253 		dev_printk(level, &pdev->dev,
1254 			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1255 		goto remove_pads;
1256 	}
1257 
1258 	return 0;
1259 
1260 remove_pads:
1261 	tegra_xusb_remove_pads(padctl);
1262 power_down:
1263 	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1264 reset:
1265 	reset_control_assert(padctl->rst);
1266 remove:
1267 	platform_set_drvdata(pdev, NULL);
1268 	soc->ops->remove(padctl);
1269 	return err;
1270 }
1271 
1272 static void tegra_xusb_padctl_remove(struct platform_device *pdev)
1273 {
1274 	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1275 	int err;
1276 
1277 	tegra_xusb_remove_ports(padctl);
1278 	tegra_xusb_remove_pads(padctl);
1279 
1280 	err = regulator_bulk_disable(padctl->soc->num_supplies,
1281 				     padctl->supplies);
1282 	if (err < 0)
1283 		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1284 
1285 	err = reset_control_assert(padctl->rst);
1286 	if (err < 0)
1287 		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1288 
1289 	padctl->soc->ops->remove(padctl);
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_new = 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