blob: 00e4fda4d303524d130979ffd6f8bd7186a48d69 [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;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530612 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530613
614 if (atomic_read(&motg->in_lpm))
615 return 0;
616
617 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530618 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530620 * Chipidea 45-nm PHY suspend sequence:
621 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530622 * Interrupt Latch Register auto-clear feature is not present
623 * in all PHY versions. Latch register is clear on read type.
624 * Clear latch register to avoid spurious wakeup from
625 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530626 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530627 * PHY comparators are disabled when PHY enters into low power
628 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
629 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
630 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530631 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530632 * PLL is not turned off when PHY enters into low power mode (LPM).
633 * Disable PLL for maximum power savings.
634 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530635
636 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
637 ulpi_read(otg, 0x14);
638 if (pdata->otg_control == OTG_PHY_CONTROL)
639 ulpi_write(otg, 0x01, 0x30);
640 ulpi_write(otg, 0x08, 0x09);
641 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530642
643 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700644 * Turn off the OTG comparators, if depends on PMIC for
645 * VBUS and ID notifications.
646 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530647 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700648 ulpi_write(otg, OTG_COMP_DISABLE,
649 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
650 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
651 }
652
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530653 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530654 * PHY may take some time or even fail to enter into low power
655 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
656 * in failure case.
657 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530658 portsc = readl_relaxed(USB_PORTSC);
659 if (!(portsc & PORTSC_PHCD)) {
660 writel_relaxed(portsc | PORTSC_PHCD,
661 USB_PORTSC);
662 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
663 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
664 break;
665 udelay(1);
666 cnt++;
667 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530668 }
669
670 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
671 dev_err(otg->dev, "Unable to suspend PHY\n");
672 msm_otg_reset(otg);
673 enable_irq(motg->irq);
674 return -ETIMEDOUT;
675 }
676
677 /*
678 * PHY has capability to generate interrupt asynchronously in low
679 * power mode (LPM). This interrupt is level triggered. So USB IRQ
680 * line must be disabled till async interrupt enable bit is cleared
681 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
682 * block data communication from PHY.
683 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530684 cmd_val = readl_relaxed(USB_USBCMD);
685 if (host_bus_suspend)
686 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
687 else
688 cmd_val |= ULPI_STP_CTRL;
689 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530690
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530691 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200692 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
693 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
694 /* Enable PHY HV interrupts to wake MPM/Link */
695 phy_ctrl_val |=
696 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
697
698 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700699 motg->lpm_flags |= PHY_RETENTIONED;
700 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530701
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700702 /* Ensure that above operation is completed before turning off clocks */
703 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530704 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800705 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530706
Anji jonnala7da3f262011-12-02 17:22:14 -0800707 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800708 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800709
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530710 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700711 msm_hsusb_ldo_enable(motg, 0);
712 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530713 }
714
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530715 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700716 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530717 msm_hsusb_mhl_switch_enable(motg, 0);
718 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700719
720 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530721 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700722 if (motg->pdata->pmic_id_irq)
723 enable_irq_wake(motg->pdata->pmic_id_irq);
724 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530725 if (bus)
726 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
727
728 atomic_set(&motg->in_lpm, 1);
729 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700730 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530731
732 dev_info(otg->dev, "USB in low power mode\n");
733
734 return 0;
735}
736
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530737static int msm_otg_resume(struct msm_otg *motg)
738{
739 struct otg_transceiver *otg = &motg->otg;
740 struct usb_bus *bus = otg->host;
741 int cnt = 0;
742 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200743 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800744 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530745
746 if (!atomic_read(&motg->in_lpm))
747 return 0;
748
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800750
751 /* Vote for TCXO when waking up the phy */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800752 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800753 if (ret)
754 dev_err(otg->dev, "%s failed to vote for "
755 "TCXO D0 buffer%d\n", __func__, ret);
756
Manu Gautam5143b252012-01-05 19:25:23 -0800757 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200758
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
762 msm_hsusb_ldo_enable(motg, 1);
763 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
764 }
765
766 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530767 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530768 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200769 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
770 phy_ctrl_val |= PHY_RETEN;
771 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
772 /* Disable PHY HV interrupts */
773 phy_ctrl_val &=
774 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
775 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530777 }
778
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530779 temp = readl(USB_USBCMD);
780 temp &= ~ASYNC_INTR_CTRL;
781 temp &= ~ULPI_STP_CTRL;
782 writel(temp, USB_USBCMD);
783
784 /*
785 * PHY comes out of low power mode (LPM) in case of wakeup
786 * from asynchronous interrupt.
787 */
788 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
789 goto skip_phy_resume;
790
791 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
792 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
793 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
794 break;
795 udelay(1);
796 cnt++;
797 }
798
799 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
800 /*
801 * This is a fatal error. Reset the link and
802 * PHY. USB state can not be restored. Re-insertion
803 * of USB cable is the only way to get USB working.
804 */
805 dev_err(otg->dev, "Unable to resume USB."
806 "Re-plugin the cable\n");
807 msm_otg_reset(otg);
808 }
809
810skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700811 /* Turn on the OTG comparators on resume */
812 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
813 ulpi_write(otg, OTG_COMP_DISABLE,
814 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
815 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
816 }
817 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530818 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 if (motg->pdata->pmic_id_irq)
820 disable_irq_wake(motg->pdata->pmic_id_irq);
821 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 if (bus)
823 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
824
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530825 atomic_set(&motg->in_lpm, 0);
826
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530827 if (motg->async_int) {
828 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530829 enable_irq(motg->irq);
830 }
831
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530832 dev_info(otg->dev, "USB exited from low power mode\n");
833
834 return 0;
835}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530836#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530837
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530838static int msm_otg_notify_chg_type(struct msm_otg *motg)
839{
840 static int charger_type;
841 /*
842 * TODO
843 * Unify OTG driver charger types and power supply charger types
844 */
845 if (charger_type == motg->chg_type)
846 return 0;
847
848 if (motg->chg_type == USB_SDP_CHARGER)
849 charger_type = POWER_SUPPLY_TYPE_USB;
850 else if (motg->chg_type == USB_CDP_CHARGER)
851 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
852 else if (motg->chg_type == USB_DCP_CHARGER)
853 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
854 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
855 motg->chg_type == USB_ACA_A_CHARGER ||
856 motg->chg_type == USB_ACA_B_CHARGER ||
857 motg->chg_type == USB_ACA_C_CHARGER))
858 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
859 else
860 charger_type = POWER_SUPPLY_TYPE_BATTERY;
861
862 return pm8921_set_usb_power_supply_type(charger_type);
863}
864
Amit Blay0f7edf72012-01-15 10:11:27 +0200865static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
866{
867 struct power_supply *psy;
868
869 psy = power_supply_get_by_name("usb");
870 if (!psy)
871 goto psy_not_supported;
872
873 if (motg->cur_power == 0 && mA > 0) {
874 /* Enable charging */
875 if (power_supply_set_online(psy, true))
876 goto psy_not_supported;
877 } else if (motg->cur_power > 0 && mA == 0) {
878 /* Disable charging */
879 if (power_supply_set_online(psy, false))
880 goto psy_not_supported;
881 return 0;
882 }
883 /* Set max current limit */
884 if (power_supply_set_current_limit(psy, 1000*mA))
885 goto psy_not_supported;
886
887 return 0;
888
889psy_not_supported:
890 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
891 return -ENXIO;
892}
893
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530894static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
895{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530896 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
897 motg->chg_type == USB_ACA_A_CHARGER ||
898 motg->chg_type == USB_ACA_B_CHARGER ||
899 motg->chg_type == USB_ACA_C_CHARGER) &&
900 mA > IDEV_ACA_CHG_LIMIT)
901 mA = IDEV_ACA_CHG_LIMIT;
902
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530903 if (msm_otg_notify_chg_type(motg))
904 dev_err(motg->otg.dev,
905 "Failed notifying %d charger type to PMIC\n",
906 motg->chg_type);
907
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530908 if (motg->cur_power == mA)
909 return;
910
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530911 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +0200912
913 /*
914 * Use Power Supply API if supported, otherwise fallback
915 * to legacy pm8921 API.
916 */
917 if (msm_otg_notify_power_supply(motg, mA))
918 pm8921_charger_vbus_draw(mA);
919
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530920 motg->cur_power = mA;
921}
922
923static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
924{
925 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
926
927 /*
928 * Gadget driver uses set_power method to notify about the
929 * available current based on suspend/configured states.
930 *
931 * IDEV_CHG can be drawn irrespective of suspend/un-configured
932 * states when CDP/ACA is connected.
933 */
934 if (motg->chg_type == USB_SDP_CHARGER)
935 msm_otg_notify_charger(motg, mA);
936
937 return 0;
938}
939
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530940static void msm_otg_start_host(struct otg_transceiver *otg, int on)
941{
942 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
943 struct msm_otg_platform_data *pdata = motg->pdata;
944 struct usb_hcd *hcd;
945
946 if (!otg->host)
947 return;
948
949 hcd = bus_to_hcd(otg->host);
950
951 if (on) {
952 dev_dbg(otg->dev, "host on\n");
953
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530954 /*
955 * Some boards have a switch cotrolled by gpio
956 * to enable/disable internal HUB. Enable internal
957 * HUB before kicking the host.
958 */
959 if (pdata->setup_gpio)
960 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530961 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530962 } else {
963 dev_dbg(otg->dev, "host off\n");
964
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530965 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530966 /* HCD core reset all bits of PORTSC. select ULPI phy */
967 writel_relaxed(0x80000000, USB_PORTSC);
968
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530969 if (pdata->setup_gpio)
970 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530971 }
972}
973
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974static int msm_otg_usbdev_notify(struct notifier_block *self,
975 unsigned long action, void *priv)
976{
977 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530978 struct usb_device *udev = priv;
979
980 if (!aca_enabled())
981 goto out;
982
983 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
984 goto out;
985
986 if (udev->bus != motg->otg.host)
987 goto out;
988 /*
989 * Interested in devices connected directly to the root hub.
990 * ACA dock can supply IDEV_CHG irrespective devices connected
991 * on the accessory port.
992 */
993 if (!udev->parent || udev->parent->parent ||
994 motg->chg_type == USB_ACA_DOCK_CHARGER)
995 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700996
997 switch (action) {
998 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530999 usb_disable_autosuspend(udev);
1000 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001001 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 if (udev->actconfig)
1003 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1004 else
1005 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301006 break;
1007 case USB_DEVICE_REMOVE:
1008 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001009 break;
1010 default:
1011 break;
1012 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301013 if (test_bit(ID_A, &motg->inputs))
1014 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1015 motg->mA_port);
1016out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017 return NOTIFY_OK;
1018}
1019
Mayank Ranae3926882011-12-26 09:47:54 +05301020static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1021{
1022 int ret;
1023 static bool vbus_is_on;
1024
1025 if (vbus_is_on == on)
1026 return;
1027
1028 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301029 ret = motg->pdata->vbus_power(on);
1030 if (!ret)
1031 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301032 return;
1033 }
1034
1035 if (!vbus_otg) {
1036 pr_err("vbus_otg is NULL.");
1037 return;
1038 }
1039
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001040 /*
1041 * if entering host mode tell the charger to not draw any current
1042 * from usb - if exiting host mode let the charger draw current
1043 */
1044 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301045 if (on) {
1046 ret = regulator_enable(vbus_otg);
1047 if (ret) {
1048 pr_err("unable to enable vbus_otg\n");
1049 return;
1050 }
1051 vbus_is_on = true;
1052 } else {
1053 ret = regulator_disable(vbus_otg);
1054 if (ret) {
1055 pr_err("unable to disable vbus_otg\n");
1056 return;
1057 }
1058 vbus_is_on = false;
1059 }
1060}
1061
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301062static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1063{
1064 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1065 struct usb_hcd *hcd;
1066
1067 /*
1068 * Fail host registration if this board can support
1069 * only peripheral configuration.
1070 */
1071 if (motg->pdata->mode == USB_PERIPHERAL) {
1072 dev_info(otg->dev, "Host mode is not supported\n");
1073 return -ENODEV;
1074 }
1075
Mayank Ranae3926882011-12-26 09:47:54 +05301076 if (!motg->pdata->vbus_power && host) {
1077 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1078 if (IS_ERR(vbus_otg)) {
1079 pr_err("Unable to get vbus_otg\n");
1080 return -ENODEV;
1081 }
1082 }
1083
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301084 if (!host) {
1085 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301086 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001087 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301088 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301089 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301090 otg->host = NULL;
1091 otg->state = OTG_STATE_UNDEFINED;
1092 schedule_work(&motg->sm_work);
1093 } else {
1094 otg->host = NULL;
1095 }
1096
Mayank Ranae3926882011-12-26 09:47:54 +05301097 if (vbus_otg)
1098 regulator_put(vbus_otg);
1099
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301100 return 0;
1101 }
1102
1103 hcd = bus_to_hcd(host);
1104 hcd->power_budget = motg->pdata->power_budget;
1105
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001106 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1107 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301108 otg->host = host;
1109 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1110
1111 /*
1112 * Kick the state machine work, if peripheral is not supported
1113 * or peripheral is already registered with us.
1114 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301115 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1116 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301117 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301118 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301119
1120 return 0;
1121}
1122
1123static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1124{
1125 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1126 struct msm_otg_platform_data *pdata = motg->pdata;
1127
1128 if (!otg->gadget)
1129 return;
1130
1131 if (on) {
1132 dev_dbg(otg->dev, "gadget on\n");
1133 /*
1134 * Some boards have a switch cotrolled by gpio
1135 * to enable/disable internal HUB. Disable internal
1136 * HUB before kicking the gadget.
1137 */
1138 if (pdata->setup_gpio)
1139 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301140 /*
1141 * vote for minimum dma_latency to prevent idle
1142 * power collapse(pc) while running in peripheral mode.
1143 */
1144 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145 usb_gadget_vbus_connect(otg->gadget);
1146 } else {
1147 dev_dbg(otg->dev, "gadget off\n");
1148 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301149 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150 if (pdata->setup_gpio)
1151 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1152 }
1153
1154}
1155
1156static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1157 struct usb_gadget *gadget)
1158{
1159 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1160
1161 /*
1162 * Fail peripheral registration if this board can support
1163 * only host configuration.
1164 */
1165 if (motg->pdata->mode == USB_HOST) {
1166 dev_info(otg->dev, "Peripheral mode is not supported\n");
1167 return -ENODEV;
1168 }
1169
1170 if (!gadget) {
1171 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301172 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173 msm_otg_start_peripheral(otg, 0);
1174 otg->gadget = NULL;
1175 otg->state = OTG_STATE_UNDEFINED;
1176 schedule_work(&motg->sm_work);
1177 } else {
1178 otg->gadget = NULL;
1179 }
1180
1181 return 0;
1182 }
1183 otg->gadget = gadget;
1184 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1185
1186 /*
1187 * Kick the state machine work, if host is not supported
1188 * or host is already registered with us.
1189 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301190 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1191 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301192 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301193 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301194
1195 return 0;
1196}
1197
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001198static bool msm_chg_aca_detect(struct msm_otg *motg)
1199{
1200 struct otg_transceiver *otg = &motg->otg;
1201 u32 int_sts;
1202 bool ret = false;
1203
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301204 if (!aca_enabled())
1205 goto out;
1206
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1208 goto out;
1209
1210 int_sts = ulpi_read(otg, 0x87);
1211 switch (int_sts & 0x1C) {
1212 case 0x08:
1213 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1214 dev_dbg(otg->dev, "ID_A\n");
1215 motg->chg_type = USB_ACA_A_CHARGER;
1216 motg->chg_state = USB_CHG_STATE_DETECTED;
1217 clear_bit(ID_B, &motg->inputs);
1218 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301219 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001220 ret = true;
1221 }
1222 break;
1223 case 0x0C:
1224 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1225 dev_dbg(otg->dev, "ID_B\n");
1226 motg->chg_type = USB_ACA_B_CHARGER;
1227 motg->chg_state = USB_CHG_STATE_DETECTED;
1228 clear_bit(ID_A, &motg->inputs);
1229 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301230 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 ret = true;
1232 }
1233 break;
1234 case 0x10:
1235 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1236 dev_dbg(otg->dev, "ID_C\n");
1237 motg->chg_type = USB_ACA_C_CHARGER;
1238 motg->chg_state = USB_CHG_STATE_DETECTED;
1239 clear_bit(ID_A, &motg->inputs);
1240 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301241 set_bit(ID, &motg->inputs);
1242 ret = true;
1243 }
1244 break;
1245 case 0x04:
1246 if (test_and_clear_bit(ID, &motg->inputs)) {
1247 dev_dbg(otg->dev, "ID_GND\n");
1248 motg->chg_type = USB_INVALID_CHARGER;
1249 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1250 clear_bit(ID_A, &motg->inputs);
1251 clear_bit(ID_B, &motg->inputs);
1252 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001253 ret = true;
1254 }
1255 break;
1256 default:
1257 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1258 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301259 test_and_clear_bit(ID_C, &motg->inputs) |
1260 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001261 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301262 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 motg->chg_type = USB_INVALID_CHARGER;
1264 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1265 }
1266 }
1267out:
1268 return ret;
1269}
1270
1271static void msm_chg_enable_aca_det(struct msm_otg *motg)
1272{
1273 struct otg_transceiver *otg = &motg->otg;
1274
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301275 if (!aca_enabled())
1276 return;
1277
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001278 switch (motg->pdata->phy_type) {
1279 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301280 /* Disable ID_GND in link and PHY */
1281 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1282 OTGSC_IDIE), USB_OTGSC);
1283 ulpi_write(otg, 0x01, 0x0C);
1284 ulpi_write(otg, 0x10, 0x0F);
1285 ulpi_write(otg, 0x10, 0x12);
1286 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301288 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 break;
1290 default:
1291 break;
1292 }
1293}
1294
1295static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1296{
1297 struct otg_transceiver *otg = &motg->otg;
1298
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301299 if (!aca_enabled())
1300 return;
1301
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001302 switch (motg->pdata->phy_type) {
1303 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301304 /* Enable ACA Detection interrupt (on any RID change) */
1305 ulpi_write(otg, 0x01, 0x94);
1306 break;
1307 default:
1308 break;
1309 }
1310}
1311
1312static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1313{
1314 struct otg_transceiver *otg = &motg->otg;
1315
1316 if (!aca_enabled())
1317 return;
1318
1319 switch (motg->pdata->phy_type) {
1320 case SNPS_28NM_INTEGRATED_PHY:
1321 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001322 break;
1323 default:
1324 break;
1325 }
1326}
1327
1328static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1329{
1330 struct otg_transceiver *otg = &motg->otg;
1331 bool ret = false;
1332
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301333 if (!aca_enabled())
1334 return ret;
1335
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001336 switch (motg->pdata->phy_type) {
1337 case SNPS_28NM_INTEGRATED_PHY:
1338 if (ulpi_read(otg, 0x91) & 1) {
1339 dev_dbg(otg->dev, "RID change\n");
1340 ulpi_write(otg, 0x01, 0x92);
1341 ret = msm_chg_aca_detect(motg);
1342 }
1343 default:
1344 break;
1345 }
1346 return ret;
1347}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301348
1349static void msm_otg_id_timer_func(unsigned long data)
1350{
1351 struct msm_otg *motg = (struct msm_otg *) data;
1352
1353 if (!aca_enabled())
1354 return;
1355
1356 if (atomic_read(&motg->in_lpm)) {
1357 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1358 return;
1359 }
1360
1361 if (msm_chg_check_aca_intr(motg)) {
1362 dev_dbg(motg->otg.dev, "timer: aca work\n");
1363 schedule_work(&motg->sm_work);
1364 }
1365
1366 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1367 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1368}
1369
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301370static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1371{
1372 struct otg_transceiver *otg = &motg->otg;
1373 u32 chg_det;
1374 bool ret = false;
1375
1376 switch (motg->pdata->phy_type) {
1377 case CI_45NM_INTEGRATED_PHY:
1378 chg_det = ulpi_read(otg, 0x34);
1379 ret = chg_det & (1 << 4);
1380 break;
1381 case SNPS_28NM_INTEGRATED_PHY:
1382 chg_det = ulpi_read(otg, 0x87);
1383 ret = chg_det & 1;
1384 break;
1385 default:
1386 break;
1387 }
1388 return ret;
1389}
1390
1391static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1392{
1393 struct otg_transceiver *otg = &motg->otg;
1394 u32 chg_det;
1395
1396 switch (motg->pdata->phy_type) {
1397 case CI_45NM_INTEGRATED_PHY:
1398 chg_det = ulpi_read(otg, 0x34);
1399 /* Turn off charger block */
1400 chg_det |= ~(1 << 1);
1401 ulpi_write(otg, chg_det, 0x34);
1402 udelay(20);
1403 /* control chg block via ULPI */
1404 chg_det &= ~(1 << 3);
1405 ulpi_write(otg, chg_det, 0x34);
1406 /* put it in host mode for enabling D- source */
1407 chg_det &= ~(1 << 2);
1408 ulpi_write(otg, chg_det, 0x34);
1409 /* Turn on chg detect block */
1410 chg_det &= ~(1 << 1);
1411 ulpi_write(otg, chg_det, 0x34);
1412 udelay(20);
1413 /* enable chg detection */
1414 chg_det &= ~(1 << 0);
1415 ulpi_write(otg, chg_det, 0x34);
1416 break;
1417 case SNPS_28NM_INTEGRATED_PHY:
1418 /*
1419 * Configure DM as current source, DP as current sink
1420 * and enable battery charging comparators.
1421 */
1422 ulpi_write(otg, 0x8, 0x85);
1423 ulpi_write(otg, 0x2, 0x85);
1424 ulpi_write(otg, 0x1, 0x85);
1425 break;
1426 default:
1427 break;
1428 }
1429}
1430
1431static bool msm_chg_check_primary_det(struct msm_otg *motg)
1432{
1433 struct otg_transceiver *otg = &motg->otg;
1434 u32 chg_det;
1435 bool ret = false;
1436
1437 switch (motg->pdata->phy_type) {
1438 case CI_45NM_INTEGRATED_PHY:
1439 chg_det = ulpi_read(otg, 0x34);
1440 ret = chg_det & (1 << 4);
1441 break;
1442 case SNPS_28NM_INTEGRATED_PHY:
1443 chg_det = ulpi_read(otg, 0x87);
1444 ret = chg_det & 1;
1445 break;
1446 default:
1447 break;
1448 }
1449 return ret;
1450}
1451
1452static void msm_chg_enable_primary_det(struct msm_otg *motg)
1453{
1454 struct otg_transceiver *otg = &motg->otg;
1455 u32 chg_det;
1456
1457 switch (motg->pdata->phy_type) {
1458 case CI_45NM_INTEGRATED_PHY:
1459 chg_det = ulpi_read(otg, 0x34);
1460 /* enable chg detection */
1461 chg_det &= ~(1 << 0);
1462 ulpi_write(otg, chg_det, 0x34);
1463 break;
1464 case SNPS_28NM_INTEGRATED_PHY:
1465 /*
1466 * Configure DP as current source, DM as current sink
1467 * and enable battery charging comparators.
1468 */
1469 ulpi_write(otg, 0x2, 0x85);
1470 ulpi_write(otg, 0x1, 0x85);
1471 break;
1472 default:
1473 break;
1474 }
1475}
1476
1477static bool msm_chg_check_dcd(struct msm_otg *motg)
1478{
1479 struct otg_transceiver *otg = &motg->otg;
1480 u32 line_state;
1481 bool ret = false;
1482
1483 switch (motg->pdata->phy_type) {
1484 case CI_45NM_INTEGRATED_PHY:
1485 line_state = ulpi_read(otg, 0x15);
1486 ret = !(line_state & 1);
1487 break;
1488 case SNPS_28NM_INTEGRATED_PHY:
1489 line_state = ulpi_read(otg, 0x87);
1490 ret = line_state & 2;
1491 break;
1492 default:
1493 break;
1494 }
1495 return ret;
1496}
1497
1498static void msm_chg_disable_dcd(struct msm_otg *motg)
1499{
1500 struct otg_transceiver *otg = &motg->otg;
1501 u32 chg_det;
1502
1503 switch (motg->pdata->phy_type) {
1504 case CI_45NM_INTEGRATED_PHY:
1505 chg_det = ulpi_read(otg, 0x34);
1506 chg_det &= ~(1 << 5);
1507 ulpi_write(otg, chg_det, 0x34);
1508 break;
1509 case SNPS_28NM_INTEGRATED_PHY:
1510 ulpi_write(otg, 0x10, 0x86);
1511 break;
1512 default:
1513 break;
1514 }
1515}
1516
1517static void msm_chg_enable_dcd(struct msm_otg *motg)
1518{
1519 struct otg_transceiver *otg = &motg->otg;
1520 u32 chg_det;
1521
1522 switch (motg->pdata->phy_type) {
1523 case CI_45NM_INTEGRATED_PHY:
1524 chg_det = ulpi_read(otg, 0x34);
1525 /* Turn on D+ current source */
1526 chg_det |= (1 << 5);
1527 ulpi_write(otg, chg_det, 0x34);
1528 break;
1529 case SNPS_28NM_INTEGRATED_PHY:
1530 /* Data contact detection enable */
1531 ulpi_write(otg, 0x10, 0x85);
1532 break;
1533 default:
1534 break;
1535 }
1536}
1537
1538static void msm_chg_block_on(struct msm_otg *motg)
1539{
1540 struct otg_transceiver *otg = &motg->otg;
1541 u32 func_ctrl, chg_det;
1542
1543 /* put the controller in non-driving mode */
1544 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1545 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1546 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1547 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1548
1549 switch (motg->pdata->phy_type) {
1550 case CI_45NM_INTEGRATED_PHY:
1551 chg_det = ulpi_read(otg, 0x34);
1552 /* control chg block via ULPI */
1553 chg_det &= ~(1 << 3);
1554 ulpi_write(otg, chg_det, 0x34);
1555 /* Turn on chg detect block */
1556 chg_det &= ~(1 << 1);
1557 ulpi_write(otg, chg_det, 0x34);
1558 udelay(20);
1559 break;
1560 case SNPS_28NM_INTEGRATED_PHY:
1561 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301562 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301563 /* Clear alt interrupt latch and enable bits */
1564 ulpi_write(otg, 0x1F, 0x92);
1565 ulpi_write(otg, 0x1F, 0x95);
1566 udelay(100);
1567 break;
1568 default:
1569 break;
1570 }
1571}
1572
1573static void msm_chg_block_off(struct msm_otg *motg)
1574{
1575 struct otg_transceiver *otg = &motg->otg;
1576 u32 func_ctrl, chg_det;
1577
1578 switch (motg->pdata->phy_type) {
1579 case CI_45NM_INTEGRATED_PHY:
1580 chg_det = ulpi_read(otg, 0x34);
1581 /* Turn off charger block */
1582 chg_det |= ~(1 << 1);
1583 ulpi_write(otg, chg_det, 0x34);
1584 break;
1585 case SNPS_28NM_INTEGRATED_PHY:
1586 /* Clear charger detecting control bits */
1587 ulpi_write(otg, 0x3F, 0x86);
1588 /* Clear alt interrupt latch and enable bits */
1589 ulpi_write(otg, 0x1F, 0x92);
1590 ulpi_write(otg, 0x1F, 0x95);
1591 break;
1592 default:
1593 break;
1594 }
1595
1596 /* put the controller in normal mode */
1597 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1598 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1599 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1600 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1601}
1602
Anji jonnalad270e2d2011-08-09 11:28:32 +05301603static const char *chg_to_string(enum usb_chg_type chg_type)
1604{
1605 switch (chg_type) {
1606 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1607 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1608 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1609 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1610 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1611 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1612 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1613 default: return "INVALID_CHARGER";
1614 }
1615}
1616
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301617#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1618#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1619#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1620#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1621static void msm_chg_detect_work(struct work_struct *w)
1622{
1623 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1624 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301625 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301626 unsigned long delay;
1627
1628 dev_dbg(otg->dev, "chg detection work\n");
1629 switch (motg->chg_state) {
1630 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301631 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301632 if (motg->pdata->enable_dcd)
1633 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001634 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301635 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1636 motg->dcd_retries = 0;
1637 delay = MSM_CHG_DCD_POLL_TIME;
1638 break;
1639 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 is_aca = msm_chg_aca_detect(motg);
1641 if (is_aca) {
1642 /*
1643 * ID_A can be ACA dock too. continue
1644 * primary detection after DCD.
1645 */
1646 if (test_bit(ID_A, &motg->inputs)) {
1647 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1648 } else {
1649 delay = 0;
1650 break;
1651 }
1652 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301653 if (motg->pdata->enable_dcd)
1654 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301655 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1656 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301657 if (motg->pdata->enable_dcd)
1658 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301659 msm_chg_enable_primary_det(motg);
1660 delay = MSM_CHG_PRIMARY_DET_TIME;
1661 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1662 } else {
1663 delay = MSM_CHG_DCD_POLL_TIME;
1664 }
1665 break;
1666 case USB_CHG_STATE_DCD_DONE:
1667 vout = msm_chg_check_primary_det(motg);
1668 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301669 if (test_bit(ID_A, &motg->inputs)) {
1670 motg->chg_type = USB_ACA_DOCK_CHARGER;
1671 motg->chg_state = USB_CHG_STATE_DETECTED;
1672 delay = 0;
1673 break;
1674 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301675 msm_chg_enable_secondary_det(motg);
1676 delay = MSM_CHG_SECONDARY_DET_TIME;
1677 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1678 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301679 if (test_bit(ID_A, &motg->inputs)) {
1680 motg->chg_type = USB_ACA_A_CHARGER;
1681 motg->chg_state = USB_CHG_STATE_DETECTED;
1682 delay = 0;
1683 break;
1684 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301685 motg->chg_type = USB_SDP_CHARGER;
1686 motg->chg_state = USB_CHG_STATE_DETECTED;
1687 delay = 0;
1688 }
1689 break;
1690 case USB_CHG_STATE_PRIMARY_DONE:
1691 vout = msm_chg_check_secondary_det(motg);
1692 if (vout)
1693 motg->chg_type = USB_DCP_CHARGER;
1694 else
1695 motg->chg_type = USB_CDP_CHARGER;
1696 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1697 /* fall through */
1698 case USB_CHG_STATE_SECONDARY_DONE:
1699 motg->chg_state = USB_CHG_STATE_DETECTED;
1700 case USB_CHG_STATE_DETECTED:
1701 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001702 msm_chg_enable_aca_det(motg);
1703 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301704 dev_dbg(otg->dev, "chg_type = %s\n",
1705 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301706 schedule_work(&motg->sm_work);
1707 return;
1708 default:
1709 return;
1710 }
1711
1712 schedule_delayed_work(&motg->chg_work, delay);
1713}
1714
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301715/*
1716 * We support OTG, Peripheral only and Host only configurations. In case
1717 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1718 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1719 * enabled when switch is controlled by user and default mode is supplied
1720 * by board file, which can be changed by userspace later.
1721 */
1722static void msm_otg_init_sm(struct msm_otg *motg)
1723{
1724 struct msm_otg_platform_data *pdata = motg->pdata;
1725 u32 otgsc = readl(USB_OTGSC);
1726
1727 switch (pdata->mode) {
1728 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001729 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301730 if (pdata->default_mode == USB_HOST) {
1731 clear_bit(ID, &motg->inputs);
1732 } else if (pdata->default_mode == USB_PERIPHERAL) {
1733 set_bit(ID, &motg->inputs);
1734 set_bit(B_SESS_VLD, &motg->inputs);
1735 } else {
1736 set_bit(ID, &motg->inputs);
1737 clear_bit(B_SESS_VLD, &motg->inputs);
1738 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301739 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1740 if (otgsc & OTGSC_ID)
1741 set_bit(ID, &motg->inputs);
1742 else
1743 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001744 if (otgsc & OTGSC_BSV)
1745 set_bit(B_SESS_VLD, &motg->inputs);
1746 else
1747 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301748 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301749 if (pdata->pmic_id_irq) {
1750 if (irq_read_line(pdata->pmic_id_irq))
1751 set_bit(ID, &motg->inputs);
1752 else
1753 clear_bit(ID, &motg->inputs);
1754 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301755 /*
1756 * VBUS initial state is reported after PMIC
1757 * driver initialization. Wait for it.
1758 */
1759 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301760 }
1761 break;
1762 case USB_HOST:
1763 clear_bit(ID, &motg->inputs);
1764 break;
1765 case USB_PERIPHERAL:
1766 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301767 if (pdata->otg_control == OTG_PHY_CONTROL) {
1768 if (otgsc & OTGSC_BSV)
1769 set_bit(B_SESS_VLD, &motg->inputs);
1770 else
1771 clear_bit(B_SESS_VLD, &motg->inputs);
1772 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1773 /*
1774 * VBUS initial state is reported after PMIC
1775 * driver initialization. Wait for it.
1776 */
1777 wait_for_completion(&pmic_vbus_init);
1778 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301779 break;
1780 default:
1781 break;
1782 }
1783}
1784
1785static void msm_otg_sm_work(struct work_struct *w)
1786{
1787 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1788 struct otg_transceiver *otg = &motg->otg;
1789
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301790 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301791 switch (otg->state) {
1792 case OTG_STATE_UNDEFINED:
1793 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1794 msm_otg_reset(otg);
1795 msm_otg_init_sm(motg);
1796 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301797 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1798 test_bit(ID, &motg->inputs)) {
1799 pm_runtime_put_noidle(otg->dev);
1800 pm_runtime_suspend(otg->dev);
1801 break;
1802 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301803 /* FALL THROUGH */
1804 case OTG_STATE_B_IDLE:
1805 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001806 if ((!test_bit(ID, &motg->inputs) ||
1807 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1809 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301810 IDEV_ACA_CHG_MAX);
1811 else if (test_bit(ID_A, &motg->inputs))
1812 msm_otg_notify_charger(motg,
1813 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301814 else
1815 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301816 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301817 /*
1818 * Link can not generate PHY_ALT interrupt
1819 * in host mode when no device is attached
1820 * to the port. It is also observed PHY_ALT
1821 * interrupt missing upon Micro-A cable disconnect.
1822 * Hence disable PHY_ALT interrupt and perform
1823 * polling to detect RID change.
1824 */
1825 msm_chg_enable_aca_det(motg);
1826 msm_chg_disable_aca_intr(motg);
1827 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301828 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301829 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1830 switch (motg->chg_state) {
1831 case USB_CHG_STATE_UNDEFINED:
1832 msm_chg_detect_work(&motg->chg_work.work);
1833 break;
1834 case USB_CHG_STATE_DETECTED:
1835 switch (motg->chg_type) {
1836 case USB_DCP_CHARGER:
1837 msm_otg_notify_charger(motg,
1838 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301839 pm_runtime_put_noidle(otg->dev);
1840 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301841 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301842 case USB_ACA_B_CHARGER:
1843 msm_otg_notify_charger(motg,
1844 IDEV_ACA_CHG_MAX);
1845 /*
1846 * (ID_B --> ID_C) PHY_ALT interrupt can
1847 * not be detected in LPM.
1848 */
1849 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301850 case USB_CDP_CHARGER:
1851 msm_otg_notify_charger(motg,
1852 IDEV_CHG_MAX);
1853 msm_otg_start_peripheral(otg, 1);
1854 otg->state = OTG_STATE_B_PERIPHERAL;
1855 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301856 case USB_ACA_C_CHARGER:
1857 msm_otg_notify_charger(motg,
1858 IDEV_ACA_CHG_MAX);
1859 msm_otg_start_peripheral(otg, 1);
1860 otg->state = OTG_STATE_B_PERIPHERAL;
1861 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301862 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301863 msm_otg_start_peripheral(otg, 1);
1864 otg->state = OTG_STATE_B_PERIPHERAL;
1865 break;
1866 default:
1867 break;
1868 }
1869 break;
1870 default:
1871 break;
1872 }
1873 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301874 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301875 msm_otg_notify_charger(motg, 0);
1876 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1877 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301878 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301879 pm_runtime_put_noidle(otg->dev);
1880 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301881 }
1882 break;
1883 case OTG_STATE_B_PERIPHERAL:
1884 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1885 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001886 !test_bit(ID, &motg->inputs) ||
1887 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301888 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001890 schedule_work(w);
1891 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301892 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 }
1894 break;
1895 case OTG_STATE_A_HOST:
1896 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1897 if (test_bit(ID, &motg->inputs) &&
1898 !test_bit(ID_A, &motg->inputs)) {
1899 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301900 msm_hsusb_vbus_power(motg, 0);
1901 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301902 /*
1903 * Exit point of host mode.
1904 *
1905 * 1. Micro-A cable disconnect: Just schedule
1906 * the work. PHY is reset in B_IDLE and LPM
1907 * is allowed.
1908 * 2. ID_GND --> ID_B: No need to reset the PHY.
1909 * HCD core clears all PORTSC bits and initializes
1910 * the controller to host mode in remove_hcd.
1911 * Restore PORTSC transceiver select bits (ULPI)
1912 * and reset the controller to change MODE bits.
1913 * PHY_ALT interrupt can not occur in host mode.
1914 */
1915 del_timer_sync(&motg->id_timer);
1916 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1917 msm_otg_link_reset(motg);
1918 msm_chg_enable_aca_intr(motg);
1919 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301920 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301921 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301923 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301925 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001927 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301928 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301929 }
1930 break;
1931 default:
1932 break;
1933 }
1934}
1935
1936static irqreturn_t msm_otg_irq(int irq, void *data)
1937{
1938 struct msm_otg *motg = data;
1939 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001940 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301941
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301942 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301943 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301944 disable_irq_nosync(irq);
1945 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301946 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301947 return IRQ_HANDLED;
1948 }
1949
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 usbsts = readl(USB_USBSTS);
1951 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301952 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301954 if (msm_chg_check_aca_intr(motg)) {
1955 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301957 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 return IRQ_HANDLED;
1959 }
1960
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301961 otgsc = readl(USB_OTGSC);
1962 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1963 return IRQ_NONE;
1964
1965 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301966 if (otgsc & OTGSC_ID) {
1967 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301968 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301969 } else {
1970 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301971 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301972 msm_chg_enable_aca_det(motg);
1973 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301975 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301976 if (otgsc & OTGSC_BSV) {
1977 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301978 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301979 } else {
1980 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301981 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301982 msm_chg_check_aca_intr(motg);
1983 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001984 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301985 }
1986
1987 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001988 return IRQ_HANDLED;
1989}
1990
1991static void msm_otg_set_vbus_state(int online)
1992{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301993 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001994 struct msm_otg *motg = the_msm_otg;
1995
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301996 if (online) {
1997 pr_debug("PMIC: BSV set\n");
1998 set_bit(B_SESS_VLD, &motg->inputs);
1999 } else {
2000 pr_debug("PMIC: BSV clear\n");
2001 clear_bit(B_SESS_VLD, &motg->inputs);
2002 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302004 if (!init) {
2005 init = true;
2006 complete(&pmic_vbus_init);
2007 pr_debug("PMIC: BSV init complete\n");
2008 return;
2009 }
2010
2011 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002012}
2013
2014static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2015{
2016 struct msm_otg *motg = data;
2017
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302018 if (aca_id_turned_on)
2019 return IRQ_HANDLED;
2020
2021 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2022 pr_debug("PMIC: ID set\n");
2023 set_bit(ID, &motg->inputs);
2024 } else {
2025 pr_debug("PMIC: ID clear\n");
2026 clear_bit(ID, &motg->inputs);
2027 }
2028
2029 if (motg->otg.state != OTG_STATE_UNDEFINED)
2030 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002031
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302032 return IRQ_HANDLED;
2033}
2034
2035static int msm_otg_mode_show(struct seq_file *s, void *unused)
2036{
2037 struct msm_otg *motg = s->private;
2038 struct otg_transceiver *otg = &motg->otg;
2039
2040 switch (otg->state) {
2041 case OTG_STATE_A_HOST:
2042 seq_printf(s, "host\n");
2043 break;
2044 case OTG_STATE_B_PERIPHERAL:
2045 seq_printf(s, "peripheral\n");
2046 break;
2047 default:
2048 seq_printf(s, "none\n");
2049 break;
2050 }
2051
2052 return 0;
2053}
2054
2055static int msm_otg_mode_open(struct inode *inode, struct file *file)
2056{
2057 return single_open(file, msm_otg_mode_show, inode->i_private);
2058}
2059
2060static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2061 size_t count, loff_t *ppos)
2062{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302063 struct seq_file *s = file->private_data;
2064 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302065 char buf[16];
2066 struct otg_transceiver *otg = &motg->otg;
2067 int status = count;
2068 enum usb_mode_type req_mode;
2069
2070 memset(buf, 0x00, sizeof(buf));
2071
2072 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2073 status = -EFAULT;
2074 goto out;
2075 }
2076
2077 if (!strncmp(buf, "host", 4)) {
2078 req_mode = USB_HOST;
2079 } else if (!strncmp(buf, "peripheral", 10)) {
2080 req_mode = USB_PERIPHERAL;
2081 } else if (!strncmp(buf, "none", 4)) {
2082 req_mode = USB_NONE;
2083 } else {
2084 status = -EINVAL;
2085 goto out;
2086 }
2087
2088 switch (req_mode) {
2089 case USB_NONE:
2090 switch (otg->state) {
2091 case OTG_STATE_A_HOST:
2092 case OTG_STATE_B_PERIPHERAL:
2093 set_bit(ID, &motg->inputs);
2094 clear_bit(B_SESS_VLD, &motg->inputs);
2095 break;
2096 default:
2097 goto out;
2098 }
2099 break;
2100 case USB_PERIPHERAL:
2101 switch (otg->state) {
2102 case OTG_STATE_B_IDLE:
2103 case OTG_STATE_A_HOST:
2104 set_bit(ID, &motg->inputs);
2105 set_bit(B_SESS_VLD, &motg->inputs);
2106 break;
2107 default:
2108 goto out;
2109 }
2110 break;
2111 case USB_HOST:
2112 switch (otg->state) {
2113 case OTG_STATE_B_IDLE:
2114 case OTG_STATE_B_PERIPHERAL:
2115 clear_bit(ID, &motg->inputs);
2116 break;
2117 default:
2118 goto out;
2119 }
2120 break;
2121 default:
2122 goto out;
2123 }
2124
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302125 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302126 schedule_work(&motg->sm_work);
2127out:
2128 return status;
2129}
2130
2131const struct file_operations msm_otg_mode_fops = {
2132 .open = msm_otg_mode_open,
2133 .read = seq_read,
2134 .write = msm_otg_mode_write,
2135 .llseek = seq_lseek,
2136 .release = single_release,
2137};
2138
Anji jonnalad270e2d2011-08-09 11:28:32 +05302139static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2140{
2141 struct msm_otg *motg = s->private;
2142
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302143 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302144 return 0;
2145}
2146
2147static int msm_otg_chg_open(struct inode *inode, struct file *file)
2148{
2149 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2150}
2151
2152const struct file_operations msm_otg_chg_fops = {
2153 .open = msm_otg_chg_open,
2154 .read = seq_read,
2155 .llseek = seq_lseek,
2156 .release = single_release,
2157};
2158
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302159static int msm_otg_aca_show(struct seq_file *s, void *unused)
2160{
2161 if (debug_aca_enabled)
2162 seq_printf(s, "enabled\n");
2163 else
2164 seq_printf(s, "disabled\n");
2165
2166 return 0;
2167}
2168
2169static int msm_otg_aca_open(struct inode *inode, struct file *file)
2170{
2171 return single_open(file, msm_otg_aca_show, inode->i_private);
2172}
2173
2174static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2175 size_t count, loff_t *ppos)
2176{
2177 char buf[8];
2178
2179 memset(buf, 0x00, sizeof(buf));
2180
2181 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2182 return -EFAULT;
2183
2184 if (!strncmp(buf, "enable", 6))
2185 debug_aca_enabled = true;
2186 else
2187 debug_aca_enabled = false;
2188
2189 return count;
2190}
2191
2192const struct file_operations msm_otg_aca_fops = {
2193 .open = msm_otg_aca_open,
2194 .read = seq_read,
2195 .write = msm_otg_aca_write,
2196 .llseek = seq_lseek,
2197 .release = single_release,
2198};
2199
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302200static struct dentry *msm_otg_dbg_root;
2201static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302202static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302203static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302204
2205static int msm_otg_debugfs_init(struct msm_otg *motg)
2206{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302207
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302208 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2209
2210 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2211 return -ENODEV;
2212
Anji jonnalad270e2d2011-08-09 11:28:32 +05302213 if (motg->pdata->mode == USB_OTG &&
2214 motg->pdata->otg_control == OTG_USER_CONTROL) {
2215
2216 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2217 S_IWUSR, msm_otg_dbg_root, motg,
2218 &msm_otg_mode_fops);
2219
2220 if (!msm_otg_dbg_mode) {
2221 debugfs_remove(msm_otg_dbg_root);
2222 msm_otg_dbg_root = NULL;
2223 return -ENODEV;
2224 }
2225 }
2226
2227 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2228 msm_otg_dbg_root, motg,
2229 &msm_otg_chg_fops);
2230
2231 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302232 debugfs_remove_recursive(msm_otg_dbg_root);
2233 return -ENODEV;
2234 }
2235
2236 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2237 msm_otg_dbg_root, motg,
2238 &msm_otg_aca_fops);
2239
2240 if (!msm_otg_dbg_aca) {
2241 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302242 return -ENODEV;
2243 }
2244
2245 return 0;
2246}
2247
2248static void msm_otg_debugfs_cleanup(void)
2249{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302250 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302251}
2252
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302253static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2254static struct platform_device *msm_otg_add_pdev(
2255 struct platform_device *ofdev, const char *name)
2256{
2257 struct platform_device *pdev;
2258 const struct resource *res = ofdev->resource;
2259 unsigned int num = ofdev->num_resources;
2260 int retval;
2261
2262 pdev = platform_device_alloc(name, -1);
2263 if (!pdev) {
2264 retval = -ENOMEM;
2265 goto error;
2266 }
2267
2268 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2269 pdev->dev.dma_mask = &msm_otg_dma_mask;
2270
2271 if (num) {
2272 retval = platform_device_add_resources(pdev, res, num);
2273 if (retval)
2274 goto error;
2275 }
2276
2277 retval = platform_device_add(pdev);
2278 if (retval)
2279 goto error;
2280
2281 return pdev;
2282
2283error:
2284 platform_device_put(pdev);
2285 return ERR_PTR(retval);
2286}
2287
2288static int msm_otg_setup_devices(struct platform_device *ofdev,
2289 enum usb_mode_type mode, bool init)
2290{
2291 const char *gadget_name = "msm_hsusb";
2292 const char *host_name = "msm_hsusb_host";
2293 static struct platform_device *gadget_pdev;
2294 static struct platform_device *host_pdev;
2295 int retval = 0;
2296
2297 if (!init) {
2298 if (gadget_pdev)
2299 platform_device_unregister(gadget_pdev);
2300 if (host_pdev)
2301 platform_device_unregister(host_pdev);
2302 return 0;
2303 }
2304
2305 switch (mode) {
2306 case USB_OTG:
2307 /* fall through */
2308 case USB_PERIPHERAL:
2309 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2310 if (IS_ERR(gadget_pdev)) {
2311 retval = PTR_ERR(gadget_pdev);
2312 break;
2313 }
2314 if (mode == USB_PERIPHERAL)
2315 break;
2316 /* fall through */
2317 case USB_HOST:
2318 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2319 if (IS_ERR(host_pdev)) {
2320 retval = PTR_ERR(host_pdev);
2321 if (mode == USB_OTG)
2322 platform_device_unregister(gadget_pdev);
2323 }
2324 break;
2325 default:
2326 break;
2327 }
2328
2329 return retval;
2330}
2331
2332struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2333{
2334 struct device_node *node = pdev->dev.of_node;
2335 struct msm_otg_platform_data *pdata;
2336 int len = 0;
2337
2338 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2339 if (!pdata) {
2340 pr_err("unable to allocate platform data\n");
2341 return NULL;
2342 }
2343 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2344 if (len) {
2345 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2346 if (!pdata->phy_init_seq)
2347 return NULL;
2348 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2349 pdata->phy_init_seq,
2350 len/sizeof(*pdata->phy_init_seq));
2351 }
2352 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2353 &pdata->power_budget);
2354 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2355 &pdata->mode);
2356 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2357 &pdata->otg_control);
2358 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2359 &pdata->default_mode);
2360 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2361 &pdata->phy_type);
2362 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2363 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302364 return pdata;
2365}
2366
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302367static int __init msm_otg_probe(struct platform_device *pdev)
2368{
2369 int ret = 0;
2370 struct resource *res;
2371 struct msm_otg *motg;
2372 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302373 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302374
2375 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302376
2377 if (pdev->dev.of_node) {
2378 dev_dbg(&pdev->dev, "device tree enabled\n");
2379 pdata = msm_otg_dt_to_pdata(pdev);
2380 if (!pdata)
2381 return -ENOMEM;
2382 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2383 if (ret) {
2384 dev_err(&pdev->dev, "devices setup failed\n");
2385 return ret;
2386 }
2387 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302388 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2389 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302390 } else {
2391 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302392 }
2393
2394 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2395 if (!motg) {
2396 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2397 return -ENOMEM;
2398 }
2399
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002400 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302401 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302402 otg = &motg->otg;
2403 otg->dev = &pdev->dev;
2404
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302405 /*
2406 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2407 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2408 * PMIC can detect ACA ID_GND and generate an interrupt.
2409 */
2410 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2411 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2412 ret = -EINVAL;
2413 goto free_motg;
2414 }
2415
Ofir Cohen4da266f2012-01-03 10:19:29 +02002416 /* initialize reset counter */
2417 motg->reset_counter = 0;
2418
Amit Blay02eff132011-09-21 16:46:24 +03002419 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002420 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002421 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002422 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302423
Manu Gautam5143b252012-01-05 19:25:23 -08002424 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302425 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002426 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302427 ret = PTR_ERR(motg->clk);
2428 goto put_phy_reset_clk;
2429 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302430 clk_set_rate(motg->clk, 60000000);
2431
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302432 /* pm qos request to prevent apps idle power collapse */
2433 if (motg->pdata->swfi_latency)
2434 pm_qos_add_request(&motg->pm_qos_req_dma,
2435 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002436
Anji jonnala0f73cac2011-05-04 10:19:46 +05302437 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002438 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302439 * CORE CLK must be running at >55Mhz for correct HSUSB
2440 * operation and USB core cannot tolerate frequency changes on
2441 * CORE CLK. For such USB cores, vote for maximum clk frequency
2442 * on pclk source
2443 */
Manu Gautam5143b252012-01-05 19:25:23 -08002444 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2445 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302446 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002447 dev_err(&pdev->dev, "failed to get core_clk\n");
2448 ret = PTR_ERR(motg->clk);
2449 goto put_clk;
2450 }
2451 clk_set_rate(motg->core_clk, INT_MAX);
2452
2453 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2454 if (IS_ERR(motg->pclk)) {
2455 dev_err(&pdev->dev, "failed to get iface_clk\n");
2456 ret = PTR_ERR(motg->pclk);
2457 goto put_core_clk;
2458 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302459
2460 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2461 if (!res) {
2462 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2463 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002464 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302465 }
2466
2467 motg->regs = ioremap(res->start, resource_size(res));
2468 if (!motg->regs) {
2469 dev_err(&pdev->dev, "ioremap failed\n");
2470 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002471 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302472 }
2473 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2474
2475 motg->irq = platform_get_irq(pdev, 0);
2476 if (!motg->irq) {
2477 dev_err(&pdev->dev, "platform_get_irq failed\n");
2478 ret = -ENODEV;
2479 goto free_regs;
2480 }
2481
Stephen Boyd7dd22662012-01-26 16:09:31 -08002482 motg->xo_handle = clk_get(&pdev->dev, "xo");
Anji jonnala7da3f262011-12-02 17:22:14 -08002483 if (IS_ERR(motg->xo_handle)) {
2484 dev_err(&pdev->dev, "%s not able to get the handle "
2485 "to vote for TCXO D0 buffer\n", __func__);
2486 ret = PTR_ERR(motg->xo_handle);
2487 goto free_regs;
2488 }
2489
Stephen Boyd7dd22662012-01-26 16:09:31 -08002490 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08002491 if (ret) {
2492 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2493 "D0 buffer%d\n", __func__, ret);
2494 goto free_xo_handle;
2495 }
2496
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302497 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302498
2499 ret = msm_hsusb_init_vddcx(motg, 1);
2500 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002502 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302503 }
2504
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002505 ret = msm_hsusb_config_vddcx(1);
2506 if (ret) {
2507 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2508 goto free_init_vddcx;
2509 }
2510
Anji jonnala11aa5c42011-05-04 10:19:48 +05302511 ret = msm_hsusb_ldo_init(motg, 1);
2512 if (ret) {
2513 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002514 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302515 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002516
2517 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302518 if (ret) {
2519 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002520 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302521 }
Manu Gautam5143b252012-01-05 19:25:23 -08002522 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302523
2524 writel(0, USB_USBINTR);
2525 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002526 /* Ensure that above STOREs are completed before enabling interrupts */
2527 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302528
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302530 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302531 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302532 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2533 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302534 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2535 "msm_otg", motg);
2536 if (ret) {
2537 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002538 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302539 }
2540
2541 otg->init = msm_otg_reset;
2542 otg->set_host = msm_otg_set_host;
2543 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302544 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302545 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302546
2547 otg->io_ops = &msm_otg_io_ops;
2548
2549 ret = otg_set_transceiver(&motg->otg);
2550 if (ret) {
2551 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2552 goto free_irq;
2553 }
2554
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302555 if (motg->pdata->mode == USB_OTG &&
2556 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002557 if (motg->pdata->pmic_id_irq) {
2558 ret = request_irq(motg->pdata->pmic_id_irq,
2559 msm_pmic_id_irq,
2560 IRQF_TRIGGER_RISING |
2561 IRQF_TRIGGER_FALLING,
2562 "msm_otg", motg);
2563 if (ret) {
2564 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2565 goto remove_otg;
2566 }
2567 } else {
2568 ret = -ENODEV;
2569 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2570 goto remove_otg;
2571 }
2572 }
2573
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302574 msm_hsusb_mhl_switch_enable(motg, 1);
2575
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302576 platform_set_drvdata(pdev, motg);
2577 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002578 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302579
Anji jonnalad270e2d2011-08-09 11:28:32 +05302580 ret = msm_otg_debugfs_init(motg);
2581 if (ret)
2582 dev_dbg(&pdev->dev, "mode debugfs file is"
2583 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302584
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002585 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2586 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2587
Amit Blay58b31472011-11-18 09:39:39 +02002588 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2589 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302590 (!(motg->pdata->mode == USB_OTG) ||
2591 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002592 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002593 ALLOW_PHY_RETENTION |
2594 ALLOW_PHY_COMP_DISABLE;
2595
Amit Blay58b31472011-11-18 09:39:39 +02002596 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2597 motg->caps = ALLOW_PHY_RETENTION;
2598 }
2599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302601 pm_runtime_set_active(&pdev->dev);
2602 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302603
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302604 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002605
2606remove_otg:
2607 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302608free_irq:
2609 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002610destroy_wlock:
2611 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002612 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002613 msm_hsusb_ldo_enable(motg, 0);
2614free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302615 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002616free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302617 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002618devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002619 clk_disable(motg->pclk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08002620 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08002621free_xo_handle:
Stephen Boyd7dd22662012-01-26 16:09:31 -08002622 clk_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302623free_regs:
2624 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002625put_pclk:
2626 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302627put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002628 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302629put_clk:
2630 clk_put(motg->clk);
2631put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002632 if (!IS_ERR(motg->phy_reset_clk))
2633 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302634free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302635 if (motg->pdata->swfi_latency)
2636 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302637 kfree(motg);
2638 return ret;
2639}
2640
2641static int __devexit msm_otg_remove(struct platform_device *pdev)
2642{
2643 struct msm_otg *motg = platform_get_drvdata(pdev);
2644 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302645 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302646
2647 if (otg->host || otg->gadget)
2648 return -EBUSY;
2649
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302650 if (pdev->dev.of_node)
2651 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2653 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302654 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302655 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302656 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302657
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302658 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302659
2660 device_init_wakeup(&pdev->dev, 0);
2661 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002662 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302663
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302664 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 if (motg->pdata->pmic_id_irq)
2666 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302667 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302668 free_irq(motg->irq, motg);
2669
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302670 /*
2671 * Put PHY in low power mode.
2672 */
2673 ulpi_read(otg, 0x14);
2674 ulpi_write(otg, 0x08, 0x09);
2675
2676 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2677 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2678 if (readl(USB_PORTSC) & PORTSC_PHCD)
2679 break;
2680 udelay(1);
2681 cnt++;
2682 }
2683 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2684 dev_err(otg->dev, "Unable to suspend PHY\n");
2685
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302686 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002687 clk_disable(motg->core_clk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08002688 clk_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002689 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302690 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002691 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302692
2693 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302694 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302695
Amit Blay02eff132011-09-21 16:46:24 +03002696 if (!IS_ERR(motg->phy_reset_clk))
2697 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302698 clk_put(motg->pclk);
2699 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002700 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302701
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302702 if (motg->pdata->swfi_latency)
2703 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302704
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302705 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302706 return 0;
2707}
2708
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302709#ifdef CONFIG_PM_RUNTIME
2710static int msm_otg_runtime_idle(struct device *dev)
2711{
2712 struct msm_otg *motg = dev_get_drvdata(dev);
2713 struct otg_transceiver *otg = &motg->otg;
2714
2715 dev_dbg(dev, "OTG runtime idle\n");
2716
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302717 if (otg->state == OTG_STATE_UNDEFINED)
2718 return -EAGAIN;
2719 else
2720 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302721}
2722
2723static int msm_otg_runtime_suspend(struct device *dev)
2724{
2725 struct msm_otg *motg = dev_get_drvdata(dev);
2726
2727 dev_dbg(dev, "OTG runtime suspend\n");
2728 return msm_otg_suspend(motg);
2729}
2730
2731static int msm_otg_runtime_resume(struct device *dev)
2732{
2733 struct msm_otg *motg = dev_get_drvdata(dev);
2734
2735 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302736 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302737 return msm_otg_resume(motg);
2738}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302739#endif
2740
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302741#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742static int msm_otg_pm_suspend(struct device *dev)
2743{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302744 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302745
2746 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302747
2748#ifdef CONFIG_PM_RUNTIME
2749 ret = pm_runtime_suspend(dev);
2750 if (ret > 0)
2751 ret = 0;
2752#else
2753 ret = msm_otg_suspend(dev_get_drvdata(dev));
2754#endif
2755 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302756}
2757
2758static int msm_otg_pm_resume(struct device *dev)
2759{
2760 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302761
2762 dev_dbg(dev, "OTG PM resume\n");
2763
Manu Gautamf284c052011-09-08 16:52:48 +05302764#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302765 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302766 * Do not resume hardware as part of system resume,
2767 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302768 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002769 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302770#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302771
Manu Gautamf284c052011-09-08 16:52:48 +05302772 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302773}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302774#endif
2775
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302776#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302777static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302778 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2779 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2780 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302781};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302782#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302783
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302784static struct of_device_id msm_otg_dt_match[] = {
2785 { .compatible = "qcom,hsusb-otg",
2786 },
2787 {}
2788};
2789
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302790static struct platform_driver msm_otg_driver = {
2791 .remove = __devexit_p(msm_otg_remove),
2792 .driver = {
2793 .name = DRIVER_NAME,
2794 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302795#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302796 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302797#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302798 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302799 },
2800};
2801
2802static int __init msm_otg_init(void)
2803{
2804 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2805}
2806
2807static void __exit msm_otg_exit(void)
2808{
2809 platform_driver_unregister(&msm_otg_driver);
2810}
2811
2812module_init(msm_otg_init);
2813module_exit(msm_otg_exit);
2814
2815MODULE_LICENSE("GPL v2");
2816MODULE_DESCRIPTION("MSM USB transceiver driver");