blob: 79a2858aba7a11b06db9b63b71d987e3118954b5 [file] [log] [blame]
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301/* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053041
42#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080043#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#define MSM_USB_BASE (motg->regs)
46#define DRIVER_NAME "msm_otg"
47
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053048#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053049#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053050
51#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
52#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
53#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
54#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
55
56#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
57#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
58#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
59#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
60
Vamsi Krishna132b2762011-11-11 16:09:20 -080061#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053062#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
63
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053065static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053067/* Prevent idle power collapse(pc) while operating in peripheral mode */
68static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
69{
70 struct msm_otg_platform_data *pdata = dev->pdata;
71 u32 swfi_latency = 0;
72
73 if (!pdata || !pdata->swfi_latency)
74 return;
75
76 swfi_latency = pdata->swfi_latency + 1;
77
78 if (vote)
79 pm_qos_update_request(&dev->pm_qos_req_dma,
80 swfi_latency);
81 else
82 pm_qos_update_request(&dev->pm_qos_req_dma,
83 PM_QOS_DEFAULT_VALUE);
84}
85
Anji jonnala11aa5c42011-05-04 10:19:48 +053086static struct regulator *hsusb_3p3;
87static struct regulator *hsusb_1p8;
88static struct regulator *hsusb_vddcx;
89
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053090static inline bool aca_enabled(void)
91{
92#ifdef CONFIG_USB_MSM_ACA
93 return true;
94#else
95 return debug_aca_enabled;
96#endif
97}
98
Anji jonnala11aa5c42011-05-04 10:19:48 +053099static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
100{
101 int ret = 0;
102
103 if (init) {
104 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
105 if (IS_ERR(hsusb_vddcx)) {
106 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
107 return PTR_ERR(hsusb_vddcx);
108 }
109
110 ret = regulator_set_voltage(hsusb_vddcx,
111 USB_PHY_VDD_DIG_VOL_MIN,
112 USB_PHY_VDD_DIG_VOL_MAX);
113 if (ret) {
114 dev_err(motg->otg.dev, "unable to set the voltage "
115 "for hsusb vddcx\n");
116 regulator_put(hsusb_vddcx);
117 return ret;
118 }
119
120 ret = regulator_enable(hsusb_vddcx);
121 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700122 regulator_set_voltage(hsusb_vddcx, 0,
123 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
126 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 return ret;
135 }
136
137 ret = regulator_set_voltage(hsusb_vddcx, 0,
138 USB_PHY_VDD_DIG_VOL_MIN);
139 if (ret) {
140 dev_err(motg->otg.dev, "unable to set the voltage"
141 "for hsusb vddcx\n");
142 return ret;
143 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530144
145 regulator_put(hsusb_vddcx);
146 }
147
148 return ret;
149}
150
151static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
152{
153 int rc = 0;
154
155 if (init) {
156 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
157 if (IS_ERR(hsusb_3p3)) {
158 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
159 return PTR_ERR(hsusb_3p3);
160 }
161
162 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
163 USB_PHY_3P3_VOL_MAX);
164 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 dev_err(motg->otg.dev, "unable to set voltage level for"
166 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530167 goto put_3p3;
168 }
169 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
170 if (IS_ERR(hsusb_1p8)) {
171 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
172 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530174 }
175 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
176 USB_PHY_1P8_VOL_MAX);
177 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178 dev_err(motg->otg.dev, "unable to set voltage level for"
179 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530180 goto put_1p8;
181 }
182
183 return 0;
184 }
185
Anji jonnala11aa5c42011-05-04 10:19:48 +0530186put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530188 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189put_3p3_lpm:
190 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191put_3p3:
192 regulator_put(hsusb_3p3);
193 return rc;
194}
195
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530196#ifdef CONFIG_PM_SLEEP
197#define USB_PHY_SUSP_DIG_VOL 500000
198static int msm_hsusb_config_vddcx(int high)
199{
200 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
201 int min_vol;
202 int ret;
203
204 if (high)
205 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
206 else
207 min_vol = USB_PHY_SUSP_DIG_VOL;
208
209 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
210 if (ret) {
211 pr_err("%s: unable to set the voltage for regulator "
212 "HSUSB_VDDCX\n", __func__);
213 return ret;
214 }
215
216 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
217
218 return ret;
219}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700220#else
221static int msm_hsusb_config_vddcx(int high)
222{
223 return 0;
224}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530225#endif
226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530228{
229 int ret = 0;
230
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530231 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
233 return -ENODEV;
234 }
235
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530236 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530237 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
238 return -ENODEV;
239 }
240
241 if (on) {
242 ret = regulator_set_optimum_mode(hsusb_1p8,
243 USB_PHY_1P8_HPM_LOAD);
244 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530246 "HSUSB_1p8\n", __func__);
247 return ret;
248 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249
250 ret = regulator_enable(hsusb_1p8);
251 if (ret) {
252 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
253 __func__);
254 regulator_set_optimum_mode(hsusb_1p8, 0);
255 return ret;
256 }
257
Anji jonnala11aa5c42011-05-04 10:19:48 +0530258 ret = regulator_set_optimum_mode(hsusb_3p3,
259 USB_PHY_3P3_HPM_LOAD);
260 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530262 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 regulator_set_optimum_mode(hsusb_1p8, 0);
264 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 return ret;
266 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267
268 ret = regulator_enable(hsusb_3p3);
269 if (ret) {
270 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
271 __func__);
272 regulator_set_optimum_mode(hsusb_3p3, 0);
273 regulator_set_optimum_mode(hsusb_1p8, 0);
274 regulator_disable(hsusb_1p8);
275 return ret;
276 }
277
Anji jonnala11aa5c42011-05-04 10:19:48 +0530278 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 ret = regulator_disable(hsusb_1p8);
280 if (ret) {
281 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
282 __func__);
283 return ret;
284 }
285
286 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530287 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530289 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290
291 ret = regulator_disable(hsusb_3p3);
292 if (ret) {
293 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
294 __func__);
295 return ret;
296 }
297 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530298 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530300 "HSUSB_3p3\n", __func__);
301 }
302
303 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
304 return ret < 0 ? ret : 0;
305}
306
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530307static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
308{
309 static struct regulator *mhl_analog_switch;
310 struct msm_otg_platform_data *pdata = motg->pdata;
311
312 if (!pdata->mhl_enable)
313 return;
314
315 if (on) {
316 mhl_analog_switch = regulator_get(motg->otg.dev,
317 "mhl_ext_3p3v");
318 if (IS_ERR(mhl_analog_switch)) {
319 pr_err("Unable to get mhl_analog_switch\n");
320 return;
321 }
322
323 if (regulator_enable(mhl_analog_switch)) {
324 pr_err("unable to enable mhl_analog_switch\n");
325 goto put_analog_switch;
326 }
327 return;
328 }
329
330 regulator_disable(mhl_analog_switch);
331put_analog_switch:
332 regulator_put(mhl_analog_switch);
333}
334
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530335static int ulpi_read(struct otg_transceiver *otg, u32 reg)
336{
337 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
338 int cnt = 0;
339
340 /* initiate read operation */
341 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
342 USB_ULPI_VIEWPORT);
343
344 /* wait for completion */
345 while (cnt < ULPI_IO_TIMEOUT_USEC) {
346 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
347 break;
348 udelay(1);
349 cnt++;
350 }
351
352 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
353 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
354 readl(USB_ULPI_VIEWPORT));
355 return -ETIMEDOUT;
356 }
357 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
358}
359
360static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
361{
362 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
363 int cnt = 0;
364
365 /* initiate write operation */
366 writel(ULPI_RUN | ULPI_WRITE |
367 ULPI_ADDR(reg) | ULPI_DATA(val),
368 USB_ULPI_VIEWPORT);
369
370 /* wait for completion */
371 while (cnt < ULPI_IO_TIMEOUT_USEC) {
372 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
373 break;
374 udelay(1);
375 cnt++;
376 }
377
378 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
379 dev_err(otg->dev, "ulpi_write: timeout\n");
380 return -ETIMEDOUT;
381 }
382 return 0;
383}
384
385static struct otg_io_access_ops msm_otg_io_ops = {
386 .read = ulpi_read,
387 .write = ulpi_write,
388};
389
390static void ulpi_init(struct msm_otg *motg)
391{
392 struct msm_otg_platform_data *pdata = motg->pdata;
393 int *seq = pdata->phy_init_seq;
394
395 if (!seq)
396 return;
397
398 while (seq[0] >= 0) {
399 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
400 seq[0], seq[1]);
401 ulpi_write(&motg->otg, seq[0], seq[1]);
402 seq += 2;
403 }
404}
405
406static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
407{
408 int ret;
409
410 if (assert) {
411 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
412 if (ret)
413 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
414 } else {
415 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
416 if (ret)
417 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
418 }
419 return ret;
420}
421
422static int msm_otg_phy_clk_reset(struct msm_otg *motg)
423{
424 int ret;
425
Amit Blay02eff132011-09-21 16:46:24 +0300426 if (IS_ERR(motg->phy_reset_clk))
427 return 0;
428
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530429 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
430 if (ret) {
431 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
432 return ret;
433 }
434 usleep_range(10000, 12000);
435 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
436 if (ret)
437 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
438 return ret;
439}
440
441static int msm_otg_phy_reset(struct msm_otg *motg)
442{
443 u32 val;
444 int ret;
445 int retries;
446
447 ret = msm_otg_link_clk_reset(motg, 1);
448 if (ret)
449 return ret;
450 ret = msm_otg_phy_clk_reset(motg);
451 if (ret)
452 return ret;
453 ret = msm_otg_link_clk_reset(motg, 0);
454 if (ret)
455 return ret;
456
457 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
458 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
459
460 for (retries = 3; retries > 0; retries--) {
461 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
462 ULPI_CLR(ULPI_FUNC_CTRL));
463 if (!ret)
464 break;
465 ret = msm_otg_phy_clk_reset(motg);
466 if (ret)
467 return ret;
468 }
469 if (!retries)
470 return -ETIMEDOUT;
471
472 /* This reset calibrates the phy, if the above write succeeded */
473 ret = msm_otg_phy_clk_reset(motg);
474 if (ret)
475 return ret;
476
477 for (retries = 3; retries > 0; retries--) {
478 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
479 if (ret != -ETIMEDOUT)
480 break;
481 ret = msm_otg_phy_clk_reset(motg);
482 if (ret)
483 return ret;
484 }
485 if (!retries)
486 return -ETIMEDOUT;
487
488 dev_info(motg->otg.dev, "phy_reset: success\n");
489 return 0;
490}
491
492#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530493static int msm_otg_link_reset(struct msm_otg *motg)
494{
495 int cnt = 0;
496
497 writel_relaxed(USBCMD_RESET, USB_USBCMD);
498 while (cnt < LINK_RESET_TIMEOUT_USEC) {
499 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
500 break;
501 udelay(1);
502 cnt++;
503 }
504 if (cnt >= LINK_RESET_TIMEOUT_USEC)
505 return -ETIMEDOUT;
506
507 /* select ULPI phy */
508 writel_relaxed(0x80000000, USB_PORTSC);
509 writel_relaxed(0x0, USB_AHBBURST);
510 writel_relaxed(0x00, USB_AHBMODE);
511
512 return 0;
513}
514
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530515static int msm_otg_reset(struct otg_transceiver *otg)
516{
517 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
518 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530519 int ret;
520 u32 val = 0;
521 u32 ulpi_val = 0;
522
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700523 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530524 ret = msm_otg_phy_reset(motg);
525 if (ret) {
526 dev_err(otg->dev, "phy_reset failed\n");
527 return ret;
528 }
529
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530530 ret = msm_otg_link_reset(motg);
531 if (ret) {
532 dev_err(otg->dev, "link reset failed\n");
533 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530534 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530535 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530536
537 ulpi_init(motg);
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 /* Ensure that RESET operation is completed before turning off clock */
540 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530541
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700542 clk_disable(motg->clk);
543
544 val = readl_relaxed(USB_OTGSC);
545 if (pdata->mode == USB_OTG) {
546 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
547 val |= OTGSC_IDIE | OTGSC_BSVIE;
548 } else if (pdata->mode == USB_PERIPHERAL) {
549 ulpi_val = ULPI_INT_SESS_VALID;
550 val |= OTGSC_BSVIE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700552 writel_relaxed(val, USB_OTGSC);
553 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
554 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
555
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530556 return 0;
557}
558
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530559static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
560{
561 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
562
563 /*
564 * Allow bus suspend only for host mode. Device mode bus suspend
565 * is not implemented yet.
566 */
567 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530568 /*
569 * ID_GND --> ID_A transition can not be detected in LPM.
570 * Disallow host bus suspend when ACA is enabled.
571 */
572 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530573 pm_runtime_put(otg->dev);
574 else
575 pm_runtime_resume(otg->dev);
576 }
577
578 return 0;
579}
580
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530581#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530582#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
583
584#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530585static int msm_otg_suspend(struct msm_otg *motg)
586{
587 struct otg_transceiver *otg = &motg->otg;
588 struct usb_bus *bus = otg->host;
589 struct msm_otg_platform_data *pdata = motg->pdata;
590 int cnt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 bool session_active;
Amit Blay58b31472011-11-18 09:39:39 +0200592 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800593 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530594 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530595
596 if (atomic_read(&motg->in_lpm))
597 return 0;
598
599 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700600 session_active = (otg->host && !test_bit(ID, &motg->inputs)) ||
601 test_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530602 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530603 * Chipidea 45-nm PHY suspend sequence:
604 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530605 * Interrupt Latch Register auto-clear feature is not present
606 * in all PHY versions. Latch register is clear on read type.
607 * Clear latch register to avoid spurious wakeup from
608 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530609 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530610 * PHY comparators are disabled when PHY enters into low power
611 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
612 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
613 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530614 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530615 * PLL is not turned off when PHY enters into low power mode (LPM).
616 * Disable PLL for maximum power savings.
617 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530618
619 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
620 ulpi_read(otg, 0x14);
621 if (pdata->otg_control == OTG_PHY_CONTROL)
622 ulpi_write(otg, 0x01, 0x30);
623 ulpi_write(otg, 0x08, 0x09);
624 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530625
626 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700627 * Turn off the OTG comparators, if depends on PMIC for
628 * VBUS and ID notifications.
629 */
630 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !session_active) {
631 ulpi_write(otg, OTG_COMP_DISABLE,
632 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
633 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
634 }
635
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530636 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530637 * PHY may take some time or even fail to enter into low power
638 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
639 * in failure case.
640 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530641 portsc = readl_relaxed(USB_PORTSC);
642 if (!(portsc & PORTSC_PHCD)) {
643 writel_relaxed(portsc | PORTSC_PHCD,
644 USB_PORTSC);
645 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
646 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
647 break;
648 udelay(1);
649 cnt++;
650 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530651 }
652
653 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
654 dev_err(otg->dev, "Unable to suspend PHY\n");
655 msm_otg_reset(otg);
656 enable_irq(motg->irq);
657 return -ETIMEDOUT;
658 }
659
660 /*
661 * PHY has capability to generate interrupt asynchronously in low
662 * power mode (LPM). This interrupt is level triggered. So USB IRQ
663 * line must be disabled till async interrupt enable bit is cleared
664 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
665 * block data communication from PHY.
666 */
667 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
668
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 if (motg->caps & ALLOW_PHY_RETENTION && !session_active) {
Amit Blay58b31472011-11-18 09:39:39 +0200670 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
671 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
672 /* Enable PHY HV interrupts to wake MPM/Link */
673 phy_ctrl_val |=
674 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
675
676 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 motg->lpm_flags |= PHY_RETENTIONED;
678 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700680 /* Ensure that above operation is completed before turning off clocks */
681 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530682 clk_disable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530683 if (motg->core_clk)
684 clk_disable(motg->core_clk);
685
Amit Blay137575f2011-11-06 15:20:54 +0200686 if (!IS_ERR(motg->system_clk))
687 clk_disable(motg->system_clk);
688
Anji jonnala0f73cac2011-05-04 10:19:46 +0530689 if (!IS_ERR(motg->pclk_src))
690 clk_disable(motg->pclk_src);
691
Anji jonnala7da3f262011-12-02 17:22:14 -0800692 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
693 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
694 if (ret)
695 dev_err(otg->dev, "%s failed to devote for "
696 "TCXO D0 buffer%d\n", __func__, ret);
697
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700698 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !session_active) {
699 msm_hsusb_ldo_enable(motg, 0);
700 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530701 }
702
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530703 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700704 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530705 msm_hsusb_mhl_switch_enable(motg, 0);
706 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700707
708 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530709 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 if (motg->pdata->pmic_id_irq)
711 enable_irq_wake(motg->pdata->pmic_id_irq);
712 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530713 if (bus)
714 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
715
716 atomic_set(&motg->in_lpm, 1);
717 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530719
720 dev_info(otg->dev, "USB in low power mode\n");
721
722 return 0;
723}
724
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530725static int msm_otg_resume(struct msm_otg *motg)
726{
727 struct otg_transceiver *otg = &motg->otg;
728 struct usb_bus *bus = otg->host;
729 int cnt = 0;
730 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200731 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800732 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530733
734 if (!atomic_read(&motg->in_lpm))
735 return 0;
736
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700737 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800738
739 /* Vote for TCXO when waking up the phy */
740 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
741 if (ret)
742 dev_err(otg->dev, "%s failed to vote for "
743 "TCXO D0 buffer%d\n", __func__, ret);
744
Anji jonnala0f73cac2011-05-04 10:19:46 +0530745 if (!IS_ERR(motg->pclk_src))
746 clk_enable(motg->pclk_src);
747
Amit Blay137575f2011-11-06 15:20:54 +0200748 if (!IS_ERR(motg->system_clk))
749 clk_enable(motg->system_clk);
750
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530751 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530752 if (motg->core_clk)
753 clk_enable(motg->core_clk);
754
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700755 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
756 msm_hsusb_ldo_enable(motg, 1);
757 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
758 }
759
760 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530761 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530762 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200763 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
764 phy_ctrl_val |= PHY_RETEN;
765 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
766 /* Disable PHY HV interrupts */
767 phy_ctrl_val &=
768 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
769 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530771 }
772
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530773 temp = readl(USB_USBCMD);
774 temp &= ~ASYNC_INTR_CTRL;
775 temp &= ~ULPI_STP_CTRL;
776 writel(temp, USB_USBCMD);
777
778 /*
779 * PHY comes out of low power mode (LPM) in case of wakeup
780 * from asynchronous interrupt.
781 */
782 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
783 goto skip_phy_resume;
784
785 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
786 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
787 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
788 break;
789 udelay(1);
790 cnt++;
791 }
792
793 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
794 /*
795 * This is a fatal error. Reset the link and
796 * PHY. USB state can not be restored. Re-insertion
797 * of USB cable is the only way to get USB working.
798 */
799 dev_err(otg->dev, "Unable to resume USB."
800 "Re-plugin the cable\n");
801 msm_otg_reset(otg);
802 }
803
804skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700805 /* Turn on the OTG comparators on resume */
806 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
807 ulpi_write(otg, OTG_COMP_DISABLE,
808 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
809 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
810 }
811 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530812 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700813 if (motg->pdata->pmic_id_irq)
814 disable_irq_wake(motg->pdata->pmic_id_irq);
815 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530816 if (bus)
817 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
818
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530819 atomic_set(&motg->in_lpm, 0);
820
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530821 if (aca_enabled() && !irq_read_line(motg->pdata->pmic_id_irq)) {
822 clear_bit(ID, &motg->inputs);
823 schedule_work(&motg->sm_work);
824 }
825
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826 if (motg->async_int) {
827 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530828 enable_irq(motg->irq);
829 }
830
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 dev_info(otg->dev, "USB exited from low power mode\n");
832
833 return 0;
834}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530835#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530836
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530837static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
838{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530839 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
840 motg->chg_type == USB_ACA_A_CHARGER ||
841 motg->chg_type == USB_ACA_B_CHARGER ||
842 motg->chg_type == USB_ACA_C_CHARGER) &&
843 mA > IDEV_ACA_CHG_LIMIT)
844 mA = IDEV_ACA_CHG_LIMIT;
845
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530846 if (motg->cur_power == mA)
847 return;
848
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530849 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530851 motg->cur_power = mA;
852}
853
854static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
855{
856 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
857
858 /*
859 * Gadget driver uses set_power method to notify about the
860 * available current based on suspend/configured states.
861 *
862 * IDEV_CHG can be drawn irrespective of suspend/un-configured
863 * states when CDP/ACA is connected.
864 */
865 if (motg->chg_type == USB_SDP_CHARGER)
866 msm_otg_notify_charger(motg, mA);
867
868 return 0;
869}
870
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530871static void msm_otg_start_host(struct otg_transceiver *otg, int on)
872{
873 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
874 struct msm_otg_platform_data *pdata = motg->pdata;
875 struct usb_hcd *hcd;
876
877 if (!otg->host)
878 return;
879
880 hcd = bus_to_hcd(otg->host);
881
882 if (on) {
883 dev_dbg(otg->dev, "host on\n");
884
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530885 /*
886 * Some boards have a switch cotrolled by gpio
887 * to enable/disable internal HUB. Enable internal
888 * HUB before kicking the host.
889 */
890 if (pdata->setup_gpio)
891 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530892 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530893 } else {
894 dev_dbg(otg->dev, "host off\n");
895
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530896 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530897 /* HCD core reset all bits of PORTSC. select ULPI phy */
898 writel_relaxed(0x80000000, USB_PORTSC);
899
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530900 if (pdata->setup_gpio)
901 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530902 }
903}
904
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905static int msm_otg_usbdev_notify(struct notifier_block *self,
906 unsigned long action, void *priv)
907{
908 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530909 struct usb_device *udev = priv;
910
911 if (!aca_enabled())
912 goto out;
913
914 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
915 goto out;
916
917 if (udev->bus != motg->otg.host)
918 goto out;
919 /*
920 * Interested in devices connected directly to the root hub.
921 * ACA dock can supply IDEV_CHG irrespective devices connected
922 * on the accessory port.
923 */
924 if (!udev->parent || udev->parent->parent ||
925 motg->chg_type == USB_ACA_DOCK_CHARGER)
926 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927
928 switch (action) {
929 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530930 usb_disable_autosuspend(udev);
931 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700933 if (udev->actconfig)
934 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
935 else
936 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530937 break;
938 case USB_DEVICE_REMOVE:
939 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 break;
941 default:
942 break;
943 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530944 if (test_bit(ID_A, &motg->inputs))
945 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
946 motg->mA_port);
947out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700948 return NOTIFY_OK;
949}
950
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530951static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
952{
953 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
954 struct usb_hcd *hcd;
955
956 /*
957 * Fail host registration if this board can support
958 * only peripheral configuration.
959 */
960 if (motg->pdata->mode == USB_PERIPHERAL) {
961 dev_info(otg->dev, "Host mode is not supported\n");
962 return -ENODEV;
963 }
964
965 if (!host) {
966 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530967 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530969 msm_otg_start_host(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700970 if (motg->pdata->vbus_power)
971 motg->pdata->vbus_power(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530972 otg->host = NULL;
973 otg->state = OTG_STATE_UNDEFINED;
974 schedule_work(&motg->sm_work);
975 } else {
976 otg->host = NULL;
977 }
978
979 return 0;
980 }
981
982 hcd = bus_to_hcd(host);
983 hcd->power_budget = motg->pdata->power_budget;
984
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
986 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530987 otg->host = host;
988 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
989
990 /*
991 * Kick the state machine work, if peripheral is not supported
992 * or peripheral is already registered with us.
993 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530994 if (motg->pdata->mode == USB_HOST || otg->gadget) {
995 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530996 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530997 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530998
999 return 0;
1000}
1001
1002static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1003{
1004 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1005 struct msm_otg_platform_data *pdata = motg->pdata;
1006
1007 if (!otg->gadget)
1008 return;
1009
1010 if (on) {
1011 dev_dbg(otg->dev, "gadget on\n");
1012 /*
1013 * Some boards have a switch cotrolled by gpio
1014 * to enable/disable internal HUB. Disable internal
1015 * HUB before kicking the gadget.
1016 */
1017 if (pdata->setup_gpio)
1018 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301019 /*
1020 * vote for minimum dma_latency to prevent idle
1021 * power collapse(pc) while running in peripheral mode.
1022 */
1023 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301024 usb_gadget_vbus_connect(otg->gadget);
1025 } else {
1026 dev_dbg(otg->dev, "gadget off\n");
1027 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301028 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301029 if (pdata->setup_gpio)
1030 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1031 }
1032
1033}
1034
1035static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1036 struct usb_gadget *gadget)
1037{
1038 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1039
1040 /*
1041 * Fail peripheral registration if this board can support
1042 * only host configuration.
1043 */
1044 if (motg->pdata->mode == USB_HOST) {
1045 dev_info(otg->dev, "Peripheral mode is not supported\n");
1046 return -ENODEV;
1047 }
1048
1049 if (!gadget) {
1050 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301051 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301052 msm_otg_start_peripheral(otg, 0);
1053 otg->gadget = NULL;
1054 otg->state = OTG_STATE_UNDEFINED;
1055 schedule_work(&motg->sm_work);
1056 } else {
1057 otg->gadget = NULL;
1058 }
1059
1060 return 0;
1061 }
1062 otg->gadget = gadget;
1063 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1064
1065 /*
1066 * Kick the state machine work, if host is not supported
1067 * or host is already registered with us.
1068 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301069 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1070 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301071 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301072 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301073
1074 return 0;
1075}
1076
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077static bool msm_chg_aca_detect(struct msm_otg *motg)
1078{
1079 struct otg_transceiver *otg = &motg->otg;
1080 u32 int_sts;
1081 bool ret = false;
1082
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301083 if (!aca_enabled())
1084 goto out;
1085
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1087 goto out;
1088
1089 int_sts = ulpi_read(otg, 0x87);
1090 switch (int_sts & 0x1C) {
1091 case 0x08:
1092 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1093 dev_dbg(otg->dev, "ID_A\n");
1094 motg->chg_type = USB_ACA_A_CHARGER;
1095 motg->chg_state = USB_CHG_STATE_DETECTED;
1096 clear_bit(ID_B, &motg->inputs);
1097 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301098 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 ret = true;
1100 }
1101 break;
1102 case 0x0C:
1103 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1104 dev_dbg(otg->dev, "ID_B\n");
1105 motg->chg_type = USB_ACA_B_CHARGER;
1106 motg->chg_state = USB_CHG_STATE_DETECTED;
1107 clear_bit(ID_A, &motg->inputs);
1108 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301109 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 ret = true;
1111 }
1112 break;
1113 case 0x10:
1114 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1115 dev_dbg(otg->dev, "ID_C\n");
1116 motg->chg_type = USB_ACA_C_CHARGER;
1117 motg->chg_state = USB_CHG_STATE_DETECTED;
1118 clear_bit(ID_A, &motg->inputs);
1119 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301120 set_bit(ID, &motg->inputs);
1121 ret = true;
1122 }
1123 break;
1124 case 0x04:
1125 if (test_and_clear_bit(ID, &motg->inputs)) {
1126 dev_dbg(otg->dev, "ID_GND\n");
1127 motg->chg_type = USB_INVALID_CHARGER;
1128 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1129 clear_bit(ID_A, &motg->inputs);
1130 clear_bit(ID_B, &motg->inputs);
1131 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001132 ret = true;
1133 }
1134 break;
1135 default:
1136 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1137 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301138 test_and_clear_bit(ID_C, &motg->inputs) |
1139 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301141 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001142 motg->chg_type = USB_INVALID_CHARGER;
1143 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1144 }
1145 }
1146out:
1147 return ret;
1148}
1149
1150static void msm_chg_enable_aca_det(struct msm_otg *motg)
1151{
1152 struct otg_transceiver *otg = &motg->otg;
1153
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301154 if (!aca_enabled())
1155 return;
1156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157 switch (motg->pdata->phy_type) {
1158 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301159 /* Disable ID_GND in link and PHY */
1160 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1161 OTGSC_IDIE), USB_OTGSC);
1162 ulpi_write(otg, 0x01, 0x0C);
1163 ulpi_write(otg, 0x10, 0x0F);
1164 ulpi_write(otg, 0x10, 0x12);
1165 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 ulpi_write(otg, 0x20, 0x85);
1167 break;
1168 default:
1169 break;
1170 }
1171}
1172
1173static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1174{
1175 struct otg_transceiver *otg = &motg->otg;
1176
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301177 if (!aca_enabled())
1178 return;
1179
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001180 switch (motg->pdata->phy_type) {
1181 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301182 /* Enable ACA Detection interrupt (on any RID change) */
1183 ulpi_write(otg, 0x01, 0x94);
1184 break;
1185 default:
1186 break;
1187 }
1188}
1189
1190static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1191{
1192 struct otg_transceiver *otg = &motg->otg;
1193
1194 if (!aca_enabled())
1195 return;
1196
1197 switch (motg->pdata->phy_type) {
1198 case SNPS_28NM_INTEGRATED_PHY:
1199 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200 break;
1201 default:
1202 break;
1203 }
1204}
1205
1206static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1207{
1208 struct otg_transceiver *otg = &motg->otg;
1209 bool ret = false;
1210
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301211 if (!aca_enabled())
1212 return ret;
1213
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001214 switch (motg->pdata->phy_type) {
1215 case SNPS_28NM_INTEGRATED_PHY:
1216 if (ulpi_read(otg, 0x91) & 1) {
1217 dev_dbg(otg->dev, "RID change\n");
1218 ulpi_write(otg, 0x01, 0x92);
1219 ret = msm_chg_aca_detect(motg);
1220 }
1221 default:
1222 break;
1223 }
1224 return ret;
1225}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301226
1227static void msm_otg_id_timer_func(unsigned long data)
1228{
1229 struct msm_otg *motg = (struct msm_otg *) data;
1230
1231 if (!aca_enabled())
1232 return;
1233
1234 if (atomic_read(&motg->in_lpm)) {
1235 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1236 return;
1237 }
1238
1239 if (msm_chg_check_aca_intr(motg)) {
1240 dev_dbg(motg->otg.dev, "timer: aca work\n");
1241 schedule_work(&motg->sm_work);
1242 }
1243
1244 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1245 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1246}
1247
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301248static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1249{
1250 struct otg_transceiver *otg = &motg->otg;
1251 u32 chg_det;
1252 bool ret = false;
1253
1254 switch (motg->pdata->phy_type) {
1255 case CI_45NM_INTEGRATED_PHY:
1256 chg_det = ulpi_read(otg, 0x34);
1257 ret = chg_det & (1 << 4);
1258 break;
1259 case SNPS_28NM_INTEGRATED_PHY:
1260 chg_det = ulpi_read(otg, 0x87);
1261 ret = chg_det & 1;
1262 break;
1263 default:
1264 break;
1265 }
1266 return ret;
1267}
1268
1269static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1270{
1271 struct otg_transceiver *otg = &motg->otg;
1272 u32 chg_det;
1273
1274 switch (motg->pdata->phy_type) {
1275 case CI_45NM_INTEGRATED_PHY:
1276 chg_det = ulpi_read(otg, 0x34);
1277 /* Turn off charger block */
1278 chg_det |= ~(1 << 1);
1279 ulpi_write(otg, chg_det, 0x34);
1280 udelay(20);
1281 /* control chg block via ULPI */
1282 chg_det &= ~(1 << 3);
1283 ulpi_write(otg, chg_det, 0x34);
1284 /* put it in host mode for enabling D- source */
1285 chg_det &= ~(1 << 2);
1286 ulpi_write(otg, chg_det, 0x34);
1287 /* Turn on chg detect block */
1288 chg_det &= ~(1 << 1);
1289 ulpi_write(otg, chg_det, 0x34);
1290 udelay(20);
1291 /* enable chg detection */
1292 chg_det &= ~(1 << 0);
1293 ulpi_write(otg, chg_det, 0x34);
1294 break;
1295 case SNPS_28NM_INTEGRATED_PHY:
1296 /*
1297 * Configure DM as current source, DP as current sink
1298 * and enable battery charging comparators.
1299 */
1300 ulpi_write(otg, 0x8, 0x85);
1301 ulpi_write(otg, 0x2, 0x85);
1302 ulpi_write(otg, 0x1, 0x85);
1303 break;
1304 default:
1305 break;
1306 }
1307}
1308
1309static bool msm_chg_check_primary_det(struct msm_otg *motg)
1310{
1311 struct otg_transceiver *otg = &motg->otg;
1312 u32 chg_det;
1313 bool ret = false;
1314
1315 switch (motg->pdata->phy_type) {
1316 case CI_45NM_INTEGRATED_PHY:
1317 chg_det = ulpi_read(otg, 0x34);
1318 ret = chg_det & (1 << 4);
1319 break;
1320 case SNPS_28NM_INTEGRATED_PHY:
1321 chg_det = ulpi_read(otg, 0x87);
1322 ret = chg_det & 1;
1323 break;
1324 default:
1325 break;
1326 }
1327 return ret;
1328}
1329
1330static void msm_chg_enable_primary_det(struct msm_otg *motg)
1331{
1332 struct otg_transceiver *otg = &motg->otg;
1333 u32 chg_det;
1334
1335 switch (motg->pdata->phy_type) {
1336 case CI_45NM_INTEGRATED_PHY:
1337 chg_det = ulpi_read(otg, 0x34);
1338 /* enable chg detection */
1339 chg_det &= ~(1 << 0);
1340 ulpi_write(otg, chg_det, 0x34);
1341 break;
1342 case SNPS_28NM_INTEGRATED_PHY:
1343 /*
1344 * Configure DP as current source, DM as current sink
1345 * and enable battery charging comparators.
1346 */
1347 ulpi_write(otg, 0x2, 0x85);
1348 ulpi_write(otg, 0x1, 0x85);
1349 break;
1350 default:
1351 break;
1352 }
1353}
1354
1355static bool msm_chg_check_dcd(struct msm_otg *motg)
1356{
1357 struct otg_transceiver *otg = &motg->otg;
1358 u32 line_state;
1359 bool ret = false;
1360
1361 switch (motg->pdata->phy_type) {
1362 case CI_45NM_INTEGRATED_PHY:
1363 line_state = ulpi_read(otg, 0x15);
1364 ret = !(line_state & 1);
1365 break;
1366 case SNPS_28NM_INTEGRATED_PHY:
1367 line_state = ulpi_read(otg, 0x87);
1368 ret = line_state & 2;
1369 break;
1370 default:
1371 break;
1372 }
1373 return ret;
1374}
1375
1376static void msm_chg_disable_dcd(struct msm_otg *motg)
1377{
1378 struct otg_transceiver *otg = &motg->otg;
1379 u32 chg_det;
1380
1381 switch (motg->pdata->phy_type) {
1382 case CI_45NM_INTEGRATED_PHY:
1383 chg_det = ulpi_read(otg, 0x34);
1384 chg_det &= ~(1 << 5);
1385 ulpi_write(otg, chg_det, 0x34);
1386 break;
1387 case SNPS_28NM_INTEGRATED_PHY:
1388 ulpi_write(otg, 0x10, 0x86);
1389 break;
1390 default:
1391 break;
1392 }
1393}
1394
1395static void msm_chg_enable_dcd(struct msm_otg *motg)
1396{
1397 struct otg_transceiver *otg = &motg->otg;
1398 u32 chg_det;
1399
1400 switch (motg->pdata->phy_type) {
1401 case CI_45NM_INTEGRATED_PHY:
1402 chg_det = ulpi_read(otg, 0x34);
1403 /* Turn on D+ current source */
1404 chg_det |= (1 << 5);
1405 ulpi_write(otg, chg_det, 0x34);
1406 break;
1407 case SNPS_28NM_INTEGRATED_PHY:
1408 /* Data contact detection enable */
1409 ulpi_write(otg, 0x10, 0x85);
1410 break;
1411 default:
1412 break;
1413 }
1414}
1415
1416static void msm_chg_block_on(struct msm_otg *motg)
1417{
1418 struct otg_transceiver *otg = &motg->otg;
1419 u32 func_ctrl, chg_det;
1420
1421 /* put the controller in non-driving mode */
1422 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1423 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1424 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1425 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1426
1427 switch (motg->pdata->phy_type) {
1428 case CI_45NM_INTEGRATED_PHY:
1429 chg_det = ulpi_read(otg, 0x34);
1430 /* control chg block via ULPI */
1431 chg_det &= ~(1 << 3);
1432 ulpi_write(otg, chg_det, 0x34);
1433 /* Turn on chg detect block */
1434 chg_det &= ~(1 << 1);
1435 ulpi_write(otg, chg_det, 0x34);
1436 udelay(20);
1437 break;
1438 case SNPS_28NM_INTEGRATED_PHY:
1439 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301440 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301441 /* Clear alt interrupt latch and enable bits */
1442 ulpi_write(otg, 0x1F, 0x92);
1443 ulpi_write(otg, 0x1F, 0x95);
1444 udelay(100);
1445 break;
1446 default:
1447 break;
1448 }
1449}
1450
1451static void msm_chg_block_off(struct msm_otg *motg)
1452{
1453 struct otg_transceiver *otg = &motg->otg;
1454 u32 func_ctrl, chg_det;
1455
1456 switch (motg->pdata->phy_type) {
1457 case CI_45NM_INTEGRATED_PHY:
1458 chg_det = ulpi_read(otg, 0x34);
1459 /* Turn off charger block */
1460 chg_det |= ~(1 << 1);
1461 ulpi_write(otg, chg_det, 0x34);
1462 break;
1463 case SNPS_28NM_INTEGRATED_PHY:
1464 /* Clear charger detecting control bits */
1465 ulpi_write(otg, 0x3F, 0x86);
1466 /* Clear alt interrupt latch and enable bits */
1467 ulpi_write(otg, 0x1F, 0x92);
1468 ulpi_write(otg, 0x1F, 0x95);
1469 break;
1470 default:
1471 break;
1472 }
1473
1474 /* put the controller in normal mode */
1475 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1476 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1477 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1478 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1479}
1480
Anji jonnalad270e2d2011-08-09 11:28:32 +05301481static const char *chg_to_string(enum usb_chg_type chg_type)
1482{
1483 switch (chg_type) {
1484 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1485 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1486 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1487 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1488 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1489 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1490 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1491 default: return "INVALID_CHARGER";
1492 }
1493}
1494
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301495#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1496#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1497#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1498#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1499static void msm_chg_detect_work(struct work_struct *w)
1500{
1501 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1502 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301504 unsigned long delay;
1505
1506 dev_dbg(otg->dev, "chg detection work\n");
1507 switch (motg->chg_state) {
1508 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301509 msm_chg_block_on(motg);
1510 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301512 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1513 motg->dcd_retries = 0;
1514 delay = MSM_CHG_DCD_POLL_TIME;
1515 break;
1516 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517 is_aca = msm_chg_aca_detect(motg);
1518 if (is_aca) {
1519 /*
1520 * ID_A can be ACA dock too. continue
1521 * primary detection after DCD.
1522 */
1523 if (test_bit(ID_A, &motg->inputs)) {
1524 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1525 } else {
1526 delay = 0;
1527 break;
1528 }
1529 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301530 is_dcd = msm_chg_check_dcd(motg);
1531 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1532 if (is_dcd || tmout) {
1533 msm_chg_disable_dcd(motg);
1534 msm_chg_enable_primary_det(motg);
1535 delay = MSM_CHG_PRIMARY_DET_TIME;
1536 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1537 } else {
1538 delay = MSM_CHG_DCD_POLL_TIME;
1539 }
1540 break;
1541 case USB_CHG_STATE_DCD_DONE:
1542 vout = msm_chg_check_primary_det(motg);
1543 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301544 if (test_bit(ID_A, &motg->inputs)) {
1545 motg->chg_type = USB_ACA_DOCK_CHARGER;
1546 motg->chg_state = USB_CHG_STATE_DETECTED;
1547 delay = 0;
1548 break;
1549 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301550 msm_chg_enable_secondary_det(motg);
1551 delay = MSM_CHG_SECONDARY_DET_TIME;
1552 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1553 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301554 if (test_bit(ID_A, &motg->inputs)) {
1555 motg->chg_type = USB_ACA_A_CHARGER;
1556 motg->chg_state = USB_CHG_STATE_DETECTED;
1557 delay = 0;
1558 break;
1559 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301560 motg->chg_type = USB_SDP_CHARGER;
1561 motg->chg_state = USB_CHG_STATE_DETECTED;
1562 delay = 0;
1563 }
1564 break;
1565 case USB_CHG_STATE_PRIMARY_DONE:
1566 vout = msm_chg_check_secondary_det(motg);
1567 if (vout)
1568 motg->chg_type = USB_DCP_CHARGER;
1569 else
1570 motg->chg_type = USB_CDP_CHARGER;
1571 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1572 /* fall through */
1573 case USB_CHG_STATE_SECONDARY_DONE:
1574 motg->chg_state = USB_CHG_STATE_DETECTED;
1575 case USB_CHG_STATE_DETECTED:
1576 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001577 msm_chg_enable_aca_det(motg);
1578 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301579 dev_dbg(otg->dev, "chg_type = %s\n",
1580 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301581 schedule_work(&motg->sm_work);
1582 return;
1583 default:
1584 return;
1585 }
1586
1587 schedule_delayed_work(&motg->chg_work, delay);
1588}
1589
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301590/*
1591 * We support OTG, Peripheral only and Host only configurations. In case
1592 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1593 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1594 * enabled when switch is controlled by user and default mode is supplied
1595 * by board file, which can be changed by userspace later.
1596 */
1597static void msm_otg_init_sm(struct msm_otg *motg)
1598{
1599 struct msm_otg_platform_data *pdata = motg->pdata;
1600 u32 otgsc = readl(USB_OTGSC);
1601
1602 switch (pdata->mode) {
1603 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605 if (pdata->default_mode == USB_HOST) {
1606 clear_bit(ID, &motg->inputs);
1607 } else if (pdata->default_mode == USB_PERIPHERAL) {
1608 set_bit(ID, &motg->inputs);
1609 set_bit(B_SESS_VLD, &motg->inputs);
1610 } else {
1611 set_bit(ID, &motg->inputs);
1612 clear_bit(B_SESS_VLD, &motg->inputs);
1613 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001614 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301615 if (aca_enabled()) {
1616 if (irq_read_line(motg->pdata->pmic_id_irq))
1617 set_bit(ID, &motg->inputs);
1618 else
1619 clear_bit(ID, &motg->inputs);
1620
1621 } else {
1622 if (otgsc & OTGSC_ID)
1623 set_bit(ID, &motg->inputs);
1624 else
1625 clear_bit(ID, &motg->inputs);
1626 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001627
1628 if (otgsc & OTGSC_BSV)
1629 set_bit(B_SESS_VLD, &motg->inputs);
1630 else
1631 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301632 }
1633 break;
1634 case USB_HOST:
1635 clear_bit(ID, &motg->inputs);
1636 break;
1637 case USB_PERIPHERAL:
1638 set_bit(ID, &motg->inputs);
1639 if (otgsc & OTGSC_BSV)
1640 set_bit(B_SESS_VLD, &motg->inputs);
1641 else
1642 clear_bit(B_SESS_VLD, &motg->inputs);
1643 break;
1644 default:
1645 break;
1646 }
1647}
1648
1649static void msm_otg_sm_work(struct work_struct *w)
1650{
1651 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1652 struct otg_transceiver *otg = &motg->otg;
1653
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301654 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301655 switch (otg->state) {
1656 case OTG_STATE_UNDEFINED:
1657 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1658 msm_otg_reset(otg);
1659 msm_otg_init_sm(motg);
1660 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301661 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1662 test_bit(ID, &motg->inputs)) {
1663 pm_runtime_put_noidle(otg->dev);
1664 pm_runtime_suspend(otg->dev);
1665 break;
1666 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301667 /* FALL THROUGH */
1668 case OTG_STATE_B_IDLE:
1669 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 if ((!test_bit(ID, &motg->inputs) ||
1671 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1673 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301674 IDEV_ACA_CHG_MAX);
1675 else if (test_bit(ID_A, &motg->inputs))
1676 msm_otg_notify_charger(motg,
1677 IDEV_ACA_CHG_MAX - IUNIT);
1678 else if (motg->pdata->vbus_power)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001679 motg->pdata->vbus_power(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301680 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301681 /*
1682 * Link can not generate PHY_ALT interrupt
1683 * in host mode when no device is attached
1684 * to the port. It is also observed PHY_ALT
1685 * interrupt missing upon Micro-A cable disconnect.
1686 * Hence disable PHY_ALT interrupt and perform
1687 * polling to detect RID change.
1688 */
1689 msm_chg_enable_aca_det(motg);
1690 msm_chg_disable_aca_intr(motg);
1691 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301692 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301693 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1694 switch (motg->chg_state) {
1695 case USB_CHG_STATE_UNDEFINED:
1696 msm_chg_detect_work(&motg->chg_work.work);
1697 break;
1698 case USB_CHG_STATE_DETECTED:
1699 switch (motg->chg_type) {
1700 case USB_DCP_CHARGER:
1701 msm_otg_notify_charger(motg,
1702 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301703 pm_runtime_put_noidle(otg->dev);
1704 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301705 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301706 case USB_ACA_B_CHARGER:
1707 msm_otg_notify_charger(motg,
1708 IDEV_ACA_CHG_MAX);
1709 /*
1710 * (ID_B --> ID_C) PHY_ALT interrupt can
1711 * not be detected in LPM.
1712 */
1713 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301714 case USB_CDP_CHARGER:
1715 msm_otg_notify_charger(motg,
1716 IDEV_CHG_MAX);
1717 msm_otg_start_peripheral(otg, 1);
1718 otg->state = OTG_STATE_B_PERIPHERAL;
1719 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301720 case USB_ACA_C_CHARGER:
1721 msm_otg_notify_charger(motg,
1722 IDEV_ACA_CHG_MAX);
1723 msm_otg_start_peripheral(otg, 1);
1724 otg->state = OTG_STATE_B_PERIPHERAL;
1725 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301726 case USB_SDP_CHARGER:
1727 msm_otg_notify_charger(motg, IUNIT);
1728 msm_otg_start_peripheral(otg, 1);
1729 otg->state = OTG_STATE_B_PERIPHERAL;
1730 break;
1731 default:
1732 break;
1733 }
1734 break;
1735 default:
1736 break;
1737 }
1738 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301740 msm_otg_notify_charger(motg, 0);
1741 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1742 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301743 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301744 pm_runtime_put_noidle(otg->dev);
1745 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301746 }
1747 break;
1748 case OTG_STATE_B_PERIPHERAL:
1749 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1750 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751 !test_bit(ID, &motg->inputs) ||
1752 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301753 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001754 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 schedule_work(w);
1756 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301757 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 }
1759 break;
1760 case OTG_STATE_A_HOST:
1761 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1762 if (test_bit(ID, &motg->inputs) &&
1763 !test_bit(ID_A, &motg->inputs)) {
1764 msm_otg_start_host(otg, 0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301765 if (motg->pdata->vbus_power) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001766 motg->pdata->vbus_power(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301767 msleep(100); /* TA_WAIT_VFALL */
1768 }
1769 /*
1770 * Exit point of host mode.
1771 *
1772 * 1. Micro-A cable disconnect: Just schedule
1773 * the work. PHY is reset in B_IDLE and LPM
1774 * is allowed.
1775 * 2. ID_GND --> ID_B: No need to reset the PHY.
1776 * HCD core clears all PORTSC bits and initializes
1777 * the controller to host mode in remove_hcd.
1778 * Restore PORTSC transceiver select bits (ULPI)
1779 * and reset the controller to change MODE bits.
1780 * PHY_ALT interrupt can not occur in host mode.
1781 */
1782 del_timer_sync(&motg->id_timer);
1783 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1784 msm_otg_link_reset(motg);
1785 msm_chg_enable_aca_intr(motg);
1786 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301787 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301788 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001789 } else if (test_bit(ID_A, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 if (motg->pdata->vbus_power)
1791 motg->pdata->vbus_power(0);
1792 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301793 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001794 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795 msm_otg_notify_charger(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001796 if (motg->pdata->vbus_power)
1797 motg->pdata->vbus_power(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301798 }
1799 break;
1800 default:
1801 break;
1802 }
1803}
1804
1805static irqreturn_t msm_otg_irq(int irq, void *data)
1806{
1807 struct msm_otg *motg = data;
1808 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301810
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301811 if (atomic_read(&motg->in_lpm)) {
1812 disable_irq_nosync(irq);
1813 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301814 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301815 return IRQ_HANDLED;
1816 }
1817
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001818 usbsts = readl(USB_USBSTS);
1819 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301820 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001821 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301822 if (msm_chg_check_aca_intr(motg)) {
1823 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301825 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001826 return IRQ_HANDLED;
1827 }
1828
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301829 otgsc = readl(USB_OTGSC);
1830 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1831 return IRQ_NONE;
1832
1833 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301834 if (otgsc & OTGSC_ID) {
1835 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301836 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301837 } else {
1838 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301839 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301840 msm_chg_enable_aca_det(motg);
1841 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301843 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301844 if (otgsc & OTGSC_BSV) {
1845 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301846 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301847 } else {
1848 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301849 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301850 msm_chg_check_aca_intr(motg);
1851 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301853 }
1854
1855 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001856 return IRQ_HANDLED;
1857}
1858
1859static void msm_otg_set_vbus_state(int online)
1860{
1861 struct msm_otg *motg = the_msm_otg;
1862
1863 /* We depend on PMIC for only VBUS ON interrupt */
Chiranjeevi Velempati62206042011-12-09 23:10:40 +05301864 if (!atomic_read(&motg->in_lpm) || !online || motg->async_int)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001865 return;
1866
1867 /*
1868 * Let interrupt handler take care of resuming
1869 * the hardware.
1870 */
1871 msm_otg_irq(motg->irq, (void *) motg);
1872}
1873
1874static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1875{
1876 struct msm_otg *motg = data;
1877
1878 if (atomic_read(&motg->in_lpm) && !motg->async_int)
1879 msm_otg_irq(motg->irq, motg);
1880
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301881 return IRQ_HANDLED;
1882}
1883
1884static int msm_otg_mode_show(struct seq_file *s, void *unused)
1885{
1886 struct msm_otg *motg = s->private;
1887 struct otg_transceiver *otg = &motg->otg;
1888
1889 switch (otg->state) {
1890 case OTG_STATE_A_HOST:
1891 seq_printf(s, "host\n");
1892 break;
1893 case OTG_STATE_B_PERIPHERAL:
1894 seq_printf(s, "peripheral\n");
1895 break;
1896 default:
1897 seq_printf(s, "none\n");
1898 break;
1899 }
1900
1901 return 0;
1902}
1903
1904static int msm_otg_mode_open(struct inode *inode, struct file *file)
1905{
1906 return single_open(file, msm_otg_mode_show, inode->i_private);
1907}
1908
1909static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1910 size_t count, loff_t *ppos)
1911{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301912 struct seq_file *s = file->private_data;
1913 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301914 char buf[16];
1915 struct otg_transceiver *otg = &motg->otg;
1916 int status = count;
1917 enum usb_mode_type req_mode;
1918
1919 memset(buf, 0x00, sizeof(buf));
1920
1921 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1922 status = -EFAULT;
1923 goto out;
1924 }
1925
1926 if (!strncmp(buf, "host", 4)) {
1927 req_mode = USB_HOST;
1928 } else if (!strncmp(buf, "peripheral", 10)) {
1929 req_mode = USB_PERIPHERAL;
1930 } else if (!strncmp(buf, "none", 4)) {
1931 req_mode = USB_NONE;
1932 } else {
1933 status = -EINVAL;
1934 goto out;
1935 }
1936
1937 switch (req_mode) {
1938 case USB_NONE:
1939 switch (otg->state) {
1940 case OTG_STATE_A_HOST:
1941 case OTG_STATE_B_PERIPHERAL:
1942 set_bit(ID, &motg->inputs);
1943 clear_bit(B_SESS_VLD, &motg->inputs);
1944 break;
1945 default:
1946 goto out;
1947 }
1948 break;
1949 case USB_PERIPHERAL:
1950 switch (otg->state) {
1951 case OTG_STATE_B_IDLE:
1952 case OTG_STATE_A_HOST:
1953 set_bit(ID, &motg->inputs);
1954 set_bit(B_SESS_VLD, &motg->inputs);
1955 break;
1956 default:
1957 goto out;
1958 }
1959 break;
1960 case USB_HOST:
1961 switch (otg->state) {
1962 case OTG_STATE_B_IDLE:
1963 case OTG_STATE_B_PERIPHERAL:
1964 clear_bit(ID, &motg->inputs);
1965 break;
1966 default:
1967 goto out;
1968 }
1969 break;
1970 default:
1971 goto out;
1972 }
1973
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301974 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301975 schedule_work(&motg->sm_work);
1976out:
1977 return status;
1978}
1979
1980const struct file_operations msm_otg_mode_fops = {
1981 .open = msm_otg_mode_open,
1982 .read = seq_read,
1983 .write = msm_otg_mode_write,
1984 .llseek = seq_lseek,
1985 .release = single_release,
1986};
1987
Anji jonnalad270e2d2011-08-09 11:28:32 +05301988static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
1989{
1990 struct msm_otg *motg = s->private;
1991
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05301992 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05301993 return 0;
1994}
1995
1996static int msm_otg_chg_open(struct inode *inode, struct file *file)
1997{
1998 return single_open(file, msm_otg_show_chg_type, inode->i_private);
1999}
2000
2001const struct file_operations msm_otg_chg_fops = {
2002 .open = msm_otg_chg_open,
2003 .read = seq_read,
2004 .llseek = seq_lseek,
2005 .release = single_release,
2006};
2007
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302008static int msm_otg_aca_show(struct seq_file *s, void *unused)
2009{
2010 if (debug_aca_enabled)
2011 seq_printf(s, "enabled\n");
2012 else
2013 seq_printf(s, "disabled\n");
2014
2015 return 0;
2016}
2017
2018static int msm_otg_aca_open(struct inode *inode, struct file *file)
2019{
2020 return single_open(file, msm_otg_aca_show, inode->i_private);
2021}
2022
2023static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2024 size_t count, loff_t *ppos)
2025{
2026 char buf[8];
2027
2028 memset(buf, 0x00, sizeof(buf));
2029
2030 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2031 return -EFAULT;
2032
2033 if (!strncmp(buf, "enable", 6))
2034 debug_aca_enabled = true;
2035 else
2036 debug_aca_enabled = false;
2037
2038 return count;
2039}
2040
2041const struct file_operations msm_otg_aca_fops = {
2042 .open = msm_otg_aca_open,
2043 .read = seq_read,
2044 .write = msm_otg_aca_write,
2045 .llseek = seq_lseek,
2046 .release = single_release,
2047};
2048
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302049static struct dentry *msm_otg_dbg_root;
2050static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302051static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302052static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302053
2054static int msm_otg_debugfs_init(struct msm_otg *motg)
2055{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302056
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302057 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2058
2059 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2060 return -ENODEV;
2061
Anji jonnalad270e2d2011-08-09 11:28:32 +05302062 if (motg->pdata->mode == USB_OTG &&
2063 motg->pdata->otg_control == OTG_USER_CONTROL) {
2064
2065 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2066 S_IWUSR, msm_otg_dbg_root, motg,
2067 &msm_otg_mode_fops);
2068
2069 if (!msm_otg_dbg_mode) {
2070 debugfs_remove(msm_otg_dbg_root);
2071 msm_otg_dbg_root = NULL;
2072 return -ENODEV;
2073 }
2074 }
2075
2076 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2077 msm_otg_dbg_root, motg,
2078 &msm_otg_chg_fops);
2079
2080 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302081 debugfs_remove_recursive(msm_otg_dbg_root);
2082 return -ENODEV;
2083 }
2084
2085 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2086 msm_otg_dbg_root, motg,
2087 &msm_otg_aca_fops);
2088
2089 if (!msm_otg_dbg_aca) {
2090 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302091 return -ENODEV;
2092 }
2093
2094 return 0;
2095}
2096
2097static void msm_otg_debugfs_cleanup(void)
2098{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302099 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302100}
2101
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302102static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2103static struct platform_device *msm_otg_add_pdev(
2104 struct platform_device *ofdev, const char *name)
2105{
2106 struct platform_device *pdev;
2107 const struct resource *res = ofdev->resource;
2108 unsigned int num = ofdev->num_resources;
2109 int retval;
2110
2111 pdev = platform_device_alloc(name, -1);
2112 if (!pdev) {
2113 retval = -ENOMEM;
2114 goto error;
2115 }
2116
2117 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2118 pdev->dev.dma_mask = &msm_otg_dma_mask;
2119
2120 if (num) {
2121 retval = platform_device_add_resources(pdev, res, num);
2122 if (retval)
2123 goto error;
2124 }
2125
2126 retval = platform_device_add(pdev);
2127 if (retval)
2128 goto error;
2129
2130 return pdev;
2131
2132error:
2133 platform_device_put(pdev);
2134 return ERR_PTR(retval);
2135}
2136
2137static int msm_otg_setup_devices(struct platform_device *ofdev,
2138 enum usb_mode_type mode, bool init)
2139{
2140 const char *gadget_name = "msm_hsusb";
2141 const char *host_name = "msm_hsusb_host";
2142 static struct platform_device *gadget_pdev;
2143 static struct platform_device *host_pdev;
2144 int retval = 0;
2145
2146 if (!init) {
2147 if (gadget_pdev)
2148 platform_device_unregister(gadget_pdev);
2149 if (host_pdev)
2150 platform_device_unregister(host_pdev);
2151 return 0;
2152 }
2153
2154 switch (mode) {
2155 case USB_OTG:
2156 /* fall through */
2157 case USB_PERIPHERAL:
2158 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2159 if (IS_ERR(gadget_pdev)) {
2160 retval = PTR_ERR(gadget_pdev);
2161 break;
2162 }
2163 if (mode == USB_PERIPHERAL)
2164 break;
2165 /* fall through */
2166 case USB_HOST:
2167 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2168 if (IS_ERR(host_pdev)) {
2169 retval = PTR_ERR(host_pdev);
2170 if (mode == USB_OTG)
2171 platform_device_unregister(gadget_pdev);
2172 }
2173 break;
2174 default:
2175 break;
2176 }
2177
2178 return retval;
2179}
2180
2181struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2182{
2183 struct device_node *node = pdev->dev.of_node;
2184 struct msm_otg_platform_data *pdata;
2185 int len = 0;
2186
2187 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2188 if (!pdata) {
2189 pr_err("unable to allocate platform data\n");
2190 return NULL;
2191 }
2192 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2193 if (len) {
2194 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2195 if (!pdata->phy_init_seq)
2196 return NULL;
2197 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2198 pdata->phy_init_seq,
2199 len/sizeof(*pdata->phy_init_seq));
2200 }
2201 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2202 &pdata->power_budget);
2203 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2204 &pdata->mode);
2205 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2206 &pdata->otg_control);
2207 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2208 &pdata->default_mode);
2209 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2210 &pdata->phy_type);
2211 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2212 &pdata->pmic_id_irq);
2213 of_property_read_string(node, "qcom,hsusb-otg-pclk-src-name",
2214 &pdata->pclk_src_name);
2215 return pdata;
2216}
2217
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302218static int __init msm_otg_probe(struct platform_device *pdev)
2219{
2220 int ret = 0;
2221 struct resource *res;
2222 struct msm_otg *motg;
2223 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302224 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302225
2226 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302227
2228 if (pdev->dev.of_node) {
2229 dev_dbg(&pdev->dev, "device tree enabled\n");
2230 pdata = msm_otg_dt_to_pdata(pdev);
2231 if (!pdata)
2232 return -ENOMEM;
2233 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2234 if (ret) {
2235 dev_err(&pdev->dev, "devices setup failed\n");
2236 return ret;
2237 }
2238 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302239 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2240 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302241 } else {
2242 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302243 }
2244
2245 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2246 if (!motg) {
2247 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2248 return -ENOMEM;
2249 }
2250
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002251 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302252 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302253 otg = &motg->otg;
2254 otg->dev = &pdev->dev;
2255
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302256 /*
2257 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2258 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2259 * PMIC can detect ACA ID_GND and generate an interrupt.
2260 */
2261 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2262 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2263 ret = -EINVAL;
2264 goto free_motg;
2265 }
2266
Amit Blay02eff132011-09-21 16:46:24 +03002267 /* Some targets don't support PHY clock. */
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302268 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002269 if (IS_ERR(motg->phy_reset_clk))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302270 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302271
2272 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
2273 if (IS_ERR(motg->clk)) {
2274 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
2275 ret = PTR_ERR(motg->clk);
2276 goto put_phy_reset_clk;
2277 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302278 clk_set_rate(motg->clk, 60000000);
2279
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302280 /* pm qos request to prevent apps idle power collapse */
2281 if (motg->pdata->swfi_latency)
2282 pm_qos_add_request(&motg->pm_qos_req_dma,
2283 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302284 /*
2285 * If USB Core is running its protocol engine based on CORE CLK,
2286 * CORE CLK must be running at >55Mhz for correct HSUSB
2287 * operation and USB core cannot tolerate frequency changes on
2288 * CORE CLK. For such USB cores, vote for maximum clk frequency
2289 * on pclk source
2290 */
2291 if (motg->pdata->pclk_src_name) {
2292 motg->pclk_src = clk_get(&pdev->dev,
2293 motg->pdata->pclk_src_name);
2294 if (IS_ERR(motg->pclk_src))
2295 goto put_clk;
2296 clk_set_rate(motg->pclk_src, INT_MAX);
2297 clk_enable(motg->pclk_src);
2298 } else
2299 motg->pclk_src = ERR_PTR(-ENOENT);
2300
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302301 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
2302 if (IS_ERR(motg->pclk)) {
2303 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
2304 ret = PTR_ERR(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302305 goto put_pclk_src;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302306 }
2307
Amit Blay02eff132011-09-21 16:46:24 +03002308 motg->system_clk = clk_get(&pdev->dev, "usb_hs_system_clk");
2309 if (!IS_ERR(motg->system_clk))
2310 clk_enable(motg->system_clk);
2311
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302312 /*
2313 * USB core clock is not present on all MSM chips. This
2314 * clock is introduced to remove the dependency on AXI
2315 * bus frequency.
2316 */
2317 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
2318 if (IS_ERR(motg->core_clk))
2319 motg->core_clk = NULL;
2320
2321 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2322 if (!res) {
2323 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2324 ret = -ENODEV;
2325 goto put_core_clk;
2326 }
2327
2328 motg->regs = ioremap(res->start, resource_size(res));
2329 if (!motg->regs) {
2330 dev_err(&pdev->dev, "ioremap failed\n");
2331 ret = -ENOMEM;
2332 goto put_core_clk;
2333 }
2334 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2335
2336 motg->irq = platform_get_irq(pdev, 0);
2337 if (!motg->irq) {
2338 dev_err(&pdev->dev, "platform_get_irq failed\n");
2339 ret = -ENODEV;
2340 goto free_regs;
2341 }
2342
Anji jonnala7da3f262011-12-02 17:22:14 -08002343 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2344 if (IS_ERR(motg->xo_handle)) {
2345 dev_err(&pdev->dev, "%s not able to get the handle "
2346 "to vote for TCXO D0 buffer\n", __func__);
2347 ret = PTR_ERR(motg->xo_handle);
2348 goto free_regs;
2349 }
2350
2351 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2352 if (ret) {
2353 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2354 "D0 buffer%d\n", __func__, ret);
2355 goto free_xo_handle;
2356 }
2357
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302358 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302359
2360 ret = msm_hsusb_init_vddcx(motg, 1);
2361 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002363 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302364 }
2365
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002366 ret = msm_hsusb_config_vddcx(1);
2367 if (ret) {
2368 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2369 goto free_init_vddcx;
2370 }
2371
Anji jonnala11aa5c42011-05-04 10:19:48 +05302372 ret = msm_hsusb_ldo_init(motg, 1);
2373 if (ret) {
2374 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002375 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302376 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002377
2378 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302379 if (ret) {
2380 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002381 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302382 }
2383
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302384 if (motg->core_clk)
2385 clk_enable(motg->core_clk);
2386
2387 writel(0, USB_USBINTR);
2388 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002389 /* Ensure that above STOREs are completed before enabling interrupts */
2390 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302391
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002392 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302393 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302394 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302395 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2396 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302397 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2398 "msm_otg", motg);
2399 if (ret) {
2400 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002401 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302402 }
2403
2404 otg->init = msm_otg_reset;
2405 otg->set_host = msm_otg_set_host;
2406 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302407 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302408 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302409
2410 otg->io_ops = &msm_otg_io_ops;
2411
2412 ret = otg_set_transceiver(&motg->otg);
2413 if (ret) {
2414 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2415 goto free_irq;
2416 }
2417
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2419 if (motg->pdata->pmic_id_irq) {
2420 ret = request_irq(motg->pdata->pmic_id_irq,
2421 msm_pmic_id_irq,
2422 IRQF_TRIGGER_RISING |
2423 IRQF_TRIGGER_FALLING,
2424 "msm_otg", motg);
2425 if (ret) {
2426 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2427 goto remove_otg;
2428 }
2429 } else {
2430 ret = -ENODEV;
2431 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2432 goto remove_otg;
2433 }
2434 }
2435
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302436 msm_hsusb_mhl_switch_enable(motg, 1);
2437
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302438 platform_set_drvdata(pdev, motg);
2439 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002440 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302441
Anji jonnalad270e2d2011-08-09 11:28:32 +05302442 ret = msm_otg_debugfs_init(motg);
2443 if (ret)
2444 dev_dbg(&pdev->dev, "mode debugfs file is"
2445 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302446
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002447 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2448 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2449
Amit Blay58b31472011-11-18 09:39:39 +02002450 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2451 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002452 motg->pdata->pmic_id_irq)
Amit Blay58b31472011-11-18 09:39:39 +02002453 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002454 ALLOW_PHY_RETENTION |
2455 ALLOW_PHY_COMP_DISABLE;
2456
Amit Blay58b31472011-11-18 09:39:39 +02002457 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2458 motg->caps = ALLOW_PHY_RETENTION;
2459 }
2460
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302462 pm_runtime_set_active(&pdev->dev);
2463 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302464
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302465 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002466
2467remove_otg:
2468 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302469free_irq:
2470 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471destroy_wlock:
2472 wake_lock_destroy(&motg->wlock);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302473 clk_disable(motg->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002474 msm_hsusb_ldo_enable(motg, 0);
2475free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302476 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002477free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302478 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002479devote_xo_handle:
2480 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2481free_xo_handle:
2482 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302483free_regs:
2484 iounmap(motg->regs);
2485put_core_clk:
2486 if (motg->core_clk)
2487 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002488
2489 if (!IS_ERR(motg->system_clk)) {
2490 clk_disable(motg->system_clk);
2491 clk_put(motg->system_clk);
2492 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302493put_pclk_src:
2494 if (!IS_ERR(motg->pclk_src)) {
2495 clk_disable(motg->pclk_src);
2496 clk_put(motg->pclk_src);
2497 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302498put_clk:
2499 clk_put(motg->clk);
2500put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002501 if (!IS_ERR(motg->phy_reset_clk))
2502 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302503free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302504 if (motg->pdata->swfi_latency)
2505 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302506 kfree(motg);
2507 return ret;
2508}
2509
2510static int __devexit msm_otg_remove(struct platform_device *pdev)
2511{
2512 struct msm_otg *motg = platform_get_drvdata(pdev);
2513 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302514 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302515
2516 if (otg->host || otg->gadget)
2517 return -EBUSY;
2518
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302519 if (pdev->dev.of_node)
2520 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2522 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302523 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302524 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302525 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302526
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302527 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302528
2529 device_init_wakeup(&pdev->dev, 0);
2530 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302532
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302533 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534 if (motg->pdata->pmic_id_irq)
2535 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302536 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302537 free_irq(motg->irq, motg);
2538
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302539 /*
2540 * Put PHY in low power mode.
2541 */
2542 ulpi_read(otg, 0x14);
2543 ulpi_write(otg, 0x08, 0x09);
2544
2545 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2546 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2547 if (readl(USB_PORTSC) & PORTSC_PHCD)
2548 break;
2549 udelay(1);
2550 cnt++;
2551 }
2552 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2553 dev_err(otg->dev, "Unable to suspend PHY\n");
2554
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302555 clk_disable(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302556 if (motg->core_clk)
2557 clk_disable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +02002558 if (!IS_ERR(motg->system_clk))
2559 clk_disable(motg->system_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302560 if (!IS_ERR(motg->pclk_src)) {
2561 clk_disable(motg->pclk_src);
2562 clk_put(motg->pclk_src);
2563 }
Anji jonnala7da3f262011-12-02 17:22:14 -08002564 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002565 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302566 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002567 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302568
2569 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302570 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571
Amit Blay02eff132011-09-21 16:46:24 +03002572 if (!IS_ERR(motg->phy_reset_clk))
2573 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302574 clk_put(motg->pclk);
2575 clk_put(motg->clk);
2576 if (motg->core_clk)
2577 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002578 if (!IS_ERR(motg->system_clk))
2579 clk_put(motg->system_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302580
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302581 if (motg->pdata->swfi_latency)
2582 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302583
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302584 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302585 return 0;
2586}
2587
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302588#ifdef CONFIG_PM_RUNTIME
2589static int msm_otg_runtime_idle(struct device *dev)
2590{
2591 struct msm_otg *motg = dev_get_drvdata(dev);
2592 struct otg_transceiver *otg = &motg->otg;
2593
2594 dev_dbg(dev, "OTG runtime idle\n");
2595
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302596 if (otg->state == OTG_STATE_UNDEFINED)
2597 return -EAGAIN;
2598 else
2599 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302600}
2601
2602static int msm_otg_runtime_suspend(struct device *dev)
2603{
2604 struct msm_otg *motg = dev_get_drvdata(dev);
2605
2606 dev_dbg(dev, "OTG runtime suspend\n");
2607 return msm_otg_suspend(motg);
2608}
2609
2610static int msm_otg_runtime_resume(struct device *dev)
2611{
2612 struct msm_otg *motg = dev_get_drvdata(dev);
2613
2614 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302615 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302616 return msm_otg_resume(motg);
2617}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302618#endif
2619
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302620#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302621static int msm_otg_pm_suspend(struct device *dev)
2622{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302623 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302624
2625 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302626
2627#ifdef CONFIG_PM_RUNTIME
2628 ret = pm_runtime_suspend(dev);
2629 if (ret > 0)
2630 ret = 0;
2631#else
2632 ret = msm_otg_suspend(dev_get_drvdata(dev));
2633#endif
2634 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302635}
2636
2637static int msm_otg_pm_resume(struct device *dev)
2638{
2639 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302640
2641 dev_dbg(dev, "OTG PM resume\n");
2642
Manu Gautamf284c052011-09-08 16:52:48 +05302643#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302644 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302645 * Do not resume hardware as part of system resume,
2646 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302647 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002648 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302649#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302650
Manu Gautamf284c052011-09-08 16:52:48 +05302651 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302652}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302653#endif
2654
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302655#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302656static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302657 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2658 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2659 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302660};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302661#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302662
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302663static struct of_device_id msm_otg_dt_match[] = {
2664 { .compatible = "qcom,hsusb-otg",
2665 },
2666 {}
2667};
2668
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302669static struct platform_driver msm_otg_driver = {
2670 .remove = __devexit_p(msm_otg_remove),
2671 .driver = {
2672 .name = DRIVER_NAME,
2673 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302674#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302675 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302676#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302677 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302678 },
2679};
2680
2681static int __init msm_otg_init(void)
2682{
2683 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2684}
2685
2686static void __exit msm_otg_exit(void)
2687{
2688 platform_driver_unregister(&msm_otg_driver);
2689}
2690
2691module_init(msm_otg_init);
2692module_exit(msm_otg_exit);
2693
2694MODULE_LICENSE("GPL v2");
2695MODULE_DESCRIPTION("MSM USB transceiver driver");