blob: 462272562c94b8ef5d130e4650cbea18372e6e54 [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>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053040
41#include <mach/clk.h>
42
43#define MSM_USB_BASE (motg->regs)
44#define DRIVER_NAME "msm_otg"
45
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053046#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053047#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053048
49#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
50#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
51#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
52#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
53
54#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
55#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
56#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
57#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
58
Vamsi Krishna132b2762011-11-11 16:09:20 -080059#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053060#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
61
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053063static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064
Anji jonnala11aa5c42011-05-04 10:19:48 +053065static struct regulator *hsusb_3p3;
66static struct regulator *hsusb_1p8;
67static struct regulator *hsusb_vddcx;
68
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053069static inline bool aca_enabled(void)
70{
71#ifdef CONFIG_USB_MSM_ACA
72 return true;
73#else
74 return debug_aca_enabled;
75#endif
76}
77
Anji jonnala11aa5c42011-05-04 10:19:48 +053078static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
79{
80 int ret = 0;
81
82 if (init) {
83 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
84 if (IS_ERR(hsusb_vddcx)) {
85 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
86 return PTR_ERR(hsusb_vddcx);
87 }
88
89 ret = regulator_set_voltage(hsusb_vddcx,
90 USB_PHY_VDD_DIG_VOL_MIN,
91 USB_PHY_VDD_DIG_VOL_MAX);
92 if (ret) {
93 dev_err(motg->otg.dev, "unable to set the voltage "
94 "for hsusb vddcx\n");
95 regulator_put(hsusb_vddcx);
96 return ret;
97 }
98
99 ret = regulator_enable(hsusb_vddcx);
100 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 regulator_set_voltage(hsusb_vddcx, 0,
102 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530103 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
105 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107
Anji jonnala11aa5c42011-05-04 10:19:48 +0530108 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700109
Anji jonnala11aa5c42011-05-04 10:19:48 +0530110 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700111 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 return ret;
114 }
115
116 ret = regulator_set_voltage(hsusb_vddcx, 0,
117 USB_PHY_VDD_DIG_VOL_MIN);
118 if (ret) {
119 dev_err(motg->otg.dev, "unable to set the voltage"
120 "for hsusb vddcx\n");
121 return ret;
122 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530123
124 regulator_put(hsusb_vddcx);
125 }
126
127 return ret;
128}
129
130static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
131{
132 int rc = 0;
133
134 if (init) {
135 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
136 if (IS_ERR(hsusb_3p3)) {
137 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
138 return PTR_ERR(hsusb_3p3);
139 }
140
141 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
142 USB_PHY_3P3_VOL_MAX);
143 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144 dev_err(motg->otg.dev, "unable to set voltage level for"
145 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530146 goto put_3p3;
147 }
148 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
149 if (IS_ERR(hsusb_1p8)) {
150 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
151 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530153 }
154 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
155 USB_PHY_1P8_VOL_MAX);
156 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157 dev_err(motg->otg.dev, "unable to set voltage level for"
158 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530159 goto put_1p8;
160 }
161
162 return 0;
163 }
164
Anji jonnala11aa5c42011-05-04 10:19:48 +0530165put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530167 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168put_3p3_lpm:
169 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530170put_3p3:
171 regulator_put(hsusb_3p3);
172 return rc;
173}
174
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530175#ifdef CONFIG_PM_SLEEP
176#define USB_PHY_SUSP_DIG_VOL 500000
177static int msm_hsusb_config_vddcx(int high)
178{
179 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
180 int min_vol;
181 int ret;
182
183 if (high)
184 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
185 else
186 min_vol = USB_PHY_SUSP_DIG_VOL;
187
188 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
189 if (ret) {
190 pr_err("%s: unable to set the voltage for regulator "
191 "HSUSB_VDDCX\n", __func__);
192 return ret;
193 }
194
195 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
196
197 return ret;
198}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700199#else
200static int msm_hsusb_config_vddcx(int high)
201{
202 return 0;
203}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530204#endif
205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207{
208 int ret = 0;
209
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530210 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530211 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
212 return -ENODEV;
213 }
214
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530215 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530216 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
217 return -ENODEV;
218 }
219
220 if (on) {
221 ret = regulator_set_optimum_mode(hsusb_1p8,
222 USB_PHY_1P8_HPM_LOAD);
223 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530225 "HSUSB_1p8\n", __func__);
226 return ret;
227 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228
229 ret = regulator_enable(hsusb_1p8);
230 if (ret) {
231 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
232 __func__);
233 regulator_set_optimum_mode(hsusb_1p8, 0);
234 return ret;
235 }
236
Anji jonnala11aa5c42011-05-04 10:19:48 +0530237 ret = regulator_set_optimum_mode(hsusb_3p3,
238 USB_PHY_3P3_HPM_LOAD);
239 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242 regulator_set_optimum_mode(hsusb_1p8, 0);
243 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530244 return ret;
245 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246
247 ret = regulator_enable(hsusb_3p3);
248 if (ret) {
249 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
250 __func__);
251 regulator_set_optimum_mode(hsusb_3p3, 0);
252 regulator_set_optimum_mode(hsusb_1p8, 0);
253 regulator_disable(hsusb_1p8);
254 return ret;
255 }
256
Anji jonnala11aa5c42011-05-04 10:19:48 +0530257 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 ret = regulator_disable(hsusb_1p8);
259 if (ret) {
260 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
261 __func__);
262 return ret;
263 }
264
265 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530268 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269
270 ret = regulator_disable(hsusb_3p3);
271 if (ret) {
272 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
273 __func__);
274 return ret;
275 }
276 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530277 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700278 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530279 "HSUSB_3p3\n", __func__);
280 }
281
282 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
283 return ret < 0 ? ret : 0;
284}
285
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530286static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
287{
288 static struct regulator *mhl_analog_switch;
289 struct msm_otg_platform_data *pdata = motg->pdata;
290
291 if (!pdata->mhl_enable)
292 return;
293
294 if (on) {
295 mhl_analog_switch = regulator_get(motg->otg.dev,
296 "mhl_ext_3p3v");
297 if (IS_ERR(mhl_analog_switch)) {
298 pr_err("Unable to get mhl_analog_switch\n");
299 return;
300 }
301
302 if (regulator_enable(mhl_analog_switch)) {
303 pr_err("unable to enable mhl_analog_switch\n");
304 goto put_analog_switch;
305 }
306 return;
307 }
308
309 regulator_disable(mhl_analog_switch);
310put_analog_switch:
311 regulator_put(mhl_analog_switch);
312}
313
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530314static int ulpi_read(struct otg_transceiver *otg, u32 reg)
315{
316 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
317 int cnt = 0;
318
319 /* initiate read operation */
320 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
321 USB_ULPI_VIEWPORT);
322
323 /* wait for completion */
324 while (cnt < ULPI_IO_TIMEOUT_USEC) {
325 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
326 break;
327 udelay(1);
328 cnt++;
329 }
330
331 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
332 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
333 readl(USB_ULPI_VIEWPORT));
334 return -ETIMEDOUT;
335 }
336 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
337}
338
339static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
340{
341 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
342 int cnt = 0;
343
344 /* initiate write operation */
345 writel(ULPI_RUN | ULPI_WRITE |
346 ULPI_ADDR(reg) | ULPI_DATA(val),
347 USB_ULPI_VIEWPORT);
348
349 /* wait for completion */
350 while (cnt < ULPI_IO_TIMEOUT_USEC) {
351 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
352 break;
353 udelay(1);
354 cnt++;
355 }
356
357 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
358 dev_err(otg->dev, "ulpi_write: timeout\n");
359 return -ETIMEDOUT;
360 }
361 return 0;
362}
363
364static struct otg_io_access_ops msm_otg_io_ops = {
365 .read = ulpi_read,
366 .write = ulpi_write,
367};
368
369static void ulpi_init(struct msm_otg *motg)
370{
371 struct msm_otg_platform_data *pdata = motg->pdata;
372 int *seq = pdata->phy_init_seq;
373
374 if (!seq)
375 return;
376
377 while (seq[0] >= 0) {
378 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
379 seq[0], seq[1]);
380 ulpi_write(&motg->otg, seq[0], seq[1]);
381 seq += 2;
382 }
383}
384
385static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
386{
387 int ret;
388
389 if (assert) {
390 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
391 if (ret)
392 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
393 } else {
394 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
395 if (ret)
396 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
397 }
398 return ret;
399}
400
401static int msm_otg_phy_clk_reset(struct msm_otg *motg)
402{
403 int ret;
404
Amit Blay02eff132011-09-21 16:46:24 +0300405 if (IS_ERR(motg->phy_reset_clk))
406 return 0;
407
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530408 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
409 if (ret) {
410 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
411 return ret;
412 }
413 usleep_range(10000, 12000);
414 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
415 if (ret)
416 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
417 return ret;
418}
419
420static int msm_otg_phy_reset(struct msm_otg *motg)
421{
422 u32 val;
423 int ret;
424 int retries;
425
426 ret = msm_otg_link_clk_reset(motg, 1);
427 if (ret)
428 return ret;
429 ret = msm_otg_phy_clk_reset(motg);
430 if (ret)
431 return ret;
432 ret = msm_otg_link_clk_reset(motg, 0);
433 if (ret)
434 return ret;
435
436 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
437 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
438
439 for (retries = 3; retries > 0; retries--) {
440 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
441 ULPI_CLR(ULPI_FUNC_CTRL));
442 if (!ret)
443 break;
444 ret = msm_otg_phy_clk_reset(motg);
445 if (ret)
446 return ret;
447 }
448 if (!retries)
449 return -ETIMEDOUT;
450
451 /* This reset calibrates the phy, if the above write succeeded */
452 ret = msm_otg_phy_clk_reset(motg);
453 if (ret)
454 return ret;
455
456 for (retries = 3; retries > 0; retries--) {
457 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
458 if (ret != -ETIMEDOUT)
459 break;
460 ret = msm_otg_phy_clk_reset(motg);
461 if (ret)
462 return ret;
463 }
464 if (!retries)
465 return -ETIMEDOUT;
466
467 dev_info(motg->otg.dev, "phy_reset: success\n");
468 return 0;
469}
470
471#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530472static int msm_otg_link_reset(struct msm_otg *motg)
473{
474 int cnt = 0;
475
476 writel_relaxed(USBCMD_RESET, USB_USBCMD);
477 while (cnt < LINK_RESET_TIMEOUT_USEC) {
478 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
479 break;
480 udelay(1);
481 cnt++;
482 }
483 if (cnt >= LINK_RESET_TIMEOUT_USEC)
484 return -ETIMEDOUT;
485
486 /* select ULPI phy */
487 writel_relaxed(0x80000000, USB_PORTSC);
488 writel_relaxed(0x0, USB_AHBBURST);
489 writel_relaxed(0x00, USB_AHBMODE);
490
491 return 0;
492}
493
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530494static int msm_otg_reset(struct otg_transceiver *otg)
495{
496 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
497 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530498 int ret;
499 u32 val = 0;
500 u32 ulpi_val = 0;
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530503 ret = msm_otg_phy_reset(motg);
504 if (ret) {
505 dev_err(otg->dev, "phy_reset failed\n");
506 return ret;
507 }
508
509 ulpi_init(motg);
510
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530511 ret = msm_otg_link_reset(motg);
512 if (ret) {
513 dev_err(otg->dev, "link reset failed\n");
514 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530515 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530516 msleep(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700517 /* Ensure that RESET operation is completed before turning off clock */
518 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700520 clk_disable(motg->clk);
521
522 val = readl_relaxed(USB_OTGSC);
523 if (pdata->mode == USB_OTG) {
524 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
525 val |= OTGSC_IDIE | OTGSC_BSVIE;
526 } else if (pdata->mode == USB_PERIPHERAL) {
527 ulpi_val = ULPI_INT_SESS_VALID;
528 val |= OTGSC_BSVIE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530529 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700530 writel_relaxed(val, USB_OTGSC);
531 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
532 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
533
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530534 return 0;
535}
536
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530537static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
538{
539 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
540
541 /*
542 * Allow bus suspend only for host mode. Device mode bus suspend
543 * is not implemented yet.
544 */
545 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 /*
547 * ID_GND --> ID_A transition can not be detected in LPM.
548 * Disallow host bus suspend when ACA is enabled.
549 */
550 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530551 pm_runtime_put(otg->dev);
552 else
553 pm_runtime_resume(otg->dev);
554 }
555
556 return 0;
557}
558
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530559#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530560#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
561
562#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530563static int msm_otg_suspend(struct msm_otg *motg)
564{
565 struct otg_transceiver *otg = &motg->otg;
566 struct usb_bus *bus = otg->host;
567 struct msm_otg_platform_data *pdata = motg->pdata;
568 int cnt = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700569 bool session_active;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530570
571 if (atomic_read(&motg->in_lpm))
572 return 0;
573
574 disable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700575 session_active = (otg->host && !test_bit(ID, &motg->inputs)) ||
576 test_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530577 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530578 * Chipidea 45-nm PHY suspend sequence:
579 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530580 * Interrupt Latch Register auto-clear feature is not present
581 * in all PHY versions. Latch register is clear on read type.
582 * Clear latch register to avoid spurious wakeup from
583 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530584 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530585 * PHY comparators are disabled when PHY enters into low power
586 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
587 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
588 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530589 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530590 * PLL is not turned off when PHY enters into low power mode (LPM).
591 * Disable PLL for maximum power savings.
592 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530593
594 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
595 ulpi_read(otg, 0x14);
596 if (pdata->otg_control == OTG_PHY_CONTROL)
597 ulpi_write(otg, 0x01, 0x30);
598 ulpi_write(otg, 0x08, 0x09);
599 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530600
601 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700602 * Turn off the OTG comparators, if depends on PMIC for
603 * VBUS and ID notifications.
604 */
605 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !session_active) {
606 ulpi_write(otg, OTG_COMP_DISABLE,
607 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
608 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
609 }
610
611 /*
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530612 * PHY may take some time or even fail to enter into low power
613 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
614 * in failure case.
615 */
616 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
617 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
618 if (readl(USB_PORTSC) & PORTSC_PHCD)
619 break;
620 udelay(1);
621 cnt++;
622 }
623
624 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
625 dev_err(otg->dev, "Unable to suspend PHY\n");
626 msm_otg_reset(otg);
627 enable_irq(motg->irq);
628 return -ETIMEDOUT;
629 }
630
631 /*
632 * PHY has capability to generate interrupt asynchronously in low
633 * power mode (LPM). This interrupt is level triggered. So USB IRQ
634 * line must be disabled till async interrupt enable bit is cleared
635 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
636 * block data communication from PHY.
637 */
638 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640 if (motg->caps & ALLOW_PHY_RETENTION && !session_active) {
641 writel_relaxed(readl_relaxed(USB_PHY_CTRL) & ~PHY_RETEN,
642 USB_PHY_CTRL);
643 motg->lpm_flags |= PHY_RETENTIONED;
644 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530645
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 /* Ensure that above operation is completed before turning off clocks */
647 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530648 clk_disable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530649 if (motg->core_clk)
650 clk_disable(motg->core_clk);
651
Amit Blay137575f2011-11-06 15:20:54 +0200652 if (!IS_ERR(motg->system_clk))
653 clk_disable(motg->system_clk);
654
Anji jonnala0f73cac2011-05-04 10:19:46 +0530655 if (!IS_ERR(motg->pclk_src))
656 clk_disable(motg->pclk_src);
657
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700658 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !session_active) {
659 msm_hsusb_ldo_enable(motg, 0);
660 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530661 }
662
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530663 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530665 msm_hsusb_mhl_switch_enable(motg, 0);
666 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700667
668 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530669 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700670 if (motg->pdata->pmic_id_irq)
671 enable_irq_wake(motg->pdata->pmic_id_irq);
672 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530673 if (bus)
674 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
675
676 atomic_set(&motg->in_lpm, 1);
677 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530679
680 dev_info(otg->dev, "USB in low power mode\n");
681
682 return 0;
683}
684
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530685static int msm_otg_resume(struct msm_otg *motg)
686{
687 struct otg_transceiver *otg = &motg->otg;
688 struct usb_bus *bus = otg->host;
689 int cnt = 0;
690 unsigned temp;
691
692 if (!atomic_read(&motg->in_lpm))
693 return 0;
694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700695 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530696 if (!IS_ERR(motg->pclk_src))
697 clk_enable(motg->pclk_src);
698
Amit Blay137575f2011-11-06 15:20:54 +0200699 if (!IS_ERR(motg->system_clk))
700 clk_enable(motg->system_clk);
701
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530702 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530703 if (motg->core_clk)
704 clk_enable(motg->core_clk);
705
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700706 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
707 msm_hsusb_ldo_enable(motg, 1);
708 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
709 }
710
711 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530712 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530713 msm_hsusb_config_vddcx(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 writel_relaxed(readl_relaxed(USB_PHY_CTRL) | PHY_RETEN,
715 USB_PHY_CTRL);
716 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530717 }
718
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530719 temp = readl(USB_USBCMD);
720 temp &= ~ASYNC_INTR_CTRL;
721 temp &= ~ULPI_STP_CTRL;
722 writel(temp, USB_USBCMD);
723
724 /*
725 * PHY comes out of low power mode (LPM) in case of wakeup
726 * from asynchronous interrupt.
727 */
728 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
729 goto skip_phy_resume;
730
731 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
732 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
733 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
734 break;
735 udelay(1);
736 cnt++;
737 }
738
739 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
740 /*
741 * This is a fatal error. Reset the link and
742 * PHY. USB state can not be restored. Re-insertion
743 * of USB cable is the only way to get USB working.
744 */
745 dev_err(otg->dev, "Unable to resume USB."
746 "Re-plugin the cable\n");
747 msm_otg_reset(otg);
748 }
749
750skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700751 /* Turn on the OTG comparators on resume */
752 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
753 ulpi_write(otg, OTG_COMP_DISABLE,
754 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
755 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
756 }
757 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530758 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 if (motg->pdata->pmic_id_irq)
760 disable_irq_wake(motg->pdata->pmic_id_irq);
761 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762 if (bus)
763 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
764
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530765 atomic_set(&motg->in_lpm, 0);
766
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530767 if (aca_enabled() && !irq_read_line(motg->pdata->pmic_id_irq)) {
768 clear_bit(ID, &motg->inputs);
769 schedule_work(&motg->sm_work);
770 }
771
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530772 if (motg->async_int) {
773 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530774 enable_irq(motg->irq);
775 }
776
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530777 dev_info(otg->dev, "USB exited from low power mode\n");
778
779 return 0;
780}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530781#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530783static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
784{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530785 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
786 motg->chg_type == USB_ACA_A_CHARGER ||
787 motg->chg_type == USB_ACA_B_CHARGER ||
788 motg->chg_type == USB_ACA_C_CHARGER) &&
789 mA > IDEV_ACA_CHG_LIMIT)
790 mA = IDEV_ACA_CHG_LIMIT;
791
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530792 if (motg->cur_power == mA)
793 return;
794
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530795 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700796 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530797 motg->cur_power = mA;
798}
799
800static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
801{
802 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
803
804 /*
805 * Gadget driver uses set_power method to notify about the
806 * available current based on suspend/configured states.
807 *
808 * IDEV_CHG can be drawn irrespective of suspend/un-configured
809 * states when CDP/ACA is connected.
810 */
811 if (motg->chg_type == USB_SDP_CHARGER)
812 msm_otg_notify_charger(motg, mA);
813
814 return 0;
815}
816
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530817static void msm_otg_start_host(struct otg_transceiver *otg, int on)
818{
819 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
820 struct msm_otg_platform_data *pdata = motg->pdata;
821 struct usb_hcd *hcd;
822
823 if (!otg->host)
824 return;
825
826 hcd = bus_to_hcd(otg->host);
827
828 if (on) {
829 dev_dbg(otg->dev, "host on\n");
830
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530831 /*
832 * Some boards have a switch cotrolled by gpio
833 * to enable/disable internal HUB. Enable internal
834 * HUB before kicking the host.
835 */
836 if (pdata->setup_gpio)
837 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530838 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530839 } else {
840 dev_dbg(otg->dev, "host off\n");
841
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530842 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530843 /* HCD core reset all bits of PORTSC. select ULPI phy */
844 writel_relaxed(0x80000000, USB_PORTSC);
845
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530846 if (pdata->setup_gpio)
847 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530848 }
849}
850
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851static int msm_otg_usbdev_notify(struct notifier_block *self,
852 unsigned long action, void *priv)
853{
854 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530855 struct usb_device *udev = priv;
856
857 if (!aca_enabled())
858 goto out;
859
860 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
861 goto out;
862
863 if (udev->bus != motg->otg.host)
864 goto out;
865 /*
866 * Interested in devices connected directly to the root hub.
867 * ACA dock can supply IDEV_CHG irrespective devices connected
868 * on the accessory port.
869 */
870 if (!udev->parent || udev->parent->parent ||
871 motg->chg_type == USB_ACA_DOCK_CHARGER)
872 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873
874 switch (action) {
875 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530876 usb_disable_autosuspend(udev);
877 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 if (udev->actconfig)
880 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
881 else
882 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530883 break;
884 case USB_DEVICE_REMOVE:
885 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700886 break;
887 default:
888 break;
889 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530890 if (test_bit(ID_A, &motg->inputs))
891 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
892 motg->mA_port);
893out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 return NOTIFY_OK;
895}
896
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530897static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
898{
899 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
900 struct usb_hcd *hcd;
901
902 /*
903 * Fail host registration if this board can support
904 * only peripheral configuration.
905 */
906 if (motg->pdata->mode == USB_PERIPHERAL) {
907 dev_info(otg->dev, "Host mode is not supported\n");
908 return -ENODEV;
909 }
910
911 if (!host) {
912 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530913 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530915 msm_otg_start_host(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700916 if (motg->pdata->vbus_power)
917 motg->pdata->vbus_power(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530918 otg->host = NULL;
919 otg->state = OTG_STATE_UNDEFINED;
920 schedule_work(&motg->sm_work);
921 } else {
922 otg->host = NULL;
923 }
924
925 return 0;
926 }
927
928 hcd = bus_to_hcd(host);
929 hcd->power_budget = motg->pdata->power_budget;
930
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
932 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530933 otg->host = host;
934 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
935
936 /*
937 * Kick the state machine work, if peripheral is not supported
938 * or peripheral is already registered with us.
939 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530940 if (motg->pdata->mode == USB_HOST || otg->gadget) {
941 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530942 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530943 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530944
945 return 0;
946}
947
948static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
949{
950 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
951 struct msm_otg_platform_data *pdata = motg->pdata;
952
953 if (!otg->gadget)
954 return;
955
956 if (on) {
957 dev_dbg(otg->dev, "gadget on\n");
958 /*
959 * Some boards have a switch cotrolled by gpio
960 * to enable/disable internal HUB. Disable internal
961 * HUB before kicking the gadget.
962 */
963 if (pdata->setup_gpio)
964 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
965 usb_gadget_vbus_connect(otg->gadget);
966 } else {
967 dev_dbg(otg->dev, "gadget off\n");
968 usb_gadget_vbus_disconnect(otg->gadget);
969 if (pdata->setup_gpio)
970 pdata->setup_gpio(OTG_STATE_UNDEFINED);
971 }
972
973}
974
975static int msm_otg_set_peripheral(struct otg_transceiver *otg,
976 struct usb_gadget *gadget)
977{
978 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
979
980 /*
981 * Fail peripheral registration if this board can support
982 * only host configuration.
983 */
984 if (motg->pdata->mode == USB_HOST) {
985 dev_info(otg->dev, "Peripheral mode is not supported\n");
986 return -ENODEV;
987 }
988
989 if (!gadget) {
990 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530991 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530992 msm_otg_start_peripheral(otg, 0);
993 otg->gadget = NULL;
994 otg->state = OTG_STATE_UNDEFINED;
995 schedule_work(&motg->sm_work);
996 } else {
997 otg->gadget = NULL;
998 }
999
1000 return 0;
1001 }
1002 otg->gadget = gadget;
1003 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1004
1005 /*
1006 * Kick the state machine work, if host is not supported
1007 * or host is already registered with us.
1008 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301009 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1010 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301011 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301012 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301013
1014 return 0;
1015}
1016
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017static bool msm_chg_aca_detect(struct msm_otg *motg)
1018{
1019 struct otg_transceiver *otg = &motg->otg;
1020 u32 int_sts;
1021 bool ret = false;
1022
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301023 if (!aca_enabled())
1024 goto out;
1025
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001026 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1027 goto out;
1028
1029 int_sts = ulpi_read(otg, 0x87);
1030 switch (int_sts & 0x1C) {
1031 case 0x08:
1032 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1033 dev_dbg(otg->dev, "ID_A\n");
1034 motg->chg_type = USB_ACA_A_CHARGER;
1035 motg->chg_state = USB_CHG_STATE_DETECTED;
1036 clear_bit(ID_B, &motg->inputs);
1037 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301038 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 ret = true;
1040 }
1041 break;
1042 case 0x0C:
1043 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1044 dev_dbg(otg->dev, "ID_B\n");
1045 motg->chg_type = USB_ACA_B_CHARGER;
1046 motg->chg_state = USB_CHG_STATE_DETECTED;
1047 clear_bit(ID_A, &motg->inputs);
1048 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301049 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001050 ret = true;
1051 }
1052 break;
1053 case 0x10:
1054 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1055 dev_dbg(otg->dev, "ID_C\n");
1056 motg->chg_type = USB_ACA_C_CHARGER;
1057 motg->chg_state = USB_CHG_STATE_DETECTED;
1058 clear_bit(ID_A, &motg->inputs);
1059 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301060 set_bit(ID, &motg->inputs);
1061 ret = true;
1062 }
1063 break;
1064 case 0x04:
1065 if (test_and_clear_bit(ID, &motg->inputs)) {
1066 dev_dbg(otg->dev, "ID_GND\n");
1067 motg->chg_type = USB_INVALID_CHARGER;
1068 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1069 clear_bit(ID_A, &motg->inputs);
1070 clear_bit(ID_B, &motg->inputs);
1071 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001072 ret = true;
1073 }
1074 break;
1075 default:
1076 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1077 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301078 test_and_clear_bit(ID_C, &motg->inputs) |
1079 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001080 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301081 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001082 motg->chg_type = USB_INVALID_CHARGER;
1083 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1084 }
1085 }
1086out:
1087 return ret;
1088}
1089
1090static void msm_chg_enable_aca_det(struct msm_otg *motg)
1091{
1092 struct otg_transceiver *otg = &motg->otg;
1093
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301094 if (!aca_enabled())
1095 return;
1096
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001097 switch (motg->pdata->phy_type) {
1098 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301099 /* Disable ID_GND in link and PHY */
1100 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1101 OTGSC_IDIE), USB_OTGSC);
1102 ulpi_write(otg, 0x01, 0x0C);
1103 ulpi_write(otg, 0x10, 0x0F);
1104 ulpi_write(otg, 0x10, 0x12);
1105 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 ulpi_write(otg, 0x20, 0x85);
1107 break;
1108 default:
1109 break;
1110 }
1111}
1112
1113static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1114{
1115 struct otg_transceiver *otg = &motg->otg;
1116
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301117 if (!aca_enabled())
1118 return;
1119
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001120 switch (motg->pdata->phy_type) {
1121 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301122 /* Enable ACA Detection interrupt (on any RID change) */
1123 ulpi_write(otg, 0x01, 0x94);
1124 break;
1125 default:
1126 break;
1127 }
1128}
1129
1130static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1131{
1132 struct otg_transceiver *otg = &motg->otg;
1133
1134 if (!aca_enabled())
1135 return;
1136
1137 switch (motg->pdata->phy_type) {
1138 case SNPS_28NM_INTEGRATED_PHY:
1139 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 break;
1141 default:
1142 break;
1143 }
1144}
1145
1146static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1147{
1148 struct otg_transceiver *otg = &motg->otg;
1149 bool ret = false;
1150
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301151 if (!aca_enabled())
1152 return ret;
1153
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154 switch (motg->pdata->phy_type) {
1155 case SNPS_28NM_INTEGRATED_PHY:
1156 if (ulpi_read(otg, 0x91) & 1) {
1157 dev_dbg(otg->dev, "RID change\n");
1158 ulpi_write(otg, 0x01, 0x92);
1159 ret = msm_chg_aca_detect(motg);
1160 }
1161 default:
1162 break;
1163 }
1164 return ret;
1165}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301166
1167static void msm_otg_id_timer_func(unsigned long data)
1168{
1169 struct msm_otg *motg = (struct msm_otg *) data;
1170
1171 if (!aca_enabled())
1172 return;
1173
1174 if (atomic_read(&motg->in_lpm)) {
1175 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1176 return;
1177 }
1178
1179 if (msm_chg_check_aca_intr(motg)) {
1180 dev_dbg(motg->otg.dev, "timer: aca work\n");
1181 schedule_work(&motg->sm_work);
1182 }
1183
1184 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1185 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1186}
1187
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301188static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1189{
1190 struct otg_transceiver *otg = &motg->otg;
1191 u32 chg_det;
1192 bool ret = false;
1193
1194 switch (motg->pdata->phy_type) {
1195 case CI_45NM_INTEGRATED_PHY:
1196 chg_det = ulpi_read(otg, 0x34);
1197 ret = chg_det & (1 << 4);
1198 break;
1199 case SNPS_28NM_INTEGRATED_PHY:
1200 chg_det = ulpi_read(otg, 0x87);
1201 ret = chg_det & 1;
1202 break;
1203 default:
1204 break;
1205 }
1206 return ret;
1207}
1208
1209static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1210{
1211 struct otg_transceiver *otg = &motg->otg;
1212 u32 chg_det;
1213
1214 switch (motg->pdata->phy_type) {
1215 case CI_45NM_INTEGRATED_PHY:
1216 chg_det = ulpi_read(otg, 0x34);
1217 /* Turn off charger block */
1218 chg_det |= ~(1 << 1);
1219 ulpi_write(otg, chg_det, 0x34);
1220 udelay(20);
1221 /* control chg block via ULPI */
1222 chg_det &= ~(1 << 3);
1223 ulpi_write(otg, chg_det, 0x34);
1224 /* put it in host mode for enabling D- source */
1225 chg_det &= ~(1 << 2);
1226 ulpi_write(otg, chg_det, 0x34);
1227 /* Turn on chg detect block */
1228 chg_det &= ~(1 << 1);
1229 ulpi_write(otg, chg_det, 0x34);
1230 udelay(20);
1231 /* enable chg detection */
1232 chg_det &= ~(1 << 0);
1233 ulpi_write(otg, chg_det, 0x34);
1234 break;
1235 case SNPS_28NM_INTEGRATED_PHY:
1236 /*
1237 * Configure DM as current source, DP as current sink
1238 * and enable battery charging comparators.
1239 */
1240 ulpi_write(otg, 0x8, 0x85);
1241 ulpi_write(otg, 0x2, 0x85);
1242 ulpi_write(otg, 0x1, 0x85);
1243 break;
1244 default:
1245 break;
1246 }
1247}
1248
1249static bool msm_chg_check_primary_det(struct msm_otg *motg)
1250{
1251 struct otg_transceiver *otg = &motg->otg;
1252 u32 chg_det;
1253 bool ret = false;
1254
1255 switch (motg->pdata->phy_type) {
1256 case CI_45NM_INTEGRATED_PHY:
1257 chg_det = ulpi_read(otg, 0x34);
1258 ret = chg_det & (1 << 4);
1259 break;
1260 case SNPS_28NM_INTEGRATED_PHY:
1261 chg_det = ulpi_read(otg, 0x87);
1262 ret = chg_det & 1;
1263 break;
1264 default:
1265 break;
1266 }
1267 return ret;
1268}
1269
1270static void msm_chg_enable_primary_det(struct msm_otg *motg)
1271{
1272 struct otg_transceiver *otg = &motg->otg;
1273 u32 chg_det;
1274
1275 switch (motg->pdata->phy_type) {
1276 case CI_45NM_INTEGRATED_PHY:
1277 chg_det = ulpi_read(otg, 0x34);
1278 /* enable chg detection */
1279 chg_det &= ~(1 << 0);
1280 ulpi_write(otg, chg_det, 0x34);
1281 break;
1282 case SNPS_28NM_INTEGRATED_PHY:
1283 /*
1284 * Configure DP as current source, DM as current sink
1285 * and enable battery charging comparators.
1286 */
1287 ulpi_write(otg, 0x2, 0x85);
1288 ulpi_write(otg, 0x1, 0x85);
1289 break;
1290 default:
1291 break;
1292 }
1293}
1294
1295static bool msm_chg_check_dcd(struct msm_otg *motg)
1296{
1297 struct otg_transceiver *otg = &motg->otg;
1298 u32 line_state;
1299 bool ret = false;
1300
1301 switch (motg->pdata->phy_type) {
1302 case CI_45NM_INTEGRATED_PHY:
1303 line_state = ulpi_read(otg, 0x15);
1304 ret = !(line_state & 1);
1305 break;
1306 case SNPS_28NM_INTEGRATED_PHY:
1307 line_state = ulpi_read(otg, 0x87);
1308 ret = line_state & 2;
1309 break;
1310 default:
1311 break;
1312 }
1313 return ret;
1314}
1315
1316static void msm_chg_disable_dcd(struct msm_otg *motg)
1317{
1318 struct otg_transceiver *otg = &motg->otg;
1319 u32 chg_det;
1320
1321 switch (motg->pdata->phy_type) {
1322 case CI_45NM_INTEGRATED_PHY:
1323 chg_det = ulpi_read(otg, 0x34);
1324 chg_det &= ~(1 << 5);
1325 ulpi_write(otg, chg_det, 0x34);
1326 break;
1327 case SNPS_28NM_INTEGRATED_PHY:
1328 ulpi_write(otg, 0x10, 0x86);
1329 break;
1330 default:
1331 break;
1332 }
1333}
1334
1335static void msm_chg_enable_dcd(struct msm_otg *motg)
1336{
1337 struct otg_transceiver *otg = &motg->otg;
1338 u32 chg_det;
1339
1340 switch (motg->pdata->phy_type) {
1341 case CI_45NM_INTEGRATED_PHY:
1342 chg_det = ulpi_read(otg, 0x34);
1343 /* Turn on D+ current source */
1344 chg_det |= (1 << 5);
1345 ulpi_write(otg, chg_det, 0x34);
1346 break;
1347 case SNPS_28NM_INTEGRATED_PHY:
1348 /* Data contact detection enable */
1349 ulpi_write(otg, 0x10, 0x85);
1350 break;
1351 default:
1352 break;
1353 }
1354}
1355
1356static void msm_chg_block_on(struct msm_otg *motg)
1357{
1358 struct otg_transceiver *otg = &motg->otg;
1359 u32 func_ctrl, chg_det;
1360
1361 /* put the controller in non-driving mode */
1362 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1363 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1364 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1365 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1366
1367 switch (motg->pdata->phy_type) {
1368 case CI_45NM_INTEGRATED_PHY:
1369 chg_det = ulpi_read(otg, 0x34);
1370 /* control chg block via ULPI */
1371 chg_det &= ~(1 << 3);
1372 ulpi_write(otg, chg_det, 0x34);
1373 /* Turn on chg detect block */
1374 chg_det &= ~(1 << 1);
1375 ulpi_write(otg, chg_det, 0x34);
1376 udelay(20);
1377 break;
1378 case SNPS_28NM_INTEGRATED_PHY:
1379 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301380 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301381 /* Clear alt interrupt latch and enable bits */
1382 ulpi_write(otg, 0x1F, 0x92);
1383 ulpi_write(otg, 0x1F, 0x95);
1384 udelay(100);
1385 break;
1386 default:
1387 break;
1388 }
1389}
1390
1391static void msm_chg_block_off(struct msm_otg *motg)
1392{
1393 struct otg_transceiver *otg = &motg->otg;
1394 u32 func_ctrl, chg_det;
1395
1396 switch (motg->pdata->phy_type) {
1397 case CI_45NM_INTEGRATED_PHY:
1398 chg_det = ulpi_read(otg, 0x34);
1399 /* Turn off charger block */
1400 chg_det |= ~(1 << 1);
1401 ulpi_write(otg, chg_det, 0x34);
1402 break;
1403 case SNPS_28NM_INTEGRATED_PHY:
1404 /* Clear charger detecting control bits */
1405 ulpi_write(otg, 0x3F, 0x86);
1406 /* Clear alt interrupt latch and enable bits */
1407 ulpi_write(otg, 0x1F, 0x92);
1408 ulpi_write(otg, 0x1F, 0x95);
1409 break;
1410 default:
1411 break;
1412 }
1413
1414 /* put the controller in normal mode */
1415 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1416 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1417 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1418 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1419}
1420
Anji jonnalad270e2d2011-08-09 11:28:32 +05301421static const char *chg_to_string(enum usb_chg_type chg_type)
1422{
1423 switch (chg_type) {
1424 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1425 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1426 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1427 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1428 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1429 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1430 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1431 default: return "INVALID_CHARGER";
1432 }
1433}
1434
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301435#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1436#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1437#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1438#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1439static void msm_chg_detect_work(struct work_struct *w)
1440{
1441 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1442 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301444 unsigned long delay;
1445
1446 dev_dbg(otg->dev, "chg detection work\n");
1447 switch (motg->chg_state) {
1448 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301449 msm_chg_block_on(motg);
1450 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301452 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1453 motg->dcd_retries = 0;
1454 delay = MSM_CHG_DCD_POLL_TIME;
1455 break;
1456 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 is_aca = msm_chg_aca_detect(motg);
1458 if (is_aca) {
1459 /*
1460 * ID_A can be ACA dock too. continue
1461 * primary detection after DCD.
1462 */
1463 if (test_bit(ID_A, &motg->inputs)) {
1464 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1465 } else {
1466 delay = 0;
1467 break;
1468 }
1469 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301470 is_dcd = msm_chg_check_dcd(motg);
1471 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1472 if (is_dcd || tmout) {
1473 msm_chg_disable_dcd(motg);
1474 msm_chg_enable_primary_det(motg);
1475 delay = MSM_CHG_PRIMARY_DET_TIME;
1476 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1477 } else {
1478 delay = MSM_CHG_DCD_POLL_TIME;
1479 }
1480 break;
1481 case USB_CHG_STATE_DCD_DONE:
1482 vout = msm_chg_check_primary_det(motg);
1483 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301484 if (test_bit(ID_A, &motg->inputs)) {
1485 motg->chg_type = USB_ACA_DOCK_CHARGER;
1486 motg->chg_state = USB_CHG_STATE_DETECTED;
1487 delay = 0;
1488 break;
1489 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301490 msm_chg_enable_secondary_det(motg);
1491 delay = MSM_CHG_SECONDARY_DET_TIME;
1492 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1493 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301494 if (test_bit(ID_A, &motg->inputs)) {
1495 motg->chg_type = USB_ACA_A_CHARGER;
1496 motg->chg_state = USB_CHG_STATE_DETECTED;
1497 delay = 0;
1498 break;
1499 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301500 motg->chg_type = USB_SDP_CHARGER;
1501 motg->chg_state = USB_CHG_STATE_DETECTED;
1502 delay = 0;
1503 }
1504 break;
1505 case USB_CHG_STATE_PRIMARY_DONE:
1506 vout = msm_chg_check_secondary_det(motg);
1507 if (vout)
1508 motg->chg_type = USB_DCP_CHARGER;
1509 else
1510 motg->chg_type = USB_CDP_CHARGER;
1511 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1512 /* fall through */
1513 case USB_CHG_STATE_SECONDARY_DONE:
1514 motg->chg_state = USB_CHG_STATE_DETECTED;
1515 case USB_CHG_STATE_DETECTED:
1516 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517 msm_chg_enable_aca_det(motg);
1518 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301519 dev_dbg(otg->dev, "chg_type = %s\n",
1520 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301521 schedule_work(&motg->sm_work);
1522 return;
1523 default:
1524 return;
1525 }
1526
1527 schedule_delayed_work(&motg->chg_work, delay);
1528}
1529
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301530/*
1531 * We support OTG, Peripheral only and Host only configurations. In case
1532 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1533 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1534 * enabled when switch is controlled by user and default mode is supplied
1535 * by board file, which can be changed by userspace later.
1536 */
1537static void msm_otg_init_sm(struct msm_otg *motg)
1538{
1539 struct msm_otg_platform_data *pdata = motg->pdata;
1540 u32 otgsc = readl(USB_OTGSC);
1541
1542 switch (pdata->mode) {
1543 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001544 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301545 if (pdata->default_mode == USB_HOST) {
1546 clear_bit(ID, &motg->inputs);
1547 } else if (pdata->default_mode == USB_PERIPHERAL) {
1548 set_bit(ID, &motg->inputs);
1549 set_bit(B_SESS_VLD, &motg->inputs);
1550 } else {
1551 set_bit(ID, &motg->inputs);
1552 clear_bit(B_SESS_VLD, &motg->inputs);
1553 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301555 if (aca_enabled()) {
1556 if (irq_read_line(motg->pdata->pmic_id_irq))
1557 set_bit(ID, &motg->inputs);
1558 else
1559 clear_bit(ID, &motg->inputs);
1560
1561 } else {
1562 if (otgsc & OTGSC_ID)
1563 set_bit(ID, &motg->inputs);
1564 else
1565 clear_bit(ID, &motg->inputs);
1566 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567
1568 if (otgsc & OTGSC_BSV)
1569 set_bit(B_SESS_VLD, &motg->inputs);
1570 else
1571 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301572 }
1573 break;
1574 case USB_HOST:
1575 clear_bit(ID, &motg->inputs);
1576 break;
1577 case USB_PERIPHERAL:
1578 set_bit(ID, &motg->inputs);
1579 if (otgsc & OTGSC_BSV)
1580 set_bit(B_SESS_VLD, &motg->inputs);
1581 else
1582 clear_bit(B_SESS_VLD, &motg->inputs);
1583 break;
1584 default:
1585 break;
1586 }
1587}
1588
1589static void msm_otg_sm_work(struct work_struct *w)
1590{
1591 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1592 struct otg_transceiver *otg = &motg->otg;
1593
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301594 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301595 switch (otg->state) {
1596 case OTG_STATE_UNDEFINED:
1597 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1598 msm_otg_reset(otg);
1599 msm_otg_init_sm(motg);
1600 otg->state = OTG_STATE_B_IDLE;
1601 /* FALL THROUGH */
1602 case OTG_STATE_B_IDLE:
1603 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 if ((!test_bit(ID, &motg->inputs) ||
1605 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1607 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301608 IDEV_ACA_CHG_MAX);
1609 else if (test_bit(ID_A, &motg->inputs))
1610 msm_otg_notify_charger(motg,
1611 IDEV_ACA_CHG_MAX - IUNIT);
1612 else if (motg->pdata->vbus_power)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613 motg->pdata->vbus_power(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301614 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301615 /*
1616 * Link can not generate PHY_ALT interrupt
1617 * in host mode when no device is attached
1618 * to the port. It is also observed PHY_ALT
1619 * interrupt missing upon Micro-A cable disconnect.
1620 * Hence disable PHY_ALT interrupt and perform
1621 * polling to detect RID change.
1622 */
1623 msm_chg_enable_aca_det(motg);
1624 msm_chg_disable_aca_intr(motg);
1625 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301626 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301627 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1628 switch (motg->chg_state) {
1629 case USB_CHG_STATE_UNDEFINED:
1630 msm_chg_detect_work(&motg->chg_work.work);
1631 break;
1632 case USB_CHG_STATE_DETECTED:
1633 switch (motg->chg_type) {
1634 case USB_DCP_CHARGER:
1635 msm_otg_notify_charger(motg,
1636 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301637 pm_runtime_put_noidle(otg->dev);
1638 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301639 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301640 case USB_ACA_B_CHARGER:
1641 msm_otg_notify_charger(motg,
1642 IDEV_ACA_CHG_MAX);
1643 /*
1644 * (ID_B --> ID_C) PHY_ALT interrupt can
1645 * not be detected in LPM.
1646 */
1647 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301648 case USB_CDP_CHARGER:
1649 msm_otg_notify_charger(motg,
1650 IDEV_CHG_MAX);
1651 msm_otg_start_peripheral(otg, 1);
1652 otg->state = OTG_STATE_B_PERIPHERAL;
1653 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301654 case USB_ACA_C_CHARGER:
1655 msm_otg_notify_charger(motg,
1656 IDEV_ACA_CHG_MAX);
1657 msm_otg_start_peripheral(otg, 1);
1658 otg->state = OTG_STATE_B_PERIPHERAL;
1659 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301660 case USB_SDP_CHARGER:
1661 msm_otg_notify_charger(motg, IUNIT);
1662 msm_otg_start_peripheral(otg, 1);
1663 otg->state = OTG_STATE_B_PERIPHERAL;
1664 break;
1665 default:
1666 break;
1667 }
1668 break;
1669 default:
1670 break;
1671 }
1672 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301673 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301674 msm_otg_notify_charger(motg, 0);
1675 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1676 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301677 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301678 pm_runtime_put_noidle(otg->dev);
1679 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301680 }
1681 break;
1682 case OTG_STATE_B_PERIPHERAL:
1683 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1684 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 !test_bit(ID, &motg->inputs) ||
1686 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301687 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001688 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689 schedule_work(w);
1690 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301691 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001692 }
1693 break;
1694 case OTG_STATE_A_HOST:
1695 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1696 if (test_bit(ID, &motg->inputs) &&
1697 !test_bit(ID_A, &motg->inputs)) {
1698 msm_otg_start_host(otg, 0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301699 if (motg->pdata->vbus_power) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001700 motg->pdata->vbus_power(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301701 msleep(100); /* TA_WAIT_VFALL */
1702 }
1703 /*
1704 * Exit point of host mode.
1705 *
1706 * 1. Micro-A cable disconnect: Just schedule
1707 * the work. PHY is reset in B_IDLE and LPM
1708 * is allowed.
1709 * 2. ID_GND --> ID_B: No need to reset the PHY.
1710 * HCD core clears all PORTSC bits and initializes
1711 * the controller to host mode in remove_hcd.
1712 * Restore PORTSC transceiver select bits (ULPI)
1713 * and reset the controller to change MODE bits.
1714 * PHY_ALT interrupt can not occur in host mode.
1715 */
1716 del_timer_sync(&motg->id_timer);
1717 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1718 msm_otg_link_reset(motg);
1719 msm_chg_enable_aca_intr(motg);
1720 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301721 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301722 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 } else if (test_bit(ID_A, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 if (motg->pdata->vbus_power)
1725 motg->pdata->vbus_power(0);
1726 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301727 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001728 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 msm_otg_notify_charger(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001730 if (motg->pdata->vbus_power)
1731 motg->pdata->vbus_power(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301732 }
1733 break;
1734 default:
1735 break;
1736 }
1737}
1738
1739static irqreturn_t msm_otg_irq(int irq, void *data)
1740{
1741 struct msm_otg *motg = data;
1742 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001743 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301744
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301745 if (atomic_read(&motg->in_lpm)) {
1746 disable_irq_nosync(irq);
1747 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301748 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301749 return IRQ_HANDLED;
1750 }
1751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001752 usbsts = readl(USB_USBSTS);
1753 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301754 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301756 if (msm_chg_check_aca_intr(motg)) {
1757 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001758 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301759 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001760 return IRQ_HANDLED;
1761 }
1762
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301763 otgsc = readl(USB_OTGSC);
1764 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1765 return IRQ_NONE;
1766
1767 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301768 if (otgsc & OTGSC_ID) {
1769 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301770 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301771 } else {
1772 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301773 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301774 msm_chg_enable_aca_det(motg);
1775 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301777 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301778 if (otgsc & OTGSC_BSV) {
1779 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301780 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301781 } else {
1782 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301783 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301784 msm_chg_check_aca_intr(motg);
1785 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001786 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301787 }
1788
1789 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 return IRQ_HANDLED;
1791}
1792
1793static void msm_otg_set_vbus_state(int online)
1794{
1795 struct msm_otg *motg = the_msm_otg;
1796
1797 /* We depend on PMIC for only VBUS ON interrupt */
1798 if (!atomic_read(&motg->in_lpm) || !online)
1799 return;
1800
1801 /*
1802 * Let interrupt handler take care of resuming
1803 * the hardware.
1804 */
1805 msm_otg_irq(motg->irq, (void *) motg);
1806}
1807
1808static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1809{
1810 struct msm_otg *motg = data;
1811
1812 if (atomic_read(&motg->in_lpm) && !motg->async_int)
1813 msm_otg_irq(motg->irq, motg);
1814
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301815 return IRQ_HANDLED;
1816}
1817
1818static int msm_otg_mode_show(struct seq_file *s, void *unused)
1819{
1820 struct msm_otg *motg = s->private;
1821 struct otg_transceiver *otg = &motg->otg;
1822
1823 switch (otg->state) {
1824 case OTG_STATE_A_HOST:
1825 seq_printf(s, "host\n");
1826 break;
1827 case OTG_STATE_B_PERIPHERAL:
1828 seq_printf(s, "peripheral\n");
1829 break;
1830 default:
1831 seq_printf(s, "none\n");
1832 break;
1833 }
1834
1835 return 0;
1836}
1837
1838static int msm_otg_mode_open(struct inode *inode, struct file *file)
1839{
1840 return single_open(file, msm_otg_mode_show, inode->i_private);
1841}
1842
1843static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1844 size_t count, loff_t *ppos)
1845{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301846 struct seq_file *s = file->private_data;
1847 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301848 char buf[16];
1849 struct otg_transceiver *otg = &motg->otg;
1850 int status = count;
1851 enum usb_mode_type req_mode;
1852
1853 memset(buf, 0x00, sizeof(buf));
1854
1855 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1856 status = -EFAULT;
1857 goto out;
1858 }
1859
1860 if (!strncmp(buf, "host", 4)) {
1861 req_mode = USB_HOST;
1862 } else if (!strncmp(buf, "peripheral", 10)) {
1863 req_mode = USB_PERIPHERAL;
1864 } else if (!strncmp(buf, "none", 4)) {
1865 req_mode = USB_NONE;
1866 } else {
1867 status = -EINVAL;
1868 goto out;
1869 }
1870
1871 switch (req_mode) {
1872 case USB_NONE:
1873 switch (otg->state) {
1874 case OTG_STATE_A_HOST:
1875 case OTG_STATE_B_PERIPHERAL:
1876 set_bit(ID, &motg->inputs);
1877 clear_bit(B_SESS_VLD, &motg->inputs);
1878 break;
1879 default:
1880 goto out;
1881 }
1882 break;
1883 case USB_PERIPHERAL:
1884 switch (otg->state) {
1885 case OTG_STATE_B_IDLE:
1886 case OTG_STATE_A_HOST:
1887 set_bit(ID, &motg->inputs);
1888 set_bit(B_SESS_VLD, &motg->inputs);
1889 break;
1890 default:
1891 goto out;
1892 }
1893 break;
1894 case USB_HOST:
1895 switch (otg->state) {
1896 case OTG_STATE_B_IDLE:
1897 case OTG_STATE_B_PERIPHERAL:
1898 clear_bit(ID, &motg->inputs);
1899 break;
1900 default:
1901 goto out;
1902 }
1903 break;
1904 default:
1905 goto out;
1906 }
1907
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301908 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301909 schedule_work(&motg->sm_work);
1910out:
1911 return status;
1912}
1913
1914const struct file_operations msm_otg_mode_fops = {
1915 .open = msm_otg_mode_open,
1916 .read = seq_read,
1917 .write = msm_otg_mode_write,
1918 .llseek = seq_lseek,
1919 .release = single_release,
1920};
1921
Anji jonnalad270e2d2011-08-09 11:28:32 +05301922static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
1923{
1924 struct msm_otg *motg = s->private;
1925
1926 seq_printf(s, chg_to_string(motg->chg_type));
1927 return 0;
1928}
1929
1930static int msm_otg_chg_open(struct inode *inode, struct file *file)
1931{
1932 return single_open(file, msm_otg_show_chg_type, inode->i_private);
1933}
1934
1935const struct file_operations msm_otg_chg_fops = {
1936 .open = msm_otg_chg_open,
1937 .read = seq_read,
1938 .llseek = seq_lseek,
1939 .release = single_release,
1940};
1941
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301942static int msm_otg_aca_show(struct seq_file *s, void *unused)
1943{
1944 if (debug_aca_enabled)
1945 seq_printf(s, "enabled\n");
1946 else
1947 seq_printf(s, "disabled\n");
1948
1949 return 0;
1950}
1951
1952static int msm_otg_aca_open(struct inode *inode, struct file *file)
1953{
1954 return single_open(file, msm_otg_aca_show, inode->i_private);
1955}
1956
1957static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
1958 size_t count, loff_t *ppos)
1959{
1960 char buf[8];
1961
1962 memset(buf, 0x00, sizeof(buf));
1963
1964 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1965 return -EFAULT;
1966
1967 if (!strncmp(buf, "enable", 6))
1968 debug_aca_enabled = true;
1969 else
1970 debug_aca_enabled = false;
1971
1972 return count;
1973}
1974
1975const struct file_operations msm_otg_aca_fops = {
1976 .open = msm_otg_aca_open,
1977 .read = seq_read,
1978 .write = msm_otg_aca_write,
1979 .llseek = seq_lseek,
1980 .release = single_release,
1981};
1982
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301983static struct dentry *msm_otg_dbg_root;
1984static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05301985static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301986static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301987
1988static int msm_otg_debugfs_init(struct msm_otg *motg)
1989{
Anji jonnalad270e2d2011-08-09 11:28:32 +05301990
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301991 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1992
1993 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1994 return -ENODEV;
1995
Anji jonnalad270e2d2011-08-09 11:28:32 +05301996 if (motg->pdata->mode == USB_OTG &&
1997 motg->pdata->otg_control == OTG_USER_CONTROL) {
1998
1999 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2000 S_IWUSR, msm_otg_dbg_root, motg,
2001 &msm_otg_mode_fops);
2002
2003 if (!msm_otg_dbg_mode) {
2004 debugfs_remove(msm_otg_dbg_root);
2005 msm_otg_dbg_root = NULL;
2006 return -ENODEV;
2007 }
2008 }
2009
2010 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2011 msm_otg_dbg_root, motg,
2012 &msm_otg_chg_fops);
2013
2014 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302015 debugfs_remove_recursive(msm_otg_dbg_root);
2016 return -ENODEV;
2017 }
2018
2019 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2020 msm_otg_dbg_root, motg,
2021 &msm_otg_aca_fops);
2022
2023 if (!msm_otg_dbg_aca) {
2024 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302025 return -ENODEV;
2026 }
2027
2028 return 0;
2029}
2030
2031static void msm_otg_debugfs_cleanup(void)
2032{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302033 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302034}
2035
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302036static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2037static struct platform_device *msm_otg_add_pdev(
2038 struct platform_device *ofdev, const char *name)
2039{
2040 struct platform_device *pdev;
2041 const struct resource *res = ofdev->resource;
2042 unsigned int num = ofdev->num_resources;
2043 int retval;
2044
2045 pdev = platform_device_alloc(name, -1);
2046 if (!pdev) {
2047 retval = -ENOMEM;
2048 goto error;
2049 }
2050
2051 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2052 pdev->dev.dma_mask = &msm_otg_dma_mask;
2053
2054 if (num) {
2055 retval = platform_device_add_resources(pdev, res, num);
2056 if (retval)
2057 goto error;
2058 }
2059
2060 retval = platform_device_add(pdev);
2061 if (retval)
2062 goto error;
2063
2064 return pdev;
2065
2066error:
2067 platform_device_put(pdev);
2068 return ERR_PTR(retval);
2069}
2070
2071static int msm_otg_setup_devices(struct platform_device *ofdev,
2072 enum usb_mode_type mode, bool init)
2073{
2074 const char *gadget_name = "msm_hsusb";
2075 const char *host_name = "msm_hsusb_host";
2076 static struct platform_device *gadget_pdev;
2077 static struct platform_device *host_pdev;
2078 int retval = 0;
2079
2080 if (!init) {
2081 if (gadget_pdev)
2082 platform_device_unregister(gadget_pdev);
2083 if (host_pdev)
2084 platform_device_unregister(host_pdev);
2085 return 0;
2086 }
2087
2088 switch (mode) {
2089 case USB_OTG:
2090 /* fall through */
2091 case USB_PERIPHERAL:
2092 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2093 if (IS_ERR(gadget_pdev)) {
2094 retval = PTR_ERR(gadget_pdev);
2095 break;
2096 }
2097 if (mode == USB_PERIPHERAL)
2098 break;
2099 /* fall through */
2100 case USB_HOST:
2101 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2102 if (IS_ERR(host_pdev)) {
2103 retval = PTR_ERR(host_pdev);
2104 if (mode == USB_OTG)
2105 platform_device_unregister(gadget_pdev);
2106 }
2107 break;
2108 default:
2109 break;
2110 }
2111
2112 return retval;
2113}
2114
2115struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2116{
2117 struct device_node *node = pdev->dev.of_node;
2118 struct msm_otg_platform_data *pdata;
2119 int len = 0;
2120
2121 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2122 if (!pdata) {
2123 pr_err("unable to allocate platform data\n");
2124 return NULL;
2125 }
2126 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2127 if (len) {
2128 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2129 if (!pdata->phy_init_seq)
2130 return NULL;
2131 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2132 pdata->phy_init_seq,
2133 len/sizeof(*pdata->phy_init_seq));
2134 }
2135 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2136 &pdata->power_budget);
2137 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2138 &pdata->mode);
2139 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2140 &pdata->otg_control);
2141 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2142 &pdata->default_mode);
2143 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2144 &pdata->phy_type);
2145 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2146 &pdata->pmic_id_irq);
2147 of_property_read_string(node, "qcom,hsusb-otg-pclk-src-name",
2148 &pdata->pclk_src_name);
2149 return pdata;
2150}
2151
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302152static int __init msm_otg_probe(struct platform_device *pdev)
2153{
2154 int ret = 0;
2155 struct resource *res;
2156 struct msm_otg *motg;
2157 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302158 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302159
2160 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302161
2162 if (pdev->dev.of_node) {
2163 dev_dbg(&pdev->dev, "device tree enabled\n");
2164 pdata = msm_otg_dt_to_pdata(pdev);
2165 if (!pdata)
2166 return -ENOMEM;
2167 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2168 if (ret) {
2169 dev_err(&pdev->dev, "devices setup failed\n");
2170 return ret;
2171 }
2172 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302173 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2174 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302175 } else {
2176 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302177 }
2178
2179 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2180 if (!motg) {
2181 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2182 return -ENOMEM;
2183 }
2184
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002185 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302186 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302187 otg = &motg->otg;
2188 otg->dev = &pdev->dev;
2189
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302190 /*
2191 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2192 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2193 * PMIC can detect ACA ID_GND and generate an interrupt.
2194 */
2195 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2196 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2197 ret = -EINVAL;
2198 goto free_motg;
2199 }
2200
Amit Blay02eff132011-09-21 16:46:24 +03002201 /* Some targets don't support PHY clock. */
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302202 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002203 if (IS_ERR(motg->phy_reset_clk))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302204 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302205
2206 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
2207 if (IS_ERR(motg->clk)) {
2208 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
2209 ret = PTR_ERR(motg->clk);
2210 goto put_phy_reset_clk;
2211 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302212 clk_set_rate(motg->clk, 60000000);
2213
2214 /*
2215 * If USB Core is running its protocol engine based on CORE CLK,
2216 * CORE CLK must be running at >55Mhz for correct HSUSB
2217 * operation and USB core cannot tolerate frequency changes on
2218 * CORE CLK. For such USB cores, vote for maximum clk frequency
2219 * on pclk source
2220 */
2221 if (motg->pdata->pclk_src_name) {
2222 motg->pclk_src = clk_get(&pdev->dev,
2223 motg->pdata->pclk_src_name);
2224 if (IS_ERR(motg->pclk_src))
2225 goto put_clk;
2226 clk_set_rate(motg->pclk_src, INT_MAX);
2227 clk_enable(motg->pclk_src);
2228 } else
2229 motg->pclk_src = ERR_PTR(-ENOENT);
2230
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302231 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
2232 if (IS_ERR(motg->pclk)) {
2233 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
2234 ret = PTR_ERR(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302235 goto put_pclk_src;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302236 }
2237
Amit Blay02eff132011-09-21 16:46:24 +03002238 motg->system_clk = clk_get(&pdev->dev, "usb_hs_system_clk");
2239 if (!IS_ERR(motg->system_clk))
2240 clk_enable(motg->system_clk);
2241
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302242 /*
2243 * USB core clock is not present on all MSM chips. This
2244 * clock is introduced to remove the dependency on AXI
2245 * bus frequency.
2246 */
2247 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
2248 if (IS_ERR(motg->core_clk))
2249 motg->core_clk = NULL;
2250
2251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2252 if (!res) {
2253 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2254 ret = -ENODEV;
2255 goto put_core_clk;
2256 }
2257
2258 motg->regs = ioremap(res->start, resource_size(res));
2259 if (!motg->regs) {
2260 dev_err(&pdev->dev, "ioremap failed\n");
2261 ret = -ENOMEM;
2262 goto put_core_clk;
2263 }
2264 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2265
2266 motg->irq = platform_get_irq(pdev, 0);
2267 if (!motg->irq) {
2268 dev_err(&pdev->dev, "platform_get_irq failed\n");
2269 ret = -ENODEV;
2270 goto free_regs;
2271 }
2272
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302273 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302274
2275 ret = msm_hsusb_init_vddcx(motg, 1);
2276 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002277 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +05302278 goto free_regs;
2279 }
2280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002281 ret = msm_hsusb_config_vddcx(1);
2282 if (ret) {
2283 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2284 goto free_init_vddcx;
2285 }
2286
Anji jonnala11aa5c42011-05-04 10:19:48 +05302287 ret = msm_hsusb_ldo_init(motg, 1);
2288 if (ret) {
2289 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002290 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302291 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002292
2293 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302294 if (ret) {
2295 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002296 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302297 }
2298
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302299 if (motg->core_clk)
2300 clk_enable(motg->core_clk);
2301
2302 writel(0, USB_USBINTR);
2303 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304 /* Ensure that above STOREs are completed before enabling interrupts */
2305 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302306
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002307 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302308 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302309 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302310 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2311 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302312 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2313 "msm_otg", motg);
2314 if (ret) {
2315 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002316 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302317 }
2318
2319 otg->init = msm_otg_reset;
2320 otg->set_host = msm_otg_set_host;
2321 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302322 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302323 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302324
2325 otg->io_ops = &msm_otg_io_ops;
2326
2327 ret = otg_set_transceiver(&motg->otg);
2328 if (ret) {
2329 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2330 goto free_irq;
2331 }
2332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2334 if (motg->pdata->pmic_id_irq) {
2335 ret = request_irq(motg->pdata->pmic_id_irq,
2336 msm_pmic_id_irq,
2337 IRQF_TRIGGER_RISING |
2338 IRQF_TRIGGER_FALLING,
2339 "msm_otg", motg);
2340 if (ret) {
2341 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2342 goto remove_otg;
2343 }
2344 } else {
2345 ret = -ENODEV;
2346 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2347 goto remove_otg;
2348 }
2349 }
2350
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302351 msm_hsusb_mhl_switch_enable(motg, 1);
2352
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302353 platform_set_drvdata(pdev, motg);
2354 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002355 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302356
Anji jonnalad270e2d2011-08-09 11:28:32 +05302357 ret = msm_otg_debugfs_init(motg);
2358 if (ret)
2359 dev_dbg(&pdev->dev, "mode debugfs file is"
2360 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302361
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002362 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2363 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2364
2365 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
2366 motg->pdata->otg_control == OTG_PMIC_CONTROL &&
2367 motg->pdata->pmic_id_irq)
2368 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
2369 ALLOW_PHY_RETENTION |
2370 ALLOW_PHY_COMP_DISABLE;
2371
2372 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302373 pm_runtime_set_active(&pdev->dev);
2374 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302375
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302376 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002377
2378remove_otg:
2379 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302380free_irq:
2381 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002382destroy_wlock:
2383 wake_lock_destroy(&motg->wlock);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302384 clk_disable(motg->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002385 msm_hsusb_ldo_enable(motg, 0);
2386free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302387 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002388free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302389 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302390free_regs:
2391 iounmap(motg->regs);
2392put_core_clk:
2393 if (motg->core_clk)
2394 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002395
2396 if (!IS_ERR(motg->system_clk)) {
2397 clk_disable(motg->system_clk);
2398 clk_put(motg->system_clk);
2399 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302400put_pclk_src:
2401 if (!IS_ERR(motg->pclk_src)) {
2402 clk_disable(motg->pclk_src);
2403 clk_put(motg->pclk_src);
2404 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302405put_clk:
2406 clk_put(motg->clk);
2407put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002408 if (!IS_ERR(motg->phy_reset_clk))
2409 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302410free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302411 kfree(motg);
2412 return ret;
2413}
2414
2415static int __devexit msm_otg_remove(struct platform_device *pdev)
2416{
2417 struct msm_otg *motg = platform_get_drvdata(pdev);
2418 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302419 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302420
2421 if (otg->host || otg->gadget)
2422 return -EBUSY;
2423
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302424 if (pdev->dev.of_node)
2425 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002426 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2427 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302428 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302429 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302430 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302431
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302432 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302433
2434 device_init_wakeup(&pdev->dev, 0);
2435 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002436 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302437
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302438 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 if (motg->pdata->pmic_id_irq)
2440 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302441 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302442 free_irq(motg->irq, motg);
2443
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302444 /*
2445 * Put PHY in low power mode.
2446 */
2447 ulpi_read(otg, 0x14);
2448 ulpi_write(otg, 0x08, 0x09);
2449
2450 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2451 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2452 if (readl(USB_PORTSC) & PORTSC_PHCD)
2453 break;
2454 udelay(1);
2455 cnt++;
2456 }
2457 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2458 dev_err(otg->dev, "Unable to suspend PHY\n");
2459
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302460 clk_disable(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302461 if (motg->core_clk)
2462 clk_disable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +02002463 if (!IS_ERR(motg->system_clk))
2464 clk_disable(motg->system_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302465 if (!IS_ERR(motg->pclk_src)) {
2466 clk_disable(motg->pclk_src);
2467 clk_put(motg->pclk_src);
2468 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302470 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002471 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302472
2473 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302474 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302475
Amit Blay02eff132011-09-21 16:46:24 +03002476 if (!IS_ERR(motg->phy_reset_clk))
2477 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302478 clk_put(motg->pclk);
2479 clk_put(motg->clk);
2480 if (motg->core_clk)
2481 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002482 if (!IS_ERR(motg->system_clk))
2483 clk_put(motg->system_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302484
2485 kfree(motg);
2486
2487 return 0;
2488}
2489
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302490#ifdef CONFIG_PM_RUNTIME
2491static int msm_otg_runtime_idle(struct device *dev)
2492{
2493 struct msm_otg *motg = dev_get_drvdata(dev);
2494 struct otg_transceiver *otg = &motg->otg;
2495
2496 dev_dbg(dev, "OTG runtime idle\n");
2497
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302498 if (otg->state == OTG_STATE_UNDEFINED)
2499 return -EAGAIN;
2500 else
2501 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302502}
2503
2504static int msm_otg_runtime_suspend(struct device *dev)
2505{
2506 struct msm_otg *motg = dev_get_drvdata(dev);
2507
2508 dev_dbg(dev, "OTG runtime suspend\n");
2509 return msm_otg_suspend(motg);
2510}
2511
2512static int msm_otg_runtime_resume(struct device *dev)
2513{
2514 struct msm_otg *motg = dev_get_drvdata(dev);
2515
2516 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302517 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302518 return msm_otg_resume(motg);
2519}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302520#endif
2521
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302522#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302523static int msm_otg_pm_suspend(struct device *dev)
2524{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302525 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302526
2527 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302528
2529#ifdef CONFIG_PM_RUNTIME
2530 ret = pm_runtime_suspend(dev);
2531 if (ret > 0)
2532 ret = 0;
2533#else
2534 ret = msm_otg_suspend(dev_get_drvdata(dev));
2535#endif
2536 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302537}
2538
2539static int msm_otg_pm_resume(struct device *dev)
2540{
2541 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302542
2543 dev_dbg(dev, "OTG PM resume\n");
2544
Manu Gautamf284c052011-09-08 16:52:48 +05302545#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302546 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302547 * Do not resume hardware as part of system resume,
2548 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302549 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002550 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302551#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302552
Manu Gautamf284c052011-09-08 16:52:48 +05302553 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302554}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302555#endif
2556
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302557#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302558static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302559 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2560 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2561 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302562};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302563#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302564
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302565static struct of_device_id msm_otg_dt_match[] = {
2566 { .compatible = "qcom,hsusb-otg",
2567 },
2568 {}
2569};
2570
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571static struct platform_driver msm_otg_driver = {
2572 .remove = __devexit_p(msm_otg_remove),
2573 .driver = {
2574 .name = DRIVER_NAME,
2575 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302576#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302577 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302578#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302579 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302580 },
2581};
2582
2583static int __init msm_otg_init(void)
2584{
2585 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2586}
2587
2588static void __exit msm_otg_exit(void)
2589{
2590 platform_driver_unregister(&msm_otg_driver);
2591}
2592
2593module_init(msm_otg_init);
2594module_exit(msm_otg_exit);
2595
2596MODULE_LICENSE("GPL v2");
2597MODULE_DESCRIPTION("MSM USB transceiver driver");