Lines Matching +full:role +full:- +full:switch +full:- +full:default +full:- +full:mode

1 // SPDX-License-Identifier: GPL-2.0
3 * drd.c - DesignWare USB2 DRD Controller Dual-role support
13 #include <linux/usb/role.h>
25 spin_lock_irqsave(&hsotg->lock, flags); in dwc2_ovr_init()
30 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) in dwc2_ovr_init()
32 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) in dwc2_ovr_init()
36 spin_unlock_irqrestore(&hsotg->lock, flags); in dwc2_ovr_init()
38 dwc2_force_mode(hsotg, (hsotg->dr_mode == USB_DR_MODE_HOST) || in dwc2_ovr_init()
39 (hsotg->role_sw_default_mode == USB_DR_MODE_HOST)); in dwc2_ovr_init()
46 /* Check if A-Session is already in the right state */ in dwc2_ovr_avalid()
49 return -EALREADY; in dwc2_ovr_avalid()
68 /* Check if B-Session is already in the right state */ in dwc2_ovr_bvalid()
71 return -EALREADY; in dwc2_ovr_bvalid()
86 static int dwc2_drd_role_sw_set(struct usb_role_switch *sw, enum usb_role role) in dwc2_drd_role_sw_set() argument
93 if ((role == USB_ROLE_DEVICE && hsotg->dr_mode == USB_DR_MODE_HOST) || in dwc2_drd_role_sw_set()
94 (role == USB_ROLE_HOST && hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)) in dwc2_drd_role_sw_set()
95 return -EINVAL; in dwc2_drd_role_sw_set()
99 /* Skip session if core is in test mode */ in dwc2_drd_role_sw_set()
100 if (role == USB_ROLE_NONE && hsotg->test_mode) { in dwc2_drd_role_sw_set()
101 dev_dbg(hsotg->dev, "Core is in test mode\n"); in dwc2_drd_role_sw_set()
102 return -EBUSY; in dwc2_drd_role_sw_set()
109 * If role-switch set is called before the udc_start, we need to enable in dwc2_drd_role_sw_set()
111 * mode and sessions. It is the case if cable is plugged at boot. in dwc2_drd_role_sw_set()
113 if (!hsotg->ll_hw_enabled && hsotg->clk) { in dwc2_drd_role_sw_set()
114 int ret = clk_prepare_enable(hsotg->clk); in dwc2_drd_role_sw_set()
120 spin_lock_irqsave(&hsotg->lock, flags); in dwc2_drd_role_sw_set()
122 if (role == USB_ROLE_NONE) { in dwc2_drd_role_sw_set()
123 /* default operation mode when usb role is USB_ROLE_NONE */ in dwc2_drd_role_sw_set()
124 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) in dwc2_drd_role_sw_set()
125 role = USB_ROLE_HOST; in dwc2_drd_role_sw_set()
126 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) in dwc2_drd_role_sw_set()
127 role = USB_ROLE_DEVICE; in dwc2_drd_role_sw_set()
133 hsotg->lx_state == DWC2_L2 && in dwc2_drd_role_sw_set()
134 hsotg->params.power_down == DWC2_POWER_DOWN_PARAM_NONE && in dwc2_drd_role_sw_set()
135 hsotg->bus_suspended && in dwc2_drd_role_sw_set()
136 !hsotg->params.no_clock_gating) in dwc2_drd_role_sw_set()
139 if (role == USB_ROLE_HOST) { in dwc2_drd_role_sw_set()
141 } else if (role == USB_ROLE_DEVICE) { in dwc2_drd_role_sw_set()
157 spin_unlock_irqrestore(&hsotg->lock, flags); in dwc2_drd_role_sw_set()
159 if (!already && hsotg->dr_mode == USB_DR_MODE_OTG) in dwc2_drd_role_sw_set()
161 dwc2_force_mode(hsotg, role == USB_ROLE_HOST); in dwc2_drd_role_sw_set()
163 if (!hsotg->ll_hw_enabled && hsotg->clk) in dwc2_drd_role_sw_set()
164 clk_disable_unprepare(hsotg->clk); in dwc2_drd_role_sw_set()
166 dev_dbg(hsotg->dev, "%s-session valid\n", in dwc2_drd_role_sw_set()
167 role == USB_ROLE_NONE ? "No" : in dwc2_drd_role_sw_set()
168 role == USB_ROLE_HOST ? "A" : "B"); in dwc2_drd_role_sw_set()
179 if (!device_property_read_bool(hsotg->dev, "usb-role-switch")) in dwc2_drd_init()
182 hsotg->role_sw_default_mode = usb_get_role_switch_default_mode(hsotg->dev); in dwc2_drd_init()
184 role_sw_desc.fwnode = dev_fwnode(hsotg->dev); in dwc2_drd_init()
188 role_sw = usb_role_switch_register(hsotg->dev, &role_sw_desc); in dwc2_drd_init()
191 dev_err(hsotg->dev, in dwc2_drd_init()
192 "failed to register role switch: %d\n", ret); in dwc2_drd_init()
196 hsotg->role_sw = role_sw; in dwc2_drd_init()
208 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) { in dwc2_drd_suspend()
220 enum usb_role role; in dwc2_drd_resume() local
222 if (hsotg->role_sw) { in dwc2_drd_resume()
223 /* get last known role (as the get ops isn't implemented by this driver) */ in dwc2_drd_resume()
224 role = usb_role_switch_get_role(hsotg->role_sw); in dwc2_drd_resume()
226 if (role == USB_ROLE_NONE) { in dwc2_drd_resume()
227 if (hsotg->role_sw_default_mode == USB_DR_MODE_HOST) in dwc2_drd_resume()
228 role = USB_ROLE_HOST; in dwc2_drd_resume()
229 else if (hsotg->role_sw_default_mode == USB_DR_MODE_PERIPHERAL) in dwc2_drd_resume()
230 role = USB_ROLE_DEVICE; in dwc2_drd_resume()
233 /* restore last role that may have been lost */ in dwc2_drd_resume()
234 if (role == USB_ROLE_HOST) in dwc2_drd_resume()
236 else if (role == USB_ROLE_DEVICE) in dwc2_drd_resume()
239 dwc2_force_mode(hsotg, role == USB_ROLE_HOST); in dwc2_drd_resume()
241 dev_dbg(hsotg->dev, "resuming %s-session valid\n", in dwc2_drd_resume()
242 role == USB_ROLE_NONE ? "No" : in dwc2_drd_resume()
243 role == USB_ROLE_HOST ? "A" : "B"); in dwc2_drd_resume()
246 if (hsotg->role_sw && !hsotg->params.external_id_pin_ctl) { in dwc2_drd_resume()
257 if (hsotg->role_sw) in dwc2_drd_exit()
258 usb_role_switch_unregister(hsotg->role_sw); in dwc2_drd_exit()