blob: 1d67cee755fceeb74317475ce0095de82bcffc12 [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
Manu Gautamab3da1b2013-04-02 14:29:01 +0530219 dwc3_otg_set_peripheral_regs(dotg);
220
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530221 /* re-init core and OTG registers as block reset clears these */
222 dwc3_post_host_reset_core_init(dwc);
223 if (ext_xceiv && !ext_xceiv->otg_capability)
224 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200225 }
226
227 return 0;
228}
229
230/**
231 * dwc3_otg_set_host - bind/unbind the host controller driver.
232 *
233 * @otg: Pointer to the otg_transceiver structure.
234 * @host: Pointer to the usb_bus structure.
235 *
236 * Returns 0 on success otherwise negative errno.
237 */
238static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
239{
240 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
241
242 if (host) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530243 dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200244 __func__, host->bus_name);
245 otg->host = host;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200246 /*
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530247 * Though XHCI power would be set by now, but some delay is
248 * required for XHCI controller before setting OTG Port Power
249 * TODO: Tune this delay
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200250 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530251 msleep(300);
252 dwc3_otg_set_host_power(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200253 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530254 otg->host = NULL;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200255 }
256
257 return 0;
258}
259
260/**
261 * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
262 *
263 * @otg: Pointer to the otg_transceiver structure.
264 * @gadget: pointer to the usb_gadget structure.
265 *
266 * Returns 0 on success otherwise negative errno.
267 */
268static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
269{
270 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
Manu Gautama302f612012-12-18 17:33:06 +0530271 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200272
273 if (!otg->gadget)
274 return -EINVAL;
275
276 if (on) {
277 dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
278 __func__, otg->gadget->name);
Manu Gautama302f612012-12-18 17:33:06 +0530279
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530280 /* Core reset is not required during start peripheral. Only
281 * DBM reset is required, hence perform only DBM reset here */
Manu Gautama302f612012-12-18 17:33:06 +0530282 if (ext_xceiv && ext_xceiv->otg_capability &&
283 ext_xceiv->ext_block_reset)
Vijayavardhan Vennapusaf7c01a42013-03-15 15:29:11 +0530284 ext_xceiv->ext_block_reset(false);
Manu Gautama302f612012-12-18 17:33:06 +0530285
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200286 dwc3_otg_set_peripheral_regs(dotg);
287 usb_gadget_vbus_connect(otg->gadget);
288 } else {
289 dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
290 __func__, otg->gadget->name);
291 usb_gadget_vbus_disconnect(otg->gadget);
292 }
293
294 return 0;
295}
296
297/**
298 * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
299 *
300 * @otg: Pointer to the otg_transceiver structure.
301 * @gadget: pointer to the usb_gadget structure.
302 *
303 * Returns 0 on success otherwise negative errno.
304 */
305static int dwc3_otg_set_peripheral(struct usb_otg *otg,
306 struct usb_gadget *gadget)
307{
308 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
309
310 if (gadget) {
311 dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
312 __func__, gadget->name);
313 otg->gadget = gadget;
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530314 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200315 } else {
316 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
317 dwc3_otg_start_peripheral(otg, 0);
318 otg->gadget = NULL;
319 otg->phy->state = OTG_STATE_UNDEFINED;
320 schedule_work(&dotg->sm_work);
321 } else {
322 otg->gadget = NULL;
323 }
324 }
325
326 return 0;
327}
328
329/**
Manu Gautam8c642812012-06-07 10:35:10 +0530330 * dwc3_ext_chg_det_done - callback to handle charger detection completion
331 * @otg: Pointer to the otg transceiver structure
332 * @charger: Pointer to the external charger structure
333 *
334 * Returns 0 on success
335 */
336static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
337{
338 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
339
340 /*
341 * Ignore chg_detection notification if BSV has gone off by this time.
342 * STOP chg_det as part of !BSV handling would reset the chg_det flags
343 */
344 if (test_bit(B_SESS_VLD, &dotg->inputs))
345 schedule_work(&dotg->sm_work);
346}
347
348/**
349 * dwc3_set_charger - bind/unbind external charger driver
350 * @otg: Pointer to the otg transceiver structure
351 * @charger: Pointer to the external charger structure
352 *
353 * Returns 0 on success
354 */
355int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
356{
357 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
358
359 dotg->charger = charger;
360 if (charger)
361 charger->notify_detection_complete = dwc3_ext_chg_det_done;
362
363 return 0;
364}
365
Manu Gautamb5067272012-07-02 09:53:41 +0530366/**
367 * dwc3_ext_event_notify - callback to handle events from external transceiver
368 * @otg: Pointer to the otg transceiver structure
369 * @event: Event reported by transceiver
370 *
371 * Returns 0 on success
372 */
373static void dwc3_ext_event_notify(struct usb_otg *otg,
374 enum dwc3_ext_events event)
375{
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530376 static bool init;
Manu Gautamb5067272012-07-02 09:53:41 +0530377 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
378 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
379 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530380 int ret = 0;
Manu Gautamb5067272012-07-02 09:53:41 +0530381
Manu Gautamf71d9cb2013-02-07 13:52:12 +0530382 /* Flush processing any pending events before handling new ones */
383 if (init)
384 flush_work(&dotg->sm_work);
385
Manu Gautamb5067272012-07-02 09:53:41 +0530386 if (event == DWC3_EVENT_PHY_RESUME) {
387 if (!pm_runtime_status_suspended(phy->dev)) {
388 dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
389 } else {
390 dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
391 /* ext_xceiver would have taken h/w out of LPM by now */
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530392 ret = pm_runtime_get(phy->dev);
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530393 if ((phy->state == OTG_STATE_A_HOST) &&
394 dotg->host_bus_suspend)
395 dotg->host_bus_suspend = 0;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530396 if (ret == -EACCES) {
397 /* pm_runtime_get may fail during system
398 resume with -EACCES error */
399 pm_runtime_disable(phy->dev);
400 pm_runtime_set_active(phy->dev);
401 pm_runtime_enable(phy->dev);
402 } else if (ret < 0) {
403 dev_warn(phy->dev, "pm_runtime_get failed!\n");
404 }
Manu Gautamb5067272012-07-02 09:53:41 +0530405 }
Manu Gautam377821c2012-09-28 16:53:24 +0530406 } else if (event == DWC3_EVENT_XCEIV_STATE) {
Manu Gautamf71d9cb2013-02-07 13:52:12 +0530407 if (pm_runtime_status_suspended(phy->dev)) {
408 dev_warn(phy->dev, "PHY_STATE event in LPM!!!!\n");
409 ret = pm_runtime_get(phy->dev);
410 if (ret < 0)
411 dev_warn(phy->dev, "pm_runtime_get failed!!\n");
412 }
Jack Pham0fc12332012-11-19 13:14:22 -0800413 if (ext_xceiv->id == DWC3_ID_FLOAT) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530414 dev_dbg(phy->dev, "XCVR: ID set\n");
415 set_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800416 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530417 dev_dbg(phy->dev, "XCVR: ID clear\n");
418 clear_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800419 }
Manu Gautam377821c2012-09-28 16:53:24 +0530420
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530421 if (ext_xceiv->bsv) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530422 dev_dbg(phy->dev, "XCVR: BSV set\n");
423 set_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530424 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530425 dev_dbg(phy->dev, "XCVR: BSV clear\n");
426 clear_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530427 }
Manu Gautam377821c2012-09-28 16:53:24 +0530428
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530429 if (!init) {
430 init = true;
Manu Gautam4ff07242013-03-27 14:31:11 +0530431 if (!work_busy(&dotg->sm_work))
432 schedule_work(&dotg->sm_work);
433
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530434 complete(&dotg->dwc3_xcvr_vbus_init);
435 dev_dbg(phy->dev, "XCVR: BSV init complete\n");
436 return;
437 }
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530438
439 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530440 }
Manu Gautamb5067272012-07-02 09:53:41 +0530441}
442
443/**
444 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
445 * @otg: Pointer to the otg transceiver structure
446 * @ext_xceiv: Pointer to the external transceiver struccture
447 *
448 * Returns 0 on success
449 */
450int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
451{
452 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
453
454 dotg->ext_xceiv = ext_xceiv;
455 if (ext_xceiv)
456 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
457
458 return 0;
459}
460
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530461static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
462{
463 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
464
465 if (!dotg->psy) {
466 dev_err(otg->phy->dev, "no usb power supply registered\n");
467 return;
468 }
469
470 if (host_mode)
471 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
472 else
473 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
474}
475
476static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
477{
478 static int power_supply_type;
479 struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
480
481
Manu Gautam6c0ff032012-11-02 14:55:35 +0530482 if (!dotg->psy || !dotg->charger) {
483 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530484 return 0;
485 }
486
Manu Gautam6c0ff032012-11-02 14:55:35 +0530487 if (dotg->charger->charging_disabled)
488 return 0;
489
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530490 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
491 power_supply_type = POWER_SUPPLY_TYPE_USB;
492 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
493 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
Manu Gautama1e331d2013-02-07 14:55:05 +0530494 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER ||
495 dotg->charger->chg_type == DWC3_PROPRIETARY_CHARGER)
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530496 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
497 else
498 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
499
500 power_supply_set_supply_type(dotg->psy, power_supply_type);
501
Vijayavardhan Vennapusa2e0b4182013-03-21 12:49:43 +0530502 if ((dotg->charger->chg_type == DWC3_CDP_CHARGER) && mA > 2)
503 mA = DWC3_IDEV_CHG_MAX;
504
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530505 if (dotg->charger->max_power == mA)
506 return 0;
507
508 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
509
510 if (dotg->charger->max_power <= 2 && mA > 2) {
511 /* Enable charging */
512 if (power_supply_set_online(dotg->psy, true))
513 goto psy_error;
514 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
515 goto psy_error;
516 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
517 /* Disable charging */
518 if (power_supply_set_online(dotg->psy, false))
519 goto psy_error;
520 /* Set max current limit */
521 if (power_supply_set_current_limit(dotg->psy, 0))
522 goto psy_error;
523 }
524
525 power_supply_changed(dotg->psy);
526 dotg->charger->max_power = mA;
527 return 0;
528
529psy_error:
530 dev_dbg(phy->dev, "power supply error when setting property\n");
531 return -ENXIO;
532}
533
Manu Gautam8c642812012-06-07 10:35:10 +0530534/* IRQs which OTG driver is interested in handling */
535#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
536 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
537
538/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200539 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
540 * @_dotg: Pointer to out controller context structure
541 *
542 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
543 */
544static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
545{
546 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530547 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200548 int ret = IRQ_NONE;
549 int handled_irqs = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530550 struct usb_phy *phy = dotg->otg.phy;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200551
552 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
553
Manu Gautam8c642812012-06-07 10:35:10 +0530554 if (!(oevt_reg & DWC3_OEVT_MASK))
555 return IRQ_NONE;
556
557 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
558
559 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
560 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200561 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530562 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200563 * function, switch from A to B or from B to A.
564 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200565
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530566 if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {
567 if (osts & DWC3_OTG_OSTS_CONIDSTS) {
568 dev_dbg(phy->dev, "ID set\n");
569 set_bit(ID, &dotg->inputs);
570 } else {
571 dev_dbg(phy->dev, "ID clear\n");
572 clear_bit(ID, &dotg->inputs);
573 }
574 handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;
575 }
Manu Gautam8c642812012-06-07 10:35:10 +0530576
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530577 if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {
578 if (osts & DWC3_OTG_OSTS_BSESVALID) {
579 dev_dbg(phy->dev, "BSV set\n");
580 set_bit(B_SESS_VLD, &dotg->inputs);
581 } else {
582 dev_dbg(phy->dev, "BSV clear\n");
583 clear_bit(B_SESS_VLD, &dotg->inputs);
584 }
585 handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;
586 }
Manu Gautam8c642812012-06-07 10:35:10 +0530587
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200588 schedule_work(&dotg->sm_work);
589
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200590 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530591
592 /* Clear the interrupts we handled */
593 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200594 }
595
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200596 return ret;
597}
598
599/**
Manu Gautam8c642812012-06-07 10:35:10 +0530600 * dwc3_otg_init_sm - initialize OTG statemachine input
601 * @dotg: Pointer to the dwc3_otg structure
602 *
603 */
604void dwc3_otg_init_sm(struct dwc3_otg *dotg)
605{
606 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
607 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530608 struct dwc3_ext_xceiv *ext_xceiv;
609 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530610
611 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
612
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530613 /*
614 * VBUS initial state is reported after PMIC
615 * driver initialization. Wait for it.
616 */
617 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
Manu Gautam4ff07242013-03-27 14:31:11 +0530618 if (!ret) {
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530619 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam4ff07242013-03-27 14:31:11 +0530620 /* We can safely assume no cable connected */
621 set_bit(ID, &dotg->inputs);
622 }
Manu Gautam8c642812012-06-07 10:35:10 +0530623
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530624 ext_xceiv = dotg->ext_xceiv;
625 dwc3_otg_reset(dotg);
626 if (ext_xceiv && !ext_xceiv->otg_capability) {
627 if (osts & DWC3_OTG_OSTS_CONIDSTS)
628 set_bit(ID, &dotg->inputs);
629 else
630 clear_bit(ID, &dotg->inputs);
631
632 if (osts & DWC3_OTG_OSTS_BSESVALID)
633 set_bit(B_SESS_VLD, &dotg->inputs);
634 else
635 clear_bit(B_SESS_VLD, &dotg->inputs);
636 }
Manu Gautam8c642812012-06-07 10:35:10 +0530637}
638
639/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200640 * dwc3_otg_sm_work - workqueue function.
641 *
642 * @w: Pointer to the dwc3 otg workqueue
643 *
644 * NOTE: After any change in phy->state,
645 * we must reschdule the state machine.
646 */
647static void dwc3_otg_sm_work(struct work_struct *w)
648{
649 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
650 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530651 struct dwc3_charger *charger = dotg->charger;
652 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200653
Manu Gautamb5067272012-07-02 09:53:41 +0530654 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200655 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
656
657 /* Check OTG state */
658 switch (phy->state) {
659 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530660 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530661 if (!dotg->psy) {
662 dotg->psy = power_supply_get_by_name("usb");
663
664 if (!dotg->psy)
665 dev_err(phy->dev,
666 "couldn't get usb power supply\n");
667 }
668
Manu Gautam8c642812012-06-07 10:35:10 +0530669 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530670 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530671 dev_dbg(phy->dev, "!id\n");
672 phy->state = OTG_STATE_A_IDLE;
673 work = 1;
674 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
675 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200676 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530677 work = 1;
678 } else {
679 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530680 dev_dbg(phy->dev, "No device, trying to suspend\n");
681 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200682 }
683 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530684
685 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530686 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530687 dev_dbg(phy->dev, "!id\n");
688 phy->state = OTG_STATE_A_IDLE;
689 work = 1;
690 if (charger) {
691 if (charger->chg_type == DWC3_INVALID_CHARGER)
692 charger->start_detection(dotg->charger,
693 false);
694 else
695 charger->chg_type =
696 DWC3_INVALID_CHARGER;
697 }
698 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
699 dev_dbg(phy->dev, "b_sess_vld\n");
700 if (charger) {
701 /* Has charger been detected? If no detect it */
702 switch (charger->chg_type) {
703 case DWC3_DCP_CHARGER:
Manu Gautama1e331d2013-02-07 14:55:05 +0530704 case DWC3_PROPRIETARY_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530705 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530706 dwc3_otg_set_power(phy,
707 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530708 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530709 break;
710 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530711 dwc3_otg_set_power(phy,
712 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530713 dwc3_otg_start_peripheral(&dotg->otg,
714 1);
715 phy->state = OTG_STATE_B_PERIPHERAL;
716 work = 1;
717 break;
718 case DWC3_SDP_CHARGER:
719 dwc3_otg_start_peripheral(&dotg->otg,
720 1);
721 phy->state = OTG_STATE_B_PERIPHERAL;
722 work = 1;
723 break;
724 default:
725 dev_dbg(phy->dev, "chg_det started\n");
726 charger->start_detection(charger, true);
727 break;
728 }
729 } else {
730 /* no charger registered, start peripheral */
731 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
732 /*
733 * Probably set_peripheral not called
734 * yet. We will re-try as soon as it
735 * will be called
736 */
Manu Gautamb5067272012-07-02 09:53:41 +0530737 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530738 "unable to start B-device\n");
739 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530740 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530741 return;
742 }
743 }
744 } else {
Manu Gautam98013c22012-11-20 17:42:42 +0530745 if (charger)
746 charger->start_detection(dotg->charger, false);
747
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530748 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530749 dev_dbg(phy->dev, "No device, trying to suspend\n");
750 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530751 }
752 break;
753
754 case OTG_STATE_B_PERIPHERAL:
755 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
756 !test_bit(ID, &dotg->inputs)) {
757 dev_dbg(phy->dev, "!id || !bsv\n");
758 dwc3_otg_start_peripheral(&dotg->otg, 0);
759 phy->state = OTG_STATE_B_IDLE;
760 if (charger)
761 charger->chg_type = DWC3_INVALID_CHARGER;
762 work = 1;
763 }
764 break;
765
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200766 case OTG_STATE_A_IDLE:
767 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530768 if (test_bit(ID, &dotg->inputs)) {
769 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200770 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530771 work = 1;
772 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530773 phy->state = OTG_STATE_A_HOST;
774 if (dwc3_otg_start_host(&dotg->otg, 1)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530775 /*
776 * Probably set_host was not called yet.
777 * We will re-try as soon as it will be called
778 */
Manu Gautamb5067272012-07-02 09:53:41 +0530779 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530780 "unable to start A-device\n");
781 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530782 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530783 return;
784 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200785 }
786 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530787
788 case OTG_STATE_A_HOST:
789 if (test_bit(ID, &dotg->inputs)) {
790 dev_dbg(phy->dev, "id\n");
791 dwc3_otg_start_host(&dotg->otg, 0);
792 phy->state = OTG_STATE_B_IDLE;
793 work = 1;
794 }
795 break;
796
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200797 default:
798 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
799
800 }
Manu Gautam8c642812012-06-07 10:35:10 +0530801
802 if (work)
803 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200804}
805
806
807/**
808 * dwc3_otg_reset - reset dwc3 otg registers.
809 *
810 * @w: Pointer to the dwc3 otg workqueue
811 */
812static void dwc3_otg_reset(struct dwc3_otg *dotg)
813{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530814 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530815 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
816
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200817 /*
818 * OCFG[2] - OTG-Version = 1
819 * OCFG[1] - HNPCap = 0
820 * OCFG[0] - SRPCap = 0
821 */
822 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
823
824 /*
825 * OCTL[6] - PeriMode = 1
826 * OCTL[5] - PrtPwrCtl = 0
827 * OCTL[4] - HNPReq = 0
828 * OCTL[3] - SesReq = 0
829 * OCTL[2] - TermSelDLPulse = 0
830 * OCTL[1] - DevSetHNPEn = 0
831 * OCTL[0] - HstSetHNPEn = 0
832 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530833 if (!once) {
834 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
835 once++;
836 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200837
838 /* Clear all otg events (interrupts) indications */
839 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
840
Manu Gautam8c642812012-06-07 10:35:10 +0530841 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530842 if (ext_xceiv && !ext_xceiv->otg_capability)
843 dwc3_writel(dotg->regs, DWC3_OEVTEN,
844 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
845 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200846}
847
848/**
849 * dwc3_otg_init - Initializes otg related registers
850 * @dwc: Pointer to out controller context structure
851 *
852 * Returns 0 on success otherwise negative errno.
853 */
854int dwc3_otg_init(struct dwc3 *dwc)
855{
856 u32 reg;
857 int ret = 0;
858 struct dwc3_otg *dotg;
859
860 dev_dbg(dwc->dev, "dwc3_otg_init\n");
861
862 /*
863 * GHWPARAMS6[10] bit is SRPSupport.
864 * This bit also reflects DWC_USB3_EN_OTG
865 */
866 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
867 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
868 /*
869 * No OTG support in the HW core.
870 * We return 0 to indicate no error, since this is acceptable
871 * situation, just continue probe the dwc3 driver without otg.
872 */
873 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
874 return 0;
875 }
876
877 /* Allocate and init otg instance */
878 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
879 if (!dotg) {
880 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
881 return -ENOMEM;
882 }
883
Manu Gautam17206c22012-06-21 10:17:53 +0530884 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
885 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
886 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200887 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530888 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200889 ret = -ENODEV;
890 goto err1;
891 }
892
893 dotg->regs = dwc->regs;
894
895 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
896 dotg->otg.set_host = dwc3_otg_set_host;
897
898 /* This reference is used by dwc3 modules for checking otg existance */
899 dwc->dotg = dotg;
900
901 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
902 if (!dotg->otg.phy) {
903 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
904 ret = -ENOMEM;
905 goto err1;
906 }
907
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530908 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200909 dotg->otg.phy->otg = &dotg->otg;
910 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530911 dotg->otg.phy->set_power = dwc3_otg_set_power;
Vijayavardhan Vennapusa45145882013-01-03 14:11:58 +0530912 dotg->otg.phy->set_suspend = dwc3_otg_set_suspend;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200913
914 ret = usb_set_transceiver(dotg->otg.phy);
915 if (ret) {
916 dev_err(dotg->otg.phy->dev,
917 "%s: failed to set transceiver, already exists\n",
918 __func__);
919 goto err2;
920 }
921
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200922 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
923
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530924 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200925 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
926
927 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
928 "dwc3_otg", dotg);
929 if (ret) {
930 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
931 dotg->irq, ret);
932 goto err3;
933 }
934
Manu Gautamb5067272012-07-02 09:53:41 +0530935 pm_runtime_get(dwc->dev);
936
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200937 return 0;
938
939err3:
940 cancel_work_sync(&dotg->sm_work);
941 usb_set_transceiver(NULL);
942err2:
943 kfree(dotg->otg.phy);
944err1:
945 dwc->dotg = NULL;
946 kfree(dotg);
947
948 return ret;
949}
950
951/**
952 * dwc3_otg_exit
953 * @dwc: Pointer to out controller context structure
954 *
955 * Returns 0 on success otherwise negative errno.
956 */
957void dwc3_otg_exit(struct dwc3 *dwc)
958{
959 struct dwc3_otg *dotg = dwc->dotg;
960
961 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
962 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530963 if (dotg->charger)
964 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200965 cancel_work_sync(&dotg->sm_work);
966 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530967 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200968 free_irq(dotg->irq, dotg);
969 kfree(dotg->otg.phy);
970 kfree(dotg);
971 dwc->dotg = NULL;
972 }
973}