1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * mtu3_dr.c - dual role switch and host glue layer 4 * 5 * Copyright (C) 2016 MediaTek Inc. 6 * 7 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com> 8 */ 9 10 #include "mtu3.h" 11 #include "mtu3_dr.h" 12 #include "mtu3_debug.h" 13 14 #define USB2_PORT 2 15 #define USB3_PORT 3 16 17 static inline struct ssusb_mtk *otg_sx_to_ssusb(struct otg_switch_mtk *otg_sx) 18 { 19 return container_of(otg_sx, struct ssusb_mtk, otg_switch); 20 } 21 22 static void toggle_opstate(struct ssusb_mtk *ssusb) 23 { 24 if (!ssusb->otg_switch.is_u3_drd) { 25 mtu3_setbits(ssusb->mac_base, U3D_DEVICE_CONTROL, DC_SESSION); 26 mtu3_setbits(ssusb->mac_base, U3D_POWER_MANAGEMENT, SOFT_CONN); 27 } 28 } 29 30 /* only port0 supports dual-role mode */ 31 static int ssusb_port0_switch(struct ssusb_mtk *ssusb, 32 int version, bool tohost) 33 { 34 void __iomem *ibase = ssusb->ippc_base; 35 u32 value; 36 37 dev_dbg(ssusb->dev, "%s (switch u%d port0 to %s)\n", __func__, 38 version, tohost ? "host" : "device"); 39 40 if (version == USB2_PORT) { 41 /* 1. power off and disable u2 port0 */ 42 value = mtu3_readl(ibase, SSUSB_U2_CTRL(0)); 43 value |= SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS; 44 mtu3_writel(ibase, SSUSB_U2_CTRL(0), value); 45 46 /* 2. power on, enable u2 port0 and select its mode */ 47 value = mtu3_readl(ibase, SSUSB_U2_CTRL(0)); 48 value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS); 49 value = tohost ? (value | SSUSB_U2_PORT_HOST_SEL) : 50 (value & (~SSUSB_U2_PORT_HOST_SEL)); 51 mtu3_writel(ibase, SSUSB_U2_CTRL(0), value); 52 } else { 53 /* 1. power off and disable u3 port0 */ 54 value = mtu3_readl(ibase, SSUSB_U3_CTRL(0)); 55 value |= SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS; 56 mtu3_writel(ibase, SSUSB_U3_CTRL(0), value); 57 58 /* 2. power on, enable u3 port0 and select its mode */ 59 value = mtu3_readl(ibase, SSUSB_U3_CTRL(0)); 60 value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS); 61 value = tohost ? (value | SSUSB_U3_PORT_HOST_SEL) : 62 (value & (~SSUSB_U3_PORT_HOST_SEL)); 63 mtu3_writel(ibase, SSUSB_U3_CTRL(0), value); 64 } 65 66 return 0; 67 } 68 69 static void switch_port_to_host(struct ssusb_mtk *ssusb) 70 { 71 u32 check_clk = 0; 72 73 dev_dbg(ssusb->dev, "%s\n", __func__); 74 75 ssusb_port0_switch(ssusb, USB2_PORT, true); 76 77 if (ssusb->otg_switch.is_u3_drd) { 78 ssusb_port0_switch(ssusb, USB3_PORT, true); 79 check_clk = SSUSB_U3_MAC_RST_B_STS; 80 } 81 82 ssusb_check_clocks(ssusb, check_clk); 83 84 /* after all clocks are stable */ 85 toggle_opstate(ssusb); 86 } 87 88 static void switch_port_to_device(struct ssusb_mtk *ssusb) 89 { 90 u32 check_clk = 0; 91 92 dev_dbg(ssusb->dev, "%s\n", __func__); 93 94 ssusb_port0_switch(ssusb, USB2_PORT, false); 95 96 if (ssusb->otg_switch.is_u3_drd) { 97 ssusb_port0_switch(ssusb, USB3_PORT, false); 98 check_clk = SSUSB_U3_MAC_RST_B_STS; 99 } 100 101 ssusb_check_clocks(ssusb, check_clk); 102 } 103 104 int ssusb_set_vbus(struct otg_switch_mtk *otg_sx, int is_on) 105 { 106 struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx); 107 struct regulator *vbus = otg_sx->vbus; 108 int ret; 109 110 /* vbus is optional */ 111 if (!vbus) 112 return 0; 113 114 dev_dbg(ssusb->dev, "%s: turn %s\n", __func__, is_on ? "on" : "off"); 115 116 if (is_on) { 117 ret = regulator_enable(vbus); 118 if (ret) { 119 dev_err(ssusb->dev, "vbus regulator enable failed\n"); 120 return ret; 121 } 122 } else { 123 regulator_disable(vbus); 124 } 125 126 return 0; 127 } 128 129 static void ssusb_mode_sw_work(struct work_struct *work) 130 { 131 struct otg_switch_mtk *otg_sx = 132 container_of(work, struct otg_switch_mtk, dr_work); 133 struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx); 134 struct mtu3 *mtu = ssusb->u3d; 135 enum usb_role desired_role = otg_sx->desired_role; 136 enum usb_role current_role; 137 138 current_role = ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE; 139 140 if (desired_role == USB_ROLE_NONE) 141 desired_role = USB_ROLE_HOST; 142 143 if (current_role == desired_role) 144 return; 145 146 dev_dbg(ssusb->dev, "set role : %s\n", usb_role_string(desired_role)); 147 mtu3_dbg_trace(ssusb->dev, "set role : %s", usb_role_string(desired_role)); 148 149 switch (desired_role) { 150 case USB_ROLE_HOST: 151 ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_HOST); 152 mtu3_stop(mtu); 153 switch_port_to_host(ssusb); 154 ssusb_set_vbus(otg_sx, 1); 155 ssusb->is_host = true; 156 break; 157 case USB_ROLE_DEVICE: 158 ssusb_set_force_mode(ssusb, MTU3_DR_FORCE_DEVICE); 159 ssusb->is_host = false; 160 ssusb_set_vbus(otg_sx, 0); 161 switch_port_to_device(ssusb); 162 mtu3_start(mtu); 163 break; 164 case USB_ROLE_NONE: 165 default: 166 dev_err(ssusb->dev, "invalid role\n"); 167 } 168 } 169 170 static void ssusb_set_mode(struct otg_switch_mtk *otg_sx, enum usb_role role) 171 { 172 struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx); 173 174 if (ssusb->dr_mode != USB_DR_MODE_OTG) 175 return; 176 177 otg_sx->desired_role = role; 178 queue_work(system_freezable_wq, &otg_sx->dr_work); 179 } 180 181 static int ssusb_id_notifier(struct notifier_block *nb, 182 unsigned long event, void *ptr) 183 { 184 struct otg_switch_mtk *otg_sx = 185 container_of(nb, struct otg_switch_mtk, id_nb); 186 187 ssusb_set_mode(otg_sx, event ? USB_ROLE_HOST : USB_ROLE_DEVICE); 188 189 return NOTIFY_DONE; 190 } 191 192 static int ssusb_extcon_register(struct otg_switch_mtk *otg_sx) 193 { 194 struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx); 195 struct extcon_dev *edev = otg_sx->edev; 196 int ret; 197 198 /* extcon is optional */ 199 if (!edev) 200 return 0; 201 202 otg_sx->id_nb.notifier_call = ssusb_id_notifier; 203 ret = devm_extcon_register_notifier(ssusb->dev, edev, EXTCON_USB_HOST, 204 &otg_sx->id_nb); 205 if (ret < 0) { 206 dev_err(ssusb->dev, "failed to register notifier for USB-HOST\n"); 207 return ret; 208 } 209 210 ret = extcon_get_state(edev, EXTCON_USB_HOST); 211 dev_dbg(ssusb->dev, "EXTCON_USB_HOST: %d\n", ret); 212 213 /* default as host, switch to device mode if needed */ 214 if (!ret) 215 ssusb_set_mode(otg_sx, USB_ROLE_DEVICE); 216 217 return 0; 218 } 219 220 /* 221 * We provide an interface via debugfs to switch between host and device modes 222 * depending on user input. 223 * This is useful in special cases, such as uses TYPE-A receptacle but also 224 * wants to support dual-role mode. 225 */ 226 void ssusb_mode_switch(struct ssusb_mtk *ssusb, int to_host) 227 { 228 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch; 229 230 ssusb_set_mode(otg_sx, to_host ? USB_ROLE_HOST : USB_ROLE_DEVICE); 231 } 232 233 void ssusb_set_force_mode(struct ssusb_mtk *ssusb, 234 enum mtu3_dr_force_mode mode) 235 { 236 u32 value; 237 238 value = mtu3_readl(ssusb->ippc_base, SSUSB_U2_CTRL(0)); 239 switch (mode) { 240 case MTU3_DR_FORCE_DEVICE: 241 value |= SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG; 242 break; 243 case MTU3_DR_FORCE_HOST: 244 value |= SSUSB_U2_PORT_FORCE_IDDIG; 245 value &= ~SSUSB_U2_PORT_RG_IDDIG; 246 break; 247 case MTU3_DR_FORCE_NONE: 248 value &= ~(SSUSB_U2_PORT_FORCE_IDDIG | SSUSB_U2_PORT_RG_IDDIG); 249 break; 250 default: 251 return; 252 } 253 mtu3_writel(ssusb->ippc_base, SSUSB_U2_CTRL(0), value); 254 } 255 256 static int ssusb_role_sw_set(struct usb_role_switch *sw, enum usb_role role) 257 { 258 struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw); 259 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch; 260 261 ssusb_set_mode(otg_sx, role); 262 263 return 0; 264 } 265 266 static enum usb_role ssusb_role_sw_get(struct usb_role_switch *sw) 267 { 268 struct ssusb_mtk *ssusb = usb_role_switch_get_drvdata(sw); 269 270 return ssusb->is_host ? USB_ROLE_HOST : USB_ROLE_DEVICE; 271 } 272 273 static int ssusb_role_sw_register(struct otg_switch_mtk *otg_sx) 274 { 275 struct usb_role_switch_desc role_sx_desc = { 0 }; 276 struct ssusb_mtk *ssusb = otg_sx_to_ssusb(otg_sx); 277 278 if (!otg_sx->role_sw_used) 279 return 0; 280 281 role_sx_desc.set = ssusb_role_sw_set; 282 role_sx_desc.get = ssusb_role_sw_get; 283 role_sx_desc.fwnode = dev_fwnode(ssusb->dev); 284 role_sx_desc.driver_data = ssusb; 285 otg_sx->role_sw = usb_role_switch_register(ssusb->dev, &role_sx_desc); 286 287 return PTR_ERR_OR_ZERO(otg_sx->role_sw); 288 } 289 290 int ssusb_otg_switch_init(struct ssusb_mtk *ssusb) 291 { 292 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch; 293 int ret = 0; 294 295 INIT_WORK(&otg_sx->dr_work, ssusb_mode_sw_work); 296 297 if (otg_sx->manual_drd_enabled) 298 ssusb_dr_debugfs_init(ssusb); 299 else if (otg_sx->role_sw_used) 300 ret = ssusb_role_sw_register(otg_sx); 301 else 302 ret = ssusb_extcon_register(otg_sx); 303 304 return ret; 305 } 306 307 void ssusb_otg_switch_exit(struct ssusb_mtk *ssusb) 308 { 309 struct otg_switch_mtk *otg_sx = &ssusb->otg_switch; 310 311 cancel_work_sync(&otg_sx->dr_work); 312 usb_role_switch_unregister(otg_sx->role_sw); 313 } 314