1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2016, 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 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 /* choose function */
325 value = padctl_readl(padctl, soc->offset);
326 value &= ~(soc->mask << soc->shift);
327 value |= lane->function << soc->shift;
328 padctl_writel(padctl, value, soc->offset);
331 static void tegra_xusb_pad_program(struct tegra_xusb_pad *pad)
335 for (i = 0; i < pad->soc->num_lanes; i++) {
336 struct tegra_xusb_lane *lane;
339 lane = phy_get_drvdata(pad->lanes[i]);
340 tegra_xusb_lane_program(lane);
345 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl *padctl)
347 struct tegra_xusb_pad *pad;
350 mutex_lock(&padctl->lock);
352 for (i = 0; i < padctl->soc->num_pads; i++) {
353 const struct tegra_xusb_pad_soc *soc = padctl->soc->pads[i];
356 pad = tegra_xusb_pad_create(padctl, soc);
359 dev_err(padctl->dev, "failed to create pad %s: %d\n",
361 __tegra_xusb_remove_pads(padctl);
362 mutex_unlock(&padctl->lock);
369 list_add_tail(&pad->list, &padctl->pads);
372 list_for_each_entry(pad, &padctl->pads, list)
373 tegra_xusb_pad_program(pad);
375 mutex_unlock(&padctl->lock);
379 static bool tegra_xusb_lane_check(struct tegra_xusb_lane *lane,
380 const char *function)
382 const char *func = lane->soc->funcs[lane->function];
384 return strcmp(function, func) == 0;
387 struct tegra_xusb_lane *tegra_xusb_find_lane(struct tegra_xusb_padctl *padctl,
391 struct tegra_xusb_lane *lane, *hit = ERR_PTR(-ENODEV);
394 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
396 return ERR_PTR(-ENOMEM);
398 list_for_each_entry(lane, &padctl->lanes, list) {
399 if (strcmp(lane->soc->name, name) == 0) {
409 struct tegra_xusb_lane *
410 tegra_xusb_port_find_lane(struct tegra_xusb_port *port,
411 const struct tegra_xusb_lane_map *map,
412 const char *function)
414 struct tegra_xusb_lane *lane, *match = ERR_PTR(-ENODEV);
416 for (; map->type; map++) {
417 if (port->index != map->port)
420 lane = tegra_xusb_find_lane(port->padctl, map->type,
425 if (!tegra_xusb_lane_check(lane, function))
429 dev_err(&port->dev, "conflicting match: %s-%u / %s\n",
430 map->type, map->index, match->soc->name);
438 static struct device_node *
439 tegra_xusb_find_port_node(struct tegra_xusb_padctl *padctl, const char *type,
442 struct device_node *ports, *np;
445 ports = of_get_child_by_name(padctl->dev->of_node, "ports");
449 name = kasprintf(GFP_KERNEL, "%s-%u", type, index);
452 return ERR_PTR(-ENOMEM);
454 np = of_get_child_by_name(ports, name);
461 struct tegra_xusb_port *
462 tegra_xusb_find_port(struct tegra_xusb_padctl *padctl, const char *type,
465 struct tegra_xusb_port *port;
466 struct device_node *np;
468 np = tegra_xusb_find_port_node(padctl, type, index);
472 list_for_each_entry(port, &padctl->ports, list) {
473 if (np == port->dev.of_node) {
484 struct tegra_xusb_usb2_port *
485 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl *padctl, unsigned int index)
487 struct tegra_xusb_port *port;
489 port = tegra_xusb_find_port(padctl, "usb2", index);
491 return to_usb2_port(port);
496 struct tegra_xusb_usb3_port *
497 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl *padctl, unsigned int index)
499 struct tegra_xusb_port *port;
501 port = tegra_xusb_find_port(padctl, "usb3", index);
503 return to_usb3_port(port);
508 static void tegra_xusb_port_release(struct device *dev)
510 struct tegra_xusb_port *port = to_tegra_xusb_port(dev);
512 if (port->ops->release)
513 port->ops->release(port);
516 static struct device_type tegra_xusb_port_type = {
517 .release = tegra_xusb_port_release,
520 static int tegra_xusb_port_init(struct tegra_xusb_port *port,
521 struct tegra_xusb_padctl *padctl,
522 struct device_node *np,
528 INIT_LIST_HEAD(&port->list);
529 port->padctl = padctl;
532 device_initialize(&port->dev);
533 port->dev.type = &tegra_xusb_port_type;
534 port->dev.of_node = of_node_get(np);
535 port->dev.parent = padctl->dev;
537 err = dev_set_name(&port->dev, "%s-%u", name, index);
541 err = device_add(&port->dev);
548 device_unregister(&port->dev);
552 static void tegra_xusb_port_unregister(struct tegra_xusb_port *port)
554 if (!IS_ERR_OR_NULL(port->usb_role_sw)) {
555 of_platform_depopulate(&port->dev);
556 usb_role_switch_unregister(port->usb_role_sw);
557 cancel_work_sync(&port->usb_phy_work);
558 usb_remove_phy(&port->usb_phy);
561 if (port->ops->remove)
562 port->ops->remove(port);
564 device_unregister(&port->dev);
567 static const char *const modes[] = {
568 [USB_DR_MODE_UNKNOWN] = "",
569 [USB_DR_MODE_HOST] = "host",
570 [USB_DR_MODE_PERIPHERAL] = "peripheral",
571 [USB_DR_MODE_OTG] = "otg",
574 static const char * const usb_roles[] = {
575 [USB_ROLE_NONE] = "none",
576 [USB_ROLE_HOST] = "host",
577 [USB_ROLE_DEVICE] = "device",
580 static enum usb_phy_events to_usb_phy_event(enum usb_role role)
583 case USB_ROLE_DEVICE:
584 return USB_EVENT_VBUS;
590 return USB_EVENT_NONE;
594 static void tegra_xusb_usb_phy_work(struct work_struct *work)
596 struct tegra_xusb_port *port = container_of(work,
597 struct tegra_xusb_port,
599 enum usb_role role = usb_role_switch_get_role(port->usb_role_sw);
601 usb_phy_set_event(&port->usb_phy, to_usb_phy_event(role));
603 dev_dbg(&port->dev, "%s(): calling notifier for role %s\n", __func__,
606 atomic_notifier_call_chain(&port->usb_phy.notifier, 0, &port->usb_phy);
609 static int tegra_xusb_role_sw_set(struct usb_role_switch *sw,
612 struct tegra_xusb_port *port = usb_role_switch_get_drvdata(sw);
614 dev_dbg(&port->dev, "%s(): role %s\n", __func__, usb_roles[role]);
616 schedule_work(&port->usb_phy_work);
621 static int tegra_xusb_set_peripheral(struct usb_otg *otg,
622 struct usb_gadget *gadget)
624 struct tegra_xusb_port *port = container_of(otg->usb_phy,
625 struct tegra_xusb_port,
629 schedule_work(&port->usb_phy_work);
634 static int tegra_xusb_set_host(struct usb_otg *otg, struct usb_bus *host)
636 struct tegra_xusb_port *port = container_of(otg->usb_phy,
637 struct tegra_xusb_port,
641 schedule_work(&port->usb_phy_work);
647 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port *port)
649 struct tegra_xusb_lane *lane;
650 struct usb_role_switch_desc role_sx_desc = {
651 .fwnode = dev_fwnode(&port->dev),
652 .set = tegra_xusb_role_sw_set,
657 * USB role switch driver needs parent driver owner info. This is a
658 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
659 * where an optimal solution is possible with changes to USB role
662 port->dev.driver = devm_kzalloc(&port->dev,
663 sizeof(struct device_driver),
665 port->dev.driver->owner = THIS_MODULE;
667 port->usb_role_sw = usb_role_switch_register(&port->dev,
669 if (IS_ERR(port->usb_role_sw)) {
670 err = PTR_ERR(port->usb_role_sw);
671 dev_err(&port->dev, "failed to register USB role switch: %d",
676 INIT_WORK(&port->usb_phy_work, tegra_xusb_usb_phy_work);
677 usb_role_switch_set_drvdata(port->usb_role_sw, port);
679 port->usb_phy.otg = devm_kzalloc(&port->dev, sizeof(struct usb_otg),
681 if (!port->usb_phy.otg)
684 lane = tegra_xusb_find_lane(port->padctl, "usb2", port->index);
687 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
688 * reference to retrieve usb-phy details.
690 port->usb_phy.dev = &lane->pad->lanes[port->index]->dev;
691 port->usb_phy.dev->driver = port->dev.driver;
692 port->usb_phy.otg->usb_phy = &port->usb_phy;
693 port->usb_phy.otg->set_peripheral = tegra_xusb_set_peripheral;
694 port->usb_phy.otg->set_host = tegra_xusb_set_host;
696 err = usb_add_phy_dev(&port->usb_phy);
698 dev_err(&port->dev, "Failed to add USB PHY: %d\n", err);
702 /* populate connector entry */
703 of_platform_populate(port->dev.of_node, NULL, NULL, &port->dev);
708 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port *usb2)
710 struct tegra_xusb_port *port = &usb2->base;
711 struct device_node *np = port->dev.of_node;
715 usb2->internal = of_property_read_bool(np, "nvidia,internal");
717 if (!of_property_read_string(np, "mode", &mode)) {
718 int err = match_string(modes, ARRAY_SIZE(modes), mode);
720 dev_err(&port->dev, "invalid value %s for \"mode\"\n",
722 usb2->mode = USB_DR_MODE_UNKNOWN;
727 usb2->mode = USB_DR_MODE_HOST;
730 /* usb-role-switch property is mandatory for OTG/Peripheral modes */
731 if (usb2->mode == USB_DR_MODE_PERIPHERAL ||
732 usb2->mode == USB_DR_MODE_OTG) {
733 if (of_property_read_bool(np, "usb-role-switch")) {
734 err = tegra_xusb_setup_usb_role_switch(port);
738 dev_err(&port->dev, "usb-role-switch not found for %s mode",
744 usb2->supply = regulator_get(&port->dev, "vbus");
745 return PTR_ERR_OR_ZERO(usb2->supply);
748 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl *padctl,
751 struct tegra_xusb_usb2_port *usb2;
752 struct device_node *np;
756 * USB2 ports don't require additional properties, but if the port is
757 * marked as disabled there is no reason to register it.
759 np = tegra_xusb_find_port_node(padctl, "usb2", index);
760 if (!np || !of_device_is_available(np))
763 usb2 = kzalloc(sizeof(*usb2), GFP_KERNEL);
769 err = tegra_xusb_port_init(&usb2->base, padctl, np, "usb2", index);
773 usb2->base.ops = padctl->soc->ports.usb2.ops;
775 usb2->base.lane = usb2->base.ops->map(&usb2->base);
776 if (IS_ERR(usb2->base.lane)) {
777 err = PTR_ERR(usb2->base.lane);
781 err = tegra_xusb_usb2_port_parse_dt(usb2);
783 tegra_xusb_port_unregister(&usb2->base);
787 list_add_tail(&usb2->base.list, &padctl->ports);
794 void tegra_xusb_usb2_port_release(struct tegra_xusb_port *port)
796 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
801 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port *port)
803 struct tegra_xusb_usb2_port *usb2 = to_usb2_port(port);
805 regulator_put(usb2->supply);
808 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port *ulpi)
810 struct tegra_xusb_port *port = &ulpi->base;
811 struct device_node *np = port->dev.of_node;
813 ulpi->internal = of_property_read_bool(np, "nvidia,internal");
818 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl *padctl,
821 struct tegra_xusb_ulpi_port *ulpi;
822 struct device_node *np;
825 np = tegra_xusb_find_port_node(padctl, "ulpi", index);
826 if (!np || !of_device_is_available(np))
829 ulpi = kzalloc(sizeof(*ulpi), GFP_KERNEL);
835 err = tegra_xusb_port_init(&ulpi->base, padctl, np, "ulpi", index);
839 ulpi->base.ops = padctl->soc->ports.ulpi.ops;
841 ulpi->base.lane = ulpi->base.ops->map(&ulpi->base);
842 if (IS_ERR(ulpi->base.lane)) {
843 err = PTR_ERR(ulpi->base.lane);
847 err = tegra_xusb_ulpi_port_parse_dt(ulpi);
849 tegra_xusb_port_unregister(&ulpi->base);
853 list_add_tail(&ulpi->base.list, &padctl->ports);
860 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port *port)
862 struct tegra_xusb_ulpi_port *ulpi = to_ulpi_port(port);
867 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port *hsic)
873 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl *padctl,
876 struct tegra_xusb_hsic_port *hsic;
877 struct device_node *np;
880 np = tegra_xusb_find_port_node(padctl, "hsic", index);
881 if (!np || !of_device_is_available(np))
884 hsic = kzalloc(sizeof(*hsic), GFP_KERNEL);
890 err = tegra_xusb_port_init(&hsic->base, padctl, np, "hsic", index);
894 hsic->base.ops = padctl->soc->ports.hsic.ops;
896 hsic->base.lane = hsic->base.ops->map(&hsic->base);
897 if (IS_ERR(hsic->base.lane)) {
898 err = PTR_ERR(hsic->base.lane);
902 err = tegra_xusb_hsic_port_parse_dt(hsic);
904 tegra_xusb_port_unregister(&hsic->base);
908 list_add_tail(&hsic->base.list, &padctl->ports);
915 void tegra_xusb_hsic_port_release(struct tegra_xusb_port *port)
917 struct tegra_xusb_hsic_port *hsic = to_hsic_port(port);
922 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port *usb3)
924 struct tegra_xusb_port *port = &usb3->base;
925 struct device_node *np = port->dev.of_node;
926 enum usb_device_speed maximum_speed;
930 err = of_property_read_u32(np, "nvidia,usb2-companion", &value);
932 dev_err(&port->dev, "failed to read port: %d\n", err);
938 usb3->internal = of_property_read_bool(np, "nvidia,internal");
940 if (device_property_present(&port->dev, "maximum-speed")) {
941 maximum_speed = usb_get_maximum_speed(&port->dev);
942 if (maximum_speed == USB_SPEED_SUPER)
943 usb3->disable_gen2 = true;
944 else if (maximum_speed == USB_SPEED_SUPER_PLUS)
945 usb3->disable_gen2 = false;
950 usb3->supply = regulator_get(&port->dev, "vbus");
951 return PTR_ERR_OR_ZERO(usb3->supply);
954 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl *padctl,
957 struct tegra_xusb_usb3_port *usb3;
958 struct device_node *np;
962 * If there is no supplemental configuration in the device tree the
963 * port is unusable. But it is valid to configure only a single port,
964 * hence return 0 instead of an error to allow ports to be optional.
966 np = tegra_xusb_find_port_node(padctl, "usb3", index);
967 if (!np || !of_device_is_available(np))
970 usb3 = kzalloc(sizeof(*usb3), GFP_KERNEL);
976 err = tegra_xusb_port_init(&usb3->base, padctl, np, "usb3", index);
980 usb3->base.ops = padctl->soc->ports.usb3.ops;
982 usb3->base.lane = usb3->base.ops->map(&usb3->base);
983 if (IS_ERR(usb3->base.lane)) {
984 err = PTR_ERR(usb3->base.lane);
988 err = tegra_xusb_usb3_port_parse_dt(usb3);
990 tegra_xusb_port_unregister(&usb3->base);
994 list_add_tail(&usb3->base.list, &padctl->ports);
1001 void tegra_xusb_usb3_port_release(struct tegra_xusb_port *port)
1003 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1008 void tegra_xusb_usb3_port_remove(struct tegra_xusb_port *port)
1010 struct tegra_xusb_usb3_port *usb3 = to_usb3_port(port);
1012 regulator_put(usb3->supply);
1015 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1017 struct tegra_xusb_port *port, *tmp;
1019 list_for_each_entry_safe_reverse(port, tmp, &padctl->ports, list) {
1020 list_del(&port->list);
1021 tegra_xusb_port_unregister(port);
1025 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl *padctl)
1027 struct device_node *np;
1030 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1031 np = tegra_xusb_find_port_node(padctl, "usb3", i);
1032 if (!np || !of_device_is_available(np))
1039 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port *usb2)
1042 struct tegra_xusb_usb3_port *usb3;
1043 struct tegra_xusb_padctl *padctl = usb2->base.padctl;
1045 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1046 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1047 if (usb3 && usb3->port == usb2->base.index)
1054 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port *usb2)
1058 /* Disable usb3_port_fake usage by default and assign if needed */
1059 usb2->usb3_port_fake = -1;
1061 if ((usb2->mode == USB_DR_MODE_OTG ||
1062 usb2->mode == USB_DR_MODE_PERIPHERAL) &&
1063 !tegra_xusb_port_is_companion(usb2)) {
1064 fake = tegra_xusb_find_unused_usb3_port(usb2->base.padctl);
1066 dev_err(&usb2->base.dev, "no unused USB3 ports available\n");
1070 dev_dbg(&usb2->base.dev, "Found unused usb3 port: %d\n", fake);
1071 usb2->usb3_port_fake = fake;
1077 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl *padctl)
1079 struct tegra_xusb_port *port;
1080 struct tegra_xusb_usb2_port *usb2;
1084 mutex_lock(&padctl->lock);
1086 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1087 err = tegra_xusb_add_usb2_port(padctl, i);
1092 for (i = 0; i < padctl->soc->ports.ulpi.count; i++) {
1093 err = tegra_xusb_add_ulpi_port(padctl, i);
1098 for (i = 0; i < padctl->soc->ports.hsic.count; i++) {
1099 err = tegra_xusb_add_hsic_port(padctl, i);
1104 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1105 err = tegra_xusb_add_usb3_port(padctl, i);
1110 if (padctl->soc->need_fake_usb3_port) {
1111 for (i = 0; i < padctl->soc->ports.usb2.count; i++) {
1112 usb2 = tegra_xusb_find_usb2_port(padctl, i);
1116 err = tegra_xusb_update_usb3_fake_port(usb2);
1122 list_for_each_entry(port, &padctl->ports, list) {
1123 err = port->ops->enable(port);
1125 dev_err(padctl->dev, "failed to enable port %s: %d\n",
1126 dev_name(&port->dev), err);
1132 __tegra_xusb_remove_ports(padctl);
1134 mutex_unlock(&padctl->lock);
1138 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl *padctl)
1140 mutex_lock(&padctl->lock);
1141 __tegra_xusb_remove_ports(padctl);
1142 mutex_unlock(&padctl->lock);
1145 static int tegra_xusb_padctl_probe(struct platform_device *pdev)
1147 struct device_node *np = pdev->dev.of_node;
1148 const struct tegra_xusb_padctl_soc *soc;
1149 struct tegra_xusb_padctl *padctl;
1150 const struct of_device_id *match;
1151 struct resource *res;
1154 /* for backwards compatibility with old device trees */
1155 np = of_get_child_by_name(np, "pads");
1157 dev_warn(&pdev->dev, "deprecated DT, using legacy driver\n");
1158 return tegra_xusb_padctl_legacy_probe(pdev);
1163 match = of_match_node(tegra_xusb_padctl_of_match, pdev->dev.of_node);
1166 padctl = soc->ops->probe(&pdev->dev, soc);
1168 return PTR_ERR(padctl);
1170 platform_set_drvdata(pdev, padctl);
1171 INIT_LIST_HEAD(&padctl->ports);
1172 INIT_LIST_HEAD(&padctl->lanes);
1173 INIT_LIST_HEAD(&padctl->pads);
1174 mutex_init(&padctl->lock);
1176 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1177 padctl->regs = devm_ioremap_resource(&pdev->dev, res);
1178 if (IS_ERR(padctl->regs)) {
1179 err = PTR_ERR(padctl->regs);
1183 padctl->rst = devm_reset_control_get(&pdev->dev, NULL);
1184 if (IS_ERR(padctl->rst)) {
1185 err = PTR_ERR(padctl->rst);
1189 padctl->supplies = devm_kcalloc(&pdev->dev, padctl->soc->num_supplies,
1190 sizeof(*padctl->supplies), GFP_KERNEL);
1191 if (!padctl->supplies) {
1196 regulator_bulk_set_supply_names(padctl->supplies,
1197 padctl->soc->supply_names,
1198 padctl->soc->num_supplies);
1200 err = devm_regulator_bulk_get(&pdev->dev, padctl->soc->num_supplies,
1203 dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1207 err = reset_control_deassert(padctl->rst);
1211 err = regulator_bulk_enable(padctl->soc->num_supplies,
1214 dev_err(&pdev->dev, "failed to enable supplies: %d\n", err);
1218 err = tegra_xusb_setup_pads(padctl);
1220 dev_err(&pdev->dev, "failed to setup pads: %d\n", err);
1224 err = tegra_xusb_setup_ports(padctl);
1226 const char *level = KERN_ERR;
1228 if (err == -EPROBE_DEFER)
1231 dev_printk(level, &pdev->dev,
1232 dev_fmt("failed to setup XUSB ports: %d\n"), err);
1239 tegra_xusb_remove_pads(padctl);
1241 regulator_bulk_disable(padctl->soc->num_supplies, padctl->supplies);
1243 reset_control_assert(padctl->rst);
1245 platform_set_drvdata(pdev, NULL);
1246 soc->ops->remove(padctl);
1250 static int tegra_xusb_padctl_remove(struct platform_device *pdev)
1252 struct tegra_xusb_padctl *padctl = platform_get_drvdata(pdev);
1255 tegra_xusb_remove_ports(padctl);
1256 tegra_xusb_remove_pads(padctl);
1258 err = regulator_bulk_disable(padctl->soc->num_supplies,
1261 dev_err(&pdev->dev, "failed to disable supplies: %d\n", err);
1263 err = reset_control_assert(padctl->rst);
1265 dev_err(&pdev->dev, "failed to assert reset: %d\n", err);
1267 padctl->soc->ops->remove(padctl);
1272 static struct platform_driver tegra_xusb_padctl_driver = {
1274 .name = "tegra-xusb-padctl",
1275 .of_match_table = tegra_xusb_padctl_of_match,
1277 .probe = tegra_xusb_padctl_probe,
1278 .remove = tegra_xusb_padctl_remove,
1280 module_platform_driver(tegra_xusb_padctl_driver);
1282 struct tegra_xusb_padctl *tegra_xusb_padctl_get(struct device *dev)
1284 struct tegra_xusb_padctl *padctl;
1285 struct platform_device *pdev;
1286 struct device_node *np;
1288 np = of_parse_phandle(dev->of_node, "nvidia,xusb-padctl", 0);
1290 return ERR_PTR(-EINVAL);
1293 * This is slightly ugly. A better implementation would be to keep a
1294 * registry of pad controllers, but since there will almost certainly
1295 * only ever be one per SoC that would be a little overkill.
1297 pdev = of_find_device_by_node(np);
1300 return ERR_PTR(-ENODEV);
1305 padctl = platform_get_drvdata(pdev);
1307 put_device(&pdev->dev);
1308 return ERR_PTR(-EPROBE_DEFER);
1313 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get);
1315 void tegra_xusb_padctl_put(struct tegra_xusb_padctl *padctl)
1318 put_device(padctl->dev);
1320 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put);
1322 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl *padctl,
1325 if (padctl->soc->ops->usb3_save_context)
1326 return padctl->soc->ops->usb3_save_context(padctl, port);
1330 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context);
1332 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl *padctl,
1333 unsigned int port, bool idle)
1335 if (padctl->soc->ops->hsic_set_idle)
1336 return padctl->soc->ops->hsic_set_idle(padctl, port, idle);
1340 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle);
1342 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl *padctl,
1343 unsigned int port, bool enable)
1345 if (padctl->soc->ops->usb3_set_lfps_detect)
1346 return padctl->soc->ops->usb3_set_lfps_detect(padctl, port,
1351 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect);
1353 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl *padctl,
1356 if (padctl->soc->ops->vbus_override)
1357 return padctl->soc->ops->vbus_override(padctl, val);
1361 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override);
1363 int tegra_phy_xusb_utmi_port_reset(struct phy *phy)
1365 struct tegra_xusb_lane *lane = phy_get_drvdata(phy);
1366 struct tegra_xusb_padctl *padctl = lane->pad->padctl;
1368 if (padctl->soc->ops->utmi_port_reset)
1369 return padctl->soc->ops->utmi_port_reset(phy);
1373 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset);
1375 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl *padctl,
1378 struct tegra_xusb_usb2_port *usb2;
1379 struct tegra_xusb_usb3_port *usb3;
1382 usb2 = tegra_xusb_find_usb2_port(padctl, port);
1386 for (i = 0; i < padctl->soc->ports.usb3.count; i++) {
1387 usb3 = tegra_xusb_find_usb3_port(padctl, i);
1388 if (usb3 && usb3->port == usb2->base.index)
1389 return usb3->base.index;
1394 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion);
1396 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1397 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1398 MODULE_LICENSE("GPL v2");