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
,
71 MODULE_DEVICE_TABLE(of
, tegra_xusb_padctl_of_match
);
73 static struct device_node
*
74 tegra_xusb_find_pad_node(struct tegra_xusb_padctl
*padctl
, const char *name
)
76 struct device_node
*pads
, *np
;
78 pads
= of_get_child_by_name(padctl
->dev
->of_node
, "pads");
82 np
= of_get_child_by_name(pads
, name
);
88 static struct device_node
*
89 tegra_xusb_pad_find_phy_node(struct tegra_xusb_pad
*pad
, unsigned int index
)
91 struct device_node
*np
, *lanes
;
93 lanes
= of_get_child_by_name(pad
->dev
.of_node
, "lanes");
97 np
= of_get_child_by_name(lanes
, pad
->soc
->lanes
[index
].name
);
103 int tegra_xusb_lane_parse_dt(struct tegra_xusb_lane
*lane
,
104 struct device_node
*np
)
106 struct device
*dev
= &lane
->pad
->dev
;
107 const char *function
;
110 err
= of_property_read_string(np
, "nvidia,function", &function
);
114 err
= match_string(lane
->soc
->funcs
, lane
->soc
->num_funcs
, function
);
116 dev_err(dev
, "invalid function \"%s\" for lane \"%pOFn\"\n",
121 lane
->function
= err
;
126 static void tegra_xusb_lane_destroy(struct phy
*phy
)
129 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
131 lane
->pad
->ops
->remove(lane
);
136 static void tegra_xusb_pad_release(struct device
*dev
)
138 struct tegra_xusb_pad
*pad
= to_tegra_xusb_pad(dev
);
140 pad
->soc
->ops
->remove(pad
);
143 static struct device_type tegra_xusb_pad_type
= {
144 .release
= tegra_xusb_pad_release
,
147 int tegra_xusb_pad_init(struct tegra_xusb_pad
*pad
,
148 struct tegra_xusb_padctl
*padctl
,
149 struct device_node
*np
)
153 device_initialize(&pad
->dev
);
154 INIT_LIST_HEAD(&pad
->list
);
155 pad
->dev
.parent
= padctl
->dev
;
156 pad
->dev
.type
= &tegra_xusb_pad_type
;
157 pad
->dev
.of_node
= np
;
158 pad
->padctl
= padctl
;
160 err
= dev_set_name(&pad
->dev
, "%s", pad
->soc
->name
);
164 err
= device_add(&pad
->dev
);
171 device_unregister(&pad
->dev
);
175 int tegra_xusb_pad_register(struct tegra_xusb_pad
*pad
,
176 const struct phy_ops
*ops
)
178 struct device_node
*children
;
183 children
= of_get_child_by_name(pad
->dev
.of_node
, "lanes");
187 pad
->lanes
= devm_kcalloc(&pad
->dev
, pad
->soc
->num_lanes
, sizeof(lane
),
190 of_node_put(children
);
194 for (i
= 0; i
< pad
->soc
->num_lanes
; i
++) {
195 struct device_node
*np
= tegra_xusb_pad_find_phy_node(pad
, i
);
196 struct tegra_xusb_lane
*lane
;
198 /* skip disabled lanes */
199 if (!np
|| !of_device_is_available(np
)) {
204 pad
->lanes
[i
] = phy_create(&pad
->dev
, np
, ops
);
205 if (IS_ERR(pad
->lanes
[i
])) {
206 err
= PTR_ERR(pad
->lanes
[i
]);
211 lane
= pad
->ops
->probe(pad
, np
, i
);
213 phy_destroy(pad
->lanes
[i
]);
218 list_add_tail(&lane
->list
, &pad
->padctl
->lanes
);
219 phy_set_drvdata(pad
->lanes
[i
], lane
);
222 pad
->provider
= of_phy_provider_register_full(&pad
->dev
, children
,
223 tegra_xusb_pad_of_xlate
);
224 if (IS_ERR(pad
->provider
)) {
225 err
= PTR_ERR(pad
->provider
);
233 tegra_xusb_lane_destroy(pad
->lanes
[i
]);
235 of_node_put(children
);
240 void tegra_xusb_pad_unregister(struct tegra_xusb_pad
*pad
)
242 unsigned int i
= pad
->soc
->num_lanes
;
244 of_phy_provider_unregister(pad
->provider
);
247 tegra_xusb_lane_destroy(pad
->lanes
[i
]);
249 device_unregister(&pad
->dev
);
252 static struct tegra_xusb_pad
*
253 tegra_xusb_pad_create(struct tegra_xusb_padctl
*padctl
,
254 const struct tegra_xusb_pad_soc
*soc
)
256 struct tegra_xusb_pad
*pad
;
257 struct device_node
*np
;
260 np
= tegra_xusb_find_pad_node(padctl
, soc
->name
);
261 if (!np
|| !of_device_is_available(np
))
264 pad
= soc
->ops
->probe(padctl
, soc
, np
);
267 dev_err(padctl
->dev
, "failed to create pad %s: %d\n",
272 /* XXX move this into ->probe() to avoid string comparison */
273 if (strcmp(soc
->name
, "pcie") == 0)
276 if (strcmp(soc
->name
, "sata") == 0)
279 if (strcmp(soc
->name
, "usb2") == 0)
282 if (strcmp(soc
->name
, "ulpi") == 0)
285 if (strcmp(soc
->name
, "hsic") == 0)
291 static void __tegra_xusb_remove_pads(struct tegra_xusb_padctl
*padctl
)
293 struct tegra_xusb_pad
*pad
, *tmp
;
295 list_for_each_entry_safe_reverse(pad
, tmp
, &padctl
->pads
, list
) {
296 list_del(&pad
->list
);
297 tegra_xusb_pad_unregister(pad
);
301 static void tegra_xusb_remove_pads(struct tegra_xusb_padctl
*padctl
)
303 mutex_lock(&padctl
->lock
);
304 __tegra_xusb_remove_pads(padctl
);
305 mutex_unlock(&padctl
->lock
);
308 static void tegra_xusb_lane_program(struct tegra_xusb_lane
*lane
)
310 struct tegra_xusb_padctl
*padctl
= lane
->pad
->padctl
;
311 const struct tegra_xusb_lane_soc
*soc
= lane
->soc
;
314 /* skip single function lanes */
315 if (soc
->num_funcs
< 2)
318 /* choose function */
319 value
= padctl_readl(padctl
, soc
->offset
);
320 value
&= ~(soc
->mask
<< soc
->shift
);
321 value
|= lane
->function
<< soc
->shift
;
322 padctl_writel(padctl
, value
, soc
->offset
);
325 static void tegra_xusb_pad_program(struct tegra_xusb_pad
*pad
)
329 for (i
= 0; i
< pad
->soc
->num_lanes
; i
++) {
330 struct tegra_xusb_lane
*lane
;
333 lane
= phy_get_drvdata(pad
->lanes
[i
]);
334 tegra_xusb_lane_program(lane
);
339 static int tegra_xusb_setup_pads(struct tegra_xusb_padctl
*padctl
)
341 struct tegra_xusb_pad
*pad
;
344 mutex_lock(&padctl
->lock
);
346 for (i
= 0; i
< padctl
->soc
->num_pads
; i
++) {
347 const struct tegra_xusb_pad_soc
*soc
= padctl
->soc
->pads
[i
];
350 pad
= tegra_xusb_pad_create(padctl
, soc
);
353 dev_err(padctl
->dev
, "failed to create pad %s: %d\n",
355 __tegra_xusb_remove_pads(padctl
);
356 mutex_unlock(&padctl
->lock
);
363 list_add_tail(&pad
->list
, &padctl
->pads
);
366 list_for_each_entry(pad
, &padctl
->pads
, list
)
367 tegra_xusb_pad_program(pad
);
369 mutex_unlock(&padctl
->lock
);
373 static bool tegra_xusb_lane_check(struct tegra_xusb_lane
*lane
,
374 const char *function
)
376 const char *func
= lane
->soc
->funcs
[lane
->function
];
378 return strcmp(function
, func
) == 0;
381 struct tegra_xusb_lane
*tegra_xusb_find_lane(struct tegra_xusb_padctl
*padctl
,
385 struct tegra_xusb_lane
*lane
, *hit
= ERR_PTR(-ENODEV
);
388 name
= kasprintf(GFP_KERNEL
, "%s-%u", type
, index
);
390 return ERR_PTR(-ENOMEM
);
392 list_for_each_entry(lane
, &padctl
->lanes
, list
) {
393 if (strcmp(lane
->soc
->name
, name
) == 0) {
403 struct tegra_xusb_lane
*
404 tegra_xusb_port_find_lane(struct tegra_xusb_port
*port
,
405 const struct tegra_xusb_lane_map
*map
,
406 const char *function
)
408 struct tegra_xusb_lane
*lane
, *match
= ERR_PTR(-ENODEV
);
410 for (; map
->type
; map
++) {
411 if (port
->index
!= map
->port
)
414 lane
= tegra_xusb_find_lane(port
->padctl
, map
->type
,
419 if (!tegra_xusb_lane_check(lane
, function
))
423 dev_err(&port
->dev
, "conflicting match: %s-%u / %s\n",
424 map
->type
, map
->index
, match
->soc
->name
);
432 static struct device_node
*
433 tegra_xusb_find_port_node(struct tegra_xusb_padctl
*padctl
, const char *type
,
436 struct device_node
*ports
, *np
;
439 ports
= of_get_child_by_name(padctl
->dev
->of_node
, "ports");
443 name
= kasprintf(GFP_KERNEL
, "%s-%u", type
, index
);
446 return ERR_PTR(-ENOMEM
);
448 np
= of_get_child_by_name(ports
, name
);
455 struct tegra_xusb_port
*
456 tegra_xusb_find_port(struct tegra_xusb_padctl
*padctl
, const char *type
,
459 struct tegra_xusb_port
*port
;
460 struct device_node
*np
;
462 np
= tegra_xusb_find_port_node(padctl
, type
, index
);
466 list_for_each_entry(port
, &padctl
->ports
, list
) {
467 if (np
== port
->dev
.of_node
) {
478 struct tegra_xusb_usb2_port
*
479 tegra_xusb_find_usb2_port(struct tegra_xusb_padctl
*padctl
, unsigned int index
)
481 struct tegra_xusb_port
*port
;
483 port
= tegra_xusb_find_port(padctl
, "usb2", index
);
485 return to_usb2_port(port
);
490 struct tegra_xusb_usb3_port
*
491 tegra_xusb_find_usb3_port(struct tegra_xusb_padctl
*padctl
, unsigned int index
)
493 struct tegra_xusb_port
*port
;
495 port
= tegra_xusb_find_port(padctl
, "usb3", index
);
497 return to_usb3_port(port
);
502 static void tegra_xusb_port_release(struct device
*dev
)
506 static struct device_type tegra_xusb_port_type
= {
507 .release
= tegra_xusb_port_release
,
510 static int tegra_xusb_port_init(struct tegra_xusb_port
*port
,
511 struct tegra_xusb_padctl
*padctl
,
512 struct device_node
*np
,
518 INIT_LIST_HEAD(&port
->list
);
519 port
->padctl
= padctl
;
522 device_initialize(&port
->dev
);
523 port
->dev
.type
= &tegra_xusb_port_type
;
524 port
->dev
.of_node
= of_node_get(np
);
525 port
->dev
.parent
= padctl
->dev
;
527 err
= dev_set_name(&port
->dev
, "%s-%u", name
, index
);
531 err
= device_add(&port
->dev
);
538 device_unregister(&port
->dev
);
542 static void tegra_xusb_port_unregister(struct tegra_xusb_port
*port
)
544 device_unregister(&port
->dev
);
547 static const char *const modes
[] = {
548 [USB_DR_MODE_UNKNOWN
] = "",
549 [USB_DR_MODE_HOST
] = "host",
550 [USB_DR_MODE_PERIPHERAL
] = "peripheral",
551 [USB_DR_MODE_OTG
] = "otg",
554 static int tegra_xusb_usb2_port_parse_dt(struct tegra_xusb_usb2_port
*usb2
)
556 struct tegra_xusb_port
*port
= &usb2
->base
;
557 struct device_node
*np
= port
->dev
.of_node
;
560 usb2
->internal
= of_property_read_bool(np
, "nvidia,internal");
562 if (!of_property_read_string(np
, "mode", &mode
)) {
563 int err
= match_string(modes
, ARRAY_SIZE(modes
), mode
);
565 dev_err(&port
->dev
, "invalid value %s for \"mode\"\n",
567 usb2
->mode
= USB_DR_MODE_UNKNOWN
;
572 usb2
->mode
= USB_DR_MODE_HOST
;
575 usb2
->supply
= devm_regulator_get(&port
->dev
, "vbus");
576 return PTR_ERR_OR_ZERO(usb2
->supply
);
579 static int tegra_xusb_add_usb2_port(struct tegra_xusb_padctl
*padctl
,
582 struct tegra_xusb_usb2_port
*usb2
;
583 struct device_node
*np
;
587 * USB2 ports don't require additional properties, but if the port is
588 * marked as disabled there is no reason to register it.
590 np
= tegra_xusb_find_port_node(padctl
, "usb2", index
);
591 if (!np
|| !of_device_is_available(np
))
594 usb2
= devm_kzalloc(padctl
->dev
, sizeof(*usb2
), GFP_KERNEL
);
600 err
= tegra_xusb_port_init(&usb2
->base
, padctl
, np
, "usb2", index
);
604 usb2
->base
.ops
= padctl
->soc
->ports
.usb2
.ops
;
606 usb2
->base
.lane
= usb2
->base
.ops
->map(&usb2
->base
);
607 if (IS_ERR(usb2
->base
.lane
)) {
608 err
= PTR_ERR(usb2
->base
.lane
);
612 err
= tegra_xusb_usb2_port_parse_dt(usb2
);
614 tegra_xusb_port_unregister(&usb2
->base
);
618 list_add_tail(&usb2
->base
.list
, &padctl
->ports
);
625 static int tegra_xusb_ulpi_port_parse_dt(struct tegra_xusb_ulpi_port
*ulpi
)
627 struct tegra_xusb_port
*port
= &ulpi
->base
;
628 struct device_node
*np
= port
->dev
.of_node
;
630 ulpi
->internal
= of_property_read_bool(np
, "nvidia,internal");
635 static int tegra_xusb_add_ulpi_port(struct tegra_xusb_padctl
*padctl
,
638 struct tegra_xusb_ulpi_port
*ulpi
;
639 struct device_node
*np
;
642 np
= tegra_xusb_find_port_node(padctl
, "ulpi", index
);
643 if (!np
|| !of_device_is_available(np
))
646 ulpi
= devm_kzalloc(padctl
->dev
, sizeof(*ulpi
), GFP_KERNEL
);
652 err
= tegra_xusb_port_init(&ulpi
->base
, padctl
, np
, "ulpi", index
);
656 ulpi
->base
.ops
= padctl
->soc
->ports
.ulpi
.ops
;
658 ulpi
->base
.lane
= ulpi
->base
.ops
->map(&ulpi
->base
);
659 if (IS_ERR(ulpi
->base
.lane
)) {
660 err
= PTR_ERR(ulpi
->base
.lane
);
664 err
= tegra_xusb_ulpi_port_parse_dt(ulpi
);
666 tegra_xusb_port_unregister(&ulpi
->base
);
670 list_add_tail(&ulpi
->base
.list
, &padctl
->ports
);
677 static int tegra_xusb_hsic_port_parse_dt(struct tegra_xusb_hsic_port
*hsic
)
683 static int tegra_xusb_add_hsic_port(struct tegra_xusb_padctl
*padctl
,
686 struct tegra_xusb_hsic_port
*hsic
;
687 struct device_node
*np
;
690 np
= tegra_xusb_find_port_node(padctl
, "hsic", index
);
691 if (!np
|| !of_device_is_available(np
))
694 hsic
= devm_kzalloc(padctl
->dev
, sizeof(*hsic
), GFP_KERNEL
);
700 err
= tegra_xusb_port_init(&hsic
->base
, padctl
, np
, "hsic", index
);
704 hsic
->base
.ops
= padctl
->soc
->ports
.hsic
.ops
;
706 hsic
->base
.lane
= hsic
->base
.ops
->map(&hsic
->base
);
707 if (IS_ERR(hsic
->base
.lane
)) {
708 err
= PTR_ERR(hsic
->base
.lane
);
712 err
= tegra_xusb_hsic_port_parse_dt(hsic
);
714 tegra_xusb_port_unregister(&hsic
->base
);
718 list_add_tail(&hsic
->base
.list
, &padctl
->ports
);
725 static int tegra_xusb_usb3_port_parse_dt(struct tegra_xusb_usb3_port
*usb3
)
727 struct tegra_xusb_port
*port
= &usb3
->base
;
728 struct device_node
*np
= port
->dev
.of_node
;
732 err
= of_property_read_u32(np
, "nvidia,usb2-companion", &value
);
734 dev_err(&port
->dev
, "failed to read port: %d\n", err
);
740 usb3
->internal
= of_property_read_bool(np
, "nvidia,internal");
742 usb3
->supply
= devm_regulator_get(&port
->dev
, "vbus");
743 return PTR_ERR_OR_ZERO(usb3
->supply
);
746 static int tegra_xusb_add_usb3_port(struct tegra_xusb_padctl
*padctl
,
749 struct tegra_xusb_usb3_port
*usb3
;
750 struct device_node
*np
;
754 * If there is no supplemental configuration in the device tree the
755 * port is unusable. But it is valid to configure only a single port,
756 * hence return 0 instead of an error to allow ports to be optional.
758 np
= tegra_xusb_find_port_node(padctl
, "usb3", index
);
759 if (!np
|| !of_device_is_available(np
))
762 usb3
= devm_kzalloc(padctl
->dev
, sizeof(*usb3
), GFP_KERNEL
);
768 err
= tegra_xusb_port_init(&usb3
->base
, padctl
, np
, "usb3", index
);
772 usb3
->base
.ops
= padctl
->soc
->ports
.usb3
.ops
;
774 usb3
->base
.lane
= usb3
->base
.ops
->map(&usb3
->base
);
775 if (IS_ERR(usb3
->base
.lane
)) {
776 err
= PTR_ERR(usb3
->base
.lane
);
780 err
= tegra_xusb_usb3_port_parse_dt(usb3
);
782 tegra_xusb_port_unregister(&usb3
->base
);
786 list_add_tail(&usb3
->base
.list
, &padctl
->ports
);
793 static void __tegra_xusb_remove_ports(struct tegra_xusb_padctl
*padctl
)
795 struct tegra_xusb_port
*port
, *tmp
;
797 list_for_each_entry_safe_reverse(port
, tmp
, &padctl
->ports
, list
) {
798 list_del(&port
->list
);
799 tegra_xusb_port_unregister(port
);
803 static int tegra_xusb_find_unused_usb3_port(struct tegra_xusb_padctl
*padctl
)
805 struct device_node
*np
;
808 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
809 np
= tegra_xusb_find_port_node(padctl
, "usb3", i
);
810 if (!np
|| !of_device_is_available(np
))
817 static bool tegra_xusb_port_is_companion(struct tegra_xusb_usb2_port
*usb2
)
820 struct tegra_xusb_usb3_port
*usb3
;
821 struct tegra_xusb_padctl
*padctl
= usb2
->base
.padctl
;
823 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
824 usb3
= tegra_xusb_find_usb3_port(padctl
, i
);
825 if (usb3
&& usb3
->port
== usb2
->base
.index
)
832 static int tegra_xusb_update_usb3_fake_port(struct tegra_xusb_usb2_port
*usb2
)
836 /* Disable usb3_port_fake usage by default and assign if needed */
837 usb2
->usb3_port_fake
= -1;
839 if ((usb2
->mode
== USB_DR_MODE_OTG
||
840 usb2
->mode
== USB_DR_MODE_PERIPHERAL
) &&
841 !tegra_xusb_port_is_companion(usb2
)) {
842 fake
= tegra_xusb_find_unused_usb3_port(usb2
->base
.padctl
);
844 dev_err(&usb2
->base
.dev
, "no unused USB3 ports available\n");
848 dev_dbg(&usb2
->base
.dev
, "Found unused usb3 port: %d\n", fake
);
849 usb2
->usb3_port_fake
= fake
;
855 static int tegra_xusb_setup_ports(struct tegra_xusb_padctl
*padctl
)
857 struct tegra_xusb_port
*port
;
858 struct tegra_xusb_usb2_port
*usb2
;
862 mutex_lock(&padctl
->lock
);
864 for (i
= 0; i
< padctl
->soc
->ports
.usb2
.count
; i
++) {
865 err
= tegra_xusb_add_usb2_port(padctl
, i
);
870 for (i
= 0; i
< padctl
->soc
->ports
.ulpi
.count
; i
++) {
871 err
= tegra_xusb_add_ulpi_port(padctl
, i
);
876 for (i
= 0; i
< padctl
->soc
->ports
.hsic
.count
; i
++) {
877 err
= tegra_xusb_add_hsic_port(padctl
, i
);
882 for (i
= 0; i
< padctl
->soc
->ports
.usb3
.count
; i
++) {
883 err
= tegra_xusb_add_usb3_port(padctl
, i
);
888 if (padctl
->soc
->need_fake_usb3_port
) {
889 for (i
= 0; i
< padctl
->soc
->ports
.usb2
.count
; i
++) {
890 usb2
= tegra_xusb_find_usb2_port(padctl
, i
);
894 err
= tegra_xusb_update_usb3_fake_port(usb2
);
900 list_for_each_entry(port
, &padctl
->ports
, list
) {
901 err
= port
->ops
->enable(port
);
903 dev_err(padctl
->dev
, "failed to enable port %s: %d\n",
904 dev_name(&port
->dev
), err
);
910 __tegra_xusb_remove_ports(padctl
);
912 mutex_unlock(&padctl
->lock
);
916 static void tegra_xusb_remove_ports(struct tegra_xusb_padctl
*padctl
)
918 mutex_lock(&padctl
->lock
);
919 __tegra_xusb_remove_ports(padctl
);
920 mutex_unlock(&padctl
->lock
);
923 static int tegra_xusb_padctl_probe(struct platform_device
*pdev
)
925 struct device_node
*np
= pdev
->dev
.of_node
;
926 const struct tegra_xusb_padctl_soc
*soc
;
927 struct tegra_xusb_padctl
*padctl
;
928 const struct of_device_id
*match
;
929 struct resource
*res
;
932 /* for backwards compatibility with old device trees */
933 np
= of_get_child_by_name(np
, "pads");
935 dev_warn(&pdev
->dev
, "deprecated DT, using legacy driver\n");
936 return tegra_xusb_padctl_legacy_probe(pdev
);
941 match
= of_match_node(tegra_xusb_padctl_of_match
, pdev
->dev
.of_node
);
944 padctl
= soc
->ops
->probe(&pdev
->dev
, soc
);
946 return PTR_ERR(padctl
);
948 platform_set_drvdata(pdev
, padctl
);
949 INIT_LIST_HEAD(&padctl
->ports
);
950 INIT_LIST_HEAD(&padctl
->lanes
);
951 INIT_LIST_HEAD(&padctl
->pads
);
952 mutex_init(&padctl
->lock
);
954 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
955 padctl
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
956 if (IS_ERR(padctl
->regs
)) {
957 err
= PTR_ERR(padctl
->regs
);
961 padctl
->rst
= devm_reset_control_get(&pdev
->dev
, NULL
);
962 if (IS_ERR(padctl
->rst
)) {
963 err
= PTR_ERR(padctl
->rst
);
967 padctl
->supplies
= devm_kcalloc(&pdev
->dev
, padctl
->soc
->num_supplies
,
968 sizeof(*padctl
->supplies
), GFP_KERNEL
);
969 if (!padctl
->supplies
) {
974 regulator_bulk_set_supply_names(padctl
->supplies
,
975 padctl
->soc
->supply_names
,
976 padctl
->soc
->num_supplies
);
978 err
= devm_regulator_bulk_get(&pdev
->dev
, padctl
->soc
->num_supplies
,
981 dev_err(&pdev
->dev
, "failed to get regulators: %d\n", err
);
985 err
= reset_control_deassert(padctl
->rst
);
989 err
= regulator_bulk_enable(padctl
->soc
->num_supplies
,
992 dev_err(&pdev
->dev
, "failed to enable supplies: %d\n", err
);
996 err
= tegra_xusb_setup_pads(padctl
);
998 dev_err(&pdev
->dev
, "failed to setup pads: %d\n", err
);
1002 err
= tegra_xusb_setup_ports(padctl
);
1004 dev_err(&pdev
->dev
, "failed to setup XUSB ports: %d\n", err
);
1011 tegra_xusb_remove_pads(padctl
);
1013 regulator_bulk_disable(padctl
->soc
->num_supplies
, padctl
->supplies
);
1015 reset_control_assert(padctl
->rst
);
1017 soc
->ops
->remove(padctl
);
1021 static int tegra_xusb_padctl_remove(struct platform_device
*pdev
)
1023 struct tegra_xusb_padctl
*padctl
= platform_get_drvdata(pdev
);
1026 tegra_xusb_remove_ports(padctl
);
1027 tegra_xusb_remove_pads(padctl
);
1029 err
= regulator_bulk_disable(padctl
->soc
->num_supplies
,
1032 dev_err(&pdev
->dev
, "failed to disable supplies: %d\n", err
);
1034 err
= reset_control_assert(padctl
->rst
);
1036 dev_err(&pdev
->dev
, "failed to assert reset: %d\n", err
);
1038 padctl
->soc
->ops
->remove(padctl
);
1043 static struct platform_driver tegra_xusb_padctl_driver
= {
1045 .name
= "tegra-xusb-padctl",
1046 .of_match_table
= tegra_xusb_padctl_of_match
,
1048 .probe
= tegra_xusb_padctl_probe
,
1049 .remove
= tegra_xusb_padctl_remove
,
1051 module_platform_driver(tegra_xusb_padctl_driver
);
1053 struct tegra_xusb_padctl
*tegra_xusb_padctl_get(struct device
*dev
)
1055 struct tegra_xusb_padctl
*padctl
;
1056 struct platform_device
*pdev
;
1057 struct device_node
*np
;
1059 np
= of_parse_phandle(dev
->of_node
, "nvidia,xusb-padctl", 0);
1061 return ERR_PTR(-EINVAL
);
1064 * This is slightly ugly. A better implementation would be to keep a
1065 * registry of pad controllers, but since there will almost certainly
1066 * only ever be one per SoC that would be a little overkill.
1068 pdev
= of_find_device_by_node(np
);
1071 return ERR_PTR(-ENODEV
);
1076 padctl
= platform_get_drvdata(pdev
);
1078 put_device(&pdev
->dev
);
1079 return ERR_PTR(-EPROBE_DEFER
);
1084 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_get
);
1086 void tegra_xusb_padctl_put(struct tegra_xusb_padctl
*padctl
)
1089 put_device(padctl
->dev
);
1091 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_put
);
1093 int tegra_xusb_padctl_usb3_save_context(struct tegra_xusb_padctl
*padctl
,
1096 if (padctl
->soc
->ops
->usb3_save_context
)
1097 return padctl
->soc
->ops
->usb3_save_context(padctl
, port
);
1101 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_save_context
);
1103 int tegra_xusb_padctl_hsic_set_idle(struct tegra_xusb_padctl
*padctl
,
1104 unsigned int port
, bool idle
)
1106 if (padctl
->soc
->ops
->hsic_set_idle
)
1107 return padctl
->soc
->ops
->hsic_set_idle(padctl
, port
, idle
);
1111 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_hsic_set_idle
);
1113 int tegra_xusb_padctl_usb3_set_lfps_detect(struct tegra_xusb_padctl
*padctl
,
1114 unsigned int port
, bool enable
)
1116 if (padctl
->soc
->ops
->usb3_set_lfps_detect
)
1117 return padctl
->soc
->ops
->usb3_set_lfps_detect(padctl
, port
,
1122 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_usb3_set_lfps_detect
);
1124 int tegra_xusb_padctl_set_vbus_override(struct tegra_xusb_padctl
*padctl
,
1127 if (padctl
->soc
->ops
->vbus_override
)
1128 return padctl
->soc
->ops
->vbus_override(padctl
, val
);
1132 EXPORT_SYMBOL_GPL(tegra_xusb_padctl_set_vbus_override
);
1134 int tegra_phy_xusb_utmi_port_reset(struct phy
*phy
)
1136 struct tegra_xusb_lane
*lane
= phy_get_drvdata(phy
);
1137 struct tegra_xusb_padctl
*padctl
= lane
->pad
->padctl
;
1139 if (padctl
->soc
->ops
->utmi_port_reset
)
1140 return padctl
->soc
->ops
->utmi_port_reset(phy
);
1144 EXPORT_SYMBOL_GPL(tegra_phy_xusb_utmi_port_reset
);
1146 MODULE_AUTHOR("Thierry Reding <treding@nvidia.com>");
1147 MODULE_DESCRIPTION("Tegra XUSB Pad Controller driver");
1148 MODULE_LICENSE("GPL v2");