1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2022, NVIDIA CORPORATION. All rights reserved.
6 #include <linux/delay.h>
8 #include <linux/mailbox_client.h>
9 #include <linux/module.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>
20 #include <soc/tegra/fuse.h>
24 static struct phy *tegra_xusb_pad_of_xlate(struct device *dev,
25 struct of_phandle_args *args)
27 struct tegra_xusb_pad *pad = dev_get_drvdata(dev);
28 struct phy *phy = NULL;
31 if (args->args_count != 0)
32 return ERR_PTR(-EINVAL);
34 for (i = 0; i < pad->soc->num_lanes; i++) {
38 if (pad->lanes[i]->dev.of_node == args->np) {
45 phy = ERR_PTR(-ENODEV);
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)
53 .compatible = "nvidia,tegra124-xusb-padctl",
54 .data = &tegra124_xusb_padctl_soc,
57 #if defined(CONFIG_ARCH_TEGRA_210_SOC)
59 .compatible = "nvidia,tegra210-xusb-padctl",
60 .data = &tegra210_xusb_padctl_soc,
63 #if defined(CONFIG_ARCH_TEGRA_186_SOC)
65 .compatible = "nvidia,tegra186-xusb-padctl",
66 .data = &tegra186_xusb_padctl_soc,
69 #if defined(CONFIG_ARCH_TEGRA_194_SOC)
71 .compatible = "nvidia,tegra194-xusb-padctl",
72 .data = &tegra194_xusb_padctl_soc,
77 MODULE_DEVICE_TABLE(of, tegra_xusb_padctl_of_match);
79 static struct device_node *
80 tegra_xusb_find_pad_node(struct tegra_xusb_padctl *padctl, const char *name)
82 struct device_node *pads, *np;
84 pads = of_get_child_by_name(padctl->dev->of_node, "pads");
88 np = of_get_child_by_name(pads, name);
94 static struct device_node *
95 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad *pad, unsigned int index)
97 struct device_node *np, *lanes;
99 lanes = of_get_child_by_name(pad->dev.of_node, "lanes");
103 np = of_get_child_by_name(lanes, pad->soc->lanes[index].name);
109 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane *lane,
110 struct device_node *np)
112 struct device *dev = &lane->pad->dev;
113 const char *function;
116 err = of_property_read_string(np, "nvidia,function", &function);
120 err = match_string(lane->soc->funcs, lane->soc->num_funcs, function);
122 dev_err(dev, "invalid function \"%s\" for lane \"%pOFn\"\n",
127 lane->function = err;
132 static void tegra_xusb_lane_destroy(struct phy *phy)
135 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
137 lane->pad->ops->remove(lane);
142 static void tegra_xusb_pad_release(struct device *dev)
144 struct tegra_xusb_pad *pad = to_tegra_xusb_pad(dev);
146 pad->soc->ops->remove(pad);
149 static const struct device_type tegra_xusb_pad_type = {
150 .release = tegra_xusb_pad_release,
153 int tegra_xusb_pad_init(struct tegra_xusb_pad *pad,
154 struct tegra_xusb_padctl *padctl,
155 struct device_node *np)
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;
166 err = dev_set_name(&pad->dev, "%s", pad->soc->name);
170 err = device_add(&pad->dev);
177 device_unregister(&pad->dev);
181 int tegra_xusb_pad_register(struct tegra_xusb_pad *pad,
182 const struct phy_ops *ops)
184 struct device_node *children;
189 children = of_get_child_by_name(pad->dev.of_node, "lanes");
193 pad->lanes = devm_kcalloc(&pad->dev, pad->soc->num_lanes, sizeof(lane),
196 of_node_put(children);
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;
204 /* skip disabled lanes */
205 if (!np || !of_device_is_available(np)) {
210 pad->lanes[i] = phy_create(&pad->dev, np, ops);
211 if (IS_ERR(pad->lanes[i])) {
212 err = PTR_ERR(pad->lanes[i]);
217 lane = pad->ops->probe(pad, np, i);
219 phy_destroy(pad->lanes[i]);
224 list_add_tail(&lane->list, &pad->padctl->lanes);
225 phy_set_drvdata(pad->lanes[i], lane);
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);
239 tegra_xusb_lane_destroy(pad->lanes[i]);
241 of_node_put(children);
246 void tegra_xusb_pad_unregister(struct tegra_xusb_pad *pad)
248 unsigned int i = pad->soc->num_lanes;
250 of_phy_provider_unregister(pad->provider);
253 tegra_xusb_lane_destroy(pad->lanes[i]);
255 device_unregister(&pad->dev);
258 static struct tegra_xusb_pad *
259 tegra_xusb_pad_create(struct tegra_xusb_padctl *padctl,
260 const struct tegra_xusb_pad_soc *soc)
262 struct tegra_xusb_pad *pad;
263 struct device_node *np;
266 np = tegra_xusb_find_pad_node(padctl, soc->name);
267 if (!np || !of_device_is_available(np))
270 pad = soc->ops->probe(padctl, soc, np);
273 dev_err(padctl->dev, "failed to create pad %s: %d\n",
278 /* XXX move this into ->probe() to avoid string comparison */
279 if (strcmp(soc->name, "pcie") == 0)
282 if (strcmp(soc->name, "sata") == 0)
285 if (strcmp(soc->name, "usb2") == 0)
288 if (strcmp(soc->name, "ulpi") == 0)
291 if (strcmp(soc->name, "hsic") == 0)
297 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
299 struct tegra_xusb_pad *pad, *tmp;
301 list_for_each_entry_safe_reverse(pad, tmp, &padctl->pads, list) {
302 list_del(&pad->list);
303 tegra_xusb_pad_unregister(pad);
307 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl *padctl)
309 mutex_lock(&padctl->lock);
310 __tegra_xusb_remove_pads(padctl);
311 mutex_unlock(&padctl->lock);
314 static void tegra_xusb_lane_program(struct tegra_xusb_lane *lane)
316 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
317 const struct tegra_xusb_lane_soc *soc = lane->soc;
320 /* skip single function lanes */
321 if (soc->num_funcs < 2)
324 if (lane->pad->ops->iddq_enable)
325 lane->pad->ops->iddq_enable(lane);
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);
333 if (lane->pad->ops->iddq_disable)
334 lane->pad->ops->iddq_disable(lane);
337 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
341 for (i = 0; i < pad->soc->num_lanes; i++) {
342 struct tegra_xusb_lane *lane;
345 lane = phy_get_drvdata(pad->lanes[i]);
346 tegra_xusb_lane_program(lane);
351 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
353 struct tegra_xusb_pad *pad;
356 mutex_lock(&padctl->lock);
358 for (i = 0; i < padctl->soc->num_pads; i++) {
359 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
362 pad = tegra_xusb_pad_create(padctl, soc);
365 dev_err(padctl->dev, "failed to create pad %s: %d\n",
367 __tegra_xusb_remove_pads(padctl);
368 mutex_unlock(&padctl->lock);
375 list_add_tail(&pad->list, &padctl->pads);
378 list_for_each_entry(pad, &padctl->pads, list)
379 tegra_xusb_pad_program(pad);
381 mutex_unlock(&padctl->lock);
385 bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
386 const char *function)
388 const char *func = lane->soc->funcs[lane->function];
390 return strcmp(function, func) == 0;
393 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
397 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
400 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
402 return ERR_PTR(-ENOMEM);
404 list_for_each_entry(lane, &padctl->lanes, list) {
405 if (strcmp(lane->soc->name, name) == 0) {
415 struct tegra_xusb_lane *
416 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
417 const struct tegra_xusb_lane_map *map,
418 const char *function)
420 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
422 for (; map->type; map++) {
423 if (port->index != map->port)
426 lane = tegra_xusb_find_lane(port->padctl, map->type,
431 if (!tegra_xusb_lane_check(lane, function))
435 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
436 map->type, map->index, match->soc->name);
444 static struct device_node *
445 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
448 struct device_node *ports, *np;
451 ports = of_get_child_by_name(padctl->dev->of_node, "ports");
455 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
460 np = of_get_child_by_name(ports, name);
467 struct tegra_xusb_port *
468 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
471 struct tegra_xusb_port *port;
472 struct device_node *np;
474 np = tegra_xusb_find_port_node(padctl, type, index);
478 list_for_each_entry(port, &padctl->ports, list) {
479 if (np == port->dev.of_node) {
490 struct tegra_xusb_usb2_port *
491 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
493 struct tegra_xusb_port *port;
495 port = tegra_xusb_find_port(padctl, "usb2", index);
497 return to_usb2_port(port);
502 struct tegra_xusb_usb3_port *
503 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
505 struct tegra_xusb_port *port;
507 port = tegra_xusb_find_port(padctl, "usb3", index);
509 return to_usb3_port(port);
514 static void tegra_xusb_port_release(struct device *dev)
516 struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
518 if (port->ops->release)
519 port->ops->release(port);
522 static const struct device_type tegra_xusb_port_type = {
523 .release = tegra_xusb_port_release,
526 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
527 struct tegra_xusb_padctl *padctl,
528 struct device_node *np,
534 INIT_LIST_HEAD(&port->list);
535 port->padctl = padctl;
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;
543 err = dev_set_name(&port->dev, "%s-%u", name, index);
547 err = device_add(&port->dev);
554 device_unregister(&port->dev);
558 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
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);
567 if (port->ops->remove)
568 port->ops->remove(port);
570 device_unregister(&port->dev);
573 static 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",
580 static const char * const usb_roles[] = {
581 [USB_ROLE_NONE] = "none",
582 [USB_ROLE_HOST] = "host",
583 [USB_ROLE_DEVICE] = "device",
586 static enum usb_phy_events to_usb_phy_event(enum usb_role role)
589 case USB_ROLE_DEVICE:
590 return USB_EVENT_VBUS;
596 return USB_EVENT_NONE;
600 static void tegra_xusb_usb_phy_work(struct work_struct *work)
602 struct tegra_xusb_port *port = container_of(work,
603 struct tegra_xusb_port,
605 enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
607 usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
609 dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
612 atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
615 static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
618 struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
620 dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
622 schedule_work(&port->usb_phy_work);
627 static int tegra_xusb_set_peripheral(struct usb_otg *otg,
628 struct usb_gadget *gadget)
630 struct tegra_xusb_port *port = container_of(otg->usb_phy,
631 struct tegra_xusb_port,
635 schedule_work(&port->usb_phy_work);
640 static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
642 struct tegra_xusb_port *port = container_of(otg->usb_phy,
643 struct tegra_xusb_port,
647 schedule_work(&port->usb_phy_work);
653 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
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,
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
669 port->dev.driver = devm_kzalloc(&port->dev,
670 sizeof(struct device_driver),
672 port->dev.driver->owner = THIS_MODULE;
674 port->usb_role_sw = usb_role_switch_register(&port->dev,
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",
683 INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
684 usb_role_switch_set_drvdata(port->usb_role_sw, port);
686 port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
688 if (!port->usb_phy.otg)
691 lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
694 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
695 * reference to retrieve usb-phy details.
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;
703 err = usb_add_phy_dev(&port->usb_phy);
705 dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
709 /* populate connector entry */
710 of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
715 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
717 struct tegra_xusb_port *port = &usb2->base;
718 struct device_node *np = port->dev.of_node;
722 usb2->internal = of_property_read_bool(np, "nvidia,internal");
724 if (!of_property_read_string(np, "mode", &mode)) {
725 int err = match_string(modes, ARRAY_SIZE(modes), mode);
727 dev_err(&port->dev, "invalid value %s for \"mode\"\n",
729 usb2->mode = USB_DR_MODE_UNKNOWN;
734 usb2->mode = USB_DR_MODE_HOST;
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);
745 dev_err(&port->dev, "usb-role-switch not found for %s mode",
751 usb2->supply = regulator_get(&port->dev, "vbus");
752 return PTR_ERR_OR_ZERO(usb2->supply);
755 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
758 struct tegra_xusb_usb2_port *usb2;
759 struct device_node *np;
763 * USB2 ports don't require additional properties, but if the port is
764 * marked as disabled there is no reason to register it.
766 np = tegra_xusb_find_port_node(padctl, "usb2", index);
767 if (!np || !of_device_is_available(np))
770 usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
776 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
780 usb2->base.ops = padctl->soc->ports.usb2.ops;
782 usb2->base.lane = usb2->base.ops->map(&usb2->base);
783 if (IS_ERR(usb2->base.lane)) {
784 err = PTR_ERR(usb2->base.lane);
788 err = tegra_xusb_usb2_port_parse_dt(usb2);
790 tegra_xusb_port_unregister(&usb2->base);
794 list_add_tail(&usb2->base.list, &padctl->ports);
801 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
803 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
808 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
810 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
812 regulator_put(usb2->supply);
815 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
817 struct tegra_xusb_port *port = &ulpi->base;
818 struct device_node *np = port->dev.of_node;
820 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
825 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
828 struct tegra_xusb_ulpi_port *ulpi;
829 struct device_node *np;
832 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
833 if (!np || !of_device_is_available(np))
836 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
842 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
846 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
848 ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
849 if (IS_ERR(ulpi->base.lane)) {
850 err = PTR_ERR(ulpi->base.lane);
854 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
856 tegra_xusb_port_unregister(&ulpi->base);
860 list_add_tail(&ulpi->base.list, &padctl->ports);
867 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
869 struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
874 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
880 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
883 struct tegra_xusb_hsic_port *hsic;
884 struct device_node *np;
887 np = tegra_xusb_find_port_node(padctl, "hsic", index);
888 if (!np || !of_device_is_available(np))
891 hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
897 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
901 hsic->base.ops = padctl->soc->ports.hsic.ops;
903 hsic->base.lane = hsic->base.ops->map(&hsic->base);
904 if (IS_ERR(hsic->base.lane)) {
905 err = PTR_ERR(hsic->base.lane);
909 err = tegra_xusb_hsic_port_parse_dt(hsic);
911 tegra_xusb_port_unregister(&hsic->base);
915 list_add_tail(&hsic->base.list, &padctl->ports);
922 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
924 struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
929 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
931 struct tegra_xusb_port *port = &usb3->base;
932 struct device_node *np = port->dev.of_node;
933 enum usb_device_speed maximum_speed;
937 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
939 dev_err(&port->dev, "failed to read port: %d\n", err);
945 usb3->internal = of_property_read_bool(np, "nvidia,internal");
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;
957 usb3->supply = regulator_get(&port->dev, "vbus");
958 return PTR_ERR_OR_ZERO(usb3->supply);
961 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
964 struct tegra_xusb_usb3_port *usb3;
965 struct device_node *np;
969 * If there is no supplemental configuration in the device tree the
970 * port is unusable. But it is valid to configure only a single port,
971 * hence return 0 instead of an error to allow ports to be optional.
973 np = tegra_xusb_find_port_node(padctl, "usb3", index);
974 if (!np || !of_device_is_available(np))
977 usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
983 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
987 usb3->base.ops = padctl->soc->ports.usb3.ops;
989 usb3->base.lane = usb3->base.ops->map(&usb3->base);
990 if (IS_ERR(usb3->base.lane)) {
991 err = PTR_ERR(usb3->base.lane);
995 err = tegra_xusb_usb3_port_parse_dt(usb3);
997 tegra_xusb_port_unregister(&usb3->base);
1001 list_add_tail(&usb3->base.list, &padctl->ports);
1008 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1010 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1015 void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1017 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1019 regulator_put(usb3->supply);
1022 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1024 struct tegra_xusb_port *port, *tmp;
1026 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1027 list_del(&port->list);
1028 tegra_xusb_port_unregister(port);
1032 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1034 struct device_node *np;
1037 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1038 np = tegra_xusb_find_port_node(padctl, "usb3", i);
1039 if (!np || !of_device_is_available(np))
1046 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1049 struct tegra_xusb_usb3_port *usb3;
1050 struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1052 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1053 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1054 if (usb3 && usb3->port == usb2->base.index)
1061 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1065 /* Disable usb3_port_fake usage by default and assign if needed */
1066 usb2->usb3_port_fake = -1;
1068 if ((usb2->mode == USB_DR_MODE_OTG ||
1069 usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1070 !tegra_xusb_port_is_companion(usb2)) {
1071 fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1073 dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1077 dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1078 usb2->usb3_port_fake = fake;
1084 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1086 struct tegra_xusb_port *port;
1087 struct tegra_xusb_usb2_port *usb2;
1091 mutex_lock(&padctl->lock);
1093 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1094 err = tegra_xusb_add_usb2_port(padctl, i);
1099 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1100 err = tegra_xusb_add_ulpi_port(padctl, i);
1105 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1106 err = tegra_xusb_add_hsic_port(padctl, i);
1111 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1112 err = tegra_xusb_add_usb3_port(padctl, i);
1117 if (padctl->soc->need_fake_usb3_port) {
1118 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1119 usb2 = tegra_xusb_find_usb2_port(padctl, i);
1123 err = tegra_xusb_update_usb3_fake_port(usb2);
1129 list_for_each_entry(port, &padctl->ports, list) {
1130 err = port->ops->enable(port);
1132 dev_err(padctl->dev, "failed to enable port %s: %d\n",
1133 dev_name(&port->dev), err);
1139 __tegra_xusb_remove_ports(padctl);
1141 mutex_unlock(&padctl->lock);
1145 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1147 mutex_lock(&padctl->lock);
1148 __tegra_xusb_remove_ports(padctl);
1149 mutex_unlock(&padctl->lock);
1152 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1154 struct device_node *np = pdev->dev.of_node;
1155 const struct tegra_xusb_padctl_soc *soc;
1156 struct tegra_xusb_padctl *padctl;
1157 const struct of_device_id *match;
1160 /* for backwards compatibility with old device trees */
1161 np = of_get_child_by_name(np, "pads");
1163 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1164 return tegra_xusb_padctl_legacy_probe(pdev);
1169 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1172 padctl = soc->ops->probe(&pdev->dev, soc);
1174 return PTR_ERR(padctl);
1176 platform_set_drvdata(pdev, padctl);
1177 INIT_LIST_HEAD(&padctl->ports);
1178 INIT_LIST_HEAD(&padctl->lanes);
1179 INIT_LIST_HEAD(&padctl->pads);
1180 mutex_init(&padctl->lock);
1182 padctl->regs = devm_platform_ioremap_resource(pdev, 0);
1183 if (IS_ERR(padctl->regs)) {
1184 err = PTR_ERR(padctl->regs);
1188 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1189 if (IS_ERR(padctl->rst)) {
1190 err = PTR_ERR(padctl->rst);
1194 padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1195 sizeof(*padctl->supplies), GFP_KERNEL);
1196 if (!padctl->supplies) {
1201 regulator_bulk_set_supply_names(padctl->supplies,
1202 padctl->soc->supply_names,
1203 padctl->soc->num_supplies);
1205 err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1208 dev_err_probe(&pdev->dev, err, "failed to get regulators\n");
1212 err = reset_control_deassert(padctl->rst);
1216 err = regulator_bulk_enable(padctl->soc->num_supplies,
1219 dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1223 err = tegra_xusb_setup_pads(padctl);
1225 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1229 err = tegra_xusb_setup_ports(padctl);
1231 const char *level = KERN_ERR;
1233 if (err == -EPROBE_DEFER)
1236 dev_printk(level, &pdev->dev,
1237 dev_fmt("failed to setup XUSB ports: %d\n"), err);
1244 tegra_xusb_remove_pads(padctl);
1246 regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1248 reset_control_assert(padctl->rst);
1250 platform_set_drvdata(pdev, NULL);
1251 soc->ops->remove(padctl);
1255 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1257 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1260 tegra_xusb_remove_ports(padctl);
1261 tegra_xusb_remove_pads(padctl);
1263 err = regulator_bulk_disable(padctl->soc->num_supplies,
1266 dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1268 err = reset_control_assert(padctl->rst);
1270 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1272 padctl->soc->ops->remove(padctl);
1277 static __maybe_unused int tegra_xusb_padctl_suspend_noirq(struct device *dev)
1279 struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1281 if (padctl->soc && padctl->soc->ops && padctl->soc->ops->suspend_noirq)
1282 return padctl->soc->ops->suspend_noirq(padctl);
1287 static __maybe_unused int tegra_xusb_padctl_resume_noirq(struct device *dev)
1289 struct tegra_xusb_padctl *padctl = dev_get_drvdata(dev);
1291 if (padctl->soc && padctl->soc->ops && padctl->soc->ops->resume_noirq)
1292 return padctl->soc->ops->resume_noirq(padctl);
1297 static const struct dev_pm_ops tegra_xusb_padctl_pm_ops = {
1298 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq,
1299 tegra_xusb_padctl_resume_noirq)
1302 static struct platform_driver tegra_xusb_padctl_driver = {
1304 .name = "tegra-xusb-padctl",
1305 .of_match_table = tegra_xusb_padctl_of_match,
1306 .pm = &tegra_xusb_padctl_pm_ops,
1308 .probe = tegra_xusb_padctl_probe,
1309 .remove = tegra_xusb_padctl_remove,
1311 module_platform_driver(tegra_xusb_padctl_driver);
1313 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1315 struct tegra_xusb_padctl *padctl;
1316 struct platform_device *pdev;
1317 struct device_node *np;
1319 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1321 return ERR_PTR(-EINVAL);
1324 * This is slightly ugly. A better implementation would be to keep a
1325 * registry of pad controllers, but since there will almost certainly
1326 * only ever be one per SoC that would be a little overkill.
1328 pdev = of_find_device_by_node(np);
1331 return ERR_PTR(-ENODEV);
1336 padctl = platform_get_drvdata(pdev);
1338 put_device(&pdev->dev);
1339 return ERR_PTR(-EPROBE_DEFER);
1344 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1346 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1349 put_device(padctl->dev);
1351 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1353 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1356 if (padctl->soc->ops->usb3_save_context)
1357 return padctl->soc->ops->usb3_save_context(padctl, port);
1361 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1363 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1364 unsigned int port, bool idle)
1366 if (padctl->soc->ops->hsic_set_idle)
1367 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1371 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1373 int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy,
1374 enum usb_device_speed speed)
1376 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1378 if (lane->pad->ops->enable_phy_sleepwalk)
1379 return lane->pad->ops->enable_phy_sleepwalk(lane, speed);
1383 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk);
1385 int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl *padctl, struct phy *phy)
1387 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1389 if (lane->pad->ops->disable_phy_sleepwalk)
1390 return lane->pad->ops->disable_phy_sleepwalk(lane);
1394 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk);
1396 int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1398 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1400 if (lane->pad->ops->enable_phy_wake)
1401 return lane->pad->ops->enable_phy_wake(lane);
1405 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake);
1407 int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl *padctl, struct phy *phy)
1409 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1411 if (lane->pad->ops->disable_phy_wake)
1412 return lane->pad->ops->disable_phy_wake(lane);
1416 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake);
1418 bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl *padctl, struct phy *phy)
1420 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1422 if (lane->pad->ops->remote_wake_detected)
1423 return lane->pad->ops->remote_wake_detected(lane);
1427 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected);
1429 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1430 unsigned int port, bool enable)
1432 if (padctl->soc->ops->usb3_set_lfps_detect)
1433 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1438 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1440 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1443 if (padctl->soc->ops->vbus_override)
1444 return padctl->soc->ops->vbus_override(padctl, val);
1448 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1450 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1452 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1453 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1455 if (padctl->soc->ops->utmi_port_reset)
1456 return padctl->soc->ops->utmi_port_reset(phy);
1460 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1462 void tegra_phy_xusb_utmi_pad_power_on(struct phy *phy)
1464 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1465 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1467 if (padctl->soc->ops->utmi_pad_power_on)
1468 padctl->soc->ops->utmi_pad_power_on(phy);
1470 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on);
1472 void tegra_phy_xusb_utmi_pad_power_down(struct phy *phy)
1474 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1475 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1477 if (padctl->soc->ops->utmi_pad_power_down)
1478 padctl->soc->ops->utmi_pad_power_down(phy);
1480 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down);
1482 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1485 struct tegra_xusb_usb2_port *usb2;
1486 struct tegra_xusb_usb3_port *usb3;
1489 usb2 = tegra_xusb_find_usb2_port(padctl, port);
1493 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1494 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1495 if (usb3 && usb3->port == usb2->base.index)
1496 return usb3->base.index;
1501 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1503 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1504 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1505 MODULE_LICENSE("GPL v2");