blob: 7b672c4af2b134df2ca1f34157338493405ebcff [file] [log] [blame]
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001/**
2 * dwc3_otg.c - DesignWare USB3 DRD Controller OTG
3 *
4 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#include <linux/usb.h>
17#include <linux/usb/hcd.h>
18#include <linux/platform_device.h>
Manu 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{
41 u32 octl;
42
43 /* Set OCTL[6](PeriMode) to 0 (host) */
44 octl = dwc3_readl(dotg->regs, DWC3_OCTL);
45 octl &= ~DWC3_OTG_OCTL_PERIMODE;
46 dwc3_writel(dotg->regs, DWC3_OCTL, octl);
Manu Gautamf1fceddf2012-10-12 14:02:50 +053047}
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020048
Manu Gautamf1fceddf2012-10-12 14:02:50 +053049/**
50 * dwc3_otg_set_host_power - Enable port power control for host operation
51 *
52 * This function enables the OTG Port Power required to operate in Host mode
53 * This function should be called only after XHCI driver has set the port
54 * power in PORTSC register.
55 *
56 * @w: Pointer to the dwc3 otg struct
57 */
58void dwc3_otg_set_host_power(struct dwc3_otg *dotg)
59{
60 u32 osts;
61
62 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
63 if (!(osts & 0x8))
64 dev_err(dotg->dwc->dev, "%s: xHCIPrtPower not set\n", __func__);
65
66 dwc3_writel(dotg->regs, DWC3_OCTL, DWC3_OTG_OCTL_PRTPWRCTL);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020067}
68
69/**
70 * dwc3_otg_set_peripheral_regs - reset dwc3 otg registers to peripheral operation.
71 *
72 * This function sets the OTG registers to work in B-Device peripheral mode.
73 * This function should be called just before entering to B-Device mode.
74 *
75 * @w: Pointer to the dwc3 otg workqueue.
76 */
77static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg)
78{
79 u32 octl;
80
81 /* Set OCTL[6](PeriMode) to 1 (peripheral) */
82 octl = dwc3_readl(dotg->regs, DWC3_OCTL);
83 octl |= DWC3_OTG_OCTL_PERIMODE;
84 dwc3_writel(dotg->regs, DWC3_OCTL, octl);
85
86 /*
87 * TODO: add more OTG registers writes for PERIPHERAL mode here,
88 * see figure 12-19 B-device flow in dwc3 Synopsis spec
89 */
90}
91
92/**
93 * dwc3_otg_start_host - helper function for starting/stoping the host controller driver.
94 *
95 * @otg: Pointer to the otg_transceiver structure.
96 * @on: start / stop the host controller driver.
97 *
98 * Returns 0 on success otherwise negative errno.
99 */
100static int dwc3_otg_start_host(struct usb_otg *otg, int on)
101{
102 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
Manu Gautam61721592012-11-06 18:09:39 +0530103 struct dwc3 *dwc = dotg->dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200104 int ret = 0;
105
Manu Gautam61721592012-11-06 18:09:39 +0530106 if (!dwc->xhci)
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200107 return -EINVAL;
108
Manu Gautam61721592012-11-06 18:09:39 +0530109 if (!dotg->vbus_otg) {
110 dotg->vbus_otg = devm_regulator_get(dwc->dev->parent,
111 "vbus_dwc3");
112 if (IS_ERR(dotg->vbus_otg)) {
113 dev_err(dwc->dev, "Failed to get vbus regulator\n");
114 ret = PTR_ERR(dotg->vbus_otg);
115 dotg->vbus_otg = 0;
116 return ret;
117 }
118 }
119
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200120 if (on) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530121 dev_dbg(otg->phy->dev, "%s: turn on host\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200122
123 /*
124 * This should be revisited for more testing post-silicon.
125 * In worst case we may need to disconnect the root hub
126 * before stopping the controller so that it does not
127 * interfere with runtime pm/system pm.
128 * We can also consider registering and unregistering xhci
129 * platform device. It is almost similar to add_hcd and
130 * remove_hcd, But we may not use standard set_host method
131 * anymore.
132 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530133 dwc3_otg_set_host_regs(dotg);
Manu Gautam61721592012-11-06 18:09:39 +0530134 ret = platform_device_add(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200135 if (ret) {
136 dev_err(otg->phy->dev,
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530137 "%s: failed to add XHCI pdev ret=%d\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200138 __func__, ret);
139 return ret;
140 }
141
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530142 dwc3_otg_notify_host_mode(otg, on);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530143 ret = regulator_enable(dotg->vbus_otg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200144 if (ret) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530145 dev_err(otg->phy->dev, "unable to enable vbus_otg\n");
Manu Gautam61721592012-11-06 18:09:39 +0530146 platform_device_del(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200147 return ret;
148 }
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530149
150 /* re-init OTG EVTEN register as XHCI reset clears it */
151 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200152 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530153 dev_dbg(otg->phy->dev, "%s: turn off host\n", __func__);
154
Manu Gautam61721592012-11-06 18:09:39 +0530155 platform_device_del(dwc->xhci);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530156
157 ret = regulator_disable(dotg->vbus_otg);
158 if (ret) {
159 dev_err(otg->phy->dev, "unable to disable vbus_otg\n");
160 return ret;
161 }
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530162 dwc3_otg_notify_host_mode(otg, on);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530163
164 /* re-init core and OTG register as XHCI reset clears it */
Manu Gautam61721592012-11-06 18:09:39 +0530165 dwc3_post_host_reset_core_init(dwc);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530166 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200167 }
168
169 return 0;
170}
171
172/**
173 * dwc3_otg_set_host - bind/unbind the host controller driver.
174 *
175 * @otg: Pointer to the otg_transceiver structure.
176 * @host: Pointer to the usb_bus structure.
177 *
178 * Returns 0 on success otherwise negative errno.
179 */
180static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
181{
182 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
183
184 if (host) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530185 dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200186 __func__, host->bus_name);
187 otg->host = host;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200188 /*
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530189 * Though XHCI power would be set by now, but some delay is
190 * required for XHCI controller before setting OTG Port Power
191 * TODO: Tune this delay
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200192 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530193 msleep(300);
194 dwc3_otg_set_host_power(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200195 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530196 otg->host = NULL;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200197 }
198
199 return 0;
200}
201
202/**
203 * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
204 *
205 * @otg: Pointer to the otg_transceiver structure.
206 * @gadget: pointer to the usb_gadget structure.
207 *
208 * Returns 0 on success otherwise negative errno.
209 */
210static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
211{
212 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
213
214 if (!otg->gadget)
215 return -EINVAL;
216
217 if (on) {
218 dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
219 __func__, otg->gadget->name);
220 dwc3_otg_set_peripheral_regs(dotg);
221 usb_gadget_vbus_connect(otg->gadget);
222 } else {
223 dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
224 __func__, otg->gadget->name);
225 usb_gadget_vbus_disconnect(otg->gadget);
226 }
227
228 return 0;
229}
230
231/**
232 * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
233 *
234 * @otg: Pointer to the otg_transceiver structure.
235 * @gadget: pointer to the usb_gadget structure.
236 *
237 * Returns 0 on success otherwise negative errno.
238 */
239static int dwc3_otg_set_peripheral(struct usb_otg *otg,
240 struct usb_gadget *gadget)
241{
242 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
243
244 if (gadget) {
245 dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
246 __func__, gadget->name);
247 otg->gadget = gadget;
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530248 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200249 } else {
250 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
251 dwc3_otg_start_peripheral(otg, 0);
252 otg->gadget = NULL;
253 otg->phy->state = OTG_STATE_UNDEFINED;
254 schedule_work(&dotg->sm_work);
255 } else {
256 otg->gadget = NULL;
257 }
258 }
259
260 return 0;
261}
262
263/**
Manu Gautam8c642812012-06-07 10:35:10 +0530264 * dwc3_ext_chg_det_done - callback to handle charger detection completion
265 * @otg: Pointer to the otg transceiver structure
266 * @charger: Pointer to the external charger structure
267 *
268 * Returns 0 on success
269 */
270static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
271{
272 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
273
274 /*
275 * Ignore chg_detection notification if BSV has gone off by this time.
276 * STOP chg_det as part of !BSV handling would reset the chg_det flags
277 */
278 if (test_bit(B_SESS_VLD, &dotg->inputs))
279 schedule_work(&dotg->sm_work);
280}
281
282/**
283 * dwc3_set_charger - bind/unbind external charger driver
284 * @otg: Pointer to the otg transceiver structure
285 * @charger: Pointer to the external charger structure
286 *
287 * Returns 0 on success
288 */
289int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
290{
291 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
292
293 dotg->charger = charger;
294 if (charger)
295 charger->notify_detection_complete = dwc3_ext_chg_det_done;
296
297 return 0;
298}
299
Manu Gautamb5067272012-07-02 09:53:41 +0530300/**
301 * dwc3_ext_event_notify - callback to handle events from external transceiver
302 * @otg: Pointer to the otg transceiver structure
303 * @event: Event reported by transceiver
304 *
305 * Returns 0 on success
306 */
307static void dwc3_ext_event_notify(struct usb_otg *otg,
308 enum dwc3_ext_events event)
309{
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530310 static bool init;
Manu Gautamb5067272012-07-02 09:53:41 +0530311 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
312 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
313 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530314 int ret = 0;
Manu Gautamb5067272012-07-02 09:53:41 +0530315
316 if (event == DWC3_EVENT_PHY_RESUME) {
317 if (!pm_runtime_status_suspended(phy->dev)) {
318 dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
319 } else {
320 dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
321 /* ext_xceiver would have taken h/w out of LPM by now */
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530322 ret = pm_runtime_get(phy->dev);
323 if (ret == -EACCES) {
324 /* pm_runtime_get may fail during system
325 resume with -EACCES error */
326 pm_runtime_disable(phy->dev);
327 pm_runtime_set_active(phy->dev);
328 pm_runtime_enable(phy->dev);
329 } else if (ret < 0) {
330 dev_warn(phy->dev, "pm_runtime_get failed!\n");
331 }
Manu Gautamb5067272012-07-02 09:53:41 +0530332 }
Manu Gautam377821c2012-09-28 16:53:24 +0530333 } else if (event == DWC3_EVENT_XCEIV_STATE) {
334 if (ext_xceiv->id == DWC3_ID_FLOAT)
335 set_bit(ID, &dotg->inputs);
336 else
337 clear_bit(ID, &dotg->inputs);
338
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530339 if (ext_xceiv->bsv) {
340 dev_dbg(phy->dev, "XCVR: BSV set\n");
Manu Gautam377821c2012-09-28 16:53:24 +0530341 set_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530342 } else {
343 dev_dbg(phy->dev, "XCVR: BSV clear\n");
Manu Gautam377821c2012-09-28 16:53:24 +0530344 clear_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530345 }
Manu Gautam377821c2012-09-28 16:53:24 +0530346
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530347 if (!init) {
348 init = true;
349 complete(&dotg->dwc3_xcvr_vbus_init);
350 dev_dbg(phy->dev, "XCVR: BSV init complete\n");
351 return;
352 }
Manu Gautam377821c2012-09-28 16:53:24 +0530353 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530354 }
Manu Gautamb5067272012-07-02 09:53:41 +0530355}
356
357/**
358 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
359 * @otg: Pointer to the otg transceiver structure
360 * @ext_xceiv: Pointer to the external transceiver struccture
361 *
362 * Returns 0 on success
363 */
364int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
365{
366 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
367
368 dotg->ext_xceiv = ext_xceiv;
369 if (ext_xceiv)
370 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
371
372 return 0;
373}
374
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530375static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
376{
377 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
378
379 if (!dotg->psy) {
380 dev_err(otg->phy->dev, "no usb power supply registered\n");
381 return;
382 }
383
384 if (host_mode)
385 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
386 else
387 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
388}
389
390static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
391{
392 static int power_supply_type;
393 struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
394
395
Manu Gautam6c0ff032012-11-02 14:55:35 +0530396 if (!dotg->psy || !dotg->charger) {
397 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530398 return 0;
399 }
400
Manu Gautam6c0ff032012-11-02 14:55:35 +0530401 if (dotg->charger->charging_disabled)
402 return 0;
403
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530404 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
405 power_supply_type = POWER_SUPPLY_TYPE_USB;
406 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
407 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
408 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER)
409 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
410 else
411 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
412
413 power_supply_set_supply_type(dotg->psy, power_supply_type);
414
415 if (dotg->charger->max_power == mA)
416 return 0;
417
418 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
419
420 if (dotg->charger->max_power <= 2 && mA > 2) {
421 /* Enable charging */
422 if (power_supply_set_online(dotg->psy, true))
423 goto psy_error;
424 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
425 goto psy_error;
426 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
427 /* Disable charging */
428 if (power_supply_set_online(dotg->psy, false))
429 goto psy_error;
430 /* Set max current limit */
431 if (power_supply_set_current_limit(dotg->psy, 0))
432 goto psy_error;
433 }
434
435 power_supply_changed(dotg->psy);
436 dotg->charger->max_power = mA;
437 return 0;
438
439psy_error:
440 dev_dbg(phy->dev, "power supply error when setting property\n");
441 return -ENXIO;
442}
443
Manu Gautam8c642812012-06-07 10:35:10 +0530444/* IRQs which OTG driver is interested in handling */
445#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
446 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
447
448/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200449 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
450 * @_dotg: Pointer to out controller context structure
451 *
452 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
453 */
454static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
455{
456 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530457 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200458 int ret = IRQ_NONE;
459 int handled_irqs = 0;
460
461 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
462
Manu Gautam8c642812012-06-07 10:35:10 +0530463 if (!(oevt_reg & DWC3_OEVT_MASK))
464 return IRQ_NONE;
465
466 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
467
468 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
469 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200470 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530471 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200472 * function, switch from A to B or from B to A.
473 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200474
Manu Gautam8c642812012-06-07 10:35:10 +0530475 if (osts & DWC3_OTG_OSTS_CONIDSTS)
476 set_bit(ID, &dotg->inputs);
477 else
478 clear_bit(ID, &dotg->inputs);
479
480 if (osts & DWC3_OTG_OSTS_BSESVALID)
481 set_bit(B_SESS_VLD, &dotg->inputs);
482 else
483 clear_bit(B_SESS_VLD, &dotg->inputs);
484
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200485 schedule_work(&dotg->sm_work);
486
Manu Gautam8c642812012-06-07 10:35:10 +0530487 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ?
488 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT : 0;
489 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) ?
490 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT : 0;
491
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200492 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530493
494 /* Clear the interrupts we handled */
495 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200496 }
497
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200498 return ret;
499}
500
501/**
Manu Gautam8c642812012-06-07 10:35:10 +0530502 * dwc3_otg_init_sm - initialize OTG statemachine input
503 * @dotg: Pointer to the dwc3_otg structure
504 *
505 */
506void dwc3_otg_init_sm(struct dwc3_otg *dotg)
507{
508 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
509 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530510 struct dwc3_ext_xceiv *ext_xceiv;
511 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530512
513 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
514
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530515 /*
516 * VBUS initial state is reported after PMIC
517 * driver initialization. Wait for it.
518 */
519 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
520 if (!ret)
521 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam8c642812012-06-07 10:35:10 +0530522
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530523 ext_xceiv = dotg->ext_xceiv;
524 dwc3_otg_reset(dotg);
525 if (ext_xceiv && !ext_xceiv->otg_capability) {
526 if (osts & DWC3_OTG_OSTS_CONIDSTS)
527 set_bit(ID, &dotg->inputs);
528 else
529 clear_bit(ID, &dotg->inputs);
530
531 if (osts & DWC3_OTG_OSTS_BSESVALID)
532 set_bit(B_SESS_VLD, &dotg->inputs);
533 else
534 clear_bit(B_SESS_VLD, &dotg->inputs);
535 }
Manu Gautam8c642812012-06-07 10:35:10 +0530536}
537
538/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200539 * dwc3_otg_sm_work - workqueue function.
540 *
541 * @w: Pointer to the dwc3 otg workqueue
542 *
543 * NOTE: After any change in phy->state,
544 * we must reschdule the state machine.
545 */
546static void dwc3_otg_sm_work(struct work_struct *w)
547{
548 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
549 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530550 struct dwc3_charger *charger = dotg->charger;
551 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200552
Manu Gautamb5067272012-07-02 09:53:41 +0530553 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200554 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
555
556 /* Check OTG state */
557 switch (phy->state) {
558 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530559 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530560 if (!dotg->psy) {
561 dotg->psy = power_supply_get_by_name("usb");
562
563 if (!dotg->psy)
564 dev_err(phy->dev,
565 "couldn't get usb power supply\n");
566 }
567
Manu Gautam8c642812012-06-07 10:35:10 +0530568 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530569 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530570 dev_dbg(phy->dev, "!id\n");
571 phy->state = OTG_STATE_A_IDLE;
572 work = 1;
573 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
574 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200575 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530576 work = 1;
577 } else {
578 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530579 dev_dbg(phy->dev, "No device, trying to suspend\n");
580 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200581 }
582 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530583
584 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530585 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530586 dev_dbg(phy->dev, "!id\n");
587 phy->state = OTG_STATE_A_IDLE;
588 work = 1;
589 if (charger) {
590 if (charger->chg_type == DWC3_INVALID_CHARGER)
591 charger->start_detection(dotg->charger,
592 false);
593 else
594 charger->chg_type =
595 DWC3_INVALID_CHARGER;
596 }
597 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
598 dev_dbg(phy->dev, "b_sess_vld\n");
599 if (charger) {
600 /* Has charger been detected? If no detect it */
601 switch (charger->chg_type) {
602 case DWC3_DCP_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530603 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530604 dwc3_otg_set_power(phy,
605 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530606 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530607 break;
608 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530609 dwc3_otg_set_power(phy,
610 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530611 dwc3_otg_start_peripheral(&dotg->otg,
612 1);
613 phy->state = OTG_STATE_B_PERIPHERAL;
614 work = 1;
615 break;
616 case DWC3_SDP_CHARGER:
617 dwc3_otg_start_peripheral(&dotg->otg,
618 1);
619 phy->state = OTG_STATE_B_PERIPHERAL;
620 work = 1;
621 break;
622 default:
623 dev_dbg(phy->dev, "chg_det started\n");
624 charger->start_detection(charger, true);
625 break;
626 }
627 } else {
628 /* no charger registered, start peripheral */
629 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
630 /*
631 * Probably set_peripheral not called
632 * yet. We will re-try as soon as it
633 * will be called
634 */
Manu Gautamb5067272012-07-02 09:53:41 +0530635 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530636 "unable to start B-device\n");
637 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530638 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530639 return;
640 }
641 }
642 } else {
643 if (charger) {
644 if (charger->chg_type == DWC3_INVALID_CHARGER)
645 charger->start_detection(dotg->charger,
646 false);
647 else
648 charger->chg_type =
649 DWC3_INVALID_CHARGER;
650 }
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530651 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530652 dev_dbg(phy->dev, "No device, trying to suspend\n");
653 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530654 }
655 break;
656
657 case OTG_STATE_B_PERIPHERAL:
658 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
659 !test_bit(ID, &dotg->inputs)) {
660 dev_dbg(phy->dev, "!id || !bsv\n");
661 dwc3_otg_start_peripheral(&dotg->otg, 0);
662 phy->state = OTG_STATE_B_IDLE;
663 if (charger)
664 charger->chg_type = DWC3_INVALID_CHARGER;
665 work = 1;
666 }
667 break;
668
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200669 case OTG_STATE_A_IDLE:
670 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530671 if (test_bit(ID, &dotg->inputs)) {
672 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200673 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530674 work = 1;
675 } else {
676 if (dwc3_otg_start_host(&dotg->otg, 1)) {
677 /*
678 * Probably set_host was not called yet.
679 * We will re-try as soon as it will be called
680 */
Manu Gautamb5067272012-07-02 09:53:41 +0530681 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530682 "unable to start A-device\n");
683 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530684 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530685 return;
686 }
687 phy->state = OTG_STATE_A_HOST;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200688 }
689 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530690
691 case OTG_STATE_A_HOST:
692 if (test_bit(ID, &dotg->inputs)) {
693 dev_dbg(phy->dev, "id\n");
694 dwc3_otg_start_host(&dotg->otg, 0);
695 phy->state = OTG_STATE_B_IDLE;
696 work = 1;
697 }
698 break;
699
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200700 default:
701 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
702
703 }
Manu Gautam8c642812012-06-07 10:35:10 +0530704
705 if (work)
706 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200707}
708
709
710/**
711 * dwc3_otg_reset - reset dwc3 otg registers.
712 *
713 * @w: Pointer to the dwc3 otg workqueue
714 */
715static void dwc3_otg_reset(struct dwc3_otg *dotg)
716{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530717 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530718 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
719
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200720 /*
721 * OCFG[2] - OTG-Version = 1
722 * OCFG[1] - HNPCap = 0
723 * OCFG[0] - SRPCap = 0
724 */
725 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
726
727 /*
728 * OCTL[6] - PeriMode = 1
729 * OCTL[5] - PrtPwrCtl = 0
730 * OCTL[4] - HNPReq = 0
731 * OCTL[3] - SesReq = 0
732 * OCTL[2] - TermSelDLPulse = 0
733 * OCTL[1] - DevSetHNPEn = 0
734 * OCTL[0] - HstSetHNPEn = 0
735 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530736 if (!once) {
737 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
738 once++;
739 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200740
741 /* Clear all otg events (interrupts) indications */
742 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
743
Manu Gautam8c642812012-06-07 10:35:10 +0530744 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530745 if (ext_xceiv && !ext_xceiv->otg_capability)
746 dwc3_writel(dotg->regs, DWC3_OEVTEN,
747 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
748 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200749}
750
751/**
752 * dwc3_otg_init - Initializes otg related registers
753 * @dwc: Pointer to out controller context structure
754 *
755 * Returns 0 on success otherwise negative errno.
756 */
757int dwc3_otg_init(struct dwc3 *dwc)
758{
759 u32 reg;
760 int ret = 0;
761 struct dwc3_otg *dotg;
762
763 dev_dbg(dwc->dev, "dwc3_otg_init\n");
764
765 /*
766 * GHWPARAMS6[10] bit is SRPSupport.
767 * This bit also reflects DWC_USB3_EN_OTG
768 */
769 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
770 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
771 /*
772 * No OTG support in the HW core.
773 * We return 0 to indicate no error, since this is acceptable
774 * situation, just continue probe the dwc3 driver without otg.
775 */
776 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
777 return 0;
778 }
779
780 /* Allocate and init otg instance */
781 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
782 if (!dotg) {
783 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
784 return -ENOMEM;
785 }
786
Manu Gautam17206c22012-06-21 10:17:53 +0530787 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
788 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
789 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200790 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530791 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200792 ret = -ENODEV;
793 goto err1;
794 }
795
796 dotg->regs = dwc->regs;
797
798 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
799 dotg->otg.set_host = dwc3_otg_set_host;
800
801 /* This reference is used by dwc3 modules for checking otg existance */
802 dwc->dotg = dotg;
803
804 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
805 if (!dotg->otg.phy) {
806 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
807 ret = -ENOMEM;
808 goto err1;
809 }
810
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530811 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200812 dotg->otg.phy->otg = &dotg->otg;
813 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530814 dotg->otg.phy->set_power = dwc3_otg_set_power;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200815
816 ret = usb_set_transceiver(dotg->otg.phy);
817 if (ret) {
818 dev_err(dotg->otg.phy->dev,
819 "%s: failed to set transceiver, already exists\n",
820 __func__);
821 goto err2;
822 }
823
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200824 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
825
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530826 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200827 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
828
829 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
830 "dwc3_otg", dotg);
831 if (ret) {
832 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
833 dotg->irq, ret);
834 goto err3;
835 }
836
Manu Gautamb5067272012-07-02 09:53:41 +0530837 pm_runtime_get(dwc->dev);
838
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200839 return 0;
840
841err3:
842 cancel_work_sync(&dotg->sm_work);
843 usb_set_transceiver(NULL);
844err2:
845 kfree(dotg->otg.phy);
846err1:
847 dwc->dotg = NULL;
848 kfree(dotg);
849
850 return ret;
851}
852
853/**
854 * dwc3_otg_exit
855 * @dwc: Pointer to out controller context structure
856 *
857 * Returns 0 on success otherwise negative errno.
858 */
859void dwc3_otg_exit(struct dwc3 *dwc)
860{
861 struct dwc3_otg *dotg = dwc->dotg;
862
863 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
864 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530865 if (dotg->charger)
866 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200867 cancel_work_sync(&dotg->sm_work);
868 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530869 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200870 free_irq(dotg->irq, dotg);
871 kfree(dotg->otg.phy);
872 kfree(dotg);
873 dwc->dotg = NULL;
874 }
875}