blob: ec9ac244bfe11d5776407a15f1e5b20d8bc5c7e3 [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020041#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053042
43#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080044#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
46#define MSM_USB_BASE (motg->regs)
47#define DRIVER_NAME "msm_otg"
48
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053049#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053051
52#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
53#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
54#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
55#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
56
57#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
58#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
59#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
60#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
61
Vamsi Krishna132b2762011-11-11 16:09:20 -080062#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053063#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
64
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053065static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053067static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053069/* Prevent idle power collapse(pc) while operating in peripheral mode */
70static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
71{
72 struct msm_otg_platform_data *pdata = dev->pdata;
73 u32 swfi_latency = 0;
74
75 if (!pdata || !pdata->swfi_latency)
76 return;
77
78 swfi_latency = pdata->swfi_latency + 1;
79
80 if (vote)
81 pm_qos_update_request(&dev->pm_qos_req_dma,
82 swfi_latency);
83 else
84 pm_qos_update_request(&dev->pm_qos_req_dma,
85 PM_QOS_DEFAULT_VALUE);
86}
87
Anji jonnala11aa5c42011-05-04 10:19:48 +053088static struct regulator *hsusb_3p3;
89static struct regulator *hsusb_1p8;
90static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053091static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053092
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053093static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053094static inline bool aca_enabled(void)
95{
96#ifdef CONFIG_USB_MSM_ACA
97 return true;
98#else
99 return debug_aca_enabled;
100#endif
101}
102
Anji jonnala11aa5c42011-05-04 10:19:48 +0530103static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
104{
105 int ret = 0;
106
107 if (init) {
108 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
109 if (IS_ERR(hsusb_vddcx)) {
110 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
111 return PTR_ERR(hsusb_vddcx);
112 }
113
114 ret = regulator_set_voltage(hsusb_vddcx,
115 USB_PHY_VDD_DIG_VOL_MIN,
116 USB_PHY_VDD_DIG_VOL_MAX);
117 if (ret) {
118 dev_err(motg->otg.dev, "unable to set the voltage "
119 "for hsusb vddcx\n");
120 regulator_put(hsusb_vddcx);
121 return ret;
122 }
123
124 ret = regulator_enable(hsusb_vddcx);
125 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 regulator_set_voltage(hsusb_vddcx, 0,
127 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
130 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 return ret;
139 }
140
141 ret = regulator_set_voltage(hsusb_vddcx, 0,
142 USB_PHY_VDD_DIG_VOL_MIN);
143 if (ret) {
144 dev_err(motg->otg.dev, "unable to set the voltage"
145 "for hsusb vddcx\n");
146 return ret;
147 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148
149 regulator_put(hsusb_vddcx);
150 }
151
152 return ret;
153}
154
155static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
156{
157 int rc = 0;
158
159 if (init) {
160 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
161 if (IS_ERR(hsusb_3p3)) {
162 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
163 return PTR_ERR(hsusb_3p3);
164 }
165
166 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
167 USB_PHY_3P3_VOL_MAX);
168 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 dev_err(motg->otg.dev, "unable to set voltage level for"
170 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 goto put_3p3;
172 }
173 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
174 if (IS_ERR(hsusb_1p8)) {
175 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
176 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 }
179 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
180 USB_PHY_1P8_VOL_MAX);
181 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 dev_err(motg->otg.dev, "unable to set voltage level for"
183 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530184 goto put_1p8;
185 }
186
187 return 0;
188 }
189
Anji jonnala11aa5c42011-05-04 10:19:48 +0530190put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193put_3p3_lpm:
194 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530195put_3p3:
196 regulator_put(hsusb_3p3);
197 return rc;
198}
199
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530200#ifdef CONFIG_PM_SLEEP
201#define USB_PHY_SUSP_DIG_VOL 500000
202static int msm_hsusb_config_vddcx(int high)
203{
204 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
205 int min_vol;
206 int ret;
207
208 if (high)
209 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
210 else
211 min_vol = USB_PHY_SUSP_DIG_VOL;
212
213 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
214 if (ret) {
215 pr_err("%s: unable to set the voltage for regulator "
216 "HSUSB_VDDCX\n", __func__);
217 return ret;
218 }
219
220 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
221
222 return ret;
223}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700224#else
225static int msm_hsusb_config_vddcx(int high)
226{
227 return 0;
228}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530229#endif
230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232{
233 int ret = 0;
234
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530235 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530236 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
237 return -ENODEV;
238 }
239
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530240 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
242 return -ENODEV;
243 }
244
245 if (on) {
246 ret = regulator_set_optimum_mode(hsusb_1p8,
247 USB_PHY_1P8_HPM_LOAD);
248 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530250 "HSUSB_1p8\n", __func__);
251 return ret;
252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253
254 ret = regulator_enable(hsusb_1p8);
255 if (ret) {
256 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
257 __func__);
258 regulator_set_optimum_mode(hsusb_1p8, 0);
259 return ret;
260 }
261
Anji jonnala11aa5c42011-05-04 10:19:48 +0530262 ret = regulator_set_optimum_mode(hsusb_3p3,
263 USB_PHY_3P3_HPM_LOAD);
264 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 regulator_set_optimum_mode(hsusb_1p8, 0);
268 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530269 return ret;
270 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271
272 ret = regulator_enable(hsusb_3p3);
273 if (ret) {
274 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
275 __func__);
276 regulator_set_optimum_mode(hsusb_3p3, 0);
277 regulator_set_optimum_mode(hsusb_1p8, 0);
278 regulator_disable(hsusb_1p8);
279 return ret;
280 }
281
Anji jonnala11aa5c42011-05-04 10:19:48 +0530282 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 ret = regulator_disable(hsusb_1p8);
284 if (ret) {
285 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
286 __func__);
287 return ret;
288 }
289
290 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530291 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530293 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 ret = regulator_disable(hsusb_3p3);
296 if (ret) {
297 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
298 __func__);
299 return ret;
300 }
301 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530302 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530304 "HSUSB_3p3\n", __func__);
305 }
306
307 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
308 return ret < 0 ? ret : 0;
309}
310
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530311static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
312{
313 static struct regulator *mhl_analog_switch;
314 struct msm_otg_platform_data *pdata = motg->pdata;
315
316 if (!pdata->mhl_enable)
317 return;
318
319 if (on) {
320 mhl_analog_switch = regulator_get(motg->otg.dev,
321 "mhl_ext_3p3v");
322 if (IS_ERR(mhl_analog_switch)) {
323 pr_err("Unable to get mhl_analog_switch\n");
324 return;
325 }
326
327 if (regulator_enable(mhl_analog_switch)) {
328 pr_err("unable to enable mhl_analog_switch\n");
329 goto put_analog_switch;
330 }
331 return;
332 }
333
334 regulator_disable(mhl_analog_switch);
335put_analog_switch:
336 regulator_put(mhl_analog_switch);
337}
338
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339static int ulpi_read(struct otg_transceiver *otg, u32 reg)
340{
341 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
342 int cnt = 0;
343
344 /* initiate read operation */
345 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
346 USB_ULPI_VIEWPORT);
347
348 /* wait for completion */
349 while (cnt < ULPI_IO_TIMEOUT_USEC) {
350 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
351 break;
352 udelay(1);
353 cnt++;
354 }
355
356 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
357 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
358 readl(USB_ULPI_VIEWPORT));
359 return -ETIMEDOUT;
360 }
361 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
362}
363
364static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
365{
366 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
367 int cnt = 0;
368
369 /* initiate write operation */
370 writel(ULPI_RUN | ULPI_WRITE |
371 ULPI_ADDR(reg) | ULPI_DATA(val),
372 USB_ULPI_VIEWPORT);
373
374 /* wait for completion */
375 while (cnt < ULPI_IO_TIMEOUT_USEC) {
376 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
377 break;
378 udelay(1);
379 cnt++;
380 }
381
382 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
383 dev_err(otg->dev, "ulpi_write: timeout\n");
384 return -ETIMEDOUT;
385 }
386 return 0;
387}
388
389static struct otg_io_access_ops msm_otg_io_ops = {
390 .read = ulpi_read,
391 .write = ulpi_write,
392};
393
394static void ulpi_init(struct msm_otg *motg)
395{
396 struct msm_otg_platform_data *pdata = motg->pdata;
397 int *seq = pdata->phy_init_seq;
398
399 if (!seq)
400 return;
401
402 while (seq[0] >= 0) {
403 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
404 seq[0], seq[1]);
405 ulpi_write(&motg->otg, seq[0], seq[1]);
406 seq += 2;
407 }
408}
409
410static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
411{
412 int ret;
413
414 if (assert) {
415 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
416 if (ret)
417 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
418 } else {
419 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
420 if (ret)
421 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
422 }
423 return ret;
424}
425
426static int msm_otg_phy_clk_reset(struct msm_otg *motg)
427{
428 int ret;
429
Amit Blay02eff132011-09-21 16:46:24 +0300430 if (IS_ERR(motg->phy_reset_clk))
431 return 0;
432
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530433 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
434 if (ret) {
435 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
436 return ret;
437 }
438 usleep_range(10000, 12000);
439 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
440 if (ret)
441 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
442 return ret;
443}
444
445static int msm_otg_phy_reset(struct msm_otg *motg)
446{
447 u32 val;
448 int ret;
449 int retries;
450
451 ret = msm_otg_link_clk_reset(motg, 1);
452 if (ret)
453 return ret;
454 ret = msm_otg_phy_clk_reset(motg);
455 if (ret)
456 return ret;
457 ret = msm_otg_link_clk_reset(motg, 0);
458 if (ret)
459 return ret;
460
461 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
462 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
463
464 for (retries = 3; retries > 0; retries--) {
465 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
466 ULPI_CLR(ULPI_FUNC_CTRL));
467 if (!ret)
468 break;
469 ret = msm_otg_phy_clk_reset(motg);
470 if (ret)
471 return ret;
472 }
473 if (!retries)
474 return -ETIMEDOUT;
475
476 /* This reset calibrates the phy, if the above write succeeded */
477 ret = msm_otg_phy_clk_reset(motg);
478 if (ret)
479 return ret;
480
481 for (retries = 3; retries > 0; retries--) {
482 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
483 if (ret != -ETIMEDOUT)
484 break;
485 ret = msm_otg_phy_clk_reset(motg);
486 if (ret)
487 return ret;
488 }
489 if (!retries)
490 return -ETIMEDOUT;
491
492 dev_info(motg->otg.dev, "phy_reset: success\n");
493 return 0;
494}
495
496#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497static int msm_otg_link_reset(struct msm_otg *motg)
498{
499 int cnt = 0;
500
501 writel_relaxed(USBCMD_RESET, USB_USBCMD);
502 while (cnt < LINK_RESET_TIMEOUT_USEC) {
503 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
504 break;
505 udelay(1);
506 cnt++;
507 }
508 if (cnt >= LINK_RESET_TIMEOUT_USEC)
509 return -ETIMEDOUT;
510
511 /* select ULPI phy */
512 writel_relaxed(0x80000000, USB_PORTSC);
513 writel_relaxed(0x0, USB_AHBBURST);
514 writel_relaxed(0x00, USB_AHBMODE);
515
516 return 0;
517}
518
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530519static int msm_otg_reset(struct otg_transceiver *otg)
520{
521 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
522 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530523 int ret;
524 u32 val = 0;
525 u32 ulpi_val = 0;
526
Ofir Cohen4da266f2012-01-03 10:19:29 +0200527 /*
528 * USB PHY and Link reset also reset the USB BAM.
529 * Thus perform reset operation only once to avoid
530 * USB BAM reset on other cases e.g. USB cable disconnections.
531 */
532 if (pdata->disable_reset_on_disconnect) {
533 if (motg->reset_counter)
534 return 0;
535 else
536 motg->reset_counter++;
537 }
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540 ret = msm_otg_phy_reset(motg);
541 if (ret) {
542 dev_err(otg->dev, "phy_reset failed\n");
543 return ret;
544 }
545
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530546 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530547 ret = msm_otg_link_reset(motg);
548 if (ret) {
549 dev_err(otg->dev, "link reset failed\n");
550 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530552 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530553
554 ulpi_init(motg);
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 /* Ensure that RESET operation is completed before turning off clock */
557 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 clk_disable(motg->clk);
560
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530561 if (pdata->otg_control == OTG_PHY_CONTROL) {
562 val = readl_relaxed(USB_OTGSC);
563 if (pdata->mode == USB_OTG) {
564 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
565 val |= OTGSC_IDIE | OTGSC_BSVIE;
566 } else if (pdata->mode == USB_PERIPHERAL) {
567 ulpi_val = ULPI_INT_SESS_VALID;
568 val |= OTGSC_BSVIE;
569 }
570 writel_relaxed(val, USB_OTGSC);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
572 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530573 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 return 0;
576}
577
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530578static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
579{
580 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
581
582 /*
583 * Allow bus suspend only for host mode. Device mode bus suspend
584 * is not implemented yet.
585 */
586 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530587 /*
588 * ID_GND --> ID_A transition can not be detected in LPM.
589 * Disallow host bus suspend when ACA is enabled.
590 */
591 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530592 pm_runtime_put(otg->dev);
593 else
594 pm_runtime_resume(otg->dev);
595 }
596
597 return 0;
598}
599
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530600#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530601#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
602
603#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530604static int msm_otg_suspend(struct msm_otg *motg)
605{
606 struct otg_transceiver *otg = &motg->otg;
607 struct usb_bus *bus = otg->host;
608 struct msm_otg_platform_data *pdata = motg->pdata;
609 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530610 bool host_bus_suspend;
611 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800612 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530613 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530614
615 if (atomic_read(&motg->in_lpm))
616 return 0;
617
618 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530619 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530620 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530621 * Chipidea 45-nm PHY suspend sequence:
622 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530623 * Interrupt Latch Register auto-clear feature is not present
624 * in all PHY versions. Latch register is clear on read type.
625 * Clear latch register to avoid spurious wakeup from
626 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530627 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530628 * PHY comparators are disabled when PHY enters into low power
629 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
630 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
631 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530632 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530633 * PLL is not turned off when PHY enters into low power mode (LPM).
634 * Disable PLL for maximum power savings.
635 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530636
637 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
638 ulpi_read(otg, 0x14);
639 if (pdata->otg_control == OTG_PHY_CONTROL)
640 ulpi_write(otg, 0x01, 0x30);
641 ulpi_write(otg, 0x08, 0x09);
642 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530643
644 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 * Turn off the OTG comparators, if depends on PMIC for
646 * VBUS and ID notifications.
647 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530648 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 ulpi_write(otg, OTG_COMP_DISABLE,
650 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
651 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
652 }
653
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530654 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530655 * PHY may take some time or even fail to enter into low power
656 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
657 * in failure case.
658 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530659 portsc = readl_relaxed(USB_PORTSC);
660 if (!(portsc & PORTSC_PHCD)) {
661 writel_relaxed(portsc | PORTSC_PHCD,
662 USB_PORTSC);
663 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
664 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
665 break;
666 udelay(1);
667 cnt++;
668 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530669 }
670
671 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
672 dev_err(otg->dev, "Unable to suspend PHY\n");
673 msm_otg_reset(otg);
674 enable_irq(motg->irq);
675 return -ETIMEDOUT;
676 }
677
678 /*
679 * PHY has capability to generate interrupt asynchronously in low
680 * power mode (LPM). This interrupt is level triggered. So USB IRQ
681 * line must be disabled till async interrupt enable bit is cleared
682 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
683 * block data communication from PHY.
684 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530685 cmd_val = readl_relaxed(USB_USBCMD);
686 if (host_bus_suspend)
687 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
688 else
689 cmd_val |= ULPI_STP_CTRL;
690 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530691
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530692 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200693 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
694 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
695 /* Enable PHY HV interrupts to wake MPM/Link */
696 phy_ctrl_val |=
697 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
698
699 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 motg->lpm_flags |= PHY_RETENTIONED;
701 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 /* Ensure that above operation is completed before turning off clocks */
704 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530705 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800706 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530707
Anji jonnala7da3f262011-12-02 17:22:14 -0800708 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
709 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
710 if (ret)
711 dev_err(otg->dev, "%s failed to devote for "
712 "TCXO D0 buffer%d\n", __func__, ret);
713
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530714 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 msm_hsusb_ldo_enable(motg, 0);
716 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530717 }
718
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530719 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530721 msm_hsusb_mhl_switch_enable(motg, 0);
722 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
724 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530725 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 if (motg->pdata->pmic_id_irq)
727 enable_irq_wake(motg->pdata->pmic_id_irq);
728 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530729 if (bus)
730 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
731
732 atomic_set(&motg->in_lpm, 1);
733 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530735
736 dev_info(otg->dev, "USB in low power mode\n");
737
738 return 0;
739}
740
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741static int msm_otg_resume(struct msm_otg *motg)
742{
743 struct otg_transceiver *otg = &motg->otg;
744 struct usb_bus *bus = otg->host;
745 int cnt = 0;
746 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200747 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800748 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749
750 if (!atomic_read(&motg->in_lpm))
751 return 0;
752
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800754
755 /* Vote for TCXO when waking up the phy */
756 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
757 if (ret)
758 dev_err(otg->dev, "%s failed to vote for "
759 "TCXO D0 buffer%d\n", __func__, ret);
760
Manu Gautam5143b252012-01-05 19:25:23 -0800761 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200762
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530764
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
766 msm_hsusb_ldo_enable(motg, 1);
767 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
768 }
769
770 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530771 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530772 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200773 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
774 phy_ctrl_val |= PHY_RETEN;
775 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
776 /* Disable PHY HV interrupts */
777 phy_ctrl_val &=
778 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
779 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 }
782
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530783 temp = readl(USB_USBCMD);
784 temp &= ~ASYNC_INTR_CTRL;
785 temp &= ~ULPI_STP_CTRL;
786 writel(temp, USB_USBCMD);
787
788 /*
789 * PHY comes out of low power mode (LPM) in case of wakeup
790 * from asynchronous interrupt.
791 */
792 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
793 goto skip_phy_resume;
794
795 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
796 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
797 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
798 break;
799 udelay(1);
800 cnt++;
801 }
802
803 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
804 /*
805 * This is a fatal error. Reset the link and
806 * PHY. USB state can not be restored. Re-insertion
807 * of USB cable is the only way to get USB working.
808 */
809 dev_err(otg->dev, "Unable to resume USB."
810 "Re-plugin the cable\n");
811 msm_otg_reset(otg);
812 }
813
814skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 /* Turn on the OTG comparators on resume */
816 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
817 ulpi_write(otg, OTG_COMP_DISABLE,
818 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
819 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
820 }
821 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (motg->pdata->pmic_id_irq)
824 disable_irq_wake(motg->pdata->pmic_id_irq);
825 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826 if (bus)
827 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
828
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530829 atomic_set(&motg->in_lpm, 0);
830
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 if (motg->async_int) {
832 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 enable_irq(motg->irq);
834 }
835
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530836 dev_info(otg->dev, "USB exited from low power mode\n");
837
838 return 0;
839}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530840#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841
Amit Blay0f7edf72012-01-15 10:11:27 +0200842static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
843{
844 struct power_supply *psy;
845
846 psy = power_supply_get_by_name("usb");
847 if (!psy)
848 goto psy_not_supported;
849
850 if (motg->cur_power == 0 && mA > 0) {
851 /* Enable charging */
852 if (power_supply_set_online(psy, true))
853 goto psy_not_supported;
854 } else if (motg->cur_power > 0 && mA == 0) {
855 /* Disable charging */
856 if (power_supply_set_online(psy, false))
857 goto psy_not_supported;
858 return 0;
859 }
860 /* Set max current limit */
861 if (power_supply_set_current_limit(psy, 1000*mA))
862 goto psy_not_supported;
863
864 return 0;
865
866psy_not_supported:
867 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
868 return -ENXIO;
869}
870
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530871static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
872{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530873 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
874 motg->chg_type == USB_ACA_A_CHARGER ||
875 motg->chg_type == USB_ACA_B_CHARGER ||
876 motg->chg_type == USB_ACA_C_CHARGER) &&
877 mA > IDEV_ACA_CHG_LIMIT)
878 mA = IDEV_ACA_CHG_LIMIT;
879
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530880 if (motg->cur_power == mA)
881 return;
882
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530883 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +0200884
885 /*
886 * Use Power Supply API if supported, otherwise fallback
887 * to legacy pm8921 API.
888 */
889 if (msm_otg_notify_power_supply(motg, mA))
890 pm8921_charger_vbus_draw(mA);
891
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530892 motg->cur_power = mA;
893}
894
895static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
896{
897 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
898
899 /*
900 * Gadget driver uses set_power method to notify about the
901 * available current based on suspend/configured states.
902 *
903 * IDEV_CHG can be drawn irrespective of suspend/un-configured
904 * states when CDP/ACA is connected.
905 */
906 if (motg->chg_type == USB_SDP_CHARGER)
907 msm_otg_notify_charger(motg, mA);
908
909 return 0;
910}
911
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530912static void msm_otg_start_host(struct otg_transceiver *otg, int on)
913{
914 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
915 struct msm_otg_platform_data *pdata = motg->pdata;
916 struct usb_hcd *hcd;
917
918 if (!otg->host)
919 return;
920
921 hcd = bus_to_hcd(otg->host);
922
923 if (on) {
924 dev_dbg(otg->dev, "host on\n");
925
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530926 /*
927 * Some boards have a switch cotrolled by gpio
928 * to enable/disable internal HUB. Enable internal
929 * HUB before kicking the host.
930 */
931 if (pdata->setup_gpio)
932 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530933 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530934 } else {
935 dev_dbg(otg->dev, "host off\n");
936
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530937 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530938 /* HCD core reset all bits of PORTSC. select ULPI phy */
939 writel_relaxed(0x80000000, USB_PORTSC);
940
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530941 if (pdata->setup_gpio)
942 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530943 }
944}
945
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700946static int msm_otg_usbdev_notify(struct notifier_block *self,
947 unsigned long action, void *priv)
948{
949 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530950 struct usb_device *udev = priv;
951
952 if (!aca_enabled())
953 goto out;
954
955 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
956 goto out;
957
958 if (udev->bus != motg->otg.host)
959 goto out;
960 /*
961 * Interested in devices connected directly to the root hub.
962 * ACA dock can supply IDEV_CHG irrespective devices connected
963 * on the accessory port.
964 */
965 if (!udev->parent || udev->parent->parent ||
966 motg->chg_type == USB_ACA_DOCK_CHARGER)
967 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700968
969 switch (action) {
970 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530971 usb_disable_autosuspend(udev);
972 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 if (udev->actconfig)
975 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
976 else
977 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530978 break;
979 case USB_DEVICE_REMOVE:
980 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700981 break;
982 default:
983 break;
984 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530985 if (test_bit(ID_A, &motg->inputs))
986 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
987 motg->mA_port);
988out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 return NOTIFY_OK;
990}
991
Mayank Ranae3926882011-12-26 09:47:54 +0530992static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
993{
994 int ret;
995 static bool vbus_is_on;
996
997 if (vbus_is_on == on)
998 return;
999
1000 if (motg->pdata->vbus_power) {
1001 motg->pdata->vbus_power(on);
1002 return;
1003 }
1004
1005 if (!vbus_otg) {
1006 pr_err("vbus_otg is NULL.");
1007 return;
1008 }
1009
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001010 /*
1011 * if entering host mode tell the charger to not draw any current
1012 * from usb - if exiting host mode let the charger draw current
1013 */
1014 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301015 if (on) {
1016 ret = regulator_enable(vbus_otg);
1017 if (ret) {
1018 pr_err("unable to enable vbus_otg\n");
1019 return;
1020 }
1021 vbus_is_on = true;
1022 } else {
1023 ret = regulator_disable(vbus_otg);
1024 if (ret) {
1025 pr_err("unable to disable vbus_otg\n");
1026 return;
1027 }
1028 vbus_is_on = false;
1029 }
1030}
1031
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301032static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1033{
1034 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1035 struct usb_hcd *hcd;
1036
1037 /*
1038 * Fail host registration if this board can support
1039 * only peripheral configuration.
1040 */
1041 if (motg->pdata->mode == USB_PERIPHERAL) {
1042 dev_info(otg->dev, "Host mode is not supported\n");
1043 return -ENODEV;
1044 }
1045
Mayank Ranae3926882011-12-26 09:47:54 +05301046 if (!motg->pdata->vbus_power && host) {
1047 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1048 if (IS_ERR(vbus_otg)) {
1049 pr_err("Unable to get vbus_otg\n");
1050 return -ENODEV;
1051 }
1052 }
1053
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301054 if (!host) {
1055 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301056 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001057 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301058 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301059 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301060 otg->host = NULL;
1061 otg->state = OTG_STATE_UNDEFINED;
1062 schedule_work(&motg->sm_work);
1063 } else {
1064 otg->host = NULL;
1065 }
1066
Mayank Ranae3926882011-12-26 09:47:54 +05301067 if (vbus_otg)
1068 regulator_put(vbus_otg);
1069
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301070 return 0;
1071 }
1072
1073 hcd = bus_to_hcd(host);
1074 hcd->power_budget = motg->pdata->power_budget;
1075
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001076 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1077 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301078 otg->host = host;
1079 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1080
1081 /*
1082 * Kick the state machine work, if peripheral is not supported
1083 * or peripheral is already registered with us.
1084 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301085 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1086 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301087 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301088 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301089
1090 return 0;
1091}
1092
1093static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1094{
1095 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1096 struct msm_otg_platform_data *pdata = motg->pdata;
1097
1098 if (!otg->gadget)
1099 return;
1100
1101 if (on) {
1102 dev_dbg(otg->dev, "gadget on\n");
1103 /*
1104 * Some boards have a switch cotrolled by gpio
1105 * to enable/disable internal HUB. Disable internal
1106 * HUB before kicking the gadget.
1107 */
1108 if (pdata->setup_gpio)
1109 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301110 /*
1111 * vote for minimum dma_latency to prevent idle
1112 * power collapse(pc) while running in peripheral mode.
1113 */
1114 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301115 usb_gadget_vbus_connect(otg->gadget);
1116 } else {
1117 dev_dbg(otg->dev, "gadget off\n");
1118 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301119 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 if (pdata->setup_gpio)
1121 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1122 }
1123
1124}
1125
1126static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1127 struct usb_gadget *gadget)
1128{
1129 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1130
1131 /*
1132 * Fail peripheral registration if this board can support
1133 * only host configuration.
1134 */
1135 if (motg->pdata->mode == USB_HOST) {
1136 dev_info(otg->dev, "Peripheral mode is not supported\n");
1137 return -ENODEV;
1138 }
1139
1140 if (!gadget) {
1141 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301142 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301143 msm_otg_start_peripheral(otg, 0);
1144 otg->gadget = NULL;
1145 otg->state = OTG_STATE_UNDEFINED;
1146 schedule_work(&motg->sm_work);
1147 } else {
1148 otg->gadget = NULL;
1149 }
1150
1151 return 0;
1152 }
1153 otg->gadget = gadget;
1154 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1155
1156 /*
1157 * Kick the state machine work, if host is not supported
1158 * or host is already registered with us.
1159 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301160 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1161 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301162 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301163 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301164
1165 return 0;
1166}
1167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168static bool msm_chg_aca_detect(struct msm_otg *motg)
1169{
1170 struct otg_transceiver *otg = &motg->otg;
1171 u32 int_sts;
1172 bool ret = false;
1173
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301174 if (!aca_enabled())
1175 goto out;
1176
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1178 goto out;
1179
1180 int_sts = ulpi_read(otg, 0x87);
1181 switch (int_sts & 0x1C) {
1182 case 0x08:
1183 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1184 dev_dbg(otg->dev, "ID_A\n");
1185 motg->chg_type = USB_ACA_A_CHARGER;
1186 motg->chg_state = USB_CHG_STATE_DETECTED;
1187 clear_bit(ID_B, &motg->inputs);
1188 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301189 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001190 ret = true;
1191 }
1192 break;
1193 case 0x0C:
1194 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1195 dev_dbg(otg->dev, "ID_B\n");
1196 motg->chg_type = USB_ACA_B_CHARGER;
1197 motg->chg_state = USB_CHG_STATE_DETECTED;
1198 clear_bit(ID_A, &motg->inputs);
1199 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301200 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 ret = true;
1202 }
1203 break;
1204 case 0x10:
1205 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1206 dev_dbg(otg->dev, "ID_C\n");
1207 motg->chg_type = USB_ACA_C_CHARGER;
1208 motg->chg_state = USB_CHG_STATE_DETECTED;
1209 clear_bit(ID_A, &motg->inputs);
1210 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301211 set_bit(ID, &motg->inputs);
1212 ret = true;
1213 }
1214 break;
1215 case 0x04:
1216 if (test_and_clear_bit(ID, &motg->inputs)) {
1217 dev_dbg(otg->dev, "ID_GND\n");
1218 motg->chg_type = USB_INVALID_CHARGER;
1219 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1220 clear_bit(ID_A, &motg->inputs);
1221 clear_bit(ID_B, &motg->inputs);
1222 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 ret = true;
1224 }
1225 break;
1226 default:
1227 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1228 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301229 test_and_clear_bit(ID_C, &motg->inputs) |
1230 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 motg->chg_type = USB_INVALID_CHARGER;
1234 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1235 }
1236 }
1237out:
1238 return ret;
1239}
1240
1241static void msm_chg_enable_aca_det(struct msm_otg *motg)
1242{
1243 struct otg_transceiver *otg = &motg->otg;
1244
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 if (!aca_enabled())
1246 return;
1247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248 switch (motg->pdata->phy_type) {
1249 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301250 /* Disable ID_GND in link and PHY */
1251 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1252 OTGSC_IDIE), USB_OTGSC);
1253 ulpi_write(otg, 0x01, 0x0C);
1254 ulpi_write(otg, 0x10, 0x0F);
1255 ulpi_write(otg, 0x10, 0x12);
1256 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301258 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001259 break;
1260 default:
1261 break;
1262 }
1263}
1264
1265static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1266{
1267 struct otg_transceiver *otg = &motg->otg;
1268
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301269 if (!aca_enabled())
1270 return;
1271
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001272 switch (motg->pdata->phy_type) {
1273 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301274 /* Enable ACA Detection interrupt (on any RID change) */
1275 ulpi_write(otg, 0x01, 0x94);
1276 break;
1277 default:
1278 break;
1279 }
1280}
1281
1282static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1283{
1284 struct otg_transceiver *otg = &motg->otg;
1285
1286 if (!aca_enabled())
1287 return;
1288
1289 switch (motg->pdata->phy_type) {
1290 case SNPS_28NM_INTEGRATED_PHY:
1291 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 break;
1293 default:
1294 break;
1295 }
1296}
1297
1298static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1299{
1300 struct otg_transceiver *otg = &motg->otg;
1301 bool ret = false;
1302
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301303 if (!aca_enabled())
1304 return ret;
1305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 switch (motg->pdata->phy_type) {
1307 case SNPS_28NM_INTEGRATED_PHY:
1308 if (ulpi_read(otg, 0x91) & 1) {
1309 dev_dbg(otg->dev, "RID change\n");
1310 ulpi_write(otg, 0x01, 0x92);
1311 ret = msm_chg_aca_detect(motg);
1312 }
1313 default:
1314 break;
1315 }
1316 return ret;
1317}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301318
1319static void msm_otg_id_timer_func(unsigned long data)
1320{
1321 struct msm_otg *motg = (struct msm_otg *) data;
1322
1323 if (!aca_enabled())
1324 return;
1325
1326 if (atomic_read(&motg->in_lpm)) {
1327 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1328 return;
1329 }
1330
1331 if (msm_chg_check_aca_intr(motg)) {
1332 dev_dbg(motg->otg.dev, "timer: aca work\n");
1333 schedule_work(&motg->sm_work);
1334 }
1335
1336 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1337 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1338}
1339
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301340static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1341{
1342 struct otg_transceiver *otg = &motg->otg;
1343 u32 chg_det;
1344 bool ret = false;
1345
1346 switch (motg->pdata->phy_type) {
1347 case CI_45NM_INTEGRATED_PHY:
1348 chg_det = ulpi_read(otg, 0x34);
1349 ret = chg_det & (1 << 4);
1350 break;
1351 case SNPS_28NM_INTEGRATED_PHY:
1352 chg_det = ulpi_read(otg, 0x87);
1353 ret = chg_det & 1;
1354 break;
1355 default:
1356 break;
1357 }
1358 return ret;
1359}
1360
1361static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1362{
1363 struct otg_transceiver *otg = &motg->otg;
1364 u32 chg_det;
1365
1366 switch (motg->pdata->phy_type) {
1367 case CI_45NM_INTEGRATED_PHY:
1368 chg_det = ulpi_read(otg, 0x34);
1369 /* Turn off charger block */
1370 chg_det |= ~(1 << 1);
1371 ulpi_write(otg, chg_det, 0x34);
1372 udelay(20);
1373 /* control chg block via ULPI */
1374 chg_det &= ~(1 << 3);
1375 ulpi_write(otg, chg_det, 0x34);
1376 /* put it in host mode for enabling D- source */
1377 chg_det &= ~(1 << 2);
1378 ulpi_write(otg, chg_det, 0x34);
1379 /* Turn on chg detect block */
1380 chg_det &= ~(1 << 1);
1381 ulpi_write(otg, chg_det, 0x34);
1382 udelay(20);
1383 /* enable chg detection */
1384 chg_det &= ~(1 << 0);
1385 ulpi_write(otg, chg_det, 0x34);
1386 break;
1387 case SNPS_28NM_INTEGRATED_PHY:
1388 /*
1389 * Configure DM as current source, DP as current sink
1390 * and enable battery charging comparators.
1391 */
1392 ulpi_write(otg, 0x8, 0x85);
1393 ulpi_write(otg, 0x2, 0x85);
1394 ulpi_write(otg, 0x1, 0x85);
1395 break;
1396 default:
1397 break;
1398 }
1399}
1400
1401static bool msm_chg_check_primary_det(struct msm_otg *motg)
1402{
1403 struct otg_transceiver *otg = &motg->otg;
1404 u32 chg_det;
1405 bool ret = false;
1406
1407 switch (motg->pdata->phy_type) {
1408 case CI_45NM_INTEGRATED_PHY:
1409 chg_det = ulpi_read(otg, 0x34);
1410 ret = chg_det & (1 << 4);
1411 break;
1412 case SNPS_28NM_INTEGRATED_PHY:
1413 chg_det = ulpi_read(otg, 0x87);
1414 ret = chg_det & 1;
1415 break;
1416 default:
1417 break;
1418 }
1419 return ret;
1420}
1421
1422static void msm_chg_enable_primary_det(struct msm_otg *motg)
1423{
1424 struct otg_transceiver *otg = &motg->otg;
1425 u32 chg_det;
1426
1427 switch (motg->pdata->phy_type) {
1428 case CI_45NM_INTEGRATED_PHY:
1429 chg_det = ulpi_read(otg, 0x34);
1430 /* enable chg detection */
1431 chg_det &= ~(1 << 0);
1432 ulpi_write(otg, chg_det, 0x34);
1433 break;
1434 case SNPS_28NM_INTEGRATED_PHY:
1435 /*
1436 * Configure DP as current source, DM as current sink
1437 * and enable battery charging comparators.
1438 */
1439 ulpi_write(otg, 0x2, 0x85);
1440 ulpi_write(otg, 0x1, 0x85);
1441 break;
1442 default:
1443 break;
1444 }
1445}
1446
1447static bool msm_chg_check_dcd(struct msm_otg *motg)
1448{
1449 struct otg_transceiver *otg = &motg->otg;
1450 u32 line_state;
1451 bool ret = false;
1452
1453 switch (motg->pdata->phy_type) {
1454 case CI_45NM_INTEGRATED_PHY:
1455 line_state = ulpi_read(otg, 0x15);
1456 ret = !(line_state & 1);
1457 break;
1458 case SNPS_28NM_INTEGRATED_PHY:
1459 line_state = ulpi_read(otg, 0x87);
1460 ret = line_state & 2;
1461 break;
1462 default:
1463 break;
1464 }
1465 return ret;
1466}
1467
1468static void msm_chg_disable_dcd(struct msm_otg *motg)
1469{
1470 struct otg_transceiver *otg = &motg->otg;
1471 u32 chg_det;
1472
1473 switch (motg->pdata->phy_type) {
1474 case CI_45NM_INTEGRATED_PHY:
1475 chg_det = ulpi_read(otg, 0x34);
1476 chg_det &= ~(1 << 5);
1477 ulpi_write(otg, chg_det, 0x34);
1478 break;
1479 case SNPS_28NM_INTEGRATED_PHY:
1480 ulpi_write(otg, 0x10, 0x86);
1481 break;
1482 default:
1483 break;
1484 }
1485}
1486
1487static void msm_chg_enable_dcd(struct msm_otg *motg)
1488{
1489 struct otg_transceiver *otg = &motg->otg;
1490 u32 chg_det;
1491
1492 switch (motg->pdata->phy_type) {
1493 case CI_45NM_INTEGRATED_PHY:
1494 chg_det = ulpi_read(otg, 0x34);
1495 /* Turn on D+ current source */
1496 chg_det |= (1 << 5);
1497 ulpi_write(otg, chg_det, 0x34);
1498 break;
1499 case SNPS_28NM_INTEGRATED_PHY:
1500 /* Data contact detection enable */
1501 ulpi_write(otg, 0x10, 0x85);
1502 break;
1503 default:
1504 break;
1505 }
1506}
1507
1508static void msm_chg_block_on(struct msm_otg *motg)
1509{
1510 struct otg_transceiver *otg = &motg->otg;
1511 u32 func_ctrl, chg_det;
1512
1513 /* put the controller in non-driving mode */
1514 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1515 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1516 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1517 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1518
1519 switch (motg->pdata->phy_type) {
1520 case CI_45NM_INTEGRATED_PHY:
1521 chg_det = ulpi_read(otg, 0x34);
1522 /* control chg block via ULPI */
1523 chg_det &= ~(1 << 3);
1524 ulpi_write(otg, chg_det, 0x34);
1525 /* Turn on chg detect block */
1526 chg_det &= ~(1 << 1);
1527 ulpi_write(otg, chg_det, 0x34);
1528 udelay(20);
1529 break;
1530 case SNPS_28NM_INTEGRATED_PHY:
1531 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301532 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301533 /* Clear alt interrupt latch and enable bits */
1534 ulpi_write(otg, 0x1F, 0x92);
1535 ulpi_write(otg, 0x1F, 0x95);
1536 udelay(100);
1537 break;
1538 default:
1539 break;
1540 }
1541}
1542
1543static void msm_chg_block_off(struct msm_otg *motg)
1544{
1545 struct otg_transceiver *otg = &motg->otg;
1546 u32 func_ctrl, chg_det;
1547
1548 switch (motg->pdata->phy_type) {
1549 case CI_45NM_INTEGRATED_PHY:
1550 chg_det = ulpi_read(otg, 0x34);
1551 /* Turn off charger block */
1552 chg_det |= ~(1 << 1);
1553 ulpi_write(otg, chg_det, 0x34);
1554 break;
1555 case SNPS_28NM_INTEGRATED_PHY:
1556 /* Clear charger detecting control bits */
1557 ulpi_write(otg, 0x3F, 0x86);
1558 /* Clear alt interrupt latch and enable bits */
1559 ulpi_write(otg, 0x1F, 0x92);
1560 ulpi_write(otg, 0x1F, 0x95);
1561 break;
1562 default:
1563 break;
1564 }
1565
1566 /* put the controller in normal mode */
1567 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1568 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1569 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1570 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1571}
1572
Anji jonnalad270e2d2011-08-09 11:28:32 +05301573static const char *chg_to_string(enum usb_chg_type chg_type)
1574{
1575 switch (chg_type) {
1576 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1577 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1578 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1579 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1580 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1581 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1582 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1583 default: return "INVALID_CHARGER";
1584 }
1585}
1586
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301587#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1588#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1589#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1590#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1591static void msm_chg_detect_work(struct work_struct *w)
1592{
1593 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1594 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595 bool is_dcd, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301596 unsigned long delay;
1597
1598 dev_dbg(otg->dev, "chg detection work\n");
1599 switch (motg->chg_state) {
1600 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301601 msm_chg_block_on(motg);
1602 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001603 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301604 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1605 motg->dcd_retries = 0;
1606 delay = MSM_CHG_DCD_POLL_TIME;
1607 break;
1608 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001609 is_aca = msm_chg_aca_detect(motg);
1610 if (is_aca) {
1611 /*
1612 * ID_A can be ACA dock too. continue
1613 * primary detection after DCD.
1614 */
1615 if (test_bit(ID_A, &motg->inputs)) {
1616 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1617 } else {
1618 delay = 0;
1619 break;
1620 }
1621 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301622 is_dcd = msm_chg_check_dcd(motg);
1623 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1624 if (is_dcd || tmout) {
1625 msm_chg_disable_dcd(motg);
1626 msm_chg_enable_primary_det(motg);
1627 delay = MSM_CHG_PRIMARY_DET_TIME;
1628 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1629 } else {
1630 delay = MSM_CHG_DCD_POLL_TIME;
1631 }
1632 break;
1633 case USB_CHG_STATE_DCD_DONE:
1634 vout = msm_chg_check_primary_det(motg);
1635 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301636 if (test_bit(ID_A, &motg->inputs)) {
1637 motg->chg_type = USB_ACA_DOCK_CHARGER;
1638 motg->chg_state = USB_CHG_STATE_DETECTED;
1639 delay = 0;
1640 break;
1641 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301642 msm_chg_enable_secondary_det(motg);
1643 delay = MSM_CHG_SECONDARY_DET_TIME;
1644 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1645 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301646 if (test_bit(ID_A, &motg->inputs)) {
1647 motg->chg_type = USB_ACA_A_CHARGER;
1648 motg->chg_state = USB_CHG_STATE_DETECTED;
1649 delay = 0;
1650 break;
1651 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301652 motg->chg_type = USB_SDP_CHARGER;
1653 motg->chg_state = USB_CHG_STATE_DETECTED;
1654 delay = 0;
1655 }
1656 break;
1657 case USB_CHG_STATE_PRIMARY_DONE:
1658 vout = msm_chg_check_secondary_det(motg);
1659 if (vout)
1660 motg->chg_type = USB_DCP_CHARGER;
1661 else
1662 motg->chg_type = USB_CDP_CHARGER;
1663 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1664 /* fall through */
1665 case USB_CHG_STATE_SECONDARY_DONE:
1666 motg->chg_state = USB_CHG_STATE_DETECTED;
1667 case USB_CHG_STATE_DETECTED:
1668 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001669 msm_chg_enable_aca_det(motg);
1670 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301671 dev_dbg(otg->dev, "chg_type = %s\n",
1672 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301673 schedule_work(&motg->sm_work);
1674 return;
1675 default:
1676 return;
1677 }
1678
1679 schedule_delayed_work(&motg->chg_work, delay);
1680}
1681
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301682/*
1683 * We support OTG, Peripheral only and Host only configurations. In case
1684 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1685 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1686 * enabled when switch is controlled by user and default mode is supplied
1687 * by board file, which can be changed by userspace later.
1688 */
1689static void msm_otg_init_sm(struct msm_otg *motg)
1690{
1691 struct msm_otg_platform_data *pdata = motg->pdata;
1692 u32 otgsc = readl(USB_OTGSC);
1693
1694 switch (pdata->mode) {
1695 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301697 if (pdata->default_mode == USB_HOST) {
1698 clear_bit(ID, &motg->inputs);
1699 } else if (pdata->default_mode == USB_PERIPHERAL) {
1700 set_bit(ID, &motg->inputs);
1701 set_bit(B_SESS_VLD, &motg->inputs);
1702 } else {
1703 set_bit(ID, &motg->inputs);
1704 clear_bit(B_SESS_VLD, &motg->inputs);
1705 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301706 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1707 if (otgsc & OTGSC_ID)
1708 set_bit(ID, &motg->inputs);
1709 else
1710 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001711 if (otgsc & OTGSC_BSV)
1712 set_bit(B_SESS_VLD, &motg->inputs);
1713 else
1714 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301715 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301716 if (pdata->pmic_id_irq) {
1717 if (irq_read_line(pdata->pmic_id_irq))
1718 set_bit(ID, &motg->inputs);
1719 else
1720 clear_bit(ID, &motg->inputs);
1721 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301722 /*
1723 * VBUS initial state is reported after PMIC
1724 * driver initialization. Wait for it.
1725 */
1726 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301727 }
1728 break;
1729 case USB_HOST:
1730 clear_bit(ID, &motg->inputs);
1731 break;
1732 case USB_PERIPHERAL:
1733 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301734 if (pdata->otg_control == OTG_PHY_CONTROL) {
1735 if (otgsc & OTGSC_BSV)
1736 set_bit(B_SESS_VLD, &motg->inputs);
1737 else
1738 clear_bit(B_SESS_VLD, &motg->inputs);
1739 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1740 /*
1741 * VBUS initial state is reported after PMIC
1742 * driver initialization. Wait for it.
1743 */
1744 wait_for_completion(&pmic_vbus_init);
1745 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301746 break;
1747 default:
1748 break;
1749 }
1750}
1751
1752static void msm_otg_sm_work(struct work_struct *w)
1753{
1754 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1755 struct otg_transceiver *otg = &motg->otg;
1756
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301757 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301758 switch (otg->state) {
1759 case OTG_STATE_UNDEFINED:
1760 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1761 msm_otg_reset(otg);
1762 msm_otg_init_sm(motg);
1763 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301764 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1765 test_bit(ID, &motg->inputs)) {
1766 pm_runtime_put_noidle(otg->dev);
1767 pm_runtime_suspend(otg->dev);
1768 break;
1769 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301770 /* FALL THROUGH */
1771 case OTG_STATE_B_IDLE:
1772 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001773 if ((!test_bit(ID, &motg->inputs) ||
1774 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001775 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1776 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301777 IDEV_ACA_CHG_MAX);
1778 else if (test_bit(ID_A, &motg->inputs))
1779 msm_otg_notify_charger(motg,
1780 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301781 else
1782 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301783 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301784 /*
1785 * Link can not generate PHY_ALT interrupt
1786 * in host mode when no device is attached
1787 * to the port. It is also observed PHY_ALT
1788 * interrupt missing upon Micro-A cable disconnect.
1789 * Hence disable PHY_ALT interrupt and perform
1790 * polling to detect RID change.
1791 */
1792 msm_chg_enable_aca_det(motg);
1793 msm_chg_disable_aca_intr(motg);
1794 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301795 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301796 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1797 switch (motg->chg_state) {
1798 case USB_CHG_STATE_UNDEFINED:
1799 msm_chg_detect_work(&motg->chg_work.work);
1800 break;
1801 case USB_CHG_STATE_DETECTED:
1802 switch (motg->chg_type) {
1803 case USB_DCP_CHARGER:
1804 msm_otg_notify_charger(motg,
1805 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301806 pm_runtime_put_noidle(otg->dev);
1807 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301808 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301809 case USB_ACA_B_CHARGER:
1810 msm_otg_notify_charger(motg,
1811 IDEV_ACA_CHG_MAX);
1812 /*
1813 * (ID_B --> ID_C) PHY_ALT interrupt can
1814 * not be detected in LPM.
1815 */
1816 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301817 case USB_CDP_CHARGER:
1818 msm_otg_notify_charger(motg,
1819 IDEV_CHG_MAX);
1820 msm_otg_start_peripheral(otg, 1);
1821 otg->state = OTG_STATE_B_PERIPHERAL;
1822 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301823 case USB_ACA_C_CHARGER:
1824 msm_otg_notify_charger(motg,
1825 IDEV_ACA_CHG_MAX);
1826 msm_otg_start_peripheral(otg, 1);
1827 otg->state = OTG_STATE_B_PERIPHERAL;
1828 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 case USB_SDP_CHARGER:
1830 msm_otg_notify_charger(motg, IUNIT);
1831 msm_otg_start_peripheral(otg, 1);
1832 otg->state = OTG_STATE_B_PERIPHERAL;
1833 break;
1834 default:
1835 break;
1836 }
1837 break;
1838 default:
1839 break;
1840 }
1841 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301842 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301843 msm_otg_notify_charger(motg, 0);
1844 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1845 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301846 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301847 pm_runtime_put_noidle(otg->dev);
1848 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301849 }
1850 break;
1851 case OTG_STATE_B_PERIPHERAL:
1852 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1853 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001854 !test_bit(ID, &motg->inputs) ||
1855 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301856 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 schedule_work(w);
1859 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301860 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 }
1862 break;
1863 case OTG_STATE_A_HOST:
1864 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1865 if (test_bit(ID, &motg->inputs) &&
1866 !test_bit(ID_A, &motg->inputs)) {
1867 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301868 msm_hsusb_vbus_power(motg, 0);
1869 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301870 /*
1871 * Exit point of host mode.
1872 *
1873 * 1. Micro-A cable disconnect: Just schedule
1874 * the work. PHY is reset in B_IDLE and LPM
1875 * is allowed.
1876 * 2. ID_GND --> ID_B: No need to reset the PHY.
1877 * HCD core clears all PORTSC bits and initializes
1878 * the controller to host mode in remove_hcd.
1879 * Restore PORTSC transceiver select bits (ULPI)
1880 * and reset the controller to change MODE bits.
1881 * PHY_ALT interrupt can not occur in host mode.
1882 */
1883 del_timer_sync(&motg->id_timer);
1884 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1885 msm_otg_link_reset(motg);
1886 msm_chg_enable_aca_intr(motg);
1887 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301888 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301889 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301891 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301893 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301896 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301897 }
1898 break;
1899 default:
1900 break;
1901 }
1902}
1903
1904static irqreturn_t msm_otg_irq(int irq, void *data)
1905{
1906 struct msm_otg *motg = data;
1907 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001908 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301909
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301910 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301911 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301912 disable_irq_nosync(irq);
1913 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301914 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301915 return IRQ_HANDLED;
1916 }
1917
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001918 usbsts = readl(USB_USBSTS);
1919 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301920 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301922 if (msm_chg_check_aca_intr(motg)) {
1923 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301925 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 return IRQ_HANDLED;
1927 }
1928
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301929 otgsc = readl(USB_OTGSC);
1930 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1931 return IRQ_NONE;
1932
1933 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301934 if (otgsc & OTGSC_ID) {
1935 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301936 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301937 } else {
1938 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301939 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301940 msm_chg_enable_aca_det(motg);
1941 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001942 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301943 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301944 if (otgsc & OTGSC_BSV) {
1945 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301946 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301947 } else {
1948 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301949 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301950 msm_chg_check_aca_intr(motg);
1951 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001952 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301953 }
1954
1955 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 return IRQ_HANDLED;
1957}
1958
1959static void msm_otg_set_vbus_state(int online)
1960{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301961 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962 struct msm_otg *motg = the_msm_otg;
1963
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301964 if (online) {
1965 pr_debug("PMIC: BSV set\n");
1966 set_bit(B_SESS_VLD, &motg->inputs);
1967 } else {
1968 pr_debug("PMIC: BSV clear\n");
1969 clear_bit(B_SESS_VLD, &motg->inputs);
1970 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001971
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301972 if (!init) {
1973 init = true;
1974 complete(&pmic_vbus_init);
1975 pr_debug("PMIC: BSV init complete\n");
1976 return;
1977 }
1978
1979 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001980}
1981
1982static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1983{
1984 struct msm_otg *motg = data;
1985
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301986 if (aca_id_turned_on)
1987 return IRQ_HANDLED;
1988
1989 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1990 pr_debug("PMIC: ID set\n");
1991 set_bit(ID, &motg->inputs);
1992 } else {
1993 pr_debug("PMIC: ID clear\n");
1994 clear_bit(ID, &motg->inputs);
1995 }
1996
1997 if (motg->otg.state != OTG_STATE_UNDEFINED)
1998 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302000 return IRQ_HANDLED;
2001}
2002
2003static int msm_otg_mode_show(struct seq_file *s, void *unused)
2004{
2005 struct msm_otg *motg = s->private;
2006 struct otg_transceiver *otg = &motg->otg;
2007
2008 switch (otg->state) {
2009 case OTG_STATE_A_HOST:
2010 seq_printf(s, "host\n");
2011 break;
2012 case OTG_STATE_B_PERIPHERAL:
2013 seq_printf(s, "peripheral\n");
2014 break;
2015 default:
2016 seq_printf(s, "none\n");
2017 break;
2018 }
2019
2020 return 0;
2021}
2022
2023static int msm_otg_mode_open(struct inode *inode, struct file *file)
2024{
2025 return single_open(file, msm_otg_mode_show, inode->i_private);
2026}
2027
2028static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2029 size_t count, loff_t *ppos)
2030{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302031 struct seq_file *s = file->private_data;
2032 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302033 char buf[16];
2034 struct otg_transceiver *otg = &motg->otg;
2035 int status = count;
2036 enum usb_mode_type req_mode;
2037
2038 memset(buf, 0x00, sizeof(buf));
2039
2040 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2041 status = -EFAULT;
2042 goto out;
2043 }
2044
2045 if (!strncmp(buf, "host", 4)) {
2046 req_mode = USB_HOST;
2047 } else if (!strncmp(buf, "peripheral", 10)) {
2048 req_mode = USB_PERIPHERAL;
2049 } else if (!strncmp(buf, "none", 4)) {
2050 req_mode = USB_NONE;
2051 } else {
2052 status = -EINVAL;
2053 goto out;
2054 }
2055
2056 switch (req_mode) {
2057 case USB_NONE:
2058 switch (otg->state) {
2059 case OTG_STATE_A_HOST:
2060 case OTG_STATE_B_PERIPHERAL:
2061 set_bit(ID, &motg->inputs);
2062 clear_bit(B_SESS_VLD, &motg->inputs);
2063 break;
2064 default:
2065 goto out;
2066 }
2067 break;
2068 case USB_PERIPHERAL:
2069 switch (otg->state) {
2070 case OTG_STATE_B_IDLE:
2071 case OTG_STATE_A_HOST:
2072 set_bit(ID, &motg->inputs);
2073 set_bit(B_SESS_VLD, &motg->inputs);
2074 break;
2075 default:
2076 goto out;
2077 }
2078 break;
2079 case USB_HOST:
2080 switch (otg->state) {
2081 case OTG_STATE_B_IDLE:
2082 case OTG_STATE_B_PERIPHERAL:
2083 clear_bit(ID, &motg->inputs);
2084 break;
2085 default:
2086 goto out;
2087 }
2088 break;
2089 default:
2090 goto out;
2091 }
2092
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302093 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302094 schedule_work(&motg->sm_work);
2095out:
2096 return status;
2097}
2098
2099const struct file_operations msm_otg_mode_fops = {
2100 .open = msm_otg_mode_open,
2101 .read = seq_read,
2102 .write = msm_otg_mode_write,
2103 .llseek = seq_lseek,
2104 .release = single_release,
2105};
2106
Anji jonnalad270e2d2011-08-09 11:28:32 +05302107static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2108{
2109 struct msm_otg *motg = s->private;
2110
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302111 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302112 return 0;
2113}
2114
2115static int msm_otg_chg_open(struct inode *inode, struct file *file)
2116{
2117 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2118}
2119
2120const struct file_operations msm_otg_chg_fops = {
2121 .open = msm_otg_chg_open,
2122 .read = seq_read,
2123 .llseek = seq_lseek,
2124 .release = single_release,
2125};
2126
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302127static int msm_otg_aca_show(struct seq_file *s, void *unused)
2128{
2129 if (debug_aca_enabled)
2130 seq_printf(s, "enabled\n");
2131 else
2132 seq_printf(s, "disabled\n");
2133
2134 return 0;
2135}
2136
2137static int msm_otg_aca_open(struct inode *inode, struct file *file)
2138{
2139 return single_open(file, msm_otg_aca_show, inode->i_private);
2140}
2141
2142static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2143 size_t count, loff_t *ppos)
2144{
2145 char buf[8];
2146
2147 memset(buf, 0x00, sizeof(buf));
2148
2149 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2150 return -EFAULT;
2151
2152 if (!strncmp(buf, "enable", 6))
2153 debug_aca_enabled = true;
2154 else
2155 debug_aca_enabled = false;
2156
2157 return count;
2158}
2159
2160const struct file_operations msm_otg_aca_fops = {
2161 .open = msm_otg_aca_open,
2162 .read = seq_read,
2163 .write = msm_otg_aca_write,
2164 .llseek = seq_lseek,
2165 .release = single_release,
2166};
2167
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302168static struct dentry *msm_otg_dbg_root;
2169static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302170static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302171static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302172
2173static int msm_otg_debugfs_init(struct msm_otg *motg)
2174{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302175
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302176 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2177
2178 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2179 return -ENODEV;
2180
Anji jonnalad270e2d2011-08-09 11:28:32 +05302181 if (motg->pdata->mode == USB_OTG &&
2182 motg->pdata->otg_control == OTG_USER_CONTROL) {
2183
2184 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2185 S_IWUSR, msm_otg_dbg_root, motg,
2186 &msm_otg_mode_fops);
2187
2188 if (!msm_otg_dbg_mode) {
2189 debugfs_remove(msm_otg_dbg_root);
2190 msm_otg_dbg_root = NULL;
2191 return -ENODEV;
2192 }
2193 }
2194
2195 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2196 msm_otg_dbg_root, motg,
2197 &msm_otg_chg_fops);
2198
2199 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302200 debugfs_remove_recursive(msm_otg_dbg_root);
2201 return -ENODEV;
2202 }
2203
2204 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2205 msm_otg_dbg_root, motg,
2206 &msm_otg_aca_fops);
2207
2208 if (!msm_otg_dbg_aca) {
2209 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302210 return -ENODEV;
2211 }
2212
2213 return 0;
2214}
2215
2216static void msm_otg_debugfs_cleanup(void)
2217{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302218 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302219}
2220
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302221static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2222static struct platform_device *msm_otg_add_pdev(
2223 struct platform_device *ofdev, const char *name)
2224{
2225 struct platform_device *pdev;
2226 const struct resource *res = ofdev->resource;
2227 unsigned int num = ofdev->num_resources;
2228 int retval;
2229
2230 pdev = platform_device_alloc(name, -1);
2231 if (!pdev) {
2232 retval = -ENOMEM;
2233 goto error;
2234 }
2235
2236 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2237 pdev->dev.dma_mask = &msm_otg_dma_mask;
2238
2239 if (num) {
2240 retval = platform_device_add_resources(pdev, res, num);
2241 if (retval)
2242 goto error;
2243 }
2244
2245 retval = platform_device_add(pdev);
2246 if (retval)
2247 goto error;
2248
2249 return pdev;
2250
2251error:
2252 platform_device_put(pdev);
2253 return ERR_PTR(retval);
2254}
2255
2256static int msm_otg_setup_devices(struct platform_device *ofdev,
2257 enum usb_mode_type mode, bool init)
2258{
2259 const char *gadget_name = "msm_hsusb";
2260 const char *host_name = "msm_hsusb_host";
2261 static struct platform_device *gadget_pdev;
2262 static struct platform_device *host_pdev;
2263 int retval = 0;
2264
2265 if (!init) {
2266 if (gadget_pdev)
2267 platform_device_unregister(gadget_pdev);
2268 if (host_pdev)
2269 platform_device_unregister(host_pdev);
2270 return 0;
2271 }
2272
2273 switch (mode) {
2274 case USB_OTG:
2275 /* fall through */
2276 case USB_PERIPHERAL:
2277 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2278 if (IS_ERR(gadget_pdev)) {
2279 retval = PTR_ERR(gadget_pdev);
2280 break;
2281 }
2282 if (mode == USB_PERIPHERAL)
2283 break;
2284 /* fall through */
2285 case USB_HOST:
2286 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2287 if (IS_ERR(host_pdev)) {
2288 retval = PTR_ERR(host_pdev);
2289 if (mode == USB_OTG)
2290 platform_device_unregister(gadget_pdev);
2291 }
2292 break;
2293 default:
2294 break;
2295 }
2296
2297 return retval;
2298}
2299
2300struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2301{
2302 struct device_node *node = pdev->dev.of_node;
2303 struct msm_otg_platform_data *pdata;
2304 int len = 0;
2305
2306 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2307 if (!pdata) {
2308 pr_err("unable to allocate platform data\n");
2309 return NULL;
2310 }
2311 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2312 if (len) {
2313 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2314 if (!pdata->phy_init_seq)
2315 return NULL;
2316 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2317 pdata->phy_init_seq,
2318 len/sizeof(*pdata->phy_init_seq));
2319 }
2320 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2321 &pdata->power_budget);
2322 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2323 &pdata->mode);
2324 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2325 &pdata->otg_control);
2326 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2327 &pdata->default_mode);
2328 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2329 &pdata->phy_type);
2330 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2331 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302332 return pdata;
2333}
2334
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302335static int __init msm_otg_probe(struct platform_device *pdev)
2336{
2337 int ret = 0;
2338 struct resource *res;
2339 struct msm_otg *motg;
2340 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302341 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302342
2343 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302344
2345 if (pdev->dev.of_node) {
2346 dev_dbg(&pdev->dev, "device tree enabled\n");
2347 pdata = msm_otg_dt_to_pdata(pdev);
2348 if (!pdata)
2349 return -ENOMEM;
2350 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2351 if (ret) {
2352 dev_err(&pdev->dev, "devices setup failed\n");
2353 return ret;
2354 }
2355 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302356 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2357 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302358 } else {
2359 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302360 }
2361
2362 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2363 if (!motg) {
2364 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2365 return -ENOMEM;
2366 }
2367
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002368 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302369 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302370 otg = &motg->otg;
2371 otg->dev = &pdev->dev;
2372
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302373 /*
2374 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2375 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2376 * PMIC can detect ACA ID_GND and generate an interrupt.
2377 */
2378 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2379 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2380 ret = -EINVAL;
2381 goto free_motg;
2382 }
2383
Ofir Cohen4da266f2012-01-03 10:19:29 +02002384 /* initialize reset counter */
2385 motg->reset_counter = 0;
2386
Amit Blay02eff132011-09-21 16:46:24 +03002387 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002388 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002389 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002390 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302391
Manu Gautam5143b252012-01-05 19:25:23 -08002392 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302393 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002394 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302395 ret = PTR_ERR(motg->clk);
2396 goto put_phy_reset_clk;
2397 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302398 clk_set_rate(motg->clk, 60000000);
2399
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302400 /* pm qos request to prevent apps idle power collapse */
2401 if (motg->pdata->swfi_latency)
2402 pm_qos_add_request(&motg->pm_qos_req_dma,
2403 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002404
Anji jonnala0f73cac2011-05-04 10:19:46 +05302405 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002406 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302407 * CORE CLK must be running at >55Mhz for correct HSUSB
2408 * operation and USB core cannot tolerate frequency changes on
2409 * CORE CLK. For such USB cores, vote for maximum clk frequency
2410 * on pclk source
2411 */
Manu Gautam5143b252012-01-05 19:25:23 -08002412 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2413 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302414 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002415 dev_err(&pdev->dev, "failed to get core_clk\n");
2416 ret = PTR_ERR(motg->clk);
2417 goto put_clk;
2418 }
2419 clk_set_rate(motg->core_clk, INT_MAX);
2420
2421 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2422 if (IS_ERR(motg->pclk)) {
2423 dev_err(&pdev->dev, "failed to get iface_clk\n");
2424 ret = PTR_ERR(motg->pclk);
2425 goto put_core_clk;
2426 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302427
2428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2429 if (!res) {
2430 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2431 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002432 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302433 }
2434
2435 motg->regs = ioremap(res->start, resource_size(res));
2436 if (!motg->regs) {
2437 dev_err(&pdev->dev, "ioremap failed\n");
2438 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002439 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302440 }
2441 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2442
2443 motg->irq = platform_get_irq(pdev, 0);
2444 if (!motg->irq) {
2445 dev_err(&pdev->dev, "platform_get_irq failed\n");
2446 ret = -ENODEV;
2447 goto free_regs;
2448 }
2449
Anji jonnala7da3f262011-12-02 17:22:14 -08002450 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
2451 if (IS_ERR(motg->xo_handle)) {
2452 dev_err(&pdev->dev, "%s not able to get the handle "
2453 "to vote for TCXO D0 buffer\n", __func__);
2454 ret = PTR_ERR(motg->xo_handle);
2455 goto free_regs;
2456 }
2457
2458 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2459 if (ret) {
2460 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2461 "D0 buffer%d\n", __func__, ret);
2462 goto free_xo_handle;
2463 }
2464
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302465 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302466
2467 ret = msm_hsusb_init_vddcx(motg, 1);
2468 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002469 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002470 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302471 }
2472
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002473 ret = msm_hsusb_config_vddcx(1);
2474 if (ret) {
2475 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2476 goto free_init_vddcx;
2477 }
2478
Anji jonnala11aa5c42011-05-04 10:19:48 +05302479 ret = msm_hsusb_ldo_init(motg, 1);
2480 if (ret) {
2481 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002482 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302483 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484
2485 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302486 if (ret) {
2487 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302489 }
Manu Gautam5143b252012-01-05 19:25:23 -08002490 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302491
2492 writel(0, USB_USBINTR);
2493 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002494 /* Ensure that above STOREs are completed before enabling interrupts */
2495 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302496
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302498 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302499 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302500 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2501 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302502 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2503 "msm_otg", motg);
2504 if (ret) {
2505 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302507 }
2508
2509 otg->init = msm_otg_reset;
2510 otg->set_host = msm_otg_set_host;
2511 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302512 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302513 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302514
2515 otg->io_ops = &msm_otg_io_ops;
2516
2517 ret = otg_set_transceiver(&motg->otg);
2518 if (ret) {
2519 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2520 goto free_irq;
2521 }
2522
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302523 if (motg->pdata->mode == USB_OTG &&
2524 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525 if (motg->pdata->pmic_id_irq) {
2526 ret = request_irq(motg->pdata->pmic_id_irq,
2527 msm_pmic_id_irq,
2528 IRQF_TRIGGER_RISING |
2529 IRQF_TRIGGER_FALLING,
2530 "msm_otg", motg);
2531 if (ret) {
2532 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2533 goto remove_otg;
2534 }
2535 } else {
2536 ret = -ENODEV;
2537 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2538 goto remove_otg;
2539 }
2540 }
2541
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302542 msm_hsusb_mhl_switch_enable(motg, 1);
2543
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302544 platform_set_drvdata(pdev, motg);
2545 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302547
Anji jonnalad270e2d2011-08-09 11:28:32 +05302548 ret = msm_otg_debugfs_init(motg);
2549 if (ret)
2550 dev_dbg(&pdev->dev, "mode debugfs file is"
2551 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302552
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002553 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2554 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2555
Amit Blay58b31472011-11-18 09:39:39 +02002556 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2557 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302558 (!(motg->pdata->mode == USB_OTG) ||
2559 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002560 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002561 ALLOW_PHY_RETENTION |
2562 ALLOW_PHY_COMP_DISABLE;
2563
Amit Blay58b31472011-11-18 09:39:39 +02002564 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2565 motg->caps = ALLOW_PHY_RETENTION;
2566 }
2567
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002568 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302569 pm_runtime_set_active(&pdev->dev);
2570 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302571
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302572 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002573
2574remove_otg:
2575 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302576free_irq:
2577 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002578destroy_wlock:
2579 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002580 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581 msm_hsusb_ldo_enable(motg, 0);
2582free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302583 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302585 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002586devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002587 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002588 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2589free_xo_handle:
2590 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302591free_regs:
2592 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002593put_pclk:
2594 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302595put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002596 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302597put_clk:
2598 clk_put(motg->clk);
2599put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002600 if (!IS_ERR(motg->phy_reset_clk))
2601 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302602free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302603 if (motg->pdata->swfi_latency)
2604 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302605 kfree(motg);
2606 return ret;
2607}
2608
2609static int __devexit msm_otg_remove(struct platform_device *pdev)
2610{
2611 struct msm_otg *motg = platform_get_drvdata(pdev);
2612 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302613 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302614
2615 if (otg->host || otg->gadget)
2616 return -EBUSY;
2617
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302618 if (pdev->dev.of_node)
2619 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002620 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2621 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302622 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302623 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302624 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302625
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302626 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302627
2628 device_init_wakeup(&pdev->dev, 0);
2629 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002630 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302631
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302632 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 if (motg->pdata->pmic_id_irq)
2634 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302635 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302636 free_irq(motg->irq, motg);
2637
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302638 /*
2639 * Put PHY in low power mode.
2640 */
2641 ulpi_read(otg, 0x14);
2642 ulpi_write(otg, 0x08, 0x09);
2643
2644 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2645 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2646 if (readl(USB_PORTSC) & PORTSC_PHCD)
2647 break;
2648 udelay(1);
2649 cnt++;
2650 }
2651 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2652 dev_err(otg->dev, "Unable to suspend PHY\n");
2653
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302654 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002655 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002656 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302658 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002659 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302660
2661 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302662 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302663
Amit Blay02eff132011-09-21 16:46:24 +03002664 if (!IS_ERR(motg->phy_reset_clk))
2665 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302666 clk_put(motg->pclk);
2667 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002668 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302669
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302670 if (motg->pdata->swfi_latency)
2671 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302672
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302673 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302674 return 0;
2675}
2676
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302677#ifdef CONFIG_PM_RUNTIME
2678static int msm_otg_runtime_idle(struct device *dev)
2679{
2680 struct msm_otg *motg = dev_get_drvdata(dev);
2681 struct otg_transceiver *otg = &motg->otg;
2682
2683 dev_dbg(dev, "OTG runtime idle\n");
2684
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302685 if (otg->state == OTG_STATE_UNDEFINED)
2686 return -EAGAIN;
2687 else
2688 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302689}
2690
2691static int msm_otg_runtime_suspend(struct device *dev)
2692{
2693 struct msm_otg *motg = dev_get_drvdata(dev);
2694
2695 dev_dbg(dev, "OTG runtime suspend\n");
2696 return msm_otg_suspend(motg);
2697}
2698
2699static int msm_otg_runtime_resume(struct device *dev)
2700{
2701 struct msm_otg *motg = dev_get_drvdata(dev);
2702
2703 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302704 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302705 return msm_otg_resume(motg);
2706}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302707#endif
2708
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302709#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302710static int msm_otg_pm_suspend(struct device *dev)
2711{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302712 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302713
2714 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302715
2716#ifdef CONFIG_PM_RUNTIME
2717 ret = pm_runtime_suspend(dev);
2718 if (ret > 0)
2719 ret = 0;
2720#else
2721 ret = msm_otg_suspend(dev_get_drvdata(dev));
2722#endif
2723 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302724}
2725
2726static int msm_otg_pm_resume(struct device *dev)
2727{
2728 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302729
2730 dev_dbg(dev, "OTG PM resume\n");
2731
Manu Gautamf284c052011-09-08 16:52:48 +05302732#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302733 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302734 * Do not resume hardware as part of system resume,
2735 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302736 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002737 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302738#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302739
Manu Gautamf284c052011-09-08 16:52:48 +05302740 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302741}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742#endif
2743
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302744#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302745static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302746 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2747 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2748 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302749};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302750#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302751
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302752static struct of_device_id msm_otg_dt_match[] = {
2753 { .compatible = "qcom,hsusb-otg",
2754 },
2755 {}
2756};
2757
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302758static struct platform_driver msm_otg_driver = {
2759 .remove = __devexit_p(msm_otg_remove),
2760 .driver = {
2761 .name = DRIVER_NAME,
2762 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302763#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302764 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302765#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302766 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302767 },
2768};
2769
2770static int __init msm_otg_init(void)
2771{
2772 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2773}
2774
2775static void __exit msm_otg_exit(void)
2776{
2777 platform_driver_unregister(&msm_otg_driver);
2778}
2779
2780module_init(msm_otg_init);
2781module_exit(msm_otg_exit);
2782
2783MODULE_LICENSE("GPL v2");
2784MODULE_DESCRIPTION("MSM USB transceiver driver");