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