blob: eb879e3452e1a2941407a119fc4163a423312e4d [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);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200182 }
183
184 return 0;
185}
186
187/**
188 * dwc3_otg_set_host - bind/unbind the host controller driver.
189 *
190 * @otg: Pointer to the otg_transceiver structure.
191 * @host: Pointer to the usb_bus structure.
192 *
193 * Returns 0 on success otherwise negative errno.
194 */
195static int dwc3_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
196{
197 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
198
199 if (host) {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530200 dev_dbg(otg->phy->dev, "%s: set host %s, portpower\n",
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200201 __func__, host->bus_name);
202 otg->host = host;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200203 /*
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530204 * Though XHCI power would be set by now, but some delay is
205 * required for XHCI controller before setting OTG Port Power
206 * TODO: Tune this delay
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200207 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530208 msleep(300);
209 dwc3_otg_set_host_power(dotg);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200210 } else {
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530211 otg->host = NULL;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200212 }
213
214 return 0;
215}
216
217/**
218 * dwc3_otg_start_peripheral - bind/unbind the peripheral controller.
219 *
220 * @otg: Pointer to the otg_transceiver structure.
221 * @gadget: pointer to the usb_gadget structure.
222 *
223 * Returns 0 on success otherwise negative errno.
224 */
225static int dwc3_otg_start_peripheral(struct usb_otg *otg, int on)
226{
227 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
Manu Gautama302f612012-12-18 17:33:06 +0530228 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
229 struct dwc3 *dwc = dotg->dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200230
231 if (!otg->gadget)
232 return -EINVAL;
233
234 if (on) {
235 dev_dbg(otg->phy->dev, "%s: turn on gadget %s\n",
236 __func__, otg->gadget->name);
Manu Gautama302f612012-12-18 17:33:06 +0530237
238 /*
239 * Hardware reset is required to support below scenarios:
240 * 1. Host <-> peripheral switching
241 * 2. Once an endpoint is configured in DBM (BAM) mode, it
242 * can be unconfigured only after RESET
243 */
244 if (ext_xceiv && ext_xceiv->otg_capability &&
245 ext_xceiv->ext_block_reset)
246 ext_xceiv->ext_block_reset();
247
248 /* re-init core and OTG registers as block reset clears these */
249 dwc3_post_host_reset_core_init(dwc);
250 if (ext_xceiv && !ext_xceiv->otg_capability)
251 dwc3_otg_reset(dotg);
252
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200253 dwc3_otg_set_peripheral_regs(dotg);
254 usb_gadget_vbus_connect(otg->gadget);
255 } else {
256 dev_dbg(otg->phy->dev, "%s: turn off gadget %s\n",
257 __func__, otg->gadget->name);
258 usb_gadget_vbus_disconnect(otg->gadget);
259 }
260
261 return 0;
262}
263
264/**
265 * dwc3_otg_set_peripheral - bind/unbind the peripheral controller driver.
266 *
267 * @otg: Pointer to the otg_transceiver structure.
268 * @gadget: pointer to the usb_gadget structure.
269 *
270 * Returns 0 on success otherwise negative errno.
271 */
272static int dwc3_otg_set_peripheral(struct usb_otg *otg,
273 struct usb_gadget *gadget)
274{
275 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
276
277 if (gadget) {
278 dev_dbg(otg->phy->dev, "%s: set gadget %s\n",
279 __func__, gadget->name);
280 otg->gadget = gadget;
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530281 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200282 } else {
283 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
284 dwc3_otg_start_peripheral(otg, 0);
285 otg->gadget = NULL;
286 otg->phy->state = OTG_STATE_UNDEFINED;
287 schedule_work(&dotg->sm_work);
288 } else {
289 otg->gadget = NULL;
290 }
291 }
292
293 return 0;
294}
295
296/**
Manu Gautam8c642812012-06-07 10:35:10 +0530297 * dwc3_ext_chg_det_done - callback to handle charger detection completion
298 * @otg: Pointer to the otg transceiver structure
299 * @charger: Pointer to the external charger structure
300 *
301 * Returns 0 on success
302 */
303static void dwc3_ext_chg_det_done(struct usb_otg *otg, struct dwc3_charger *chg)
304{
305 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
306
307 /*
308 * Ignore chg_detection notification if BSV has gone off by this time.
309 * STOP chg_det as part of !BSV handling would reset the chg_det flags
310 */
311 if (test_bit(B_SESS_VLD, &dotg->inputs))
312 schedule_work(&dotg->sm_work);
313}
314
315/**
316 * dwc3_set_charger - bind/unbind external charger driver
317 * @otg: Pointer to the otg transceiver structure
318 * @charger: Pointer to the external charger structure
319 *
320 * Returns 0 on success
321 */
322int dwc3_set_charger(struct usb_otg *otg, struct dwc3_charger *charger)
323{
324 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
325
326 dotg->charger = charger;
327 if (charger)
328 charger->notify_detection_complete = dwc3_ext_chg_det_done;
329
330 return 0;
331}
332
Manu Gautamb5067272012-07-02 09:53:41 +0530333/**
334 * dwc3_ext_event_notify - callback to handle events from external transceiver
335 * @otg: Pointer to the otg transceiver structure
336 * @event: Event reported by transceiver
337 *
338 * Returns 0 on success
339 */
340static void dwc3_ext_event_notify(struct usb_otg *otg,
341 enum dwc3_ext_events event)
342{
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530343 static bool init;
Manu Gautamb5067272012-07-02 09:53:41 +0530344 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
345 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
346 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530347 int ret = 0;
Manu Gautamb5067272012-07-02 09:53:41 +0530348
349 if (event == DWC3_EVENT_PHY_RESUME) {
350 if (!pm_runtime_status_suspended(phy->dev)) {
351 dev_warn(phy->dev, "PHY_RESUME event out of LPM!!!!\n");
352 } else {
353 dev_dbg(phy->dev, "ext PHY_RESUME event received\n");
354 /* ext_xceiver would have taken h/w out of LPM by now */
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530355 ret = pm_runtime_get(phy->dev);
356 if (ret == -EACCES) {
357 /* pm_runtime_get may fail during system
358 resume with -EACCES error */
359 pm_runtime_disable(phy->dev);
360 pm_runtime_set_active(phy->dev);
361 pm_runtime_enable(phy->dev);
362 } else if (ret < 0) {
363 dev_warn(phy->dev, "pm_runtime_get failed!\n");
364 }
Manu Gautamb5067272012-07-02 09:53:41 +0530365 }
Manu Gautam377821c2012-09-28 16:53:24 +0530366 } else if (event == DWC3_EVENT_XCEIV_STATE) {
Jack Pham0fc12332012-11-19 13:14:22 -0800367 if (ext_xceiv->id == DWC3_ID_FLOAT) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530368 dev_dbg(phy->dev, "XCVR: ID set\n");
369 set_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800370 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530371 dev_dbg(phy->dev, "XCVR: ID clear\n");
372 clear_bit(ID, &dotg->inputs);
Jack Pham0fc12332012-11-19 13:14:22 -0800373 }
Manu Gautam377821c2012-09-28 16:53:24 +0530374
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530375 if (ext_xceiv->bsv) {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530376 dev_dbg(phy->dev, "XCVR: BSV set\n");
377 set_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530378 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530379 dev_dbg(phy->dev, "XCVR: BSV clear\n");
380 clear_bit(B_SESS_VLD, &dotg->inputs);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530381 }
Manu Gautam377821c2012-09-28 16:53:24 +0530382
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530383 if (!init) {
384 init = true;
385 complete(&dotg->dwc3_xcvr_vbus_init);
386 dev_dbg(phy->dev, "XCVR: BSV init complete\n");
387 return;
388 }
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530389
390 schedule_work(&dotg->sm_work);
Manu Gautamb5067272012-07-02 09:53:41 +0530391 }
Manu Gautamb5067272012-07-02 09:53:41 +0530392}
393
394/**
395 * dwc3_set_ext_xceiv - bind/unbind external transceiver driver
396 * @otg: Pointer to the otg transceiver structure
397 * @ext_xceiv: Pointer to the external transceiver struccture
398 *
399 * Returns 0 on success
400 */
401int dwc3_set_ext_xceiv(struct usb_otg *otg, struct dwc3_ext_xceiv *ext_xceiv)
402{
403 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
404
405 dotg->ext_xceiv = ext_xceiv;
406 if (ext_xceiv)
407 ext_xceiv->notify_ext_events = dwc3_ext_event_notify;
408
409 return 0;
410}
411
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530412static void dwc3_otg_notify_host_mode(struct usb_otg *otg, int host_mode)
413{
414 struct dwc3_otg *dotg = container_of(otg, struct dwc3_otg, otg);
415
416 if (!dotg->psy) {
417 dev_err(otg->phy->dev, "no usb power supply registered\n");
418 return;
419 }
420
421 if (host_mode)
422 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_SYSTEM);
423 else
424 power_supply_set_scope(dotg->psy, POWER_SUPPLY_SCOPE_DEVICE);
425}
426
427static int dwc3_otg_set_power(struct usb_phy *phy, unsigned mA)
428{
429 static int power_supply_type;
430 struct dwc3_otg *dotg = container_of(phy->otg, struct dwc3_otg, otg);
431
432
Manu Gautam6c0ff032012-11-02 14:55:35 +0530433 if (!dotg->psy || !dotg->charger) {
434 dev_err(phy->dev, "no usb power supply/charger registered\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530435 return 0;
436 }
437
Manu Gautam6c0ff032012-11-02 14:55:35 +0530438 if (dotg->charger->charging_disabled)
439 return 0;
440
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530441 if (dotg->charger->chg_type == DWC3_SDP_CHARGER)
442 power_supply_type = POWER_SUPPLY_TYPE_USB;
443 else if (dotg->charger->chg_type == DWC3_CDP_CHARGER)
444 power_supply_type = POWER_SUPPLY_TYPE_USB_CDP;
445 else if (dotg->charger->chg_type == DWC3_DCP_CHARGER)
446 power_supply_type = POWER_SUPPLY_TYPE_USB_DCP;
447 else
448 power_supply_type = POWER_SUPPLY_TYPE_BATTERY;
449
450 power_supply_set_supply_type(dotg->psy, power_supply_type);
451
452 if (dotg->charger->max_power == mA)
453 return 0;
454
455 dev_info(phy->dev, "Avail curr from USB = %u\n", mA);
456
457 if (dotg->charger->max_power <= 2 && mA > 2) {
458 /* Enable charging */
459 if (power_supply_set_online(dotg->psy, true))
460 goto psy_error;
461 if (power_supply_set_current_limit(dotg->psy, 1000*mA))
462 goto psy_error;
463 } else if (dotg->charger->max_power > 0 && (mA == 0 || mA == 2)) {
464 /* Disable charging */
465 if (power_supply_set_online(dotg->psy, false))
466 goto psy_error;
467 /* Set max current limit */
468 if (power_supply_set_current_limit(dotg->psy, 0))
469 goto psy_error;
470 }
471
472 power_supply_changed(dotg->psy);
473 dotg->charger->max_power = mA;
474 return 0;
475
476psy_error:
477 dev_dbg(phy->dev, "power supply error when setting property\n");
478 return -ENXIO;
479}
480
Manu Gautam8c642812012-06-07 10:35:10 +0530481/* IRQs which OTG driver is interested in handling */
482#define DWC3_OEVT_MASK (DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT | \
483 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)
484
485/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200486 * dwc3_otg_interrupt - interrupt handler for dwc3 otg events.
487 * @_dotg: Pointer to out controller context structure
488 *
489 * Returns IRQ_HANDLED on success otherwise IRQ_NONE.
490 */
491static irqreturn_t dwc3_otg_interrupt(int irq, void *_dotg)
492{
493 struct dwc3_otg *dotg = (struct dwc3_otg *)_dotg;
Manu Gautam8c642812012-06-07 10:35:10 +0530494 u32 osts, oevt_reg;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200495 int ret = IRQ_NONE;
496 int handled_irqs = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530497 struct usb_phy *phy = dotg->otg.phy;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200498
499 oevt_reg = dwc3_readl(dotg->regs, DWC3_OEVT);
500
Manu Gautam8c642812012-06-07 10:35:10 +0530501 if (!(oevt_reg & DWC3_OEVT_MASK))
502 return IRQ_NONE;
503
504 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
505
506 if ((oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) ||
507 (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT)) {
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200508 /*
Manu Gautam8c642812012-06-07 10:35:10 +0530509 * ID sts has changed, set inputs later, in the workqueue
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200510 * function, switch from A to B or from B to A.
511 */
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200512
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530513 if (oevt_reg & DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT) {
514 if (osts & DWC3_OTG_OSTS_CONIDSTS) {
515 dev_dbg(phy->dev, "ID set\n");
516 set_bit(ID, &dotg->inputs);
517 } else {
518 dev_dbg(phy->dev, "ID clear\n");
519 clear_bit(ID, &dotg->inputs);
520 }
521 handled_irqs |= DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT;
522 }
Manu Gautam8c642812012-06-07 10:35:10 +0530523
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +0530524 if (oevt_reg & DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT) {
525 if (osts & DWC3_OTG_OSTS_BSESVALID) {
526 dev_dbg(phy->dev, "BSV set\n");
527 set_bit(B_SESS_VLD, &dotg->inputs);
528 } else {
529 dev_dbg(phy->dev, "BSV clear\n");
530 clear_bit(B_SESS_VLD, &dotg->inputs);
531 }
532 handled_irqs |= DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT;
533 }
Manu Gautam8c642812012-06-07 10:35:10 +0530534
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200535 schedule_work(&dotg->sm_work);
536
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200537 ret = IRQ_HANDLED;
Manu Gautam8c642812012-06-07 10:35:10 +0530538
539 /* Clear the interrupts we handled */
540 dwc3_writel(dotg->regs, DWC3_OEVT, handled_irqs);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200541 }
542
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200543 return ret;
544}
545
546/**
Manu Gautam8c642812012-06-07 10:35:10 +0530547 * dwc3_otg_init_sm - initialize OTG statemachine input
548 * @dotg: Pointer to the dwc3_otg structure
549 *
550 */
551void dwc3_otg_init_sm(struct dwc3_otg *dotg)
552{
553 u32 osts = dwc3_readl(dotg->regs, DWC3_OSTS);
554 struct usb_phy *phy = dotg->otg.phy;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530555 struct dwc3_ext_xceiv *ext_xceiv;
556 int ret;
Manu Gautam8c642812012-06-07 10:35:10 +0530557
558 dev_dbg(phy->dev, "Initialize OTG inputs, osts: 0x%x\n", osts);
559
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530560 /*
561 * VBUS initial state is reported after PMIC
562 * driver initialization. Wait for it.
563 */
564 ret = wait_for_completion_timeout(&dotg->dwc3_xcvr_vbus_init, HZ * 5);
565 if (!ret)
566 dev_err(phy->dev, "%s: completion timeout\n", __func__);
Manu Gautam8c642812012-06-07 10:35:10 +0530567
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530568 ext_xceiv = dotg->ext_xceiv;
569 dwc3_otg_reset(dotg);
570 if (ext_xceiv && !ext_xceiv->otg_capability) {
571 if (osts & DWC3_OTG_OSTS_CONIDSTS)
572 set_bit(ID, &dotg->inputs);
573 else
574 clear_bit(ID, &dotg->inputs);
575
576 if (osts & DWC3_OTG_OSTS_BSESVALID)
577 set_bit(B_SESS_VLD, &dotg->inputs);
578 else
579 clear_bit(B_SESS_VLD, &dotg->inputs);
580 }
Manu Gautam8c642812012-06-07 10:35:10 +0530581}
582
583/**
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200584 * dwc3_otg_sm_work - workqueue function.
585 *
586 * @w: Pointer to the dwc3 otg workqueue
587 *
588 * NOTE: After any change in phy->state,
589 * we must reschdule the state machine.
590 */
591static void dwc3_otg_sm_work(struct work_struct *w)
592{
593 struct dwc3_otg *dotg = container_of(w, struct dwc3_otg, sm_work);
594 struct usb_phy *phy = dotg->otg.phy;
Manu Gautam8c642812012-06-07 10:35:10 +0530595 struct dwc3_charger *charger = dotg->charger;
596 bool work = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200597
Manu Gautamb5067272012-07-02 09:53:41 +0530598 pm_runtime_resume(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200599 dev_dbg(phy->dev, "%s state\n", otg_state_string(phy->state));
600
601 /* Check OTG state */
602 switch (phy->state) {
603 case OTG_STATE_UNDEFINED:
Manu Gautam8c642812012-06-07 10:35:10 +0530604 dwc3_otg_init_sm(dotg);
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530605 if (!dotg->psy) {
606 dotg->psy = power_supply_get_by_name("usb");
607
608 if (!dotg->psy)
609 dev_err(phy->dev,
610 "couldn't get usb power supply\n");
611 }
612
Manu Gautam8c642812012-06-07 10:35:10 +0530613 /* Switch to A or B-Device according to ID / BSV */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530614 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530615 dev_dbg(phy->dev, "!id\n");
616 phy->state = OTG_STATE_A_IDLE;
617 work = 1;
618 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
619 dev_dbg(phy->dev, "b_sess_vld\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200620 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530621 work = 1;
622 } else {
623 phy->state = OTG_STATE_B_IDLE;
Manu Gautamb5067272012-07-02 09:53:41 +0530624 dev_dbg(phy->dev, "No device, trying to suspend\n");
625 pm_runtime_put_sync(phy->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200626 }
627 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530628
629 case OTG_STATE_B_IDLE:
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530630 if (!test_bit(ID, &dotg->inputs)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530631 dev_dbg(phy->dev, "!id\n");
632 phy->state = OTG_STATE_A_IDLE;
633 work = 1;
634 if (charger) {
635 if (charger->chg_type == DWC3_INVALID_CHARGER)
636 charger->start_detection(dotg->charger,
637 false);
638 else
639 charger->chg_type =
640 DWC3_INVALID_CHARGER;
641 }
642 } else if (test_bit(B_SESS_VLD, &dotg->inputs)) {
643 dev_dbg(phy->dev, "b_sess_vld\n");
644 if (charger) {
645 /* Has charger been detected? If no detect it */
646 switch (charger->chg_type) {
647 case DWC3_DCP_CHARGER:
Manu Gautamb5067272012-07-02 09:53:41 +0530648 dev_dbg(phy->dev, "lpm, DCP charger\n");
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530649 dwc3_otg_set_power(phy,
650 DWC3_IDEV_CHG_MAX);
Manu Gautamb5067272012-07-02 09:53:41 +0530651 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530652 break;
653 case DWC3_CDP_CHARGER:
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530654 dwc3_otg_set_power(phy,
655 DWC3_IDEV_CHG_MAX);
Manu Gautam8c642812012-06-07 10:35:10 +0530656 dwc3_otg_start_peripheral(&dotg->otg,
657 1);
658 phy->state = OTG_STATE_B_PERIPHERAL;
659 work = 1;
660 break;
661 case DWC3_SDP_CHARGER:
662 dwc3_otg_start_peripheral(&dotg->otg,
663 1);
664 phy->state = OTG_STATE_B_PERIPHERAL;
665 work = 1;
666 break;
667 default:
668 dev_dbg(phy->dev, "chg_det started\n");
669 charger->start_detection(charger, true);
670 break;
671 }
672 } else {
673 /* no charger registered, start peripheral */
674 if (dwc3_otg_start_peripheral(&dotg->otg, 1)) {
675 /*
676 * Probably set_peripheral not called
677 * yet. We will re-try as soon as it
678 * will be called
679 */
Manu Gautamb5067272012-07-02 09:53:41 +0530680 dev_err(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530681 "unable to start B-device\n");
682 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530683 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530684 return;
685 }
686 }
687 } else {
Manu Gautam98013c22012-11-20 17:42:42 +0530688 if (charger)
689 charger->start_detection(dotg->charger, false);
690
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530691 dwc3_otg_set_power(phy, 0);
Manu Gautamb5067272012-07-02 09:53:41 +0530692 dev_dbg(phy->dev, "No device, trying to suspend\n");
693 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530694 }
695 break;
696
697 case OTG_STATE_B_PERIPHERAL:
698 if (!test_bit(B_SESS_VLD, &dotg->inputs) ||
699 !test_bit(ID, &dotg->inputs)) {
700 dev_dbg(phy->dev, "!id || !bsv\n");
701 dwc3_otg_start_peripheral(&dotg->otg, 0);
702 phy->state = OTG_STATE_B_IDLE;
703 if (charger)
704 charger->chg_type = DWC3_INVALID_CHARGER;
705 work = 1;
706 }
707 break;
708
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200709 case OTG_STATE_A_IDLE:
710 /* Switch to A-Device*/
Manu Gautam8c642812012-06-07 10:35:10 +0530711 if (test_bit(ID, &dotg->inputs)) {
712 dev_dbg(phy->dev, "id\n");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200713 phy->state = OTG_STATE_B_IDLE;
Manu Gautam8c642812012-06-07 10:35:10 +0530714 work = 1;
715 } else {
Manu Gautama4c3c1f2012-12-18 13:56:43 +0530716 phy->state = OTG_STATE_A_HOST;
717 if (dwc3_otg_start_host(&dotg->otg, 1)) {
Manu Gautam8c642812012-06-07 10:35:10 +0530718 /*
719 * Probably set_host was not called yet.
720 * We will re-try as soon as it will be called
721 */
Manu Gautamb5067272012-07-02 09:53:41 +0530722 dev_dbg(phy->dev, "enter lpm as\n"
Manu Gautam8c642812012-06-07 10:35:10 +0530723 "unable to start A-device\n");
724 phy->state = OTG_STATE_UNDEFINED;
Manu Gautamb5067272012-07-02 09:53:41 +0530725 pm_runtime_put_sync(phy->dev);
Manu Gautam8c642812012-06-07 10:35:10 +0530726 return;
727 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200728 }
729 break;
Manu Gautam8c642812012-06-07 10:35:10 +0530730
731 case OTG_STATE_A_HOST:
732 if (test_bit(ID, &dotg->inputs)) {
733 dev_dbg(phy->dev, "id\n");
734 dwc3_otg_start_host(&dotg->otg, 0);
735 phy->state = OTG_STATE_B_IDLE;
736 work = 1;
737 }
738 break;
739
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200740 default:
741 dev_err(phy->dev, "%s: invalid otg-state\n", __func__);
742
743 }
Manu Gautam8c642812012-06-07 10:35:10 +0530744
745 if (work)
746 schedule_work(&dotg->sm_work);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200747}
748
749
750/**
751 * dwc3_otg_reset - reset dwc3 otg registers.
752 *
753 * @w: Pointer to the dwc3 otg workqueue
754 */
755static void dwc3_otg_reset(struct dwc3_otg *dotg)
756{
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530757 static int once;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530758 struct dwc3_ext_xceiv *ext_xceiv = dotg->ext_xceiv;
759
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200760 /*
761 * OCFG[2] - OTG-Version = 1
762 * OCFG[1] - HNPCap = 0
763 * OCFG[0] - SRPCap = 0
764 */
765 dwc3_writel(dotg->regs, DWC3_OCFG, 0x4);
766
767 /*
768 * OCTL[6] - PeriMode = 1
769 * OCTL[5] - PrtPwrCtl = 0
770 * OCTL[4] - HNPReq = 0
771 * OCTL[3] - SesReq = 0
772 * OCTL[2] - TermSelDLPulse = 0
773 * OCTL[1] - DevSetHNPEn = 0
774 * OCTL[0] - HstSetHNPEn = 0
775 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530776 if (!once) {
777 dwc3_writel(dotg->regs, DWC3_OCTL, 0x40);
778 once++;
779 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200780
781 /* Clear all otg events (interrupts) indications */
782 dwc3_writel(dotg->regs, DWC3_OEVT, 0xFFFF);
783
Manu Gautam8c642812012-06-07 10:35:10 +0530784 /* Enable ID/BSV StsChngEn event*/
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530785 if (ext_xceiv && !ext_xceiv->otg_capability)
786 dwc3_writel(dotg->regs, DWC3_OEVTEN,
787 DWC3_OEVTEN_OTGCONIDSTSCHNGEVNT |
788 DWC3_OEVTEN_OTGBDEVVBUSCHNGEVNT);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200789}
790
791/**
792 * dwc3_otg_init - Initializes otg related registers
793 * @dwc: Pointer to out controller context structure
794 *
795 * Returns 0 on success otherwise negative errno.
796 */
797int dwc3_otg_init(struct dwc3 *dwc)
798{
799 u32 reg;
800 int ret = 0;
801 struct dwc3_otg *dotg;
802
803 dev_dbg(dwc->dev, "dwc3_otg_init\n");
804
805 /*
806 * GHWPARAMS6[10] bit is SRPSupport.
807 * This bit also reflects DWC_USB3_EN_OTG
808 */
809 reg = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
810 if (!(reg & DWC3_GHWPARAMS6_SRP_SUPPORT)) {
811 /*
812 * No OTG support in the HW core.
813 * We return 0 to indicate no error, since this is acceptable
814 * situation, just continue probe the dwc3 driver without otg.
815 */
816 dev_dbg(dwc->dev, "dwc3_otg address space is not supported\n");
817 return 0;
818 }
819
820 /* Allocate and init otg instance */
821 dotg = kzalloc(sizeof(struct dwc3_otg), GFP_KERNEL);
822 if (!dotg) {
823 dev_err(dwc->dev, "unable to allocate dwc3_otg\n");
824 return -ENOMEM;
825 }
826
Manu Gautam17206c22012-06-21 10:17:53 +0530827 /* DWC3 has separate IRQ line for OTG events (ID/BSV etc.) */
828 dotg->irq = platform_get_irq_byname(to_platform_device(dwc->dev),
829 "otg_irq");
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200830 if (dotg->irq < 0) {
Manu Gautam17206c22012-06-21 10:17:53 +0530831 dev_err(dwc->dev, "%s: missing OTG IRQ\n", __func__);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200832 ret = -ENODEV;
833 goto err1;
834 }
835
836 dotg->regs = dwc->regs;
837
838 dotg->otg.set_peripheral = dwc3_otg_set_peripheral;
839 dotg->otg.set_host = dwc3_otg_set_host;
840
841 /* This reference is used by dwc3 modules for checking otg existance */
842 dwc->dotg = dotg;
843
844 dotg->otg.phy = kzalloc(sizeof(struct usb_phy), GFP_KERNEL);
845 if (!dotg->otg.phy) {
846 dev_err(dwc->dev, "unable to allocate dwc3_otg.phy\n");
847 ret = -ENOMEM;
848 goto err1;
849 }
850
Manu Gautamf1fceddf2012-10-12 14:02:50 +0530851 dotg->dwc = dwc;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200852 dotg->otg.phy->otg = &dotg->otg;
853 dotg->otg.phy->dev = dwc->dev;
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530854 dotg->otg.phy->set_power = dwc3_otg_set_power;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200855
856 ret = usb_set_transceiver(dotg->otg.phy);
857 if (ret) {
858 dev_err(dotg->otg.phy->dev,
859 "%s: failed to set transceiver, already exists\n",
860 __func__);
861 goto err2;
862 }
863
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200864 dotg->otg.phy->state = OTG_STATE_UNDEFINED;
865
Vijayavardhan Vennapusa42eeea32012-10-22 17:56:11 +0530866 init_completion(&dotg->dwc3_xcvr_vbus_init);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200867 INIT_WORK(&dotg->sm_work, dwc3_otg_sm_work);
868
869 ret = request_irq(dotg->irq, dwc3_otg_interrupt, IRQF_SHARED,
870 "dwc3_otg", dotg);
871 if (ret) {
872 dev_err(dotg->otg.phy->dev, "failed to request irq #%d --> %d\n",
873 dotg->irq, ret);
874 goto err3;
875 }
876
Manu Gautamb5067272012-07-02 09:53:41 +0530877 pm_runtime_get(dwc->dev);
878
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200879 return 0;
880
881err3:
882 cancel_work_sync(&dotg->sm_work);
883 usb_set_transceiver(NULL);
884err2:
885 kfree(dotg->otg.phy);
886err1:
887 dwc->dotg = NULL;
888 kfree(dotg);
889
890 return ret;
891}
892
893/**
894 * dwc3_otg_exit
895 * @dwc: Pointer to out controller context structure
896 *
897 * Returns 0 on success otherwise negative errno.
898 */
899void dwc3_otg_exit(struct dwc3 *dwc)
900{
901 struct dwc3_otg *dotg = dwc->dotg;
902
903 /* dotg is null when GHWPARAMS6[10]=SRPSupport=0, see dwc3_otg_init */
904 if (dotg) {
Manu Gautam8c642812012-06-07 10:35:10 +0530905 if (dotg->charger)
906 dotg->charger->start_detection(dotg->charger, false);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200907 cancel_work_sync(&dotg->sm_work);
908 usb_set_transceiver(NULL);
Manu Gautamb5067272012-07-02 09:53:41 +0530909 pm_runtime_put(dwc->dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +0200910 free_irq(dotg->irq, dotg);
911 kfree(dotg->otg.phy);
912 kfree(dotg);
913 dwc->dotg = NULL;
914 }
915}