Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [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 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/clk.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/ioport.h> |
| 23 | #include <linux/pm_runtime.h> |
| 24 | |
| 25 | #include <linux/device.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 26 | #include <linux/pm_qos.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 27 | #include <mach/msm_hsusb_hw.h> |
| 28 | #include <mach/msm72k_otg.h> |
| 29 | #include <mach/msm_hsusb.h> |
| 30 | #include <linux/debugfs.h> |
| 31 | #include <linux/uaccess.h> |
| 32 | #include <mach/clk.h> |
| 33 | #include <mach/msm_xo.h> |
| 34 | |
| 35 | #define MSM_USB_BASE (dev->regs) |
| 36 | #define USB_LINK_RESET_TIMEOUT (msecs_to_jiffies(10)) |
| 37 | #define DRIVER_NAME "msm_otg" |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 38 | static void otg_reset(struct usb_phy *phy, int phy_reset); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | static void msm_otg_set_vbus_state(int online); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 40 | #ifdef CONFIG_USB_EHCI_MSM_72K |
| 41 | static void msm_otg_set_id_state(int id); |
| 42 | #else |
| 43 | static void msm_otg_set_id_state(int id) |
| 44 | { |
| 45 | } |
| 46 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 47 | |
| 48 | struct msm_otg *the_msm_otg; |
| 49 | |
| 50 | static int is_host(void) |
| 51 | { |
| 52 | struct msm_otg *dev = the_msm_otg; |
| 53 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 54 | if (dev->pdata->otg_mode == OTG_ID) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 55 | return (OTGSC_ID & readl(USB_OTGSC)) ? 0 : 1; |
| 56 | else |
| 57 | return !test_bit(ID, &dev->inputs); |
| 58 | } |
| 59 | |
| 60 | static int is_b_sess_vld(void) |
| 61 | { |
| 62 | struct msm_otg *dev = the_msm_otg; |
| 63 | |
| 64 | if (dev->pdata->otg_mode == OTG_ID) |
| 65 | return (OTGSC_BSV & readl(USB_OTGSC)) ? 1 : 0; |
| 66 | else |
| 67 | return test_bit(B_SESS_VLD, &dev->inputs); |
| 68 | } |
| 69 | |
| 70 | static unsigned ulpi_read(struct msm_otg *dev, unsigned reg) |
| 71 | { |
| 72 | unsigned ret, timeout = 100000; |
| 73 | unsigned long flags; |
| 74 | |
| 75 | spin_lock_irqsave(&dev->lock, flags); |
| 76 | |
| 77 | /* initiate read operation */ |
| 78 | writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg), |
| 79 | USB_ULPI_VIEWPORT); |
| 80 | |
| 81 | /* wait for completion */ |
| 82 | while ((readl(USB_ULPI_VIEWPORT) & ULPI_RUN) && (--timeout)) |
| 83 | cpu_relax(); |
| 84 | |
| 85 | if (timeout == 0) { |
| 86 | pr_err("%s: timeout %08x\n", __func__, |
| 87 | readl(USB_ULPI_VIEWPORT)); |
| 88 | spin_unlock_irqrestore(&dev->lock, flags); |
| 89 | return 0xffffffff; |
| 90 | } |
| 91 | ret = ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT)); |
| 92 | |
| 93 | spin_unlock_irqrestore(&dev->lock, flags); |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | static int ulpi_write(struct msm_otg *dev, unsigned val, unsigned reg) |
| 99 | { |
| 100 | unsigned timeout = 10000; |
| 101 | unsigned long flags; |
| 102 | |
| 103 | spin_lock_irqsave(&dev->lock, flags); |
| 104 | |
| 105 | /* initiate write operation */ |
| 106 | writel(ULPI_RUN | ULPI_WRITE | |
| 107 | ULPI_ADDR(reg) | ULPI_DATA(val), |
| 108 | USB_ULPI_VIEWPORT); |
| 109 | |
| 110 | /* wait for completion */ |
| 111 | while ((readl(USB_ULPI_VIEWPORT) & ULPI_RUN) && (--timeout)) |
| 112 | ; |
| 113 | |
| 114 | if (timeout == 0) { |
| 115 | pr_err("%s: timeout\n", __func__); |
| 116 | spin_unlock_irqrestore(&dev->lock, flags); |
| 117 | return -1; |
| 118 | } |
| 119 | |
| 120 | spin_unlock_irqrestore(&dev->lock, flags); |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 125 | static int usb_ulpi_write(struct usb_phy *xceiv, u32 val, u32 reg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 126 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 127 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 128 | |
| 129 | return ulpi_write(dev, val, reg); |
| 130 | } |
| 131 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 132 | static int usb_ulpi_read(struct usb_phy *xceiv, u32 reg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 134 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 135 | |
| 136 | return ulpi_read(dev, reg); |
| 137 | } |
| 138 | |
| 139 | #ifdef CONFIG_USB_EHCI_MSM_72K |
| 140 | static void enable_idgnd(struct msm_otg *dev) |
| 141 | { |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 142 | unsigned temp; |
| 143 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 144 | /* Do nothing if instead of ID pin, USER controls mode switch */ |
| 145 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) |
| 146 | return; |
| 147 | |
| 148 | ulpi_write(dev, (1<<4), 0x0E); |
| 149 | ulpi_write(dev, (1<<4), 0x11); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 150 | ulpi_write(dev, (1<<0), 0x0B); |
| 151 | temp = OTGSC_IDIE | OTGSC_IDPU; |
| 152 | writel_relaxed(readl_relaxed(USB_OTGSC) | temp, USB_OTGSC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static void disable_idgnd(struct msm_otg *dev) |
| 156 | { |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 157 | unsigned temp; |
| 158 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 159 | /* Do nothing if instead of ID pin, USER controls mode switch */ |
| 160 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) |
| 161 | return; |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 162 | temp = OTGSC_IDIE | OTGSC_IDPU; |
| 163 | writel_relaxed(readl_relaxed(USB_OTGSC) & ~temp, USB_OTGSC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | ulpi_write(dev, (1<<4), 0x0F); |
| 165 | ulpi_write(dev, (1<<4), 0x12); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 166 | ulpi_write(dev, (1<<0), 0x0C); |
| 167 | } |
| 168 | #else |
| 169 | static void enable_idgnd(struct msm_otg *dev) |
| 170 | { |
| 171 | } |
| 172 | static void disable_idgnd(struct msm_otg *dev) |
| 173 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 174 | } |
| 175 | #endif |
| 176 | |
| 177 | static void enable_idabc(struct msm_otg *dev) |
| 178 | { |
| 179 | #ifdef CONFIG_USB_MSM_ACA |
| 180 | ulpi_write(dev, (1<<5), 0x0E); |
| 181 | ulpi_write(dev, (1<<5), 0x11); |
| 182 | #endif |
| 183 | } |
| 184 | static void disable_idabc(struct msm_otg *dev) |
| 185 | { |
| 186 | #ifdef CONFIG_USB_MSM_ACA |
| 187 | ulpi_write(dev, (1<<5), 0x0F); |
| 188 | ulpi_write(dev, (1<<5), 0x12); |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | static void enable_sess_valid(struct msm_otg *dev) |
| 193 | { |
| 194 | /* Do nothing if instead of ID pin, USER controls mode switch */ |
| 195 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) |
| 196 | return; |
| 197 | |
| 198 | ulpi_write(dev, (1<<2), 0x0E); |
| 199 | ulpi_write(dev, (1<<2), 0x11); |
| 200 | writel(readl(USB_OTGSC) | OTGSC_BSVIE, USB_OTGSC); |
| 201 | } |
| 202 | |
| 203 | static void disable_sess_valid(struct msm_otg *dev) |
| 204 | { |
| 205 | /* Do nothing if instead of ID pin, USER controls mode switch */ |
| 206 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) |
| 207 | return; |
| 208 | |
| 209 | ulpi_write(dev, (1<<2), 0x0F); |
| 210 | ulpi_write(dev, (1<<2), 0x12); |
| 211 | writel(readl(USB_OTGSC) & ~OTGSC_BSVIE, USB_OTGSC); |
| 212 | } |
| 213 | #ifdef CONFIG_USB_MSM_ACA |
| 214 | static void set_aca_id_inputs(struct msm_otg *dev) |
| 215 | { |
| 216 | u8 phy_ints; |
| 217 | |
| 218 | phy_ints = ulpi_read(dev, 0x13); |
| 219 | if (phy_ints == -ETIMEDOUT) |
| 220 | return; |
| 221 | |
| 222 | pr_debug("phy_ints = %x\n", phy_ints); |
| 223 | clear_bit(ID_A, &dev->inputs); |
| 224 | clear_bit(ID_B, &dev->inputs); |
| 225 | clear_bit(ID_C, &dev->inputs); |
| 226 | if (phy_id_state_a(phy_ints)) { |
| 227 | pr_debug("ID_A set\n"); |
| 228 | set_bit(ID_A, &dev->inputs); |
| 229 | set_bit(A_BUS_REQ, &dev->inputs); |
| 230 | } else if (phy_id_state_b(phy_ints)) { |
| 231 | pr_debug("ID_B set\n"); |
| 232 | set_bit(ID_B, &dev->inputs); |
| 233 | } else if (phy_id_state_c(phy_ints)) { |
| 234 | pr_debug("ID_C set\n"); |
| 235 | set_bit(ID_C, &dev->inputs); |
| 236 | } |
| 237 | if (is_b_sess_vld()) |
| 238 | set_bit(B_SESS_VLD, &dev->inputs); |
| 239 | else |
| 240 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 241 | } |
| 242 | #define get_aca_bmaxpower(dev) (dev->b_max_power) |
| 243 | #define set_aca_bmaxpower(dev, power) (dev->b_max_power = power) |
| 244 | #else |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 245 | static void set_aca_id_inputs(struct msm_otg *dev) |
| 246 | { |
| 247 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 248 | #define get_aca_bmaxpower(dev) 0 |
| 249 | #define set_aca_bmaxpower(dev, power) |
| 250 | #endif |
| 251 | static inline void set_pre_emphasis_level(struct msm_otg *dev) |
| 252 | { |
| 253 | unsigned res = 0; |
| 254 | |
| 255 | if (!dev->pdata || dev->pdata->pemp_level == PRE_EMPHASIS_DEFAULT) |
| 256 | return; |
| 257 | |
| 258 | res = ulpi_read(dev, ULPI_CONFIG_REG3); |
| 259 | res &= ~(ULPI_PRE_EMPHASIS_MASK); |
| 260 | if (dev->pdata->pemp_level != PRE_EMPHASIS_DISABLE) |
| 261 | res |= dev->pdata->pemp_level; |
| 262 | ulpi_write(dev, res, ULPI_CONFIG_REG3); |
| 263 | } |
| 264 | |
| 265 | static inline void set_hsdrv_slope(struct msm_otg *dev) |
| 266 | { |
| 267 | unsigned res = 0; |
| 268 | |
| 269 | if (!dev->pdata || dev->pdata->hsdrvslope == HS_DRV_SLOPE_DEFAULT) |
| 270 | return; |
| 271 | |
| 272 | res = ulpi_read(dev, ULPI_CONFIG_REG3); |
| 273 | res &= ~(ULPI_HSDRVSLOPE_MASK); |
| 274 | res |= (dev->pdata->hsdrvslope & ULPI_HSDRVSLOPE_MASK); |
| 275 | ulpi_write(dev, res, ULPI_CONFIG_REG3); |
| 276 | } |
| 277 | |
| 278 | static inline void set_cdr_auto_reset(struct msm_otg *dev) |
| 279 | { |
| 280 | unsigned res = 0; |
| 281 | |
| 282 | if (!dev->pdata || dev->pdata->cdr_autoreset == CDR_AUTO_RESET_DEFAULT) |
| 283 | return; |
| 284 | |
| 285 | res = ulpi_read(dev, ULPI_DIGOUT_CTRL); |
| 286 | if (dev->pdata->cdr_autoreset == CDR_AUTO_RESET_ENABLE) |
| 287 | res &= ~ULPI_CDR_AUTORESET; |
| 288 | else |
| 289 | res |= ULPI_CDR_AUTORESET; |
| 290 | ulpi_write(dev, res, ULPI_DIGOUT_CTRL); |
| 291 | } |
| 292 | |
| 293 | static inline void set_se1_gating(struct msm_otg *dev) |
| 294 | { |
| 295 | unsigned res = 0; |
| 296 | |
| 297 | if (!dev->pdata || dev->pdata->se1_gating == SE1_GATING_DEFAULT) |
| 298 | return; |
| 299 | |
| 300 | res = ulpi_read(dev, ULPI_DIGOUT_CTRL); |
| 301 | if (dev->pdata->se1_gating == SE1_GATING_ENABLE) |
| 302 | res &= ~ULPI_SE1_GATE; |
| 303 | else |
| 304 | res |= ULPI_SE1_GATE; |
| 305 | ulpi_write(dev, res, ULPI_DIGOUT_CTRL); |
| 306 | } |
| 307 | static inline void set_driver_amplitude(struct msm_otg *dev) |
| 308 | { |
| 309 | unsigned res = 0; |
| 310 | |
| 311 | if (!dev->pdata || dev->pdata->drv_ampl == HS_DRV_AMPLITUDE_DEFAULT) |
| 312 | return; |
| 313 | |
| 314 | res = ulpi_read(dev, ULPI_CONFIG_REG2); |
| 315 | res &= ~ULPI_DRV_AMPL_MASK; |
| 316 | if (dev->pdata->drv_ampl != HS_DRV_AMPLITUDE_ZERO_PERCENT) |
| 317 | res |= dev->pdata->drv_ampl; |
| 318 | ulpi_write(dev, res, ULPI_CONFIG_REG2); |
| 319 | } |
| 320 | |
| 321 | static const char *state_string(enum usb_otg_state state) |
| 322 | { |
| 323 | switch (state) { |
| 324 | case OTG_STATE_A_IDLE: return "a_idle"; |
| 325 | case OTG_STATE_A_WAIT_VRISE: return "a_wait_vrise"; |
| 326 | case OTG_STATE_A_WAIT_BCON: return "a_wait_bcon"; |
| 327 | case OTG_STATE_A_HOST: return "a_host"; |
| 328 | case OTG_STATE_A_SUSPEND: return "a_suspend"; |
| 329 | case OTG_STATE_A_PERIPHERAL: return "a_peripheral"; |
| 330 | case OTG_STATE_A_WAIT_VFALL: return "a_wait_vfall"; |
| 331 | case OTG_STATE_A_VBUS_ERR: return "a_vbus_err"; |
| 332 | case OTG_STATE_B_IDLE: return "b_idle"; |
| 333 | case OTG_STATE_B_SRP_INIT: return "b_srp_init"; |
| 334 | case OTG_STATE_B_PERIPHERAL: return "b_peripheral"; |
| 335 | case OTG_STATE_B_WAIT_ACON: return "b_wait_acon"; |
| 336 | case OTG_STATE_B_HOST: return "b_host"; |
| 337 | default: return "UNDEFINED"; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static const char *timer_string(int bit) |
| 342 | { |
| 343 | switch (bit) { |
| 344 | case A_WAIT_VRISE: return "a_wait_vrise"; |
| 345 | case A_WAIT_VFALL: return "a_wait_vfall"; |
| 346 | case B_SRP_FAIL: return "b_srp_fail"; |
| 347 | case A_WAIT_BCON: return "a_wait_bcon"; |
| 348 | case A_AIDL_BDIS: return "a_aidl_bdis"; |
| 349 | case A_BIDL_ADIS: return "a_bidl_adis"; |
| 350 | case B_ASE0_BRST: return "b_ase0_brst"; |
| 351 | default: return "UNDEFINED"; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /* Prevent idle power collapse(pc) while operating in peripheral mode */ |
| 356 | static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote) |
| 357 | { |
| 358 | struct msm_otg_platform_data *pdata = dev->pdata; |
| 359 | u32 swfi_latency = 0; |
| 360 | |
| 361 | if (pdata) |
| 362 | swfi_latency = pdata->swfi_latency + 1; |
| 363 | |
| 364 | if (vote) |
| 365 | pm_qos_update_request(&pdata->pm_qos_req_dma, |
| 366 | swfi_latency); |
| 367 | else |
| 368 | pm_qos_update_request(&pdata->pm_qos_req_dma, |
| 369 | PM_QOS_DEFAULT_VALUE); |
| 370 | } |
| 371 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | /* Controller gives interrupt for every 1 mesc if 1MSIE is set in OTGSC. |
| 373 | * This interrupt can be used as a timer source and OTG timers can be |
| 374 | * implemented. But hrtimers on MSM hardware can give atleast 1/32 KHZ |
| 375 | * precision. This precision is more than enough for OTG timers. |
| 376 | */ |
| 377 | static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *_timer) |
| 378 | { |
| 379 | struct msm_otg *dev = container_of(_timer, struct msm_otg, timer); |
| 380 | |
| 381 | /* Phy lockup issues are observed when VBUS Valid interrupt is |
| 382 | * enabled. Hence set A_VBUS_VLD upon timer exipration. |
| 383 | */ |
| 384 | if (dev->active_tmout == A_WAIT_VRISE) |
| 385 | set_bit(A_VBUS_VLD, &dev->inputs); |
| 386 | else |
| 387 | set_bit(dev->active_tmout, &dev->tmouts); |
| 388 | |
| 389 | pr_debug("expired %s timer\n", timer_string(dev->active_tmout)); |
| 390 | queue_work(dev->wq, &dev->sm_work); |
| 391 | return HRTIMER_NORESTART; |
| 392 | } |
| 393 | |
| 394 | static void msm_otg_del_timer(struct msm_otg *dev) |
| 395 | { |
| 396 | int bit = dev->active_tmout; |
| 397 | |
| 398 | pr_debug("deleting %s timer. remaining %lld msec \n", timer_string(bit), |
| 399 | div_s64(ktime_to_us(hrtimer_get_remaining(&dev->timer)), |
| 400 | 1000)); |
| 401 | hrtimer_cancel(&dev->timer); |
| 402 | clear_bit(bit, &dev->tmouts); |
| 403 | } |
| 404 | |
| 405 | static void msm_otg_start_timer(struct msm_otg *dev, int time, int bit) |
| 406 | { |
| 407 | clear_bit(bit, &dev->tmouts); |
| 408 | dev->active_tmout = bit; |
| 409 | pr_debug("starting %s timer\n", timer_string(bit)); |
| 410 | hrtimer_start(&dev->timer, |
| 411 | ktime_set(time / 1000, (time % 1000) * 1000000), |
| 412 | HRTIMER_MODE_REL); |
| 413 | } |
| 414 | |
| 415 | /* No two otg timers run in parallel. So one hrtimer is sufficient */ |
| 416 | static void msm_otg_init_timer(struct msm_otg *dev) |
| 417 | { |
| 418 | hrtimer_init(&dev->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 419 | dev->timer.function = msm_otg_timer_func; |
| 420 | } |
| 421 | |
| 422 | static const char *event_string(enum usb_otg_event event) |
| 423 | { |
| 424 | switch (event) { |
| 425 | case OTG_EVENT_DEV_CONN_TMOUT: |
| 426 | return "DEV_CONN_TMOUT"; |
| 427 | case OTG_EVENT_NO_RESP_FOR_HNP_ENABLE: |
| 428 | return "NO_RESP_FOR_HNP_ENABLE"; |
| 429 | case OTG_EVENT_HUB_NOT_SUPPORTED: |
| 430 | return "HUB_NOT_SUPPORTED"; |
| 431 | case OTG_EVENT_DEV_NOT_SUPPORTED: |
| 432 | return "DEV_NOT_SUPPORTED,"; |
| 433 | case OTG_EVENT_HNP_FAILED: |
| 434 | return "HNP_FAILED"; |
| 435 | case OTG_EVENT_NO_RESP_FOR_SRP: |
| 436 | return "NO_RESP_FOR_SRP"; |
| 437 | default: |
| 438 | return "UNDEFINED"; |
| 439 | } |
| 440 | } |
| 441 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 442 | static int msm_otg_send_event(struct usb_otg *otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | enum usb_otg_event event) |
| 444 | { |
| 445 | char module_name[16]; |
| 446 | char udev_event[128]; |
| 447 | char *envp[] = { module_name, udev_event, NULL }; |
| 448 | int ret; |
| 449 | |
| 450 | pr_debug("sending %s event\n", event_string(event)); |
| 451 | |
| 452 | snprintf(module_name, 16, "MODULE=%s", DRIVER_NAME); |
| 453 | snprintf(udev_event, 128, "EVENT=%s", event_string(event)); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 454 | ret = kobject_uevent_env(&otg->phy->dev->kobj, KOBJ_CHANGE, envp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | if (ret < 0) |
| 456 | pr_info("uevent sending failed with ret = %d\n", ret); |
| 457 | return ret; |
| 458 | } |
| 459 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 460 | static int msm_otg_start_hnp(struct usb_otg *otg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 461 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 462 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 463 | enum usb_otg_state state; |
| 464 | unsigned long flags; |
| 465 | |
| 466 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 467 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 468 | spin_unlock_irqrestore(&dev->lock, flags); |
| 469 | |
| 470 | if (state != OTG_STATE_A_HOST) { |
| 471 | pr_err("HNP can not be initiated in %s state\n", |
| 472 | state_string(state)); |
| 473 | return -EINVAL; |
| 474 | } |
| 475 | |
| 476 | pr_debug("A-Host: HNP initiated\n"); |
| 477 | clear_bit(A_BUS_REQ, &dev->inputs); |
| 478 | wake_lock(&dev->wlock); |
| 479 | queue_work(dev->wq, &dev->sm_work); |
| 480 | return 0; |
| 481 | } |
| 482 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 483 | static int msm_otg_start_srp(struct usb_otg *otg) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 484 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 485 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 486 | u32 val; |
| 487 | int ret = 0; |
| 488 | enum usb_otg_state state; |
| 489 | unsigned long flags; |
| 490 | |
| 491 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 492 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 493 | spin_unlock_irqrestore(&dev->lock, flags); |
| 494 | |
| 495 | if (state != OTG_STATE_B_IDLE) { |
| 496 | pr_err("SRP can not be initiated in %s state\n", |
| 497 | state_string(state)); |
| 498 | ret = -EINVAL; |
| 499 | goto out; |
| 500 | } |
| 501 | |
| 502 | if ((jiffies - dev->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) { |
| 503 | pr_debug("initial conditions of SRP are not met. Try again" |
| 504 | "after some time\n"); |
| 505 | ret = -EAGAIN; |
| 506 | goto out; |
| 507 | } |
| 508 | |
| 509 | /* Harware auto assist data pulsing: Data pulse is given |
| 510 | * for 7msec; wait for vbus |
| 511 | */ |
| 512 | val = readl(USB_OTGSC); |
| 513 | writel((val & ~OTGSC_INTR_STS_MASK) | OTGSC_HADP, USB_OTGSC); |
| 514 | |
| 515 | /* VBUS plusing is obsoleted in OTG 2.0 supplement */ |
| 516 | out: |
| 517 | return ret; |
| 518 | } |
| 519 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 520 | static int msm_otg_set_power(struct usb_phy *xceiv, unsigned mA) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 521 | { |
| 522 | static enum chg_type curr_chg = USB_CHG_TYPE__INVALID; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 523 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 524 | struct msm_otg_platform_data *pdata = dev->pdata; |
| 525 | enum chg_type new_chg = atomic_read(&dev->chg_type); |
| 526 | unsigned charge = mA; |
| 527 | |
| 528 | /* Call chg_connected only if the charger has changed */ |
| 529 | if (new_chg != curr_chg && pdata->chg_connected) { |
| 530 | curr_chg = new_chg; |
| 531 | pdata->chg_connected(new_chg); |
| 532 | } |
| 533 | |
| 534 | /* Always use USB_IDCHG_MAX for charging in ID_B and ID_C */ |
| 535 | if (test_bit(ID_C, &dev->inputs) || |
| 536 | test_bit(ID_B, &dev->inputs)) |
| 537 | charge = USB_IDCHG_MAX; |
| 538 | |
| 539 | pr_debug("Charging with %dmA current\n", charge); |
| 540 | /* Call vbus_draw only if the charger is of known type and also |
| 541 | * ignore request to stop charging as a result of suspend interrupt |
| 542 | * when wall-charger is used. |
| 543 | */ |
| 544 | if (pdata->chg_vbus_draw && new_chg != USB_CHG_TYPE__INVALID && |
| 545 | (charge || new_chg != USB_CHG_TYPE__WALLCHARGER)) |
| 546 | pdata->chg_vbus_draw(charge); |
| 547 | |
| 548 | if (new_chg == USB_CHG_TYPE__WALLCHARGER) { |
| 549 | wake_lock(&dev->wlock); |
| 550 | queue_work(dev->wq, &dev->sm_work); |
| 551 | } |
| 552 | |
| 553 | return 0; |
| 554 | } |
| 555 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 556 | static int msm_otg_set_clk(struct usb_phy *xceiv, int on) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 557 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 558 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | |
| 560 | if (!dev || (dev != the_msm_otg)) |
| 561 | return -ENODEV; |
| 562 | |
| 563 | if (on) |
| 564 | /* enable clocks */ |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 565 | clk_prepare_enable(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 566 | else |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 567 | clk_disable_unprepare(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 568 | |
| 569 | return 0; |
| 570 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 571 | static void msm_otg_start_peripheral(struct usb_otg *otg, int on) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 572 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 573 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 574 | struct msm_otg_platform_data *pdata = dev->pdata; |
| 575 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 576 | if (!otg->gadget) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 577 | return; |
| 578 | |
| 579 | if (on) { |
| 580 | if (pdata->setup_gpio) |
| 581 | pdata->setup_gpio(USB_SWITCH_PERIPHERAL); |
| 582 | /* vote for minimum dma_latency to prevent idle |
| 583 | * power collapse(pc) while running in peripheral mode. |
| 584 | */ |
| 585 | otg_pm_qos_update_latency(dev, 1); |
| 586 | |
| 587 | /* increment the clk reference count so that |
| 588 | * it would be still on when disabled from |
| 589 | * low power mode routine |
| 590 | */ |
| 591 | if (dev->pdata->pclk_required_during_lpm) |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 592 | clk_prepare_enable(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 593 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 594 | usb_gadget_vbus_connect(otg->gadget); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 595 | } else { |
| 596 | atomic_set(&dev->chg_type, USB_CHG_TYPE__INVALID); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 597 | usb_gadget_vbus_disconnect(otg->gadget); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 598 | |
| 599 | /* decrement the clk reference count so that |
| 600 | * it would be off when disabled from |
| 601 | * low power mode routine |
| 602 | */ |
| 603 | if (dev->pdata->pclk_required_during_lpm) |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 604 | clk_disable_unprepare(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 605 | |
| 606 | otg_pm_qos_update_latency(dev, 0); |
| 607 | if (pdata->setup_gpio) |
| 608 | pdata->setup_gpio(USB_SWITCH_DISABLE); |
| 609 | } |
| 610 | } |
| 611 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 612 | static void msm_otg_start_host(struct usb_otg *otg, int on) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 613 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 614 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 615 | struct msm_otg_platform_data *pdata = dev->pdata; |
| 616 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 617 | if (!otg->host) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 618 | return; |
| 619 | |
| 620 | if (dev->start_host) { |
| 621 | /* Some targets, e.g. ST1.5, use GPIO to choose b/w connector */ |
| 622 | if (on && pdata->setup_gpio) |
| 623 | pdata->setup_gpio(USB_SWITCH_HOST); |
| 624 | |
| 625 | /* increment or decrement the clk reference count |
| 626 | * to avoid usb h/w lockup issues when low power |
| 627 | * mode is initiated and vbus is on. |
| 628 | */ |
| 629 | if (dev->pdata->pclk_required_during_lpm) { |
| 630 | if (on) |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 631 | clk_prepare_enable(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 632 | else |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 633 | clk_disable_unprepare(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 636 | dev->start_host(otg->host, on); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | |
| 638 | if (!on && pdata->setup_gpio) |
| 639 | pdata->setup_gpio(USB_SWITCH_DISABLE); |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | static int msm_otg_suspend(struct msm_otg *dev) |
| 644 | { |
| 645 | unsigned long timeout; |
| 646 | bool host_bus_suspend; |
| 647 | unsigned ret; |
| 648 | enum chg_type chg_type = atomic_read(&dev->chg_type); |
| 649 | unsigned long flags; |
| 650 | |
| 651 | disable_irq(dev->irq); |
| 652 | if (atomic_read(&dev->in_lpm)) |
| 653 | goto out; |
| 654 | #ifdef CONFIG_USB_MSM_ACA |
| 655 | /* |
| 656 | * ACA interrupts are disabled before entering into LPM. |
| 657 | * If LPM is allowed in host mode with accessory charger |
| 658 | * connected or only accessory charger is connected, |
| 659 | * there is a chance that charger is removed and we will |
| 660 | * not know about it. |
| 661 | * |
| 662 | * REVISIT |
| 663 | * |
| 664 | * Allowing LPM in case of gadget bus suspend is tricky. |
| 665 | * Bus suspend can happen in two states. |
| 666 | * 1. ID_float: Allowing LPM has pros and cons. If LPM is allowed |
| 667 | * and accessory charger is connected, we miss ID_float --> ID_C |
| 668 | * transition where we could draw large amount of current |
| 669 | * compared to the suspend current. |
| 670 | * 2. ID_C: We can not allow LPM. If accessory charger is removed |
| 671 | * we should not draw more than what host could supply which will |
| 672 | * be less compared to accessory charger. |
| 673 | * |
| 674 | * For simplicity, LPM is not allowed in bus suspend. |
| 675 | */ |
| 676 | #ifndef CONFIG_USB_MSM_STANDARD_ACA |
| 677 | /* |
| 678 | * RID_A and IdGnd states are only possible with standard ACA. We can |
| 679 | * exit from low power mode with !BSV or IdGnd interrupt. Hence LPM |
| 680 | * is allowed. |
| 681 | */ |
| 682 | if ((test_bit(ID, &dev->inputs) && test_bit(B_SESS_VLD, &dev->inputs) && |
| 683 | chg_type != USB_CHG_TYPE__WALLCHARGER) || |
| 684 | test_bit(ID_A, &dev->inputs)) |
| 685 | goto out; |
| 686 | #endif |
| 687 | /* Disable ID_abc interrupts else it causes spurious interrupt */ |
| 688 | disable_idabc(dev); |
| 689 | #endif |
| 690 | ulpi_read(dev, 0x14);/* clear PHY interrupt latch register */ |
| 691 | |
| 692 | /* |
| 693 | * Turn on PHY comparators if, |
| 694 | * 1. USB wall charger is connected (bus suspend is not supported) |
| 695 | * 2. Host bus suspend |
| 696 | * 3. host is supported, but, id is not routed to pmic |
| 697 | * 4. peripheral is supported, but, vbus is not routed to pmic |
| 698 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 699 | host_bus_suspend = dev->phy.otg->host && is_host(); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 700 | |
| 701 | /* |
| 702 | * Configure the PMIC ID only in case of cable disconnect. |
| 703 | * PMIC doesn't generate interrupt for ID_GND to ID_A |
| 704 | * transistion. hence use the PHY ID cricuit. |
| 705 | */ |
| 706 | if (dev->pdata->pmic_id_notif_init && !host_bus_suspend && |
| 707 | !test_bit(ID_A, &dev->inputs)) { |
| 708 | disable_idgnd(dev); |
| 709 | ret = dev->pdata->pmic_id_notif_init( |
| 710 | &msm_otg_set_id_state, 1); |
| 711 | if (!ret) { |
| 712 | dev->pmic_id_notif_supp = 1; |
| 713 | if (dev->pdata->pmic_id_irq) |
| 714 | dev->id_irq = dev->pdata->pmic_id_irq; |
| 715 | } else if (ret == -ENOTSUPP) { |
| 716 | pr_debug("%s:USB ID is not routed to pmic", |
| 717 | __func__); |
| 718 | enable_idgnd(dev); |
| 719 | } else { |
| 720 | pr_err("%s: pmic_id_ notif_init failed err:%d", |
| 721 | __func__, ret); |
| 722 | } |
| 723 | } |
| 724 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 725 | if ((dev->phy.otg->gadget && chg_type == USB_CHG_TYPE__WALLCHARGER) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 726 | host_bus_suspend || |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 727 | (dev->phy.otg->host && !dev->pmic_id_notif_supp) || |
| 728 | (dev->phy.otg->gadget && !dev->pmic_vbus_notif_supp)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 729 | ulpi_write(dev, 0x01, 0x30); |
| 730 | } |
| 731 | |
| 732 | ulpi_write(dev, 0x08, 0x09);/* turn off PLL on integrated phy */ |
| 733 | |
| 734 | timeout = jiffies + msecs_to_jiffies(500); |
| 735 | disable_phy_clk(); |
| 736 | while (!is_phy_clk_disabled()) { |
| 737 | if (time_after(jiffies, timeout)) { |
| 738 | pr_err("%s: Unable to suspend phy\n", __func__); |
| 739 | /* |
| 740 | * Start otg state machine in default state upon |
| 741 | * phy suspend failure*/ |
| 742 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 743 | dev->phy.state = OTG_STATE_UNDEFINED; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 744 | spin_unlock_irqrestore(&dev->lock, flags); |
| 745 | queue_work(dev->wq, &dev->sm_work); |
| 746 | goto out; |
| 747 | } |
| 748 | msleep(1); |
| 749 | /* check if there are any pending interrupts*/ |
| 750 | if (((readl(USB_OTGSC) & OTGSC_INTR_MASK) >> 8) & |
| 751 | readl(USB_OTGSC)) { |
| 752 | enable_idabc(dev); |
| 753 | goto out; |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | writel(readl(USB_USBCMD) | ASYNC_INTR_CTRL | ULPI_STP_CTRL, USB_USBCMD); |
| 758 | /* Ensure that above operation is completed before turning off clocks */ |
| 759 | mb(); |
| 760 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 761 | if (dev->iface_clk) |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 762 | clk_disable_unprepare(dev->iface_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 763 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 764 | clk_disable_unprepare(dev->core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 765 | /* usb phy no more require TCXO clock, hence vote for TCXO disable*/ |
| 766 | ret = msm_xo_mode_vote(dev->xo_handle, MSM_XO_MODE_OFF); |
| 767 | if (ret) |
| 768 | pr_err("%s failed to devote for" |
| 769 | "TCXO D1 buffer%d\n", __func__, ret); |
| 770 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 771 | if (device_may_wakeup(dev->phy.dev)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 772 | enable_irq_wake(dev->irq); |
| 773 | if (dev->vbus_on_irq) |
| 774 | enable_irq_wake(dev->vbus_on_irq); |
Anji jonnala | eb9e60d | 2011-10-05 12:19:46 +0530 | [diff] [blame] | 775 | if (dev->id_irq) |
| 776 | enable_irq_wake(dev->id_irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 779 | atomic_set(&dev->in_lpm, 1); |
| 780 | |
Anji jonnala | 3498c47 | 2011-11-04 13:30:14 +0530 | [diff] [blame] | 781 | /* |
| 782 | * TODO: put regulators in low power mode by assuming that |
| 783 | * regulators are brought back to active state before PHY |
| 784 | * becomes active. But this assumption becomes wrong in case of |
| 785 | * ACA charger where PHY itself will generate the wakeup |
| 786 | * interrupt. This creates a small window where PHY regulators |
| 787 | * are in LPM but PHY is in active state and this patch assumes |
| 788 | * that there is no harm with this. Till hw folks confirms this |
| 789 | * put regulators in lpm. |
| 790 | */ |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 791 | if (!host_bus_suspend && dev->pmic_vbus_notif_supp && |
| 792 | !test_bit(ID_A, &dev->inputs)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | pr_debug("phy can power collapse: (%d)\n", |
| 794 | can_phy_power_collapse(dev)); |
| 795 | if (can_phy_power_collapse(dev) && dev->pdata->ldo_enable) { |
| 796 | pr_debug("disabling the regulators\n"); |
| 797 | dev->pdata->ldo_enable(0); |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* phy can interrupts when vddcx is at 0.75, so irrespective |
| 802 | * of pmic notification support, configure vddcx @0.75 |
| 803 | */ |
| 804 | if (dev->pdata->config_vddcx) |
| 805 | dev->pdata->config_vddcx(0); |
| 806 | pr_info("%s: usb in low power mode\n", __func__); |
| 807 | |
| 808 | out: |
| 809 | enable_irq(dev->irq); |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static int msm_otg_resume(struct msm_otg *dev) |
| 815 | { |
| 816 | unsigned temp; |
| 817 | unsigned ret; |
| 818 | |
| 819 | if (!atomic_read(&dev->in_lpm)) |
| 820 | return 0; |
| 821 | /* vote for vddcx, as PHY cannot tolerate vddcx below 1.0V */ |
| 822 | if (dev->pdata->config_vddcx) { |
| 823 | ret = dev->pdata->config_vddcx(1); |
| 824 | if (ret) { |
| 825 | pr_err("%s: unable to enable vddcx digital core:%d\n", |
| 826 | __func__, ret); |
| 827 | } |
| 828 | } |
| 829 | if (dev->pdata->ldo_set_voltage) |
| 830 | dev->pdata->ldo_set_voltage(3400); |
| 831 | |
| 832 | /* Vote for TCXO when waking up the phy */ |
| 833 | ret = msm_xo_mode_vote(dev->xo_handle, MSM_XO_MODE_ON); |
| 834 | if (ret) |
| 835 | pr_err("%s failed to vote for" |
| 836 | "TCXO D1 buffer%d\n", __func__, ret); |
| 837 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 838 | clk_prepare_enable(dev->core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 839 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 840 | if (dev->iface_clk) |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 841 | clk_prepare_enable(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 842 | |
| 843 | temp = readl(USB_USBCMD); |
| 844 | temp &= ~ASYNC_INTR_CTRL; |
| 845 | temp &= ~ULPI_STP_CTRL; |
| 846 | writel(temp, USB_USBCMD); |
| 847 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 848 | if (device_may_wakeup(dev->phy.dev)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 849 | disable_irq_wake(dev->irq); |
| 850 | if (dev->vbus_on_irq) |
| 851 | disable_irq_wake(dev->vbus_on_irq); |
Anji jonnala | eb9e60d | 2011-10-05 12:19:46 +0530 | [diff] [blame] | 852 | if (dev->id_irq) |
| 853 | disable_irq_wake(dev->id_irq); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | atomic_set(&dev->in_lpm, 0); |
| 857 | |
| 858 | pr_info("%s: usb exited from low power mode\n", __func__); |
| 859 | |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | static void msm_otg_get_resume(struct msm_otg *dev) |
| 864 | { |
| 865 | #ifdef CONFIG_PM_RUNTIME |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 866 | pm_runtime_get_noresume(dev->phy.dev); |
| 867 | pm_runtime_resume(dev->phy.dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 868 | #else |
| 869 | msm_otg_resume(dev); |
| 870 | #endif |
| 871 | } |
| 872 | |
| 873 | static void msm_otg_put_suspend(struct msm_otg *dev) |
| 874 | { |
| 875 | #ifdef CONFIG_PM_RUNTIME |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 876 | pm_runtime_put_sync(dev->phy.dev); |
Pavankumar Kondeti | dc60f12 | 2011-09-13 11:17:16 +0530 | [diff] [blame] | 877 | if (!atomic_read(&dev->in_lpm)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 878 | pm_runtime_get_sync(dev->phy.dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 879 | #else |
| 880 | msm_otg_suspend(dev); |
| 881 | #endif |
| 882 | } |
| 883 | |
| 884 | static void msm_otg_resume_w(struct work_struct *w) |
| 885 | { |
| 886 | struct msm_otg *dev = container_of(w, struct msm_otg, otg_resume_work); |
| 887 | unsigned long timeout; |
| 888 | |
Anji jonnala | 3498c47 | 2011-11-04 13:30:14 +0530 | [diff] [blame] | 889 | if (can_phy_power_collapse(dev) && dev->pdata->ldo_enable) |
| 890 | dev->pdata->ldo_enable(1); |
| 891 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 892 | if (pm_runtime_enabled(dev->phy.dev)) { |
Chiranjeevi Velempati | d515ac1 | 2012-05-22 12:08:13 +0530 | [diff] [blame] | 893 | msm_otg_get_resume(dev); |
| 894 | } else { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 895 | pm_runtime_get_noresume(dev->phy.dev); |
Chiranjeevi Velempati | d515ac1 | 2012-05-22 12:08:13 +0530 | [diff] [blame] | 896 | msm_otg_resume(dev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 897 | pm_runtime_set_active(dev->phy.dev); |
Chiranjeevi Velempati | d515ac1 | 2012-05-22 12:08:13 +0530 | [diff] [blame] | 898 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 899 | |
| 900 | if (!is_phy_clk_disabled()) |
| 901 | goto phy_resumed; |
| 902 | |
| 903 | timeout = jiffies + usecs_to_jiffies(100); |
| 904 | enable_phy_clk(); |
| 905 | while (is_phy_clk_disabled() || !is_phy_active()) { |
| 906 | if (time_after(jiffies, timeout)) { |
| 907 | pr_err("%s: Unable to wakeup phy. is_phy_active: %x\n", |
| 908 | __func__, !!is_phy_active()); |
| 909 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 910 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 911 | break; |
| 912 | } |
| 913 | udelay(10); |
| 914 | } |
| 915 | |
| 916 | phy_resumed: |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 917 | /* |
| 918 | * It is observed that BSVIS may get set immediatly |
| 919 | * after PHY becomes active upon micro-B cable connect. |
| 920 | * But BSVIS might get cleared by below enable_idgnd |
| 921 | * function which causes hw to not generate the BSV interrupt. |
| 922 | * Hence check for BSV interrupt explictly and schedule the |
| 923 | * work. |
| 924 | */ |
| 925 | if (readl_relaxed(USB_OTGSC) & OTGSC_BSVIS) { |
| 926 | set_bit(B_SESS_VLD, &dev->inputs); |
| 927 | queue_work(dev->wq, &dev->sm_work); |
| 928 | } |
| 929 | if (dev->pmic_id_notif_supp) { |
| 930 | dev->pdata->pmic_id_notif_init(&msm_otg_set_id_state, 0); |
| 931 | dev->pmic_id_notif_supp = 0; |
| 932 | enable_idgnd(dev); |
| 933 | } |
| 934 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 935 | /* Enable Idabc interrupts as these were disabled before entering LPM */ |
| 936 | enable_idabc(dev); |
| 937 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 938 | /* |
| 939 | * There is corner case where host won't be resumed |
| 940 | * while transitioning from ID_GND to ID_A. In that |
| 941 | * IDGND might have cleared and ID_A might not have updated |
| 942 | * yet. Hence update the ACA states explicitly. |
| 943 | */ |
| 944 | set_aca_id_inputs(dev); |
| 945 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 946 | /* If resume signalling finishes before lpm exit, PCD is not set in |
| 947 | * USBSTS register. Drive resume signal to the downstream device now |
| 948 | * so that host driver can process the upcoming port change interrupt.*/ |
| 949 | if (is_host() || test_bit(ID_A, &dev->inputs)) { |
| 950 | writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 951 | msm_otg_start_host(dev->phy.otg, REQUEST_RESUME); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | /* Enable irq which was disabled before scheduling this work. |
| 955 | * But don't release wake_lock, as we got async interrupt and |
| 956 | * there will be some work pending for OTG state machine. |
| 957 | */ |
| 958 | enable_irq(dev->irq); |
| 959 | } |
| 960 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 961 | static int msm_otg_set_suspend(struct usb_phy *xceiv, int suspend) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 962 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 963 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 964 | enum usb_otg_state state; |
| 965 | unsigned long flags; |
| 966 | |
| 967 | if (!dev || (dev != the_msm_otg)) |
| 968 | return -ENODEV; |
| 969 | |
| 970 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 971 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 972 | spin_unlock_irqrestore(&dev->lock, flags); |
| 973 | |
| 974 | pr_debug("suspend request in state: %s\n", |
| 975 | state_string(state)); |
| 976 | |
| 977 | if (suspend) { |
| 978 | switch (state) { |
| 979 | #ifndef CONFIG_MSM_OTG_ENABLE_A_WAIT_BCON_TIMEOUT |
| 980 | case OTG_STATE_A_WAIT_BCON: |
| 981 | if (test_bit(ID_A, &dev->inputs)) |
| 982 | msm_otg_set_power(xceiv, USB_IDCHG_MIN - 100); |
| 983 | msm_otg_put_suspend(dev); |
| 984 | break; |
| 985 | #endif |
| 986 | case OTG_STATE_A_HOST: |
| 987 | clear_bit(A_BUS_REQ, &dev->inputs); |
| 988 | wake_lock(&dev->wlock); |
| 989 | queue_work(dev->wq, &dev->sm_work); |
| 990 | break; |
| 991 | case OTG_STATE_B_PERIPHERAL: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 992 | if (xceiv->otg->gadget->b_hnp_enable) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | set_bit(A_BUS_SUSPEND, &dev->inputs); |
| 994 | set_bit(B_BUS_REQ, &dev->inputs); |
| 995 | wake_lock(&dev->wlock); |
| 996 | queue_work(dev->wq, &dev->sm_work); |
| 997 | } |
| 998 | break; |
| 999 | case OTG_STATE_A_PERIPHERAL: |
| 1000 | msm_otg_start_timer(dev, TA_BIDL_ADIS, |
| 1001 | A_BIDL_ADIS); |
| 1002 | break; |
| 1003 | default: |
| 1004 | break; |
| 1005 | } |
| 1006 | } else { |
| 1007 | unsigned long timeout; |
| 1008 | |
| 1009 | switch (state) { |
| 1010 | case OTG_STATE_A_PERIPHERAL: |
| 1011 | /* A-peripheral observed activity on bus. |
| 1012 | * clear A_BIDL_ADIS timer. |
| 1013 | */ |
| 1014 | msm_otg_del_timer(dev); |
| 1015 | break; |
| 1016 | case OTG_STATE_A_SUSPEND: |
| 1017 | /* Remote wakeup or resume */ |
| 1018 | set_bit(A_BUS_REQ, &dev->inputs); |
| 1019 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1020 | dev->phy.state = OTG_STATE_A_HOST; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1021 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1022 | if (test_bit(ID_A, &dev->inputs) && |
| 1023 | (get_aca_bmaxpower(dev) < USB_IDCHG_MIN)) |
| 1024 | msm_otg_set_power(xceiv, |
| 1025 | USB_IDCHG_MIN - get_aca_bmaxpower(dev)); |
| 1026 | break; |
| 1027 | default: |
| 1028 | break; |
| 1029 | } |
| 1030 | |
| 1031 | if (suspend == atomic_read(&dev->in_lpm)) |
| 1032 | return 0; |
| 1033 | |
| 1034 | disable_irq(dev->irq); |
| 1035 | if (dev->pmic_vbus_notif_supp) |
| 1036 | if (can_phy_power_collapse(dev) && |
| 1037 | dev->pdata->ldo_enable) |
| 1038 | dev->pdata->ldo_enable(1); |
| 1039 | |
| 1040 | msm_otg_get_resume(dev); |
| 1041 | |
| 1042 | if (!is_phy_clk_disabled()) |
| 1043 | goto out; |
| 1044 | |
| 1045 | timeout = jiffies + usecs_to_jiffies(100); |
| 1046 | enable_phy_clk(); |
| 1047 | while (is_phy_clk_disabled() || !is_phy_active()) { |
| 1048 | if (time_after(jiffies, timeout)) { |
| 1049 | pr_err("%s: Unable to wakeup phy. " |
| 1050 | "is_phy_active: %x\n", |
| 1051 | __func__, !!is_phy_active()); |
| 1052 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1053 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | udelay(10); |
| 1057 | } |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 1058 | if (dev->pmic_id_notif_supp) { |
| 1059 | dev->pdata->pmic_id_notif_init( |
| 1060 | &msm_otg_set_id_state, 0); |
| 1061 | dev->pmic_id_notif_supp = 0; |
| 1062 | enable_idgnd(dev); |
| 1063 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1064 | out: |
| 1065 | enable_idabc(dev); |
| 1066 | enable_irq(dev->irq); |
| 1067 | |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1073 | static int msm_otg_set_peripheral(struct usb_otg *otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1074 | struct usb_gadget *gadget) |
| 1075 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1076 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1077 | |
| 1078 | if (!dev || (dev != the_msm_otg)) |
| 1079 | return -ENODEV; |
| 1080 | |
| 1081 | if (!gadget) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1082 | msm_otg_start_peripheral(otg, 0); |
| 1083 | otg->gadget = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1084 | disable_sess_valid(dev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1085 | if (!otg->host) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1086 | disable_idabc(dev); |
| 1087 | return 0; |
| 1088 | } |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1089 | otg->gadget = gadget; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1090 | pr_info("peripheral driver registered w/ tranceiver\n"); |
| 1091 | |
| 1092 | wake_lock(&dev->wlock); |
| 1093 | queue_work(dev->wq, &dev->sm_work); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | #ifdef CONFIG_USB_EHCI_MSM_72K |
| 1098 | static int usbdev_notify(struct notifier_block *self, |
| 1099 | unsigned long action, void *device) |
| 1100 | { |
| 1101 | enum usb_otg_state state; |
| 1102 | struct msm_otg *dev = container_of(self, struct msm_otg, usbdev_nb); |
| 1103 | struct usb_device *udev = device; |
| 1104 | int work = 1; |
| 1105 | unsigned long flags; |
| 1106 | |
| 1107 | /* Interested in only devices directly connected |
| 1108 | * to root hub directly. |
| 1109 | */ |
| 1110 | if (!udev->parent || udev->parent->parent) |
| 1111 | goto out; |
| 1112 | |
| 1113 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1114 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1115 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1116 | |
| 1117 | switch (state) { |
| 1118 | case OTG_STATE_A_WAIT_BCON: |
| 1119 | if (action == USB_DEVICE_ADD) { |
| 1120 | pr_debug("B_CONN set\n"); |
| 1121 | set_bit(B_CONN, &dev->inputs); |
| 1122 | if (udev->actconfig) { |
| 1123 | set_aca_bmaxpower(dev, |
| 1124 | udev->actconfig->desc.bMaxPower * 2); |
| 1125 | goto do_work; |
| 1126 | } |
| 1127 | if (udev->portnum == udev->bus->otg_port) |
| 1128 | set_aca_bmaxpower(dev, USB_IB_UNCFG); |
| 1129 | else |
| 1130 | set_aca_bmaxpower(dev, 100); |
| 1131 | } |
| 1132 | break; |
| 1133 | case OTG_STATE_A_HOST: |
| 1134 | if (action == USB_DEVICE_REMOVE) { |
| 1135 | pr_debug("B_CONN clear\n"); |
| 1136 | clear_bit(B_CONN, &dev->inputs); |
| 1137 | set_aca_bmaxpower(dev, 0); |
| 1138 | } |
| 1139 | break; |
| 1140 | default: |
| 1141 | work = 0; |
| 1142 | break; |
| 1143 | } |
| 1144 | do_work: |
| 1145 | if (work) { |
| 1146 | wake_lock(&dev->wlock); |
| 1147 | queue_work(dev->wq, &dev->sm_work); |
| 1148 | } |
| 1149 | out: |
| 1150 | return NOTIFY_OK; |
| 1151 | } |
| 1152 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1153 | static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1154 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1155 | struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1156 | |
| 1157 | if (!dev || (dev != the_msm_otg)) |
| 1158 | return -ENODEV; |
| 1159 | |
| 1160 | if (!dev->start_host) |
| 1161 | return -ENODEV; |
| 1162 | |
| 1163 | if (!host) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1164 | msm_otg_start_host(otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1165 | usb_unregister_notify(&dev->usbdev_nb); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1166 | otg->host = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1167 | dev->start_host = 0; |
| 1168 | disable_idgnd(dev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1169 | if (!otg->gadget) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1170 | disable_idabc(dev); |
| 1171 | return 0; |
| 1172 | } |
| 1173 | #ifdef CONFIG_USB_OTG |
| 1174 | host->otg_port = 1; |
| 1175 | #endif |
| 1176 | dev->usbdev_nb.notifier_call = usbdev_notify; |
| 1177 | usb_register_notify(&dev->usbdev_nb); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1178 | otg->host = host; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1179 | pr_info("host driver registered w/ tranceiver\n"); |
| 1180 | |
| 1181 | #ifndef CONFIG_USB_MSM_72K |
| 1182 | wake_lock(&dev->wlock); |
| 1183 | queue_work(dev->wq, &dev->sm_work); |
| 1184 | #endif |
| 1185 | return 0; |
| 1186 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1187 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 1188 | static void msm_otg_set_id_state(int id) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1189 | { |
| 1190 | struct msm_otg *dev = the_msm_otg; |
| 1191 | unsigned long flags; |
| 1192 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 1193 | if (!atomic_read(&dev->in_lpm)) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1194 | return; |
| 1195 | |
| 1196 | if (id) { |
| 1197 | set_bit(ID, &dev->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1198 | } else { |
| 1199 | clear_bit(ID, &dev->inputs); |
| 1200 | set_bit(A_BUS_REQ, &dev->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1201 | } |
| 1202 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1203 | if (dev->phy.state != OTG_STATE_UNDEFINED) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1204 | wake_lock(&dev->wlock); |
| 1205 | queue_work(dev->wq, &dev->sm_work); |
| 1206 | } |
| 1207 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1208 | } |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 1209 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1210 | |
| 1211 | void msm_otg_set_vbus_state(int online) |
| 1212 | { |
| 1213 | struct msm_otg *dev = the_msm_otg; |
| 1214 | |
Rajkumar Raghupathy | f5c6c60 | 2011-11-25 16:20:42 +0530 | [diff] [blame] | 1215 | /* |
| 1216 | * Process disconnect only for wallcharger |
| 1217 | * during fast plug-out plug-in at the |
| 1218 | * AC source side. |
| 1219 | */ |
| 1220 | if (online) |
| 1221 | set_bit(B_SESS_VLD, &dev->inputs); |
| 1222 | else |
| 1223 | clear_bit(B_SESS_VLD, &dev->inputs); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1224 | |
| 1225 | wake_lock(&dev->wlock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1226 | queue_work(dev->wq, &dev->sm_work); |
| 1227 | } |
| 1228 | |
| 1229 | static irqreturn_t msm_otg_irq(int irq, void *data) |
| 1230 | { |
| 1231 | struct msm_otg *dev = data; |
Rajkumar Raghupathy | 020d62d | 2012-09-24 15:19:51 +0530 | [diff] [blame] | 1232 | u32 otgsc, sts, pc; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1233 | irqreturn_t ret = IRQ_HANDLED; |
| 1234 | int work = 0; |
| 1235 | enum usb_otg_state state; |
| 1236 | unsigned long flags; |
| 1237 | |
| 1238 | if (atomic_read(&dev->in_lpm)) { |
| 1239 | disable_irq_nosync(dev->irq); |
| 1240 | wake_lock(&dev->wlock); |
| 1241 | queue_work(dev->wq, &dev->otg_resume_work); |
| 1242 | goto out; |
| 1243 | } |
| 1244 | |
| 1245 | /* Return immediately if instead of ID pin, USER controls mode switch */ |
| 1246 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) |
| 1247 | return IRQ_NONE; |
| 1248 | |
| 1249 | |
| 1250 | otgsc = readl(USB_OTGSC); |
| 1251 | sts = readl(USB_USBSTS); |
| 1252 | |
Rajkumar Raghupathy | 020d62d | 2012-09-24 15:19:51 +0530 | [diff] [blame] | 1253 | /* At times during USB disconnect, hardware generates 1MSIS interrupt |
| 1254 | * during PHY reset, which leads to irq not handled error as IRQ_NONE |
| 1255 | * is notified. To workaround this issue, check for all the |
| 1256 | * OTG_INTR_STS_MASK bits and if set, clear them and notify IRQ_HANDLED. |
| 1257 | */ |
| 1258 | if (!((otgsc & OTGSC_INTR_STS_MASK) || (sts & STS_PCI))) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1259 | ret = IRQ_NONE; |
| 1260 | goto out; |
| 1261 | } |
Rajkumar Raghupathy | 020d62d | 2012-09-24 15:19:51 +0530 | [diff] [blame] | 1262 | writel_relaxed(otgsc, USB_OTGSC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1263 | |
| 1264 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1265 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1266 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1267 | |
| 1268 | pr_debug("IRQ state: %s\n", state_string(state)); |
| 1269 | pr_debug("otgsc = %x\n", otgsc); |
| 1270 | |
| 1271 | if ((otgsc & OTGSC_IDIE) && (otgsc & OTGSC_IDIS)) { |
| 1272 | if (otgsc & OTGSC_ID) { |
| 1273 | pr_debug("Id set\n"); |
| 1274 | set_bit(ID, &dev->inputs); |
| 1275 | } else { |
| 1276 | pr_debug("Id clear\n"); |
| 1277 | /* Assert a_bus_req to supply power on |
| 1278 | * VBUS when Micro/Mini-A cable is connected |
| 1279 | * with out user intervention. |
| 1280 | */ |
| 1281 | set_bit(A_BUS_REQ, &dev->inputs); |
| 1282 | clear_bit(ID, &dev->inputs); |
| 1283 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1284 | work = 1; |
| 1285 | } else if (otgsc & OTGSC_BSVIS) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1286 | /* BSV interrupt comes when operating as an A-device |
| 1287 | * (VBUS on/off). |
| 1288 | * But, handle BSV when charger is removed from ACA in ID_A |
| 1289 | */ |
| 1290 | if ((state >= OTG_STATE_A_IDLE) && |
| 1291 | !test_bit(ID_A, &dev->inputs)) |
| 1292 | goto out; |
| 1293 | if (otgsc & OTGSC_BSV) { |
| 1294 | pr_debug("BSV set\n"); |
| 1295 | set_bit(B_SESS_VLD, &dev->inputs); |
| 1296 | } else { |
| 1297 | pr_debug("BSV clear\n"); |
| 1298 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 1299 | } |
| 1300 | work = 1; |
| 1301 | } else if (otgsc & OTGSC_DPIS) { |
| 1302 | pr_debug("DPIS detected\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1303 | set_bit(A_SRP_DET, &dev->inputs); |
| 1304 | set_bit(A_BUS_REQ, &dev->inputs); |
| 1305 | work = 1; |
| 1306 | } else if (sts & STS_PCI) { |
| 1307 | pc = readl(USB_PORTSC); |
| 1308 | pr_debug("portsc = %x\n", pc); |
| 1309 | ret = IRQ_NONE; |
| 1310 | /* HCD Acks PCI interrupt. We use this to switch |
| 1311 | * between different OTG states. |
| 1312 | */ |
| 1313 | work = 1; |
| 1314 | switch (state) { |
| 1315 | case OTG_STATE_A_SUSPEND: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1316 | if (dev->phy.otg->host->b_hnp_enable && |
| 1317 | (pc & PORTSC_CSC) && |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1318 | !(pc & PORTSC_CCS)) { |
| 1319 | pr_debug("B_CONN clear\n"); |
| 1320 | clear_bit(B_CONN, &dev->inputs); |
| 1321 | } |
| 1322 | break; |
| 1323 | case OTG_STATE_B_WAIT_ACON: |
| 1324 | if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) { |
| 1325 | pr_debug("A_CONN set\n"); |
| 1326 | set_bit(A_CONN, &dev->inputs); |
| 1327 | /* Clear ASE0_BRST timer */ |
| 1328 | msm_otg_del_timer(dev); |
| 1329 | } |
| 1330 | break; |
| 1331 | case OTG_STATE_B_HOST: |
| 1332 | if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) { |
| 1333 | pr_debug("A_CONN clear\n"); |
| 1334 | clear_bit(A_CONN, &dev->inputs); |
| 1335 | } |
| 1336 | break; |
| 1337 | default: |
| 1338 | work = 0; |
| 1339 | break; |
| 1340 | } |
| 1341 | } |
| 1342 | if (work) { |
| 1343 | #ifdef CONFIG_USB_MSM_ACA |
| 1344 | /* With ACA, ID can change bcoz of BSVIS as well, so update */ |
| 1345 | if ((otgsc & OTGSC_IDIS) || (otgsc & OTGSC_BSVIS)) |
| 1346 | set_aca_id_inputs(dev); |
| 1347 | #endif |
| 1348 | wake_lock(&dev->wlock); |
| 1349 | queue_work(dev->wq, &dev->sm_work); |
| 1350 | } |
| 1351 | out: |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | #define ULPI_VERIFY_MAX_LOOP_COUNT 5 |
| 1356 | #define PHY_CALIB_RETRY_COUNT 10 |
| 1357 | static void phy_clk_reset(struct msm_otg *dev) |
| 1358 | { |
| 1359 | unsigned rc; |
| 1360 | enum clk_reset_action assert = CLK_RESET_ASSERT; |
| 1361 | |
| 1362 | if (dev->pdata->phy_reset_sig_inverted) |
| 1363 | assert = CLK_RESET_DEASSERT; |
| 1364 | |
| 1365 | rc = clk_reset(dev->phy_reset_clk, assert); |
| 1366 | if (rc) { |
| 1367 | pr_err("%s: phy clk assert failed\n", __func__); |
| 1368 | return; |
| 1369 | } |
| 1370 | |
| 1371 | msleep(1); |
| 1372 | |
| 1373 | rc = clk_reset(dev->phy_reset_clk, !assert); |
| 1374 | if (rc) { |
| 1375 | pr_err("%s: phy clk deassert failed\n", __func__); |
| 1376 | return; |
| 1377 | } |
| 1378 | |
| 1379 | msleep(1); |
| 1380 | } |
| 1381 | |
| 1382 | static unsigned ulpi_read_with_reset(struct msm_otg *dev, unsigned reg) |
| 1383 | { |
| 1384 | int temp; |
| 1385 | unsigned res; |
| 1386 | |
| 1387 | for (temp = 0; temp < ULPI_VERIFY_MAX_LOOP_COUNT; temp++) { |
| 1388 | res = ulpi_read(dev, reg); |
| 1389 | if (res != 0xffffffff) |
| 1390 | return res; |
| 1391 | |
| 1392 | phy_clk_reset(dev); |
| 1393 | } |
| 1394 | |
| 1395 | pr_err("%s: ulpi read failed for %d times\n", |
| 1396 | __func__, ULPI_VERIFY_MAX_LOOP_COUNT); |
| 1397 | |
| 1398 | return -1; |
| 1399 | } |
| 1400 | |
| 1401 | static int ulpi_write_with_reset(struct msm_otg *dev, |
| 1402 | unsigned val, unsigned reg) |
| 1403 | { |
| 1404 | int temp, res; |
| 1405 | |
| 1406 | for (temp = 0; temp < ULPI_VERIFY_MAX_LOOP_COUNT; temp++) { |
| 1407 | res = ulpi_write(dev, val, reg); |
| 1408 | if (!res) |
| 1409 | return 0; |
| 1410 | phy_clk_reset(dev); |
| 1411 | } |
| 1412 | pr_err("%s: ulpi write failed for %d times\n", |
| 1413 | __func__, ULPI_VERIFY_MAX_LOOP_COUNT); |
| 1414 | |
| 1415 | return -1; |
| 1416 | } |
| 1417 | |
| 1418 | /* some of the older targets does not turn off the PLL |
| 1419 | * if onclock bit is set and clocksuspendM bit is on, |
| 1420 | * hence clear them too and initiate the suspend mode |
| 1421 | * by clearing SupendM bit. |
| 1422 | */ |
| 1423 | static inline int turn_off_phy_pll(struct msm_otg *dev) |
| 1424 | { |
| 1425 | unsigned res; |
| 1426 | |
| 1427 | res = ulpi_read_with_reset(dev, ULPI_CONFIG_REG1); |
| 1428 | if (res == 0xffffffff) |
| 1429 | return -ETIMEDOUT; |
| 1430 | |
| 1431 | res = ulpi_write_with_reset(dev, |
| 1432 | res & ~(ULPI_ONCLOCK), ULPI_CONFIG_REG1); |
| 1433 | if (res) |
| 1434 | return -ETIMEDOUT; |
| 1435 | |
| 1436 | res = ulpi_write_with_reset(dev, |
| 1437 | ULPI_CLOCK_SUSPENDM, ULPI_IFC_CTRL_CLR); |
| 1438 | if (res) |
| 1439 | return -ETIMEDOUT; |
| 1440 | |
| 1441 | /*Clear SuspendM bit to initiate suspend mode */ |
| 1442 | res = ulpi_write_with_reset(dev, |
| 1443 | ULPI_SUSPENDM, ULPI_FUNC_CTRL_CLR); |
| 1444 | if (res) |
| 1445 | return -ETIMEDOUT; |
| 1446 | |
| 1447 | return res; |
| 1448 | } |
| 1449 | |
| 1450 | static inline int check_phy_caliberation(struct msm_otg *dev) |
| 1451 | { |
| 1452 | unsigned res; |
| 1453 | |
| 1454 | res = ulpi_read_with_reset(dev, ULPI_DEBUG); |
| 1455 | |
| 1456 | if (res == 0xffffffff) |
| 1457 | return -ETIMEDOUT; |
| 1458 | |
| 1459 | if (!(res & ULPI_CALIB_STS) && ULPI_CALIB_VAL(res)) |
| 1460 | return 0; |
| 1461 | |
| 1462 | return -1; |
| 1463 | } |
| 1464 | |
| 1465 | static int msm_otg_phy_caliberate(struct msm_otg *dev) |
| 1466 | { |
| 1467 | int i = 0; |
| 1468 | unsigned long res; |
| 1469 | |
| 1470 | do { |
| 1471 | res = turn_off_phy_pll(dev); |
| 1472 | if (res) |
| 1473 | return -ETIMEDOUT; |
| 1474 | |
| 1475 | /* bring phy out of suspend */ |
| 1476 | phy_clk_reset(dev); |
| 1477 | |
| 1478 | res = check_phy_caliberation(dev); |
| 1479 | if (!res) |
| 1480 | return res; |
| 1481 | i++; |
| 1482 | |
| 1483 | } while (i < PHY_CALIB_RETRY_COUNT); |
| 1484 | |
| 1485 | return res; |
| 1486 | } |
| 1487 | |
| 1488 | static int msm_otg_phy_reset(struct msm_otg *dev) |
| 1489 | { |
| 1490 | unsigned rc; |
| 1491 | unsigned temp; |
| 1492 | unsigned long timeout; |
| 1493 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 1494 | rc = clk_reset(dev->alt_core_clk, CLK_RESET_ASSERT); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1495 | if (rc) { |
| 1496 | pr_err("%s: usb hs clk assert failed\n", __func__); |
| 1497 | return -1; |
| 1498 | } |
| 1499 | |
| 1500 | phy_clk_reset(dev); |
| 1501 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 1502 | rc = clk_reset(dev->alt_core_clk, CLK_RESET_DEASSERT); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1503 | if (rc) { |
| 1504 | pr_err("%s: usb hs clk deassert failed\n", __func__); |
| 1505 | return -1; |
| 1506 | } |
| 1507 | /* Observing ulpi timeouts as part of PHY calibration. On resetting |
| 1508 | * the HW link explicity by setting the RESET bit in the USBCMD |
| 1509 | * register before PHY calibration fixes the ulpi timeout issue. |
| 1510 | * This workaround is required for unicorn target |
| 1511 | */ |
| 1512 | writel_relaxed(USBCMD_RESET, USB_USBCMD); |
| 1513 | timeout = jiffies + USB_LINK_RESET_TIMEOUT; |
| 1514 | do { |
| 1515 | if (time_after(jiffies, timeout)) { |
| 1516 | pr_err("msm_otg: usb link reset timeout\n"); |
| 1517 | break; |
| 1518 | } |
| 1519 | usleep_range(1000, 1200); |
| 1520 | } while (readl_relaxed(USB_USBCMD) & USBCMD_RESET); |
| 1521 | |
| 1522 | /* select ULPI phy */ |
| 1523 | temp = (readl(USB_PORTSC) & ~PORTSC_PTS); |
| 1524 | writel(temp | PORTSC_PTS_ULPI, USB_PORTSC); |
| 1525 | |
| 1526 | if (atomic_read(&dev->chg_type) != |
| 1527 | USB_CHG_TYPE__WALLCHARGER) { |
| 1528 | rc = msm_otg_phy_caliberate(dev); |
| 1529 | if (rc) |
| 1530 | return rc; |
| 1531 | } |
| 1532 | |
| 1533 | /* TBD: There are two link resets. One is below and other one |
| 1534 | * is done immediately after this function. See if we can |
| 1535 | * eliminate one of these. |
| 1536 | */ |
| 1537 | writel(USBCMD_RESET, USB_USBCMD); |
| 1538 | timeout = jiffies + USB_LINK_RESET_TIMEOUT; |
| 1539 | do { |
| 1540 | if (time_after(jiffies, timeout)) { |
| 1541 | pr_err("msm_otg: usb link reset timeout\n"); |
| 1542 | break; |
| 1543 | } |
| 1544 | msleep(1); |
| 1545 | } while (readl(USB_USBCMD) & USBCMD_RESET); |
| 1546 | |
| 1547 | if (readl(USB_USBCMD) & USBCMD_RESET) { |
| 1548 | pr_err("%s: usb core reset failed\n", __func__); |
| 1549 | return -1; |
| 1550 | } |
| 1551 | |
| 1552 | return 0; |
| 1553 | } |
| 1554 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1555 | static void otg_reset(struct usb_phy *xceiv, int phy_reset) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1556 | { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1557 | struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1558 | unsigned long timeout; |
| 1559 | u32 mode, work = 0; |
| 1560 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 1561 | clk_prepare_enable(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1562 | |
| 1563 | if (!phy_reset) |
| 1564 | goto reset_link; |
| 1565 | |
| 1566 | if (dev->pdata->phy_reset) |
| 1567 | dev->pdata->phy_reset(dev->regs); |
| 1568 | else |
| 1569 | msm_otg_phy_reset(dev); |
| 1570 | |
| 1571 | /*disable all phy interrupts*/ |
| 1572 | ulpi_write(dev, 0xFF, 0x0F); |
| 1573 | ulpi_write(dev, 0xFF, 0x12); |
| 1574 | msleep(100); |
| 1575 | |
| 1576 | reset_link: |
| 1577 | writel(USBCMD_RESET, USB_USBCMD); |
| 1578 | timeout = jiffies + USB_LINK_RESET_TIMEOUT; |
| 1579 | do { |
| 1580 | if (time_after(jiffies, timeout)) { |
| 1581 | pr_err("msm_otg: usb link reset timeout\n"); |
| 1582 | break; |
| 1583 | } |
| 1584 | msleep(1); |
| 1585 | } while (readl(USB_USBCMD) & USBCMD_RESET); |
| 1586 | |
| 1587 | /* select ULPI phy */ |
| 1588 | writel(0x80000000, USB_PORTSC); |
| 1589 | |
| 1590 | set_pre_emphasis_level(dev); |
| 1591 | set_hsdrv_slope(dev); |
| 1592 | set_cdr_auto_reset(dev); |
| 1593 | set_driver_amplitude(dev); |
| 1594 | set_se1_gating(dev); |
| 1595 | |
| 1596 | writel(0x0, USB_AHB_BURST); |
| 1597 | writel(0x00, USB_AHB_MODE); |
Chandra Devireddy | b3fc78c | 2011-08-30 17:25:55 +0530 | [diff] [blame] | 1598 | if (dev->pdata->bam_disable) { |
| 1599 | writel_relaxed((readl_relaxed(USB_GEN_CONFIG) | |
| 1600 | USB_BAM_DISABLE), USB_GEN_CONFIG); |
| 1601 | pr_debug("%s(): USB_GEN_CONFIG = %x\n", |
| 1602 | __func__, readl_relaxed(USB_GEN_CONFIG)); |
| 1603 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1604 | /* Ensure that RESET operation is completed before turning off clock */ |
| 1605 | mb(); |
| 1606 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 1607 | clk_disable_unprepare(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1608 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1609 | if ((xceiv->otg->gadget && xceiv->otg->gadget->is_a_peripheral) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1610 | test_bit(ID, &dev->inputs)) |
| 1611 | mode = USBMODE_SDIS | USBMODE_DEVICE; |
| 1612 | else |
| 1613 | mode = USBMODE_SDIS | USBMODE_HOST; |
| 1614 | writel(mode, USB_USBMODE); |
| 1615 | |
| 1616 | writel_relaxed((readl_relaxed(USB_OTGSC) | OTGSC_IDPU), USB_OTGSC); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1617 | if (dev->phy.otg->gadget) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1618 | enable_sess_valid(dev); |
| 1619 | /* Due to the above 100ms delay, interrupts from PHY are |
| 1620 | * sometimes missed during fast plug-in/plug-out of cable. |
| 1621 | * Check for such cases here. |
| 1622 | */ |
| 1623 | if (is_b_sess_vld() && !test_bit(B_SESS_VLD, &dev->inputs)) { |
| 1624 | pr_debug("%s: handle missing BSV event\n", __func__); |
| 1625 | set_bit(B_SESS_VLD, &dev->inputs); |
| 1626 | work = 1; |
| 1627 | } else if (!is_b_sess_vld() && test_bit(B_SESS_VLD, |
| 1628 | &dev->inputs)) { |
| 1629 | pr_debug("%s: handle missing !BSV event\n", __func__); |
| 1630 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 1631 | work = 1; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | #ifdef CONFIG_USB_EHCI_MSM_72K |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1636 | if (dev->phy.otg->host && !dev->pmic_id_notif_supp) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1637 | enable_idgnd(dev); |
| 1638 | /* Handle missing ID_GND interrupts during fast PIPO */ |
| 1639 | if (is_host() && test_bit(ID, &dev->inputs)) { |
| 1640 | pr_debug("%s: handle missing ID_GND event\n", __func__); |
| 1641 | clear_bit(ID, &dev->inputs); |
| 1642 | work = 1; |
| 1643 | } else if (!is_host() && !test_bit(ID, &dev->inputs)) { |
| 1644 | pr_debug("%s: handle missing !ID_GND event\n", |
| 1645 | __func__); |
| 1646 | set_bit(ID, &dev->inputs); |
| 1647 | work = 1; |
| 1648 | } |
| 1649 | } else { |
| 1650 | disable_idgnd(dev); |
| 1651 | } |
| 1652 | #endif |
| 1653 | |
| 1654 | enable_idabc(dev); |
| 1655 | |
| 1656 | if (work) { |
| 1657 | wake_lock(&dev->wlock); |
| 1658 | queue_work(dev->wq, &dev->sm_work); |
| 1659 | } |
| 1660 | } |
| 1661 | |
| 1662 | static void msm_otg_sm_work(struct work_struct *w) |
| 1663 | { |
| 1664 | struct msm_otg *dev = container_of(w, struct msm_otg, sm_work); |
| 1665 | enum chg_type chg_type = atomic_read(&dev->chg_type); |
| 1666 | int ret; |
| 1667 | int work = 0; |
| 1668 | enum usb_otg_state state; |
| 1669 | unsigned long flags; |
| 1670 | |
| 1671 | if (atomic_read(&dev->in_lpm)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1672 | msm_otg_set_suspend(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1673 | |
| 1674 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1675 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1676 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1677 | |
| 1678 | switch (state) { |
| 1679 | case OTG_STATE_UNDEFINED: |
| 1680 | |
| 1681 | /* |
| 1682 | * We can come here when LPM fails with wall charger |
Pavankumar Kondeti | dc60f12 | 2011-09-13 11:17:16 +0530 | [diff] [blame] | 1683 | * connected. Change the state to B_PERIPHERAL and |
| 1684 | * schedule the work which takes care of resetting the |
| 1685 | * PHY and putting the hardware in low power mode. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1686 | */ |
| 1687 | if (atomic_read(&dev->chg_type) == |
| 1688 | USB_CHG_TYPE__WALLCHARGER) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1689 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1690 | dev->phy.state = OTG_STATE_B_PERIPHERAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1691 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1692 | work = 1; |
| 1693 | break; |
| 1694 | } |
| 1695 | |
| 1696 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1697 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1698 | |
| 1699 | #ifdef CONFIG_USB_MSM_ACA |
| 1700 | set_aca_id_inputs(dev); |
| 1701 | #endif |
| 1702 | if (dev->pdata->otg_mode == OTG_USER_CONTROL) { |
| 1703 | if ((dev->pdata->usb_mode == USB_PERIPHERAL_MODE) || |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1704 | !dev->phy.otg->host) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1705 | set_bit(ID, &dev->inputs); |
| 1706 | set_bit(B_SESS_VLD, &dev->inputs); |
| 1707 | } |
| 1708 | } else { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1709 | if (!dev->phy.otg->host || !is_host()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1710 | set_bit(ID, &dev->inputs); |
| 1711 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1712 | if (dev->phy.otg->gadget && is_b_sess_vld()) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1713 | set_bit(B_SESS_VLD, &dev->inputs); |
| 1714 | } |
| 1715 | spin_lock_irqsave(&dev->lock, flags); |
| 1716 | if ((test_bit(ID, &dev->inputs)) && |
| 1717 | !test_bit(ID_A, &dev->inputs)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1718 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1719 | } else { |
| 1720 | set_bit(A_BUS_REQ, &dev->inputs); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1721 | dev->phy.state = OTG_STATE_A_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1722 | } |
| 1723 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1724 | |
| 1725 | work = 1; |
| 1726 | break; |
| 1727 | case OTG_STATE_B_IDLE: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1728 | dev->phy.otg->default_a = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1729 | if (!test_bit(ID, &dev->inputs) || |
| 1730 | test_bit(ID_A, &dev->inputs)) { |
| 1731 | pr_debug("!id || id_A\n"); |
| 1732 | clear_bit(B_BUS_REQ, &dev->inputs); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1733 | otg_reset(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1734 | |
| 1735 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1736 | dev->phy.state = OTG_STATE_A_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1737 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1738 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1739 | work = 1; |
| 1740 | } else if (test_bit(B_SESS_VLD, &dev->inputs) && |
| 1741 | !test_bit(ID_B, &dev->inputs)) { |
| 1742 | pr_debug("b_sess_vld\n"); |
| 1743 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1744 | dev->phy.state = OTG_STATE_B_PERIPHERAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1745 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1746 | msm_otg_set_power(&dev->phy, 0); |
| 1747 | msm_otg_start_peripheral(dev->phy.otg, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1748 | } else if (test_bit(B_BUS_REQ, &dev->inputs)) { |
| 1749 | pr_debug("b_sess_end && b_bus_req\n"); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1750 | ret = msm_otg_start_srp(dev->phy.otg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1751 | if (ret < 0) { |
| 1752 | /* notify user space */ |
| 1753 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1754 | work = 1; |
| 1755 | break; |
| 1756 | } |
| 1757 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1758 | dev->phy.state = OTG_STATE_B_SRP_INIT; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1759 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1760 | msm_otg_start_timer(dev, TB_SRP_FAIL, B_SRP_FAIL); |
| 1761 | break; |
| 1762 | } else if (test_bit(ID_B, &dev->inputs)) { |
| 1763 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1764 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1765 | } else { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1766 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1767 | pr_debug("entering into lpm\n"); |
| 1768 | msm_otg_put_suspend(dev); |
| 1769 | |
| 1770 | if (dev->pdata->ldo_set_voltage) |
| 1771 | dev->pdata->ldo_set_voltage(3075); |
| 1772 | } |
| 1773 | break; |
| 1774 | case OTG_STATE_B_SRP_INIT: |
| 1775 | if (!test_bit(ID, &dev->inputs) || |
| 1776 | test_bit(ID_A, &dev->inputs) || |
| 1777 | test_bit(ID_C, &dev->inputs) || |
| 1778 | (test_bit(B_SESS_VLD, &dev->inputs) && |
| 1779 | !test_bit(ID_B, &dev->inputs))) { |
| 1780 | pr_debug("!id || id_a/c || b_sess_vld+!id_b\n"); |
| 1781 | msm_otg_del_timer(dev); |
| 1782 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1783 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1784 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1785 | work = 1; |
| 1786 | } else if (test_bit(B_SRP_FAIL, &dev->tmouts)) { |
| 1787 | pr_debug("b_srp_fail\n"); |
| 1788 | /* notify user space */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1789 | msm_otg_send_event(dev->phy.otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1790 | OTG_EVENT_NO_RESP_FOR_SRP); |
| 1791 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1792 | clear_bit(B_SRP_FAIL, &dev->tmouts); |
| 1793 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1794 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1795 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1796 | dev->b_last_se0_sess = jiffies; |
| 1797 | work = 1; |
| 1798 | } |
| 1799 | break; |
| 1800 | case OTG_STATE_B_PERIPHERAL: |
| 1801 | if (!test_bit(ID, &dev->inputs) || |
| 1802 | test_bit(ID_A, &dev->inputs) || |
| 1803 | test_bit(ID_B, &dev->inputs) || |
| 1804 | !test_bit(B_SESS_VLD, &dev->inputs)) { |
| 1805 | pr_debug("!id || id_a/b || !b_sess_vld\n"); |
| 1806 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1807 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1808 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1809 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1810 | msm_otg_start_peripheral(dev->phy.otg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1811 | dev->b_last_se0_sess = jiffies; |
| 1812 | |
| 1813 | /* Workaround: Reset phy after session */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1814 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1815 | work = 1; |
| 1816 | } else if (test_bit(B_BUS_REQ, &dev->inputs) && |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1817 | dev->phy.otg->gadget->b_hnp_enable && |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1818 | test_bit(A_BUS_SUSPEND, &dev->inputs)) { |
| 1819 | pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n"); |
| 1820 | msm_otg_start_timer(dev, TB_ASE0_BRST, B_ASE0_BRST); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1821 | msm_otg_start_peripheral(dev->phy.otg, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1822 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1823 | dev->phy.state = OTG_STATE_B_WAIT_ACON; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1824 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1825 | /* start HCD even before A-device enable |
| 1826 | * pull-up to meet HNP timings. |
| 1827 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1828 | dev->phy.otg->host->is_b_host = 1; |
| 1829 | msm_otg_start_host(dev->phy.otg, REQUEST_START); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1830 | |
| 1831 | } else if (test_bit(ID_C, &dev->inputs)) { |
| 1832 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1833 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1834 | } else if (chg_type == USB_CHG_TYPE__WALLCHARGER) { |
| 1835 | #ifdef CONFIG_USB_MSM_ACA |
| 1836 | del_timer_sync(&dev->id_timer); |
| 1837 | #endif |
| 1838 | /* Workaround: Reset PHY in SE1 state */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1839 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1840 | pr_debug("entering into lpm with wall-charger\n"); |
| 1841 | msm_otg_put_suspend(dev); |
| 1842 | /* Allow idle power collapse */ |
| 1843 | otg_pm_qos_update_latency(dev, 0); |
| 1844 | } |
| 1845 | break; |
| 1846 | case OTG_STATE_B_WAIT_ACON: |
| 1847 | if (!test_bit(ID, &dev->inputs) || |
| 1848 | test_bit(ID_A, &dev->inputs) || |
| 1849 | test_bit(ID_B, &dev->inputs) || |
| 1850 | !test_bit(B_SESS_VLD, &dev->inputs)) { |
| 1851 | pr_debug("!id || id_a/b || !b_sess_vld\n"); |
| 1852 | msm_otg_del_timer(dev); |
| 1853 | /* A-device is physically disconnected during |
| 1854 | * HNP. Remove HCD. |
| 1855 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1856 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
| 1857 | dev->phy.otg->host->is_b_host = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1858 | |
| 1859 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1860 | clear_bit(A_BUS_SUSPEND, &dev->inputs); |
| 1861 | dev->b_last_se0_sess = jiffies; |
| 1862 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1863 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1864 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1865 | |
| 1866 | /* Workaround: Reset phy after session */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1867 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1868 | work = 1; |
| 1869 | } else if (test_bit(A_CONN, &dev->inputs)) { |
| 1870 | pr_debug("a_conn\n"); |
| 1871 | clear_bit(A_BUS_SUSPEND, &dev->inputs); |
| 1872 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1873 | dev->phy.state = OTG_STATE_B_HOST; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1874 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1875 | if (test_bit(ID_C, &dev->inputs)) { |
| 1876 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1877 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1878 | } |
| 1879 | } else if (test_bit(B_ASE0_BRST, &dev->tmouts)) { |
| 1880 | /* TODO: A-device may send reset after |
| 1881 | * enabling HNP; a_bus_resume case is |
| 1882 | * not handled for now. |
| 1883 | */ |
| 1884 | pr_debug("b_ase0_brst_tmout\n"); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1885 | msm_otg_send_event(dev->phy.otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1886 | OTG_EVENT_HNP_FAILED); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1887 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
| 1888 | dev->phy.otg->host->is_b_host = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1889 | clear_bit(B_ASE0_BRST, &dev->tmouts); |
| 1890 | clear_bit(A_BUS_SUSPEND, &dev->inputs); |
| 1891 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1892 | |
| 1893 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1894 | dev->phy.state = OTG_STATE_B_PERIPHERAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1895 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1896 | msm_otg_start_peripheral(dev->phy.otg, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1897 | } else if (test_bit(ID_C, &dev->inputs)) { |
| 1898 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1899 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1900 | } |
| 1901 | break; |
| 1902 | case OTG_STATE_B_HOST: |
| 1903 | /* B_BUS_REQ is not exposed to user space. So |
| 1904 | * it must be A_CONN for now. |
| 1905 | */ |
| 1906 | if (!test_bit(B_BUS_REQ, &dev->inputs) || |
| 1907 | !test_bit(A_CONN, &dev->inputs)) { |
| 1908 | pr_debug("!b_bus_req || !a_conn\n"); |
| 1909 | clear_bit(A_CONN, &dev->inputs); |
| 1910 | clear_bit(B_BUS_REQ, &dev->inputs); |
| 1911 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1912 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
| 1913 | dev->phy.otg->host->is_b_host = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1914 | |
| 1915 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1916 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1917 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1918 | /* Workaround: Reset phy after session */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1919 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1920 | work = 1; |
| 1921 | } else if (test_bit(ID_C, &dev->inputs)) { |
| 1922 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1923 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1924 | } |
| 1925 | break; |
| 1926 | case OTG_STATE_A_IDLE: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1927 | dev->phy.otg->default_a = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1928 | if (test_bit(ID, &dev->inputs) && |
| 1929 | !test_bit(ID_A, &dev->inputs)) { |
| 1930 | pr_debug("id && !id_a\n"); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1931 | dev->phy.otg->default_a = 0; |
| 1932 | otg_reset(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1933 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1934 | dev->phy.state = OTG_STATE_B_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1935 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1936 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1937 | work = 1; |
| 1938 | } else if (!test_bit(A_BUS_DROP, &dev->inputs) && |
| 1939 | (test_bit(A_SRP_DET, &dev->inputs) || |
| 1940 | test_bit(A_BUS_REQ, &dev->inputs))) { |
| 1941 | pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n"); |
| 1942 | |
| 1943 | clear_bit(A_SRP_DET, &dev->inputs); |
| 1944 | /* Disable SRP detection */ |
| 1945 | writel((readl(USB_OTGSC) & ~OTGSC_INTR_STS_MASK) & |
| 1946 | ~OTGSC_DPIE, USB_OTGSC); |
| 1947 | |
| 1948 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1949 | dev->phy.state = OTG_STATE_A_WAIT_VRISE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1950 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1951 | /* ACA: ID_A: Stop charging untill enumeration */ |
| 1952 | if (test_bit(ID_A, &dev->inputs)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1953 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1954 | else |
| 1955 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 1); |
| 1956 | msm_otg_start_timer(dev, TA_WAIT_VRISE, A_WAIT_VRISE); |
| 1957 | /* no need to schedule work now */ |
| 1958 | } else { |
| 1959 | pr_debug("No session requested\n"); |
| 1960 | |
| 1961 | /* A-device is not providing power on VBUS. |
| 1962 | * Enable SRP detection. |
| 1963 | */ |
| 1964 | writel((readl(USB_OTGSC) & ~OTGSC_INTR_STS_MASK) | |
| 1965 | OTGSC_DPIE, USB_OTGSC); |
| 1966 | msm_otg_put_suspend(dev); |
| 1967 | |
| 1968 | } |
| 1969 | break; |
| 1970 | case OTG_STATE_A_WAIT_VRISE: |
| 1971 | if ((test_bit(ID, &dev->inputs) && |
| 1972 | !test_bit(ID_A, &dev->inputs)) || |
| 1973 | test_bit(A_BUS_DROP, &dev->inputs) || |
| 1974 | test_bit(A_WAIT_VRISE, &dev->tmouts)) { |
| 1975 | pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n"); |
| 1976 | clear_bit(A_BUS_REQ, &dev->inputs); |
| 1977 | msm_otg_del_timer(dev); |
| 1978 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 1979 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1980 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1981 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1982 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 1983 | } else if (test_bit(A_VBUS_VLD, &dev->inputs)) { |
| 1984 | pr_debug("a_vbus_vld\n"); |
| 1985 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1986 | dev->phy.state = OTG_STATE_A_WAIT_BCON; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1987 | spin_unlock_irqrestore(&dev->lock, flags); |
| 1988 | if (TA_WAIT_BCON > 0) |
| 1989 | msm_otg_start_timer(dev, TA_WAIT_BCON, |
| 1990 | A_WAIT_BCON); |
| 1991 | /* Start HCD to detect peripherals. */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 1992 | msm_otg_start_host(dev->phy.otg, REQUEST_START); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1993 | } |
| 1994 | break; |
| 1995 | case OTG_STATE_A_WAIT_BCON: |
| 1996 | if ((test_bit(ID, &dev->inputs) && |
| 1997 | !test_bit(ID_A, &dev->inputs)) || |
| 1998 | test_bit(A_BUS_DROP, &dev->inputs) || |
| 1999 | test_bit(A_WAIT_BCON, &dev->tmouts)) { |
| 2000 | pr_debug("id_f/b/c || a_bus_drop ||" |
| 2001 | "a_wait_bcon_tmout\n"); |
| 2002 | if (test_bit(A_WAIT_BCON, &dev->tmouts)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2003 | msm_otg_send_event(dev->phy.otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2004 | OTG_EVENT_DEV_CONN_TMOUT); |
| 2005 | msm_otg_del_timer(dev); |
| 2006 | clear_bit(A_BUS_REQ, &dev->inputs); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2007 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2008 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2009 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2010 | /* ACA: ID_A with NO accessory, just the A plug is |
| 2011 | * attached to ACA: Use IDCHG_MAX for charging |
| 2012 | */ |
| 2013 | if (test_bit(ID_A, &dev->inputs)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2014 | msm_otg_set_power(&dev->phy, USB_IDCHG_MAX); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2015 | else |
| 2016 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2017 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2018 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2019 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2020 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
| 2021 | } else if (test_bit(B_CONN, &dev->inputs)) { |
| 2022 | pr_debug("b_conn\n"); |
| 2023 | msm_otg_del_timer(dev); |
| 2024 | /* HCD is added already. just move to |
| 2025 | * A_HOST state. |
| 2026 | */ |
| 2027 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2028 | dev->phy.state = OTG_STATE_A_HOST; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2029 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2030 | if (test_bit(ID_A, &dev->inputs)) { |
| 2031 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2032 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2033 | USB_IDCHG_MIN - get_aca_bmaxpower(dev)); |
| 2034 | } |
| 2035 | } else if (!test_bit(A_VBUS_VLD, &dev->inputs)) { |
| 2036 | pr_debug("!a_vbus_vld\n"); |
| 2037 | msm_otg_del_timer(dev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2038 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2039 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2040 | dev->phy.state = OTG_STATE_A_VBUS_ERR; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2041 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2042 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2043 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2044 | } else if (test_bit(ID_A, &dev->inputs)) { |
| 2045 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2046 | } else if (!test_bit(ID, &dev->inputs)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2047 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2048 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 1); |
| 2049 | } |
| 2050 | break; |
| 2051 | case OTG_STATE_A_HOST: |
| 2052 | if ((test_bit(ID, &dev->inputs) && |
| 2053 | !test_bit(ID_A, &dev->inputs)) || |
| 2054 | test_bit(A_BUS_DROP, &dev->inputs)) { |
| 2055 | pr_debug("id_f/b/c || a_bus_drop\n"); |
| 2056 | clear_bit(B_CONN, &dev->inputs); |
| 2057 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2058 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2059 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2060 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2061 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2062 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2063 | if (!test_bit(ID_A, &dev->inputs)) |
| 2064 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2065 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2066 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2067 | } else if (!test_bit(A_VBUS_VLD, &dev->inputs)) { |
| 2068 | pr_debug("!a_vbus_vld\n"); |
| 2069 | clear_bit(B_CONN, &dev->inputs); |
| 2070 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2071 | dev->phy.state = OTG_STATE_A_VBUS_ERR; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2072 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2073 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2074 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2075 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2076 | /* no work */ |
| 2077 | } else if (!test_bit(A_BUS_REQ, &dev->inputs)) { |
| 2078 | /* a_bus_req is de-asserted when root hub is |
| 2079 | * suspended or HNP is in progress. |
| 2080 | */ |
| 2081 | pr_debug("!a_bus_req\n"); |
| 2082 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2083 | dev->phy.state = OTG_STATE_A_SUSPEND; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2084 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2085 | if (dev->phy.otg->host->b_hnp_enable) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2086 | msm_otg_start_timer(dev, TA_AIDL_BDIS, |
| 2087 | A_AIDL_BDIS); |
| 2088 | } else { |
| 2089 | /* No HNP. Root hub suspended */ |
| 2090 | msm_otg_put_suspend(dev); |
| 2091 | } |
| 2092 | if (test_bit(ID_A, &dev->inputs)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2093 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2094 | USB_IDCHG_MIN - USB_IB_UNCFG); |
| 2095 | } else if (!test_bit(B_CONN, &dev->inputs)) { |
| 2096 | pr_debug("!b_conn\n"); |
| 2097 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2098 | dev->phy.state = OTG_STATE_A_WAIT_BCON; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2099 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2100 | if (TA_WAIT_BCON > 0) |
| 2101 | msm_otg_start_timer(dev, TA_WAIT_BCON, |
| 2102 | A_WAIT_BCON); |
| 2103 | } else if (test_bit(ID_A, &dev->inputs)) { |
| 2104 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
| 2105 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2106 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2107 | USB_IDCHG_MIN - get_aca_bmaxpower(dev)); |
| 2108 | } else if (!test_bit(ID, &dev->inputs)) { |
| 2109 | atomic_set(&dev->chg_type, USB_CHG_TYPE__INVALID); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2110 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2111 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 1); |
| 2112 | } |
| 2113 | break; |
| 2114 | case OTG_STATE_A_SUSPEND: |
| 2115 | if ((test_bit(ID, &dev->inputs) && |
| 2116 | !test_bit(ID_A, &dev->inputs)) || |
| 2117 | test_bit(A_BUS_DROP, &dev->inputs) || |
| 2118 | test_bit(A_AIDL_BDIS, &dev->tmouts)) { |
| 2119 | pr_debug("id_f/b/c || a_bus_drop ||" |
| 2120 | "a_aidl_bdis_tmout\n"); |
| 2121 | if (test_bit(A_AIDL_BDIS, &dev->tmouts)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2122 | msm_otg_send_event(dev->phy.otg, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2123 | OTG_EVENT_HNP_FAILED); |
| 2124 | msm_otg_del_timer(dev); |
| 2125 | clear_bit(B_CONN, &dev->inputs); |
| 2126 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2127 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2128 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2129 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2130 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2131 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2132 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2133 | if (!test_bit(ID_A, &dev->inputs)) |
| 2134 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2135 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2136 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2137 | } else if (!test_bit(A_VBUS_VLD, &dev->inputs)) { |
| 2138 | pr_debug("!a_vbus_vld\n"); |
| 2139 | msm_otg_del_timer(dev); |
| 2140 | clear_bit(B_CONN, &dev->inputs); |
| 2141 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2142 | dev->phy.state = OTG_STATE_A_VBUS_ERR; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2143 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2144 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2145 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2146 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2147 | } else if (!test_bit(B_CONN, &dev->inputs) && |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2148 | dev->phy.otg->host->b_hnp_enable) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2149 | pr_debug("!b_conn && b_hnp_enable"); |
| 2150 | /* Clear AIDL_BDIS timer */ |
| 2151 | msm_otg_del_timer(dev); |
| 2152 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2153 | dev->phy.state = OTG_STATE_A_PERIPHERAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2154 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2155 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2156 | msm_otg_start_host(dev->phy.otg, REQUEST_HNP_SUSPEND); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2157 | |
| 2158 | /* We may come here even when B-dev is physically |
| 2159 | * disconnected during HNP. We go back to host |
| 2160 | * role if bus is idle for BIDL_ADIS time. |
| 2161 | */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2162 | dev->phy.otg->gadget->is_a_peripheral = 1; |
| 2163 | msm_otg_start_peripheral(dev->phy.otg, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2164 | /* If ID_A: we can charge in a_peripheral as well */ |
| 2165 | if (test_bit(ID_A, &dev->inputs)) { |
| 2166 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2167 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2168 | USB_IDCHG_MIN - USB_IB_UNCFG); |
| 2169 | } |
| 2170 | } else if (!test_bit(B_CONN, &dev->inputs) && |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2171 | !dev->phy.otg->host->b_hnp_enable) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2172 | pr_debug("!b_conn && !b_hnp_enable"); |
| 2173 | /* bus request is dropped during suspend. |
| 2174 | * acquire again for next device. |
| 2175 | */ |
| 2176 | set_bit(A_BUS_REQ, &dev->inputs); |
| 2177 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2178 | dev->phy.state = OTG_STATE_A_WAIT_BCON; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2179 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2180 | if (TA_WAIT_BCON > 0) |
| 2181 | msm_otg_start_timer(dev, TA_WAIT_BCON, |
| 2182 | A_WAIT_BCON); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2183 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2184 | } else if (test_bit(ID_A, &dev->inputs)) { |
| 2185 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2186 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2187 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2188 | USB_IDCHG_MIN - USB_IB_UNCFG); |
| 2189 | } else if (!test_bit(ID, &dev->inputs)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2190 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2191 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 1); |
| 2192 | } |
| 2193 | break; |
| 2194 | case OTG_STATE_A_PERIPHERAL: |
| 2195 | if ((test_bit(ID, &dev->inputs) && |
| 2196 | !test_bit(ID_A, &dev->inputs)) || |
| 2197 | test_bit(A_BUS_DROP, &dev->inputs)) { |
| 2198 | pr_debug("id _f/b/c || a_bus_drop\n"); |
| 2199 | /* Clear BIDL_ADIS timer */ |
| 2200 | msm_otg_del_timer(dev); |
| 2201 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2202 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2203 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2204 | msm_otg_start_peripheral(dev->phy.otg, 0); |
| 2205 | dev->phy.otg->gadget->is_a_peripheral = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2206 | /* HCD was suspended before. Stop it now */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2207 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2208 | |
| 2209 | /* Reset both phy and link */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2210 | otg_reset(&dev->phy, 1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2211 | if (!test_bit(ID_A, &dev->inputs)) |
| 2212 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2213 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2214 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2215 | } else if (!test_bit(A_VBUS_VLD, &dev->inputs)) { |
| 2216 | pr_debug("!a_vbus_vld\n"); |
| 2217 | /* Clear BIDL_ADIS timer */ |
| 2218 | msm_otg_del_timer(dev); |
| 2219 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2220 | dev->phy.state = OTG_STATE_A_VBUS_ERR; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2221 | spin_unlock_irqrestore(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2222 | msm_otg_start_peripheral(dev->phy.otg, 0); |
| 2223 | dev->phy.otg->gadget->is_a_peripheral = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2224 | /* HCD was suspended before. Stop it now */ |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2225 | msm_otg_start_host(dev->phy.otg, REQUEST_STOP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2226 | } else if (test_bit(A_BIDL_ADIS, &dev->tmouts)) { |
| 2227 | pr_debug("a_bidl_adis_tmout\n"); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2228 | msm_otg_start_peripheral(dev->phy.otg, 0); |
| 2229 | dev->phy.otg->gadget->is_a_peripheral = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2230 | |
| 2231 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2232 | dev->phy.state = OTG_STATE_A_WAIT_BCON; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2233 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2234 | set_bit(A_BUS_REQ, &dev->inputs); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2235 | msm_otg_start_host(dev->phy.otg, REQUEST_HNP_RESUME); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2236 | if (TA_WAIT_BCON > 0) |
| 2237 | msm_otg_start_timer(dev, TA_WAIT_BCON, |
| 2238 | A_WAIT_BCON); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2239 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2240 | } else if (test_bit(ID_A, &dev->inputs)) { |
| 2241 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2242 | atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2243 | msm_otg_set_power(&dev->phy, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2244 | USB_IDCHG_MIN - USB_IB_UNCFG); |
| 2245 | } else if (!test_bit(ID, &dev->inputs)) { |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2246 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2247 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 1); |
| 2248 | } |
| 2249 | break; |
| 2250 | case OTG_STATE_A_WAIT_VFALL: |
| 2251 | if (test_bit(A_WAIT_VFALL, &dev->tmouts)) { |
| 2252 | clear_bit(A_VBUS_VLD, &dev->inputs); |
| 2253 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2254 | dev->phy.state = OTG_STATE_A_IDLE; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2255 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2256 | work = 1; |
| 2257 | } |
| 2258 | break; |
| 2259 | case OTG_STATE_A_VBUS_ERR: |
| 2260 | if ((test_bit(ID, &dev->inputs) && |
| 2261 | !test_bit(ID_A, &dev->inputs)) || |
| 2262 | test_bit(A_BUS_DROP, &dev->inputs) || |
| 2263 | test_bit(A_CLR_ERR, &dev->inputs)) { |
| 2264 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2265 | dev->phy.state = OTG_STATE_A_WAIT_VFALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2266 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2267 | if (!test_bit(ID_A, &dev->inputs)) |
| 2268 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2269 | msm_otg_start_timer(dev, TA_WAIT_VFALL, A_WAIT_VFALL); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2270 | msm_otg_set_power(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2271 | } |
| 2272 | break; |
| 2273 | default: |
| 2274 | pr_err("invalid OTG state\n"); |
| 2275 | } |
| 2276 | |
| 2277 | if (work) |
| 2278 | queue_work(dev->wq, &dev->sm_work); |
| 2279 | |
| 2280 | #ifdef CONFIG_USB_MSM_ACA |
| 2281 | /* Start id_polling if (ID_FLOAT&BSV) || ID_A/B/C */ |
| 2282 | if ((test_bit(ID, &dev->inputs) && |
| 2283 | test_bit(B_SESS_VLD, &dev->inputs) && |
| 2284 | chg_type != USB_CHG_TYPE__WALLCHARGER) || |
| 2285 | test_bit(ID_A, &dev->inputs)) { |
| 2286 | mod_timer(&dev->id_timer, jiffies + |
| 2287 | msecs_to_jiffies(OTG_ID_POLL_MS)); |
| 2288 | return; |
| 2289 | } |
| 2290 | del_timer(&dev->id_timer); |
| 2291 | #endif |
| 2292 | /* IRQ/sysfs may queue work. Check work_pending. otherwise |
| 2293 | * we might endup releasing wakelock after it is acquired |
| 2294 | * in IRQ/sysfs. |
| 2295 | */ |
| 2296 | if (!work_pending(&dev->sm_work) && !hrtimer_active(&dev->timer) && |
| 2297 | !work_pending(&dev->otg_resume_work)) |
| 2298 | wake_unlock(&dev->wlock); |
| 2299 | } |
| 2300 | |
| 2301 | #ifdef CONFIG_USB_MSM_ACA |
| 2302 | static void msm_otg_id_func(unsigned long _dev) |
| 2303 | { |
| 2304 | struct msm_otg *dev = (struct msm_otg *) _dev; |
| 2305 | u8 phy_ints; |
| 2306 | |
| 2307 | #ifdef CONFIG_USB_MSM_STANDARD_ACA |
| 2308 | /* |
| 2309 | * When standard ACA is attached RID_A and RID_GND states are only |
| 2310 | * possible. RID_A-->RID_GND transition generates IdGnd interrupt |
| 2311 | * from PHY. Hence polling is disabled. |
| 2312 | */ |
| 2313 | if (test_bit(ID_A, &dev->inputs)) |
| 2314 | goto out; |
| 2315 | #endif |
| 2316 | |
| 2317 | if (atomic_read(&dev->in_lpm)) |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2318 | msm_otg_set_suspend(&dev->phy, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2319 | |
| 2320 | phy_ints = ulpi_read(dev, 0x13); |
| 2321 | |
| 2322 | /* |
| 2323 | * ACA timer will be kicked again after the PHY |
| 2324 | * state is recovered. |
| 2325 | */ |
| 2326 | if (phy_ints == -ETIMEDOUT) |
| 2327 | return; |
| 2328 | |
| 2329 | |
| 2330 | /* If id_gnd happened then stop and let isr take care of this */ |
| 2331 | if (phy_id_state_gnd(phy_ints)) |
| 2332 | goto out; |
| 2333 | |
| 2334 | if ((test_bit(ID_A, &dev->inputs) == phy_id_state_a(phy_ints)) && |
| 2335 | (test_bit(ID_B, &dev->inputs) == phy_id_state_b(phy_ints)) && |
| 2336 | (test_bit(ID_C, &dev->inputs) == phy_id_state_c(phy_ints))) { |
| 2337 | mod_timer(&dev->id_timer, |
| 2338 | jiffies + msecs_to_jiffies(OTG_ID_POLL_MS)); |
| 2339 | goto out; |
| 2340 | } else { |
| 2341 | set_aca_id_inputs(dev); |
| 2342 | } |
| 2343 | wake_lock(&dev->wlock); |
| 2344 | queue_work(dev->wq, &dev->sm_work); |
| 2345 | out: |
| 2346 | /* OOPS: runing while !BSV, schedule work to initiate LPM */ |
| 2347 | if (!is_b_sess_vld()) { |
| 2348 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 2349 | wake_lock(&dev->wlock); |
| 2350 | queue_work(dev->wq, &dev->sm_work); |
| 2351 | } |
| 2352 | return; |
| 2353 | } |
| 2354 | #endif |
| 2355 | #ifdef CONFIG_USB_OTG |
| 2356 | static ssize_t |
| 2357 | set_pwr_down(struct device *_dev, struct device_attribute *attr, |
| 2358 | const char *buf, size_t count) |
| 2359 | { |
| 2360 | struct msm_otg *dev = the_msm_otg; |
| 2361 | int value; |
| 2362 | enum usb_otg_state state; |
| 2363 | unsigned long flags; |
| 2364 | |
| 2365 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2366 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2367 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2368 | |
| 2369 | /* Applicable for only A-Device */ |
| 2370 | if (state <= OTG_STATE_A_IDLE) |
| 2371 | return -EINVAL; |
| 2372 | |
| 2373 | sscanf(buf, "%d", &value); |
| 2374 | |
| 2375 | if (test_bit(A_BUS_DROP, &dev->inputs) != !!value) { |
| 2376 | change_bit(A_BUS_DROP, &dev->inputs); |
| 2377 | wake_lock(&dev->wlock); |
| 2378 | queue_work(dev->wq, &dev->sm_work); |
| 2379 | } |
| 2380 | |
| 2381 | return count; |
| 2382 | } |
| 2383 | static DEVICE_ATTR(pwr_down, S_IRUGO | S_IWUSR, NULL, set_pwr_down); |
| 2384 | |
| 2385 | static ssize_t |
| 2386 | set_srp_req(struct device *_dev, struct device_attribute *attr, |
| 2387 | const char *buf, size_t count) |
| 2388 | { |
| 2389 | struct msm_otg *dev = the_msm_otg; |
| 2390 | enum usb_otg_state state; |
| 2391 | unsigned long flags; |
| 2392 | |
| 2393 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2394 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2395 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2396 | |
| 2397 | if (state != OTG_STATE_B_IDLE) |
| 2398 | return -EINVAL; |
| 2399 | |
| 2400 | set_bit(B_BUS_REQ, &dev->inputs); |
| 2401 | wake_lock(&dev->wlock); |
| 2402 | queue_work(dev->wq, &dev->sm_work); |
| 2403 | |
| 2404 | return count; |
| 2405 | } |
| 2406 | static DEVICE_ATTR(srp_req, S_IRUGO | S_IWUSR, NULL, set_srp_req); |
| 2407 | |
| 2408 | static ssize_t |
| 2409 | set_clr_err(struct device *_dev, struct device_attribute *attr, |
| 2410 | const char *buf, size_t count) |
| 2411 | { |
| 2412 | struct msm_otg *dev = the_msm_otg; |
| 2413 | enum usb_otg_state state; |
| 2414 | unsigned long flags; |
| 2415 | |
| 2416 | spin_lock_irqsave(&dev->lock, flags); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2417 | state = dev->phy.state; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2418 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2419 | |
| 2420 | if (state == OTG_STATE_A_VBUS_ERR) { |
| 2421 | set_bit(A_CLR_ERR, &dev->inputs); |
| 2422 | wake_lock(&dev->wlock); |
| 2423 | queue_work(dev->wq, &dev->sm_work); |
| 2424 | } |
| 2425 | |
| 2426 | return count; |
| 2427 | } |
| 2428 | static DEVICE_ATTR(clr_err, S_IRUGO | S_IWUSR, NULL, set_clr_err); |
| 2429 | |
| 2430 | static struct attribute *msm_otg_attrs[] = { |
| 2431 | &dev_attr_pwr_down.attr, |
| 2432 | &dev_attr_srp_req.attr, |
| 2433 | &dev_attr_clr_err.attr, |
| 2434 | NULL, |
| 2435 | }; |
| 2436 | |
| 2437 | static struct attribute_group msm_otg_attr_grp = { |
| 2438 | .attrs = msm_otg_attrs, |
| 2439 | }; |
| 2440 | #endif |
| 2441 | |
| 2442 | #ifdef CONFIG_DEBUG_FS |
| 2443 | static int otg_open(struct inode *inode, struct file *file) |
| 2444 | { |
| 2445 | file->private_data = inode->i_private; |
| 2446 | return 0; |
| 2447 | } |
| 2448 | static ssize_t otg_mode_write(struct file *file, const char __user *buf, |
| 2449 | size_t count, loff_t *ppos) |
| 2450 | { |
| 2451 | struct msm_otg *dev = file->private_data; |
| 2452 | int ret = count; |
| 2453 | int work = 0; |
| 2454 | unsigned long flags; |
| 2455 | |
| 2456 | spin_lock_irqsave(&dev->lock, flags); |
| 2457 | dev->pdata->otg_mode = OTG_USER_CONTROL; |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 2458 | if (!memcmp(buf, "none", 4)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2459 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 2460 | set_bit(ID, &dev->inputs); |
| 2461 | work = 1; |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 2462 | } else if (!memcmp(buf, "peripheral", 10)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2463 | set_bit(B_SESS_VLD, &dev->inputs); |
| 2464 | set_bit(ID, &dev->inputs); |
| 2465 | work = 1; |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 2466 | } else if (!memcmp(buf, "host", 4)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2467 | clear_bit(B_SESS_VLD, &dev->inputs); |
| 2468 | clear_bit(ID, &dev->inputs); |
| 2469 | set_bit(A_BUS_REQ, &dev->inputs); |
| 2470 | work = 1; |
| 2471 | } else { |
| 2472 | pr_info("%s: unknown mode specified\n", __func__); |
| 2473 | ret = -EINVAL; |
| 2474 | } |
| 2475 | spin_unlock_irqrestore(&dev->lock, flags); |
| 2476 | |
| 2477 | if (work) { |
| 2478 | wake_lock(&dev->wlock); |
| 2479 | queue_work(dev->wq, &dev->sm_work); |
| 2480 | } |
| 2481 | |
| 2482 | return ret; |
| 2483 | } |
| 2484 | const struct file_operations otgfs_fops = { |
| 2485 | .open = otg_open, |
| 2486 | .write = otg_mode_write, |
| 2487 | }; |
| 2488 | |
| 2489 | #define OTG_INFO_SIZE 512 |
| 2490 | static ssize_t otg_info_read(struct file *file, char __user *ubuf, |
| 2491 | size_t count, loff_t *ppos) |
| 2492 | { |
| 2493 | char *buf; |
| 2494 | int temp = 0; |
| 2495 | int ret; |
| 2496 | struct msm_otg *dev = file->private_data; |
| 2497 | |
| 2498 | buf = kzalloc(sizeof(char) * OTG_INFO_SIZE, GFP_KERNEL); |
| 2499 | if (!buf) |
| 2500 | return -ENOMEM; |
| 2501 | |
| 2502 | temp += scnprintf(buf + temp, OTG_INFO_SIZE - temp, |
| 2503 | "OTG State: %s\n" |
| 2504 | "OTG Mode: %d\n" |
| 2505 | "OTG Inputs: 0x%lx\n" |
| 2506 | "Charger Type: %d\n" |
| 2507 | "PMIC VBUS Support: %u\n" |
| 2508 | "PMIC ID Support: %u\n" |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2509 | "USB In SPS: %d\n" |
| 2510 | "pre_emphasis_level: 0x%x\n" |
| 2511 | "cdr_auto_reset: 0x%x\n" |
| 2512 | "hs_drv_amplitude: 0x%x\n" |
| 2513 | "se1_gate_state: 0x%x\n" |
| 2514 | "swfi_latency: 0x%x\n" |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2515 | "PHY Powercollapse: 0x%x\n", |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2516 | state_string(dev->phy.state), |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2517 | dev->pdata->otg_mode, |
| 2518 | dev->inputs, |
| 2519 | atomic_read(&dev->chg_type), |
| 2520 | dev->pmic_vbus_notif_supp, |
| 2521 | dev->pmic_id_notif_supp, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2522 | dev->pdata->usb_in_sps, |
| 2523 | dev->pdata->pemp_level, |
| 2524 | dev->pdata->cdr_autoreset, |
| 2525 | dev->pdata->drv_ampl, |
| 2526 | dev->pdata->se1_gating, |
| 2527 | dev->pdata->swfi_latency, |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2528 | dev->pdata->phy_can_powercollapse); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2529 | |
| 2530 | ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp); |
| 2531 | |
| 2532 | kfree(buf); |
| 2533 | |
| 2534 | return ret; |
| 2535 | } |
| 2536 | |
| 2537 | const struct file_operations otgfs_info_fops = { |
| 2538 | .open = otg_open, |
| 2539 | .read = otg_info_read, |
| 2540 | }; |
| 2541 | |
| 2542 | struct dentry *otg_debug_root; |
| 2543 | struct dentry *otg_debug_mode; |
| 2544 | struct dentry *otg_debug_info; |
| 2545 | #endif |
| 2546 | |
| 2547 | static int otg_debugfs_init(struct msm_otg *dev) |
| 2548 | { |
| 2549 | #ifdef CONFIG_DEBUG_FS |
| 2550 | otg_debug_root = debugfs_create_dir("otg", NULL); |
| 2551 | if (!otg_debug_root) |
| 2552 | return -ENOENT; |
| 2553 | |
| 2554 | otg_debug_mode = debugfs_create_file("mode", 0222, |
| 2555 | otg_debug_root, dev, |
| 2556 | &otgfs_fops); |
| 2557 | if (!otg_debug_mode) |
| 2558 | goto free_root; |
| 2559 | |
| 2560 | otg_debug_info = debugfs_create_file("info", 0444, |
| 2561 | otg_debug_root, dev, |
| 2562 | &otgfs_info_fops); |
| 2563 | if (!otg_debug_info) |
| 2564 | goto free_mode; |
| 2565 | |
| 2566 | return 0; |
| 2567 | |
| 2568 | free_mode: |
| 2569 | debugfs_remove(otg_debug_mode); |
| 2570 | otg_debug_mode = NULL; |
| 2571 | |
| 2572 | free_root: |
| 2573 | debugfs_remove(otg_debug_root); |
| 2574 | otg_debug_root = NULL; |
| 2575 | return -ENOENT; |
| 2576 | #endif |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | static void otg_debugfs_cleanup(void) |
| 2581 | { |
| 2582 | #ifdef CONFIG_DEBUG_FS |
| 2583 | debugfs_remove(otg_debug_info); |
| 2584 | debugfs_remove(otg_debug_mode); |
| 2585 | debugfs_remove(otg_debug_root); |
| 2586 | #endif |
| 2587 | } |
| 2588 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2589 | struct usb_phy_io_ops msm_otg_io_ops = { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2590 | .read = usb_ulpi_read, |
| 2591 | .write = usb_ulpi_write, |
| 2592 | }; |
| 2593 | |
| 2594 | static int __init msm_otg_probe(struct platform_device *pdev) |
| 2595 | { |
| 2596 | int ret = 0; |
| 2597 | struct resource *res; |
| 2598 | struct msm_otg *dev; |
| 2599 | |
| 2600 | dev = kzalloc(sizeof(struct msm_otg), GFP_KERNEL); |
| 2601 | if (!dev) |
| 2602 | return -ENOMEM; |
| 2603 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2604 | dev->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL); |
| 2605 | if (!dev->phy.otg) { |
| 2606 | kfree(dev); |
| 2607 | return -ENOMEM; |
| 2608 | } |
| 2609 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2610 | the_msm_otg = dev; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2611 | dev->phy.dev = &pdev->dev; |
| 2612 | dev->phy.otg->phy = &dev->phy; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2613 | dev->pdata = pdev->dev.platform_data; |
| 2614 | |
| 2615 | if (!dev->pdata) { |
| 2616 | ret = -ENODEV; |
| 2617 | goto free_dev; |
| 2618 | } |
| 2619 | |
| 2620 | #ifdef CONFIG_USB_EHCI_MSM_72K |
| 2621 | if (!dev->pdata->vbus_power) { |
| 2622 | ret = -ENODEV; |
| 2623 | goto free_dev; |
| 2624 | } else |
| 2625 | dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0); |
| 2626 | |
| 2627 | #endif |
| 2628 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2629 | if (dev->pdata->rpc_connect) { |
| 2630 | ret = dev->pdata->rpc_connect(1); |
| 2631 | pr_debug("%s: rpc_connect(%d)\n", __func__, ret); |
| 2632 | if (ret) { |
| 2633 | pr_err("%s: rpc connect failed\n", __func__); |
| 2634 | ret = -ENODEV; |
| 2635 | goto free_dev; |
| 2636 | } |
| 2637 | } |
| 2638 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2639 | dev->alt_core_clk = clk_get(&pdev->dev, "alt_core_clk"); |
| 2640 | if (IS_ERR(dev->alt_core_clk)) { |
| 2641 | pr_err("%s: failed to get alt_core_clk\n", __func__); |
| 2642 | ret = PTR_ERR(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2643 | goto rpc_fail; |
| 2644 | } |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2645 | clk_set_rate(dev->alt_core_clk, 60000000); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2646 | |
| 2647 | /* pm qos request to prevent apps idle power collapse */ |
| 2648 | pm_qos_add_request(&dev->pdata->pm_qos_req_dma, PM_QOS_CPU_DMA_LATENCY, |
| 2649 | PM_QOS_DEFAULT_VALUE); |
| 2650 | |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2651 | dev->core_clk = clk_get(&pdev->dev, "core_clk"); |
| 2652 | if (IS_ERR(dev->core_clk)) { |
| 2653 | pr_err("%s: failed to get core_clk\n", __func__); |
| 2654 | ret = PTR_ERR(dev->core_clk); |
| 2655 | goto put_alt_core_clk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2656 | } |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2657 | /* CORE clk must be running at >60Mhz for correct HSUSB operation |
| 2658 | * and USB core cannot tolerate frequency changes on CORE CLK. |
| 2659 | * Vote for maximum clk frequency for CORE clock. |
| 2660 | */ |
| 2661 | clk_set_rate(dev->core_clk, INT_MAX); |
| 2662 | |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2663 | clk_prepare_enable(dev->core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2664 | |
| 2665 | if (!dev->pdata->pclk_is_hw_gated) { |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2666 | dev->iface_clk = clk_get(&pdev->dev, "iface_clk"); |
| 2667 | if (IS_ERR(dev->iface_clk)) { |
| 2668 | pr_err("%s: failed to get abh_clk\n", __func__); |
| 2669 | ret = PTR_ERR(dev->iface_clk); |
| 2670 | goto put_core_clk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2671 | } |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2672 | clk_prepare_enable(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
| 2675 | if (!dev->pdata->phy_reset) { |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2676 | dev->phy_reset_clk = clk_get(&pdev->dev, "phy_clk"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2677 | if (IS_ERR(dev->phy_reset_clk)) { |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2678 | pr_err("%s: failed to get phy_clk\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2679 | ret = PTR_ERR(dev->phy_reset_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2680 | goto put_iface_clk; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | |
| 2684 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2685 | if (!res) { |
| 2686 | pr_err("%s: failed to get platform resource mem\n", __func__); |
| 2687 | ret = -ENODEV; |
| 2688 | goto put_phy_clk; |
| 2689 | } |
| 2690 | |
| 2691 | dev->regs = ioremap(res->start, resource_size(res)); |
| 2692 | if (!dev->regs) { |
| 2693 | pr_err("%s: ioremap failed\n", __func__); |
| 2694 | ret = -ENOMEM; |
| 2695 | goto put_phy_clk; |
| 2696 | } |
| 2697 | dev->irq = platform_get_irq(pdev, 0); |
| 2698 | if (!dev->irq) { |
| 2699 | pr_err("%s: platform_get_irq failed\n", __func__); |
| 2700 | ret = -ENODEV; |
| 2701 | goto free_regs; |
| 2702 | } |
| 2703 | dev->xo_handle = msm_xo_get(MSM_XO_TCXO_D1, "usb"); |
| 2704 | if (IS_ERR(dev->xo_handle)) { |
| 2705 | pr_err(" %s not able to get the handle" |
| 2706 | "to vote for TCXO D1 buffer\n", __func__); |
| 2707 | ret = PTR_ERR(dev->xo_handle); |
| 2708 | goto free_regs; |
| 2709 | } |
| 2710 | |
| 2711 | ret = msm_xo_mode_vote(dev->xo_handle, MSM_XO_MODE_ON); |
| 2712 | if (ret) { |
| 2713 | pr_err("%s failed to vote for TCXO" |
| 2714 | "D1 buffer%d\n", __func__, ret); |
| 2715 | goto free_xo_handle; |
| 2716 | } |
| 2717 | |
| 2718 | |
| 2719 | msm_otg_init_timer(dev); |
| 2720 | INIT_WORK(&dev->sm_work, msm_otg_sm_work); |
| 2721 | INIT_WORK(&dev->otg_resume_work, msm_otg_resume_w); |
| 2722 | spin_lock_init(&dev->lock); |
| 2723 | wake_lock_init(&dev->wlock, WAKE_LOCK_SUSPEND, "msm_otg"); |
| 2724 | |
| 2725 | dev->wq = alloc_workqueue("k_otg", WQ_NON_REENTRANT, 0); |
| 2726 | if (!dev->wq) { |
| 2727 | ret = -ENOMEM; |
| 2728 | goto free_wlock; |
| 2729 | } |
| 2730 | |
| 2731 | if (dev->pdata->init_gpio) { |
| 2732 | ret = dev->pdata->init_gpio(1); |
| 2733 | if (ret) { |
| 2734 | pr_err("%s: gpio init failed with err:%d\n", |
| 2735 | __func__, ret); |
| 2736 | goto free_wq; |
| 2737 | } |
| 2738 | } |
| 2739 | /* To reduce phy power consumption and to avoid external LDO |
| 2740 | * on the board, PMIC comparators can be used to detect VBUS |
| 2741 | * session change. |
| 2742 | */ |
| 2743 | if (dev->pdata->pmic_vbus_notif_init) { |
| 2744 | ret = dev->pdata->pmic_vbus_notif_init |
| 2745 | (&msm_otg_set_vbus_state, 1); |
| 2746 | if (!ret) { |
| 2747 | dev->pmic_vbus_notif_supp = 1; |
| 2748 | } else if (ret != -ENOTSUPP) { |
| 2749 | pr_err("%s: pmic_vbus_notif_init() failed, err:%d\n", |
| 2750 | __func__, ret); |
| 2751 | goto free_gpio; |
| 2752 | } |
| 2753 | } |
| 2754 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 2755 | if (dev->pdata->phy_id_setup_init) { |
| 2756 | ret = dev->pdata->phy_id_setup_init(1); |
| 2757 | if (ret) { |
| 2758 | pr_err("%s: phy_id_setup_init failed err:%d", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2759 | __func__, ret); |
| 2760 | goto free_pmic_vbus_notif; |
| 2761 | } |
| 2762 | } |
| 2763 | |
| 2764 | if (dev->pdata->pmic_vbus_irq) |
| 2765 | dev->vbus_on_irq = dev->pdata->pmic_vbus_irq; |
| 2766 | |
| 2767 | /* vote for vddcx, as PHY cannot tolerate vddcx below 1.0V */ |
| 2768 | if (dev->pdata->init_vddcx) { |
| 2769 | ret = dev->pdata->init_vddcx(1); |
| 2770 | if (ret) { |
| 2771 | pr_err("%s: unable to enable vddcx digital core:%d\n", |
| 2772 | __func__, ret); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 2773 | goto free_phy_id_setup; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | |
| 2777 | if (dev->pdata->ldo_init) { |
| 2778 | ret = dev->pdata->ldo_init(1); |
| 2779 | if (ret) { |
| 2780 | pr_err("%s: ldo_init failed with err:%d\n", |
| 2781 | __func__, ret); |
| 2782 | goto free_config_vddcx; |
| 2783 | } |
| 2784 | } |
| 2785 | |
| 2786 | if (dev->pdata->ldo_enable) { |
| 2787 | ret = dev->pdata->ldo_enable(1); |
| 2788 | if (ret) { |
| 2789 | pr_err("%s: ldo_enable failed with err:%d\n", |
| 2790 | __func__, ret); |
| 2791 | goto free_ldo_init; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | |
| 2796 | /* ACk all pending interrupts and clear interrupt enable registers */ |
| 2797 | writel((readl(USB_OTGSC) & ~OTGSC_INTR_MASK), USB_OTGSC); |
| 2798 | writel(readl(USB_USBSTS), USB_USBSTS); |
| 2799 | writel(0, USB_USBINTR); |
| 2800 | /* Ensure that above STOREs are completed before enabling interrupts */ |
| 2801 | mb(); |
| 2802 | |
| 2803 | ret = request_irq(dev->irq, msm_otg_irq, IRQF_SHARED, |
| 2804 | "msm_otg", dev); |
| 2805 | if (ret) { |
| 2806 | pr_err("%s: request irq failed\n", __func__); |
| 2807 | goto free_ldo_enable; |
| 2808 | } |
| 2809 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2810 | dev->phy.set_suspend = msm_otg_set_suspend; |
| 2811 | dev->phy.set_power = msm_otg_set_power; |
| 2812 | |
| 2813 | dev->phy.otg->set_peripheral = msm_otg_set_peripheral; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2814 | #ifdef CONFIG_USB_EHCI_MSM_72K |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2815 | dev->phy.otg->set_host = msm_otg_set_host; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2816 | #endif |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2817 | dev->phy.otg->start_hnp = msm_otg_start_hnp; |
| 2818 | dev->phy.otg->send_event = msm_otg_send_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2819 | dev->set_clk = msm_otg_set_clk; |
| 2820 | dev->reset = otg_reset; |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2821 | dev->phy.io_ops = &msm_otg_io_ops; |
| 2822 | if (usb_set_transceiver(&dev->phy)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2823 | WARN_ON(1); |
| 2824 | goto free_otg_irq; |
| 2825 | } |
| 2826 | #ifdef CONFIG_USB_MSM_ACA |
| 2827 | /* Link doesnt support id_a/b/c interrupts, hence polling |
| 2828 | * needs to be done to support ACA charger |
| 2829 | */ |
| 2830 | init_timer(&dev->id_timer); |
| 2831 | dev->id_timer.function = msm_otg_id_func; |
| 2832 | dev->id_timer.data = (unsigned long) dev; |
| 2833 | #endif |
| 2834 | |
| 2835 | atomic_set(&dev->chg_type, USB_CHG_TYPE__INVALID); |
| 2836 | if (dev->pdata->chg_init && dev->pdata->chg_init(1)) |
| 2837 | pr_err("%s: chg_init failed\n", __func__); |
| 2838 | |
| 2839 | device_init_wakeup(&pdev->dev, 1); |
| 2840 | |
| 2841 | ret = pm_runtime_set_active(&pdev->dev); |
| 2842 | if (ret < 0) |
| 2843 | pr_err("%s: pm_runtime: Fail to set active\n", __func__); |
| 2844 | |
| 2845 | ret = 0; |
| 2846 | pm_runtime_enable(&pdev->dev); |
| 2847 | pm_runtime_get(&pdev->dev); |
| 2848 | |
| 2849 | |
| 2850 | ret = otg_debugfs_init(dev); |
| 2851 | if (ret) { |
| 2852 | pr_err("%s: otg_debugfs_init failed\n", __func__); |
| 2853 | goto chg_deinit; |
| 2854 | } |
| 2855 | |
| 2856 | #ifdef CONFIG_USB_OTG |
| 2857 | ret = sysfs_create_group(&pdev->dev.kobj, &msm_otg_attr_grp); |
| 2858 | if (ret < 0) { |
| 2859 | pr_err("%s: Failed to create the sysfs entry\n", __func__); |
| 2860 | otg_debugfs_cleanup(); |
| 2861 | goto chg_deinit; |
| 2862 | } |
| 2863 | #endif |
| 2864 | |
| 2865 | |
| 2866 | return 0; |
| 2867 | |
| 2868 | chg_deinit: |
| 2869 | if (dev->pdata->chg_init) |
| 2870 | dev->pdata->chg_init(0); |
| 2871 | free_otg_irq: |
| 2872 | free_irq(dev->irq, dev); |
| 2873 | free_ldo_enable: |
| 2874 | if (dev->pdata->ldo_enable) |
| 2875 | dev->pdata->ldo_enable(0); |
| 2876 | if (dev->pdata->setup_gpio) |
| 2877 | dev->pdata->setup_gpio(USB_SWITCH_DISABLE); |
| 2878 | free_ldo_init: |
| 2879 | if (dev->pdata->ldo_init) |
| 2880 | dev->pdata->ldo_init(0); |
| 2881 | free_config_vddcx: |
| 2882 | if (dev->pdata->init_vddcx) |
| 2883 | dev->pdata->init_vddcx(0); |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 2884 | free_phy_id_setup: |
| 2885 | if (dev->pdata->phy_id_setup_init) |
| 2886 | dev->pdata->phy_id_setup_init(0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2887 | free_pmic_vbus_notif: |
| 2888 | if (dev->pdata->pmic_vbus_notif_init && dev->pmic_vbus_notif_supp) |
| 2889 | dev->pdata->pmic_vbus_notif_init(&msm_otg_set_vbus_state, 0); |
| 2890 | free_gpio: |
| 2891 | if (dev->pdata->init_gpio) |
| 2892 | dev->pdata->init_gpio(0); |
| 2893 | free_wq: |
| 2894 | destroy_workqueue(dev->wq); |
| 2895 | free_wlock: |
| 2896 | wake_lock_destroy(&dev->wlock); |
| 2897 | free_xo_handle: |
| 2898 | msm_xo_put(dev->xo_handle); |
| 2899 | free_regs: |
| 2900 | iounmap(dev->regs); |
| 2901 | put_phy_clk: |
| 2902 | if (dev->phy_reset_clk) |
| 2903 | clk_put(dev->phy_reset_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2904 | put_iface_clk: |
| 2905 | if (dev->iface_clk) { |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2906 | clk_disable_unprepare(dev->iface_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2907 | clk_put(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2908 | } |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2909 | put_core_clk: |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2910 | clk_disable_unprepare(dev->core_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2911 | clk_put(dev->core_clk); |
| 2912 | put_alt_core_clk: |
| 2913 | clk_put(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2914 | rpc_fail: |
| 2915 | if (dev->pdata->rpc_connect) |
| 2916 | dev->pdata->rpc_connect(0); |
| 2917 | free_dev: |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2918 | kfree(dev->phy.otg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2919 | kfree(dev); |
| 2920 | return ret; |
| 2921 | } |
| 2922 | |
| 2923 | static int __exit msm_otg_remove(struct platform_device *pdev) |
| 2924 | { |
| 2925 | struct msm_otg *dev = the_msm_otg; |
| 2926 | |
| 2927 | otg_debugfs_cleanup(); |
| 2928 | #ifdef CONFIG_USB_OTG |
| 2929 | sysfs_remove_group(&pdev->dev.kobj, &msm_otg_attr_grp); |
| 2930 | #endif |
| 2931 | destroy_workqueue(dev->wq); |
| 2932 | wake_lock_destroy(&dev->wlock); |
| 2933 | |
| 2934 | if (dev->pdata->setup_gpio) |
| 2935 | dev->pdata->setup_gpio(USB_SWITCH_DISABLE); |
| 2936 | |
| 2937 | if (dev->pdata->init_vddcx) |
| 2938 | dev->pdata->init_vddcx(0); |
| 2939 | if (dev->pdata->ldo_enable) |
| 2940 | dev->pdata->ldo_enable(0); |
| 2941 | |
| 2942 | if (dev->pdata->ldo_init) |
| 2943 | dev->pdata->ldo_init(0); |
| 2944 | |
| 2945 | if (dev->pmic_vbus_notif_supp) |
| 2946 | dev->pdata->pmic_vbus_notif_init(&msm_otg_set_vbus_state, 0); |
| 2947 | |
Anji jonnala | ae745e9 | 2011-11-14 18:34:31 +0530 | [diff] [blame] | 2948 | if (dev->pdata->phy_id_setup_init) |
| 2949 | dev->pdata->phy_id_setup_init(0); |
| 2950 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2951 | if (dev->pmic_id_notif_supp) |
| 2952 | dev->pdata->pmic_id_notif_init(&msm_otg_set_id_state, 0); |
| 2953 | |
| 2954 | #ifdef CONFIG_USB_MSM_ACA |
| 2955 | del_timer_sync(&dev->id_timer); |
| 2956 | #endif |
| 2957 | if (dev->pdata->chg_init) |
| 2958 | dev->pdata->chg_init(0); |
| 2959 | free_irq(dev->irq, pdev); |
| 2960 | iounmap(dev->regs); |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2961 | clk_disable_unprepare(dev->core_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2962 | clk_put(dev->core_clk); |
| 2963 | if (dev->iface_clk) { |
Manu Gautam | 28b1bac | 2012-01-30 16:43:06 +0530 | [diff] [blame] | 2964 | clk_disable_unprepare(dev->iface_clk); |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2965 | clk_put(dev->iface_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2966 | } |
Manu Gautam | 5143b25 | 2012-01-05 19:25:23 -0800 | [diff] [blame] | 2967 | if (dev->alt_core_clk) |
| 2968 | clk_put(dev->alt_core_clk); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2969 | if (dev->phy_reset_clk) |
| 2970 | clk_put(dev->phy_reset_clk); |
| 2971 | if (dev->pdata->rpc_connect) |
| 2972 | dev->pdata->rpc_connect(0); |
| 2973 | msm_xo_put(dev->xo_handle); |
Pavankumar Kondeti | f0f95d8 | 2011-09-23 11:38:57 +0530 | [diff] [blame] | 2974 | pm_qos_remove_request(&dev->pdata->pm_qos_req_dma); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2975 | |
| 2976 | pm_runtime_put(&pdev->dev); |
| 2977 | pm_runtime_disable(&pdev->dev); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2978 | kfree(dev->phy.otg); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2979 | kfree(dev); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2980 | return 0; |
| 2981 | } |
| 2982 | |
| 2983 | static int msm_otg_runtime_suspend(struct device *dev) |
| 2984 | { |
| 2985 | struct msm_otg *otg = the_msm_otg; |
| 2986 | |
| 2987 | dev_dbg(dev, "pm_runtime: suspending...\n"); |
| 2988 | msm_otg_suspend(otg); |
| 2989 | return 0; |
| 2990 | } |
| 2991 | |
| 2992 | static int msm_otg_runtime_resume(struct device *dev) |
| 2993 | { |
| 2994 | struct msm_otg *otg = the_msm_otg; |
| 2995 | |
| 2996 | dev_dbg(dev, "pm_runtime: resuming...\n"); |
| 2997 | msm_otg_resume(otg); |
| 2998 | return 0; |
| 2999 | } |
| 3000 | |
| 3001 | static int msm_otg_runtime_idle(struct device *dev) |
| 3002 | { |
| 3003 | dev_dbg(dev, "pm_runtime: idling...\n"); |
| 3004 | return 0; |
| 3005 | } |
| 3006 | |
| 3007 | static struct dev_pm_ops msm_otg_dev_pm_ops = { |
| 3008 | .runtime_suspend = msm_otg_runtime_suspend, |
| 3009 | .runtime_resume = msm_otg_runtime_resume, |
| 3010 | .runtime_idle = msm_otg_runtime_idle, |
| 3011 | }; |
| 3012 | |
| 3013 | static struct platform_driver msm_otg_driver = { |
| 3014 | .remove = __exit_p(msm_otg_remove), |
| 3015 | .driver = { |
| 3016 | .name = DRIVER_NAME, |
| 3017 | .owner = THIS_MODULE, |
| 3018 | .pm = &msm_otg_dev_pm_ops, |
| 3019 | }, |
| 3020 | }; |
| 3021 | |
| 3022 | static int __init msm_otg_init(void) |
| 3023 | { |
| 3024 | return platform_driver_probe(&msm_otg_driver, msm_otg_probe); |
| 3025 | } |
| 3026 | |
| 3027 | static void __exit msm_otg_exit(void) |
| 3028 | { |
| 3029 | platform_driver_unregister(&msm_otg_driver); |
| 3030 | } |
| 3031 | |
| 3032 | module_init(msm_otg_init); |
| 3033 | module_exit(msm_otg_exit); |
| 3034 | |
| 3035 | MODULE_LICENSE("GPL v2"); |
| 3036 | MODULE_DESCRIPTION("MSM usb transceiver driver"); |
| 3037 | MODULE_VERSION("1.00"); |