Skip to content

Commit b77f2ff

Browse files
Nagarjuna Kristamthierryreding
authored andcommitted
usb: gadget: tegra-xudc: Add usb-phy support
usb-phy is used to get notified on the USB role changes. Get usb-phy from the UTMI PHY. Signed-off-by: Nagarjuna Kristam <[email protected]> Acked-by: Felipe Balbi <[email protected]> Signed-off-by: Thierry Reding <[email protected]>
1 parent 9ce0a14 commit b77f2ff

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

drivers/usb/gadget/udc/tegra-xudc.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
#include <linux/reset.h>
2727
#include <linux/usb/ch9.h>
2828
#include <linux/usb/gadget.h>
29+
#include <linux/usb/otg.h>
2930
#include <linux/usb/role.h>
31+
#include <linux/usb/phy.h>
3032
#include <linux/workqueue.h>
3133

3234
/* XUSB_DEV registers */
@@ -487,6 +489,9 @@ struct tegra_xudc {
487489
bool suspended;
488490
bool powergated;
489491

492+
struct usb_phy *usbphy;
493+
struct notifier_block vbus_nb;
494+
490495
struct completion disconnect_complete;
491496

492497
bool selfpowered;
@@ -669,6 +674,31 @@ static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
669674
tegra_xudc_device_mode_off(xudc);
670675
}
671676

677+
static int tegra_xudc_vbus_notify(struct notifier_block *nb,
678+
unsigned long action, void *data)
679+
{
680+
struct tegra_xudc *xudc = container_of(nb, struct tegra_xudc,
681+
vbus_nb);
682+
struct usb_phy *usbphy = (struct usb_phy *)data;
683+
684+
dev_dbg(xudc->dev, "%s(): event is %d\n", __func__, usbphy->last_event);
685+
686+
if ((xudc->device_mode && usbphy->last_event == USB_EVENT_VBUS) ||
687+
(!xudc->device_mode && usbphy->last_event != USB_EVENT_VBUS)) {
688+
dev_dbg(xudc->dev, "Same role(%d) received. Ignore",
689+
xudc->device_mode);
690+
return NOTIFY_OK;
691+
}
692+
693+
xudc->device_mode = (usbphy->last_event == USB_EVENT_VBUS) ? true :
694+
false;
695+
696+
if (!xudc->suspended)
697+
schedule_work(&xudc->usb_role_sw_work);
698+
699+
return NOTIFY_OK;
700+
}
701+
672702
static void tegra_xudc_plc_reset_work(struct work_struct *work)
673703
{
674704
struct delayed_work *dwork = to_delayed_work(work);
@@ -1937,6 +1967,9 @@ static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
19371967
xudc_writel(xudc, val, CTRL);
19381968
}
19391969

1970+
if (xudc->usbphy)
1971+
otg_set_peripheral(xudc->usbphy->otg, gadget);
1972+
19401973
xudc->driver = driver;
19411974
unlock:
19421975
dev_dbg(xudc->dev, "%s: ret value is %d", __func__, ret);
@@ -1957,6 +1990,9 @@ static int tegra_xudc_gadget_stop(struct usb_gadget *gadget)
19571990

19581991
spin_lock_irqsave(&xudc->lock, flags);
19591992

1993+
if (xudc->usbphy)
1994+
otg_set_peripheral(xudc->usbphy->otg, NULL);
1995+
19601996
val = xudc_readl(xudc, CTRL);
19611997
val &= ~(CTRL_IE | CTRL_ENABLE);
19621998
xudc_writel(xudc, val, CTRL);
@@ -3558,9 +3594,15 @@ static int tegra_xudc_probe(struct platform_device *pdev)
35583594
INIT_DELAYED_WORK(&xudc->port_reset_war_work,
35593595
tegra_xudc_port_reset_war_work);
35603596

3561-
/* Set the mode as device mode and this keeps phy always ON */
3562-
xudc->device_mode = true;
3563-
schedule_work(&xudc->usb_role_sw_work);
3597+
xudc->vbus_nb.notifier_call = tegra_xudc_vbus_notify;
3598+
xudc->usbphy = devm_usb_get_phy_by_node(xudc->dev,
3599+
xudc->utmi_phy->dev.of_node,
3600+
&xudc->vbus_nb);
3601+
if (IS_ERR(xudc->usbphy)) {
3602+
err = PTR_ERR(xudc->usbphy);
3603+
dev_err(xudc->dev, "failed to get USB PHY: %d\n", err);
3604+
goto free_eps;
3605+
}
35643606

35653607
pm_runtime_enable(&pdev->dev);
35663608

0 commit comments

Comments
 (0)