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