blob: daf9a269cd2a00892a84e23858542b3761dcf01c [file] [log] [blame]
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001/**
2 * dwc3_otg.c - DesignWare USB3 DRD Controller OTG
3 *
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +05304 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02005 *
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 Gautamf1fceddf2012-10-12 14:02:50 +053019#include <linux/regulator/consumer.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020020
21#include "core.h"
22#include "dwc3_otg.h"
23#include "io.h"
24#include "xhci.h"
25
Manu Gautamf1fceddf2012-10-12 14:02:50 +053026static void dwc3_otg_reset(struct dwc3_otg *dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020027
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +053028static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode);
29static void dwc3_otg_reset(struct dwc3_otg *dotg);
30
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020031/**
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 Gautamf1fceddf2012-10-12 14:02:50 +053037 * @w: Pointer to the dwc3 otg struct
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020038 */
39static void dwc3_otg_set_host_regs(struct dwc3_otg *dotg)
40{
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053041 u32 reg;
42 struct dwc3 *dwc = dotg->dwc;
43 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020044
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053045 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 Gautamf1fceddf2012-10-12 14:02:50 +053056}
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020057
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +053058static 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 Gautamf1fceddf2012-10-12 14:02:50 +053077/**
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 */
86void 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 Shayevitzcdeef4c2012-05-29 13:17:41 +020095}
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 */
105static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg)
106{
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530107 u32 reg;
108 struct dwc3 *dwc = dotg->dwc;
109 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200110
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530111 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200126}
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 */
136static 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 Vennapusab7434562012-12-12 16:48:49 +0530139 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Manu Gautam61721592012-11-06 18:09:39 +0530140 struct dwc3 *dwc = dotg->dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200141 int ret = 0;
142
Manu Gautam61721592012-11-06 18:09:39 +0530143 if (!dwc->xhci)
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200144 return -EINVAL;
145
Manu Gautam61721592012-11-06 18:09:39 +0530146 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200157 if (on) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530158 dev_dbg(otg->phy->dev, "%s: turn on host\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200159
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 Gautamf1fceddf2012-10-12 14:02:50 +0530170 dwc3_otg_set_host_regs(dotg);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530171 /*
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 Gautam61721592012-11-06 18:09:39 +0530179 ret = platform_device_add(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200180 if (ret) {
181 dev_err(otg->phy->dev,
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530182 "%s: failed to add XHCI pdev ret=%d\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200183 __func__, ret);
184 return ret;
185 }
186
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530187 dwc3_otg_notify_host_mode(otg, on);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530188 ret = regulator_enable(dotg->vbus_otg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200189 if (ret) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530190 dev_err(otg->phy->dev, "unable to enable vbus_otg\n");
Manu Gautam61721592012-11-06 18:09:39 +0530191 platform_device_del(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200192 return ret;
193 }
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530194
195 /* re-init OTG EVTEN register as XHCI reset clears it */
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530196 if (ext_xceiv && !ext_xceiv->otg_capability)
197 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200198 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530199 dev_dbg(otg->phy->dev, "%s: turn off host\n", __func__);
200
Manu Gautam61721592012-11-06 18:09:39 +0530201 platform_device_del(dwc->xhci);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530202
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 Vennapusa42eeea32012-10-22 17:56:11 +0530208 dwc3_otg_notify_host_mode(otg, on);
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530209
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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200223 }
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 */
236static 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 Gautamf1fceddf2012-10-12 14:02:50 +0530241 dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200242 __func__, host->bus_name);
243 otg->host = host;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200244 /*
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530245 * 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200248 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530249 msleep(300);
250 dwc3_otg_set_host_power(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200251 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530252 otg->host = NULL;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200253 }
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 */
266static 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 Gautama302f612012-12-18 17:33:06 +0530269 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200270
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 Gautama302f612012-12-18 17:33:06 +0530277
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530278 /* Core reset is not required during start peripheral. Only
279 * DBM reset is required, hence perform only DBM reset here */
Manu Gautama302f612012-12-18 17:33:06 +0530280 if (ext_xceiv && ext_xceiv->otg_capability &&
281 ext_xceiv->ext_block_reset)
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530282 ext_xceiv->ext_block_reset(false);
Manu Gautama302f612012-12-18 17:33:06 +0530283
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200284 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 */
303static 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 Gautamf1fceddf2012-10-12 14:02:50 +0530312 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200313 } 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 Gautam8c642812012-06-07 10:35:10 +0530328 * 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 */
334static 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 */
353int 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 Gautamb5067272012-07-02 09:53:41 +0530364/**
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 */
371static void dwc3_ext_event_notify(struct usb_otg *otg,
372 enum dwc3_ext_events event)
373{
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530374 static bool init;
Manu Gautamb5067272012-07-02 09:53:41 +0530375 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 Vennapusa42eeea32012-10-22 17:56:11 +0530378 int ret = 0;
Manu Gautamb5067272012-07-02 09:53:41 +0530379
Manu Gautamf71d9cb2013-02-07 13:52:12 +0530380 /* Flush processing any pending events before handling new ones */
381 if (init)
382 flush_work(&dotg->sm_work);
383
Manu Gautamb5067272012-07-02 09:53:41 +0530384 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 Vennapusa42eeea32012-10-22 17:56:11 +0530390 ret = pm_runtime_get(phy->dev);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530391 if ((phy->state == OTG_STATE_A_HOST) &&
392 dotg->host_bus_suspend)
393 dotg->host_bus_suspend = 0;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530394 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 Gautamb5067272012-07-02 09:53:41 +0530403 }
Manu Gautam377821c2012-09-28 16:53:24 +0530404 } else if (event == DWC3_EVENT_XCEIV_STATE) {
Manu Gautamf71d9cb2013-02-07 13:52:12 +0530405 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 Pham0fc12332012-11-19 13:14:22 -0800411 if (ext_xceiv->id == DWC3_ID_FLOAT) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530412 dev_dbg(phy->dev, "XCVR: ID set\n");
413 set_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800414 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530415 dev_dbg(phy->dev, "XCVR: ID clear\n");
416 clear_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800417 }
Manu Gautam377821c2012-09-28 16:53:24 +0530418
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530419 if (ext_xceiv->bsv) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530420 dev_dbg(phy->dev, "XCVR: BSV set\n");
421 set_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530422 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530423 dev_dbg(phy->dev, "XCVR: BSV clear\n");
424 clear_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530425 }
Manu Gautam377821c2012-09-28 16:53:24 +0530426
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530427 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 Gautama4c3c1f2012-12-18 13:56:43 +0530433
434 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530435 }
Manu Gautamb5067272012-07-02 09:53:41 +0530436}
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 */
445int 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 Vennapusa42eeea32012-10-22 17:56:11 +0530456static 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
471static 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 Gautam6c0ff032012-11-02 14:55:35 +0530477 if (!dotg->psy || !dotg->charger) {
478 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530479 return 0;
480 }
481
Manu Gautam6c0ff032012-11-02 14:55:35 +0530482 if (dotg->charger->charging_disabled)
483 return 0;
484
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530485 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 Gautama1e331d2013-02-07 14:55:05 +0530489 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER ||
490 dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER)
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530491 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 Vennapusa2e0b4182013-03-21 12:49:43 +0530497 if ((dotg->charger->chg_type == DWC3_CDP_CHARGER) && mA > 2)
498 mA = DWC3_IDEV_CHG_MAX;
499
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530500 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
524psy_error:
525 dev_dbg(phy->dev, "power supply error when setting property\n");
526 return -ENXIO;
527}
528
Manu Gautam8c642812012-06-07 10:35:10 +0530529/* IRQs which OTG driver is interested in handling */
530#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
531 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
532
533/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200534 * 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 */
539static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
540{
541 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530542 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200543 int ret = IRQ_NONE;
544 int handled_irqs = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530545 struct usb_phy *phy = dotg->otg.phy;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200546
547 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
548
Manu Gautam8c642812012-06-07 10:35:10 +0530549 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200556 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530557 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200558 * function, switch from A to B or from B to A.
559 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200560
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530561 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 Gautam8c642812012-06-07 10:35:10 +0530571
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530572 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 Gautam8c642812012-06-07 10:35:10 +0530582
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200583 schedule_work(&dotg->sm_work);
584
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200585 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530586
587 /* Clear the interrupts we handled */
588 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200589 }
590
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200591 return ret;
592}
593
594/**
Manu Gautam8c642812012-06-07 10:35:10 +0530595 * dwc3_otg_init_sm - initialize OTG statemachine input
596 * @dotg: Pointer to the dwc3_otg structure
597 *
598 */
599void 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 Vennapusa42eeea32012-10-22 17:56:11 +0530603 struct dwc3_ext_xceiv *ext_xceiv;
604 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530605
606 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
607
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530608 /*
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 Gautam8c642812012-06-07 10:35:10 +0530615
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530616 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 Gautam8c642812012-06-07 10:35:10 +0530629}
630
631/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200632 * 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 */
639static 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 Gautam8c642812012-06-07 10:35:10 +0530643 struct dwc3_charger *charger = dotg->charger;
644 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200645
Manu Gautamb5067272012-07-02 09:53:41 +0530646 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200647 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 Gautam8c642812012-06-07 10:35:10 +0530652 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530653 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 Gautam8c642812012-06-07 10:35:10 +0530661 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530662 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530663 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200668 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530669 work = 1;
670 } else {
671 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530672 dev_dbg(phy->dev, "No device, trying to suspend\n");
673 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200674 }
675 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530676
677 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530678 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530679 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 Gautama1e331d2013-02-07 14:55:05 +0530696 case DWC3_PROPRIETARY_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530697 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530698 dwc3_otg_set_power(phy,
699 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530700 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530701 break;
702 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530703 dwc3_otg_set_power(phy,
704 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530705 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 Gautamb5067272012-07-02 09:53:41 +0530729 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530730 "unable to start B-device\n");
731 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530732 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530733 return;
734 }
735 }
736 } else {
Manu Gautam98013c22012-11-20 17:42:42 +0530737 if (charger)
738 charger->start_detection(dotg->charger, false);
739
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530740 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530741 dev_dbg(phy->dev, "No device, trying to suspend\n");
742 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530743 }
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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200758 case OTG_STATE_A_IDLE:
759 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530760 if (test_bit(ID, &dotg->inputs)) {
761 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200762 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530763 work = 1;
764 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530765 phy->state = OTG_STATE_A_HOST;
766 if (dwc3_otg_start_host(&dotg->otg, 1)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530767 /*
768 * Probably set_host was not called yet.
769 * We will re-try as soon as it will be called
770 */
Manu Gautamb5067272012-07-02 09:53:41 +0530771 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530772 "unable to start A-device\n");
773 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530774 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530775 return;
776 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200777 }
778 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530779
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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200789 default:
790 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
791
792 }
Manu Gautam8c642812012-06-07 10:35:10 +0530793
794 if (work)
795 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200796}
797
798
799/**
800 * dwc3_otg_reset - reset dwc3 otg registers.
801 *
802 * @w: Pointer to the dwc3 otg workqueue
803 */
804static void dwc3_otg_reset(struct dwc3_otg *dotg)
805{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530806 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530807 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
808
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200809 /*
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 Gautamf1fceddf2012-10-12 14:02:50 +0530825 if (!once) {
826 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
827 once++;
828 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200829
830 /* Clear all otg events (interrupts) indications */
831 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
832
Manu Gautam8c642812012-06-07 10:35:10 +0530833 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530834 if (ext_xceiv && !ext_xceiv->otg_capability)
835 dwc3_writel(dotg->regs, DWC3_OEVTEN,
836 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
837 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200838}
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 */
846int 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 Gautam17206c22012-06-21 10:17:53 +0530876 /* 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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200879 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530880 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200881 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 Gautamf1fceddf2012-10-12 14:02:50 +0530900 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200901 dotg->otg.phy->otg = &dotg->otg;
902 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530903 dotg->otg.phy->set_power = dwc3_otg_set_power;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530904 dotg->otg.phy->set_suspend = dwc3_otg_set_suspend;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200905
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 Shayevitzcdeef4c2012-05-29 13:17:41 +0200914 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
915
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530916 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200917 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 Gautamb5067272012-07-02 09:53:41 +0530927 pm_runtime_get(dwc->dev);
928
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200929 return 0;
930
931err3:
932 cancel_work_sync(&dotg->sm_work);
933 usb_set_transceiver(NULL);
934err2:
935 kfree(dotg->otg.phy);
936err1:
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 */
949void 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 Gautam8c642812012-06-07 10:35:10 +0530955 if (dotg->charger)
956 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200957 cancel_work_sync(&dotg->sm_work);
958 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530959 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200960 free_irq(dotg->irq, dotg);
961 kfree(dotg->otg.phy);
962 kfree(dotg);
963 dwc->dotg = NULL;
964 }
965}