blob: d756dd82360f2edcfd80aec89366a0ba598c876f [file] [log] [blame]
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301/* Copyright (c) 2009-2011, 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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700526 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530527 ret = msm_otg_phy_reset(motg);
528 if (ret) {
529 dev_err(otg->dev, "phy_reset failed\n");
530 return ret;
531 }
532
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530533 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530534 ret = msm_otg_link_reset(motg);
535 if (ret) {
536 dev_err(otg->dev, "link reset failed\n");
537 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530538 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530539 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530540
541 ulpi_init(motg);
542
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700543 /* Ensure that RESET operation is completed before turning off clock */
544 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530545
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546 clk_disable(motg->clk);
547
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530548 if (pdata->otg_control == OTG_PHY_CONTROL) {
549 val = readl_relaxed(USB_OTGSC);
550 if (pdata->mode == USB_OTG) {
551 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
552 val |= OTGSC_IDIE | OTGSC_BSVIE;
553 } else if (pdata->mode == USB_PERIPHERAL) {
554 ulpi_val = ULPI_INT_SESS_VALID;
555 val |= OTGSC_BSVIE;
556 }
557 writel_relaxed(val, USB_OTGSC);
558 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
559 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530560 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530562 return 0;
563}
564
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530565static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
566{
567 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
568
569 /*
570 * Allow bus suspend only for host mode. Device mode bus suspend
571 * is not implemented yet.
572 */
573 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530574 /*
575 * ID_GND --> ID_A transition can not be detected in LPM.
576 * Disallow host bus suspend when ACA is enabled.
577 */
578 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530579 pm_runtime_put(otg->dev);
580 else
581 pm_runtime_resume(otg->dev);
582 }
583
584 return 0;
585}
586
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530587#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530588#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
589
590#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530591static int msm_otg_suspend(struct msm_otg *motg)
592{
593 struct otg_transceiver *otg = &motg->otg;
594 struct usb_bus *bus = otg->host;
595 struct msm_otg_platform_data *pdata = motg->pdata;
596 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530597 bool host_bus_suspend;
598 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800599 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530600 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530601
602 if (atomic_read(&motg->in_lpm))
603 return 0;
604
605 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530606 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530607 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530608 * Chipidea 45-nm PHY suspend sequence:
609 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530610 * Interrupt Latch Register auto-clear feature is not present
611 * in all PHY versions. Latch register is clear on read type.
612 * Clear latch register to avoid spurious wakeup from
613 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530614 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530615 * PHY comparators are disabled when PHY enters into low power
616 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
617 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
618 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530619 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530620 * PLL is not turned off when PHY enters into low power mode (LPM).
621 * Disable PLL for maximum power savings.
622 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530623
624 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
625 ulpi_read(otg, 0x14);
626 if (pdata->otg_control == OTG_PHY_CONTROL)
627 ulpi_write(otg, 0x01, 0x30);
628 ulpi_write(otg, 0x08, 0x09);
629 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530630
631 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700632 * Turn off the OTG comparators, if depends on PMIC for
633 * VBUS and ID notifications.
634 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530635 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636 ulpi_write(otg, OTG_COMP_DISABLE,
637 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
638 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
639 }
640
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530641 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530642 * PHY may take some time or even fail to enter into low power
643 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
644 * in failure case.
645 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530646 portsc = readl_relaxed(USB_PORTSC);
647 if (!(portsc & PORTSC_PHCD)) {
648 writel_relaxed(portsc | PORTSC_PHCD,
649 USB_PORTSC);
650 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
651 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
652 break;
653 udelay(1);
654 cnt++;
655 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530656 }
657
658 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
659 dev_err(otg->dev, "Unable to suspend PHY\n");
660 msm_otg_reset(otg);
661 enable_irq(motg->irq);
662 return -ETIMEDOUT;
663 }
664
665 /*
666 * PHY has capability to generate interrupt asynchronously in low
667 * power mode (LPM). This interrupt is level triggered. So USB IRQ
668 * line must be disabled till async interrupt enable bit is cleared
669 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
670 * block data communication from PHY.
671 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530672 cmd_val = readl_relaxed(USB_USBCMD);
673 if (host_bus_suspend)
674 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
675 else
676 cmd_val |= ULPI_STP_CTRL;
677 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530678
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530679 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200680 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
681 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
682 /* Enable PHY HV interrupts to wake MPM/Link */
683 phy_ctrl_val |=
684 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
685
686 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700687 motg->lpm_flags |= PHY_RETENTIONED;
688 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 /* Ensure that above operation is completed before turning off clocks */
691 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530692 clk_disable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530693 if (motg->core_clk)
694 clk_disable(motg->core_clk);
695
Amit Blay137575f2011-11-06 15:20:54 +0200696 if (!IS_ERR(motg->system_clk))
697 clk_disable(motg->system_clk);
698
Anji jonnala0f73cac2011-05-04 10:19:46 +0530699 if (!IS_ERR(motg->pclk_src))
700 clk_disable(motg->pclk_src);
701
Anji jonnala7da3f262011-12-02 17:22:14 -0800702 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
703 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
704 if (ret)
705 dev_err(otg->dev, "%s failed to devote for "
706 "TCXO D0 buffer%d\n", __func__, ret);
707
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530708 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700709 msm_hsusb_ldo_enable(motg, 0);
710 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530711 }
712
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530713 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530715 msm_hsusb_mhl_switch_enable(motg, 0);
716 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700717
718 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530719 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 if (motg->pdata->pmic_id_irq)
721 enable_irq_wake(motg->pdata->pmic_id_irq);
722 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530723 if (bus)
724 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
725
726 atomic_set(&motg->in_lpm, 1);
727 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530729
730 dev_info(otg->dev, "USB in low power mode\n");
731
732 return 0;
733}
734
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530735static int msm_otg_resume(struct msm_otg *motg)
736{
737 struct otg_transceiver *otg = &motg->otg;
738 struct usb_bus *bus = otg->host;
739 int cnt = 0;
740 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200741 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800742 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530743
744 if (!atomic_read(&motg->in_lpm))
745 return 0;
746
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700747 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800748
749 /* Vote for TCXO when waking up the phy */
750 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
751 if (ret)
752 dev_err(otg->dev, "%s failed to vote for "
753 "TCXO D0 buffer%d\n", __func__, ret);
754
Anji jonnala0f73cac2011-05-04 10:19:46 +0530755 if (!IS_ERR(motg->pclk_src))
756 clk_enable(motg->pclk_src);
757
Amit Blay137575f2011-11-06 15:20:54 +0200758 if (!IS_ERR(motg->system_clk))
759 clk_enable(motg->system_clk);
760
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530761 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762 if (motg->core_clk)
763 clk_enable(motg->core_clk);
764
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
766 msm_hsusb_ldo_enable(motg, 1);
767 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
768 }
769
770 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530771 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530772 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200773 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
774 phy_ctrl_val |= PHY_RETEN;
775 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
776 /* Disable PHY HV interrupts */
777 phy_ctrl_val &=
778 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
779 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 }
782
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530783 temp = readl(USB_USBCMD);
784 temp &= ~ASYNC_INTR_CTRL;
785 temp &= ~ULPI_STP_CTRL;
786 writel(temp, USB_USBCMD);
787
788 /*
789 * PHY comes out of low power mode (LPM) in case of wakeup
790 * from asynchronous interrupt.
791 */
792 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
793 goto skip_phy_resume;
794
795 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
796 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
797 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
798 break;
799 udelay(1);
800 cnt++;
801 }
802
803 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
804 /*
805 * This is a fatal error. Reset the link and
806 * PHY. USB state can not be restored. Re-insertion
807 * of USB cable is the only way to get USB working.
808 */
809 dev_err(otg->dev, "Unable to resume USB."
810 "Re-plugin the cable\n");
811 msm_otg_reset(otg);
812 }
813
814skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 /* Turn on the OTG comparators on resume */
816 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
817 ulpi_write(otg, OTG_COMP_DISABLE,
818 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
819 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
820 }
821 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (motg->pdata->pmic_id_irq)
824 disable_irq_wake(motg->pdata->pmic_id_irq);
825 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826 if (bus)
827 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
828
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530829 atomic_set(&motg->in_lpm, 0);
830
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 if (motg->async_int) {
832 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 enable_irq(motg->irq);
834 }
835
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530836 dev_info(otg->dev, "USB exited from low power mode\n");
837
838 return 0;
839}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530840#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530842static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
843{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530844 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
845 motg->chg_type == USB_ACA_A_CHARGER ||
846 motg->chg_type == USB_ACA_B_CHARGER ||
847 motg->chg_type == USB_ACA_C_CHARGER) &&
848 mA > IDEV_ACA_CHG_LIMIT)
849 mA = IDEV_ACA_CHG_LIMIT;
850
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530851 if (motg->cur_power == mA)
852 return;
853
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530854 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530856 motg->cur_power = mA;
857}
858
859static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
860{
861 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
862
863 /*
864 * Gadget driver uses set_power method to notify about the
865 * available current based on suspend/configured states.
866 *
867 * IDEV_CHG can be drawn irrespective of suspend/un-configured
868 * states when CDP/ACA is connected.
869 */
870 if (motg->chg_type == USB_SDP_CHARGER)
871 msm_otg_notify_charger(motg, mA);
872
873 return 0;
874}
875
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530876static void msm_otg_start_host(struct otg_transceiver *otg, int on)
877{
878 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
879 struct msm_otg_platform_data *pdata = motg->pdata;
880 struct usb_hcd *hcd;
881
882 if (!otg->host)
883 return;
884
885 hcd = bus_to_hcd(otg->host);
886
887 if (on) {
888 dev_dbg(otg->dev, "host on\n");
889
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530890 /*
891 * Some boards have a switch cotrolled by gpio
892 * to enable/disable internal HUB. Enable internal
893 * HUB before kicking the host.
894 */
895 if (pdata->setup_gpio)
896 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530897 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530898 } else {
899 dev_dbg(otg->dev, "host off\n");
900
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530901 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530902 /* HCD core reset all bits of PORTSC. select ULPI phy */
903 writel_relaxed(0x80000000, USB_PORTSC);
904
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530905 if (pdata->setup_gpio)
906 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530907 }
908}
909
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700910static int msm_otg_usbdev_notify(struct notifier_block *self,
911 unsigned long action, void *priv)
912{
913 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530914 struct usb_device *udev = priv;
915
916 if (!aca_enabled())
917 goto out;
918
919 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
920 goto out;
921
922 if (udev->bus != motg->otg.host)
923 goto out;
924 /*
925 * Interested in devices connected directly to the root hub.
926 * ACA dock can supply IDEV_CHG irrespective devices connected
927 * on the accessory port.
928 */
929 if (!udev->parent || udev->parent->parent ||
930 motg->chg_type == USB_ACA_DOCK_CHARGER)
931 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932
933 switch (action) {
934 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530935 usb_disable_autosuspend(udev);
936 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700938 if (udev->actconfig)
939 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
940 else
941 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530942 break;
943 case USB_DEVICE_REMOVE:
944 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 break;
946 default:
947 break;
948 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530949 if (test_bit(ID_A, &motg->inputs))
950 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
951 motg->mA_port);
952out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 return NOTIFY_OK;
954}
955
Mayank Ranae3926882011-12-26 09:47:54 +0530956static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
957{
958 int ret;
959 static bool vbus_is_on;
960
961 if (vbus_is_on == on)
962 return;
963
964 if (motg->pdata->vbus_power) {
965 motg->pdata->vbus_power(on);
966 return;
967 }
968
969 if (!vbus_otg) {
970 pr_err("vbus_otg is NULL.");
971 return;
972 }
973
974 if (on) {
975 ret = regulator_enable(vbus_otg);
976 if (ret) {
977 pr_err("unable to enable vbus_otg\n");
978 return;
979 }
980 vbus_is_on = true;
981 } else {
982 ret = regulator_disable(vbus_otg);
983 if (ret) {
984 pr_err("unable to disable vbus_otg\n");
985 return;
986 }
987 vbus_is_on = false;
988 }
989}
990
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530991static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
992{
993 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
994 struct usb_hcd *hcd;
995
996 /*
997 * Fail host registration if this board can support
998 * only peripheral configuration.
999 */
1000 if (motg->pdata->mode == USB_PERIPHERAL) {
1001 dev_info(otg->dev, "Host mode is not supported\n");
1002 return -ENODEV;
1003 }
1004
Mayank Ranae3926882011-12-26 09:47:54 +05301005 if (!motg->pdata->vbus_power && host) {
1006 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1007 if (IS_ERR(vbus_otg)) {
1008 pr_err("Unable to get vbus_otg\n");
1009 return -ENODEV;
1010 }
1011 }
1012
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301013 if (!host) {
1014 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301015 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001016 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301017 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301018 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301019 otg->host = NULL;
1020 otg->state = OTG_STATE_UNDEFINED;
1021 schedule_work(&motg->sm_work);
1022 } else {
1023 otg->host = NULL;
1024 }
1025
Mayank Ranae3926882011-12-26 09:47:54 +05301026 if (vbus_otg)
1027 regulator_put(vbus_otg);
1028
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301029 return 0;
1030 }
1031
1032 hcd = bus_to_hcd(host);
1033 hcd->power_budget = motg->pdata->power_budget;
1034
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001035 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1036 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301037 otg->host = host;
1038 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1039
1040 /*
1041 * Kick the state machine work, if peripheral is not supported
1042 * or peripheral is already registered with us.
1043 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301044 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1045 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301046 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301047 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301048
1049 return 0;
1050}
1051
1052static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1053{
1054 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1055 struct msm_otg_platform_data *pdata = motg->pdata;
1056
1057 if (!otg->gadget)
1058 return;
1059
1060 if (on) {
1061 dev_dbg(otg->dev, "gadget on\n");
1062 /*
1063 * Some boards have a switch cotrolled by gpio
1064 * to enable/disable internal HUB. Disable internal
1065 * HUB before kicking the gadget.
1066 */
1067 if (pdata->setup_gpio)
1068 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301069 /*
1070 * vote for minimum dma_latency to prevent idle
1071 * power collapse(pc) while running in peripheral mode.
1072 */
1073 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301074 usb_gadget_vbus_connect(otg->gadget);
1075 } else {
1076 dev_dbg(otg->dev, "gadget off\n");
1077 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301078 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301079 if (pdata->setup_gpio)
1080 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1081 }
1082
1083}
1084
1085static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1086 struct usb_gadget *gadget)
1087{
1088 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1089
1090 /*
1091 * Fail peripheral registration if this board can support
1092 * only host configuration.
1093 */
1094 if (motg->pdata->mode == USB_HOST) {
1095 dev_info(otg->dev, "Peripheral mode is not supported\n");
1096 return -ENODEV;
1097 }
1098
1099 if (!gadget) {
1100 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301101 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301102 msm_otg_start_peripheral(otg, 0);
1103 otg->gadget = NULL;
1104 otg->state = OTG_STATE_UNDEFINED;
1105 schedule_work(&motg->sm_work);
1106 } else {
1107 otg->gadget = NULL;
1108 }
1109
1110 return 0;
1111 }
1112 otg->gadget = gadget;
1113 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1114
1115 /*
1116 * Kick the state machine work, if host is not supported
1117 * or host is already registered with us.
1118 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301119 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1120 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301122 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123
1124 return 0;
1125}
1126
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001127static bool msm_chg_aca_detect(struct msm_otg *motg)
1128{
1129 struct otg_transceiver *otg = &motg->otg;
1130 u32 int_sts;
1131 bool ret = false;
1132
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301133 if (!aca_enabled())
1134 goto out;
1135
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001136 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1137 goto out;
1138
1139 int_sts = ulpi_read(otg, 0x87);
1140 switch (int_sts & 0x1C) {
1141 case 0x08:
1142 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1143 dev_dbg(otg->dev, "ID_A\n");
1144 motg->chg_type = USB_ACA_A_CHARGER;
1145 motg->chg_state = USB_CHG_STATE_DETECTED;
1146 clear_bit(ID_B, &motg->inputs);
1147 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301148 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001149 ret = true;
1150 }
1151 break;
1152 case 0x0C:
1153 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1154 dev_dbg(otg->dev, "ID_B\n");
1155 motg->chg_type = USB_ACA_B_CHARGER;
1156 motg->chg_state = USB_CHG_STATE_DETECTED;
1157 clear_bit(ID_A, &motg->inputs);
1158 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301159 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001160 ret = true;
1161 }
1162 break;
1163 case 0x10:
1164 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1165 dev_dbg(otg->dev, "ID_C\n");
1166 motg->chg_type = USB_ACA_C_CHARGER;
1167 motg->chg_state = USB_CHG_STATE_DETECTED;
1168 clear_bit(ID_A, &motg->inputs);
1169 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301170 set_bit(ID, &motg->inputs);
1171 ret = true;
1172 }
1173 break;
1174 case 0x04:
1175 if (test_and_clear_bit(ID, &motg->inputs)) {
1176 dev_dbg(otg->dev, "ID_GND\n");
1177 motg->chg_type = USB_INVALID_CHARGER;
1178 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1179 clear_bit(ID_A, &motg->inputs);
1180 clear_bit(ID_B, &motg->inputs);
1181 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182 ret = true;
1183 }
1184 break;
1185 default:
1186 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1187 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301188 test_and_clear_bit(ID_C, &motg->inputs) |
1189 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301191 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001192 motg->chg_type = USB_INVALID_CHARGER;
1193 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1194 }
1195 }
1196out:
1197 return ret;
1198}
1199
1200static void msm_chg_enable_aca_det(struct msm_otg *motg)
1201{
1202 struct otg_transceiver *otg = &motg->otg;
1203
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301204 if (!aca_enabled())
1205 return;
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 switch (motg->pdata->phy_type) {
1208 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301209 /* Disable ID_GND in link and PHY */
1210 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1211 OTGSC_IDIE), USB_OTGSC);
1212 ulpi_write(otg, 0x01, 0x0C);
1213 ulpi_write(otg, 0x10, 0x0F);
1214 ulpi_write(otg, 0x10, 0x12);
1215 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301217 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001218 break;
1219 default:
1220 break;
1221 }
1222}
1223
1224static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1225{
1226 struct otg_transceiver *otg = &motg->otg;
1227
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301228 if (!aca_enabled())
1229 return;
1230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 switch (motg->pdata->phy_type) {
1232 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301233 /* Enable ACA Detection interrupt (on any RID change) */
1234 ulpi_write(otg, 0x01, 0x94);
1235 break;
1236 default:
1237 break;
1238 }
1239}
1240
1241static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1242{
1243 struct otg_transceiver *otg = &motg->otg;
1244
1245 if (!aca_enabled())
1246 return;
1247
1248 switch (motg->pdata->phy_type) {
1249 case SNPS_28NM_INTEGRATED_PHY:
1250 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251 break;
1252 default:
1253 break;
1254 }
1255}
1256
1257static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1258{
1259 struct otg_transceiver *otg = &motg->otg;
1260 bool ret = false;
1261
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301262 if (!aca_enabled())
1263 return ret;
1264
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 switch (motg->pdata->phy_type) {
1266 case SNPS_28NM_INTEGRATED_PHY:
1267 if (ulpi_read(otg, 0x91) & 1) {
1268 dev_dbg(otg->dev, "RID change\n");
1269 ulpi_write(otg, 0x01, 0x92);
1270 ret = msm_chg_aca_detect(motg);
1271 }
1272 default:
1273 break;
1274 }
1275 return ret;
1276}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301277
1278static void msm_otg_id_timer_func(unsigned long data)
1279{
1280 struct msm_otg *motg = (struct msm_otg *) data;
1281
1282 if (!aca_enabled())
1283 return;
1284
1285 if (atomic_read(&motg->in_lpm)) {
1286 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1287 return;
1288 }
1289
1290 if (msm_chg_check_aca_intr(motg)) {
1291 dev_dbg(motg->otg.dev, "timer: aca work\n");
1292 schedule_work(&motg->sm_work);
1293 }
1294
1295 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1296 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1297}
1298
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301299static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1300{
1301 struct otg_transceiver *otg = &motg->otg;
1302 u32 chg_det;
1303 bool ret = false;
1304
1305 switch (motg->pdata->phy_type) {
1306 case CI_45NM_INTEGRATED_PHY:
1307 chg_det = ulpi_read(otg, 0x34);
1308 ret = chg_det & (1 << 4);
1309 break;
1310 case SNPS_28NM_INTEGRATED_PHY:
1311 chg_det = ulpi_read(otg, 0x87);
1312 ret = chg_det & 1;
1313 break;
1314 default:
1315 break;
1316 }
1317 return ret;
1318}
1319
1320static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1321{
1322 struct otg_transceiver *otg = &motg->otg;
1323 u32 chg_det;
1324
1325 switch (motg->pdata->phy_type) {
1326 case CI_45NM_INTEGRATED_PHY:
1327 chg_det = ulpi_read(otg, 0x34);
1328 /* Turn off charger block */
1329 chg_det |= ~(1 << 1);
1330 ulpi_write(otg, chg_det, 0x34);
1331 udelay(20);
1332 /* control chg block via ULPI */
1333 chg_det &= ~(1 << 3);
1334 ulpi_write(otg, chg_det, 0x34);
1335 /* put it in host mode for enabling D- source */
1336 chg_det &= ~(1 << 2);
1337 ulpi_write(otg, chg_det, 0x34);
1338 /* Turn on chg detect block */
1339 chg_det &= ~(1 << 1);
1340 ulpi_write(otg, chg_det, 0x34);
1341 udelay(20);
1342 /* enable chg detection */
1343 chg_det &= ~(1 << 0);
1344 ulpi_write(otg, chg_det, 0x34);
1345 break;
1346 case SNPS_28NM_INTEGRATED_PHY:
1347 /*
1348 * Configure DM as current source, DP as current sink
1349 * and enable battery charging comparators.
1350 */
1351 ulpi_write(otg, 0x8, 0x85);
1352 ulpi_write(otg, 0x2, 0x85);
1353 ulpi_write(otg, 0x1, 0x85);
1354 break;
1355 default:
1356 break;
1357 }
1358}
1359
1360static bool msm_chg_check_primary_det(struct msm_otg *motg)
1361{
1362 struct otg_transceiver *otg = &motg->otg;
1363 u32 chg_det;
1364 bool ret = false;
1365
1366 switch (motg->pdata->phy_type) {
1367 case CI_45NM_INTEGRATED_PHY:
1368 chg_det = ulpi_read(otg, 0x34);
1369 ret = chg_det & (1 << 4);
1370 break;
1371 case SNPS_28NM_INTEGRATED_PHY:
1372 chg_det = ulpi_read(otg, 0x87);
1373 ret = chg_det & 1;
1374 break;
1375 default:
1376 break;
1377 }
1378 return ret;
1379}
1380
1381static void msm_chg_enable_primary_det(struct msm_otg *motg)
1382{
1383 struct otg_transceiver *otg = &motg->otg;
1384 u32 chg_det;
1385
1386 switch (motg->pdata->phy_type) {
1387 case CI_45NM_INTEGRATED_PHY:
1388 chg_det = ulpi_read(otg, 0x34);
1389 /* enable chg detection */
1390 chg_det &= ~(1 << 0);
1391 ulpi_write(otg, chg_det, 0x34);
1392 break;
1393 case SNPS_28NM_INTEGRATED_PHY:
1394 /*
1395 * Configure DP as current source, DM as current sink
1396 * and enable battery charging comparators.
1397 */
1398 ulpi_write(otg, 0x2, 0x85);
1399 ulpi_write(otg, 0x1, 0x85);
1400 break;
1401 default:
1402 break;
1403 }
1404}
1405
1406static bool msm_chg_check_dcd(struct msm_otg *motg)
1407{
1408 struct otg_transceiver *otg = &motg->otg;
1409 u32 line_state;
1410 bool ret = false;
1411
1412 switch (motg->pdata->phy_type) {
1413 case CI_45NM_INTEGRATED_PHY:
1414 line_state = ulpi_read(otg, 0x15);
1415 ret = !(line_state & 1);
1416 break;
1417 case SNPS_28NM_INTEGRATED_PHY:
1418 line_state = ulpi_read(otg, 0x87);
1419 ret = line_state & 2;
1420 break;
1421 default:
1422 break;
1423 }
1424 return ret;
1425}
1426
1427static void msm_chg_disable_dcd(struct msm_otg *motg)
1428{
1429 struct otg_transceiver *otg = &motg->otg;
1430 u32 chg_det;
1431
1432 switch (motg->pdata->phy_type) {
1433 case CI_45NM_INTEGRATED_PHY:
1434 chg_det = ulpi_read(otg, 0x34);
1435 chg_det &= ~(1 << 5);
1436 ulpi_write(otg, chg_det, 0x34);
1437 break;
1438 case SNPS_28NM_INTEGRATED_PHY:
1439 ulpi_write(otg, 0x10, 0x86);
1440 break;
1441 default:
1442 break;
1443 }
1444}
1445
1446static void msm_chg_enable_dcd(struct msm_otg *motg)
1447{
1448 struct otg_transceiver *otg = &motg->otg;
1449 u32 chg_det;
1450
1451 switch (motg->pdata->phy_type) {
1452 case CI_45NM_INTEGRATED_PHY:
1453 chg_det = ulpi_read(otg, 0x34);
1454 /* Turn on D+ current source */
1455 chg_det |= (1 << 5);
1456 ulpi_write(otg, chg_det, 0x34);
1457 break;
1458 case SNPS_28NM_INTEGRATED_PHY:
1459 /* Data contact detection enable */
1460 ulpi_write(otg, 0x10, 0x85);
1461 break;
1462 default:
1463 break;
1464 }
1465}
1466
1467static void msm_chg_block_on(struct msm_otg *motg)
1468{
1469 struct otg_transceiver *otg = &motg->otg;
1470 u32 func_ctrl, chg_det;
1471
1472 /* put the controller in non-driving mode */
1473 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1474 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1475 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1476 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1477
1478 switch (motg->pdata->phy_type) {
1479 case CI_45NM_INTEGRATED_PHY:
1480 chg_det = ulpi_read(otg, 0x34);
1481 /* control chg block via ULPI */
1482 chg_det &= ~(1 << 3);
1483 ulpi_write(otg, chg_det, 0x34);
1484 /* Turn on chg detect block */
1485 chg_det &= ~(1 << 1);
1486 ulpi_write(otg, chg_det, 0x34);
1487 udelay(20);
1488 break;
1489 case SNPS_28NM_INTEGRATED_PHY:
1490 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301491 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301492 /* Clear alt interrupt latch and enable bits */
1493 ulpi_write(otg, 0x1F, 0x92);
1494 ulpi_write(otg, 0x1F, 0x95);
1495 udelay(100);
1496 break;
1497 default:
1498 break;
1499 }
1500}
1501
1502static void msm_chg_block_off(struct msm_otg *motg)
1503{
1504 struct otg_transceiver *otg = &motg->otg;
1505 u32 func_ctrl, chg_det;
1506
1507 switch (motg->pdata->phy_type) {
1508 case CI_45NM_INTEGRATED_PHY:
1509 chg_det = ulpi_read(otg, 0x34);
1510 /* Turn off charger block */
1511 chg_det |= ~(1 << 1);
1512 ulpi_write(otg, chg_det, 0x34);
1513 break;
1514 case SNPS_28NM_INTEGRATED_PHY:
1515 /* Clear charger detecting control bits */
1516 ulpi_write(otg, 0x3F, 0x86);
1517 /* Clear alt interrupt latch and enable bits */
1518 ulpi_write(otg, 0x1F, 0x92);
1519 ulpi_write(otg, 0x1F, 0x95);
1520 break;
1521 default:
1522 break;
1523 }
1524
1525 /* put the controller in normal mode */
1526 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1527 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1528 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1529 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1530}
1531
Anji jonnalad270e2d2011-08-09 11:28:32 +05301532static const char *chg_to_string(enum usb_chg_type chg_type)
1533{
1534 switch (chg_type) {
1535 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1536 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1537 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1538 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1539 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1540 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1541 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1542 default: return "INVALID_CHARGER";
1543 }
1544}
1545
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301546#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1547#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1548#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1549#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1550static void msm_chg_detect_work(struct work_struct *w)
1551{
1552 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1553 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001554 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301555 unsigned long delay;
1556
1557 dev_dbg(otg->dev, "chg detection work\n");
1558 switch (motg->chg_state) {
1559 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301560 msm_chg_block_on(motg);
1561 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001562 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301563 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1564 motg->dcd_retries = 0;
1565 delay = MSM_CHG_DCD_POLL_TIME;
1566 break;
1567 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001568 is_aca = msm_chg_aca_detect(motg);
1569 if (is_aca) {
1570 /*
1571 * ID_A can be ACA dock too. continue
1572 * primary detection after DCD.
1573 */
1574 if (test_bit(ID_A, &motg->inputs)) {
1575 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1576 } else {
1577 delay = 0;
1578 break;
1579 }
1580 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301581 is_dcd = msm_chg_check_dcd(motg);
1582 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1583 if (is_dcd || tmout) {
1584 msm_chg_disable_dcd(motg);
1585 msm_chg_enable_primary_det(motg);
1586 delay = MSM_CHG_PRIMARY_DET_TIME;
1587 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1588 } else {
1589 delay = MSM_CHG_DCD_POLL_TIME;
1590 }
1591 break;
1592 case USB_CHG_STATE_DCD_DONE:
1593 vout = msm_chg_check_primary_det(motg);
1594 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301595 if (test_bit(ID_A, &motg->inputs)) {
1596 motg->chg_type = USB_ACA_DOCK_CHARGER;
1597 motg->chg_state = USB_CHG_STATE_DETECTED;
1598 delay = 0;
1599 break;
1600 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301601 msm_chg_enable_secondary_det(motg);
1602 delay = MSM_CHG_SECONDARY_DET_TIME;
1603 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1604 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301605 if (test_bit(ID_A, &motg->inputs)) {
1606 motg->chg_type = USB_ACA_A_CHARGER;
1607 motg->chg_state = USB_CHG_STATE_DETECTED;
1608 delay = 0;
1609 break;
1610 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301611 motg->chg_type = USB_SDP_CHARGER;
1612 motg->chg_state = USB_CHG_STATE_DETECTED;
1613 delay = 0;
1614 }
1615 break;
1616 case USB_CHG_STATE_PRIMARY_DONE:
1617 vout = msm_chg_check_secondary_det(motg);
1618 if (vout)
1619 motg->chg_type = USB_DCP_CHARGER;
1620 else
1621 motg->chg_type = USB_CDP_CHARGER;
1622 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1623 /* fall through */
1624 case USB_CHG_STATE_SECONDARY_DONE:
1625 motg->chg_state = USB_CHG_STATE_DETECTED;
1626 case USB_CHG_STATE_DETECTED:
1627 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001628 msm_chg_enable_aca_det(motg);
1629 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301630 dev_dbg(otg->dev, "chg_type = %s\n",
1631 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301632 schedule_work(&motg->sm_work);
1633 return;
1634 default:
1635 return;
1636 }
1637
1638 schedule_delayed_work(&motg->chg_work, delay);
1639}
1640
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301641/*
1642 * We support OTG, Peripheral only and Host only configurations. In case
1643 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1644 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1645 * enabled when switch is controlled by user and default mode is supplied
1646 * by board file, which can be changed by userspace later.
1647 */
1648static void msm_otg_init_sm(struct msm_otg *motg)
1649{
1650 struct msm_otg_platform_data *pdata = motg->pdata;
1651 u32 otgsc = readl(USB_OTGSC);
1652
1653 switch (pdata->mode) {
1654 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301656 if (pdata->default_mode == USB_HOST) {
1657 clear_bit(ID, &motg->inputs);
1658 } else if (pdata->default_mode == USB_PERIPHERAL) {
1659 set_bit(ID, &motg->inputs);
1660 set_bit(B_SESS_VLD, &motg->inputs);
1661 } else {
1662 set_bit(ID, &motg->inputs);
1663 clear_bit(B_SESS_VLD, &motg->inputs);
1664 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301665 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1666 if (otgsc & OTGSC_ID)
1667 set_bit(ID, &motg->inputs);
1668 else
1669 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 if (otgsc & OTGSC_BSV)
1671 set_bit(B_SESS_VLD, &motg->inputs);
1672 else
1673 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301674 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1675 if (irq_read_line(motg->pdata->pmic_id_irq))
1676 set_bit(ID, &motg->inputs);
1677 else
1678 clear_bit(ID, &motg->inputs);
1679
1680 /*
1681 * VBUS initial state is reported after PMIC
1682 * driver initialization. Wait for it.
1683 */
1684 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301685 }
1686 break;
1687 case USB_HOST:
1688 clear_bit(ID, &motg->inputs);
1689 break;
1690 case USB_PERIPHERAL:
1691 set_bit(ID, &motg->inputs);
1692 if (otgsc & OTGSC_BSV)
1693 set_bit(B_SESS_VLD, &motg->inputs);
1694 else
1695 clear_bit(B_SESS_VLD, &motg->inputs);
1696 break;
1697 default:
1698 break;
1699 }
1700}
1701
1702static void msm_otg_sm_work(struct work_struct *w)
1703{
1704 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1705 struct otg_transceiver *otg = &motg->otg;
1706
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301707 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301708 switch (otg->state) {
1709 case OTG_STATE_UNDEFINED:
1710 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1711 msm_otg_reset(otg);
1712 msm_otg_init_sm(motg);
1713 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301714 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1715 test_bit(ID, &motg->inputs)) {
1716 pm_runtime_put_noidle(otg->dev);
1717 pm_runtime_suspend(otg->dev);
1718 break;
1719 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301720 /* FALL THROUGH */
1721 case OTG_STATE_B_IDLE:
1722 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001723 if ((!test_bit(ID, &motg->inputs) ||
1724 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1726 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301727 IDEV_ACA_CHG_MAX);
1728 else if (test_bit(ID_A, &motg->inputs))
1729 msm_otg_notify_charger(motg,
1730 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301731 else
1732 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301733 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301734 /*
1735 * Link can not generate PHY_ALT interrupt
1736 * in host mode when no device is attached
1737 * to the port. It is also observed PHY_ALT
1738 * interrupt missing upon Micro-A cable disconnect.
1739 * Hence disable PHY_ALT interrupt and perform
1740 * polling to detect RID change.
1741 */
1742 msm_chg_enable_aca_det(motg);
1743 msm_chg_disable_aca_intr(motg);
1744 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301745 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301746 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1747 switch (motg->chg_state) {
1748 case USB_CHG_STATE_UNDEFINED:
1749 msm_chg_detect_work(&motg->chg_work.work);
1750 break;
1751 case USB_CHG_STATE_DETECTED:
1752 switch (motg->chg_type) {
1753 case USB_DCP_CHARGER:
1754 msm_otg_notify_charger(motg,
1755 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301756 pm_runtime_put_noidle(otg->dev);
1757 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301759 case USB_ACA_B_CHARGER:
1760 msm_otg_notify_charger(motg,
1761 IDEV_ACA_CHG_MAX);
1762 /*
1763 * (ID_B --> ID_C) PHY_ALT interrupt can
1764 * not be detected in LPM.
1765 */
1766 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301767 case USB_CDP_CHARGER:
1768 msm_otg_notify_charger(motg,
1769 IDEV_CHG_MAX);
1770 msm_otg_start_peripheral(otg, 1);
1771 otg->state = OTG_STATE_B_PERIPHERAL;
1772 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301773 case USB_ACA_C_CHARGER:
1774 msm_otg_notify_charger(motg,
1775 IDEV_ACA_CHG_MAX);
1776 msm_otg_start_peripheral(otg, 1);
1777 otg->state = OTG_STATE_B_PERIPHERAL;
1778 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301779 case USB_SDP_CHARGER:
1780 msm_otg_notify_charger(motg, IUNIT);
1781 msm_otg_start_peripheral(otg, 1);
1782 otg->state = OTG_STATE_B_PERIPHERAL;
1783 break;
1784 default:
1785 break;
1786 }
1787 break;
1788 default:
1789 break;
1790 }
1791 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301792 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301793 msm_otg_notify_charger(motg, 0);
1794 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1795 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301796 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301797 pm_runtime_put_noidle(otg->dev);
1798 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301799 }
1800 break;
1801 case OTG_STATE_B_PERIPHERAL:
1802 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1803 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001804 !test_bit(ID, &motg->inputs) ||
1805 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301806 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 schedule_work(w);
1809 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301810 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001811 }
1812 break;
1813 case OTG_STATE_A_HOST:
1814 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1815 if (test_bit(ID, &motg->inputs) &&
1816 !test_bit(ID_A, &motg->inputs)) {
1817 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301818 msm_hsusb_vbus_power(motg, 0);
1819 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301820 /*
1821 * Exit point of host mode.
1822 *
1823 * 1. Micro-A cable disconnect: Just schedule
1824 * the work. PHY is reset in B_IDLE and LPM
1825 * is allowed.
1826 * 2. ID_GND --> ID_B: No need to reset the PHY.
1827 * HCD core clears all PORTSC bits and initializes
1828 * the controller to host mode in remove_hcd.
1829 * Restore PORTSC transceiver select bits (ULPI)
1830 * and reset the controller to change MODE bits.
1831 * PHY_ALT interrupt can not occur in host mode.
1832 */
1833 del_timer_sync(&motg->id_timer);
1834 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1835 msm_otg_link_reset(motg);
1836 msm_chg_enable_aca_intr(motg);
1837 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301838 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301839 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301841 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301843 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001845 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301846 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301847 }
1848 break;
1849 default:
1850 break;
1851 }
1852}
1853
1854static irqreturn_t msm_otg_irq(int irq, void *data)
1855{
1856 struct msm_otg *motg = data;
1857 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301859
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301860 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301861 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301862 disable_irq_nosync(irq);
1863 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301864 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301865 return IRQ_HANDLED;
1866 }
1867
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001868 usbsts = readl(USB_USBSTS);
1869 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301870 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001871 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301872 if (msm_chg_check_aca_intr(motg)) {
1873 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001874 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301875 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001876 return IRQ_HANDLED;
1877 }
1878
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301879 otgsc = readl(USB_OTGSC);
1880 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1881 return IRQ_NONE;
1882
1883 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301884 if (otgsc & OTGSC_ID) {
1885 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301886 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301887 } else {
1888 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301889 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301890 msm_chg_enable_aca_det(motg);
1891 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301893 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301894 if (otgsc & OTGSC_BSV) {
1895 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301896 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301897 } else {
1898 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301899 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301900 msm_chg_check_aca_intr(motg);
1901 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001902 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301903 }
1904
1905 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001906 return IRQ_HANDLED;
1907}
1908
1909static void msm_otg_set_vbus_state(int online)
1910{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301911 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001912 struct msm_otg *motg = the_msm_otg;
1913
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301914 if (online) {
1915 pr_debug("PMIC: BSV set\n");
1916 set_bit(B_SESS_VLD, &motg->inputs);
1917 } else {
1918 pr_debug("PMIC: BSV clear\n");
1919 clear_bit(B_SESS_VLD, &motg->inputs);
1920 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301922 if (!init) {
1923 init = true;
1924 complete(&pmic_vbus_init);
1925 pr_debug("PMIC: BSV init complete\n");
1926 return;
1927 }
1928
1929 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930}
1931
1932static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1933{
1934 struct msm_otg *motg = data;
1935
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301936 if (aca_id_turned_on)
1937 return IRQ_HANDLED;
1938
1939 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1940 pr_debug("PMIC: ID set\n");
1941 set_bit(ID, &motg->inputs);
1942 } else {
1943 pr_debug("PMIC: ID clear\n");
1944 clear_bit(ID, &motg->inputs);
1945 }
1946
1947 if (motg->otg.state != OTG_STATE_UNDEFINED)
1948 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301950 return IRQ_HANDLED;
1951}
1952
1953static int msm_otg_mode_show(struct seq_file *s, void *unused)
1954{
1955 struct msm_otg *motg = s->private;
1956 struct otg_transceiver *otg = &motg->otg;
1957
1958 switch (otg->state) {
1959 case OTG_STATE_A_HOST:
1960 seq_printf(s, "host\n");
1961 break;
1962 case OTG_STATE_B_PERIPHERAL:
1963 seq_printf(s, "peripheral\n");
1964 break;
1965 default:
1966 seq_printf(s, "none\n");
1967 break;
1968 }
1969
1970 return 0;
1971}
1972
1973static int msm_otg_mode_open(struct inode *inode, struct file *file)
1974{
1975 return single_open(file, msm_otg_mode_show, inode->i_private);
1976}
1977
1978static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1979 size_t count, loff_t *ppos)
1980{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301981 struct seq_file *s = file->private_data;
1982 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301983 char buf[16];
1984 struct otg_transceiver *otg = &motg->otg;
1985 int status = count;
1986 enum usb_mode_type req_mode;
1987
1988 memset(buf, 0x00, sizeof(buf));
1989
1990 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1991 status = -EFAULT;
1992 goto out;
1993 }
1994
1995 if (!strncmp(buf, "host", 4)) {
1996 req_mode = USB_HOST;
1997 } else if (!strncmp(buf, "peripheral", 10)) {
1998 req_mode = USB_PERIPHERAL;
1999 } else if (!strncmp(buf, "none", 4)) {
2000 req_mode = USB_NONE;
2001 } else {
2002 status = -EINVAL;
2003 goto out;
2004 }
2005
2006 switch (req_mode) {
2007 case USB_NONE:
2008 switch (otg->state) {
2009 case OTG_STATE_A_HOST:
2010 case OTG_STATE_B_PERIPHERAL:
2011 set_bit(ID, &motg->inputs);
2012 clear_bit(B_SESS_VLD, &motg->inputs);
2013 break;
2014 default:
2015 goto out;
2016 }
2017 break;
2018 case USB_PERIPHERAL:
2019 switch (otg->state) {
2020 case OTG_STATE_B_IDLE:
2021 case OTG_STATE_A_HOST:
2022 set_bit(ID, &motg->inputs);
2023 set_bit(B_SESS_VLD, &motg->inputs);
2024 break;
2025 default:
2026 goto out;
2027 }
2028 break;
2029 case USB_HOST:
2030 switch (otg->state) {
2031 case OTG_STATE_B_IDLE:
2032 case OTG_STATE_B_PERIPHERAL:
2033 clear_bit(ID, &motg->inputs);
2034 break;
2035 default:
2036 goto out;
2037 }
2038 break;
2039 default:
2040 goto out;
2041 }
2042
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302043 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302044 schedule_work(&motg->sm_work);
2045out:
2046 return status;
2047}
2048
2049const struct file_operations msm_otg_mode_fops = {
2050 .open = msm_otg_mode_open,
2051 .read = seq_read,
2052 .write = msm_otg_mode_write,
2053 .llseek = seq_lseek,
2054 .release = single_release,
2055};
2056
Anji jonnalad270e2d2011-08-09 11:28:32 +05302057static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2058{
2059 struct msm_otg *motg = s->private;
2060
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302061 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302062 return 0;
2063}
2064
2065static int msm_otg_chg_open(struct inode *inode, struct file *file)
2066{
2067 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2068}
2069
2070const struct file_operations msm_otg_chg_fops = {
2071 .open = msm_otg_chg_open,
2072 .read = seq_read,
2073 .llseek = seq_lseek,
2074 .release = single_release,
2075};
2076
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302077static int msm_otg_aca_show(struct seq_file *s, void *unused)
2078{
2079 if (debug_aca_enabled)
2080 seq_printf(s, "enabled\n");
2081 else
2082 seq_printf(s, "disabled\n");
2083
2084 return 0;
2085}
2086
2087static int msm_otg_aca_open(struct inode *inode, struct file *file)
2088{
2089 return single_open(file, msm_otg_aca_show, inode->i_private);
2090}
2091
2092static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2093 size_t count, loff_t *ppos)
2094{
2095 char buf[8];
2096
2097 memset(buf, 0x00, sizeof(buf));
2098
2099 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2100 return -EFAULT;
2101
2102 if (!strncmp(buf, "enable", 6))
2103 debug_aca_enabled = true;
2104 else
2105 debug_aca_enabled = false;
2106
2107 return count;
2108}
2109
2110const struct file_operations msm_otg_aca_fops = {
2111 .open = msm_otg_aca_open,
2112 .read = seq_read,
2113 .write = msm_otg_aca_write,
2114 .llseek = seq_lseek,
2115 .release = single_release,
2116};
2117
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302118static struct dentry *msm_otg_dbg_root;
2119static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302120static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302121static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302122
2123static int msm_otg_debugfs_init(struct msm_otg *motg)
2124{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302125
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302126 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2127
2128 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2129 return -ENODEV;
2130
Anji jonnalad270e2d2011-08-09 11:28:32 +05302131 if (motg->pdata->mode == USB_OTG &&
2132 motg->pdata->otg_control == OTG_USER_CONTROL) {
2133
2134 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2135 S_IWUSR, msm_otg_dbg_root, motg,
2136 &msm_otg_mode_fops);
2137
2138 if (!msm_otg_dbg_mode) {
2139 debugfs_remove(msm_otg_dbg_root);
2140 msm_otg_dbg_root = NULL;
2141 return -ENODEV;
2142 }
2143 }
2144
2145 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2146 msm_otg_dbg_root, motg,
2147 &msm_otg_chg_fops);
2148
2149 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302150 debugfs_remove_recursive(msm_otg_dbg_root);
2151 return -ENODEV;
2152 }
2153
2154 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2155 msm_otg_dbg_root, motg,
2156 &msm_otg_aca_fops);
2157
2158 if (!msm_otg_dbg_aca) {
2159 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302160 return -ENODEV;
2161 }
2162
2163 return 0;
2164}
2165
2166static void msm_otg_debugfs_cleanup(void)
2167{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302168 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302169}
2170
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302171static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2172static struct platform_device *msm_otg_add_pdev(
2173 struct platform_device *ofdev, const char *name)
2174{
2175 struct platform_device *pdev;
2176 const struct resource *res = ofdev->resource;
2177 unsigned int num = ofdev->num_resources;
2178 int retval;
2179
2180 pdev = platform_device_alloc(name, -1);
2181 if (!pdev) {
2182 retval = -ENOMEM;
2183 goto error;
2184 }
2185
2186 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2187 pdev->dev.dma_mask = &msm_otg_dma_mask;
2188
2189 if (num) {
2190 retval = platform_device_add_resources(pdev, res, num);
2191 if (retval)
2192 goto error;
2193 }
2194
2195 retval = platform_device_add(pdev);
2196 if (retval)
2197 goto error;
2198
2199 return pdev;
2200
2201error:
2202 platform_device_put(pdev);
2203 return ERR_PTR(retval);
2204}
2205
2206static int msm_otg_setup_devices(struct platform_device *ofdev,
2207 enum usb_mode_type mode, bool init)
2208{
2209 const char *gadget_name = "msm_hsusb";
2210 const char *host_name = "msm_hsusb_host";
2211 static struct platform_device *gadget_pdev;
2212 static struct platform_device *host_pdev;
2213 int retval = 0;
2214
2215 if (!init) {
2216 if (gadget_pdev)
2217 platform_device_unregister(gadget_pdev);
2218 if (host_pdev)
2219 platform_device_unregister(host_pdev);
2220 return 0;
2221 }
2222
2223 switch (mode) {
2224 case USB_OTG:
2225 /* fall through */
2226 case USB_PERIPHERAL:
2227 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2228 if (IS_ERR(gadget_pdev)) {
2229 retval = PTR_ERR(gadget_pdev);
2230 break;
2231 }
2232 if (mode == USB_PERIPHERAL)
2233 break;
2234 /* fall through */
2235 case USB_HOST:
2236 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2237 if (IS_ERR(host_pdev)) {
2238 retval = PTR_ERR(host_pdev);
2239 if (mode == USB_OTG)
2240 platform_device_unregister(gadget_pdev);
2241 }
2242 break;
2243 default:
2244 break;
2245 }
2246
2247 return retval;
2248}
2249
2250struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2251{
2252 struct device_node *node = pdev->dev.of_node;
2253 struct msm_otg_platform_data *pdata;
2254 int len = 0;
2255
2256 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2257 if (!pdata) {
2258 pr_err("unable to allocate platform data\n");
2259 return NULL;
2260 }
2261 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2262 if (len) {
2263 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2264 if (!pdata->phy_init_seq)
2265 return NULL;
2266 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2267 pdata->phy_init_seq,
2268 len/sizeof(*pdata->phy_init_seq));
2269 }
2270 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2271 &pdata->power_budget);
2272 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2273 &pdata->mode);
2274 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2275 &pdata->otg_control);
2276 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2277 &pdata->default_mode);
2278 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2279 &pdata->phy_type);
2280 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2281 &pdata->pmic_id_irq);
2282 of_property_read_string(node, "qcom,hsusb-otg-pclk-src-name",
2283 &pdata->pclk_src_name);
2284 return pdata;
2285}
2286
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302287static int __init msm_otg_probe(struct platform_device *pdev)
2288{
2289 int ret = 0;
2290 struct resource *res;
2291 struct msm_otg *motg;
2292 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302293 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302294
2295 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302296
2297 if (pdev->dev.of_node) {
2298 dev_dbg(&pdev->dev, "device tree enabled\n");
2299 pdata = msm_otg_dt_to_pdata(pdev);
2300 if (!pdata)
2301 return -ENOMEM;
2302 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2303 if (ret) {
2304 dev_err(&pdev->dev, "devices setup failed\n");
2305 return ret;
2306 }
2307 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302308 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2309 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302310 } else {
2311 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302312 }
2313
2314 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2315 if (!motg) {
2316 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2317 return -ENOMEM;
2318 }
2319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002320 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302321 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302322 otg = &motg->otg;
2323 otg->dev = &pdev->dev;
2324
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302325 /*
2326 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2327 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2328 * PMIC can detect ACA ID_GND and generate an interrupt.
2329 */
2330 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2331 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2332 ret = -EINVAL;
2333 goto free_motg;
2334 }
2335
Amit Blay02eff132011-09-21 16:46:24 +03002336 /* Some targets don't support PHY clock. */
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302337 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002338 if (IS_ERR(motg->phy_reset_clk))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302339 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302340
2341 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
2342 if (IS_ERR(motg->clk)) {
2343 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
2344 ret = PTR_ERR(motg->clk);
2345 goto put_phy_reset_clk;
2346 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302347 clk_set_rate(motg->clk, 60000000);
2348
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302349 /* pm qos request to prevent apps idle power collapse */
2350 if (motg->pdata->swfi_latency)
2351 pm_qos_add_request(&motg->pm_qos_req_dma,
2352 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302353 /*
2354 * If USB Core is running its protocol engine based on CORE CLK,
2355 * CORE CLK must be running at >55Mhz for correct HSUSB
2356 * operation and USB core cannot tolerate frequency changes on
2357 * CORE CLK. For such USB cores, vote for maximum clk frequency
2358 * on pclk source
2359 */
2360 if (motg->pdata->pclk_src_name) {
2361 motg->pclk_src = clk_get(&pdev->dev,
2362 motg->pdata->pclk_src_name);
2363 if (IS_ERR(motg->pclk_src))
2364 goto put_clk;
2365 clk_set_rate(motg->pclk_src, INT_MAX);
2366 clk_enable(motg->pclk_src);
2367 } else
2368 motg->pclk_src = ERR_PTR(-ENOENT);
2369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302370 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
2371 if (IS_ERR(motg->pclk)) {
2372 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
2373 ret = PTR_ERR(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302374 goto put_pclk_src;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302375 }
2376
Amit Blay02eff132011-09-21 16:46:24 +03002377 motg->system_clk = clk_get(&pdev->dev, "usb_hs_system_clk");
2378 if (!IS_ERR(motg->system_clk))
2379 clk_enable(motg->system_clk);
2380
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302381 /*
2382 * USB core clock is not present on all MSM chips. This
2383 * clock is introduced to remove the dependency on AXI
2384 * bus frequency.
2385 */
2386 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
2387 if (IS_ERR(motg->core_clk))
2388 motg->core_clk = NULL;
2389
2390 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2391 if (!res) {
2392 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2393 ret = -ENODEV;
2394 goto put_core_clk;
2395 }
2396
2397 motg->regs = ioremap(res->start, resource_size(res));
2398 if (!motg->regs) {
2399 dev_err(&pdev->dev, "ioremap failed\n");
2400 ret = -ENOMEM;
2401 goto put_core_clk;
2402 }
2403 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2404
2405 motg->irq = platform_get_irq(pdev, 0);
2406 if (!motg->irq) {
2407 dev_err(&pdev->dev, "platform_get_irq failed\n");
2408 ret = -ENODEV;
2409 goto free_regs;
2410 }
2411
Anji jonnala7da3f262011-12-02 17:22:14 -08002412 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2413 if (IS_ERR(motg->xo_handle)) {
2414 dev_err(&pdev->dev, "%s not able to get the handle "
2415 "to vote for TCXO D0 buffer\n", __func__);
2416 ret = PTR_ERR(motg->xo_handle);
2417 goto free_regs;
2418 }
2419
2420 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2421 if (ret) {
2422 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2423 "D0 buffer%d\n", __func__, ret);
2424 goto free_xo_handle;
2425 }
2426
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302427 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302428
2429 ret = msm_hsusb_init_vddcx(motg, 1);
2430 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002431 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002432 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302433 }
2434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002435 ret = msm_hsusb_config_vddcx(1);
2436 if (ret) {
2437 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2438 goto free_init_vddcx;
2439 }
2440
Anji jonnala11aa5c42011-05-04 10:19:48 +05302441 ret = msm_hsusb_ldo_init(motg, 1);
2442 if (ret) {
2443 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002444 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302445 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002446
2447 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302448 if (ret) {
2449 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302451 }
2452
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302453 if (motg->core_clk)
2454 clk_enable(motg->core_clk);
2455
2456 writel(0, USB_USBINTR);
2457 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002458 /* Ensure that above STOREs are completed before enabling interrupts */
2459 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302460
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302462 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302463 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302464 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2465 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302466 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2467 "msm_otg", motg);
2468 if (ret) {
2469 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002470 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302471 }
2472
2473 otg->init = msm_otg_reset;
2474 otg->set_host = msm_otg_set_host;
2475 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302476 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302477 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302478
2479 otg->io_ops = &msm_otg_io_ops;
2480
2481 ret = otg_set_transceiver(&motg->otg);
2482 if (ret) {
2483 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2484 goto free_irq;
2485 }
2486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2488 if (motg->pdata->pmic_id_irq) {
2489 ret = request_irq(motg->pdata->pmic_id_irq,
2490 msm_pmic_id_irq,
2491 IRQF_TRIGGER_RISING |
2492 IRQF_TRIGGER_FALLING,
2493 "msm_otg", motg);
2494 if (ret) {
2495 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2496 goto remove_otg;
2497 }
2498 } else {
2499 ret = -ENODEV;
2500 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2501 goto remove_otg;
2502 }
2503 }
2504
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302505 msm_hsusb_mhl_switch_enable(motg, 1);
2506
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302507 platform_set_drvdata(pdev, motg);
2508 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302510
Anji jonnalad270e2d2011-08-09 11:28:32 +05302511 ret = msm_otg_debugfs_init(motg);
2512 if (ret)
2513 dev_dbg(&pdev->dev, "mode debugfs file is"
2514 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302515
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2517 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2518
Amit Blay58b31472011-11-18 09:39:39 +02002519 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2520 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521 motg->pdata->pmic_id_irq)
Amit Blay58b31472011-11-18 09:39:39 +02002522 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523 ALLOW_PHY_RETENTION |
2524 ALLOW_PHY_COMP_DISABLE;
2525
Amit Blay58b31472011-11-18 09:39:39 +02002526 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2527 motg->caps = ALLOW_PHY_RETENTION;
2528 }
2529
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002530 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302531 pm_runtime_set_active(&pdev->dev);
2532 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302533
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302534 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002535
2536remove_otg:
2537 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302538free_irq:
2539 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540destroy_wlock:
2541 wake_lock_destroy(&motg->wlock);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302542 clk_disable(motg->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543 msm_hsusb_ldo_enable(motg, 0);
2544free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302545 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302547 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002548devote_xo_handle:
2549 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2550free_xo_handle:
2551 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302552free_regs:
2553 iounmap(motg->regs);
2554put_core_clk:
2555 if (motg->core_clk)
2556 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002557
2558 if (!IS_ERR(motg->system_clk)) {
2559 clk_disable(motg->system_clk);
2560 clk_put(motg->system_clk);
2561 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302562put_pclk_src:
2563 if (!IS_ERR(motg->pclk_src)) {
2564 clk_disable(motg->pclk_src);
2565 clk_put(motg->pclk_src);
2566 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302567put_clk:
2568 clk_put(motg->clk);
2569put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002570 if (!IS_ERR(motg->phy_reset_clk))
2571 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302572free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302573 if (motg->pdata->swfi_latency)
2574 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302575 kfree(motg);
2576 return ret;
2577}
2578
2579static int __devexit msm_otg_remove(struct platform_device *pdev)
2580{
2581 struct msm_otg *motg = platform_get_drvdata(pdev);
2582 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302583 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302584
2585 if (otg->host || otg->gadget)
2586 return -EBUSY;
2587
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302588 if (pdev->dev.of_node)
2589 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2591 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302592 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302593 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302594 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302595
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302596 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302597
2598 device_init_wakeup(&pdev->dev, 0);
2599 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302601
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302602 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603 if (motg->pdata->pmic_id_irq)
2604 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302605 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302606 free_irq(motg->irq, motg);
2607
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302608 /*
2609 * Put PHY in low power mode.
2610 */
2611 ulpi_read(otg, 0x14);
2612 ulpi_write(otg, 0x08, 0x09);
2613
2614 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2615 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2616 if (readl(USB_PORTSC) & PORTSC_PHCD)
2617 break;
2618 udelay(1);
2619 cnt++;
2620 }
2621 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2622 dev_err(otg->dev, "Unable to suspend PHY\n");
2623
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302624 clk_disable(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302625 if (motg->core_clk)
2626 clk_disable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +02002627 if (!IS_ERR(motg->system_clk))
2628 clk_disable(motg->system_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302629 if (!IS_ERR(motg->pclk_src)) {
2630 clk_disable(motg->pclk_src);
2631 clk_put(motg->pclk_src);
2632 }
Anji jonnala7da3f262011-12-02 17:22:14 -08002633 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002634 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302635 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302637
2638 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302639 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302640
Amit Blay02eff132011-09-21 16:46:24 +03002641 if (!IS_ERR(motg->phy_reset_clk))
2642 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302643 clk_put(motg->pclk);
2644 clk_put(motg->clk);
2645 if (motg->core_clk)
2646 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002647 if (!IS_ERR(motg->system_clk))
2648 clk_put(motg->system_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302649
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302650 if (motg->pdata->swfi_latency)
2651 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302652
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302653 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302654 return 0;
2655}
2656
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302657#ifdef CONFIG_PM_RUNTIME
2658static int msm_otg_runtime_idle(struct device *dev)
2659{
2660 struct msm_otg *motg = dev_get_drvdata(dev);
2661 struct otg_transceiver *otg = &motg->otg;
2662
2663 dev_dbg(dev, "OTG runtime idle\n");
2664
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302665 if (otg->state == OTG_STATE_UNDEFINED)
2666 return -EAGAIN;
2667 else
2668 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302669}
2670
2671static int msm_otg_runtime_suspend(struct device *dev)
2672{
2673 struct msm_otg *motg = dev_get_drvdata(dev);
2674
2675 dev_dbg(dev, "OTG runtime suspend\n");
2676 return msm_otg_suspend(motg);
2677}
2678
2679static int msm_otg_runtime_resume(struct device *dev)
2680{
2681 struct msm_otg *motg = dev_get_drvdata(dev);
2682
2683 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302684 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302685 return msm_otg_resume(motg);
2686}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302687#endif
2688
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302689#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302690static int msm_otg_pm_suspend(struct device *dev)
2691{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302692 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302693
2694 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302695
2696#ifdef CONFIG_PM_RUNTIME
2697 ret = pm_runtime_suspend(dev);
2698 if (ret > 0)
2699 ret = 0;
2700#else
2701 ret = msm_otg_suspend(dev_get_drvdata(dev));
2702#endif
2703 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302704}
2705
2706static int msm_otg_pm_resume(struct device *dev)
2707{
2708 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302709
2710 dev_dbg(dev, "OTG PM resume\n");
2711
Manu Gautamf284c052011-09-08 16:52:48 +05302712#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302713 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302714 * Do not resume hardware as part of system resume,
2715 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302716 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002717 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302718#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302719
Manu Gautamf284c052011-09-08 16:52:48 +05302720 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302721}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302722#endif
2723
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302724#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302725static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302726 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2727 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2728 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302729};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302730#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302731
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302732static struct of_device_id msm_otg_dt_match[] = {
2733 { .compatible = "qcom,hsusb-otg",
2734 },
2735 {}
2736};
2737
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302738static struct platform_driver msm_otg_driver = {
2739 .remove = __devexit_p(msm_otg_remove),
2740 .driver = {
2741 .name = DRIVER_NAME,
2742 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302743#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302744 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302745#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302746 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302747 },
2748};
2749
2750static int __init msm_otg_init(void)
2751{
2752 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2753}
2754
2755static void __exit msm_otg_exit(void)
2756{
2757 platform_driver_unregister(&msm_otg_driver);
2758}
2759
2760module_init(msm_otg_init);
2761module_exit(msm_otg_exit);
2762
2763MODULE_LICENSE("GPL v2");
2764MODULE_DESCRIPTION("MSM USB transceiver driver");