blob: ca1f817808574e1cd4b8930571902402df60a97b [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;
Manu Gautam4ff07242013-03-27 14:31:11 +0530429 if (!work_busy(&dotg->sm_work))
430 schedule_work(&dotg->sm_work);
431
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530432 complete(&dotg->dwc3_xcvr_vbus_init);
433 dev_dbg(phy->dev, "XCVR: BSV init complete\n");
434 return;
435 }
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530436
437 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530438 }
Manu Gautamb5067272012-07-02 09:53:41 +0530439}
440
441/**
442 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
443 * @otg: Pointer to the otg transceiver structure
444 * @ext_xceiv: Pointer to the external transceiver struccture
445 *
446 * Returns 0 on success
447 */
448int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
449{
450 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
451
452 dotg->ext_xceiv = ext_xceiv;
453 if (ext_xceiv)
454 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
455
456 return 0;
457}
458
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530459static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
460{
461 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
462
463 if (!dotg->psy) {
464 dev_err(otg->phy->dev, "no usb power supply registered\n");
465 return;
466 }
467
468 if (host_mode)
469 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
470 else
471 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
472}
473
474static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
475{
476 static int power_supply_type;
477 struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
478
479
Manu Gautam6c0ff032012-11-02 14:55:35 +0530480 if (!dotg->psy || !dotg->charger) {
481 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530482 return 0;
483 }
484
Manu Gautam6c0ff032012-11-02 14:55:35 +0530485 if (dotg->charger->charging_disabled)
486 return 0;
487
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530488 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
489 power_supply_type = POWER_SUPPLY_TYPE_USB;
490 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
491 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
Manu Gautama1e331d2013-02-07 14:55:05 +0530492 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER ||
493 dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER)
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530494 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
495 else
496 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
497
498 power_supply_set_supply_type(dotg->psy, power_supply_type);
499
Vijayavardhan Vennapusa2e0b4182013-03-21 12:49:43 +0530500 if ((dotg->charger->chg_type == DWC3_CDP_CHARGER) && mA > 2)
501 mA = DWC3_IDEV_CHG_MAX;
502
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530503 if (dotg->charger->max_power == mA)
504 return 0;
505
506 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
507
508 if (dotg->charger->max_power <= 2 && mA > 2) {
509 /* Enable charging */
510 if (power_supply_set_online(dotg->psy, true))
511 goto psy_error;
512 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
513 goto psy_error;
514 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
515 /* Disable charging */
516 if (power_supply_set_online(dotg->psy, false))
517 goto psy_error;
518 /* Set max current limit */
519 if (power_supply_set_current_limit(dotg->psy, 0))
520 goto psy_error;
521 }
522
523 power_supply_changed(dotg->psy);
524 dotg->charger->max_power = mA;
525 return 0;
526
527psy_error:
528 dev_dbg(phy->dev, "power supply error when setting property\n");
529 return -ENXIO;
530}
531
Manu Gautam8c642812012-06-07 10:35:10 +0530532/* IRQs which OTG driver is interested in handling */
533#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
534 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
535
536/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200537 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
538 * @_dotg: Pointer to out controller context structure
539 *
540 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
541 */
542static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
543{
544 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530545 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200546 int ret = IRQ_NONE;
547 int handled_irqs = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530548 struct usb_phy *phy = dotg->otg.phy;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200549
550 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
551
Manu Gautam8c642812012-06-07 10:35:10 +0530552 if (!(oevt_reg & DWC3_OEVT_MASK))
553 return IRQ_NONE;
554
555 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
556
557 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
558 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200559 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530560 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200561 * function, switch from A to B or from B to A.
562 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200563
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530564 if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {
565 if (osts & DWC3_OTG_OSTS_CONIDSTS) {
566 dev_dbg(phy->dev, "ID set\n");
567 set_bit(ID, &dotg->inputs);
568 } else {
569 dev_dbg(phy->dev, "ID clear\n");
570 clear_bit(ID, &dotg->inputs);
571 }
572 handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;
573 }
Manu Gautam8c642812012-06-07 10:35:10 +0530574
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530575 if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {
576 if (osts & DWC3_OTG_OSTS_BSESVALID) {
577 dev_dbg(phy->dev, "BSV set\n");
578 set_bit(B_SESS_VLD, &dotg->inputs);
579 } else {
580 dev_dbg(phy->dev, "BSV clear\n");
581 clear_bit(B_SESS_VLD, &dotg->inputs);
582 }
583 handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;
584 }
Manu Gautam8c642812012-06-07 10:35:10 +0530585
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200586 schedule_work(&dotg->sm_work);
587
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200588 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530589
590 /* Clear the interrupts we handled */
591 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200592 }
593
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200594 return ret;
595}
596
597/**
Manu Gautam8c642812012-06-07 10:35:10 +0530598 * dwc3_otg_init_sm - initialize OTG statemachine input
599 * @dotg: Pointer to the dwc3_otg structure
600 *
601 */
602void dwc3_otg_init_sm(struct dwc3_otg *dotg)
603{
604 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
605 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530606 struct dwc3_ext_xceiv *ext_xceiv;
607 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530608
609 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
610
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530611 /*
612 * VBUS initial state is reported after PMIC
613 * driver initialization. Wait for it.
614 */
615 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
Manu Gautam4ff07242013-03-27 14:31:11 +0530616 if (!ret) {
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530617 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam4ff07242013-03-27 14:31:11 +0530618 /* We can safely assume no cable connected */
619 set_bit(ID, &dotg->inputs);
620 }
Manu Gautam8c642812012-06-07 10:35:10 +0530621
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530622 ext_xceiv = dotg->ext_xceiv;
623 dwc3_otg_reset(dotg);
624 if (ext_xceiv && !ext_xceiv->otg_capability) {
625 if (osts & DWC3_OTG_OSTS_CONIDSTS)
626 set_bit(ID, &dotg->inputs);
627 else
628 clear_bit(ID, &dotg->inputs);
629
630 if (osts & DWC3_OTG_OSTS_BSESVALID)
631 set_bit(B_SESS_VLD, &dotg->inputs);
632 else
633 clear_bit(B_SESS_VLD, &dotg->inputs);
634 }
Manu Gautam8c642812012-06-07 10:35:10 +0530635}
636
637/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200638 * dwc3_otg_sm_work - workqueue function.
639 *
640 * @w: Pointer to the dwc3 otg workqueue
641 *
642 * NOTE: After any change in phy->state,
643 * we must reschdule the state machine.
644 */
645static void dwc3_otg_sm_work(struct work_struct *w)
646{
647 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
648 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530649 struct dwc3_charger *charger = dotg->charger;
650 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200651
Manu Gautamb5067272012-07-02 09:53:41 +0530652 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200653 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
654
655 /* Check OTG state */
656 switch (phy->state) {
657 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530658 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530659 if (!dotg->psy) {
660 dotg->psy = power_supply_get_by_name("usb");
661
662 if (!dotg->psy)
663 dev_err(phy->dev,
664 "couldn't get usb power supply\n");
665 }
666
Manu Gautam8c642812012-06-07 10:35:10 +0530667 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530668 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530669 dev_dbg(phy->dev, "!id\n");
670 phy->state = OTG_STATE_A_IDLE;
671 work = 1;
672 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
673 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200674 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530675 work = 1;
676 } else {
677 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530678 dev_dbg(phy->dev, "No device, trying to suspend\n");
679 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200680 }
681 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530682
683 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530684 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530685 dev_dbg(phy->dev, "!id\n");
686 phy->state = OTG_STATE_A_IDLE;
687 work = 1;
688 if (charger) {
689 if (charger->chg_type == DWC3_INVALID_CHARGER)
690 charger->start_detection(dotg->charger,
691 false);
692 else
693 charger->chg_type =
694 DWC3_INVALID_CHARGER;
695 }
696 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
697 dev_dbg(phy->dev, "b_sess_vld\n");
698 if (charger) {
699 /* Has charger been detected? If no detect it */
700 switch (charger->chg_type) {
701 case DWC3_DCP_CHARGER:
Manu Gautama1e331d2013-02-07 14:55:05 +0530702 case DWC3_PROPRIETARY_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530703 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530704 dwc3_otg_set_power(phy,
705 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530706 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530707 break;
708 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530709 dwc3_otg_set_power(phy,
710 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530711 dwc3_otg_start_peripheral(&dotg->otg,
712 1);
713 phy->state = OTG_STATE_B_PERIPHERAL;
714 work = 1;
715 break;
716 case DWC3_SDP_CHARGER:
717 dwc3_otg_start_peripheral(&dotg->otg,
718 1);
719 phy->state = OTG_STATE_B_PERIPHERAL;
720 work = 1;
721 break;
722 default:
723 dev_dbg(phy->dev, "chg_det started\n");
724 charger->start_detection(charger, true);
725 break;
726 }
727 } else {
728 /* no charger registered, start peripheral */
729 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
730 /*
731 * Probably set_peripheral not called
732 * yet. We will re-try as soon as it
733 * will be called
734 */
Manu Gautamb5067272012-07-02 09:53:41 +0530735 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530736 "unable to start B-device\n");
737 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530738 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530739 return;
740 }
741 }
742 } else {
Manu Gautam98013c22012-11-20 17:42:42 +0530743 if (charger)
744 charger->start_detection(dotg->charger, false);
745
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530746 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530747 dev_dbg(phy->dev, "No device, trying to suspend\n");
748 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530749 }
750 break;
751
752 case OTG_STATE_B_PERIPHERAL:
753 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
754 !test_bit(ID, &dotg->inputs)) {
755 dev_dbg(phy->dev, "!id || !bsv\n");
756 dwc3_otg_start_peripheral(&dotg->otg, 0);
757 phy->state = OTG_STATE_B_IDLE;
758 if (charger)
759 charger->chg_type = DWC3_INVALID_CHARGER;
760 work = 1;
761 }
762 break;
763
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200764 case OTG_STATE_A_IDLE:
765 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530766 if (test_bit(ID, &dotg->inputs)) {
767 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200768 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530769 work = 1;
770 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530771 phy->state = OTG_STATE_A_HOST;
772 if (dwc3_otg_start_host(&dotg->otg, 1)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530773 /*
774 * Probably set_host was not called yet.
775 * We will re-try as soon as it will be called
776 */
Manu Gautamb5067272012-07-02 09:53:41 +0530777 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530778 "unable to start A-device\n");
779 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530780 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530781 return;
782 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200783 }
784 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530785
786 case OTG_STATE_A_HOST:
787 if (test_bit(ID, &dotg->inputs)) {
788 dev_dbg(phy->dev, "id\n");
789 dwc3_otg_start_host(&dotg->otg, 0);
790 phy->state = OTG_STATE_B_IDLE;
791 work = 1;
792 }
793 break;
794
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200795 default:
796 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
797
798 }
Manu Gautam8c642812012-06-07 10:35:10 +0530799
800 if (work)
801 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200802}
803
804
805/**
806 * dwc3_otg_reset - reset dwc3 otg registers.
807 *
808 * @w: Pointer to the dwc3 otg workqueue
809 */
810static void dwc3_otg_reset(struct dwc3_otg *dotg)
811{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530812 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530813 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
814
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200815 /*
816 * OCFG[2] - OTG-Version = 1
817 * OCFG[1] - HNPCap = 0
818 * OCFG[0] - SRPCap = 0
819 */
820 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
821
822 /*
823 * OCTL[6] - PeriMode = 1
824 * OCTL[5] - PrtPwrCtl = 0
825 * OCTL[4] - HNPReq = 0
826 * OCTL[3] - SesReq = 0
827 * OCTL[2] - TermSelDLPulse = 0
828 * OCTL[1] - DevSetHNPEn = 0
829 * OCTL[0] - HstSetHNPEn = 0
830 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530831 if (!once) {
832 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
833 once++;
834 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200835
836 /* Clear all otg events (interrupts) indications */
837 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
838
Manu Gautam8c642812012-06-07 10:35:10 +0530839 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530840 if (ext_xceiv && !ext_xceiv->otg_capability)
841 dwc3_writel(dotg->regs, DWC3_OEVTEN,
842 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
843 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200844}
845
846/**
847 * dwc3_otg_init - Initializes otg related registers
848 * @dwc: Pointer to out controller context structure
849 *
850 * Returns 0 on success otherwise negative errno.
851 */
852int dwc3_otg_init(struct dwc3 *dwc)
853{
854 u32 reg;
855 int ret = 0;
856 struct dwc3_otg *dotg;
857
858 dev_dbg(dwc->dev, "dwc3_otg_init\n");
859
860 /*
861 * GHWPARAMS6[10] bit is SRPSupport.
862 * This bit also reflects DWC_USB3_EN_OTG
863 */
864 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
865 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
866 /*
867 * No OTG support in the HW core.
868 * We return 0 to indicate no error, since this is acceptable
869 * situation, just continue probe the dwc3 driver without otg.
870 */
871 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
872 return 0;
873 }
874
875 /* Allocate and init otg instance */
876 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
877 if (!dotg) {
878 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
879 return -ENOMEM;
880 }
881
Manu Gautam17206c22012-06-21 10:17:53 +0530882 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
883 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
884 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200885 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530886 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200887 ret = -ENODEV;
888 goto err1;
889 }
890
891 dotg->regs = dwc->regs;
892
893 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
894 dotg->otg.set_host = dwc3_otg_set_host;
895
896 /* This reference is used by dwc3 modules for checking otg existance */
897 dwc->dotg = dotg;
898
899 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
900 if (!dotg->otg.phy) {
901 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
902 ret = -ENOMEM;
903 goto err1;
904 }
905
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530906 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200907 dotg->otg.phy->otg = &dotg->otg;
908 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530909 dotg->otg.phy->set_power = dwc3_otg_set_power;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530910 dotg->otg.phy->set_suspend = dwc3_otg_set_suspend;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200911
912 ret = usb_set_transceiver(dotg->otg.phy);
913 if (ret) {
914 dev_err(dotg->otg.phy->dev,
915 "%s: failed to set transceiver, already exists\n",
916 __func__);
917 goto err2;
918 }
919
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200920 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
921
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530922 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200923 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
924
925 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
926 "dwc3_otg", dotg);
927 if (ret) {
928 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
929 dotg->irq, ret);
930 goto err3;
931 }
932
Manu Gautamb5067272012-07-02 09:53:41 +0530933 pm_runtime_get(dwc->dev);
934
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200935 return 0;
936
937err3:
938 cancel_work_sync(&dotg->sm_work);
939 usb_set_transceiver(NULL);
940err2:
941 kfree(dotg->otg.phy);
942err1:
943 dwc->dotg = NULL;
944 kfree(dotg);
945
946 return ret;
947}
948
949/**
950 * dwc3_otg_exit
951 * @dwc: Pointer to out controller context structure
952 *
953 * Returns 0 on success otherwise negative errno.
954 */
955void dwc3_otg_exit(struct dwc3 *dwc)
956{
957 struct dwc3_otg *dotg = dwc->dotg;
958
959 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
960 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530961 if (dotg->charger)
962 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200963 cancel_work_sync(&dotg->sm_work);
964 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530965 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200966 free_irq(dotg->irq, dotg);
967 kfree(dotg->otg.phy);
968 kfree(dotg);
969 dwc->dotg = NULL;
970 }
971}