blob: e596b3fef81611739e518e54ef0aa82bc6be7fda [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>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053042#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020043#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080046#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053047#include <mach/msm_bus.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048
49#define MSM_USB_BASE (motg->regs)
50#define DRIVER_NAME "msm_otg"
51
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053052#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053053#define ID_TIMER_INITIAL_FREQ (jiffies + msecs_to_jiffies(1000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053054#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053055#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
56#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
57#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
58#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
59
60#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
61#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
62#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
63#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
64
Vamsi Krishna132b2762011-11-11 16:09:20 -080065#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053066#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
67
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053068static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053070static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053071static bool debug_bus_voting_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053073/* Prevent idle power collapse(pc) while operating in peripheral mode */
74static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
75{
76 struct msm_otg_platform_data *pdata = dev->pdata;
77 u32 swfi_latency = 0;
78
79 if (!pdata || !pdata->swfi_latency)
80 return;
81
82 swfi_latency = pdata->swfi_latency + 1;
83
84 if (vote)
85 pm_qos_update_request(&dev->pm_qos_req_dma,
86 swfi_latency);
87 else
88 pm_qos_update_request(&dev->pm_qos_req_dma,
89 PM_QOS_DEFAULT_VALUE);
90}
91
Anji jonnala11aa5c42011-05-04 10:19:48 +053092static struct regulator *hsusb_3p3;
93static struct regulator *hsusb_1p8;
94static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053095static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053096
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053097static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053098static inline bool aca_enabled(void)
99{
100#ifdef CONFIG_USB_MSM_ACA
101 return true;
102#else
103 return debug_aca_enabled;
104#endif
105}
106
Anji jonnala11aa5c42011-05-04 10:19:48 +0530107static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
108{
109 int ret = 0;
110
111 if (init) {
112 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
113 if (IS_ERR(hsusb_vddcx)) {
114 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
115 return PTR_ERR(hsusb_vddcx);
116 }
117
118 ret = regulator_set_voltage(hsusb_vddcx,
119 USB_PHY_VDD_DIG_VOL_MIN,
120 USB_PHY_VDD_DIG_VOL_MAX);
121 if (ret) {
122 dev_err(motg->otg.dev, "unable to set the voltage "
123 "for hsusb vddcx\n");
124 regulator_put(hsusb_vddcx);
125 return ret;
126 }
127
128 ret = regulator_enable(hsusb_vddcx);
129 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130 regulator_set_voltage(hsusb_vddcx, 0,
131 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
134 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138
Anji jonnala11aa5c42011-05-04 10:19:48 +0530139 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530141 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 return ret;
143 }
144
145 ret = regulator_set_voltage(hsusb_vddcx, 0,
146 USB_PHY_VDD_DIG_VOL_MIN);
147 if (ret) {
148 dev_err(motg->otg.dev, "unable to set the voltage"
149 "for hsusb vddcx\n");
150 return ret;
151 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530152
153 regulator_put(hsusb_vddcx);
154 }
155
156 return ret;
157}
158
159static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
160{
161 int rc = 0;
162
163 if (init) {
164 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
165 if (IS_ERR(hsusb_3p3)) {
166 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
167 return PTR_ERR(hsusb_3p3);
168 }
169
170 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
171 USB_PHY_3P3_VOL_MAX);
172 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173 dev_err(motg->otg.dev, "unable to set voltage level for"
174 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530175 goto put_3p3;
176 }
177 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
178 if (IS_ERR(hsusb_1p8)) {
179 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
180 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700181 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530182 }
183 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
184 USB_PHY_1P8_VOL_MAX);
185 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 dev_err(motg->otg.dev, "unable to set voltage level for"
187 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530188 goto put_1p8;
189 }
190
191 return 0;
192 }
193
Anji jonnala11aa5c42011-05-04 10:19:48 +0530194put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530196 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197put_3p3_lpm:
198 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530199put_3p3:
200 regulator_put(hsusb_3p3);
201 return rc;
202}
203
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530204#ifdef CONFIG_PM_SLEEP
205#define USB_PHY_SUSP_DIG_VOL 500000
206static int msm_hsusb_config_vddcx(int high)
207{
208 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
209 int min_vol;
210 int ret;
211
212 if (high)
213 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
214 else
215 min_vol = USB_PHY_SUSP_DIG_VOL;
216
217 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
218 if (ret) {
219 pr_err("%s: unable to set the voltage for regulator "
220 "HSUSB_VDDCX\n", __func__);
221 return ret;
222 }
223
224 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
225
226 return ret;
227}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700228#else
229static int msm_hsusb_config_vddcx(int high)
230{
231 return 0;
232}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530233#endif
234
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530236{
237 int ret = 0;
238
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530239 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530240 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
241 return -ENODEV;
242 }
243
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530244 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530245 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
246 return -ENODEV;
247 }
248
249 if (on) {
250 ret = regulator_set_optimum_mode(hsusb_1p8,
251 USB_PHY_1P8_HPM_LOAD);
252 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530254 "HSUSB_1p8\n", __func__);
255 return ret;
256 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700257
258 ret = regulator_enable(hsusb_1p8);
259 if (ret) {
260 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
261 __func__);
262 regulator_set_optimum_mode(hsusb_1p8, 0);
263 return ret;
264 }
265
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 ret = regulator_set_optimum_mode(hsusb_3p3,
267 USB_PHY_3P3_HPM_LOAD);
268 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700269 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530270 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271 regulator_set_optimum_mode(hsusb_1p8, 0);
272 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530273 return ret;
274 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700275
276 ret = regulator_enable(hsusb_3p3);
277 if (ret) {
278 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
279 __func__);
280 regulator_set_optimum_mode(hsusb_3p3, 0);
281 regulator_set_optimum_mode(hsusb_1p8, 0);
282 regulator_disable(hsusb_1p8);
283 return ret;
284 }
285
Anji jonnala11aa5c42011-05-04 10:19:48 +0530286 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700287 ret = regulator_disable(hsusb_1p8);
288 if (ret) {
289 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
290 __func__);
291 return ret;
292 }
293
294 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530295 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530297 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700298
299 ret = regulator_disable(hsusb_3p3);
300 if (ret) {
301 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
302 __func__);
303 return ret;
304 }
305 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530306 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700307 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530308 "HSUSB_3p3\n", __func__);
309 }
310
311 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
312 return ret < 0 ? ret : 0;
313}
314
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530315static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
316{
317 static struct regulator *mhl_analog_switch;
318 struct msm_otg_platform_data *pdata = motg->pdata;
319
320 if (!pdata->mhl_enable)
321 return;
322
323 if (on) {
324 mhl_analog_switch = regulator_get(motg->otg.dev,
325 "mhl_ext_3p3v");
326 if (IS_ERR(mhl_analog_switch)) {
327 pr_err("Unable to get mhl_analog_switch\n");
328 return;
329 }
330
331 if (regulator_enable(mhl_analog_switch)) {
332 pr_err("unable to enable mhl_analog_switch\n");
333 goto put_analog_switch;
334 }
335 return;
336 }
337
338 regulator_disable(mhl_analog_switch);
339put_analog_switch:
340 regulator_put(mhl_analog_switch);
341}
342
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530343static int ulpi_read(struct otg_transceiver *otg, u32 reg)
344{
345 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
346 int cnt = 0;
347
348 /* initiate read operation */
349 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
350 USB_ULPI_VIEWPORT);
351
352 /* wait for completion */
353 while (cnt < ULPI_IO_TIMEOUT_USEC) {
354 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
355 break;
356 udelay(1);
357 cnt++;
358 }
359
360 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
361 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
362 readl(USB_ULPI_VIEWPORT));
363 return -ETIMEDOUT;
364 }
365 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
366}
367
368static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
369{
370 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
371 int cnt = 0;
372
373 /* initiate write operation */
374 writel(ULPI_RUN | ULPI_WRITE |
375 ULPI_ADDR(reg) | ULPI_DATA(val),
376 USB_ULPI_VIEWPORT);
377
378 /* wait for completion */
379 while (cnt < ULPI_IO_TIMEOUT_USEC) {
380 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
381 break;
382 udelay(1);
383 cnt++;
384 }
385
386 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
387 dev_err(otg->dev, "ulpi_write: timeout\n");
388 return -ETIMEDOUT;
389 }
390 return 0;
391}
392
393static struct otg_io_access_ops msm_otg_io_ops = {
394 .read = ulpi_read,
395 .write = ulpi_write,
396};
397
398static void ulpi_init(struct msm_otg *motg)
399{
400 struct msm_otg_platform_data *pdata = motg->pdata;
401 int *seq = pdata->phy_init_seq;
402
403 if (!seq)
404 return;
405
406 while (seq[0] >= 0) {
407 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
408 seq[0], seq[1]);
409 ulpi_write(&motg->otg, seq[0], seq[1]);
410 seq += 2;
411 }
412}
413
414static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
415{
416 int ret;
417
418 if (assert) {
419 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
420 if (ret)
421 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
422 } else {
423 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
424 if (ret)
425 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
426 }
427 return ret;
428}
429
430static int msm_otg_phy_clk_reset(struct msm_otg *motg)
431{
432 int ret;
433
Amit Blay02eff132011-09-21 16:46:24 +0300434 if (IS_ERR(motg->phy_reset_clk))
435 return 0;
436
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530437 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
438 if (ret) {
439 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
440 return ret;
441 }
442 usleep_range(10000, 12000);
443 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
444 if (ret)
445 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
446 return ret;
447}
448
449static int msm_otg_phy_reset(struct msm_otg *motg)
450{
451 u32 val;
452 int ret;
453 int retries;
454
455 ret = msm_otg_link_clk_reset(motg, 1);
456 if (ret)
457 return ret;
458 ret = msm_otg_phy_clk_reset(motg);
459 if (ret)
460 return ret;
461 ret = msm_otg_link_clk_reset(motg, 0);
462 if (ret)
463 return ret;
464
465 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
466 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
467
468 for (retries = 3; retries > 0; retries--) {
469 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
470 ULPI_CLR(ULPI_FUNC_CTRL));
471 if (!ret)
472 break;
473 ret = msm_otg_phy_clk_reset(motg);
474 if (ret)
475 return ret;
476 }
477 if (!retries)
478 return -ETIMEDOUT;
479
480 /* This reset calibrates the phy, if the above write succeeded */
481 ret = msm_otg_phy_clk_reset(motg);
482 if (ret)
483 return ret;
484
485 for (retries = 3; retries > 0; retries--) {
486 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
487 if (ret != -ETIMEDOUT)
488 break;
489 ret = msm_otg_phy_clk_reset(motg);
490 if (ret)
491 return ret;
492 }
493 if (!retries)
494 return -ETIMEDOUT;
495
496 dev_info(motg->otg.dev, "phy_reset: success\n");
497 return 0;
498}
499
500#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530501static int msm_otg_link_reset(struct msm_otg *motg)
502{
503 int cnt = 0;
504
505 writel_relaxed(USBCMD_RESET, USB_USBCMD);
506 while (cnt < LINK_RESET_TIMEOUT_USEC) {
507 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
508 break;
509 udelay(1);
510 cnt++;
511 }
512 if (cnt >= LINK_RESET_TIMEOUT_USEC)
513 return -ETIMEDOUT;
514
515 /* select ULPI phy */
516 writel_relaxed(0x80000000, USB_PORTSC);
517 writel_relaxed(0x0, USB_AHBBURST);
518 writel_relaxed(0x00, USB_AHBMODE);
519
520 return 0;
521}
522
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530523static int msm_otg_reset(struct otg_transceiver *otg)
524{
525 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
526 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530527 int ret;
528 u32 val = 0;
529 u32 ulpi_val = 0;
530
Ofir Cohen4da266f2012-01-03 10:19:29 +0200531 /*
532 * USB PHY and Link reset also reset the USB BAM.
533 * Thus perform reset operation only once to avoid
534 * USB BAM reset on other cases e.g. USB cable disconnections.
535 */
536 if (pdata->disable_reset_on_disconnect) {
537 if (motg->reset_counter)
538 return 0;
539 else
540 motg->reset_counter++;
541 }
542
Manu Gautam28b1bac2012-01-30 16:43:06 +0530543 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530544 ret = msm_otg_phy_reset(motg);
545 if (ret) {
546 dev_err(otg->dev, "phy_reset failed\n");
547 return ret;
548 }
549
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530550 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530551 ret = msm_otg_link_reset(motg);
552 if (ret) {
553 dev_err(otg->dev, "link reset failed\n");
554 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530555 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530556 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530557
558 ulpi_init(motg);
559
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700560 /* Ensure that RESET operation is completed before turning off clock */
561 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530562
Manu Gautam28b1bac2012-01-30 16:43:06 +0530563 clk_disable_unprepare(motg->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530565 if (pdata->otg_control == OTG_PHY_CONTROL) {
566 val = readl_relaxed(USB_OTGSC);
567 if (pdata->mode == USB_OTG) {
568 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
569 val |= OTGSC_IDIE | OTGSC_BSVIE;
570 } else if (pdata->mode == USB_PERIPHERAL) {
571 ulpi_val = ULPI_INT_SESS_VALID;
572 val |= OTGSC_BSVIE;
573 }
574 writel_relaxed(val, USB_OTGSC);
575 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
576 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530577 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
578 /* Enable PMIC pull-up */
579 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530580 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700581
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530582 return 0;
583}
584
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530585static const char *timer_string(int bit)
586{
587 switch (bit) {
588 case A_WAIT_VRISE: return "a_wait_vrise";
589 case A_WAIT_VFALL: return "a_wait_vfall";
590 case B_SRP_FAIL: return "b_srp_fail";
591 case A_WAIT_BCON: return "a_wait_bcon";
592 case A_AIDL_BDIS: return "a_aidl_bdis";
593 case A_BIDL_ADIS: return "a_bidl_adis";
594 case B_ASE0_BRST: return "b_ase0_brst";
595 case A_TST_MAINT: return "a_tst_maint";
596 case B_TST_SRP: return "b_tst_srp";
597 case B_TST_CONFIG: return "b_tst_config";
598 default: return "UNDEFINED";
599 }
600}
601
602static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
603{
604 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
605
606 switch (motg->active_tmout) {
607 case A_WAIT_VRISE:
608 /* TODO: use vbus_vld interrupt */
609 set_bit(A_VBUS_VLD, &motg->inputs);
610 break;
611 case A_TST_MAINT:
612 /* OTG PET: End session after TA_TST_MAINT */
613 set_bit(A_BUS_DROP, &motg->inputs);
614 break;
615 case B_TST_SRP:
616 /*
617 * OTG PET: Initiate SRP after TB_TST_SRP of
618 * previous session end.
619 */
620 set_bit(B_BUS_REQ, &motg->inputs);
621 break;
622 case B_TST_CONFIG:
623 clear_bit(A_CONN, &motg->inputs);
624 break;
625 default:
626 set_bit(motg->active_tmout, &motg->tmouts);
627 }
628
629 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
630 queue_work(system_nrt_wq, &motg->sm_work);
631 return HRTIMER_NORESTART;
632}
633
634static void msm_otg_del_timer(struct msm_otg *motg)
635{
636 int bit = motg->active_tmout;
637
638 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
639 div_s64(ktime_to_us(hrtimer_get_remaining(
640 &motg->timer)), 1000));
641 hrtimer_cancel(&motg->timer);
642 clear_bit(bit, &motg->tmouts);
643}
644
645static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
646{
647 clear_bit(bit, &motg->tmouts);
648 motg->active_tmout = bit;
649 pr_debug("starting %s timer\n", timer_string(bit));
650 hrtimer_start(&motg->timer,
651 ktime_set(time / 1000, (time % 1000) * 1000000),
652 HRTIMER_MODE_REL);
653}
654
655static void msm_otg_init_timer(struct msm_otg *motg)
656{
657 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
658 motg->timer.function = msm_otg_timer_func;
659}
660
661static int msm_otg_start_hnp(struct otg_transceiver *otg)
662{
663 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
664
665 if (otg->state != OTG_STATE_A_HOST) {
666 pr_err("HNP can not be initiated in %s state\n",
667 otg_state_string(otg->state));
668 return -EINVAL;
669 }
670
671 pr_debug("A-Host: HNP initiated\n");
672 clear_bit(A_BUS_REQ, &motg->inputs);
673 queue_work(system_nrt_wq, &motg->sm_work);
674 return 0;
675}
676
677static int msm_otg_start_srp(struct otg_transceiver *otg)
678{
679 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
680 u32 val;
681 int ret = 0;
682
683 if (otg->state != OTG_STATE_B_IDLE) {
684 pr_err("SRP can not be initiated in %s state\n",
685 otg_state_string(otg->state));
686 ret = -EINVAL;
687 goto out;
688 }
689
690 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
691 pr_debug("initial conditions of SRP are not met. Try again"
692 "after some time\n");
693 ret = -EAGAIN;
694 goto out;
695 }
696
697 pr_debug("B-Device SRP started\n");
698
699 /*
700 * PHY won't pull D+ high unless it detects Vbus valid.
701 * Since by definition, SRP is only done when Vbus is not valid,
702 * software work-around needs to be used to spoof the PHY into
703 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
704 * VBUSVLDEXT register bits.
705 */
706 ulpi_write(otg, 0x03, 0x97);
707 /*
708 * Harware auto assist data pulsing: Data pulse is given
709 * for 7msec; wait for vbus
710 */
711 val = readl_relaxed(USB_OTGSC);
712 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
713
714 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
715out:
716 return ret;
717}
718
719static void msm_otg_host_hnp_enable(struct otg_transceiver *otg, bool enable)
720{
721 struct usb_hcd *hcd = bus_to_hcd(otg->host);
722 struct usb_device *rhub = otg->host->root_hub;
723
724 if (enable) {
725 pm_runtime_disable(&rhub->dev);
726 rhub->state = USB_STATE_NOTATTACHED;
727 hcd->driver->bus_suspend(hcd);
728 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
729 } else {
730 usb_remove_hcd(hcd);
731 msm_otg_reset(otg);
732 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
733 }
734}
735
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530736static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
737{
738 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
739
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530740 if (aca_enabled() || (test_bit(ID, &motg->inputs) &&
741 !test_bit(ID_A, &motg->inputs)))
742 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530743
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530744 if (suspend) {
745 switch (otg->state) {
746 case OTG_STATE_A_WAIT_BCON:
747 if (TA_WAIT_BCON > 0)
748 break;
749 /* fall through */
750 case OTG_STATE_A_HOST:
751 pr_debug("host bus suspend\n");
752 clear_bit(A_BUS_REQ, &motg->inputs);
753 queue_work(system_nrt_wq, &motg->sm_work);
754 break;
755 default:
756 break;
757 }
758 } else {
759 switch (otg->state) {
760 case OTG_STATE_A_SUSPEND:
761 /* Remote wakeup or resume */
762 set_bit(A_BUS_REQ, &motg->inputs);
763 otg->state = OTG_STATE_A_HOST;
764 break;
765 default:
766 break;
767 }
768 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530769 return 0;
770}
771
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530772#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530773#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
774
775#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530776static int msm_otg_suspend(struct msm_otg *motg)
777{
778 struct otg_transceiver *otg = &motg->otg;
779 struct usb_bus *bus = otg->host;
780 struct msm_otg_platform_data *pdata = motg->pdata;
781 int cnt = 0;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530782 bool host_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530783 u32 phy_ctrl_val = 0, cmd_val;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530784 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530785
786 if (atomic_read(&motg->in_lpm))
787 return 0;
788
789 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530790 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530791 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530792 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530793 * Chipidea 45-nm PHY suspend sequence:
794 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530795 * Interrupt Latch Register auto-clear feature is not present
796 * in all PHY versions. Latch register is clear on read type.
797 * Clear latch register to avoid spurious wakeup from
798 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530799 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530800 * PHY comparators are disabled when PHY enters into low power
801 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
802 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
803 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530804 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530805 * PLL is not turned off when PHY enters into low power mode (LPM).
806 * Disable PLL for maximum power savings.
807 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530808
809 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
810 ulpi_read(otg, 0x14);
811 if (pdata->otg_control == OTG_PHY_CONTROL)
812 ulpi_write(otg, 0x01, 0x30);
813 ulpi_write(otg, 0x08, 0x09);
814 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530815
816 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700817 * Turn off the OTG comparators, if depends on PMIC for
818 * VBUS and ID notifications.
819 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530820 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700821 ulpi_write(otg, OTG_COMP_DISABLE,
822 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
823 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
824 }
825
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530826 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530827 * PHY may take some time or even fail to enter into low power
828 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
829 * in failure case.
830 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530831 portsc = readl_relaxed(USB_PORTSC);
832 if (!(portsc & PORTSC_PHCD)) {
833 writel_relaxed(portsc | PORTSC_PHCD,
834 USB_PORTSC);
835 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
836 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
837 break;
838 udelay(1);
839 cnt++;
840 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841 }
842
843 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
844 dev_err(otg->dev, "Unable to suspend PHY\n");
845 msm_otg_reset(otg);
846 enable_irq(motg->irq);
847 return -ETIMEDOUT;
848 }
849
850 /*
851 * PHY has capability to generate interrupt asynchronously in low
852 * power mode (LPM). This interrupt is level triggered. So USB IRQ
853 * line must be disabled till async interrupt enable bit is cleared
854 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
855 * block data communication from PHY.
856 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530857 cmd_val = readl_relaxed(USB_USBCMD);
858 if (host_bus_suspend)
859 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
860 else
861 cmd_val |= ULPI_STP_CTRL;
862 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530863
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530864 /*
865 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
866 * PHY retention and collapse can not happen with VDP_SRC enabled.
867 */
868 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200869 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
870 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
871 /* Enable PHY HV interrupts to wake MPM/Link */
872 phy_ctrl_val |=
873 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
874
875 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 motg->lpm_flags |= PHY_RETENTIONED;
877 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530878
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 /* Ensure that above operation is completed before turning off clocks */
880 mb();
Manu Gautam28b1bac2012-01-30 16:43:06 +0530881 clk_disable_unprepare(motg->pclk);
882 clk_disable_unprepare(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530883
Anji jonnala7da3f262011-12-02 17:22:14 -0800884 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800885 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800886
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530887 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
888 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 msm_hsusb_ldo_enable(motg, 0);
890 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530891 }
892
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530893 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530895 msm_hsusb_mhl_switch_enable(motg, 0);
896 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
898 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530899 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 if (motg->pdata->pmic_id_irq)
901 enable_irq_wake(motg->pdata->pmic_id_irq);
902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903 if (bus)
904 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
905
906 atomic_set(&motg->in_lpm, 1);
907 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909
910 dev_info(otg->dev, "USB in low power mode\n");
911
912 return 0;
913}
914
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530915static int msm_otg_resume(struct msm_otg *motg)
916{
917 struct otg_transceiver *otg = &motg->otg;
918 struct usb_bus *bus = otg->host;
919 int cnt = 0;
920 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200921 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800922 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530923
924 if (!atomic_read(&motg->in_lpm))
925 return 0;
926
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800928
929 /* Vote for TCXO when waking up the phy */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800930 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800931 if (ret)
932 dev_err(otg->dev, "%s failed to vote for "
933 "TCXO D0 buffer%d\n", __func__, ret);
934
Manu Gautam28b1bac2012-01-30 16:43:06 +0530935 clk_prepare_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200936
Manu Gautam28b1bac2012-01-30 16:43:06 +0530937 clk_prepare_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530938
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
940 msm_hsusb_ldo_enable(motg, 1);
941 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
942 }
943
944 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530945 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530946 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200947 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
948 phy_ctrl_val |= PHY_RETEN;
949 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
950 /* Disable PHY HV interrupts */
951 phy_ctrl_val &=
952 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
953 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530955 }
956
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530957 temp = readl(USB_USBCMD);
958 temp &= ~ASYNC_INTR_CTRL;
959 temp &= ~ULPI_STP_CTRL;
960 writel(temp, USB_USBCMD);
961
962 /*
963 * PHY comes out of low power mode (LPM) in case of wakeup
964 * from asynchronous interrupt.
965 */
966 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
967 goto skip_phy_resume;
968
969 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
970 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
971 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
972 break;
973 udelay(1);
974 cnt++;
975 }
976
977 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
978 /*
979 * This is a fatal error. Reset the link and
980 * PHY. USB state can not be restored. Re-insertion
981 * of USB cable is the only way to get USB working.
982 */
983 dev_err(otg->dev, "Unable to resume USB."
984 "Re-plugin the cable\n");
985 msm_otg_reset(otg);
986 }
987
988skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 /* Turn on the OTG comparators on resume */
990 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
991 ulpi_write(otg, OTG_COMP_DISABLE,
992 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
993 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
994 }
995 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 if (motg->pdata->pmic_id_irq)
998 disable_irq_wake(motg->pdata->pmic_id_irq);
999 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301000 if (bus)
1001 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1002
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301003 atomic_set(&motg->in_lpm, 0);
1004
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301005 if (motg->async_int) {
1006 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301007 enable_irq(motg->irq);
1008 }
1009
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301010 dev_info(otg->dev, "USB exited from low power mode\n");
1011
1012 return 0;
1013}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301014#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301015
Amit Blay0f7edf72012-01-15 10:11:27 +02001016static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1017{
1018 struct power_supply *psy;
1019
1020 psy = power_supply_get_by_name("usb");
1021 if (!psy)
1022 goto psy_not_supported;
1023
1024 if (motg->cur_power == 0 && mA > 0) {
1025 /* Enable charging */
1026 if (power_supply_set_online(psy, true))
1027 goto psy_not_supported;
1028 } else if (motg->cur_power > 0 && mA == 0) {
1029 /* Disable charging */
1030 if (power_supply_set_online(psy, false))
1031 goto psy_not_supported;
1032 return 0;
1033 }
1034 /* Set max current limit */
1035 if (power_supply_set_current_limit(psy, 1000*mA))
1036 goto psy_not_supported;
1037
1038 return 0;
1039
1040psy_not_supported:
1041 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
1042 return -ENXIO;
1043}
1044
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301045static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1046{
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301047 struct usb_gadget *g = motg->otg.gadget;
1048
1049 if (g && g->is_a_peripheral)
1050 return;
1051
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301052 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1053 motg->chg_type == USB_ACA_A_CHARGER ||
1054 motg->chg_type == USB_ACA_B_CHARGER ||
1055 motg->chg_type == USB_ACA_C_CHARGER) &&
1056 mA > IDEV_ACA_CHG_LIMIT)
1057 mA = IDEV_ACA_CHG_LIMIT;
1058
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301059 if (motg->cur_power == mA)
1060 return;
1061
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301062 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001063
1064 /*
1065 * Use Power Supply API if supported, otherwise fallback
1066 * to legacy pm8921 API.
1067 */
1068 if (msm_otg_notify_power_supply(motg, mA))
1069 pm8921_charger_vbus_draw(mA);
1070
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301071 motg->cur_power = mA;
1072}
1073
1074static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
1075{
1076 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1077
1078 /*
1079 * Gadget driver uses set_power method to notify about the
1080 * available current based on suspend/configured states.
1081 *
1082 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1083 * states when CDP/ACA is connected.
1084 */
1085 if (motg->chg_type == USB_SDP_CHARGER)
1086 msm_otg_notify_charger(motg, mA);
1087
1088 return 0;
1089}
1090
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301091static void msm_otg_start_host(struct otg_transceiver *otg, int on)
1092{
1093 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1094 struct msm_otg_platform_data *pdata = motg->pdata;
1095 struct usb_hcd *hcd;
1096
1097 if (!otg->host)
1098 return;
1099
1100 hcd = bus_to_hcd(otg->host);
1101
1102 if (on) {
1103 dev_dbg(otg->dev, "host on\n");
1104
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301105 /*
1106 * Some boards have a switch cotrolled by gpio
1107 * to enable/disable internal HUB. Enable internal
1108 * HUB before kicking the host.
1109 */
1110 if (pdata->setup_gpio)
1111 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301112 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301113 } else {
1114 dev_dbg(otg->dev, "host off\n");
1115
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301116 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301117 /* HCD core reset all bits of PORTSC. select ULPI phy */
1118 writel_relaxed(0x80000000, USB_PORTSC);
1119
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 if (pdata->setup_gpio)
1121 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301122 }
1123}
1124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001125static int msm_otg_usbdev_notify(struct notifier_block *self,
1126 unsigned long action, void *priv)
1127{
1128 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301129 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301130 struct usb_device *udev = priv;
1131
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301132 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1133 goto out;
1134
1135 if (udev->bus != motg->otg.host)
1136 goto out;
1137 /*
1138 * Interested in devices connected directly to the root hub.
1139 * ACA dock can supply IDEV_CHG irrespective devices connected
1140 * on the accessory port.
1141 */
1142 if (!udev->parent || udev->parent->parent ||
1143 motg->chg_type == USB_ACA_DOCK_CHARGER)
1144 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001145
1146 switch (action) {
1147 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301148 if (aca_enabled())
1149 usb_disable_autosuspend(udev);
1150 if (otg->state == OTG_STATE_A_WAIT_BCON) {
1151 pr_debug("B_CONN set\n");
1152 set_bit(B_CONN, &motg->inputs);
1153 msm_otg_del_timer(motg);
1154 otg->state = OTG_STATE_A_HOST;
1155 /*
1156 * OTG PET: A-device must end session within
1157 * 10 sec after PET enumeration.
1158 */
1159 if (udev->quirks & USB_QUIRK_OTG_PET)
1160 msm_otg_start_timer(motg, TA_TST_MAINT,
1161 A_TST_MAINT);
1162 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301163 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001164 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001165 if (udev->actconfig)
1166 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1167 else
1168 motg->mA_port = IUNIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301169 if (otg->state == OTG_STATE_B_HOST)
1170 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301171 break;
1172 case USB_DEVICE_REMOVE:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301173 if ((otg->state == OTG_STATE_A_HOST) ||
1174 (otg->state == OTG_STATE_A_SUSPEND)) {
1175 pr_debug("B_CONN clear\n");
1176 clear_bit(B_CONN, &motg->inputs);
1177 /*
1178 * OTG PET: A-device must end session after
1179 * PET disconnection if it is enumerated
1180 * with bcdDevice[0] = 1. USB core sets
1181 * bus->otg_vbus_off for us. clear it here.
1182 */
1183 if (udev->bus->otg_vbus_off) {
1184 udev->bus->otg_vbus_off = 0;
1185 set_bit(A_BUS_DROP, &motg->inputs);
1186 }
1187 queue_work(system_nrt_wq, &motg->sm_work);
1188 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001189 default:
1190 break;
1191 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301192 if (test_bit(ID_A, &motg->inputs))
1193 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1194 motg->mA_port);
1195out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 return NOTIFY_OK;
1197}
1198
Mayank Ranae3926882011-12-26 09:47:54 +05301199static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1200{
1201 int ret;
1202 static bool vbus_is_on;
1203
1204 if (vbus_is_on == on)
1205 return;
1206
1207 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301208 ret = motg->pdata->vbus_power(on);
1209 if (!ret)
1210 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301211 return;
1212 }
1213
1214 if (!vbus_otg) {
1215 pr_err("vbus_otg is NULL.");
1216 return;
1217 }
1218
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001219 /*
1220 * if entering host mode tell the charger to not draw any current
1221 * from usb - if exiting host mode let the charger draw current
1222 */
1223 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301224 if (on) {
1225 ret = regulator_enable(vbus_otg);
1226 if (ret) {
1227 pr_err("unable to enable vbus_otg\n");
1228 return;
1229 }
1230 vbus_is_on = true;
1231 } else {
1232 ret = regulator_disable(vbus_otg);
1233 if (ret) {
1234 pr_err("unable to disable vbus_otg\n");
1235 return;
1236 }
1237 vbus_is_on = false;
1238 }
1239}
1240
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301241static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1242{
1243 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1244 struct usb_hcd *hcd;
1245
1246 /*
1247 * Fail host registration if this board can support
1248 * only peripheral configuration.
1249 */
1250 if (motg->pdata->mode == USB_PERIPHERAL) {
1251 dev_info(otg->dev, "Host mode is not supported\n");
1252 return -ENODEV;
1253 }
1254
Mayank Ranae3926882011-12-26 09:47:54 +05301255 if (!motg->pdata->vbus_power && host) {
1256 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1257 if (IS_ERR(vbus_otg)) {
1258 pr_err("Unable to get vbus_otg\n");
1259 return -ENODEV;
1260 }
1261 }
1262
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301263 if (!host) {
1264 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301265 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001266 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301267 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301268 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301269 otg->host = NULL;
1270 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301271 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301272 } else {
1273 otg->host = NULL;
1274 }
1275
Mayank Ranae3926882011-12-26 09:47:54 +05301276 if (vbus_otg)
1277 regulator_put(vbus_otg);
1278
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301279 return 0;
1280 }
1281
1282 hcd = bus_to_hcd(host);
1283 hcd->power_budget = motg->pdata->power_budget;
1284
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301285#ifdef CONFIG_USB_OTG
1286 host->otg_port = 1;
1287#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1289 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301290 otg->host = host;
1291 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1292
1293 /*
1294 * Kick the state machine work, if peripheral is not supported
1295 * or peripheral is already registered with us.
1296 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301297 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1298 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301299 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301300 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301301
1302 return 0;
1303}
1304
1305static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1306{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301307 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301308 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1309 struct msm_otg_platform_data *pdata = motg->pdata;
1310
1311 if (!otg->gadget)
1312 return;
1313
1314 if (on) {
1315 dev_dbg(otg->dev, "gadget on\n");
1316 /*
1317 * Some boards have a switch cotrolled by gpio
1318 * to enable/disable internal HUB. Disable internal
1319 * HUB before kicking the gadget.
1320 */
1321 if (pdata->setup_gpio)
1322 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301323 /*
1324 * vote for minimum dma_latency to prevent idle
1325 * power collapse(pc) while running in peripheral mode.
1326 */
1327 otg_pm_qos_update_latency(motg, 1);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301328 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301329 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301330 ret = msm_bus_scale_client_update_request(
1331 motg->bus_perf_client, 1);
1332 if (ret)
1333 dev_err(motg->otg.dev, "%s: Failed to vote for "
1334 "bus bandwidth %d\n", __func__, ret);
1335 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301336 usb_gadget_vbus_connect(otg->gadget);
1337 } else {
1338 dev_dbg(otg->dev, "gadget off\n");
1339 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301340 otg_pm_qos_update_latency(motg, 0);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301341 /* Configure BUS performance parameters to default */
1342 if (motg->bus_perf_client) {
1343 ret = msm_bus_scale_client_update_request(
1344 motg->bus_perf_client, 0);
1345 if (ret)
1346 dev_err(motg->otg.dev, "%s: Failed to devote "
1347 "for bus bw %d\n", __func__, ret);
1348 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301349 if (pdata->setup_gpio)
1350 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1351 }
1352
1353}
1354
1355static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1356 struct usb_gadget *gadget)
1357{
1358 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1359
1360 /*
1361 * Fail peripheral registration if this board can support
1362 * only host configuration.
1363 */
1364 if (motg->pdata->mode == USB_HOST) {
1365 dev_info(otg->dev, "Peripheral mode is not supported\n");
1366 return -ENODEV;
1367 }
1368
1369 if (!gadget) {
1370 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301371 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301372 msm_otg_start_peripheral(otg, 0);
1373 otg->gadget = NULL;
1374 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301375 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301376 } else {
1377 otg->gadget = NULL;
1378 }
1379
1380 return 0;
1381 }
1382 otg->gadget = gadget;
1383 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1384
1385 /*
1386 * Kick the state machine work, if host is not supported
1387 * or host is already registered with us.
1388 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301389 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1390 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301391 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301392 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301393
1394 return 0;
1395}
1396
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397static bool msm_chg_aca_detect(struct msm_otg *motg)
1398{
1399 struct otg_transceiver *otg = &motg->otg;
1400 u32 int_sts;
1401 bool ret = false;
1402
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301403 if (!aca_enabled())
1404 goto out;
1405
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001406 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1407 goto out;
1408
1409 int_sts = ulpi_read(otg, 0x87);
1410 switch (int_sts & 0x1C) {
1411 case 0x08:
1412 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1413 dev_dbg(otg->dev, "ID_A\n");
1414 motg->chg_type = USB_ACA_A_CHARGER;
1415 motg->chg_state = USB_CHG_STATE_DETECTED;
1416 clear_bit(ID_B, &motg->inputs);
1417 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301418 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001419 ret = true;
1420 }
1421 break;
1422 case 0x0C:
1423 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1424 dev_dbg(otg->dev, "ID_B\n");
1425 motg->chg_type = USB_ACA_B_CHARGER;
1426 motg->chg_state = USB_CHG_STATE_DETECTED;
1427 clear_bit(ID_A, &motg->inputs);
1428 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301429 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001430 ret = true;
1431 }
1432 break;
1433 case 0x10:
1434 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1435 dev_dbg(otg->dev, "ID_C\n");
1436 motg->chg_type = USB_ACA_C_CHARGER;
1437 motg->chg_state = USB_CHG_STATE_DETECTED;
1438 clear_bit(ID_A, &motg->inputs);
1439 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301440 set_bit(ID, &motg->inputs);
1441 ret = true;
1442 }
1443 break;
1444 case 0x04:
1445 if (test_and_clear_bit(ID, &motg->inputs)) {
1446 dev_dbg(otg->dev, "ID_GND\n");
1447 motg->chg_type = USB_INVALID_CHARGER;
1448 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1449 clear_bit(ID_A, &motg->inputs);
1450 clear_bit(ID_B, &motg->inputs);
1451 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001452 ret = true;
1453 }
1454 break;
1455 default:
1456 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1457 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301458 test_and_clear_bit(ID_C, &motg->inputs) |
1459 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301461 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001462 motg->chg_type = USB_INVALID_CHARGER;
1463 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1464 }
1465 }
1466out:
1467 return ret;
1468}
1469
1470static void msm_chg_enable_aca_det(struct msm_otg *motg)
1471{
1472 struct otg_transceiver *otg = &motg->otg;
1473
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301474 if (!aca_enabled())
1475 return;
1476
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477 switch (motg->pdata->phy_type) {
1478 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301479 /* Disable ID_GND in link and PHY */
1480 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1481 OTGSC_IDIE), USB_OTGSC);
1482 ulpi_write(otg, 0x01, 0x0C);
1483 ulpi_write(otg, 0x10, 0x0F);
1484 ulpi_write(otg, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301485 /* Disable PMIC ID pull-up */
1486 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301487 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001488 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301489 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 break;
1491 default:
1492 break;
1493 }
1494}
1495
1496static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1497{
1498 struct otg_transceiver *otg = &motg->otg;
1499
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301500 if (!aca_enabled())
1501 return;
1502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001503 switch (motg->pdata->phy_type) {
1504 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301505 /* Enable ACA Detection interrupt (on any RID change) */
1506 ulpi_write(otg, 0x01, 0x94);
1507 break;
1508 default:
1509 break;
1510 }
1511}
1512
1513static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1514{
1515 struct otg_transceiver *otg = &motg->otg;
1516
1517 if (!aca_enabled())
1518 return;
1519
1520 switch (motg->pdata->phy_type) {
1521 case SNPS_28NM_INTEGRATED_PHY:
1522 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001523 break;
1524 default:
1525 break;
1526 }
1527}
1528
1529static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1530{
1531 struct otg_transceiver *otg = &motg->otg;
1532 bool ret = false;
1533
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301534 if (!aca_enabled())
1535 return ret;
1536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001537 switch (motg->pdata->phy_type) {
1538 case SNPS_28NM_INTEGRATED_PHY:
1539 if (ulpi_read(otg, 0x91) & 1) {
1540 dev_dbg(otg->dev, "RID change\n");
1541 ulpi_write(otg, 0x01, 0x92);
1542 ret = msm_chg_aca_detect(motg);
1543 }
1544 default:
1545 break;
1546 }
1547 return ret;
1548}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301549
1550static void msm_otg_id_timer_func(unsigned long data)
1551{
1552 struct msm_otg *motg = (struct msm_otg *) data;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301553 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301554
1555 if (!aca_enabled())
1556 return;
1557
1558 if (atomic_read(&motg->in_lpm)) {
1559 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1560 return;
1561 }
1562
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301563 if (otg->state == OTG_STATE_A_SUSPEND)
1564 goto out;
1565
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301566 if (msm_chg_check_aca_intr(motg)) {
1567 dev_dbg(motg->otg.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301568 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301569 }
1570
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301571out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301572 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1573 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1574}
1575
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301576static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1577{
1578 struct otg_transceiver *otg = &motg->otg;
1579 u32 chg_det;
1580 bool ret = false;
1581
1582 switch (motg->pdata->phy_type) {
1583 case CI_45NM_INTEGRATED_PHY:
1584 chg_det = ulpi_read(otg, 0x34);
1585 ret = chg_det & (1 << 4);
1586 break;
1587 case SNPS_28NM_INTEGRATED_PHY:
1588 chg_det = ulpi_read(otg, 0x87);
1589 ret = chg_det & 1;
1590 break;
1591 default:
1592 break;
1593 }
1594 return ret;
1595}
1596
1597static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1598{
1599 struct otg_transceiver *otg = &motg->otg;
1600 u32 chg_det;
1601
1602 switch (motg->pdata->phy_type) {
1603 case CI_45NM_INTEGRATED_PHY:
1604 chg_det = ulpi_read(otg, 0x34);
1605 /* Turn off charger block */
1606 chg_det |= ~(1 << 1);
1607 ulpi_write(otg, chg_det, 0x34);
1608 udelay(20);
1609 /* control chg block via ULPI */
1610 chg_det &= ~(1 << 3);
1611 ulpi_write(otg, chg_det, 0x34);
1612 /* put it in host mode for enabling D- source */
1613 chg_det &= ~(1 << 2);
1614 ulpi_write(otg, chg_det, 0x34);
1615 /* Turn on chg detect block */
1616 chg_det &= ~(1 << 1);
1617 ulpi_write(otg, chg_det, 0x34);
1618 udelay(20);
1619 /* enable chg detection */
1620 chg_det &= ~(1 << 0);
1621 ulpi_write(otg, chg_det, 0x34);
1622 break;
1623 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301624 /* Turn off VDP_SRC */
1625 ulpi_write(otg, 0x3, 0x86);
1626 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301627 /*
1628 * Configure DM as current source, DP as current sink
1629 * and enable battery charging comparators.
1630 */
1631 ulpi_write(otg, 0x8, 0x85);
1632 ulpi_write(otg, 0x2, 0x85);
1633 ulpi_write(otg, 0x1, 0x85);
1634 break;
1635 default:
1636 break;
1637 }
1638}
1639
1640static bool msm_chg_check_primary_det(struct msm_otg *motg)
1641{
1642 struct otg_transceiver *otg = &motg->otg;
1643 u32 chg_det;
1644 bool ret = false;
1645
1646 switch (motg->pdata->phy_type) {
1647 case CI_45NM_INTEGRATED_PHY:
1648 chg_det = ulpi_read(otg, 0x34);
1649 ret = chg_det & (1 << 4);
1650 break;
1651 case SNPS_28NM_INTEGRATED_PHY:
1652 chg_det = ulpi_read(otg, 0x87);
1653 ret = chg_det & 1;
1654 break;
1655 default:
1656 break;
1657 }
1658 return ret;
1659}
1660
1661static void msm_chg_enable_primary_det(struct msm_otg *motg)
1662{
1663 struct otg_transceiver *otg = &motg->otg;
1664 u32 chg_det;
1665
1666 switch (motg->pdata->phy_type) {
1667 case CI_45NM_INTEGRATED_PHY:
1668 chg_det = ulpi_read(otg, 0x34);
1669 /* enable chg detection */
1670 chg_det &= ~(1 << 0);
1671 ulpi_write(otg, chg_det, 0x34);
1672 break;
1673 case SNPS_28NM_INTEGRATED_PHY:
1674 /*
1675 * Configure DP as current source, DM as current sink
1676 * and enable battery charging comparators.
1677 */
1678 ulpi_write(otg, 0x2, 0x85);
1679 ulpi_write(otg, 0x1, 0x85);
1680 break;
1681 default:
1682 break;
1683 }
1684}
1685
1686static bool msm_chg_check_dcd(struct msm_otg *motg)
1687{
1688 struct otg_transceiver *otg = &motg->otg;
1689 u32 line_state;
1690 bool ret = false;
1691
1692 switch (motg->pdata->phy_type) {
1693 case CI_45NM_INTEGRATED_PHY:
1694 line_state = ulpi_read(otg, 0x15);
1695 ret = !(line_state & 1);
1696 break;
1697 case SNPS_28NM_INTEGRATED_PHY:
1698 line_state = ulpi_read(otg, 0x87);
1699 ret = line_state & 2;
1700 break;
1701 default:
1702 break;
1703 }
1704 return ret;
1705}
1706
1707static void msm_chg_disable_dcd(struct msm_otg *motg)
1708{
1709 struct otg_transceiver *otg = &motg->otg;
1710 u32 chg_det;
1711
1712 switch (motg->pdata->phy_type) {
1713 case CI_45NM_INTEGRATED_PHY:
1714 chg_det = ulpi_read(otg, 0x34);
1715 chg_det &= ~(1 << 5);
1716 ulpi_write(otg, chg_det, 0x34);
1717 break;
1718 case SNPS_28NM_INTEGRATED_PHY:
1719 ulpi_write(otg, 0x10, 0x86);
1720 break;
1721 default:
1722 break;
1723 }
1724}
1725
1726static void msm_chg_enable_dcd(struct msm_otg *motg)
1727{
1728 struct otg_transceiver *otg = &motg->otg;
1729 u32 chg_det;
1730
1731 switch (motg->pdata->phy_type) {
1732 case CI_45NM_INTEGRATED_PHY:
1733 chg_det = ulpi_read(otg, 0x34);
1734 /* Turn on D+ current source */
1735 chg_det |= (1 << 5);
1736 ulpi_write(otg, chg_det, 0x34);
1737 break;
1738 case SNPS_28NM_INTEGRATED_PHY:
1739 /* Data contact detection enable */
1740 ulpi_write(otg, 0x10, 0x85);
1741 break;
1742 default:
1743 break;
1744 }
1745}
1746
1747static void msm_chg_block_on(struct msm_otg *motg)
1748{
1749 struct otg_transceiver *otg = &motg->otg;
1750 u32 func_ctrl, chg_det;
1751
1752 /* put the controller in non-driving mode */
1753 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1754 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1755 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1756 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1757
1758 switch (motg->pdata->phy_type) {
1759 case CI_45NM_INTEGRATED_PHY:
1760 chg_det = ulpi_read(otg, 0x34);
1761 /* control chg block via ULPI */
1762 chg_det &= ~(1 << 3);
1763 ulpi_write(otg, chg_det, 0x34);
1764 /* Turn on chg detect block */
1765 chg_det &= ~(1 << 1);
1766 ulpi_write(otg, chg_det, 0x34);
1767 udelay(20);
1768 break;
1769 case SNPS_28NM_INTEGRATED_PHY:
1770 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301771 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301772 /* Clear alt interrupt latch and enable bits */
1773 ulpi_write(otg, 0x1F, 0x92);
1774 ulpi_write(otg, 0x1F, 0x95);
1775 udelay(100);
1776 break;
1777 default:
1778 break;
1779 }
1780}
1781
1782static void msm_chg_block_off(struct msm_otg *motg)
1783{
1784 struct otg_transceiver *otg = &motg->otg;
1785 u32 func_ctrl, chg_det;
1786
1787 switch (motg->pdata->phy_type) {
1788 case CI_45NM_INTEGRATED_PHY:
1789 chg_det = ulpi_read(otg, 0x34);
1790 /* Turn off charger block */
1791 chg_det |= ~(1 << 1);
1792 ulpi_write(otg, chg_det, 0x34);
1793 break;
1794 case SNPS_28NM_INTEGRATED_PHY:
1795 /* Clear charger detecting control bits */
1796 ulpi_write(otg, 0x3F, 0x86);
1797 /* Clear alt interrupt latch and enable bits */
1798 ulpi_write(otg, 0x1F, 0x92);
1799 ulpi_write(otg, 0x1F, 0x95);
1800 break;
1801 default:
1802 break;
1803 }
1804
1805 /* put the controller in normal mode */
1806 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1807 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1808 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1809 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1810}
1811
Anji jonnalad270e2d2011-08-09 11:28:32 +05301812static const char *chg_to_string(enum usb_chg_type chg_type)
1813{
1814 switch (chg_type) {
1815 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1816 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1817 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1818 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1819 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1820 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1821 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1822 default: return "INVALID_CHARGER";
1823 }
1824}
1825
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301826#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1827#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301828#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1829#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301830static void msm_chg_detect_work(struct work_struct *w)
1831{
1832 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1833 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301834 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301835 unsigned long delay;
1836
1837 dev_dbg(otg->dev, "chg detection work\n");
1838 switch (motg->chg_state) {
1839 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301840 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301841 if (motg->pdata->enable_dcd)
1842 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001843 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1845 motg->dcd_retries = 0;
1846 delay = MSM_CHG_DCD_POLL_TIME;
1847 break;
1848 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 is_aca = msm_chg_aca_detect(motg);
1850 if (is_aca) {
1851 /*
1852 * ID_A can be ACA dock too. continue
1853 * primary detection after DCD.
1854 */
1855 if (test_bit(ID_A, &motg->inputs)) {
1856 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1857 } else {
1858 delay = 0;
1859 break;
1860 }
1861 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301862 if (motg->pdata->enable_dcd)
1863 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1865 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301866 if (motg->pdata->enable_dcd)
1867 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 msm_chg_enable_primary_det(motg);
1869 delay = MSM_CHG_PRIMARY_DET_TIME;
1870 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1871 } else {
1872 delay = MSM_CHG_DCD_POLL_TIME;
1873 }
1874 break;
1875 case USB_CHG_STATE_DCD_DONE:
1876 vout = msm_chg_check_primary_det(motg);
1877 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301878 if (test_bit(ID_A, &motg->inputs)) {
1879 motg->chg_type = USB_ACA_DOCK_CHARGER;
1880 motg->chg_state = USB_CHG_STATE_DETECTED;
1881 delay = 0;
1882 break;
1883 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301884 msm_chg_enable_secondary_det(motg);
1885 delay = MSM_CHG_SECONDARY_DET_TIME;
1886 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1887 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301888 if (test_bit(ID_A, &motg->inputs)) {
1889 motg->chg_type = USB_ACA_A_CHARGER;
1890 motg->chg_state = USB_CHG_STATE_DETECTED;
1891 delay = 0;
1892 break;
1893 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 motg->chg_type = USB_SDP_CHARGER;
1895 motg->chg_state = USB_CHG_STATE_DETECTED;
1896 delay = 0;
1897 }
1898 break;
1899 case USB_CHG_STATE_PRIMARY_DONE:
1900 vout = msm_chg_check_secondary_det(motg);
1901 if (vout)
1902 motg->chg_type = USB_DCP_CHARGER;
1903 else
1904 motg->chg_type = USB_CDP_CHARGER;
1905 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1906 /* fall through */
1907 case USB_CHG_STATE_SECONDARY_DONE:
1908 motg->chg_state = USB_CHG_STATE_DETECTED;
1909 case USB_CHG_STATE_DETECTED:
1910 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301912 /*
1913 * Spurious interrupt is seen after enabling ACA detection
1914 * due to which charger detection fails in case of PET.
1915 * Add delay of 100 microsec to avoid that.
1916 */
1917 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001918 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301919 dev_dbg(otg->dev, "chg_type = %s\n",
1920 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301921 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301922 return;
1923 default:
1924 return;
1925 }
1926
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301927 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301928}
1929
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301930/*
1931 * We support OTG, Peripheral only and Host only configurations. In case
1932 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1933 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1934 * enabled when switch is controlled by user and default mode is supplied
1935 * by board file, which can be changed by userspace later.
1936 */
1937static void msm_otg_init_sm(struct msm_otg *motg)
1938{
1939 struct msm_otg_platform_data *pdata = motg->pdata;
1940 u32 otgsc = readl(USB_OTGSC);
1941
1942 switch (pdata->mode) {
1943 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001944 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301945 if (pdata->default_mode == USB_HOST) {
1946 clear_bit(ID, &motg->inputs);
1947 } else if (pdata->default_mode == USB_PERIPHERAL) {
1948 set_bit(ID, &motg->inputs);
1949 set_bit(B_SESS_VLD, &motg->inputs);
1950 } else {
1951 set_bit(ID, &motg->inputs);
1952 clear_bit(B_SESS_VLD, &motg->inputs);
1953 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301954 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301955 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301956 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301957 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301958 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301959 set_bit(A_BUS_REQ, &motg->inputs);
1960 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 if (otgsc & OTGSC_BSV)
1962 set_bit(B_SESS_VLD, &motg->inputs);
1963 else
1964 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301965 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301966 if (pdata->pmic_id_irq) {
1967 if (irq_read_line(pdata->pmic_id_irq))
1968 set_bit(ID, &motg->inputs);
1969 else
1970 clear_bit(ID, &motg->inputs);
1971 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301972 /*
1973 * VBUS initial state is reported after PMIC
1974 * driver initialization. Wait for it.
1975 */
1976 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301977 }
1978 break;
1979 case USB_HOST:
1980 clear_bit(ID, &motg->inputs);
1981 break;
1982 case USB_PERIPHERAL:
1983 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301984 if (pdata->otg_control == OTG_PHY_CONTROL) {
1985 if (otgsc & OTGSC_BSV)
1986 set_bit(B_SESS_VLD, &motg->inputs);
1987 else
1988 clear_bit(B_SESS_VLD, &motg->inputs);
1989 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1990 /*
1991 * VBUS initial state is reported after PMIC
1992 * driver initialization. Wait for it.
1993 */
1994 wait_for_completion(&pmic_vbus_init);
1995 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301996 break;
1997 default:
1998 break;
1999 }
2000}
2001
2002static void msm_otg_sm_work(struct work_struct *w)
2003{
2004 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
2005 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302006 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302007
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302008 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302009 pr_debug("%s work\n", otg_state_string(otg->state));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302010 switch (otg->state) {
2011 case OTG_STATE_UNDEFINED:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302012 msm_otg_reset(otg);
2013 msm_otg_init_sm(motg);
2014 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302015 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2016 test_bit(ID, &motg->inputs)) {
2017 pm_runtime_put_noidle(otg->dev);
2018 pm_runtime_suspend(otg->dev);
2019 break;
2020 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302021 /* FALL THROUGH */
2022 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002023 if ((!test_bit(ID, &motg->inputs) ||
2024 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302025 pr_debug("!id || id_A\n");
2026 clear_bit(B_BUS_REQ, &motg->inputs);
2027 set_bit(A_BUS_REQ, &motg->inputs);
2028 otg->state = OTG_STATE_A_IDLE;
2029 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302030 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302031 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302032 switch (motg->chg_state) {
2033 case USB_CHG_STATE_UNDEFINED:
2034 msm_chg_detect_work(&motg->chg_work.work);
2035 break;
2036 case USB_CHG_STATE_DETECTED:
2037 switch (motg->chg_type) {
2038 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302039 /* Enable VDP_SRC */
2040 ulpi_write(otg, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302041 msm_otg_notify_charger(motg,
2042 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302043 pm_runtime_put_noidle(otg->dev);
2044 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302045 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302046 case USB_ACA_B_CHARGER:
2047 msm_otg_notify_charger(motg,
2048 IDEV_ACA_CHG_MAX);
2049 /*
2050 * (ID_B --> ID_C) PHY_ALT interrupt can
2051 * not be detected in LPM.
2052 */
2053 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302054 case USB_CDP_CHARGER:
2055 msm_otg_notify_charger(motg,
2056 IDEV_CHG_MAX);
2057 msm_otg_start_peripheral(otg, 1);
2058 otg->state = OTG_STATE_B_PERIPHERAL;
2059 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302060 case USB_ACA_C_CHARGER:
2061 msm_otg_notify_charger(motg,
2062 IDEV_ACA_CHG_MAX);
2063 msm_otg_start_peripheral(otg, 1);
2064 otg->state = OTG_STATE_B_PERIPHERAL;
2065 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302066 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302067 msm_otg_start_peripheral(otg, 1);
2068 otg->state = OTG_STATE_B_PERIPHERAL;
2069 break;
2070 default:
2071 break;
2072 }
2073 break;
2074 default:
2075 break;
2076 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302077 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2078 pr_debug("b_sess_end && b_bus_req\n");
2079 if (msm_otg_start_srp(otg) < 0) {
2080 clear_bit(B_BUS_REQ, &motg->inputs);
2081 work = 1;
2082 break;
2083 }
2084 otg->state = OTG_STATE_B_SRP_INIT;
2085 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2086 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302087 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302088 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302089 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 msm_otg_notify_charger(motg, 0);
2091 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2092 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302093 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302094 pm_runtime_put_noidle(otg->dev);
2095 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302096 }
2097 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302098 case OTG_STATE_B_SRP_INIT:
2099 if (!test_bit(ID, &motg->inputs) ||
2100 test_bit(ID_A, &motg->inputs) ||
2101 test_bit(ID_C, &motg->inputs) ||
2102 (test_bit(B_SESS_VLD, &motg->inputs) &&
2103 !test_bit(ID_B, &motg->inputs))) {
2104 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2105 msm_otg_del_timer(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002106 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302107 /*
2108 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2109 * bits after SRP initiation.
2110 */
2111 ulpi_write(otg, 0x0, 0x98);
2112 work = 1;
2113 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2114 pr_debug("b_srp_fail\n");
2115 pr_info("A-device did not respond to SRP\n");
2116 clear_bit(B_BUS_REQ, &motg->inputs);
2117 clear_bit(B_SRP_FAIL, &motg->tmouts);
2118 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
2119 ulpi_write(otg, 0x0, 0x98);
2120 otg->state = OTG_STATE_B_IDLE;
2121 motg->b_last_se0_sess = jiffies;
2122 work = 1;
2123 }
2124 break;
2125 case OTG_STATE_B_PERIPHERAL:
2126 if (!test_bit(ID, &motg->inputs) ||
2127 test_bit(ID_A, &motg->inputs) ||
2128 test_bit(ID_B, &motg->inputs) ||
2129 !test_bit(B_SESS_VLD, &motg->inputs)) {
2130 pr_debug("!id || id_a/b || !b_sess_vld\n");
2131 msm_otg_notify_charger(motg, 0);
2132 srp_reqd = otg->gadget->otg_srp_reqd;
2133 msm_otg_start_peripheral(otg, 0);
2134 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2135 motg->chg_type = USB_INVALID_CHARGER;
2136 if (test_bit(ID_B, &motg->inputs))
2137 clear_bit(ID_B, &motg->inputs);
2138 clear_bit(B_BUS_REQ, &motg->inputs);
2139 otg->state = OTG_STATE_B_IDLE;
2140 motg->b_last_se0_sess = jiffies;
2141 if (srp_reqd)
2142 msm_otg_start_timer(motg,
2143 TB_TST_SRP, B_TST_SRP);
2144 else
2145 work = 1;
2146 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2147 otg->gadget->b_hnp_enable &&
2148 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2149 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2150 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2151 /* D+ pullup should not be disconnected within 4msec
2152 * after A device suspends the bus. Otherwise PET will
2153 * fail the compliance test.
2154 */
2155 udelay(1000);
2156 msm_otg_start_peripheral(otg, 0);
2157 otg->state = OTG_STATE_B_WAIT_ACON;
2158 /*
2159 * start HCD even before A-device enable
2160 * pull-up to meet HNP timings.
2161 */
2162 otg->host->is_b_host = 1;
2163 msm_otg_start_host(otg, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302165 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002166 }
2167 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302168 case OTG_STATE_B_WAIT_ACON:
2169 if (!test_bit(ID, &motg->inputs) ||
2170 test_bit(ID_A, &motg->inputs) ||
2171 test_bit(ID_B, &motg->inputs) ||
2172 !test_bit(B_SESS_VLD, &motg->inputs)) {
2173 pr_debug("!id || id_a/b || !b_sess_vld\n");
2174 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302175 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302176 * A-device is physically disconnected during
2177 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302178 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302179 msm_otg_start_host(otg, 0);
2180 otg->host->is_b_host = 0;
2181
2182 clear_bit(B_BUS_REQ, &motg->inputs);
2183 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2184 motg->b_last_se0_sess = jiffies;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302185 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302186 msm_otg_reset(otg);
2187 work = 1;
2188 } else if (test_bit(A_CONN, &motg->inputs)) {
2189 pr_debug("a_conn\n");
2190 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2191 otg->state = OTG_STATE_B_HOST;
2192 /*
2193 * PET disconnects D+ pullup after reset is generated
2194 * by B device in B_HOST role which is not detected by
2195 * B device. As workaorund , start timer of 300msec
2196 * and stop timer if A device is enumerated else clear
2197 * A_CONN.
2198 */
2199 msm_otg_start_timer(motg, TB_TST_CONFIG,
2200 B_TST_CONFIG);
2201 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2202 pr_debug("b_ase0_brst_tmout\n");
2203 pr_info("B HNP fail:No response from A device\n");
2204 msm_otg_start_host(otg, 0);
2205 msm_otg_reset(otg);
2206 otg->host->is_b_host = 0;
2207 clear_bit(B_ASE0_BRST, &motg->tmouts);
2208 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2209 clear_bit(B_BUS_REQ, &motg->inputs);
2210 otg_send_event(OTG_EVENT_HNP_FAILED);
2211 otg->state = OTG_STATE_B_IDLE;
2212 work = 1;
2213 } else if (test_bit(ID_C, &motg->inputs)) {
2214 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2215 }
2216 break;
2217 case OTG_STATE_B_HOST:
2218 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2219 !test_bit(A_CONN, &motg->inputs) ||
2220 !test_bit(B_SESS_VLD, &motg->inputs)) {
2221 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2222 clear_bit(A_CONN, &motg->inputs);
2223 clear_bit(B_BUS_REQ, &motg->inputs);
2224 msm_otg_start_host(otg, 0);
2225 otg->host->is_b_host = 0;
2226 otg->state = OTG_STATE_B_IDLE;
2227 msm_otg_reset(otg);
2228 work = 1;
2229 } else if (test_bit(ID_C, &motg->inputs)) {
2230 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2231 }
2232 break;
2233 case OTG_STATE_A_IDLE:
2234 otg->default_a = 1;
2235 if (test_bit(ID, &motg->inputs) &&
2236 !test_bit(ID_A, &motg->inputs)) {
2237 pr_debug("id && !id_a\n");
2238 otg->default_a = 0;
2239 clear_bit(A_BUS_DROP, &motg->inputs);
2240 otg->state = OTG_STATE_B_IDLE;
2241 del_timer_sync(&motg->id_timer);
2242 msm_otg_link_reset(motg);
2243 msm_chg_enable_aca_intr(motg);
2244 msm_otg_notify_charger(motg, 0);
2245 work = 1;
2246 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2247 (test_bit(A_SRP_DET, &motg->inputs) ||
2248 test_bit(A_BUS_REQ, &motg->inputs))) {
2249 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2250
2251 clear_bit(A_SRP_DET, &motg->inputs);
2252 /* Disable SRP detection */
2253 writel_relaxed((readl_relaxed(USB_OTGSC) &
2254 ~OTGSC_INTSTS_MASK) &
2255 ~OTGSC_DPIE, USB_OTGSC);
2256
2257 otg->state = OTG_STATE_A_WAIT_VRISE;
2258 /* VBUS should not be supplied before end of SRP pulse
2259 * generated by PET, if not complaince test fail.
2260 */
2261 usleep_range(10000, 12000);
2262 /* ACA: ID_A: Stop charging untill enumeration */
2263 if (test_bit(ID_A, &motg->inputs))
2264 msm_otg_notify_charger(motg, 0);
2265 else
2266 msm_hsusb_vbus_power(motg, 1);
2267 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2268 } else {
2269 pr_debug("No session requested\n");
2270 clear_bit(A_BUS_DROP, &motg->inputs);
2271 if (test_bit(ID_A, &motg->inputs)) {
2272 msm_otg_notify_charger(motg,
2273 IDEV_ACA_CHG_MAX);
2274 } else if (!test_bit(ID, &motg->inputs)) {
2275 msm_otg_notify_charger(motg, 0);
2276 /*
2277 * A-device is not providing power on VBUS.
2278 * Enable SRP detection.
2279 */
2280 writel_relaxed(0x13, USB_USBMODE);
2281 writel_relaxed((readl_relaxed(USB_OTGSC) &
2282 ~OTGSC_INTSTS_MASK) |
2283 OTGSC_DPIE, USB_OTGSC);
2284 mb();
2285 }
2286 }
2287 break;
2288 case OTG_STATE_A_WAIT_VRISE:
2289 if ((test_bit(ID, &motg->inputs) &&
2290 !test_bit(ID_A, &motg->inputs)) ||
2291 test_bit(A_BUS_DROP, &motg->inputs) ||
2292 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2293 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2294 clear_bit(A_BUS_REQ, &motg->inputs);
2295 msm_otg_del_timer(motg);
2296 msm_hsusb_vbus_power(motg, 0);
2297 otg->state = OTG_STATE_A_WAIT_VFALL;
2298 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2299 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2300 pr_debug("a_vbus_vld\n");
2301 otg->state = OTG_STATE_A_WAIT_BCON;
2302 if (TA_WAIT_BCON > 0)
2303 msm_otg_start_timer(motg, TA_WAIT_BCON,
2304 A_WAIT_BCON);
2305 msm_otg_start_host(otg, 1);
2306 msm_chg_enable_aca_det(motg);
2307 msm_chg_disable_aca_intr(motg);
2308 mod_timer(&motg->id_timer, ID_TIMER_INITIAL_FREQ);
2309 if (msm_chg_check_aca_intr(motg))
2310 work = 1;
2311 }
2312 break;
2313 case OTG_STATE_A_WAIT_BCON:
2314 if ((test_bit(ID, &motg->inputs) &&
2315 !test_bit(ID_A, &motg->inputs)) ||
2316 test_bit(A_BUS_DROP, &motg->inputs) ||
2317 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2318 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2319 "a_wait_bcon_tmout\n");
2320 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2321 pr_info("Device No Response\n");
2322 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2323 }
2324 msm_otg_del_timer(motg);
2325 clear_bit(A_BUS_REQ, &motg->inputs);
2326 clear_bit(B_CONN, &motg->inputs);
2327 msm_otg_start_host(otg, 0);
2328 /*
2329 * ACA: ID_A with NO accessory, just the A plug is
2330 * attached to ACA: Use IDCHG_MAX for charging
2331 */
2332 if (test_bit(ID_A, &motg->inputs))
2333 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2334 else
2335 msm_hsusb_vbus_power(motg, 0);
2336 otg->state = OTG_STATE_A_WAIT_VFALL;
2337 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2338 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2339 pr_debug("!a_vbus_vld\n");
2340 clear_bit(B_CONN, &motg->inputs);
2341 msm_otg_del_timer(motg);
2342 msm_otg_start_host(otg, 0);
2343 otg->state = OTG_STATE_A_VBUS_ERR;
2344 msm_otg_reset(otg);
2345 } else if (test_bit(ID_A, &motg->inputs)) {
2346 msm_hsusb_vbus_power(motg, 0);
2347 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2348 /*
2349 * If TA_WAIT_BCON is infinite, we don;t
2350 * turn off VBUS. Enter low power mode.
2351 */
2352 if (TA_WAIT_BCON < 0)
2353 pm_runtime_put_sync(otg->dev);
2354 } else if (!test_bit(ID, &motg->inputs)) {
2355 msm_hsusb_vbus_power(motg, 1);
2356 }
2357 break;
2358 case OTG_STATE_A_HOST:
2359 if ((test_bit(ID, &motg->inputs) &&
2360 !test_bit(ID_A, &motg->inputs)) ||
2361 test_bit(A_BUS_DROP, &motg->inputs)) {
2362 pr_debug("id_a/b/c || a_bus_drop\n");
2363 clear_bit(B_CONN, &motg->inputs);
2364 clear_bit(A_BUS_REQ, &motg->inputs);
2365 msm_otg_del_timer(motg);
2366 otg->state = OTG_STATE_A_WAIT_VFALL;
2367 msm_otg_start_host(otg, 0);
2368 if (!test_bit(ID_A, &motg->inputs))
2369 msm_hsusb_vbus_power(motg, 0);
2370 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2371 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2372 pr_debug("!a_vbus_vld\n");
2373 clear_bit(B_CONN, &motg->inputs);
2374 msm_otg_del_timer(motg);
2375 otg->state = OTG_STATE_A_VBUS_ERR;
2376 msm_otg_start_host(otg, 0);
2377 msm_otg_reset(otg);
2378 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2379 /*
2380 * a_bus_req is de-asserted when root hub is
2381 * suspended or HNP is in progress.
2382 */
2383 pr_debug("!a_bus_req\n");
2384 msm_otg_del_timer(motg);
2385 otg->state = OTG_STATE_A_SUSPEND;
2386 if (otg->host->b_hnp_enable)
2387 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2388 A_AIDL_BDIS);
2389 else
2390 pm_runtime_put_sync(otg->dev);
2391 } else if (!test_bit(B_CONN, &motg->inputs)) {
2392 pr_debug("!b_conn\n");
2393 msm_otg_del_timer(motg);
2394 otg->state = OTG_STATE_A_WAIT_BCON;
2395 if (TA_WAIT_BCON > 0)
2396 msm_otg_start_timer(motg, TA_WAIT_BCON,
2397 A_WAIT_BCON);
2398 if (msm_chg_check_aca_intr(motg))
2399 work = 1;
2400 } else if (test_bit(ID_A, &motg->inputs)) {
2401 msm_otg_del_timer(motg);
2402 msm_hsusb_vbus_power(motg, 0);
2403 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2404 msm_otg_notify_charger(motg,
2405 IDEV_ACA_CHG_MAX);
2406 else
2407 msm_otg_notify_charger(motg,
2408 IDEV_CHG_MIN - motg->mA_port);
2409 } else if (!test_bit(ID, &motg->inputs)) {
2410 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2411 motg->chg_type = USB_INVALID_CHARGER;
2412 msm_otg_notify_charger(motg, 0);
2413 msm_hsusb_vbus_power(motg, 1);
2414 }
2415 break;
2416 case OTG_STATE_A_SUSPEND:
2417 if ((test_bit(ID, &motg->inputs) &&
2418 !test_bit(ID_A, &motg->inputs)) ||
2419 test_bit(A_BUS_DROP, &motg->inputs) ||
2420 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2421 pr_debug("id_a/b/c || a_bus_drop ||"
2422 "a_aidl_bdis_tmout\n");
2423 msm_otg_del_timer(motg);
2424 clear_bit(B_CONN, &motg->inputs);
2425 otg->state = OTG_STATE_A_WAIT_VFALL;
2426 msm_otg_start_host(otg, 0);
2427 msm_otg_reset(otg);
2428 if (!test_bit(ID_A, &motg->inputs))
2429 msm_hsusb_vbus_power(motg, 0);
2430 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2431 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2432 pr_debug("!a_vbus_vld\n");
2433 msm_otg_del_timer(motg);
2434 clear_bit(B_CONN, &motg->inputs);
2435 otg->state = OTG_STATE_A_VBUS_ERR;
2436 msm_otg_start_host(otg, 0);
2437 msm_otg_reset(otg);
2438 } else if (!test_bit(B_CONN, &motg->inputs) &&
2439 otg->host->b_hnp_enable) {
2440 pr_debug("!b_conn && b_hnp_enable");
2441 otg->state = OTG_STATE_A_PERIPHERAL;
2442 msm_otg_host_hnp_enable(otg, 1);
2443 otg->gadget->is_a_peripheral = 1;
2444 msm_otg_start_peripheral(otg, 1);
2445 } else if (!test_bit(B_CONN, &motg->inputs) &&
2446 !otg->host->b_hnp_enable) {
2447 pr_debug("!b_conn && !b_hnp_enable");
2448 /*
2449 * bus request is dropped during suspend.
2450 * acquire again for next device.
2451 */
2452 set_bit(A_BUS_REQ, &motg->inputs);
2453 otg->state = OTG_STATE_A_WAIT_BCON;
2454 if (TA_WAIT_BCON > 0)
2455 msm_otg_start_timer(motg, TA_WAIT_BCON,
2456 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302458 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002459 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302460 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002461 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302463 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302464 }
2465 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 case OTG_STATE_A_PERIPHERAL:
2467 if ((test_bit(ID, &motg->inputs) &&
2468 !test_bit(ID_A, &motg->inputs)) ||
2469 test_bit(A_BUS_DROP, &motg->inputs)) {
2470 pr_debug("id _f/b/c || a_bus_drop\n");
2471 /* Clear BIDL_ADIS timer */
2472 msm_otg_del_timer(motg);
2473 otg->state = OTG_STATE_A_WAIT_VFALL;
2474 msm_otg_start_peripheral(otg, 0);
2475 otg->gadget->is_a_peripheral = 0;
2476 msm_otg_start_host(otg, 0);
2477 msm_otg_reset(otg);
2478 if (!test_bit(ID_A, &motg->inputs))
2479 msm_hsusb_vbus_power(motg, 0);
2480 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2481 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2482 pr_debug("!a_vbus_vld\n");
2483 /* Clear BIDL_ADIS timer */
2484 msm_otg_del_timer(motg);
2485 otg->state = OTG_STATE_A_VBUS_ERR;
2486 msm_otg_start_peripheral(otg, 0);
2487 otg->gadget->is_a_peripheral = 0;
2488 msm_otg_start_host(otg, 0);
2489 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2490 pr_debug("a_bidl_adis_tmout\n");
2491 msm_otg_start_peripheral(otg, 0);
2492 otg->gadget->is_a_peripheral = 0;
2493 otg->state = OTG_STATE_A_WAIT_BCON;
2494 set_bit(A_BUS_REQ, &motg->inputs);
2495 msm_otg_host_hnp_enable(otg, 0);
2496 if (TA_WAIT_BCON > 0)
2497 msm_otg_start_timer(motg, TA_WAIT_BCON,
2498 A_WAIT_BCON);
2499 } else if (test_bit(ID_A, &motg->inputs)) {
2500 msm_hsusb_vbus_power(motg, 0);
2501 msm_otg_notify_charger(motg,
2502 IDEV_CHG_MIN - motg->mA_port);
2503 } else if (!test_bit(ID, &motg->inputs)) {
2504 msm_otg_notify_charger(motg, 0);
2505 msm_hsusb_vbus_power(motg, 1);
2506 }
2507 break;
2508 case OTG_STATE_A_WAIT_VFALL:
2509 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2510 clear_bit(A_VBUS_VLD, &motg->inputs);
2511 otg->state = OTG_STATE_A_IDLE;
2512 work = 1;
2513 }
2514 break;
2515 case OTG_STATE_A_VBUS_ERR:
2516 if ((test_bit(ID, &motg->inputs) &&
2517 !test_bit(ID_A, &motg->inputs)) ||
2518 test_bit(A_BUS_DROP, &motg->inputs) ||
2519 test_bit(A_CLR_ERR, &motg->inputs)) {
2520 otg->state = OTG_STATE_A_WAIT_VFALL;
2521 if (!test_bit(ID_A, &motg->inputs))
2522 msm_hsusb_vbus_power(motg, 0);
2523 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2524 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2525 motg->chg_type = USB_INVALID_CHARGER;
2526 msm_otg_notify_charger(motg, 0);
2527 }
2528 break;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302529 default:
2530 break;
2531 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302532 if (work)
2533 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302534}
2535
2536static irqreturn_t msm_otg_irq(int irq, void *data)
2537{
2538 struct msm_otg *motg = data;
2539 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 u32 otgsc = 0, usbsts, pc;
2541 bool work = 0;
2542 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302543
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302544 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302545 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302546 disable_irq_nosync(irq);
2547 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302548 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302549 return IRQ_HANDLED;
2550 }
2551
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002552 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302553 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302554
2555 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302556 return IRQ_NONE;
2557
2558 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302559 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302560 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302561 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302562 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302563 pr_debug("Id clear\n");
2564 /*
2565 * Assert a_bus_req to supply power on
2566 * VBUS when Micro/Mini-A cable is connected
2567 * with out user intervention.
2568 */
2569 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302570 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302571 msm_chg_enable_aca_det(motg);
2572 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 writel_relaxed(otgsc, USB_OTGSC);
2574 work = 1;
2575 } else if (otgsc & OTGSC_DPIS) {
2576 pr_debug("DPIS detected\n");
2577 writel_relaxed(otgsc, USB_OTGSC);
2578 set_bit(A_SRP_DET, &motg->inputs);
2579 set_bit(A_BUS_REQ, &motg->inputs);
2580 work = 1;
2581 } else if (otgsc & OTGSC_BSVIS) {
2582 writel_relaxed(otgsc, USB_OTGSC);
2583 /*
2584 * BSV interrupt comes when operating as an A-device
2585 * (VBUS on/off).
2586 * But, handle BSV when charger is removed from ACA in ID_A
2587 */
2588 if ((otg->state >= OTG_STATE_A_IDLE) &&
2589 !test_bit(ID_A, &motg->inputs))
2590 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302591 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302592 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302593 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302594 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302595 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302596 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302597 msm_chg_check_aca_intr(motg);
2598 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302599 work = 1;
2600 } else if (usbsts & STS_PCI) {
2601 pc = readl_relaxed(USB_PORTSC);
2602 pr_debug("portsc = %x\n", pc);
2603 ret = IRQ_NONE;
2604 /*
2605 * HCD Acks PCI interrupt. We use this to switch
2606 * between different OTG states.
2607 */
2608 work = 1;
2609 switch (otg->state) {
2610 case OTG_STATE_A_SUSPEND:
2611 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2612 !(pc & PORTSC_CCS)) {
2613 pr_debug("B_CONN clear\n");
2614 clear_bit(B_CONN, &motg->inputs);
2615 msm_otg_del_timer(motg);
2616 }
2617 break;
2618 case OTG_STATE_A_PERIPHERAL:
2619 /*
2620 * A-peripheral observed activity on bus.
2621 * clear A_BIDL_ADIS timer.
2622 */
2623 msm_otg_del_timer(motg);
2624 work = 0;
2625 break;
2626 case OTG_STATE_B_WAIT_ACON:
2627 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2628 pr_debug("A_CONN set\n");
2629 set_bit(A_CONN, &motg->inputs);
2630 /* Clear ASE0_BRST timer */
2631 msm_otg_del_timer(motg);
2632 }
2633 break;
2634 case OTG_STATE_B_HOST:
2635 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2636 pr_debug("A_CONN clear\n");
2637 clear_bit(A_CONN, &motg->inputs);
2638 msm_otg_del_timer(motg);
2639 }
2640 break;
2641 case OTG_STATE_A_WAIT_BCON:
2642 if (TA_WAIT_BCON < 0)
2643 set_bit(A_BUS_REQ, &motg->inputs);
2644 default:
2645 work = 0;
2646 break;
2647 }
2648 } else if (usbsts & STS_URI) {
2649 ret = IRQ_NONE;
2650 switch (otg->state) {
2651 case OTG_STATE_A_PERIPHERAL:
2652 /*
2653 * A-peripheral observed activity on bus.
2654 * clear A_BIDL_ADIS timer.
2655 */
2656 msm_otg_del_timer(motg);
2657 work = 0;
2658 break;
2659 default:
2660 work = 0;
2661 break;
2662 }
2663 } else if (usbsts & STS_SLI) {
2664 ret = IRQ_NONE;
2665 work = 0;
2666 switch (otg->state) {
2667 case OTG_STATE_B_PERIPHERAL:
2668 if (otg->gadget->b_hnp_enable) {
2669 set_bit(A_BUS_SUSPEND, &motg->inputs);
2670 set_bit(B_BUS_REQ, &motg->inputs);
2671 work = 1;
2672 }
2673 break;
2674 case OTG_STATE_A_PERIPHERAL:
2675 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2676 A_BIDL_ADIS);
2677 break;
2678 default:
2679 break;
2680 }
2681 } else if ((usbsts & PHY_ALT_INT)) {
2682 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2683 if (msm_chg_check_aca_intr(motg))
2684 work = 1;
2685 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302686 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302687 if (work)
2688 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302689
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302690 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002691}
2692
2693static void msm_otg_set_vbus_state(int online)
2694{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302695 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696 struct msm_otg *motg = the_msm_otg;
2697
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302698 if (online) {
2699 pr_debug("PMIC: BSV set\n");
2700 set_bit(B_SESS_VLD, &motg->inputs);
2701 } else {
2702 pr_debug("PMIC: BSV clear\n");
2703 clear_bit(B_SESS_VLD, &motg->inputs);
2704 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002705
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302706 if (!init) {
2707 init = true;
2708 complete(&pmic_vbus_init);
2709 pr_debug("PMIC: BSV init complete\n");
2710 return;
2711 }
2712
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302713 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002714}
2715
2716static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2717{
2718 struct msm_otg *motg = data;
2719
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302720 if (aca_id_turned_on)
2721 return IRQ_HANDLED;
2722
2723 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2724 pr_debug("PMIC: ID set\n");
2725 set_bit(ID, &motg->inputs);
2726 } else {
2727 pr_debug("PMIC: ID clear\n");
2728 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302729 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302730 }
2731
2732 if (motg->otg.state != OTG_STATE_UNDEFINED)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302733 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002734
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302735 return IRQ_HANDLED;
2736}
2737
2738static int msm_otg_mode_show(struct seq_file *s, void *unused)
2739{
2740 struct msm_otg *motg = s->private;
2741 struct otg_transceiver *otg = &motg->otg;
2742
2743 switch (otg->state) {
2744 case OTG_STATE_A_HOST:
2745 seq_printf(s, "host\n");
2746 break;
2747 case OTG_STATE_B_PERIPHERAL:
2748 seq_printf(s, "peripheral\n");
2749 break;
2750 default:
2751 seq_printf(s, "none\n");
2752 break;
2753 }
2754
2755 return 0;
2756}
2757
2758static int msm_otg_mode_open(struct inode *inode, struct file *file)
2759{
2760 return single_open(file, msm_otg_mode_show, inode->i_private);
2761}
2762
2763static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2764 size_t count, loff_t *ppos)
2765{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302766 struct seq_file *s = file->private_data;
2767 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302768 char buf[16];
2769 struct otg_transceiver *otg = &motg->otg;
2770 int status = count;
2771 enum usb_mode_type req_mode;
2772
2773 memset(buf, 0x00, sizeof(buf));
2774
2775 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2776 status = -EFAULT;
2777 goto out;
2778 }
2779
2780 if (!strncmp(buf, "host", 4)) {
2781 req_mode = USB_HOST;
2782 } else if (!strncmp(buf, "peripheral", 10)) {
2783 req_mode = USB_PERIPHERAL;
2784 } else if (!strncmp(buf, "none", 4)) {
2785 req_mode = USB_NONE;
2786 } else {
2787 status = -EINVAL;
2788 goto out;
2789 }
2790
2791 switch (req_mode) {
2792 case USB_NONE:
2793 switch (otg->state) {
2794 case OTG_STATE_A_HOST:
2795 case OTG_STATE_B_PERIPHERAL:
2796 set_bit(ID, &motg->inputs);
2797 clear_bit(B_SESS_VLD, &motg->inputs);
2798 break;
2799 default:
2800 goto out;
2801 }
2802 break;
2803 case USB_PERIPHERAL:
2804 switch (otg->state) {
2805 case OTG_STATE_B_IDLE:
2806 case OTG_STATE_A_HOST:
2807 set_bit(ID, &motg->inputs);
2808 set_bit(B_SESS_VLD, &motg->inputs);
2809 break;
2810 default:
2811 goto out;
2812 }
2813 break;
2814 case USB_HOST:
2815 switch (otg->state) {
2816 case OTG_STATE_B_IDLE:
2817 case OTG_STATE_B_PERIPHERAL:
2818 clear_bit(ID, &motg->inputs);
2819 break;
2820 default:
2821 goto out;
2822 }
2823 break;
2824 default:
2825 goto out;
2826 }
2827
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302828 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302829 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302830out:
2831 return status;
2832}
2833
2834const struct file_operations msm_otg_mode_fops = {
2835 .open = msm_otg_mode_open,
2836 .read = seq_read,
2837 .write = msm_otg_mode_write,
2838 .llseek = seq_lseek,
2839 .release = single_release,
2840};
2841
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302842static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2843{
2844 struct msm_otg *motg = s->private;
2845 struct otg_transceiver *otg = &motg->otg;
2846
2847 seq_printf(s, "%s\n", otg_state_string(otg->state));
2848 return 0;
2849}
2850
2851static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2852{
2853 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2854}
2855
2856const struct file_operations msm_otg_state_fops = {
2857 .open = msm_otg_otg_state_open,
2858 .read = seq_read,
2859 .llseek = seq_lseek,
2860 .release = single_release,
2861};
2862
Anji jonnalad270e2d2011-08-09 11:28:32 +05302863static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2864{
2865 struct msm_otg *motg = s->private;
2866
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302867 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302868 return 0;
2869}
2870
2871static int msm_otg_chg_open(struct inode *inode, struct file *file)
2872{
2873 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2874}
2875
2876const struct file_operations msm_otg_chg_fops = {
2877 .open = msm_otg_chg_open,
2878 .read = seq_read,
2879 .llseek = seq_lseek,
2880 .release = single_release,
2881};
2882
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302883static int msm_otg_aca_show(struct seq_file *s, void *unused)
2884{
2885 if (debug_aca_enabled)
2886 seq_printf(s, "enabled\n");
2887 else
2888 seq_printf(s, "disabled\n");
2889
2890 return 0;
2891}
2892
2893static int msm_otg_aca_open(struct inode *inode, struct file *file)
2894{
2895 return single_open(file, msm_otg_aca_show, inode->i_private);
2896}
2897
2898static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2899 size_t count, loff_t *ppos)
2900{
2901 char buf[8];
2902
2903 memset(buf, 0x00, sizeof(buf));
2904
2905 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2906 return -EFAULT;
2907
2908 if (!strncmp(buf, "enable", 6))
2909 debug_aca_enabled = true;
2910 else
2911 debug_aca_enabled = false;
2912
2913 return count;
2914}
2915
2916const struct file_operations msm_otg_aca_fops = {
2917 .open = msm_otg_aca_open,
2918 .read = seq_read,
2919 .write = msm_otg_aca_write,
2920 .llseek = seq_lseek,
2921 .release = single_release,
2922};
2923
Manu Gautam8bdcc592012-03-06 11:26:06 +05302924static int msm_otg_bus_show(struct seq_file *s, void *unused)
2925{
2926 if (debug_bus_voting_enabled)
2927 seq_printf(s, "enabled\n");
2928 else
2929 seq_printf(s, "disabled\n");
2930
2931 return 0;
2932}
2933
2934static int msm_otg_bus_open(struct inode *inode, struct file *file)
2935{
2936 return single_open(file, msm_otg_bus_show, inode->i_private);
2937}
2938
2939static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2940 size_t count, loff_t *ppos)
2941{
2942 char buf[8];
2943 int ret;
2944 struct seq_file *s = file->private_data;
2945 struct msm_otg *motg = s->private;
2946
2947 memset(buf, 0x00, sizeof(buf));
2948
2949 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2950 return -EFAULT;
2951
2952 if (!strncmp(buf, "enable", 6)) {
2953 /* Do not vote here. Let OTG statemachine decide when to vote */
2954 debug_bus_voting_enabled = true;
2955 } else {
2956 debug_bus_voting_enabled = false;
2957 if (motg->bus_perf_client) {
2958 ret = msm_bus_scale_client_update_request(
2959 motg->bus_perf_client, 0);
2960 if (ret)
2961 dev_err(motg->otg.dev, "%s: Failed to devote "
2962 "for bus bw %d\n", __func__, ret);
2963 }
2964 }
2965
2966 return count;
2967}
2968
2969const struct file_operations msm_otg_bus_fops = {
2970 .open = msm_otg_bus_open,
2971 .read = seq_read,
2972 .write = msm_otg_bus_write,
2973 .llseek = seq_lseek,
2974 .release = single_release,
2975};
2976
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302977static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302978
2979static int msm_otg_debugfs_init(struct msm_otg *motg)
2980{
Manu Gautam8bdcc592012-03-06 11:26:06 +05302981 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302982
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302983 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2984
2985 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2986 return -ENODEV;
2987
Anji jonnalad270e2d2011-08-09 11:28:32 +05302988 if (motg->pdata->mode == USB_OTG &&
2989 motg->pdata->otg_control == OTG_USER_CONTROL) {
2990
Manu Gautam8bdcc592012-03-06 11:26:06 +05302991 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05302992 S_IWUSR, msm_otg_dbg_root, motg,
2993 &msm_otg_mode_fops);
2994
Manu Gautam8bdcc592012-03-06 11:26:06 +05302995 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05302996 debugfs_remove(msm_otg_dbg_root);
2997 msm_otg_dbg_root = NULL;
2998 return -ENODEV;
2999 }
3000 }
3001
Manu Gautam8bdcc592012-03-06 11:26:06 +05303002 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303003 msm_otg_dbg_root, motg,
3004 &msm_otg_chg_fops);
3005
Manu Gautam8bdcc592012-03-06 11:26:06 +05303006 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303007 debugfs_remove_recursive(msm_otg_dbg_root);
3008 return -ENODEV;
3009 }
3010
Manu Gautam8bdcc592012-03-06 11:26:06 +05303011 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303012 msm_otg_dbg_root, motg,
3013 &msm_otg_aca_fops);
3014
Manu Gautam8bdcc592012-03-06 11:26:06 +05303015 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303016 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303017 return -ENODEV;
3018 }
3019
Manu Gautam8bdcc592012-03-06 11:26:06 +05303020 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3021 msm_otg_dbg_root, motg,
3022 &msm_otg_bus_fops);
3023
3024 if (!msm_otg_dentry) {
3025 debugfs_remove_recursive(msm_otg_dbg_root);
3026 return -ENODEV;
3027 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303028
3029 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3030 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3031
3032 if (!msm_otg_dentry) {
3033 debugfs_remove_recursive(msm_otg_dbg_root);
3034 return -ENODEV;
3035 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303036 return 0;
3037}
3038
3039static void msm_otg_debugfs_cleanup(void)
3040{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303041 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303042}
3043
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303044static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3045static struct platform_device *msm_otg_add_pdev(
3046 struct platform_device *ofdev, const char *name)
3047{
3048 struct platform_device *pdev;
3049 const struct resource *res = ofdev->resource;
3050 unsigned int num = ofdev->num_resources;
3051 int retval;
3052
3053 pdev = platform_device_alloc(name, -1);
3054 if (!pdev) {
3055 retval = -ENOMEM;
3056 goto error;
3057 }
3058
3059 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3060 pdev->dev.dma_mask = &msm_otg_dma_mask;
3061
3062 if (num) {
3063 retval = platform_device_add_resources(pdev, res, num);
3064 if (retval)
3065 goto error;
3066 }
3067
3068 retval = platform_device_add(pdev);
3069 if (retval)
3070 goto error;
3071
3072 return pdev;
3073
3074error:
3075 platform_device_put(pdev);
3076 return ERR_PTR(retval);
3077}
3078
3079static int msm_otg_setup_devices(struct platform_device *ofdev,
3080 enum usb_mode_type mode, bool init)
3081{
3082 const char *gadget_name = "msm_hsusb";
3083 const char *host_name = "msm_hsusb_host";
3084 static struct platform_device *gadget_pdev;
3085 static struct platform_device *host_pdev;
3086 int retval = 0;
3087
3088 if (!init) {
3089 if (gadget_pdev)
3090 platform_device_unregister(gadget_pdev);
3091 if (host_pdev)
3092 platform_device_unregister(host_pdev);
3093 return 0;
3094 }
3095
3096 switch (mode) {
3097 case USB_OTG:
3098 /* fall through */
3099 case USB_PERIPHERAL:
3100 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3101 if (IS_ERR(gadget_pdev)) {
3102 retval = PTR_ERR(gadget_pdev);
3103 break;
3104 }
3105 if (mode == USB_PERIPHERAL)
3106 break;
3107 /* fall through */
3108 case USB_HOST:
3109 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3110 if (IS_ERR(host_pdev)) {
3111 retval = PTR_ERR(host_pdev);
3112 if (mode == USB_OTG)
3113 platform_device_unregister(gadget_pdev);
3114 }
3115 break;
3116 default:
3117 break;
3118 }
3119
3120 return retval;
3121}
3122
3123struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3124{
3125 struct device_node *node = pdev->dev.of_node;
3126 struct msm_otg_platform_data *pdata;
3127 int len = 0;
3128
3129 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3130 if (!pdata) {
3131 pr_err("unable to allocate platform data\n");
3132 return NULL;
3133 }
3134 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3135 if (len) {
3136 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3137 if (!pdata->phy_init_seq)
3138 return NULL;
3139 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3140 pdata->phy_init_seq,
3141 len/sizeof(*pdata->phy_init_seq));
3142 }
3143 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3144 &pdata->power_budget);
3145 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3146 &pdata->mode);
3147 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3148 &pdata->otg_control);
3149 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3150 &pdata->default_mode);
3151 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3152 &pdata->phy_type);
3153 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3154 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303155 return pdata;
3156}
3157
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303158static int __init msm_otg_probe(struct platform_device *pdev)
3159{
3160 int ret = 0;
3161 struct resource *res;
3162 struct msm_otg *motg;
3163 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303164 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303165
3166 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303167
3168 if (pdev->dev.of_node) {
3169 dev_dbg(&pdev->dev, "device tree enabled\n");
3170 pdata = msm_otg_dt_to_pdata(pdev);
3171 if (!pdata)
3172 return -ENOMEM;
3173 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3174 if (ret) {
3175 dev_err(&pdev->dev, "devices setup failed\n");
3176 return ret;
3177 }
3178 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303179 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3180 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303181 } else {
3182 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303183 }
3184
3185 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3186 if (!motg) {
3187 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3188 return -ENOMEM;
3189 }
3190
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003191 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303192 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303193 otg = &motg->otg;
3194 otg->dev = &pdev->dev;
3195
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303196 /*
3197 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3198 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3199 * PMIC can detect ACA ID_GND and generate an interrupt.
3200 */
3201 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3202 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3203 ret = -EINVAL;
3204 goto free_motg;
3205 }
3206
Ofir Cohen4da266f2012-01-03 10:19:29 +02003207 /* initialize reset counter */
3208 motg->reset_counter = 0;
3209
Amit Blay02eff132011-09-21 16:46:24 +03003210 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003211 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003212 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003213 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303214
Manu Gautam5143b252012-01-05 19:25:23 -08003215 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303216 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08003217 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303218 ret = PTR_ERR(motg->clk);
3219 goto put_phy_reset_clk;
3220 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05303221 clk_set_rate(motg->clk, 60000000);
3222
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303223 /* pm qos request to prevent apps idle power collapse */
3224 if (motg->pdata->swfi_latency)
3225 pm_qos_add_request(&motg->pm_qos_req_dma,
3226 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08003227
Anji jonnala0f73cac2011-05-04 10:19:46 +05303228 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003229 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303230 * CORE CLK must be running at >55Mhz for correct HSUSB
3231 * operation and USB core cannot tolerate frequency changes on
3232 * CORE CLK. For such USB cores, vote for maximum clk frequency
3233 * on pclk source
3234 */
Manu Gautam5143b252012-01-05 19:25:23 -08003235 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3236 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303237 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003238 dev_err(&pdev->dev, "failed to get core_clk\n");
3239 ret = PTR_ERR(motg->clk);
3240 goto put_clk;
3241 }
3242 clk_set_rate(motg->core_clk, INT_MAX);
3243
3244 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3245 if (IS_ERR(motg->pclk)) {
3246 dev_err(&pdev->dev, "failed to get iface_clk\n");
3247 ret = PTR_ERR(motg->pclk);
3248 goto put_core_clk;
3249 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303250
3251 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3252 if (!res) {
3253 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3254 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003255 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303256 }
3257
3258 motg->regs = ioremap(res->start, resource_size(res));
3259 if (!motg->regs) {
3260 dev_err(&pdev->dev, "ioremap failed\n");
3261 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003262 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303263 }
3264 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3265
3266 motg->irq = platform_get_irq(pdev, 0);
3267 if (!motg->irq) {
3268 dev_err(&pdev->dev, "platform_get_irq failed\n");
3269 ret = -ENODEV;
3270 goto free_regs;
3271 }
3272
Stephen Boyd7dd22662012-01-26 16:09:31 -08003273 motg->xo_handle = clk_get(&pdev->dev, "xo");
Anji jonnala7da3f262011-12-02 17:22:14 -08003274 if (IS_ERR(motg->xo_handle)) {
3275 dev_err(&pdev->dev, "%s not able to get the handle "
3276 "to vote for TCXO D0 buffer\n", __func__);
3277 ret = PTR_ERR(motg->xo_handle);
3278 goto free_regs;
3279 }
3280
Stephen Boyd7dd22662012-01-26 16:09:31 -08003281 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08003282 if (ret) {
3283 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3284 "D0 buffer%d\n", __func__, ret);
3285 goto free_xo_handle;
3286 }
3287
Manu Gautam28b1bac2012-01-30 16:43:06 +05303288 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303289
3290 ret = msm_hsusb_init_vddcx(motg, 1);
3291 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003292 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08003293 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303294 }
3295
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003296 ret = msm_hsusb_config_vddcx(1);
3297 if (ret) {
3298 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
3299 goto free_init_vddcx;
3300 }
3301
Anji jonnala11aa5c42011-05-04 10:19:48 +05303302 ret = msm_hsusb_ldo_init(motg, 1);
3303 if (ret) {
3304 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003305 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303306 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003307
3308 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303309 if (ret) {
3310 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003311 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303312 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303313 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303314
3315 writel(0, USB_USBINTR);
3316 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003317 /* Ensure that above STOREs are completed before enabling interrupts */
3318 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003320 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303321 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303322 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303323 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303324 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3325 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303326 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3327 "msm_otg", motg);
3328 if (ret) {
3329 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003330 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303331 }
3332
3333 otg->init = msm_otg_reset;
3334 otg->set_host = msm_otg_set_host;
3335 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303336 otg->set_power = msm_otg_set_power;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303337 otg->start_hnp = msm_otg_start_hnp;
3338 otg->start_srp = msm_otg_start_srp;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303339 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303340
3341 otg->io_ops = &msm_otg_io_ops;
3342
3343 ret = otg_set_transceiver(&motg->otg);
3344 if (ret) {
3345 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
3346 goto free_irq;
3347 }
3348
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303349 if (motg->pdata->mode == USB_OTG &&
3350 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003351 if (motg->pdata->pmic_id_irq) {
3352 ret = request_irq(motg->pdata->pmic_id_irq,
3353 msm_pmic_id_irq,
3354 IRQF_TRIGGER_RISING |
3355 IRQF_TRIGGER_FALLING,
3356 "msm_otg", motg);
3357 if (ret) {
3358 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
3359 goto remove_otg;
3360 }
3361 } else {
3362 ret = -ENODEV;
3363 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
3364 goto remove_otg;
3365 }
3366 }
3367
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303368 msm_hsusb_mhl_switch_enable(motg, 1);
3369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303370 platform_set_drvdata(pdev, motg);
3371 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003372 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303373
Anji jonnalad270e2d2011-08-09 11:28:32 +05303374 ret = msm_otg_debugfs_init(motg);
3375 if (ret)
3376 dev_dbg(&pdev->dev, "mode debugfs file is"
3377 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303378
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003379 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3380 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3381
Amit Blay58b31472011-11-18 09:39:39 +02003382 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3383 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303384 (!(motg->pdata->mode == USB_OTG) ||
3385 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003386 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003387 ALLOW_PHY_RETENTION |
3388 ALLOW_PHY_COMP_DISABLE;
3389
Amit Blay58b31472011-11-18 09:39:39 +02003390 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3391 motg->caps = ALLOW_PHY_RETENTION;
3392 }
3393
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003394 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303395 pm_runtime_set_active(&pdev->dev);
3396 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303397
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303398 if (motg->pdata->bus_scale_table) {
3399 motg->bus_perf_client =
3400 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3401 if (!motg->bus_perf_client)
3402 dev_err(motg->otg.dev, "%s: Failed to register BUS "
3403 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303404 else
3405 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303406 }
3407
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303408 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003409
3410remove_otg:
3411 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303412free_irq:
3413 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003414destroy_wlock:
3415 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303416 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003417 msm_hsusb_ldo_enable(motg, 0);
3418free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303419 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003420free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303421 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08003422devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303423 clk_disable_unprepare(motg->pclk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08003424 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08003425free_xo_handle:
Stephen Boyd7dd22662012-01-26 16:09:31 -08003426 clk_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303427free_regs:
3428 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003429put_pclk:
3430 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303431put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003432 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303433put_clk:
3434 clk_put(motg->clk);
3435put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03003436 if (!IS_ERR(motg->phy_reset_clk))
3437 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303438free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303439 if (motg->pdata->swfi_latency)
3440 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303441 kfree(motg);
3442 return ret;
3443}
3444
3445static int __devexit msm_otg_remove(struct platform_device *pdev)
3446{
3447 struct msm_otg *motg = platform_get_drvdata(pdev);
3448 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303449 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303450
3451 if (otg->host || otg->gadget)
3452 return -EBUSY;
3453
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303454 if (pdev->dev.of_node)
3455 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003456 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3457 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303458 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303459 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303460 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303461
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303462 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303463
3464 device_init_wakeup(&pdev->dev, 0);
3465 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003466 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303467
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303468 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003469 if (motg->pdata->pmic_id_irq)
3470 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303471 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303472 free_irq(motg->irq, motg);
3473
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303474 /*
3475 * Put PHY in low power mode.
3476 */
3477 ulpi_read(otg, 0x14);
3478 ulpi_write(otg, 0x08, 0x09);
3479
3480 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3481 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3482 if (readl(USB_PORTSC) & PORTSC_PHCD)
3483 break;
3484 udelay(1);
3485 cnt++;
3486 }
3487 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
3488 dev_err(otg->dev, "Unable to suspend PHY\n");
3489
Manu Gautam28b1bac2012-01-30 16:43:06 +05303490 clk_disable_unprepare(motg->pclk);
3491 clk_disable_unprepare(motg->core_clk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08003492 clk_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003493 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303494 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003495 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303496
3497 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303498 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303499
Amit Blay02eff132011-09-21 16:46:24 +03003500 if (!IS_ERR(motg->phy_reset_clk))
3501 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303502 clk_put(motg->pclk);
3503 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003504 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303505
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303506 if (motg->pdata->swfi_latency)
3507 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303508
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303509 if (motg->bus_perf_client)
3510 msm_bus_scale_unregister_client(motg->bus_perf_client);
3511
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303512 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303513 return 0;
3514}
3515
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303516#ifdef CONFIG_PM_RUNTIME
3517static int msm_otg_runtime_idle(struct device *dev)
3518{
3519 struct msm_otg *motg = dev_get_drvdata(dev);
3520 struct otg_transceiver *otg = &motg->otg;
3521
3522 dev_dbg(dev, "OTG runtime idle\n");
3523
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303524 if (otg->state == OTG_STATE_UNDEFINED)
3525 return -EAGAIN;
3526 else
3527 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303528}
3529
3530static int msm_otg_runtime_suspend(struct device *dev)
3531{
3532 struct msm_otg *motg = dev_get_drvdata(dev);
3533
3534 dev_dbg(dev, "OTG runtime suspend\n");
3535 return msm_otg_suspend(motg);
3536}
3537
3538static int msm_otg_runtime_resume(struct device *dev)
3539{
3540 struct msm_otg *motg = dev_get_drvdata(dev);
3541
3542 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303543 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303544 return msm_otg_resume(motg);
3545}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303546#endif
3547
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303548#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303549static int msm_otg_pm_suspend(struct device *dev)
3550{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303551 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303552
3553 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303554
3555#ifdef CONFIG_PM_RUNTIME
3556 ret = pm_runtime_suspend(dev);
3557 if (ret > 0)
3558 ret = 0;
3559#else
3560 ret = msm_otg_suspend(dev_get_drvdata(dev));
3561#endif
3562 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303563}
3564
3565static int msm_otg_pm_resume(struct device *dev)
3566{
3567 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303568
3569 dev_dbg(dev, "OTG PM resume\n");
3570
Manu Gautamf284c052011-09-08 16:52:48 +05303571#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303572 /*
Manu Gautamf284c052011-09-08 16:52:48 +05303573 * Do not resume hardware as part of system resume,
3574 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303575 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07003576 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05303577#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303578
Manu Gautamf284c052011-09-08 16:52:48 +05303579 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303580}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303581#endif
3582
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303583#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303584static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303585 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3586 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3587 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303588};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303589#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303590
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303591static struct of_device_id msm_otg_dt_match[] = {
3592 { .compatible = "qcom,hsusb-otg",
3593 },
3594 {}
3595};
3596
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303597static struct platform_driver msm_otg_driver = {
3598 .remove = __devexit_p(msm_otg_remove),
3599 .driver = {
3600 .name = DRIVER_NAME,
3601 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303602#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303603 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303604#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303605 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303606 },
3607};
3608
3609static int __init msm_otg_init(void)
3610{
3611 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3612}
3613
3614static void __exit msm_otg_exit(void)
3615{
3616 platform_driver_unregister(&msm_otg_driver);
3617}
3618
3619module_init(msm_otg_init);
3620module_exit(msm_otg_exit);
3621
3622MODULE_LICENSE("GPL v2");
3623MODULE_DESCRIPTION("MSM USB transceiver driver");