• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014-2016, 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 
tegra_xusb_pad_of_xlate(struct device * dev,struct of_phandle_args * args)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 	{ }
76 };
77 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
78 
79 static struct device_node *
tegra_xusb_find_pad_node(struct tegra_xusb_padctl * padctl,const char * name)80 tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
81 {
82 	struct device_node *pads, *np;
83 
84 	pads = of_get_child_by_name(padctl->dev->of_node, "pads");
85 	if (!pads)
86 		return NULL;
87 
88 	np = of_get_child_by_name(pads, name);
89 	of_node_put(pads);
90 
91 	return np;
92 }
93 
94 static struct device_node *
tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad * pad,unsigned int index)95 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
96 {
97 	struct device_node *np, *lanes;
98 
99 	lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
100 	if (!lanes)
101 		return NULL;
102 
103 	np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
104 	of_node_put(lanes);
105 
106 	return np;
107 }
108 
tegra_xusb_lane_parse_dt(struct tegra_xusb_lane * lane,struct device_node * np)109 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
110 			     struct device_node *np)
111 {
112 	struct device *dev = &lane->pad->dev;
113 	const char *function;
114 	int err;
115 
116 	err = of_property_read_string(np, "nvidia,function", &function);
117 	if (err < 0)
118 		return err;
119 
120 	err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
121 	if (err < 0) {
122 		dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
123 			function, np);
124 		return err;
125 	}
126 
127 	lane->function = err;
128 
129 	return 0;
130 }
131 
tegra_xusb_lane_destroy(struct phy * phy)132 static void tegra_xusb_lane_destroy(struct phy *phy)
133 {
134 	if (phy) {
135 		struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
136 
137 		lane->pad->ops->remove(lane);
138 		phy_destroy(phy);
139 	}
140 }
141 
tegra_xusb_pad_release(struct device * dev)142 static void tegra_xusb_pad_release(struct device *dev)
143 {
144 	struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
145 
146 	pad->soc->ops->remove(pad);
147 }
148 
149 static struct device_type tegra_xusb_pad_type = {
150 	.release = tegra_xusb_pad_release,
151 };
152 
tegra_xusb_pad_init(struct tegra_xusb_pad * pad,struct tegra_xusb_padctl * padctl,struct device_node * np)153 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
154 			struct tegra_xusb_padctl *padctl,
155 			struct device_node *np)
156 {
157 	int err;
158 
159 	device_initialize(&pad->dev);
160 	INIT_LIST_HEAD(&pad->list);
161 	pad->dev.parent = padctl->dev;
162 	pad->dev.type = &tegra_xusb_pad_type;
163 	pad->dev.of_node = np;
164 	pad->padctl = padctl;
165 
166 	err = dev_set_name(&pad->dev, "%s", pad->soc->name);
167 	if (err < 0)
168 		goto unregister;
169 
170 	err = device_add(&pad->dev);
171 	if (err < 0)
172 		goto unregister;
173 
174 	return 0;
175 
176 unregister:
177 	device_unregister(&pad->dev);
178 	return err;
179 }
180 
tegra_xusb_pad_register(struct tegra_xusb_pad * pad,const struct phy_ops * ops)181 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
182 			    const struct phy_ops *ops)
183 {
184 	struct device_node *children;
185 	struct phy *lane;
186 	unsigned int i;
187 	int err;
188 
189 	children = of_get_child_by_name(pad->dev.of_node, "lanes");
190 	if (!children)
191 		return -ENODEV;
192 
193 	pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
194 				  GFP_KERNEL);
195 	if (!pad->lanes) {
196 		of_node_put(children);
197 		return -ENOMEM;
198 	}
199 
200 	for (i = 0; i < pad->soc->num_lanes; i++) {
201 		struct device_node *np = tegra_xusb_pad_find_phy_node(pad, i);
202 		struct tegra_xusb_lane *lane;
203 
204 		/* skip disabled lanes */
205 		if (!np || !of_device_is_available(np)) {
206 			of_node_put(np);
207 			continue;
208 		}
209 
210 		pad->lanes[i] = phy_create(&pad->dev, np, ops);
211 		if (IS_ERR(pad->lanes[i])) {
212 			err = PTR_ERR(pad->lanes[i]);
213 			of_node_put(np);
214 			goto remove;
215 		}
216 
217 		lane = pad->ops->probe(pad, np, i);
218 		if (IS_ERR(lane)) {
219 			phy_destroy(pad->lanes[i]);
220 			err = PTR_ERR(lane);
221 			goto remove;
222 		}
223 
224 		list_add_tail(&lane->list, &pad->padctl->lanes);
225 		phy_set_drvdata(pad->lanes[i], lane);
226 	}
227 
228 	pad->provider = of_phy_provider_register_full(&pad->dev, children,
229 						      tegra_xusb_pad_of_xlate);
230 	if (IS_ERR(pad->provider)) {
231 		err = PTR_ERR(pad->provider);
232 		goto remove;
233 	}
234 
235 	return 0;
236 
237 remove:
238 	while (i--)
239 		tegra_xusb_lane_destroy(pad->lanes[i]);
240 
241 	of_node_put(children);
242 
243 	return err;
244 }
245 
tegra_xusb_pad_unregister(struct tegra_xusb_pad * pad)246 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
247 {
248 	unsigned int i = pad->soc->num_lanes;
249 
250 	of_phy_provider_unregister(pad->provider);
251 
252 	while (i--)
253 		tegra_xusb_lane_destroy(pad->lanes[i]);
254 
255 	device_unregister(&pad->dev);
256 }
257 
258 static struct tegra_xusb_pad *
tegra_xusb_pad_create(struct tegra_xusb_padctl * padctl,const struct tegra_xusb_pad_soc * soc)259 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
260 		      const struct tegra_xusb_pad_soc *soc)
261 {
262 	struct tegra_xusb_pad *pad;
263 	struct device_node *np;
264 	int err;
265 
266 	np = tegra_xusb_find_pad_node(padctl, soc->name);
267 	if (!np || !of_device_is_available(np))
268 		return NULL;
269 
270 	pad = soc->ops->probe(padctl, soc, np);
271 	if (IS_ERR(pad)) {
272 		err = PTR_ERR(pad);
273 		dev_err(padctl->dev, "failed to create pad %s: %d\n",
274 			soc->name, err);
275 		return ERR_PTR(err);
276 	}
277 
278 	/* XXX move this into ->probe() to avoid string comparison */
279 	if (strcmp(soc->name, "pcie") == 0)
280 		padctl->pcie = pad;
281 
282 	if (strcmp(soc->name, "sata") == 0)
283 		padctl->sata = pad;
284 
285 	if (strcmp(soc->name, "usb2") == 0)
286 		padctl->usb2 = pad;
287 
288 	if (strcmp(soc->name, "ulpi") == 0)
289 		padctl->ulpi = pad;
290 
291 	if (strcmp(soc->name, "hsic") == 0)
292 		padctl->hsic = pad;
293 
294 	return pad;
295 }
296 
__tegra_xusb_remove_pads(struct tegra_xusb_padctl * padctl)297 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
298 {
299 	struct tegra_xusb_pad *pad, *tmp;
300 
301 	list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
302 		list_del(&pad->list);
303 		tegra_xusb_pad_unregister(pad);
304 	}
305 }
306 
tegra_xusb_remove_pads(struct tegra_xusb_padctl * padctl)307 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
308 {
309 	mutex_lock(&padctl->lock);
310 	__tegra_xusb_remove_pads(padctl);
311 	mutex_unlock(&padctl->lock);
312 }
313 
tegra_xusb_lane_program(struct tegra_xusb_lane * lane)314 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
315 {
316 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
317 	const struct tegra_xusb_lane_soc *soc = lane->soc;
318 	u32 value;
319 
320 	/* skip single function lanes */
321 	if (soc->num_funcs < 2)
322 		return;
323 
324 	/* choose function */
325 	value = padctl_readl(padctl, soc->offset);
326 	value &= ~(soc->mask << soc->shift);
327 	value |= lane->function << soc->shift;
328 	padctl_writel(padctl, value, soc->offset);
329 }
330 
tegra_xusb_pad_program(struct tegra_xusb_pad * pad)331 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
332 {
333 	unsigned int i;
334 
335 	for (i = 0; i < pad->soc->num_lanes; i++) {
336 		struct tegra_xusb_lane *lane;
337 
338 		if (pad->lanes[i]) {
339 			lane = phy_get_drvdata(pad->lanes[i]);
340 			tegra_xusb_lane_program(lane);
341 		}
342 	}
343 }
344 
tegra_xusb_setup_pads(struct tegra_xusb_padctl * padctl)345 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
346 {
347 	struct tegra_xusb_pad *pad;
348 	unsigned int i;
349 
350 	mutex_lock(&padctl->lock);
351 
352 	for (i = 0; i < padctl->soc->num_pads; i++) {
353 		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
354 		int err;
355 
356 		pad = tegra_xusb_pad_create(padctl, soc);
357 		if (IS_ERR(pad)) {
358 			err = PTR_ERR(pad);
359 			dev_err(padctl->dev, "failed to create pad %s: %d\n",
360 				soc->name, err);
361 			__tegra_xusb_remove_pads(padctl);
362 			mutex_unlock(&padctl->lock);
363 			return err;
364 		}
365 
366 		if (!pad)
367 			continue;
368 
369 		list_add_tail(&pad->list, &padctl->pads);
370 	}
371 
372 	list_for_each_entry(pad, &padctl->pads, list)
373 		tegra_xusb_pad_program(pad);
374 
375 	mutex_unlock(&padctl->lock);
376 	return 0;
377 }
378 
tegra_xusb_lane_check(struct tegra_xusb_lane * lane,const char * function)379 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
380 				  const char *function)
381 {
382 	const char *func = lane->soc->funcs[lane->function];
383 
384 	return strcmp(function, func) == 0;
385 }
386 
tegra_xusb_find_lane(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)387 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
388 					     const char *type,
389 					     unsigned int index)
390 {
391 	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
392 	char *name;
393 
394 	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
395 	if (!name)
396 		return ERR_PTR(-ENOMEM);
397 
398 	list_for_each_entry(lane, &padctl->lanes, list) {
399 		if (strcmp(lane->soc->name, name) == 0) {
400 			hit = lane;
401 			break;
402 		}
403 	}
404 
405 	kfree(name);
406 	return hit;
407 }
408 
409 struct tegra_xusb_lane *
tegra_xusb_port_find_lane(struct tegra_xusb_port * port,const struct tegra_xusb_lane_map * map,const char * function)410 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
411 			  const struct tegra_xusb_lane_map *map,
412 			  const char *function)
413 {
414 	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
415 
416 	for (; map->type; map++) {
417 		if (port->index != map->port)
418 			continue;
419 
420 		lane = tegra_xusb_find_lane(port->padctl, map->type,
421 					    map->index);
422 		if (IS_ERR(lane))
423 			continue;
424 
425 		if (!tegra_xusb_lane_check(lane, function))
426 			continue;
427 
428 		if (!IS_ERR(match))
429 			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
430 				map->type, map->index, match->soc->name);
431 		else
432 			match = lane;
433 	}
434 
435 	return match;
436 }
437 
438 static struct device_node *
tegra_xusb_find_port_node(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)439 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
440 			  unsigned int index)
441 {
442 	struct device_node *ports, *np;
443 	char *name;
444 
445 	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
446 	if (!ports)
447 		return NULL;
448 
449 	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
450 	if (!name) {
451 		of_node_put(ports);
452 		return NULL;
453 	}
454 	np = of_get_child_by_name(ports, name);
455 	kfree(name);
456 	of_node_put(ports);
457 
458 	return np;
459 }
460 
461 struct tegra_xusb_port *
tegra_xusb_find_port(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)462 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
463 		     unsigned int index)
464 {
465 	struct tegra_xusb_port *port;
466 	struct device_node *np;
467 
468 	np = tegra_xusb_find_port_node(padctl, type, index);
469 	if (!np)
470 		return NULL;
471 
472 	list_for_each_entry(port, &padctl->ports, list) {
473 		if (np == port->dev.of_node) {
474 			of_node_put(np);
475 			return port;
476 		}
477 	}
478 
479 	of_node_put(np);
480 
481 	return NULL;
482 }
483 
484 struct tegra_xusb_usb2_port *
tegra_xusb_find_usb2_port(struct tegra_xusb_padctl * padctl,unsigned int index)485 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
486 {
487 	struct tegra_xusb_port *port;
488 
489 	port = tegra_xusb_find_port(padctl, "usb2", index);
490 	if (port)
491 		return to_usb2_port(port);
492 
493 	return NULL;
494 }
495 
496 struct tegra_xusb_usb3_port *
tegra_xusb_find_usb3_port(struct tegra_xusb_padctl * padctl,unsigned int index)497 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
498 {
499 	struct tegra_xusb_port *port;
500 
501 	port = tegra_xusb_find_port(padctl, "usb3", index);
502 	if (port)
503 		return to_usb3_port(port);
504 
505 	return NULL;
506 }
507 
tegra_xusb_port_release(struct device * dev)508 static void tegra_xusb_port_release(struct device *dev)
509 {
510 	struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
511 
512 	if (port->ops->release)
513 		port->ops->release(port);
514 }
515 
516 static struct device_type tegra_xusb_port_type = {
517 	.release = tegra_xusb_port_release,
518 };
519 
tegra_xusb_port_init(struct tegra_xusb_port * port,struct tegra_xusb_padctl * padctl,struct device_node * np,const char * name,unsigned int index)520 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
521 				struct tegra_xusb_padctl *padctl,
522 				struct device_node *np,
523 				const char *name,
524 				unsigned int index)
525 {
526 	int err;
527 
528 	INIT_LIST_HEAD(&port->list);
529 	port->padctl = padctl;
530 	port->index = index;
531 
532 	device_initialize(&port->dev);
533 	port->dev.type = &tegra_xusb_port_type;
534 	port->dev.of_node = of_node_get(np);
535 	port->dev.parent = padctl->dev;
536 
537 	err = dev_set_name(&port->dev, "%s-%u", name, index);
538 	if (err < 0)
539 		goto unregister;
540 
541 	err = device_add(&port->dev);
542 	if (err < 0)
543 		goto unregister;
544 
545 	return 0;
546 
547 unregister:
548 	device_unregister(&port->dev);
549 	return err;
550 }
551 
tegra_xusb_port_unregister(struct tegra_xusb_port * port)552 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
553 {
554 	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
555 		of_platform_depopulate(&port->dev);
556 		usb_role_switch_unregister(port->usb_role_sw);
557 		cancel_work_sync(&port->usb_phy_work);
558 		usb_remove_phy(&port->usb_phy);
559 	}
560 
561 	if (port->ops->remove)
562 		port->ops->remove(port);
563 
564 	device_unregister(&port->dev);
565 }
566 
567 static const char *const modes[] = {
568 	[USB_DR_MODE_UNKNOWN] = "",
569 	[USB_DR_MODE_HOST] = "host",
570 	[USB_DR_MODE_PERIPHERAL] = "peripheral",
571 	[USB_DR_MODE_OTG] = "otg",
572 };
573 
574 static const char * const usb_roles[] = {
575 	[USB_ROLE_NONE]		= "none",
576 	[USB_ROLE_HOST]		= "host",
577 	[USB_ROLE_DEVICE]	= "device",
578 };
579 
to_usb_phy_event(enum usb_role role)580 static enum usb_phy_events to_usb_phy_event(enum usb_role role)
581 {
582 	switch (role) {
583 	case USB_ROLE_DEVICE:
584 		return USB_EVENT_VBUS;
585 
586 	case USB_ROLE_HOST:
587 		return USB_EVENT_ID;
588 
589 	default:
590 		return USB_EVENT_NONE;
591 	}
592 }
593 
tegra_xusb_usb_phy_work(struct work_struct * work)594 static void tegra_xusb_usb_phy_work(struct work_struct *work)
595 {
596 	struct tegra_xusb_port *port = container_of(work,
597 						    struct tegra_xusb_port,
598 						    usb_phy_work);
599 	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
600 
601 	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
602 
603 	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
604 		usb_roles[role]);
605 
606 	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
607 }
608 
tegra_xusb_role_sw_set(struct usb_role_switch * sw,enum usb_role role)609 static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
610 				  enum usb_role role)
611 {
612 	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
613 
614 	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
615 
616 	schedule_work(&port->usb_phy_work);
617 
618 	return 0;
619 }
620 
tegra_xusb_set_peripheral(struct usb_otg * otg,struct usb_gadget * gadget)621 static int tegra_xusb_set_peripheral(struct usb_otg *otg,
622 				     struct usb_gadget *gadget)
623 {
624 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
625 						    struct tegra_xusb_port,
626 						    usb_phy);
627 
628 	if (gadget != NULL)
629 		schedule_work(&port->usb_phy_work);
630 
631 	return 0;
632 }
633 
tegra_xusb_set_host(struct usb_otg * otg,struct usb_bus * host)634 static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
635 {
636 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
637 						    struct tegra_xusb_port,
638 						    usb_phy);
639 
640 	if (host != NULL)
641 		schedule_work(&port->usb_phy_work);
642 
643 	return 0;
644 }
645 
646 
tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port * port)647 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
648 {
649 	struct tegra_xusb_lane *lane;
650 	struct usb_role_switch_desc role_sx_desc = {
651 		.fwnode = dev_fwnode(&port->dev),
652 		.set = tegra_xusb_role_sw_set,
653 	};
654 	int err = 0;
655 
656 	/*
657 	 * USB role switch driver needs parent driver owner info. This is a
658 	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
659 	 * where an optimal solution is possible with changes to USB role
660 	 * switch driver.
661 	 */
662 	port->dev.driver = devm_kzalloc(&port->dev,
663 					sizeof(struct device_driver),
664 					GFP_KERNEL);
665 	port->dev.driver->owner	 = THIS_MODULE;
666 
667 	port->usb_role_sw = usb_role_switch_register(&port->dev,
668 						     &role_sx_desc);
669 	if (IS_ERR(port->usb_role_sw)) {
670 		err = PTR_ERR(port->usb_role_sw);
671 		dev_err(&port->dev, "failed to register USB role switch: %d",
672 			err);
673 		return err;
674 	}
675 
676 	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
677 	usb_role_switch_set_drvdata(port->usb_role_sw, port);
678 
679 	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
680 					 GFP_KERNEL);
681 	if (!port->usb_phy.otg)
682 		return -ENOMEM;
683 
684 	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
685 
686 	/*
687 	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
688 	 * reference to retrieve usb-phy details.
689 	 */
690 	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
691 	port->usb_phy.dev->driver = port->dev.driver;
692 	port->usb_phy.otg->usb_phy = &port->usb_phy;
693 	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
694 	port->usb_phy.otg->set_host = tegra_xusb_set_host;
695 
696 	err = usb_add_phy_dev(&port->usb_phy);
697 	if (err < 0) {
698 		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
699 		return err;
700 	}
701 
702 	/* populate connector entry */
703 	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
704 
705 	return err;
706 }
707 
tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port * usb2)708 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
709 {
710 	struct tegra_xusb_port *port = &usb2->base;
711 	struct device_node *np = port->dev.of_node;
712 	const char *mode;
713 	int err;
714 
715 	usb2->internal = of_property_read_bool(np, "nvidia,internal");
716 
717 	if (!of_property_read_string(np, "mode", &mode)) {
718 		int err = match_string(modes, ARRAY_SIZE(modes), mode);
719 		if (err < 0) {
720 			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
721 				mode);
722 			usb2->mode = USB_DR_MODE_UNKNOWN;
723 		} else {
724 			usb2->mode = err;
725 		}
726 	} else {
727 		usb2->mode = USB_DR_MODE_HOST;
728 	}
729 
730 	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
731 	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
732 	    usb2->mode == USB_DR_MODE_OTG) {
733 		if (of_property_read_bool(np, "usb-role-switch")) {
734 			err = tegra_xusb_setup_usb_role_switch(port);
735 			if (err < 0)
736 				return err;
737 		} else {
738 			dev_err(&port->dev, "usb-role-switch not found for %s mode",
739 				modes[usb2->mode]);
740 			return -EINVAL;
741 		}
742 	}
743 
744 	usb2->supply = regulator_get(&port->dev, "vbus");
745 	return PTR_ERR_OR_ZERO(usb2->supply);
746 }
747 
tegra_xusb_add_usb2_port(struct tegra_xusb_padctl * padctl,unsigned int index)748 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
749 				    unsigned int index)
750 {
751 	struct tegra_xusb_usb2_port *usb2;
752 	struct device_node *np;
753 	int err = 0;
754 
755 	/*
756 	 * USB2 ports don't require additional properties, but if the port is
757 	 * marked as disabled there is no reason to register it.
758 	 */
759 	np = tegra_xusb_find_port_node(padctl, "usb2", index);
760 	if (!np || !of_device_is_available(np))
761 		goto out;
762 
763 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
764 	if (!usb2) {
765 		err = -ENOMEM;
766 		goto out;
767 	}
768 
769 	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
770 	if (err < 0)
771 		goto out;
772 
773 	usb2->base.ops = padctl->soc->ports.usb2.ops;
774 
775 	usb2->base.lane = usb2->base.ops->map(&usb2->base);
776 	if (IS_ERR(usb2->base.lane)) {
777 		err = PTR_ERR(usb2->base.lane);
778 		tegra_xusb_port_unregister(&usb2->base);
779 		goto out;
780 	}
781 
782 	err = tegra_xusb_usb2_port_parse_dt(usb2);
783 	if (err < 0) {
784 		tegra_xusb_port_unregister(&usb2->base);
785 		goto out;
786 	}
787 
788 	list_add_tail(&usb2->base.list, &padctl->ports);
789 
790 out:
791 	of_node_put(np);
792 	return err;
793 }
794 
tegra_xusb_usb2_port_release(struct tegra_xusb_port * port)795 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
796 {
797 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
798 
799 	kfree(usb2);
800 }
801 
tegra_xusb_usb2_port_remove(struct tegra_xusb_port * port)802 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
803 {
804 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
805 
806 	regulator_put(usb2->supply);
807 }
808 
tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port * ulpi)809 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
810 {
811 	struct tegra_xusb_port *port = &ulpi->base;
812 	struct device_node *np = port->dev.of_node;
813 
814 	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
815 
816 	return 0;
817 }
818 
tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl * padctl,unsigned int index)819 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
820 				    unsigned int index)
821 {
822 	struct tegra_xusb_ulpi_port *ulpi;
823 	struct device_node *np;
824 	int err = 0;
825 
826 	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
827 	if (!np || !of_device_is_available(np))
828 		goto out;
829 
830 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
831 	if (!ulpi) {
832 		err = -ENOMEM;
833 		goto out;
834 	}
835 
836 	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
837 	if (err < 0)
838 		goto out;
839 
840 	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
841 
842 	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
843 	if (IS_ERR(ulpi->base.lane)) {
844 		err = PTR_ERR(ulpi->base.lane);
845 		tegra_xusb_port_unregister(&ulpi->base);
846 		goto out;
847 	}
848 
849 	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
850 	if (err < 0) {
851 		tegra_xusb_port_unregister(&ulpi->base);
852 		goto out;
853 	}
854 
855 	list_add_tail(&ulpi->base.list, &padctl->ports);
856 
857 out:
858 	of_node_put(np);
859 	return err;
860 }
861 
tegra_xusb_ulpi_port_release(struct tegra_xusb_port * port)862 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
863 {
864 	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
865 
866 	kfree(ulpi);
867 }
868 
tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port * hsic)869 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
870 {
871 	/* XXX */
872 	return 0;
873 }
874 
tegra_xusb_add_hsic_port(struct tegra_xusb_padctl * padctl,unsigned int index)875 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
876 				    unsigned int index)
877 {
878 	struct tegra_xusb_hsic_port *hsic;
879 	struct device_node *np;
880 	int err = 0;
881 
882 	np = tegra_xusb_find_port_node(padctl, "hsic", index);
883 	if (!np || !of_device_is_available(np))
884 		goto out;
885 
886 	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
887 	if (!hsic) {
888 		err = -ENOMEM;
889 		goto out;
890 	}
891 
892 	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
893 	if (err < 0)
894 		goto out;
895 
896 	hsic->base.ops = padctl->soc->ports.hsic.ops;
897 
898 	hsic->base.lane = hsic->base.ops->map(&hsic->base);
899 	if (IS_ERR(hsic->base.lane)) {
900 		err = PTR_ERR(hsic->base.lane);
901 		goto out;
902 	}
903 
904 	err = tegra_xusb_hsic_port_parse_dt(hsic);
905 	if (err < 0) {
906 		tegra_xusb_port_unregister(&hsic->base);
907 		goto out;
908 	}
909 
910 	list_add_tail(&hsic->base.list, &padctl->ports);
911 
912 out:
913 	of_node_put(np);
914 	return err;
915 }
916 
tegra_xusb_hsic_port_release(struct tegra_xusb_port * port)917 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
918 {
919 	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
920 
921 	kfree(hsic);
922 }
923 
tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port * usb3)924 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
925 {
926 	struct tegra_xusb_port *port = &usb3->base;
927 	struct device_node *np = port->dev.of_node;
928 	enum usb_device_speed maximum_speed;
929 	u32 value;
930 	int err;
931 
932 	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
933 	if (err < 0) {
934 		dev_err(&port->dev, "failed to read port: %d\n", err);
935 		return err;
936 	}
937 
938 	usb3->port = value;
939 
940 	usb3->internal = of_property_read_bool(np, "nvidia,internal");
941 
942 	if (device_property_present(&port->dev, "maximum-speed")) {
943 		maximum_speed =  usb_get_maximum_speed(&port->dev);
944 		if (maximum_speed == USB_SPEED_SUPER)
945 			usb3->disable_gen2 = true;
946 		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
947 			usb3->disable_gen2 = false;
948 		else
949 			return -EINVAL;
950 	}
951 
952 	usb3->supply = regulator_get(&port->dev, "vbus");
953 	return PTR_ERR_OR_ZERO(usb3->supply);
954 }
955 
tegra_xusb_add_usb3_port(struct tegra_xusb_padctl * padctl,unsigned int index)956 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
957 				    unsigned int index)
958 {
959 	struct tegra_xusb_usb3_port *usb3;
960 	struct device_node *np;
961 	int err = 0;
962 
963 	/*
964 	 * If there is no supplemental configuration in the device tree the
965 	 * port is unusable. But it is valid to configure only a single port,
966 	 * hence return 0 instead of an error to allow ports to be optional.
967 	 */
968 	np = tegra_xusb_find_port_node(padctl, "usb3", index);
969 	if (!np || !of_device_is_available(np))
970 		goto out;
971 
972 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
973 	if (!usb3) {
974 		err = -ENOMEM;
975 		goto out;
976 	}
977 
978 	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
979 	if (err < 0)
980 		goto out;
981 
982 	usb3->base.ops = padctl->soc->ports.usb3.ops;
983 
984 	usb3->base.lane = usb3->base.ops->map(&usb3->base);
985 	if (IS_ERR(usb3->base.lane)) {
986 		err = PTR_ERR(usb3->base.lane);
987 		goto out;
988 	}
989 
990 	err = tegra_xusb_usb3_port_parse_dt(usb3);
991 	if (err < 0) {
992 		tegra_xusb_port_unregister(&usb3->base);
993 		goto out;
994 	}
995 
996 	list_add_tail(&usb3->base.list, &padctl->ports);
997 
998 out:
999 	of_node_put(np);
1000 	return err;
1001 }
1002 
tegra_xusb_usb3_port_release(struct tegra_xusb_port * port)1003 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1004 {
1005 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1006 
1007 	kfree(usb3);
1008 }
1009 
tegra_xusb_usb3_port_remove(struct tegra_xusb_port * port)1010 void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1011 {
1012 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1013 
1014 	regulator_put(usb3->supply);
1015 }
1016 
__tegra_xusb_remove_ports(struct tegra_xusb_padctl * padctl)1017 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1018 {
1019 	struct tegra_xusb_port *port, *tmp;
1020 
1021 	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1022 		list_del(&port->list);
1023 		tegra_xusb_port_unregister(port);
1024 	}
1025 }
1026 
tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl * padctl)1027 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1028 {
1029 	struct device_node *np;
1030 	unsigned int i;
1031 
1032 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1033 		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1034 		if (!np || !of_device_is_available(np))
1035 			return i;
1036 	}
1037 
1038 	return -ENODEV;
1039 }
1040 
tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port * usb2)1041 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1042 {
1043 	unsigned int i;
1044 	struct tegra_xusb_usb3_port *usb3;
1045 	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1046 
1047 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1048 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1049 		if (usb3 && usb3->port == usb2->base.index)
1050 			return true;
1051 	}
1052 
1053 	return false;
1054 }
1055 
tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port * usb2)1056 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1057 {
1058 	int fake;
1059 
1060 	/* Disable usb3_port_fake usage by default and assign if needed */
1061 	usb2->usb3_port_fake = -1;
1062 
1063 	if ((usb2->mode == USB_DR_MODE_OTG ||
1064 	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1065 		!tegra_xusb_port_is_companion(usb2)) {
1066 		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1067 		if (fake < 0) {
1068 			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1069 			return -ENODEV;
1070 		}
1071 
1072 		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1073 		usb2->usb3_port_fake = fake;
1074 	}
1075 
1076 	return 0;
1077 }
1078 
tegra_xusb_setup_ports(struct tegra_xusb_padctl * padctl)1079 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1080 {
1081 	struct tegra_xusb_port *port;
1082 	struct tegra_xusb_usb2_port *usb2;
1083 	unsigned int i;
1084 	int err = 0;
1085 
1086 	mutex_lock(&padctl->lock);
1087 
1088 	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1089 		err = tegra_xusb_add_usb2_port(padctl, i);
1090 		if (err < 0)
1091 			goto remove_ports;
1092 	}
1093 
1094 	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1095 		err = tegra_xusb_add_ulpi_port(padctl, i);
1096 		if (err < 0)
1097 			goto remove_ports;
1098 	}
1099 
1100 	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1101 		err = tegra_xusb_add_hsic_port(padctl, i);
1102 		if (err < 0)
1103 			goto remove_ports;
1104 	}
1105 
1106 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1107 		err = tegra_xusb_add_usb3_port(padctl, i);
1108 		if (err < 0)
1109 			goto remove_ports;
1110 	}
1111 
1112 	if (padctl->soc->need_fake_usb3_port) {
1113 		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1114 			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1115 			if (!usb2)
1116 				continue;
1117 
1118 			err = tegra_xusb_update_usb3_fake_port(usb2);
1119 			if (err < 0)
1120 				goto remove_ports;
1121 		}
1122 	}
1123 
1124 	list_for_each_entry(port, &padctl->ports, list) {
1125 		err = port->ops->enable(port);
1126 		if (err < 0)
1127 			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1128 				dev_name(&port->dev), err);
1129 	}
1130 
1131 	goto unlock;
1132 
1133 remove_ports:
1134 	__tegra_xusb_remove_ports(padctl);
1135 unlock:
1136 	mutex_unlock(&padctl->lock);
1137 	return err;
1138 }
1139 
tegra_xusb_remove_ports(struct tegra_xusb_padctl * padctl)1140 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1141 {
1142 	mutex_lock(&padctl->lock);
1143 	__tegra_xusb_remove_ports(padctl);
1144 	mutex_unlock(&padctl->lock);
1145 }
1146 
tegra_xusb_padctl_probe(struct platform_device * pdev)1147 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1148 {
1149 	struct device_node *np = pdev->dev.of_node;
1150 	const struct tegra_xusb_padctl_soc *soc;
1151 	struct tegra_xusb_padctl *padctl;
1152 	const struct of_device_id *match;
1153 	struct resource *res;
1154 	int err;
1155 
1156 	/* for backwards compatibility with old device trees */
1157 	np = of_get_child_by_name(np, "pads");
1158 	if (!np) {
1159 		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1160 		return tegra_xusb_padctl_legacy_probe(pdev);
1161 	}
1162 
1163 	of_node_put(np);
1164 
1165 	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1166 	soc = match->data;
1167 
1168 	padctl = soc->ops->probe(&pdev->dev, soc);
1169 	if (IS_ERR(padctl))
1170 		return PTR_ERR(padctl);
1171 
1172 	platform_set_drvdata(pdev, padctl);
1173 	INIT_LIST_HEAD(&padctl->ports);
1174 	INIT_LIST_HEAD(&padctl->lanes);
1175 	INIT_LIST_HEAD(&padctl->pads);
1176 	mutex_init(&padctl->lock);
1177 
1178 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1179 	padctl->regs = devm_ioremap_resource(&pdev->dev, res);
1180 	if (IS_ERR(padctl->regs)) {
1181 		err = PTR_ERR(padctl->regs);
1182 		goto remove;
1183 	}
1184 
1185 	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1186 	if (IS_ERR(padctl->rst)) {
1187 		err = PTR_ERR(padctl->rst);
1188 		goto remove;
1189 	}
1190 
1191 	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1192 					sizeof(*padctl->supplies), GFP_KERNEL);
1193 	if (!padctl->supplies) {
1194 		err = -ENOMEM;
1195 		goto remove;
1196 	}
1197 
1198 	regulator_bulk_set_supply_names(padctl->supplies,
1199 					padctl->soc->supply_names,
1200 					padctl->soc->num_supplies);
1201 
1202 	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1203 				      padctl->supplies);
1204 	if (err < 0) {
1205 		dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1206 		goto remove;
1207 	}
1208 
1209 	err = reset_control_deassert(padctl->rst);
1210 	if (err < 0)
1211 		goto remove;
1212 
1213 	err = regulator_bulk_enable(padctl->soc->num_supplies,
1214 				    padctl->supplies);
1215 	if (err < 0) {
1216 		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1217 		goto reset;
1218 	}
1219 
1220 	err = tegra_xusb_setup_pads(padctl);
1221 	if (err < 0) {
1222 		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1223 		goto power_down;
1224 	}
1225 
1226 	err = tegra_xusb_setup_ports(padctl);
1227 	if (err) {
1228 		const char *level = KERN_ERR;
1229 
1230 		if (err == -EPROBE_DEFER)
1231 			level = KERN_DEBUG;
1232 
1233 		dev_printk(level, &pdev->dev,
1234 			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1235 		goto remove_pads;
1236 	}
1237 
1238 	return 0;
1239 
1240 remove_pads:
1241 	tegra_xusb_remove_pads(padctl);
1242 power_down:
1243 	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1244 reset:
1245 	reset_control_assert(padctl->rst);
1246 remove:
1247 	platform_set_drvdata(pdev, NULL);
1248 	soc->ops->remove(padctl);
1249 	return err;
1250 }
1251 
tegra_xusb_padctl_remove(struct platform_device * pdev)1252 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1253 {
1254 	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1255 	int err;
1256 
1257 	tegra_xusb_remove_ports(padctl);
1258 	tegra_xusb_remove_pads(padctl);
1259 
1260 	err = regulator_bulk_disable(padctl->soc->num_supplies,
1261 				     padctl->supplies);
1262 	if (err < 0)
1263 		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1264 
1265 	err = reset_control_assert(padctl->rst);
1266 	if (err < 0)
1267 		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1268 
1269 	padctl->soc->ops->remove(padctl);
1270 
1271 	return err;
1272 }
1273 
1274 static struct platform_driver tegra_xusb_padctl_driver = {
1275 	.driver = {
1276 		.name = "tegra-xusb-padctl",
1277 		.of_match_table = tegra_xusb_padctl_of_match,
1278 	},
1279 	.probe = tegra_xusb_padctl_probe,
1280 	.remove = tegra_xusb_padctl_remove,
1281 };
1282 module_platform_driver(tegra_xusb_padctl_driver);
1283 
tegra_xusb_padctl_get(struct device * dev)1284 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1285 {
1286 	struct tegra_xusb_padctl *padctl;
1287 	struct platform_device *pdev;
1288 	struct device_node *np;
1289 
1290 	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1291 	if (!np)
1292 		return ERR_PTR(-EINVAL);
1293 
1294 	/*
1295 	 * This is slightly ugly. A better implementation would be to keep a
1296 	 * registry of pad controllers, but since there will almost certainly
1297 	 * only ever be one per SoC that would be a little overkill.
1298 	 */
1299 	pdev = of_find_device_by_node(np);
1300 	if (!pdev) {
1301 		of_node_put(np);
1302 		return ERR_PTR(-ENODEV);
1303 	}
1304 
1305 	of_node_put(np);
1306 
1307 	padctl = platform_get_drvdata(pdev);
1308 	if (!padctl) {
1309 		put_device(&pdev->dev);
1310 		return ERR_PTR(-EPROBE_DEFER);
1311 	}
1312 
1313 	return padctl;
1314 }
1315 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1316 
tegra_xusb_padctl_put(struct tegra_xusb_padctl * padctl)1317 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1318 {
1319 	if (padctl)
1320 		put_device(padctl->dev);
1321 }
1322 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1323 
tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl * padctl,unsigned int port)1324 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1325 					unsigned int port)
1326 {
1327 	if (padctl->soc->ops->usb3_save_context)
1328 		return padctl->soc->ops->usb3_save_context(padctl, port);
1329 
1330 	return -ENOSYS;
1331 }
1332 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1333 
tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl * padctl,unsigned int port,bool idle)1334 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1335 				    unsigned int port, bool idle)
1336 {
1337 	if (padctl->soc->ops->hsic_set_idle)
1338 		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1339 
1340 	return -ENOSYS;
1341 }
1342 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1343 
tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl * padctl,unsigned int port,bool enable)1344 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1345 					   unsigned int port, bool enable)
1346 {
1347 	if (padctl->soc->ops->usb3_set_lfps_detect)
1348 		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1349 							      enable);
1350 
1351 	return -ENOSYS;
1352 }
1353 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1354 
tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl * padctl,bool val)1355 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1356 							bool val)
1357 {
1358 	if (padctl->soc->ops->vbus_override)
1359 		return padctl->soc->ops->vbus_override(padctl, val);
1360 
1361 	return -ENOTSUPP;
1362 }
1363 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1364 
tegra_phy_xusb_utmi_port_reset(struct phy * phy)1365 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1366 {
1367 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1368 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1369 
1370 	if (padctl->soc->ops->utmi_port_reset)
1371 		return padctl->soc->ops->utmi_port_reset(phy);
1372 
1373 	return -ENOTSUPP;
1374 }
1375 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1376 
tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl * padctl,unsigned int port)1377 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1378 				    unsigned int port)
1379 {
1380 	struct tegra_xusb_usb2_port *usb2;
1381 	struct tegra_xusb_usb3_port *usb3;
1382 	int i;
1383 
1384 	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1385 	if (!usb2)
1386 		return -EINVAL;
1387 
1388 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1389 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1390 		if (usb3 && usb3->port == usb2->base.index)
1391 			return usb3->base.index;
1392 	}
1393 
1394 	return -ENODEV;
1395 }
1396 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1397 
1398 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1399 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1400 MODULE_LICENSE("GPL v2");
1401