19 #include <linux/module.h>
20 #include <linux/device.h>
23 #include <linux/slab.h>
45 #define MSM_USB_BASE (motg->regs)
46 #define DRIVER_NAME "msm_otg"
48 #define ULPI_IO_TIMEOUT_USEC (10 * 1000)
50 #define USB_PHY_3P3_VOL_MIN 3050000
51 #define USB_PHY_3P3_VOL_MAX 3300000
52 #define USB_PHY_3P3_HPM_LOAD 50000
53 #define USB_PHY_3P3_LPM_LOAD 4000
55 #define USB_PHY_1P8_VOL_MIN 1800000
56 #define USB_PHY_1P8_VOL_MAX 1800000
57 #define USB_PHY_1P8_HPM_LOAD 50000
58 #define USB_PHY_1P8_LPM_LOAD 4000
60 #define USB_PHY_VDD_DIG_VOL_MIN 1000000
61 #define USB_PHY_VDD_DIG_VOL_MAX 1320000
67 static int msm_hsusb_init_vddcx(
struct msm_otg *motg,
int init)
73 if (IS_ERR(hsusb_vddcx)) {
74 dev_err(motg->
phy.dev,
"unable to get hsusb vddcx\n");
75 return PTR_ERR(hsusb_vddcx);
82 dev_err(motg->
phy.dev,
"unable to set the voltage "
90 dev_err(motg->
phy.dev,
"unable to enable hsusb vddcx\n");
97 dev_err(motg->
phy.dev,
"unable to set the voltage "
101 dev_err(motg->
phy.dev,
"unable to disable hsusb vddcx\n");
109 static int msm_hsusb_ldo_init(
struct msm_otg *motg,
int init)
115 if (IS_ERR(hsusb_3p3)) {
116 dev_err(motg->
phy.dev,
"unable to get hsusb 3p3\n");
117 return PTR_ERR(hsusb_3p3);
123 dev_err(motg->
phy.dev,
"unable to set voltage level "
129 dev_err(motg->
phy.dev,
"unable to enable the hsusb 3p3\n");
133 if (IS_ERR(hsusb_1p8)) {
134 dev_err(motg->
phy.dev,
"unable to get hsusb 1p8\n");
135 rc = PTR_ERR(hsusb_1p8);
141 dev_err(motg->
phy.dev,
"unable to set voltage level "
147 dev_err(motg->
phy.dev,
"unable to enable the hsusb 1p8\n");
164 #ifdef CONFIG_PM_SLEEP
165 #define USB_PHY_SUSP_DIG_VOL 500000
166 static int msm_hsusb_config_vddcx(
int high)
175 min_vol = USB_PHY_SUSP_DIG_VOL;
179 pr_err(
"%s: unable to set the voltage for regulator "
180 "HSUSB_VDDCX\n", __func__);
184 pr_debug(
"%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
190 static int msm_hsusb_ldo_set_mode(
int on)
194 if (!hsusb_1p8 || IS_ERR(hsusb_1p8)) {
195 pr_err(
"%s: HSUSB_1p8 is not initialized\n", __func__);
199 if (!hsusb_3p3 || IS_ERR(hsusb_3p3)) {
200 pr_err(
"%s: HSUSB_3p3 is not initialized\n", __func__);
208 pr_err(
"%s: Unable to set HPM of the regulator "
209 "HSUSB_1p8\n", __func__);
215 pr_err(
"%s: Unable to set HPM of the regulator "
216 "HSUSB_3p3\n", __func__);
225 pr_err(
"%s: Unable to set LPM of the regulator "
226 "HSUSB_1p8\n", __func__);
230 pr_err(
"%s: Unable to set LPM of the regulator "
231 "HSUSB_3p3\n", __func__);
234 pr_debug(
"reg (%s)\n", on ?
"HPM" :
"LPM");
235 return ret < 0 ? ret : 0;
256 dev_err(phy->
dev,
"ulpi_read: timeout %08x\n",
293 static void ulpi_init(
struct msm_otg *motg)
301 while (seq[0] >= 0) {
302 dev_vdbg(motg->
phy.dev,
"ulpi: write 0x%02x to 0x%02x\n",
304 ulpi_write(&motg->
phy, seq[0], seq[1]);
309 static int msm_otg_link_clk_reset(
struct msm_otg *motg,
bool assert)
316 dev_err(motg->
phy.dev,
"usb hs_clk assert failed\n");
320 dev_err(motg->
phy.dev,
"usb hs_clk deassert failed\n");
325 static int msm_otg_phy_clk_reset(
struct msm_otg *motg)
331 dev_err(motg->
phy.dev,
"usb phy clk assert failed\n");
337 dev_err(motg->
phy.dev,
"usb phy clk deassert failed\n");
341 static int msm_otg_phy_reset(
struct msm_otg *motg)
347 ret = msm_otg_link_clk_reset(motg, 1);
350 ret = msm_otg_phy_clk_reset(motg);
353 ret = msm_otg_link_clk_reset(motg, 0);
360 for (retries = 3; retries > 0; retries--) {
365 ret = msm_otg_phy_clk_reset(motg);
373 ret = msm_otg_phy_clk_reset(motg);
377 for (retries = 3; retries > 0; retries--) {
381 ret = msm_otg_phy_clk_reset(motg);
392 #define LINK_RESET_TIMEOUT_USEC (250 * 1000)
393 static int msm_otg_reset(
struct usb_phy *phy)
402 ret = msm_otg_phy_reset(motg);
445 #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
446 #define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
448 #ifdef CONFIG_PM_SLEEP
449 static int msm_otg_suspend(
struct msm_otg *motg)
452 struct usb_bus *
bus = phy->
otg->host;
478 ulpi_read(phy, 0x14);
480 ulpi_write(phy, 0x01, 0x30);
481 ulpi_write(phy, 0x08, 0x09);
527 msm_hsusb_ldo_set_mode(0);
528 msm_hsusb_config_vddcx(0);
531 if (device_may_wakeup(phy->
dev))
532 enable_irq_wake(motg->
irq);
544 static int msm_otg_resume(
struct msm_otg *motg)
547 struct usb_bus *
bus = phy->
otg->host;
564 msm_hsusb_ldo_set_mode(1);
565 msm_hsusb_config_vddcx(1);
579 goto skip_phy_resume;
596 "Re-plugin the cable\n");
601 if (device_may_wakeup(phy->
dev))
602 disable_irq_wake(motg->
irq);
604 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->
flags);
610 pm_runtime_put(phy->
dev);
614 dev_info(phy->
dev,
"USB exited from low power mode\n");
620 static void msm_otg_notify_charger(
struct msm_otg *motg,
unsigned mA)
626 dev_info(motg->
phy.dev,
"Avail curr from USB = %u\n", mA);
630 static int msm_otg_set_power(
struct usb_phy *phy,
unsigned mA)
642 msm_otg_notify_charger(motg, mA);
647 static void msm_otg_start_host(
struct usb_phy *phy,
int on)
656 hcd = bus_to_hcd(phy->
otg->host);
686 static int msm_otg_set_host(
struct usb_otg *otg,
struct usb_bus *
host)
696 dev_info(otg->
phy->dev,
"Host mode is not supported\n");
702 pm_runtime_get_sync(otg->
phy->dev);
703 msm_otg_start_host(otg->
phy, 0);
714 hcd = bus_to_hcd(host);
715 hcd->power_budget = motg->
pdata->power_budget;
718 dev_dbg(otg->
phy->dev,
"host driver registered w/ tranceiver\n");
725 pm_runtime_get_sync(otg->
phy->dev);
732 static void msm_otg_start_peripheral(
struct usb_phy *phy,
int on)
737 if (!phy->
otg->gadget)
749 usb_gadget_vbus_connect(phy->
otg->gadget);
752 usb_gadget_vbus_disconnect(phy->
otg->gadget);
759 static int msm_otg_set_peripheral(
struct usb_otg *otg,
769 dev_info(otg->
phy->dev,
"Peripheral mode is not supported\n");
775 pm_runtime_get_sync(otg->
phy->dev);
776 msm_otg_start_peripheral(otg->
phy, 0);
787 dev_dbg(otg->
phy->dev,
"peripheral driver registered w/ tranceiver\n");
794 pm_runtime_get_sync(otg->
phy->dev);
801 static bool msm_chg_check_secondary_det(
struct msm_otg *motg)
807 switch (motg->
pdata->phy_type) {
809 chg_det = ulpi_read(phy, 0x34);
810 ret = chg_det & (1 << 4);
813 chg_det = ulpi_read(phy, 0x87);
822 static void msm_chg_enable_secondary_det(
struct msm_otg *motg)
827 switch (motg->
pdata->phy_type) {
829 chg_det = ulpi_read(phy, 0x34);
831 chg_det |= ~(1 << 1);
832 ulpi_write(phy, chg_det, 0x34);
835 chg_det &= ~(1 << 3);
836 ulpi_write(phy, chg_det, 0x34);
838 chg_det &= ~(1 << 2);
839 ulpi_write(phy, chg_det, 0x34);
841 chg_det &= ~(1 << 1);
842 ulpi_write(phy, chg_det, 0x34);
845 chg_det &= ~(1 << 0);
846 ulpi_write(phy, chg_det, 0x34);
853 ulpi_write(phy, 0x8, 0x85);
854 ulpi_write(phy, 0x2, 0x85);
855 ulpi_write(phy, 0x1, 0x85);
862 static bool msm_chg_check_primary_det(
struct msm_otg *motg)
868 switch (motg->
pdata->phy_type) {
870 chg_det = ulpi_read(phy, 0x34);
871 ret = chg_det & (1 << 4);
874 chg_det = ulpi_read(phy, 0x87);
883 static void msm_chg_enable_primary_det(
struct msm_otg *motg)
888 switch (motg->
pdata->phy_type) {
890 chg_det = ulpi_read(phy, 0x34);
892 chg_det &= ~(1 << 0);
893 ulpi_write(phy, chg_det, 0x34);
900 ulpi_write(phy, 0x2, 0x85);
901 ulpi_write(phy, 0x1, 0x85);
908 static bool msm_chg_check_dcd(
struct msm_otg *motg)
914 switch (motg->
pdata->phy_type) {
916 line_state = ulpi_read(phy, 0x15);
917 ret = !(line_state & 1);
920 line_state = ulpi_read(phy, 0x87);
921 ret = line_state & 2;
929 static void msm_chg_disable_dcd(
struct msm_otg *motg)
934 switch (motg->
pdata->phy_type) {
936 chg_det = ulpi_read(phy, 0x34);
937 chg_det &= ~(1 << 5);
938 ulpi_write(phy, chg_det, 0x34);
941 ulpi_write(phy, 0x10, 0
x86);
948 static void msm_chg_enable_dcd(
struct msm_otg *motg)
953 switch (motg->
pdata->phy_type) {
955 chg_det = ulpi_read(phy, 0x34);
958 ulpi_write(phy, chg_det, 0x34);
962 ulpi_write(phy, 0x10, 0x85);
969 static void msm_chg_block_on(
struct msm_otg *motg)
972 u32 func_ctrl, chg_det;
980 switch (motg->
pdata->phy_type) {
982 chg_det = ulpi_read(phy, 0x34);
984 chg_det &= ~(1 << 3);
985 ulpi_write(phy, chg_det, 0x34);
987 chg_det &= ~(1 << 1);
988 ulpi_write(phy, chg_det, 0x34);
993 ulpi_write(phy, 0x3F, 0
x86);
995 ulpi_write(phy, 0x1F, 0x92);
996 ulpi_write(phy, 0x1F, 0x95);
1004 static void msm_chg_block_off(
struct msm_otg *motg)
1007 u32 func_ctrl, chg_det;
1009 switch (motg->
pdata->phy_type) {
1011 chg_det = ulpi_read(phy, 0x34);
1013 chg_det |= ~(1 << 1);
1014 ulpi_write(phy, chg_det, 0x34);
1018 ulpi_write(phy, 0x3F, 0
x86);
1020 ulpi_write(phy, 0x1F, 0x92);
1021 ulpi_write(phy, 0x1F, 0x95);
1034 #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000)
1035 #define MSM_CHG_DCD_MAX_RETRIES 6
1036 #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000)
1037 #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000)
1042 bool is_dcd, tmout, vout;
1043 unsigned long delay;
1048 pm_runtime_get_sync(phy->
dev);
1049 msm_chg_block_on(motg);
1050 msm_chg_enable_dcd(motg);
1056 is_dcd = msm_chg_check_dcd(motg);
1058 if (is_dcd || tmout) {
1059 msm_chg_disable_dcd(motg);
1060 msm_chg_enable_primary_det(motg);
1068 vout = msm_chg_check_primary_det(motg);
1070 msm_chg_enable_secondary_det(motg);
1080 vout = msm_chg_check_secondary_det(motg);
1090 msm_chg_block_off(motg);
1108 static void msm_otg_init_sm(
struct msm_otg *motg)
1113 switch (pdata->
mode) {
1157 switch (otg->
phy->state) {
1159 dev_dbg(otg->
phy->dev,
"OTG_STATE_UNDEFINED state\n");
1160 msm_otg_reset(otg->
phy);
1161 msm_otg_init_sm(motg);
1165 dev_dbg(otg->
phy->dev,
"OTG_STATE_B_IDLE state\n");
1169 msm_otg_start_host(otg->
phy, 1);
1174 msm_chg_detect_work(&motg->
chg_work.work);
1179 msm_otg_notify_charger(motg,
1183 msm_otg_notify_charger(motg,
1185 msm_otg_start_peripheral(otg->
phy, 1);
1190 msm_otg_notify_charger(motg,
IUNIT);
1191 msm_otg_start_peripheral(otg->
phy, 1);
1209 pm_runtime_put_sync(otg->
phy->dev);
1210 msm_otg_reset(otg->
phy);
1212 msm_otg_notify_charger(motg, 0);
1216 pm_runtime_put_sync(otg->
phy->dev);
1219 dev_dbg(otg->
phy->dev,
"OTG_STATE_B_PERIPHERAL state\n");
1222 msm_otg_notify_charger(motg, 0);
1223 msm_otg_start_peripheral(otg->
phy, 0);
1227 msm_otg_reset(otg->
phy);
1232 dev_dbg(otg->
phy->dev,
"OTG_STATE_A_HOST state\n");
1234 msm_otg_start_host(otg->
phy, 0);
1236 msm_otg_reset(otg->
phy);
1254 pm_runtime_get(phy->
dev);
1263 if (otgsc & OTGSC_ID)
1268 pm_runtime_get_noresume(phy->
dev);
1275 pm_runtime_get_noresume(phy->
dev);
1288 switch (otg->
phy->state) {
1308 static ssize_t msm_otg_mode_write(
struct file *
file,
const char __user *ubuf,
1309 size_t count, loff_t *ppos)
1318 memset(buf, 0x00,
sizeof(buf));
1325 if (!
strncmp(buf,
"host", 4)) {
1327 }
else if (!
strncmp(buf,
"peripheral", 10)) {
1329 }
else if (!
strncmp(buf,
"none", 4)) {
1338 switch (otg->
phy->state) {
1349 switch (otg->
phy->state) {
1360 switch (otg->
phy->state) {
1373 pm_runtime_get_sync(otg->
phy->dev);
1380 .open = msm_otg_mode_open,
1382 .write = msm_otg_mode_write,
1387 static struct dentry *msm_otg_dbg_root;
1388 static struct dentry *msm_otg_dbg_mode;
1390 static int msm_otg_debugfs_init(
struct msm_otg *motg)
1394 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
1398 msm_otg_dbg_root, motg, &msm_otg_mode_fops);
1399 if (!msm_otg_dbg_mode) {
1401 msm_otg_dbg_root =
NULL;
1408 static void msm_otg_debugfs_cleanup(
void)
1422 if (!pdev->
dev.platform_data) {
1423 dev_err(&pdev->
dev,
"No platform data given. Bailing out\n");
1429 dev_err(&pdev->
dev,
"unable to allocate msm_otg\n");
1434 if (!motg->
phy.otg) {
1435 dev_err(&pdev->
dev,
"unable to allocate msm_otg\n");
1439 motg->
pdata = pdev->
dev.platform_data;
1445 dev_err(&pdev->
dev,
"failed to get usb_phy_clk\n");
1451 if (IS_ERR(motg->
clk)) {
1452 dev_err(&pdev->
dev,
"failed to get usb_hs_clk\n");
1453 ret = PTR_ERR(motg->
clk);
1454 goto put_phy_reset_clk;
1465 if (motg->
pdata->pclk_src_name) {
1467 motg->
pdata->pclk_src_name);
1477 if (IS_ERR(motg->
pclk)) {
1478 dev_err(&pdev->
dev,
"failed to get usb_hs_pclk\n");
1479 ret = PTR_ERR(motg->
pclk);
1494 dev_err(&pdev->
dev,
"failed to get platform resource mem\n");
1509 dev_err(&pdev->
dev,
"platform_get_irq failed\n");
1517 ret = msm_hsusb_init_vddcx(motg, 1);
1519 dev_err(&pdev->
dev,
"hsusb vddcx configuration failed\n");
1523 ret = msm_hsusb_ldo_init(motg, 1);
1525 dev_err(&pdev->
dev,
"hsusb vreg configuration failed\n");
1528 ret = msm_hsusb_ldo_set_mode(1);
1530 dev_err(&pdev->
dev,
"hsusb vreg enable failed\n");
1549 phy->
init = msm_otg_reset;
1552 phy->
io_ops = &msm_otg_io_ops;
1554 phy->
otg->phy = &motg->
phy;
1555 phy->
otg->set_host = msm_otg_set_host;
1556 phy->
otg->set_peripheral = msm_otg_set_peripheral;
1564 platform_set_drvdata(pdev, motg);
1569 ret = msm_otg_debugfs_init(motg);
1575 pm_runtime_set_active(&pdev->
dev);
1585 msm_hsusb_ldo_init(motg, 0);
1587 msm_hsusb_init_vddcx(motg, 0);
1611 struct msm_otg *motg = platform_get_drvdata(pdev);
1615 if (phy->
otg->host || phy->
otg->gadget)
1618 msm_otg_debugfs_cleanup();
1622 pm_runtime_resume(&pdev->
dev);
1625 pm_runtime_disable(&pdev->
dev);
1633 ulpi_read(phy, 0x14);
1634 ulpi_write(phy, 0x08, 0x09);
1644 dev_err(phy->
dev,
"Unable to suspend PHY\n");
1654 msm_hsusb_ldo_init(motg, 0);
1657 pm_runtime_set_suspended(&pdev->
dev);
1671 #ifdef CONFIG_PM_RUNTIME
1672 static int msm_otg_runtime_idle(
struct device *
dev)
1677 dev_dbg(dev,
"OTG runtime idle\n");
1691 static int msm_otg_runtime_suspend(
struct device *dev)
1695 dev_dbg(dev,
"OTG runtime suspend\n");
1696 return msm_otg_suspend(motg);
1699 static int msm_otg_runtime_resume(
struct device *dev)
1703 dev_dbg(dev,
"OTG runtime resume\n");
1704 return msm_otg_resume(motg);
1708 #ifdef CONFIG_PM_SLEEP
1709 static int msm_otg_pm_suspend(
struct device *dev)
1713 dev_dbg(dev,
"OTG PM suspend\n");
1714 return msm_otg_suspend(motg);
1717 static int msm_otg_pm_resume(
struct device *dev)
1722 dev_dbg(dev,
"OTG PM resume\n");
1724 ret = msm_otg_resume(motg);
1732 pm_runtime_disable(dev);
1733 pm_runtime_set_active(dev);
1741 static const struct dev_pm_ops msm_otg_dev_pm_ops = {
1744 msm_otg_runtime_idle)
1754 .pm = &msm_otg_dev_pm_ops,
1759 static int __init msm_otg_init(
void)
1764 static void __exit msm_otg_exit(
void)