Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1 | /* Copyright (c) 2009-2011, Code Aurora Forum. All rights reserved. |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 2 | * |
| 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. |
| 6 | * |
| 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. |
| 11 | * |
| 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 |
| 15 | * 02110-1301, USA. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/clk.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/ioport.h> |
| 29 | #include <linux/uaccess.h> |
| 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/seq_file.h> |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 32 | #include <linux/pm_runtime.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 33 | |
| 34 | #include <linux/usb.h> |
| 35 | #include <linux/usb/otg.h> |
| 36 | #include <linux/usb/ulpi.h> |
| 37 | #include <linux/usb/gadget.h> |
| 38 | #include <linux/usb/hcd.h> |
| 39 | #include <linux/usb/msm_hsusb.h> |
| 40 | #include <linux/usb/msm_hsusb_hw.h> |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 41 | #include <linux/regulator/consumer.h> |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 42 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 43 | #define MSM_USB_BASE (motg->regs) |
| 44 | #define DRIVER_NAME "msm_otg" |
| 45 | |
| 46 | #define ULPI_IO_TIMEOUT_USEC (10 * 1000) |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 47 | |
| 48 | #define USB_PHY_3P3_VOL_MIN 3050000 /* uV */ |
| 49 | #define USB_PHY_3P3_VOL_MAX 3300000 /* uV */ |
| 50 | #define USB_PHY_3P3_HPM_LOAD 50000 /* uA */ |
| 51 | #define USB_PHY_3P3_LPM_LOAD 4000 /* uA */ |
| 52 | |
| 53 | #define USB_PHY_1P8_VOL_MIN 1800000 /* uV */ |
| 54 | #define USB_PHY_1P8_VOL_MAX 1800000 /* uV */ |
| 55 | #define USB_PHY_1P8_HPM_LOAD 50000 /* uA */ |
| 56 | #define USB_PHY_1P8_LPM_LOAD 4000 /* uA */ |
| 57 | |
| 58 | #define USB_PHY_VDD_DIG_VOL_MIN 1000000 /* uV */ |
| 59 | #define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */ |
| 60 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 61 | static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init) |
| 62 | { |
| 63 | int ret = 0; |
| 64 | |
| 65 | if (init) { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 66 | ret = regulator_set_voltage(motg->vddcx, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 67 | USB_PHY_VDD_DIG_VOL_MIN, |
| 68 | USB_PHY_VDD_DIG_VOL_MAX); |
| 69 | if (ret) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 70 | dev_err(motg->phy.dev, "Cannot set vddcx voltage\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 71 | return ret; |
| 72 | } |
| 73 | |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 74 | ret = regulator_enable(motg->vddcx); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 75 | if (ret) |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 76 | dev_err(motg->phy.dev, "unable to enable hsusb vddcx\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 77 | } else { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 78 | ret = regulator_set_voltage(motg->vddcx, 0, |
Mark Brown | 7b521fc | 2011-05-15 09:55:57 -0700 | [diff] [blame] | 79 | USB_PHY_VDD_DIG_VOL_MAX); |
Mark Brown | e99c430 | 2011-05-15 09:55:58 -0700 | [diff] [blame] | 80 | if (ret) |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 81 | dev_err(motg->phy.dev, "Cannot set vddcx voltage\n"); |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 82 | ret = regulator_disable(motg->vddcx); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 83 | if (ret) |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 84 | dev_err(motg->phy.dev, "unable to disable hsusb vddcx\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
| 90 | static int msm_hsusb_ldo_init(struct msm_otg *motg, int init) |
| 91 | { |
| 92 | int rc = 0; |
| 93 | |
| 94 | if (init) { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 95 | rc = regulator_set_voltage(motg->v3p3, USB_PHY_3P3_VOL_MIN, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 96 | USB_PHY_3P3_VOL_MAX); |
| 97 | if (rc) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 98 | dev_err(motg->phy.dev, "Cannot set v3p3 voltage\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 99 | goto exit; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 100 | } |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 101 | rc = regulator_enable(motg->v3p3); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 102 | if (rc) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 103 | dev_err(motg->phy.dev, "unable to enable the hsusb 3p3\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 104 | goto exit; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 105 | } |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 106 | rc = regulator_set_voltage(motg->v1p8, USB_PHY_1P8_VOL_MIN, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 107 | USB_PHY_1P8_VOL_MAX); |
| 108 | if (rc) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 109 | dev_err(motg->phy.dev, "Cannot set v1p8 voltage\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 110 | goto disable_3p3; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 111 | } |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 112 | rc = regulator_enable(motg->v1p8); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 113 | if (rc) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 114 | dev_err(motg->phy.dev, "unable to enable the hsusb 1p8\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 115 | goto disable_3p3; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 121 | regulator_disable(motg->v1p8); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 122 | disable_3p3: |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 123 | regulator_disable(motg->v3p3); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 124 | exit: |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 125 | return rc; |
| 126 | } |
| 127 | |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 128 | static int msm_hsusb_ldo_set_mode(struct msm_otg *motg, int on) |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 129 | { |
| 130 | int ret = 0; |
| 131 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 132 | if (on) { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 133 | ret = regulator_set_optimum_mode(motg->v1p8, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 134 | USB_PHY_1P8_HPM_LOAD); |
| 135 | if (ret < 0) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 136 | pr_err("Could not set HPM for v1p8\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 137 | return ret; |
| 138 | } |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 139 | ret = regulator_set_optimum_mode(motg->v3p3, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 140 | USB_PHY_3P3_HPM_LOAD); |
| 141 | if (ret < 0) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 142 | pr_err("Could not set HPM for v3p3\n"); |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 143 | regulator_set_optimum_mode(motg->v1p8, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 144 | USB_PHY_1P8_LPM_LOAD); |
| 145 | return ret; |
| 146 | } |
| 147 | } else { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 148 | ret = regulator_set_optimum_mode(motg->v1p8, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 149 | USB_PHY_1P8_LPM_LOAD); |
| 150 | if (ret < 0) |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 151 | pr_err("Could not set LPM for v1p8\n"); |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 152 | ret = regulator_set_optimum_mode(motg->v3p3, |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 153 | USB_PHY_3P3_LPM_LOAD); |
| 154 | if (ret < 0) |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 155 | pr_err("Could not set LPM for v3p3\n"); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | pr_debug("reg (%s)\n", on ? "HPM" : "LPM"); |
| 159 | return ret < 0 ? ret : 0; |
| 160 | } |
| 161 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 162 | static int ulpi_read(struct usb_phy *phy, u32 reg) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 163 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 164 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 165 | int cnt = 0; |
| 166 | |
| 167 | /* initiate read operation */ |
| 168 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), |
| 169 | USB_ULPI_VIEWPORT); |
| 170 | |
| 171 | /* wait for completion */ |
| 172 | while (cnt < ULPI_IO_TIMEOUT_USEC) { |
| 173 | if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN)) |
| 174 | break; |
| 175 | udelay(1); |
| 176 | cnt++; |
| 177 | } |
| 178 | |
| 179 | if (cnt >= ULPI_IO_TIMEOUT_USEC) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 180 | dev_err(phy->dev, "ulpi_read: timeout %08x\n", |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 181 | readl(USB_ULPI_VIEWPORT)); |
| 182 | return -ETIMEDOUT; |
| 183 | } |
| 184 | return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 185 | } |
| 186 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 187 | static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 188 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 189 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 190 | int cnt = 0; |
| 191 | |
| 192 | /* initiate write operation */ |
| 193 | writel(ULPI_RUN | ULPI_WRITE | |
| 194 | ULPI_ADDR(reg) | ULPI_DATA(val), |
| 195 | USB_ULPI_VIEWPORT); |
| 196 | |
| 197 | /* wait for completion */ |
| 198 | while (cnt < ULPI_IO_TIMEOUT_USEC) { |
| 199 | if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN)) |
| 200 | break; |
| 201 | udelay(1); |
| 202 | cnt++; |
| 203 | } |
| 204 | |
| 205 | if (cnt >= ULPI_IO_TIMEOUT_USEC) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 206 | dev_err(phy->dev, "ulpi_write: timeout\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 207 | return -ETIMEDOUT; |
| 208 | } |
| 209 | return 0; |
| 210 | } |
| 211 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 212 | static struct usb_phy_io_ops msm_otg_io_ops = { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 213 | .read = ulpi_read, |
| 214 | .write = ulpi_write, |
| 215 | }; |
| 216 | |
| 217 | static void ulpi_init(struct msm_otg *motg) |
| 218 | { |
| 219 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 220 | int *seq = pdata->phy_init_seq; |
| 221 | |
| 222 | if (!seq) |
| 223 | return; |
| 224 | |
| 225 | while (seq[0] >= 0) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 226 | dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n", |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 227 | seq[0], seq[1]); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 228 | ulpi_write(&motg->phy, seq[0], seq[1]); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 229 | seq += 2; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert) |
| 234 | { |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 235 | int ret = 0; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 236 | |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 237 | if (!motg->pdata->link_clk_reset) |
| 238 | return ret; |
| 239 | |
| 240 | ret = motg->pdata->link_clk_reset(motg->clk, assert); |
| 241 | if (ret) |
| 242 | dev_err(motg->phy.dev, "usb link clk reset %s failed\n", |
| 243 | assert ? "assert" : "deassert"); |
| 244 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 245 | return ret; |
| 246 | } |
| 247 | |
| 248 | static int msm_otg_phy_clk_reset(struct msm_otg *motg) |
| 249 | { |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 250 | int ret = 0; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 251 | |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 252 | if (!motg->pdata->phy_clk_reset) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 253 | return ret; |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 254 | |
| 255 | ret = motg->pdata->phy_clk_reset(motg->phy_reset_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 256 | if (ret) |
Ivan T. Ivanov | 5146d77 | 2013-12-30 13:15:27 -0800 | [diff] [blame] | 257 | dev_err(motg->phy.dev, "usb phy clk reset failed\n"); |
| 258 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static int msm_otg_phy_reset(struct msm_otg *motg) |
| 263 | { |
| 264 | u32 val; |
| 265 | int ret; |
| 266 | int retries; |
| 267 | |
| 268 | ret = msm_otg_link_clk_reset(motg, 1); |
| 269 | if (ret) |
| 270 | return ret; |
| 271 | ret = msm_otg_phy_clk_reset(motg); |
| 272 | if (ret) |
| 273 | return ret; |
| 274 | ret = msm_otg_link_clk_reset(motg, 0); |
| 275 | if (ret) |
| 276 | return ret; |
| 277 | |
| 278 | val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK; |
| 279 | writel(val | PORTSC_PTS_ULPI, USB_PORTSC); |
| 280 | |
| 281 | for (retries = 3; retries > 0; retries--) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 282 | ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM, |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 283 | ULPI_CLR(ULPI_FUNC_CTRL)); |
| 284 | if (!ret) |
| 285 | break; |
| 286 | ret = msm_otg_phy_clk_reset(motg); |
| 287 | if (ret) |
| 288 | return ret; |
| 289 | } |
| 290 | if (!retries) |
| 291 | return -ETIMEDOUT; |
| 292 | |
| 293 | /* This reset calibrates the phy, if the above write succeeded */ |
| 294 | ret = msm_otg_phy_clk_reset(motg); |
| 295 | if (ret) |
| 296 | return ret; |
| 297 | |
| 298 | for (retries = 3; retries > 0; retries--) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 299 | ret = ulpi_read(&motg->phy, ULPI_DEBUG); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 300 | if (ret != -ETIMEDOUT) |
| 301 | break; |
| 302 | ret = msm_otg_phy_clk_reset(motg); |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | } |
| 306 | if (!retries) |
| 307 | return -ETIMEDOUT; |
| 308 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 309 | dev_info(motg->phy.dev, "phy_reset: success\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | #define LINK_RESET_TIMEOUT_USEC (250 * 1000) |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 314 | static int msm_otg_reset(struct usb_phy *phy) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 315 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 316 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 317 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 318 | int cnt = 0; |
| 319 | int ret; |
| 320 | u32 val = 0; |
| 321 | u32 ulpi_val = 0; |
| 322 | |
| 323 | ret = msm_otg_phy_reset(motg); |
| 324 | if (ret) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 325 | dev_err(phy->dev, "phy_reset failed\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | ulpi_init(motg); |
| 330 | |
| 331 | writel(USBCMD_RESET, USB_USBCMD); |
| 332 | while (cnt < LINK_RESET_TIMEOUT_USEC) { |
| 333 | if (!(readl(USB_USBCMD) & USBCMD_RESET)) |
| 334 | break; |
| 335 | udelay(1); |
| 336 | cnt++; |
| 337 | } |
| 338 | if (cnt >= LINK_RESET_TIMEOUT_USEC) |
| 339 | return -ETIMEDOUT; |
| 340 | |
| 341 | /* select ULPI phy */ |
| 342 | writel(0x80000000, USB_PORTSC); |
| 343 | |
| 344 | msleep(100); |
| 345 | |
| 346 | writel(0x0, USB_AHBBURST); |
| 347 | writel(0x00, USB_AHBMODE); |
| 348 | |
| 349 | if (pdata->otg_control == OTG_PHY_CONTROL) { |
| 350 | val = readl(USB_OTGSC); |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 351 | if (pdata->mode == USB_DR_MODE_OTG) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 352 | ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID; |
| 353 | val |= OTGSC_IDIE | OTGSC_BSVIE; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 354 | } else if (pdata->mode == USB_DR_MODE_PERIPHERAL) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 355 | ulpi_val = ULPI_INT_SESS_VALID; |
| 356 | val |= OTGSC_BSVIE; |
| 357 | } |
| 358 | writel(val, USB_OTGSC); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 359 | ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE); |
| 360 | ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 366 | #define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000) |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 367 | #define PHY_RESUME_TIMEOUT_USEC (100 * 1000) |
| 368 | |
Josh Cartwright | e7d613d | 2014-02-18 10:36:29 -0600 | [diff] [blame] | 369 | #ifdef CONFIG_PM |
| 370 | |
| 371 | #define USB_PHY_SUSP_DIG_VOL 500000 |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 372 | static int msm_hsusb_config_vddcx(struct msm_otg *motg, int high) |
Josh Cartwright | e7d613d | 2014-02-18 10:36:29 -0600 | [diff] [blame] | 373 | { |
| 374 | int max_vol = USB_PHY_VDD_DIG_VOL_MAX; |
| 375 | int min_vol; |
| 376 | int ret; |
| 377 | |
| 378 | if (high) |
| 379 | min_vol = USB_PHY_VDD_DIG_VOL_MIN; |
| 380 | else |
| 381 | min_vol = USB_PHY_SUSP_DIG_VOL; |
| 382 | |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 383 | ret = regulator_set_voltage(motg->vddcx, min_vol, max_vol); |
Josh Cartwright | e7d613d | 2014-02-18 10:36:29 -0600 | [diff] [blame] | 384 | if (ret) { |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 385 | pr_err("Cannot set vddcx voltage\n"); |
Josh Cartwright | e7d613d | 2014-02-18 10:36:29 -0600 | [diff] [blame] | 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol); |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 394 | static int msm_otg_suspend(struct msm_otg *motg) |
| 395 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 396 | struct usb_phy *phy = &motg->phy; |
| 397 | struct usb_bus *bus = phy->otg->host; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 398 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 399 | int cnt = 0; |
| 400 | |
| 401 | if (atomic_read(&motg->in_lpm)) |
| 402 | return 0; |
| 403 | |
| 404 | disable_irq(motg->irq); |
| 405 | /* |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 406 | * Chipidea 45-nm PHY suspend sequence: |
| 407 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 408 | * Interrupt Latch Register auto-clear feature is not present |
| 409 | * in all PHY versions. Latch register is clear on read type. |
| 410 | * Clear latch register to avoid spurious wakeup from |
| 411 | * low power mode (LPM). |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 412 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 413 | * PHY comparators are disabled when PHY enters into low power |
| 414 | * mode (LPM). Keep PHY comparators ON in LPM only when we expect |
| 415 | * VBUS/Id notifications from USB PHY. Otherwise turn off USB |
| 416 | * PHY comparators. This save significant amount of power. |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 417 | * |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 418 | * PLL is not turned off when PHY enters into low power mode (LPM). |
| 419 | * Disable PLL for maximum power savings. |
| 420 | */ |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 421 | |
| 422 | if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 423 | ulpi_read(phy, 0x14); |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 424 | if (pdata->otg_control == OTG_PHY_CONTROL) |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 425 | ulpi_write(phy, 0x01, 0x30); |
| 426 | ulpi_write(phy, 0x08, 0x09); |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 427 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * PHY may take some time or even fail to enter into low power |
| 431 | * mode (LPM). Hence poll for 500 msec and reset the PHY and link |
| 432 | * in failure case. |
| 433 | */ |
| 434 | writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC); |
| 435 | while (cnt < PHY_SUSPEND_TIMEOUT_USEC) { |
| 436 | if (readl(USB_PORTSC) & PORTSC_PHCD) |
| 437 | break; |
| 438 | udelay(1); |
| 439 | cnt++; |
| 440 | } |
| 441 | |
| 442 | if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 443 | dev_err(phy->dev, "Unable to suspend PHY\n"); |
| 444 | msm_otg_reset(phy); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 445 | enable_irq(motg->irq); |
| 446 | return -ETIMEDOUT; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * PHY has capability to generate interrupt asynchronously in low |
| 451 | * power mode (LPM). This interrupt is level triggered. So USB IRQ |
| 452 | * line must be disabled till async interrupt enable bit is cleared |
| 453 | * in USBCMD register. Assert STP (ULPI interface STOP signal) to |
| 454 | * block data communication from PHY. |
| 455 | */ |
| 456 | writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD); |
| 457 | |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 458 | if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY && |
| 459 | motg->pdata->otg_control == OTG_PMIC_CONTROL) |
| 460 | writel(readl(USB_PHY_CTRL) | PHY_RETEN, USB_PHY_CTRL); |
| 461 | |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 462 | clk_disable_unprepare(motg->pclk); |
| 463 | clk_disable_unprepare(motg->clk); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 464 | if (!IS_ERR(motg->core_clk)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 465 | clk_disable_unprepare(motg->core_clk); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 466 | |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 467 | if (!IS_ERR(motg->pclk_src)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 468 | clk_disable_unprepare(motg->pclk_src); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 469 | |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 470 | if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY && |
| 471 | motg->pdata->otg_control == OTG_PMIC_CONTROL) { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 472 | msm_hsusb_ldo_set_mode(motg, 0); |
| 473 | msm_hsusb_config_vddcx(motg, 0); |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 474 | } |
| 475 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 476 | if (device_may_wakeup(phy->dev)) |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 477 | enable_irq_wake(motg->irq); |
| 478 | if (bus) |
| 479 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags); |
| 480 | |
| 481 | atomic_set(&motg->in_lpm, 1); |
| 482 | enable_irq(motg->irq); |
| 483 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 484 | dev_info(phy->dev, "USB in low power mode\n"); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 489 | static int msm_otg_resume(struct msm_otg *motg) |
| 490 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 491 | struct usb_phy *phy = &motg->phy; |
| 492 | struct usb_bus *bus = phy->otg->host; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 493 | int cnt = 0; |
| 494 | unsigned temp; |
| 495 | |
| 496 | if (!atomic_read(&motg->in_lpm)) |
| 497 | return 0; |
| 498 | |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 499 | if (!IS_ERR(motg->pclk_src)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 500 | clk_prepare_enable(motg->pclk_src); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 501 | |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 502 | clk_prepare_enable(motg->pclk); |
| 503 | clk_prepare_enable(motg->clk); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 504 | if (!IS_ERR(motg->core_clk)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 505 | clk_prepare_enable(motg->core_clk); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 506 | |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 507 | if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY && |
| 508 | motg->pdata->otg_control == OTG_PMIC_CONTROL) { |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 509 | msm_hsusb_ldo_set_mode(motg, 1); |
| 510 | msm_hsusb_config_vddcx(motg, 1); |
Pavankumar Kondeti | 04aebcb | 2011-05-04 10:19:49 +0530 | [diff] [blame] | 511 | writel(readl(USB_PHY_CTRL) & ~PHY_RETEN, USB_PHY_CTRL); |
| 512 | } |
| 513 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 514 | temp = readl(USB_USBCMD); |
| 515 | temp &= ~ASYNC_INTR_CTRL; |
| 516 | temp &= ~ULPI_STP_CTRL; |
| 517 | writel(temp, USB_USBCMD); |
| 518 | |
| 519 | /* |
| 520 | * PHY comes out of low power mode (LPM) in case of wakeup |
| 521 | * from asynchronous interrupt. |
| 522 | */ |
| 523 | if (!(readl(USB_PORTSC) & PORTSC_PHCD)) |
| 524 | goto skip_phy_resume; |
| 525 | |
| 526 | writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC); |
| 527 | while (cnt < PHY_RESUME_TIMEOUT_USEC) { |
| 528 | if (!(readl(USB_PORTSC) & PORTSC_PHCD)) |
| 529 | break; |
| 530 | udelay(1); |
| 531 | cnt++; |
| 532 | } |
| 533 | |
| 534 | if (cnt >= PHY_RESUME_TIMEOUT_USEC) { |
| 535 | /* |
| 536 | * This is a fatal error. Reset the link and |
| 537 | * PHY. USB state can not be restored. Re-insertion |
| 538 | * of USB cable is the only way to get USB working. |
| 539 | */ |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 540 | dev_err(phy->dev, "Unable to resume USB. Re-plugin the cable\n"); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 541 | msm_otg_reset(phy); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | skip_phy_resume: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 545 | if (device_may_wakeup(phy->dev)) |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 546 | disable_irq_wake(motg->irq); |
| 547 | if (bus) |
| 548 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags); |
| 549 | |
Pavankumar Kondeti | 2ce2c3a | 2011-05-02 11:56:33 +0530 | [diff] [blame] | 550 | atomic_set(&motg->in_lpm, 0); |
| 551 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 552 | if (motg->async_int) { |
| 553 | motg->async_int = 0; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 554 | pm_runtime_put(phy->dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 555 | enable_irq(motg->irq); |
| 556 | } |
| 557 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 558 | dev_info(phy->dev, "USB exited from low power mode\n"); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 559 | |
| 560 | return 0; |
| 561 | } |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 562 | #endif |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 563 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 564 | static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA) |
| 565 | { |
| 566 | if (motg->cur_power == mA) |
| 567 | return; |
| 568 | |
| 569 | /* TODO: Notify PMIC about available current */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 570 | dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 571 | motg->cur_power = mA; |
| 572 | } |
| 573 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 574 | static int msm_otg_set_power(struct usb_phy *phy, unsigned mA) |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 575 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 576 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 577 | |
| 578 | /* |
| 579 | * Gadget driver uses set_power method to notify about the |
| 580 | * available current based on suspend/configured states. |
| 581 | * |
| 582 | * IDEV_CHG can be drawn irrespective of suspend/un-configured |
| 583 | * states when CDP/ACA is connected. |
| 584 | */ |
| 585 | if (motg->chg_type == USB_SDP_CHARGER) |
| 586 | msm_otg_notify_charger(motg, mA); |
| 587 | |
| 588 | return 0; |
| 589 | } |
| 590 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 591 | static void msm_otg_start_host(struct usb_phy *phy, int on) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 592 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 593 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 594 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 595 | struct usb_hcd *hcd; |
| 596 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 597 | if (!phy->otg->host) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 598 | return; |
| 599 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 600 | hcd = bus_to_hcd(phy->otg->host); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 601 | |
| 602 | if (on) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 603 | dev_dbg(phy->dev, "host on\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 604 | |
| 605 | if (pdata->vbus_power) |
| 606 | pdata->vbus_power(1); |
| 607 | /* |
| 608 | * Some boards have a switch cotrolled by gpio |
| 609 | * to enable/disable internal HUB. Enable internal |
| 610 | * HUB before kicking the host. |
| 611 | */ |
| 612 | if (pdata->setup_gpio) |
| 613 | pdata->setup_gpio(OTG_STATE_A_HOST); |
| 614 | #ifdef CONFIG_USB |
| 615 | usb_add_hcd(hcd, hcd->irq, IRQF_SHARED); |
Peter Chen | 3c9740a | 2013-11-05 10:46:02 +0800 | [diff] [blame] | 616 | device_wakeup_enable(hcd->self.controller); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 617 | #endif |
| 618 | } else { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 619 | dev_dbg(phy->dev, "host off\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 620 | |
| 621 | #ifdef CONFIG_USB |
| 622 | usb_remove_hcd(hcd); |
| 623 | #endif |
| 624 | if (pdata->setup_gpio) |
| 625 | pdata->setup_gpio(OTG_STATE_UNDEFINED); |
| 626 | if (pdata->vbus_power) |
| 627 | pdata->vbus_power(0); |
| 628 | } |
| 629 | } |
| 630 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 631 | static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 632 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 633 | struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 634 | struct usb_hcd *hcd; |
| 635 | |
| 636 | /* |
| 637 | * Fail host registration if this board can support |
| 638 | * only peripheral configuration. |
| 639 | */ |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 640 | if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 641 | dev_info(otg->phy->dev, "Host mode is not supported\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 642 | return -ENODEV; |
| 643 | } |
| 644 | |
| 645 | if (!host) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 646 | if (otg->phy->state == OTG_STATE_A_HOST) { |
| 647 | pm_runtime_get_sync(otg->phy->dev); |
| 648 | msm_otg_start_host(otg->phy, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 649 | otg->host = NULL; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 650 | otg->phy->state = OTG_STATE_UNDEFINED; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 651 | schedule_work(&motg->sm_work); |
| 652 | } else { |
| 653 | otg->host = NULL; |
| 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | hcd = bus_to_hcd(host); |
| 660 | hcd->power_budget = motg->pdata->power_budget; |
| 661 | |
| 662 | otg->host = host; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 663 | dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Kick the state machine work, if peripheral is not supported |
| 667 | * or peripheral is already registered with us. |
| 668 | */ |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 669 | if (motg->pdata->mode == USB_DR_MODE_HOST || otg->gadget) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 670 | pm_runtime_get_sync(otg->phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 671 | schedule_work(&motg->sm_work); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 672 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 677 | static void msm_otg_start_peripheral(struct usb_phy *phy, int on) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 678 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 679 | struct msm_otg *motg = container_of(phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 680 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 681 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 682 | if (!phy->otg->gadget) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 683 | return; |
| 684 | |
| 685 | if (on) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 686 | dev_dbg(phy->dev, "gadget on\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 687 | /* |
| 688 | * Some boards have a switch cotrolled by gpio |
| 689 | * to enable/disable internal HUB. Disable internal |
| 690 | * HUB before kicking the gadget. |
| 691 | */ |
| 692 | if (pdata->setup_gpio) |
| 693 | pdata->setup_gpio(OTG_STATE_B_PERIPHERAL); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 694 | usb_gadget_vbus_connect(phy->otg->gadget); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 695 | } else { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 696 | dev_dbg(phy->dev, "gadget off\n"); |
| 697 | usb_gadget_vbus_disconnect(phy->otg->gadget); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 698 | if (pdata->setup_gpio) |
| 699 | pdata->setup_gpio(OTG_STATE_UNDEFINED); |
| 700 | } |
| 701 | |
| 702 | } |
| 703 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 704 | static int msm_otg_set_peripheral(struct usb_otg *otg, |
| 705 | struct usb_gadget *gadget) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 706 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 707 | struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 708 | |
| 709 | /* |
| 710 | * Fail peripheral registration if this board can support |
| 711 | * only host configuration. |
| 712 | */ |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 713 | if (motg->pdata->mode == USB_DR_MODE_HOST) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 714 | dev_info(otg->phy->dev, "Peripheral mode is not supported\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 715 | return -ENODEV; |
| 716 | } |
| 717 | |
| 718 | if (!gadget) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 719 | if (otg->phy->state == OTG_STATE_B_PERIPHERAL) { |
| 720 | pm_runtime_get_sync(otg->phy->dev); |
| 721 | msm_otg_start_peripheral(otg->phy, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 722 | otg->gadget = NULL; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 723 | otg->phy->state = OTG_STATE_UNDEFINED; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 724 | schedule_work(&motg->sm_work); |
| 725 | } else { |
| 726 | otg->gadget = NULL; |
| 727 | } |
| 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | otg->gadget = gadget; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 732 | dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * Kick the state machine work, if host is not supported |
| 736 | * or host is already registered with us. |
| 737 | */ |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 738 | if (motg->pdata->mode == USB_DR_MODE_PERIPHERAL || otg->host) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 739 | pm_runtime_get_sync(otg->phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 740 | schedule_work(&motg->sm_work); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 741 | } |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 746 | static bool msm_chg_check_secondary_det(struct msm_otg *motg) |
| 747 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 748 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 749 | u32 chg_det; |
| 750 | bool ret = false; |
| 751 | |
| 752 | switch (motg->pdata->phy_type) { |
| 753 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 754 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 755 | ret = chg_det & (1 << 4); |
| 756 | break; |
| 757 | case SNPS_28NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 758 | chg_det = ulpi_read(phy, 0x87); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 759 | ret = chg_det & 1; |
| 760 | break; |
| 761 | default: |
| 762 | break; |
| 763 | } |
| 764 | return ret; |
| 765 | } |
| 766 | |
| 767 | static void msm_chg_enable_secondary_det(struct msm_otg *motg) |
| 768 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 769 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 770 | u32 chg_det; |
| 771 | |
| 772 | switch (motg->pdata->phy_type) { |
| 773 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 774 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 775 | /* Turn off charger block */ |
| 776 | chg_det |= ~(1 << 1); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 777 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 778 | udelay(20); |
| 779 | /* control chg block via ULPI */ |
| 780 | chg_det &= ~(1 << 3); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 781 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 782 | /* put it in host mode for enabling D- source */ |
| 783 | chg_det &= ~(1 << 2); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 784 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 785 | /* Turn on chg detect block */ |
| 786 | chg_det &= ~(1 << 1); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 787 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 788 | udelay(20); |
| 789 | /* enable chg detection */ |
| 790 | chg_det &= ~(1 << 0); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 791 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 792 | break; |
| 793 | case SNPS_28NM_INTEGRATED_PHY: |
| 794 | /* |
| 795 | * Configure DM as current source, DP as current sink |
| 796 | * and enable battery charging comparators. |
| 797 | */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 798 | ulpi_write(phy, 0x8, 0x85); |
| 799 | ulpi_write(phy, 0x2, 0x85); |
| 800 | ulpi_write(phy, 0x1, 0x85); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 801 | break; |
| 802 | default: |
| 803 | break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | static bool msm_chg_check_primary_det(struct msm_otg *motg) |
| 808 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 809 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 810 | u32 chg_det; |
| 811 | bool ret = false; |
| 812 | |
| 813 | switch (motg->pdata->phy_type) { |
| 814 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 815 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 816 | ret = chg_det & (1 << 4); |
| 817 | break; |
| 818 | case SNPS_28NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 819 | chg_det = ulpi_read(phy, 0x87); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 820 | ret = chg_det & 1; |
| 821 | break; |
| 822 | default: |
| 823 | break; |
| 824 | } |
| 825 | return ret; |
| 826 | } |
| 827 | |
| 828 | static void msm_chg_enable_primary_det(struct msm_otg *motg) |
| 829 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 830 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 831 | u32 chg_det; |
| 832 | |
| 833 | switch (motg->pdata->phy_type) { |
| 834 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 835 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 836 | /* enable chg detection */ |
| 837 | chg_det &= ~(1 << 0); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 838 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 839 | break; |
| 840 | case SNPS_28NM_INTEGRATED_PHY: |
| 841 | /* |
| 842 | * Configure DP as current source, DM as current sink |
| 843 | * and enable battery charging comparators. |
| 844 | */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 845 | ulpi_write(phy, 0x2, 0x85); |
| 846 | ulpi_write(phy, 0x1, 0x85); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 847 | break; |
| 848 | default: |
| 849 | break; |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static bool msm_chg_check_dcd(struct msm_otg *motg) |
| 854 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 855 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 856 | u32 line_state; |
| 857 | bool ret = false; |
| 858 | |
| 859 | switch (motg->pdata->phy_type) { |
| 860 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 861 | line_state = ulpi_read(phy, 0x15); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 862 | ret = !(line_state & 1); |
| 863 | break; |
| 864 | case SNPS_28NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 865 | line_state = ulpi_read(phy, 0x87); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 866 | ret = line_state & 2; |
| 867 | break; |
| 868 | default: |
| 869 | break; |
| 870 | } |
| 871 | return ret; |
| 872 | } |
| 873 | |
| 874 | static void msm_chg_disable_dcd(struct msm_otg *motg) |
| 875 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 876 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 877 | u32 chg_det; |
| 878 | |
| 879 | switch (motg->pdata->phy_type) { |
| 880 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 881 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 882 | chg_det &= ~(1 << 5); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 883 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 884 | break; |
| 885 | case SNPS_28NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 886 | ulpi_write(phy, 0x10, 0x86); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 887 | break; |
| 888 | default: |
| 889 | break; |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | static void msm_chg_enable_dcd(struct msm_otg *motg) |
| 894 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 895 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 896 | u32 chg_det; |
| 897 | |
| 898 | switch (motg->pdata->phy_type) { |
| 899 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 900 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 901 | /* Turn on D+ current source */ |
| 902 | chg_det |= (1 << 5); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 903 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 904 | break; |
| 905 | case SNPS_28NM_INTEGRATED_PHY: |
| 906 | /* Data contact detection enable */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 907 | ulpi_write(phy, 0x10, 0x85); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 908 | break; |
| 909 | default: |
| 910 | break; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | static void msm_chg_block_on(struct msm_otg *motg) |
| 915 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 916 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 917 | u32 func_ctrl, chg_det; |
| 918 | |
| 919 | /* put the controller in non-driving mode */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 920 | func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 921 | func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK; |
| 922 | func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 923 | ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 924 | |
| 925 | switch (motg->pdata->phy_type) { |
| 926 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 927 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 928 | /* control chg block via ULPI */ |
| 929 | chg_det &= ~(1 << 3); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 930 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 931 | /* Turn on chg detect block */ |
| 932 | chg_det &= ~(1 << 1); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 933 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 934 | udelay(20); |
| 935 | break; |
| 936 | case SNPS_28NM_INTEGRATED_PHY: |
| 937 | /* Clear charger detecting control bits */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 938 | ulpi_write(phy, 0x3F, 0x86); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 939 | /* Clear alt interrupt latch and enable bits */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 940 | ulpi_write(phy, 0x1F, 0x92); |
| 941 | ulpi_write(phy, 0x1F, 0x95); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 942 | udelay(100); |
| 943 | break; |
| 944 | default: |
| 945 | break; |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static void msm_chg_block_off(struct msm_otg *motg) |
| 950 | { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 951 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 952 | u32 func_ctrl, chg_det; |
| 953 | |
| 954 | switch (motg->pdata->phy_type) { |
| 955 | case CI_45NM_INTEGRATED_PHY: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 956 | chg_det = ulpi_read(phy, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 957 | /* Turn off charger block */ |
| 958 | chg_det |= ~(1 << 1); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 959 | ulpi_write(phy, chg_det, 0x34); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 960 | break; |
| 961 | case SNPS_28NM_INTEGRATED_PHY: |
| 962 | /* Clear charger detecting control bits */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 963 | ulpi_write(phy, 0x3F, 0x86); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 964 | /* Clear alt interrupt latch and enable bits */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 965 | ulpi_write(phy, 0x1F, 0x92); |
| 966 | ulpi_write(phy, 0x1F, 0x95); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 967 | break; |
| 968 | default: |
| 969 | break; |
| 970 | } |
| 971 | |
| 972 | /* put the controller in normal mode */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 973 | func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 974 | func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK; |
| 975 | func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 976 | ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | #define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */ |
| 980 | #define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */ |
| 981 | #define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */ |
| 982 | #define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */ |
| 983 | static void msm_chg_detect_work(struct work_struct *w) |
| 984 | { |
| 985 | struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 986 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 987 | bool is_dcd, tmout, vout; |
| 988 | unsigned long delay; |
| 989 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 990 | dev_dbg(phy->dev, "chg detection work\n"); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 991 | switch (motg->chg_state) { |
| 992 | case USB_CHG_STATE_UNDEFINED: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 993 | pm_runtime_get_sync(phy->dev); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 994 | msm_chg_block_on(motg); |
| 995 | msm_chg_enable_dcd(motg); |
| 996 | motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD; |
| 997 | motg->dcd_retries = 0; |
| 998 | delay = MSM_CHG_DCD_POLL_TIME; |
| 999 | break; |
| 1000 | case USB_CHG_STATE_WAIT_FOR_DCD: |
| 1001 | is_dcd = msm_chg_check_dcd(motg); |
| 1002 | tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES; |
| 1003 | if (is_dcd || tmout) { |
| 1004 | msm_chg_disable_dcd(motg); |
| 1005 | msm_chg_enable_primary_det(motg); |
| 1006 | delay = MSM_CHG_PRIMARY_DET_TIME; |
| 1007 | motg->chg_state = USB_CHG_STATE_DCD_DONE; |
| 1008 | } else { |
| 1009 | delay = MSM_CHG_DCD_POLL_TIME; |
| 1010 | } |
| 1011 | break; |
| 1012 | case USB_CHG_STATE_DCD_DONE: |
| 1013 | vout = msm_chg_check_primary_det(motg); |
| 1014 | if (vout) { |
| 1015 | msm_chg_enable_secondary_det(motg); |
| 1016 | delay = MSM_CHG_SECONDARY_DET_TIME; |
| 1017 | motg->chg_state = USB_CHG_STATE_PRIMARY_DONE; |
| 1018 | } else { |
| 1019 | motg->chg_type = USB_SDP_CHARGER; |
| 1020 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1021 | delay = 0; |
| 1022 | } |
| 1023 | break; |
| 1024 | case USB_CHG_STATE_PRIMARY_DONE: |
| 1025 | vout = msm_chg_check_secondary_det(motg); |
| 1026 | if (vout) |
| 1027 | motg->chg_type = USB_DCP_CHARGER; |
| 1028 | else |
| 1029 | motg->chg_type = USB_CDP_CHARGER; |
| 1030 | motg->chg_state = USB_CHG_STATE_SECONDARY_DONE; |
| 1031 | /* fall through */ |
| 1032 | case USB_CHG_STATE_SECONDARY_DONE: |
| 1033 | motg->chg_state = USB_CHG_STATE_DETECTED; |
| 1034 | case USB_CHG_STATE_DETECTED: |
| 1035 | msm_chg_block_off(motg); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1036 | dev_dbg(phy->dev, "charger = %d\n", motg->chg_type); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1037 | schedule_work(&motg->sm_work); |
| 1038 | return; |
| 1039 | default: |
| 1040 | return; |
| 1041 | } |
| 1042 | |
| 1043 | schedule_delayed_work(&motg->chg_work, delay); |
| 1044 | } |
| 1045 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1046 | /* |
| 1047 | * We support OTG, Peripheral only and Host only configurations. In case |
| 1048 | * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen |
| 1049 | * via Id pin status or user request (debugfs). Id/BSV interrupts are not |
| 1050 | * enabled when switch is controlled by user and default mode is supplied |
| 1051 | * by board file, which can be changed by userspace later. |
| 1052 | */ |
| 1053 | static void msm_otg_init_sm(struct msm_otg *motg) |
| 1054 | { |
| 1055 | struct msm_otg_platform_data *pdata = motg->pdata; |
| 1056 | u32 otgsc = readl(USB_OTGSC); |
| 1057 | |
| 1058 | switch (pdata->mode) { |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1059 | case USB_DR_MODE_OTG: |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1060 | if (pdata->otg_control == OTG_PHY_CONTROL) { |
| 1061 | if (otgsc & OTGSC_ID) |
| 1062 | set_bit(ID, &motg->inputs); |
| 1063 | else |
| 1064 | clear_bit(ID, &motg->inputs); |
| 1065 | |
| 1066 | if (otgsc & OTGSC_BSV) |
| 1067 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1068 | else |
| 1069 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 1070 | } else if (pdata->otg_control == OTG_USER_CONTROL) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1071 | set_bit(ID, &motg->inputs); |
| 1072 | clear_bit(B_SESS_VLD, &motg->inputs); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1073 | } |
| 1074 | break; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1075 | case USB_DR_MODE_HOST: |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1076 | clear_bit(ID, &motg->inputs); |
| 1077 | break; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1078 | case USB_DR_MODE_PERIPHERAL: |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1079 | set_bit(ID, &motg->inputs); |
| 1080 | if (otgsc & OTGSC_BSV) |
| 1081 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1082 | else |
| 1083 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 1084 | break; |
| 1085 | default: |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | |
| 1090 | static void msm_otg_sm_work(struct work_struct *w) |
| 1091 | { |
| 1092 | struct msm_otg *motg = container_of(w, struct msm_otg, sm_work); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1093 | struct usb_otg *otg = motg->phy.otg; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1094 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1095 | switch (otg->phy->state) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1096 | case OTG_STATE_UNDEFINED: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1097 | dev_dbg(otg->phy->dev, "OTG_STATE_UNDEFINED state\n"); |
| 1098 | msm_otg_reset(otg->phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1099 | msm_otg_init_sm(motg); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1100 | otg->phy->state = OTG_STATE_B_IDLE; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1101 | /* FALL THROUGH */ |
| 1102 | case OTG_STATE_B_IDLE: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1103 | dev_dbg(otg->phy->dev, "OTG_STATE_B_IDLE state\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1104 | if (!test_bit(ID, &motg->inputs) && otg->host) { |
| 1105 | /* disable BSV bit */ |
| 1106 | writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1107 | msm_otg_start_host(otg->phy, 1); |
| 1108 | otg->phy->state = OTG_STATE_A_HOST; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1109 | } else if (test_bit(B_SESS_VLD, &motg->inputs)) { |
| 1110 | switch (motg->chg_state) { |
| 1111 | case USB_CHG_STATE_UNDEFINED: |
| 1112 | msm_chg_detect_work(&motg->chg_work.work); |
| 1113 | break; |
| 1114 | case USB_CHG_STATE_DETECTED: |
| 1115 | switch (motg->chg_type) { |
| 1116 | case USB_DCP_CHARGER: |
| 1117 | msm_otg_notify_charger(motg, |
| 1118 | IDEV_CHG_MAX); |
| 1119 | break; |
| 1120 | case USB_CDP_CHARGER: |
| 1121 | msm_otg_notify_charger(motg, |
| 1122 | IDEV_CHG_MAX); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1123 | msm_otg_start_peripheral(otg->phy, 1); |
| 1124 | otg->phy->state |
| 1125 | = OTG_STATE_B_PERIPHERAL; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1126 | break; |
| 1127 | case USB_SDP_CHARGER: |
| 1128 | msm_otg_notify_charger(motg, IUNIT); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1129 | msm_otg_start_peripheral(otg->phy, 1); |
| 1130 | otg->phy->state |
| 1131 | = OTG_STATE_B_PERIPHERAL; |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1132 | break; |
| 1133 | default: |
| 1134 | break; |
| 1135 | } |
| 1136 | break; |
| 1137 | default: |
| 1138 | break; |
| 1139 | } |
| 1140 | } else { |
| 1141 | /* |
| 1142 | * If charger detection work is pending, decrement |
| 1143 | * the pm usage counter to balance with the one that |
| 1144 | * is incremented in charger detection work. |
| 1145 | */ |
| 1146 | if (cancel_delayed_work_sync(&motg->chg_work)) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1147 | pm_runtime_put_sync(otg->phy->dev); |
| 1148 | msm_otg_reset(otg->phy); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1149 | } |
| 1150 | msm_otg_notify_charger(motg, 0); |
| 1151 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1152 | motg->chg_type = USB_INVALID_CHARGER; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1153 | } |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1154 | pm_runtime_put_sync(otg->phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1155 | break; |
| 1156 | case OTG_STATE_B_PERIPHERAL: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1157 | dev_dbg(otg->phy->dev, "OTG_STATE_B_PERIPHERAL state\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1158 | if (!test_bit(B_SESS_VLD, &motg->inputs) || |
| 1159 | !test_bit(ID, &motg->inputs)) { |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1160 | msm_otg_notify_charger(motg, 0); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1161 | msm_otg_start_peripheral(otg->phy, 0); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1162 | motg->chg_state = USB_CHG_STATE_UNDEFINED; |
| 1163 | motg->chg_type = USB_INVALID_CHARGER; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1164 | otg->phy->state = OTG_STATE_B_IDLE; |
| 1165 | msm_otg_reset(otg->phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1166 | schedule_work(w); |
| 1167 | } |
| 1168 | break; |
| 1169 | case OTG_STATE_A_HOST: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1170 | dev_dbg(otg->phy->dev, "OTG_STATE_A_HOST state\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1171 | if (test_bit(ID, &motg->inputs)) { |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1172 | msm_otg_start_host(otg->phy, 0); |
| 1173 | otg->phy->state = OTG_STATE_B_IDLE; |
| 1174 | msm_otg_reset(otg->phy); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1175 | schedule_work(w); |
| 1176 | } |
| 1177 | break; |
| 1178 | default: |
| 1179 | break; |
| 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | static irqreturn_t msm_otg_irq(int irq, void *data) |
| 1184 | { |
| 1185 | struct msm_otg *motg = data; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1186 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1187 | u32 otgsc = 0; |
| 1188 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1189 | if (atomic_read(&motg->in_lpm)) { |
| 1190 | disable_irq_nosync(irq); |
| 1191 | motg->async_int = 1; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1192 | pm_runtime_get(phy->dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1193 | return IRQ_HANDLED; |
| 1194 | } |
| 1195 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1196 | otgsc = readl(USB_OTGSC); |
| 1197 | if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS))) |
| 1198 | return IRQ_NONE; |
| 1199 | |
| 1200 | if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) { |
| 1201 | if (otgsc & OTGSC_ID) |
| 1202 | set_bit(ID, &motg->inputs); |
| 1203 | else |
| 1204 | clear_bit(ID, &motg->inputs); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1205 | dev_dbg(phy->dev, "ID set/clear\n"); |
| 1206 | pm_runtime_get_noresume(phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1207 | } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) { |
| 1208 | if (otgsc & OTGSC_BSV) |
| 1209 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1210 | else |
| 1211 | clear_bit(B_SESS_VLD, &motg->inputs); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1212 | dev_dbg(phy->dev, "BSV set/clear\n"); |
| 1213 | pm_runtime_get_noresume(phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | writel(otgsc, USB_OTGSC); |
| 1217 | schedule_work(&motg->sm_work); |
| 1218 | return IRQ_HANDLED; |
| 1219 | } |
| 1220 | |
| 1221 | static int msm_otg_mode_show(struct seq_file *s, void *unused) |
| 1222 | { |
| 1223 | struct msm_otg *motg = s->private; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1224 | struct usb_otg *otg = motg->phy.otg; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1225 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1226 | switch (otg->phy->state) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1227 | case OTG_STATE_A_HOST: |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 1228 | seq_puts(s, "host\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1229 | break; |
| 1230 | case OTG_STATE_B_PERIPHERAL: |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 1231 | seq_puts(s, "peripheral\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1232 | break; |
| 1233 | default: |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 1234 | seq_puts(s, "none\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1235 | break; |
| 1236 | } |
| 1237 | |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int msm_otg_mode_open(struct inode *inode, struct file *file) |
| 1242 | { |
| 1243 | return single_open(file, msm_otg_mode_show, inode->i_private); |
| 1244 | } |
| 1245 | |
| 1246 | static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf, |
| 1247 | size_t count, loff_t *ppos) |
| 1248 | { |
Pavankumar Kondeti | e2904ee | 2011-02-15 09:42:35 +0530 | [diff] [blame] | 1249 | struct seq_file *s = file->private_data; |
| 1250 | struct msm_otg *motg = s->private; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1251 | char buf[16]; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1252 | struct usb_otg *otg = motg->phy.otg; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1253 | int status = count; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1254 | enum usb_dr_mode req_mode; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1255 | |
| 1256 | memset(buf, 0x00, sizeof(buf)); |
| 1257 | |
| 1258 | if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) { |
| 1259 | status = -EFAULT; |
| 1260 | goto out; |
| 1261 | } |
| 1262 | |
| 1263 | if (!strncmp(buf, "host", 4)) { |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1264 | req_mode = USB_DR_MODE_HOST; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1265 | } else if (!strncmp(buf, "peripheral", 10)) { |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1266 | req_mode = USB_DR_MODE_PERIPHERAL; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1267 | } else if (!strncmp(buf, "none", 4)) { |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1268 | req_mode = USB_DR_MODE_UNKNOWN; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1269 | } else { |
| 1270 | status = -EINVAL; |
| 1271 | goto out; |
| 1272 | } |
| 1273 | |
| 1274 | switch (req_mode) { |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1275 | case USB_DR_MODE_UNKNOWN: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1276 | switch (otg->phy->state) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1277 | case OTG_STATE_A_HOST: |
| 1278 | case OTG_STATE_B_PERIPHERAL: |
| 1279 | set_bit(ID, &motg->inputs); |
| 1280 | clear_bit(B_SESS_VLD, &motg->inputs); |
| 1281 | break; |
| 1282 | default: |
| 1283 | goto out; |
| 1284 | } |
| 1285 | break; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1286 | case USB_DR_MODE_PERIPHERAL: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1287 | switch (otg->phy->state) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1288 | case OTG_STATE_B_IDLE: |
| 1289 | case OTG_STATE_A_HOST: |
| 1290 | set_bit(ID, &motg->inputs); |
| 1291 | set_bit(B_SESS_VLD, &motg->inputs); |
| 1292 | break; |
| 1293 | default: |
| 1294 | goto out; |
| 1295 | } |
| 1296 | break; |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1297 | case USB_DR_MODE_HOST: |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1298 | switch (otg->phy->state) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1299 | case OTG_STATE_B_IDLE: |
| 1300 | case OTG_STATE_B_PERIPHERAL: |
| 1301 | clear_bit(ID, &motg->inputs); |
| 1302 | break; |
| 1303 | default: |
| 1304 | goto out; |
| 1305 | } |
| 1306 | break; |
| 1307 | default: |
| 1308 | goto out; |
| 1309 | } |
| 1310 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1311 | pm_runtime_get_sync(otg->phy->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1312 | schedule_work(&motg->sm_work); |
| 1313 | out: |
| 1314 | return status; |
| 1315 | } |
| 1316 | |
| 1317 | const struct file_operations msm_otg_mode_fops = { |
| 1318 | .open = msm_otg_mode_open, |
| 1319 | .read = seq_read, |
| 1320 | .write = msm_otg_mode_write, |
| 1321 | .llseek = seq_lseek, |
| 1322 | .release = single_release, |
| 1323 | }; |
| 1324 | |
| 1325 | static struct dentry *msm_otg_dbg_root; |
| 1326 | static struct dentry *msm_otg_dbg_mode; |
| 1327 | |
| 1328 | static int msm_otg_debugfs_init(struct msm_otg *motg) |
| 1329 | { |
| 1330 | msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL); |
| 1331 | |
| 1332 | if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root)) |
| 1333 | return -ENODEV; |
| 1334 | |
| 1335 | msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO | S_IWUSR, |
| 1336 | msm_otg_dbg_root, motg, &msm_otg_mode_fops); |
| 1337 | if (!msm_otg_dbg_mode) { |
| 1338 | debugfs_remove(msm_otg_dbg_root); |
| 1339 | msm_otg_dbg_root = NULL; |
| 1340 | return -ENODEV; |
| 1341 | } |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | static void msm_otg_debugfs_cleanup(void) |
| 1347 | { |
| 1348 | debugfs_remove(msm_otg_dbg_mode); |
| 1349 | debugfs_remove(msm_otg_dbg_root); |
| 1350 | } |
| 1351 | |
Ivan T. Ivanov | 06a6ec4 | 2014-04-28 16:34:07 +0300 | [diff] [blame] | 1352 | static int msm_otg_probe(struct platform_device *pdev) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1353 | { |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1354 | struct regulator_bulk_data regs[3]; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1355 | int ret = 0; |
| 1356 | struct resource *res; |
| 1357 | struct msm_otg *motg; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1358 | struct usb_phy *phy; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1359 | |
| 1360 | dev_info(&pdev->dev, "msm_otg probe\n"); |
Jingoo Han | 19f9e18 | 2013-07-30 17:02:13 +0900 | [diff] [blame] | 1361 | if (!dev_get_platdata(&pdev->dev)) { |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1362 | dev_err(&pdev->dev, "No platform data given. Bailing out\n"); |
| 1363 | return -ENODEV; |
| 1364 | } |
| 1365 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1366 | motg = devm_kzalloc(&pdev->dev, sizeof(struct msm_otg), GFP_KERNEL); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1367 | if (!motg) { |
| 1368 | dev_err(&pdev->dev, "unable to allocate msm_otg\n"); |
| 1369 | return -ENOMEM; |
| 1370 | } |
| 1371 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1372 | motg->phy.otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg), |
| 1373 | GFP_KERNEL); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1374 | if (!motg->phy.otg) { |
| 1375 | dev_err(&pdev->dev, "unable to allocate msm_otg\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1376 | return -ENOMEM; |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1377 | } |
| 1378 | |
Jingoo Han | 19f9e18 | 2013-07-30 17:02:13 +0900 | [diff] [blame] | 1379 | motg->pdata = dev_get_platdata(&pdev->dev); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1380 | phy = &motg->phy; |
| 1381 | phy->dev = &pdev->dev; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1382 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1383 | motg->phy_reset_clk = devm_clk_get(&pdev->dev, "usb_phy_clk"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1384 | if (IS_ERR(motg->phy_reset_clk)) { |
| 1385 | dev_err(&pdev->dev, "failed to get usb_phy_clk\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1386 | return PTR_ERR(motg->phy_reset_clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1387 | } |
| 1388 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1389 | motg->clk = devm_clk_get(&pdev->dev, "usb_hs_clk"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1390 | if (IS_ERR(motg->clk)) { |
| 1391 | dev_err(&pdev->dev, "failed to get usb_hs_clk\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1392 | return PTR_ERR(motg->clk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1393 | } |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 1394 | |
| 1395 | /* |
| 1396 | * If USB Core is running its protocol engine based on CORE CLK, |
| 1397 | * CORE CLK must be running at >55Mhz for correct HSUSB |
| 1398 | * operation and USB core cannot tolerate frequency changes on |
| 1399 | * CORE CLK. For such USB cores, vote for maximum clk frequency |
| 1400 | * on pclk source |
| 1401 | */ |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1402 | motg->pclk_src = ERR_PTR(-ENOENT); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 1403 | if (motg->pdata->pclk_src_name) { |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1404 | motg->pclk_src = devm_clk_get(&pdev->dev, |
| 1405 | motg->pdata->pclk_src_name); |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 1406 | if (IS_ERR(motg->pclk_src)) |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1407 | return PTR_ERR(motg->pclk_src); |
| 1408 | } |
Anji jonnala | 0f73cac | 2011-05-04 10:19:46 +0530 | [diff] [blame] | 1409 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1410 | motg->pclk = devm_clk_get(&pdev->dev, "usb_hs_pclk"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1411 | if (IS_ERR(motg->pclk)) { |
| 1412 | dev_err(&pdev->dev, "failed to get usb_hs_pclk\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1413 | return PTR_ERR(motg->pclk); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * USB core clock is not present on all MSM chips. This |
| 1418 | * clock is introduced to remove the dependency on AXI |
| 1419 | * bus frequency. |
| 1420 | */ |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1421 | motg->core_clk = devm_clk_get(&pdev->dev, "usb_hs_core_clk"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1422 | |
| 1423 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1424 | motg->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 1425 | if (IS_ERR(motg->regs)) |
| 1426 | return PTR_ERR(motg->regs); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1427 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1428 | dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs); |
| 1429 | |
| 1430 | motg->irq = platform_get_irq(pdev, 0); |
| 1431 | if (!motg->irq) { |
| 1432 | dev_err(&pdev->dev, "platform_get_irq failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1433 | return motg->irq; |
| 1434 | } |
| 1435 | |
| 1436 | regs[0].supply = "HSUSB_VDDCX"; |
| 1437 | regs[1].supply = "HSUSB_3p3"; |
| 1438 | regs[2].supply = "HSUSB_1p8"; |
| 1439 | |
| 1440 | ret = devm_regulator_bulk_get(motg->phy.dev, ARRAY_SIZE(regs), regs); |
| 1441 | if (ret) |
| 1442 | return ret; |
| 1443 | |
| 1444 | motg->vddcx = regs[0].consumer; |
| 1445 | motg->v3p3 = regs[1].consumer; |
| 1446 | motg->v1p8 = regs[2].consumer; |
| 1447 | |
| 1448 | clk_set_rate(motg->clk, 60000000); |
| 1449 | if (!IS_ERR(motg->pclk_src)) { |
| 1450 | clk_set_rate(motg->pclk_src, INT_MAX); |
| 1451 | clk_prepare_enable(motg->pclk_src); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1452 | } |
| 1453 | |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1454 | clk_prepare_enable(motg->clk); |
| 1455 | clk_prepare_enable(motg->pclk); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1456 | |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1457 | if (!IS_ERR(motg->core_clk)) |
| 1458 | clk_prepare_enable(motg->core_clk); |
| 1459 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1460 | ret = msm_hsusb_init_vddcx(motg, 1); |
| 1461 | if (ret) { |
| 1462 | dev_err(&pdev->dev, "hsusb vddcx configuration failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1463 | goto disable_clks; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | ret = msm_hsusb_ldo_init(motg, 1); |
| 1467 | if (ret) { |
| 1468 | dev_err(&pdev->dev, "hsusb vreg configuration failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1469 | goto disable_vddcx; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1470 | } |
Ivan T. Ivanov | 37cfdaf | 2014-04-28 16:34:06 +0300 | [diff] [blame] | 1471 | ret = msm_hsusb_ldo_set_mode(motg, 1); |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1472 | if (ret) { |
| 1473 | dev_err(&pdev->dev, "hsusb vreg enable failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1474 | goto disable_ldo; |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1475 | } |
| 1476 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1477 | writel(0, USB_USBINTR); |
| 1478 | writel(0, USB_OTGSC); |
| 1479 | |
| 1480 | INIT_WORK(&motg->sm_work, msm_otg_sm_work); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1481 | INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1482 | ret = devm_request_irq(&pdev->dev, motg->irq, msm_otg_irq, IRQF_SHARED, |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1483 | "msm_otg", motg); |
| 1484 | if (ret) { |
| 1485 | dev_err(&pdev->dev, "request irq failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1486 | goto disable_ldo; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1487 | } |
| 1488 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1489 | phy->init = msm_otg_reset; |
| 1490 | phy->set_power = msm_otg_set_power; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1491 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1492 | phy->io_ops = &msm_otg_io_ops; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1493 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1494 | phy->otg->phy = &motg->phy; |
| 1495 | phy->otg->set_host = msm_otg_set_host; |
| 1496 | phy->otg->set_peripheral = msm_otg_set_peripheral; |
| 1497 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 1498 | ret = usb_add_phy(&motg->phy, USB_PHY_TYPE_USB2); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1499 | if (ret) { |
Kishon Vijay Abraham I | 721002e | 2012-06-22 17:02:45 +0530 | [diff] [blame] | 1500 | dev_err(&pdev->dev, "usb_add_phy failed\n"); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1501 | goto disable_ldo; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | platform_set_drvdata(pdev, motg); |
| 1505 | device_init_wakeup(&pdev->dev, 1); |
| 1506 | |
Ivan T. Ivanov | 971232c | 2014-04-28 16:34:11 +0300 | [diff] [blame^] | 1507 | if (motg->pdata->mode == USB_DR_MODE_OTG && |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1508 | motg->pdata->otg_control == OTG_USER_CONTROL) { |
| 1509 | ret = msm_otg_debugfs_init(motg); |
| 1510 | if (ret) |
Ivan T. Ivanov | 3aca0fa | 2014-04-28 16:34:10 +0300 | [diff] [blame] | 1511 | dev_dbg(&pdev->dev, "Can not create mode change file\n"); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1512 | } |
| 1513 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1514 | pm_runtime_set_active(&pdev->dev); |
| 1515 | pm_runtime_enable(&pdev->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1516 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1517 | return 0; |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1518 | |
| 1519 | disable_ldo: |
| 1520 | msm_hsusb_ldo_init(motg, 0); |
| 1521 | disable_vddcx: |
| 1522 | msm_hsusb_init_vddcx(motg, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1523 | disable_clks: |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1524 | clk_disable_unprepare(motg->pclk); |
| 1525 | clk_disable_unprepare(motg->clk); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1526 | if (!IS_ERR(motg->core_clk)) |
| 1527 | clk_disable_unprepare(motg->core_clk); |
| 1528 | if (!IS_ERR(motg->pclk_src)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1529 | clk_disable_unprepare(motg->pclk_src); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1530 | return ret; |
| 1531 | } |
| 1532 | |
Bill Pemberton | fb4e98a | 2012-11-19 13:26:20 -0500 | [diff] [blame] | 1533 | static int msm_otg_remove(struct platform_device *pdev) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1534 | { |
| 1535 | struct msm_otg *motg = platform_get_drvdata(pdev); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1536 | struct usb_phy *phy = &motg->phy; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1537 | int cnt = 0; |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1538 | |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1539 | if (phy->otg->host || phy->otg->gadget) |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1540 | return -EBUSY; |
| 1541 | |
| 1542 | msm_otg_debugfs_cleanup(); |
Pavankumar Kondeti | d860852 | 2011-05-04 10:19:47 +0530 | [diff] [blame] | 1543 | cancel_delayed_work_sync(&motg->chg_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1544 | cancel_work_sync(&motg->sm_work); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1545 | |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 1546 | pm_runtime_resume(&pdev->dev); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1547 | |
| 1548 | device_init_wakeup(&pdev->dev, 0); |
| 1549 | pm_runtime_disable(&pdev->dev); |
| 1550 | |
Kishon Vijay Abraham I | 662dca5 | 2012-06-22 17:02:46 +0530 | [diff] [blame] | 1551 | usb_remove_phy(phy); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1552 | disable_irq(motg->irq); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1553 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1554 | /* |
| 1555 | * Put PHY in low power mode. |
| 1556 | */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1557 | ulpi_read(phy, 0x14); |
| 1558 | ulpi_write(phy, 0x08, 0x09); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1559 | |
| 1560 | writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC); |
| 1561 | while (cnt < PHY_SUSPEND_TIMEOUT_USEC) { |
| 1562 | if (readl(USB_PORTSC) & PORTSC_PHCD) |
| 1563 | break; |
| 1564 | udelay(1); |
| 1565 | cnt++; |
| 1566 | } |
| 1567 | if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1568 | dev_err(phy->dev, "Unable to suspend PHY\n"); |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1569 | |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1570 | clk_disable_unprepare(motg->pclk); |
| 1571 | clk_disable_unprepare(motg->clk); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1572 | if (!IS_ERR(motg->core_clk)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1573 | clk_disable_unprepare(motg->core_clk); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1574 | if (!IS_ERR(motg->pclk_src)) |
Stephen Boyd | b99a8f6 | 2013-06-17 10:43:10 -0700 | [diff] [blame] | 1575 | clk_disable_unprepare(motg->pclk_src); |
Ivan T. Ivanov | 6b99c68e | 2014-04-28 16:34:08 +0300 | [diff] [blame] | 1576 | |
Anji jonnala | 11aa5c4 | 2011-05-04 10:19:48 +0530 | [diff] [blame] | 1577 | msm_hsusb_ldo_init(motg, 0); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1578 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1579 | pm_runtime_set_suspended(&pdev->dev); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1580 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1581 | return 0; |
| 1582 | } |
| 1583 | |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1584 | #ifdef CONFIG_PM_RUNTIME |
| 1585 | static int msm_otg_runtime_idle(struct device *dev) |
| 1586 | { |
| 1587 | struct msm_otg *motg = dev_get_drvdata(dev); |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1588 | struct usb_otg *otg = motg->phy.otg; |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1589 | |
| 1590 | dev_dbg(dev, "OTG runtime idle\n"); |
| 1591 | |
| 1592 | /* |
| 1593 | * It is observed some times that a spurious interrupt |
| 1594 | * comes when PHY is put into LPM immediately after PHY reset. |
| 1595 | * This 1 sec delay also prevents entering into LPM immediately |
| 1596 | * after asynchronous interrupt. |
| 1597 | */ |
Heikki Krogerus | 1d4c929 | 2012-02-13 13:24:09 +0200 | [diff] [blame] | 1598 | if (otg->phy->state != OTG_STATE_UNDEFINED) |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1599 | pm_schedule_suspend(dev, 1000); |
| 1600 | |
| 1601 | return -EAGAIN; |
| 1602 | } |
| 1603 | |
| 1604 | static int msm_otg_runtime_suspend(struct device *dev) |
| 1605 | { |
| 1606 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 1607 | |
| 1608 | dev_dbg(dev, "OTG runtime suspend\n"); |
| 1609 | return msm_otg_suspend(motg); |
| 1610 | } |
| 1611 | |
| 1612 | static int msm_otg_runtime_resume(struct device *dev) |
| 1613 | { |
| 1614 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 1615 | |
| 1616 | dev_dbg(dev, "OTG runtime resume\n"); |
| 1617 | return msm_otg_resume(motg); |
| 1618 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1619 | #endif |
| 1620 | |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 1621 | #ifdef CONFIG_PM_SLEEP |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1622 | static int msm_otg_pm_suspend(struct device *dev) |
| 1623 | { |
| 1624 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 1625 | |
| 1626 | dev_dbg(dev, "OTG PM suspend\n"); |
| 1627 | return msm_otg_suspend(motg); |
| 1628 | } |
| 1629 | |
| 1630 | static int msm_otg_pm_resume(struct device *dev) |
| 1631 | { |
| 1632 | struct msm_otg *motg = dev_get_drvdata(dev); |
| 1633 | int ret; |
| 1634 | |
| 1635 | dev_dbg(dev, "OTG PM resume\n"); |
| 1636 | |
| 1637 | ret = msm_otg_resume(motg); |
| 1638 | if (ret) |
| 1639 | return ret; |
| 1640 | |
| 1641 | /* |
| 1642 | * Runtime PM Documentation recommends bringing the |
| 1643 | * device to full powered state upon resume. |
| 1644 | */ |
| 1645 | pm_runtime_disable(dev); |
| 1646 | pm_runtime_set_active(dev); |
| 1647 | pm_runtime_enable(dev); |
| 1648 | |
| 1649 | return 0; |
| 1650 | } |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1651 | #endif |
| 1652 | |
| 1653 | static const struct dev_pm_ops msm_otg_dev_pm_ops = { |
Pavankumar Kondeti | 7018773 | 2011-02-15 09:42:34 +0530 | [diff] [blame] | 1654 | SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume) |
| 1655 | SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume, |
| 1656 | msm_otg_runtime_idle) |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1657 | }; |
| 1658 | |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1659 | static struct platform_driver msm_otg_driver = { |
Ivan T. Ivanov | 06a6ec4 | 2014-04-28 16:34:07 +0300 | [diff] [blame] | 1660 | .probe = msm_otg_probe, |
Bill Pemberton | 7690417 | 2012-11-19 13:21:08 -0500 | [diff] [blame] | 1661 | .remove = msm_otg_remove, |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1662 | .driver = { |
| 1663 | .name = DRIVER_NAME, |
| 1664 | .owner = THIS_MODULE, |
Pavankumar Kondeti | 87c0104 | 2010-12-07 17:53:58 +0530 | [diff] [blame] | 1665 | .pm = &msm_otg_dev_pm_ops, |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1666 | }, |
| 1667 | }; |
| 1668 | |
Ivan T. Ivanov | 06a6ec4 | 2014-04-28 16:34:07 +0300 | [diff] [blame] | 1669 | module_platform_driver(msm_otg_driver); |
Pavankumar Kondeti | e0c201f | 2010-12-07 17:53:55 +0530 | [diff] [blame] | 1670 | |
| 1671 | MODULE_LICENSE("GPL v2"); |
| 1672 | MODULE_DESCRIPTION("MSM USB transceiver driver"); |