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 | |
| 219 | /* re-init core and OTG registers as block reset clears these */ |
| 220 | dwc3_post_host_reset_core_init(dwc); |
| 221 | if (ext_xceiv && !ext_xceiv->otg_capability) |
| 222 | dwc3_otg_reset(dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * dwc3_otg_set_host - bind/unbind the host controller driver. |
| 230 | * |
| 231 | * @otg: Pointer to the otg_transceiver structure. |
| 232 | * @host: Pointer to the usb_bus structure. |
| 233 | * |
| 234 | * Returns 0 on success otherwise negative errno. |
| 235 | */ |
| 236 | static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host) |
| 237 | { |
| 238 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 239 | |
| 240 | if (host) { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 241 | dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n", |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 242 | __func__, host->bus_name); |
| 243 | otg->host = host; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 244 | /* |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 245 | * Though XHCI power would be set by now, but some delay is |
| 246 | * required for XHCI controller before setting OTG Port Power |
| 247 | * TODO: Tune this delay |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 248 | */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 249 | msleep(300); |
| 250 | dwc3_otg_set_host_power(dotg); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 251 | } else { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 252 | otg->host = NULL; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * dwc3_otg_start_peripheral - bind/unbind the peripheral controller. |
| 260 | * |
| 261 | * @otg: Pointer to the otg_transceiver structure. |
| 262 | * @gadget: pointer to the usb_gadget structure. |
| 263 | * |
| 264 | * Returns 0 on success otherwise negative errno. |
| 265 | */ |
| 266 | static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on) |
| 267 | { |
| 268 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 269 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 270 | |
| 271 | if (!otg->gadget) |
| 272 | return -EINVAL; |
| 273 | |
| 274 | if (on) { |
| 275 | dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n", |
| 276 | __func__, otg->gadget->name); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 277 | |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 278 | /* Core reset is not required during start peripheral. Only |
| 279 | * DBM reset is required, hence perform only DBM reset here */ |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 280 | if (ext_xceiv && ext_xceiv->otg_capability && |
| 281 | ext_xceiv->ext_block_reset) |
Vijayavardhan Vennapusa | f7c01a4 | 2013-03-15 15:29:11 +0530 | [diff] [blame] | 282 | ext_xceiv->ext_block_reset(false); |
Manu Gautam | a302f61 | 2012-12-18 17:33:06 +0530 | [diff] [blame] | 283 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 284 | dwc3_otg_set_peripheral_regs(dotg); |
| 285 | usb_gadget_vbus_connect(otg->gadget); |
| 286 | } else { |
| 287 | dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n", |
| 288 | __func__, otg->gadget->name); |
| 289 | usb_gadget_vbus_disconnect(otg->gadget); |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver. |
| 297 | * |
| 298 | * @otg: Pointer to the otg_transceiver structure. |
| 299 | * @gadget: pointer to the usb_gadget structure. |
| 300 | * |
| 301 | * Returns 0 on success otherwise negative errno. |
| 302 | */ |
| 303 | static int dwc3_otg_set_peripheral(struct usb_otg *otg, |
| 304 | struct usb_gadget *gadget) |
| 305 | { |
| 306 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 307 | |
| 308 | if (gadget) { |
| 309 | dev_dbg(otg->phy->dev, "%s: set gadget %s\n", |
| 310 | __func__, gadget->name); |
| 311 | otg->gadget = gadget; |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 312 | schedule_work(&dotg->sm_work); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 313 | } else { |
| 314 | if (otg->phy->state == OTG_STATE_B_PERIPHERAL) { |
| 315 | dwc3_otg_start_peripheral(otg, 0); |
| 316 | otg->gadget = NULL; |
| 317 | otg->phy->state = OTG_STATE_UNDEFINED; |
| 318 | schedule_work(&dotg->sm_work); |
| 319 | } else { |
| 320 | otg->gadget = NULL; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
| 327 | /** |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 328 | * dwc3_ext_chg_det_done - callback to handle charger detection completion |
| 329 | * @otg: Pointer to the otg transceiver structure |
| 330 | * @charger: Pointer to the external charger structure |
| 331 | * |
| 332 | * Returns 0 on success |
| 333 | */ |
| 334 | static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg) |
| 335 | { |
| 336 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 337 | |
| 338 | /* |
| 339 | * Ignore chg_detection notification if BSV has gone off by this time. |
| 340 | * STOP chg_det as part of !BSV handling would reset the chg_det flags |
| 341 | */ |
| 342 | if (test_bit(B_SESS_VLD, &dotg->inputs)) |
| 343 | schedule_work(&dotg->sm_work); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * dwc3_set_charger - bind/unbind external charger driver |
| 348 | * @otg: Pointer to the otg transceiver structure |
| 349 | * @charger: Pointer to the external charger structure |
| 350 | * |
| 351 | * Returns 0 on success |
| 352 | */ |
| 353 | int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger) |
| 354 | { |
| 355 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 356 | |
| 357 | dotg->charger = charger; |
| 358 | if (charger) |
| 359 | charger->notify_detection_complete = dwc3_ext_chg_det_done; |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 364 | /** |
| 365 | * dwc3_ext_event_notify - callback to handle events from external transceiver |
| 366 | * @otg: Pointer to the otg transceiver structure |
| 367 | * @event: Event reported by transceiver |
| 368 | * |
| 369 | * Returns 0 on success |
| 370 | */ |
| 371 | static void dwc3_ext_event_notify(struct usb_otg *otg, |
| 372 | enum dwc3_ext_events event) |
| 373 | { |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 374 | static bool init; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 375 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 376 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
| 377 | struct usb_phy *phy = dotg->otg.phy; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 378 | int ret = 0; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 379 | |
Manu Gautam | f71d9cb | 2013-02-07 13:52:12 +0530 | [diff] [blame] | 380 | /* Flush processing any pending events before handling new ones */ |
| 381 | if (init) |
| 382 | flush_work(&dotg->sm_work); |
| 383 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 384 | if (event == DWC3_EVENT_PHY_RESUME) { |
| 385 | if (!pm_runtime_status_suspended(phy->dev)) { |
| 386 | dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n"); |
| 387 | } else { |
| 388 | dev_dbg(phy->dev, "ext PHY_RESUME event received\n"); |
| 389 | /* ext_xceiver would have taken h/w out of LPM by now */ |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 390 | ret = pm_runtime_get(phy->dev); |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 391 | if ((phy->state == OTG_STATE_A_HOST) && |
| 392 | dotg->host_bus_suspend) |
| 393 | dotg->host_bus_suspend = 0; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 394 | if (ret == -EACCES) { |
| 395 | /* pm_runtime_get may fail during system |
| 396 | resume with -EACCES error */ |
| 397 | pm_runtime_disable(phy->dev); |
| 398 | pm_runtime_set_active(phy->dev); |
| 399 | pm_runtime_enable(phy->dev); |
| 400 | } else if (ret < 0) { |
| 401 | dev_warn(phy->dev, "pm_runtime_get failed!\n"); |
| 402 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 403 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 404 | } else if (event == DWC3_EVENT_XCEIV_STATE) { |
Manu Gautam | f71d9cb | 2013-02-07 13:52:12 +0530 | [diff] [blame] | 405 | if (pm_runtime_status_suspended(phy->dev)) { |
| 406 | dev_warn(phy->dev, "PHY_STATE event in LPM!!!!\n"); |
| 407 | ret = pm_runtime_get(phy->dev); |
| 408 | if (ret < 0) |
| 409 | dev_warn(phy->dev, "pm_runtime_get failed!!\n"); |
| 410 | } |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 411 | if (ext_xceiv->id == DWC3_ID_FLOAT) { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 412 | dev_dbg(phy->dev, "XCVR: ID set\n"); |
| 413 | set_bit(ID, &dotg->inputs); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 414 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 415 | dev_dbg(phy->dev, "XCVR: ID clear\n"); |
| 416 | clear_bit(ID, &dotg->inputs); |
Jack Pham | 0fc1233 | 2012-11-19 13:14:22 -0800 | [diff] [blame] | 417 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 418 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 419 | if (ext_xceiv->bsv) { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 420 | dev_dbg(phy->dev, "XCVR: BSV set\n"); |
| 421 | set_bit(B_SESS_VLD, &dotg->inputs); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 422 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 423 | dev_dbg(phy->dev, "XCVR: BSV clear\n"); |
| 424 | clear_bit(B_SESS_VLD, &dotg->inputs); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 425 | } |
Manu Gautam | 377821c | 2012-09-28 16:53:24 +0530 | [diff] [blame] | 426 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 427 | if (!init) { |
| 428 | init = true; |
| 429 | complete(&dotg->dwc3_xcvr_vbus_init); |
| 430 | dev_dbg(phy->dev, "XCVR: BSV init complete\n"); |
| 431 | return; |
| 432 | } |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 433 | |
| 434 | schedule_work(&dotg->sm_work); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 435 | } |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * dwc3_set_ext_xceiv - bind/unbind external transceiver driver |
| 440 | * @otg: Pointer to the otg transceiver structure |
| 441 | * @ext_xceiv: Pointer to the external transceiver struccture |
| 442 | * |
| 443 | * Returns 0 on success |
| 444 | */ |
| 445 | int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv) |
| 446 | { |
| 447 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 448 | |
| 449 | dotg->ext_xceiv = ext_xceiv; |
| 450 | if (ext_xceiv) |
| 451 | ext_xceiv->notify_ext_events = dwc3_ext_event_notify; |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 456 | static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode) |
| 457 | { |
| 458 | struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg); |
| 459 | |
| 460 | if (!dotg->psy) { |
| 461 | dev_err(otg->phy->dev, "no usb power supply registered\n"); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | if (host_mode) |
| 466 | power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM); |
| 467 | else |
| 468 | power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE); |
| 469 | } |
| 470 | |
| 471 | static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA) |
| 472 | { |
| 473 | static int power_supply_type; |
| 474 | struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg); |
| 475 | |
| 476 | |
Manu Gautam | 6c0ff03 | 2012-11-02 14:55:35 +0530 | [diff] [blame] | 477 | if (!dotg->psy || !dotg->charger) { |
| 478 | dev_err(phy->dev, "no usb power supply/charger registered\n"); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 479 | return 0; |
| 480 | } |
| 481 | |
Manu Gautam | 6c0ff03 | 2012-11-02 14:55:35 +0530 | [diff] [blame] | 482 | if (dotg->charger->charging_disabled) |
| 483 | return 0; |
| 484 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 485 | if (dotg->charger->chg_type == DWC3_SDP_CHARGER) |
| 486 | power_supply_type = POWER_SUPPLY_TYPE_USB; |
| 487 | else if (dotg->charger->chg_type == DWC3_CDP_CHARGER) |
| 488 | power_supply_type = POWER_SUPPLY_TYPE_USB_CDP; |
Manu Gautam | a1e331d | 2013-02-07 14:55:05 +0530 | [diff] [blame] | 489 | else if (dotg->charger->chg_type == DWC3_DCP_CHARGER || |
| 490 | dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER) |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 491 | power_supply_type = POWER_SUPPLY_TYPE_USB_DCP; |
| 492 | else |
| 493 | power_supply_type = POWER_SUPPLY_TYPE_BATTERY; |
| 494 | |
| 495 | power_supply_set_supply_type(dotg->psy, power_supply_type); |
| 496 | |
Vijayavardhan Vennapusa | 2e0b418 | 2013-03-21 12:49:43 +0530 | [diff] [blame] | 497 | if ((dotg->charger->chg_type == DWC3_CDP_CHARGER) && mA > 2) |
| 498 | mA = DWC3_IDEV_CHG_MAX; |
| 499 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 500 | if (dotg->charger->max_power == mA) |
| 501 | return 0; |
| 502 | |
| 503 | dev_info(phy->dev, "Avail curr from USB = %u\n", mA); |
| 504 | |
| 505 | if (dotg->charger->max_power <= 2 && mA > 2) { |
| 506 | /* Enable charging */ |
| 507 | if (power_supply_set_online(dotg->psy, true)) |
| 508 | goto psy_error; |
| 509 | if (power_supply_set_current_limit(dotg->psy, 1000*mA)) |
| 510 | goto psy_error; |
| 511 | } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) { |
| 512 | /* Disable charging */ |
| 513 | if (power_supply_set_online(dotg->psy, false)) |
| 514 | goto psy_error; |
| 515 | /* Set max current limit */ |
| 516 | if (power_supply_set_current_limit(dotg->psy, 0)) |
| 517 | goto psy_error; |
| 518 | } |
| 519 | |
| 520 | power_supply_changed(dotg->psy); |
| 521 | dotg->charger->max_power = mA; |
| 522 | return 0; |
| 523 | |
| 524 | psy_error: |
| 525 | dev_dbg(phy->dev, "power supply error when setting property\n"); |
| 526 | return -ENXIO; |
| 527 | } |
| 528 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 529 | /* IRQs which OTG driver is interested in handling */ |
| 530 | #define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \ |
| 531 | DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) |
| 532 | |
| 533 | /** |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 534 | * dwc3_otg_interrupt - interrupt handler for dwc3 otg events. |
| 535 | * @_dotg: Pointer to out controller context structure |
| 536 | * |
| 537 | * Returns IRQ_HANDLED on success otherwise IRQ_NONE. |
| 538 | */ |
| 539 | static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg) |
| 540 | { |
| 541 | struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 542 | u32 osts, oevt_reg; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 543 | int ret = IRQ_NONE; |
| 544 | int handled_irqs = 0; |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 545 | struct usb_phy *phy = dotg->otg.phy; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 546 | |
| 547 | oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT); |
| 548 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 549 | if (!(oevt_reg & DWC3_OEVT_MASK)) |
| 550 | return IRQ_NONE; |
| 551 | |
| 552 | osts = dwc3_readl(dotg->regs, DWC3_OSTS); |
| 553 | |
| 554 | if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) || |
| 555 | (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) { |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 556 | /* |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 557 | * ID sts has changed, set inputs later, in the workqueue |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 558 | * function, switch from A to B or from B to A. |
| 559 | */ |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 560 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 561 | if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) { |
| 562 | if (osts & DWC3_OTG_OSTS_CONIDSTS) { |
| 563 | dev_dbg(phy->dev, "ID set\n"); |
| 564 | set_bit(ID, &dotg->inputs); |
| 565 | } else { |
| 566 | dev_dbg(phy->dev, "ID clear\n"); |
| 567 | clear_bit(ID, &dotg->inputs); |
| 568 | } |
| 569 | handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT; |
| 570 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 571 | |
Vijayavardhan Vennapusa | b743456 | 2012-12-12 16:48:49 +0530 | [diff] [blame] | 572 | if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) { |
| 573 | if (osts & DWC3_OTG_OSTS_BSESVALID) { |
| 574 | dev_dbg(phy->dev, "BSV set\n"); |
| 575 | set_bit(B_SESS_VLD, &dotg->inputs); |
| 576 | } else { |
| 577 | dev_dbg(phy->dev, "BSV clear\n"); |
| 578 | clear_bit(B_SESS_VLD, &dotg->inputs); |
| 579 | } |
| 580 | handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT; |
| 581 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 582 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 583 | schedule_work(&dotg->sm_work); |
| 584 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 585 | ret = IRQ_HANDLED; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 586 | |
| 587 | /* Clear the interrupts we handled */ |
| 588 | dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 591 | return ret; |
| 592 | } |
| 593 | |
| 594 | /** |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 595 | * dwc3_otg_init_sm - initialize OTG statemachine input |
| 596 | * @dotg: Pointer to the dwc3_otg structure |
| 597 | * |
| 598 | */ |
| 599 | void dwc3_otg_init_sm(struct dwc3_otg *dotg) |
| 600 | { |
| 601 | u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS); |
| 602 | struct usb_phy *phy = dotg->otg.phy; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 603 | struct dwc3_ext_xceiv *ext_xceiv; |
| 604 | int ret; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 605 | |
| 606 | dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts); |
| 607 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 608 | /* |
| 609 | * VBUS initial state is reported after PMIC |
| 610 | * driver initialization. Wait for it. |
| 611 | */ |
| 612 | ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5); |
| 613 | if (!ret) |
| 614 | dev_err(phy->dev, "%s: completion timeout\n", __func__); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 615 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 616 | ext_xceiv = dotg->ext_xceiv; |
| 617 | dwc3_otg_reset(dotg); |
| 618 | if (ext_xceiv && !ext_xceiv->otg_capability) { |
| 619 | if (osts & DWC3_OTG_OSTS_CONIDSTS) |
| 620 | set_bit(ID, &dotg->inputs); |
| 621 | else |
| 622 | clear_bit(ID, &dotg->inputs); |
| 623 | |
| 624 | if (osts & DWC3_OTG_OSTS_BSESVALID) |
| 625 | set_bit(B_SESS_VLD, &dotg->inputs); |
| 626 | else |
| 627 | clear_bit(B_SESS_VLD, &dotg->inputs); |
| 628 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | /** |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 632 | * dwc3_otg_sm_work - workqueue function. |
| 633 | * |
| 634 | * @w: Pointer to the dwc3 otg workqueue |
| 635 | * |
| 636 | * NOTE: After any change in phy->state, |
| 637 | * we must reschdule the state machine. |
| 638 | */ |
| 639 | static void dwc3_otg_sm_work(struct work_struct *w) |
| 640 | { |
| 641 | struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work); |
| 642 | struct usb_phy *phy = dotg->otg.phy; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 643 | struct dwc3_charger *charger = dotg->charger; |
| 644 | bool work = 0; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 645 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 646 | pm_runtime_resume(phy->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 647 | dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state)); |
| 648 | |
| 649 | /* Check OTG state */ |
| 650 | switch (phy->state) { |
| 651 | case OTG_STATE_UNDEFINED: |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 652 | dwc3_otg_init_sm(dotg); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 653 | if (!dotg->psy) { |
| 654 | dotg->psy = power_supply_get_by_name("usb"); |
| 655 | |
| 656 | if (!dotg->psy) |
| 657 | dev_err(phy->dev, |
| 658 | "couldn't get usb power supply\n"); |
| 659 | } |
| 660 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 661 | /* Switch to A or B-Device according to ID / BSV */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 662 | if (!test_bit(ID, &dotg->inputs)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 663 | dev_dbg(phy->dev, "!id\n"); |
| 664 | phy->state = OTG_STATE_A_IDLE; |
| 665 | work = 1; |
| 666 | } else if (test_bit(B_SESS_VLD, &dotg->inputs)) { |
| 667 | dev_dbg(phy->dev, "b_sess_vld\n"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 668 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 669 | work = 1; |
| 670 | } else { |
| 671 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 672 | dev_dbg(phy->dev, "No device, trying to suspend\n"); |
| 673 | pm_runtime_put_sync(phy->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 674 | } |
| 675 | break; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 676 | |
| 677 | case OTG_STATE_B_IDLE: |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 678 | if (!test_bit(ID, &dotg->inputs)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 679 | dev_dbg(phy->dev, "!id\n"); |
| 680 | phy->state = OTG_STATE_A_IDLE; |
| 681 | work = 1; |
| 682 | if (charger) { |
| 683 | if (charger->chg_type == DWC3_INVALID_CHARGER) |
| 684 | charger->start_detection(dotg->charger, |
| 685 | false); |
| 686 | else |
| 687 | charger->chg_type = |
| 688 | DWC3_INVALID_CHARGER; |
| 689 | } |
| 690 | } else if (test_bit(B_SESS_VLD, &dotg->inputs)) { |
| 691 | dev_dbg(phy->dev, "b_sess_vld\n"); |
| 692 | if (charger) { |
| 693 | /* Has charger been detected? If no detect it */ |
| 694 | switch (charger->chg_type) { |
| 695 | case DWC3_DCP_CHARGER: |
Manu Gautam | a1e331d | 2013-02-07 14:55:05 +0530 | [diff] [blame] | 696 | case DWC3_PROPRIETARY_CHARGER: |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 697 | dev_dbg(phy->dev, "lpm, DCP charger\n"); |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 698 | dwc3_otg_set_power(phy, |
| 699 | DWC3_IDEV_CHG_MAX); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 700 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 701 | break; |
| 702 | case DWC3_CDP_CHARGER: |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 703 | dwc3_otg_set_power(phy, |
| 704 | DWC3_IDEV_CHG_MAX); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 705 | dwc3_otg_start_peripheral(&dotg->otg, |
| 706 | 1); |
| 707 | phy->state = OTG_STATE_B_PERIPHERAL; |
| 708 | work = 1; |
| 709 | break; |
| 710 | case DWC3_SDP_CHARGER: |
| 711 | dwc3_otg_start_peripheral(&dotg->otg, |
| 712 | 1); |
| 713 | phy->state = OTG_STATE_B_PERIPHERAL; |
| 714 | work = 1; |
| 715 | break; |
| 716 | default: |
| 717 | dev_dbg(phy->dev, "chg_det started\n"); |
| 718 | charger->start_detection(charger, true); |
| 719 | break; |
| 720 | } |
| 721 | } else { |
| 722 | /* no charger registered, start peripheral */ |
| 723 | if (dwc3_otg_start_peripheral(&dotg->otg, 1)) { |
| 724 | /* |
| 725 | * Probably set_peripheral not called |
| 726 | * yet. We will re-try as soon as it |
| 727 | * will be called |
| 728 | */ |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 729 | dev_err(phy->dev, "enter lpm as\n" |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 730 | "unable to start B-device\n"); |
| 731 | phy->state = OTG_STATE_UNDEFINED; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 732 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 733 | return; |
| 734 | } |
| 735 | } |
| 736 | } else { |
Manu Gautam | 98013c2 | 2012-11-20 17:42:42 +0530 | [diff] [blame] | 737 | if (charger) |
| 738 | charger->start_detection(dotg->charger, false); |
| 739 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 740 | dwc3_otg_set_power(phy, 0); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 741 | dev_dbg(phy->dev, "No device, trying to suspend\n"); |
| 742 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 743 | } |
| 744 | break; |
| 745 | |
| 746 | case OTG_STATE_B_PERIPHERAL: |
| 747 | if (!test_bit(B_SESS_VLD, &dotg->inputs) || |
| 748 | !test_bit(ID, &dotg->inputs)) { |
| 749 | dev_dbg(phy->dev, "!id || !bsv\n"); |
| 750 | dwc3_otg_start_peripheral(&dotg->otg, 0); |
| 751 | phy->state = OTG_STATE_B_IDLE; |
| 752 | if (charger) |
| 753 | charger->chg_type = DWC3_INVALID_CHARGER; |
| 754 | work = 1; |
| 755 | } |
| 756 | break; |
| 757 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 758 | case OTG_STATE_A_IDLE: |
| 759 | /* Switch to A-Device*/ |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 760 | if (test_bit(ID, &dotg->inputs)) { |
| 761 | dev_dbg(phy->dev, "id\n"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 762 | phy->state = OTG_STATE_B_IDLE; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 763 | work = 1; |
| 764 | } else { |
Manu Gautam | a4c3c1f | 2012-12-18 13:56:43 +0530 | [diff] [blame] | 765 | phy->state = OTG_STATE_A_HOST; |
| 766 | if (dwc3_otg_start_host(&dotg->otg, 1)) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 767 | /* |
| 768 | * Probably set_host was not called yet. |
| 769 | * We will re-try as soon as it will be called |
| 770 | */ |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 771 | dev_dbg(phy->dev, "enter lpm as\n" |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 772 | "unable to start A-device\n"); |
| 773 | phy->state = OTG_STATE_UNDEFINED; |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 774 | pm_runtime_put_sync(phy->dev); |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 775 | return; |
| 776 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 777 | } |
| 778 | break; |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 779 | |
| 780 | case OTG_STATE_A_HOST: |
| 781 | if (test_bit(ID, &dotg->inputs)) { |
| 782 | dev_dbg(phy->dev, "id\n"); |
| 783 | dwc3_otg_start_host(&dotg->otg, 0); |
| 784 | phy->state = OTG_STATE_B_IDLE; |
| 785 | work = 1; |
| 786 | } |
| 787 | break; |
| 788 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 789 | default: |
| 790 | dev_err(phy->dev, "%s: invalid otg-state\n", __func__); |
| 791 | |
| 792 | } |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 793 | |
| 794 | if (work) |
| 795 | schedule_work(&dotg->sm_work); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | |
| 799 | /** |
| 800 | * dwc3_otg_reset - reset dwc3 otg registers. |
| 801 | * |
| 802 | * @w: Pointer to the dwc3 otg workqueue |
| 803 | */ |
| 804 | static void dwc3_otg_reset(struct dwc3_otg *dotg) |
| 805 | { |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 806 | static int once; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 807 | struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv; |
| 808 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 809 | /* |
| 810 | * OCFG[2] - OTG-Version = 1 |
| 811 | * OCFG[1] - HNPCap = 0 |
| 812 | * OCFG[0] - SRPCap = 0 |
| 813 | */ |
| 814 | dwc3_writel(dotg->regs, DWC3_OCFG, 0x4); |
| 815 | |
| 816 | /* |
| 817 | * OCTL[6] - PeriMode = 1 |
| 818 | * OCTL[5] - PrtPwrCtl = 0 |
| 819 | * OCTL[4] - HNPReq = 0 |
| 820 | * OCTL[3] - SesReq = 0 |
| 821 | * OCTL[2] - TermSelDLPulse = 0 |
| 822 | * OCTL[1] - DevSetHNPEn = 0 |
| 823 | * OCTL[0] - HstSetHNPEn = 0 |
| 824 | */ |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 825 | if (!once) { |
| 826 | dwc3_writel(dotg->regs, DWC3_OCTL, 0x40); |
| 827 | once++; |
| 828 | } |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 829 | |
| 830 | /* Clear all otg events (interrupts) indications */ |
| 831 | dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF); |
| 832 | |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 833 | /* Enable ID/BSV StsChngEn event*/ |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 834 | if (ext_xceiv && !ext_xceiv->otg_capability) |
| 835 | dwc3_writel(dotg->regs, DWC3_OEVTEN, |
| 836 | DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | |
| 837 | DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /** |
| 841 | * dwc3_otg_init - Initializes otg related registers |
| 842 | * @dwc: Pointer to out controller context structure |
| 843 | * |
| 844 | * Returns 0 on success otherwise negative errno. |
| 845 | */ |
| 846 | int dwc3_otg_init(struct dwc3 *dwc) |
| 847 | { |
| 848 | u32 reg; |
| 849 | int ret = 0; |
| 850 | struct dwc3_otg *dotg; |
| 851 | |
| 852 | dev_dbg(dwc->dev, "dwc3_otg_init\n"); |
| 853 | |
| 854 | /* |
| 855 | * GHWPARAMS6[10] bit is SRPSupport. |
| 856 | * This bit also reflects DWC_USB3_EN_OTG |
| 857 | */ |
| 858 | reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6); |
| 859 | if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) { |
| 860 | /* |
| 861 | * No OTG support in the HW core. |
| 862 | * We return 0 to indicate no error, since this is acceptable |
| 863 | * situation, just continue probe the dwc3 driver without otg. |
| 864 | */ |
| 865 | dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n"); |
| 866 | return 0; |
| 867 | } |
| 868 | |
| 869 | /* Allocate and init otg instance */ |
| 870 | dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL); |
| 871 | if (!dotg) { |
| 872 | dev_err(dwc->dev, "unable to allocate dwc3_otg\n"); |
| 873 | return -ENOMEM; |
| 874 | } |
| 875 | |
Manu Gautam | 17206c2 | 2012-06-21 10:17:53 +0530 | [diff] [blame] | 876 | /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */ |
| 877 | dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev), |
| 878 | "otg_irq"); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 879 | if (dotg->irq < 0) { |
Manu Gautam | 17206c2 | 2012-06-21 10:17:53 +0530 | [diff] [blame] | 880 | dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 881 | ret = -ENODEV; |
| 882 | goto err1; |
| 883 | } |
| 884 | |
| 885 | dotg->regs = dwc->regs; |
| 886 | |
| 887 | dotg->otg.set_peripheral = dwc3_otg_set_peripheral; |
| 888 | dotg->otg.set_host = dwc3_otg_set_host; |
| 889 | |
| 890 | /* This reference is used by dwc3 modules for checking otg existance */ |
| 891 | dwc->dotg = dotg; |
| 892 | |
| 893 | dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL); |
| 894 | if (!dotg->otg.phy) { |
| 895 | dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n"); |
| 896 | ret = -ENOMEM; |
| 897 | goto err1; |
| 898 | } |
| 899 | |
Manu Gautam | f1fceddf | 2012-10-12 14:02:50 +0530 | [diff] [blame] | 900 | dotg->dwc = dwc; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 901 | dotg->otg.phy->otg = &dotg->otg; |
| 902 | dotg->otg.phy->dev = dwc->dev; |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 903 | dotg->otg.phy->set_power = dwc3_otg_set_power; |
Vijayavardhan Vennapusa | 4514588 | 2013-01-03 14:11:58 +0530 | [diff] [blame] | 904 | dotg->otg.phy->set_suspend = dwc3_otg_set_suspend; |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 905 | |
| 906 | ret = usb_set_transceiver(dotg->otg.phy); |
| 907 | if (ret) { |
| 908 | dev_err(dotg->otg.phy->dev, |
| 909 | "%s: failed to set transceiver, already exists\n", |
| 910 | __func__); |
| 911 | goto err2; |
| 912 | } |
| 913 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 914 | dotg->otg.phy->state = OTG_STATE_UNDEFINED; |
| 915 | |
Vijayavardhan Vennapusa | 42eeea3 | 2012-10-22 17:56:11 +0530 | [diff] [blame] | 916 | init_completion(&dotg->dwc3_xcvr_vbus_init); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 917 | INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work); |
| 918 | |
| 919 | ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED, |
| 920 | "dwc3_otg", dotg); |
| 921 | if (ret) { |
| 922 | dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n", |
| 923 | dotg->irq, ret); |
| 924 | goto err3; |
| 925 | } |
| 926 | |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 927 | pm_runtime_get(dwc->dev); |
| 928 | |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 929 | return 0; |
| 930 | |
| 931 | err3: |
| 932 | cancel_work_sync(&dotg->sm_work); |
| 933 | usb_set_transceiver(NULL); |
| 934 | err2: |
| 935 | kfree(dotg->otg.phy); |
| 936 | err1: |
| 937 | dwc->dotg = NULL; |
| 938 | kfree(dotg); |
| 939 | |
| 940 | return ret; |
| 941 | } |
| 942 | |
| 943 | /** |
| 944 | * dwc3_otg_exit |
| 945 | * @dwc: Pointer to out controller context structure |
| 946 | * |
| 947 | * Returns 0 on success otherwise negative errno. |
| 948 | */ |
| 949 | void dwc3_otg_exit(struct dwc3 *dwc) |
| 950 | { |
| 951 | struct dwc3_otg *dotg = dwc->dotg; |
| 952 | |
| 953 | /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */ |
| 954 | if (dotg) { |
Manu Gautam | 8c64281 | 2012-06-07 10:35:10 +0530 | [diff] [blame] | 955 | if (dotg->charger) |
| 956 | dotg->charger->start_detection(dotg->charger, false); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 957 | cancel_work_sync(&dotg->sm_work); |
| 958 | usb_set_transceiver(NULL); |
Manu Gautam | b506727 | 2012-07-02 09:53:41 +0530 | [diff] [blame] | 959 | pm_runtime_put(dwc->dev); |
Ido Shayevitz | cdeef4c | 2012-05-29 13:17:41 +0200 | [diff] [blame] | 960 | free_irq(dotg->irq, dotg); |
| 961 | kfree(dotg->otg.phy); |
| 962 | kfree(dotg); |
| 963 | dwc->dotg = NULL; |
| 964 | } |
| 965 | } |