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