blob: 41dd144ba1d7b8dd34122d8ece5efe3fa49ab725 [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
396 if (!dotg->psy) {
397 dev_err(phy->dev, "no usb power supply registered\n");
398 return 0;
399 }
400
401 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
402 power_supply_type = POWER_SUPPLY_TYPE_USB;
403 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
404 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
405 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER)
406 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
407 else
408 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
409
410 power_supply_set_supply_type(dotg->psy, power_supply_type);
411
412 if (dotg->charger->max_power == mA)
413 return 0;
414
415 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
416
417 if (dotg->charger->max_power <= 2 && mA > 2) {
418 /* Enable charging */
419 if (power_supply_set_online(dotg->psy, true))
420 goto psy_error;
421 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
422 goto psy_error;
423 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
424 /* Disable charging */
425 if (power_supply_set_online(dotg->psy, false))
426 goto psy_error;
427 /* Set max current limit */
428 if (power_supply_set_current_limit(dotg->psy, 0))
429 goto psy_error;
430 }
431
432 power_supply_changed(dotg->psy);
433 dotg->charger->max_power = mA;
434 return 0;
435
436psy_error:
437 dev_dbg(phy->dev, "power supply error when setting property\n");
438 return -ENXIO;
439}
440
Manu Gautam8c642812012-06-07 10:35:10 +0530441/* IRQs which OTG driver is interested in handling */
442#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
443 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
444
445/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200446 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
447 * @_dotg: Pointer to out controller context structure
448 *
449 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
450 */
451static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
452{
453 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530454 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200455 int ret = IRQ_NONE;
456 int handled_irqs = 0;
457
458 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
459
Manu Gautam8c642812012-06-07 10:35:10 +0530460 if (!(oevt_reg & DWC3_OEVT_MASK))
461 return IRQ_NONE;
462
463 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
464
465 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
466 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200467 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530468 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200469 * function, switch from A to B or from B to A.
470 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200471
Manu Gautam8c642812012-06-07 10:35:10 +0530472 if (osts & DWC3_OTG_OSTS_CONIDSTS)
473 set_bit(ID, &dotg->inputs);
474 else
475 clear_bit(ID, &dotg->inputs);
476
477 if (osts & DWC3_OTG_OSTS_BSESVALID)
478 set_bit(B_SESS_VLD, &dotg->inputs);
479 else
480 clear_bit(B_SESS_VLD, &dotg->inputs);
481
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200482 schedule_work(&dotg->sm_work);
483
Manu Gautam8c642812012-06-07 10:35:10 +0530484 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ?
485 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT : 0;
486 handled_irqs |= (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) ?
487 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT : 0;
488
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200489 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530490
491 /* Clear the interrupts we handled */
492 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200493 }
494
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200495 return ret;
496}
497
498/**
Manu Gautam8c642812012-06-07 10:35:10 +0530499 * dwc3_otg_init_sm - initialize OTG statemachine input
500 * @dotg: Pointer to the dwc3_otg structure
501 *
502 */
503void dwc3_otg_init_sm(struct dwc3_otg *dotg)
504{
505 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
506 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530507 struct dwc3_ext_xceiv *ext_xceiv;
508 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530509
510 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
511
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530512 /*
513 * VBUS initial state is reported after PMIC
514 * driver initialization. Wait for it.
515 */
516 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
517 if (!ret)
518 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam8c642812012-06-07 10:35:10 +0530519
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530520 ext_xceiv = dotg->ext_xceiv;
521 dwc3_otg_reset(dotg);
522 if (ext_xceiv && !ext_xceiv->otg_capability) {
523 if (osts & DWC3_OTG_OSTS_CONIDSTS)
524 set_bit(ID, &dotg->inputs);
525 else
526 clear_bit(ID, &dotg->inputs);
527
528 if (osts & DWC3_OTG_OSTS_BSESVALID)
529 set_bit(B_SESS_VLD, &dotg->inputs);
530 else
531 clear_bit(B_SESS_VLD, &dotg->inputs);
532 }
Manu Gautam8c642812012-06-07 10:35:10 +0530533}
534
535/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200536 * dwc3_otg_sm_work - workqueue function.
537 *
538 * @w: Pointer to the dwc3 otg workqueue
539 *
540 * NOTE: After any change in phy->state,
541 * we must reschdule the state machine.
542 */
543static void dwc3_otg_sm_work(struct work_struct *w)
544{
545 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
546 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530547 struct dwc3_charger *charger = dotg->charger;
548 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200549
Manu Gautamb5067272012-07-02 09:53:41 +0530550 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200551 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
552
553 /* Check OTG state */
554 switch (phy->state) {
555 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530556 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530557 if (!dotg->psy) {
558 dotg->psy = power_supply_get_by_name("usb");
559
560 if (!dotg->psy)
561 dev_err(phy->dev,
562 "couldn't get usb power supply\n");
563 }
564
Manu Gautam8c642812012-06-07 10:35:10 +0530565 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530566 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530567 dev_dbg(phy->dev, "!id\n");
568 phy->state = OTG_STATE_A_IDLE;
569 work = 1;
570 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
571 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200572 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530573 work = 1;
574 } else {
575 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530576 dev_dbg(phy->dev, "No device, trying to suspend\n");
577 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200578 }
579 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530580
581 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530582 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530583 dev_dbg(phy->dev, "!id\n");
584 phy->state = OTG_STATE_A_IDLE;
585 work = 1;
586 if (charger) {
587 if (charger->chg_type == DWC3_INVALID_CHARGER)
588 charger->start_detection(dotg->charger,
589 false);
590 else
591 charger->chg_type =
592 DWC3_INVALID_CHARGER;
593 }
594 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
595 dev_dbg(phy->dev, "b_sess_vld\n");
596 if (charger) {
597 /* Has charger been detected? If no detect it */
598 switch (charger->chg_type) {
599 case DWC3_DCP_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530600 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530601 dwc3_otg_set_power(phy,
602 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530603 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530604 break;
605 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530606 dwc3_otg_set_power(phy,
607 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530608 dwc3_otg_start_peripheral(&dotg->otg,
609 1);
610 phy->state = OTG_STATE_B_PERIPHERAL;
611 work = 1;
612 break;
613 case DWC3_SDP_CHARGER:
614 dwc3_otg_start_peripheral(&dotg->otg,
615 1);
616 phy->state = OTG_STATE_B_PERIPHERAL;
617 work = 1;
618 break;
619 default:
620 dev_dbg(phy->dev, "chg_det started\n");
621 charger->start_detection(charger, true);
622 break;
623 }
624 } else {
625 /* no charger registered, start peripheral */
626 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
627 /*
628 * Probably set_peripheral not called
629 * yet. We will re-try as soon as it
630 * will be called
631 */
Manu Gautamb5067272012-07-02 09:53:41 +0530632 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530633 "unable to start B-device\n");
634 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530635 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530636 return;
637 }
638 }
639 } else {
640 if (charger) {
641 if (charger->chg_type == DWC3_INVALID_CHARGER)
642 charger->start_detection(dotg->charger,
643 false);
644 else
645 charger->chg_type =
646 DWC3_INVALID_CHARGER;
647 }
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530648 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530649 dev_dbg(phy->dev, "No device, trying to suspend\n");
650 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530651 }
652 break;
653
654 case OTG_STATE_B_PERIPHERAL:
655 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
656 !test_bit(ID, &dotg->inputs)) {
657 dev_dbg(phy->dev, "!id || !bsv\n");
658 dwc3_otg_start_peripheral(&dotg->otg, 0);
659 phy->state = OTG_STATE_B_IDLE;
660 if (charger)
661 charger->chg_type = DWC3_INVALID_CHARGER;
662 work = 1;
663 }
664 break;
665
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200666 case OTG_STATE_A_IDLE:
667 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530668 if (test_bit(ID, &dotg->inputs)) {
669 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200670 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530671 work = 1;
672 } else {
673 if (dwc3_otg_start_host(&dotg->otg, 1)) {
674 /*
675 * Probably set_host was not called yet.
676 * We will re-try as soon as it will be called
677 */
Manu Gautamb5067272012-07-02 09:53:41 +0530678 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530679 "unable to start A-device\n");
680 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530681 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530682 return;
683 }
684 phy->state = OTG_STATE_A_HOST;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200685 }
686 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530687
688 case OTG_STATE_A_HOST:
689 if (test_bit(ID, &dotg->inputs)) {
690 dev_dbg(phy->dev, "id\n");
691 dwc3_otg_start_host(&dotg->otg, 0);
692 phy->state = OTG_STATE_B_IDLE;
693 work = 1;
694 }
695 break;
696
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200697 default:
698 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
699
700 }
Manu Gautam8c642812012-06-07 10:35:10 +0530701
702 if (work)
703 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200704}
705
706
707/**
708 * dwc3_otg_reset - reset dwc3 otg registers.
709 *
710 * @w: Pointer to the dwc3 otg workqueue
711 */
712static void dwc3_otg_reset(struct dwc3_otg *dotg)
713{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530714 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530715 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
716
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200717 /*
718 * OCFG[2] - OTG-Version = 1
719 * OCFG[1] - HNPCap = 0
720 * OCFG[0] - SRPCap = 0
721 */
722 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
723
724 /*
725 * OCTL[6] - PeriMode = 1
726 * OCTL[5] - PrtPwrCtl = 0
727 * OCTL[4] - HNPReq = 0
728 * OCTL[3] - SesReq = 0
729 * OCTL[2] - TermSelDLPulse = 0
730 * OCTL[1] - DevSetHNPEn = 0
731 * OCTL[0] - HstSetHNPEn = 0
732 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530733 if (!once) {
734 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
735 once++;
736 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200737
738 /* Clear all otg events (interrupts) indications */
739 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
740
Manu Gautam8c642812012-06-07 10:35:10 +0530741 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530742 if (ext_xceiv && !ext_xceiv->otg_capability)
743 dwc3_writel(dotg->regs, DWC3_OEVTEN,
744 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
745 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200746}
747
748/**
749 * dwc3_otg_init - Initializes otg related registers
750 * @dwc: Pointer to out controller context structure
751 *
752 * Returns 0 on success otherwise negative errno.
753 */
754int dwc3_otg_init(struct dwc3 *dwc)
755{
756 u32 reg;
757 int ret = 0;
758 struct dwc3_otg *dotg;
759
760 dev_dbg(dwc->dev, "dwc3_otg_init\n");
761
762 /*
763 * GHWPARAMS6[10] bit is SRPSupport.
764 * This bit also reflects DWC_USB3_EN_OTG
765 */
766 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
767 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
768 /*
769 * No OTG support in the HW core.
770 * We return 0 to indicate no error, since this is acceptable
771 * situation, just continue probe the dwc3 driver without otg.
772 */
773 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
774 return 0;
775 }
776
777 /* Allocate and init otg instance */
778 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
779 if (!dotg) {
780 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
781 return -ENOMEM;
782 }
783
Manu Gautam17206c22012-06-21 10:17:53 +0530784 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
785 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
786 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200787 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530788 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200789 ret = -ENODEV;
790 goto err1;
791 }
792
793 dotg->regs = dwc->regs;
794
795 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
796 dotg->otg.set_host = dwc3_otg_set_host;
797
798 /* This reference is used by dwc3 modules for checking otg existance */
799 dwc->dotg = dotg;
800
801 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
802 if (!dotg->otg.phy) {
803 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
804 ret = -ENOMEM;
805 goto err1;
806 }
807
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530808 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200809 dotg->otg.phy->otg = &dotg->otg;
810 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530811 dotg->otg.phy->set_power = dwc3_otg_set_power;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200812
813 ret = usb_set_transceiver(dotg->otg.phy);
814 if (ret) {
815 dev_err(dotg->otg.phy->dev,
816 "%s: failed to set transceiver, already exists\n",
817 __func__);
818 goto err2;
819 }
820
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200821 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
822
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530823 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200824 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
825
826 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
827 "dwc3_otg", dotg);
828 if (ret) {
829 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
830 dotg->irq, ret);
831 goto err3;
832 }
833
Manu Gautamb5067272012-07-02 09:53:41 +0530834 pm_runtime_get(dwc->dev);
835
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200836 return 0;
837
838err3:
839 cancel_work_sync(&dotg->sm_work);
840 usb_set_transceiver(NULL);
841err2:
842 kfree(dotg->otg.phy);
843err1:
844 dwc->dotg = NULL;
845 kfree(dotg);
846
847 return ret;
848}
849
850/**
851 * dwc3_otg_exit
852 * @dwc: Pointer to out controller context structure
853 *
854 * Returns 0 on success otherwise negative errno.
855 */
856void dwc3_otg_exit(struct dwc3 *dwc)
857{
858 struct dwc3_otg *dotg = dwc->dotg;
859
860 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
861 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530862 if (dotg->charger)
863 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200864 cancel_work_sync(&dotg->sm_work);
865 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530866 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200867 free_irq(dotg->irq, dotg);
868 kfree(dotg->otg.phy);
869 kfree(dotg);
870 dwc->dotg = NULL;
871 }
872}