blob: 628ba7d943da57760319701067ecfafec5ef6010 [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 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/device.h>
21#include <linux/platform_device.h>
22#include <linux/clk.h>
23#include <linux/slab.h>
24#include <linux/interrupt.h>
25#include <linux/err.h>
26#include <linux/delay.h>
27#include <linux/io.h>
28#include <linux/ioport.h>
29#include <linux/uaccess.h>
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053032#include <linux/pm_runtime.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053033
34#include <linux/usb.h>
35#include <linux/usb/otg.h>
36#include <linux/usb/ulpi.h>
37#include <linux/usb/gadget.h>
38#include <linux/usb/hcd.h>
39#include <linux/usb/msm_hsusb.h>
40#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053041#include <linux/regulator/consumer.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053042
43#include <mach/clk.h>
44
45#define MSM_USB_BASE (motg->regs)
46#define DRIVER_NAME "msm_otg"
47
48#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053049
50#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
51#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
52#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
53#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
54
55#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
56#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
57#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
58#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
59
60#define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
61#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
62
63static struct regulator *hsusb_3p3;
64static struct regulator *hsusb_1p8;
65static struct regulator *hsusb_vddcx;
66
67static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
68{
69 int ret = 0;
70
71 if (init) {
72 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
73 if (IS_ERR(hsusb_vddcx)) {
74 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
75 return PTR_ERR(hsusb_vddcx);
76 }
77
78 ret = regulator_set_voltage(hsusb_vddcx,
79 USB_PHY_VDD_DIG_VOL_MIN,
80 USB_PHY_VDD_DIG_VOL_MAX);
81 if (ret) {
82 dev_err(motg->otg.dev, "unable to set the voltage "
83 "for hsusb vddcx\n");
84 regulator_put(hsusb_vddcx);
85 return ret;
86 }
87
88 ret = regulator_enable(hsusb_vddcx);
89 if (ret) {
90 dev_err(motg->otg.dev, "unable to enable hsusb vddcx\n");
91 regulator_put(hsusb_vddcx);
92 }
93 } else {
94 ret = regulator_set_voltage(hsusb_vddcx, 0,
95 USB_PHY_VDD_DIG_VOL_MIN);
96 if (ret) {
97 dev_err(motg->otg.dev, "unable to set the voltage "
98 "for hsusb vddcx\n");
99 return ret;
100 }
101 ret = regulator_disable(hsusb_vddcx);
102 if (ret)
103 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
104
105 regulator_put(hsusb_vddcx);
106 }
107
108 return ret;
109}
110
111static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
112{
113 int rc = 0;
114
115 if (init) {
116 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
117 if (IS_ERR(hsusb_3p3)) {
118 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
119 return PTR_ERR(hsusb_3p3);
120 }
121
122 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
123 USB_PHY_3P3_VOL_MAX);
124 if (rc) {
125 dev_err(motg->otg.dev, "unable to set voltage level "
126 "for hsusb 3p3\n");
127 goto put_3p3;
128 }
129 rc = regulator_enable(hsusb_3p3);
130 if (rc) {
131 dev_err(motg->otg.dev, "unable to enable the hsusb 3p3\n");
132 goto put_3p3;
133 }
134 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
135 if (IS_ERR(hsusb_1p8)) {
136 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
137 rc = PTR_ERR(hsusb_1p8);
138 goto disable_3p3;
139 }
140 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
141 USB_PHY_1P8_VOL_MAX);
142 if (rc) {
143 dev_err(motg->otg.dev, "unable to set voltage level "
144 "for hsusb 1p8\n");
145 goto put_1p8;
146 }
147 rc = regulator_enable(hsusb_1p8);
148 if (rc) {
149 dev_err(motg->otg.dev, "unable to enable the hsusb 1p8\n");
150 goto put_1p8;
151 }
152
153 return 0;
154 }
155
156 regulator_disable(hsusb_1p8);
157put_1p8:
158 regulator_put(hsusb_1p8);
159disable_3p3:
160 regulator_disable(hsusb_3p3);
161put_3p3:
162 regulator_put(hsusb_3p3);
163 return rc;
164}
165
166static int msm_hsusb_ldo_set_mode(int on)
167{
168 int ret = 0;
169
170 if (!hsusb_1p8 || IS_ERR(hsusb_1p8)) {
171 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
172 return -ENODEV;
173 }
174
175 if (!hsusb_3p3 || IS_ERR(hsusb_3p3)) {
176 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
177 return -ENODEV;
178 }
179
180 if (on) {
181 ret = regulator_set_optimum_mode(hsusb_1p8,
182 USB_PHY_1P8_HPM_LOAD);
183 if (ret < 0) {
184 pr_err("%s: Unable to set HPM of the regulator "
185 "HSUSB_1p8\n", __func__);
186 return ret;
187 }
188 ret = regulator_set_optimum_mode(hsusb_3p3,
189 USB_PHY_3P3_HPM_LOAD);
190 if (ret < 0) {
191 pr_err("%s: Unable to set HPM of the regulator "
192 "HSUSB_3p3\n", __func__);
193 regulator_set_optimum_mode(hsusb_1p8,
194 USB_PHY_1P8_LPM_LOAD);
195 return ret;
196 }
197 } else {
198 ret = regulator_set_optimum_mode(hsusb_1p8,
199 USB_PHY_1P8_LPM_LOAD);
200 if (ret < 0)
201 pr_err("%s: Unable to set LPM of the regulator "
202 "HSUSB_1p8\n", __func__);
203 ret = regulator_set_optimum_mode(hsusb_3p3,
204 USB_PHY_3P3_LPM_LOAD);
205 if (ret < 0)
206 pr_err("%s: Unable to set LPM of the regulator "
207 "HSUSB_3p3\n", __func__);
208 }
209
210 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
211 return ret < 0 ? ret : 0;
212}
213
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530214static int ulpi_read(struct otg_transceiver *otg, u32 reg)
215{
216 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
217 int cnt = 0;
218
219 /* initiate read operation */
220 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
221 USB_ULPI_VIEWPORT);
222
223 /* wait for completion */
224 while (cnt < ULPI_IO_TIMEOUT_USEC) {
225 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
226 break;
227 udelay(1);
228 cnt++;
229 }
230
231 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
232 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
233 readl(USB_ULPI_VIEWPORT));
234 return -ETIMEDOUT;
235 }
236 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
237}
238
239static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
240{
241 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
242 int cnt = 0;
243
244 /* initiate write operation */
245 writel(ULPI_RUN | ULPI_WRITE |
246 ULPI_ADDR(reg) | ULPI_DATA(val),
247 USB_ULPI_VIEWPORT);
248
249 /* wait for completion */
250 while (cnt < ULPI_IO_TIMEOUT_USEC) {
251 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
252 break;
253 udelay(1);
254 cnt++;
255 }
256
257 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
258 dev_err(otg->dev, "ulpi_write: timeout\n");
259 return -ETIMEDOUT;
260 }
261 return 0;
262}
263
264static struct otg_io_access_ops msm_otg_io_ops = {
265 .read = ulpi_read,
266 .write = ulpi_write,
267};
268
269static void ulpi_init(struct msm_otg *motg)
270{
271 struct msm_otg_platform_data *pdata = motg->pdata;
272 int *seq = pdata->phy_init_seq;
273
274 if (!seq)
275 return;
276
277 while (seq[0] >= 0) {
278 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
279 seq[0], seq[1]);
280 ulpi_write(&motg->otg, seq[0], seq[1]);
281 seq += 2;
282 }
283}
284
285static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
286{
287 int ret;
288
289 if (assert) {
290 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
291 if (ret)
292 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
293 } else {
294 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
295 if (ret)
296 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
297 }
298 return ret;
299}
300
301static int msm_otg_phy_clk_reset(struct msm_otg *motg)
302{
303 int ret;
304
305 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
306 if (ret) {
307 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
308 return ret;
309 }
310 usleep_range(10000, 12000);
311 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
312 if (ret)
313 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
314 return ret;
315}
316
317static int msm_otg_phy_reset(struct msm_otg *motg)
318{
319 u32 val;
320 int ret;
321 int retries;
322
323 ret = msm_otg_link_clk_reset(motg, 1);
324 if (ret)
325 return ret;
326 ret = msm_otg_phy_clk_reset(motg);
327 if (ret)
328 return ret;
329 ret = msm_otg_link_clk_reset(motg, 0);
330 if (ret)
331 return ret;
332
333 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
334 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
335
336 for (retries = 3; retries > 0; retries--) {
337 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
338 ULPI_CLR(ULPI_FUNC_CTRL));
339 if (!ret)
340 break;
341 ret = msm_otg_phy_clk_reset(motg);
342 if (ret)
343 return ret;
344 }
345 if (!retries)
346 return -ETIMEDOUT;
347
348 /* This reset calibrates the phy, if the above write succeeded */
349 ret = msm_otg_phy_clk_reset(motg);
350 if (ret)
351 return ret;
352
353 for (retries = 3; retries > 0; retries--) {
354 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
355 if (ret != -ETIMEDOUT)
356 break;
357 ret = msm_otg_phy_clk_reset(motg);
358 if (ret)
359 return ret;
360 }
361 if (!retries)
362 return -ETIMEDOUT;
363
364 dev_info(motg->otg.dev, "phy_reset: success\n");
365 return 0;
366}
367
368#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
369static int msm_otg_reset(struct otg_transceiver *otg)
370{
371 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
372 struct msm_otg_platform_data *pdata = motg->pdata;
373 int cnt = 0;
374 int ret;
375 u32 val = 0;
376 u32 ulpi_val = 0;
377
378 ret = msm_otg_phy_reset(motg);
379 if (ret) {
380 dev_err(otg->dev, "phy_reset failed\n");
381 return ret;
382 }
383
384 ulpi_init(motg);
385
386 writel(USBCMD_RESET, USB_USBCMD);
387 while (cnt < LINK_RESET_TIMEOUT_USEC) {
388 if (!(readl(USB_USBCMD) & USBCMD_RESET))
389 break;
390 udelay(1);
391 cnt++;
392 }
393 if (cnt >= LINK_RESET_TIMEOUT_USEC)
394 return -ETIMEDOUT;
395
396 /* select ULPI phy */
397 writel(0x80000000, USB_PORTSC);
398
399 msleep(100);
400
401 writel(0x0, USB_AHBBURST);
402 writel(0x00, USB_AHBMODE);
403
404 if (pdata->otg_control == OTG_PHY_CONTROL) {
405 val = readl(USB_OTGSC);
406 if (pdata->mode == USB_OTG) {
407 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
408 val |= OTGSC_IDIE | OTGSC_BSVIE;
409 } else if (pdata->mode == USB_PERIPHERAL) {
410 ulpi_val = ULPI_INT_SESS_VALID;
411 val |= OTGSC_BSVIE;
412 }
413 writel(val, USB_OTGSC);
414 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
415 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
416 }
417
418 return 0;
419}
420
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530421#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530422#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
423
424#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530425static int msm_otg_suspend(struct msm_otg *motg)
426{
427 struct otg_transceiver *otg = &motg->otg;
428 struct usb_bus *bus = otg->host;
429 struct msm_otg_platform_data *pdata = motg->pdata;
430 int cnt = 0;
431
432 if (atomic_read(&motg->in_lpm))
433 return 0;
434
435 disable_irq(motg->irq);
436 /*
437 * Interrupt Latch Register auto-clear feature is not present
438 * in all PHY versions. Latch register is clear on read type.
439 * Clear latch register to avoid spurious wakeup from
440 * low power mode (LPM).
441 */
442 ulpi_read(otg, 0x14);
443
444 /*
445 * PHY comparators are disabled when PHY enters into low power
446 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
447 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
448 * PHY comparators. This save significant amount of power.
449 */
450 if (pdata->otg_control == OTG_PHY_CONTROL)
451 ulpi_write(otg, 0x01, 0x30);
452
453 /*
454 * PLL is not turned off when PHY enters into low power mode (LPM).
455 * Disable PLL for maximum power savings.
456 */
457 ulpi_write(otg, 0x08, 0x09);
458
459 /*
460 * PHY may take some time or even fail to enter into low power
461 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
462 * in failure case.
463 */
464 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
465 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
466 if (readl(USB_PORTSC) & PORTSC_PHCD)
467 break;
468 udelay(1);
469 cnt++;
470 }
471
472 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
473 dev_err(otg->dev, "Unable to suspend PHY\n");
474 msm_otg_reset(otg);
475 enable_irq(motg->irq);
476 return -ETIMEDOUT;
477 }
478
479 /*
480 * PHY has capability to generate interrupt asynchronously in low
481 * power mode (LPM). This interrupt is level triggered. So USB IRQ
482 * line must be disabled till async interrupt enable bit is cleared
483 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
484 * block data communication from PHY.
485 */
486 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
487
488 clk_disable(motg->pclk);
489 clk_disable(motg->clk);
490 if (motg->core_clk)
491 clk_disable(motg->core_clk);
492
Anji jonnala0f73cac2011-05-04 10:19:46 +0530493 if (!IS_ERR(motg->pclk_src))
494 clk_disable(motg->pclk_src);
495
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530496 if (device_may_wakeup(otg->dev))
497 enable_irq_wake(motg->irq);
498 if (bus)
499 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
500
501 atomic_set(&motg->in_lpm, 1);
502 enable_irq(motg->irq);
503
504 dev_info(otg->dev, "USB in low power mode\n");
505
506 return 0;
507}
508
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530509static int msm_otg_resume(struct msm_otg *motg)
510{
511 struct otg_transceiver *otg = &motg->otg;
512 struct usb_bus *bus = otg->host;
513 int cnt = 0;
514 unsigned temp;
515
516 if (!atomic_read(&motg->in_lpm))
517 return 0;
518
Anji jonnala0f73cac2011-05-04 10:19:46 +0530519 if (!IS_ERR(motg->pclk_src))
520 clk_enable(motg->pclk_src);
521
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530522 clk_enable(motg->pclk);
523 clk_enable(motg->clk);
524 if (motg->core_clk)
525 clk_enable(motg->core_clk);
526
527 temp = readl(USB_USBCMD);
528 temp &= ~ASYNC_INTR_CTRL;
529 temp &= ~ULPI_STP_CTRL;
530 writel(temp, USB_USBCMD);
531
532 /*
533 * PHY comes out of low power mode (LPM) in case of wakeup
534 * from asynchronous interrupt.
535 */
536 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
537 goto skip_phy_resume;
538
539 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
540 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
541 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
542 break;
543 udelay(1);
544 cnt++;
545 }
546
547 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
548 /*
549 * This is a fatal error. Reset the link and
550 * PHY. USB state can not be restored. Re-insertion
551 * of USB cable is the only way to get USB working.
552 */
553 dev_err(otg->dev, "Unable to resume USB."
554 "Re-plugin the cable\n");
555 msm_otg_reset(otg);
556 }
557
558skip_phy_resume:
559 if (device_may_wakeup(otg->dev))
560 disable_irq_wake(motg->irq);
561 if (bus)
562 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
563
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530564 atomic_set(&motg->in_lpm, 0);
565
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530566 if (motg->async_int) {
567 motg->async_int = 0;
568 pm_runtime_put(otg->dev);
569 enable_irq(motg->irq);
570 }
571
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530572 dev_info(otg->dev, "USB exited from low power mode\n");
573
574 return 0;
575}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530576#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530577
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530578static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
579{
580 if (motg->cur_power == mA)
581 return;
582
583 /* TODO: Notify PMIC about available current */
584 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
585 motg->cur_power = mA;
586}
587
588static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
589{
590 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
591
592 /*
593 * Gadget driver uses set_power method to notify about the
594 * available current based on suspend/configured states.
595 *
596 * IDEV_CHG can be drawn irrespective of suspend/un-configured
597 * states when CDP/ACA is connected.
598 */
599 if (motg->chg_type == USB_SDP_CHARGER)
600 msm_otg_notify_charger(motg, mA);
601
602 return 0;
603}
604
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530605static void msm_otg_start_host(struct otg_transceiver *otg, int on)
606{
607 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
608 struct msm_otg_platform_data *pdata = motg->pdata;
609 struct usb_hcd *hcd;
610
611 if (!otg->host)
612 return;
613
614 hcd = bus_to_hcd(otg->host);
615
616 if (on) {
617 dev_dbg(otg->dev, "host on\n");
618
619 if (pdata->vbus_power)
620 pdata->vbus_power(1);
621 /*
622 * Some boards have a switch cotrolled by gpio
623 * to enable/disable internal HUB. Enable internal
624 * HUB before kicking the host.
625 */
626 if (pdata->setup_gpio)
627 pdata->setup_gpio(OTG_STATE_A_HOST);
628#ifdef CONFIG_USB
629 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
630#endif
631 } else {
632 dev_dbg(otg->dev, "host off\n");
633
634#ifdef CONFIG_USB
635 usb_remove_hcd(hcd);
636#endif
637 if (pdata->setup_gpio)
638 pdata->setup_gpio(OTG_STATE_UNDEFINED);
639 if (pdata->vbus_power)
640 pdata->vbus_power(0);
641 }
642}
643
644static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
645{
646 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
647 struct usb_hcd *hcd;
648
649 /*
650 * Fail host registration if this board can support
651 * only peripheral configuration.
652 */
653 if (motg->pdata->mode == USB_PERIPHERAL) {
654 dev_info(otg->dev, "Host mode is not supported\n");
655 return -ENODEV;
656 }
657
658 if (!host) {
659 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530660 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530661 msm_otg_start_host(otg, 0);
662 otg->host = NULL;
663 otg->state = OTG_STATE_UNDEFINED;
664 schedule_work(&motg->sm_work);
665 } else {
666 otg->host = NULL;
667 }
668
669 return 0;
670 }
671
672 hcd = bus_to_hcd(host);
673 hcd->power_budget = motg->pdata->power_budget;
674
675 otg->host = host;
676 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
677
678 /*
679 * Kick the state machine work, if peripheral is not supported
680 * or peripheral is already registered with us.
681 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530682 if (motg->pdata->mode == USB_HOST || otg->gadget) {
683 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530684 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530685 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530686
687 return 0;
688}
689
690static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
691{
692 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
693 struct msm_otg_platform_data *pdata = motg->pdata;
694
695 if (!otg->gadget)
696 return;
697
698 if (on) {
699 dev_dbg(otg->dev, "gadget on\n");
700 /*
701 * Some boards have a switch cotrolled by gpio
702 * to enable/disable internal HUB. Disable internal
703 * HUB before kicking the gadget.
704 */
705 if (pdata->setup_gpio)
706 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
707 usb_gadget_vbus_connect(otg->gadget);
708 } else {
709 dev_dbg(otg->dev, "gadget off\n");
710 usb_gadget_vbus_disconnect(otg->gadget);
711 if (pdata->setup_gpio)
712 pdata->setup_gpio(OTG_STATE_UNDEFINED);
713 }
714
715}
716
717static int msm_otg_set_peripheral(struct otg_transceiver *otg,
718 struct usb_gadget *gadget)
719{
720 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
721
722 /*
723 * Fail peripheral registration if this board can support
724 * only host configuration.
725 */
726 if (motg->pdata->mode == USB_HOST) {
727 dev_info(otg->dev, "Peripheral mode is not supported\n");
728 return -ENODEV;
729 }
730
731 if (!gadget) {
732 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530733 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530734 msm_otg_start_peripheral(otg, 0);
735 otg->gadget = NULL;
736 otg->state = OTG_STATE_UNDEFINED;
737 schedule_work(&motg->sm_work);
738 } else {
739 otg->gadget = NULL;
740 }
741
742 return 0;
743 }
744 otg->gadget = gadget;
745 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
746
747 /*
748 * Kick the state machine work, if host is not supported
749 * or host is already registered with us.
750 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530751 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
752 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530753 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530754 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530755
756 return 0;
757}
758
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530759static bool msm_chg_check_secondary_det(struct msm_otg *motg)
760{
761 struct otg_transceiver *otg = &motg->otg;
762 u32 chg_det;
763 bool ret = false;
764
765 switch (motg->pdata->phy_type) {
766 case CI_45NM_INTEGRATED_PHY:
767 chg_det = ulpi_read(otg, 0x34);
768 ret = chg_det & (1 << 4);
769 break;
770 case SNPS_28NM_INTEGRATED_PHY:
771 chg_det = ulpi_read(otg, 0x87);
772 ret = chg_det & 1;
773 break;
774 default:
775 break;
776 }
777 return ret;
778}
779
780static void msm_chg_enable_secondary_det(struct msm_otg *motg)
781{
782 struct otg_transceiver *otg = &motg->otg;
783 u32 chg_det;
784
785 switch (motg->pdata->phy_type) {
786 case CI_45NM_INTEGRATED_PHY:
787 chg_det = ulpi_read(otg, 0x34);
788 /* Turn off charger block */
789 chg_det |= ~(1 << 1);
790 ulpi_write(otg, chg_det, 0x34);
791 udelay(20);
792 /* control chg block via ULPI */
793 chg_det &= ~(1 << 3);
794 ulpi_write(otg, chg_det, 0x34);
795 /* put it in host mode for enabling D- source */
796 chg_det &= ~(1 << 2);
797 ulpi_write(otg, chg_det, 0x34);
798 /* Turn on chg detect block */
799 chg_det &= ~(1 << 1);
800 ulpi_write(otg, chg_det, 0x34);
801 udelay(20);
802 /* enable chg detection */
803 chg_det &= ~(1 << 0);
804 ulpi_write(otg, chg_det, 0x34);
805 break;
806 case SNPS_28NM_INTEGRATED_PHY:
807 /*
808 * Configure DM as current source, DP as current sink
809 * and enable battery charging comparators.
810 */
811 ulpi_write(otg, 0x8, 0x85);
812 ulpi_write(otg, 0x2, 0x85);
813 ulpi_write(otg, 0x1, 0x85);
814 break;
815 default:
816 break;
817 }
818}
819
820static bool msm_chg_check_primary_det(struct msm_otg *motg)
821{
822 struct otg_transceiver *otg = &motg->otg;
823 u32 chg_det;
824 bool ret = false;
825
826 switch (motg->pdata->phy_type) {
827 case CI_45NM_INTEGRATED_PHY:
828 chg_det = ulpi_read(otg, 0x34);
829 ret = chg_det & (1 << 4);
830 break;
831 case SNPS_28NM_INTEGRATED_PHY:
832 chg_det = ulpi_read(otg, 0x87);
833 ret = chg_det & 1;
834 break;
835 default:
836 break;
837 }
838 return ret;
839}
840
841static void msm_chg_enable_primary_det(struct msm_otg *motg)
842{
843 struct otg_transceiver *otg = &motg->otg;
844 u32 chg_det;
845
846 switch (motg->pdata->phy_type) {
847 case CI_45NM_INTEGRATED_PHY:
848 chg_det = ulpi_read(otg, 0x34);
849 /* enable chg detection */
850 chg_det &= ~(1 << 0);
851 ulpi_write(otg, chg_det, 0x34);
852 break;
853 case SNPS_28NM_INTEGRATED_PHY:
854 /*
855 * Configure DP as current source, DM as current sink
856 * and enable battery charging comparators.
857 */
858 ulpi_write(otg, 0x2, 0x85);
859 ulpi_write(otg, 0x1, 0x85);
860 break;
861 default:
862 break;
863 }
864}
865
866static bool msm_chg_check_dcd(struct msm_otg *motg)
867{
868 struct otg_transceiver *otg = &motg->otg;
869 u32 line_state;
870 bool ret = false;
871
872 switch (motg->pdata->phy_type) {
873 case CI_45NM_INTEGRATED_PHY:
874 line_state = ulpi_read(otg, 0x15);
875 ret = !(line_state & 1);
876 break;
877 case SNPS_28NM_INTEGRATED_PHY:
878 line_state = ulpi_read(otg, 0x87);
879 ret = line_state & 2;
880 break;
881 default:
882 break;
883 }
884 return ret;
885}
886
887static void msm_chg_disable_dcd(struct msm_otg *motg)
888{
889 struct otg_transceiver *otg = &motg->otg;
890 u32 chg_det;
891
892 switch (motg->pdata->phy_type) {
893 case CI_45NM_INTEGRATED_PHY:
894 chg_det = ulpi_read(otg, 0x34);
895 chg_det &= ~(1 << 5);
896 ulpi_write(otg, chg_det, 0x34);
897 break;
898 case SNPS_28NM_INTEGRATED_PHY:
899 ulpi_write(otg, 0x10, 0x86);
900 break;
901 default:
902 break;
903 }
904}
905
906static void msm_chg_enable_dcd(struct msm_otg *motg)
907{
908 struct otg_transceiver *otg = &motg->otg;
909 u32 chg_det;
910
911 switch (motg->pdata->phy_type) {
912 case CI_45NM_INTEGRATED_PHY:
913 chg_det = ulpi_read(otg, 0x34);
914 /* Turn on D+ current source */
915 chg_det |= (1 << 5);
916 ulpi_write(otg, chg_det, 0x34);
917 break;
918 case SNPS_28NM_INTEGRATED_PHY:
919 /* Data contact detection enable */
920 ulpi_write(otg, 0x10, 0x85);
921 break;
922 default:
923 break;
924 }
925}
926
927static void msm_chg_block_on(struct msm_otg *motg)
928{
929 struct otg_transceiver *otg = &motg->otg;
930 u32 func_ctrl, chg_det;
931
932 /* put the controller in non-driving mode */
933 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
934 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
935 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
936 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
937
938 switch (motg->pdata->phy_type) {
939 case CI_45NM_INTEGRATED_PHY:
940 chg_det = ulpi_read(otg, 0x34);
941 /* control chg block via ULPI */
942 chg_det &= ~(1 << 3);
943 ulpi_write(otg, chg_det, 0x34);
944 /* Turn on chg detect block */
945 chg_det &= ~(1 << 1);
946 ulpi_write(otg, chg_det, 0x34);
947 udelay(20);
948 break;
949 case SNPS_28NM_INTEGRATED_PHY:
950 /* Clear charger detecting control bits */
951 ulpi_write(otg, 0x3F, 0x86);
952 /* Clear alt interrupt latch and enable bits */
953 ulpi_write(otg, 0x1F, 0x92);
954 ulpi_write(otg, 0x1F, 0x95);
955 udelay(100);
956 break;
957 default:
958 break;
959 }
960}
961
962static void msm_chg_block_off(struct msm_otg *motg)
963{
964 struct otg_transceiver *otg = &motg->otg;
965 u32 func_ctrl, chg_det;
966
967 switch (motg->pdata->phy_type) {
968 case CI_45NM_INTEGRATED_PHY:
969 chg_det = ulpi_read(otg, 0x34);
970 /* Turn off charger block */
971 chg_det |= ~(1 << 1);
972 ulpi_write(otg, chg_det, 0x34);
973 break;
974 case SNPS_28NM_INTEGRATED_PHY:
975 /* Clear charger detecting control bits */
976 ulpi_write(otg, 0x3F, 0x86);
977 /* Clear alt interrupt latch and enable bits */
978 ulpi_write(otg, 0x1F, 0x92);
979 ulpi_write(otg, 0x1F, 0x95);
980 break;
981 default:
982 break;
983 }
984
985 /* put the controller in normal mode */
986 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
987 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
988 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
989 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
990}
991
992#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
993#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
994#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
995#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
996static void msm_chg_detect_work(struct work_struct *w)
997{
998 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
999 struct otg_transceiver *otg = &motg->otg;
1000 bool is_dcd, tmout, vout;
1001 unsigned long delay;
1002
1003 dev_dbg(otg->dev, "chg detection work\n");
1004 switch (motg->chg_state) {
1005 case USB_CHG_STATE_UNDEFINED:
1006 pm_runtime_get_sync(otg->dev);
1007 msm_chg_block_on(motg);
1008 msm_chg_enable_dcd(motg);
1009 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1010 motg->dcd_retries = 0;
1011 delay = MSM_CHG_DCD_POLL_TIME;
1012 break;
1013 case USB_CHG_STATE_WAIT_FOR_DCD:
1014 is_dcd = msm_chg_check_dcd(motg);
1015 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1016 if (is_dcd || tmout) {
1017 msm_chg_disable_dcd(motg);
1018 msm_chg_enable_primary_det(motg);
1019 delay = MSM_CHG_PRIMARY_DET_TIME;
1020 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1021 } else {
1022 delay = MSM_CHG_DCD_POLL_TIME;
1023 }
1024 break;
1025 case USB_CHG_STATE_DCD_DONE:
1026 vout = msm_chg_check_primary_det(motg);
1027 if (vout) {
1028 msm_chg_enable_secondary_det(motg);
1029 delay = MSM_CHG_SECONDARY_DET_TIME;
1030 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1031 } else {
1032 motg->chg_type = USB_SDP_CHARGER;
1033 motg->chg_state = USB_CHG_STATE_DETECTED;
1034 delay = 0;
1035 }
1036 break;
1037 case USB_CHG_STATE_PRIMARY_DONE:
1038 vout = msm_chg_check_secondary_det(motg);
1039 if (vout)
1040 motg->chg_type = USB_DCP_CHARGER;
1041 else
1042 motg->chg_type = USB_CDP_CHARGER;
1043 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1044 /* fall through */
1045 case USB_CHG_STATE_SECONDARY_DONE:
1046 motg->chg_state = USB_CHG_STATE_DETECTED;
1047 case USB_CHG_STATE_DETECTED:
1048 msm_chg_block_off(motg);
1049 dev_dbg(otg->dev, "charger = %d\n", motg->chg_type);
1050 schedule_work(&motg->sm_work);
1051 return;
1052 default:
1053 return;
1054 }
1055
1056 schedule_delayed_work(&motg->chg_work, delay);
1057}
1058
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301059/*
1060 * We support OTG, Peripheral only and Host only configurations. In case
1061 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1062 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1063 * enabled when switch is controlled by user and default mode is supplied
1064 * by board file, which can be changed by userspace later.
1065 */
1066static void msm_otg_init_sm(struct msm_otg *motg)
1067{
1068 struct msm_otg_platform_data *pdata = motg->pdata;
1069 u32 otgsc = readl(USB_OTGSC);
1070
1071 switch (pdata->mode) {
1072 case USB_OTG:
1073 if (pdata->otg_control == OTG_PHY_CONTROL) {
1074 if (otgsc & OTGSC_ID)
1075 set_bit(ID, &motg->inputs);
1076 else
1077 clear_bit(ID, &motg->inputs);
1078
1079 if (otgsc & OTGSC_BSV)
1080 set_bit(B_SESS_VLD, &motg->inputs);
1081 else
1082 clear_bit(B_SESS_VLD, &motg->inputs);
1083 } else if (pdata->otg_control == OTG_USER_CONTROL) {
1084 if (pdata->default_mode == USB_HOST) {
1085 clear_bit(ID, &motg->inputs);
1086 } else if (pdata->default_mode == USB_PERIPHERAL) {
1087 set_bit(ID, &motg->inputs);
1088 set_bit(B_SESS_VLD, &motg->inputs);
1089 } else {
1090 set_bit(ID, &motg->inputs);
1091 clear_bit(B_SESS_VLD, &motg->inputs);
1092 }
1093 }
1094 break;
1095 case USB_HOST:
1096 clear_bit(ID, &motg->inputs);
1097 break;
1098 case USB_PERIPHERAL:
1099 set_bit(ID, &motg->inputs);
1100 if (otgsc & OTGSC_BSV)
1101 set_bit(B_SESS_VLD, &motg->inputs);
1102 else
1103 clear_bit(B_SESS_VLD, &motg->inputs);
1104 break;
1105 default:
1106 break;
1107 }
1108}
1109
1110static void msm_otg_sm_work(struct work_struct *w)
1111{
1112 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1113 struct otg_transceiver *otg = &motg->otg;
1114
1115 switch (otg->state) {
1116 case OTG_STATE_UNDEFINED:
1117 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1118 msm_otg_reset(otg);
1119 msm_otg_init_sm(motg);
1120 otg->state = OTG_STATE_B_IDLE;
1121 /* FALL THROUGH */
1122 case OTG_STATE_B_IDLE:
1123 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
1124 if (!test_bit(ID, &motg->inputs) && otg->host) {
1125 /* disable BSV bit */
1126 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1127 msm_otg_start_host(otg, 1);
1128 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301129 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1130 switch (motg->chg_state) {
1131 case USB_CHG_STATE_UNDEFINED:
1132 msm_chg_detect_work(&motg->chg_work.work);
1133 break;
1134 case USB_CHG_STATE_DETECTED:
1135 switch (motg->chg_type) {
1136 case USB_DCP_CHARGER:
1137 msm_otg_notify_charger(motg,
1138 IDEV_CHG_MAX);
1139 break;
1140 case USB_CDP_CHARGER:
1141 msm_otg_notify_charger(motg,
1142 IDEV_CHG_MAX);
1143 msm_otg_start_peripheral(otg, 1);
1144 otg->state = OTG_STATE_B_PERIPHERAL;
1145 break;
1146 case USB_SDP_CHARGER:
1147 msm_otg_notify_charger(motg, IUNIT);
1148 msm_otg_start_peripheral(otg, 1);
1149 otg->state = OTG_STATE_B_PERIPHERAL;
1150 break;
1151 default:
1152 break;
1153 }
1154 break;
1155 default:
1156 break;
1157 }
1158 } else {
1159 /*
1160 * If charger detection work is pending, decrement
1161 * the pm usage counter to balance with the one that
1162 * is incremented in charger detection work.
1163 */
1164 if (cancel_delayed_work_sync(&motg->chg_work)) {
1165 pm_runtime_put_sync(otg->dev);
1166 msm_otg_reset(otg);
1167 }
1168 msm_otg_notify_charger(motg, 0);
1169 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1170 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301171 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301172 pm_runtime_put_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173 break;
1174 case OTG_STATE_B_PERIPHERAL:
1175 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1176 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1177 !test_bit(ID, &motg->inputs)) {
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301178 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301179 msm_otg_start_peripheral(otg, 0);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301180 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1181 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301182 otg->state = OTG_STATE_B_IDLE;
1183 msm_otg_reset(otg);
1184 schedule_work(w);
1185 }
1186 break;
1187 case OTG_STATE_A_HOST:
1188 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1189 if (test_bit(ID, &motg->inputs)) {
1190 msm_otg_start_host(otg, 0);
1191 otg->state = OTG_STATE_B_IDLE;
1192 msm_otg_reset(otg);
1193 schedule_work(w);
1194 }
1195 break;
1196 default:
1197 break;
1198 }
1199}
1200
1201static irqreturn_t msm_otg_irq(int irq, void *data)
1202{
1203 struct msm_otg *motg = data;
1204 struct otg_transceiver *otg = &motg->otg;
1205 u32 otgsc = 0;
1206
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301207 if (atomic_read(&motg->in_lpm)) {
1208 disable_irq_nosync(irq);
1209 motg->async_int = 1;
1210 pm_runtime_get(otg->dev);
1211 return IRQ_HANDLED;
1212 }
1213
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301214 otgsc = readl(USB_OTGSC);
1215 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1216 return IRQ_NONE;
1217
1218 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1219 if (otgsc & OTGSC_ID)
1220 set_bit(ID, &motg->inputs);
1221 else
1222 clear_bit(ID, &motg->inputs);
1223 dev_dbg(otg->dev, "ID set/clear\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301224 pm_runtime_get_noresume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301225 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1226 if (otgsc & OTGSC_BSV)
1227 set_bit(B_SESS_VLD, &motg->inputs);
1228 else
1229 clear_bit(B_SESS_VLD, &motg->inputs);
1230 dev_dbg(otg->dev, "BSV set/clear\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301231 pm_runtime_get_noresume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301232 }
1233
1234 writel(otgsc, USB_OTGSC);
1235 schedule_work(&motg->sm_work);
1236 return IRQ_HANDLED;
1237}
1238
1239static int msm_otg_mode_show(struct seq_file *s, void *unused)
1240{
1241 struct msm_otg *motg = s->private;
1242 struct otg_transceiver *otg = &motg->otg;
1243
1244 switch (otg->state) {
1245 case OTG_STATE_A_HOST:
1246 seq_printf(s, "host\n");
1247 break;
1248 case OTG_STATE_B_PERIPHERAL:
1249 seq_printf(s, "peripheral\n");
1250 break;
1251 default:
1252 seq_printf(s, "none\n");
1253 break;
1254 }
1255
1256 return 0;
1257}
1258
1259static int msm_otg_mode_open(struct inode *inode, struct file *file)
1260{
1261 return single_open(file, msm_otg_mode_show, inode->i_private);
1262}
1263
1264static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1265 size_t count, loff_t *ppos)
1266{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301267 struct seq_file *s = file->private_data;
1268 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301269 char buf[16];
1270 struct otg_transceiver *otg = &motg->otg;
1271 int status = count;
1272 enum usb_mode_type req_mode;
1273
1274 memset(buf, 0x00, sizeof(buf));
1275
1276 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1277 status = -EFAULT;
1278 goto out;
1279 }
1280
1281 if (!strncmp(buf, "host", 4)) {
1282 req_mode = USB_HOST;
1283 } else if (!strncmp(buf, "peripheral", 10)) {
1284 req_mode = USB_PERIPHERAL;
1285 } else if (!strncmp(buf, "none", 4)) {
1286 req_mode = USB_NONE;
1287 } else {
1288 status = -EINVAL;
1289 goto out;
1290 }
1291
1292 switch (req_mode) {
1293 case USB_NONE:
1294 switch (otg->state) {
1295 case OTG_STATE_A_HOST:
1296 case OTG_STATE_B_PERIPHERAL:
1297 set_bit(ID, &motg->inputs);
1298 clear_bit(B_SESS_VLD, &motg->inputs);
1299 break;
1300 default:
1301 goto out;
1302 }
1303 break;
1304 case USB_PERIPHERAL:
1305 switch (otg->state) {
1306 case OTG_STATE_B_IDLE:
1307 case OTG_STATE_A_HOST:
1308 set_bit(ID, &motg->inputs);
1309 set_bit(B_SESS_VLD, &motg->inputs);
1310 break;
1311 default:
1312 goto out;
1313 }
1314 break;
1315 case USB_HOST:
1316 switch (otg->state) {
1317 case OTG_STATE_B_IDLE:
1318 case OTG_STATE_B_PERIPHERAL:
1319 clear_bit(ID, &motg->inputs);
1320 break;
1321 default:
1322 goto out;
1323 }
1324 break;
1325 default:
1326 goto out;
1327 }
1328
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301329 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301330 schedule_work(&motg->sm_work);
1331out:
1332 return status;
1333}
1334
1335const struct file_operations msm_otg_mode_fops = {
1336 .open = msm_otg_mode_open,
1337 .read = seq_read,
1338 .write = msm_otg_mode_write,
1339 .llseek = seq_lseek,
1340 .release = single_release,
1341};
1342
1343static struct dentry *msm_otg_dbg_root;
1344static struct dentry *msm_otg_dbg_mode;
1345
1346static int msm_otg_debugfs_init(struct msm_otg *motg)
1347{
1348 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1349
1350 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1351 return -ENODEV;
1352
1353 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1354 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1355 if (!msm_otg_dbg_mode) {
1356 debugfs_remove(msm_otg_dbg_root);
1357 msm_otg_dbg_root = NULL;
1358 return -ENODEV;
1359 }
1360
1361 return 0;
1362}
1363
1364static void msm_otg_debugfs_cleanup(void)
1365{
1366 debugfs_remove(msm_otg_dbg_mode);
1367 debugfs_remove(msm_otg_dbg_root);
1368}
1369
1370static int __init msm_otg_probe(struct platform_device *pdev)
1371{
1372 int ret = 0;
1373 struct resource *res;
1374 struct msm_otg *motg;
1375 struct otg_transceiver *otg;
1376
1377 dev_info(&pdev->dev, "msm_otg probe\n");
1378 if (!pdev->dev.platform_data) {
1379 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
1380 return -ENODEV;
1381 }
1382
1383 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
1384 if (!motg) {
1385 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1386 return -ENOMEM;
1387 }
1388
1389 motg->pdata = pdev->dev.platform_data;
1390 otg = &motg->otg;
1391 otg->dev = &pdev->dev;
1392
1393 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
1394 if (IS_ERR(motg->phy_reset_clk)) {
1395 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
1396 ret = PTR_ERR(motg->phy_reset_clk);
1397 goto free_motg;
1398 }
1399
1400 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
1401 if (IS_ERR(motg->clk)) {
1402 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1403 ret = PTR_ERR(motg->clk);
1404 goto put_phy_reset_clk;
1405 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05301406 clk_set_rate(motg->clk, 60000000);
1407
1408 /*
1409 * If USB Core is running its protocol engine based on CORE CLK,
1410 * CORE CLK must be running at >55Mhz for correct HSUSB
1411 * operation and USB core cannot tolerate frequency changes on
1412 * CORE CLK. For such USB cores, vote for maximum clk frequency
1413 * on pclk source
1414 */
1415 if (motg->pdata->pclk_src_name) {
1416 motg->pclk_src = clk_get(&pdev->dev,
1417 motg->pdata->pclk_src_name);
1418 if (IS_ERR(motg->pclk_src))
1419 goto put_clk;
1420 clk_set_rate(motg->pclk_src, INT_MAX);
1421 clk_enable(motg->pclk_src);
1422 } else
1423 motg->pclk_src = ERR_PTR(-ENOENT);
1424
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301425
1426 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
1427 if (IS_ERR(motg->pclk)) {
1428 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1429 ret = PTR_ERR(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301430 goto put_pclk_src;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301431 }
1432
1433 /*
1434 * USB core clock is not present on all MSM chips. This
1435 * clock is introduced to remove the dependency on AXI
1436 * bus frequency.
1437 */
1438 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
1439 if (IS_ERR(motg->core_clk))
1440 motg->core_clk = NULL;
1441
1442 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1443 if (!res) {
1444 dev_err(&pdev->dev, "failed to get platform resource mem\n");
1445 ret = -ENODEV;
1446 goto put_core_clk;
1447 }
1448
1449 motg->regs = ioremap(res->start, resource_size(res));
1450 if (!motg->regs) {
1451 dev_err(&pdev->dev, "ioremap failed\n");
1452 ret = -ENOMEM;
1453 goto put_core_clk;
1454 }
1455 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1456
1457 motg->irq = platform_get_irq(pdev, 0);
1458 if (!motg->irq) {
1459 dev_err(&pdev->dev, "platform_get_irq failed\n");
1460 ret = -ENODEV;
1461 goto free_regs;
1462 }
1463
1464 clk_enable(motg->clk);
1465 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301466
1467 ret = msm_hsusb_init_vddcx(motg, 1);
1468 if (ret) {
1469 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1470 goto free_regs;
1471 }
1472
1473 ret = msm_hsusb_ldo_init(motg, 1);
1474 if (ret) {
1475 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1476 goto vddcx_exit;
1477 }
1478 ret = msm_hsusb_ldo_set_mode(1);
1479 if (ret) {
1480 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1481 goto ldo_exit;
1482 }
1483
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301484 if (motg->core_clk)
1485 clk_enable(motg->core_clk);
1486
1487 writel(0, USB_USBINTR);
1488 writel(0, USB_OTGSC);
1489
1490 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301491 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301492 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
1493 "msm_otg", motg);
1494 if (ret) {
1495 dev_err(&pdev->dev, "request irq failed\n");
1496 goto disable_clks;
1497 }
1498
1499 otg->init = msm_otg_reset;
1500 otg->set_host = msm_otg_set_host;
1501 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301502 otg->set_power = msm_otg_set_power;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301503
1504 otg->io_ops = &msm_otg_io_ops;
1505
1506 ret = otg_set_transceiver(&motg->otg);
1507 if (ret) {
1508 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
1509 goto free_irq;
1510 }
1511
1512 platform_set_drvdata(pdev, motg);
1513 device_init_wakeup(&pdev->dev, 1);
1514
1515 if (motg->pdata->mode == USB_OTG &&
1516 motg->pdata->otg_control == OTG_USER_CONTROL) {
1517 ret = msm_otg_debugfs_init(motg);
1518 if (ret)
1519 dev_dbg(&pdev->dev, "mode debugfs file is"
1520 "not available\n");
1521 }
1522
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301523 pm_runtime_set_active(&pdev->dev);
1524 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301525
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301526 return 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301527free_irq:
1528 free_irq(motg->irq, motg);
1529disable_clks:
1530 clk_disable(motg->pclk);
1531 clk_disable(motg->clk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05301532ldo_exit:
1533 msm_hsusb_ldo_init(motg, 0);
1534vddcx_exit:
1535 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301536free_regs:
1537 iounmap(motg->regs);
1538put_core_clk:
1539 if (motg->core_clk)
1540 clk_put(motg->core_clk);
1541 clk_put(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301542put_pclk_src:
1543 if (!IS_ERR(motg->pclk_src)) {
1544 clk_disable(motg->pclk_src);
1545 clk_put(motg->pclk_src);
1546 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301547put_clk:
1548 clk_put(motg->clk);
1549put_phy_reset_clk:
1550 clk_put(motg->phy_reset_clk);
1551free_motg:
1552 kfree(motg);
1553 return ret;
1554}
1555
1556static int __devexit msm_otg_remove(struct platform_device *pdev)
1557{
1558 struct msm_otg *motg = platform_get_drvdata(pdev);
1559 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301560 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301561
1562 if (otg->host || otg->gadget)
1563 return -EBUSY;
1564
1565 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301566 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301567 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301568
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301569 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301570
1571 device_init_wakeup(&pdev->dev, 0);
1572 pm_runtime_disable(&pdev->dev);
1573
1574 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301575 free_irq(motg->irq, motg);
1576
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301577 /*
1578 * Put PHY in low power mode.
1579 */
1580 ulpi_read(otg, 0x14);
1581 ulpi_write(otg, 0x08, 0x09);
1582
1583 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1584 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1585 if (readl(USB_PORTSC) & PORTSC_PHCD)
1586 break;
1587 udelay(1);
1588 cnt++;
1589 }
1590 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1591 dev_err(otg->dev, "Unable to suspend PHY\n");
1592
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301593 clk_disable(motg->pclk);
1594 clk_disable(motg->clk);
1595 if (motg->core_clk)
1596 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05301597 if (!IS_ERR(motg->pclk_src)) {
1598 clk_disable(motg->pclk_src);
1599 clk_put(motg->pclk_src);
1600 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05301601 msm_hsusb_ldo_init(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301602
1603 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301604 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301605
1606 clk_put(motg->phy_reset_clk);
1607 clk_put(motg->pclk);
1608 clk_put(motg->clk);
1609 if (motg->core_clk)
1610 clk_put(motg->core_clk);
1611
1612 kfree(motg);
1613
1614 return 0;
1615}
1616
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301617#ifdef CONFIG_PM_RUNTIME
1618static int msm_otg_runtime_idle(struct device *dev)
1619{
1620 struct msm_otg *motg = dev_get_drvdata(dev);
1621 struct otg_transceiver *otg = &motg->otg;
1622
1623 dev_dbg(dev, "OTG runtime idle\n");
1624
1625 /*
1626 * It is observed some times that a spurious interrupt
1627 * comes when PHY is put into LPM immediately after PHY reset.
1628 * This 1 sec delay also prevents entering into LPM immediately
1629 * after asynchronous interrupt.
1630 */
1631 if (otg->state != OTG_STATE_UNDEFINED)
1632 pm_schedule_suspend(dev, 1000);
1633
1634 return -EAGAIN;
1635}
1636
1637static int msm_otg_runtime_suspend(struct device *dev)
1638{
1639 struct msm_otg *motg = dev_get_drvdata(dev);
1640
1641 dev_dbg(dev, "OTG runtime suspend\n");
1642 return msm_otg_suspend(motg);
1643}
1644
1645static int msm_otg_runtime_resume(struct device *dev)
1646{
1647 struct msm_otg *motg = dev_get_drvdata(dev);
1648
1649 dev_dbg(dev, "OTG runtime resume\n");
1650 return msm_otg_resume(motg);
1651}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301652#endif
1653
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301654#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301655static int msm_otg_pm_suspend(struct device *dev)
1656{
1657 struct msm_otg *motg = dev_get_drvdata(dev);
1658
1659 dev_dbg(dev, "OTG PM suspend\n");
1660 return msm_otg_suspend(motg);
1661}
1662
1663static int msm_otg_pm_resume(struct device *dev)
1664{
1665 struct msm_otg *motg = dev_get_drvdata(dev);
1666 int ret;
1667
1668 dev_dbg(dev, "OTG PM resume\n");
1669
1670 ret = msm_otg_resume(motg);
1671 if (ret)
1672 return ret;
1673
1674 /*
1675 * Runtime PM Documentation recommends bringing the
1676 * device to full powered state upon resume.
1677 */
1678 pm_runtime_disable(dev);
1679 pm_runtime_set_active(dev);
1680 pm_runtime_enable(dev);
1681
1682 return 0;
1683}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301684#endif
1685
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301686#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301687static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301688 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1689 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1690 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301691};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301692#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301693
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301694static struct platform_driver msm_otg_driver = {
1695 .remove = __devexit_p(msm_otg_remove),
1696 .driver = {
1697 .name = DRIVER_NAME,
1698 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301699#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301700 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301701#endif
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301702 },
1703};
1704
1705static int __init msm_otg_init(void)
1706{
1707 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
1708}
1709
1710static void __exit msm_otg_exit(void)
1711{
1712 platform_driver_unregister(&msm_otg_driver);
1713}
1714
1715module_init(msm_otg_init);
1716module_exit(msm_otg_exit);
1717
1718MODULE_LICENSE("GPL v2");
1719MODULE_DESCRIPTION("MSM USB transceiver driver");