23
23
#include "usb_phy.h"
24
24
25
25
#include <zephyr/logging/log.h>
26
+ #include <zephyr/pm/device.h>
27
+ #include <zephyr/pm/policy.h>
26
28
LOG_MODULE_REGISTER (udc_mcux , CONFIG_UDC_DRIVER_LOG_LEVEL );
27
29
28
30
/*
@@ -34,6 +36,7 @@ LOG_MODULE_REGISTER(udc_mcux, CONFIG_UDC_DRIVER_LOG_LEVEL);
34
36
35
37
#define PRV_DATA_HANDLE (_handle ) CONTAINER_OF(_handle, struct udc_mcux_data, mcux_device)
36
38
39
+
37
40
struct udc_mcux_config {
38
41
const usb_device_controller_interface_struct_t * mcux_if ;
39
42
void (* irq_enable_func )(const struct device * dev );
@@ -51,6 +54,8 @@ struct udc_mcux_data {
51
54
usb_device_struct_t mcux_device ;
52
55
struct k_work work ;
53
56
struct k_fifo fifo ;
57
+ bool enabled ;
58
+ bool vbus_present ;
54
59
uint8_t controller_id ; /* 0xFF is invalid value */
55
60
};
56
61
@@ -92,6 +97,44 @@ static int udc_mcux_control(const struct device *dev, usb_device_control_type_t
92
97
return 0 ;
93
98
}
94
99
100
+ static void udc_mcux_change_state (const struct device * dev , const bool enabled , const bool vbus_present )
101
+ {
102
+ struct udc_mcux_data * data = udc_get_private (dev );
103
+
104
+ unsigned int key = irq_lock ();
105
+
106
+ if (data -> enabled == enabled && data -> vbus_present == vbus_present ) {
107
+ return ;
108
+ }
109
+
110
+ if (vbus_present != data -> vbus_present ) {
111
+ udc_submit_event (data -> dev ,
112
+ vbus_present ? UDC_EVT_VBUS_READY : UDC_EVT_VBUS_REMOVED , 0 );
113
+ }
114
+
115
+ if (enabled && vbus_present ) {
116
+
117
+ /*
118
+ * Block PM when usb is recieves VBUS signal or device
119
+ * is explicitly told to be enabled.
120
+ */
121
+ pm_policy_device_power_lock_get (dev );
122
+ } else if (data -> enabled && data -> vbus_present ) {
123
+
124
+ /*
125
+ * USB was previously busy, but now has either lost
126
+ * VBUS signal or application has disabled udc.
127
+ * UDC will now unblock PM.
128
+ */
129
+ pm_policy_device_power_lock_put (dev );
130
+ }
131
+
132
+ data -> enabled = enabled ;
133
+ data -> vbus_present = vbus_present ;
134
+
135
+ irq_unlock (key );
136
+ }
137
+
95
138
/* If ep is busy, return busy. Otherwise feed the buf to controller */
96
139
static int udc_mcux_ep_feed (const struct device * dev ,
97
140
struct udc_ep_config * const cfg ,
@@ -525,10 +568,10 @@ usb_status_t USB_DeviceNotificationTrigger(void *handle, void *msg)
525
568
case kUSB_DeviceNotifyLPMSleep :
526
569
break ;
527
570
case kUSB_DeviceNotifyDetach :
528
- udc_submit_event (dev , UDC_EVT_VBUS_REMOVED , 0 );
571
+ udc_mcux_change_state (dev , priv -> enabled , 0 );
529
572
break ;
530
573
case kUSB_DeviceNotifyAttach :
531
- udc_submit_event (dev , UDC_EVT_VBUS_READY , 0 );
574
+ udc_mcux_change_state (dev , priv -> enabled , 1 );
532
575
break ;
533
576
case kUSB_DeviceNotifySOF :
534
577
udc_submit_event (dev , UDC_EVT_SOF , 0 );
@@ -676,11 +719,19 @@ static int udc_mcux_set_address(const struct device *dev, const uint8_t addr)
676
719
677
720
static int udc_mcux_enable (const struct device * dev )
678
721
{
722
+ struct udc_mcux_data * priv = udc_get_private (dev );
723
+
724
+ udc_mcux_change_state (dev , 1 , priv -> vbus_present );
725
+
679
726
return udc_mcux_control (dev , kUSB_DeviceControlRun , NULL );
680
727
}
681
728
682
729
static int udc_mcux_disable (const struct device * dev )
683
730
{
731
+ struct udc_mcux_data * priv = udc_get_private (dev );
732
+
733
+ udc_mcux_change_state (dev , 0 , priv -> vbus_present );
734
+
684
735
return udc_mcux_control (dev , kUSB_DeviceControlStop , NULL );
685
736
}
686
737
@@ -759,7 +810,9 @@ static inline void udc_mcux_get_hal_driver_id(struct udc_mcux_data *priv,
759
810
}
760
811
}
761
812
762
- static int udc_mcux_driver_preinit (const struct device * dev )
813
+
814
+
815
+ static int udc_mcux_init_common (const struct device * dev )
763
816
{
764
817
const struct udc_mcux_config * config = dev -> config ;
765
818
struct udc_data * data = dev -> data ;
@@ -771,10 +824,6 @@ static int udc_mcux_driver_preinit(const struct device *dev)
771
824
return - ENOMEM ;
772
825
}
773
826
774
- k_mutex_init (& data -> mutex );
775
- k_fifo_init (& priv -> fifo );
776
- k_work_init (& priv -> work , udc_mcux_work_handler );
777
-
778
827
for (int i = 0 ; i < config -> num_of_eps ; i ++ ) {
779
828
config -> ep_cfg_out [i ].caps .out = 1 ;
780
829
if (i == 0 ) {
@@ -823,11 +872,49 @@ static int udc_mcux_driver_preinit(const struct device *dev)
823
872
data -> caps .hs = true;
824
873
priv -> dev = dev ;
825
874
875
+
826
876
pinctrl_apply_state (config -> pincfg , PINCTRL_STATE_DEFAULT );
827
877
828
878
return 0 ;
829
879
}
830
880
881
+ static int udc_mcux_driver_preinit (const struct device * dev )
882
+ {
883
+ const struct udc_mcux_config * config = dev -> config ;
884
+ struct udc_data * data = dev -> data ;
885
+ struct udc_mcux_data * priv = data -> priv ;
886
+
887
+ k_mutex_init (& data -> mutex );
888
+ k_fifo_init (& priv -> fifo );
889
+ k_work_init (& priv -> work , udc_mcux_work_handler );
890
+ return udc_mcux_init_common (dev );
891
+ }
892
+
893
+ #ifdef CONFIG_PM_DEVICE
894
+ static int udc_mcux_pm_action (const struct device * dev , enum pm_device_action action )
895
+ {
896
+ struct udc_mcux_data * priv = udc_get_private (dev );
897
+
898
+ switch (action ) {
899
+ case PM_DEVICE_ACTION_RESUME :
900
+ case PM_DEVICE_ACTION_SUSPEND :
901
+ break ;
902
+ case PM_DEVICE_ACTION_TURN_OFF :
903
+ udc_mcux_shutdown (dev );
904
+ break ;
905
+ case PM_DEVICE_ACTION_TURN_ON :
906
+ udc_mcux_init_common (dev );
907
+ if (priv -> enabled ) {
908
+ udc_mcux_control (dev , kUSB_DeviceControlRun , NULL );
909
+ }
910
+ break ;
911
+ default :
912
+ return - ENOTSUP ;
913
+ }
914
+ return 0 ;
915
+ }
916
+ #endif /* CONFIG_PM_DEVICE */
917
+
831
918
static const struct udc_api udc_mcux_api = {
832
919
.device_speed = udc_mcux_device_speed ,
833
920
.ep_enqueue = udc_mcux_ep_enqueue ,
@@ -917,7 +1004,8 @@ static usb_phy_config_struct_t phy_config_##n = { \
917
1004
.priv = &priv_data_##n, \
918
1005
}; \
919
1006
\
920
- DEVICE_DT_INST_DEFINE(n, udc_mcux_driver_preinit, NULL, \
1007
+ PM_DEVICE_DT_INST_DEFINE(n, udc_mcux_pm_action); \
1008
+ DEVICE_DT_INST_DEFINE(n, udc_mcux_driver_preinit, PM_DEVICE_DT_INST_GET(n), \
921
1009
&udc_data_##n, &priv_config_##n, \
922
1010
POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEVICE, \
923
1011
&udc_mcux_api);
0 commit comments