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