Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 1 | /** |
| 2 | * dwc3_otg.c - DesignWare USB3 DRD Controller OTG |
| 3 | * |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 4 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 and |
| 8 | * only version 2 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/usb.h> |
| 17 | #include <linux/usb/hcd.h> |
| 18 | #include <linux/platform_device.h> |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 19 | #include <linux/regulator/consumer.h> |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 20 | |
| 21 | #include "core.h" |
| 22 | #include "dwc3_otg.h" |
| 23 | #include "io.h" |
| 24 | #include "xhci.h" |
| 25 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 26 | static void dwc3_otg_reset(struct dwc3_otg *dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 27 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 28 | static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode); |
| 29 | static void dwc3_otg_reset(struct dwc3_otg *dotg); |
| 30 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 31 | /** |
| 32 | * dwc3_otg_set_host_regs - reset dwc3 otg registers to host operation. |
| 33 | * |
| 34 | * This function sets the OTG registers to work in A-Device host mode. |
| 35 | * This function should be called just before entering to A-Device mode. |
| 36 | * |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 37 | * @w: Pointer to the dwc3 otg struct |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 38 | */ |
| 39 | static void dwc3_otg_set_host_regs(struct dwc3_otg *dotg) |
| 40 | { |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 41 | u32 reg; |
| 42 | struct dwc3 *dwc = dotg->dwc; |
| 43 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 44 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 45 | if (ext_xceiv && !ext_xceiv->otg_capability) { |
| 46 | /* Set OCTL[6](PeriMode) to 0 (host) */ |
| 47 | reg = dwc3_readl(dotg->regs, DWC3_OCTL); |
| 48 | reg &= ~DWC3_OTG_OCTL_PERIMODE; |
| 49 | dwc3_writel(dotg->regs, DWC3_OCTL, reg); |
| 50 | } else { |
| 51 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 52 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 53 | reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_HOST); |
| 54 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 55 | } |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 56 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 57 | |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 58 | static int dwc3_otg_set_suspend(struct usb_phy *phy, int suspend) |
| 59 | { |
| 60 | struct usb_otg *otg = phy->otg; |
| 61 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 62 | |
| 63 | if (dotg->host_bus_suspend == suspend) |
| 64 | return 0; |
| 65 | |
| 66 | dotg->host_bus_suspend = suspend; |
| 67 | if (suspend) { |
| 68 | pm_runtime_put_sync(phy->dev); |
| 69 | } else { |
| 70 | pm_runtime_get_noresume(phy->dev); |
| 71 | pm_runtime_resume(phy->dev); |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 77 | /** |
| 78 | * dwc3_otg_set_host_power - Enable port power control for host operation |
| 79 | * |
| 80 | * This function enables the OTG Port Power required to operate in Host mode |
| 81 | * This function should be called only after XHCI driver has set the port |
| 82 | * power in PORTSC register. |
| 83 | * |
| 84 | * @w: Pointer to the dwc3 otg struct |
| 85 | */ |
| 86 | void dwc3_otg_set_host_power(struct dwc3_otg *dotg) |
| 87 | { |
| 88 | u32 osts; |
| 89 | |
| 90 | osts = dwc3_readl(dotg->regs, DWC3_OSTS); |
| 91 | if (!(osts & 0x8)) |
| 92 | dev_err(dotg->dwc->dev, "%s: xHCIPrtPower not set\n", __func__); |
| 93 | |
| 94 | dwc3_writel(dotg->regs, DWC3_OCTL, DWC3_OTG_OCTL_PRTPWRCTL); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * dwc3_otg_set_peripheral_regs - reset dwc3 otg registers to peripheral operation. |
| 99 | * |
| 100 | * This function sets the OTG registers to work in B-Device peripheral mode. |
| 101 | * This function should be called just before entering to B-Device mode. |
| 102 | * |
| 103 | * @w: Pointer to the dwc3 otg workqueue. |
| 104 | */ |
| 105 | static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg) |
| 106 | { |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 107 | u32 reg; |
| 108 | struct dwc3 *dwc = dotg->dwc; |
| 109 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 110 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 111 | if (ext_xceiv && !ext_xceiv->otg_capability) { |
| 112 | /* Set OCTL[6](PeriMode) to 1 (peripheral) */ |
| 113 | reg = dwc3_readl(dotg->regs, DWC3_OCTL); |
| 114 | reg |= DWC3_OTG_OCTL_PERIMODE; |
| 115 | dwc3_writel(dotg->regs, DWC3_OCTL, reg); |
| 116 | /* |
| 117 | * TODO: add more OTG registers writes for PERIPHERAL mode here, |
| 118 | * see figure 12-19 B-device flow in dwc3 Synopsis spec |
| 119 | */ |
| 120 | } else { |
| 121 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 122 | reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG)); |
| 123 | reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE); |
| 124 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 125 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * dwc3_otg_start_host - helper function for starting/stoping the host controller driver. |
| 130 | * |
| 131 | * @otg: Pointer to the otg_transceiver structure. |
| 132 | * @on: start / stop the host controller driver. |
| 133 | * |
| 134 | * Returns 0 on success otherwise negative errno. |
| 135 | */ |
| 136 | static int dwc3_otg_start_host(struct usb_otg *otg, int on) |
| 137 | { |
| 138 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 139 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 140 | struct dwc3 *dwc = dotg->dwc; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 141 | int ret = 0; |
| 142 | |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 143 | if (!dwc->xhci) |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 144 | return -EINVAL; |
| 145 | |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 146 | if (!dotg->vbus_otg) { |
| 147 | dotg->vbus_otg = devm_regulator_get(dwc->dev->parent, |
| 148 | "vbus_dwc3"); |
| 149 | if (IS_ERR(dotg->vbus_otg)) { |
| 150 | dev_err(dwc->dev, "Failed to get vbus regulator\n"); |
| 151 | ret = PTR_ERR(dotg->vbus_otg); |
| 152 | dotg->vbus_otg = 0; |
| 153 | return ret; |
| 154 | } |
| 155 | } |
| 156 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 157 | if (on) { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 158 | dev_dbg(otg->phy->dev, "%s: turn on host\n", __func__); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 159 | |
| 160 | /* |
| 161 | * This should be revisited for more testing post-silicon. |
| 162 | * In worst case we may need to disconnect the root hub |
| 163 | * before stopping the controller so that it does not |
| 164 | * interfere with runtime pm/system pm. |
| 165 | * We can also consider registering and unregistering xhci |
| 166 | * platform device. It is almost similar to add_hcd and |
| 167 | * remove_hcd, But we may not use standard set_host method |
| 168 | * anymore. |
| 169 | */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 170 | dwc3_otg_set_host_regs(dotg); |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 171 | /* |
| 172 | * FIXME If micro A cable is disconnected during system suspend, |
| 173 | * xhci platform device will be removed before runtime pm is |
| 174 | * enabled for xhci device. Due to this, disable_depth becomes |
| 175 | * greater than one and runtimepm is not enabled for next microA |
| 176 | * connect. Fix this by calling pm_runtime_init for xhci device. |
| 177 | */ |
| 178 | pm_runtime_init(&dwc->xhci->dev); |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 179 | ret = platform_device_add(dwc->xhci); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 180 | if (ret) { |
| 181 | dev_err(otg->phy->dev, |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 182 | "%s: failed to add XHCI pdev ret=%d\n", |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 183 | __func__, ret); |
| 184 | return ret; |
| 185 | } |
| 186 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 187 | dwc3_otg_notify_host_mode(otg, on); |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 188 | ret = regulator_enable(dotg->vbus_otg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 189 | if (ret) { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 190 | dev_err(otg->phy->dev, "unable to enable vbus_otg\n"); |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 191 | platform_device_del(dwc->xhci); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 192 | return ret; |
| 193 | } |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 194 | |
| 195 | /* re-init OTG EVTEN register as XHCI reset clears it */ |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 196 | if (ext_xceiv && !ext_xceiv->otg_capability) |
| 197 | dwc3_otg_reset(dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 198 | } else { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 199 | dev_dbg(otg->phy->dev, "%s: turn off host\n", __func__); |
| 200 | |
Manu Gautam | 6172159 | 2012-11-06 18:09:39 +0530 | [diff] [blame] | 201 | platform_device_del(dwc->xhci); |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 202 | |
| 203 | ret = regulator_disable(dotg->vbus_otg); |
| 204 | if (ret) { |
| 205 | dev_err(otg->phy->dev, "unable to disable vbus_otg\n"); |
| 206 | return ret; |
| 207 | } |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 208 | dwc3_otg_notify_host_mode(otg, on); |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * Perform USB hardware RESET (both core reset and DBM reset) |
| 212 | * when moving from host to peripheral. This is required for |
| 213 | * peripheral mode to work. |
| 214 | */ |
| 215 | if (ext_xceiv && ext_xceiv->otg_capability && |
| 216 | ext_xceiv->ext_block_reset) |
| 217 | ext_xceiv->ext_block_reset(true); |
| 218 | |
Manu Gautam | ab3da1b | 2013-04-02 14:29:01 +0530 | [diff] [blame] | 219 | dwc3_otg_set_peripheral_regs(dotg); |
| 220 | |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 221 | /* re-init core and OTG registers as block reset clears these */ |
| 222 | dwc3_post_host_reset_core_init(dwc); |
| 223 | if (ext_xceiv && !ext_xceiv->otg_capability) |
| 224 | dwc3_otg_reset(dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * dwc3_otg_set_host - bind/unbind the host controller driver. |
| 232 | * |
| 233 | * @otg: Pointer to the otg_transceiver structure. |
| 234 | * @host: Pointer to the usb_bus structure. |
| 235 | * |
| 236 | * Returns 0 on success otherwise negative errno. |
| 237 | */ |
| 238 | static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host) |
| 239 | { |
| 240 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 241 | |
| 242 | if (host) { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 243 | dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n", |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 244 | __func__, host->bus_name); |
| 245 | otg->host = host; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 246 | /* |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 247 | * Though XHCI power would be set by now, but some delay is |
| 248 | * required for XHCI controller before setting OTG Port Power |
| 249 | * TODO: Tune this delay |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 250 | */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 251 | msleep(300); |
| 252 | dwc3_otg_set_host_power(dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 253 | } else { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 254 | otg->host = NULL; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * dwc3_otg_start_peripheral - bind/unbind the peripheral controller. |
| 262 | * |
| 263 | * @otg: Pointer to the otg_transceiver structure. |
| 264 | * @gadget: pointer to the usb_gadget structure. |
| 265 | * |
| 266 | * Returns 0 on success otherwise negative errno. |
| 267 | */ |
| 268 | static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on) |
| 269 | { |
| 270 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 271 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 272 | |
| 273 | if (!otg->gadget) |
| 274 | return -EINVAL; |
| 275 | |
| 276 | if (on) { |
| 277 | dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n", |
| 278 | __func__, otg->gadget->name); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 279 | |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 280 | /* Core reset is not required during start peripheral. Only |
| 281 | * DBM reset is required, hence perform only DBM reset here */ |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 282 | if (ext_xceiv && ext_xceiv->otg_capability && |
| 283 | ext_xceiv->ext_block_reset) |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 284 | ext_xceiv->ext_block_reset(false); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 285 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 286 | dwc3_otg_set_peripheral_regs(dotg); |
| 287 | usb_gadget_vbus_connect(otg->gadget); |
| 288 | } else { |
| 289 | dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n", |
| 290 | __func__, otg->gadget->name); |
| 291 | usb_gadget_vbus_disconnect(otg->gadget); |
| 292 | } |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver. |
| 299 | * |
| 300 | * @otg: Pointer to the otg_transceiver structure. |
| 301 | * @gadget: pointer to the usb_gadget structure. |
| 302 | * |
| 303 | * Returns 0 on success otherwise negative errno. |
| 304 | */ |
| 305 | static int dwc3_otg_set_peripheral(struct usb_otg *otg, |
| 306 | struct usb_gadget *gadget) |
| 307 | { |
| 308 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 309 | |
| 310 | if (gadget) { |
| 311 | dev_dbg(otg->phy->dev, "%s: set gadget %s\n", |
| 312 | __func__, gadget->name); |
| 313 | otg->gadget = gadget; |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 314 | schedule_work(&dotg->sm_work); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 315 | } else { |
| 316 | if (otg->phy->state == OTG_STATE_B_PERIPHERAL) { |
| 317 | dwc3_otg_start_peripheral(otg, 0); |
| 318 | otg->gadget = NULL; |
| 319 | otg->phy->state = OTG_STATE_UNDEFINED; |
| 320 | schedule_work(&dotg->sm_work); |
| 321 | } else { |
| 322 | otg->gadget = NULL; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /** |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 330 | * dwc3_ext_chg_det_done - callback to handle charger detection completion |
| 331 | * @otg: Pointer to the otg transceiver structure |
| 332 | * @charger: Pointer to the external charger structure |
| 333 | * |
| 334 | * Returns 0 on success |
| 335 | */ |
| 336 | static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg) |
| 337 | { |
| 338 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 339 | |
| 340 | /* |
| 341 | * Ignore chg_detection notification if BSV has gone off by this time. |
| 342 | * STOP chg_det as part of !BSV handling would reset the chg_det flags |
| 343 | */ |
| 344 | if (test_bit(B_SESS_VLD, &dotg->inputs)) |
| 345 | schedule_work(&dotg->sm_work); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * dwc3_set_charger - bind/unbind external charger driver |
| 350 | * @otg: Pointer to the otg transceiver structure |
| 351 | * @charger: Pointer to the external charger structure |
| 352 | * |
| 353 | * Returns 0 on success |
| 354 | */ |
| 355 | int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger) |
| 356 | { |
| 357 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 358 | |
| 359 | dotg->charger = charger; |
| 360 | if (charger) |
| 361 | charger->notify_detection_complete = dwc3_ext_chg_det_done; |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 366 | /** |
| 367 | * dwc3_ext_event_notify - callback to handle events from external transceiver |
| 368 | * @otg: Pointer to the otg transceiver structure |
| 369 | * @event: Event reported by transceiver |
| 370 | * |
| 371 | * Returns 0 on success |
| 372 | */ |
| 373 | static void dwc3_ext_event_notify(struct usb_otg *otg, |
| 374 | enum dwc3_ext_events event) |
| 375 | { |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 376 | static bool init; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 377 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 378 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
| 379 | struct usb_phy *phy = dotg->otg.phy; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 380 | int ret = 0; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 381 | |
Manu Gautam | f71d9cb | 2013-02-07 13:52:12 +0530 | [diff] [blame] | 382 | /* Flush processing any pending events before handling new ones */ |
| 383 | if (init) |
| 384 | flush_work(&dotg->sm_work); |
| 385 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 386 | if (event == DWC3_EVENT_PHY_RESUME) { |
| 387 | if (!pm_runtime_status_suspended(phy->dev)) { |
| 388 | dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n"); |
| 389 | } else { |
| 390 | dev_dbg(phy->dev, "ext PHY_RESUME event received\n"); |
| 391 | /* ext_xceiver would have taken h/w out of LPM by now */ |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 392 | ret = pm_runtime_get(phy->dev); |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 393 | if ((phy->state == OTG_STATE_A_HOST) && |
| 394 | dotg->host_bus_suspend) |
| 395 | dotg->host_bus_suspend = 0; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 396 | if (ret == -EACCES) { |
| 397 | /* pm_runtime_get may fail during system |
| 398 | resume with -EACCES error */ |
| 399 | pm_runtime_disable(phy->dev); |
| 400 | pm_runtime_set_active(phy->dev); |
| 401 | pm_runtime_enable(phy->dev); |
| 402 | } else if (ret < 0) { |
| 403 | dev_warn(phy->dev, "pm_runtime_get failed!\n"); |
| 404 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 405 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 406 | } else if (event == DWC3_EVENT_XCEIV_STATE) { |
Manu Gautam | f71d9cb | 2013-02-07 13:52:12 +0530 | [diff] [blame] | 407 | if (pm_runtime_status_suspended(phy->dev)) { |
| 408 | dev_warn(phy->dev, "PHY_STATE event in LPM!!!!\n"); |
| 409 | ret = pm_runtime_get(phy->dev); |
| 410 | if (ret < 0) |
| 411 | dev_warn(phy->dev, "pm_runtime_get failed!!\n"); |
| 412 | } |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 413 | if (ext_xceiv->id == DWC3_ID_FLOAT) { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 414 | dev_dbg(phy->dev, "XCVR: ID set\n"); |
| 415 | set_bit(ID, &dotg->inputs); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 416 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 417 | dev_dbg(phy->dev, "XCVR: ID clear\n"); |
| 418 | clear_bit(ID, &dotg->inputs); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 419 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 420 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 421 | if (ext_xceiv->bsv) { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 422 | dev_dbg(phy->dev, "XCVR: BSV set\n"); |
| 423 | set_bit(B_SESS_VLD, &dotg->inputs); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 424 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 425 | dev_dbg(phy->dev, "XCVR: BSV clear\n"); |
| 426 | clear_bit(B_SESS_VLD, &dotg->inputs); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 427 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 428 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 429 | if (!init) { |
| 430 | init = true; |
Manu Gautam | 4ff0724 | 2013-03-27 14:31:11 +0530 | [diff] [blame] | 431 | if (!work_busy(&dotg->sm_work)) |
| 432 | schedule_work(&dotg->sm_work); |
| 433 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 434 | complete(&dotg->dwc3_xcvr_vbus_init); |
| 435 | dev_dbg(phy->dev, "XCVR: BSV init complete\n"); |
| 436 | return; |
| 437 | } |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 438 | |
| 439 | schedule_work(&dotg->sm_work); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 440 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /** |
| 444 | * dwc3_set_ext_xceiv - bind/unbind external transceiver driver |
| 445 | * @otg: Pointer to the otg transceiver structure |
| 446 | * @ext_xceiv: Pointer to the external transceiver struccture |
| 447 | * |
| 448 | * Returns 0 on success |
| 449 | */ |
| 450 | int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv) |
| 451 | { |
| 452 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 453 | |
| 454 | dotg->ext_xceiv = ext_xceiv; |
| 455 | if (ext_xceiv) |
| 456 | ext_xceiv->notify_ext_events = dwc3_ext_event_notify; |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 461 | static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode) |
| 462 | { |
| 463 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 464 | |
| 465 | if (!dotg->psy) { |
| 466 | dev_err(otg->phy->dev, "no usb power supply registered\n"); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | if (host_mode) |
| 471 | power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM); |
| 472 | else |
| 473 | power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE); |
| 474 | } |
| 475 | |
| 476 | static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA) |
| 477 | { |
| 478 | static int power_supply_type; |
| 479 | struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg); |
| 480 | |
| 481 | |
Manu Gautam | 6c0ff03 | 2012-11-02 14:55:35 +0530 | [diff] [blame] | 482 | if (!dotg->psy || !dotg->charger) { |
| 483 | dev_err(phy->dev, "no usb power supply/charger registered\n"); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
Manu Gautam | 6c0ff03 | 2012-11-02 14:55:35 +0530 | [diff] [blame] | 487 | if (dotg->charger->charging_disabled) |
| 488 | return 0; |
| 489 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 490 | if (dotg->charger->chg_type == DWC3_SDP_CHARGER) |
| 491 | power_supply_type = POWER_SUPPLY_TYPE_USB; |
| 492 | else if (dotg->charger->chg_type == DWC3_CDP_CHARGER) |
| 493 | power_supply_type = POWER_SUPPLY_TYPE_USB_CDP; |
Manu Gautam | a1e331d | 2013-02-07 14:55:05 +0530 | [diff] [blame] | 494 | else if (dotg->charger->chg_type == DWC3_DCP_CHARGER || |
| 495 | dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER) |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 496 | power_supply_type = POWER_SUPPLY_TYPE_USB_DCP; |
| 497 | else |
| 498 | power_supply_type = POWER_SUPPLY_TYPE_BATTERY; |
| 499 | |
| 500 | power_supply_set_supply_type(dotg->psy, power_supply_type); |
| 501 | |
Vijayavardhan Vennapusa | 2e0b418 | 2013-03-21 12:49:43 +0530 | [diff] [blame] | 502 | if ((dotg->charger->chg_type == DWC3_CDP_CHARGER) && mA > 2) |
| 503 | mA = DWC3_IDEV_CHG_MAX; |
| 504 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 505 | if (dotg->charger->max_power == mA) |
| 506 | return 0; |
| 507 | |
| 508 | dev_info(phy->dev, "Avail curr from USB = %u\n", mA); |
| 509 | |
| 510 | if (dotg->charger->max_power <= 2 && mA > 2) { |
| 511 | /* Enable charging */ |
| 512 | if (power_supply_set_online(dotg->psy, true)) |
| 513 | goto psy_error; |
| 514 | if (power_supply_set_current_limit(dotg->psy, 1000*mA)) |
| 515 | goto psy_error; |
| 516 | } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) { |
| 517 | /* Disable charging */ |
| 518 | if (power_supply_set_online(dotg->psy, false)) |
| 519 | goto psy_error; |
| 520 | /* Set max current limit */ |
| 521 | if (power_supply_set_current_limit(dotg->psy, 0)) |
| 522 | goto psy_error; |
| 523 | } |
| 524 | |
| 525 | power_supply_changed(dotg->psy); |
| 526 | dotg->charger->max_power = mA; |
| 527 | return 0; |
| 528 | |
| 529 | psy_error: |
| 530 | dev_dbg(phy->dev, "power supply error when setting property\n"); |
| 531 | return -ENXIO; |
| 532 | } |
| 533 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 534 | /* IRQs which OTG driver is interested in handling */ |
| 535 | #define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \ |
| 536 | DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) |
| 537 | |
| 538 | /** |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 539 | * dwc3_otg_interrupt - interrupt handler for dwc3 otg events. |
| 540 | * @_dotg: Pointer to out controller context structure |
| 541 | * |
| 542 | * Returns IRQ_HANDLED on success otherwise IRQ_NONE. |
| 543 | */ |
| 544 | static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg) |
| 545 | { |
| 546 | struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 547 | u32 osts, oevt_reg; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 548 | int ret = IRQ_NONE; |
| 549 | int handled_irqs = 0; |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 550 | struct usb_phy *phy = dotg->otg.phy; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 551 | |
| 552 | oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT); |
| 553 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 554 | if (!(oevt_reg & DWC3_OEVT_MASK)) |
| 555 | return IRQ_NONE; |
| 556 | |
| 557 | osts = dwc3_readl(dotg->regs, DWC3_OSTS); |
| 558 | |
| 559 | if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) || |
| 560 | (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) { |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 561 | /* |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 562 | * ID sts has changed, set inputs later, in the workqueue |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 563 | * function, switch from A to B or from B to A. |
| 564 | */ |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 565 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 566 | if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) { |
| 567 | if (osts & DWC3_OTG_OSTS_CONIDSTS) { |
| 568 | dev_dbg(phy->dev, "ID set\n"); |
| 569 | set_bit(ID, &dotg->inputs); |
| 570 | } else { |
| 571 | dev_dbg(phy->dev, "ID clear\n"); |
| 572 | clear_bit(ID, &dotg->inputs); |
| 573 | } |
| 574 | handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT; |
| 575 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 576 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 577 | if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) { |
| 578 | if (osts & DWC3_OTG_OSTS_BSESVALID) { |
| 579 | dev_dbg(phy->dev, "BSV set\n"); |
| 580 | set_bit(B_SESS_VLD, &dotg->inputs); |
| 581 | } else { |
| 582 | dev_dbg(phy->dev, "BSV clear\n"); |
| 583 | clear_bit(B_SESS_VLD, &dotg->inputs); |
| 584 | } |
| 585 | handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT; |
| 586 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 587 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 588 | schedule_work(&dotg->sm_work); |
| 589 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 590 | ret = IRQ_HANDLED; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 591 | |
| 592 | /* Clear the interrupts we handled */ |
| 593 | dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | /** |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 600 | * dwc3_otg_init_sm - initialize OTG statemachine input |
| 601 | * @dotg: Pointer to the dwc3_otg structure |
| 602 | * |
| 603 | */ |
| 604 | void dwc3_otg_init_sm(struct dwc3_otg *dotg) |
| 605 | { |
| 606 | u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS); |
| 607 | struct usb_phy *phy = dotg->otg.phy; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 608 | struct dwc3_ext_xceiv *ext_xceiv; |
| 609 | int ret; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 610 | |
| 611 | dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts); |
| 612 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 613 | /* |
| 614 | * VBUS initial state is reported after PMIC |
| 615 | * driver initialization. Wait for it. |
| 616 | */ |
| 617 | ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5); |
Manu Gautam | 4ff0724 | 2013-03-27 14:31:11 +0530 | [diff] [blame] | 618 | if (!ret) { |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 619 | dev_err(phy->dev, "%s: completion timeout\n", __func__); |
Manu Gautam | 4ff0724 | 2013-03-27 14:31:11 +0530 | [diff] [blame] | 620 | /* We can safely assume no cable connected */ |
| 621 | set_bit(ID, &dotg->inputs); |
| 622 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 623 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 624 | ext_xceiv = dotg->ext_xceiv; |
| 625 | dwc3_otg_reset(dotg); |
| 626 | if (ext_xceiv && !ext_xceiv->otg_capability) { |
| 627 | if (osts & DWC3_OTG_OSTS_CONIDSTS) |
| 628 | set_bit(ID, &dotg->inputs); |
| 629 | else |
| 630 | clear_bit(ID, &dotg->inputs); |
| 631 | |
| 632 | if (osts & DWC3_OTG_OSTS_BSESVALID) |
| 633 | set_bit(B_SESS_VLD, &dotg->inputs); |
| 634 | else |
| 635 | clear_bit(B_SESS_VLD, &dotg->inputs); |
| 636 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | /** |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 640 | * dwc3_otg_sm_work - workqueue function. |
| 641 | * |
| 642 | * @w: Pointer to the dwc3 otg workqueue |
| 643 | * |
| 644 | * NOTE: After any change in phy->state, |
| 645 | * we must reschdule the state machine. |
| 646 | */ |
| 647 | static void dwc3_otg_sm_work(struct work_struct *w) |
| 648 | { |
| 649 | struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work); |
| 650 | struct usb_phy *phy = dotg->otg.phy; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 651 | struct dwc3_charger *charger = dotg->charger; |
| 652 | bool work = 0; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 653 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 654 | pm_runtime_resume(phy->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 655 | dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state)); |
| 656 | |
| 657 | /* Check OTG state */ |
| 658 | switch (phy->state) { |
| 659 | case OTG_STATE_UNDEFINED: |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 660 | dwc3_otg_init_sm(dotg); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 661 | if (!dotg->psy) { |
| 662 | dotg->psy = power_supply_get_by_name("usb"); |
| 663 | |
| 664 | if (!dotg->psy) |
| 665 | dev_err(phy->dev, |
| 666 | "couldn't get usb power supply\n"); |
| 667 | } |
| 668 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 669 | /* Switch to A or B-Device according to ID / BSV */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 670 | if (!test_bit(ID, &dotg->inputs)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 671 | dev_dbg(phy->dev, "!id\n"); |
| 672 | phy->state = OTG_STATE_A_IDLE; |
| 673 | work = 1; |
| 674 | } else if (test_bit(B_SESS_VLD, &dotg->inputs)) { |
| 675 | dev_dbg(phy->dev, "b_sess_vld\n"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 676 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 677 | work = 1; |
| 678 | } else { |
| 679 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 680 | dev_dbg(phy->dev, "No device, trying to suspend\n"); |
| 681 | pm_runtime_put_sync(phy->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 682 | } |
| 683 | break; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 684 | |
| 685 | case OTG_STATE_B_IDLE: |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 686 | if (!test_bit(ID, &dotg->inputs)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 687 | dev_dbg(phy->dev, "!id\n"); |
| 688 | phy->state = OTG_STATE_A_IDLE; |
| 689 | work = 1; |
| 690 | if (charger) { |
| 691 | if (charger->chg_type == DWC3_INVALID_CHARGER) |
| 692 | charger->start_detection(dotg->charger, |
| 693 | false); |
| 694 | else |
| 695 | charger->chg_type = |
| 696 | DWC3_INVALID_CHARGER; |
| 697 | } |
| 698 | } else if (test_bit(B_SESS_VLD, &dotg->inputs)) { |
| 699 | dev_dbg(phy->dev, "b_sess_vld\n"); |
| 700 | if (charger) { |
| 701 | /* Has charger been detected? If no detect it */ |
| 702 | switch (charger->chg_type) { |
| 703 | case DWC3_DCP_CHARGER: |
Manu Gautam | a1e331d | 2013-02-07 14:55:05 +0530 | [diff] [blame] | 704 | case DWC3_PROPRIETARY_CHARGER: |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 705 | dev_dbg(phy->dev, "lpm, DCP charger\n"); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 706 | dwc3_otg_set_power(phy, |
| 707 | DWC3_IDEV_CHG_MAX); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 708 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 709 | break; |
| 710 | case DWC3_CDP_CHARGER: |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 711 | dwc3_otg_set_power(phy, |
| 712 | DWC3_IDEV_CHG_MAX); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 713 | dwc3_otg_start_peripheral(&dotg->otg, |
| 714 | 1); |
| 715 | phy->state = OTG_STATE_B_PERIPHERAL; |
| 716 | work = 1; |
| 717 | break; |
| 718 | case DWC3_SDP_CHARGER: |
| 719 | dwc3_otg_start_peripheral(&dotg->otg, |
| 720 | 1); |
| 721 | phy->state = OTG_STATE_B_PERIPHERAL; |
| 722 | work = 1; |
| 723 | break; |
| 724 | default: |
| 725 | dev_dbg(phy->dev, "chg_det started\n"); |
| 726 | charger->start_detection(charger, true); |
| 727 | break; |
| 728 | } |
| 729 | } else { |
| 730 | /* no charger registered, start peripheral */ |
| 731 | if (dwc3_otg_start_peripheral(&dotg->otg, 1)) { |
| 732 | /* |
| 733 | * Probably set_peripheral not called |
| 734 | * yet. We will re-try as soon as it |
| 735 | * will be called |
| 736 | */ |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 737 | dev_err(phy->dev, "enter lpm as\n" |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 738 | "unable to start B-device\n"); |
| 739 | phy->state = OTG_STATE_UNDEFINED; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 740 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 741 | return; |
| 742 | } |
| 743 | } |
| 744 | } else { |
Manu Gautam | 98013c2 | 2012-11-20 17:42:42 +0530 | [diff] [blame] | 745 | if (charger) |
| 746 | charger->start_detection(dotg->charger, false); |
| 747 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 748 | dwc3_otg_set_power(phy, 0); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 749 | dev_dbg(phy->dev, "No device, trying to suspend\n"); |
| 750 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 751 | } |
| 752 | break; |
| 753 | |
| 754 | case OTG_STATE_B_PERIPHERAL: |
| 755 | if (!test_bit(B_SESS_VLD, &dotg->inputs) || |
| 756 | !test_bit(ID, &dotg->inputs)) { |
| 757 | dev_dbg(phy->dev, "!id || !bsv\n"); |
| 758 | dwc3_otg_start_peripheral(&dotg->otg, 0); |
| 759 | phy->state = OTG_STATE_B_IDLE; |
| 760 | if (charger) |
| 761 | charger->chg_type = DWC3_INVALID_CHARGER; |
| 762 | work = 1; |
| 763 | } |
| 764 | break; |
| 765 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 766 | case OTG_STATE_A_IDLE: |
| 767 | /* Switch to A-Device*/ |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 768 | if (test_bit(ID, &dotg->inputs)) { |
| 769 | dev_dbg(phy->dev, "id\n"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 770 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 771 | work = 1; |
| 772 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 773 | phy->state = OTG_STATE_A_HOST; |
| 774 | if (dwc3_otg_start_host(&dotg->otg, 1)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 775 | /* |
| 776 | * Probably set_host was not called yet. |
| 777 | * We will re-try as soon as it will be called |
| 778 | */ |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 779 | dev_dbg(phy->dev, "enter lpm as\n" |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 780 | "unable to start A-device\n"); |
| 781 | phy->state = OTG_STATE_UNDEFINED; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 782 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 783 | return; |
| 784 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 785 | } |
| 786 | break; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 787 | |
| 788 | case OTG_STATE_A_HOST: |
| 789 | if (test_bit(ID, &dotg->inputs)) { |
| 790 | dev_dbg(phy->dev, "id\n"); |
| 791 | dwc3_otg_start_host(&dotg->otg, 0); |
| 792 | phy->state = OTG_STATE_B_IDLE; |
| 793 | work = 1; |
| 794 | } |
| 795 | break; |
| 796 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 797 | default: |
| 798 | dev_err(phy->dev, "%s: invalid otg-state\n", __func__); |
| 799 | |
| 800 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 801 | |
| 802 | if (work) |
| 803 | schedule_work(&dotg->sm_work); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 804 | } |
| 805 | |
| 806 | |
| 807 | /** |
| 808 | * dwc3_otg_reset - reset dwc3 otg registers. |
| 809 | * |
| 810 | * @w: Pointer to the dwc3 otg workqueue |
| 811 | */ |
| 812 | static void dwc3_otg_reset(struct dwc3_otg *dotg) |
| 813 | { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 814 | static int once; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 815 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
| 816 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 817 | /* |
| 818 | * OCFG[2] - OTG-Version = 1 |
| 819 | * OCFG[1] - HNPCap = 0 |
| 820 | * OCFG[0] - SRPCap = 0 |
| 821 | */ |
| 822 | dwc3_writel(dotg->regs, DWC3_OCFG, 0x4); |
| 823 | |
| 824 | /* |
| 825 | * OCTL[6] - PeriMode = 1 |
| 826 | * OCTL[5] - PrtPwrCtl = 0 |
| 827 | * OCTL[4] - HNPReq = 0 |
| 828 | * OCTL[3] - SesReq = 0 |
| 829 | * OCTL[2] - TermSelDLPulse = 0 |
| 830 | * OCTL[1] - DevSetHNPEn = 0 |
| 831 | * OCTL[0] - HstSetHNPEn = 0 |
| 832 | */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 833 | if (!once) { |
| 834 | dwc3_writel(dotg->regs, DWC3_OCTL, 0x40); |
| 835 | once++; |
| 836 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 837 | |
| 838 | /* Clear all otg events (interrupts) indications */ |
| 839 | dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF); |
| 840 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 841 | /* Enable ID/BSV StsChngEn event*/ |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 842 | if (ext_xceiv && !ext_xceiv->otg_capability) |
| 843 | dwc3_writel(dotg->regs, DWC3_OEVTEN, |
| 844 | DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | |
| 845 | DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | /** |
| 849 | * dwc3_otg_init - Initializes otg related registers |
| 850 | * @dwc: Pointer to out controller context structure |
| 851 | * |
| 852 | * Returns 0 on success otherwise negative errno. |
| 853 | */ |
| 854 | int dwc3_otg_init(struct dwc3 *dwc) |
| 855 | { |
| 856 | u32 reg; |
| 857 | int ret = 0; |
| 858 | struct dwc3_otg *dotg; |
| 859 | |
| 860 | dev_dbg(dwc->dev, "dwc3_otg_init\n"); |
| 861 | |
| 862 | /* |
| 863 | * GHWPARAMS6[10] bit is SRPSupport. |
| 864 | * This bit also reflects DWC_USB3_EN_OTG |
| 865 | */ |
| 866 | reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 867 | if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) { |
| 868 | /* |
| 869 | * No OTG support in the HW core. |
| 870 | * We return 0 to indicate no error, since this is acceptable |
| 871 | * situation, just continue probe the dwc3 driver without otg. |
| 872 | */ |
| 873 | dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n"); |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* Allocate and init otg instance */ |
| 878 | dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL); |
| 879 | if (!dotg) { |
| 880 | dev_err(dwc->dev, "unable to allocate dwc3_otg\n"); |
| 881 | return -ENOMEM; |
| 882 | } |
| 883 | |
Manu Gautam | 17206c2 | 2012-06-21 10:17:53 +0530 | [diff] [blame] | 884 | /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */ |
| 885 | dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev), |
| 886 | "otg_irq"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 887 | if (dotg->irq < 0) { |
Manu Gautam | 17206c2 | 2012-06-21 10:17:53 +0530 | [diff] [blame] | 888 | dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 889 | ret = -ENODEV; |
| 890 | goto err1; |
| 891 | } |
| 892 | |
| 893 | dotg->regs = dwc->regs; |
| 894 | |
| 895 | dotg->otg.set_peripheral = dwc3_otg_set_peripheral; |
| 896 | dotg->otg.set_host = dwc3_otg_set_host; |
| 897 | |
| 898 | /* This reference is used by dwc3 modules for checking otg existance */ |
| 899 | dwc->dotg = dotg; |
| 900 | |
| 901 | dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL); |
| 902 | if (!dotg->otg.phy) { |
| 903 | dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n"); |
| 904 | ret = -ENOMEM; |
| 905 | goto err1; |
| 906 | } |
| 907 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 908 | dotg->dwc = dwc; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 909 | dotg->otg.phy->otg = &dotg->otg; |
| 910 | dotg->otg.phy->dev = dwc->dev; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 911 | dotg->otg.phy->set_power = dwc3_otg_set_power; |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 912 | dotg->otg.phy->set_suspend = dwc3_otg_set_suspend; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 913 | |
| 914 | ret = usb_set_transceiver(dotg->otg.phy); |
| 915 | if (ret) { |
| 916 | dev_err(dotg->otg.phy->dev, |
| 917 | "%s: failed to set transceiver, already exists\n", |
| 918 | __func__); |
| 919 | goto err2; |
| 920 | } |
| 921 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 922 | dotg->otg.phy->state = OTG_STATE_UNDEFINED; |
| 923 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 924 | init_completion(&dotg->dwc3_xcvr_vbus_init); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 925 | INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work); |
| 926 | |
| 927 | ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED, |
| 928 | "dwc3_otg", dotg); |
| 929 | if (ret) { |
| 930 | dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n", |
| 931 | dotg->irq, ret); |
| 932 | goto err3; |
| 933 | } |
| 934 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 935 | pm_runtime_get(dwc->dev); |
| 936 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 937 | return 0; |
| 938 | |
| 939 | err3: |
| 940 | cancel_work_sync(&dotg->sm_work); |
| 941 | usb_set_transceiver(NULL); |
| 942 | err2: |
| 943 | kfree(dotg->otg.phy); |
| 944 | err1: |
| 945 | dwc->dotg = NULL; |
| 946 | kfree(dotg); |
| 947 | |
| 948 | return ret; |
| 949 | } |
| 950 | |
| 951 | /** |
| 952 | * dwc3_otg_exit |
| 953 | * @dwc: Pointer to out controller context structure |
| 954 | * |
| 955 | * Returns 0 on success otherwise negative errno. |
| 956 | */ |
| 957 | void dwc3_otg_exit(struct dwc3 *dwc) |
| 958 | { |
| 959 | struct dwc3_otg *dotg = dwc->dotg; |
| 960 | |
| 961 | /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */ |
| 962 | if (dotg) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 963 | if (dotg->charger) |
| 964 | dotg->charger->start_detection(dotg->charger, false); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 965 | cancel_work_sync(&dotg->sm_work); |
| 966 | usb_set_transceiver(NULL); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 967 | pm_runtime_put(dwc->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 968 | free_irq(dotg->irq, dotg); |
| 969 | kfree(dotg->otg.phy); |
| 970 | kfree(dotg); |
| 971 | dwc->dotg = NULL; |
| 972 | } |
| 973 | } |