blob: f2279be1cc70dcb84ea444bb54759c7a6d3c9e87 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053041
42#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080043#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#define MSM_USB_BASE (motg->regs)
46#define DRIVER_NAME "msm_otg"
47
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053048#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053049#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053050
51#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
52#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
53#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
54#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
55
56#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
57#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
58#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
59#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
60
Vamsi Krishna132b2762011-11-11 16:09:20 -080061#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053062#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
63
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053064static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053066static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053068/* Prevent idle power collapse(pc) while operating in peripheral mode */
69static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
70{
71 struct msm_otg_platform_data *pdata = dev->pdata;
72 u32 swfi_latency = 0;
73
74 if (!pdata || !pdata->swfi_latency)
75 return;
76
77 swfi_latency = pdata->swfi_latency + 1;
78
79 if (vote)
80 pm_qos_update_request(&dev->pm_qos_req_dma,
81 swfi_latency);
82 else
83 pm_qos_update_request(&dev->pm_qos_req_dma,
84 PM_QOS_DEFAULT_VALUE);
85}
86
Anji jonnala11aa5c42011-05-04 10:19:48 +053087static struct regulator *hsusb_3p3;
88static struct regulator *hsusb_1p8;
89static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053090static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053091
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053092static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053093static inline bool aca_enabled(void)
94{
95#ifdef CONFIG_USB_MSM_ACA
96 return true;
97#else
98 return debug_aca_enabled;
99#endif
100}
101
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
103{
104 int ret = 0;
105
106 if (init) {
107 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
108 if (IS_ERR(hsusb_vddcx)) {
109 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
110 return PTR_ERR(hsusb_vddcx);
111 }
112
113 ret = regulator_set_voltage(hsusb_vddcx,
114 USB_PHY_VDD_DIG_VOL_MIN,
115 USB_PHY_VDD_DIG_VOL_MAX);
116 if (ret) {
117 dev_err(motg->otg.dev, "unable to set the voltage "
118 "for hsusb vddcx\n");
119 regulator_put(hsusb_vddcx);
120 return ret;
121 }
122
123 ret = regulator_enable(hsusb_vddcx);
124 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 regulator_set_voltage(hsusb_vddcx, 0,
126 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
129 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530130 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530136 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137 return ret;
138 }
139
140 ret = regulator_set_voltage(hsusb_vddcx, 0,
141 USB_PHY_VDD_DIG_VOL_MIN);
142 if (ret) {
143 dev_err(motg->otg.dev, "unable to set the voltage"
144 "for hsusb vddcx\n");
145 return ret;
146 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530147
148 regulator_put(hsusb_vddcx);
149 }
150
151 return ret;
152}
153
154static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
155{
156 int rc = 0;
157
158 if (init) {
159 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
160 if (IS_ERR(hsusb_3p3)) {
161 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
162 return PTR_ERR(hsusb_3p3);
163 }
164
165 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
166 USB_PHY_3P3_VOL_MAX);
167 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700168 dev_err(motg->otg.dev, "unable to set voltage level for"
169 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530170 goto put_3p3;
171 }
172 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
173 if (IS_ERR(hsusb_1p8)) {
174 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
175 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700176 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530177 }
178 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
179 USB_PHY_1P8_VOL_MAX);
180 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 dev_err(motg->otg.dev, "unable to set voltage level for"
182 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530183 goto put_1p8;
184 }
185
186 return 0;
187 }
188
Anji jonnala11aa5c42011-05-04 10:19:48 +0530189put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530191 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192put_3p3_lpm:
193 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530194put_3p3:
195 regulator_put(hsusb_3p3);
196 return rc;
197}
198
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530199#ifdef CONFIG_PM_SLEEP
200#define USB_PHY_SUSP_DIG_VOL 500000
201static int msm_hsusb_config_vddcx(int high)
202{
203 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
204 int min_vol;
205 int ret;
206
207 if (high)
208 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
209 else
210 min_vol = USB_PHY_SUSP_DIG_VOL;
211
212 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
213 if (ret) {
214 pr_err("%s: unable to set the voltage for regulator "
215 "HSUSB_VDDCX\n", __func__);
216 return ret;
217 }
218
219 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
220
221 return ret;
222}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700223#else
224static int msm_hsusb_config_vddcx(int high)
225{
226 return 0;
227}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530228#endif
229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530231{
232 int ret = 0;
233
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530234 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530235 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
236 return -ENODEV;
237 }
238
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530239 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530240 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
241 return -ENODEV;
242 }
243
244 if (on) {
245 ret = regulator_set_optimum_mode(hsusb_1p8,
246 USB_PHY_1P8_HPM_LOAD);
247 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530249 "HSUSB_1p8\n", __func__);
250 return ret;
251 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252
253 ret = regulator_enable(hsusb_1p8);
254 if (ret) {
255 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
256 __func__);
257 regulator_set_optimum_mode(hsusb_1p8, 0);
258 return ret;
259 }
260
Anji jonnala11aa5c42011-05-04 10:19:48 +0530261 ret = regulator_set_optimum_mode(hsusb_3p3,
262 USB_PHY_3P3_HPM_LOAD);
263 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 regulator_set_optimum_mode(hsusb_1p8, 0);
267 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530268 return ret;
269 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270
271 ret = regulator_enable(hsusb_3p3);
272 if (ret) {
273 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
274 __func__);
275 regulator_set_optimum_mode(hsusb_3p3, 0);
276 regulator_set_optimum_mode(hsusb_1p8, 0);
277 regulator_disable(hsusb_1p8);
278 return ret;
279 }
280
Anji jonnala11aa5c42011-05-04 10:19:48 +0530281 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700282 ret = regulator_disable(hsusb_1p8);
283 if (ret) {
284 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
285 __func__);
286 return ret;
287 }
288
289 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530290 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530292 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293
294 ret = regulator_disable(hsusb_3p3);
295 if (ret) {
296 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
297 __func__);
298 return ret;
299 }
300 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530301 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700302 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530303 "HSUSB_3p3\n", __func__);
304 }
305
306 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
307 return ret < 0 ? ret : 0;
308}
309
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530310static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
311{
312 static struct regulator *mhl_analog_switch;
313 struct msm_otg_platform_data *pdata = motg->pdata;
314
315 if (!pdata->mhl_enable)
316 return;
317
318 if (on) {
319 mhl_analog_switch = regulator_get(motg->otg.dev,
320 "mhl_ext_3p3v");
321 if (IS_ERR(mhl_analog_switch)) {
322 pr_err("Unable to get mhl_analog_switch\n");
323 return;
324 }
325
326 if (regulator_enable(mhl_analog_switch)) {
327 pr_err("unable to enable mhl_analog_switch\n");
328 goto put_analog_switch;
329 }
330 return;
331 }
332
333 regulator_disable(mhl_analog_switch);
334put_analog_switch:
335 regulator_put(mhl_analog_switch);
336}
337
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530338static int ulpi_read(struct otg_transceiver *otg, u32 reg)
339{
340 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
341 int cnt = 0;
342
343 /* initiate read operation */
344 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
345 USB_ULPI_VIEWPORT);
346
347 /* wait for completion */
348 while (cnt < ULPI_IO_TIMEOUT_USEC) {
349 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
350 break;
351 udelay(1);
352 cnt++;
353 }
354
355 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
356 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
357 readl(USB_ULPI_VIEWPORT));
358 return -ETIMEDOUT;
359 }
360 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
361}
362
363static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
364{
365 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
366 int cnt = 0;
367
368 /* initiate write operation */
369 writel(ULPI_RUN | ULPI_WRITE |
370 ULPI_ADDR(reg) | ULPI_DATA(val),
371 USB_ULPI_VIEWPORT);
372
373 /* wait for completion */
374 while (cnt < ULPI_IO_TIMEOUT_USEC) {
375 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
376 break;
377 udelay(1);
378 cnt++;
379 }
380
381 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
382 dev_err(otg->dev, "ulpi_write: timeout\n");
383 return -ETIMEDOUT;
384 }
385 return 0;
386}
387
388static struct otg_io_access_ops msm_otg_io_ops = {
389 .read = ulpi_read,
390 .write = ulpi_write,
391};
392
393static void ulpi_init(struct msm_otg *motg)
394{
395 struct msm_otg_platform_data *pdata = motg->pdata;
396 int *seq = pdata->phy_init_seq;
397
398 if (!seq)
399 return;
400
401 while (seq[0] >= 0) {
402 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
403 seq[0], seq[1]);
404 ulpi_write(&motg->otg, seq[0], seq[1]);
405 seq += 2;
406 }
407}
408
409static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
410{
411 int ret;
412
413 if (assert) {
414 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
415 if (ret)
416 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
417 } else {
418 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
419 if (ret)
420 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
421 }
422 return ret;
423}
424
425static int msm_otg_phy_clk_reset(struct msm_otg *motg)
426{
427 int ret;
428
Amit Blay02eff132011-09-21 16:46:24 +0300429 if (IS_ERR(motg->phy_reset_clk))
430 return 0;
431
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
433 if (ret) {
434 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
435 return ret;
436 }
437 usleep_range(10000, 12000);
438 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
439 if (ret)
440 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
441 return ret;
442}
443
444static int msm_otg_phy_reset(struct msm_otg *motg)
445{
446 u32 val;
447 int ret;
448 int retries;
449
450 ret = msm_otg_link_clk_reset(motg, 1);
451 if (ret)
452 return ret;
453 ret = msm_otg_phy_clk_reset(motg);
454 if (ret)
455 return ret;
456 ret = msm_otg_link_clk_reset(motg, 0);
457 if (ret)
458 return ret;
459
460 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
461 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
462
463 for (retries = 3; retries > 0; retries--) {
464 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
465 ULPI_CLR(ULPI_FUNC_CTRL));
466 if (!ret)
467 break;
468 ret = msm_otg_phy_clk_reset(motg);
469 if (ret)
470 return ret;
471 }
472 if (!retries)
473 return -ETIMEDOUT;
474
475 /* This reset calibrates the phy, if the above write succeeded */
476 ret = msm_otg_phy_clk_reset(motg);
477 if (ret)
478 return ret;
479
480 for (retries = 3; retries > 0; retries--) {
481 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
482 if (ret != -ETIMEDOUT)
483 break;
484 ret = msm_otg_phy_clk_reset(motg);
485 if (ret)
486 return ret;
487 }
488 if (!retries)
489 return -ETIMEDOUT;
490
491 dev_info(motg->otg.dev, "phy_reset: success\n");
492 return 0;
493}
494
495#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530496static int msm_otg_link_reset(struct msm_otg *motg)
497{
498 int cnt = 0;
499
500 writel_relaxed(USBCMD_RESET, USB_USBCMD);
501 while (cnt < LINK_RESET_TIMEOUT_USEC) {
502 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
503 break;
504 udelay(1);
505 cnt++;
506 }
507 if (cnt >= LINK_RESET_TIMEOUT_USEC)
508 return -ETIMEDOUT;
509
510 /* select ULPI phy */
511 writel_relaxed(0x80000000, USB_PORTSC);
512 writel_relaxed(0x0, USB_AHBBURST);
513 writel_relaxed(0x00, USB_AHBMODE);
514
515 return 0;
516}
517
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530518static int msm_otg_reset(struct otg_transceiver *otg)
519{
520 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
521 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530522 int ret;
523 u32 val = 0;
524 u32 ulpi_val = 0;
525
Ofir Cohen4da266f2012-01-03 10:19:29 +0200526 /*
527 * USB PHY and Link reset also reset the USB BAM.
528 * Thus perform reset operation only once to avoid
529 * USB BAM reset on other cases e.g. USB cable disconnections.
530 */
531 if (pdata->disable_reset_on_disconnect) {
532 if (motg->reset_counter)
533 return 0;
534 else
535 motg->reset_counter++;
536 }
537
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530539 ret = msm_otg_phy_reset(motg);
540 if (ret) {
541 dev_err(otg->dev, "phy_reset failed\n");
542 return ret;
543 }
544
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530545 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530546 ret = msm_otg_link_reset(motg);
547 if (ret) {
548 dev_err(otg->dev, "link reset failed\n");
549 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530550 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530552
553 ulpi_init(motg);
554
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700555 /* Ensure that RESET operation is completed before turning off clock */
556 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530557
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700558 clk_disable(motg->clk);
559
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530560 if (pdata->otg_control == OTG_PHY_CONTROL) {
561 val = readl_relaxed(USB_OTGSC);
562 if (pdata->mode == USB_OTG) {
563 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
564 val |= OTGSC_IDIE | OTGSC_BSVIE;
565 } else if (pdata->mode == USB_PERIPHERAL) {
566 ulpi_val = ULPI_INT_SESS_VALID;
567 val |= OTGSC_BSVIE;
568 }
569 writel_relaxed(val, USB_OTGSC);
570 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530574 return 0;
575}
576
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530577static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
578{
579 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
580
581 /*
582 * Allow bus suspend only for host mode. Device mode bus suspend
583 * is not implemented yet.
584 */
585 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530586 /*
587 * ID_GND --> ID_A transition can not be detected in LPM.
588 * Disallow host bus suspend when ACA is enabled.
589 */
590 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530591 pm_runtime_put(otg->dev);
592 else
593 pm_runtime_resume(otg->dev);
594 }
595
596 return 0;
597}
598
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530599#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530600#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
601
602#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530603static int msm_otg_suspend(struct msm_otg *motg)
604{
605 struct otg_transceiver *otg = &motg->otg;
606 struct usb_bus *bus = otg->host;
607 struct msm_otg_platform_data *pdata = motg->pdata;
608 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530609 bool host_bus_suspend;
610 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800611 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530612 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530613
614 if (atomic_read(&motg->in_lpm))
615 return 0;
616
617 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530618 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530620 * Chipidea 45-nm PHY suspend sequence:
621 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530622 * Interrupt Latch Register auto-clear feature is not present
623 * in all PHY versions. Latch register is clear on read type.
624 * Clear latch register to avoid spurious wakeup from
625 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530626 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530627 * PHY comparators are disabled when PHY enters into low power
628 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
629 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
630 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530631 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530632 * PLL is not turned off when PHY enters into low power mode (LPM).
633 * Disable PLL for maximum power savings.
634 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530635
636 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
637 ulpi_read(otg, 0x14);
638 if (pdata->otg_control == OTG_PHY_CONTROL)
639 ulpi_write(otg, 0x01, 0x30);
640 ulpi_write(otg, 0x08, 0x09);
641 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530642
643 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 * Turn off the OTG comparators, if depends on PMIC for
645 * VBUS and ID notifications.
646 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530647 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 ulpi_write(otg, OTG_COMP_DISABLE,
649 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
650 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
651 }
652
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530653 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530654 * PHY may take some time or even fail to enter into low power
655 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
656 * in failure case.
657 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530658 portsc = readl_relaxed(USB_PORTSC);
659 if (!(portsc & PORTSC_PHCD)) {
660 writel_relaxed(portsc | PORTSC_PHCD,
661 USB_PORTSC);
662 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
663 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
664 break;
665 udelay(1);
666 cnt++;
667 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530668 }
669
670 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
671 dev_err(otg->dev, "Unable to suspend PHY\n");
672 msm_otg_reset(otg);
673 enable_irq(motg->irq);
674 return -ETIMEDOUT;
675 }
676
677 /*
678 * PHY has capability to generate interrupt asynchronously in low
679 * power mode (LPM). This interrupt is level triggered. So USB IRQ
680 * line must be disabled till async interrupt enable bit is cleared
681 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
682 * block data communication from PHY.
683 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530684 cmd_val = readl_relaxed(USB_USBCMD);
685 if (host_bus_suspend)
686 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
687 else
688 cmd_val |= ULPI_STP_CTRL;
689 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530690
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530691 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200692 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
693 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
694 /* Enable PHY HV interrupts to wake MPM/Link */
695 phy_ctrl_val |=
696 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
697
698 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 motg->lpm_flags |= PHY_RETENTIONED;
700 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 /* Ensure that above operation is completed before turning off clocks */
703 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530704 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800705 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530706
Anji jonnala7da3f262011-12-02 17:22:14 -0800707 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
708 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
709 if (ret)
710 dev_err(otg->dev, "%s failed to devote for "
711 "TCXO D0 buffer%d\n", __func__, ret);
712
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530713 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700714 msm_hsusb_ldo_enable(motg, 0);
715 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530716 }
717
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530718 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530720 msm_hsusb_mhl_switch_enable(motg, 0);
721 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722
723 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530724 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700725 if (motg->pdata->pmic_id_irq)
726 enable_irq_wake(motg->pdata->pmic_id_irq);
727 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530728 if (bus)
729 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
730
731 atomic_set(&motg->in_lpm, 1);
732 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700733 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530734
735 dev_info(otg->dev, "USB in low power mode\n");
736
737 return 0;
738}
739
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530740static int msm_otg_resume(struct msm_otg *motg)
741{
742 struct otg_transceiver *otg = &motg->otg;
743 struct usb_bus *bus = otg->host;
744 int cnt = 0;
745 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200746 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800747 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530748
749 if (!atomic_read(&motg->in_lpm))
750 return 0;
751
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800753
754 /* Vote for TCXO when waking up the phy */
755 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
756 if (ret)
757 dev_err(otg->dev, "%s failed to vote for "
758 "TCXO D0 buffer%d\n", __func__, ret);
759
Manu Gautam5143b252012-01-05 19:25:23 -0800760 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200761
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
765 msm_hsusb_ldo_enable(motg, 1);
766 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
767 }
768
769 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530770 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530771 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200772 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
773 phy_ctrl_val |= PHY_RETEN;
774 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
775 /* Disable PHY HV interrupts */
776 phy_ctrl_val &=
777 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
778 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700779 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530780 }
781
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530782 temp = readl(USB_USBCMD);
783 temp &= ~ASYNC_INTR_CTRL;
784 temp &= ~ULPI_STP_CTRL;
785 writel(temp, USB_USBCMD);
786
787 /*
788 * PHY comes out of low power mode (LPM) in case of wakeup
789 * from asynchronous interrupt.
790 */
791 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
792 goto skip_phy_resume;
793
794 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
795 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
796 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
797 break;
798 udelay(1);
799 cnt++;
800 }
801
802 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
803 /*
804 * This is a fatal error. Reset the link and
805 * PHY. USB state can not be restored. Re-insertion
806 * of USB cable is the only way to get USB working.
807 */
808 dev_err(otg->dev, "Unable to resume USB."
809 "Re-plugin the cable\n");
810 msm_otg_reset(otg);
811 }
812
813skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700814 /* Turn on the OTG comparators on resume */
815 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
816 ulpi_write(otg, OTG_COMP_DISABLE,
817 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
818 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
819 }
820 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530821 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 if (motg->pdata->pmic_id_irq)
823 disable_irq_wake(motg->pdata->pmic_id_irq);
824 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530825 if (bus)
826 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
827
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530828 atomic_set(&motg->in_lpm, 0);
829
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530830 if (motg->async_int) {
831 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530832 enable_irq(motg->irq);
833 }
834
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530835 dev_info(otg->dev, "USB exited from low power mode\n");
836
837 return 0;
838}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530839#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530840
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530841static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
842{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530843 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
844 motg->chg_type == USB_ACA_A_CHARGER ||
845 motg->chg_type == USB_ACA_B_CHARGER ||
846 motg->chg_type == USB_ACA_C_CHARGER) &&
847 mA > IDEV_ACA_CHG_LIMIT)
848 mA = IDEV_ACA_CHG_LIMIT;
849
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530850 if (motg->cur_power == mA)
851 return;
852
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530853 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 pm8921_charger_vbus_draw(mA);
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530855 motg->cur_power = mA;
856}
857
858static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
859{
860 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
861
862 /*
863 * Gadget driver uses set_power method to notify about the
864 * available current based on suspend/configured states.
865 *
866 * IDEV_CHG can be drawn irrespective of suspend/un-configured
867 * states when CDP/ACA is connected.
868 */
869 if (motg->chg_type == USB_SDP_CHARGER)
870 msm_otg_notify_charger(motg, mA);
871
872 return 0;
873}
874
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530875static void msm_otg_start_host(struct otg_transceiver *otg, int on)
876{
877 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
878 struct msm_otg_platform_data *pdata = motg->pdata;
879 struct usb_hcd *hcd;
880
881 if (!otg->host)
882 return;
883
884 hcd = bus_to_hcd(otg->host);
885
886 if (on) {
887 dev_dbg(otg->dev, "host on\n");
888
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530889 /*
890 * Some boards have a switch cotrolled by gpio
891 * to enable/disable internal HUB. Enable internal
892 * HUB before kicking the host.
893 */
894 if (pdata->setup_gpio)
895 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530896 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530897 } else {
898 dev_dbg(otg->dev, "host off\n");
899
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530900 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530901 /* HCD core reset all bits of PORTSC. select ULPI phy */
902 writel_relaxed(0x80000000, USB_PORTSC);
903
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530904 if (pdata->setup_gpio)
905 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530906 }
907}
908
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909static int msm_otg_usbdev_notify(struct notifier_block *self,
910 unsigned long action, void *priv)
911{
912 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530913 struct usb_device *udev = priv;
914
915 if (!aca_enabled())
916 goto out;
917
918 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
919 goto out;
920
921 if (udev->bus != motg->otg.host)
922 goto out;
923 /*
924 * Interested in devices connected directly to the root hub.
925 * ACA dock can supply IDEV_CHG irrespective devices connected
926 * on the accessory port.
927 */
928 if (!udev->parent || udev->parent->parent ||
929 motg->chg_type == USB_ACA_DOCK_CHARGER)
930 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700931
932 switch (action) {
933 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530934 usb_disable_autosuspend(udev);
935 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700937 if (udev->actconfig)
938 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
939 else
940 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530941 break;
942 case USB_DEVICE_REMOVE:
943 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700944 break;
945 default:
946 break;
947 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530948 if (test_bit(ID_A, &motg->inputs))
949 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
950 motg->mA_port);
951out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952 return NOTIFY_OK;
953}
954
Mayank Ranae3926882011-12-26 09:47:54 +0530955static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
956{
957 int ret;
958 static bool vbus_is_on;
959
960 if (vbus_is_on == on)
961 return;
962
963 if (motg->pdata->vbus_power) {
964 motg->pdata->vbus_power(on);
965 return;
966 }
967
968 if (!vbus_otg) {
969 pr_err("vbus_otg is NULL.");
970 return;
971 }
972
973 if (on) {
974 ret = regulator_enable(vbus_otg);
975 if (ret) {
976 pr_err("unable to enable vbus_otg\n");
977 return;
978 }
979 vbus_is_on = true;
980 } else {
981 ret = regulator_disable(vbus_otg);
982 if (ret) {
983 pr_err("unable to disable vbus_otg\n");
984 return;
985 }
986 vbus_is_on = false;
987 }
988}
989
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530990static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
991{
992 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
993 struct usb_hcd *hcd;
994
995 /*
996 * Fail host registration if this board can support
997 * only peripheral configuration.
998 */
999 if (motg->pdata->mode == USB_PERIPHERAL) {
1000 dev_info(otg->dev, "Host mode is not supported\n");
1001 return -ENODEV;
1002 }
1003
Mayank Ranae3926882011-12-26 09:47:54 +05301004 if (!motg->pdata->vbus_power && host) {
1005 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1006 if (IS_ERR(vbus_otg)) {
1007 pr_err("Unable to get vbus_otg\n");
1008 return -ENODEV;
1009 }
1010 }
1011
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301012 if (!host) {
1013 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301014 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001015 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301016 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301017 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301018 otg->host = NULL;
1019 otg->state = OTG_STATE_UNDEFINED;
1020 schedule_work(&motg->sm_work);
1021 } else {
1022 otg->host = NULL;
1023 }
1024
Mayank Ranae3926882011-12-26 09:47:54 +05301025 if (vbus_otg)
1026 regulator_put(vbus_otg);
1027
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301028 return 0;
1029 }
1030
1031 hcd = bus_to_hcd(host);
1032 hcd->power_budget = motg->pdata->power_budget;
1033
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001034 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1035 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301036 otg->host = host;
1037 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1038
1039 /*
1040 * Kick the state machine work, if peripheral is not supported
1041 * or peripheral is already registered with us.
1042 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301043 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1044 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301045 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301046 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301047
1048 return 0;
1049}
1050
1051static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1052{
1053 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1054 struct msm_otg_platform_data *pdata = motg->pdata;
1055
1056 if (!otg->gadget)
1057 return;
1058
1059 if (on) {
1060 dev_dbg(otg->dev, "gadget on\n");
1061 /*
1062 * Some boards have a switch cotrolled by gpio
1063 * to enable/disable internal HUB. Disable internal
1064 * HUB before kicking the gadget.
1065 */
1066 if (pdata->setup_gpio)
1067 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301068 /*
1069 * vote for minimum dma_latency to prevent idle
1070 * power collapse(pc) while running in peripheral mode.
1071 */
1072 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301073 usb_gadget_vbus_connect(otg->gadget);
1074 } else {
1075 dev_dbg(otg->dev, "gadget off\n");
1076 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301077 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301078 if (pdata->setup_gpio)
1079 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1080 }
1081
1082}
1083
1084static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1085 struct usb_gadget *gadget)
1086{
1087 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1088
1089 /*
1090 * Fail peripheral registration if this board can support
1091 * only host configuration.
1092 */
1093 if (motg->pdata->mode == USB_HOST) {
1094 dev_info(otg->dev, "Peripheral mode is not supported\n");
1095 return -ENODEV;
1096 }
1097
1098 if (!gadget) {
1099 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301100 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301101 msm_otg_start_peripheral(otg, 0);
1102 otg->gadget = NULL;
1103 otg->state = OTG_STATE_UNDEFINED;
1104 schedule_work(&motg->sm_work);
1105 } else {
1106 otg->gadget = NULL;
1107 }
1108
1109 return 0;
1110 }
1111 otg->gadget = gadget;
1112 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1113
1114 /*
1115 * Kick the state machine work, if host is not supported
1116 * or host is already registered with us.
1117 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301118 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1119 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301121 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301122
1123 return 0;
1124}
1125
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001126static bool msm_chg_aca_detect(struct msm_otg *motg)
1127{
1128 struct otg_transceiver *otg = &motg->otg;
1129 u32 int_sts;
1130 bool ret = false;
1131
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301132 if (!aca_enabled())
1133 goto out;
1134
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001135 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1136 goto out;
1137
1138 int_sts = ulpi_read(otg, 0x87);
1139 switch (int_sts & 0x1C) {
1140 case 0x08:
1141 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1142 dev_dbg(otg->dev, "ID_A\n");
1143 motg->chg_type = USB_ACA_A_CHARGER;
1144 motg->chg_state = USB_CHG_STATE_DETECTED;
1145 clear_bit(ID_B, &motg->inputs);
1146 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301147 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001148 ret = true;
1149 }
1150 break;
1151 case 0x0C:
1152 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1153 dev_dbg(otg->dev, "ID_B\n");
1154 motg->chg_type = USB_ACA_B_CHARGER;
1155 motg->chg_state = USB_CHG_STATE_DETECTED;
1156 clear_bit(ID_A, &motg->inputs);
1157 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301158 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001159 ret = true;
1160 }
1161 break;
1162 case 0x10:
1163 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1164 dev_dbg(otg->dev, "ID_C\n");
1165 motg->chg_type = USB_ACA_C_CHARGER;
1166 motg->chg_state = USB_CHG_STATE_DETECTED;
1167 clear_bit(ID_A, &motg->inputs);
1168 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301169 set_bit(ID, &motg->inputs);
1170 ret = true;
1171 }
1172 break;
1173 case 0x04:
1174 if (test_and_clear_bit(ID, &motg->inputs)) {
1175 dev_dbg(otg->dev, "ID_GND\n");
1176 motg->chg_type = USB_INVALID_CHARGER;
1177 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1178 clear_bit(ID_A, &motg->inputs);
1179 clear_bit(ID_B, &motg->inputs);
1180 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001181 ret = true;
1182 }
1183 break;
1184 default:
1185 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1186 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301187 test_and_clear_bit(ID_C, &motg->inputs) |
1188 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301190 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001191 motg->chg_type = USB_INVALID_CHARGER;
1192 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1193 }
1194 }
1195out:
1196 return ret;
1197}
1198
1199static void msm_chg_enable_aca_det(struct msm_otg *motg)
1200{
1201 struct otg_transceiver *otg = &motg->otg;
1202
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301203 if (!aca_enabled())
1204 return;
1205
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206 switch (motg->pdata->phy_type) {
1207 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301208 /* Disable ID_GND in link and PHY */
1209 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1210 OTGSC_IDIE), USB_OTGSC);
1211 ulpi_write(otg, 0x01, 0x0C);
1212 ulpi_write(otg, 0x10, 0x0F);
1213 ulpi_write(otg, 0x10, 0x12);
1214 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001215 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301216 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001217 break;
1218 default:
1219 break;
1220 }
1221}
1222
1223static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1224{
1225 struct otg_transceiver *otg = &motg->otg;
1226
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301227 if (!aca_enabled())
1228 return;
1229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001230 switch (motg->pdata->phy_type) {
1231 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 /* Enable ACA Detection interrupt (on any RID change) */
1233 ulpi_write(otg, 0x01, 0x94);
1234 break;
1235 default:
1236 break;
1237 }
1238}
1239
1240static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1241{
1242 struct otg_transceiver *otg = &motg->otg;
1243
1244 if (!aca_enabled())
1245 return;
1246
1247 switch (motg->pdata->phy_type) {
1248 case SNPS_28NM_INTEGRATED_PHY:
1249 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001250 break;
1251 default:
1252 break;
1253 }
1254}
1255
1256static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1257{
1258 struct otg_transceiver *otg = &motg->otg;
1259 bool ret = false;
1260
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301261 if (!aca_enabled())
1262 return ret;
1263
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001264 switch (motg->pdata->phy_type) {
1265 case SNPS_28NM_INTEGRATED_PHY:
1266 if (ulpi_read(otg, 0x91) & 1) {
1267 dev_dbg(otg->dev, "RID change\n");
1268 ulpi_write(otg, 0x01, 0x92);
1269 ret = msm_chg_aca_detect(motg);
1270 }
1271 default:
1272 break;
1273 }
1274 return ret;
1275}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301276
1277static void msm_otg_id_timer_func(unsigned long data)
1278{
1279 struct msm_otg *motg = (struct msm_otg *) data;
1280
1281 if (!aca_enabled())
1282 return;
1283
1284 if (atomic_read(&motg->in_lpm)) {
1285 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1286 return;
1287 }
1288
1289 if (msm_chg_check_aca_intr(motg)) {
1290 dev_dbg(motg->otg.dev, "timer: aca work\n");
1291 schedule_work(&motg->sm_work);
1292 }
1293
1294 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1295 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1296}
1297
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301298static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1299{
1300 struct otg_transceiver *otg = &motg->otg;
1301 u32 chg_det;
1302 bool ret = false;
1303
1304 switch (motg->pdata->phy_type) {
1305 case CI_45NM_INTEGRATED_PHY:
1306 chg_det = ulpi_read(otg, 0x34);
1307 ret = chg_det & (1 << 4);
1308 break;
1309 case SNPS_28NM_INTEGRATED_PHY:
1310 chg_det = ulpi_read(otg, 0x87);
1311 ret = chg_det & 1;
1312 break;
1313 default:
1314 break;
1315 }
1316 return ret;
1317}
1318
1319static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1320{
1321 struct otg_transceiver *otg = &motg->otg;
1322 u32 chg_det;
1323
1324 switch (motg->pdata->phy_type) {
1325 case CI_45NM_INTEGRATED_PHY:
1326 chg_det = ulpi_read(otg, 0x34);
1327 /* Turn off charger block */
1328 chg_det |= ~(1 << 1);
1329 ulpi_write(otg, chg_det, 0x34);
1330 udelay(20);
1331 /* control chg block via ULPI */
1332 chg_det &= ~(1 << 3);
1333 ulpi_write(otg, chg_det, 0x34);
1334 /* put it in host mode for enabling D- source */
1335 chg_det &= ~(1 << 2);
1336 ulpi_write(otg, chg_det, 0x34);
1337 /* Turn on chg detect block */
1338 chg_det &= ~(1 << 1);
1339 ulpi_write(otg, chg_det, 0x34);
1340 udelay(20);
1341 /* enable chg detection */
1342 chg_det &= ~(1 << 0);
1343 ulpi_write(otg, chg_det, 0x34);
1344 break;
1345 case SNPS_28NM_INTEGRATED_PHY:
1346 /*
1347 * Configure DM as current source, DP as current sink
1348 * and enable battery charging comparators.
1349 */
1350 ulpi_write(otg, 0x8, 0x85);
1351 ulpi_write(otg, 0x2, 0x85);
1352 ulpi_write(otg, 0x1, 0x85);
1353 break;
1354 default:
1355 break;
1356 }
1357}
1358
1359static bool msm_chg_check_primary_det(struct msm_otg *motg)
1360{
1361 struct otg_transceiver *otg = &motg->otg;
1362 u32 chg_det;
1363 bool ret = false;
1364
1365 switch (motg->pdata->phy_type) {
1366 case CI_45NM_INTEGRATED_PHY:
1367 chg_det = ulpi_read(otg, 0x34);
1368 ret = chg_det & (1 << 4);
1369 break;
1370 case SNPS_28NM_INTEGRATED_PHY:
1371 chg_det = ulpi_read(otg, 0x87);
1372 ret = chg_det & 1;
1373 break;
1374 default:
1375 break;
1376 }
1377 return ret;
1378}
1379
1380static void msm_chg_enable_primary_det(struct msm_otg *motg)
1381{
1382 struct otg_transceiver *otg = &motg->otg;
1383 u32 chg_det;
1384
1385 switch (motg->pdata->phy_type) {
1386 case CI_45NM_INTEGRATED_PHY:
1387 chg_det = ulpi_read(otg, 0x34);
1388 /* enable chg detection */
1389 chg_det &= ~(1 << 0);
1390 ulpi_write(otg, chg_det, 0x34);
1391 break;
1392 case SNPS_28NM_INTEGRATED_PHY:
1393 /*
1394 * Configure DP as current source, DM as current sink
1395 * and enable battery charging comparators.
1396 */
1397 ulpi_write(otg, 0x2, 0x85);
1398 ulpi_write(otg, 0x1, 0x85);
1399 break;
1400 default:
1401 break;
1402 }
1403}
1404
1405static bool msm_chg_check_dcd(struct msm_otg *motg)
1406{
1407 struct otg_transceiver *otg = &motg->otg;
1408 u32 line_state;
1409 bool ret = false;
1410
1411 switch (motg->pdata->phy_type) {
1412 case CI_45NM_INTEGRATED_PHY:
1413 line_state = ulpi_read(otg, 0x15);
1414 ret = !(line_state & 1);
1415 break;
1416 case SNPS_28NM_INTEGRATED_PHY:
1417 line_state = ulpi_read(otg, 0x87);
1418 ret = line_state & 2;
1419 break;
1420 default:
1421 break;
1422 }
1423 return ret;
1424}
1425
1426static void msm_chg_disable_dcd(struct msm_otg *motg)
1427{
1428 struct otg_transceiver *otg = &motg->otg;
1429 u32 chg_det;
1430
1431 switch (motg->pdata->phy_type) {
1432 case CI_45NM_INTEGRATED_PHY:
1433 chg_det = ulpi_read(otg, 0x34);
1434 chg_det &= ~(1 << 5);
1435 ulpi_write(otg, chg_det, 0x34);
1436 break;
1437 case SNPS_28NM_INTEGRATED_PHY:
1438 ulpi_write(otg, 0x10, 0x86);
1439 break;
1440 default:
1441 break;
1442 }
1443}
1444
1445static void msm_chg_enable_dcd(struct msm_otg *motg)
1446{
1447 struct otg_transceiver *otg = &motg->otg;
1448 u32 chg_det;
1449
1450 switch (motg->pdata->phy_type) {
1451 case CI_45NM_INTEGRATED_PHY:
1452 chg_det = ulpi_read(otg, 0x34);
1453 /* Turn on D+ current source */
1454 chg_det |= (1 << 5);
1455 ulpi_write(otg, chg_det, 0x34);
1456 break;
1457 case SNPS_28NM_INTEGRATED_PHY:
1458 /* Data contact detection enable */
1459 ulpi_write(otg, 0x10, 0x85);
1460 break;
1461 default:
1462 break;
1463 }
1464}
1465
1466static void msm_chg_block_on(struct msm_otg *motg)
1467{
1468 struct otg_transceiver *otg = &motg->otg;
1469 u32 func_ctrl, chg_det;
1470
1471 /* put the controller in non-driving mode */
1472 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1473 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1474 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1475 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1476
1477 switch (motg->pdata->phy_type) {
1478 case CI_45NM_INTEGRATED_PHY:
1479 chg_det = ulpi_read(otg, 0x34);
1480 /* control chg block via ULPI */
1481 chg_det &= ~(1 << 3);
1482 ulpi_write(otg, chg_det, 0x34);
1483 /* Turn on chg detect block */
1484 chg_det &= ~(1 << 1);
1485 ulpi_write(otg, chg_det, 0x34);
1486 udelay(20);
1487 break;
1488 case SNPS_28NM_INTEGRATED_PHY:
1489 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301490 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301491 /* Clear alt interrupt latch and enable bits */
1492 ulpi_write(otg, 0x1F, 0x92);
1493 ulpi_write(otg, 0x1F, 0x95);
1494 udelay(100);
1495 break;
1496 default:
1497 break;
1498 }
1499}
1500
1501static void msm_chg_block_off(struct msm_otg *motg)
1502{
1503 struct otg_transceiver *otg = &motg->otg;
1504 u32 func_ctrl, chg_det;
1505
1506 switch (motg->pdata->phy_type) {
1507 case CI_45NM_INTEGRATED_PHY:
1508 chg_det = ulpi_read(otg, 0x34);
1509 /* Turn off charger block */
1510 chg_det |= ~(1 << 1);
1511 ulpi_write(otg, chg_det, 0x34);
1512 break;
1513 case SNPS_28NM_INTEGRATED_PHY:
1514 /* Clear charger detecting control bits */
1515 ulpi_write(otg, 0x3F, 0x86);
1516 /* Clear alt interrupt latch and enable bits */
1517 ulpi_write(otg, 0x1F, 0x92);
1518 ulpi_write(otg, 0x1F, 0x95);
1519 break;
1520 default:
1521 break;
1522 }
1523
1524 /* put the controller in normal mode */
1525 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1526 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1527 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1528 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1529}
1530
Anji jonnalad270e2d2011-08-09 11:28:32 +05301531static const char *chg_to_string(enum usb_chg_type chg_type)
1532{
1533 switch (chg_type) {
1534 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1535 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1536 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1537 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1538 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1539 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1540 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1541 default: return "INVALID_CHARGER";
1542 }
1543}
1544
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301545#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1546#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1547#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1548#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1549static void msm_chg_detect_work(struct work_struct *w)
1550{
1551 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1552 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301554 unsigned long delay;
1555
1556 dev_dbg(otg->dev, "chg detection work\n");
1557 switch (motg->chg_state) {
1558 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301559 msm_chg_block_on(motg);
1560 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001561 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301562 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1563 motg->dcd_retries = 0;
1564 delay = MSM_CHG_DCD_POLL_TIME;
1565 break;
1566 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 is_aca = msm_chg_aca_detect(motg);
1568 if (is_aca) {
1569 /*
1570 * ID_A can be ACA dock too. continue
1571 * primary detection after DCD.
1572 */
1573 if (test_bit(ID_A, &motg->inputs)) {
1574 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1575 } else {
1576 delay = 0;
1577 break;
1578 }
1579 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301580 is_dcd = msm_chg_check_dcd(motg);
1581 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1582 if (is_dcd || tmout) {
1583 msm_chg_disable_dcd(motg);
1584 msm_chg_enable_primary_det(motg);
1585 delay = MSM_CHG_PRIMARY_DET_TIME;
1586 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1587 } else {
1588 delay = MSM_CHG_DCD_POLL_TIME;
1589 }
1590 break;
1591 case USB_CHG_STATE_DCD_DONE:
1592 vout = msm_chg_check_primary_det(motg);
1593 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301594 if (test_bit(ID_A, &motg->inputs)) {
1595 motg->chg_type = USB_ACA_DOCK_CHARGER;
1596 motg->chg_state = USB_CHG_STATE_DETECTED;
1597 delay = 0;
1598 break;
1599 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301600 msm_chg_enable_secondary_det(motg);
1601 delay = MSM_CHG_SECONDARY_DET_TIME;
1602 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1603 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301604 if (test_bit(ID_A, &motg->inputs)) {
1605 motg->chg_type = USB_ACA_A_CHARGER;
1606 motg->chg_state = USB_CHG_STATE_DETECTED;
1607 delay = 0;
1608 break;
1609 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301610 motg->chg_type = USB_SDP_CHARGER;
1611 motg->chg_state = USB_CHG_STATE_DETECTED;
1612 delay = 0;
1613 }
1614 break;
1615 case USB_CHG_STATE_PRIMARY_DONE:
1616 vout = msm_chg_check_secondary_det(motg);
1617 if (vout)
1618 motg->chg_type = USB_DCP_CHARGER;
1619 else
1620 motg->chg_type = USB_CDP_CHARGER;
1621 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1622 /* fall through */
1623 case USB_CHG_STATE_SECONDARY_DONE:
1624 motg->chg_state = USB_CHG_STATE_DETECTED;
1625 case USB_CHG_STATE_DETECTED:
1626 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001627 msm_chg_enable_aca_det(motg);
1628 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301629 dev_dbg(otg->dev, "chg_type = %s\n",
1630 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301631 schedule_work(&motg->sm_work);
1632 return;
1633 default:
1634 return;
1635 }
1636
1637 schedule_delayed_work(&motg->chg_work, delay);
1638}
1639
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301640/*
1641 * We support OTG, Peripheral only and Host only configurations. In case
1642 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1643 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1644 * enabled when switch is controlled by user and default mode is supplied
1645 * by board file, which can be changed by userspace later.
1646 */
1647static void msm_otg_init_sm(struct msm_otg *motg)
1648{
1649 struct msm_otg_platform_data *pdata = motg->pdata;
1650 u32 otgsc = readl(USB_OTGSC);
1651
1652 switch (pdata->mode) {
1653 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001654 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301655 if (pdata->default_mode == USB_HOST) {
1656 clear_bit(ID, &motg->inputs);
1657 } else if (pdata->default_mode == USB_PERIPHERAL) {
1658 set_bit(ID, &motg->inputs);
1659 set_bit(B_SESS_VLD, &motg->inputs);
1660 } else {
1661 set_bit(ID, &motg->inputs);
1662 clear_bit(B_SESS_VLD, &motg->inputs);
1663 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301664 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1665 if (otgsc & OTGSC_ID)
1666 set_bit(ID, &motg->inputs);
1667 else
1668 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 if (otgsc & OTGSC_BSV)
1670 set_bit(B_SESS_VLD, &motg->inputs);
1671 else
1672 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301673 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1674 if (irq_read_line(motg->pdata->pmic_id_irq))
1675 set_bit(ID, &motg->inputs);
1676 else
1677 clear_bit(ID, &motg->inputs);
1678
1679 /*
1680 * VBUS initial state is reported after PMIC
1681 * driver initialization. Wait for it.
1682 */
1683 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301684 }
1685 break;
1686 case USB_HOST:
1687 clear_bit(ID, &motg->inputs);
1688 break;
1689 case USB_PERIPHERAL:
1690 set_bit(ID, &motg->inputs);
1691 if (otgsc & OTGSC_BSV)
1692 set_bit(B_SESS_VLD, &motg->inputs);
1693 else
1694 clear_bit(B_SESS_VLD, &motg->inputs);
1695 break;
1696 default:
1697 break;
1698 }
1699}
1700
1701static void msm_otg_sm_work(struct work_struct *w)
1702{
1703 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1704 struct otg_transceiver *otg = &motg->otg;
1705
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301706 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301707 switch (otg->state) {
1708 case OTG_STATE_UNDEFINED:
1709 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1710 msm_otg_reset(otg);
1711 msm_otg_init_sm(motg);
1712 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301713 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1714 test_bit(ID, &motg->inputs)) {
1715 pm_runtime_put_noidle(otg->dev);
1716 pm_runtime_suspend(otg->dev);
1717 break;
1718 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301719 /* FALL THROUGH */
1720 case OTG_STATE_B_IDLE:
1721 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001722 if ((!test_bit(ID, &motg->inputs) ||
1723 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001724 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1725 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301726 IDEV_ACA_CHG_MAX);
1727 else if (test_bit(ID_A, &motg->inputs))
1728 msm_otg_notify_charger(motg,
1729 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301730 else
1731 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301732 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301733 /*
1734 * Link can not generate PHY_ALT interrupt
1735 * in host mode when no device is attached
1736 * to the port. It is also observed PHY_ALT
1737 * interrupt missing upon Micro-A cable disconnect.
1738 * Hence disable PHY_ALT interrupt and perform
1739 * polling to detect RID change.
1740 */
1741 msm_chg_enable_aca_det(motg);
1742 msm_chg_disable_aca_intr(motg);
1743 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301744 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301745 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1746 switch (motg->chg_state) {
1747 case USB_CHG_STATE_UNDEFINED:
1748 msm_chg_detect_work(&motg->chg_work.work);
1749 break;
1750 case USB_CHG_STATE_DETECTED:
1751 switch (motg->chg_type) {
1752 case USB_DCP_CHARGER:
1753 msm_otg_notify_charger(motg,
1754 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301755 pm_runtime_put_noidle(otg->dev);
1756 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301757 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301758 case USB_ACA_B_CHARGER:
1759 msm_otg_notify_charger(motg,
1760 IDEV_ACA_CHG_MAX);
1761 /*
1762 * (ID_B --> ID_C) PHY_ALT interrupt can
1763 * not be detected in LPM.
1764 */
1765 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301766 case USB_CDP_CHARGER:
1767 msm_otg_notify_charger(motg,
1768 IDEV_CHG_MAX);
1769 msm_otg_start_peripheral(otg, 1);
1770 otg->state = OTG_STATE_B_PERIPHERAL;
1771 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301772 case USB_ACA_C_CHARGER:
1773 msm_otg_notify_charger(motg,
1774 IDEV_ACA_CHG_MAX);
1775 msm_otg_start_peripheral(otg, 1);
1776 otg->state = OTG_STATE_B_PERIPHERAL;
1777 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301778 case USB_SDP_CHARGER:
1779 msm_otg_notify_charger(motg, IUNIT);
1780 msm_otg_start_peripheral(otg, 1);
1781 otg->state = OTG_STATE_B_PERIPHERAL;
1782 break;
1783 default:
1784 break;
1785 }
1786 break;
1787 default:
1788 break;
1789 }
1790 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301791 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301792 msm_otg_notify_charger(motg, 0);
1793 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1794 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301795 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301796 pm_runtime_put_noidle(otg->dev);
1797 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301798 }
1799 break;
1800 case OTG_STATE_B_PERIPHERAL:
1801 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1802 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001803 !test_bit(ID, &motg->inputs) ||
1804 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301805 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001807 schedule_work(w);
1808 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301809 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 }
1811 break;
1812 case OTG_STATE_A_HOST:
1813 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1814 if (test_bit(ID, &motg->inputs) &&
1815 !test_bit(ID_A, &motg->inputs)) {
1816 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301817 msm_hsusb_vbus_power(motg, 0);
1818 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301819 /*
1820 * Exit point of host mode.
1821 *
1822 * 1. Micro-A cable disconnect: Just schedule
1823 * the work. PHY is reset in B_IDLE and LPM
1824 * is allowed.
1825 * 2. ID_GND --> ID_B: No need to reset the PHY.
1826 * HCD core clears all PORTSC bits and initializes
1827 * the controller to host mode in remove_hcd.
1828 * Restore PORTSC transceiver select bits (ULPI)
1829 * and reset the controller to change MODE bits.
1830 * PHY_ALT interrupt can not occur in host mode.
1831 */
1832 del_timer_sync(&motg->id_timer);
1833 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1834 msm_otg_link_reset(motg);
1835 msm_chg_enable_aca_intr(motg);
1836 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301837 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301838 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001839 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301840 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001841 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301842 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001843 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001844 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301845 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301846 }
1847 break;
1848 default:
1849 break;
1850 }
1851}
1852
1853static irqreturn_t msm_otg_irq(int irq, void *data)
1854{
1855 struct msm_otg *motg = data;
1856 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301858
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301859 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301860 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301861 disable_irq_nosync(irq);
1862 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301863 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301864 return IRQ_HANDLED;
1865 }
1866
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001867 usbsts = readl(USB_USBSTS);
1868 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301869 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001870 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301871 if (msm_chg_check_aca_intr(motg)) {
1872 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001873 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301874 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001875 return IRQ_HANDLED;
1876 }
1877
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301878 otgsc = readl(USB_OTGSC);
1879 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1880 return IRQ_NONE;
1881
1882 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301883 if (otgsc & OTGSC_ID) {
1884 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301885 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301886 } else {
1887 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301888 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301889 msm_chg_enable_aca_det(motg);
1890 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301892 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301893 if (otgsc & OTGSC_BSV) {
1894 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301895 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301896 } else {
1897 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301898 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301899 msm_chg_check_aca_intr(motg);
1900 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001901 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301902 }
1903
1904 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001905 return IRQ_HANDLED;
1906}
1907
1908static void msm_otg_set_vbus_state(int online)
1909{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301910 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911 struct msm_otg *motg = the_msm_otg;
1912
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301913 if (online) {
1914 pr_debug("PMIC: BSV set\n");
1915 set_bit(B_SESS_VLD, &motg->inputs);
1916 } else {
1917 pr_debug("PMIC: BSV clear\n");
1918 clear_bit(B_SESS_VLD, &motg->inputs);
1919 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001920
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301921 if (!init) {
1922 init = true;
1923 complete(&pmic_vbus_init);
1924 pr_debug("PMIC: BSV init complete\n");
1925 return;
1926 }
1927
1928 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929}
1930
1931static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1932{
1933 struct msm_otg *motg = data;
1934
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301935 if (aca_id_turned_on)
1936 return IRQ_HANDLED;
1937
1938 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1939 pr_debug("PMIC: ID set\n");
1940 set_bit(ID, &motg->inputs);
1941 } else {
1942 pr_debug("PMIC: ID clear\n");
1943 clear_bit(ID, &motg->inputs);
1944 }
1945
1946 if (motg->otg.state != OTG_STATE_UNDEFINED)
1947 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001948
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301949 return IRQ_HANDLED;
1950}
1951
1952static int msm_otg_mode_show(struct seq_file *s, void *unused)
1953{
1954 struct msm_otg *motg = s->private;
1955 struct otg_transceiver *otg = &motg->otg;
1956
1957 switch (otg->state) {
1958 case OTG_STATE_A_HOST:
1959 seq_printf(s, "host\n");
1960 break;
1961 case OTG_STATE_B_PERIPHERAL:
1962 seq_printf(s, "peripheral\n");
1963 break;
1964 default:
1965 seq_printf(s, "none\n");
1966 break;
1967 }
1968
1969 return 0;
1970}
1971
1972static int msm_otg_mode_open(struct inode *inode, struct file *file)
1973{
1974 return single_open(file, msm_otg_mode_show, inode->i_private);
1975}
1976
1977static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
1978 size_t count, loff_t *ppos)
1979{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05301980 struct seq_file *s = file->private_data;
1981 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301982 char buf[16];
1983 struct otg_transceiver *otg = &motg->otg;
1984 int status = count;
1985 enum usb_mode_type req_mode;
1986
1987 memset(buf, 0x00, sizeof(buf));
1988
1989 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
1990 status = -EFAULT;
1991 goto out;
1992 }
1993
1994 if (!strncmp(buf, "host", 4)) {
1995 req_mode = USB_HOST;
1996 } else if (!strncmp(buf, "peripheral", 10)) {
1997 req_mode = USB_PERIPHERAL;
1998 } else if (!strncmp(buf, "none", 4)) {
1999 req_mode = USB_NONE;
2000 } else {
2001 status = -EINVAL;
2002 goto out;
2003 }
2004
2005 switch (req_mode) {
2006 case USB_NONE:
2007 switch (otg->state) {
2008 case OTG_STATE_A_HOST:
2009 case OTG_STATE_B_PERIPHERAL:
2010 set_bit(ID, &motg->inputs);
2011 clear_bit(B_SESS_VLD, &motg->inputs);
2012 break;
2013 default:
2014 goto out;
2015 }
2016 break;
2017 case USB_PERIPHERAL:
2018 switch (otg->state) {
2019 case OTG_STATE_B_IDLE:
2020 case OTG_STATE_A_HOST:
2021 set_bit(ID, &motg->inputs);
2022 set_bit(B_SESS_VLD, &motg->inputs);
2023 break;
2024 default:
2025 goto out;
2026 }
2027 break;
2028 case USB_HOST:
2029 switch (otg->state) {
2030 case OTG_STATE_B_IDLE:
2031 case OTG_STATE_B_PERIPHERAL:
2032 clear_bit(ID, &motg->inputs);
2033 break;
2034 default:
2035 goto out;
2036 }
2037 break;
2038 default:
2039 goto out;
2040 }
2041
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302042 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302043 schedule_work(&motg->sm_work);
2044out:
2045 return status;
2046}
2047
2048const struct file_operations msm_otg_mode_fops = {
2049 .open = msm_otg_mode_open,
2050 .read = seq_read,
2051 .write = msm_otg_mode_write,
2052 .llseek = seq_lseek,
2053 .release = single_release,
2054};
2055
Anji jonnalad270e2d2011-08-09 11:28:32 +05302056static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2057{
2058 struct msm_otg *motg = s->private;
2059
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302060 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302061 return 0;
2062}
2063
2064static int msm_otg_chg_open(struct inode *inode, struct file *file)
2065{
2066 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2067}
2068
2069const struct file_operations msm_otg_chg_fops = {
2070 .open = msm_otg_chg_open,
2071 .read = seq_read,
2072 .llseek = seq_lseek,
2073 .release = single_release,
2074};
2075
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302076static int msm_otg_aca_show(struct seq_file *s, void *unused)
2077{
2078 if (debug_aca_enabled)
2079 seq_printf(s, "enabled\n");
2080 else
2081 seq_printf(s, "disabled\n");
2082
2083 return 0;
2084}
2085
2086static int msm_otg_aca_open(struct inode *inode, struct file *file)
2087{
2088 return single_open(file, msm_otg_aca_show, inode->i_private);
2089}
2090
2091static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2092 size_t count, loff_t *ppos)
2093{
2094 char buf[8];
2095
2096 memset(buf, 0x00, sizeof(buf));
2097
2098 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2099 return -EFAULT;
2100
2101 if (!strncmp(buf, "enable", 6))
2102 debug_aca_enabled = true;
2103 else
2104 debug_aca_enabled = false;
2105
2106 return count;
2107}
2108
2109const struct file_operations msm_otg_aca_fops = {
2110 .open = msm_otg_aca_open,
2111 .read = seq_read,
2112 .write = msm_otg_aca_write,
2113 .llseek = seq_lseek,
2114 .release = single_release,
2115};
2116
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302117static struct dentry *msm_otg_dbg_root;
2118static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302119static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302120static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302121
2122static int msm_otg_debugfs_init(struct msm_otg *motg)
2123{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302124
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302125 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2126
2127 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2128 return -ENODEV;
2129
Anji jonnalad270e2d2011-08-09 11:28:32 +05302130 if (motg->pdata->mode == USB_OTG &&
2131 motg->pdata->otg_control == OTG_USER_CONTROL) {
2132
2133 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2134 S_IWUSR, msm_otg_dbg_root, motg,
2135 &msm_otg_mode_fops);
2136
2137 if (!msm_otg_dbg_mode) {
2138 debugfs_remove(msm_otg_dbg_root);
2139 msm_otg_dbg_root = NULL;
2140 return -ENODEV;
2141 }
2142 }
2143
2144 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2145 msm_otg_dbg_root, motg,
2146 &msm_otg_chg_fops);
2147
2148 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302149 debugfs_remove_recursive(msm_otg_dbg_root);
2150 return -ENODEV;
2151 }
2152
2153 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2154 msm_otg_dbg_root, motg,
2155 &msm_otg_aca_fops);
2156
2157 if (!msm_otg_dbg_aca) {
2158 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302159 return -ENODEV;
2160 }
2161
2162 return 0;
2163}
2164
2165static void msm_otg_debugfs_cleanup(void)
2166{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302167 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302168}
2169
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302170static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2171static struct platform_device *msm_otg_add_pdev(
2172 struct platform_device *ofdev, const char *name)
2173{
2174 struct platform_device *pdev;
2175 const struct resource *res = ofdev->resource;
2176 unsigned int num = ofdev->num_resources;
2177 int retval;
2178
2179 pdev = platform_device_alloc(name, -1);
2180 if (!pdev) {
2181 retval = -ENOMEM;
2182 goto error;
2183 }
2184
2185 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2186 pdev->dev.dma_mask = &msm_otg_dma_mask;
2187
2188 if (num) {
2189 retval = platform_device_add_resources(pdev, res, num);
2190 if (retval)
2191 goto error;
2192 }
2193
2194 retval = platform_device_add(pdev);
2195 if (retval)
2196 goto error;
2197
2198 return pdev;
2199
2200error:
2201 platform_device_put(pdev);
2202 return ERR_PTR(retval);
2203}
2204
2205static int msm_otg_setup_devices(struct platform_device *ofdev,
2206 enum usb_mode_type mode, bool init)
2207{
2208 const char *gadget_name = "msm_hsusb";
2209 const char *host_name = "msm_hsusb_host";
2210 static struct platform_device *gadget_pdev;
2211 static struct platform_device *host_pdev;
2212 int retval = 0;
2213
2214 if (!init) {
2215 if (gadget_pdev)
2216 platform_device_unregister(gadget_pdev);
2217 if (host_pdev)
2218 platform_device_unregister(host_pdev);
2219 return 0;
2220 }
2221
2222 switch (mode) {
2223 case USB_OTG:
2224 /* fall through */
2225 case USB_PERIPHERAL:
2226 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2227 if (IS_ERR(gadget_pdev)) {
2228 retval = PTR_ERR(gadget_pdev);
2229 break;
2230 }
2231 if (mode == USB_PERIPHERAL)
2232 break;
2233 /* fall through */
2234 case USB_HOST:
2235 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2236 if (IS_ERR(host_pdev)) {
2237 retval = PTR_ERR(host_pdev);
2238 if (mode == USB_OTG)
2239 platform_device_unregister(gadget_pdev);
2240 }
2241 break;
2242 default:
2243 break;
2244 }
2245
2246 return retval;
2247}
2248
2249struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2250{
2251 struct device_node *node = pdev->dev.of_node;
2252 struct msm_otg_platform_data *pdata;
2253 int len = 0;
2254
2255 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2256 if (!pdata) {
2257 pr_err("unable to allocate platform data\n");
2258 return NULL;
2259 }
2260 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2261 if (len) {
2262 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2263 if (!pdata->phy_init_seq)
2264 return NULL;
2265 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2266 pdata->phy_init_seq,
2267 len/sizeof(*pdata->phy_init_seq));
2268 }
2269 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2270 &pdata->power_budget);
2271 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2272 &pdata->mode);
2273 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2274 &pdata->otg_control);
2275 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2276 &pdata->default_mode);
2277 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2278 &pdata->phy_type);
2279 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2280 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302281 return pdata;
2282}
2283
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302284static int __init msm_otg_probe(struct platform_device *pdev)
2285{
2286 int ret = 0;
2287 struct resource *res;
2288 struct msm_otg *motg;
2289 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302290 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302291
2292 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302293
2294 if (pdev->dev.of_node) {
2295 dev_dbg(&pdev->dev, "device tree enabled\n");
2296 pdata = msm_otg_dt_to_pdata(pdev);
2297 if (!pdata)
2298 return -ENOMEM;
2299 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2300 if (ret) {
2301 dev_err(&pdev->dev, "devices setup failed\n");
2302 return ret;
2303 }
2304 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302305 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2306 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302307 } else {
2308 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302309 }
2310
2311 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2312 if (!motg) {
2313 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2314 return -ENOMEM;
2315 }
2316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002317 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302318 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302319 otg = &motg->otg;
2320 otg->dev = &pdev->dev;
2321
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302322 /*
2323 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2324 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2325 * PMIC can detect ACA ID_GND and generate an interrupt.
2326 */
2327 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2328 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2329 ret = -EINVAL;
2330 goto free_motg;
2331 }
2332
Ofir Cohen4da266f2012-01-03 10:19:29 +02002333 /* initialize reset counter */
2334 motg->reset_counter = 0;
2335
Amit Blay02eff132011-09-21 16:46:24 +03002336 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002337 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002338 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002339 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302340
Manu Gautam5143b252012-01-05 19:25:23 -08002341 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302342 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002343 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302344 ret = PTR_ERR(motg->clk);
2345 goto put_phy_reset_clk;
2346 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302347 clk_set_rate(motg->clk, 60000000);
2348
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302349 /* pm qos request to prevent apps idle power collapse */
2350 if (motg->pdata->swfi_latency)
2351 pm_qos_add_request(&motg->pm_qos_req_dma,
2352 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002353
Anji jonnala0f73cac2011-05-04 10:19:46 +05302354 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002355 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302356 * CORE CLK must be running at >55Mhz for correct HSUSB
2357 * operation and USB core cannot tolerate frequency changes on
2358 * CORE CLK. For such USB cores, vote for maximum clk frequency
2359 * on pclk source
2360 */
Manu Gautam5143b252012-01-05 19:25:23 -08002361 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2362 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302363 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002364 dev_err(&pdev->dev, "failed to get core_clk\n");
2365 ret = PTR_ERR(motg->clk);
2366 goto put_clk;
2367 }
2368 clk_set_rate(motg->core_clk, INT_MAX);
2369
2370 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2371 if (IS_ERR(motg->pclk)) {
2372 dev_err(&pdev->dev, "failed to get iface_clk\n");
2373 ret = PTR_ERR(motg->pclk);
2374 goto put_core_clk;
2375 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302376
2377 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2378 if (!res) {
2379 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2380 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002381 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302382 }
2383
2384 motg->regs = ioremap(res->start, resource_size(res));
2385 if (!motg->regs) {
2386 dev_err(&pdev->dev, "ioremap failed\n");
2387 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002388 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302389 }
2390 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2391
2392 motg->irq = platform_get_irq(pdev, 0);
2393 if (!motg->irq) {
2394 dev_err(&pdev->dev, "platform_get_irq failed\n");
2395 ret = -ENODEV;
2396 goto free_regs;
2397 }
2398
Anji jonnala7da3f262011-12-02 17:22:14 -08002399 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2400 if (IS_ERR(motg->xo_handle)) {
2401 dev_err(&pdev->dev, "%s not able to get the handle "
2402 "to vote for TCXO D0 buffer\n", __func__);
2403 ret = PTR_ERR(motg->xo_handle);
2404 goto free_regs;
2405 }
2406
2407 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2408 if (ret) {
2409 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2410 "D0 buffer%d\n", __func__, ret);
2411 goto free_xo_handle;
2412 }
2413
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302414 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302415
2416 ret = msm_hsusb_init_vddcx(motg, 1);
2417 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002418 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002419 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302420 }
2421
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002422 ret = msm_hsusb_config_vddcx(1);
2423 if (ret) {
2424 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2425 goto free_init_vddcx;
2426 }
2427
Anji jonnala11aa5c42011-05-04 10:19:48 +05302428 ret = msm_hsusb_ldo_init(motg, 1);
2429 if (ret) {
2430 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002431 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302432 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002433
2434 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302435 if (ret) {
2436 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002437 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302438 }
Manu Gautam5143b252012-01-05 19:25:23 -08002439 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302440
2441 writel(0, USB_USBINTR);
2442 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002443 /* Ensure that above STOREs are completed before enabling interrupts */
2444 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302445
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002446 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302447 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302448 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302449 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2450 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302451 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2452 "msm_otg", motg);
2453 if (ret) {
2454 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002455 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302456 }
2457
2458 otg->init = msm_otg_reset;
2459 otg->set_host = msm_otg_set_host;
2460 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302461 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302462 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302463
2464 otg->io_ops = &msm_otg_io_ops;
2465
2466 ret = otg_set_transceiver(&motg->otg);
2467 if (ret) {
2468 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2469 goto free_irq;
2470 }
2471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472 if (motg->pdata->otg_control == OTG_PMIC_CONTROL) {
2473 if (motg->pdata->pmic_id_irq) {
2474 ret = request_irq(motg->pdata->pmic_id_irq,
2475 msm_pmic_id_irq,
2476 IRQF_TRIGGER_RISING |
2477 IRQF_TRIGGER_FALLING,
2478 "msm_otg", motg);
2479 if (ret) {
2480 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2481 goto remove_otg;
2482 }
2483 } else {
2484 ret = -ENODEV;
2485 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2486 goto remove_otg;
2487 }
2488 }
2489
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302490 msm_hsusb_mhl_switch_enable(motg, 1);
2491
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302492 platform_set_drvdata(pdev, motg);
2493 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302495
Anji jonnalad270e2d2011-08-09 11:28:32 +05302496 ret = msm_otg_debugfs_init(motg);
2497 if (ret)
2498 dev_dbg(&pdev->dev, "mode debugfs file is"
2499 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302500
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2502 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2503
Amit Blay58b31472011-11-18 09:39:39 +02002504 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2505 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 motg->pdata->pmic_id_irq)
Amit Blay58b31472011-11-18 09:39:39 +02002507 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508 ALLOW_PHY_RETENTION |
2509 ALLOW_PHY_COMP_DISABLE;
2510
Amit Blay58b31472011-11-18 09:39:39 +02002511 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2512 motg->caps = ALLOW_PHY_RETENTION;
2513 }
2514
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002515 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302516 pm_runtime_set_active(&pdev->dev);
2517 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302518
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302519 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520
2521remove_otg:
2522 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302523free_irq:
2524 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525destroy_wlock:
2526 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002527 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 msm_hsusb_ldo_enable(motg, 0);
2529free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302530 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302532 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002533devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002534 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002535 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2536free_xo_handle:
2537 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302538free_regs:
2539 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002540put_pclk:
2541 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302542put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002543 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302544put_clk:
2545 clk_put(motg->clk);
2546put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002547 if (!IS_ERR(motg->phy_reset_clk))
2548 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302549free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302550 if (motg->pdata->swfi_latency)
2551 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302552 kfree(motg);
2553 return ret;
2554}
2555
2556static int __devexit msm_otg_remove(struct platform_device *pdev)
2557{
2558 struct msm_otg *motg = platform_get_drvdata(pdev);
2559 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302560 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302561
2562 if (otg->host || otg->gadget)
2563 return -EBUSY;
2564
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302565 if (pdev->dev.of_node)
2566 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002567 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2568 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302569 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302570 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302572
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302573 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302574
2575 device_init_wakeup(&pdev->dev, 0);
2576 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302578
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302579 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002580 if (motg->pdata->pmic_id_irq)
2581 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302582 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302583 free_irq(motg->irq, motg);
2584
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302585 /*
2586 * Put PHY in low power mode.
2587 */
2588 ulpi_read(otg, 0x14);
2589 ulpi_write(otg, 0x08, 0x09);
2590
2591 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2592 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2593 if (readl(USB_PORTSC) & PORTSC_PHCD)
2594 break;
2595 udelay(1);
2596 cnt++;
2597 }
2598 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2599 dev_err(otg->dev, "Unable to suspend PHY\n");
2600
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302601 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002602 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002603 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002604 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302605 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002606 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302607
2608 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302609 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302610
Amit Blay02eff132011-09-21 16:46:24 +03002611 if (!IS_ERR(motg->phy_reset_clk))
2612 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302613 clk_put(motg->pclk);
2614 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002615 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302616
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302617 if (motg->pdata->swfi_latency)
2618 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302619
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302620 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302621 return 0;
2622}
2623
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302624#ifdef CONFIG_PM_RUNTIME
2625static int msm_otg_runtime_idle(struct device *dev)
2626{
2627 struct msm_otg *motg = dev_get_drvdata(dev);
2628 struct otg_transceiver *otg = &motg->otg;
2629
2630 dev_dbg(dev, "OTG runtime idle\n");
2631
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302632 if (otg->state == OTG_STATE_UNDEFINED)
2633 return -EAGAIN;
2634 else
2635 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302636}
2637
2638static int msm_otg_runtime_suspend(struct device *dev)
2639{
2640 struct msm_otg *motg = dev_get_drvdata(dev);
2641
2642 dev_dbg(dev, "OTG runtime suspend\n");
2643 return msm_otg_suspend(motg);
2644}
2645
2646static int msm_otg_runtime_resume(struct device *dev)
2647{
2648 struct msm_otg *motg = dev_get_drvdata(dev);
2649
2650 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302651 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302652 return msm_otg_resume(motg);
2653}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302654#endif
2655
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302656#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302657static int msm_otg_pm_suspend(struct device *dev)
2658{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302659 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302660
2661 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302662
2663#ifdef CONFIG_PM_RUNTIME
2664 ret = pm_runtime_suspend(dev);
2665 if (ret > 0)
2666 ret = 0;
2667#else
2668 ret = msm_otg_suspend(dev_get_drvdata(dev));
2669#endif
2670 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302671}
2672
2673static int msm_otg_pm_resume(struct device *dev)
2674{
2675 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302676
2677 dev_dbg(dev, "OTG PM resume\n");
2678
Manu Gautamf284c052011-09-08 16:52:48 +05302679#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302680 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302681 * Do not resume hardware as part of system resume,
2682 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302683 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002684 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302685#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302686
Manu Gautamf284c052011-09-08 16:52:48 +05302687 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302688}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302689#endif
2690
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302691#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302692static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302693 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2694 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2695 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302696};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302697#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302698
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302699static struct of_device_id msm_otg_dt_match[] = {
2700 { .compatible = "qcom,hsusb-otg",
2701 },
2702 {}
2703};
2704
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302705static struct platform_driver msm_otg_driver = {
2706 .remove = __devexit_p(msm_otg_remove),
2707 .driver = {
2708 .name = DRIVER_NAME,
2709 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302710#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302711 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302712#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302713 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302714 },
2715};
2716
2717static int __init msm_otg_init(void)
2718{
2719 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2720}
2721
2722static void __exit msm_otg_exit(void)
2723{
2724 platform_driver_unregister(&msm_otg_driver);
2725}
2726
2727module_init(msm_otg_init);
2728module_exit(msm_otg_exit);
2729
2730MODULE_LICENSE("GPL v2");
2731MODULE_DESCRIPTION("MSM USB transceiver driver");