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/of_device.h>
35 #include <linux/reset.h>
37 #include <linux/usb.h>
38 #include <linux/usb/otg.h>
39 #include <linux/usb/of.h>
40 #include <linux/usb/ulpi.h>
41 #include <linux/usb/gadget.h>
42 #include <linux/usb/hcd.h>
43 #include <linux/usb/msm_hsusb.h>
44 #include <linux/usb/msm_hsusb_hw.h>
45 #include <linux/regulator/consumer.h>
47 #define MSM_USB_BASE (motg->regs)
48 #define DRIVER_NAME "msm_otg"
50 #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
51 #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
53 #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
54 #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
55 #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
56 #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
58 #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
59 #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
60 #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
61 #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
63 #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
64 #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
65 #define USB_PHY_SUSP_DIG_VOL 500000 /* uV */
73 static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
78 ret = regulator_set_voltage(motg->vddcx,
79 motg->vdd_levels[VDD_LEVEL_MIN],
80 motg->vdd_levels[VDD_LEVEL_MAX]);
82 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
86 ret = regulator_enable(motg->vddcx);
88 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
90 ret = regulator_set_voltage(motg->vddcx, 0,
91 motg->vdd_levels[VDD_LEVEL_MAX]);
93 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
94 ret = regulator_disable(motg->vddcx);
96 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
102 static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
107 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
108 USB_PHY_3P3_VOL_MAX);
110 dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
113 rc = regulator_enable(motg->v3p3);
115 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
118 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
119 USB_PHY_1P8_VOL_MAX);
121 dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
124 rc = regulator_enable(motg->v1p8);
126 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
133 regulator_disable(motg->v1p8);
135 regulator_disable(motg->v3p3);
140 static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
145 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
147 pr_err("Could not set HPM for v1p8\n");
150 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
152 pr_err("Could not set HPM for v3p3\n");
153 regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
157 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
159 pr_err("Could not set LPM for v1p8\n");
160 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
162 pr_err("Could not set LPM for v3p3\n");
165 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
166 return ret < 0 ? ret : 0;
169 static int ulpi_read(struct usb_phy *phy, u32 reg)
171 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
174 /* initiate read operation */
175 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
178 /* wait for completion */
179 while (cnt < ULPI_IO_TIMEOUT_USEC) {
180 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
186 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
187 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
188 readl(USB_ULPI_VIEWPORT));
191 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
194 static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
196 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
199 /* initiate write operation */
200 writel(ULPI_RUN | ULPI_WRITE |
201 ULPI_ADDR(reg) | ULPI_DATA(val),
204 /* wait for completion */
205 while (cnt < ULPI_IO_TIMEOUT_USEC) {
206 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
212 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
213 dev_err(phy->dev, "ulpi_write: timeout\n");
219 static struct usb_phy_io_ops msm_otg_io_ops = {
224 static void ulpi_init(struct msm_otg *motg)
226 struct msm_otg_platform_data *pdata = motg->pdata;
227 int *seq = pdata->phy_init_seq, idx;
228 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
230 for (idx = 0; idx < pdata->phy_init_sz; idx++) {
234 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
235 seq[idx], addr + idx);
236 ulpi_write(&motg->phy, seq[idx], addr + idx);
240 static int msm_phy_notify_disconnect(struct usb_phy *phy,
241 enum usb_device_speed speed)
246 * Put the transceiver in non-driving mode. Otherwise host
247 * may not detect soft-disconnection.
249 val = ulpi_read(phy, ULPI_FUNC_CTRL);
250 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
251 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
252 ulpi_write(phy, val, ULPI_FUNC_CTRL);
257 static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
262 ret = reset_control_assert(motg->link_rst);
264 ret = reset_control_deassert(motg->link_rst);
267 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
268 assert ? "assert" : "deassert");
273 static int msm_otg_phy_clk_reset(struct msm_otg *motg)
278 ret = reset_control_reset(motg->phy_rst);
281 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
286 static int msm_link_reset(struct msm_otg *motg)
291 ret = msm_otg_link_clk_reset(motg, 1);
295 /* wait for 1ms delay as suggested in HPG. */
296 usleep_range(1000, 1200);
298 ret = msm_otg_link_clk_reset(motg, 0);
302 if (motg->phy_number)
303 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
305 /* put transceiver in serial mode as part of reset */
306 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
307 writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
312 static int msm_otg_reset(struct usb_phy *phy)
314 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
317 writel(USBCMD_RESET, USB_USBCMD);
318 while (cnt < LINK_RESET_TIMEOUT_USEC) {
319 if (!(readl(USB_USBCMD) & USBCMD_RESET))
324 if (cnt >= LINK_RESET_TIMEOUT_USEC)
327 /* select ULPI phy and clear other status/control bits in PORTSC */
328 writel(PORTSC_PTS_ULPI, USB_PORTSC);
330 writel(0x0, USB_AHBBURST);
331 writel(0x08, USB_AHBMODE);
333 if (motg->phy_number)
334 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
338 static void msm_phy_reset(struct msm_otg *motg)
342 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
343 msm_otg_phy_clk_reset(motg);
348 if (motg->phy_number)
349 addr = USB_PHY_CTRL2;
351 /* Assert USB PHY_POR */
352 writel(readl(addr) | PHY_POR_ASSERT, addr);
355 * wait for minimum 10 microseconds as suggested in HPG.
356 * Use a slightly larger value since the exact value didn't
357 * work 100% of the time.
361 /* Deassert USB PHY_POR */
362 writel(readl(addr) & ~PHY_POR_ASSERT, addr);
365 static int msm_usb_reset(struct usb_phy *phy)
367 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
370 if (!IS_ERR(motg->core_clk))
371 clk_prepare_enable(motg->core_clk);
373 ret = msm_link_reset(motg);
375 dev_err(phy->dev, "phy_reset failed\n");
379 ret = msm_otg_reset(&motg->phy);
381 dev_err(phy->dev, "link reset failed\n");
387 /* Reset USB PHY after performing USB Link RESET */
390 if (!IS_ERR(motg->core_clk))
391 clk_disable_unprepare(motg->core_clk);
396 static int msm_phy_init(struct usb_phy *phy)
398 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
399 struct msm_otg_platform_data *pdata = motg->pdata;
400 u32 val, ulpi_val = 0;
402 /* Program USB PHY Override registers. */
406 * It is recommended in HPG to reset USB PHY after programming
407 * USB PHY Override registers.
411 if (pdata->otg_control == OTG_PHY_CONTROL) {
412 val = readl(USB_OTGSC);
413 if (pdata->mode == USB_DR_MODE_OTG) {
414 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
415 val |= OTGSC_IDIE | OTGSC_BSVIE;
416 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
417 ulpi_val = ULPI_INT_SESS_VALID;
420 writel(val, USB_OTGSC);
421 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
422 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
425 if (motg->phy_number)
426 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
431 #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
432 #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
436 static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
438 int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
443 min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
445 min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
447 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
449 pr_err("Cannot set vddcx voltage\n");
453 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
458 static int msm_otg_suspend(struct msm_otg *motg)
460 struct usb_phy *phy = &motg->phy;
461 struct usb_bus *bus = phy->otg->host;
462 struct msm_otg_platform_data *pdata = motg->pdata;
466 if (atomic_read(&motg->in_lpm))
469 disable_irq(motg->irq);
471 * Chipidea 45-nm PHY suspend sequence:
473 * Interrupt Latch Register auto-clear feature is not present
474 * in all PHY versions. Latch register is clear on read type.
475 * Clear latch register to avoid spurious wakeup from
476 * low power mode (LPM).
478 * PHY comparators are disabled when PHY enters into low power
479 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
480 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
481 * PHY comparators. This save significant amount of power.
483 * PLL is not turned off when PHY enters into low power mode (LPM).
484 * Disable PLL for maximum power savings.
487 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
488 ulpi_read(phy, 0x14);
489 if (pdata->otg_control == OTG_PHY_CONTROL)
490 ulpi_write(phy, 0x01, 0x30);
491 ulpi_write(phy, 0x08, 0x09);
495 * PHY may take some time or even fail to enter into low power
496 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
499 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
500 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
501 if (readl(USB_PORTSC) & PORTSC_PHCD)
507 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
508 dev_err(phy->dev, "Unable to suspend PHY\n");
510 enable_irq(motg->irq);
515 * PHY has capability to generate interrupt asynchronously in low
516 * power mode (LPM). This interrupt is level triggered. So USB IRQ
517 * line must be disabled till async interrupt enable bit is cleared
518 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
519 * block data communication from PHY.
521 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
524 if (motg->phy_number)
525 addr = USB_PHY_CTRL2;
527 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
528 motg->pdata->otg_control == OTG_PMIC_CONTROL)
529 writel(readl(addr) | PHY_RETEN, addr);
531 clk_disable_unprepare(motg->pclk);
532 clk_disable_unprepare(motg->clk);
533 if (!IS_ERR(motg->core_clk))
534 clk_disable_unprepare(motg->core_clk);
536 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
537 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
538 msm_hsusb_ldo_set_mode(motg, 0);
539 msm_hsusb_config_vddcx(motg, 0);
542 if (device_may_wakeup(phy->dev))
543 enable_irq_wake(motg->irq);
545 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
547 atomic_set(&motg->in_lpm, 1);
548 enable_irq(motg->irq);
550 dev_info(phy->dev, "USB in low power mode\n");
555 static int msm_otg_resume(struct msm_otg *motg)
557 struct usb_phy *phy = &motg->phy;
558 struct usb_bus *bus = phy->otg->host;
563 if (!atomic_read(&motg->in_lpm))
566 clk_prepare_enable(motg->pclk);
567 clk_prepare_enable(motg->clk);
568 if (!IS_ERR(motg->core_clk))
569 clk_prepare_enable(motg->core_clk);
571 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
572 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
575 if (motg->phy_number)
576 addr = USB_PHY_CTRL2;
578 msm_hsusb_ldo_set_mode(motg, 1);
579 msm_hsusb_config_vddcx(motg, 1);
580 writel(readl(addr) & ~PHY_RETEN, addr);
583 temp = readl(USB_USBCMD);
584 temp &= ~ASYNC_INTR_CTRL;
585 temp &= ~ULPI_STP_CTRL;
586 writel(temp, USB_USBCMD);
589 * PHY comes out of low power mode (LPM) in case of wakeup
590 * from asynchronous interrupt.
592 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
593 goto skip_phy_resume;
595 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
596 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
597 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
603 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
605 * This is a fatal error. Reset the link and
606 * PHY. USB state can not be restored. Re-insertion
607 * of USB cable is the only way to get USB working.
609 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
614 if (device_may_wakeup(phy->dev))
615 disable_irq_wake(motg->irq);
617 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
619 atomic_set(&motg->in_lpm, 0);
621 if (motg->async_int) {
623 pm_runtime_put(phy->dev);
624 enable_irq(motg->irq);
627 dev_info(phy->dev, "USB exited from low power mode\n");
633 static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
635 if (motg->cur_power == mA)
638 /* TODO: Notify PMIC about available current */
639 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
640 motg->cur_power = mA;
643 static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
645 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
648 * Gadget driver uses set_power method to notify about the
649 * available current based on suspend/configured states.
651 * IDEV_CHG can be drawn irrespective of suspend/un-configured
652 * states when CDP/ACA is connected.
654 if (motg->chg_type == USB_SDP_CHARGER)
655 msm_otg_notify_charger(motg, mA);
660 static void msm_otg_start_host(struct usb_phy *phy, int on)
662 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
663 struct msm_otg_platform_data *pdata = motg->pdata;
669 hcd = bus_to_hcd(phy->otg->host);
672 dev_dbg(phy->dev, "host on\n");
674 if (pdata->vbus_power)
675 pdata->vbus_power(1);
677 * Some boards have a switch cotrolled by gpio
678 * to enable/disable internal HUB. Enable internal
679 * HUB before kicking the host.
681 if (pdata->setup_gpio)
682 pdata->setup_gpio(OTG_STATE_A_HOST);
684 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
685 device_wakeup_enable(hcd->self.controller);
688 dev_dbg(phy->dev, "host off\n");
693 if (pdata->setup_gpio)
694 pdata->setup_gpio(OTG_STATE_UNDEFINED);
695 if (pdata->vbus_power)
696 pdata->vbus_power(0);
700 static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
702 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
706 * Fail host registration if this board can support
707 * only peripheral configuration.
709 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
710 dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
715 if (otg->state == OTG_STATE_A_HOST) {
716 pm_runtime_get_sync(otg->usb_phy->dev);
717 msm_otg_start_host(otg->usb_phy, 0);
719 otg->state = OTG_STATE_UNDEFINED;
720 schedule_work(&motg->sm_work);
728 hcd = bus_to_hcd(host);
729 hcd->power_budget = motg->pdata->power_budget;
732 dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
735 * Kick the state machine work, if peripheral is not supported
736 * or peripheral is already registered with us.
738 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
739 pm_runtime_get_sync(otg->usb_phy->dev);
740 schedule_work(&motg->sm_work);
746 static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
748 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
749 struct msm_otg_platform_data *pdata = motg->pdata;
751 if (!phy->otg->gadget)
755 dev_dbg(phy->dev, "gadget on\n");
757 * Some boards have a switch cotrolled by gpio
758 * to enable/disable internal HUB. Disable internal
759 * HUB before kicking the gadget.
761 if (pdata->setup_gpio)
762 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
763 usb_gadget_vbus_connect(phy->otg->gadget);
765 dev_dbg(phy->dev, "gadget off\n");
766 usb_gadget_vbus_disconnect(phy->otg->gadget);
767 if (pdata->setup_gpio)
768 pdata->setup_gpio(OTG_STATE_UNDEFINED);
773 static int msm_otg_set_peripheral(struct usb_otg *otg,
774 struct usb_gadget *gadget)
776 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
779 * Fail peripheral registration if this board can support
780 * only host configuration.
782 if (motg->pdata->mode == USB_DR_MODE_HOST) {
783 dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
788 if (otg->state == OTG_STATE_B_PERIPHERAL) {
789 pm_runtime_get_sync(otg->usb_phy->dev);
790 msm_otg_start_peripheral(otg->usb_phy, 0);
792 otg->state = OTG_STATE_UNDEFINED;
793 schedule_work(&motg->sm_work);
800 otg->gadget = gadget;
801 dev_dbg(otg->usb_phy->dev,
802 "peripheral driver registered w/ tranceiver\n");
805 * Kick the state machine work, if host is not supported
806 * or host is already registered with us.
808 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
809 pm_runtime_get_sync(otg->usb_phy->dev);
810 schedule_work(&motg->sm_work);
816 static bool msm_chg_check_secondary_det(struct msm_otg *motg)
818 struct usb_phy *phy = &motg->phy;
822 switch (motg->pdata->phy_type) {
823 case CI_45NM_INTEGRATED_PHY:
824 chg_det = ulpi_read(phy, 0x34);
825 ret = chg_det & (1 << 4);
827 case SNPS_28NM_INTEGRATED_PHY:
828 chg_det = ulpi_read(phy, 0x87);
837 static void msm_chg_enable_secondary_det(struct msm_otg *motg)
839 struct usb_phy *phy = &motg->phy;
842 switch (motg->pdata->phy_type) {
843 case CI_45NM_INTEGRATED_PHY:
844 chg_det = ulpi_read(phy, 0x34);
845 /* Turn off charger block */
846 chg_det |= ~(1 << 1);
847 ulpi_write(phy, chg_det, 0x34);
849 /* control chg block via ULPI */
850 chg_det &= ~(1 << 3);
851 ulpi_write(phy, chg_det, 0x34);
852 /* put it in host mode for enabling D- source */
853 chg_det &= ~(1 << 2);
854 ulpi_write(phy, chg_det, 0x34);
855 /* Turn on chg detect block */
856 chg_det &= ~(1 << 1);
857 ulpi_write(phy, chg_det, 0x34);
859 /* enable chg detection */
860 chg_det &= ~(1 << 0);
861 ulpi_write(phy, chg_det, 0x34);
863 case SNPS_28NM_INTEGRATED_PHY:
865 * Configure DM as current source, DP as current sink
866 * and enable battery charging comparators.
868 ulpi_write(phy, 0x8, 0x85);
869 ulpi_write(phy, 0x2, 0x85);
870 ulpi_write(phy, 0x1, 0x85);
877 static bool msm_chg_check_primary_det(struct msm_otg *motg)
879 struct usb_phy *phy = &motg->phy;
883 switch (motg->pdata->phy_type) {
884 case CI_45NM_INTEGRATED_PHY:
885 chg_det = ulpi_read(phy, 0x34);
886 ret = chg_det & (1 << 4);
888 case SNPS_28NM_INTEGRATED_PHY:
889 chg_det = ulpi_read(phy, 0x87);
898 static void msm_chg_enable_primary_det(struct msm_otg *motg)
900 struct usb_phy *phy = &motg->phy;
903 switch (motg->pdata->phy_type) {
904 case CI_45NM_INTEGRATED_PHY:
905 chg_det = ulpi_read(phy, 0x34);
906 /* enable chg detection */
907 chg_det &= ~(1 << 0);
908 ulpi_write(phy, chg_det, 0x34);
910 case SNPS_28NM_INTEGRATED_PHY:
912 * Configure DP as current source, DM as current sink
913 * and enable battery charging comparators.
915 ulpi_write(phy, 0x2, 0x85);
916 ulpi_write(phy, 0x1, 0x85);
923 static bool msm_chg_check_dcd(struct msm_otg *motg)
925 struct usb_phy *phy = &motg->phy;
929 switch (motg->pdata->phy_type) {
930 case CI_45NM_INTEGRATED_PHY:
931 line_state = ulpi_read(phy, 0x15);
932 ret = !(line_state & 1);
934 case SNPS_28NM_INTEGRATED_PHY:
935 line_state = ulpi_read(phy, 0x87);
936 ret = line_state & 2;
944 static void msm_chg_disable_dcd(struct msm_otg *motg)
946 struct usb_phy *phy = &motg->phy;
949 switch (motg->pdata->phy_type) {
950 case CI_45NM_INTEGRATED_PHY:
951 chg_det = ulpi_read(phy, 0x34);
952 chg_det &= ~(1 << 5);
953 ulpi_write(phy, chg_det, 0x34);
955 case SNPS_28NM_INTEGRATED_PHY:
956 ulpi_write(phy, 0x10, 0x86);
963 static void msm_chg_enable_dcd(struct msm_otg *motg)
965 struct usb_phy *phy = &motg->phy;
968 switch (motg->pdata->phy_type) {
969 case CI_45NM_INTEGRATED_PHY:
970 chg_det = ulpi_read(phy, 0x34);
971 /* Turn on D+ current source */
973 ulpi_write(phy, chg_det, 0x34);
975 case SNPS_28NM_INTEGRATED_PHY:
976 /* Data contact detection enable */
977 ulpi_write(phy, 0x10, 0x85);
984 static void msm_chg_block_on(struct msm_otg *motg)
986 struct usb_phy *phy = &motg->phy;
987 u32 func_ctrl, chg_det;
989 /* put the controller in non-driving mode */
990 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
991 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
992 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
993 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
995 switch (motg->pdata->phy_type) {
996 case CI_45NM_INTEGRATED_PHY:
997 chg_det = ulpi_read(phy, 0x34);
998 /* control chg block via ULPI */
999 chg_det &= ~(1 << 3);
1000 ulpi_write(phy, chg_det, 0x34);
1001 /* Turn on chg detect block */
1002 chg_det &= ~(1 << 1);
1003 ulpi_write(phy, chg_det, 0x34);
1006 case SNPS_28NM_INTEGRATED_PHY:
1007 /* Clear charger detecting control bits */
1008 ulpi_write(phy, 0x3F, 0x86);
1009 /* Clear alt interrupt latch and enable bits */
1010 ulpi_write(phy, 0x1F, 0x92);
1011 ulpi_write(phy, 0x1F, 0x95);
1019 static void msm_chg_block_off(struct msm_otg *motg)
1021 struct usb_phy *phy = &motg->phy;
1022 u32 func_ctrl, chg_det;
1024 switch (motg->pdata->phy_type) {
1025 case CI_45NM_INTEGRATED_PHY:
1026 chg_det = ulpi_read(phy, 0x34);
1027 /* Turn off charger block */
1028 chg_det |= ~(1 << 1);
1029 ulpi_write(phy, chg_det, 0x34);
1031 case SNPS_28NM_INTEGRATED_PHY:
1032 /* Clear charger detecting control bits */
1033 ulpi_write(phy, 0x3F, 0x86);
1034 /* Clear alt interrupt latch and enable bits */
1035 ulpi_write(phy, 0x1F, 0x92);
1036 ulpi_write(phy, 0x1F, 0x95);
1042 /* put the controller in normal mode */
1043 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1044 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1045 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1046 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1049 #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1050 #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1051 #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1052 #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1053 static void msm_chg_detect_work(struct work_struct *w)
1055 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1056 struct usb_phy *phy = &motg->phy;
1057 bool is_dcd, tmout, vout;
1058 unsigned long delay;
1060 dev_dbg(phy->dev, "chg detection work\n");
1061 switch (motg->chg_state) {
1062 case USB_CHG_STATE_UNDEFINED:
1063 pm_runtime_get_sync(phy->dev);
1064 msm_chg_block_on(motg);
1065 msm_chg_enable_dcd(motg);
1066 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1067 motg->dcd_retries = 0;
1068 delay = MSM_CHG_DCD_POLL_TIME;
1070 case USB_CHG_STATE_WAIT_FOR_DCD:
1071 is_dcd = msm_chg_check_dcd(motg);
1072 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1073 if (is_dcd || tmout) {
1074 msm_chg_disable_dcd(motg);
1075 msm_chg_enable_primary_det(motg);
1076 delay = MSM_CHG_PRIMARY_DET_TIME;
1077 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1079 delay = MSM_CHG_DCD_POLL_TIME;
1082 case USB_CHG_STATE_DCD_DONE:
1083 vout = msm_chg_check_primary_det(motg);
1085 msm_chg_enable_secondary_det(motg);
1086 delay = MSM_CHG_SECONDARY_DET_TIME;
1087 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1089 motg->chg_type = USB_SDP_CHARGER;
1090 motg->chg_state = USB_CHG_STATE_DETECTED;
1094 case USB_CHG_STATE_PRIMARY_DONE:
1095 vout = msm_chg_check_secondary_det(motg);
1097 motg->chg_type = USB_DCP_CHARGER;
1099 motg->chg_type = USB_CDP_CHARGER;
1100 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1102 case USB_CHG_STATE_SECONDARY_DONE:
1103 motg->chg_state = USB_CHG_STATE_DETECTED;
1104 case USB_CHG_STATE_DETECTED:
1105 msm_chg_block_off(motg);
1106 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1107 schedule_work(&motg->sm_work);
1113 schedule_delayed_work(&motg->chg_work, delay);
1117 * We support OTG, Peripheral only and Host only configurations. In case
1118 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1119 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1120 * enabled when switch is controlled by user and default mode is supplied
1121 * by board file, which can be changed by userspace later.
1123 static void msm_otg_init_sm(struct msm_otg *motg)
1125 struct msm_otg_platform_data *pdata = motg->pdata;
1126 u32 otgsc = readl(USB_OTGSC);
1128 switch (pdata->mode) {
1129 case USB_DR_MODE_OTG:
1130 if (pdata->otg_control == OTG_PHY_CONTROL) {
1131 if (otgsc & OTGSC_ID)
1132 set_bit(ID, &motg->inputs);
1134 clear_bit(ID, &motg->inputs);
1136 if (otgsc & OTGSC_BSV)
1137 set_bit(B_SESS_VLD, &motg->inputs);
1139 clear_bit(B_SESS_VLD, &motg->inputs);
1140 } else if (pdata->otg_control == OTG_USER_CONTROL) {
1141 set_bit(ID, &motg->inputs);
1142 clear_bit(B_SESS_VLD, &motg->inputs);
1145 case USB_DR_MODE_HOST:
1146 clear_bit(ID, &motg->inputs);
1148 case USB_DR_MODE_PERIPHERAL:
1149 set_bit(ID, &motg->inputs);
1150 if (otgsc & OTGSC_BSV)
1151 set_bit(B_SESS_VLD, &motg->inputs);
1153 clear_bit(B_SESS_VLD, &motg->inputs);
1160 static void msm_otg_sm_work(struct work_struct *w)
1162 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1163 struct usb_otg *otg = motg->phy.otg;
1165 switch (otg->state) {
1166 case OTG_STATE_UNDEFINED:
1167 dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
1168 msm_otg_reset(otg->usb_phy);
1169 msm_otg_init_sm(motg);
1170 otg->state = OTG_STATE_B_IDLE;
1172 case OTG_STATE_B_IDLE:
1173 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
1174 if (!test_bit(ID, &motg->inputs) && otg->host) {
1175 /* disable BSV bit */
1176 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1177 msm_otg_start_host(otg->usb_phy, 1);
1178 otg->state = OTG_STATE_A_HOST;
1179 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1180 switch (motg->chg_state) {
1181 case USB_CHG_STATE_UNDEFINED:
1182 msm_chg_detect_work(&motg->chg_work.work);
1184 case USB_CHG_STATE_DETECTED:
1185 switch (motg->chg_type) {
1186 case USB_DCP_CHARGER:
1187 msm_otg_notify_charger(motg,
1190 case USB_CDP_CHARGER:
1191 msm_otg_notify_charger(motg,
1193 msm_otg_start_peripheral(otg->usb_phy,
1196 = OTG_STATE_B_PERIPHERAL;
1198 case USB_SDP_CHARGER:
1199 msm_otg_notify_charger(motg, IUNIT);
1200 msm_otg_start_peripheral(otg->usb_phy,
1203 = OTG_STATE_B_PERIPHERAL;
1214 * If charger detection work is pending, decrement
1215 * the pm usage counter to balance with the one that
1216 * is incremented in charger detection work.
1218 if (cancel_delayed_work_sync(&motg->chg_work)) {
1219 pm_runtime_put_sync(otg->usb_phy->dev);
1220 msm_otg_reset(otg->usb_phy);
1222 msm_otg_notify_charger(motg, 0);
1223 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1224 motg->chg_type = USB_INVALID_CHARGER;
1227 if (otg->state == OTG_STATE_B_IDLE)
1228 pm_runtime_put_sync(otg->usb_phy->dev);
1230 case OTG_STATE_B_PERIPHERAL:
1231 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1232 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1233 !test_bit(ID, &motg->inputs)) {
1234 msm_otg_notify_charger(motg, 0);
1235 msm_otg_start_peripheral(otg->usb_phy, 0);
1236 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1237 motg->chg_type = USB_INVALID_CHARGER;
1238 otg->state = OTG_STATE_B_IDLE;
1239 msm_otg_reset(otg->usb_phy);
1243 case OTG_STATE_A_HOST:
1244 dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
1245 if (test_bit(ID, &motg->inputs)) {
1246 msm_otg_start_host(otg->usb_phy, 0);
1247 otg->state = OTG_STATE_B_IDLE;
1248 msm_otg_reset(otg->usb_phy);
1257 static irqreturn_t msm_otg_irq(int irq, void *data)
1259 struct msm_otg *motg = data;
1260 struct usb_phy *phy = &motg->phy;
1263 if (atomic_read(&motg->in_lpm)) {
1264 disable_irq_nosync(irq);
1265 motg->async_int = 1;
1266 pm_runtime_get(phy->dev);
1270 otgsc = readl(USB_OTGSC);
1271 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1274 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1275 if (otgsc & OTGSC_ID)
1276 set_bit(ID, &motg->inputs);
1278 clear_bit(ID, &motg->inputs);
1279 dev_dbg(phy->dev, "ID set/clear\n");
1280 pm_runtime_get_noresume(phy->dev);
1281 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1282 if (otgsc & OTGSC_BSV)
1283 set_bit(B_SESS_VLD, &motg->inputs);
1285 clear_bit(B_SESS_VLD, &motg->inputs);
1286 dev_dbg(phy->dev, "BSV set/clear\n");
1287 pm_runtime_get_noresume(phy->dev);
1290 writel(otgsc, USB_OTGSC);
1291 schedule_work(&motg->sm_work);
1295 static int msm_otg_mode_show(struct seq_file *s, void *unused)
1297 struct msm_otg *motg = s->private;
1298 struct usb_otg *otg = motg->phy.otg;
1300 switch (otg->state) {
1301 case OTG_STATE_A_HOST:
1302 seq_puts(s, "host\n");
1304 case OTG_STATE_B_PERIPHERAL:
1305 seq_puts(s, "peripheral\n");
1308 seq_puts(s, "none\n");
1315 static int msm_otg_mode_open(struct inode *inode, struct file *file)
1317 return single_open(file, msm_otg_mode_show, inode->i_private);
1320 static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1321 size_t count, loff_t *ppos)
1323 struct seq_file *s = file->private_data;
1324 struct msm_otg *motg = s->private;
1326 struct usb_otg *otg = motg->phy.otg;
1328 enum usb_dr_mode req_mode;
1330 memset(buf, 0x00, sizeof(buf));
1332 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1337 if (!strncmp(buf, "host", 4)) {
1338 req_mode = USB_DR_MODE_HOST;
1339 } else if (!strncmp(buf, "peripheral", 10)) {
1340 req_mode = USB_DR_MODE_PERIPHERAL;
1341 } else if (!strncmp(buf, "none", 4)) {
1342 req_mode = USB_DR_MODE_UNKNOWN;
1349 case USB_DR_MODE_UNKNOWN:
1350 switch (otg->state) {
1351 case OTG_STATE_A_HOST:
1352 case OTG_STATE_B_PERIPHERAL:
1353 set_bit(ID, &motg->inputs);
1354 clear_bit(B_SESS_VLD, &motg->inputs);
1360 case USB_DR_MODE_PERIPHERAL:
1361 switch (otg->state) {
1362 case OTG_STATE_B_IDLE:
1363 case OTG_STATE_A_HOST:
1364 set_bit(ID, &motg->inputs);
1365 set_bit(B_SESS_VLD, &motg->inputs);
1371 case USB_DR_MODE_HOST:
1372 switch (otg->state) {
1373 case OTG_STATE_B_IDLE:
1374 case OTG_STATE_B_PERIPHERAL:
1375 clear_bit(ID, &motg->inputs);
1385 pm_runtime_get_sync(otg->usb_phy->dev);
1386 schedule_work(&motg->sm_work);
1391 static const struct file_operations msm_otg_mode_fops = {
1392 .open = msm_otg_mode_open,
1394 .write = msm_otg_mode_write,
1395 .llseek = seq_lseek,
1396 .release = single_release,
1399 static struct dentry *msm_otg_dbg_root;
1400 static struct dentry *msm_otg_dbg_mode;
1402 static int msm_otg_debugfs_init(struct msm_otg *motg)
1404 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1406 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1409 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1410 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1411 if (!msm_otg_dbg_mode) {
1412 debugfs_remove(msm_otg_dbg_root);
1413 msm_otg_dbg_root = NULL;
1420 static void msm_otg_debugfs_cleanup(void)
1422 debugfs_remove(msm_otg_dbg_mode);
1423 debugfs_remove(msm_otg_dbg_root);
1426 static const struct of_device_id msm_otg_dt_match[] = {
1428 .compatible = "qcom,usb-otg-ci",
1429 .data = (void *) CI_45NM_INTEGRATED_PHY
1432 .compatible = "qcom,usb-otg-snps",
1433 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1437 MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1439 static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1441 struct msm_otg_platform_data *pdata;
1442 const struct of_device_id *id;
1443 struct device_node *node = pdev->dev.of_node;
1444 struct property *prop;
1445 int len, ret, words;
1448 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1452 motg->pdata = pdata;
1454 id = of_match_device(msm_otg_dt_match, &pdev->dev);
1455 pdata->phy_type = (enum msm_usb_phy_type) id->data;
1457 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1458 if (IS_ERR(motg->link_rst))
1459 return PTR_ERR(motg->link_rst);
1461 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1462 if (IS_ERR(motg->phy_rst))
1463 motg->phy_rst = NULL;
1465 pdata->mode = of_usb_get_dr_mode(node);
1466 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1467 pdata->mode = USB_DR_MODE_OTG;
1469 pdata->otg_control = OTG_PHY_CONTROL;
1470 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1471 if (val == OTG_PMIC_CONTROL)
1472 pdata->otg_control = val;
1474 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1475 motg->phy_number = val;
1477 motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
1478 motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
1479 motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
1481 if (of_get_property(node, "qcom,vdd-levels", &len) &&
1482 len == sizeof(tmp)) {
1483 of_property_read_u32_array(node, "qcom,vdd-levels",
1484 tmp, len / sizeof(*tmp));
1485 motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
1486 motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
1487 motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
1490 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1494 words = len / sizeof(u32);
1496 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1497 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1501 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
1502 if (!pdata->phy_init_seq)
1505 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1506 pdata->phy_init_seq, words);
1508 pdata->phy_init_sz = words;
1513 static int msm_otg_probe(struct platform_device *pdev)
1515 struct regulator_bulk_data regs[3];
1517 struct device_node *np = pdev->dev.of_node;
1518 struct msm_otg_platform_data *pdata;
1519 struct resource *res;
1520 struct msm_otg *motg;
1521 struct usb_phy *phy;
1522 void __iomem *phy_select;
1524 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
1528 pdata = dev_get_platdata(&pdev->dev);
1532 ret = msm_otg_read_dt(pdev, motg);
1537 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1543 phy->dev = &pdev->dev;
1545 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
1546 if (IS_ERR(motg->clk)) {
1547 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1548 return PTR_ERR(motg->clk);
1552 * If USB Core is running its protocol engine based on CORE CLK,
1553 * CORE CLK must be running at >55Mhz for correct HSUSB
1554 * operation and USB core cannot tolerate frequency changes on
1557 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
1558 if (IS_ERR(motg->pclk)) {
1559 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1560 return PTR_ERR(motg->pclk);
1564 * USB core clock is not present on all MSM chips. This
1565 * clock is introduced to remove the dependency on AXI
1568 motg->core_clk = devm_clk_get(&pdev->dev,
1569 np ? "alt_core" : "usb_hs_core_clk");
1571 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1574 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1579 * NOTE: The PHYs can be multiplexed between the chipidea controller
1580 * and the dwc3 controller, using a single bit. It is important that
1581 * the dwc3 driver does not set this bit in an incompatible way.
1583 if (motg->phy_number) {
1584 phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
1587 /* Enable second PHY with the OTG port */
1588 writel(0x1, phy_select);
1591 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1593 motg->irq = platform_get_irq(pdev, 0);
1594 if (motg->irq < 0) {
1595 dev_err(&pdev->dev, "platform_get_irq failed\n");
1599 regs[0].supply = "vddcx";
1600 regs[1].supply = "v3p3";
1601 regs[2].supply = "v1p8";
1603 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1607 motg->vddcx = regs[0].consumer;
1608 motg->v3p3 = regs[1].consumer;
1609 motg->v1p8 = regs[2].consumer;
1611 clk_set_rate(motg->clk, 60000000);
1613 clk_prepare_enable(motg->clk);
1614 clk_prepare_enable(motg->pclk);
1616 if (!IS_ERR(motg->core_clk))
1617 clk_prepare_enable(motg->core_clk);
1619 ret = msm_hsusb_init_vddcx(motg, 1);
1621 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1625 ret = msm_hsusb_ldo_init(motg, 1);
1627 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1630 ret = msm_hsusb_ldo_set_mode(motg, 1);
1632 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1636 writel(0, USB_USBINTR);
1637 writel(0, USB_OTGSC);
1639 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1640 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1641 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
1644 dev_err(&pdev->dev, "request irq failed\n");
1648 phy->init = msm_phy_init;
1649 phy->set_power = msm_otg_set_power;
1650 phy->notify_disconnect = msm_phy_notify_disconnect;
1651 phy->type = USB_PHY_TYPE_USB2;
1653 phy->io_ops = &msm_otg_io_ops;
1655 phy->otg->usb_phy = &motg->phy;
1656 phy->otg->set_host = msm_otg_set_host;
1657 phy->otg->set_peripheral = msm_otg_set_peripheral;
1661 ret = usb_add_phy_dev(&motg->phy);
1663 dev_err(&pdev->dev, "usb_add_phy failed\n");
1667 platform_set_drvdata(pdev, motg);
1668 device_init_wakeup(&pdev->dev, 1);
1670 if (motg->pdata->mode == USB_DR_MODE_OTG &&
1671 motg->pdata->otg_control == OTG_USER_CONTROL) {
1672 ret = msm_otg_debugfs_init(motg);
1674 dev_dbg(&pdev->dev, "Can not create mode change file\n");
1677 pm_runtime_set_active(&pdev->dev);
1678 pm_runtime_enable(&pdev->dev);
1683 msm_hsusb_ldo_init(motg, 0);
1685 msm_hsusb_init_vddcx(motg, 0);
1687 clk_disable_unprepare(motg->pclk);
1688 clk_disable_unprepare(motg->clk);
1689 if (!IS_ERR(motg->core_clk))
1690 clk_disable_unprepare(motg->core_clk);
1694 static int msm_otg_remove(struct platform_device *pdev)
1696 struct msm_otg *motg = platform_get_drvdata(pdev);
1697 struct usb_phy *phy = &motg->phy;
1700 if (phy->otg->host || phy->otg->gadget)
1703 msm_otg_debugfs_cleanup();
1704 cancel_delayed_work_sync(&motg->chg_work);
1705 cancel_work_sync(&motg->sm_work);
1707 pm_runtime_resume(&pdev->dev);
1709 device_init_wakeup(&pdev->dev, 0);
1710 pm_runtime_disable(&pdev->dev);
1712 usb_remove_phy(phy);
1713 disable_irq(motg->irq);
1716 * Put PHY in low power mode.
1718 ulpi_read(phy, 0x14);
1719 ulpi_write(phy, 0x08, 0x09);
1721 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1722 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1723 if (readl(USB_PORTSC) & PORTSC_PHCD)
1728 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1729 dev_err(phy->dev, "Unable to suspend PHY\n");
1731 clk_disable_unprepare(motg->pclk);
1732 clk_disable_unprepare(motg->clk);
1733 if (!IS_ERR(motg->core_clk))
1734 clk_disable_unprepare(motg->core_clk);
1735 msm_hsusb_ldo_init(motg, 0);
1737 pm_runtime_set_suspended(&pdev->dev);
1743 static int msm_otg_runtime_idle(struct device *dev)
1745 struct msm_otg *motg = dev_get_drvdata(dev);
1746 struct usb_otg *otg = motg->phy.otg;
1748 dev_dbg(dev, "OTG runtime idle\n");
1751 * It is observed some times that a spurious interrupt
1752 * comes when PHY is put into LPM immediately after PHY reset.
1753 * This 1 sec delay also prevents entering into LPM immediately
1754 * after asynchronous interrupt.
1756 if (otg->state != OTG_STATE_UNDEFINED)
1757 pm_schedule_suspend(dev, 1000);
1762 static int msm_otg_runtime_suspend(struct device *dev)
1764 struct msm_otg *motg = dev_get_drvdata(dev);
1766 dev_dbg(dev, "OTG runtime suspend\n");
1767 return msm_otg_suspend(motg);
1770 static int msm_otg_runtime_resume(struct device *dev)
1772 struct msm_otg *motg = dev_get_drvdata(dev);
1774 dev_dbg(dev, "OTG runtime resume\n");
1775 return msm_otg_resume(motg);
1779 #ifdef CONFIG_PM_SLEEP
1780 static int msm_otg_pm_suspend(struct device *dev)
1782 struct msm_otg *motg = dev_get_drvdata(dev);
1784 dev_dbg(dev, "OTG PM suspend\n");
1785 return msm_otg_suspend(motg);
1788 static int msm_otg_pm_resume(struct device *dev)
1790 struct msm_otg *motg = dev_get_drvdata(dev);
1793 dev_dbg(dev, "OTG PM resume\n");
1795 ret = msm_otg_resume(motg);
1800 * Runtime PM Documentation recommends bringing the
1801 * device to full powered state upon resume.
1803 pm_runtime_disable(dev);
1804 pm_runtime_set_active(dev);
1805 pm_runtime_enable(dev);
1811 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1812 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
1813 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
1814 msm_otg_runtime_idle)
1817 static struct platform_driver msm_otg_driver = {
1818 .probe = msm_otg_probe,
1819 .remove = msm_otg_remove,
1821 .name = DRIVER_NAME,
1822 .pm = &msm_otg_dev_pm_ops,
1823 .of_match_table = msm_otg_dt_match,
1827 module_platform_driver(msm_otg_driver);
1829 MODULE_LICENSE("GPL v2");
1830 MODULE_DESCRIPTION("MSM USB transceiver driver");