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 ||
769 motg->pdata->mode == USB_DR_MODE_OTG || otg->gadget) {
770 pm_runtime_get_sync(otg->usb_phy->dev);
771 schedule_work(&motg->sm_work);
777 static void msm_otg_start_peripheral(struct usb_phy *phy, int on)
779 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
780 struct msm_otg_platform_data *pdata = motg->pdata;
782 if (!phy->otg->gadget)
786 dev_dbg(phy->dev, "gadget on\n");
788 * Some boards have a switch cotrolled by gpio
789 * to enable/disable internal HUB. Disable internal
790 * HUB before kicking the gadget.
792 if (pdata->setup_gpio)
793 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
794 usb_gadget_vbus_connect(phy->otg->gadget);
796 dev_dbg(phy->dev, "gadget off\n");
797 usb_gadget_vbus_disconnect(phy->otg->gadget);
798 if (pdata->setup_gpio)
799 pdata->setup_gpio(OTG_STATE_UNDEFINED);
804 static int msm_otg_set_peripheral(struct usb_otg *otg,
805 struct usb_gadget *gadget)
807 struct msm_otg *motg = container_of(otg->usb_phy, struct msm_otg, phy);
810 * Fail peripheral registration if this board can support
811 * only host configuration.
813 if (motg->pdata->mode == USB_DR_MODE_HOST) {
814 dev_info(otg->usb_phy->dev, "Peripheral mode is not supported\n");
819 if (otg->state == OTG_STATE_B_PERIPHERAL) {
820 pm_runtime_get_sync(otg->usb_phy->dev);
821 msm_otg_start_peripheral(otg->usb_phy, 0);
823 otg->state = OTG_STATE_UNDEFINED;
824 schedule_work(&motg->sm_work);
831 otg->gadget = gadget;
832 dev_dbg(otg->usb_phy->dev,
833 "peripheral driver registered w/ tranceiver\n");
836 * Kick the state machine work, if host is not supported
837 * or host is already registered with us.
839 if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL ||
840 motg->pdata->mode == USB_DR_MODE_OTG || otg->host) {
841 pm_runtime_get_sync(otg->usb_phy->dev);
842 schedule_work(&motg->sm_work);
848 static bool msm_chg_check_secondary_det(struct msm_otg *motg)
850 struct usb_phy *phy = &motg->phy;
854 switch (motg->pdata->phy_type) {
855 case CI_45NM_INTEGRATED_PHY:
856 chg_det = ulpi_read(phy, 0x34);
857 ret = chg_det & (1 << 4);
859 case SNPS_28NM_INTEGRATED_PHY:
860 chg_det = ulpi_read(phy, 0x87);
869 static void msm_chg_enable_secondary_det(struct msm_otg *motg)
871 struct usb_phy *phy = &motg->phy;
874 switch (motg->pdata->phy_type) {
875 case CI_45NM_INTEGRATED_PHY:
876 chg_det = ulpi_read(phy, 0x34);
877 /* Turn off charger block */
878 chg_det |= ~(1 << 1);
879 ulpi_write(phy, chg_det, 0x34);
881 /* control chg block via ULPI */
882 chg_det &= ~(1 << 3);
883 ulpi_write(phy, chg_det, 0x34);
884 /* put it in host mode for enabling D- source */
885 chg_det &= ~(1 << 2);
886 ulpi_write(phy, chg_det, 0x34);
887 /* Turn on chg detect block */
888 chg_det &= ~(1 << 1);
889 ulpi_write(phy, chg_det, 0x34);
891 /* enable chg detection */
892 chg_det &= ~(1 << 0);
893 ulpi_write(phy, chg_det, 0x34);
895 case SNPS_28NM_INTEGRATED_PHY:
897 * Configure DM as current source, DP as current sink
898 * and enable battery charging comparators.
900 ulpi_write(phy, 0x8, 0x85);
901 ulpi_write(phy, 0x2, 0x85);
902 ulpi_write(phy, 0x1, 0x85);
909 static bool msm_chg_check_primary_det(struct msm_otg *motg)
911 struct usb_phy *phy = &motg->phy;
915 switch (motg->pdata->phy_type) {
916 case CI_45NM_INTEGRATED_PHY:
917 chg_det = ulpi_read(phy, 0x34);
918 ret = chg_det & (1 << 4);
920 case SNPS_28NM_INTEGRATED_PHY:
921 chg_det = ulpi_read(phy, 0x87);
930 static void msm_chg_enable_primary_det(struct msm_otg *motg)
932 struct usb_phy *phy = &motg->phy;
935 switch (motg->pdata->phy_type) {
936 case CI_45NM_INTEGRATED_PHY:
937 chg_det = ulpi_read(phy, 0x34);
938 /* enable chg detection */
939 chg_det &= ~(1 << 0);
940 ulpi_write(phy, chg_det, 0x34);
942 case SNPS_28NM_INTEGRATED_PHY:
944 * Configure DP as current source, DM as current sink
945 * and enable battery charging comparators.
947 ulpi_write(phy, 0x2, 0x85);
948 ulpi_write(phy, 0x1, 0x85);
955 static bool msm_chg_check_dcd(struct msm_otg *motg)
957 struct usb_phy *phy = &motg->phy;
961 switch (motg->pdata->phy_type) {
962 case CI_45NM_INTEGRATED_PHY:
963 line_state = ulpi_read(phy, 0x15);
964 ret = !(line_state & 1);
966 case SNPS_28NM_INTEGRATED_PHY:
967 line_state = ulpi_read(phy, 0x87);
968 ret = line_state & 2;
976 static void msm_chg_disable_dcd(struct msm_otg *motg)
978 struct usb_phy *phy = &motg->phy;
981 switch (motg->pdata->phy_type) {
982 case CI_45NM_INTEGRATED_PHY:
983 chg_det = ulpi_read(phy, 0x34);
984 chg_det &= ~(1 << 5);
985 ulpi_write(phy, chg_det, 0x34);
987 case SNPS_28NM_INTEGRATED_PHY:
988 ulpi_write(phy, 0x10, 0x86);
995 static void msm_chg_enable_dcd(struct msm_otg *motg)
997 struct usb_phy *phy = &motg->phy;
1000 switch (motg->pdata->phy_type) {
1001 case CI_45NM_INTEGRATED_PHY:
1002 chg_det = ulpi_read(phy, 0x34);
1003 /* Turn on D+ current source */
1004 chg_det |= (1 << 5);
1005 ulpi_write(phy, chg_det, 0x34);
1007 case SNPS_28NM_INTEGRATED_PHY:
1008 /* Data contact detection enable */
1009 ulpi_write(phy, 0x10, 0x85);
1016 static void msm_chg_block_on(struct msm_otg *motg)
1018 struct usb_phy *phy = &motg->phy;
1019 u32 func_ctrl, chg_det;
1021 /* put the controller in non-driving mode */
1022 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1023 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1024 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1025 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1027 switch (motg->pdata->phy_type) {
1028 case CI_45NM_INTEGRATED_PHY:
1029 chg_det = ulpi_read(phy, 0x34);
1030 /* control chg block via ULPI */
1031 chg_det &= ~(1 << 3);
1032 ulpi_write(phy, chg_det, 0x34);
1033 /* Turn on chg detect block */
1034 chg_det &= ~(1 << 1);
1035 ulpi_write(phy, chg_det, 0x34);
1038 case SNPS_28NM_INTEGRATED_PHY:
1039 /* Clear charger detecting control bits */
1040 ulpi_write(phy, 0x3F, 0x86);
1041 /* Clear alt interrupt latch and enable bits */
1042 ulpi_write(phy, 0x1F, 0x92);
1043 ulpi_write(phy, 0x1F, 0x95);
1051 static void msm_chg_block_off(struct msm_otg *motg)
1053 struct usb_phy *phy = &motg->phy;
1054 u32 func_ctrl, chg_det;
1056 switch (motg->pdata->phy_type) {
1057 case CI_45NM_INTEGRATED_PHY:
1058 chg_det = ulpi_read(phy, 0x34);
1059 /* Turn off charger block */
1060 chg_det |= ~(1 << 1);
1061 ulpi_write(phy, chg_det, 0x34);
1063 case SNPS_28NM_INTEGRATED_PHY:
1064 /* Clear charger detecting control bits */
1065 ulpi_write(phy, 0x3F, 0x86);
1066 /* Clear alt interrupt latch and enable bits */
1067 ulpi_write(phy, 0x1F, 0x92);
1068 ulpi_write(phy, 0x1F, 0x95);
1074 /* put the controller in normal mode */
1075 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
1076 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1077 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1078 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
1081 #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1082 #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1083 #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1084 #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1085 static void msm_chg_detect_work(struct work_struct *w)
1087 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1088 struct usb_phy *phy = &motg->phy;
1089 bool is_dcd, tmout, vout;
1090 unsigned long delay;
1092 dev_dbg(phy->dev, "chg detection work\n");
1093 switch (motg->chg_state) {
1094 case USB_CHG_STATE_UNDEFINED:
1095 pm_runtime_get_sync(phy->dev);
1096 msm_chg_block_on(motg);
1097 msm_chg_enable_dcd(motg);
1098 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1099 motg->dcd_retries = 0;
1100 delay = MSM_CHG_DCD_POLL_TIME;
1102 case USB_CHG_STATE_WAIT_FOR_DCD:
1103 is_dcd = msm_chg_check_dcd(motg);
1104 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1105 if (is_dcd || tmout) {
1106 msm_chg_disable_dcd(motg);
1107 msm_chg_enable_primary_det(motg);
1108 delay = MSM_CHG_PRIMARY_DET_TIME;
1109 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1111 delay = MSM_CHG_DCD_POLL_TIME;
1114 case USB_CHG_STATE_DCD_DONE:
1115 vout = msm_chg_check_primary_det(motg);
1117 msm_chg_enable_secondary_det(motg);
1118 delay = MSM_CHG_SECONDARY_DET_TIME;
1119 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1121 motg->chg_type = USB_SDP_CHARGER;
1122 motg->chg_state = USB_CHG_STATE_DETECTED;
1126 case USB_CHG_STATE_PRIMARY_DONE:
1127 vout = msm_chg_check_secondary_det(motg);
1129 motg->chg_type = USB_DCP_CHARGER;
1131 motg->chg_type = USB_CDP_CHARGER;
1132 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1134 case USB_CHG_STATE_SECONDARY_DONE:
1135 motg->chg_state = USB_CHG_STATE_DETECTED;
1136 case USB_CHG_STATE_DETECTED:
1137 msm_chg_block_off(motg);
1138 dev_dbg(phy->dev, "charger = %d\n", motg->chg_type);
1139 schedule_work(&motg->sm_work);
1145 schedule_delayed_work(&motg->chg_work, delay);
1149 * We support OTG, Peripheral only and Host only configurations. In case
1150 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1151 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1152 * enabled when switch is controlled by user and default mode is supplied
1153 * by board file, which can be changed by userspace later.
1155 static void msm_otg_init_sm(struct msm_otg *motg)
1157 struct msm_otg_platform_data *pdata = motg->pdata;
1158 u32 otgsc = readl(USB_OTGSC);
1160 switch (pdata->mode) {
1161 case USB_DR_MODE_OTG:
1162 if (pdata->otg_control == OTG_PHY_CONTROL) {
1163 if (otgsc & OTGSC_ID)
1164 set_bit(ID, &motg->inputs);
1166 clear_bit(ID, &motg->inputs);
1168 if (otgsc & OTGSC_BSV)
1169 set_bit(B_SESS_VLD, &motg->inputs);
1171 clear_bit(B_SESS_VLD, &motg->inputs);
1172 } else if (pdata->otg_control == OTG_USER_CONTROL) {
1173 set_bit(ID, &motg->inputs);
1174 clear_bit(B_SESS_VLD, &motg->inputs);
1177 case USB_DR_MODE_HOST:
1178 clear_bit(ID, &motg->inputs);
1180 case USB_DR_MODE_PERIPHERAL:
1181 set_bit(ID, &motg->inputs);
1182 if (otgsc & OTGSC_BSV)
1183 set_bit(B_SESS_VLD, &motg->inputs);
1185 clear_bit(B_SESS_VLD, &motg->inputs);
1192 static void msm_otg_sm_work(struct work_struct *w)
1194 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1195 struct usb_otg *otg = motg->phy.otg;
1197 switch (otg->state) {
1198 case OTG_STATE_UNDEFINED:
1199 dev_dbg(otg->usb_phy->dev, "OTG_STATE_UNDEFINED state\n");
1200 msm_otg_reset(otg->usb_phy);
1201 msm_otg_init_sm(motg);
1202 otg->state = OTG_STATE_B_IDLE;
1204 case OTG_STATE_B_IDLE:
1205 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_IDLE state\n");
1206 if (!test_bit(ID, &motg->inputs) && otg->host) {
1207 /* disable BSV bit */
1208 writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC);
1209 msm_otg_start_host(otg->usb_phy, 1);
1210 otg->state = OTG_STATE_A_HOST;
1211 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1212 switch (motg->chg_state) {
1213 case USB_CHG_STATE_UNDEFINED:
1214 msm_chg_detect_work(&motg->chg_work.work);
1216 case USB_CHG_STATE_DETECTED:
1217 switch (motg->chg_type) {
1218 case USB_DCP_CHARGER:
1219 msm_otg_notify_charger(motg,
1222 case USB_CDP_CHARGER:
1223 msm_otg_notify_charger(motg,
1225 msm_otg_start_peripheral(otg->usb_phy,
1228 = OTG_STATE_B_PERIPHERAL;
1230 case USB_SDP_CHARGER:
1231 msm_otg_notify_charger(motg, IUNIT);
1232 msm_otg_start_peripheral(otg->usb_phy,
1235 = OTG_STATE_B_PERIPHERAL;
1246 * If charger detection work is pending, decrement
1247 * the pm usage counter to balance with the one that
1248 * is incremented in charger detection work.
1250 if (cancel_delayed_work_sync(&motg->chg_work)) {
1251 pm_runtime_put_sync(otg->usb_phy->dev);
1252 msm_otg_reset(otg->usb_phy);
1254 msm_otg_notify_charger(motg, 0);
1255 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1256 motg->chg_type = USB_INVALID_CHARGER;
1259 if (otg->state == OTG_STATE_B_IDLE)
1260 pm_runtime_put_sync(otg->usb_phy->dev);
1262 case OTG_STATE_B_PERIPHERAL:
1263 dev_dbg(otg->usb_phy->dev, "OTG_STATE_B_PERIPHERAL state\n");
1264 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
1265 !test_bit(ID, &motg->inputs)) {
1266 msm_otg_notify_charger(motg, 0);
1267 msm_otg_start_peripheral(otg->usb_phy, 0);
1268 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1269 motg->chg_type = USB_INVALID_CHARGER;
1270 otg->state = OTG_STATE_B_IDLE;
1271 msm_otg_reset(otg->usb_phy);
1275 case OTG_STATE_A_HOST:
1276 dev_dbg(otg->usb_phy->dev, "OTG_STATE_A_HOST state\n");
1277 if (test_bit(ID, &motg->inputs)) {
1278 msm_otg_start_host(otg->usb_phy, 0);
1279 otg->state = OTG_STATE_B_IDLE;
1280 msm_otg_reset(otg->usb_phy);
1289 static irqreturn_t msm_otg_irq(int irq, void *data)
1291 struct msm_otg *motg = data;
1292 struct usb_phy *phy = &motg->phy;
1295 if (atomic_read(&motg->in_lpm)) {
1296 disable_irq_nosync(irq);
1297 motg->async_int = 1;
1298 pm_runtime_get(phy->dev);
1302 otgsc = readl(USB_OTGSC);
1303 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1306 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
1307 if (otgsc & OTGSC_ID)
1308 set_bit(ID, &motg->inputs);
1310 clear_bit(ID, &motg->inputs);
1311 dev_dbg(phy->dev, "ID set/clear\n");
1312 pm_runtime_get_noresume(phy->dev);
1313 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
1314 if (otgsc & OTGSC_BSV)
1315 set_bit(B_SESS_VLD, &motg->inputs);
1317 clear_bit(B_SESS_VLD, &motg->inputs);
1318 dev_dbg(phy->dev, "BSV set/clear\n");
1319 pm_runtime_get_noresume(phy->dev);
1322 writel(otgsc, USB_OTGSC);
1323 schedule_work(&motg->sm_work);
1327 static int msm_otg_mode_show(struct seq_file *s, void *unused)
1329 struct msm_otg *motg = s->private;
1330 struct usb_otg *otg = motg->phy.otg;
1332 switch (otg->state) {
1333 case OTG_STATE_A_HOST:
1334 seq_puts(s, "host\n");
1336 case OTG_STATE_B_PERIPHERAL:
1337 seq_puts(s, "peripheral\n");
1340 seq_puts(s, "none\n");
1347 static int msm_otg_mode_open(struct inode *inode, struct file *file)
1349 return single_open(file, msm_otg_mode_show, inode->i_private);
1352 static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1353 size_t count, loff_t *ppos)
1355 struct seq_file *s = file->private_data;
1356 struct msm_otg *motg = s->private;
1358 struct usb_otg *otg = motg->phy.otg;
1360 enum usb_dr_mode req_mode;
1362 memset(buf, 0x00, sizeof(buf));
1364 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1369 if (!strncmp(buf, "host", 4)) {
1370 req_mode = USB_DR_MODE_HOST;
1371 } else if (!strncmp(buf, "peripheral", 10)) {
1372 req_mode = USB_DR_MODE_PERIPHERAL;
1373 } else if (!strncmp(buf, "none", 4)) {
1374 req_mode = USB_DR_MODE_UNKNOWN;
1381 case USB_DR_MODE_UNKNOWN:
1382 switch (otg->state) {
1383 case OTG_STATE_A_HOST:
1384 case OTG_STATE_B_PERIPHERAL:
1385 set_bit(ID, &motg->inputs);
1386 clear_bit(B_SESS_VLD, &motg->inputs);
1392 case USB_DR_MODE_PERIPHERAL:
1393 switch (otg->state) {
1394 case OTG_STATE_B_IDLE:
1395 case OTG_STATE_A_HOST:
1396 set_bit(ID, &motg->inputs);
1397 set_bit(B_SESS_VLD, &motg->inputs);
1403 case USB_DR_MODE_HOST:
1404 switch (otg->state) {
1405 case OTG_STATE_B_IDLE:
1406 case OTG_STATE_B_PERIPHERAL:
1407 clear_bit(ID, &motg->inputs);
1417 pm_runtime_get_sync(otg->usb_phy->dev);
1418 schedule_work(&motg->sm_work);
1423 static const struct file_operations msm_otg_mode_fops = {
1424 .open = msm_otg_mode_open,
1426 .write = msm_otg_mode_write,
1427 .llseek = seq_lseek,
1428 .release = single_release,
1431 static struct dentry *msm_otg_dbg_root;
1432 static struct dentry *msm_otg_dbg_mode;
1434 static int msm_otg_debugfs_init(struct msm_otg *motg)
1436 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
1438 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1441 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR,
1442 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1443 if (!msm_otg_dbg_mode) {
1444 debugfs_remove(msm_otg_dbg_root);
1445 msm_otg_dbg_root = NULL;
1452 static void msm_otg_debugfs_cleanup(void)
1454 debugfs_remove(msm_otg_dbg_mode);
1455 debugfs_remove(msm_otg_dbg_root);
1458 static const struct of_device_id msm_otg_dt_match[] = {
1460 .compatible = "qcom,usb-otg-ci",
1461 .data = (void *) CI_45NM_INTEGRATED_PHY
1464 .compatible = "qcom,usb-otg-snps",
1465 .data = (void *) SNPS_28NM_INTEGRATED_PHY
1469 MODULE_DEVICE_TABLE(of, msm_otg_dt_match);
1471 static int msm_otg_vbus_notifier(struct notifier_block *nb, unsigned long event,
1474 struct msm_usb_cable *vbus = container_of(nb, struct msm_usb_cable, nb);
1475 struct msm_otg *motg = container_of(vbus, struct msm_otg, vbus);
1478 set_bit(B_SESS_VLD, &motg->inputs);
1480 clear_bit(B_SESS_VLD, &motg->inputs);
1482 if (test_bit(B_SESS_VLD, &motg->inputs)) {
1483 /* Switch D+/D- lines to Device connector */
1484 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1486 /* Switch D+/D- lines to Hub */
1487 gpiod_set_value_cansleep(motg->switch_gpio, 1);
1490 schedule_work(&motg->sm_work);
1495 static int msm_otg_id_notifier(struct notifier_block *nb, unsigned long event,
1498 struct msm_usb_cable *id = container_of(nb, struct msm_usb_cable, nb);
1499 struct msm_otg *motg = container_of(id, struct msm_otg, id);
1502 clear_bit(ID, &motg->inputs);
1504 set_bit(ID, &motg->inputs);
1506 schedule_work(&motg->sm_work);
1511 static int msm_otg_read_dt(struct platform_device *pdev, struct msm_otg *motg)
1513 struct msm_otg_platform_data *pdata;
1514 struct extcon_dev *ext_id, *ext_vbus;
1515 struct device_node *node = pdev->dev.of_node;
1516 struct property *prop;
1517 int len, ret, words;
1520 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1524 motg->pdata = pdata;
1526 pdata->phy_type = (enum msm_usb_phy_type)of_device_get_match_data(&pdev->dev);
1527 if (!pdata->phy_type)
1530 motg->link_rst = devm_reset_control_get(&pdev->dev, "link");
1531 if (IS_ERR(motg->link_rst))
1532 return PTR_ERR(motg->link_rst);
1534 motg->phy_rst = devm_reset_control_get(&pdev->dev, "phy");
1535 if (IS_ERR(motg->phy_rst))
1536 motg->phy_rst = NULL;
1538 pdata->mode = usb_get_dr_mode(&pdev->dev);
1539 if (pdata->mode == USB_DR_MODE_UNKNOWN)
1540 pdata->mode = USB_DR_MODE_OTG;
1542 pdata->otg_control = OTG_PHY_CONTROL;
1543 if (!of_property_read_u32(node, "qcom,otg-control", &val))
1544 if (val == OTG_PMIC_CONTROL)
1545 pdata->otg_control = val;
1547 if (!of_property_read_u32(node, "qcom,phy-num", &val) && val < 2)
1548 motg->phy_number = val;
1550 motg->vdd_levels[VDD_LEVEL_NONE] = USB_PHY_SUSP_DIG_VOL;
1551 motg->vdd_levels[VDD_LEVEL_MIN] = USB_PHY_VDD_DIG_VOL_MIN;
1552 motg->vdd_levels[VDD_LEVEL_MAX] = USB_PHY_VDD_DIG_VOL_MAX;
1554 if (of_get_property(node, "qcom,vdd-levels", &len) &&
1555 len == sizeof(tmp)) {
1556 of_property_read_u32_array(node, "qcom,vdd-levels",
1557 tmp, len / sizeof(*tmp));
1558 motg->vdd_levels[VDD_LEVEL_NONE] = tmp[VDD_LEVEL_NONE];
1559 motg->vdd_levels[VDD_LEVEL_MIN] = tmp[VDD_LEVEL_MIN];
1560 motg->vdd_levels[VDD_LEVEL_MAX] = tmp[VDD_LEVEL_MAX];
1563 motg->manual_pullup = of_property_read_bool(node, "qcom,manual-pullup");
1565 motg->switch_gpio = devm_gpiod_get_optional(&pdev->dev, "switch",
1567 if (IS_ERR(motg->switch_gpio))
1568 return PTR_ERR(motg->switch_gpio);
1570 ext_id = ERR_PTR(-ENODEV);
1571 ext_vbus = ERR_PTR(-ENODEV);
1572 if (of_property_read_bool(node, "extcon")) {
1574 /* Each one of them is not mandatory */
1575 ext_vbus = extcon_get_edev_by_phandle(&pdev->dev, 0);
1576 if (IS_ERR(ext_vbus) && PTR_ERR(ext_vbus) != -ENODEV)
1577 return PTR_ERR(ext_vbus);
1579 ext_id = extcon_get_edev_by_phandle(&pdev->dev, 1);
1580 if (IS_ERR(ext_id) && PTR_ERR(ext_id) != -ENODEV)
1581 return PTR_ERR(ext_id);
1584 if (!IS_ERR(ext_vbus)) {
1585 motg->vbus.extcon = ext_vbus;
1586 motg->vbus.nb.notifier_call = msm_otg_vbus_notifier;
1587 ret = extcon_register_notifier(ext_vbus, EXTCON_USB,
1590 dev_err(&pdev->dev, "register VBUS notifier failed\n");
1594 ret = extcon_get_cable_state_(ext_vbus, EXTCON_USB);
1596 set_bit(B_SESS_VLD, &motg->inputs);
1598 clear_bit(B_SESS_VLD, &motg->inputs);
1601 if (!IS_ERR(ext_id)) {
1602 motg->id.extcon = ext_id;
1603 motg->id.nb.notifier_call = msm_otg_id_notifier;
1604 ret = extcon_register_notifier(ext_id, EXTCON_USB_HOST,
1607 dev_err(&pdev->dev, "register ID notifier failed\n");
1608 extcon_unregister_notifier(motg->vbus.extcon,
1609 EXTCON_USB, &motg->vbus.nb);
1613 ret = extcon_get_cable_state_(ext_id, EXTCON_USB_HOST);
1615 clear_bit(ID, &motg->inputs);
1617 set_bit(ID, &motg->inputs);
1620 prop = of_find_property(node, "qcom,phy-init-sequence", &len);
1624 words = len / sizeof(u32);
1626 if (words >= ULPI_EXT_VENDOR_SPECIFIC) {
1627 dev_warn(&pdev->dev, "Too big PHY init sequence %d\n", words);
1631 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
1632 if (!pdata->phy_init_seq)
1635 ret = of_property_read_u32_array(node, "qcom,phy-init-sequence",
1636 pdata->phy_init_seq, words);
1638 pdata->phy_init_sz = words;
1643 static int msm_otg_reboot_notify(struct notifier_block *this,
1644 unsigned long code, void *unused)
1646 struct msm_otg *motg = container_of(this, struct msm_otg, reboot);
1649 * Ensure that D+/D- lines are routed to uB connector, so
1650 * we could load bootloader/kernel at next reboot
1652 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1656 static void msm_otg_bus_vote(struct msm_otg *motg, enum usb_bus_vote vote)
1660 if (motg->bus_perf_client) {
1661 ret = msm_bus_scale_client_update_request(
1662 motg->bus_perf_client, vote);
1664 dev_err(motg->phy.dev, "%s: Failed to vote (%d)\n"
1665 "for bus bw %d\n", __func__, vote, ret);
1669 static int msm_otg_probe(struct platform_device *pdev)
1671 struct regulator_bulk_data regs[2];
1673 struct device_node *np = pdev->dev.of_node;
1674 struct msm_otg_platform_data *pdata;
1675 struct resource *res;
1676 struct msm_otg *motg;
1677 struct usb_phy *phy;
1678 void __iomem *phy_select;
1680 motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL);
1684 motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
1690 phy->dev = &pdev->dev;
1691 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
1692 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
1694 motg->clk = devm_clk_get(&pdev->dev, np ? "core" : "usb_hs_clk");
1695 if (IS_ERR(motg->clk)) {
1696 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
1697 return PTR_ERR(motg->clk);
1701 * If USB Core is running its protocol engine based on CORE CLK,
1702 * CORE CLK must be running at >55Mhz for correct HSUSB
1703 * operation and USB core cannot tolerate frequency changes on
1706 motg->pclk = devm_clk_get(&pdev->dev, np ? "iface" : "usb_hs_pclk");
1707 if (IS_ERR(motg->pclk)) {
1708 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
1709 return PTR_ERR(motg->pclk);
1713 * USB core clock is not present on all MSM chips. This
1714 * clock is introduced to remove the dependency on AXI
1717 motg->core_clk = devm_clk_get(&pdev->dev,
1718 np ? "alt_core" : "usb_hs_core_clk");
1720 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1723 motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1727 pdata = dev_get_platdata(&pdev->dev);
1731 ret = msm_otg_read_dt(pdev, motg);
1737 * NOTE: The PHYs can be multiplexed between the chipidea controller
1738 * and the dwc3 controller, using a single bit. It is important that
1739 * the dwc3 driver does not set this bit in an incompatible way.
1741 if (motg->phy_number) {
1742 phy_select = devm_ioremap_nocache(&pdev->dev, USB2_PHY_SEL, 4);
1745 goto unregister_extcon;
1747 /* Enable second PHY with the OTG port */
1748 writel(0x1, phy_select);
1751 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
1753 motg->irq = platform_get_irq(pdev, 0);
1754 if (motg->irq < 0) {
1755 dev_err(&pdev->dev, "platform_get_irq failed\n");
1757 goto unregister_extcon;
1760 regs[0].supply = "v3p3";
1761 regs[1].supply = "v1p8";
1763 ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs);
1765 dev_err(&pdev->dev, "no v3p3 or v1p8\n");
1766 goto unregister_extcon;
1769 motg->v3p3 = regs[0].consumer;
1770 motg->v1p8 = regs[1].consumer;
1772 motg->vddcx = devm_regulator_get_optional(motg->phy.dev, "vddcx");
1773 if (IS_ERR(motg->vddcx))
1774 dev_info(&pdev->dev, "no vddcx\n");
1776 clk_set_rate(motg->clk, 60000000);
1778 clk_prepare_enable(motg->clk);
1779 clk_prepare_enable(motg->pclk);
1781 if (!IS_ERR(motg->core_clk))
1782 clk_prepare_enable(motg->core_clk);
1784 ret = msm_hsusb_init_vddcx(motg, 1);
1786 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1790 ret = msm_hsusb_ldo_init(motg, 1);
1792 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1795 ret = msm_hsusb_ldo_set_mode(motg, 1);
1797 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1801 writel(0, USB_USBINTR);
1802 writel(0, USB_OTGSC);
1804 ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED,
1807 dev_err(&pdev->dev, "request irq failed\n");
1811 phy->init = msm_phy_init;
1812 phy->set_power = msm_otg_set_power;
1813 phy->notify_disconnect = msm_phy_notify_disconnect;
1814 phy->type = USB_PHY_TYPE_USB2;
1816 phy->io_ops = &msm_otg_io_ops;
1818 phy->otg->usb_phy = &motg->phy;
1819 phy->otg->set_host = msm_otg_set_host;
1820 phy->otg->set_peripheral = msm_otg_set_peripheral;
1824 ret = usb_add_phy_dev(&motg->phy);
1826 dev_err(&pdev->dev, "usb_add_phy failed\n");
1830 motg->pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
1831 if (!motg->pdata->bus_scale_table)
1832 dev_dbg(&pdev->dev, "bus scaling is disabled\n");
1834 motg->bus_perf_client =
1835 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
1836 if (!motg->bus_perf_client)
1837 dev_err(motg->phy.dev, "%s: Failed to register BUS\n"
1838 "scaling client!!\n", __func__);
1840 /* Hack to max out usb performace */
1841 msm_otg_bus_vote(motg, USB_MAX_PERF_VOTE);
1843 platform_set_drvdata(pdev, motg);
1844 device_init_wakeup(&pdev->dev, 1);
1846 if (motg->pdata->mode == USB_DR_MODE_OTG &&
1847 motg->pdata->otg_control == OTG_USER_CONTROL) {
1848 ret = msm_otg_debugfs_init(motg);
1850 dev_dbg(&pdev->dev, "Can not create mode change file\n");
1853 if (test_bit(B_SESS_VLD, &motg->inputs)) {
1854 /* Switch D+/D- lines to Device connector */
1855 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1857 /* Switch D+/D- lines to Hub */
1858 gpiod_set_value_cansleep(motg->switch_gpio, 1);
1861 motg->reboot.notifier_call = msm_otg_reboot_notify;
1862 register_reboot_notifier(&motg->reboot);
1864 pm_runtime_set_active(&pdev->dev);
1869 msm_hsusb_ldo_init(motg, 0);
1871 msm_hsusb_init_vddcx(motg, 0);
1873 clk_disable_unprepare(motg->pclk);
1874 clk_disable_unprepare(motg->clk);
1875 if (!IS_ERR(motg->core_clk))
1876 clk_disable_unprepare(motg->core_clk);
1878 extcon_unregister_notifier(motg->id.extcon,
1879 EXTCON_USB_HOST, &motg->id.nb);
1880 extcon_unregister_notifier(motg->vbus.extcon,
1881 EXTCON_USB, &motg->vbus.nb);
1886 static int msm_otg_remove(struct platform_device *pdev)
1888 struct msm_otg *motg = platform_get_drvdata(pdev);
1889 struct usb_phy *phy = &motg->phy;
1892 if (phy->otg->host || phy->otg->gadget)
1895 unregister_reboot_notifier(&motg->reboot);
1898 * Ensure that D+/D- lines are routed to uB connector, so
1899 * we could load bootloader/kernel at next reboot
1901 gpiod_set_value_cansleep(motg->switch_gpio, 0);
1903 extcon_unregister_notifier(motg->id.extcon, EXTCON_USB_HOST, &motg->id.nb);
1904 extcon_unregister_notifier(motg->vbus.extcon, EXTCON_USB, &motg->vbus.nb);
1906 msm_otg_debugfs_cleanup();
1907 cancel_delayed_work_sync(&motg->chg_work);
1908 cancel_work_sync(&motg->sm_work);
1910 pm_runtime_resume(&pdev->dev);
1912 device_init_wakeup(&pdev->dev, 0);
1913 pm_runtime_disable(&pdev->dev);
1915 usb_remove_phy(phy);
1916 disable_irq(motg->irq);
1917 msm_bus_scale_unregister_client(motg->bus_perf_client);
1920 * Put PHY in low power mode.
1922 ulpi_read(phy, 0x14);
1923 ulpi_write(phy, 0x08, 0x09);
1925 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
1926 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
1927 if (readl(USB_PORTSC) & PORTSC_PHCD)
1932 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
1933 dev_err(phy->dev, "Unable to suspend PHY\n");
1935 clk_disable_unprepare(motg->pclk);
1936 clk_disable_unprepare(motg->clk);
1937 if (!IS_ERR(motg->core_clk))
1938 clk_disable_unprepare(motg->core_clk);
1939 msm_hsusb_ldo_init(motg, 0);
1941 pm_runtime_set_suspended(&pdev->dev);
1947 static int msm_otg_runtime_idle(struct device *dev)
1949 struct msm_otg *motg = dev_get_drvdata(dev);
1950 struct usb_otg *otg = motg->phy.otg;
1952 dev_dbg(dev, "OTG runtime idle\n");
1955 * It is observed some times that a spurious interrupt
1956 * comes when PHY is put into LPM immediately after PHY reset.
1957 * This 1 sec delay also prevents entering into LPM immediately
1958 * after asynchronous interrupt.
1960 if (otg->state != OTG_STATE_UNDEFINED)
1961 pm_schedule_suspend(dev, 1000);
1966 static int msm_otg_runtime_suspend(struct device *dev)
1968 struct msm_otg *motg = dev_get_drvdata(dev);
1970 dev_dbg(dev, "OTG runtime suspend\n");
1971 return msm_otg_suspend(motg);
1974 static int msm_otg_runtime_resume(struct device *dev)
1976 struct msm_otg *motg = dev_get_drvdata(dev);
1978 dev_dbg(dev, "OTG runtime resume\n");
1979 return msm_otg_resume(motg);
1983 #ifdef CONFIG_PM_SLEEP
1984 static int msm_otg_pm_suspend(struct device *dev)
1986 struct msm_otg *motg = dev_get_drvdata(dev);
1988 dev_dbg(dev, "OTG PM suspend\n");
1989 return msm_otg_suspend(motg);
1992 static int msm_otg_pm_resume(struct device *dev)
1994 struct msm_otg *motg = dev_get_drvdata(dev);
1997 dev_dbg(dev, "OTG PM resume\n");
1999 ret = msm_otg_resume(motg);
2004 * Runtime PM Documentation recommends bringing the
2005 * device to full powered state upon resume.
2007 pm_runtime_disable(dev);
2008 pm_runtime_set_active(dev);
2009 pm_runtime_enable(dev);
2015 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
2016 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2017 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2018 msm_otg_runtime_idle)
2021 static struct platform_driver msm_otg_driver = {
2022 .probe = msm_otg_probe,
2023 .remove = msm_otg_remove,
2025 .name = DRIVER_NAME,
2026 .pm = &msm_otg_dev_pm_ops,
2027 .of_match_table = msm_otg_dt_match,
2031 module_platform_driver(msm_otg_driver);
2033 MODULE_LICENSE("GPL v2");
2034 MODULE_DESCRIPTION("MSM USB transceiver driver");