blob: 361aa321f091c12d05c588c3b948bfbe2597dd5c [file] [log] [blame]
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001/**
2 * dwc3_otg.c - DesignWare USB3 DRD Controller OTG
3 *
Jack Pham0fc12332012-11-19 13:14:22 -08004 * Copyright (c) 2012, 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
Manu Gautamf1fceddf2012-10-12 14:02:50 +053058/**
59 * dwc3_otg_set_host_power - Enable port power control for host operation
60 *
61 * This function enables the OTG Port Power required to operate in Host mode
62 * This function should be called only after XHCI driver has set the port
63 * power in PORTSC register.
64 *
65 * @w: Pointer to the dwc3 otg struct
66 */
67void dwc3_otg_set_host_power(struct dwc3_otg *dotg)
68{
69 u32 osts;
70
71 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
72 if (!(osts & 0x8))
73 dev_err(dotg->dwc->dev, "%s: xHCIPrtPower not set\n", __func__);
74
75 dwc3_writel(dotg->regs, DWC3_OCTL, DWC3_OTG_OCTL_PRTPWRCTL);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020076}
77
78/**
79 * dwc3_otg_set_peripheral_regs - reset dwc3 otg registers to peripheral operation.
80 *
81 * This function sets the OTG registers to work in B-Device peripheral mode.
82 * This function should be called just before entering to B-Device mode.
83 *
84 * @w: Pointer to the dwc3 otg workqueue.
85 */
86static void dwc3_otg_set_peripheral_regs(struct dwc3_otg *dotg)
87{
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053088 u32 reg;
89 struct dwc3 *dwc = dotg->dwc;
90 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020091
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +053092 if (ext_xceiv && !ext_xceiv->otg_capability) {
93 /* Set OCTL[6](PeriMode) to 1 (peripheral) */
94 reg = dwc3_readl(dotg->regs, DWC3_OCTL);
95 reg |= DWC3_OTG_OCTL_PERIMODE;
96 dwc3_writel(dotg->regs, DWC3_OCTL, reg);
97 /*
98 * TODO: add more OTG registers writes for PERIPHERAL mode here,
99 * see figure 12-19 B-device flow in dwc3 Synopsis spec
100 */
101 } else {
102 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
103 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
104 reg |= DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
105 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
106 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200107}
108
109/**
110 * dwc3_otg_start_host - helper function for starting/stoping the host controller driver.
111 *
112 * @otg: Pointer to the otg_transceiver structure.
113 * @on: start / stop the host controller driver.
114 *
115 * Returns 0 on success otherwise negative errno.
116 */
117static int dwc3_otg_start_host(struct usb_otg *otg, int on)
118{
119 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530120 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
Manu Gautam61721592012-11-06 18:09:39 +0530121 struct dwc3 *dwc = dotg->dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200122 int ret = 0;
123
Manu Gautam61721592012-11-06 18:09:39 +0530124 if (!dwc->xhci)
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200125 return -EINVAL;
126
Manu Gautam61721592012-11-06 18:09:39 +0530127 if (!dotg->vbus_otg) {
128 dotg->vbus_otg = devm_regulator_get(dwc->dev->parent,
129 "vbus_dwc3");
130 if (IS_ERR(dotg->vbus_otg)) {
131 dev_err(dwc->dev, "Failed to get vbus regulator\n");
132 ret = PTR_ERR(dotg->vbus_otg);
133 dotg->vbus_otg = 0;
134 return ret;
135 }
136 }
137
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200138 if (on) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530139 dev_dbg(otg->phy->dev, "%s: turn on host\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200140
141 /*
142 * This should be revisited for more testing post-silicon.
143 * In worst case we may need to disconnect the root hub
144 * before stopping the controller so that it does not
145 * interfere with runtime pm/system pm.
146 * We can also consider registering and unregistering xhci
147 * platform device. It is almost similar to add_hcd and
148 * remove_hcd, But we may not use standard set_host method
149 * anymore.
150 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530151 dwc3_otg_set_host_regs(dotg);
Manu Gautam61721592012-11-06 18:09:39 +0530152 ret = platform_device_add(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200153 if (ret) {
154 dev_err(otg->phy->dev,
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530155 "%s: failed to add XHCI pdev ret=%d\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200156 __func__, ret);
157 return ret;
158 }
159
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530160 dwc3_otg_notify_host_mode(otg, on);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530161 ret = regulator_enable(dotg->vbus_otg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200162 if (ret) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530163 dev_err(otg->phy->dev, "unable to enable vbus_otg\n");
Manu Gautam61721592012-11-06 18:09:39 +0530164 platform_device_del(dwc->xhci);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200165 return ret;
166 }
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530167
168 /* re-init OTG EVTEN register as XHCI reset clears it */
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530169 if (ext_xceiv && !ext_xceiv->otg_capability)
170 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200171 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530172 dev_dbg(otg->phy->dev, "%s: turn off host\n", __func__);
173
Manu Gautam61721592012-11-06 18:09:39 +0530174 platform_device_del(dwc->xhci);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530175
176 ret = regulator_disable(dotg->vbus_otg);
177 if (ret) {
178 dev_err(otg->phy->dev, "unable to disable vbus_otg\n");
179 return ret;
180 }
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530181 dwc3_otg_notify_host_mode(otg, on);
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530182
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530183 /* Do block reset for Host <-> peripheral switching to work */
184 if (ext_xceiv && ext_xceiv->otg_capability &&
185 ext_xceiv->ext_block_reset)
186 ext_xceiv->ext_block_reset();
187
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530188 /* re-init core and OTG register as XHCI reset clears it */
Manu Gautam61721592012-11-06 18:09:39 +0530189 dwc3_post_host_reset_core_init(dwc);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530190 if (ext_xceiv && !ext_xceiv->otg_capability)
191 dwc3_otg_reset(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200192 }
193
194 return 0;
195}
196
197/**
198 * dwc3_otg_set_host - bind/unbind the host controller driver.
199 *
200 * @otg: Pointer to the otg_transceiver structure.
201 * @host: Pointer to the usb_bus structure.
202 *
203 * Returns 0 on success otherwise negative errno.
204 */
205static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
206{
207 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
208
209 if (host) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530210 dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200211 __func__, host->bus_name);
212 otg->host = host;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200213 /*
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530214 * Though XHCI power would be set by now, but some delay is
215 * required for XHCI controller before setting OTG Port Power
216 * TODO: Tune this delay
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200217 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530218 msleep(300);
219 dwc3_otg_set_host_power(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200220 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530221 otg->host = NULL;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200222 }
223
224 return 0;
225}
226
227/**
228 * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
229 *
230 * @otg: Pointer to the otg_transceiver structure.
231 * @gadget: pointer to the usb_gadget structure.
232 *
233 * Returns 0 on success otherwise negative errno.
234 */
235static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
236{
237 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
238
239 if (!otg->gadget)
240 return -EINVAL;
241
242 if (on) {
243 dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
244 __func__, otg->gadget->name);
245 dwc3_otg_set_peripheral_regs(dotg);
246 usb_gadget_vbus_connect(otg->gadget);
247 } else {
248 dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
249 __func__, otg->gadget->name);
250 usb_gadget_vbus_disconnect(otg->gadget);
251 }
252
253 return 0;
254}
255
256/**
257 * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
258 *
259 * @otg: Pointer to the otg_transceiver structure.
260 * @gadget: pointer to the usb_gadget structure.
261 *
262 * Returns 0 on success otherwise negative errno.
263 */
264static int dwc3_otg_set_peripheral(struct usb_otg *otg,
265 struct usb_gadget *gadget)
266{
267 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
268
269 if (gadget) {
270 dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
271 __func__, gadget->name);
272 otg->gadget = gadget;
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530273 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200274 } else {
275 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
276 dwc3_otg_start_peripheral(otg, 0);
277 otg->gadget = NULL;
278 otg->phy->state = OTG_STATE_UNDEFINED;
279 schedule_work(&dotg->sm_work);
280 } else {
281 otg->gadget = NULL;
282 }
283 }
284
285 return 0;
286}
287
288/**
Manu Gautam8c642812012-06-07 10:35:10 +0530289 * dwc3_ext_chg_det_done - callback to handle charger detection completion
290 * @otg: Pointer to the otg transceiver structure
291 * @charger: Pointer to the external charger structure
292 *
293 * Returns 0 on success
294 */
295static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
296{
297 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
298
299 /*
300 * Ignore chg_detection notification if BSV has gone off by this time.
301 * STOP chg_det as part of !BSV handling would reset the chg_det flags
302 */
303 if (test_bit(B_SESS_VLD, &dotg->inputs))
304 schedule_work(&dotg->sm_work);
305}
306
307/**
308 * dwc3_set_charger - bind/unbind external charger driver
309 * @otg: Pointer to the otg transceiver structure
310 * @charger: Pointer to the external charger structure
311 *
312 * Returns 0 on success
313 */
314int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
315{
316 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
317
318 dotg->charger = charger;
319 if (charger)
320 charger->notify_detection_complete = dwc3_ext_chg_det_done;
321
322 return 0;
323}
324
Manu Gautamb5067272012-07-02 09:53:41 +0530325/**
326 * dwc3_ext_event_notify - callback to handle events from external transceiver
327 * @otg: Pointer to the otg transceiver structure
328 * @event: Event reported by transceiver
329 *
330 * Returns 0 on success
331 */
332static void dwc3_ext_event_notify(struct usb_otg *otg,
333 enum dwc3_ext_events event)
334{
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530335 static bool init;
Manu Gautamb5067272012-07-02 09:53:41 +0530336 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
337 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
338 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530339 int ret = 0;
Manu Gautamb5067272012-07-02 09:53:41 +0530340
341 if (event == DWC3_EVENT_PHY_RESUME) {
342 if (!pm_runtime_status_suspended(phy->dev)) {
343 dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
344 } else {
345 dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
346 /* ext_xceiver would have taken h/w out of LPM by now */
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530347 ret = pm_runtime_get(phy->dev);
348 if (ret == -EACCES) {
349 /* pm_runtime_get may fail during system
350 resume with -EACCES error */
351 pm_runtime_disable(phy->dev);
352 pm_runtime_set_active(phy->dev);
353 pm_runtime_enable(phy->dev);
354 } else if (ret < 0) {
355 dev_warn(phy->dev, "pm_runtime_get failed!\n");
356 }
Manu Gautamb5067272012-07-02 09:53:41 +0530357 }
Manu Gautam377821c2012-09-28 16:53:24 +0530358 } else if (event == DWC3_EVENT_XCEIV_STATE) {
Jack Pham0fc12332012-11-19 13:14:22 -0800359 if (ext_xceiv->id == DWC3_ID_FLOAT) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530360 dev_dbg(phy->dev, "XCVR: ID set\n");
361 set_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800362 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530363 dev_dbg(phy->dev, "XCVR: ID clear\n");
364 clear_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800365 }
Manu Gautam377821c2012-09-28 16:53:24 +0530366
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530367 if (ext_xceiv->bsv) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530368 dev_dbg(phy->dev, "XCVR: BSV set\n");
369 set_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530370 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530371 dev_dbg(phy->dev, "XCVR: BSV clear\n");
372 clear_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530373 }
Manu Gautam377821c2012-09-28 16:53:24 +0530374
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530375 if (!init) {
376 init = true;
377 complete(&dotg->dwc3_xcvr_vbus_init);
378 dev_dbg(phy->dev, "XCVR: BSV init complete\n");
379 return;
380 }
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530381
382 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530383 }
Manu Gautamb5067272012-07-02 09:53:41 +0530384}
385
386/**
387 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
388 * @otg: Pointer to the otg transceiver structure
389 * @ext_xceiv: Pointer to the external transceiver struccture
390 *
391 * Returns 0 on success
392 */
393int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
394{
395 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
396
397 dotg->ext_xceiv = ext_xceiv;
398 if (ext_xceiv)
399 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
400
401 return 0;
402}
403
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530404static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
405{
406 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
407
408 if (!dotg->psy) {
409 dev_err(otg->phy->dev, "no usb power supply registered\n");
410 return;
411 }
412
413 if (host_mode)
414 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
415 else
416 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
417}
418
419static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
420{
421 static int power_supply_type;
422 struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
423
424
Manu Gautam6c0ff032012-11-02 14:55:35 +0530425 if (!dotg->psy || !dotg->charger) {
426 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530427 return 0;
428 }
429
Manu Gautam6c0ff032012-11-02 14:55:35 +0530430 if (dotg->charger->charging_disabled)
431 return 0;
432
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530433 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
434 power_supply_type = POWER_SUPPLY_TYPE_USB;
435 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
436 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
437 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER)
438 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
439 else
440 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
441
442 power_supply_set_supply_type(dotg->psy, power_supply_type);
443
444 if (dotg->charger->max_power == mA)
445 return 0;
446
447 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
448
449 if (dotg->charger->max_power <= 2 && mA > 2) {
450 /* Enable charging */
451 if (power_supply_set_online(dotg->psy, true))
452 goto psy_error;
453 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
454 goto psy_error;
455 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
456 /* Disable charging */
457 if (power_supply_set_online(dotg->psy, false))
458 goto psy_error;
459 /* Set max current limit */
460 if (power_supply_set_current_limit(dotg->psy, 0))
461 goto psy_error;
462 }
463
464 power_supply_changed(dotg->psy);
465 dotg->charger->max_power = mA;
466 return 0;
467
468psy_error:
469 dev_dbg(phy->dev, "power supply error when setting property\n");
470 return -ENXIO;
471}
472
Manu Gautam8c642812012-06-07 10:35:10 +0530473/* IRQs which OTG driver is interested in handling */
474#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
475 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
476
477/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200478 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
479 * @_dotg: Pointer to out controller context structure
480 *
481 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
482 */
483static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
484{
485 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530486 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200487 int ret = IRQ_NONE;
488 int handled_irqs = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530489 struct usb_phy *phy = dotg->otg.phy;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200490
491 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
492
Manu Gautam8c642812012-06-07 10:35:10 +0530493 if (!(oevt_reg & DWC3_OEVT_MASK))
494 return IRQ_NONE;
495
496 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
497
498 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
499 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200500 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530501 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200502 * function, switch from A to B or from B to A.
503 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200504
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530505 if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {
506 if (osts & DWC3_OTG_OSTS_CONIDSTS) {
507 dev_dbg(phy->dev, "ID set\n");
508 set_bit(ID, &dotg->inputs);
509 } else {
510 dev_dbg(phy->dev, "ID clear\n");
511 clear_bit(ID, &dotg->inputs);
512 }
513 handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;
514 }
Manu Gautam8c642812012-06-07 10:35:10 +0530515
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530516 if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {
517 if (osts & DWC3_OTG_OSTS_BSESVALID) {
518 dev_dbg(phy->dev, "BSV set\n");
519 set_bit(B_SESS_VLD, &dotg->inputs);
520 } else {
521 dev_dbg(phy->dev, "BSV clear\n");
522 clear_bit(B_SESS_VLD, &dotg->inputs);
523 }
524 handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;
525 }
Manu Gautam8c642812012-06-07 10:35:10 +0530526
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200527 schedule_work(&dotg->sm_work);
528
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200529 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530530
531 /* Clear the interrupts we handled */
532 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200533 }
534
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200535 return ret;
536}
537
538/**
Manu Gautam8c642812012-06-07 10:35:10 +0530539 * dwc3_otg_init_sm - initialize OTG statemachine input
540 * @dotg: Pointer to the dwc3_otg structure
541 *
542 */
543void dwc3_otg_init_sm(struct dwc3_otg *dotg)
544{
545 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
546 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530547 struct dwc3_ext_xceiv *ext_xceiv;
548 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530549
550 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
551
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530552 /*
553 * VBUS initial state is reported after PMIC
554 * driver initialization. Wait for it.
555 */
556 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
557 if (!ret)
558 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam8c642812012-06-07 10:35:10 +0530559
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530560 ext_xceiv = dotg->ext_xceiv;
561 dwc3_otg_reset(dotg);
562 if (ext_xceiv && !ext_xceiv->otg_capability) {
563 if (osts & DWC3_OTG_OSTS_CONIDSTS)
564 set_bit(ID, &dotg->inputs);
565 else
566 clear_bit(ID, &dotg->inputs);
567
568 if (osts & DWC3_OTG_OSTS_BSESVALID)
569 set_bit(B_SESS_VLD, &dotg->inputs);
570 else
571 clear_bit(B_SESS_VLD, &dotg->inputs);
572 }
Manu Gautam8c642812012-06-07 10:35:10 +0530573}
574
575/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200576 * dwc3_otg_sm_work - workqueue function.
577 *
578 * @w: Pointer to the dwc3 otg workqueue
579 *
580 * NOTE: After any change in phy->state,
581 * we must reschdule the state machine.
582 */
583static void dwc3_otg_sm_work(struct work_struct *w)
584{
585 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
586 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530587 struct dwc3_charger *charger = dotg->charger;
588 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200589
Manu Gautamb5067272012-07-02 09:53:41 +0530590 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200591 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
592
593 /* Check OTG state */
594 switch (phy->state) {
595 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530596 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530597 if (!dotg->psy) {
598 dotg->psy = power_supply_get_by_name("usb");
599
600 if (!dotg->psy)
601 dev_err(phy->dev,
602 "couldn't get usb power supply\n");
603 }
604
Manu Gautam8c642812012-06-07 10:35:10 +0530605 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530606 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530607 dev_dbg(phy->dev, "!id\n");
608 phy->state = OTG_STATE_A_IDLE;
609 work = 1;
610 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
611 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200612 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530613 work = 1;
614 } else {
615 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530616 dev_dbg(phy->dev, "No device, trying to suspend\n");
617 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200618 }
619 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530620
621 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530622 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530623 dev_dbg(phy->dev, "!id\n");
624 phy->state = OTG_STATE_A_IDLE;
625 work = 1;
626 if (charger) {
627 if (charger->chg_type == DWC3_INVALID_CHARGER)
628 charger->start_detection(dotg->charger,
629 false);
630 else
631 charger->chg_type =
632 DWC3_INVALID_CHARGER;
633 }
634 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
635 dev_dbg(phy->dev, "b_sess_vld\n");
636 if (charger) {
637 /* Has charger been detected? If no detect it */
638 switch (charger->chg_type) {
639 case DWC3_DCP_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530640 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530641 dwc3_otg_set_power(phy,
642 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530643 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530644 break;
645 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530646 dwc3_otg_set_power(phy,
647 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530648 dwc3_otg_start_peripheral(&dotg->otg,
649 1);
650 phy->state = OTG_STATE_B_PERIPHERAL;
651 work = 1;
652 break;
653 case DWC3_SDP_CHARGER:
654 dwc3_otg_start_peripheral(&dotg->otg,
655 1);
656 phy->state = OTG_STATE_B_PERIPHERAL;
657 work = 1;
658 break;
659 default:
660 dev_dbg(phy->dev, "chg_det started\n");
661 charger->start_detection(charger, true);
662 break;
663 }
664 } else {
665 /* no charger registered, start peripheral */
666 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
667 /*
668 * Probably set_peripheral not called
669 * yet. We will re-try as soon as it
670 * will be called
671 */
Manu Gautamb5067272012-07-02 09:53:41 +0530672 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530673 "unable to start B-device\n");
674 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530675 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530676 return;
677 }
678 }
679 } else {
Manu Gautam98013c22012-11-20 17:42:42 +0530680 if (charger)
681 charger->start_detection(dotg->charger, false);
682
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530683 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530684 dev_dbg(phy->dev, "No device, trying to suspend\n");
685 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530686 }
687 break;
688
689 case OTG_STATE_B_PERIPHERAL:
690 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
691 !test_bit(ID, &dotg->inputs)) {
692 dev_dbg(phy->dev, "!id || !bsv\n");
693 dwc3_otg_start_peripheral(&dotg->otg, 0);
694 phy->state = OTG_STATE_B_IDLE;
695 if (charger)
696 charger->chg_type = DWC3_INVALID_CHARGER;
697 work = 1;
698 }
699 break;
700
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200701 case OTG_STATE_A_IDLE:
702 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530703 if (test_bit(ID, &dotg->inputs)) {
704 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200705 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530706 work = 1;
707 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530708 phy->state = OTG_STATE_A_HOST;
709 if (dwc3_otg_start_host(&dotg->otg, 1)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530710 /*
711 * Probably set_host was not called yet.
712 * We will re-try as soon as it will be called
713 */
Manu Gautamb5067272012-07-02 09:53:41 +0530714 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530715 "unable to start A-device\n");
716 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530717 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530718 return;
719 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200720 }
721 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530722
723 case OTG_STATE_A_HOST:
724 if (test_bit(ID, &dotg->inputs)) {
725 dev_dbg(phy->dev, "id\n");
726 dwc3_otg_start_host(&dotg->otg, 0);
727 phy->state = OTG_STATE_B_IDLE;
728 work = 1;
729 }
730 break;
731
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200732 default:
733 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
734
735 }
Manu Gautam8c642812012-06-07 10:35:10 +0530736
737 if (work)
738 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200739}
740
741
742/**
743 * dwc3_otg_reset - reset dwc3 otg registers.
744 *
745 * @w: Pointer to the dwc3 otg workqueue
746 */
747static void dwc3_otg_reset(struct dwc3_otg *dotg)
748{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530749 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530750 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
751
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200752 /*
753 * OCFG[2] - OTG-Version = 1
754 * OCFG[1] - HNPCap = 0
755 * OCFG[0] - SRPCap = 0
756 */
757 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
758
759 /*
760 * OCTL[6] - PeriMode = 1
761 * OCTL[5] - PrtPwrCtl = 0
762 * OCTL[4] - HNPReq = 0
763 * OCTL[3] - SesReq = 0
764 * OCTL[2] - TermSelDLPulse = 0
765 * OCTL[1] - DevSetHNPEn = 0
766 * OCTL[0] - HstSetHNPEn = 0
767 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530768 if (!once) {
769 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
770 once++;
771 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200772
773 /* Clear all otg events (interrupts) indications */
774 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
775
Manu Gautam8c642812012-06-07 10:35:10 +0530776 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530777 if (ext_xceiv && !ext_xceiv->otg_capability)
778 dwc3_writel(dotg->regs, DWC3_OEVTEN,
779 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
780 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200781}
782
783/**
784 * dwc3_otg_init - Initializes otg related registers
785 * @dwc: Pointer to out controller context structure
786 *
787 * Returns 0 on success otherwise negative errno.
788 */
789int dwc3_otg_init(struct dwc3 *dwc)
790{
791 u32 reg;
792 int ret = 0;
793 struct dwc3_otg *dotg;
794
795 dev_dbg(dwc->dev, "dwc3_otg_init\n");
796
797 /*
798 * GHWPARAMS6[10] bit is SRPSupport.
799 * This bit also reflects DWC_USB3_EN_OTG
800 */
801 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
802 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
803 /*
804 * No OTG support in the HW core.
805 * We return 0 to indicate no error, since this is acceptable
806 * situation, just continue probe the dwc3 driver without otg.
807 */
808 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
809 return 0;
810 }
811
812 /* Allocate and init otg instance */
813 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
814 if (!dotg) {
815 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
816 return -ENOMEM;
817 }
818
Manu Gautam17206c22012-06-21 10:17:53 +0530819 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
820 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
821 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200822 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530823 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200824 ret = -ENODEV;
825 goto err1;
826 }
827
828 dotg->regs = dwc->regs;
829
830 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
831 dotg->otg.set_host = dwc3_otg_set_host;
832
833 /* This reference is used by dwc3 modules for checking otg existance */
834 dwc->dotg = dotg;
835
836 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
837 if (!dotg->otg.phy) {
838 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
839 ret = -ENOMEM;
840 goto err1;
841 }
842
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530843 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200844 dotg->otg.phy->otg = &dotg->otg;
845 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530846 dotg->otg.phy->set_power = dwc3_otg_set_power;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200847
848 ret = usb_set_transceiver(dotg->otg.phy);
849 if (ret) {
850 dev_err(dotg->otg.phy->dev,
851 "%s: failed to set transceiver, already exists\n",
852 __func__);
853 goto err2;
854 }
855
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200856 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
857
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530858 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200859 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
860
861 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
862 "dwc3_otg", dotg);
863 if (ret) {
864 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
865 dotg->irq, ret);
866 goto err3;
867 }
868
Manu Gautamb5067272012-07-02 09:53:41 +0530869 pm_runtime_get(dwc->dev);
870
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200871 return 0;
872
873err3:
874 cancel_work_sync(&dotg->sm_work);
875 usb_set_transceiver(NULL);
876err2:
877 kfree(dotg->otg.phy);
878err1:
879 dwc->dotg = NULL;
880 kfree(dotg);
881
882 return ret;
883}
884
885/**
886 * dwc3_otg_exit
887 * @dwc: Pointer to out controller context structure
888 *
889 * Returns 0 on success otherwise negative errno.
890 */
891void dwc3_otg_exit(struct dwc3 *dwc)
892{
893 struct dwc3_otg *dotg = dwc->dotg;
894
895 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
896 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530897 if (dotg->charger)
898 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200899 cancel_work_sync(&dotg->sm_work);
900 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530901 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200902 free_irq(dotg->irq, dotg);
903 kfree(dotg->otg.phy);
904 kfree(dotg);
905 dwc->dotg = NULL;
906 }
907}