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/gpio/consumer.h>
22 #include <linux/platform_device.h>
23 #include <linux/clk.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/err.h>
27 #include <linux/delay.h>
29 #include <linux/ioport.h>
30 #include <linux/uaccess.h>
31 #include <linux/debugfs.h>
32 #include <linux/seq_file.h>
33 #include <linux/pm_runtime.h>
35 #include <linux/of_device.h>
36 #include <linux/reboot.h>
37 #include <linux/reset.h>
39 #include <linux/usb.h>
40 #include <linux/usb/otg.h>
41 #include <linux/usb/of.h>
42 #include <linux/usb/ulpi.h>
43 #include <linux/usb/gadget.h>
44 #include <linux/usb/hcd.h>
45 #include <linux/usb/msm_hsusb.h>
46 #include <linux/usb/msm_hsusb_hw.h>
47 #include <linux/regulator/consumer.h>
48 #include <linux/msm-bus.h>
50 #define MSM_USB_BASE (motg->regs)
51 #define DRIVER_NAME "msm_otg"
53 #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
54 #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
56 #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
57 #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
58 #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
59 #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
61 #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
62 #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
63 #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
64 #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
66 #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */
67 #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
68 #define USB_PHY_SUSP_DIG_VOL 500000 /* uV */
76 static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
80 if (IS_ERR(motg->vddcx))
84 ret = regulator_set_voltage(motg->vddcx,
85 motg->vdd_levels[VDD_LEVEL_MIN],
86 motg->vdd_levels[VDD_LEVEL_MAX]);
88 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
92 ret = regulator_enable(motg->vddcx);
94 dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n");
96 ret = regulator_set_voltage(motg->vddcx, 0,
97 motg->vdd_levels[VDD_LEVEL_MAX]);
99 dev_err(motg->phy.dev, "Cannot set vddcx voltage\n");
100 ret = regulator_disable(motg->vddcx);
102 dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n");
108 static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
113 rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN,
114 USB_PHY_3P3_VOL_MAX);
116 dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n");
119 rc = regulator_enable(motg->v3p3);
121 dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n");
124 rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN,
125 USB_PHY_1P8_VOL_MAX);
127 dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n");
130 rc = regulator_enable(motg->v1p8);
132 dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n");
139 regulator_disable(motg->v1p8);
141 regulator_disable(motg->v3p3);
146 static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on)
151 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_HPM_LOAD);
153 pr_err("Could not set HPM for v1p8\n");
156 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_HPM_LOAD);
158 pr_err("Could not set HPM for v3p3\n");
159 regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
163 ret = regulator_set_load(motg->v1p8, USB_PHY_1P8_LPM_LOAD);
165 pr_err("Could not set LPM for v1p8\n");
166 ret = regulator_set_load(motg->v3p3, USB_PHY_3P3_LPM_LOAD);
168 pr_err("Could not set LPM for v3p3\n");
171 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
172 return ret < 0 ? ret : 0;
175 static int ulpi_read(struct usb_phy *phy, u32 reg)
177 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
180 /* initiate read operation */
181 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
184 /* wait for completion */
185 while (cnt < ULPI_IO_TIMEOUT_USEC) {
186 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
192 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
193 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
194 readl(USB_ULPI_VIEWPORT));
197 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
200 static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
202 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
205 /* initiate write operation */
206 writel(ULPI_RUN | ULPI_WRITE |
207 ULPI_ADDR(reg) | ULPI_DATA(val),
210 /* wait for completion */
211 while (cnt < ULPI_IO_TIMEOUT_USEC) {
212 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
218 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
219 dev_err(phy->dev, "ulpi_write: timeout\n");
225 static struct usb_phy_io_ops msm_otg_io_ops = {
230 static void ulpi_init(struct msm_otg *motg)
232 struct msm_otg_platform_data *pdata = motg->pdata;
233 int *seq = pdata->phy_init_seq, idx;
234 u32 addr = ULPI_EXT_VENDOR_SPECIFIC;
236 for (idx = 0; idx < pdata->phy_init_sz; idx++) {
240 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
241 seq[idx], addr + idx);
242 ulpi_write(&motg->phy, seq[idx], addr + idx);
246 static int msm_phy_notify_disconnect(struct usb_phy *phy,
247 enum usb_device_speed speed)
249 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
252 if (motg->manual_pullup) {
253 val = ULPI_MISC_A_VBUSVLDEXT | ULPI_MISC_A_VBUSVLDEXTSEL;
254 usb_phy_io_write(phy, val, ULPI_CLR(ULPI_MISC_A));
258 * Put the transceiver in non-driving mode. Otherwise host
259 * may not detect soft-disconnection.
261 val = ulpi_read(phy, ULPI_FUNC_CTRL);
262 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
263 val |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
264 ulpi_write(phy, val, ULPI_FUNC_CTRL);
269 static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
274 ret = reset_control_assert(motg->link_rst);
276 ret = reset_control_deassert(motg->link_rst);
279 dev_err(motg->phy.dev, "usb link clk reset %s failed\n",
280 assert ? "assert" : "deassert");
285 static int msm_otg_phy_clk_reset(struct msm_otg *motg)
290 ret = reset_control_reset(motg->phy_rst);
293 dev_err(motg->phy.dev, "usb phy clk reset failed\n");
298 static int msm_link_reset(struct msm_otg *motg)
303 ret = msm_otg_link_clk_reset(motg, 1);
307 /* wait for 1ms delay as suggested in HPG. */
308 usleep_range(1000, 1200);
310 ret = msm_otg_link_clk_reset(motg, 0);
314 if (motg->phy_number)
315 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
317 /* put transceiver in serial mode as part of reset */
318 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
319 writel(val | PORTSC_PTS_SERIAL, USB_PORTSC);
324 static int msm_otg_reset(struct usb_phy *phy)
326 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
329 writel(USBCMD_RESET, USB_USBCMD);
330 while (cnt < LINK_RESET_TIMEOUT_USEC) {
331 if (!(readl(USB_USBCMD) & USBCMD_RESET))
336 if (cnt >= LINK_RESET_TIMEOUT_USEC)
339 /* select ULPI phy and clear other status/control bits in PORTSC */
340 writel(PORTSC_PTS_ULPI, USB_PORTSC);
342 writel(0x0, USB_AHBBURST);
343 writel(0x08, USB_AHBMODE);
345 if (motg->phy_number)
346 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
350 static void msm_phy_reset(struct msm_otg *motg)
354 if (motg->pdata->phy_type != SNPS_28NM_INTEGRATED_PHY) {
355 msm_otg_phy_clk_reset(motg);
360 if (motg->phy_number)
361 addr = USB_PHY_CTRL2;
363 /* Assert USB PHY_POR */
364 writel(readl(addr) | PHY_POR_ASSERT, addr);
367 * wait for minimum 10 microseconds as suggested in HPG.
368 * Use a slightly larger value since the exact value didn't
369 * work 100% of the time.
373 /* Deassert USB PHY_POR */
374 writel(readl(addr) & ~PHY_POR_ASSERT, addr);
377 static int msm_usb_reset(struct usb_phy *phy)
379 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
382 if (!IS_ERR(motg->core_clk))
383 clk_prepare_enable(motg->core_clk);
385 ret = msm_link_reset(motg);
387 dev_err(phy->dev, "phy_reset failed\n");
391 ret = msm_otg_reset(&motg->phy);
393 dev_err(phy->dev, "link reset failed\n");
399 /* Reset USB PHY after performing USB Link RESET */
402 if (!IS_ERR(motg->core_clk))
403 clk_disable_unprepare(motg->core_clk);
408 static int msm_phy_init(struct usb_phy *phy)
410 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
411 struct msm_otg_platform_data *pdata = motg->pdata;
412 u32 val, ulpi_val = 0;
414 /* Program USB PHY Override registers. */
418 * It is recommended in HPG to reset USB PHY after programming
419 * USB PHY Override registers.
423 if (pdata->otg_control == OTG_PHY_CONTROL) {
424 val = readl(USB_OTGSC);
425 if (pdata->mode == USB_DR_MODE_OTG) {
426 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
427 val |= OTGSC_IDIE | OTGSC_BSVIE;
428 } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) {
429 ulpi_val = ULPI_INT_SESS_VALID;
432 writel(val, USB_OTGSC);
433 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
434 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
437 if (motg->manual_pullup) {
438 val = ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT;
439 ulpi_write(phy, val, ULPI_SET(ULPI_MISC_A));
441 val = readl(USB_GENCONFIG_2);
442 val |= GENCONFIG_2_SESS_VLD_CTRL_EN;
443 writel(val, USB_GENCONFIG_2);
445 val = readl(USB_USBCMD);
446 val |= USBCMD_SESS_VLD_CTRL;
447 writel(val, USB_USBCMD);
449 val = ulpi_read(phy, ULPI_FUNC_CTRL);
450 val &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
451 val |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
452 ulpi_write(phy, val, ULPI_FUNC_CTRL);
455 if (motg->phy_number)
456 writel(readl(USB_PHY_CTRL2) | BIT(16), USB_PHY_CTRL2);
461 #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
462 #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
466 static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high)
468 int max_vol = motg->vdd_levels[VDD_LEVEL_MAX];
473 min_vol = motg->vdd_levels[VDD_LEVEL_MIN];
475 min_vol = motg->vdd_levels[VDD_LEVEL_NONE];
477 ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol);
479 pr_err("Cannot set vddcx voltage\n");
483 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
488 static int msm_otg_suspend(struct msm_otg *motg)
490 struct usb_phy *phy = &motg->phy;
491 struct usb_bus *bus = phy->otg->host;
492 struct msm_otg_platform_data *pdata = motg->pdata;
496 if (atomic_read(&motg->in_lpm))
499 disable_irq(motg->irq);
501 * Chipidea 45-nm PHY suspend sequence:
503 * Interrupt Latch Register auto-clear feature is not present
504 * in all PHY versions. Latch register is clear on read type.
505 * Clear latch register to avoid spurious wakeup from
506 * low power mode (LPM).
508 * PHY comparators are disabled when PHY enters into low power
509 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
510 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
511 * PHY comparators. This save significant amount of power.
513 * PLL is not turned off when PHY enters into low power mode (LPM).
514 * Disable PLL for maximum power savings.
517 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
518 ulpi_read(phy, 0x14);
519 if (pdata->otg_control == OTG_PHY_CONTROL)
520 ulpi_write(phy, 0x01, 0x30);
521 ulpi_write(phy, 0x08, 0x09);
525 * PHY may take some time or even fail to enter into low power
526 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
529 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
530 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
531 if (readl(USB_PORTSC) & PORTSC_PHCD)
537 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
538 dev_err(phy->dev, "Unable to suspend PHY\n");
540 enable_irq(motg->irq);
545 * PHY has capability to generate interrupt asynchronously in low
546 * power mode (LPM). This interrupt is level triggered. So USB IRQ
547 * line must be disabled till async interrupt enable bit is cleared
548 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
549 * block data communication from PHY.
551 writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD);
554 if (motg->phy_number)
555 addr = USB_PHY_CTRL2;
557 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
558 motg->pdata->otg_control == OTG_PMIC_CONTROL)
559 writel(readl(addr) | PHY_RETEN, addr);
561 clk_disable_unprepare(motg->pclk);
562 clk_disable_unprepare(motg->clk);
563 if (!IS_ERR(motg->core_clk))
564 clk_disable_unprepare(motg->core_clk);
566 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
567 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
568 msm_hsusb_ldo_set_mode(motg, 0);
569 msm_hsusb_config_vddcx(motg, 0);
572 if (device_may_wakeup(phy->dev))
573 enable_irq_wake(motg->irq);
575 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
577 atomic_set(&motg->in_lpm, 1);
578 enable_irq(motg->irq);
580 dev_info(phy->dev, "USB in low power mode\n");
585 static int msm_otg_resume(struct msm_otg *motg)
587 struct usb_phy *phy = &motg->phy;
588 struct usb_bus *bus = phy->otg->host;
593 if (!atomic_read(&motg->in_lpm))
596 clk_prepare_enable(motg->pclk);
597 clk_prepare_enable(motg->clk);
598 if (!IS_ERR(motg->core_clk))
599 clk_prepare_enable(motg->core_clk);
601 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY &&
602 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
605 if (motg->phy_number)
606 addr = USB_PHY_CTRL2;
608 msm_hsusb_ldo_set_mode(motg, 1);
609 msm_hsusb_config_vddcx(motg, 1);
610 writel(readl(addr) & ~PHY_RETEN, addr);
613 temp = readl(USB_USBCMD);
614 temp &= ~ASYNC_INTR_CTRL;
615 temp &= ~ULPI_STP_CTRL;
616 writel(temp, USB_USBCMD);
619 * PHY comes out of low power mode (LPM) in case of wakeup
620 * from asynchronous interrupt.
622 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
623 goto skip_phy_resume;
625 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
626 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
627 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
633 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
635 * This is a fatal error. Reset the link and
636 * PHY. USB state can not be restored. Re-insertion
637 * of USB cable is the only way to get USB working.
639 dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n");
644 if (device_may_wakeup(phy->dev))
645 disable_irq_wake(motg->irq);
647 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
649 atomic_set(&motg->in_lpm, 0);
651 if (motg->async_int) {
653 pm_runtime_put(phy->dev);
654 enable_irq(motg->irq);
657 dev_info(phy->dev, "USB exited from low power mode\n");
663 static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
665 if (motg->cur_power == mA)
668 /* TODO: Notify PMIC about available current */
669 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
670 motg->cur_power = mA;
673 static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
675 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
678 * Gadget driver uses set_power method to notify about the
679 * available current based on suspend/configured states.
681 * IDEV_CHG can be drawn irrespective of suspend/un-configured
682 * states when CDP/ACA is connected.
684 if (motg->chg_type == USB_SDP_CHARGER)
685 msm_otg_notify_charger(motg, mA);
690 static void msm_otg_start_host(struct usb_phy *phy, int on)
692 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
693 struct msm_otg_platform_data *pdata = motg->pdata;
699 hcd = bus_to_hcd(phy->otg->host);
702 dev_dbg(phy->dev, "host on\n");
704 if (pdata->vbus_power)
705 pdata->vbus_power(1);
707 * Some boards have a switch cotrolled by gpio
708 * to enable/disable internal HUB. Enable internal
709 * HUB before kicking the host.
711 if (pdata->setup_gpio)
712 pdata->setup_gpio(OTG_STATE_A_HOST);
714 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
715 device_wakeup_enable(hcd->self.controller);
718 dev_dbg(phy->dev, "host off\n");
723 if (pdata->setup_gpio)
724 pdata->setup_gpio(OTG_STATE_UNDEFINED);
725 if (pdata->vbus_power)
726 pdata->vbus_power(0);
730 static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
732 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
736 * Fail host registration if this board can support
737 * only peripheral configuration.
739 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) {
740 dev_info(otg->usb_phy->dev, "Host mode is not supported\n");
745 if (otg->state == OTG_STATE_A_HOST) {
746 pm_runtime_get_sync(otg->usb_phy->dev);
747 msm_otg_start_host(otg->usb_phy, 0);
749 otg->state = OTG_STATE_UNDEFINED;
750 schedule_work(&motg->sm_work);
758 hcd = bus_to_hcd(host);
759 hcd->power_budget = motg->pdata->power_budget;
762 dev_dbg(otg->usb_phy->dev, "host driver registered w/ tranceiver\n");
765 * Kick the state machine work, if peripheral is not supported
766 * or peripheral is already registered with us.
768 if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) {
769 pm_runtime_get_sync(otg->usb_phy->dev);
770 schedule_work(&motg->sm_work);
776 static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
778 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
779 struct msm_otg_platform_data *pdata = motg->pdata;
781 if (!phy->otg->gadget)
785 dev_dbg(phy->dev, "gadget on\n");
787 * Some boards have a switch cotrolled by gpio
788 * to enable/disable internal HUB. Disable internal
789 * HUB before kicking the gadget.
791 if (pdata->setup_gpio)
792 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
793 usb_gadget_vbus_connect(phy->otg->gadget);
795 dev_dbg(phy->dev, "gadget off\n");
796 usb_gadget_vbus_disconnect(phy->otg->gadget);
797 if (pdata->setup_gpio)
798 pdata->setup_gpio(OTG_STATE_UNDEFINED);
803 static int msm_otg_set_peripheral(struct usb_otg *otg,
804 struct usb_gadget *gadget)
806 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
809 * Fail peripheral registration if this board can support
810 * only host configuration.
812 if (motg->pdata->mode == USB_DR_MODE_HOST) {
813 dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
818 if (otg->state == OTG_STATE_B_PERIPHERAL) {
819 pm_runtime_get_sync(otg->usb_phy->dev);
820 msm_otg_start_peripheral(otg->usb_phy, 0);
822 otg->state = OTG_STATE_UNDEFINED;
823 schedule_work(&motg->sm_work);
830 otg->gadget = gadget;
831 dev_dbg(otg->usb_phy->dev,
832 "peripheral driver registered w/ tranceiver\n");
835 * Kick the state machine work, if host is not supported
836 * or host is already registered with us.
838 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) {
839 pm_runtime_get_sync(otg->usb_phy->dev);
840 schedule_work(&motg->sm_work);
846 static bool msm_chg_check_secondary_det(struct msm_otg *motg)
848 struct usb_phy *phy = &motg->phy;
852 switch (motg->pdata->phy_type) {
853 case CI_45NM_INTEGRATED_PHY:
854 chg_det = ulpi_read(phy, 0x34);
855 ret = chg_det & (1 << 4);
857 case SNPS_28NM_INTEGRATED_PHY:
858 chg_det = ulpi_read(phy, 0x87);
867 static void msm_chg_enable_secondary_det(struct msm_otg *motg)
869 struct usb_phy *phy = &motg->phy;
872 switch (motg->pdata->phy_type) {
873 case CI_45NM_INTEGRATED_PHY:
874 chg_det = ulpi_read(phy, 0x34);
875 /* Turn off charger block */
876 chg_det |= ~(1 << 1);
877 ulpi_write(phy, chg_det, 0x34);
879 /* control chg block via ULPI */
880 chg_det &= ~(1 << 3);
881 ulpi_write(phy, chg_det, 0x34);
882 /* put it in host mode for enabling D- source */
883 chg_det &= ~(1 << 2);
884 ulpi_write(phy, chg_det, 0x34);
885 /* Turn on chg detect block */
886 chg_det &= ~(1 << 1);
887 ulpi_write(phy, chg_det, 0x34);
889 /* enable chg detection */
890 chg_det &= ~(1 << 0);
891 ulpi_write(phy, chg_det, 0x34);
893 case SNPS_28NM_INTEGRATED_PHY:
895 * Configure DM as current source, DP as current sink
896 * and enable battery charging comparators.
898 ulpi_write(phy, 0x8, 0x85);
899 ulpi_write(phy, 0x2, 0x85);
900 ulpi_write(phy, 0x1, 0x85);
907 static bool msm_chg_check_primary_det(struct msm_otg *motg)
909 struct usb_phy *phy = &motg->phy;
913 switch (motg->pdata->phy_type) {
914 case CI_45NM_INTEGRATED_PHY:
915 chg_det = ulpi_read(phy, 0x34);
916 ret = chg_det & (1 << 4);
918 case SNPS_28NM_INTEGRATED_PHY:
919 chg_det = ulpi_read(phy, 0x87);
928 static void msm_chg_enable_primary_det(struct msm_otg *motg)
930 struct usb_phy *phy = &motg->phy;
933 switch (motg->pdata->phy_type) {
934 case CI_45NM_INTEGRATED_PHY:
935 chg_det = ulpi_read(phy, 0x34);
936 /* enable chg detection */
937 chg_det &= ~(1 << 0);
938 ulpi_write(phy, chg_det, 0x34);
940 case SNPS_28NM_INTEGRATED_PHY:
942 * Configure DP as current source, DM as current sink
943 * and enable battery charging comparators.
945 ulpi_write(phy, 0x2, 0x85);
946 ulpi_write(phy, 0x1, 0x85);
953 static bool msm_chg_check_dcd(struct msm_otg *motg)
955 struct usb_phy *phy = &motg->phy;
959 switch (motg->pdata->phy_type) {
960 case CI_45NM_INTEGRATED_PHY:
961 line_state = ulpi_read(phy, 0x15);
962 ret = !(line_state & 1);
964 case SNPS_28NM_INTEGRATED_PHY:
965 line_state = ulpi_read(phy, 0x87);
966 ret = line_state & 2;
974 static void msm_chg_disable_dcd(struct msm_otg *motg)
976 struct usb_phy *phy = &motg->phy;
979 switch (motg->pdata->phy_type) {
980 case CI_45NM_INTEGRATED_PHY:
981 chg_det = ulpi_read(phy, 0x34);
982 chg_det &= ~(1 << 5);
983 ulpi_write(phy, chg_det, 0x34);
985 case SNPS_28NM_INTEGRATED_PHY:
986 ulpi_write(phy, 0x10, 0x86);
993 static void msm_chg_enable_dcd(struct msm_otg *motg)
995 struct usb_phy *phy = &motg->phy;
998 switch (motg->pdata->phy_type) {
999 case CI_45NM_INTEGRATED_PHY:
1000 chg_det = ulpi_read(phy, 0x34);
1001 /* Turn on D+ current source */
1002 chg_det |= (1 << 5);
1003 ulpi_write(phy, chg_det, 0x34);
1005 case SNPS_28NM_INTEGRATED_PHY:
1006 /* Data contact detection enable */
1007 ulpi_write(phy, 0x10, 0x85);
1014 static void msm_chg_block_on(struct msm_otg *motg)
1016 struct usb_phy *phy = &motg->phy;
1017 u32 func_ctrl, chg_det;
1019 /* put the controller in non-driving mode */
1020 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1021 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1022 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1023 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1025 switch (motg->pdata->phy_type) {
1026 case CI_45NM_INTEGRATED_PHY:
1027 chg_det = ulpi_read(phy, 0x34);
1028 /* control chg block via ULPI */
1029 chg_det &= ~(1 << 3);
1030 ulpi_write(phy, chg_det, 0x34);
1031 /* Turn on chg detect block */
1032 chg_det &= ~(1 << 1);
1033 ulpi_write(phy, chg_det, 0x34);
1036 case SNPS_28NM_INTEGRATED_PHY:
1037 /* Clear charger detecting control bits */
1038 ulpi_write(phy, 0x3F, 0x86);
1039 /* Clear alt interrupt latch and enable bits */
1040 ulpi_write(phy, 0x1F, 0x92);
1041 ulpi_write(phy, 0x1F, 0x95);
1049 static void msm_chg_block_off(struct msm_otg *motg)
1051 struct usb_phy *phy = &motg->phy;
1052 u32 func_ctrl, chg_det;
1054 switch (motg->pdata->phy_type) {
1055 case CI_45NM_INTEGRATED_PHY:
1056 chg_det = ulpi_read(phy, 0x34);
1057 /* Turn off charger block */
1058 chg_det |= ~(1 << 1);
1059 ulpi_write(phy, chg_det, 0x34);
1061 case SNPS_28NM_INTEGRATED_PHY:
1062 /* Clear charger detecting control bits */
1063 ulpi_write(phy, 0x3F, 0x86);
1064 /* Clear alt interrupt latch and enable bits */
1065 ulpi_write(phy, 0x1F, 0x92);
1066 ulpi_write(phy, 0x1F, 0x95);
1072 /* put the controller in normal mode */
1073 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1074 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1075 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1076 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1079 #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1080 #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1081 #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1082 #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1083 static void msm_chg_detect_work(struct work_struct *w)
1085 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1086 struct usb_phy *phy = &motg->phy;
1087 bool is_dcd, tmout, vout;
1088 unsigned long delay;
1090 dev_dbg(phy->dev, "chg detection work\n");
1091 switch (motg->chg_state) {
1092 case USB_CHG_STATE_UNDEFINED:
1093 pm_runtime_get_sync(phy->dev);
1094 msm_chg_block_on(motg);
1095 msm_chg_enable_dcd(motg);
1096 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1097 motg->dcd_retries = 0;
1098 delay = MSM_CHG_DCD_POLL_TIME;
1100 case USB_CHG_STATE_WAIT_FOR_DCD:
1101 is_dcd = msm_chg_check_dcd(motg);
1102 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1103 if (is_dcd || tmout) {
1104 msm_chg_disable_dcd(motg);
1105 msm_chg_enable_primary_det(motg);
1106 delay = MSM_CHG_PRIMARY_DET_TIME;
1107 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1109 delay = MSM_CHG_DCD_POLL_TIME;
1112 case USB_CHG_STATE_DCD_DONE:
1113 vout = msm_chg_check_primary_det(motg);
1115 msm_chg_enable_secondary_det(motg);
1116 delay = MSM_CHG_SECONDARY_DET_TIME;
1117 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1119 motg->chg_type = USB_SDP_CHARGER;
1120 motg->chg_state = USB_CHG_STATE_DETECTED;
1124 case USB_CHG_STATE_PRIMARY_DONE:
1125 vout = msm_chg_check_secondary_det(motg);
1127 motg->chg_type = USB_DCP_CHARGER;
1129 motg->chg_type = USB_CDP_CHARGER;
1130 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1132 case USB_CHG_STATE_SECONDARY_DONE:
1133 motg->chg_state = USB_CHG_STATE_DETECTED;
1134 case USB_CHG_STATE_DETECTED:
1135 msm_chg_block_off(motg);
1136 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1137 schedule_work(&motg->sm_work);
1143 schedule_delayed_work(&motg->chg_work, delay);
1147 * We support OTG, Peripheral only and Host only configurations. In case
1148 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1149 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1150 * enabled when switch is controlled by user and default mode is supplied
1151 * by board file, which can be changed by userspace later.
1153 static void msm_otg_init_sm(struct msm_otg *motg)
1155 struct msm_otg_platform_data *pdata = motg->pdata;
1156 u32 otgsc = readl(USB_OTGSC);
1158 switch (pdata->mode) {
1159 case USB_DR_MODE_OTG:
1160 if (pdata->otg_control == OTG_PHY_CONTROL) {
1161 if (otgsc & OTGSC_ID)
1162 set_bit(ID, &motg->inputs);
1164 clear_bit(ID, &motg->inputs);
1166 if (otgsc & OTGSC_BSV)
1167 set_bit(B_SESS_VLD, &motg->inputs);
1169 clear_bit(B_SESS_VLD, &motg->inputs);
1170 } else if (pdata->otg_control == OTG_USER_CONTROL) {
1171 set_bit(ID, &motg->inputs);
1172 clear_bit(B_SESS_VLD, &motg->inputs);
1175 case USB_DR_MODE_HOST:
1176 clear_bit(ID, &motg->inputs);
1178 case USB_DR_MODE_PERIPHERAL:
1179 set_bit(ID, &motg->inputs);
1180 if (otgsc & OTGSC_BSV)
1181 set_bit(B_SESS_VLD, &motg->inputs);
1183 clear_bit(B_SESS_VLD, &motg->inputs);
1190 static void msm_otg_sm_work(struct work_struct *w)
1192 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1193 struct usb_otg *otg = motg->phy.otg;
1195 switch (otg->state) {
1196 case OTG_STATE_UNDEFINED:
1197 dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
1198 msm_otg_reset(otg->usb_phy);
1199 msm_otg_init_sm(motg);
1200 otg->state = OTG_STATE_B_IDLE;
1202 case OTG_STATE_B_IDLE:
1203 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
1204 if (!test_bit(ID, &motg->inputs) && otg->host) {
1205 /* disable BSV bit */
1206 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1207 msm_otg_start_host(otg->usb_phy, 1);
1208 otg->state = OTG_STATE_A_HOST;
1209 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1210 switch (motg->chg_state) {
1211 case USB_CHG_STATE_UNDEFINED:
1212 msm_chg_detect_work(&motg->chg_work.work);
1214 case USB_CHG_STATE_DETECTED:
1215 switch (motg->chg_type) {
1216 case USB_DCP_CHARGER:
1217 msm_otg_notify_charger(motg,
1220 case USB_CDP_CHARGER:
1221 msm_otg_notify_charger(motg,
1223 msm_otg_start_peripheral(otg->usb_phy,
1226 = OTG_STATE_B_PERIPHERAL;
1228 case USB_SDP_CHARGER:
1229 msm_otg_notify_charger(motg, IUNIT);
1230 msm_otg_start_peripheral(otg->usb_phy,
1233 = OTG_STATE_B_PERIPHERAL;
1244 * If charger detection work is pending, decrement
1245 * the pm usage counter to balance with the one that
1246 * is incremented in charger detection work.
1248 if (cancel_delayed_work_sync(&motg->chg_work)) {
1249 pm_runtime_put_sync(otg->usb_phy->dev);
1250 msm_otg_reset(otg->usb_phy);
1252 msm_otg_notify_charger(motg, 0);
1253 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1254 motg->chg_type = USB_INVALID_CHARGER;
1257 if (otg->state == OTG_STATE_B_IDLE)
1258 pm_runtime_put_sync(otg->usb_phy->dev);
1260 case OTG_STATE_B_PERIPHERAL:
1261 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1262 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1263 !test_bit(ID, &motg->inputs)) {
1264 msm_otg_notify_charger(motg, 0);
1265 msm_otg_start_peripheral(otg->usb_phy, 0);
1266 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1267 motg->chg_type = USB_INVALID_CHARGER;
1268 otg->state = OTG_STATE_B_IDLE;
1269 msm_otg_reset(otg->usb_phy);
1273 case OTG_STATE_A_HOST:
1274 dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
1275 if (test_bit(ID, &motg->inputs)) {
1276 msm_otg_start_host(otg->usb_phy, 0);
1277 otg->state = OTG_STATE_B_IDLE;
1278 msm_otg_reset(otg->usb_phy);
1287 static irqreturn_t msm_otg_irq(int irq, void *data)
1289 struct msm_otg *motg = data;
1290 struct usb_phy *phy = &motg->phy;
1293 if (atomic_read(&motg->in_lpm)) {
1294 disable_irq_nosync(irq);
1295 motg->async_int = 1;
1296 pm_runtime_get(phy->dev);
1300 otgsc = readl(USB_OTGSC);
1301 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1304 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1305 if (otgsc & OTGSC_ID)
1306 set_bit(ID, &motg->inputs);
1308 clear_bit(ID, &motg->inputs);
1309 dev_dbg(phy->dev, "ID set/clear\n");
1310 pm_runtime_get_noresume(phy->dev);
1311 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1312 if (otgsc & OTGSC_BSV)
1313 set_bit(B_SESS_VLD, &motg->inputs);
1315 clear_bit(B_SESS_VLD, &motg->inputs);
1316 dev_dbg(phy->dev, "BSV set/clear\n");
1317 pm_runtime_get_noresume(phy->dev);
1320 writel(otgsc, USB_OTGSC);
1321 schedule_work(&motg->sm_work);
1325 static int msm_otg_mode_show(struct seq_file *s, void *unused)
1327 struct msm_otg *motg = s->private;
1328 struct usb_otg *otg = motg->phy.otg;
1330 switch (otg->state) {
1331 case OTG_STATE_A_HOST:
1332 seq_puts(s, "host\n");
1334 case OTG_STATE_B_PERIPHERAL:
1335 seq_puts(s, "peripheral\n");
1338 seq_puts(s, "none\n");
1345 static int msm_otg_mode_open(struct inode *inode, struct file *file)
1347 return single_open(file, msm_otg_mode_show, inode->i_private);
1350 static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1351 size_t count, loff_t *ppos)
1353 struct seq_file *s = file->private_data;
1354 struct msm_otg *motg = s->private;
1356 struct usb_otg *otg = motg->phy.otg;
1358 enum usb_dr_mode req_mode;
1360 memset(buf, 0x00, sizeof(buf));
1362 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1367 if (!strncmp(buf, "host", 4)) {
1368 req_mode = USB_DR_MODE_HOST;
1369 } else if (!strncmp(buf, "peripheral", 10)) {
1370 req_mode = USB_DR_MODE_PERIPHERAL;
1371 } else if (!strncmp(buf, "none", 4)) {
1372 req_mode = USB_DR_MODE_UNKNOWN;
1379 case USB_DR_MODE_UNKNOWN:
1380 switch (otg->state) {
1381 case OTG_STATE_A_HOST:
1382 case OTG_STATE_B_PERIPHERAL:
1383 set_bit(ID, &motg->inputs);
1384 clear_bit(B_SESS_VLD, &motg->inputs);
1390 case USB_DR_MODE_PERIPHERAL:
1391 switch (otg->state) {
1392 case OTG_STATE_B_IDLE:
1393 case OTG_STATE_A_HOST:
1394 set_bit(ID, &motg->inputs);
1395 set_bit(B_SESS_VLD, &motg->inputs);
1401 case USB_DR_MODE_HOST:
1402 switch (otg->state) {
1403 case OTG_STATE_B_IDLE:
1404 case OTG_STATE_B_PERIPHERAL:
1405 clear_bit(ID, &motg->inputs);
1415 pm_runtime_get_sync(otg->usb_phy->dev);
1416 schedule_work(&motg->sm_work);
1421 static const struct file_operations msm_otg_mode_fops = {
1422 .open = msm_otg_mode_open,
1424 .write = msm_otg_mode_write,
1425 .llseek = seq_lseek,
1426 .release = single_release,
1429 static struct dentry *msm_otg_dbg_root;
1430 static struct dentry *msm_otg_dbg_mode;
1432 static int msm_otg_debugfs_init(struct msm_otg *motg)
1434 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1436 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1439 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1440 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1441 if (!msm_otg_dbg_mode) {
1442 debugfs_remove(msm_otg_dbg_root);
1443 msm_otg_dbg_root = NULL;
1450 static void msm_otg_debugfs_cleanup(void)
1452 debugfs_remove(msm_otg_dbg_mode);
1453 debugfs_remove(msm_otg_dbg_root);
1456 static const struct of_device_id msm_otg_dt_match[] = {
1458 .compatible = "qcom,usb-otg-ci",
1459 .data = (void *) CI_45NM_INTEGRATED_PHY
1462 .compatible = "qcom,usb-otg-snps",
1463 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1467 MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1469 static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
1472 struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb);
1473 struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus);
1476 set_bit(B_SESS_VLD, &motg->inputs);
1478 clear_bit(B_SESS_VLD, &motg->inputs);
1480 if (test_bit(B_SESS_VLD, &motg->inputs)) {
1481 /* Switch D+/D- lines to Device connector */
1482 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1484 /* Switch D+/D- lines to Hub */
1485 gpiod_set_value_cansleep(motg->switch_gpio, 1);
1488 schedule_work(&motg->sm_work);
1493 static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
1496 struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb);
1497 struct msm_otg *motg = container_of(id, struct msm_otg, id);
1500 clear_bit(ID, &motg->inputs);
1502 set_bit(ID, &motg->inputs);
1504 schedule_work(&motg->sm_work);
1509 static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1511 struct msm_otg_platform_data *pdata;
1512 struct extcon_dev *ext_id, *ext_vbus;
1513 struct device_node *node = pdev->dev.of_node;
1514 struct property *prop;
1515 int len, ret, words;
1518 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1522 motg->pdata = pdata;
1524 pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
1525 if (!pdata->phy_type)
1528 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1529 if (IS_ERR(motg->link_rst))
1530 return PTR_ERR(motg->link_rst);
1532 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1533 if (IS_ERR(motg->phy_rst))
1534 motg->phy_rst = NULL;
1536 pdata->mode = usb_get_dr_mode(&pdev->dev);
1537 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1538 pdata->mode = USB_DR_MODE_OTG;
1540 pdata->otg_control = OTG_PHY_CONTROL;
1541 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1542 if (val == OTG_PMIC_CONTROL)
1543 pdata->otg_control = val;
1545 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1546 motg->phy_number = val;
1548 motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
1549 motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
1550 motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
1552 if (of_get_property(node, "qcom,vdd-levels", &len) &&
1553 len == sizeof(tmp)) {
1554 of_property_read_u32_array(node, "qcom,vdd-levels",
1555 tmp, len / sizeof(*tmp));
1556 motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
1557 motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
1558 motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
1561 motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");
1563 motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
1565 if (IS_ERR(motg->switch_gpio))
1566 return PTR_ERR(motg->switch_gpio);
1568 ext_id = ERR_PTR(-ENODEV);
1569 ext_vbus = ERR_PTR(-ENODEV);
1570 if (of_property_read_bool(node, "extcon")) {
1572 /* Each one of them is not mandatory */
1573 ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0);
1574 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
1575 return PTR_ERR(ext_vbus);
1577 ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1);
1578 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
1579 return PTR_ERR(ext_id);
1582 if (!IS_ERR(ext_vbus)) {
1583 motg->vbus.extcon = ext_vbus;
1584 motg->vbus.nb.notifier_call = msm_otg_vbus_notifier;
1585 ret = extcon_register_notifier(ext_vbus, EXTCON_USB,
1588 dev_err(&pdev->dev, "register VBUS notifier failed\n");
1592 ret = extcon_get_cable_state_(ext_vbus, EXTCON_USB);
1594 set_bit(B_SESS_VLD, &motg->inputs);
1596 clear_bit(B_SESS_VLD, &motg->inputs);
1599 if (!IS_ERR(ext_id)) {
1600 motg->id.extcon = ext_id;
1601 motg->id.nb.notifier_call = msm_otg_id_notifier;
1602 ret = extcon_register_notifier(ext_id, EXTCON_USB_HOST,
1605 dev_err(&pdev->dev, "register ID notifier failed\n");
1606 extcon_unregister_notifier(motg->vbus.extcon,
1607 EXTCON_USB, &motg->vbus.nb);
1611 ret = extcon_get_cable_state_(ext_id, EXTCON_USB_HOST);
1613 clear_bit(ID, &motg->inputs);
1615 set_bit(ID, &motg->inputs);
1618 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1622 words = len / sizeof(u32);
1624 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1625 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1629 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
1630 if (!pdata->phy_init_seq)
1633 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1634 pdata->phy_init_seq, words);
1636 pdata->phy_init_sz = words;
1641 static int msm_otg_reboot_notify(struct notifier_block *this,
1642 unsigned long code, void *unused)
1644 struct msm_otg *motg = container_of(this, struct msm_otg, reboot);
1647 * Ensure that D+/D- lines are routed to uB connector, so
1648 * we could load bootloader/kernel at next reboot
1650 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1654 static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
1658 if (motg->bus_perf_client) {
1659 ret = msm_bus_scale_client_update_request(
1660 motg->bus_perf_client, vote);
1662 dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
1663 "for bus bw %d\n", __func__, vote, ret);
1667 static int msm_otg_probe(struct platform_device *pdev)
1669 struct regulator_bulk_data regs[2];
1671 struct device_node *np = pdev->dev.of_node;
1672 struct msm_otg_platform_data *pdata;
1673 struct resource *res;
1674 struct msm_otg *motg;
1675 struct usb_phy *phy;
1676 void __iomem *phy_select;
1678 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
1682 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1688 phy->dev = &pdev->dev;
1689 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1690 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1692 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
1693 if (IS_ERR(motg->clk)) {
1694 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1695 return PTR_ERR(motg->clk);
1699 * If USB Core is running its protocol engine based on CORE CLK,
1700 * CORE CLK must be running at >55Mhz for correct HSUSB
1701 * operation and USB core cannot tolerate frequency changes on
1704 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
1705 if (IS_ERR(motg->pclk)) {
1706 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1707 return PTR_ERR(motg->pclk);
1711 * USB core clock is not present on all MSM chips. This
1712 * clock is introduced to remove the dependency on AXI
1715 motg->core_clk = devm_clk_get(&pdev->dev,
1716 np ? "alt_core" : "usb_hs_core_clk");
1718 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1721 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1725 pdata = dev_get_platdata(&pdev->dev);
1729 ret = msm_otg_read_dt(pdev, motg);
1735 * NOTE: The PHYs can be multiplexed between the chipidea controller
1736 * and the dwc3 controller, using a single bit. It is important that
1737 * the dwc3 driver does not set this bit in an incompatible way.
1739 if (motg->phy_number) {
1740 phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
1743 goto unregister_extcon;
1745 /* Enable second PHY with the OTG port */
1746 writel(0x1, phy_select);
1749 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1751 motg->irq = platform_get_irq(pdev, 0);
1752 if (motg->irq < 0) {
1753 dev_err(&pdev->dev, "platform_get_irq failed\n");
1755 goto unregister_extcon;
1758 regs[0].supply = "v3p3";
1759 regs[1].supply = "v1p8";
1761 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1763 dev_err(&pdev->dev, "no v3p3 or v1p8\n");
1764 goto unregister_extcon;
1767 motg->v3p3 = regs[0].consumer;
1768 motg->v1p8 = regs[1].consumer;
1770 motg->vddcx = devm_regulator_get_optional(motg->phy.dev, "vddcx");
1771 if (IS_ERR(motg->vddcx))
1772 dev_info(&pdev->dev, "no vddcx\n");
1774 clk_set_rate(motg->clk, 60000000);
1776 clk_prepare_enable(motg->clk);
1777 clk_prepare_enable(motg->pclk);
1779 if (!IS_ERR(motg->core_clk))
1780 clk_prepare_enable(motg->core_clk);
1782 ret = msm_hsusb_init_vddcx(motg, 1);
1784 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1788 ret = msm_hsusb_ldo_init(motg, 1);
1790 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1793 ret = msm_hsusb_ldo_set_mode(motg, 1);
1795 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1799 writel(0, USB_USBINTR);
1800 writel(0, USB_OTGSC);
1802 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
1805 dev_err(&pdev->dev, "request irq failed\n");
1809 phy->init = msm_phy_init;
1810 phy->set_power = msm_otg_set_power;
1811 phy->notify_disconnect = msm_phy_notify_disconnect;
1812 phy->type = USB_PHY_TYPE_USB2;
1814 phy->io_ops = &msm_otg_io_ops;
1816 phy->otg->usb_phy = &motg->phy;
1817 phy->otg->set_host = msm_otg_set_host;
1818 phy->otg->set_peripheral = msm_otg_set_peripheral;
1822 ret = usb_add_phy_dev(&motg->phy);
1824 dev_err(&pdev->dev, "usb_add_phy failed\n");
1828 motg->pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
1829 if (!motg->pdata->bus_scale_table)
1830 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
1832 motg->bus_perf_client =
1833 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
1834 if (!motg->bus_perf_client)
1835 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
1836 "scaling client!!\n", __func__);
1838 /* Hack to max out usb performace */
1839 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1841 platform_set_drvdata(pdev, motg);
1842 device_init_wakeup(&pdev->dev, 1);
1844 if (motg->pdata->mode == USB_DR_MODE_OTG &&
1845 motg->pdata->otg_control == OTG_USER_CONTROL) {
1846 ret = msm_otg_debugfs_init(motg);
1848 dev_dbg(&pdev->dev, "Can not create mode change file\n");
1851 if (test_bit(B_SESS_VLD, &motg->inputs)) {
1852 /* Switch D+/D- lines to Device connector */
1853 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1855 /* Switch D+/D- lines to Hub */
1856 gpiod_set_value_cansleep(motg->switch_gpio, 1);
1859 motg->reboot.notifier_call = msm_otg_reboot_notify;
1860 register_reboot_notifier(&motg->reboot);
1862 pm_runtime_set_active(&pdev->dev);
1867 msm_hsusb_ldo_init(motg, 0);
1869 msm_hsusb_init_vddcx(motg, 0);
1871 clk_disable_unprepare(motg->pclk);
1872 clk_disable_unprepare(motg->clk);
1873 if (!IS_ERR(motg->core_clk))
1874 clk_disable_unprepare(motg->core_clk);
1876 extcon_unregister_notifier(motg->id.extcon,
1877 EXTCON_USB_HOST, &motg->id.nb);
1878 extcon_unregister_notifier(motg->vbus.extcon,
1879 EXTCON_USB, &motg->vbus.nb);
1884 static int msm_otg_remove(struct platform_device *pdev)
1886 struct msm_otg *motg = platform_get_drvdata(pdev);
1887 struct usb_phy *phy = &motg->phy;
1890 if (phy->otg->host || phy->otg->gadget)
1893 unregister_reboot_notifier(&motg->reboot);
1896 * Ensure that D+/D- lines are routed to uB connector, so
1897 * we could load bootloader/kernel at next reboot
1899 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1901 extcon_unregister_notifier(motg->id.extcon, EXTCON_USB_HOST, &motg->id.nb);
1902 extcon_unregister_notifier(motg->vbus.extcon, EXTCON_USB, &motg->vbus.nb);
1904 msm_otg_debugfs_cleanup();
1905 cancel_delayed_work_sync(&motg->chg_work);
1906 cancel_work_sync(&motg->sm_work);
1908 pm_runtime_resume(&pdev->dev);
1910 device_init_wakeup(&pdev->dev, 0);
1911 pm_runtime_disable(&pdev->dev);
1913 usb_remove_phy(phy);
1914 disable_irq(motg->irq);
1915 msm_bus_scale_unregister_client(motg->bus_perf_client);
1918 * Put PHY in low power mode.
1920 ulpi_read(phy, 0x14);
1921 ulpi_write(phy, 0x08, 0x09);
1923 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1924 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1925 if (readl(USB_PORTSC) & PORTSC_PHCD)
1930 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1931 dev_err(phy->dev, "Unable to suspend PHY\n");
1933 clk_disable_unprepare(motg->pclk);
1934 clk_disable_unprepare(motg->clk);
1935 if (!IS_ERR(motg->core_clk))
1936 clk_disable_unprepare(motg->core_clk);
1937 msm_hsusb_ldo_init(motg, 0);
1939 pm_runtime_set_suspended(&pdev->dev);
1945 static int msm_otg_runtime_idle(struct device *dev)
1947 struct msm_otg *motg = dev_get_drvdata(dev);
1948 struct usb_otg *otg = motg->phy.otg;
1950 dev_dbg(dev, "OTG runtime idle\n");
1953 * It is observed some times that a spurious interrupt
1954 * comes when PHY is put into LPM immediately after PHY reset.
1955 * This 1 sec delay also prevents entering into LPM immediately
1956 * after asynchronous interrupt.
1958 if (otg->state != OTG_STATE_UNDEFINED)
1959 pm_schedule_suspend(dev, 1000);
1964 static int msm_otg_runtime_suspend(struct device *dev)
1966 struct msm_otg *motg = dev_get_drvdata(dev);
1968 dev_dbg(dev, "OTG runtime suspend\n");
1969 return msm_otg_suspend(motg);
1972 static int msm_otg_runtime_resume(struct device *dev)
1974 struct msm_otg *motg = dev_get_drvdata(dev);
1976 dev_dbg(dev, "OTG runtime resume\n");
1977 return msm_otg_resume(motg);
1981 #ifdef CONFIG_PM_SLEEP
1982 static int msm_otg_pm_suspend(struct device *dev)
1984 struct msm_otg *motg = dev_get_drvdata(dev);
1986 dev_dbg(dev, "OTG PM suspend\n");
1987 return msm_otg_suspend(motg);
1990 static int msm_otg_pm_resume(struct device *dev)
1992 struct msm_otg *motg = dev_get_drvdata(dev);
1995 dev_dbg(dev, "OTG PM resume\n");
1997 ret = msm_otg_resume(motg);
2002 * Runtime PM Documentation recommends bringing the
2003 * device to full powered state upon resume.
2005 pm_runtime_disable(dev);
2006 pm_runtime_set_active(dev);
2007 pm_runtime_enable(dev);
2013 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
2014 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2015 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2016 msm_otg_runtime_idle)
2019 static struct platform_driver msm_otg_driver = {
2020 .probe = msm_otg_probe,
2021 .remove = msm_otg_remove,
2023 .name = DRIVER_NAME,
2024 .pm = &msm_otg_dev_pm_ops,
2025 .of_match_table = msm_otg_dt_match,
2029 module_platform_driver(msm_otg_driver);
2031 MODULE_LICENSE("GPL v2");
2032 MODULE_DESCRIPTION("MSM USB transceiver driver");