blob: 56559bfb95f6d4201715426e331d10527fa4ad5c [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
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 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
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/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053041
42#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080043#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#define MSM_USB_BASE (motg->regs)
46#define DRIVER_NAME "msm_otg"
47
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053048#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053049#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053050
51#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
52#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
53#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
54#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
55
56#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
57#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
58#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
59#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
60
Vamsi Krishna132b2762011-11-11 16:09:20 -080061#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053062#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
63
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053064static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053066static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053068/* Prevent idle power collapse(pc) while operating in peripheral mode */
69static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
70{
71 struct msm_otg_platform_data *pdata = dev->pdata;
72 u32 swfi_latency = 0;
73
74 if (!pdata || !pdata->swfi_latency)
75 return;
76
77 swfi_latency = pdata->swfi_latency + 1;
78
79 if (vote)
80 pm_qos_update_request(&dev->pm_qos_req_dma,
81 swfi_latency);
82 else
83 pm_qos_update_request(&dev->pm_qos_req_dma,
84 PM_QOS_DEFAULT_VALUE);
85}
86
Anji jonnala11aa5c42011-05-04 10:19:48 +053087static struct regulator *hsusb_3p3;
88static struct regulator *hsusb_1p8;
89static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053090static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053091
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053092static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053093static inline bool aca_enabled(void)
94{
95#ifdef CONFIG_USB_MSM_ACA
96 return true;
97#else
98 return debug_aca_enabled;
99#endif
100}
101
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
103{
104 int ret = 0;
105
106 if (init) {
107 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
108 if (IS_ERR(hsusb_vddcx)) {
109 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
110 return PTR_ERR(hsusb_vddcx);
111 }
112
113 ret = regulator_set_voltage(hsusb_vddcx,
114 USB_PHY_VDD_DIG_VOL_MIN,
115 USB_PHY_VDD_DIG_VOL_MAX);
116 if (ret) {
117 dev_err(motg->otg.dev, "unable to set the voltage "
118 "for hsusb vddcx\n");
119 regulator_put(hsusb_vddcx);
120 return ret;
121 }
122
123 ret = regulator_enable(hsusb_vddcx);
124 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 regulator_set_voltage(hsusb_vddcx, 0,
126 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
129 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530136 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 return ret;
138 }
139
140 ret = regulator_set_voltage(hsusb_vddcx, 0,
141 USB_PHY_VDD_DIG_VOL_MIN);
142 if (ret) {
143 dev_err(motg->otg.dev, "unable to set the voltage"
144 "for hsusb vddcx\n");
145 return ret;
146 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530147
148 regulator_put(hsusb_vddcx);
149 }
150
151 return ret;
152}
153
154static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
155{
156 int rc = 0;
157
158 if (init) {
159 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
160 if (IS_ERR(hsusb_3p3)) {
161 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
162 return PTR_ERR(hsusb_3p3);
163 }
164
165 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
166 USB_PHY_3P3_VOL_MAX);
167 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 dev_err(motg->otg.dev, "unable to set voltage level for"
169 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530170 goto put_3p3;
171 }
172 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
173 if (IS_ERR(hsusb_1p8)) {
174 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
175 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 }
178 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
179 USB_PHY_1P8_VOL_MAX);
180 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 dev_err(motg->otg.dev, "unable to set voltage level for"
182 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530183 goto put_1p8;
184 }
185
186 return 0;
187 }
188
Anji jonnala11aa5c42011-05-04 10:19:48 +0530189put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192put_3p3_lpm:
193 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530194put_3p3:
195 regulator_put(hsusb_3p3);
196 return rc;
197}
198
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530199#ifdef CONFIG_PM_SLEEP
200#define USB_PHY_SUSP_DIG_VOL 500000
201static int msm_hsusb_config_vddcx(int high)
202{
203 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
204 int min_vol;
205 int ret;
206
207 if (high)
208 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
209 else
210 min_vol = USB_PHY_SUSP_DIG_VOL;
211
212 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
213 if (ret) {
214 pr_err("%s: unable to set the voltage for regulator "
215 "HSUSB_VDDCX\n", __func__);
216 return ret;
217 }
218
219 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
220
221 return ret;
222}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700223#else
224static int msm_hsusb_config_vddcx(int high)
225{
226 return 0;
227}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530228#endif
229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530231{
232 int ret = 0;
233
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530234 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530235 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
236 return -ENODEV;
237 }
238
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530239 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530240 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
241 return -ENODEV;
242 }
243
244 if (on) {
245 ret = regulator_set_optimum_mode(hsusb_1p8,
246 USB_PHY_1P8_HPM_LOAD);
247 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530249 "HSUSB_1p8\n", __func__);
250 return ret;
251 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252
253 ret = regulator_enable(hsusb_1p8);
254 if (ret) {
255 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
256 __func__);
257 regulator_set_optimum_mode(hsusb_1p8, 0);
258 return ret;
259 }
260
Anji jonnala11aa5c42011-05-04 10:19:48 +0530261 ret = regulator_set_optimum_mode(hsusb_3p3,
262 USB_PHY_3P3_HPM_LOAD);
263 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 regulator_set_optimum_mode(hsusb_1p8, 0);
267 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530268 return ret;
269 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270
271 ret = regulator_enable(hsusb_3p3);
272 if (ret) {
273 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
274 __func__);
275 regulator_set_optimum_mode(hsusb_3p3, 0);
276 regulator_set_optimum_mode(hsusb_1p8, 0);
277 regulator_disable(hsusb_1p8);
278 return ret;
279 }
280
Anji jonnala11aa5c42011-05-04 10:19:48 +0530281 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 ret = regulator_disable(hsusb_1p8);
283 if (ret) {
284 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
285 __func__);
286 return ret;
287 }
288
289 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530290 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530292 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
294 ret = regulator_disable(hsusb_3p3);
295 if (ret) {
296 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
297 __func__);
298 return ret;
299 }
300 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530301 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530303 "HSUSB_3p3\n", __func__);
304 }
305
306 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
307 return ret < 0 ? ret : 0;
308}
309
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530310static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
311{
312 static struct regulator *mhl_analog_switch;
313 struct msm_otg_platform_data *pdata = motg->pdata;
314
315 if (!pdata->mhl_enable)
316 return;
317
318 if (on) {
319 mhl_analog_switch = regulator_get(motg->otg.dev,
320 "mhl_ext_3p3v");
321 if (IS_ERR(mhl_analog_switch)) {
322 pr_err("Unable to get mhl_analog_switch\n");
323 return;
324 }
325
326 if (regulator_enable(mhl_analog_switch)) {
327 pr_err("unable to enable mhl_analog_switch\n");
328 goto put_analog_switch;
329 }
330 return;
331 }
332
333 regulator_disable(mhl_analog_switch);
334put_analog_switch:
335 regulator_put(mhl_analog_switch);
336}
337
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530338static int ulpi_read(struct otg_transceiver *otg, u32 reg)
339{
340 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
341 int cnt = 0;
342
343 /* initiate read operation */
344 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
345 USB_ULPI_VIEWPORT);
346
347 /* wait for completion */
348 while (cnt < ULPI_IO_TIMEOUT_USEC) {
349 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
350 break;
351 udelay(1);
352 cnt++;
353 }
354
355 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
356 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
357 readl(USB_ULPI_VIEWPORT));
358 return -ETIMEDOUT;
359 }
360 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
361}
362
363static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
364{
365 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
366 int cnt = 0;
367
368 /* initiate write operation */
369 writel(ULPI_RUN | ULPI_WRITE |
370 ULPI_ADDR(reg) | ULPI_DATA(val),
371 USB_ULPI_VIEWPORT);
372
373 /* wait for completion */
374 while (cnt < ULPI_IO_TIMEOUT_USEC) {
375 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
376 break;
377 udelay(1);
378 cnt++;
379 }
380
381 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
382 dev_err(otg->dev, "ulpi_write: timeout\n");
383 return -ETIMEDOUT;
384 }
385 return 0;
386}
387
388static struct otg_io_access_ops msm_otg_io_ops = {
389 .read = ulpi_read,
390 .write = ulpi_write,
391};
392
393static void ulpi_init(struct msm_otg *motg)
394{
395 struct msm_otg_platform_data *pdata = motg->pdata;
396 int *seq = pdata->phy_init_seq;
397
398 if (!seq)
399 return;
400
401 while (seq[0] >= 0) {
402 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
403 seq[0], seq[1]);
404 ulpi_write(&motg->otg, seq[0], seq[1]);
405 seq += 2;
406 }
407}
408
409static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
410{
411 int ret;
412
413 if (assert) {
414 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
415 if (ret)
416 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
417 } else {
418 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
419 if (ret)
420 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
421 }
422 return ret;
423}
424
425static int msm_otg_phy_clk_reset(struct msm_otg *motg)
426{
427 int ret;
428
Amit Blay02eff132011-09-21 16:46:24 +0300429 if (IS_ERR(motg->phy_reset_clk))
430 return 0;
431
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
433 if (ret) {
434 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
435 return ret;
436 }
437 usleep_range(10000, 12000);
438 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
439 if (ret)
440 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
441 return ret;
442}
443
444static int msm_otg_phy_reset(struct msm_otg *motg)
445{
446 u32 val;
447 int ret;
448 int retries;
449
450 ret = msm_otg_link_clk_reset(motg, 1);
451 if (ret)
452 return ret;
453 ret = msm_otg_phy_clk_reset(motg);
454 if (ret)
455 return ret;
456 ret = msm_otg_link_clk_reset(motg, 0);
457 if (ret)
458 return ret;
459
460 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
461 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
462
463 for (retries = 3; retries > 0; retries--) {
464 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
465 ULPI_CLR(ULPI_FUNC_CTRL));
466 if (!ret)
467 break;
468 ret = msm_otg_phy_clk_reset(motg);
469 if (ret)
470 return ret;
471 }
472 if (!retries)
473 return -ETIMEDOUT;
474
475 /* This reset calibrates the phy, if the above write succeeded */
476 ret = msm_otg_phy_clk_reset(motg);
477 if (ret)
478 return ret;
479
480 for (retries = 3; retries > 0; retries--) {
481 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
482 if (ret != -ETIMEDOUT)
483 break;
484 ret = msm_otg_phy_clk_reset(motg);
485 if (ret)
486 return ret;
487 }
488 if (!retries)
489 return -ETIMEDOUT;
490
491 dev_info(motg->otg.dev, "phy_reset: success\n");
492 return 0;
493}
494
495#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530496static int msm_otg_link_reset(struct msm_otg *motg)
497{
498 int cnt = 0;
499
500 writel_relaxed(USBCMD_RESET, USB_USBCMD);
501 while (cnt < LINK_RESET_TIMEOUT_USEC) {
502 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
503 break;
504 udelay(1);
505 cnt++;
506 }
507 if (cnt >= LINK_RESET_TIMEOUT_USEC)
508 return -ETIMEDOUT;
509
510 /* select ULPI phy */
511 writel_relaxed(0x80000000, USB_PORTSC);
512 writel_relaxed(0x0, USB_AHBBURST);
513 writel_relaxed(0x00, USB_AHBMODE);
514
515 return 0;
516}
517
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530518static int msm_otg_reset(struct otg_transceiver *otg)
519{
520 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
521 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530522 int ret;
523 u32 val = 0;
524 u32 ulpi_val = 0;
525
Ofir Cohen4da266f2012-01-03 10:19:29 +0200526 /*
527 * USB PHY and Link reset also reset the USB BAM.
528 * Thus perform reset operation only once to avoid
529 * USB BAM reset on other cases e.g. USB cable disconnections.
530 */
531 if (pdata->disable_reset_on_disconnect) {
532 if (motg->reset_counter)
533 return 0;
534 else
535 motg->reset_counter++;
536 }
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530539 ret = msm_otg_phy_reset(motg);
540 if (ret) {
541 dev_err(otg->dev, "phy_reset failed\n");
542 return ret;
543 }
544
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530545 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 ret = msm_otg_link_reset(motg);
547 if (ret) {
548 dev_err(otg->dev, "link reset failed\n");
549 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530550 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530552
553 ulpi_init(motg);
554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 /* Ensure that RESET operation is completed before turning off clock */
556 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 clk_disable(motg->clk);
559
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530560 if (pdata->otg_control == OTG_PHY_CONTROL) {
561 val = readl_relaxed(USB_OTGSC);
562 if (pdata->mode == USB_OTG) {
563 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
564 val |= OTGSC_IDIE | OTGSC_BSVIE;
565 } else if (pdata->mode == USB_PERIPHERAL) {
566 ulpi_val = ULPI_INT_SESS_VALID;
567 val |= OTGSC_BSVIE;
568 }
569 writel_relaxed(val, USB_OTGSC);
570 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530574 return 0;
575}
576
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530577static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
578{
579 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
580
581 /*
582 * Allow bus suspend only for host mode. Device mode bus suspend
583 * is not implemented yet.
584 */
585 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530586 /*
587 * ID_GND --> ID_A transition can not be detected in LPM.
588 * Disallow host bus suspend when ACA is enabled.
589 */
590 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530591 pm_runtime_put(otg->dev);
592 else
593 pm_runtime_resume(otg->dev);
594 }
595
596 return 0;
597}
598
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530599#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530600#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
601
602#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530603static int msm_otg_suspend(struct msm_otg *motg)
604{
605 struct otg_transceiver *otg = &motg->otg;
606 struct usb_bus *bus = otg->host;
607 struct msm_otg_platform_data *pdata = motg->pdata;
608 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530609 bool host_bus_suspend;
610 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800611 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530612 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530613
614 if (atomic_read(&motg->in_lpm))
615 return 0;
616
617 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530618 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530620 * Chipidea 45-nm PHY suspend sequence:
621 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530622 * Interrupt Latch Register auto-clear feature is not present
623 * in all PHY versions. Latch register is clear on read type.
624 * Clear latch register to avoid spurious wakeup from
625 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530626 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530627 * PHY comparators are disabled when PHY enters into low power
628 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
629 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
630 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530631 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530632 * PLL is not turned off when PHY enters into low power mode (LPM).
633 * Disable PLL for maximum power savings.
634 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530635
636 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
637 ulpi_read(otg, 0x14);
638 if (pdata->otg_control == OTG_PHY_CONTROL)
639 ulpi_write(otg, 0x01, 0x30);
640 ulpi_write(otg, 0x08, 0x09);
641 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530642
643 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 * Turn off the OTG comparators, if depends on PMIC for
645 * VBUS and ID notifications.
646 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530647 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 ulpi_write(otg, OTG_COMP_DISABLE,
649 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
650 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
651 }
652
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530653 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530654 * PHY may take some time or even fail to enter into low power
655 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
656 * in failure case.
657 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530658 portsc = readl_relaxed(USB_PORTSC);
659 if (!(portsc & PORTSC_PHCD)) {
660 writel_relaxed(portsc | PORTSC_PHCD,
661 USB_PORTSC);
662 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
663 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
664 break;
665 udelay(1);
666 cnt++;
667 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530668 }
669
670 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
671 dev_err(otg->dev, "Unable to suspend PHY\n");
672 msm_otg_reset(otg);
673 enable_irq(motg->irq);
674 return -ETIMEDOUT;
675 }
676
677 /*
678 * PHY has capability to generate interrupt asynchronously in low
679 * power mode (LPM). This interrupt is level triggered. So USB IRQ
680 * line must be disabled till async interrupt enable bit is cleared
681 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
682 * block data communication from PHY.
683 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530684 cmd_val = readl_relaxed(USB_USBCMD);
685 if (host_bus_suspend)
686 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
687 else
688 cmd_val |= ULPI_STP_CTRL;
689 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530690
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530691 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200692 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
693 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
694 /* Enable PHY HV interrupts to wake MPM/Link */
695 phy_ctrl_val |=
696 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
697
698 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 motg->lpm_flags |= PHY_RETENTIONED;
700 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 /* Ensure that above operation is completed before turning off clocks */
703 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530704 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800705 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530706
Anji jonnala7da3f262011-12-02 17:22:14 -0800707 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
708 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
709 if (ret)
710 dev_err(otg->dev, "%s failed to devote for "
711 "TCXO D0 buffer%d\n", __func__, ret);
712
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530713 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 msm_hsusb_ldo_enable(motg, 0);
715 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530716 }
717
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530718 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530720 msm_hsusb_mhl_switch_enable(motg, 0);
721 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722
723 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530724 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 if (motg->pdata->pmic_id_irq)
726 enable_irq_wake(motg->pdata->pmic_id_irq);
727 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530728 if (bus)
729 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
730
731 atomic_set(&motg->in_lpm, 1);
732 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530734
735 dev_info(otg->dev, "USB in low power mode\n");
736
737 return 0;
738}
739
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530740static int msm_otg_resume(struct msm_otg *motg)
741{
742 struct otg_transceiver *otg = &motg->otg;
743 struct usb_bus *bus = otg->host;
744 int cnt = 0;
745 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200746 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800747 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530748
749 if (!atomic_read(&motg->in_lpm))
750 return 0;
751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800753
754 /* Vote for TCXO when waking up the phy */
755 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
756 if (ret)
757 dev_err(otg->dev, "%s failed to vote for "
758 "TCXO D0 buffer%d\n", __func__, ret);
759
Manu Gautam5143b252012-01-05 19:25:23 -0800760 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200761
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
765 msm_hsusb_ldo_enable(motg, 1);
766 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
767 }
768
769 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530770 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530771 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200772 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
773 phy_ctrl_val |= PHY_RETEN;
774 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
775 /* Disable PHY HV interrupts */
776 phy_ctrl_val &=
777 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
778 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530780 }
781
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782 temp = readl(USB_USBCMD);
783 temp &= ~ASYNC_INTR_CTRL;
784 temp &= ~ULPI_STP_CTRL;
785 writel(temp, USB_USBCMD);
786
787 /*
788 * PHY comes out of low power mode (LPM) in case of wakeup
789 * from asynchronous interrupt.
790 */
791 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
792 goto skip_phy_resume;
793
794 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
795 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
796 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
797 break;
798 udelay(1);
799 cnt++;
800 }
801
802 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
803 /*
804 * This is a fatal error. Reset the link and
805 * PHY. USB state can not be restored. Re-insertion
806 * of USB cable is the only way to get USB working.
807 */
808 dev_err(otg->dev, "Unable to resume USB."
809 "Re-plugin the cable\n");
810 msm_otg_reset(otg);
811 }
812
813skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 /* Turn on the OTG comparators on resume */
815 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
816 ulpi_write(otg, OTG_COMP_DISABLE,
817 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
818 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
819 }
820 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530821 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 if (motg->pdata->pmic_id_irq)
823 disable_irq_wake(motg->pdata->pmic_id_irq);
824 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530825 if (bus)
826 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
827
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530828 atomic_set(&motg->in_lpm, 0);
829
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530830 if (motg->async_int) {
831 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530832 enable_irq(motg->irq);
833 }
834
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530835 dev_info(otg->dev, "USB exited from low power mode\n");
836
837 return 0;
838}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530839#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530840
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530841static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
842{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530843 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
844 motg->chg_type == USB_ACA_A_CHARGER ||
845 motg->chg_type == USB_ACA_B_CHARGER ||
846 motg->chg_type == USB_ACA_C_CHARGER) &&
847 mA > IDEV_ACA_CHG_LIMIT)
848 mA = IDEV_ACA_CHG_LIMIT;
849
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530850 if (motg->cur_power == mA)
851 return;
852
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530853 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530855 motg->cur_power = mA;
856}
857
858static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
859{
860 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
861
862 /*
863 * Gadget driver uses set_power method to notify about the
864 * available current based on suspend/configured states.
865 *
866 * IDEV_CHG can be drawn irrespective of suspend/un-configured
867 * states when CDP/ACA is connected.
868 */
869 if (motg->chg_type == USB_SDP_CHARGER)
870 msm_otg_notify_charger(motg, mA);
871
872 return 0;
873}
874
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530875static void msm_otg_start_host(struct otg_transceiver *otg, int on)
876{
877 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
878 struct msm_otg_platform_data *pdata = motg->pdata;
879 struct usb_hcd *hcd;
880
881 if (!otg->host)
882 return;
883
884 hcd = bus_to_hcd(otg->host);
885
886 if (on) {
887 dev_dbg(otg->dev, "host on\n");
888
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530889 /*
890 * Some boards have a switch cotrolled by gpio
891 * to enable/disable internal HUB. Enable internal
892 * HUB before kicking the host.
893 */
894 if (pdata->setup_gpio)
895 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530896 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530897 } else {
898 dev_dbg(otg->dev, "host off\n");
899
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530900 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530901 /* HCD core reset all bits of PORTSC. select ULPI phy */
902 writel_relaxed(0x80000000, USB_PORTSC);
903
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530904 if (pdata->setup_gpio)
905 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530906 }
907}
908
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909static int msm_otg_usbdev_notify(struct notifier_block *self,
910 unsigned long action, void *priv)
911{
912 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530913 struct usb_device *udev = priv;
914
915 if (!aca_enabled())
916 goto out;
917
918 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
919 goto out;
920
921 if (udev->bus != motg->otg.host)
922 goto out;
923 /*
924 * Interested in devices connected directly to the root hub.
925 * ACA dock can supply IDEV_CHG irrespective devices connected
926 * on the accessory port.
927 */
928 if (!udev->parent || udev->parent->parent ||
929 motg->chg_type == USB_ACA_DOCK_CHARGER)
930 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931
932 switch (action) {
933 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530934 usb_disable_autosuspend(udev);
935 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 if (udev->actconfig)
938 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
939 else
940 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530941 break;
942 case USB_DEVICE_REMOVE:
943 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 break;
945 default:
946 break;
947 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530948 if (test_bit(ID_A, &motg->inputs))
949 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
950 motg->mA_port);
951out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 return NOTIFY_OK;
953}
954
Mayank Ranae3926882011-12-26 09:47:54 +0530955static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
956{
957 int ret;
958 static bool vbus_is_on;
959
960 if (vbus_is_on == on)
961 return;
962
963 if (motg->pdata->vbus_power) {
964 motg->pdata->vbus_power(on);
965 return;
966 }
967
968 if (!vbus_otg) {
969 pr_err("vbus_otg is NULL.");
970 return;
971 }
972
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -0800973 /*
974 * if entering host mode tell the charger to not draw any current
975 * from usb - if exiting host mode let the charger draw current
976 */
977 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +0530978 if (on) {
979 ret = regulator_enable(vbus_otg);
980 if (ret) {
981 pr_err("unable to enable vbus_otg\n");
982 return;
983 }
984 vbus_is_on = true;
985 } else {
986 ret = regulator_disable(vbus_otg);
987 if (ret) {
988 pr_err("unable to disable vbus_otg\n");
989 return;
990 }
991 vbus_is_on = false;
992 }
993}
994
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530995static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
996{
997 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
998 struct usb_hcd *hcd;
999
1000 /*
1001 * Fail host registration if this board can support
1002 * only peripheral configuration.
1003 */
1004 if (motg->pdata->mode == USB_PERIPHERAL) {
1005 dev_info(otg->dev, "Host mode is not supported\n");
1006 return -ENODEV;
1007 }
1008
Mayank Ranae3926882011-12-26 09:47:54 +05301009 if (!motg->pdata->vbus_power && host) {
1010 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1011 if (IS_ERR(vbus_otg)) {
1012 pr_err("Unable to get vbus_otg\n");
1013 return -ENODEV;
1014 }
1015 }
1016
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301017 if (!host) {
1018 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301019 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001020 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301021 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301022 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301023 otg->host = NULL;
1024 otg->state = OTG_STATE_UNDEFINED;
1025 schedule_work(&motg->sm_work);
1026 } else {
1027 otg->host = NULL;
1028 }
1029
Mayank Ranae3926882011-12-26 09:47:54 +05301030 if (vbus_otg)
1031 regulator_put(vbus_otg);
1032
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301033 return 0;
1034 }
1035
1036 hcd = bus_to_hcd(host);
1037 hcd->power_budget = motg->pdata->power_budget;
1038
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001039 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1040 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301041 otg->host = host;
1042 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1043
1044 /*
1045 * Kick the state machine work, if peripheral is not supported
1046 * or peripheral is already registered with us.
1047 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301048 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1049 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301050 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301051 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301052
1053 return 0;
1054}
1055
1056static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1057{
1058 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1059 struct msm_otg_platform_data *pdata = motg->pdata;
1060
1061 if (!otg->gadget)
1062 return;
1063
1064 if (on) {
1065 dev_dbg(otg->dev, "gadget on\n");
1066 /*
1067 * Some boards have a switch cotrolled by gpio
1068 * to enable/disable internal HUB. Disable internal
1069 * HUB before kicking the gadget.
1070 */
1071 if (pdata->setup_gpio)
1072 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301073 /*
1074 * vote for minimum dma_latency to prevent idle
1075 * power collapse(pc) while running in peripheral mode.
1076 */
1077 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301078 usb_gadget_vbus_connect(otg->gadget);
1079 } else {
1080 dev_dbg(otg->dev, "gadget off\n");
1081 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301082 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301083 if (pdata->setup_gpio)
1084 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1085 }
1086
1087}
1088
1089static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1090 struct usb_gadget *gadget)
1091{
1092 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1093
1094 /*
1095 * Fail peripheral registration if this board can support
1096 * only host configuration.
1097 */
1098 if (motg->pdata->mode == USB_HOST) {
1099 dev_info(otg->dev, "Peripheral mode is not supported\n");
1100 return -ENODEV;
1101 }
1102
1103 if (!gadget) {
1104 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301105 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301106 msm_otg_start_peripheral(otg, 0);
1107 otg->gadget = NULL;
1108 otg->state = OTG_STATE_UNDEFINED;
1109 schedule_work(&motg->sm_work);
1110 } else {
1111 otg->gadget = NULL;
1112 }
1113
1114 return 0;
1115 }
1116 otg->gadget = gadget;
1117 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1118
1119 /*
1120 * Kick the state machine work, if host is not supported
1121 * or host is already registered with us.
1122 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301123 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1124 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301125 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301126 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301127
1128 return 0;
1129}
1130
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001131static bool msm_chg_aca_detect(struct msm_otg *motg)
1132{
1133 struct otg_transceiver *otg = &motg->otg;
1134 u32 int_sts;
1135 bool ret = false;
1136
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301137 if (!aca_enabled())
1138 goto out;
1139
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001140 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1141 goto out;
1142
1143 int_sts = ulpi_read(otg, 0x87);
1144 switch (int_sts & 0x1C) {
1145 case 0x08:
1146 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1147 dev_dbg(otg->dev, "ID_A\n");
1148 motg->chg_type = USB_ACA_A_CHARGER;
1149 motg->chg_state = USB_CHG_STATE_DETECTED;
1150 clear_bit(ID_B, &motg->inputs);
1151 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301152 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001153 ret = true;
1154 }
1155 break;
1156 case 0x0C:
1157 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1158 dev_dbg(otg->dev, "ID_B\n");
1159 motg->chg_type = USB_ACA_B_CHARGER;
1160 motg->chg_state = USB_CHG_STATE_DETECTED;
1161 clear_bit(ID_A, &motg->inputs);
1162 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301163 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 ret = true;
1165 }
1166 break;
1167 case 0x10:
1168 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1169 dev_dbg(otg->dev, "ID_C\n");
1170 motg->chg_type = USB_ACA_C_CHARGER;
1171 motg->chg_state = USB_CHG_STATE_DETECTED;
1172 clear_bit(ID_A, &motg->inputs);
1173 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301174 set_bit(ID, &motg->inputs);
1175 ret = true;
1176 }
1177 break;
1178 case 0x04:
1179 if (test_and_clear_bit(ID, &motg->inputs)) {
1180 dev_dbg(otg->dev, "ID_GND\n");
1181 motg->chg_type = USB_INVALID_CHARGER;
1182 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1183 clear_bit(ID_A, &motg->inputs);
1184 clear_bit(ID_B, &motg->inputs);
1185 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001186 ret = true;
1187 }
1188 break;
1189 default:
1190 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1191 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301192 test_and_clear_bit(ID_C, &motg->inputs) |
1193 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301195 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 motg->chg_type = USB_INVALID_CHARGER;
1197 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1198 }
1199 }
1200out:
1201 return ret;
1202}
1203
1204static void msm_chg_enable_aca_det(struct msm_otg *motg)
1205{
1206 struct otg_transceiver *otg = &motg->otg;
1207
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301208 if (!aca_enabled())
1209 return;
1210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 switch (motg->pdata->phy_type) {
1212 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301213 /* Disable ID_GND in link and PHY */
1214 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1215 OTGSC_IDIE), USB_OTGSC);
1216 ulpi_write(otg, 0x01, 0x0C);
1217 ulpi_write(otg, 0x10, 0x0F);
1218 ulpi_write(otg, 0x10, 0x12);
1219 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301221 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 break;
1223 default:
1224 break;
1225 }
1226}
1227
1228static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1229{
1230 struct otg_transceiver *otg = &motg->otg;
1231
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 if (!aca_enabled())
1233 return;
1234
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 switch (motg->pdata->phy_type) {
1236 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301237 /* Enable ACA Detection interrupt (on any RID change) */
1238 ulpi_write(otg, 0x01, 0x94);
1239 break;
1240 default:
1241 break;
1242 }
1243}
1244
1245static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1246{
1247 struct otg_transceiver *otg = &motg->otg;
1248
1249 if (!aca_enabled())
1250 return;
1251
1252 switch (motg->pdata->phy_type) {
1253 case SNPS_28NM_INTEGRATED_PHY:
1254 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 break;
1256 default:
1257 break;
1258 }
1259}
1260
1261static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1262{
1263 struct otg_transceiver *otg = &motg->otg;
1264 bool ret = false;
1265
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301266 if (!aca_enabled())
1267 return ret;
1268
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 switch (motg->pdata->phy_type) {
1270 case SNPS_28NM_INTEGRATED_PHY:
1271 if (ulpi_read(otg, 0x91) & 1) {
1272 dev_dbg(otg->dev, "RID change\n");
1273 ulpi_write(otg, 0x01, 0x92);
1274 ret = msm_chg_aca_detect(motg);
1275 }
1276 default:
1277 break;
1278 }
1279 return ret;
1280}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301281
1282static void msm_otg_id_timer_func(unsigned long data)
1283{
1284 struct msm_otg *motg = (struct msm_otg *) data;
1285
1286 if (!aca_enabled())
1287 return;
1288
1289 if (atomic_read(&motg->in_lpm)) {
1290 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1291 return;
1292 }
1293
1294 if (msm_chg_check_aca_intr(motg)) {
1295 dev_dbg(motg->otg.dev, "timer: aca work\n");
1296 schedule_work(&motg->sm_work);
1297 }
1298
1299 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1300 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1301}
1302
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301303static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1304{
1305 struct otg_transceiver *otg = &motg->otg;
1306 u32 chg_det;
1307 bool ret = false;
1308
1309 switch (motg->pdata->phy_type) {
1310 case CI_45NM_INTEGRATED_PHY:
1311 chg_det = ulpi_read(otg, 0x34);
1312 ret = chg_det & (1 << 4);
1313 break;
1314 case SNPS_28NM_INTEGRATED_PHY:
1315 chg_det = ulpi_read(otg, 0x87);
1316 ret = chg_det & 1;
1317 break;
1318 default:
1319 break;
1320 }
1321 return ret;
1322}
1323
1324static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1325{
1326 struct otg_transceiver *otg = &motg->otg;
1327 u32 chg_det;
1328
1329 switch (motg->pdata->phy_type) {
1330 case CI_45NM_INTEGRATED_PHY:
1331 chg_det = ulpi_read(otg, 0x34);
1332 /* Turn off charger block */
1333 chg_det |= ~(1 << 1);
1334 ulpi_write(otg, chg_det, 0x34);
1335 udelay(20);
1336 /* control chg block via ULPI */
1337 chg_det &= ~(1 << 3);
1338 ulpi_write(otg, chg_det, 0x34);
1339 /* put it in host mode for enabling D- source */
1340 chg_det &= ~(1 << 2);
1341 ulpi_write(otg, chg_det, 0x34);
1342 /* Turn on chg detect block */
1343 chg_det &= ~(1 << 1);
1344 ulpi_write(otg, chg_det, 0x34);
1345 udelay(20);
1346 /* enable chg detection */
1347 chg_det &= ~(1 << 0);
1348 ulpi_write(otg, chg_det, 0x34);
1349 break;
1350 case SNPS_28NM_INTEGRATED_PHY:
1351 /*
1352 * Configure DM as current source, DP as current sink
1353 * and enable battery charging comparators.
1354 */
1355 ulpi_write(otg, 0x8, 0x85);
1356 ulpi_write(otg, 0x2, 0x85);
1357 ulpi_write(otg, 0x1, 0x85);
1358 break;
1359 default:
1360 break;
1361 }
1362}
1363
1364static bool msm_chg_check_primary_det(struct msm_otg *motg)
1365{
1366 struct otg_transceiver *otg = &motg->otg;
1367 u32 chg_det;
1368 bool ret = false;
1369
1370 switch (motg->pdata->phy_type) {
1371 case CI_45NM_INTEGRATED_PHY:
1372 chg_det = ulpi_read(otg, 0x34);
1373 ret = chg_det & (1 << 4);
1374 break;
1375 case SNPS_28NM_INTEGRATED_PHY:
1376 chg_det = ulpi_read(otg, 0x87);
1377 ret = chg_det & 1;
1378 break;
1379 default:
1380 break;
1381 }
1382 return ret;
1383}
1384
1385static void msm_chg_enable_primary_det(struct msm_otg *motg)
1386{
1387 struct otg_transceiver *otg = &motg->otg;
1388 u32 chg_det;
1389
1390 switch (motg->pdata->phy_type) {
1391 case CI_45NM_INTEGRATED_PHY:
1392 chg_det = ulpi_read(otg, 0x34);
1393 /* enable chg detection */
1394 chg_det &= ~(1 << 0);
1395 ulpi_write(otg, chg_det, 0x34);
1396 break;
1397 case SNPS_28NM_INTEGRATED_PHY:
1398 /*
1399 * Configure DP as current source, DM as current sink
1400 * and enable battery charging comparators.
1401 */
1402 ulpi_write(otg, 0x2, 0x85);
1403 ulpi_write(otg, 0x1, 0x85);
1404 break;
1405 default:
1406 break;
1407 }
1408}
1409
1410static bool msm_chg_check_dcd(struct msm_otg *motg)
1411{
1412 struct otg_transceiver *otg = &motg->otg;
1413 u32 line_state;
1414 bool ret = false;
1415
1416 switch (motg->pdata->phy_type) {
1417 case CI_45NM_INTEGRATED_PHY:
1418 line_state = ulpi_read(otg, 0x15);
1419 ret = !(line_state & 1);
1420 break;
1421 case SNPS_28NM_INTEGRATED_PHY:
1422 line_state = ulpi_read(otg, 0x87);
1423 ret = line_state & 2;
1424 break;
1425 default:
1426 break;
1427 }
1428 return ret;
1429}
1430
1431static void msm_chg_disable_dcd(struct msm_otg *motg)
1432{
1433 struct otg_transceiver *otg = &motg->otg;
1434 u32 chg_det;
1435
1436 switch (motg->pdata->phy_type) {
1437 case CI_45NM_INTEGRATED_PHY:
1438 chg_det = ulpi_read(otg, 0x34);
1439 chg_det &= ~(1 << 5);
1440 ulpi_write(otg, chg_det, 0x34);
1441 break;
1442 case SNPS_28NM_INTEGRATED_PHY:
1443 ulpi_write(otg, 0x10, 0x86);
1444 break;
1445 default:
1446 break;
1447 }
1448}
1449
1450static void msm_chg_enable_dcd(struct msm_otg *motg)
1451{
1452 struct otg_transceiver *otg = &motg->otg;
1453 u32 chg_det;
1454
1455 switch (motg->pdata->phy_type) {
1456 case CI_45NM_INTEGRATED_PHY:
1457 chg_det = ulpi_read(otg, 0x34);
1458 /* Turn on D+ current source */
1459 chg_det |= (1 << 5);
1460 ulpi_write(otg, chg_det, 0x34);
1461 break;
1462 case SNPS_28NM_INTEGRATED_PHY:
1463 /* Data contact detection enable */
1464 ulpi_write(otg, 0x10, 0x85);
1465 break;
1466 default:
1467 break;
1468 }
1469}
1470
1471static void msm_chg_block_on(struct msm_otg *motg)
1472{
1473 struct otg_transceiver *otg = &motg->otg;
1474 u32 func_ctrl, chg_det;
1475
1476 /* put the controller in non-driving mode */
1477 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1478 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1479 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1480 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1481
1482 switch (motg->pdata->phy_type) {
1483 case CI_45NM_INTEGRATED_PHY:
1484 chg_det = ulpi_read(otg, 0x34);
1485 /* control chg block via ULPI */
1486 chg_det &= ~(1 << 3);
1487 ulpi_write(otg, chg_det, 0x34);
1488 /* Turn on chg detect block */
1489 chg_det &= ~(1 << 1);
1490 ulpi_write(otg, chg_det, 0x34);
1491 udelay(20);
1492 break;
1493 case SNPS_28NM_INTEGRATED_PHY:
1494 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301495 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301496 /* Clear alt interrupt latch and enable bits */
1497 ulpi_write(otg, 0x1F, 0x92);
1498 ulpi_write(otg, 0x1F, 0x95);
1499 udelay(100);
1500 break;
1501 default:
1502 break;
1503 }
1504}
1505
1506static void msm_chg_block_off(struct msm_otg *motg)
1507{
1508 struct otg_transceiver *otg = &motg->otg;
1509 u32 func_ctrl, chg_det;
1510
1511 switch (motg->pdata->phy_type) {
1512 case CI_45NM_INTEGRATED_PHY:
1513 chg_det = ulpi_read(otg, 0x34);
1514 /* Turn off charger block */
1515 chg_det |= ~(1 << 1);
1516 ulpi_write(otg, chg_det, 0x34);
1517 break;
1518 case SNPS_28NM_INTEGRATED_PHY:
1519 /* Clear charger detecting control bits */
1520 ulpi_write(otg, 0x3F, 0x86);
1521 /* Clear alt interrupt latch and enable bits */
1522 ulpi_write(otg, 0x1F, 0x92);
1523 ulpi_write(otg, 0x1F, 0x95);
1524 break;
1525 default:
1526 break;
1527 }
1528
1529 /* put the controller in normal mode */
1530 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1531 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1532 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1533 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1534}
1535
Anji jonnalad270e2d2011-08-09 11:28:32 +05301536static const char *chg_to_string(enum usb_chg_type chg_type)
1537{
1538 switch (chg_type) {
1539 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1540 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1541 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1542 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1543 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1544 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1545 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1546 default: return "INVALID_CHARGER";
1547 }
1548}
1549
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301550#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1551#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1552#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1553#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1554static void msm_chg_detect_work(struct work_struct *w)
1555{
1556 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1557 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301559 unsigned long delay;
1560
1561 dev_dbg(otg->dev, "chg detection work\n");
1562 switch (motg->chg_state) {
1563 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301564 msm_chg_block_on(motg);
1565 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301567 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1568 motg->dcd_retries = 0;
1569 delay = MSM_CHG_DCD_POLL_TIME;
1570 break;
1571 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572 is_aca = msm_chg_aca_detect(motg);
1573 if (is_aca) {
1574 /*
1575 * ID_A can be ACA dock too. continue
1576 * primary detection after DCD.
1577 */
1578 if (test_bit(ID_A, &motg->inputs)) {
1579 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1580 } else {
1581 delay = 0;
1582 break;
1583 }
1584 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301585 is_dcd = msm_chg_check_dcd(motg);
1586 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1587 if (is_dcd || tmout) {
1588 msm_chg_disable_dcd(motg);
1589 msm_chg_enable_primary_det(motg);
1590 delay = MSM_CHG_PRIMARY_DET_TIME;
1591 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1592 } else {
1593 delay = MSM_CHG_DCD_POLL_TIME;
1594 }
1595 break;
1596 case USB_CHG_STATE_DCD_DONE:
1597 vout = msm_chg_check_primary_det(motg);
1598 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301599 if (test_bit(ID_A, &motg->inputs)) {
1600 motg->chg_type = USB_ACA_DOCK_CHARGER;
1601 motg->chg_state = USB_CHG_STATE_DETECTED;
1602 delay = 0;
1603 break;
1604 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301605 msm_chg_enable_secondary_det(motg);
1606 delay = MSM_CHG_SECONDARY_DET_TIME;
1607 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1608 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301609 if (test_bit(ID_A, &motg->inputs)) {
1610 motg->chg_type = USB_ACA_A_CHARGER;
1611 motg->chg_state = USB_CHG_STATE_DETECTED;
1612 delay = 0;
1613 break;
1614 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301615 motg->chg_type = USB_SDP_CHARGER;
1616 motg->chg_state = USB_CHG_STATE_DETECTED;
1617 delay = 0;
1618 }
1619 break;
1620 case USB_CHG_STATE_PRIMARY_DONE:
1621 vout = msm_chg_check_secondary_det(motg);
1622 if (vout)
1623 motg->chg_type = USB_DCP_CHARGER;
1624 else
1625 motg->chg_type = USB_CDP_CHARGER;
1626 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1627 /* fall through */
1628 case USB_CHG_STATE_SECONDARY_DONE:
1629 motg->chg_state = USB_CHG_STATE_DETECTED;
1630 case USB_CHG_STATE_DETECTED:
1631 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632 msm_chg_enable_aca_det(motg);
1633 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301634 dev_dbg(otg->dev, "chg_type = %s\n",
1635 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301636 schedule_work(&motg->sm_work);
1637 return;
1638 default:
1639 return;
1640 }
1641
1642 schedule_delayed_work(&motg->chg_work, delay);
1643}
1644
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301645/*
1646 * We support OTG, Peripheral only and Host only configurations. In case
1647 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1648 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1649 * enabled when switch is controlled by user and default mode is supplied
1650 * by board file, which can be changed by userspace later.
1651 */
1652static void msm_otg_init_sm(struct msm_otg *motg)
1653{
1654 struct msm_otg_platform_data *pdata = motg->pdata;
1655 u32 otgsc = readl(USB_OTGSC);
1656
1657 switch (pdata->mode) {
1658 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001659 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301660 if (pdata->default_mode == USB_HOST) {
1661 clear_bit(ID, &motg->inputs);
1662 } else if (pdata->default_mode == USB_PERIPHERAL) {
1663 set_bit(ID, &motg->inputs);
1664 set_bit(B_SESS_VLD, &motg->inputs);
1665 } else {
1666 set_bit(ID, &motg->inputs);
1667 clear_bit(B_SESS_VLD, &motg->inputs);
1668 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301669 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1670 if (otgsc & OTGSC_ID)
1671 set_bit(ID, &motg->inputs);
1672 else
1673 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 if (otgsc & OTGSC_BSV)
1675 set_bit(B_SESS_VLD, &motg->inputs);
1676 else
1677 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301678 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1679 if (irq_read_line(motg->pdata->pmic_id_irq))
1680 set_bit(ID, &motg->inputs);
1681 else
1682 clear_bit(ID, &motg->inputs);
1683
1684 /*
1685 * VBUS initial state is reported after PMIC
1686 * driver initialization. Wait for it.
1687 */
1688 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301689 }
1690 break;
1691 case USB_HOST:
1692 clear_bit(ID, &motg->inputs);
1693 break;
1694 case USB_PERIPHERAL:
1695 set_bit(ID, &motg->inputs);
1696 if (otgsc & OTGSC_BSV)
1697 set_bit(B_SESS_VLD, &motg->inputs);
1698 else
1699 clear_bit(B_SESS_VLD, &motg->inputs);
1700 break;
1701 default:
1702 break;
1703 }
1704}
1705
1706static void msm_otg_sm_work(struct work_struct *w)
1707{
1708 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1709 struct otg_transceiver *otg = &motg->otg;
1710
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301711 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301712 switch (otg->state) {
1713 case OTG_STATE_UNDEFINED:
1714 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1715 msm_otg_reset(otg);
1716 msm_otg_init_sm(motg);
1717 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301718 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1719 test_bit(ID, &motg->inputs)) {
1720 pm_runtime_put_noidle(otg->dev);
1721 pm_runtime_suspend(otg->dev);
1722 break;
1723 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301724 /* FALL THROUGH */
1725 case OTG_STATE_B_IDLE:
1726 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001727 if ((!test_bit(ID, &motg->inputs) ||
1728 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1730 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301731 IDEV_ACA_CHG_MAX);
1732 else if (test_bit(ID_A, &motg->inputs))
1733 msm_otg_notify_charger(motg,
1734 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301735 else
1736 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301737 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301738 /*
1739 * Link can not generate PHY_ALT interrupt
1740 * in host mode when no device is attached
1741 * to the port. It is also observed PHY_ALT
1742 * interrupt missing upon Micro-A cable disconnect.
1743 * Hence disable PHY_ALT interrupt and perform
1744 * polling to detect RID change.
1745 */
1746 msm_chg_enable_aca_det(motg);
1747 msm_chg_disable_aca_intr(motg);
1748 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301749 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301750 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1751 switch (motg->chg_state) {
1752 case USB_CHG_STATE_UNDEFINED:
1753 msm_chg_detect_work(&motg->chg_work.work);
1754 break;
1755 case USB_CHG_STATE_DETECTED:
1756 switch (motg->chg_type) {
1757 case USB_DCP_CHARGER:
1758 msm_otg_notify_charger(motg,
1759 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301760 pm_runtime_put_noidle(otg->dev);
1761 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301762 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301763 case USB_ACA_B_CHARGER:
1764 msm_otg_notify_charger(motg,
1765 IDEV_ACA_CHG_MAX);
1766 /*
1767 * (ID_B --> ID_C) PHY_ALT interrupt can
1768 * not be detected in LPM.
1769 */
1770 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301771 case USB_CDP_CHARGER:
1772 msm_otg_notify_charger(motg,
1773 IDEV_CHG_MAX);
1774 msm_otg_start_peripheral(otg, 1);
1775 otg->state = OTG_STATE_B_PERIPHERAL;
1776 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301777 case USB_ACA_C_CHARGER:
1778 msm_otg_notify_charger(motg,
1779 IDEV_ACA_CHG_MAX);
1780 msm_otg_start_peripheral(otg, 1);
1781 otg->state = OTG_STATE_B_PERIPHERAL;
1782 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301783 case USB_SDP_CHARGER:
1784 msm_otg_notify_charger(motg, IUNIT);
1785 msm_otg_start_peripheral(otg, 1);
1786 otg->state = OTG_STATE_B_PERIPHERAL;
1787 break;
1788 default:
1789 break;
1790 }
1791 break;
1792 default:
1793 break;
1794 }
1795 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301796 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301797 msm_otg_notify_charger(motg, 0);
1798 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1799 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301800 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301801 pm_runtime_put_noidle(otg->dev);
1802 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301803 }
1804 break;
1805 case OTG_STATE_B_PERIPHERAL:
1806 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1807 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 !test_bit(ID, &motg->inputs) ||
1809 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301810 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 schedule_work(w);
1813 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301814 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001815 }
1816 break;
1817 case OTG_STATE_A_HOST:
1818 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1819 if (test_bit(ID, &motg->inputs) &&
1820 !test_bit(ID_A, &motg->inputs)) {
1821 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301822 msm_hsusb_vbus_power(motg, 0);
1823 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301824 /*
1825 * Exit point of host mode.
1826 *
1827 * 1. Micro-A cable disconnect: Just schedule
1828 * the work. PHY is reset in B_IDLE and LPM
1829 * is allowed.
1830 * 2. ID_GND --> ID_B: No need to reset the PHY.
1831 * HCD core clears all PORTSC bits and initializes
1832 * the controller to host mode in remove_hcd.
1833 * Restore PORTSC transceiver select bits (ULPI)
1834 * and reset the controller to change MODE bits.
1835 * PHY_ALT interrupt can not occur in host mode.
1836 */
1837 del_timer_sync(&motg->id_timer);
1838 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1839 msm_otg_link_reset(motg);
1840 msm_chg_enable_aca_intr(motg);
1841 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301842 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301843 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301845 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001846 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301847 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001848 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301850 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301851 }
1852 break;
1853 default:
1854 break;
1855 }
1856}
1857
1858static irqreturn_t msm_otg_irq(int irq, void *data)
1859{
1860 struct msm_otg *motg = data;
1861 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001862 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301863
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301864 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301865 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301866 disable_irq_nosync(irq);
1867 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301868 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301869 return IRQ_HANDLED;
1870 }
1871
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872 usbsts = readl(USB_USBSTS);
1873 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301874 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301876 if (msm_chg_check_aca_intr(motg)) {
1877 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301879 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 return IRQ_HANDLED;
1881 }
1882
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301883 otgsc = readl(USB_OTGSC);
1884 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1885 return IRQ_NONE;
1886
1887 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301888 if (otgsc & OTGSC_ID) {
1889 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301890 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301891 } else {
1892 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301893 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301894 msm_chg_enable_aca_det(motg);
1895 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301897 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301898 if (otgsc & OTGSC_BSV) {
1899 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301900 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301901 } else {
1902 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301903 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301904 msm_chg_check_aca_intr(motg);
1905 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001906 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301907 }
1908
1909 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001910 return IRQ_HANDLED;
1911}
1912
1913static void msm_otg_set_vbus_state(int online)
1914{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301915 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001916 struct msm_otg *motg = the_msm_otg;
1917
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301918 if (online) {
1919 pr_debug("PMIC: BSV set\n");
1920 set_bit(B_SESS_VLD, &motg->inputs);
1921 } else {
1922 pr_debug("PMIC: BSV clear\n");
1923 clear_bit(B_SESS_VLD, &motg->inputs);
1924 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301926 if (!init) {
1927 init = true;
1928 complete(&pmic_vbus_init);
1929 pr_debug("PMIC: BSV init complete\n");
1930 return;
1931 }
1932
1933 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934}
1935
1936static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1937{
1938 struct msm_otg *motg = data;
1939
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301940 if (aca_id_turned_on)
1941 return IRQ_HANDLED;
1942
1943 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1944 pr_debug("PMIC: ID set\n");
1945 set_bit(ID, &motg->inputs);
1946 } else {
1947 pr_debug("PMIC: ID clear\n");
1948 clear_bit(ID, &motg->inputs);
1949 }
1950
1951 if (motg->otg.state != OTG_STATE_UNDEFINED)
1952 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301954 return IRQ_HANDLED;
1955}
1956
1957static int msm_otg_mode_show(struct seq_file *s, void *unused)
1958{
1959 struct msm_otg *motg = s->private;
1960 struct otg_transceiver *otg = &motg->otg;
1961
1962 switch (otg->state) {
1963 case OTG_STATE_A_HOST:
1964 seq_printf(s, "host\n");
1965 break;
1966 case OTG_STATE_B_PERIPHERAL:
1967 seq_printf(s, "peripheral\n");
1968 break;
1969 default:
1970 seq_printf(s, "none\n");
1971 break;
1972 }
1973
1974 return 0;
1975}
1976
1977static int msm_otg_mode_open(struct inode *inode, struct file *file)
1978{
1979 return single_open(file, msm_otg_mode_show, inode->i_private);
1980}
1981
1982static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1983 size_t count, loff_t *ppos)
1984{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301985 struct seq_file *s = file->private_data;
1986 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301987 char buf[16];
1988 struct otg_transceiver *otg = &motg->otg;
1989 int status = count;
1990 enum usb_mode_type req_mode;
1991
1992 memset(buf, 0x00, sizeof(buf));
1993
1994 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1995 status = -EFAULT;
1996 goto out;
1997 }
1998
1999 if (!strncmp(buf, "host", 4)) {
2000 req_mode = USB_HOST;
2001 } else if (!strncmp(buf, "peripheral", 10)) {
2002 req_mode = USB_PERIPHERAL;
2003 } else if (!strncmp(buf, "none", 4)) {
2004 req_mode = USB_NONE;
2005 } else {
2006 status = -EINVAL;
2007 goto out;
2008 }
2009
2010 switch (req_mode) {
2011 case USB_NONE:
2012 switch (otg->state) {
2013 case OTG_STATE_A_HOST:
2014 case OTG_STATE_B_PERIPHERAL:
2015 set_bit(ID, &motg->inputs);
2016 clear_bit(B_SESS_VLD, &motg->inputs);
2017 break;
2018 default:
2019 goto out;
2020 }
2021 break;
2022 case USB_PERIPHERAL:
2023 switch (otg->state) {
2024 case OTG_STATE_B_IDLE:
2025 case OTG_STATE_A_HOST:
2026 set_bit(ID, &motg->inputs);
2027 set_bit(B_SESS_VLD, &motg->inputs);
2028 break;
2029 default:
2030 goto out;
2031 }
2032 break;
2033 case USB_HOST:
2034 switch (otg->state) {
2035 case OTG_STATE_B_IDLE:
2036 case OTG_STATE_B_PERIPHERAL:
2037 clear_bit(ID, &motg->inputs);
2038 break;
2039 default:
2040 goto out;
2041 }
2042 break;
2043 default:
2044 goto out;
2045 }
2046
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302047 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302048 schedule_work(&motg->sm_work);
2049out:
2050 return status;
2051}
2052
2053const struct file_operations msm_otg_mode_fops = {
2054 .open = msm_otg_mode_open,
2055 .read = seq_read,
2056 .write = msm_otg_mode_write,
2057 .llseek = seq_lseek,
2058 .release = single_release,
2059};
2060
Anji jonnalad270e2d2011-08-09 11:28:32 +05302061static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2062{
2063 struct msm_otg *motg = s->private;
2064
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302065 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302066 return 0;
2067}
2068
2069static int msm_otg_chg_open(struct inode *inode, struct file *file)
2070{
2071 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2072}
2073
2074const struct file_operations msm_otg_chg_fops = {
2075 .open = msm_otg_chg_open,
2076 .read = seq_read,
2077 .llseek = seq_lseek,
2078 .release = single_release,
2079};
2080
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302081static int msm_otg_aca_show(struct seq_file *s, void *unused)
2082{
2083 if (debug_aca_enabled)
2084 seq_printf(s, "enabled\n");
2085 else
2086 seq_printf(s, "disabled\n");
2087
2088 return 0;
2089}
2090
2091static int msm_otg_aca_open(struct inode *inode, struct file *file)
2092{
2093 return single_open(file, msm_otg_aca_show, inode->i_private);
2094}
2095
2096static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2097 size_t count, loff_t *ppos)
2098{
2099 char buf[8];
2100
2101 memset(buf, 0x00, sizeof(buf));
2102
2103 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2104 return -EFAULT;
2105
2106 if (!strncmp(buf, "enable", 6))
2107 debug_aca_enabled = true;
2108 else
2109 debug_aca_enabled = false;
2110
2111 return count;
2112}
2113
2114const struct file_operations msm_otg_aca_fops = {
2115 .open = msm_otg_aca_open,
2116 .read = seq_read,
2117 .write = msm_otg_aca_write,
2118 .llseek = seq_lseek,
2119 .release = single_release,
2120};
2121
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302122static struct dentry *msm_otg_dbg_root;
2123static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302124static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302125static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302126
2127static int msm_otg_debugfs_init(struct msm_otg *motg)
2128{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302129
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302130 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2131
2132 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2133 return -ENODEV;
2134
Anji jonnalad270e2d2011-08-09 11:28:32 +05302135 if (motg->pdata->mode == USB_OTG &&
2136 motg->pdata->otg_control == OTG_USER_CONTROL) {
2137
2138 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2139 S_IWUSR, msm_otg_dbg_root, motg,
2140 &msm_otg_mode_fops);
2141
2142 if (!msm_otg_dbg_mode) {
2143 debugfs_remove(msm_otg_dbg_root);
2144 msm_otg_dbg_root = NULL;
2145 return -ENODEV;
2146 }
2147 }
2148
2149 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2150 msm_otg_dbg_root, motg,
2151 &msm_otg_chg_fops);
2152
2153 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302154 debugfs_remove_recursive(msm_otg_dbg_root);
2155 return -ENODEV;
2156 }
2157
2158 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2159 msm_otg_dbg_root, motg,
2160 &msm_otg_aca_fops);
2161
2162 if (!msm_otg_dbg_aca) {
2163 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302164 return -ENODEV;
2165 }
2166
2167 return 0;
2168}
2169
2170static void msm_otg_debugfs_cleanup(void)
2171{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302172 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302173}
2174
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302175static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2176static struct platform_device *msm_otg_add_pdev(
2177 struct platform_device *ofdev, const char *name)
2178{
2179 struct platform_device *pdev;
2180 const struct resource *res = ofdev->resource;
2181 unsigned int num = ofdev->num_resources;
2182 int retval;
2183
2184 pdev = platform_device_alloc(name, -1);
2185 if (!pdev) {
2186 retval = -ENOMEM;
2187 goto error;
2188 }
2189
2190 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2191 pdev->dev.dma_mask = &msm_otg_dma_mask;
2192
2193 if (num) {
2194 retval = platform_device_add_resources(pdev, res, num);
2195 if (retval)
2196 goto error;
2197 }
2198
2199 retval = platform_device_add(pdev);
2200 if (retval)
2201 goto error;
2202
2203 return pdev;
2204
2205error:
2206 platform_device_put(pdev);
2207 return ERR_PTR(retval);
2208}
2209
2210static int msm_otg_setup_devices(struct platform_device *ofdev,
2211 enum usb_mode_type mode, bool init)
2212{
2213 const char *gadget_name = "msm_hsusb";
2214 const char *host_name = "msm_hsusb_host";
2215 static struct platform_device *gadget_pdev;
2216 static struct platform_device *host_pdev;
2217 int retval = 0;
2218
2219 if (!init) {
2220 if (gadget_pdev)
2221 platform_device_unregister(gadget_pdev);
2222 if (host_pdev)
2223 platform_device_unregister(host_pdev);
2224 return 0;
2225 }
2226
2227 switch (mode) {
2228 case USB_OTG:
2229 /* fall through */
2230 case USB_PERIPHERAL:
2231 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2232 if (IS_ERR(gadget_pdev)) {
2233 retval = PTR_ERR(gadget_pdev);
2234 break;
2235 }
2236 if (mode == USB_PERIPHERAL)
2237 break;
2238 /* fall through */
2239 case USB_HOST:
2240 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2241 if (IS_ERR(host_pdev)) {
2242 retval = PTR_ERR(host_pdev);
2243 if (mode == USB_OTG)
2244 platform_device_unregister(gadget_pdev);
2245 }
2246 break;
2247 default:
2248 break;
2249 }
2250
2251 return retval;
2252}
2253
2254struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2255{
2256 struct device_node *node = pdev->dev.of_node;
2257 struct msm_otg_platform_data *pdata;
2258 int len = 0;
2259
2260 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2261 if (!pdata) {
2262 pr_err("unable to allocate platform data\n");
2263 return NULL;
2264 }
2265 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2266 if (len) {
2267 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2268 if (!pdata->phy_init_seq)
2269 return NULL;
2270 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2271 pdata->phy_init_seq,
2272 len/sizeof(*pdata->phy_init_seq));
2273 }
2274 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2275 &pdata->power_budget);
2276 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2277 &pdata->mode);
2278 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2279 &pdata->otg_control);
2280 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2281 &pdata->default_mode);
2282 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2283 &pdata->phy_type);
2284 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2285 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302286 return pdata;
2287}
2288
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302289static int __init msm_otg_probe(struct platform_device *pdev)
2290{
2291 int ret = 0;
2292 struct resource *res;
2293 struct msm_otg *motg;
2294 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302295 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302296
2297 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302298
2299 if (pdev->dev.of_node) {
2300 dev_dbg(&pdev->dev, "device tree enabled\n");
2301 pdata = msm_otg_dt_to_pdata(pdev);
2302 if (!pdata)
2303 return -ENOMEM;
2304 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2305 if (ret) {
2306 dev_err(&pdev->dev, "devices setup failed\n");
2307 return ret;
2308 }
2309 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302310 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2311 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302312 } else {
2313 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302314 }
2315
2316 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2317 if (!motg) {
2318 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2319 return -ENOMEM;
2320 }
2321
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302323 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302324 otg = &motg->otg;
2325 otg->dev = &pdev->dev;
2326
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302327 /*
2328 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2329 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2330 * PMIC can detect ACA ID_GND and generate an interrupt.
2331 */
2332 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2333 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2334 ret = -EINVAL;
2335 goto free_motg;
2336 }
2337
Ofir Cohen4da266f2012-01-03 10:19:29 +02002338 /* initialize reset counter */
2339 motg->reset_counter = 0;
2340
Amit Blay02eff132011-09-21 16:46:24 +03002341 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002342 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002343 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002344 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302345
Manu Gautam5143b252012-01-05 19:25:23 -08002346 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302347 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002348 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302349 ret = PTR_ERR(motg->clk);
2350 goto put_phy_reset_clk;
2351 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302352 clk_set_rate(motg->clk, 60000000);
2353
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302354 /* pm qos request to prevent apps idle power collapse */
2355 if (motg->pdata->swfi_latency)
2356 pm_qos_add_request(&motg->pm_qos_req_dma,
2357 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002358
Anji jonnala0f73cac2011-05-04 10:19:46 +05302359 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002360 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302361 * CORE CLK must be running at >55Mhz for correct HSUSB
2362 * operation and USB core cannot tolerate frequency changes on
2363 * CORE CLK. For such USB cores, vote for maximum clk frequency
2364 * on pclk source
2365 */
Manu Gautam5143b252012-01-05 19:25:23 -08002366 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2367 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302368 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002369 dev_err(&pdev->dev, "failed to get core_clk\n");
2370 ret = PTR_ERR(motg->clk);
2371 goto put_clk;
2372 }
2373 clk_set_rate(motg->core_clk, INT_MAX);
2374
2375 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2376 if (IS_ERR(motg->pclk)) {
2377 dev_err(&pdev->dev, "failed to get iface_clk\n");
2378 ret = PTR_ERR(motg->pclk);
2379 goto put_core_clk;
2380 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302381
2382 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2383 if (!res) {
2384 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2385 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002386 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302387 }
2388
2389 motg->regs = ioremap(res->start, resource_size(res));
2390 if (!motg->regs) {
2391 dev_err(&pdev->dev, "ioremap failed\n");
2392 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002393 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302394 }
2395 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2396
2397 motg->irq = platform_get_irq(pdev, 0);
2398 if (!motg->irq) {
2399 dev_err(&pdev->dev, "platform_get_irq failed\n");
2400 ret = -ENODEV;
2401 goto free_regs;
2402 }
2403
Anji jonnala7da3f262011-12-02 17:22:14 -08002404 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2405 if (IS_ERR(motg->xo_handle)) {
2406 dev_err(&pdev->dev, "%s not able to get the handle "
2407 "to vote for TCXO D0 buffer\n", __func__);
2408 ret = PTR_ERR(motg->xo_handle);
2409 goto free_regs;
2410 }
2411
2412 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2413 if (ret) {
2414 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2415 "D0 buffer%d\n", __func__, ret);
2416 goto free_xo_handle;
2417 }
2418
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302419 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302420
2421 ret = msm_hsusb_init_vddcx(motg, 1);
2422 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002423 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002424 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302425 }
2426
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002427 ret = msm_hsusb_config_vddcx(1);
2428 if (ret) {
2429 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2430 goto free_init_vddcx;
2431 }
2432
Anji jonnala11aa5c42011-05-04 10:19:48 +05302433 ret = msm_hsusb_ldo_init(motg, 1);
2434 if (ret) {
2435 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002436 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302437 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002438
2439 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302440 if (ret) {
2441 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002442 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302443 }
Manu Gautam5143b252012-01-05 19:25:23 -08002444 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302445
2446 writel(0, USB_USBINTR);
2447 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002448 /* Ensure that above STOREs are completed before enabling interrupts */
2449 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002451 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302452 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302453 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302454 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2455 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302456 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2457 "msm_otg", motg);
2458 if (ret) {
2459 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002460 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302461 }
2462
2463 otg->init = msm_otg_reset;
2464 otg->set_host = msm_otg_set_host;
2465 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302466 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302467 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302468
2469 otg->io_ops = &msm_otg_io_ops;
2470
2471 ret = otg_set_transceiver(&motg->otg);
2472 if (ret) {
2473 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2474 goto free_irq;
2475 }
2476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002477 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2478 if (motg->pdata->pmic_id_irq) {
2479 ret = request_irq(motg->pdata->pmic_id_irq,
2480 msm_pmic_id_irq,
2481 IRQF_TRIGGER_RISING |
2482 IRQF_TRIGGER_FALLING,
2483 "msm_otg", motg);
2484 if (ret) {
2485 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2486 goto remove_otg;
2487 }
2488 } else {
2489 ret = -ENODEV;
2490 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2491 goto remove_otg;
2492 }
2493 }
2494
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302495 msm_hsusb_mhl_switch_enable(motg, 1);
2496
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302497 platform_set_drvdata(pdev, motg);
2498 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302500
Anji jonnalad270e2d2011-08-09 11:28:32 +05302501 ret = msm_otg_debugfs_init(motg);
2502 if (ret)
2503 dev_dbg(&pdev->dev, "mode debugfs file is"
2504 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2507 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2508
Amit Blay58b31472011-11-18 09:39:39 +02002509 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2510 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002511 motg->pdata->pmic_id_irq)
Amit Blay58b31472011-11-18 09:39:39 +02002512 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002513 ALLOW_PHY_RETENTION |
2514 ALLOW_PHY_COMP_DISABLE;
2515
Amit Blay58b31472011-11-18 09:39:39 +02002516 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2517 motg->caps = ALLOW_PHY_RETENTION;
2518 }
2519
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302521 pm_runtime_set_active(&pdev->dev);
2522 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302523
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302524 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525
2526remove_otg:
2527 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302528free_irq:
2529 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530destroy_wlock:
2531 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002532 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002533 msm_hsusb_ldo_enable(motg, 0);
2534free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302535 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002536free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302537 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002538devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002539 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002540 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2541free_xo_handle:
2542 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302543free_regs:
2544 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002545put_pclk:
2546 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302547put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002548 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302549put_clk:
2550 clk_put(motg->clk);
2551put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002552 if (!IS_ERR(motg->phy_reset_clk))
2553 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302554free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302555 if (motg->pdata->swfi_latency)
2556 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302557 kfree(motg);
2558 return ret;
2559}
2560
2561static int __devexit msm_otg_remove(struct platform_device *pdev)
2562{
2563 struct msm_otg *motg = platform_get_drvdata(pdev);
2564 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302565 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302566
2567 if (otg->host || otg->gadget)
2568 return -EBUSY;
2569
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302570 if (pdev->dev.of_node)
2571 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002572 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2573 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302574 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302575 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302576 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302577
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302578 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302579
2580 device_init_wakeup(&pdev->dev, 0);
2581 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002582 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302583
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302584 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585 if (motg->pdata->pmic_id_irq)
2586 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302587 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302588 free_irq(motg->irq, motg);
2589
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302590 /*
2591 * Put PHY in low power mode.
2592 */
2593 ulpi_read(otg, 0x14);
2594 ulpi_write(otg, 0x08, 0x09);
2595
2596 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2597 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2598 if (readl(USB_PORTSC) & PORTSC_PHCD)
2599 break;
2600 udelay(1);
2601 cnt++;
2602 }
2603 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2604 dev_err(otg->dev, "Unable to suspend PHY\n");
2605
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302606 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002607 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002608 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302610 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302612
2613 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302614 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302615
Amit Blay02eff132011-09-21 16:46:24 +03002616 if (!IS_ERR(motg->phy_reset_clk))
2617 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302618 clk_put(motg->pclk);
2619 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002620 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302621
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302622 if (motg->pdata->swfi_latency)
2623 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302624
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302625 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302626 return 0;
2627}
2628
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302629#ifdef CONFIG_PM_RUNTIME
2630static int msm_otg_runtime_idle(struct device *dev)
2631{
2632 struct msm_otg *motg = dev_get_drvdata(dev);
2633 struct otg_transceiver *otg = &motg->otg;
2634
2635 dev_dbg(dev, "OTG runtime idle\n");
2636
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302637 if (otg->state == OTG_STATE_UNDEFINED)
2638 return -EAGAIN;
2639 else
2640 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302641}
2642
2643static int msm_otg_runtime_suspend(struct device *dev)
2644{
2645 struct msm_otg *motg = dev_get_drvdata(dev);
2646
2647 dev_dbg(dev, "OTG runtime suspend\n");
2648 return msm_otg_suspend(motg);
2649}
2650
2651static int msm_otg_runtime_resume(struct device *dev)
2652{
2653 struct msm_otg *motg = dev_get_drvdata(dev);
2654
2655 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302656 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302657 return msm_otg_resume(motg);
2658}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302659#endif
2660
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302661#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302662static int msm_otg_pm_suspend(struct device *dev)
2663{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302664 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302665
2666 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302667
2668#ifdef CONFIG_PM_RUNTIME
2669 ret = pm_runtime_suspend(dev);
2670 if (ret > 0)
2671 ret = 0;
2672#else
2673 ret = msm_otg_suspend(dev_get_drvdata(dev));
2674#endif
2675 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302676}
2677
2678static int msm_otg_pm_resume(struct device *dev)
2679{
2680 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302681
2682 dev_dbg(dev, "OTG PM resume\n");
2683
Manu Gautamf284c052011-09-08 16:52:48 +05302684#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302685 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302686 * Do not resume hardware as part of system resume,
2687 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302688 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002689 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302690#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302691
Manu Gautamf284c052011-09-08 16:52:48 +05302692 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302693}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302694#endif
2695
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302696#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302697static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302698 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2699 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2700 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302701};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302702#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302703
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302704static struct of_device_id msm_otg_dt_match[] = {
2705 { .compatible = "qcom,hsusb-otg",
2706 },
2707 {}
2708};
2709
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302710static struct platform_driver msm_otg_driver = {
2711 .remove = __devexit_p(msm_otg_remove),
2712 .driver = {
2713 .name = DRIVER_NAME,
2714 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302715#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302716 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302717#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302718 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302719 },
2720};
2721
2722static int __init msm_otg_init(void)
2723{
2724 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2725}
2726
2727static void __exit msm_otg_exit(void)
2728{
2729 platform_driver_unregister(&msm_otg_driver);
2730}
2731
2732module_init(msm_otg_init);
2733module_exit(msm_otg_exit);
2734
2735MODULE_LICENSE("GPL v2");
2736MODULE_DESCRIPTION("MSM USB transceiver driver");