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