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