blob: ca1b1559607698510311332f46dba65b516a11f7 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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 Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027#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 Mucklef132c6c2012-06-06 18:30:57 -070038static void otg_reset(struct usb_phy *phy, int phy_reset);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039static void msm_otg_set_vbus_state(int online);
Anji jonnalaae745e92011-11-14 18:34:31 +053040#ifdef CONFIG_USB_EHCI_MSM_72K
41static void msm_otg_set_id_state(int id);
42#else
43static void msm_otg_set_id_state(int id)
44{
45}
46#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047
48struct msm_otg *the_msm_otg;
49
50static int is_host(void)
51{
52 struct msm_otg *dev = the_msm_otg;
53
Anji jonnalaae745e92011-11-14 18:34:31 +053054 if (dev->pdata->otg_mode == OTG_ID)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070055 return (OTGSC_ID & readl(USB_OTGSC)) ? 0 : 1;
56 else
57 return !test_bit(ID, &dev->inputs);
58}
59
60static 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
70static 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
98static 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 Mucklef132c6c2012-06-06 18:30:57 -0700125static int usb_ulpi_write(struct usb_phy *xceiv, u32 val, u32 reg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700127 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128
129 return ulpi_write(dev, val, reg);
130}
131
Steve Mucklef132c6c2012-06-06 18:30:57 -0700132static int usb_ulpi_read(struct usb_phy *xceiv, u32 reg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700134 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
136 return ulpi_read(dev, reg);
137}
138
139#ifdef CONFIG_USB_EHCI_MSM_72K
140static void enable_idgnd(struct msm_otg *dev)
141{
Anji jonnalaae745e92011-11-14 18:34:31 +0530142 unsigned temp;
143
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144 /* 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 jonnalaae745e92011-11-14 18:34:31 +0530150 ulpi_write(dev, (1<<0), 0x0B);
151 temp = OTGSC_IDIE | OTGSC_IDPU;
152 writel_relaxed(readl_relaxed(USB_OTGSC) | temp, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700153}
154
155static void disable_idgnd(struct msm_otg *dev)
156{
Anji jonnalaae745e92011-11-14 18:34:31 +0530157 unsigned temp;
158
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 /* Do nothing if instead of ID pin, USER controls mode switch */
160 if (dev->pdata->otg_mode == OTG_USER_CONTROL)
161 return;
Anji jonnalaae745e92011-11-14 18:34:31 +0530162 temp = OTGSC_IDIE | OTGSC_IDPU;
163 writel_relaxed(readl_relaxed(USB_OTGSC) & ~temp, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700164 ulpi_write(dev, (1<<4), 0x0F);
165 ulpi_write(dev, (1<<4), 0x12);
Anji jonnalaae745e92011-11-14 18:34:31 +0530166 ulpi_write(dev, (1<<0), 0x0C);
167}
168#else
169static void enable_idgnd(struct msm_otg *dev)
170{
171}
172static void disable_idgnd(struct msm_otg *dev)
173{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174}
175#endif
176
177static 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}
184static 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
192static 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
203static 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
214static 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 jonnalaae745e92011-11-14 18:34:31 +0530245static void set_aca_id_inputs(struct msm_otg *dev)
246{
247}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248#define get_aca_bmaxpower(dev) 0
249#define set_aca_bmaxpower(dev, power)
250#endif
251static 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
265static 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
278static 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
293static 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}
307static 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
321static 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
341static 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 */
356static 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700372/* 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 */
377static 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
394static 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
405static 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 */
416static 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
422static 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 Mucklef132c6c2012-06-06 18:30:57 -0700442static int msm_otg_send_event(struct usb_otg *otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700443 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 Mucklef132c6c2012-06-06 18:30:57 -0700454 ret = kobject_uevent_env(&otg->phy->dev->kobj, KOBJ_CHANGE, envp);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455 if (ret < 0)
456 pr_info("uevent sending failed with ret = %d\n", ret);
457 return ret;
458}
459
Steve Mucklef132c6c2012-06-06 18:30:57 -0700460static int msm_otg_start_hnp(struct usb_otg *otg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700462 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700463 enum usb_otg_state state;
464 unsigned long flags;
465
466 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700467 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468 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 Mucklef132c6c2012-06-06 18:30:57 -0700483static int msm_otg_start_srp(struct usb_otg *otg)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700485 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700486 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 Mucklef132c6c2012-06-06 18:30:57 -0700492 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700493 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 */
516out:
517 return ret;
518}
519
Steve Mucklef132c6c2012-06-06 18:30:57 -0700520static int msm_otg_set_power(struct usb_phy *xceiv, unsigned mA)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700521{
522 static enum chg_type curr_chg = USB_CHG_TYPE__INVALID;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700523 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524 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 Mucklef132c6c2012-06-06 18:30:57 -0700556static int msm_otg_set_clk(struct usb_phy *xceiv, int on)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700557{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700558 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559
560 if (!dev || (dev != the_msm_otg))
561 return -ENODEV;
562
563 if (on)
564 /* enable clocks */
Manu Gautam28b1bac2012-01-30 16:43:06 +0530565 clk_prepare_enable(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 else
Manu Gautam28b1bac2012-01-30 16:43:06 +0530567 clk_disable_unprepare(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568
569 return 0;
570}
Steve Mucklef132c6c2012-06-06 18:30:57 -0700571static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700572{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700573 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574 struct msm_otg_platform_data *pdata = dev->pdata;
575
Steve Mucklef132c6c2012-06-06 18:30:57 -0700576 if (!otg->gadget)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700577 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 Gautam28b1bac2012-01-30 16:43:06 +0530592 clk_prepare_enable(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700593
Steve Mucklef132c6c2012-06-06 18:30:57 -0700594 usb_gadget_vbus_connect(otg->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 } else {
596 atomic_set(&dev->chg_type, USB_CHG_TYPE__INVALID);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700597 usb_gadget_vbus_disconnect(otg->gadget);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700598
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 Gautam28b1bac2012-01-30 16:43:06 +0530604 clk_disable_unprepare(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605
606 otg_pm_qos_update_latency(dev, 0);
607 if (pdata->setup_gpio)
608 pdata->setup_gpio(USB_SWITCH_DISABLE);
609 }
610}
611
Steve Mucklef132c6c2012-06-06 18:30:57 -0700612static void msm_otg_start_host(struct usb_otg *otg, int on)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700613{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700614 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700615 struct msm_otg_platform_data *pdata = dev->pdata;
616
Steve Mucklef132c6c2012-06-06 18:30:57 -0700617 if (!otg->host)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 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 Gautam28b1bac2012-01-30 16:43:06 +0530631 clk_prepare_enable(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 else
Manu Gautam28b1bac2012-01-30 16:43:06 +0530633 clk_disable_unprepare(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700634 }
635
Steve Mucklef132c6c2012-06-06 18:30:57 -0700636 dev->start_host(otg->host, on);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
638 if (!on && pdata->setup_gpio)
639 pdata->setup_gpio(USB_SWITCH_DISABLE);
640 }
641}
642
643static 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 Mucklef132c6c2012-06-06 18:30:57 -0700699 host_bus_suspend = dev->phy.otg->host && is_host();
Anji jonnalaae745e92011-11-14 18:34:31 +0530700
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 Mucklef132c6c2012-06-06 18:30:57 -0700725 if ((dev->phy.otg->gadget && chg_type == USB_CHG_TYPE__WALLCHARGER) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 host_bus_suspend ||
Steve Mucklef132c6c2012-06-06 18:30:57 -0700727 (dev->phy.otg->host && !dev->pmic_id_notif_supp) ||
728 (dev->phy.otg->gadget && !dev->pmic_vbus_notif_supp)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 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 Mucklef132c6c2012-06-06 18:30:57 -0700743 dev->phy.state = OTG_STATE_UNDEFINED;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700744 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 Gautam5143b252012-01-05 19:25:23 -0800761 if (dev->iface_clk)
Manu Gautam28b1bac2012-01-30 16:43:06 +0530762 clk_disable_unprepare(dev->iface_clk);
Manu Gautam5143b252012-01-05 19:25:23 -0800763
Manu Gautam28b1bac2012-01-30 16:43:06 +0530764 clk_disable_unprepare(dev->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 /* 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 Mucklef132c6c2012-06-06 18:30:57 -0700771 if (device_may_wakeup(dev->phy.dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700772 enable_irq_wake(dev->irq);
773 if (dev->vbus_on_irq)
774 enable_irq_wake(dev->vbus_on_irq);
Anji jonnalaeb9e60d2011-10-05 12:19:46 +0530775 if (dev->id_irq)
776 enable_irq_wake(dev->id_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 }
778
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 atomic_set(&dev->in_lpm, 1);
780
Anji jonnala3498c472011-11-04 13:30:14 +0530781 /*
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 jonnalaae745e92011-11-14 18:34:31 +0530791 if (!host_bus_suspend && dev->pmic_vbus_notif_supp &&
792 !test_bit(ID_A, &dev->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700793 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
808out:
809 enable_irq(dev->irq);
810
811 return 0;
812}
813
814static 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 Gautam28b1bac2012-01-30 16:43:06 +0530838 clk_prepare_enable(dev->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700839
Manu Gautam5143b252012-01-05 19:25:23 -0800840 if (dev->iface_clk)
Manu Gautam28b1bac2012-01-30 16:43:06 +0530841 clk_prepare_enable(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700842
843 temp = readl(USB_USBCMD);
844 temp &= ~ASYNC_INTR_CTRL;
845 temp &= ~ULPI_STP_CTRL;
846 writel(temp, USB_USBCMD);
847
Steve Mucklef132c6c2012-06-06 18:30:57 -0700848 if (device_may_wakeup(dev->phy.dev)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700849 disable_irq_wake(dev->irq);
850 if (dev->vbus_on_irq)
851 disable_irq_wake(dev->vbus_on_irq);
Anji jonnalaeb9e60d2011-10-05 12:19:46 +0530852 if (dev->id_irq)
853 disable_irq_wake(dev->id_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 }
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
863static void msm_otg_get_resume(struct msm_otg *dev)
864{
865#ifdef CONFIG_PM_RUNTIME
Steve Mucklef132c6c2012-06-06 18:30:57 -0700866 pm_runtime_get_noresume(dev->phy.dev);
867 pm_runtime_resume(dev->phy.dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700868#else
869 msm_otg_resume(dev);
870#endif
871}
872
873static void msm_otg_put_suspend(struct msm_otg *dev)
874{
875#ifdef CONFIG_PM_RUNTIME
Steve Mucklef132c6c2012-06-06 18:30:57 -0700876 pm_runtime_put_sync(dev->phy.dev);
Pavankumar Kondetidc60f122011-09-13 11:17:16 +0530877 if (!atomic_read(&dev->in_lpm))
Steve Mucklef132c6c2012-06-06 18:30:57 -0700878 pm_runtime_get_sync(dev->phy.dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879#else
880 msm_otg_suspend(dev);
881#endif
882}
883
884static 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 jonnala3498c472011-11-04 13:30:14 +0530889 if (can_phy_power_collapse(dev) && dev->pdata->ldo_enable)
890 dev->pdata->ldo_enable(1);
891
Steve Mucklef132c6c2012-06-06 18:30:57 -0700892 if (pm_runtime_enabled(dev->phy.dev)) {
Chiranjeevi Velempatid515ac12012-05-22 12:08:13 +0530893 msm_otg_get_resume(dev);
894 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700895 pm_runtime_get_noresume(dev->phy.dev);
Chiranjeevi Velempatid515ac12012-05-22 12:08:13 +0530896 msm_otg_resume(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700897 pm_runtime_set_active(dev->phy.dev);
Chiranjeevi Velempatid515ac12012-05-22 12:08:13 +0530898 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700899
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 Mucklef132c6c2012-06-06 18:30:57 -0700910 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700911 break;
912 }
913 udelay(10);
914 }
915
916phy_resumed:
Anji jonnalaae745e92011-11-14 18:34:31 +0530917 /*
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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 /* Enable Idabc interrupts as these were disabled before entering LPM */
936 enable_idabc(dev);
937
Anji jonnalaae745e92011-11-14 18:34:31 +0530938 /*
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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700946 /* 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 Mucklef132c6c2012-06-06 18:30:57 -0700951 msm_otg_start_host(dev->phy.otg, REQUEST_RESUME);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 }
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 Mucklef132c6c2012-06-06 18:30:57 -0700961static int msm_otg_set_suspend(struct usb_phy *xceiv, int suspend)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700963 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700964 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 Mucklef132c6c2012-06-06 18:30:57 -0700971 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700972 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 Mucklef132c6c2012-06-06 18:30:57 -0700992 if (xceiv->otg->gadget->b_hnp_enable) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700993 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 Mucklef132c6c2012-06-06 18:30:57 -07001020 dev->phy.state = OTG_STATE_A_HOST;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 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 Mucklef132c6c2012-06-06 18:30:57 -07001053 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001054 break;
1055 }
1056 udelay(10);
1057 }
Anji jonnalaae745e92011-11-14 18:34:31 +05301058 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001064out:
1065 enable_idabc(dev);
1066 enable_irq(dev->irq);
1067
1068 }
1069
1070 return 0;
1071}
1072
Steve Mucklef132c6c2012-06-06 18:30:57 -07001073static int msm_otg_set_peripheral(struct usb_otg *otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001074 struct usb_gadget *gadget)
1075{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001076 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001077
1078 if (!dev || (dev != the_msm_otg))
1079 return -ENODEV;
1080
1081 if (!gadget) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001082 msm_otg_start_peripheral(otg, 0);
1083 otg->gadget = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001084 disable_sess_valid(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001085 if (!otg->host)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001086 disable_idabc(dev);
1087 return 0;
1088 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07001089 otg->gadget = gadget;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001090 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
1098static 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 Mucklef132c6c2012-06-06 18:30:57 -07001114 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001115 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 }
1144do_work:
1145 if (work) {
1146 wake_lock(&dev->wlock);
1147 queue_work(dev->wq, &dev->sm_work);
1148 }
1149out:
1150 return NOTIFY_OK;
1151}
1152
Steve Mucklef132c6c2012-06-06 18:30:57 -07001153static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001154{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001155 struct msm_otg *dev = container_of(otg->phy, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001156
1157 if (!dev || (dev != the_msm_otg))
1158 return -ENODEV;
1159
1160 if (!dev->start_host)
1161 return -ENODEV;
1162
1163 if (!host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001164 msm_otg_start_host(otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 usb_unregister_notify(&dev->usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001166 otg->host = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167 dev->start_host = 0;
1168 disable_idgnd(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001169 if (!otg->gadget)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001170 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 Mucklef132c6c2012-06-06 18:30:57 -07001178 otg->host = host;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001187
Anji jonnalaae745e92011-11-14 18:34:31 +05301188static void msm_otg_set_id_state(int id)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189{
1190 struct msm_otg *dev = the_msm_otg;
1191 unsigned long flags;
1192
Anji jonnalaae745e92011-11-14 18:34:31 +05301193 if (!atomic_read(&dev->in_lpm))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 return;
1195
1196 if (id) {
1197 set_bit(ID, &dev->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198 } else {
1199 clear_bit(ID, &dev->inputs);
1200 set_bit(A_BUS_REQ, &dev->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 }
1202 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001203 if (dev->phy.state != OTG_STATE_UNDEFINED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204 wake_lock(&dev->wlock);
1205 queue_work(dev->wq, &dev->sm_work);
1206 }
1207 spin_unlock_irqrestore(&dev->lock, flags);
1208}
Anji jonnalaae745e92011-11-14 18:34:31 +05301209#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210
1211void msm_otg_set_vbus_state(int online)
1212{
1213 struct msm_otg *dev = the_msm_otg;
1214
Rajkumar Raghupathyf5c6c602011-11-25 16:20:42 +05301215 /*
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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001224
1225 wake_lock(&dev->wlock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 queue_work(dev->wq, &dev->sm_work);
1227}
1228
1229static irqreturn_t msm_otg_irq(int irq, void *data)
1230{
1231 struct msm_otg *dev = data;
Rajkumar Raghupathy020d62d2012-09-24 15:19:51 +05301232 u32 otgsc, sts, pc;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 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 Raghupathy020d62d2012-09-24 15:19:51 +05301253 /* 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 ret = IRQ_NONE;
1260 goto out;
1261 }
Rajkumar Raghupathy020d62d2012-09-24 15:19:51 +05301262 writel_relaxed(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263
1264 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001265 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001284 work = 1;
1285 } else if (otgsc & OTGSC_BSVIS) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001286 /* 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001303 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 Mucklef132c6c2012-06-06 18:30:57 -07001316 if (dev->phy.otg->host->b_hnp_enable &&
1317 (pc & PORTSC_CSC) &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 !(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 }
1351out:
1352 return ret;
1353}
1354
1355#define ULPI_VERIFY_MAX_LOOP_COUNT 5
1356#define PHY_CALIB_RETRY_COUNT 10
1357static 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
1382static 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
1401static int ulpi_write_with_reset(struct msm_otg *dev,
1402unsigned 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 */
1423static 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
1450static 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
1465static 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
1488static int msm_otg_phy_reset(struct msm_otg *dev)
1489{
1490 unsigned rc;
1491 unsigned temp;
1492 unsigned long timeout;
1493
Manu Gautam5143b252012-01-05 19:25:23 -08001494 rc = clk_reset(dev->alt_core_clk, CLK_RESET_ASSERT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495 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 Gautam5143b252012-01-05 19:25:23 -08001502 rc = clk_reset(dev->alt_core_clk, CLK_RESET_DEASSERT);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 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 Mucklef132c6c2012-06-06 18:30:57 -07001555static void otg_reset(struct usb_phy *xceiv, int phy_reset)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001556{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001557 struct msm_otg *dev = container_of(xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558 unsigned long timeout;
1559 u32 mode, work = 0;
1560
Manu Gautam28b1bac2012-01-30 16:43:06 +05301561 clk_prepare_enable(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562
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
1576reset_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 Devireddyb3fc78c2011-08-30 17:25:55 +05301598 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 /* Ensure that RESET operation is completed before turning off clock */
1605 mb();
1606
Manu Gautam28b1bac2012-01-30 16:43:06 +05301607 clk_disable_unprepare(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001608
Steve Mucklef132c6c2012-06-06 18:30:57 -07001609 if ((xceiv->otg->gadget && xceiv->otg->gadget->is_a_peripheral) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 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 Mucklef132c6c2012-06-06 18:30:57 -07001617 if (dev->phy.otg->gadget) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618 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 Mucklef132c6c2012-06-06 18:30:57 -07001636 if (dev->phy.otg->host && !dev->pmic_id_notif_supp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001637 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
1662static 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 Mucklef132c6c2012-06-06 18:30:57 -07001672 msm_otg_set_suspend(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001673
1674 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001675 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 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 Kondetidc60f122011-09-13 11:17:16 +05301683 * 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07001686 */
1687 if (atomic_read(&dev->chg_type) ==
1688 USB_CHG_TYPE__WALLCHARGER) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001689 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 dev->phy.state = OTG_STATE_B_PERIPHERAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001691 spin_unlock_irqrestore(&dev->lock, flags);
1692 work = 1;
1693 break;
1694 }
1695
1696 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001697 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698
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 Mucklef132c6c2012-06-06 18:30:57 -07001704 !dev->phy.otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001705 set_bit(ID, &dev->inputs);
1706 set_bit(B_SESS_VLD, &dev->inputs);
1707 }
1708 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001709 if (!dev->phy.otg->host || !is_host())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001710 set_bit(ID, &dev->inputs);
1711
Steve Mucklef132c6c2012-06-06 18:30:57 -07001712 if (dev->phy.otg->gadget && is_b_sess_vld())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001713 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 Mucklef132c6c2012-06-06 18:30:57 -07001718 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001719 } else {
1720 set_bit(A_BUS_REQ, &dev->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001721 dev->phy.state = OTG_STATE_A_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 }
1723 spin_unlock_irqrestore(&dev->lock, flags);
1724
1725 work = 1;
1726 break;
1727 case OTG_STATE_B_IDLE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001728 dev->phy.otg->default_a = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 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 Mucklef132c6c2012-06-06 18:30:57 -07001733 otg_reset(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001734
1735 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001736 dev->phy.state = OTG_STATE_A_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001738 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001739 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 Mucklef132c6c2012-06-06 18:30:57 -07001744 dev->phy.state = OTG_STATE_B_PERIPHERAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001745 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001746 msm_otg_set_power(&dev->phy, 0);
1747 msm_otg_start_peripheral(dev->phy.otg, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 } else if (test_bit(B_BUS_REQ, &dev->inputs)) {
1749 pr_debug("b_sess_end && b_bus_req\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07001750 ret = msm_otg_start_srp(dev->phy.otg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001751 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 Mucklef132c6c2012-06-06 18:30:57 -07001758 dev->phy.state = OTG_STATE_B_SRP_INIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001759 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 Mucklef132c6c2012-06-06 18:30:57 -07001764 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001765 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001766 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001767 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 Mucklef132c6c2012-06-06 18:30:57 -07001783 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001784 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 Mucklef132c6c2012-06-06 18:30:57 -07001789 msm_otg_send_event(dev->phy.otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001790 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 Mucklef132c6c2012-06-06 18:30:57 -07001794 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001795 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 Mucklef132c6c2012-06-06 18:30:57 -07001808 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001809 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001810 msm_otg_start_peripheral(dev->phy.otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811 dev->b_last_se0_sess = jiffies;
1812
1813 /* Workaround: Reset phy after session */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001814 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815 work = 1;
1816 } else if (test_bit(B_BUS_REQ, &dev->inputs) &&
Steve Mucklef132c6c2012-06-06 18:30:57 -07001817 dev->phy.otg->gadget->b_hnp_enable &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001818 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 Mucklef132c6c2012-06-06 18:30:57 -07001821 msm_otg_start_peripheral(dev->phy.otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001822 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001823 dev->phy.state = OTG_STATE_B_WAIT_ACON;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001824 spin_unlock_irqrestore(&dev->lock, flags);
1825 /* start HCD even before A-device enable
1826 * pull-up to meet HNP timings.
1827 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001828 dev->phy.otg->host->is_b_host = 1;
1829 msm_otg_start_host(dev->phy.otg, REQUEST_START);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001830
1831 } else if (test_bit(ID_C, &dev->inputs)) {
1832 atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001833 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001834 } 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 Mucklef132c6c2012-06-06 18:30:57 -07001839 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 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 Mucklef132c6c2012-06-06 18:30:57 -07001856 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
1857 dev->phy.otg->host->is_b_host = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858
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 Mucklef132c6c2012-06-06 18:30:57 -07001863 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 spin_unlock_irqrestore(&dev->lock, flags);
1865
1866 /* Workaround: Reset phy after session */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001867 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 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 Mucklef132c6c2012-06-06 18:30:57 -07001873 dev->phy.state = OTG_STATE_B_HOST;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 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 Mucklef132c6c2012-06-06 18:30:57 -07001877 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 }
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 Mucklef132c6c2012-06-06 18:30:57 -07001885 msm_otg_send_event(dev->phy.otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001887 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
1888 dev->phy.otg->host->is_b_host = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 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 Mucklef132c6c2012-06-06 18:30:57 -07001894 dev->phy.state = OTG_STATE_B_PERIPHERAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001896 msm_otg_start_peripheral(dev->phy.otg, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 } else if (test_bit(ID_C, &dev->inputs)) {
1898 atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001899 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001900 }
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 Mucklef132c6c2012-06-06 18:30:57 -07001912 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
1913 dev->phy.otg->host->is_b_host = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914
1915 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001916 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917 spin_unlock_irqrestore(&dev->lock, flags);
1918 /* Workaround: Reset phy after session */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001919 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920 work = 1;
1921 } else if (test_bit(ID_C, &dev->inputs)) {
1922 atomic_set(&dev->chg_type, USB_CHG_TYPE__SDP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001923 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 }
1925 break;
1926 case OTG_STATE_A_IDLE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001927 dev->phy.otg->default_a = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928 if (test_bit(ID, &dev->inputs) &&
1929 !test_bit(ID_A, &dev->inputs)) {
1930 pr_debug("id && !id_a\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07001931 dev->phy.otg->default_a = 0;
1932 otg_reset(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001934 dev->phy.state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001935 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001936 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001937 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 Mucklef132c6c2012-06-06 18:30:57 -07001949 dev->phy.state = OTG_STATE_A_WAIT_VRISE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 spin_unlock_irqrestore(&dev->lock, flags);
1951 /* ACA: ID_A: Stop charging untill enumeration */
1952 if (test_bit(ID_A, &dev->inputs))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001953 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001954 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 Mucklef132c6c2012-06-06 18:30:57 -07001980 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001981 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 Mucklef132c6c2012-06-06 18:30:57 -07001986 dev->phy.state = OTG_STATE_A_WAIT_BCON;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987 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 Mucklef132c6c2012-06-06 18:30:57 -07001992 msm_otg_start_host(dev->phy.otg, REQUEST_START);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001993 }
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 Mucklef132c6c2012-06-06 18:30:57 -07002003 msm_otg_send_event(dev->phy.otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002004 OTG_EVENT_DEV_CONN_TMOUT);
2005 msm_otg_del_timer(dev);
2006 clear_bit(A_BUS_REQ, &dev->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002007 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002008 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002009 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 /* 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 Mucklef132c6c2012-06-06 18:30:57 -07002014 msm_otg_set_power(&dev->phy, USB_IDCHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015 else
2016 dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0);
2017 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002018 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002019 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 Mucklef132c6c2012-06-06 18:30:57 -07002028 dev->phy.state = OTG_STATE_A_HOST;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002029 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 Mucklef132c6c2012-06-06 18:30:57 -07002032 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033 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 Mucklef132c6c2012-06-06 18:30:57 -07002038 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002039 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002040 dev->phy.state = OTG_STATE_A_VBUS_ERR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002041 spin_unlock_irqrestore(&dev->lock, flags);
2042 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002043 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002047 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 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 Mucklef132c6c2012-06-06 18:30:57 -07002058 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002060 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002061 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002062 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002063 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 Mucklef132c6c2012-06-06 18:30:57 -07002066 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002071 dev->phy.state = OTG_STATE_A_VBUS_ERR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002072 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002073 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002074 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002075 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002076 /* 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 Mucklef132c6c2012-06-06 18:30:57 -07002083 dev->phy.state = OTG_STATE_A_SUSPEND;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002084 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002085 if (dev->phy.otg->host->b_hnp_enable) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002086 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 Mucklef132c6c2012-06-06 18:30:57 -07002093 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094 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 Mucklef132c6c2012-06-06 18:30:57 -07002098 dev->phy.state = OTG_STATE_A_WAIT_BCON;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 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 Mucklef132c6c2012-06-06 18:30:57 -07002106 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002107 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 Mucklef132c6c2012-06-06 18:30:57 -07002110 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002111 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 Mucklef132c6c2012-06-06 18:30:57 -07002122 msm_otg_send_event(dev->phy.otg,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002123 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 Mucklef132c6c2012-06-06 18:30:57 -07002127 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002128 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002129 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002130 dev->pdata->vbus_power(USB_PHY_INTEGRATED, 0);
2131 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002132 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133 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 Mucklef132c6c2012-06-06 18:30:57 -07002136 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002137 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002142 dev->phy.state = OTG_STATE_A_VBUS_ERR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002143 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002144 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002145 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002146 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002147 } else if (!test_bit(B_CONN, &dev->inputs) &&
Steve Mucklef132c6c2012-06-06 18:30:57 -07002148 dev->phy.otg->host->b_hnp_enable) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002149 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 Mucklef132c6c2012-06-06 18:30:57 -07002153 dev->phy.state = OTG_STATE_A_PERIPHERAL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002154 spin_unlock_irqrestore(&dev->lock, flags);
2155
Steve Mucklef132c6c2012-06-06 18:30:57 -07002156 msm_otg_start_host(dev->phy.otg, REQUEST_HNP_SUSPEND);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002157
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 Mucklef132c6c2012-06-06 18:30:57 -07002162 dev->phy.otg->gadget->is_a_peripheral = 1;
2163 msm_otg_start_peripheral(dev->phy.otg, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 /* 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 Mucklef132c6c2012-06-06 18:30:57 -07002167 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002168 USB_IDCHG_MIN - USB_IB_UNCFG);
2169 }
2170 } else if (!test_bit(B_CONN, &dev->inputs) &&
Steve Mucklef132c6c2012-06-06 18:30:57 -07002171 !dev->phy.otg->host->b_hnp_enable) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002172 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 Mucklef132c6c2012-06-06 18:30:57 -07002178 dev->phy.state = OTG_STATE_A_WAIT_BCON;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002179 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 Mucklef132c6c2012-06-06 18:30:57 -07002183 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002184 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002187 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 USB_IDCHG_MIN - USB_IB_UNCFG);
2189 } else if (!test_bit(ID, &dev->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002190 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002191 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 Mucklef132c6c2012-06-06 18:30:57 -07002202 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002203 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002204 msm_otg_start_peripheral(dev->phy.otg, 0);
2205 dev->phy.otg->gadget->is_a_peripheral = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 /* HCD was suspended before. Stop it now */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002207 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002208
2209 /* Reset both phy and link */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002210 otg_reset(&dev->phy, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211 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 Mucklef132c6c2012-06-06 18:30:57 -07002214 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002215 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002220 dev->phy.state = OTG_STATE_A_VBUS_ERR;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002221 spin_unlock_irqrestore(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002222 msm_otg_start_peripheral(dev->phy.otg, 0);
2223 dev->phy.otg->gadget->is_a_peripheral = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002224 /* HCD was suspended before. Stop it now */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002225 msm_otg_start_host(dev->phy.otg, REQUEST_STOP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002226 } else if (test_bit(A_BIDL_ADIS, &dev->tmouts)) {
2227 pr_debug("a_bidl_adis_tmout\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002228 msm_otg_start_peripheral(dev->phy.otg, 0);
2229 dev->phy.otg->gadget->is_a_peripheral = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002230
2231 spin_lock_irqsave(&dev->lock, flags);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002232 dev->phy.state = OTG_STATE_A_WAIT_BCON;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002233 spin_unlock_irqrestore(&dev->lock, flags);
2234 set_bit(A_BUS_REQ, &dev->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002235 msm_otg_start_host(dev->phy.otg, REQUEST_HNP_RESUME);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002236 if (TA_WAIT_BCON > 0)
2237 msm_otg_start_timer(dev, TA_WAIT_BCON,
2238 A_WAIT_BCON);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002239 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002240 } 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 Mucklef132c6c2012-06-06 18:30:57 -07002243 msm_otg_set_power(&dev->phy,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002244 USB_IDCHG_MIN - USB_IB_UNCFG);
2245 } else if (!test_bit(ID, &dev->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002246 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002247 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 Mucklef132c6c2012-06-06 18:30:57 -07002254 dev->phy.state = OTG_STATE_A_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002255 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 Mucklef132c6c2012-06-06 18:30:57 -07002265 dev->phy.state = OTG_STATE_A_WAIT_VFALL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002266 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 Mucklef132c6c2012-06-06 18:30:57 -07002270 msm_otg_set_power(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002271 }
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
2302static 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 Mucklef132c6c2012-06-06 18:30:57 -07002318 msm_otg_set_suspend(&dev->phy, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002319
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);
2345out:
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
2356static ssize_t
2357set_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 Mucklef132c6c2012-06-06 18:30:57 -07002366 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002367 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}
2383static DEVICE_ATTR(pwr_down, S_IRUGO | S_IWUSR, NULL, set_pwr_down);
2384
2385static ssize_t
2386set_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 Mucklef132c6c2012-06-06 18:30:57 -07002394 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002395 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}
2406static DEVICE_ATTR(srp_req, S_IRUGO | S_IWUSR, NULL, set_srp_req);
2407
2408static ssize_t
2409set_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 Mucklef132c6c2012-06-06 18:30:57 -07002417 state = dev->phy.state;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418 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}
2428static DEVICE_ATTR(clr_err, S_IRUGO | S_IWUSR, NULL, set_clr_err);
2429
2430static 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
2437static struct attribute_group msm_otg_attr_grp = {
2438 .attrs = msm_otg_attrs,
2439};
2440#endif
2441
2442#ifdef CONFIG_DEBUG_FS
2443static int otg_open(struct inode *inode, struct file *file)
2444{
2445 file->private_data = inode->i_private;
2446 return 0;
2447}
2448static 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 Kondetif0f95d82011-09-23 11:38:57 +05302458 if (!memcmp(buf, "none", 4)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459 clear_bit(B_SESS_VLD, &dev->inputs);
2460 set_bit(ID, &dev->inputs);
2461 work = 1;
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302462 } else if (!memcmp(buf, "peripheral", 10)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002463 set_bit(B_SESS_VLD, &dev->inputs);
2464 set_bit(ID, &dev->inputs);
2465 work = 1;
Pavankumar Kondetif0f95d82011-09-23 11:38:57 +05302466 } else if (!memcmp(buf, "host", 4)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 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}
2484const struct file_operations otgfs_fops = {
2485 .open = otg_open,
2486 .write = otg_mode_write,
2487};
2488
2489#define OTG_INFO_SIZE 512
2490static 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002509 "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 Gautam5143b252012-01-05 19:25:23 -08002515 "PHY Powercollapse: 0x%x\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -07002516 state_string(dev->phy.state),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002522 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 Gautam5143b252012-01-05 19:25:23 -08002528 dev->pdata->phy_can_powercollapse);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529
2530 ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp);
2531
2532 kfree(buf);
2533
2534 return ret;
2535}
2536
2537const struct file_operations otgfs_info_fops = {
2538 .open = otg_open,
2539 .read = otg_info_read,
2540};
2541
2542struct dentry *otg_debug_root;
2543struct dentry *otg_debug_mode;
2544struct dentry *otg_debug_info;
2545#endif
2546
2547static 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
2568free_mode:
2569 debugfs_remove(otg_debug_mode);
2570 otg_debug_mode = NULL;
2571
2572free_root:
2573 debugfs_remove(otg_debug_root);
2574 otg_debug_root = NULL;
2575 return -ENOENT;
2576#endif
2577 return 0;
2578}
2579
2580static 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 Mucklef132c6c2012-06-06 18:30:57 -07002589struct usb_phy_io_ops msm_otg_io_ops = {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 .read = usb_ulpi_read,
2591 .write = usb_ulpi_write,
2592};
2593
2594static 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 Mucklef132c6c2012-06-06 18:30:57 -07002604 dev->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
2605 if (!dev->phy.otg) {
2606 kfree(dev);
2607 return -ENOMEM;
2608 }
2609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002610 the_msm_otg = dev;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002611 dev->phy.dev = &pdev->dev;
2612 dev->phy.otg->phy = &dev->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002613 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002629 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 Gautam5143b252012-01-05 19:25:23 -08002639 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002643 goto rpc_fail;
2644 }
Manu Gautam5143b252012-01-05 19:25:23 -08002645 clk_set_rate(dev->alt_core_clk, 60000000);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002646
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 Gautam5143b252012-01-05 19:25:23 -08002651 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002656 }
Manu Gautam5143b252012-01-05 19:25:23 -08002657 /* 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 Gautam28b1bac2012-01-30 16:43:06 +05302663 clk_prepare_enable(dev->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002664
2665 if (!dev->pdata->pclk_is_hw_gated) {
Manu Gautam5143b252012-01-05 19:25:23 -08002666 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002671 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05302672 clk_prepare_enable(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002673 }
2674
2675 if (!dev->pdata->phy_reset) {
Manu Gautam5143b252012-01-05 19:25:23 -08002676 dev->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002677 if (IS_ERR(dev->phy_reset_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002678 pr_err("%s: failed to get phy_clk\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002679 ret = PTR_ERR(dev->phy_reset_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002680 goto put_iface_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002681 }
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 jonnalaae745e92011-11-14 18:34:31 +05302755 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002759 __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 jonnalaae745e92011-11-14 18:34:31 +05302773 goto free_phy_id_setup;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002774 }
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 Mucklef132c6c2012-06-06 18:30:57 -07002810 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 Huntsman3f2bc4d2011-08-16 17:27:22 -07002814#ifdef CONFIG_USB_EHCI_MSM_72K
Steve Mucklef132c6c2012-06-06 18:30:57 -07002815 dev->phy.otg->set_host = msm_otg_set_host;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002816#endif
Steve Mucklef132c6c2012-06-06 18:30:57 -07002817 dev->phy.otg->start_hnp = msm_otg_start_hnp;
2818 dev->phy.otg->send_event = msm_otg_send_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002819 dev->set_clk = msm_otg_set_clk;
2820 dev->reset = otg_reset;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002821 dev->phy.io_ops = &msm_otg_io_ops;
2822 if (usb_set_transceiver(&dev->phy)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002823 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
2868chg_deinit:
2869 if (dev->pdata->chg_init)
2870 dev->pdata->chg_init(0);
2871free_otg_irq:
2872 free_irq(dev->irq, dev);
2873free_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);
2878free_ldo_init:
2879 if (dev->pdata->ldo_init)
2880 dev->pdata->ldo_init(0);
2881free_config_vddcx:
2882 if (dev->pdata->init_vddcx)
2883 dev->pdata->init_vddcx(0);
Anji jonnalaae745e92011-11-14 18:34:31 +05302884free_phy_id_setup:
2885 if (dev->pdata->phy_id_setup_init)
2886 dev->pdata->phy_id_setup_init(0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002887free_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);
2890free_gpio:
2891 if (dev->pdata->init_gpio)
2892 dev->pdata->init_gpio(0);
2893free_wq:
2894 destroy_workqueue(dev->wq);
2895free_wlock:
2896 wake_lock_destroy(&dev->wlock);
2897free_xo_handle:
2898 msm_xo_put(dev->xo_handle);
2899free_regs:
2900 iounmap(dev->regs);
2901put_phy_clk:
2902 if (dev->phy_reset_clk)
2903 clk_put(dev->phy_reset_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002904put_iface_clk:
2905 if (dev->iface_clk) {
Manu Gautam28b1bac2012-01-30 16:43:06 +05302906 clk_disable_unprepare(dev->iface_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002907 clk_put(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002908 }
Manu Gautam5143b252012-01-05 19:25:23 -08002909put_core_clk:
Manu Gautam28b1bac2012-01-30 16:43:06 +05302910 clk_disable_unprepare(dev->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002911 clk_put(dev->core_clk);
2912put_alt_core_clk:
2913 clk_put(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002914rpc_fail:
2915 if (dev->pdata->rpc_connect)
2916 dev->pdata->rpc_connect(0);
2917free_dev:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002918 kfree(dev->phy.otg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002919 kfree(dev);
2920 return ret;
2921}
2922
2923static 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 jonnalaae745e92011-11-14 18:34:31 +05302948 if (dev->pdata->phy_id_setup_init)
2949 dev->pdata->phy_id_setup_init(0);
2950
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002951 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 Gautam28b1bac2012-01-30 16:43:06 +05302961 clk_disable_unprepare(dev->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002962 clk_put(dev->core_clk);
2963 if (dev->iface_clk) {
Manu Gautam28b1bac2012-01-30 16:43:06 +05302964 clk_disable_unprepare(dev->iface_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002965 clk_put(dev->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002966 }
Manu Gautam5143b252012-01-05 19:25:23 -08002967 if (dev->alt_core_clk)
2968 clk_put(dev->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002969 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 Kondetif0f95d82011-09-23 11:38:57 +05302974 pm_qos_remove_request(&dev->pdata->pm_qos_req_dma);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002975
2976 pm_runtime_put(&pdev->dev);
2977 pm_runtime_disable(&pdev->dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002978 kfree(dev->phy.otg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002979 kfree(dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002980 return 0;
2981}
2982
2983static 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
2992static 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
3001static int msm_otg_runtime_idle(struct device *dev)
3002{
3003 dev_dbg(dev, "pm_runtime: idling...\n");
3004 return 0;
3005}
3006
3007static 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
3013static 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
3022static int __init msm_otg_init(void)
3023{
3024 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3025}
3026
3027static void __exit msm_otg_exit(void)
3028{
3029 platform_driver_unregister(&msm_otg_driver);
3030}
3031
3032module_init(msm_otg_init);
3033module_exit(msm_otg_exit);
3034
3035MODULE_LICENSE("GPL v2");
3036MODULE_DESCRIPTION("MSM usb transceiver driver");
3037MODULE_VERSION("1.00");