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_platform.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 const 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
,
75 #if defined(CONFIG_ARCH_TEGRA_234_SOC)
77 .compatible
= "nvidia,tegra234-xusb-padctl",
78 .data
= &tegra234_xusb_padctl_soc
,
83 MODULE_DEVICE_TABLE(of
, tegra_xusb_padctl_of_match
);
85 static struct device_node
*
86 tegra_xusb_find_pad_node(struct tegra_xusb_padctl
*padctl
, const char *name
)
88 struct device_node
*pads
, *np
;
90 pads
= of_get_child_by_name(padctl
->dev
->of_node
, "pads");
94 np
= of_get_child_by_name(pads
, name
);
100 static struct device_node
*
101 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad
*pad
, unsigned int index
)
103 struct device_node
*np
, *lanes
;
105 lanes
= of_get_child_by_name(pad
->dev
.of_node
, "lanes");
109 np
= of_get_child_by_name(lanes
, pad
->soc
->lanes
[index
].name
);
115 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane
*lane
,
116 struct device_node
*np
)
118 struct device
*dev
= &lane
->pad
->dev
;
119 const char *function
;
122 err
= of_property_read_string(np
, "nvidia,function", &function
);
126 err
= match_string(lane
->soc
->funcs
, lane
->soc
->num_funcs
, function
);
128 dev_err(dev
, "invalid function \"%s\" for lane \"%pOFn\"\n",
133 lane
->function
= err
;
138 static void tegra_xusb_lane_destroy(struct phy
*phy
)
141 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
143 lane
->pad
->ops
->remove(lane
);
148 static void tegra_xusb_pad_release(struct device
*dev
)
150 struct tegra_xusb_pad
*pad
= to_tegra_xusb_pad(dev
);
152 pad
->soc
->ops
->remove(pad
);
155 static const struct device_type tegra_xusb_pad_type
= {
156 .release
= tegra_xusb_pad_release
,
159 int tegra_xusb_pad_init(struct tegra_xusb_pad
*pad
,
160 struct tegra_xusb_padctl
*padctl
,
161 struct device_node
*np
)
165 device_initialize(&pad
->dev
);
166 INIT_LIST_HEAD(&pad
->list
);
167 pad
->dev
.parent
= padctl
->dev
;
168 pad
->dev
.type
= &tegra_xusb_pad_type
;
169 pad
->dev
.of_node
= np
;
170 pad
->padctl
= padctl
;
172 err
= dev_set_name(&pad
->dev
, "%s", pad
->soc
->name
);
176 err
= device_add(&pad
->dev
);
183 device_unregister(&pad
->dev
);
187 int tegra_xusb_pad_register(struct tegra_xusb_pad
*pad
,
188 const struct phy_ops
*ops
)
190 struct device_node
*children
;
195 children
= of_get_child_by_name(pad
->dev
.of_node
, "lanes");
199 pad
->lanes
= devm_kcalloc(&pad
->dev
, pad
->soc
->num_lanes
, sizeof(lane
),
202 of_node_put(children
);
206 for (i
= 0; i
< pad
->soc
->num_lanes
; i
++) {
207 struct device_node
*np
= tegra_xusb_pad_find_phy_node(pad
, i
);
208 struct tegra_xusb_lane
*lane
;
210 /* skip disabled lanes */
211 if (!np
|| !of_device_is_available(np
)) {
216 pad
->lanes
[i
] = phy_create(&pad
->dev
, np
, ops
);
217 if (IS_ERR(pad
->lanes
[i
])) {
218 err
= PTR_ERR(pad
->lanes
[i
]);
223 lane
= pad
->ops
->probe(pad
, np
, i
);
225 phy_destroy(pad
->lanes
[i
]);
230 list_add_tail(&lane
->list
, &pad
->padctl
->lanes
);
231 phy_set_drvdata(pad
->lanes
[i
], lane
);
234 pad
->provider
= of_phy_provider_register_full(&pad
->dev
, children
,
235 tegra_xusb_pad_of_xlate
);
236 if (IS_ERR(pad
->provider
)) {
237 err
= PTR_ERR(pad
->provider
);
245 tegra_xusb_lane_destroy(pad
->lanes
[i
]);
247 of_node_put(children
);
252 void tegra_xusb_pad_unregister(struct tegra_xusb_pad
*pad
)
254 unsigned int i
= pad
->soc
->num_lanes
;
256 of_phy_provider_unregister(pad
->provider
);
259 tegra_xusb_lane_destroy(pad
->lanes
[i
]);
261 device_unregister(&pad
->dev
);
264 static struct tegra_xusb_pad
*
265 tegra_xusb_pad_create(struct tegra_xusb_padctl
*padctl
,
266 const struct tegra_xusb_pad_soc
*soc
)
268 struct tegra_xusb_pad
*pad
;
269 struct device_node
*np
;
272 np
= tegra_xusb_find_pad_node(padctl
, soc
->name
);
273 if (!np
|| !of_device_is_available(np
))
276 pad
= soc
->ops
->probe(padctl
, soc
, np
);
279 dev_err(padctl
->dev
, "failed to create pad %s: %d\n",
284 /* XXX move this into ->probe() to avoid string comparison */
285 if (strcmp(soc
->name
, "pcie") == 0)
288 if (strcmp(soc
->name
, "sata") == 0)
291 if (strcmp(soc
->name
, "usb2") == 0)
294 if (strcmp(soc
->name
, "ulpi") == 0)
297 if (strcmp(soc
->name
, "hsic") == 0)
303 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl
*padctl
)
305 struct tegra_xusb_pad
*pad
, *tmp
;
307 list_for_each_entry_safe_reverse(pad
, tmp
, &padctl
->pads
, list
) {
308 list_del(&pad
->list
);
309 tegra_xusb_pad_unregister(pad
);
313 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl
*padctl
)
315 mutex_lock(&padctl
->lock
);
316 __tegra_xusb_remove_pads(padctl
);
317 mutex_unlock(&padctl
->lock
);
320 static void tegra_xusb_lane_program(struct tegra_xusb_lane
*lane
)
322 struct tegra_xusb_padctl
*padctl
= lane
->pad
->padctl
;
323 const struct tegra_xusb_lane_soc
*soc
= lane
->soc
;
326 /* skip single function lanes */
327 if (soc
->num_funcs
< 2)
330 if (lane
->pad
->ops
->iddq_enable
)
331 lane
->pad
->ops
->iddq_enable(lane
);
333 /* choose function */
334 value
= padctl_readl(padctl
, soc
->offset
);
335 value
&= ~(soc
->mask
<< soc
->shift
);
336 value
|= lane
->function
<< soc
->shift
;
337 padctl_writel(padctl
, value
, soc
->offset
);
339 if (lane
->pad
->ops
->iddq_disable
)
340 lane
->pad
->ops
->iddq_disable(lane
);
343 static void tegra_xusb_pad_program(struct tegra_xusb_pad
*pad
)
347 for (i
= 0; i
< pad
->soc
->num_lanes
; i
++) {
348 struct tegra_xusb_lane
*lane
;
351 lane
= phy_get_drvdata(pad
->lanes
[i
]);
352 tegra_xusb_lane_program(lane
);
357 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl
*padctl
)
359 struct tegra_xusb_pad
*pad
;
362 mutex_lock(&padctl
->lock
);
364 for (i
= 0; i
< padctl
->soc
->num_pads
; i
++) {
365 const struct tegra_xusb_pad_soc
*soc
= padctl
->soc
->pads
[i
];
368 pad
= tegra_xusb_pad_create(padctl
, soc
);
371 dev_err(padctl
->dev
, "failed to create pad %s: %d\n",
373 __tegra_xusb_remove_pads(padctl
);
374 mutex_unlock(&padctl
->lock
);
381 list_add_tail(&pad
->list
, &padctl
->pads
);
384 list_for_each_entry(pad
, &padctl
->pads
, list
)
385 tegra_xusb_pad_program(pad
);
387 mutex_unlock(&padctl
->lock
);
391 bool tegra_xusb_lane_check(struct tegra_xusb_lane
*lane
,
392 const char *function
)
394 const char *func
= lane
->soc
->funcs
[lane
->function
];
396 return strcmp(function
, func
) == 0;
399 struct tegra_xusb_lane
*tegra_xusb_find_lane(struct tegra_xusb_padctl
*padctl
,
403 struct tegra_xusb_lane
*lane
, *hit
= ERR_PTR(-ENODEV
);
406 name
= kasprintf(GFP_KERNEL
, "%s-%u", type
, index
);
408 return ERR_PTR(-ENOMEM
);
410 list_for_each_entry(lane
, &padctl
->lanes
, list
) {
411 if (strcmp(lane
->soc
->name
, name
) == 0) {
421 struct tegra_xusb_lane
*
422 tegra_xusb_port_find_lane(struct tegra_xusb_port
*port
,
423 const struct tegra_xusb_lane_map
*map
,
424 const char *function
)
426 struct tegra_xusb_lane
*lane
, *match
= ERR_PTR(-ENODEV
);
428 for (; map
->type
; map
++) {
429 if (port
->index
!= map
->port
)
432 lane
= tegra_xusb_find_lane(port
->padctl
, map
->type
,
437 if (!tegra_xusb_lane_check(lane
, function
))
441 dev_err(&port
->dev
, "conflicting match: %s-%u / %s\n",
442 map
->type
, map
->index
, match
->soc
->name
);
450 static struct device_node
*
451 tegra_xusb_find_port_node(struct tegra_xusb_padctl
*padctl
, const char *type
,
454 struct device_node
*ports
, *np
;
457 ports
= of_get_child_by_name(padctl
->dev
->of_node
, "ports");
461 name
= kasprintf(GFP_KERNEL
, "%s-%u", type
, index
);
466 np
= of_get_child_by_name(ports
, name
);
473 struct tegra_xusb_port
*
474 tegra_xusb_find_port(struct tegra_xusb_padctl
*padctl
, const char *type
,
477 struct tegra_xusb_port
*port
;
478 struct device_node
*np
;
480 np
= tegra_xusb_find_port_node(padctl
, type
, index
);
484 list_for_each_entry(port
, &padctl
->ports
, list
) {
485 if (np
== port
->dev
.of_node
) {
496 struct tegra_xusb_usb2_port
*
497 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl
*padctl
, unsigned int index
)
499 struct tegra_xusb_port
*port
;
501 port
= tegra_xusb_find_port(padctl
, "usb2", index
);
503 return to_usb2_port(port
);
508 struct tegra_xusb_usb3_port
*
509 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl
*padctl
, unsigned int index
)
511 struct tegra_xusb_port
*port
;
513 port
= tegra_xusb_find_port(padctl
, "usb3", index
);
515 return to_usb3_port(port
);
520 static void tegra_xusb_port_release(struct device
*dev
)
522 struct tegra_xusb_port
*port
= to_tegra_xusb_port(dev
);
524 if (port
->ops
->release
)
525 port
->ops
->release(port
);
528 static const struct device_type tegra_xusb_port_type
= {
529 .release
= tegra_xusb_port_release
,
532 static int tegra_xusb_port_init(struct tegra_xusb_port
*port
,
533 struct tegra_xusb_padctl
*padctl
,
534 struct device_node
*np
,
540 INIT_LIST_HEAD(&port
->list
);
541 port
->padctl
= padctl
;
544 device_initialize(&port
->dev
);
545 port
->dev
.type
= &tegra_xusb_port_type
;
546 device_set_node(&port
->dev
, of_fwnode_handle(of_node_get(np
)));
547 port
->dev
.parent
= padctl
->dev
;
549 err
= dev_set_name(&port
->dev
, "%s-%u", name
, index
);
553 err
= device_add(&port
->dev
);
560 device_unregister(&port
->dev
);
564 static void tegra_xusb_port_unregister(struct tegra_xusb_port
*port
)
566 if (!IS_ERR_OR_NULL(port
->usb_role_sw
)) {
567 of_platform_depopulate(&port
->dev
);
568 usb_role_switch_unregister(port
->usb_role_sw
);
569 cancel_work_sync(&port
->usb_phy_work
);
570 usb_remove_phy(&port
->usb_phy
);
571 port
->usb_phy
.dev
->driver
= NULL
;
574 if (port
->ops
->remove
)
575 port
->ops
->remove(port
);
577 device_unregister(&port
->dev
);
580 static const char *const modes
[] = {
581 [USB_DR_MODE_UNKNOWN
] = "",
582 [USB_DR_MODE_HOST
] = "host",
583 [USB_DR_MODE_PERIPHERAL
] = "peripheral",
584 [USB_DR_MODE_OTG
] = "otg",
587 static const char * const usb_roles
[] = {
588 [USB_ROLE_NONE
] = "none",
589 [USB_ROLE_HOST
] = "host",
590 [USB_ROLE_DEVICE
] = "device",
593 static enum usb_phy_events
to_usb_phy_event(enum usb_role role
)
596 case USB_ROLE_DEVICE
:
597 return USB_EVENT_VBUS
;
603 return USB_EVENT_NONE
;
607 static void tegra_xusb_usb_phy_work(struct work_struct
*work
)
609 struct tegra_xusb_port
*port
= container_of(work
,
610 struct tegra_xusb_port
,
612 enum usb_role role
= usb_role_switch_get_role(port
->usb_role_sw
);
614 usb_phy_set_event(&port
->usb_phy
, to_usb_phy_event(role
));
616 dev_dbg(&port
->dev
, "%s(): calling notifier for role %s\n", __func__
,
619 atomic_notifier_call_chain(&port
->usb_phy
.notifier
, 0, &port
->usb_phy
);
622 static int tegra_xusb_role_sw_set(struct usb_role_switch
*sw
,
625 struct tegra_xusb_port
*port
= usb_role_switch_get_drvdata(sw
);
627 dev_dbg(&port
->dev
, "%s(): role %s\n", __func__
, usb_roles
[role
]);
629 schedule_work(&port
->usb_phy_work
);
634 static int tegra_xusb_set_peripheral(struct usb_otg
*otg
,
635 struct usb_gadget
*gadget
)
637 struct tegra_xusb_port
*port
= container_of(otg
->usb_phy
,
638 struct tegra_xusb_port
,
642 schedule_work(&port
->usb_phy_work
);
647 static int tegra_xusb_set_host(struct usb_otg
*otg
, struct usb_bus
*host
)
649 struct tegra_xusb_port
*port
= container_of(otg
->usb_phy
,
650 struct tegra_xusb_port
,
654 schedule_work(&port
->usb_phy_work
);
660 static int tegra_xusb_setup_usb_role_switch(struct tegra_xusb_port
*port
)
662 struct tegra_xusb_lane
*lane
;
663 struct usb_role_switch_desc role_sx_desc
= {
664 .fwnode
= dev_fwnode(&port
->dev
),
665 .set
= tegra_xusb_role_sw_set
,
666 .allow_userspace_control
= true,
671 * USB role switch driver needs parent driver owner info. This is a
672 * suboptimal solution. TODO: Need to revisit this in a follow-up patch
673 * where an optimal solution is possible with changes to USB role
676 port
->dev
.driver
= devm_kzalloc(&port
->dev
,
677 sizeof(struct device_driver
),
679 if (!port
->dev
.driver
)
682 port
->dev
.driver
->owner
= THIS_MODULE
;
684 port
->usb_role_sw
= usb_role_switch_register(&port
->dev
,
686 if (IS_ERR(port
->usb_role_sw
)) {
687 err
= PTR_ERR(port
->usb_role_sw
);
688 dev_err(&port
->dev
, "failed to register USB role switch: %d",
693 INIT_WORK(&port
->usb_phy_work
, tegra_xusb_usb_phy_work
);
694 usb_role_switch_set_drvdata(port
->usb_role_sw
, port
);
696 port
->usb_phy
.otg
= devm_kzalloc(&port
->dev
, sizeof(struct usb_otg
),
698 if (!port
->usb_phy
.otg
)
701 lane
= tegra_xusb_find_lane(port
->padctl
, "usb2", port
->index
);
703 return PTR_ERR(lane
);
706 * Assign phy dev to usb-phy dev. Host/device drivers can use phy
707 * reference to retrieve usb-phy details.
709 port
->usb_phy
.dev
= &lane
->pad
->lanes
[port
->index
]->dev
;
710 port
->usb_phy
.dev
->driver
= port
->dev
.driver
;
711 port
->usb_phy
.otg
->usb_phy
= &port
->usb_phy
;
712 port
->usb_phy
.otg
->set_peripheral
= tegra_xusb_set_peripheral
;
713 port
->usb_phy
.otg
->set_host
= tegra_xusb_set_host
;
715 err
= usb_add_phy_dev(&port
->usb_phy
);
717 dev_err(&port
->dev
, "Failed to add USB PHY: %d\n", err
);
721 /* populate connector entry */
722 of_platform_populate(port
->dev
.of_node
, NULL
, NULL
, &port
->dev
);
727 static void tegra_xusb_parse_usb_role_default_mode(struct tegra_xusb_port
*port
)
729 enum usb_role role
= USB_ROLE_NONE
;
730 enum usb_dr_mode mode
= usb_get_role_switch_default_mode(&port
->dev
);
732 if (mode
== USB_DR_MODE_HOST
)
733 role
= USB_ROLE_HOST
;
734 else if (mode
== USB_DR_MODE_PERIPHERAL
)
735 role
= USB_ROLE_DEVICE
;
737 if (role
!= USB_ROLE_NONE
) {
738 usb_role_switch_set_role(port
->usb_role_sw
, role
);
739 dev_dbg(&port
->dev
, "usb role default mode is %s", modes
[mode
]);
743 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port
*usb2
)
745 struct tegra_xusb_port
*port
= &usb2
->base
;
746 struct device_node
*np
= port
->dev
.of_node
;
750 usb2
->internal
= of_property_read_bool(np
, "nvidia,internal");
752 if (!of_property_read_string(np
, "mode", &mode
)) {
753 int err
= match_string(modes
, ARRAY_SIZE(modes
), mode
);
755 dev_err(&port
->dev
, "invalid value %s for \"mode\"\n",
757 usb2
->mode
= USB_DR_MODE_UNKNOWN
;
762 usb2
->mode
= USB_DR_MODE_HOST
;
765 /* usb-role-switch property is mandatory for OTG/Peripheral modes */
766 if (usb2
->mode
== USB_DR_MODE_PERIPHERAL
||
767 usb2
->mode
== USB_DR_MODE_OTG
) {
768 if (of_property_read_bool(np
, "usb-role-switch")) {
769 err
= tegra_xusb_setup_usb_role_switch(port
);
772 tegra_xusb_parse_usb_role_default_mode(port
);
774 dev_err(&port
->dev
, "usb-role-switch not found for %s mode",
780 usb2
->supply
= regulator_get(&port
->dev
, "vbus");
781 return PTR_ERR_OR_ZERO(usb2
->supply
);
784 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl
*padctl
,
787 struct tegra_xusb_usb2_port
*usb2
;
788 struct device_node
*np
;
792 * USB2 ports don't require additional properties, but if the port is
793 * marked as disabled there is no reason to register it.
795 np
= tegra_xusb_find_port_node(padctl
, "usb2", index
);
796 if (!np
|| !of_device_is_available(np
))
799 usb2
= kzalloc(sizeof(*usb2
), GFP_KERNEL
);
805 err
= tegra_xusb_port_init(&usb2
->base
, padctl
, np
, "usb2", index
);
809 usb2
->base
.ops
= padctl
->soc
->ports
.usb2
.ops
;
811 usb2
->base
.lane
= usb2
->base
.ops
->map(&usb2
->base
);
812 if (IS_ERR(usb2
->base
.lane
)) {
813 err
= PTR_ERR(usb2
->base
.lane
);
814 tegra_xusb_port_unregister(&usb2
->base
);
818 err
= tegra_xusb_usb2_port_parse_dt(usb2
);
820 tegra_xusb_port_unregister(&usb2
->base
);
824 list_add_tail(&usb2
->base
.list
, &padctl
->ports
);
831 void tegra_xusb_usb2_port_release(struct tegra_xusb_port
*port
)
833 struct tegra_xusb_usb2_port
*usb2
= to_usb2_port(port
);
838 void tegra_xusb_usb2_port_remove(struct tegra_xusb_port
*port
)
840 struct tegra_xusb_usb2_port
*usb2
= to_usb2_port(port
);
842 regulator_put(usb2
->supply
);
845 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port
*ulpi
)
847 struct tegra_xusb_port
*port
= &ulpi
->base
;
848 struct device_node
*np
= port
->dev
.of_node
;
850 ulpi
->internal
= of_property_read_bool(np
, "nvidia,internal");
855 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl
*padctl
,
858 struct tegra_xusb_ulpi_port
*ulpi
;
859 struct device_node
*np
;
862 np
= tegra_xusb_find_port_node(padctl
, "ulpi", index
);
863 if (!np
|| !of_device_is_available(np
))
866 ulpi
= kzalloc(sizeof(*ulpi
), GFP_KERNEL
);
872 err
= tegra_xusb_port_init(&ulpi
->base
, padctl
, np
, "ulpi", index
);
876 ulpi
->base
.ops
= padctl
->soc
->ports
.ulpi
.ops
;
878 ulpi
->base
.lane
= ulpi
->base
.ops
->map(&ulpi
->base
);
879 if (IS_ERR(ulpi
->base
.lane
)) {
880 err
= PTR_ERR(ulpi
->base
.lane
);
881 tegra_xusb_port_unregister(&ulpi
->base
);
885 err
= tegra_xusb_ulpi_port_parse_dt(ulpi
);
887 tegra_xusb_port_unregister(&ulpi
->base
);
891 list_add_tail(&ulpi
->base
.list
, &padctl
->ports
);
898 void tegra_xusb_ulpi_port_release(struct tegra_xusb_port
*port
)
900 struct tegra_xusb_ulpi_port
*ulpi
= to_ulpi_port(port
);
905 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port
*hsic
)
911 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl
*padctl
,
914 struct tegra_xusb_hsic_port
*hsic
;
915 struct device_node
*np
;
918 np
= tegra_xusb_find_port_node(padctl
, "hsic", index
);
919 if (!np
|| !of_device_is_available(np
))
922 hsic
= kzalloc(sizeof(*hsic
), GFP_KERNEL
);
928 err
= tegra_xusb_port_init(&hsic
->base
, padctl
, np
, "hsic", index
);
932 hsic
->base
.ops
= padctl
->soc
->ports
.hsic
.ops
;
934 hsic
->base
.lane
= hsic
->base
.ops
->map(&hsic
->base
);
935 if (IS_ERR(hsic
->base
.lane
)) {
936 err
= PTR_ERR(hsic
->base
.lane
);
940 err
= tegra_xusb_hsic_port_parse_dt(hsic
);
942 tegra_xusb_port_unregister(&hsic
->base
);
946 list_add_tail(&hsic
->base
.list
, &padctl
->ports
);
953 void tegra_xusb_hsic_port_release(struct tegra_xusb_port
*port
)
955 struct tegra_xusb_hsic_port
*hsic
= to_hsic_port(port
);
960 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port
*usb3
)
962 struct tegra_xusb_port
*port
= &usb3
->base
;
963 struct device_node
*np
= port
->dev
.of_node
;
964 enum usb_device_speed maximum_speed
;
968 err
= of_property_read_u32(np
, "nvidia,usb2-companion", &value
);
970 dev_err(&port
->dev
, "failed to read port: %d\n", err
);
976 usb3
->internal
= of_property_read_bool(np
, "nvidia,internal");
978 if (device_property_present(&port
->dev
, "maximum-speed")) {
979 maximum_speed
= usb_get_maximum_speed(&port
->dev
);
980 if (maximum_speed
== USB_SPEED_SUPER
)
981 usb3
->disable_gen2
= true;
982 else if (maximum_speed
== USB_SPEED_SUPER_PLUS
)
983 usb3
->disable_gen2
= false;
991 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl
*padctl
,
994 struct tegra_xusb_usb3_port
*usb3
;
995 struct device_node
*np
;
999 * If there is no supplemental configuration in the device tree the
1000 * port is unusable. But it is valid to configure only a single port,
1001 * hence return 0 instead of an error to allow ports to be optional.
1003 np
= tegra_xusb_find_port_node(padctl
, "usb3", index
);
1004 if (!np
|| !of_device_is_available(np
))
1007 usb3
= kzalloc(sizeof(*usb3
), GFP_KERNEL
);
1013 err
= tegra_xusb_port_init(&usb3
->base
, padctl
, np
, "usb3", index
);
1017 usb3
->base
.ops
= padctl
->soc
->ports
.usb3
.ops
;
1019 usb3
->base
.lane
= usb3
->base
.ops
->map(&usb3
->base
);
1020 if (IS_ERR(usb3
->base
.lane
)) {
1021 err
= PTR_ERR(usb3
->base
.lane
);
1025 err
= tegra_xusb_usb3_port_parse_dt(usb3
);
1027 tegra_xusb_port_unregister(&usb3
->base
);
1031 list_add_tail(&usb3
->base
.list
, &padctl
->ports
);
1038 void tegra_xusb_usb3_port_release(struct tegra_xusb_port
*port
)
1040 struct tegra_xusb_usb3_port
*usb3
= to_usb3_port(port
);
1045 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl
*padctl
)
1047 struct tegra_xusb_port
*port
, *tmp
;
1049 list_for_each_entry_safe_reverse(port
, tmp
, &padctl
->ports
, list
) {
1050 list_del(&port
->list
);
1051 tegra_xusb_port_unregister(port
);
1055 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl
*padctl
)
1057 struct device_node
*np
;
1060 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
1061 np
= tegra_xusb_find_port_node(padctl
, "usb3", i
);
1062 if (!np
|| !of_device_is_available(np
))
1069 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port
*usb2
)
1072 struct tegra_xusb_usb3_port
*usb3
;
1073 struct tegra_xusb_padctl
*padctl
= usb2
->base
.padctl
;
1075 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
1076 usb3
= tegra_xusb_find_usb3_port(padctl
, i
);
1077 if (usb3
&& usb3
->port
== usb2
->base
.index
)
1084 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port
*usb2
)
1088 /* Disable usb3_port_fake usage by default and assign if needed */
1089 usb2
->usb3_port_fake
= -1;
1091 if ((usb2
->mode
== USB_DR_MODE_OTG
||
1092 usb2
->mode
== USB_DR_MODE_PERIPHERAL
) &&
1093 !tegra_xusb_port_is_companion(usb2
)) {
1094 fake
= tegra_xusb_find_unused_usb3_port(usb2
->base
.padctl
);
1096 dev_err(&usb2
->base
.dev
, "no unused USB3 ports available\n");
1100 dev_dbg(&usb2
->base
.dev
, "Found unused usb3 port: %d\n", fake
);
1101 usb2
->usb3_port_fake
= fake
;
1107 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl
*padctl
)
1109 struct tegra_xusb_port
*port
;
1110 struct tegra_xusb_usb2_port
*usb2
;
1114 mutex_lock(&padctl
->lock
);
1116 for (i
= 0; i
< padctl
->soc
->ports
.usb2
.count
; i
++) {
1117 err
= tegra_xusb_add_usb2_port(padctl
, i
);
1122 for (i
= 0; i
< padctl
->soc
->ports
.ulpi
.count
; i
++) {
1123 err
= tegra_xusb_add_ulpi_port(padctl
, i
);
1128 for (i
= 0; i
< padctl
->soc
->ports
.hsic
.count
; i
++) {
1129 err
= tegra_xusb_add_hsic_port(padctl
, i
);
1134 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
1135 err
= tegra_xusb_add_usb3_port(padctl
, i
);
1140 if (padctl
->soc
->need_fake_usb3_port
) {
1141 for (i
= 0; i
< padctl
->soc
->ports
.usb2
.count
; i
++) {
1142 usb2
= tegra_xusb_find_usb2_port(padctl
, i
);
1146 err
= tegra_xusb_update_usb3_fake_port(usb2
);
1152 list_for_each_entry(port
, &padctl
->ports
, list
) {
1153 err
= port
->ops
->enable(port
);
1155 dev_err(padctl
->dev
, "failed to enable port %s: %d\n",
1156 dev_name(&port
->dev
), err
);
1162 __tegra_xusb_remove_ports(padctl
);
1164 mutex_unlock(&padctl
->lock
);
1168 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl
*padctl
)
1170 mutex_lock(&padctl
->lock
);
1171 __tegra_xusb_remove_ports(padctl
);
1172 mutex_unlock(&padctl
->lock
);
1175 static int tegra_xusb_padctl_probe(struct platform_device
*pdev
)
1177 struct device_node
*np
= pdev
->dev
.of_node
;
1178 const struct tegra_xusb_padctl_soc
*soc
;
1179 struct tegra_xusb_padctl
*padctl
;
1180 const struct of_device_id
*match
;
1183 /* for backwards compatibility with old device trees */
1184 np
= of_get_child_by_name(np
, "pads");
1186 dev_warn(&pdev
->dev
, "deprecated DT, using legacy driver\n");
1187 return tegra_xusb_padctl_legacy_probe(pdev
);
1192 match
= of_match_node(tegra_xusb_padctl_of_match
, pdev
->dev
.of_node
);
1195 padctl
= soc
->ops
->probe(&pdev
->dev
, soc
);
1197 return PTR_ERR(padctl
);
1199 platform_set_drvdata(pdev
, padctl
);
1200 INIT_LIST_HEAD(&padctl
->ports
);
1201 INIT_LIST_HEAD(&padctl
->lanes
);
1202 INIT_LIST_HEAD(&padctl
->pads
);
1203 mutex_init(&padctl
->lock
);
1205 padctl
->regs
= devm_platform_ioremap_resource(pdev
, 0);
1206 if (IS_ERR(padctl
->regs
)) {
1207 err
= PTR_ERR(padctl
->regs
);
1211 padctl
->rst
= devm_reset_control_get(&pdev
->dev
, NULL
);
1212 if (IS_ERR(padctl
->rst
)) {
1213 err
= PTR_ERR(padctl
->rst
);
1217 padctl
->supplies
= devm_kcalloc(&pdev
->dev
, padctl
->soc
->num_supplies
,
1218 sizeof(*padctl
->supplies
), GFP_KERNEL
);
1219 if (!padctl
->supplies
) {
1224 regulator_bulk_set_supply_names(padctl
->supplies
,
1225 padctl
->soc
->supply_names
,
1226 padctl
->soc
->num_supplies
);
1228 err
= devm_regulator_bulk_get(&pdev
->dev
, padctl
->soc
->num_supplies
,
1231 dev_err_probe(&pdev
->dev
, err
, "failed to get regulators\n");
1235 err
= reset_control_deassert(padctl
->rst
);
1239 err
= regulator_bulk_enable(padctl
->soc
->num_supplies
,
1242 dev_err(&pdev
->dev
, "failed to enable supplies: %d\n", err
);
1246 err
= tegra_xusb_setup_pads(padctl
);
1248 dev_err(&pdev
->dev
, "failed to setup pads: %d\n", err
);
1252 err
= tegra_xusb_setup_ports(padctl
);
1254 const char *level
= KERN_ERR
;
1256 if (err
== -EPROBE_DEFER
)
1259 dev_printk(level
, &pdev
->dev
,
1260 dev_fmt("failed to setup XUSB ports: %d\n"), err
);
1267 tegra_xusb_remove_pads(padctl
);
1269 regulator_bulk_disable(padctl
->soc
->num_supplies
, padctl
->supplies
);
1271 reset_control_assert(padctl
->rst
);
1273 platform_set_drvdata(pdev
, NULL
);
1274 soc
->ops
->remove(padctl
);
1278 static void tegra_xusb_padctl_remove(struct platform_device
*pdev
)
1280 struct tegra_xusb_padctl
*padctl
= platform_get_drvdata(pdev
);
1283 tegra_xusb_remove_ports(padctl
);
1284 tegra_xusb_remove_pads(padctl
);
1286 err
= regulator_bulk_disable(padctl
->soc
->num_supplies
,
1289 dev_err(&pdev
->dev
, "failed to disable supplies: %d\n", err
);
1291 err
= reset_control_assert(padctl
->rst
);
1293 dev_err(&pdev
->dev
, "failed to assert reset: %d\n", err
);
1295 padctl
->soc
->ops
->remove(padctl
);
1298 static __maybe_unused
int tegra_xusb_padctl_suspend_noirq(struct device
*dev
)
1300 struct tegra_xusb_padctl
*padctl
= dev_get_drvdata(dev
);
1302 if (padctl
->soc
&& padctl
->soc
->ops
&& padctl
->soc
->ops
->suspend_noirq
)
1303 return padctl
->soc
->ops
->suspend_noirq(padctl
);
1308 static __maybe_unused
int tegra_xusb_padctl_resume_noirq(struct device
*dev
)
1310 struct tegra_xusb_padctl
*padctl
= dev_get_drvdata(dev
);
1312 if (padctl
->soc
&& padctl
->soc
->ops
&& padctl
->soc
->ops
->resume_noirq
)
1313 return padctl
->soc
->ops
->resume_noirq(padctl
);
1318 static const struct dev_pm_ops tegra_xusb_padctl_pm_ops
= {
1319 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(tegra_xusb_padctl_suspend_noirq
,
1320 tegra_xusb_padctl_resume_noirq
)
1323 static struct platform_driver tegra_xusb_padctl_driver
= {
1325 .name
= "tegra-xusb-padctl",
1326 .of_match_table
= tegra_xusb_padctl_of_match
,
1327 .pm
= &tegra_xusb_padctl_pm_ops
,
1329 .probe
= tegra_xusb_padctl_probe
,
1330 .remove
= tegra_xusb_padctl_remove
,
1332 module_platform_driver(tegra_xusb_padctl_driver
);
1334 struct tegra_xusb_padctl
*tegra_xusb_padctl_get(struct device
*dev
)
1336 struct tegra_xusb_padctl
*padctl
;
1337 struct platform_device
*pdev
;
1338 struct device_node
*np
;
1340 np
= of_parse_phandle(dev
->of_node
, "nvidia,xusb-padctl", 0);
1342 return ERR_PTR(-EINVAL
);
1345 * This is slightly ugly. A better implementation would be to keep a
1346 * registry of pad controllers, but since there will almost certainly
1347 * only ever be one per SoC that would be a little overkill.
1349 pdev
= of_find_device_by_node(np
);
1352 return ERR_PTR(-ENODEV
);
1357 padctl
= platform_get_drvdata(pdev
);
1359 put_device(&pdev
->dev
);
1360 return ERR_PTR(-EPROBE_DEFER
);
1365 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get
);
1367 void tegra_xusb_padctl_put(struct tegra_xusb_padctl
*padctl
)
1370 put_device(padctl
->dev
);
1372 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put
);
1374 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl
*padctl
,
1377 if (padctl
->soc
->ops
->usb3_save_context
)
1378 return padctl
->soc
->ops
->usb3_save_context(padctl
, port
);
1382 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context
);
1384 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl
*padctl
,
1385 unsigned int port
, bool idle
)
1387 if (padctl
->soc
->ops
->hsic_set_idle
)
1388 return padctl
->soc
->ops
->hsic_set_idle(padctl
, port
, idle
);
1392 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle
);
1394 int tegra_xusb_padctl_enable_phy_sleepwalk(struct tegra_xusb_padctl
*padctl
, struct phy
*phy
,
1395 enum usb_device_speed speed
)
1397 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1399 if (lane
->pad
->ops
->enable_phy_sleepwalk
)
1400 return lane
->pad
->ops
->enable_phy_sleepwalk(lane
, speed
);
1404 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_sleepwalk
);
1406 int tegra_xusb_padctl_disable_phy_sleepwalk(struct tegra_xusb_padctl
*padctl
, struct phy
*phy
)
1408 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1410 if (lane
->pad
->ops
->disable_phy_sleepwalk
)
1411 return lane
->pad
->ops
->disable_phy_sleepwalk(lane
);
1415 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_sleepwalk
);
1417 int tegra_xusb_padctl_enable_phy_wake(struct tegra_xusb_padctl
*padctl
, struct phy
*phy
)
1419 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1421 if (lane
->pad
->ops
->enable_phy_wake
)
1422 return lane
->pad
->ops
->enable_phy_wake(lane
);
1426 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_enable_phy_wake
);
1428 int tegra_xusb_padctl_disable_phy_wake(struct tegra_xusb_padctl
*padctl
, struct phy
*phy
)
1430 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1432 if (lane
->pad
->ops
->disable_phy_wake
)
1433 return lane
->pad
->ops
->disable_phy_wake(lane
);
1437 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_disable_phy_wake
);
1439 bool tegra_xusb_padctl_remote_wake_detected(struct tegra_xusb_padctl
*padctl
, struct phy
*phy
)
1441 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1443 if (lane
->pad
->ops
->remote_wake_detected
)
1444 return lane
->pad
->ops
->remote_wake_detected(lane
);
1448 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_remote_wake_detected
);
1450 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl
*padctl
,
1451 unsigned int port
, bool enable
)
1453 if (padctl
->soc
->ops
->usb3_set_lfps_detect
)
1454 return padctl
->soc
->ops
->usb3_set_lfps_detect(padctl
, port
,
1459 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect
);
1461 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl
*padctl
,
1464 if (padctl
->soc
->ops
->vbus_override
)
1465 return padctl
->soc
->ops
->vbus_override(padctl
, val
);
1469 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override
);
1471 int tegra_phy_xusb_utmi_port_reset(struct phy
*phy
)
1473 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1474 struct tegra_xusb_padctl
*padctl
= lane
->pad
->padctl
;
1476 if (padctl
->soc
->ops
->utmi_port_reset
)
1477 return padctl
->soc
->ops
->utmi_port_reset(phy
);
1481 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset
);
1483 void tegra_phy_xusb_utmi_pad_power_on(struct phy
*phy
)
1485 struct tegra_xusb_lane
*lane
;
1486 struct tegra_xusb_padctl
*padctl
;
1491 lane
= phy_get_drvdata(phy
);
1492 padctl
= lane
->pad
->padctl
;
1494 if (padctl
->soc
->ops
->utmi_pad_power_on
)
1495 padctl
->soc
->ops
->utmi_pad_power_on(phy
);
1497 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_on
);
1499 void tegra_phy_xusb_utmi_pad_power_down(struct phy
*phy
)
1501 struct tegra_xusb_lane
*lane
;
1502 struct tegra_xusb_padctl
*padctl
;
1507 lane
= phy_get_drvdata(phy
);
1508 padctl
= lane
->pad
->padctl
;
1510 if (padctl
->soc
->ops
->utmi_pad_power_down
)
1511 padctl
->soc
->ops
->utmi_pad_power_down(phy
);
1513 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_pad_power_down
);
1515 int tegra_xusb_padctl_get_usb3_companion(struct tegra_xusb_padctl
*padctl
,
1518 struct tegra_xusb_usb2_port
*usb2
;
1519 struct tegra_xusb_usb3_port
*usb3
;
1522 usb2
= tegra_xusb_find_usb2_port(padctl
, port
);
1526 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
1527 usb3
= tegra_xusb_find_usb3_port(padctl
, i
);
1528 if (usb3
&& usb3
->port
== usb2
->base
.index
)
1529 return usb3
->base
.index
;
1534 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_usb3_companion
);
1536 int tegra_xusb_padctl_get_port_number(struct phy
*phy
)
1538 struct tegra_xusb_lane
*lane
;
1543 lane
= phy_get_drvdata(phy
);
1547 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get_port_number
);
1549 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1550 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1551 MODULE_LICENSE("GPL v2");