• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014-2020, 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 const 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 	if (lane->pad->ops->iddq_enable)
325 		lane->pad->ops->iddq_enable(lane);
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 	if (lane->pad->ops->iddq_disable)
334 		lane->pad->ops->iddq_disable(lane);
335 }
336 
tegra_xusb_pad_program(struct tegra_xusb_pad * pad)337 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
338 {
339 	unsigned int i;
340 
341 	for (i = 0; i < pad->soc->num_lanes; i++) {
342 		struct tegra_xusb_lane *lane;
343 
344 		if (pad->lanes[i]) {
345 			lane = phy_get_drvdata(pad->lanes[i]);
346 			tegra_xusb_lane_program(lane);
347 		}
348 	}
349 }
350 
tegra_xusb_setup_pads(struct tegra_xusb_padctl * padctl)351 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
352 {
353 	struct tegra_xusb_pad *pad;
354 	unsigned int i;
355 
356 	mutex_lock(&padctl->lock);
357 
358 	for (i = 0; i < padctl->soc->num_pads; i++) {
359 		const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
360 		int err;
361 
362 		pad = tegra_xusb_pad_create(padctl, soc);
363 		if (IS_ERR(pad)) {
364 			err = PTR_ERR(pad);
365 			dev_err(padctl->dev, "failed to create pad %s: %d\n",
366 				soc->name, err);
367 			__tegra_xusb_remove_pads(padctl);
368 			mutex_unlock(&padctl->lock);
369 			return err;
370 		}
371 
372 		if (!pad)
373 			continue;
374 
375 		list_add_tail(&pad->list, &padctl->pads);
376 	}
377 
378 	list_for_each_entry(pad, &padctl->pads, list)
379 		tegra_xusb_pad_program(pad);
380 
381 	mutex_unlock(&padctl->lock);
382 	return 0;
383 }
384 
tegra_xusb_lane_check(struct tegra_xusb_lane * lane,const char * function)385 bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
386 				  const char *function)
387 {
388 	const char *func = lane->soc->funcs[lane->function];
389 
390 	return strcmp(function, func) == 0;
391 }
392 
tegra_xusb_find_lane(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)393 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
394 					     const char *type,
395 					     unsigned int index)
396 {
397 	struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
398 	char *name;
399 
400 	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
401 	if (!name)
402 		return ERR_PTR(-ENOMEM);
403 
404 	list_for_each_entry(lane, &padctl->lanes, list) {
405 		if (strcmp(lane->soc->name, name) == 0) {
406 			hit = lane;
407 			break;
408 		}
409 	}
410 
411 	kfree(name);
412 	return hit;
413 }
414 
415 struct tegra_xusb_lane *
tegra_xusb_port_find_lane(struct tegra_xusb_port * port,const struct tegra_xusb_lane_map * map,const char * function)416 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
417 			  const struct tegra_xusb_lane_map *map,
418 			  const char *function)
419 {
420 	struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
421 
422 	for (; map->type; map++) {
423 		if (port->index != map->port)
424 			continue;
425 
426 		lane = tegra_xusb_find_lane(port->padctl, map->type,
427 					    map->index);
428 		if (IS_ERR(lane))
429 			continue;
430 
431 		if (!tegra_xusb_lane_check(lane, function))
432 			continue;
433 
434 		if (!IS_ERR(match))
435 			dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
436 				map->type, map->index, match->soc->name);
437 		else
438 			match = lane;
439 	}
440 
441 	return match;
442 }
443 
444 static struct device_node *
tegra_xusb_find_port_node(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)445 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
446 			  unsigned int index)
447 {
448 	struct device_node *ports, *np;
449 	char *name;
450 
451 	ports = of_get_child_by_name(padctl->dev->of_node, "ports");
452 	if (!ports)
453 		return NULL;
454 
455 	name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
456 	if (!name) {
457 		of_node_put(ports);
458 		return ERR_PTR(-ENOMEM);
459 	}
460 	np = of_get_child_by_name(ports, name);
461 	kfree(name);
462 	of_node_put(ports);
463 
464 	return np;
465 }
466 
467 struct tegra_xusb_port *
tegra_xusb_find_port(struct tegra_xusb_padctl * padctl,const char * type,unsigned int index)468 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
469 		     unsigned int index)
470 {
471 	struct tegra_xusb_port *port;
472 	struct device_node *np;
473 
474 	np = tegra_xusb_find_port_node(padctl, type, index);
475 	if (!np)
476 		return NULL;
477 
478 	list_for_each_entry(port, &padctl->ports, list) {
479 		if (np == port->dev.of_node) {
480 			of_node_put(np);
481 			return port;
482 		}
483 	}
484 
485 	of_node_put(np);
486 
487 	return NULL;
488 }
489 
490 struct tegra_xusb_usb2_port *
tegra_xusb_find_usb2_port(struct tegra_xusb_padctl * padctl,unsigned int index)491 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
492 {
493 	struct tegra_xusb_port *port;
494 
495 	port = tegra_xusb_find_port(padctl, "usb2", index);
496 	if (port)
497 		return to_usb2_port(port);
498 
499 	return NULL;
500 }
501 
502 struct tegra_xusb_usb3_port *
tegra_xusb_find_usb3_port(struct tegra_xusb_padctl * padctl,unsigned int index)503 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
504 {
505 	struct tegra_xusb_port *port;
506 
507 	port = tegra_xusb_find_port(padctl, "usb3", index);
508 	if (port)
509 		return to_usb3_port(port);
510 
511 	return NULL;
512 }
513 
tegra_xusb_port_release(struct device * dev)514 static void tegra_xusb_port_release(struct device *dev)
515 {
516 	struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
517 
518 	if (port->ops->release)
519 		port->ops->release(port);
520 }
521 
522 static const struct device_type tegra_xusb_port_type = {
523 	.release = tegra_xusb_port_release,
524 };
525 
tegra_xusb_port_init(struct tegra_xusb_port * port,struct tegra_xusb_padctl * padctl,struct device_node * np,const char * name,unsigned int index)526 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
527 				struct tegra_xusb_padctl *padctl,
528 				struct device_node *np,
529 				const char *name,
530 				unsigned int index)
531 {
532 	int err;
533 
534 	INIT_LIST_HEAD(&port->list);
535 	port->padctl = padctl;
536 	port->index = index;
537 
538 	device_initialize(&port->dev);
539 	port->dev.type = &tegra_xusb_port_type;
540 	port->dev.of_node = of_node_get(np);
541 	port->dev.parent = padctl->dev;
542 
543 	err = dev_set_name(&port->dev, "%s-%u", name, index);
544 	if (err < 0)
545 		goto unregister;
546 
547 	err = device_add(&port->dev);
548 	if (err < 0)
549 		goto unregister;
550 
551 	return 0;
552 
553 unregister:
554 	device_unregister(&port->dev);
555 	return err;
556 }
557 
tegra_xusb_port_unregister(struct tegra_xusb_port * port)558 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
559 {
560 	if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
561 		of_platform_depopulate(&port->dev);
562 		usb_role_switch_unregister(port->usb_role_sw);
563 		cancel_work_sync(&port->usb_phy_work);
564 		usb_remove_phy(&port->usb_phy);
565 		port->usb_phy.dev->driver = NULL;
566 	}
567 
568 	if (port->ops->remove)
569 		port->ops->remove(port);
570 
571 	device_unregister(&port->dev);
572 }
573 
574 static const char *const modes[] = {
575 	[USB_DR_MODE_UNKNOWN] = "",
576 	[USB_DR_MODE_HOST] = "host",
577 	[USB_DR_MODE_PERIPHERAL] = "peripheral",
578 	[USB_DR_MODE_OTG] = "otg",
579 };
580 
581 static const char * const usb_roles[] = {
582 	[USB_ROLE_NONE]		= "none",
583 	[USB_ROLE_HOST]		= "host",
584 	[USB_ROLE_DEVICE]	= "device",
585 };
586 
to_usb_phy_event(enum usb_role role)587 static enum usb_phy_events to_usb_phy_event(enum usb_role role)
588 {
589 	switch (role) {
590 	case USB_ROLE_DEVICE:
591 		return USB_EVENT_VBUS;
592 
593 	case USB_ROLE_HOST:
594 		return USB_EVENT_ID;
595 
596 	default:
597 		return USB_EVENT_NONE;
598 	}
599 }
600 
tegra_xusb_usb_phy_work(struct work_struct * work)601 static void tegra_xusb_usb_phy_work(struct work_struct *work)
602 {
603 	struct tegra_xusb_port *port = container_of(work,
604 						    struct tegra_xusb_port,
605 						    usb_phy_work);
606 	enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
607 
608 	usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
609 
610 	dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
611 		usb_roles[role]);
612 
613 	atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
614 }
615 
tegra_xusb_role_sw_set(struct usb_role_switch * sw,enum usb_role role)616 static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
617 				  enum usb_role role)
618 {
619 	struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
620 
621 	dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
622 
623 	schedule_work(&port->usb_phy_work);
624 
625 	return 0;
626 }
627 
tegra_xusb_set_peripheral(struct usb_otg * otg,struct usb_gadget * gadget)628 static int tegra_xusb_set_peripheral(struct usb_otg *otg,
629 				     struct usb_gadget *gadget)
630 {
631 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
632 						    struct tegra_xusb_port,
633 						    usb_phy);
634 
635 	if (gadget != NULL)
636 		schedule_work(&port->usb_phy_work);
637 
638 	return 0;
639 }
640 
tegra_xusb_set_host(struct usb_otg * otg,struct usb_bus * host)641 static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
642 {
643 	struct tegra_xusb_port *port = container_of(otg->usb_phy,
644 						    struct tegra_xusb_port,
645 						    usb_phy);
646 
647 	if (host != NULL)
648 		schedule_work(&port->usb_phy_work);
649 
650 	return 0;
651 }
652 
653 
tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port * port)654 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
655 {
656 	struct tegra_xusb_lane *lane;
657 	struct usb_role_switch_desc role_sx_desc = {
658 		.fwnode = dev_fwnode(&port->dev),
659 		.set = tegra_xusb_role_sw_set,
660 	};
661 	int err = 0;
662 
663 	/*
664 	 * USB role switch driver needs parent driver owner info. This is a
665 	 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
666 	 * where an optimal solution is possible with changes to USB role
667 	 * switch driver.
668 	 */
669 	port->dev.driver = devm_kzalloc(&port->dev,
670 					sizeof(struct device_driver),
671 					GFP_KERNEL);
672 	if (!port->dev.driver)
673 		return -ENOMEM;
674 
675 	port->dev.driver->owner	 = THIS_MODULE;
676 
677 	port->usb_role_sw = usb_role_switch_register(&port->dev,
678 						     &role_sx_desc);
679 	if (IS_ERR(port->usb_role_sw)) {
680 		err = PTR_ERR(port->usb_role_sw);
681 		dev_err(&port->dev, "failed to register USB role switch: %d",
682 			err);
683 		return err;
684 	}
685 
686 	INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
687 	usb_role_switch_set_drvdata(port->usb_role_sw, port);
688 
689 	port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
690 					 GFP_KERNEL);
691 	if (!port->usb_phy.otg)
692 		return -ENOMEM;
693 
694 	lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
695 
696 	/*
697 	 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
698 	 * reference to retrieve usb-phy details.
699 	 */
700 	port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
701 	port->usb_phy.dev->driver = port->dev.driver;
702 	port->usb_phy.otg->usb_phy = &port->usb_phy;
703 	port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
704 	port->usb_phy.otg->set_host = tegra_xusb_set_host;
705 
706 	err = usb_add_phy_dev(&port->usb_phy);
707 	if (err < 0) {
708 		dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
709 		return err;
710 	}
711 
712 	/* populate connector entry */
713 	of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
714 
715 	return err;
716 }
717 
tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port * usb2)718 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
719 {
720 	struct tegra_xusb_port *port = &usb2->base;
721 	struct device_node *np = port->dev.of_node;
722 	const char *mode;
723 	int err;
724 
725 	usb2->internal = of_property_read_bool(np, "nvidia,internal");
726 
727 	if (!of_property_read_string(np, "mode", &mode)) {
728 		int err = match_string(modes, ARRAY_SIZE(modes), mode);
729 		if (err < 0) {
730 			dev_err(&port->dev, "invalid value %s for \"mode\"\n",
731 				mode);
732 			usb2->mode = USB_DR_MODE_UNKNOWN;
733 		} else {
734 			usb2->mode = err;
735 		}
736 	} else {
737 		usb2->mode = USB_DR_MODE_HOST;
738 	}
739 
740 	/* usb-role-switch property is mandatory for OTG/Peripheral modes */
741 	if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
742 	    usb2->mode == USB_DR_MODE_OTG) {
743 		if (of_property_read_bool(np, "usb-role-switch")) {
744 			err = tegra_xusb_setup_usb_role_switch(port);
745 			if (err < 0)
746 				return err;
747 		} else {
748 			dev_err(&port->dev, "usb-role-switch not found for %s mode",
749 				modes[usb2->mode]);
750 			return -EINVAL;
751 		}
752 	}
753 
754 	usb2->supply = regulator_get(&port->dev, "vbus");
755 	return PTR_ERR_OR_ZERO(usb2->supply);
756 }
757 
tegra_xusb_add_usb2_port(struct tegra_xusb_padctl * padctl,unsigned int index)758 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
759 				    unsigned int index)
760 {
761 	struct tegra_xusb_usb2_port *usb2;
762 	struct device_node *np;
763 	int err = 0;
764 
765 	/*
766 	 * USB2 ports don't require additional properties, but if the port is
767 	 * marked as disabled there is no reason to register it.
768 	 */
769 	np = tegra_xusb_find_port_node(padctl, "usb2", index);
770 	if (!np || !of_device_is_available(np))
771 		goto out;
772 
773 	usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
774 	if (!usb2) {
775 		err = -ENOMEM;
776 		goto out;
777 	}
778 
779 	err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
780 	if (err < 0)
781 		goto out;
782 
783 	usb2->base.ops = padctl->soc->ports.usb2.ops;
784 
785 	usb2->base.lane = usb2->base.ops->map(&usb2->base);
786 	if (IS_ERR(usb2->base.lane)) {
787 		err = PTR_ERR(usb2->base.lane);
788 		tegra_xusb_port_unregister(&usb2->base);
789 		goto out;
790 	}
791 
792 	err = tegra_xusb_usb2_port_parse_dt(usb2);
793 	if (err < 0) {
794 		tegra_xusb_port_unregister(&usb2->base);
795 		goto out;
796 	}
797 
798 	list_add_tail(&usb2->base.list, &padctl->ports);
799 
800 out:
801 	of_node_put(np);
802 	return err;
803 }
804 
tegra_xusb_usb2_port_release(struct tegra_xusb_port * port)805 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
806 {
807 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
808 
809 	kfree(usb2);
810 }
811 
tegra_xusb_usb2_port_remove(struct tegra_xusb_port * port)812 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
813 {
814 	struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
815 
816 	regulator_put(usb2->supply);
817 }
818 
tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port * ulpi)819 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
820 {
821 	struct tegra_xusb_port *port = &ulpi->base;
822 	struct device_node *np = port->dev.of_node;
823 
824 	ulpi->internal = of_property_read_bool(np, "nvidia,internal");
825 
826 	return 0;
827 }
828 
tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl * padctl,unsigned int index)829 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
830 				    unsigned int index)
831 {
832 	struct tegra_xusb_ulpi_port *ulpi;
833 	struct device_node *np;
834 	int err = 0;
835 
836 	np = tegra_xusb_find_port_node(padctl, "ulpi", index);
837 	if (!np || !of_device_is_available(np))
838 		goto out;
839 
840 	ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
841 	if (!ulpi) {
842 		err = -ENOMEM;
843 		goto out;
844 	}
845 
846 	err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
847 	if (err < 0)
848 		goto out;
849 
850 	ulpi->base.ops = padctl->soc->ports.ulpi.ops;
851 
852 	ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
853 	if (IS_ERR(ulpi->base.lane)) {
854 		err = PTR_ERR(ulpi->base.lane);
855 		tegra_xusb_port_unregister(&ulpi->base);
856 		goto out;
857 	}
858 
859 	err = tegra_xusb_ulpi_port_parse_dt(ulpi);
860 	if (err < 0) {
861 		tegra_xusb_port_unregister(&ulpi->base);
862 		goto out;
863 	}
864 
865 	list_add_tail(&ulpi->base.list, &padctl->ports);
866 
867 out:
868 	of_node_put(np);
869 	return err;
870 }
871 
tegra_xusb_ulpi_port_release(struct tegra_xusb_port * port)872 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
873 {
874 	struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
875 
876 	kfree(ulpi);
877 }
878 
tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port * hsic)879 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
880 {
881 	/* XXX */
882 	return 0;
883 }
884 
tegra_xusb_add_hsic_port(struct tegra_xusb_padctl * padctl,unsigned int index)885 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
886 				    unsigned int index)
887 {
888 	struct tegra_xusb_hsic_port *hsic;
889 	struct device_node *np;
890 	int err = 0;
891 
892 	np = tegra_xusb_find_port_node(padctl, "hsic", index);
893 	if (!np || !of_device_is_available(np))
894 		goto out;
895 
896 	hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
897 	if (!hsic) {
898 		err = -ENOMEM;
899 		goto out;
900 	}
901 
902 	err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
903 	if (err < 0)
904 		goto out;
905 
906 	hsic->base.ops = padctl->soc->ports.hsic.ops;
907 
908 	hsic->base.lane = hsic->base.ops->map(&hsic->base);
909 	if (IS_ERR(hsic->base.lane)) {
910 		err = PTR_ERR(hsic->base.lane);
911 		goto out;
912 	}
913 
914 	err = tegra_xusb_hsic_port_parse_dt(hsic);
915 	if (err < 0) {
916 		tegra_xusb_port_unregister(&hsic->base);
917 		goto out;
918 	}
919 
920 	list_add_tail(&hsic->base.list, &padctl->ports);
921 
922 out:
923 	of_node_put(np);
924 	return err;
925 }
926 
tegra_xusb_hsic_port_release(struct tegra_xusb_port * port)927 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
928 {
929 	struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
930 
931 	kfree(hsic);
932 }
933 
tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port * usb3)934 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
935 {
936 	struct tegra_xusb_port *port = &usb3->base;
937 	struct device_node *np = port->dev.of_node;
938 	enum usb_device_speed maximum_speed;
939 	u32 value;
940 	int err;
941 
942 	err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
943 	if (err < 0) {
944 		dev_err(&port->dev, "failed to read port: %d\n", err);
945 		return err;
946 	}
947 
948 	usb3->port = value;
949 
950 	usb3->internal = of_property_read_bool(np, "nvidia,internal");
951 
952 	if (device_property_present(&port->dev, "maximum-speed")) {
953 		maximum_speed =  usb_get_maximum_speed(&port->dev);
954 		if (maximum_speed == USB_SPEED_SUPER)
955 			usb3->disable_gen2 = true;
956 		else if (maximum_speed == USB_SPEED_SUPER_PLUS)
957 			usb3->disable_gen2 = false;
958 		else
959 			return -EINVAL;
960 	}
961 
962 	usb3->supply = regulator_get(&port->dev, "vbus");
963 	return PTR_ERR_OR_ZERO(usb3->supply);
964 }
965 
tegra_xusb_add_usb3_port(struct tegra_xusb_padctl * padctl,unsigned int index)966 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
967 				    unsigned int index)
968 {
969 	struct tegra_xusb_usb3_port *usb3;
970 	struct device_node *np;
971 	int err = 0;
972 
973 	/*
974 	 * If there is no supplemental configuration in the device tree the
975 	 * port is unusable. But it is valid to configure only a single port,
976 	 * hence return 0 instead of an error to allow ports to be optional.
977 	 */
978 	np = tegra_xusb_find_port_node(padctl, "usb3", index);
979 	if (!np || !of_device_is_available(np))
980 		goto out;
981 
982 	usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
983 	if (!usb3) {
984 		err = -ENOMEM;
985 		goto out;
986 	}
987 
988 	err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
989 	if (err < 0)
990 		goto out;
991 
992 	usb3->base.ops = padctl->soc->ports.usb3.ops;
993 
994 	usb3->base.lane = usb3->base.ops->map(&usb3->base);
995 	if (IS_ERR(usb3->base.lane)) {
996 		err = PTR_ERR(usb3->base.lane);
997 		goto out;
998 	}
999 
1000 	err = tegra_xusb_usb3_port_parse_dt(usb3);
1001 	if (err < 0) {
1002 		tegra_xusb_port_unregister(&usb3->base);
1003 		goto out;
1004 	}
1005 
1006 	list_add_tail(&usb3->base.list, &padctl->ports);
1007 
1008 out:
1009 	of_node_put(np);
1010 	return err;
1011 }
1012 
tegra_xusb_usb3_port_release(struct tegra_xusb_port * port)1013 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1014 {
1015 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1016 
1017 	kfree(usb3);
1018 }
1019 
tegra_xusb_usb3_port_remove(struct tegra_xusb_port * port)1020 void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1021 {
1022 	struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1023 
1024 	regulator_put(usb3->supply);
1025 }
1026 
__tegra_xusb_remove_ports(struct tegra_xusb_padctl * padctl)1027 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1028 {
1029 	struct tegra_xusb_port *port, *tmp;
1030 
1031 	list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1032 		list_del(&port->list);
1033 		tegra_xusb_port_unregister(port);
1034 	}
1035 }
1036 
tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl * padctl)1037 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1038 {
1039 	struct device_node *np;
1040 	unsigned int i;
1041 
1042 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1043 		np = tegra_xusb_find_port_node(padctl, "usb3", i);
1044 		if (!np || !of_device_is_available(np))
1045 			return i;
1046 	}
1047 
1048 	return -ENODEV;
1049 }
1050 
tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port * usb2)1051 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1052 {
1053 	unsigned int i;
1054 	struct tegra_xusb_usb3_port *usb3;
1055 	struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1056 
1057 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1058 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1059 		if (usb3 && usb3->port == usb2->base.index)
1060 			return true;
1061 	}
1062 
1063 	return false;
1064 }
1065 
tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port * usb2)1066 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1067 {
1068 	int fake;
1069 
1070 	/* Disable usb3_port_fake usage by default and assign if needed */
1071 	usb2->usb3_port_fake = -1;
1072 
1073 	if ((usb2->mode == USB_DR_MODE_OTG ||
1074 	     usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1075 		!tegra_xusb_port_is_companion(usb2)) {
1076 		fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1077 		if (fake < 0) {
1078 			dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1079 			return -ENODEV;
1080 		}
1081 
1082 		dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1083 		usb2->usb3_port_fake = fake;
1084 	}
1085 
1086 	return 0;
1087 }
1088 
tegra_xusb_setup_ports(struct tegra_xusb_padctl * padctl)1089 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1090 {
1091 	struct tegra_xusb_port *port;
1092 	struct tegra_xusb_usb2_port *usb2;
1093 	unsigned int i;
1094 	int err = 0;
1095 
1096 	mutex_lock(&padctl->lock);
1097 
1098 	for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1099 		err = tegra_xusb_add_usb2_port(padctl, i);
1100 		if (err < 0)
1101 			goto remove_ports;
1102 	}
1103 
1104 	for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1105 		err = tegra_xusb_add_ulpi_port(padctl, i);
1106 		if (err < 0)
1107 			goto remove_ports;
1108 	}
1109 
1110 	for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1111 		err = tegra_xusb_add_hsic_port(padctl, i);
1112 		if (err < 0)
1113 			goto remove_ports;
1114 	}
1115 
1116 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1117 		err = tegra_xusb_add_usb3_port(padctl, i);
1118 		if (err < 0)
1119 			goto remove_ports;
1120 	}
1121 
1122 	if (padctl->soc->need_fake_usb3_port) {
1123 		for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1124 			usb2 = tegra_xusb_find_usb2_port(padctl, i);
1125 			if (!usb2)
1126 				continue;
1127 
1128 			err = tegra_xusb_update_usb3_fake_port(usb2);
1129 			if (err < 0)
1130 				goto remove_ports;
1131 		}
1132 	}
1133 
1134 	list_for_each_entry(port, &padctl->ports, list) {
1135 		err = port->ops->enable(port);
1136 		if (err < 0)
1137 			dev_err(padctl->dev, "failed to enable port %s: %d\n",
1138 				dev_name(&port->dev), err);
1139 	}
1140 
1141 	goto unlock;
1142 
1143 remove_ports:
1144 	__tegra_xusb_remove_ports(padctl);
1145 unlock:
1146 	mutex_unlock(&padctl->lock);
1147 	return err;
1148 }
1149 
tegra_xusb_remove_ports(struct tegra_xusb_padctl * padctl)1150 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1151 {
1152 	mutex_lock(&padctl->lock);
1153 	__tegra_xusb_remove_ports(padctl);
1154 	mutex_unlock(&padctl->lock);
1155 }
1156 
tegra_xusb_padctl_probe(struct platform_device * pdev)1157 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1158 {
1159 	struct device_node *np = pdev->dev.of_node;
1160 	const struct tegra_xusb_padctl_soc *soc;
1161 	struct tegra_xusb_padctl *padctl;
1162 	const struct of_device_id *match;
1163 	int err;
1164 
1165 	/* for backwards compatibility with old device trees */
1166 	np = of_get_child_by_name(np, "pads");
1167 	if (!np) {
1168 		dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1169 		return tegra_xusb_padctl_legacy_probe(pdev);
1170 	}
1171 
1172 	of_node_put(np);
1173 
1174 	match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1175 	soc = match->data;
1176 
1177 	padctl = soc->ops->probe(&pdev->dev, soc);
1178 	if (IS_ERR(padctl))
1179 		return PTR_ERR(padctl);
1180 
1181 	platform_set_drvdata(pdev, padctl);
1182 	INIT_LIST_HEAD(&padctl->ports);
1183 	INIT_LIST_HEAD(&padctl->lanes);
1184 	INIT_LIST_HEAD(&padctl->pads);
1185 	mutex_init(&padctl->lock);
1186 
1187 	padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1188 	if (IS_ERR(padctl->regs)) {
1189 		err = PTR_ERR(padctl->regs);
1190 		goto remove;
1191 	}
1192 
1193 	padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1194 	if (IS_ERR(padctl->rst)) {
1195 		err = PTR_ERR(padctl->rst);
1196 		goto remove;
1197 	}
1198 
1199 	padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1200 					sizeof(*padctl->supplies), GFP_KERNEL);
1201 	if (!padctl->supplies) {
1202 		err = -ENOMEM;
1203 		goto remove;
1204 	}
1205 
1206 	regulator_bulk_set_supply_names(padctl->supplies,
1207 					padctl->soc->supply_names,
1208 					padctl->soc->num_supplies);
1209 
1210 	err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1211 				      padctl->supplies);
1212 	if (err < 0) {
1213 		dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1214 		goto remove;
1215 	}
1216 
1217 	err = reset_control_deassert(padctl->rst);
1218 	if (err < 0)
1219 		goto remove;
1220 
1221 	err = regulator_bulk_enable(padctl->soc->num_supplies,
1222 				    padctl->supplies);
1223 	if (err < 0) {
1224 		dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1225 		goto reset;
1226 	}
1227 
1228 	err = tegra_xusb_setup_pads(padctl);
1229 	if (err < 0) {
1230 		dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1231 		goto power_down;
1232 	}
1233 
1234 	err = tegra_xusb_setup_ports(padctl);
1235 	if (err) {
1236 		const char *level = KERN_ERR;
1237 
1238 		if (err == -EPROBE_DEFER)
1239 			level = KERN_DEBUG;
1240 
1241 		dev_printk(level, &pdev->dev,
1242 			   dev_fmt("failed to setup XUSB ports: %d\n"), err);
1243 		goto remove_pads;
1244 	}
1245 
1246 	return 0;
1247 
1248 remove_pads:
1249 	tegra_xusb_remove_pads(padctl);
1250 power_down:
1251 	regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1252 reset:
1253 	reset_control_assert(padctl->rst);
1254 remove:
1255 	platform_set_drvdata(pdev, NULL);
1256 	soc->ops->remove(padctl);
1257 	return err;
1258 }
1259 
tegra_xusb_padctl_remove(struct platform_device * pdev)1260 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1261 {
1262 	struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1263 	int err;
1264 
1265 	tegra_xusb_remove_ports(padctl);
1266 	tegra_xusb_remove_pads(padctl);
1267 
1268 	err = regulator_bulk_disable(padctl->soc->num_supplies,
1269 				     padctl->supplies);
1270 	if (err < 0)
1271 		dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1272 
1273 	err = reset_control_assert(padctl->rst);
1274 	if (err < 0)
1275 		dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1276 
1277 	padctl->soc->ops->remove(padctl);
1278 
1279 	return err;
1280 }
1281 
tegra_xusb_padctl_suspend_noirq(struct device * dev)1282 static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1283 {
1284 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1285 
1286 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
1287 		return padctl->soc->ops->suspend_noirq(padctl);
1288 
1289 	return 0;
1290 }
1291 
tegra_xusb_padctl_resume_noirq(struct device * dev)1292 static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
1293 {
1294 	struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1295 
1296 	if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
1297 		return padctl->soc->ops->resume_noirq(padctl);
1298 
1299 	return 0;
1300 }
1301 
1302 static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
1303 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
1304 				      tegra_xusb_padctl_resume_noirq)
1305 };
1306 
1307 static struct platform_driver tegra_xusb_padctl_driver = {
1308 	.driver = {
1309 		.name = "tegra-xusb-padctl",
1310 		.of_match_table = tegra_xusb_padctl_of_match,
1311 		.pm = &tegra_xusb_padctl_pm_ops,
1312 	},
1313 	.probe = tegra_xusb_padctl_probe,
1314 	.remove = tegra_xusb_padctl_remove,
1315 };
1316 module_platform_driver(tegra_xusb_padctl_driver);
1317 
tegra_xusb_padctl_get(struct device * dev)1318 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1319 {
1320 	struct tegra_xusb_padctl *padctl;
1321 	struct platform_device *pdev;
1322 	struct device_node *np;
1323 
1324 	np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1325 	if (!np)
1326 		return ERR_PTR(-EINVAL);
1327 
1328 	/*
1329 	 * This is slightly ugly. A better implementation would be to keep a
1330 	 * registry of pad controllers, but since there will almost certainly
1331 	 * only ever be one per SoC that would be a little overkill.
1332 	 */
1333 	pdev = of_find_device_by_node(np);
1334 	if (!pdev) {
1335 		of_node_put(np);
1336 		return ERR_PTR(-ENODEV);
1337 	}
1338 
1339 	of_node_put(np);
1340 
1341 	padctl = platform_get_drvdata(pdev);
1342 	if (!padctl) {
1343 		put_device(&pdev->dev);
1344 		return ERR_PTR(-EPROBE_DEFER);
1345 	}
1346 
1347 	return padctl;
1348 }
1349 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1350 
tegra_xusb_padctl_put(struct tegra_xusb_padctl * padctl)1351 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1352 {
1353 	if (padctl)
1354 		put_device(padctl->dev);
1355 }
1356 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1357 
tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl * padctl,unsigned int port)1358 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1359 					unsigned int port)
1360 {
1361 	if (padctl->soc->ops->usb3_save_context)
1362 		return padctl->soc->ops->usb3_save_context(padctl, port);
1363 
1364 	return -ENOSYS;
1365 }
1366 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1367 
tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl * padctl,unsigned int port,bool idle)1368 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1369 				    unsigned int port, bool idle)
1370 {
1371 	if (padctl->soc->ops->hsic_set_idle)
1372 		return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1373 
1374 	return -ENOSYS;
1375 }
1376 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1377 
tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl * padctl,struct phy * phy,enum usb_device_speed speed)1378 int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
1379 					   enum usb_device_speed speed)
1380 {
1381 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1382 
1383 	if (lane->pad->ops->enable_phy_sleepwalk)
1384 		return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
1385 
1386 	return -EOPNOTSUPP;
1387 }
1388 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
1389 
tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl * padctl,struct phy * phy)1390 int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
1391 {
1392 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1393 
1394 	if (lane->pad->ops->disable_phy_sleepwalk)
1395 		return lane->pad->ops->disable_phy_sleepwalk(lane);
1396 
1397 	return -EOPNOTSUPP;
1398 }
1399 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
1400 
tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl * padctl,struct phy * phy)1401 int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1402 {
1403 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1404 
1405 	if (lane->pad->ops->enable_phy_wake)
1406 		return lane->pad->ops->enable_phy_wake(lane);
1407 
1408 	return -EOPNOTSUPP;
1409 }
1410 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
1411 
tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl * padctl,struct phy * phy)1412 int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1413 {
1414 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1415 
1416 	if (lane->pad->ops->disable_phy_wake)
1417 		return lane->pad->ops->disable_phy_wake(lane);
1418 
1419 	return -EOPNOTSUPP;
1420 }
1421 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
1422 
tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl * padctl,struct phy * phy)1423 bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
1424 {
1425 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1426 
1427 	if (lane->pad->ops->remote_wake_detected)
1428 		return lane->pad->ops->remote_wake_detected(lane);
1429 
1430 	return false;
1431 }
1432 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
1433 
tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl * padctl,unsigned int port,bool enable)1434 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1435 					   unsigned int port, bool enable)
1436 {
1437 	if (padctl->soc->ops->usb3_set_lfps_detect)
1438 		return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1439 							      enable);
1440 
1441 	return -ENOSYS;
1442 }
1443 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1444 
tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl * padctl,bool val)1445 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1446 							bool val)
1447 {
1448 	if (padctl->soc->ops->vbus_override)
1449 		return padctl->soc->ops->vbus_override(padctl, val);
1450 
1451 	return -ENOTSUPP;
1452 }
1453 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1454 
tegra_phy_xusb_utmi_port_reset(struct phy * phy)1455 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1456 {
1457 	struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1458 	struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1459 
1460 	if (padctl->soc->ops->utmi_port_reset)
1461 		return padctl->soc->ops->utmi_port_reset(phy);
1462 
1463 	return -ENOTSUPP;
1464 }
1465 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1466 
tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl * padctl,unsigned int port)1467 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1468 				    unsigned int port)
1469 {
1470 	struct tegra_xusb_usb2_port *usb2;
1471 	struct tegra_xusb_usb3_port *usb3;
1472 	int i;
1473 
1474 	usb2 = tegra_xusb_find_usb2_port(padctl, port);
1475 	if (!usb2)
1476 		return -EINVAL;
1477 
1478 	for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1479 		usb3 = tegra_xusb_find_usb3_port(padctl, i);
1480 		if (usb3 && usb3->port == usb2->base.index)
1481 			return usb3->base.index;
1482 	}
1483 
1484 	return -ENODEV;
1485 }
1486 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1487 
1488 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1489 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1490 MODULE_LICENSE("GPL v2");
1491