1 /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved.
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.
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.
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
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>
28 #include <linux/ioport.h>
29 #include <linux/uaccess.h>
30 #include <linux/debugfs.h>
31 #include <linux/seq_file.h>
32 #include <linux/pm_runtime.h>
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>
41 #include <linux/regulator/consumer.h>
43 #define MSM_USB_BASE (motg->regs)
44 #define DRIVER_NAME "msm_otg"
46 #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
48 #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
49 #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
50 #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
51 #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
53 #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
54 #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
55 #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
56 #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
58 #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
59 #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
61 static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
66 ret = regulator_set_voltage(motg->vddcx,
67 USB_PHY_VDD_DIG_VOL_MIN,
68 USB_PHY_VDD_DIG_VOL_MAX);
70 dev_err(motg->phy.dev, "unable to set the voltage "
75 ret = regulator_enable(motg->vddcx);
77 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
79 ret = regulator_set_voltage(motg->vddcx, 0,
80 USB_PHY_VDD_DIG_VOL_MAX);
82 dev_err(motg->phy.dev, "unable to set the voltage "
84 ret = regulator_disable(motg->vddcx);
86 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
92 static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
97 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
100 dev_err(motg->phy.dev, "unable to set voltage level "
104 rc = regulator_enable(motg->v3p3);
106 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
109 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
110 USB_PHY_1P8_VOL_MAX);
112 dev_err(motg->phy.dev, "unable to set voltage level "
116 rc = regulator_enable(motg->v1p8);
118 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
125 regulator_disable(motg->v1p8);
127 regulator_disable(motg->v3p3);
132 static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
136 if (!motg->v1p8 || IS_ERR(motg->v1p8)) {
137 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
141 if (!motg->v3p3 || IS_ERR(motg->v3p3)) {
142 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
147 ret = regulator_set_optimum_mode(motg->v1p8,
148 USB_PHY_1P8_HPM_LOAD);
150 pr_err("%s: Unable to set HPM of the regulator "
151 "HSUSB_1p8\n", __func__);
154 ret = regulator_set_optimum_mode(motg->v3p3,
155 USB_PHY_3P3_HPM_LOAD);
157 pr_err("%s: Unable to set HPM of the regulator "
158 "HSUSB_3p3\n", __func__);
159 regulator_set_optimum_mode(motg->v1p8,
160 USB_PHY_1P8_LPM_LOAD);
164 ret = regulator_set_optimum_mode(motg->v1p8,
165 USB_PHY_1P8_LPM_LOAD);
167 pr_err("%s: Unable to set LPM of the regulator "
168 "HSUSB_1p8\n", __func__);
169 ret = regulator_set_optimum_mode(motg->v3p3,
170 USB_PHY_3P3_LPM_LOAD);
172 pr_err("%s: Unable to set LPM of the regulator "
173 "HSUSB_3p3\n", __func__);
176 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
177 return ret < 0 ? ret : 0;
180 static int ulpi_read(struct usb_phy *phy, u32 reg)
182 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
185 /* initiate read operation */
186 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
189 /* wait for completion */
190 while (cnt < ULPI_IO_TIMEOUT_USEC) {
191 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
197 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
198 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
199 readl(USB_ULPI_VIEWPORT));
202 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
205 static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
207 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
210 /* initiate write operation */
211 writel(ULPI_RUN | ULPI_WRITE |
212 ULPI_ADDR(reg) | ULPI_DATA(val),
215 /* wait for completion */
216 while (cnt < ULPI_IO_TIMEOUT_USEC) {
217 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
223 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
224 dev_err(phy->dev, "ulpi_write: timeout\n");
230 static struct usb_phy_io_ops msm_otg_io_ops = {
235 static void ulpi_init(struct msm_otg *motg)
237 struct msm_otg_platform_data *pdata = motg->pdata;
238 int *seq = pdata->phy_init_seq;
243 while (seq[0] >= 0) {
244 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
246 ulpi_write(&motg->phy, seq[0], seq[1]);
251 static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
255 if (!motg->pdata->link_clk_reset)
258 ret = motg->pdata->link_clk_reset(motg->clk, assert);
260 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
261 assert ? "assert" : "deassert");
266 static int msm_otg_phy_clk_reset(struct msm_otg *motg)
270 if (!motg->pdata->phy_clk_reset)
273 ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk);
275 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
280 static int msm_otg_phy_reset(struct msm_otg *motg)
286 ret = msm_otg_link_clk_reset(motg, 1);
289 ret = msm_otg_phy_clk_reset(motg);
292 ret = msm_otg_link_clk_reset(motg, 0);
296 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
297 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
299 for (retries = 3; retries > 0; retries--) {
300 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
301 ULPI_CLR(ULPI_FUNC_CTRL));
304 ret = msm_otg_phy_clk_reset(motg);
311 /* This reset calibrates the phy, if the above write succeeded */
312 ret = msm_otg_phy_clk_reset(motg);
316 for (retries = 3; retries > 0; retries--) {
317 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
318 if (ret != -ETIMEDOUT)
320 ret = msm_otg_phy_clk_reset(motg);
327 dev_info(motg->phy.dev, "phy_reset: success\n");
331 #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
332 static int msm_otg_reset(struct usb_phy *phy)
334 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
335 struct msm_otg_platform_data *pdata = motg->pdata;
341 ret = msm_otg_phy_reset(motg);
343 dev_err(phy->dev, "phy_reset failed\n");
349 writel(USBCMD_RESET, USB_USBCMD);
350 while (cnt < LINK_RESET_TIMEOUT_USEC) {
351 if (!(readl(USB_USBCMD) & USBCMD_RESET))
356 if (cnt >= LINK_RESET_TIMEOUT_USEC)
359 /* select ULPI phy */
360 writel(0x80000000, USB_PORTSC);
364 writel(0x0, USB_AHBBURST);
365 writel(0x00, USB_AHBMODE);
367 if (pdata->otg_control == OTG_PHY_CONTROL) {
368 val = readl(USB_OTGSC);
369 if (pdata->mode == USB_OTG) {
370 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
371 val |= OTGSC_IDIE | OTGSC_BSVIE;
372 } else if (pdata->mode == USB_PERIPHERAL) {
373 ulpi_val = ULPI_INT_SESS_VALID;
376 writel(val, USB_OTGSC);
377 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
378 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
384 #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
385 #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
389 #define USB_PHY_SUSP_DIG_VOL 500000
390 static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
392 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
397 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
399 min_vol = USB_PHY_SUSP_DIG_VOL;
401 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
403 pr_err("%s: unable to set the voltage for regulator "
404 "HSUSB_VDDCX\n", __func__);
408 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
413 static int msm_otg_suspend(struct msm_otg *motg)
415 struct usb_phy *phy = &motg->phy;
416 struct usb_bus *bus = phy->otg->host;
417 struct msm_otg_platform_data *pdata = motg->pdata;
420 if (atomic_read(&motg->in_lpm))
423 disable_irq(motg->irq);
425 * Chipidea 45-nm PHY suspend sequence:
427 * Interrupt Latch Register auto-clear feature is not present
428 * in all PHY versions. Latch register is clear on read type.
429 * Clear latch register to avoid spurious wakeup from
430 * low power mode (LPM).
432 * PHY comparators are disabled when PHY enters into low power
433 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
434 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
435 * PHY comparators. This save significant amount of power.
437 * PLL is not turned off when PHY enters into low power mode (LPM).
438 * Disable PLL for maximum power savings.
441 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
442 ulpi_read(phy, 0x14);
443 if (pdata->otg_control == OTG_PHY_CONTROL)
444 ulpi_write(phy, 0x01, 0x30);
445 ulpi_write(phy, 0x08, 0x09);
449 * PHY may take some time or even fail to enter into low power
450 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
453 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
454 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
455 if (readl(USB_PORTSC) & PORTSC_PHCD)
461 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
462 dev_err(phy->dev, "Unable to suspend PHY\n");
464 enable_irq(motg->irq);
469 * PHY has capability to generate interrupt asynchronously in low
470 * power mode (LPM). This interrupt is level triggered. So USB IRQ
471 * line must be disabled till async interrupt enable bit is cleared
472 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
473 * block data communication from PHY.
475 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
477 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
478 motg->pdata->otg_control == OTG_PMIC_CONTROL)
479 writel(readl(USB_PHY_CTRL) | PHY_RETEN, USB_PHY_CTRL);
481 clk_disable_unprepare(motg->pclk);
482 clk_disable_unprepare(motg->clk);
483 if (!IS_ERR(motg->core_clk))
484 clk_disable_unprepare(motg->core_clk);
486 if (!IS_ERR(motg->pclk_src))
487 clk_disable_unprepare(motg->pclk_src);
489 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
490 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
491 msm_hsusb_ldo_set_mode(motg, 0);
492 msm_hsusb_config_vddcx(motg, 0);
495 if (device_may_wakeup(phy->dev))
496 enable_irq_wake(motg->irq);
498 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
500 atomic_set(&motg->in_lpm, 1);
501 enable_irq(motg->irq);
503 dev_info(phy->dev, "USB in low power mode\n");
508 static int msm_otg_resume(struct msm_otg *motg)
510 struct usb_phy *phy = &motg->phy;
511 struct usb_bus *bus = phy->otg->host;
515 if (!atomic_read(&motg->in_lpm))
518 if (!IS_ERR(motg->pclk_src))
519 clk_prepare_enable(motg->pclk_src);
521 clk_prepare_enable(motg->pclk);
522 clk_prepare_enable(motg->clk);
523 if (!IS_ERR(motg->core_clk))
524 clk_prepare_enable(motg->core_clk);
526 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
527 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
528 msm_hsusb_ldo_set_mode(motg, 1);
529 msm_hsusb_config_vddcx(motg, 1);
530 writel(readl(USB_PHY_CTRL) & ~PHY_RETEN, USB_PHY_CTRL);
533 temp = readl(USB_USBCMD);
534 temp &= ~ASYNC_INTR_CTRL;
535 temp &= ~ULPI_STP_CTRL;
536 writel(temp, USB_USBCMD);
539 * PHY comes out of low power mode (LPM) in case of wakeup
540 * from asynchronous interrupt.
542 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
543 goto skip_phy_resume;
545 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
546 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
547 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
553 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
555 * This is a fatal error. Reset the link and
556 * PHY. USB state can not be restored. Re-insertion
557 * of USB cable is the only way to get USB working.
559 dev_err(phy->dev, "Unable to resume USB."
560 "Re-plugin the cable\n");
565 if (device_may_wakeup(phy->dev))
566 disable_irq_wake(motg->irq);
568 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
570 atomic_set(&motg->in_lpm, 0);
572 if (motg->async_int) {
574 pm_runtime_put(phy->dev);
575 enable_irq(motg->irq);
578 dev_info(phy->dev, "USB exited from low power mode\n");
584 static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
586 if (motg->cur_power == mA)
589 /* TODO: Notify PMIC about available current */
590 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
591 motg->cur_power = mA;
594 static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
596 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
599 * Gadget driver uses set_power method to notify about the
600 * available current based on suspend/configured states.
602 * IDEV_CHG can be drawn irrespective of suspend/un-configured
603 * states when CDP/ACA is connected.
605 if (motg->chg_type == USB_SDP_CHARGER)
606 msm_otg_notify_charger(motg, mA);
611 static void msm_otg_start_host(struct usb_phy *phy, int on)
613 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
614 struct msm_otg_platform_data *pdata = motg->pdata;
620 hcd = bus_to_hcd(phy->otg->host);
623 dev_dbg(phy->dev, "host on\n");
625 if (pdata->vbus_power)
626 pdata->vbus_power(1);
628 * Some boards have a switch cotrolled by gpio
629 * to enable/disable internal HUB. Enable internal
630 * HUB before kicking the host.
632 if (pdata->setup_gpio)
633 pdata->setup_gpio(OTG_STATE_A_HOST);
635 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
636 device_wakeup_enable(hcd->self.controller);
639 dev_dbg(phy->dev, "host off\n");
644 if (pdata->setup_gpio)
645 pdata->setup_gpio(OTG_STATE_UNDEFINED);
646 if (pdata->vbus_power)
647 pdata->vbus_power(0);
651 static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
653 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
657 * Fail host registration if this board can support
658 * only peripheral configuration.
660 if (motg->pdata->mode == USB_PERIPHERAL) {
661 dev_info(otg->phy->dev, "Host mode is not supported\n");
666 if (otg->phy->state == OTG_STATE_A_HOST) {
667 pm_runtime_get_sync(otg->phy->dev);
668 msm_otg_start_host(otg->phy, 0);
670 otg->phy->state = OTG_STATE_UNDEFINED;
671 schedule_work(&motg->sm_work);
679 hcd = bus_to_hcd(host);
680 hcd->power_budget = motg->pdata->power_budget;
683 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
686 * Kick the state machine work, if peripheral is not supported
687 * or peripheral is already registered with us.
689 if (motg->pdata->mode == USB_HOST || otg->gadget) {
690 pm_runtime_get_sync(otg->phy->dev);
691 schedule_work(&motg->sm_work);
697 static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
699 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
700 struct msm_otg_platform_data *pdata = motg->pdata;
702 if (!phy->otg->gadget)
706 dev_dbg(phy->dev, "gadget on\n");
708 * Some boards have a switch cotrolled by gpio
709 * to enable/disable internal HUB. Disable internal
710 * HUB before kicking the gadget.
712 if (pdata->setup_gpio)
713 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
714 usb_gadget_vbus_connect(phy->otg->gadget);
716 dev_dbg(phy->dev, "gadget off\n");
717 usb_gadget_vbus_disconnect(phy->otg->gadget);
718 if (pdata->setup_gpio)
719 pdata->setup_gpio(OTG_STATE_UNDEFINED);
724 static int msm_otg_set_peripheral(struct usb_otg *otg,
725 struct usb_gadget *gadget)
727 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
730 * Fail peripheral registration if this board can support
731 * only host configuration.
733 if (motg->pdata->mode == USB_HOST) {
734 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
739 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
740 pm_runtime_get_sync(otg->phy->dev);
741 msm_otg_start_peripheral(otg->phy, 0);
743 otg->phy->state = OTG_STATE_UNDEFINED;
744 schedule_work(&motg->sm_work);
751 otg->gadget = gadget;
752 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
755 * Kick the state machine work, if host is not supported
756 * or host is already registered with us.
758 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
759 pm_runtime_get_sync(otg->phy->dev);
760 schedule_work(&motg->sm_work);
766 static bool msm_chg_check_secondary_det(struct msm_otg *motg)
768 struct usb_phy *phy = &motg->phy;
772 switch (motg->pdata->phy_type) {
773 case CI_45NM_INTEGRATED_PHY:
774 chg_det = ulpi_read(phy, 0x34);
775 ret = chg_det & (1 << 4);
777 case SNPS_28NM_INTEGRATED_PHY:
778 chg_det = ulpi_read(phy, 0x87);
787 static void msm_chg_enable_secondary_det(struct msm_otg *motg)
789 struct usb_phy *phy = &motg->phy;
792 switch (motg->pdata->phy_type) {
793 case CI_45NM_INTEGRATED_PHY:
794 chg_det = ulpi_read(phy, 0x34);
795 /* Turn off charger block */
796 chg_det |= ~(1 << 1);
797 ulpi_write(phy, chg_det, 0x34);
799 /* control chg block via ULPI */
800 chg_det &= ~(1 << 3);
801 ulpi_write(phy, chg_det, 0x34);
802 /* put it in host mode for enabling D- source */
803 chg_det &= ~(1 << 2);
804 ulpi_write(phy, chg_det, 0x34);
805 /* Turn on chg detect block */
806 chg_det &= ~(1 << 1);
807 ulpi_write(phy, chg_det, 0x34);
809 /* enable chg detection */
810 chg_det &= ~(1 << 0);
811 ulpi_write(phy, chg_det, 0x34);
813 case SNPS_28NM_INTEGRATED_PHY:
815 * Configure DM as current source, DP as current sink
816 * and enable battery charging comparators.
818 ulpi_write(phy, 0x8, 0x85);
819 ulpi_write(phy, 0x2, 0x85);
820 ulpi_write(phy, 0x1, 0x85);
827 static bool msm_chg_check_primary_det(struct msm_otg *motg)
829 struct usb_phy *phy = &motg->phy;
833 switch (motg->pdata->phy_type) {
834 case CI_45NM_INTEGRATED_PHY:
835 chg_det = ulpi_read(phy, 0x34);
836 ret = chg_det & (1 << 4);
838 case SNPS_28NM_INTEGRATED_PHY:
839 chg_det = ulpi_read(phy, 0x87);
848 static void msm_chg_enable_primary_det(struct msm_otg *motg)
850 struct usb_phy *phy = &motg->phy;
853 switch (motg->pdata->phy_type) {
854 case CI_45NM_INTEGRATED_PHY:
855 chg_det = ulpi_read(phy, 0x34);
856 /* enable chg detection */
857 chg_det &= ~(1 << 0);
858 ulpi_write(phy, chg_det, 0x34);
860 case SNPS_28NM_INTEGRATED_PHY:
862 * Configure DP as current source, DM as current sink
863 * and enable battery charging comparators.
865 ulpi_write(phy, 0x2, 0x85);
866 ulpi_write(phy, 0x1, 0x85);
873 static bool msm_chg_check_dcd(struct msm_otg *motg)
875 struct usb_phy *phy = &motg->phy;
879 switch (motg->pdata->phy_type) {
880 case CI_45NM_INTEGRATED_PHY:
881 line_state = ulpi_read(phy, 0x15);
882 ret = !(line_state & 1);
884 case SNPS_28NM_INTEGRATED_PHY:
885 line_state = ulpi_read(phy, 0x87);
886 ret = line_state & 2;
894 static void msm_chg_disable_dcd(struct msm_otg *motg)
896 struct usb_phy *phy = &motg->phy;
899 switch (motg->pdata->phy_type) {
900 case CI_45NM_INTEGRATED_PHY:
901 chg_det = ulpi_read(phy, 0x34);
902 chg_det &= ~(1 << 5);
903 ulpi_write(phy, chg_det, 0x34);
905 case SNPS_28NM_INTEGRATED_PHY:
906 ulpi_write(phy, 0x10, 0x86);
913 static void msm_chg_enable_dcd(struct msm_otg *motg)
915 struct usb_phy *phy = &motg->phy;
918 switch (motg->pdata->phy_type) {
919 case CI_45NM_INTEGRATED_PHY:
920 chg_det = ulpi_read(phy, 0x34);
921 /* Turn on D+ current source */
923 ulpi_write(phy, chg_det, 0x34);
925 case SNPS_28NM_INTEGRATED_PHY:
926 /* Data contact detection enable */
927 ulpi_write(phy, 0x10, 0x85);
934 static void msm_chg_block_on(struct msm_otg *motg)
936 struct usb_phy *phy = &motg->phy;
937 u32 func_ctrl, chg_det;
939 /* put the controller in non-driving mode */
940 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
941 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
942 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
943 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
945 switch (motg->pdata->phy_type) {
946 case CI_45NM_INTEGRATED_PHY:
947 chg_det = ulpi_read(phy, 0x34);
948 /* control chg block via ULPI */
949 chg_det &= ~(1 << 3);
950 ulpi_write(phy, chg_det, 0x34);
951 /* Turn on chg detect block */
952 chg_det &= ~(1 << 1);
953 ulpi_write(phy, chg_det, 0x34);
956 case SNPS_28NM_INTEGRATED_PHY:
957 /* Clear charger detecting control bits */
958 ulpi_write(phy, 0x3F, 0x86);
959 /* Clear alt interrupt latch and enable bits */
960 ulpi_write(phy, 0x1F, 0x92);
961 ulpi_write(phy, 0x1F, 0x95);
969 static void msm_chg_block_off(struct msm_otg *motg)
971 struct usb_phy *phy = &motg->phy;
972 u32 func_ctrl, chg_det;
974 switch (motg->pdata->phy_type) {
975 case CI_45NM_INTEGRATED_PHY:
976 chg_det = ulpi_read(phy, 0x34);
977 /* Turn off charger block */
978 chg_det |= ~(1 << 1);
979 ulpi_write(phy, chg_det, 0x34);
981 case SNPS_28NM_INTEGRATED_PHY:
982 /* Clear charger detecting control bits */
983 ulpi_write(phy, 0x3F, 0x86);
984 /* Clear alt interrupt latch and enable bits */
985 ulpi_write(phy, 0x1F, 0x92);
986 ulpi_write(phy, 0x1F, 0x95);
992 /* put the controller in normal mode */
993 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
994 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
995 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
996 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
999 #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1000 #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1001 #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1002 #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1003 static void msm_chg_detect_work(struct work_struct *w)
1005 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1006 struct usb_phy *phy = &motg->phy;
1007 bool is_dcd, tmout, vout;
1008 unsigned long delay;
1010 dev_dbg(phy->dev, "chg detection work\n");
1011 switch (motg->chg_state) {
1012 case USB_CHG_STATE_UNDEFINED:
1013 pm_runtime_get_sync(phy->dev);
1014 msm_chg_block_on(motg);
1015 msm_chg_enable_dcd(motg);
1016 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1017 motg->dcd_retries = 0;
1018 delay = MSM_CHG_DCD_POLL_TIME;
1020 case USB_CHG_STATE_WAIT_FOR_DCD:
1021 is_dcd = msm_chg_check_dcd(motg);
1022 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1023 if (is_dcd || tmout) {
1024 msm_chg_disable_dcd(motg);
1025 msm_chg_enable_primary_det(motg);
1026 delay = MSM_CHG_PRIMARY_DET_TIME;
1027 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1029 delay = MSM_CHG_DCD_POLL_TIME;
1032 case USB_CHG_STATE_DCD_DONE:
1033 vout = msm_chg_check_primary_det(motg);
1035 msm_chg_enable_secondary_det(motg);
1036 delay = MSM_CHG_SECONDARY_DET_TIME;
1037 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1039 motg->chg_type = USB_SDP_CHARGER;
1040 motg->chg_state = USB_CHG_STATE_DETECTED;
1044 case USB_CHG_STATE_PRIMARY_DONE:
1045 vout = msm_chg_check_secondary_det(motg);
1047 motg->chg_type = USB_DCP_CHARGER;
1049 motg->chg_type = USB_CDP_CHARGER;
1050 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1052 case USB_CHG_STATE_SECONDARY_DONE:
1053 motg->chg_state = USB_CHG_STATE_DETECTED;
1054 case USB_CHG_STATE_DETECTED:
1055 msm_chg_block_off(motg);
1056 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1057 schedule_work(&motg->sm_work);
1063 schedule_delayed_work(&motg->chg_work, delay);
1067 * We support OTG, Peripheral only and Host only configurations. In case
1068 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1069 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1070 * enabled when switch is controlled by user and default mode is supplied
1071 * by board file, which can be changed by userspace later.
1073 static void msm_otg_init_sm(struct msm_otg *motg)
1075 struct msm_otg_platform_data *pdata = motg->pdata;
1076 u32 otgsc = readl(USB_OTGSC);
1078 switch (pdata->mode) {
1080 if (pdata->otg_control == OTG_PHY_CONTROL) {
1081 if (otgsc & OTGSC_ID)
1082 set_bit(ID, &motg->inputs);
1084 clear_bit(ID, &motg->inputs);
1086 if (otgsc & OTGSC_BSV)
1087 set_bit(B_SESS_VLD, &motg->inputs);
1089 clear_bit(B_SESS_VLD, &motg->inputs);
1090 } else if (pdata->otg_control == OTG_USER_CONTROL) {
1091 if (pdata->default_mode == USB_HOST) {
1092 clear_bit(ID, &motg->inputs);
1093 } else if (pdata->default_mode == USB_PERIPHERAL) {
1094 set_bit(ID, &motg->inputs);
1095 set_bit(B_SESS_VLD, &motg->inputs);
1097 set_bit(ID, &motg->inputs);
1098 clear_bit(B_SESS_VLD, &motg->inputs);
1103 clear_bit(ID, &motg->inputs);
1105 case USB_PERIPHERAL:
1106 set_bit(ID, &motg->inputs);
1107 if (otgsc & OTGSC_BSV)
1108 set_bit(B_SESS_VLD, &motg->inputs);
1110 clear_bit(B_SESS_VLD, &motg->inputs);
1117 static void msm_otg_sm_work(struct work_struct *w)
1119 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1120 struct usb_otg *otg = motg->phy.otg;
1122 switch (otg->phy->state) {
1123 case OTG_STATE_UNDEFINED:
1124 dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n");
1125 msm_otg_reset(otg->phy);
1126 msm_otg_init_sm(motg);
1127 otg->phy->state = OTG_STATE_B_IDLE;
1129 case OTG_STATE_B_IDLE:
1130 dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n");
1131 if (!test_bit(ID, &motg->inputs) && otg->host) {
1132 /* disable BSV bit */
1133 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1134 msm_otg_start_host(otg->phy, 1);
1135 otg->phy->state = OTG_STATE_A_HOST;
1136 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1137 switch (motg->chg_state) {
1138 case USB_CHG_STATE_UNDEFINED:
1139 msm_chg_detect_work(&motg->chg_work.work);
1141 case USB_CHG_STATE_DETECTED:
1142 switch (motg->chg_type) {
1143 case USB_DCP_CHARGER:
1144 msm_otg_notify_charger(motg,
1147 case USB_CDP_CHARGER:
1148 msm_otg_notify_charger(motg,
1150 msm_otg_start_peripheral(otg->phy, 1);
1152 = OTG_STATE_B_PERIPHERAL;
1154 case USB_SDP_CHARGER:
1155 msm_otg_notify_charger(motg, IUNIT);
1156 msm_otg_start_peripheral(otg->phy, 1);
1158 = OTG_STATE_B_PERIPHERAL;
1169 * If charger detection work is pending, decrement
1170 * the pm usage counter to balance with the one that
1171 * is incremented in charger detection work.
1173 if (cancel_delayed_work_sync(&motg->chg_work)) {
1174 pm_runtime_put_sync(otg->phy->dev);
1175 msm_otg_reset(otg->phy);
1177 msm_otg_notify_charger(motg, 0);
1178 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1179 motg->chg_type = USB_INVALID_CHARGER;
1181 pm_runtime_put_sync(otg->phy->dev);
1183 case OTG_STATE_B_PERIPHERAL:
1184 dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1185 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1186 !test_bit(ID, &motg->inputs)) {
1187 msm_otg_notify_charger(motg, 0);
1188 msm_otg_start_peripheral(otg->phy, 0);
1189 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1190 motg->chg_type = USB_INVALID_CHARGER;
1191 otg->phy->state = OTG_STATE_B_IDLE;
1192 msm_otg_reset(otg->phy);
1196 case OTG_STATE_A_HOST:
1197 dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n");
1198 if (test_bit(ID, &motg->inputs)) {
1199 msm_otg_start_host(otg->phy, 0);
1200 otg->phy->state = OTG_STATE_B_IDLE;
1201 msm_otg_reset(otg->phy);
1210 static irqreturn_t msm_otg_irq(int irq, void *data)
1212 struct msm_otg *motg = data;
1213 struct usb_phy *phy = &motg->phy;
1216 if (atomic_read(&motg->in_lpm)) {
1217 disable_irq_nosync(irq);
1218 motg->async_int = 1;
1219 pm_runtime_get(phy->dev);
1223 otgsc = readl(USB_OTGSC);
1224 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1227 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1228 if (otgsc & OTGSC_ID)
1229 set_bit(ID, &motg->inputs);
1231 clear_bit(ID, &motg->inputs);
1232 dev_dbg(phy->dev, "ID set/clear\n");
1233 pm_runtime_get_noresume(phy->dev);
1234 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1235 if (otgsc & OTGSC_BSV)
1236 set_bit(B_SESS_VLD, &motg->inputs);
1238 clear_bit(B_SESS_VLD, &motg->inputs);
1239 dev_dbg(phy->dev, "BSV set/clear\n");
1240 pm_runtime_get_noresume(phy->dev);
1243 writel(otgsc, USB_OTGSC);
1244 schedule_work(&motg->sm_work);
1248 static int msm_otg_mode_show(struct seq_file *s, void *unused)
1250 struct msm_otg *motg = s->private;
1251 struct usb_otg *otg = motg->phy.otg;
1253 switch (otg->phy->state) {
1254 case OTG_STATE_A_HOST:
1255 seq_printf(s, "host\n");
1257 case OTG_STATE_B_PERIPHERAL:
1258 seq_printf(s, "peripheral\n");
1261 seq_printf(s, "none\n");
1268 static int msm_otg_mode_open(struct inode *inode, struct file *file)
1270 return single_open(file, msm_otg_mode_show, inode->i_private);
1273 static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1274 size_t count, loff_t *ppos)
1276 struct seq_file *s = file->private_data;
1277 struct msm_otg *motg = s->private;
1279 struct usb_otg *otg = motg->phy.otg;
1281 enum usb_mode_type req_mode;
1283 memset(buf, 0x00, sizeof(buf));
1285 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1290 if (!strncmp(buf, "host", 4)) {
1291 req_mode = USB_HOST;
1292 } else if (!strncmp(buf, "peripheral", 10)) {
1293 req_mode = USB_PERIPHERAL;
1294 } else if (!strncmp(buf, "none", 4)) {
1295 req_mode = USB_NONE;
1303 switch (otg->phy->state) {
1304 case OTG_STATE_A_HOST:
1305 case OTG_STATE_B_PERIPHERAL:
1306 set_bit(ID, &motg->inputs);
1307 clear_bit(B_SESS_VLD, &motg->inputs);
1313 case USB_PERIPHERAL:
1314 switch (otg->phy->state) {
1315 case OTG_STATE_B_IDLE:
1316 case OTG_STATE_A_HOST:
1317 set_bit(ID, &motg->inputs);
1318 set_bit(B_SESS_VLD, &motg->inputs);
1325 switch (otg->phy->state) {
1326 case OTG_STATE_B_IDLE:
1327 case OTG_STATE_B_PERIPHERAL:
1328 clear_bit(ID, &motg->inputs);
1338 pm_runtime_get_sync(otg->phy->dev);
1339 schedule_work(&motg->sm_work);
1344 const struct file_operations msm_otg_mode_fops = {
1345 .open = msm_otg_mode_open,
1347 .write = msm_otg_mode_write,
1348 .llseek = seq_lseek,
1349 .release = single_release,
1352 static struct dentry *msm_otg_dbg_root;
1353 static struct dentry *msm_otg_dbg_mode;
1355 static int msm_otg_debugfs_init(struct msm_otg *motg)
1357 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1359 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1362 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1363 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1364 if (!msm_otg_dbg_mode) {
1365 debugfs_remove(msm_otg_dbg_root);
1366 msm_otg_dbg_root = NULL;
1373 static void msm_otg_debugfs_cleanup(void)
1375 debugfs_remove(msm_otg_dbg_mode);
1376 debugfs_remove(msm_otg_dbg_root);
1379 static int msm_otg_probe(struct platform_device *pdev)
1381 struct regulator_bulk_data regs[3];
1383 struct resource *res;
1384 struct msm_otg *motg;
1385 struct usb_phy *phy;
1387 dev_info(&pdev->dev, "msm_otg probe\n");
1388 if (!dev_get_platdata(&pdev->dev)) {
1389 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
1393 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
1395 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1399 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1401 if (!motg->phy.otg) {
1402 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
1406 motg->pdata = dev_get_platdata(&pdev->dev);
1408 phy->dev = &pdev->dev;
1410 motg->phy_reset_clk = devm_clk_get(&pdev->dev, "usb_phy_clk");
1411 if (IS_ERR(motg->phy_reset_clk)) {
1412 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
1413 return PTR_ERR(motg->phy_reset_clk);
1416 motg->clk = devm_clk_get(&pdev->dev, "usb_hs_clk");
1417 if (IS_ERR(motg->clk)) {
1418 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1419 return PTR_ERR(motg->clk);
1423 * If USB Core is running its protocol engine based on CORE CLK,
1424 * CORE CLK must be running at >55Mhz for correct HSUSB
1425 * operation and USB core cannot tolerate frequency changes on
1426 * CORE CLK. For such USB cores, vote for maximum clk frequency
1429 motg->pclk_src = ERR_PTR(-ENOENT);
1430 if (motg->pdata->pclk_src_name) {
1431 motg->pclk_src = devm_clk_get(&pdev->dev,
1432 motg->pdata->pclk_src_name);
1433 if (IS_ERR(motg->pclk_src))
1434 return PTR_ERR(motg->pclk_src);
1437 motg->pclk = devm_clk_get(&pdev->dev, "usb_hs_pclk");
1438 if (IS_ERR(motg->pclk)) {
1439 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1440 return PTR_ERR(motg->pclk);
1444 * USB core clock is not present on all MSM chips. This
1445 * clock is introduced to remove the dependency on AXI
1448 motg->core_clk = devm_clk_get(&pdev->dev, "usb_hs_core_clk");
1450 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1451 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1452 if (IS_ERR(motg->regs))
1453 return PTR_ERR(motg->regs);
1455 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1457 motg->irq = platform_get_irq(pdev, 0);
1459 dev_err(&pdev->dev, "platform_get_irq failed\n");
1463 regs[0].supply = "HSUSB_VDDCX";
1464 regs[1].supply = "HSUSB_3p3";
1465 regs[2].supply = "HSUSB_1p8";
1467 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1471 motg->vddcx = regs[0].consumer;
1472 motg->v3p3 = regs[1].consumer;
1473 motg->v1p8 = regs[2].consumer;
1475 clk_set_rate(motg->clk, 60000000);
1476 if (!IS_ERR(motg->pclk_src)) {
1477 clk_set_rate(motg->pclk_src, INT_MAX);
1478 clk_prepare_enable(motg->pclk_src);
1481 clk_prepare_enable(motg->clk);
1482 clk_prepare_enable(motg->pclk);
1484 if (!IS_ERR(motg->core_clk))
1485 clk_prepare_enable(motg->core_clk);
1487 ret = msm_hsusb_init_vddcx(motg, 1);
1489 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1493 ret = msm_hsusb_ldo_init(motg, 1);
1495 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1498 ret = msm_hsusb_ldo_set_mode(motg, 1);
1500 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1504 writel(0, USB_USBINTR);
1505 writel(0, USB_OTGSC);
1507 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1508 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1509 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
1512 dev_err(&pdev->dev, "request irq failed\n");
1516 phy->init = msm_otg_reset;
1517 phy->set_power = msm_otg_set_power;
1519 phy->io_ops = &msm_otg_io_ops;
1521 phy->otg->phy = &motg->phy;
1522 phy->otg->set_host = msm_otg_set_host;
1523 phy->otg->set_peripheral = msm_otg_set_peripheral;
1525 ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2);
1527 dev_err(&pdev->dev, "usb_add_phy failed\n");
1531 platform_set_drvdata(pdev, motg);
1532 device_init_wakeup(&pdev->dev, 1);
1534 if (motg->pdata->mode == USB_OTG &&
1535 motg->pdata->otg_control == OTG_USER_CONTROL) {
1536 ret = msm_otg_debugfs_init(motg);
1538 dev_dbg(&pdev->dev, "mode debugfs file is"
1542 pm_runtime_set_active(&pdev->dev);
1543 pm_runtime_enable(&pdev->dev);
1548 msm_hsusb_ldo_init(motg, 0);
1550 msm_hsusb_init_vddcx(motg, 0);
1552 clk_disable_unprepare(motg->pclk);
1553 clk_disable_unprepare(motg->clk);
1554 if (!IS_ERR(motg->core_clk))
1555 clk_disable_unprepare(motg->core_clk);
1556 if (!IS_ERR(motg->pclk_src))
1557 clk_disable_unprepare(motg->pclk_src);
1561 static int msm_otg_remove(struct platform_device *pdev)
1563 struct msm_otg *motg = platform_get_drvdata(pdev);
1564 struct usb_phy *phy = &motg->phy;
1567 if (phy->otg->host || phy->otg->gadget)
1570 msm_otg_debugfs_cleanup();
1571 cancel_delayed_work_sync(&motg->chg_work);
1572 cancel_work_sync(&motg->sm_work);
1574 pm_runtime_resume(&pdev->dev);
1576 device_init_wakeup(&pdev->dev, 0);
1577 pm_runtime_disable(&pdev->dev);
1579 usb_remove_phy(phy);
1580 disable_irq(motg->irq);
1583 * Put PHY in low power mode.
1585 ulpi_read(phy, 0x14);
1586 ulpi_write(phy, 0x08, 0x09);
1588 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1589 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1590 if (readl(USB_PORTSC) & PORTSC_PHCD)
1595 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1596 dev_err(phy->dev, "Unable to suspend PHY\n");
1598 clk_disable_unprepare(motg->pclk);
1599 clk_disable_unprepare(motg->clk);
1600 if (!IS_ERR(motg->core_clk))
1601 clk_disable_unprepare(motg->core_clk);
1602 if (!IS_ERR(motg->pclk_src))
1603 clk_disable_unprepare(motg->pclk_src);
1605 msm_hsusb_ldo_init(motg, 0);
1607 pm_runtime_set_suspended(&pdev->dev);
1612 #ifdef CONFIG_PM_RUNTIME
1613 static int msm_otg_runtime_idle(struct device *dev)
1615 struct msm_otg *motg = dev_get_drvdata(dev);
1616 struct usb_otg *otg = motg->phy.otg;
1618 dev_dbg(dev, "OTG runtime idle\n");
1621 * It is observed some times that a spurious interrupt
1622 * comes when PHY is put into LPM immediately after PHY reset.
1623 * This 1 sec delay also prevents entering into LPM immediately
1624 * after asynchronous interrupt.
1626 if (otg->phy->state != OTG_STATE_UNDEFINED)
1627 pm_schedule_suspend(dev, 1000);
1632 static int msm_otg_runtime_suspend(struct device *dev)
1634 struct msm_otg *motg = dev_get_drvdata(dev);
1636 dev_dbg(dev, "OTG runtime suspend\n");
1637 return msm_otg_suspend(motg);
1640 static int msm_otg_runtime_resume(struct device *dev)
1642 struct msm_otg *motg = dev_get_drvdata(dev);
1644 dev_dbg(dev, "OTG runtime resume\n");
1645 return msm_otg_resume(motg);
1649 #ifdef CONFIG_PM_SLEEP
1650 static int msm_otg_pm_suspend(struct device *dev)
1652 struct msm_otg *motg = dev_get_drvdata(dev);
1654 dev_dbg(dev, "OTG PM suspend\n");
1655 return msm_otg_suspend(motg);
1658 static int msm_otg_pm_resume(struct device *dev)
1660 struct msm_otg *motg = dev_get_drvdata(dev);
1663 dev_dbg(dev, "OTG PM resume\n");
1665 ret = msm_otg_resume(motg);
1670 * Runtime PM Documentation recommends bringing the
1671 * device to full powered state upon resume.
1673 pm_runtime_disable(dev);
1674 pm_runtime_set_active(dev);
1675 pm_runtime_enable(dev);
1681 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1682 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1683 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1684 msm_otg_runtime_idle)
1687 static struct platform_driver msm_otg_driver = {
1688 .probe = msm_otg_probe,
1689 .remove = msm_otg_remove,
1691 .name = DRIVER_NAME,
1692 .owner = THIS_MODULE,
1693 .pm = &msm_otg_dev_pm_ops,
1697 module_platform_driver(msm_otg_driver);
1699 MODULE_LICENSE("GPL v2");
1700 MODULE_DESCRIPTION("MSM USB transceiver driver");