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