26
26
#include <linux/reset.h>
27
27
#include <linux/usb/ch9.h>
28
28
#include <linux/usb/gadget.h>
29
+ #include <linux/usb/otg.h>
29
30
#include <linux/usb/role.h>
31
+ #include <linux/usb/phy.h>
30
32
#include <linux/workqueue.h>
31
33
32
34
/* XUSB_DEV registers */
@@ -487,6 +489,9 @@ struct tegra_xudc {
487
489
bool suspended ;
488
490
bool powergated ;
489
491
492
+ struct usb_phy * usbphy ;
493
+ struct notifier_block vbus_nb ;
494
+
490
495
struct completion disconnect_complete ;
491
496
492
497
bool selfpowered ;
@@ -669,6 +674,31 @@ static void tegra_xudc_usb_role_sw_work(struct work_struct *work)
669
674
tegra_xudc_device_mode_off (xudc );
670
675
}
671
676
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
+
672
702
static void tegra_xudc_plc_reset_work (struct work_struct * work )
673
703
{
674
704
struct delayed_work * dwork = to_delayed_work (work );
@@ -1937,6 +1967,9 @@ static int tegra_xudc_gadget_start(struct usb_gadget *gadget,
1937
1967
xudc_writel (xudc , val , CTRL );
1938
1968
}
1939
1969
1970
+ if (xudc -> usbphy )
1971
+ otg_set_peripheral (xudc -> usbphy -> otg , gadget );
1972
+
1940
1973
xudc -> driver = driver ;
1941
1974
unlock :
1942
1975
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)
1957
1990
1958
1991
spin_lock_irqsave (& xudc -> lock , flags );
1959
1992
1993
+ if (xudc -> usbphy )
1994
+ otg_set_peripheral (xudc -> usbphy -> otg , NULL );
1995
+
1960
1996
val = xudc_readl (xudc , CTRL );
1961
1997
val &= ~(CTRL_IE | CTRL_ENABLE );
1962
1998
xudc_writel (xudc , val , CTRL );
@@ -3558,9 +3594,15 @@ static int tegra_xudc_probe(struct platform_device *pdev)
3558
3594
INIT_DELAYED_WORK (& xudc -> port_reset_war_work ,
3559
3595
tegra_xudc_port_reset_war_work );
3560
3596
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
+ }
3564
3606
3565
3607
pm_runtime_enable (& pdev -> dev );
3566
3608
0 commit comments