blob: 4932551a075ec13b62fa412019550f06597ded93 [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
Ofir Cohen4da266f2012-01-03 10:19:29 +0200526 /*
527 * USB PHY and Link reset also reset the USB BAM.
528 * Thus perform reset operation only once to avoid
529 * USB BAM reset on other cases e.g. USB cable disconnections.
530 */
531 if (pdata->disable_reset_on_disconnect) {
532 if (motg->reset_counter)
533 return 0;
534 else
535 motg->reset_counter++;
536 }
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530539 ret = msm_otg_phy_reset(motg);
540 if (ret) {
541 dev_err(otg->dev, "phy_reset failed\n");
542 return ret;
543 }
544
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530545 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 ret = msm_otg_link_reset(motg);
547 if (ret) {
548 dev_err(otg->dev, "link reset failed\n");
549 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530550 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530552
553 ulpi_init(motg);
554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 /* Ensure that RESET operation is completed before turning off clock */
556 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 clk_disable(motg->clk);
559
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530560 if (pdata->otg_control == OTG_PHY_CONTROL) {
561 val = readl_relaxed(USB_OTGSC);
562 if (pdata->mode == USB_OTG) {
563 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
564 val |= OTGSC_IDIE | OTGSC_BSVIE;
565 } else if (pdata->mode == USB_PERIPHERAL) {
566 ulpi_val = ULPI_INT_SESS_VALID;
567 val |= OTGSC_BSVIE;
568 }
569 writel_relaxed(val, USB_OTGSC);
570 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530574 return 0;
575}
576
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530577static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
578{
579 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
580
581 /*
582 * Allow bus suspend only for host mode. Device mode bus suspend
583 * is not implemented yet.
584 */
585 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530586 /*
587 * ID_GND --> ID_A transition can not be detected in LPM.
588 * Disallow host bus suspend when ACA is enabled.
589 */
590 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530591 pm_runtime_put(otg->dev);
592 else
593 pm_runtime_resume(otg->dev);
594 }
595
596 return 0;
597}
598
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530599#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530600#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
601
602#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530603static int msm_otg_suspend(struct msm_otg *motg)
604{
605 struct otg_transceiver *otg = &motg->otg;
606 struct usb_bus *bus = otg->host;
607 struct msm_otg_platform_data *pdata = motg->pdata;
608 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530609 bool host_bus_suspend;
610 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800611 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530612 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530613
614 if (atomic_read(&motg->in_lpm))
615 return 0;
616
617 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530618 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530620 * Chipidea 45-nm PHY suspend sequence:
621 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530622 * Interrupt Latch Register auto-clear feature is not present
623 * in all PHY versions. Latch register is clear on read type.
624 * Clear latch register to avoid spurious wakeup from
625 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530626 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530627 * PHY comparators are disabled when PHY enters into low power
628 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
629 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
630 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530631 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530632 * PLL is not turned off when PHY enters into low power mode (LPM).
633 * Disable PLL for maximum power savings.
634 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530635
636 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
637 ulpi_read(otg, 0x14);
638 if (pdata->otg_control == OTG_PHY_CONTROL)
639 ulpi_write(otg, 0x01, 0x30);
640 ulpi_write(otg, 0x08, 0x09);
641 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530642
643 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 * Turn off the OTG comparators, if depends on PMIC for
645 * VBUS and ID notifications.
646 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530647 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 ulpi_write(otg, OTG_COMP_DISABLE,
649 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
650 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
651 }
652
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530653 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530654 * PHY may take some time or even fail to enter into low power
655 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
656 * in failure case.
657 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530658 portsc = readl_relaxed(USB_PORTSC);
659 if (!(portsc & PORTSC_PHCD)) {
660 writel_relaxed(portsc | PORTSC_PHCD,
661 USB_PORTSC);
662 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
663 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
664 break;
665 udelay(1);
666 cnt++;
667 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530668 }
669
670 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
671 dev_err(otg->dev, "Unable to suspend PHY\n");
672 msm_otg_reset(otg);
673 enable_irq(motg->irq);
674 return -ETIMEDOUT;
675 }
676
677 /*
678 * PHY has capability to generate interrupt asynchronously in low
679 * power mode (LPM). This interrupt is level triggered. So USB IRQ
680 * line must be disabled till async interrupt enable bit is cleared
681 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
682 * block data communication from PHY.
683 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530684 cmd_val = readl_relaxed(USB_USBCMD);
685 if (host_bus_suspend)
686 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
687 else
688 cmd_val |= ULPI_STP_CTRL;
689 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530690
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530691 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200692 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
693 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
694 /* Enable PHY HV interrupts to wake MPM/Link */
695 phy_ctrl_val |=
696 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
697
698 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 motg->lpm_flags |= PHY_RETENTIONED;
700 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 /* Ensure that above operation is completed before turning off clocks */
703 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530704 clk_disable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530705 if (motg->core_clk)
706 clk_disable(motg->core_clk);
707
Amit Blay137575f2011-11-06 15:20:54 +0200708 if (!IS_ERR(motg->system_clk))
709 clk_disable(motg->system_clk);
710
Anji jonnala0f73cac2011-05-04 10:19:46 +0530711 if (!IS_ERR(motg->pclk_src))
712 clk_disable(motg->pclk_src);
713
Anji jonnala7da3f262011-12-02 17:22:14 -0800714 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
715 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
716 if (ret)
717 dev_err(otg->dev, "%s failed to devote for "
718 "TCXO D0 buffer%d\n", __func__, ret);
719
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530720 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700721 msm_hsusb_ldo_enable(motg, 0);
722 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530723 }
724
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530725 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530727 msm_hsusb_mhl_switch_enable(motg, 0);
728 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729
730 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530731 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 if (motg->pdata->pmic_id_irq)
733 enable_irq_wake(motg->pdata->pmic_id_irq);
734 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530735 if (bus)
736 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
737
738 atomic_set(&motg->in_lpm, 1);
739 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700740 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741
742 dev_info(otg->dev, "USB in low power mode\n");
743
744 return 0;
745}
746
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530747static int msm_otg_resume(struct msm_otg *motg)
748{
749 struct otg_transceiver *otg = &motg->otg;
750 struct usb_bus *bus = otg->host;
751 int cnt = 0;
752 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200753 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800754 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530755
756 if (!atomic_read(&motg->in_lpm))
757 return 0;
758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700759 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800760
761 /* Vote for TCXO when waking up the phy */
762 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
763 if (ret)
764 dev_err(otg->dev, "%s failed to vote for "
765 "TCXO D0 buffer%d\n", __func__, ret);
766
Anji jonnala0f73cac2011-05-04 10:19:46 +0530767 if (!IS_ERR(motg->pclk_src))
768 clk_enable(motg->pclk_src);
769
Amit Blay137575f2011-11-06 15:20:54 +0200770 if (!IS_ERR(motg->system_clk))
771 clk_enable(motg->system_clk);
772
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530773 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530774 if (motg->core_clk)
775 clk_enable(motg->core_clk);
776
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700777 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
778 msm_hsusb_ldo_enable(motg, 1);
779 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
780 }
781
782 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530783 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530784 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200785 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
786 phy_ctrl_val |= PHY_RETEN;
787 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
788 /* Disable PHY HV interrupts */
789 phy_ctrl_val &=
790 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
791 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530793 }
794
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530795 temp = readl(USB_USBCMD);
796 temp &= ~ASYNC_INTR_CTRL;
797 temp &= ~ULPI_STP_CTRL;
798 writel(temp, USB_USBCMD);
799
800 /*
801 * PHY comes out of low power mode (LPM) in case of wakeup
802 * from asynchronous interrupt.
803 */
804 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
805 goto skip_phy_resume;
806
807 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
808 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
809 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
810 break;
811 udelay(1);
812 cnt++;
813 }
814
815 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
816 /*
817 * This is a fatal error. Reset the link and
818 * PHY. USB state can not be restored. Re-insertion
819 * of USB cable is the only way to get USB working.
820 */
821 dev_err(otg->dev, "Unable to resume USB."
822 "Re-plugin the cable\n");
823 msm_otg_reset(otg);
824 }
825
826skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 /* Turn on the OTG comparators on resume */
828 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
829 ulpi_write(otg, OTG_COMP_DISABLE,
830 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
831 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
832 }
833 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700835 if (motg->pdata->pmic_id_irq)
836 disable_irq_wake(motg->pdata->pmic_id_irq);
837 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530838 if (bus)
839 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
840
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530841 atomic_set(&motg->in_lpm, 0);
842
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530843 if (motg->async_int) {
844 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530845 enable_irq(motg->irq);
846 }
847
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530848 dev_info(otg->dev, "USB exited from low power mode\n");
849
850 return 0;
851}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530852#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530853
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530854static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
855{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530856 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
857 motg->chg_type == USB_ACA_A_CHARGER ||
858 motg->chg_type == USB_ACA_B_CHARGER ||
859 motg->chg_type == USB_ACA_C_CHARGER) &&
860 mA > IDEV_ACA_CHG_LIMIT)
861 mA = IDEV_ACA_CHG_LIMIT;
862
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530863 if (motg->cur_power == mA)
864 return;
865
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530866 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530868 motg->cur_power = mA;
869}
870
871static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
872{
873 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
874
875 /*
876 * Gadget driver uses set_power method to notify about the
877 * available current based on suspend/configured states.
878 *
879 * IDEV_CHG can be drawn irrespective of suspend/un-configured
880 * states when CDP/ACA is connected.
881 */
882 if (motg->chg_type == USB_SDP_CHARGER)
883 msm_otg_notify_charger(motg, mA);
884
885 return 0;
886}
887
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530888static void msm_otg_start_host(struct otg_transceiver *otg, int on)
889{
890 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
891 struct msm_otg_platform_data *pdata = motg->pdata;
892 struct usb_hcd *hcd;
893
894 if (!otg->host)
895 return;
896
897 hcd = bus_to_hcd(otg->host);
898
899 if (on) {
900 dev_dbg(otg->dev, "host on\n");
901
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530902 /*
903 * Some boards have a switch cotrolled by gpio
904 * to enable/disable internal HUB. Enable internal
905 * HUB before kicking the host.
906 */
907 if (pdata->setup_gpio)
908 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530909 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530910 } else {
911 dev_dbg(otg->dev, "host off\n");
912
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530913 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530914 /* HCD core reset all bits of PORTSC. select ULPI phy */
915 writel_relaxed(0x80000000, USB_PORTSC);
916
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530917 if (pdata->setup_gpio)
918 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530919 }
920}
921
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700922static int msm_otg_usbdev_notify(struct notifier_block *self,
923 unsigned long action, void *priv)
924{
925 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530926 struct usb_device *udev = priv;
927
928 if (!aca_enabled())
929 goto out;
930
931 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
932 goto out;
933
934 if (udev->bus != motg->otg.host)
935 goto out;
936 /*
937 * Interested in devices connected directly to the root hub.
938 * ACA dock can supply IDEV_CHG irrespective devices connected
939 * on the accessory port.
940 */
941 if (!udev->parent || udev->parent->parent ||
942 motg->chg_type == USB_ACA_DOCK_CHARGER)
943 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944
945 switch (action) {
946 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530947 usb_disable_autosuspend(udev);
948 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700949 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950 if (udev->actconfig)
951 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
952 else
953 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530954 break;
955 case USB_DEVICE_REMOVE:
956 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700957 break;
958 default:
959 break;
960 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530961 if (test_bit(ID_A, &motg->inputs))
962 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
963 motg->mA_port);
964out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 return NOTIFY_OK;
966}
967
Mayank Ranae3926882011-12-26 09:47:54 +0530968static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
969{
970 int ret;
971 static bool vbus_is_on;
972
973 if (vbus_is_on == on)
974 return;
975
976 if (motg->pdata->vbus_power) {
977 motg->pdata->vbus_power(on);
978 return;
979 }
980
981 if (!vbus_otg) {
982 pr_err("vbus_otg is NULL.");
983 return;
984 }
985
986 if (on) {
987 ret = regulator_enable(vbus_otg);
988 if (ret) {
989 pr_err("unable to enable vbus_otg\n");
990 return;
991 }
992 vbus_is_on = true;
993 } else {
994 ret = regulator_disable(vbus_otg);
995 if (ret) {
996 pr_err("unable to disable vbus_otg\n");
997 return;
998 }
999 vbus_is_on = false;
1000 }
1001}
1002
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301003static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1004{
1005 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1006 struct usb_hcd *hcd;
1007
1008 /*
1009 * Fail host registration if this board can support
1010 * only peripheral configuration.
1011 */
1012 if (motg->pdata->mode == USB_PERIPHERAL) {
1013 dev_info(otg->dev, "Host mode is not supported\n");
1014 return -ENODEV;
1015 }
1016
Mayank Ranae3926882011-12-26 09:47:54 +05301017 if (!motg->pdata->vbus_power && host) {
1018 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1019 if (IS_ERR(vbus_otg)) {
1020 pr_err("Unable to get vbus_otg\n");
1021 return -ENODEV;
1022 }
1023 }
1024
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301025 if (!host) {
1026 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301027 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001028 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301029 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301030 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301031 otg->host = NULL;
1032 otg->state = OTG_STATE_UNDEFINED;
1033 schedule_work(&motg->sm_work);
1034 } else {
1035 otg->host = NULL;
1036 }
1037
Mayank Ranae3926882011-12-26 09:47:54 +05301038 if (vbus_otg)
1039 regulator_put(vbus_otg);
1040
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301041 return 0;
1042 }
1043
1044 hcd = bus_to_hcd(host);
1045 hcd->power_budget = motg->pdata->power_budget;
1046
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001047 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1048 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301049 otg->host = host;
1050 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1051
1052 /*
1053 * Kick the state machine work, if peripheral is not supported
1054 * or peripheral is already registered with us.
1055 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301056 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1057 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301058 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301059 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301060
1061 return 0;
1062}
1063
1064static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1065{
1066 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1067 struct msm_otg_platform_data *pdata = motg->pdata;
1068
1069 if (!otg->gadget)
1070 return;
1071
1072 if (on) {
1073 dev_dbg(otg->dev, "gadget on\n");
1074 /*
1075 * Some boards have a switch cotrolled by gpio
1076 * to enable/disable internal HUB. Disable internal
1077 * HUB before kicking the gadget.
1078 */
1079 if (pdata->setup_gpio)
1080 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301081 /*
1082 * vote for minimum dma_latency to prevent idle
1083 * power collapse(pc) while running in peripheral mode.
1084 */
1085 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301086 usb_gadget_vbus_connect(otg->gadget);
1087 } else {
1088 dev_dbg(otg->dev, "gadget off\n");
1089 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301090 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301091 if (pdata->setup_gpio)
1092 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1093 }
1094
1095}
1096
1097static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1098 struct usb_gadget *gadget)
1099{
1100 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1101
1102 /*
1103 * Fail peripheral registration if this board can support
1104 * only host configuration.
1105 */
1106 if (motg->pdata->mode == USB_HOST) {
1107 dev_info(otg->dev, "Peripheral mode is not supported\n");
1108 return -ENODEV;
1109 }
1110
1111 if (!gadget) {
1112 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301113 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301114 msm_otg_start_peripheral(otg, 0);
1115 otg->gadget = NULL;
1116 otg->state = OTG_STATE_UNDEFINED;
1117 schedule_work(&motg->sm_work);
1118 } else {
1119 otg->gadget = NULL;
1120 }
1121
1122 return 0;
1123 }
1124 otg->gadget = gadget;
1125 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1126
1127 /*
1128 * Kick the state machine work, if host is not supported
1129 * or host is already registered with us.
1130 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301131 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1132 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301133 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301134 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301135
1136 return 0;
1137}
1138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001139static bool msm_chg_aca_detect(struct msm_otg *motg)
1140{
1141 struct otg_transceiver *otg = &motg->otg;
1142 u32 int_sts;
1143 bool ret = false;
1144
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301145 if (!aca_enabled())
1146 goto out;
1147
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1149 goto out;
1150
1151 int_sts = ulpi_read(otg, 0x87);
1152 switch (int_sts & 0x1C) {
1153 case 0x08:
1154 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1155 dev_dbg(otg->dev, "ID_A\n");
1156 motg->chg_type = USB_ACA_A_CHARGER;
1157 motg->chg_state = USB_CHG_STATE_DETECTED;
1158 clear_bit(ID_B, &motg->inputs);
1159 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301160 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001161 ret = true;
1162 }
1163 break;
1164 case 0x0C:
1165 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1166 dev_dbg(otg->dev, "ID_B\n");
1167 motg->chg_type = USB_ACA_B_CHARGER;
1168 motg->chg_state = USB_CHG_STATE_DETECTED;
1169 clear_bit(ID_A, &motg->inputs);
1170 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301171 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 ret = true;
1173 }
1174 break;
1175 case 0x10:
1176 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1177 dev_dbg(otg->dev, "ID_C\n");
1178 motg->chg_type = USB_ACA_C_CHARGER;
1179 motg->chg_state = USB_CHG_STATE_DETECTED;
1180 clear_bit(ID_A, &motg->inputs);
1181 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301182 set_bit(ID, &motg->inputs);
1183 ret = true;
1184 }
1185 break;
1186 case 0x04:
1187 if (test_and_clear_bit(ID, &motg->inputs)) {
1188 dev_dbg(otg->dev, "ID_GND\n");
1189 motg->chg_type = USB_INVALID_CHARGER;
1190 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1191 clear_bit(ID_A, &motg->inputs);
1192 clear_bit(ID_B, &motg->inputs);
1193 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194 ret = true;
1195 }
1196 break;
1197 default:
1198 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1199 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301200 test_and_clear_bit(ID_C, &motg->inputs) |
1201 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301203 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001204 motg->chg_type = USB_INVALID_CHARGER;
1205 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1206 }
1207 }
1208out:
1209 return ret;
1210}
1211
1212static void msm_chg_enable_aca_det(struct msm_otg *motg)
1213{
1214 struct otg_transceiver *otg = &motg->otg;
1215
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301216 if (!aca_enabled())
1217 return;
1218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 switch (motg->pdata->phy_type) {
1220 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301221 /* Disable ID_GND in link and PHY */
1222 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1223 OTGSC_IDIE), USB_OTGSC);
1224 ulpi_write(otg, 0x01, 0x0C);
1225 ulpi_write(otg, 0x10, 0x0F);
1226 ulpi_write(otg, 0x10, 0x12);
1227 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301229 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 break;
1231 default:
1232 break;
1233 }
1234}
1235
1236static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1237{
1238 struct otg_transceiver *otg = &motg->otg;
1239
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301240 if (!aca_enabled())
1241 return;
1242
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001243 switch (motg->pdata->phy_type) {
1244 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 /* Enable ACA Detection interrupt (on any RID change) */
1246 ulpi_write(otg, 0x01, 0x94);
1247 break;
1248 default:
1249 break;
1250 }
1251}
1252
1253static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1254{
1255 struct otg_transceiver *otg = &motg->otg;
1256
1257 if (!aca_enabled())
1258 return;
1259
1260 switch (motg->pdata->phy_type) {
1261 case SNPS_28NM_INTEGRATED_PHY:
1262 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 break;
1264 default:
1265 break;
1266 }
1267}
1268
1269static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1270{
1271 struct otg_transceiver *otg = &motg->otg;
1272 bool ret = false;
1273
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301274 if (!aca_enabled())
1275 return ret;
1276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277 switch (motg->pdata->phy_type) {
1278 case SNPS_28NM_INTEGRATED_PHY:
1279 if (ulpi_read(otg, 0x91) & 1) {
1280 dev_dbg(otg->dev, "RID change\n");
1281 ulpi_write(otg, 0x01, 0x92);
1282 ret = msm_chg_aca_detect(motg);
1283 }
1284 default:
1285 break;
1286 }
1287 return ret;
1288}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301289
1290static void msm_otg_id_timer_func(unsigned long data)
1291{
1292 struct msm_otg *motg = (struct msm_otg *) data;
1293
1294 if (!aca_enabled())
1295 return;
1296
1297 if (atomic_read(&motg->in_lpm)) {
1298 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1299 return;
1300 }
1301
1302 if (msm_chg_check_aca_intr(motg)) {
1303 dev_dbg(motg->otg.dev, "timer: aca work\n");
1304 schedule_work(&motg->sm_work);
1305 }
1306
1307 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1308 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1309}
1310
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301311static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1312{
1313 struct otg_transceiver *otg = &motg->otg;
1314 u32 chg_det;
1315 bool ret = false;
1316
1317 switch (motg->pdata->phy_type) {
1318 case CI_45NM_INTEGRATED_PHY:
1319 chg_det = ulpi_read(otg, 0x34);
1320 ret = chg_det & (1 << 4);
1321 break;
1322 case SNPS_28NM_INTEGRATED_PHY:
1323 chg_det = ulpi_read(otg, 0x87);
1324 ret = chg_det & 1;
1325 break;
1326 default:
1327 break;
1328 }
1329 return ret;
1330}
1331
1332static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1333{
1334 struct otg_transceiver *otg = &motg->otg;
1335 u32 chg_det;
1336
1337 switch (motg->pdata->phy_type) {
1338 case CI_45NM_INTEGRATED_PHY:
1339 chg_det = ulpi_read(otg, 0x34);
1340 /* Turn off charger block */
1341 chg_det |= ~(1 << 1);
1342 ulpi_write(otg, chg_det, 0x34);
1343 udelay(20);
1344 /* control chg block via ULPI */
1345 chg_det &= ~(1 << 3);
1346 ulpi_write(otg, chg_det, 0x34);
1347 /* put it in host mode for enabling D- source */
1348 chg_det &= ~(1 << 2);
1349 ulpi_write(otg, chg_det, 0x34);
1350 /* Turn on chg detect block */
1351 chg_det &= ~(1 << 1);
1352 ulpi_write(otg, chg_det, 0x34);
1353 udelay(20);
1354 /* enable chg detection */
1355 chg_det &= ~(1 << 0);
1356 ulpi_write(otg, chg_det, 0x34);
1357 break;
1358 case SNPS_28NM_INTEGRATED_PHY:
1359 /*
1360 * Configure DM as current source, DP as current sink
1361 * and enable battery charging comparators.
1362 */
1363 ulpi_write(otg, 0x8, 0x85);
1364 ulpi_write(otg, 0x2, 0x85);
1365 ulpi_write(otg, 0x1, 0x85);
1366 break;
1367 default:
1368 break;
1369 }
1370}
1371
1372static bool msm_chg_check_primary_det(struct msm_otg *motg)
1373{
1374 struct otg_transceiver *otg = &motg->otg;
1375 u32 chg_det;
1376 bool ret = false;
1377
1378 switch (motg->pdata->phy_type) {
1379 case CI_45NM_INTEGRATED_PHY:
1380 chg_det = ulpi_read(otg, 0x34);
1381 ret = chg_det & (1 << 4);
1382 break;
1383 case SNPS_28NM_INTEGRATED_PHY:
1384 chg_det = ulpi_read(otg, 0x87);
1385 ret = chg_det & 1;
1386 break;
1387 default:
1388 break;
1389 }
1390 return ret;
1391}
1392
1393static void msm_chg_enable_primary_det(struct msm_otg *motg)
1394{
1395 struct otg_transceiver *otg = &motg->otg;
1396 u32 chg_det;
1397
1398 switch (motg->pdata->phy_type) {
1399 case CI_45NM_INTEGRATED_PHY:
1400 chg_det = ulpi_read(otg, 0x34);
1401 /* enable chg detection */
1402 chg_det &= ~(1 << 0);
1403 ulpi_write(otg, chg_det, 0x34);
1404 break;
1405 case SNPS_28NM_INTEGRATED_PHY:
1406 /*
1407 * Configure DP as current source, DM as current sink
1408 * and enable battery charging comparators.
1409 */
1410 ulpi_write(otg, 0x2, 0x85);
1411 ulpi_write(otg, 0x1, 0x85);
1412 break;
1413 default:
1414 break;
1415 }
1416}
1417
1418static bool msm_chg_check_dcd(struct msm_otg *motg)
1419{
1420 struct otg_transceiver *otg = &motg->otg;
1421 u32 line_state;
1422 bool ret = false;
1423
1424 switch (motg->pdata->phy_type) {
1425 case CI_45NM_INTEGRATED_PHY:
1426 line_state = ulpi_read(otg, 0x15);
1427 ret = !(line_state & 1);
1428 break;
1429 case SNPS_28NM_INTEGRATED_PHY:
1430 line_state = ulpi_read(otg, 0x87);
1431 ret = line_state & 2;
1432 break;
1433 default:
1434 break;
1435 }
1436 return ret;
1437}
1438
1439static void msm_chg_disable_dcd(struct msm_otg *motg)
1440{
1441 struct otg_transceiver *otg = &motg->otg;
1442 u32 chg_det;
1443
1444 switch (motg->pdata->phy_type) {
1445 case CI_45NM_INTEGRATED_PHY:
1446 chg_det = ulpi_read(otg, 0x34);
1447 chg_det &= ~(1 << 5);
1448 ulpi_write(otg, chg_det, 0x34);
1449 break;
1450 case SNPS_28NM_INTEGRATED_PHY:
1451 ulpi_write(otg, 0x10, 0x86);
1452 break;
1453 default:
1454 break;
1455 }
1456}
1457
1458static void msm_chg_enable_dcd(struct msm_otg *motg)
1459{
1460 struct otg_transceiver *otg = &motg->otg;
1461 u32 chg_det;
1462
1463 switch (motg->pdata->phy_type) {
1464 case CI_45NM_INTEGRATED_PHY:
1465 chg_det = ulpi_read(otg, 0x34);
1466 /* Turn on D+ current source */
1467 chg_det |= (1 << 5);
1468 ulpi_write(otg, chg_det, 0x34);
1469 break;
1470 case SNPS_28NM_INTEGRATED_PHY:
1471 /* Data contact detection enable */
1472 ulpi_write(otg, 0x10, 0x85);
1473 break;
1474 default:
1475 break;
1476 }
1477}
1478
1479static void msm_chg_block_on(struct msm_otg *motg)
1480{
1481 struct otg_transceiver *otg = &motg->otg;
1482 u32 func_ctrl, chg_det;
1483
1484 /* put the controller in non-driving mode */
1485 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1486 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1487 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1488 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1489
1490 switch (motg->pdata->phy_type) {
1491 case CI_45NM_INTEGRATED_PHY:
1492 chg_det = ulpi_read(otg, 0x34);
1493 /* control chg block via ULPI */
1494 chg_det &= ~(1 << 3);
1495 ulpi_write(otg, chg_det, 0x34);
1496 /* Turn on chg detect block */
1497 chg_det &= ~(1 << 1);
1498 ulpi_write(otg, chg_det, 0x34);
1499 udelay(20);
1500 break;
1501 case SNPS_28NM_INTEGRATED_PHY:
1502 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301503 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301504 /* Clear alt interrupt latch and enable bits */
1505 ulpi_write(otg, 0x1F, 0x92);
1506 ulpi_write(otg, 0x1F, 0x95);
1507 udelay(100);
1508 break;
1509 default:
1510 break;
1511 }
1512}
1513
1514static void msm_chg_block_off(struct msm_otg *motg)
1515{
1516 struct otg_transceiver *otg = &motg->otg;
1517 u32 func_ctrl, chg_det;
1518
1519 switch (motg->pdata->phy_type) {
1520 case CI_45NM_INTEGRATED_PHY:
1521 chg_det = ulpi_read(otg, 0x34);
1522 /* Turn off charger block */
1523 chg_det |= ~(1 << 1);
1524 ulpi_write(otg, chg_det, 0x34);
1525 break;
1526 case SNPS_28NM_INTEGRATED_PHY:
1527 /* Clear charger detecting control bits */
1528 ulpi_write(otg, 0x3F, 0x86);
1529 /* Clear alt interrupt latch and enable bits */
1530 ulpi_write(otg, 0x1F, 0x92);
1531 ulpi_write(otg, 0x1F, 0x95);
1532 break;
1533 default:
1534 break;
1535 }
1536
1537 /* put the controller in normal mode */
1538 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1539 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1540 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1541 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1542}
1543
Anji jonnalad270e2d2011-08-09 11:28:32 +05301544static const char *chg_to_string(enum usb_chg_type chg_type)
1545{
1546 switch (chg_type) {
1547 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1548 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1549 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1550 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1551 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1552 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1553 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1554 default: return "INVALID_CHARGER";
1555 }
1556}
1557
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301558#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1559#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1560#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1561#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1562static void msm_chg_detect_work(struct work_struct *w)
1563{
1564 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1565 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301567 unsigned long delay;
1568
1569 dev_dbg(otg->dev, "chg detection work\n");
1570 switch (motg->chg_state) {
1571 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301572 msm_chg_block_on(motg);
1573 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001574 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301575 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1576 motg->dcd_retries = 0;
1577 delay = MSM_CHG_DCD_POLL_TIME;
1578 break;
1579 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580 is_aca = msm_chg_aca_detect(motg);
1581 if (is_aca) {
1582 /*
1583 * ID_A can be ACA dock too. continue
1584 * primary detection after DCD.
1585 */
1586 if (test_bit(ID_A, &motg->inputs)) {
1587 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1588 } else {
1589 delay = 0;
1590 break;
1591 }
1592 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301593 is_dcd = msm_chg_check_dcd(motg);
1594 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1595 if (is_dcd || tmout) {
1596 msm_chg_disable_dcd(motg);
1597 msm_chg_enable_primary_det(motg);
1598 delay = MSM_CHG_PRIMARY_DET_TIME;
1599 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1600 } else {
1601 delay = MSM_CHG_DCD_POLL_TIME;
1602 }
1603 break;
1604 case USB_CHG_STATE_DCD_DONE:
1605 vout = msm_chg_check_primary_det(motg);
1606 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301607 if (test_bit(ID_A, &motg->inputs)) {
1608 motg->chg_type = USB_ACA_DOCK_CHARGER;
1609 motg->chg_state = USB_CHG_STATE_DETECTED;
1610 delay = 0;
1611 break;
1612 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301613 msm_chg_enable_secondary_det(motg);
1614 delay = MSM_CHG_SECONDARY_DET_TIME;
1615 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1616 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301617 if (test_bit(ID_A, &motg->inputs)) {
1618 motg->chg_type = USB_ACA_A_CHARGER;
1619 motg->chg_state = USB_CHG_STATE_DETECTED;
1620 delay = 0;
1621 break;
1622 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301623 motg->chg_type = USB_SDP_CHARGER;
1624 motg->chg_state = USB_CHG_STATE_DETECTED;
1625 delay = 0;
1626 }
1627 break;
1628 case USB_CHG_STATE_PRIMARY_DONE:
1629 vout = msm_chg_check_secondary_det(motg);
1630 if (vout)
1631 motg->chg_type = USB_DCP_CHARGER;
1632 else
1633 motg->chg_type = USB_CDP_CHARGER;
1634 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1635 /* fall through */
1636 case USB_CHG_STATE_SECONDARY_DONE:
1637 motg->chg_state = USB_CHG_STATE_DETECTED;
1638 case USB_CHG_STATE_DETECTED:
1639 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 msm_chg_enable_aca_det(motg);
1641 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301642 dev_dbg(otg->dev, "chg_type = %s\n",
1643 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301644 schedule_work(&motg->sm_work);
1645 return;
1646 default:
1647 return;
1648 }
1649
1650 schedule_delayed_work(&motg->chg_work, delay);
1651}
1652
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301653/*
1654 * We support OTG, Peripheral only and Host only configurations. In case
1655 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1656 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1657 * enabled when switch is controlled by user and default mode is supplied
1658 * by board file, which can be changed by userspace later.
1659 */
1660static void msm_otg_init_sm(struct msm_otg *motg)
1661{
1662 struct msm_otg_platform_data *pdata = motg->pdata;
1663 u32 otgsc = readl(USB_OTGSC);
1664
1665 switch (pdata->mode) {
1666 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001667 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301668 if (pdata->default_mode == USB_HOST) {
1669 clear_bit(ID, &motg->inputs);
1670 } else if (pdata->default_mode == USB_PERIPHERAL) {
1671 set_bit(ID, &motg->inputs);
1672 set_bit(B_SESS_VLD, &motg->inputs);
1673 } else {
1674 set_bit(ID, &motg->inputs);
1675 clear_bit(B_SESS_VLD, &motg->inputs);
1676 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301677 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1678 if (otgsc & OTGSC_ID)
1679 set_bit(ID, &motg->inputs);
1680 else
1681 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001682 if (otgsc & OTGSC_BSV)
1683 set_bit(B_SESS_VLD, &motg->inputs);
1684 else
1685 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301686 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1687 if (irq_read_line(motg->pdata->pmic_id_irq))
1688 set_bit(ID, &motg->inputs);
1689 else
1690 clear_bit(ID, &motg->inputs);
1691
1692 /*
1693 * VBUS initial state is reported after PMIC
1694 * driver initialization. Wait for it.
1695 */
1696 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301697 }
1698 break;
1699 case USB_HOST:
1700 clear_bit(ID, &motg->inputs);
1701 break;
1702 case USB_PERIPHERAL:
1703 set_bit(ID, &motg->inputs);
1704 if (otgsc & OTGSC_BSV)
1705 set_bit(B_SESS_VLD, &motg->inputs);
1706 else
1707 clear_bit(B_SESS_VLD, &motg->inputs);
1708 break;
1709 default:
1710 break;
1711 }
1712}
1713
1714static void msm_otg_sm_work(struct work_struct *w)
1715{
1716 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1717 struct otg_transceiver *otg = &motg->otg;
1718
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301719 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301720 switch (otg->state) {
1721 case OTG_STATE_UNDEFINED:
1722 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1723 msm_otg_reset(otg);
1724 msm_otg_init_sm(motg);
1725 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301726 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1727 test_bit(ID, &motg->inputs)) {
1728 pm_runtime_put_noidle(otg->dev);
1729 pm_runtime_suspend(otg->dev);
1730 break;
1731 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301732 /* FALL THROUGH */
1733 case OTG_STATE_B_IDLE:
1734 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001735 if ((!test_bit(ID, &motg->inputs) ||
1736 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001737 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1738 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301739 IDEV_ACA_CHG_MAX);
1740 else if (test_bit(ID_A, &motg->inputs))
1741 msm_otg_notify_charger(motg,
1742 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301743 else
1744 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301745 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301746 /*
1747 * Link can not generate PHY_ALT interrupt
1748 * in host mode when no device is attached
1749 * to the port. It is also observed PHY_ALT
1750 * interrupt missing upon Micro-A cable disconnect.
1751 * Hence disable PHY_ALT interrupt and perform
1752 * polling to detect RID change.
1753 */
1754 msm_chg_enable_aca_det(motg);
1755 msm_chg_disable_aca_intr(motg);
1756 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301757 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1759 switch (motg->chg_state) {
1760 case USB_CHG_STATE_UNDEFINED:
1761 msm_chg_detect_work(&motg->chg_work.work);
1762 break;
1763 case USB_CHG_STATE_DETECTED:
1764 switch (motg->chg_type) {
1765 case USB_DCP_CHARGER:
1766 msm_otg_notify_charger(motg,
1767 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301768 pm_runtime_put_noidle(otg->dev);
1769 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301770 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301771 case USB_ACA_B_CHARGER:
1772 msm_otg_notify_charger(motg,
1773 IDEV_ACA_CHG_MAX);
1774 /*
1775 * (ID_B --> ID_C) PHY_ALT interrupt can
1776 * not be detected in LPM.
1777 */
1778 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301779 case USB_CDP_CHARGER:
1780 msm_otg_notify_charger(motg,
1781 IDEV_CHG_MAX);
1782 msm_otg_start_peripheral(otg, 1);
1783 otg->state = OTG_STATE_B_PERIPHERAL;
1784 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301785 case USB_ACA_C_CHARGER:
1786 msm_otg_notify_charger(motg,
1787 IDEV_ACA_CHG_MAX);
1788 msm_otg_start_peripheral(otg, 1);
1789 otg->state = OTG_STATE_B_PERIPHERAL;
1790 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 case USB_SDP_CHARGER:
1792 msm_otg_notify_charger(motg, IUNIT);
1793 msm_otg_start_peripheral(otg, 1);
1794 otg->state = OTG_STATE_B_PERIPHERAL;
1795 break;
1796 default:
1797 break;
1798 }
1799 break;
1800 default:
1801 break;
1802 }
1803 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301804 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301805 msm_otg_notify_charger(motg, 0);
1806 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1807 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301808 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301809 pm_runtime_put_noidle(otg->dev);
1810 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301811 }
1812 break;
1813 case OTG_STATE_B_PERIPHERAL:
1814 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1815 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001816 !test_bit(ID, &motg->inputs) ||
1817 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301818 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001819 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001820 schedule_work(w);
1821 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301822 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001823 }
1824 break;
1825 case OTG_STATE_A_HOST:
1826 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1827 if (test_bit(ID, &motg->inputs) &&
1828 !test_bit(ID_A, &motg->inputs)) {
1829 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301830 msm_hsusb_vbus_power(motg, 0);
1831 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301832 /*
1833 * Exit point of host mode.
1834 *
1835 * 1. Micro-A cable disconnect: Just schedule
1836 * the work. PHY is reset in B_IDLE and LPM
1837 * is allowed.
1838 * 2. ID_GND --> ID_B: No need to reset the PHY.
1839 * HCD core clears all PORTSC bits and initializes
1840 * the controller to host mode in remove_hcd.
1841 * Restore PORTSC transceiver select bits (ULPI)
1842 * and reset the controller to change MODE bits.
1843 * PHY_ALT interrupt can not occur in host mode.
1844 */
1845 del_timer_sync(&motg->id_timer);
1846 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1847 msm_otg_link_reset(motg);
1848 msm_chg_enable_aca_intr(motg);
1849 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301850 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301851 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301853 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301855 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001856 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301858 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301859 }
1860 break;
1861 default:
1862 break;
1863 }
1864}
1865
1866static irqreturn_t msm_otg_irq(int irq, void *data)
1867{
1868 struct msm_otg *motg = data;
1869 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001870 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301871
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301872 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301873 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301874 disable_irq_nosync(irq);
1875 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301876 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301877 return IRQ_HANDLED;
1878 }
1879
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001880 usbsts = readl(USB_USBSTS);
1881 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301882 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001883 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301884 if (msm_chg_check_aca_intr(motg)) {
1885 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301887 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001888 return IRQ_HANDLED;
1889 }
1890
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301891 otgsc = readl(USB_OTGSC);
1892 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1893 return IRQ_NONE;
1894
1895 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301896 if (otgsc & OTGSC_ID) {
1897 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301898 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301899 } else {
1900 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301901 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301902 msm_chg_enable_aca_det(motg);
1903 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001904 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301905 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301906 if (otgsc & OTGSC_BSV) {
1907 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301908 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301909 } else {
1910 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301911 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301912 msm_chg_check_aca_intr(motg);
1913 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001914 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301915 }
1916
1917 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001918 return IRQ_HANDLED;
1919}
1920
1921static void msm_otg_set_vbus_state(int online)
1922{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301923 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 struct msm_otg *motg = the_msm_otg;
1925
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301926 if (online) {
1927 pr_debug("PMIC: BSV set\n");
1928 set_bit(B_SESS_VLD, &motg->inputs);
1929 } else {
1930 pr_debug("PMIC: BSV clear\n");
1931 clear_bit(B_SESS_VLD, &motg->inputs);
1932 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301934 if (!init) {
1935 init = true;
1936 complete(&pmic_vbus_init);
1937 pr_debug("PMIC: BSV init complete\n");
1938 return;
1939 }
1940
1941 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001942}
1943
1944static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1945{
1946 struct msm_otg *motg = data;
1947
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301948 if (aca_id_turned_on)
1949 return IRQ_HANDLED;
1950
1951 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1952 pr_debug("PMIC: ID set\n");
1953 set_bit(ID, &motg->inputs);
1954 } else {
1955 pr_debug("PMIC: ID clear\n");
1956 clear_bit(ID, &motg->inputs);
1957 }
1958
1959 if (motg->otg.state != OTG_STATE_UNDEFINED)
1960 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301962 return IRQ_HANDLED;
1963}
1964
1965static int msm_otg_mode_show(struct seq_file *s, void *unused)
1966{
1967 struct msm_otg *motg = s->private;
1968 struct otg_transceiver *otg = &motg->otg;
1969
1970 switch (otg->state) {
1971 case OTG_STATE_A_HOST:
1972 seq_printf(s, "host\n");
1973 break;
1974 case OTG_STATE_B_PERIPHERAL:
1975 seq_printf(s, "peripheral\n");
1976 break;
1977 default:
1978 seq_printf(s, "none\n");
1979 break;
1980 }
1981
1982 return 0;
1983}
1984
1985static int msm_otg_mode_open(struct inode *inode, struct file *file)
1986{
1987 return single_open(file, msm_otg_mode_show, inode->i_private);
1988}
1989
1990static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1991 size_t count, loff_t *ppos)
1992{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301993 struct seq_file *s = file->private_data;
1994 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301995 char buf[16];
1996 struct otg_transceiver *otg = &motg->otg;
1997 int status = count;
1998 enum usb_mode_type req_mode;
1999
2000 memset(buf, 0x00, sizeof(buf));
2001
2002 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2003 status = -EFAULT;
2004 goto out;
2005 }
2006
2007 if (!strncmp(buf, "host", 4)) {
2008 req_mode = USB_HOST;
2009 } else if (!strncmp(buf, "peripheral", 10)) {
2010 req_mode = USB_PERIPHERAL;
2011 } else if (!strncmp(buf, "none", 4)) {
2012 req_mode = USB_NONE;
2013 } else {
2014 status = -EINVAL;
2015 goto out;
2016 }
2017
2018 switch (req_mode) {
2019 case USB_NONE:
2020 switch (otg->state) {
2021 case OTG_STATE_A_HOST:
2022 case OTG_STATE_B_PERIPHERAL:
2023 set_bit(ID, &motg->inputs);
2024 clear_bit(B_SESS_VLD, &motg->inputs);
2025 break;
2026 default:
2027 goto out;
2028 }
2029 break;
2030 case USB_PERIPHERAL:
2031 switch (otg->state) {
2032 case OTG_STATE_B_IDLE:
2033 case OTG_STATE_A_HOST:
2034 set_bit(ID, &motg->inputs);
2035 set_bit(B_SESS_VLD, &motg->inputs);
2036 break;
2037 default:
2038 goto out;
2039 }
2040 break;
2041 case USB_HOST:
2042 switch (otg->state) {
2043 case OTG_STATE_B_IDLE:
2044 case OTG_STATE_B_PERIPHERAL:
2045 clear_bit(ID, &motg->inputs);
2046 break;
2047 default:
2048 goto out;
2049 }
2050 break;
2051 default:
2052 goto out;
2053 }
2054
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302055 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302056 schedule_work(&motg->sm_work);
2057out:
2058 return status;
2059}
2060
2061const struct file_operations msm_otg_mode_fops = {
2062 .open = msm_otg_mode_open,
2063 .read = seq_read,
2064 .write = msm_otg_mode_write,
2065 .llseek = seq_lseek,
2066 .release = single_release,
2067};
2068
Anji jonnalad270e2d2011-08-09 11:28:32 +05302069static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2070{
2071 struct msm_otg *motg = s->private;
2072
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302073 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302074 return 0;
2075}
2076
2077static int msm_otg_chg_open(struct inode *inode, struct file *file)
2078{
2079 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2080}
2081
2082const struct file_operations msm_otg_chg_fops = {
2083 .open = msm_otg_chg_open,
2084 .read = seq_read,
2085 .llseek = seq_lseek,
2086 .release = single_release,
2087};
2088
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302089static int msm_otg_aca_show(struct seq_file *s, void *unused)
2090{
2091 if (debug_aca_enabled)
2092 seq_printf(s, "enabled\n");
2093 else
2094 seq_printf(s, "disabled\n");
2095
2096 return 0;
2097}
2098
2099static int msm_otg_aca_open(struct inode *inode, struct file *file)
2100{
2101 return single_open(file, msm_otg_aca_show, inode->i_private);
2102}
2103
2104static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2105 size_t count, loff_t *ppos)
2106{
2107 char buf[8];
2108
2109 memset(buf, 0x00, sizeof(buf));
2110
2111 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2112 return -EFAULT;
2113
2114 if (!strncmp(buf, "enable", 6))
2115 debug_aca_enabled = true;
2116 else
2117 debug_aca_enabled = false;
2118
2119 return count;
2120}
2121
2122const struct file_operations msm_otg_aca_fops = {
2123 .open = msm_otg_aca_open,
2124 .read = seq_read,
2125 .write = msm_otg_aca_write,
2126 .llseek = seq_lseek,
2127 .release = single_release,
2128};
2129
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302130static struct dentry *msm_otg_dbg_root;
2131static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302132static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302133static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302134
2135static int msm_otg_debugfs_init(struct msm_otg *motg)
2136{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302137
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302138 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2139
2140 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2141 return -ENODEV;
2142
Anji jonnalad270e2d2011-08-09 11:28:32 +05302143 if (motg->pdata->mode == USB_OTG &&
2144 motg->pdata->otg_control == OTG_USER_CONTROL) {
2145
2146 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2147 S_IWUSR, msm_otg_dbg_root, motg,
2148 &msm_otg_mode_fops);
2149
2150 if (!msm_otg_dbg_mode) {
2151 debugfs_remove(msm_otg_dbg_root);
2152 msm_otg_dbg_root = NULL;
2153 return -ENODEV;
2154 }
2155 }
2156
2157 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2158 msm_otg_dbg_root, motg,
2159 &msm_otg_chg_fops);
2160
2161 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302162 debugfs_remove_recursive(msm_otg_dbg_root);
2163 return -ENODEV;
2164 }
2165
2166 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2167 msm_otg_dbg_root, motg,
2168 &msm_otg_aca_fops);
2169
2170 if (!msm_otg_dbg_aca) {
2171 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302172 return -ENODEV;
2173 }
2174
2175 return 0;
2176}
2177
2178static void msm_otg_debugfs_cleanup(void)
2179{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302180 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302181}
2182
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302183static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2184static struct platform_device *msm_otg_add_pdev(
2185 struct platform_device *ofdev, const char *name)
2186{
2187 struct platform_device *pdev;
2188 const struct resource *res = ofdev->resource;
2189 unsigned int num = ofdev->num_resources;
2190 int retval;
2191
2192 pdev = platform_device_alloc(name, -1);
2193 if (!pdev) {
2194 retval = -ENOMEM;
2195 goto error;
2196 }
2197
2198 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2199 pdev->dev.dma_mask = &msm_otg_dma_mask;
2200
2201 if (num) {
2202 retval = platform_device_add_resources(pdev, res, num);
2203 if (retval)
2204 goto error;
2205 }
2206
2207 retval = platform_device_add(pdev);
2208 if (retval)
2209 goto error;
2210
2211 return pdev;
2212
2213error:
2214 platform_device_put(pdev);
2215 return ERR_PTR(retval);
2216}
2217
2218static int msm_otg_setup_devices(struct platform_device *ofdev,
2219 enum usb_mode_type mode, bool init)
2220{
2221 const char *gadget_name = "msm_hsusb";
2222 const char *host_name = "msm_hsusb_host";
2223 static struct platform_device *gadget_pdev;
2224 static struct platform_device *host_pdev;
2225 int retval = 0;
2226
2227 if (!init) {
2228 if (gadget_pdev)
2229 platform_device_unregister(gadget_pdev);
2230 if (host_pdev)
2231 platform_device_unregister(host_pdev);
2232 return 0;
2233 }
2234
2235 switch (mode) {
2236 case USB_OTG:
2237 /* fall through */
2238 case USB_PERIPHERAL:
2239 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2240 if (IS_ERR(gadget_pdev)) {
2241 retval = PTR_ERR(gadget_pdev);
2242 break;
2243 }
2244 if (mode == USB_PERIPHERAL)
2245 break;
2246 /* fall through */
2247 case USB_HOST:
2248 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2249 if (IS_ERR(host_pdev)) {
2250 retval = PTR_ERR(host_pdev);
2251 if (mode == USB_OTG)
2252 platform_device_unregister(gadget_pdev);
2253 }
2254 break;
2255 default:
2256 break;
2257 }
2258
2259 return retval;
2260}
2261
2262struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2263{
2264 struct device_node *node = pdev->dev.of_node;
2265 struct msm_otg_platform_data *pdata;
2266 int len = 0;
2267
2268 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2269 if (!pdata) {
2270 pr_err("unable to allocate platform data\n");
2271 return NULL;
2272 }
2273 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2274 if (len) {
2275 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2276 if (!pdata->phy_init_seq)
2277 return NULL;
2278 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2279 pdata->phy_init_seq,
2280 len/sizeof(*pdata->phy_init_seq));
2281 }
2282 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2283 &pdata->power_budget);
2284 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2285 &pdata->mode);
2286 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2287 &pdata->otg_control);
2288 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2289 &pdata->default_mode);
2290 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2291 &pdata->phy_type);
2292 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2293 &pdata->pmic_id_irq);
2294 of_property_read_string(node, "qcom,hsusb-otg-pclk-src-name",
2295 &pdata->pclk_src_name);
2296 return pdata;
2297}
2298
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302299static int __init msm_otg_probe(struct platform_device *pdev)
2300{
2301 int ret = 0;
2302 struct resource *res;
2303 struct msm_otg *motg;
2304 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302305 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302306
2307 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302308
2309 if (pdev->dev.of_node) {
2310 dev_dbg(&pdev->dev, "device tree enabled\n");
2311 pdata = msm_otg_dt_to_pdata(pdev);
2312 if (!pdata)
2313 return -ENOMEM;
2314 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2315 if (ret) {
2316 dev_err(&pdev->dev, "devices setup failed\n");
2317 return ret;
2318 }
2319 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302320 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2321 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302322 } else {
2323 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302324 }
2325
2326 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2327 if (!motg) {
2328 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2329 return -ENOMEM;
2330 }
2331
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002332 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302333 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302334 otg = &motg->otg;
2335 otg->dev = &pdev->dev;
2336
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302337 /*
2338 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2339 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2340 * PMIC can detect ACA ID_GND and generate an interrupt.
2341 */
2342 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2343 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2344 ret = -EINVAL;
2345 goto free_motg;
2346 }
2347
Ofir Cohen4da266f2012-01-03 10:19:29 +02002348 /* initialize reset counter */
2349 motg->reset_counter = 0;
2350
Amit Blay02eff132011-09-21 16:46:24 +03002351 /* Some targets don't support PHY clock. */
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302352 motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002353 if (IS_ERR(motg->phy_reset_clk))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302354 dev_err(&pdev->dev, "failed to get usb_phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302355
2356 motg->clk = clk_get(&pdev->dev, "usb_hs_clk");
2357 if (IS_ERR(motg->clk)) {
2358 dev_err(&pdev->dev, "failed to get usb_hs_clk\n");
2359 ret = PTR_ERR(motg->clk);
2360 goto put_phy_reset_clk;
2361 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302362 clk_set_rate(motg->clk, 60000000);
2363
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302364 /* pm qos request to prevent apps idle power collapse */
2365 if (motg->pdata->swfi_latency)
2366 pm_qos_add_request(&motg->pm_qos_req_dma,
2367 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302368 /*
2369 * If USB Core is running its protocol engine based on CORE CLK,
2370 * CORE CLK must be running at >55Mhz for correct HSUSB
2371 * operation and USB core cannot tolerate frequency changes on
2372 * CORE CLK. For such USB cores, vote for maximum clk frequency
2373 * on pclk source
2374 */
2375 if (motg->pdata->pclk_src_name) {
2376 motg->pclk_src = clk_get(&pdev->dev,
2377 motg->pdata->pclk_src_name);
2378 if (IS_ERR(motg->pclk_src))
2379 goto put_clk;
2380 clk_set_rate(motg->pclk_src, INT_MAX);
2381 clk_enable(motg->pclk_src);
2382 } else
2383 motg->pclk_src = ERR_PTR(-ENOENT);
2384
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302385 motg->pclk = clk_get(&pdev->dev, "usb_hs_pclk");
2386 if (IS_ERR(motg->pclk)) {
2387 dev_err(&pdev->dev, "failed to get usb_hs_pclk\n");
2388 ret = PTR_ERR(motg->pclk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302389 goto put_pclk_src;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302390 }
2391
Amit Blay02eff132011-09-21 16:46:24 +03002392 motg->system_clk = clk_get(&pdev->dev, "usb_hs_system_clk");
2393 if (!IS_ERR(motg->system_clk))
2394 clk_enable(motg->system_clk);
2395
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302396 /*
2397 * USB core clock is not present on all MSM chips. This
2398 * clock is introduced to remove the dependency on AXI
2399 * bus frequency.
2400 */
2401 motg->core_clk = clk_get(&pdev->dev, "usb_hs_core_clk");
2402 if (IS_ERR(motg->core_clk))
2403 motg->core_clk = NULL;
2404
2405 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2406 if (!res) {
2407 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2408 ret = -ENODEV;
2409 goto put_core_clk;
2410 }
2411
2412 motg->regs = ioremap(res->start, resource_size(res));
2413 if (!motg->regs) {
2414 dev_err(&pdev->dev, "ioremap failed\n");
2415 ret = -ENOMEM;
2416 goto put_core_clk;
2417 }
2418 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2419
2420 motg->irq = platform_get_irq(pdev, 0);
2421 if (!motg->irq) {
2422 dev_err(&pdev->dev, "platform_get_irq failed\n");
2423 ret = -ENODEV;
2424 goto free_regs;
2425 }
2426
Anji jonnala7da3f262011-12-02 17:22:14 -08002427 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2428 if (IS_ERR(motg->xo_handle)) {
2429 dev_err(&pdev->dev, "%s not able to get the handle "
2430 "to vote for TCXO D0 buffer\n", __func__);
2431 ret = PTR_ERR(motg->xo_handle);
2432 goto free_regs;
2433 }
2434
2435 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2436 if (ret) {
2437 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2438 "D0 buffer%d\n", __func__, ret);
2439 goto free_xo_handle;
2440 }
2441
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302442 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302443
2444 ret = msm_hsusb_init_vddcx(motg, 1);
2445 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002446 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002447 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302448 }
2449
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002450 ret = msm_hsusb_config_vddcx(1);
2451 if (ret) {
2452 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2453 goto free_init_vddcx;
2454 }
2455
Anji jonnala11aa5c42011-05-04 10:19:48 +05302456 ret = msm_hsusb_ldo_init(motg, 1);
2457 if (ret) {
2458 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302460 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461
2462 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302463 if (ret) {
2464 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002465 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302466 }
2467
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302468 if (motg->core_clk)
2469 clk_enable(motg->core_clk);
2470
2471 writel(0, USB_USBINTR);
2472 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473 /* Ensure that above STOREs are completed before enabling interrupts */
2474 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302477 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302478 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302479 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2480 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302481 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2482 "msm_otg", motg);
2483 if (ret) {
2484 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002485 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302486 }
2487
2488 otg->init = msm_otg_reset;
2489 otg->set_host = msm_otg_set_host;
2490 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302491 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302492 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302493
2494 otg->io_ops = &msm_otg_io_ops;
2495
2496 ret = otg_set_transceiver(&motg->otg);
2497 if (ret) {
2498 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2499 goto free_irq;
2500 }
2501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2503 if (motg->pdata->pmic_id_irq) {
2504 ret = request_irq(motg->pdata->pmic_id_irq,
2505 msm_pmic_id_irq,
2506 IRQF_TRIGGER_RISING |
2507 IRQF_TRIGGER_FALLING,
2508 "msm_otg", motg);
2509 if (ret) {
2510 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2511 goto remove_otg;
2512 }
2513 } else {
2514 ret = -ENODEV;
2515 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2516 goto remove_otg;
2517 }
2518 }
2519
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302520 msm_hsusb_mhl_switch_enable(motg, 1);
2521
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302522 platform_set_drvdata(pdev, motg);
2523 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002524 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302525
Anji jonnalad270e2d2011-08-09 11:28:32 +05302526 ret = msm_otg_debugfs_init(motg);
2527 if (ret)
2528 dev_dbg(&pdev->dev, "mode debugfs file is"
2529 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302530
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2532 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2533
Amit Blay58b31472011-11-18 09:39:39 +02002534 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2535 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002536 motg->pdata->pmic_id_irq)
Amit Blay58b31472011-11-18 09:39:39 +02002537 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538 ALLOW_PHY_RETENTION |
2539 ALLOW_PHY_COMP_DISABLE;
2540
Amit Blay58b31472011-11-18 09:39:39 +02002541 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2542 motg->caps = ALLOW_PHY_RETENTION;
2543 }
2544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002545 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302546 pm_runtime_set_active(&pdev->dev);
2547 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302548
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302549 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002550
2551remove_otg:
2552 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302553free_irq:
2554 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002555destroy_wlock:
2556 wake_lock_destroy(&motg->wlock);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302557 clk_disable(motg->pclk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002558 msm_hsusb_ldo_enable(motg, 0);
2559free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302560 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002561free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302562 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002563devote_xo_handle:
2564 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2565free_xo_handle:
2566 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302567free_regs:
2568 iounmap(motg->regs);
2569put_core_clk:
2570 if (motg->core_clk)
2571 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002572
2573 if (!IS_ERR(motg->system_clk)) {
2574 clk_disable(motg->system_clk);
2575 clk_put(motg->system_clk);
2576 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302577put_pclk_src:
2578 if (!IS_ERR(motg->pclk_src)) {
2579 clk_disable(motg->pclk_src);
2580 clk_put(motg->pclk_src);
2581 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302582put_clk:
2583 clk_put(motg->clk);
2584put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002585 if (!IS_ERR(motg->phy_reset_clk))
2586 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302587free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302588 if (motg->pdata->swfi_latency)
2589 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302590 kfree(motg);
2591 return ret;
2592}
2593
2594static int __devexit msm_otg_remove(struct platform_device *pdev)
2595{
2596 struct msm_otg *motg = platform_get_drvdata(pdev);
2597 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302598 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302599
2600 if (otg->host || otg->gadget)
2601 return -EBUSY;
2602
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302603 if (pdev->dev.of_node)
2604 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002605 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2606 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302607 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302608 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302609 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302610
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302611 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302612
2613 device_init_wakeup(&pdev->dev, 0);
2614 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002615 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302616
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302617 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002618 if (motg->pdata->pmic_id_irq)
2619 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302620 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302621 free_irq(motg->irq, motg);
2622
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302623 /*
2624 * Put PHY in low power mode.
2625 */
2626 ulpi_read(otg, 0x14);
2627 ulpi_write(otg, 0x08, 0x09);
2628
2629 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2630 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2631 if (readl(USB_PORTSC) & PORTSC_PHCD)
2632 break;
2633 udelay(1);
2634 cnt++;
2635 }
2636 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2637 dev_err(otg->dev, "Unable to suspend PHY\n");
2638
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302639 clk_disable(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302640 if (motg->core_clk)
2641 clk_disable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +02002642 if (!IS_ERR(motg->system_clk))
2643 clk_disable(motg->system_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +05302644 if (!IS_ERR(motg->pclk_src)) {
2645 clk_disable(motg->pclk_src);
2646 clk_put(motg->pclk_src);
2647 }
Anji jonnala7da3f262011-12-02 17:22:14 -08002648 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002649 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302650 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002651 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302652
2653 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302654 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302655
Amit Blay02eff132011-09-21 16:46:24 +03002656 if (!IS_ERR(motg->phy_reset_clk))
2657 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302658 clk_put(motg->pclk);
2659 clk_put(motg->clk);
2660 if (motg->core_clk)
2661 clk_put(motg->core_clk);
Amit Blay02eff132011-09-21 16:46:24 +03002662 if (!IS_ERR(motg->system_clk))
2663 clk_put(motg->system_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302664
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302665 if (motg->pdata->swfi_latency)
2666 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302667
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302668 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302669 return 0;
2670}
2671
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302672#ifdef CONFIG_PM_RUNTIME
2673static int msm_otg_runtime_idle(struct device *dev)
2674{
2675 struct msm_otg *motg = dev_get_drvdata(dev);
2676 struct otg_transceiver *otg = &motg->otg;
2677
2678 dev_dbg(dev, "OTG runtime idle\n");
2679
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302680 if (otg->state == OTG_STATE_UNDEFINED)
2681 return -EAGAIN;
2682 else
2683 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302684}
2685
2686static int msm_otg_runtime_suspend(struct device *dev)
2687{
2688 struct msm_otg *motg = dev_get_drvdata(dev);
2689
2690 dev_dbg(dev, "OTG runtime suspend\n");
2691 return msm_otg_suspend(motg);
2692}
2693
2694static int msm_otg_runtime_resume(struct device *dev)
2695{
2696 struct msm_otg *motg = dev_get_drvdata(dev);
2697
2698 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302699 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302700 return msm_otg_resume(motg);
2701}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302702#endif
2703
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302704#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302705static int msm_otg_pm_suspend(struct device *dev)
2706{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302707 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302708
2709 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302710
2711#ifdef CONFIG_PM_RUNTIME
2712 ret = pm_runtime_suspend(dev);
2713 if (ret > 0)
2714 ret = 0;
2715#else
2716 ret = msm_otg_suspend(dev_get_drvdata(dev));
2717#endif
2718 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302719}
2720
2721static int msm_otg_pm_resume(struct device *dev)
2722{
2723 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302724
2725 dev_dbg(dev, "OTG PM resume\n");
2726
Manu Gautamf284c052011-09-08 16:52:48 +05302727#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302728 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302729 * Do not resume hardware as part of system resume,
2730 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302731 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002732 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302733#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302734
Manu Gautamf284c052011-09-08 16:52:48 +05302735 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302736}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302737#endif
2738
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302739#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302740static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302741 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2742 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2743 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302744};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302745#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302746
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302747static struct of_device_id msm_otg_dt_match[] = {
2748 { .compatible = "qcom,hsusb-otg",
2749 },
2750 {}
2751};
2752
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302753static struct platform_driver msm_otg_driver = {
2754 .remove = __devexit_p(msm_otg_remove),
2755 .driver = {
2756 .name = DRIVER_NAME,
2757 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302758#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302759 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302760#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302761 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302762 },
2763};
2764
2765static int __init msm_otg_init(void)
2766{
2767 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2768}
2769
2770static void __exit msm_otg_exit(void)
2771{
2772 platform_driver_unregister(&msm_otg_driver);
2773}
2774
2775module_init(msm_otg_init);
2776module_exit(msm_otg_exit);
2777
2778MODULE_LICENSE("GPL v2");
2779MODULE_DESCRIPTION("MSM USB transceiver driver");