blob: 5caecfb447c539cdb0c834e741557d0889444bcf [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020041#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053042
43#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080044#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
46#define MSM_USB_BASE (motg->regs)
47#define DRIVER_NAME "msm_otg"
48
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053049#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053051
52#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
53#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
54#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
55#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
56
57#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
58#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
59#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
60#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
61
Vamsi Krishna132b2762011-11-11 16:09:20 -080062#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053063#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
64
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053065static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053067static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053069/* Prevent idle power collapse(pc) while operating in peripheral mode */
70static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
71{
72 struct msm_otg_platform_data *pdata = dev->pdata;
73 u32 swfi_latency = 0;
74
75 if (!pdata || !pdata->swfi_latency)
76 return;
77
78 swfi_latency = pdata->swfi_latency + 1;
79
80 if (vote)
81 pm_qos_update_request(&dev->pm_qos_req_dma,
82 swfi_latency);
83 else
84 pm_qos_update_request(&dev->pm_qos_req_dma,
85 PM_QOS_DEFAULT_VALUE);
86}
87
Anji jonnala11aa5c42011-05-04 10:19:48 +053088static struct regulator *hsusb_3p3;
89static struct regulator *hsusb_1p8;
90static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053091static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053092
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053093static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053094static inline bool aca_enabled(void)
95{
96#ifdef CONFIG_USB_MSM_ACA
97 return true;
98#else
99 return debug_aca_enabled;
100#endif
101}
102
Anji jonnala11aa5c42011-05-04 10:19:48 +0530103static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
104{
105 int ret = 0;
106
107 if (init) {
108 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
109 if (IS_ERR(hsusb_vddcx)) {
110 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
111 return PTR_ERR(hsusb_vddcx);
112 }
113
114 ret = regulator_set_voltage(hsusb_vddcx,
115 USB_PHY_VDD_DIG_VOL_MIN,
116 USB_PHY_VDD_DIG_VOL_MAX);
117 if (ret) {
118 dev_err(motg->otg.dev, "unable to set the voltage "
119 "for hsusb vddcx\n");
120 regulator_put(hsusb_vddcx);
121 return ret;
122 }
123
124 ret = regulator_enable(hsusb_vddcx);
125 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 regulator_set_voltage(hsusb_vddcx, 0,
127 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
130 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 return ret;
139 }
140
141 ret = regulator_set_voltage(hsusb_vddcx, 0,
142 USB_PHY_VDD_DIG_VOL_MIN);
143 if (ret) {
144 dev_err(motg->otg.dev, "unable to set the voltage"
145 "for hsusb vddcx\n");
146 return ret;
147 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148
149 regulator_put(hsusb_vddcx);
150 }
151
152 return ret;
153}
154
155static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
156{
157 int rc = 0;
158
159 if (init) {
160 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
161 if (IS_ERR(hsusb_3p3)) {
162 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
163 return PTR_ERR(hsusb_3p3);
164 }
165
166 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
167 USB_PHY_3P3_VOL_MAX);
168 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 dev_err(motg->otg.dev, "unable to set voltage level for"
170 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 goto put_3p3;
172 }
173 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
174 if (IS_ERR(hsusb_1p8)) {
175 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
176 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 }
179 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
180 USB_PHY_1P8_VOL_MAX);
181 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 dev_err(motg->otg.dev, "unable to set voltage level for"
183 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530184 goto put_1p8;
185 }
186
187 return 0;
188 }
189
Anji jonnala11aa5c42011-05-04 10:19:48 +0530190put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193put_3p3_lpm:
194 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530195put_3p3:
196 regulator_put(hsusb_3p3);
197 return rc;
198}
199
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530200#ifdef CONFIG_PM_SLEEP
201#define USB_PHY_SUSP_DIG_VOL 500000
202static int msm_hsusb_config_vddcx(int high)
203{
204 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
205 int min_vol;
206 int ret;
207
208 if (high)
209 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
210 else
211 min_vol = USB_PHY_SUSP_DIG_VOL;
212
213 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
214 if (ret) {
215 pr_err("%s: unable to set the voltage for regulator "
216 "HSUSB_VDDCX\n", __func__);
217 return ret;
218 }
219
220 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
221
222 return ret;
223}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700224#else
225static int msm_hsusb_config_vddcx(int high)
226{
227 return 0;
228}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530229#endif
230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232{
233 int ret = 0;
234
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530235 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530236 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
237 return -ENODEV;
238 }
239
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530240 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
242 return -ENODEV;
243 }
244
245 if (on) {
246 ret = regulator_set_optimum_mode(hsusb_1p8,
247 USB_PHY_1P8_HPM_LOAD);
248 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530250 "HSUSB_1p8\n", __func__);
251 return ret;
252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253
254 ret = regulator_enable(hsusb_1p8);
255 if (ret) {
256 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
257 __func__);
258 regulator_set_optimum_mode(hsusb_1p8, 0);
259 return ret;
260 }
261
Anji jonnala11aa5c42011-05-04 10:19:48 +0530262 ret = regulator_set_optimum_mode(hsusb_3p3,
263 USB_PHY_3P3_HPM_LOAD);
264 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 regulator_set_optimum_mode(hsusb_1p8, 0);
268 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530269 return ret;
270 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271
272 ret = regulator_enable(hsusb_3p3);
273 if (ret) {
274 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
275 __func__);
276 regulator_set_optimum_mode(hsusb_3p3, 0);
277 regulator_set_optimum_mode(hsusb_1p8, 0);
278 regulator_disable(hsusb_1p8);
279 return ret;
280 }
281
Anji jonnala11aa5c42011-05-04 10:19:48 +0530282 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 ret = regulator_disable(hsusb_1p8);
284 if (ret) {
285 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
286 __func__);
287 return ret;
288 }
289
290 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530291 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530293 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 ret = regulator_disable(hsusb_3p3);
296 if (ret) {
297 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
298 __func__);
299 return ret;
300 }
301 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530302 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530304 "HSUSB_3p3\n", __func__);
305 }
306
307 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
308 return ret < 0 ? ret : 0;
309}
310
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530311static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
312{
313 static struct regulator *mhl_analog_switch;
314 struct msm_otg_platform_data *pdata = motg->pdata;
315
316 if (!pdata->mhl_enable)
317 return;
318
319 if (on) {
320 mhl_analog_switch = regulator_get(motg->otg.dev,
321 "mhl_ext_3p3v");
322 if (IS_ERR(mhl_analog_switch)) {
323 pr_err("Unable to get mhl_analog_switch\n");
324 return;
325 }
326
327 if (regulator_enable(mhl_analog_switch)) {
328 pr_err("unable to enable mhl_analog_switch\n");
329 goto put_analog_switch;
330 }
331 return;
332 }
333
334 regulator_disable(mhl_analog_switch);
335put_analog_switch:
336 regulator_put(mhl_analog_switch);
337}
338
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339static int ulpi_read(struct otg_transceiver *otg, u32 reg)
340{
341 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
342 int cnt = 0;
343
344 /* initiate read operation */
345 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
346 USB_ULPI_VIEWPORT);
347
348 /* wait for completion */
349 while (cnt < ULPI_IO_TIMEOUT_USEC) {
350 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
351 break;
352 udelay(1);
353 cnt++;
354 }
355
356 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
357 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
358 readl(USB_ULPI_VIEWPORT));
359 return -ETIMEDOUT;
360 }
361 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
362}
363
364static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
365{
366 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
367 int cnt = 0;
368
369 /* initiate write operation */
370 writel(ULPI_RUN | ULPI_WRITE |
371 ULPI_ADDR(reg) | ULPI_DATA(val),
372 USB_ULPI_VIEWPORT);
373
374 /* wait for completion */
375 while (cnt < ULPI_IO_TIMEOUT_USEC) {
376 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
377 break;
378 udelay(1);
379 cnt++;
380 }
381
382 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
383 dev_err(otg->dev, "ulpi_write: timeout\n");
384 return -ETIMEDOUT;
385 }
386 return 0;
387}
388
389static struct otg_io_access_ops msm_otg_io_ops = {
390 .read = ulpi_read,
391 .write = ulpi_write,
392};
393
394static void ulpi_init(struct msm_otg *motg)
395{
396 struct msm_otg_platform_data *pdata = motg->pdata;
397 int *seq = pdata->phy_init_seq;
398
399 if (!seq)
400 return;
401
402 while (seq[0] >= 0) {
403 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
404 seq[0], seq[1]);
405 ulpi_write(&motg->otg, seq[0], seq[1]);
406 seq += 2;
407 }
408}
409
410static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
411{
412 int ret;
413
414 if (assert) {
415 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
416 if (ret)
417 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
418 } else {
419 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
420 if (ret)
421 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
422 }
423 return ret;
424}
425
426static int msm_otg_phy_clk_reset(struct msm_otg *motg)
427{
428 int ret;
429
Amit Blay02eff132011-09-21 16:46:24 +0300430 if (IS_ERR(motg->phy_reset_clk))
431 return 0;
432
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530433 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
434 if (ret) {
435 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
436 return ret;
437 }
438 usleep_range(10000, 12000);
439 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
440 if (ret)
441 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
442 return ret;
443}
444
445static int msm_otg_phy_reset(struct msm_otg *motg)
446{
447 u32 val;
448 int ret;
449 int retries;
450
451 ret = msm_otg_link_clk_reset(motg, 1);
452 if (ret)
453 return ret;
454 ret = msm_otg_phy_clk_reset(motg);
455 if (ret)
456 return ret;
457 ret = msm_otg_link_clk_reset(motg, 0);
458 if (ret)
459 return ret;
460
461 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
462 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
463
464 for (retries = 3; retries > 0; retries--) {
465 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
466 ULPI_CLR(ULPI_FUNC_CTRL));
467 if (!ret)
468 break;
469 ret = msm_otg_phy_clk_reset(motg);
470 if (ret)
471 return ret;
472 }
473 if (!retries)
474 return -ETIMEDOUT;
475
476 /* This reset calibrates the phy, if the above write succeeded */
477 ret = msm_otg_phy_clk_reset(motg);
478 if (ret)
479 return ret;
480
481 for (retries = 3; retries > 0; retries--) {
482 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
483 if (ret != -ETIMEDOUT)
484 break;
485 ret = msm_otg_phy_clk_reset(motg);
486 if (ret)
487 return ret;
488 }
489 if (!retries)
490 return -ETIMEDOUT;
491
492 dev_info(motg->otg.dev, "phy_reset: success\n");
493 return 0;
494}
495
496#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497static int msm_otg_link_reset(struct msm_otg *motg)
498{
499 int cnt = 0;
500
501 writel_relaxed(USBCMD_RESET, USB_USBCMD);
502 while (cnt < LINK_RESET_TIMEOUT_USEC) {
503 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
504 break;
505 udelay(1);
506 cnt++;
507 }
508 if (cnt >= LINK_RESET_TIMEOUT_USEC)
509 return -ETIMEDOUT;
510
511 /* select ULPI phy */
512 writel_relaxed(0x80000000, USB_PORTSC);
513 writel_relaxed(0x0, USB_AHBBURST);
514 writel_relaxed(0x00, USB_AHBMODE);
515
516 return 0;
517}
518
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530519static int msm_otg_reset(struct otg_transceiver *otg)
520{
521 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
522 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530523 int ret;
524 u32 val = 0;
525 u32 ulpi_val = 0;
526
Ofir Cohen4da266f2012-01-03 10:19:29 +0200527 /*
528 * USB PHY and Link reset also reset the USB BAM.
529 * Thus perform reset operation only once to avoid
530 * USB BAM reset on other cases e.g. USB cable disconnections.
531 */
532 if (pdata->disable_reset_on_disconnect) {
533 if (motg->reset_counter)
534 return 0;
535 else
536 motg->reset_counter++;
537 }
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540 ret = msm_otg_phy_reset(motg);
541 if (ret) {
542 dev_err(otg->dev, "phy_reset failed\n");
543 return ret;
544 }
545
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530546 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530547 ret = msm_otg_link_reset(motg);
548 if (ret) {
549 dev_err(otg->dev, "link reset failed\n");
550 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530552 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530553
554 ulpi_init(motg);
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 /* Ensure that RESET operation is completed before turning off clock */
557 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 clk_disable(motg->clk);
560
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530561 if (pdata->otg_control == OTG_PHY_CONTROL) {
562 val = readl_relaxed(USB_OTGSC);
563 if (pdata->mode == USB_OTG) {
564 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
565 val |= OTGSC_IDIE | OTGSC_BSVIE;
566 } else if (pdata->mode == USB_PERIPHERAL) {
567 ulpi_val = ULPI_INT_SESS_VALID;
568 val |= OTGSC_BSVIE;
569 }
570 writel_relaxed(val, USB_OTGSC);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
572 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530573 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 return 0;
576}
577
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530578static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
579{
580 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
581
582 /*
583 * Allow bus suspend only for host mode. Device mode bus suspend
584 * is not implemented yet.
585 */
586 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530587 /*
588 * ID_GND --> ID_A transition can not be detected in LPM.
589 * Disallow host bus suspend when ACA is enabled.
590 */
591 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530592 pm_runtime_put(otg->dev);
593 else
594 pm_runtime_resume(otg->dev);
595 }
596
597 return 0;
598}
599
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530600#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530601#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
602
603#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530604static int msm_otg_suspend(struct msm_otg *motg)
605{
606 struct otg_transceiver *otg = &motg->otg;
607 struct usb_bus *bus = otg->host;
608 struct msm_otg_platform_data *pdata = motg->pdata;
609 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530610 bool host_bus_suspend;
611 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800612 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530613 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530614
615 if (atomic_read(&motg->in_lpm))
616 return 0;
617
618 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530619 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530620 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530621 * Chipidea 45-nm PHY suspend sequence:
622 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530623 * Interrupt Latch Register auto-clear feature is not present
624 * in all PHY versions. Latch register is clear on read type.
625 * Clear latch register to avoid spurious wakeup from
626 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530627 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530628 * PHY comparators are disabled when PHY enters into low power
629 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
630 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
631 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530632 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530633 * PLL is not turned off when PHY enters into low power mode (LPM).
634 * Disable PLL for maximum power savings.
635 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530636
637 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
638 ulpi_read(otg, 0x14);
639 if (pdata->otg_control == OTG_PHY_CONTROL)
640 ulpi_write(otg, 0x01, 0x30);
641 ulpi_write(otg, 0x08, 0x09);
642 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530643
644 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 * Turn off the OTG comparators, if depends on PMIC for
646 * VBUS and ID notifications.
647 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530648 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 ulpi_write(otg, OTG_COMP_DISABLE,
650 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
651 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
652 }
653
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530654 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530655 * PHY may take some time or even fail to enter into low power
656 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
657 * in failure case.
658 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530659 portsc = readl_relaxed(USB_PORTSC);
660 if (!(portsc & PORTSC_PHCD)) {
661 writel_relaxed(portsc | PORTSC_PHCD,
662 USB_PORTSC);
663 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
664 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
665 break;
666 udelay(1);
667 cnt++;
668 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530669 }
670
671 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
672 dev_err(otg->dev, "Unable to suspend PHY\n");
673 msm_otg_reset(otg);
674 enable_irq(motg->irq);
675 return -ETIMEDOUT;
676 }
677
678 /*
679 * PHY has capability to generate interrupt asynchronously in low
680 * power mode (LPM). This interrupt is level triggered. So USB IRQ
681 * line must be disabled till async interrupt enable bit is cleared
682 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
683 * block data communication from PHY.
684 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530685 cmd_val = readl_relaxed(USB_USBCMD);
686 if (host_bus_suspend)
687 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
688 else
689 cmd_val |= ULPI_STP_CTRL;
690 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530691
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530692 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200693 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
694 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
695 /* Enable PHY HV interrupts to wake MPM/Link */
696 phy_ctrl_val |=
697 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
698
699 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 motg->lpm_flags |= PHY_RETENTIONED;
701 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 /* Ensure that above operation is completed before turning off clocks */
704 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530705 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800706 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530707
Anji jonnala7da3f262011-12-02 17:22:14 -0800708 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
709 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
710 if (ret)
711 dev_err(otg->dev, "%s failed to devote for "
712 "TCXO D0 buffer%d\n", __func__, ret);
713
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530714 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 msm_hsusb_ldo_enable(motg, 0);
716 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530717 }
718
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530719 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530721 msm_hsusb_mhl_switch_enable(motg, 0);
722 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
724 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530725 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 if (motg->pdata->pmic_id_irq)
727 enable_irq_wake(motg->pdata->pmic_id_irq);
728 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530729 if (bus)
730 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
731
732 atomic_set(&motg->in_lpm, 1);
733 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530735
736 dev_info(otg->dev, "USB in low power mode\n");
737
738 return 0;
739}
740
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741static int msm_otg_resume(struct msm_otg *motg)
742{
743 struct otg_transceiver *otg = &motg->otg;
744 struct usb_bus *bus = otg->host;
745 int cnt = 0;
746 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200747 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800748 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749
750 if (!atomic_read(&motg->in_lpm))
751 return 0;
752
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800754
755 /* Vote for TCXO when waking up the phy */
756 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
757 if (ret)
758 dev_err(otg->dev, "%s failed to vote for "
759 "TCXO D0 buffer%d\n", __func__, ret);
760
Manu Gautam5143b252012-01-05 19:25:23 -0800761 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200762
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530764
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
766 msm_hsusb_ldo_enable(motg, 1);
767 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
768 }
769
770 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530771 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530772 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200773 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
774 phy_ctrl_val |= PHY_RETEN;
775 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
776 /* Disable PHY HV interrupts */
777 phy_ctrl_val &=
778 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
779 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 }
782
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530783 temp = readl(USB_USBCMD);
784 temp &= ~ASYNC_INTR_CTRL;
785 temp &= ~ULPI_STP_CTRL;
786 writel(temp, USB_USBCMD);
787
788 /*
789 * PHY comes out of low power mode (LPM) in case of wakeup
790 * from asynchronous interrupt.
791 */
792 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
793 goto skip_phy_resume;
794
795 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
796 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
797 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
798 break;
799 udelay(1);
800 cnt++;
801 }
802
803 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
804 /*
805 * This is a fatal error. Reset the link and
806 * PHY. USB state can not be restored. Re-insertion
807 * of USB cable is the only way to get USB working.
808 */
809 dev_err(otg->dev, "Unable to resume USB."
810 "Re-plugin the cable\n");
811 msm_otg_reset(otg);
812 }
813
814skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 /* Turn on the OTG comparators on resume */
816 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
817 ulpi_write(otg, OTG_COMP_DISABLE,
818 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
819 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
820 }
821 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (motg->pdata->pmic_id_irq)
824 disable_irq_wake(motg->pdata->pmic_id_irq);
825 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826 if (bus)
827 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
828
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530829 atomic_set(&motg->in_lpm, 0);
830
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 if (motg->async_int) {
832 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 enable_irq(motg->irq);
834 }
835
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530836 dev_info(otg->dev, "USB exited from low power mode\n");
837
838 return 0;
839}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530840#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530842static int msm_otg_notify_chg_type(struct msm_otg *motg)
843{
844 static int charger_type;
845 /*
846 * TODO
847 * Unify OTG driver charger types and power supply charger types
848 */
849 if (charger_type == motg->chg_type)
850 return 0;
851
852 if (motg->chg_type == USB_SDP_CHARGER)
853 charger_type = POWER_SUPPLY_TYPE_USB;
854 else if (motg->chg_type == USB_CDP_CHARGER)
855 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
856 else if (motg->chg_type == USB_DCP_CHARGER)
857 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
858 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
859 motg->chg_type == USB_ACA_A_CHARGER ||
860 motg->chg_type == USB_ACA_B_CHARGER ||
861 motg->chg_type == USB_ACA_C_CHARGER))
862 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
863 else
864 charger_type = POWER_SUPPLY_TYPE_BATTERY;
865
866 return pm8921_set_usb_power_supply_type(charger_type);
867}
868
Amit Blay0f7edf72012-01-15 10:11:27 +0200869static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
870{
871 struct power_supply *psy;
872
873 psy = power_supply_get_by_name("usb");
874 if (!psy)
875 goto psy_not_supported;
876
877 if (motg->cur_power == 0 && mA > 0) {
878 /* Enable charging */
879 if (power_supply_set_online(psy, true))
880 goto psy_not_supported;
881 } else if (motg->cur_power > 0 && mA == 0) {
882 /* Disable charging */
883 if (power_supply_set_online(psy, false))
884 goto psy_not_supported;
885 return 0;
886 }
887 /* Set max current limit */
888 if (power_supply_set_current_limit(psy, 1000*mA))
889 goto psy_not_supported;
890
891 return 0;
892
893psy_not_supported:
894 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
895 return -ENXIO;
896}
897
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530898static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
899{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530900 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
901 motg->chg_type == USB_ACA_A_CHARGER ||
902 motg->chg_type == USB_ACA_B_CHARGER ||
903 motg->chg_type == USB_ACA_C_CHARGER) &&
904 mA > IDEV_ACA_CHG_LIMIT)
905 mA = IDEV_ACA_CHG_LIMIT;
906
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530907 if (msm_otg_notify_chg_type(motg))
908 dev_err(motg->otg.dev,
909 "Failed notifying %d charger type to PMIC\n",
910 motg->chg_type);
911
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530912 if (motg->cur_power == mA)
913 return;
914
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530915 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +0200916
917 /*
918 * Use Power Supply API if supported, otherwise fallback
919 * to legacy pm8921 API.
920 */
921 if (msm_otg_notify_power_supply(motg, mA))
922 pm8921_charger_vbus_draw(mA);
923
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530924 motg->cur_power = mA;
925}
926
927static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
928{
929 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
930
931 /*
932 * Gadget driver uses set_power method to notify about the
933 * available current based on suspend/configured states.
934 *
935 * IDEV_CHG can be drawn irrespective of suspend/un-configured
936 * states when CDP/ACA is connected.
937 */
938 if (motg->chg_type == USB_SDP_CHARGER)
939 msm_otg_notify_charger(motg, mA);
940
941 return 0;
942}
943
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530944static void msm_otg_start_host(struct otg_transceiver *otg, int on)
945{
946 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
947 struct msm_otg_platform_data *pdata = motg->pdata;
948 struct usb_hcd *hcd;
949
950 if (!otg->host)
951 return;
952
953 hcd = bus_to_hcd(otg->host);
954
955 if (on) {
956 dev_dbg(otg->dev, "host on\n");
957
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530958 /*
959 * Some boards have a switch cotrolled by gpio
960 * to enable/disable internal HUB. Enable internal
961 * HUB before kicking the host.
962 */
963 if (pdata->setup_gpio)
964 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530965 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530966 } else {
967 dev_dbg(otg->dev, "host off\n");
968
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530969 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530970 /* HCD core reset all bits of PORTSC. select ULPI phy */
971 writel_relaxed(0x80000000, USB_PORTSC);
972
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530973 if (pdata->setup_gpio)
974 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530975 }
976}
977
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978static int msm_otg_usbdev_notify(struct notifier_block *self,
979 unsigned long action, void *priv)
980{
981 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530982 struct usb_device *udev = priv;
983
984 if (!aca_enabled())
985 goto out;
986
987 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
988 goto out;
989
990 if (udev->bus != motg->otg.host)
991 goto out;
992 /*
993 * Interested in devices connected directly to the root hub.
994 * ACA dock can supply IDEV_CHG irrespective devices connected
995 * on the accessory port.
996 */
997 if (!udev->parent || udev->parent->parent ||
998 motg->chg_type == USB_ACA_DOCK_CHARGER)
999 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
1001 switch (action) {
1002 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301003 usb_disable_autosuspend(udev);
1004 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 if (udev->actconfig)
1007 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1008 else
1009 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301010 break;
1011 case USB_DEVICE_REMOVE:
1012 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 break;
1014 default:
1015 break;
1016 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301017 if (test_bit(ID_A, &motg->inputs))
1018 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1019 motg->mA_port);
1020out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 return NOTIFY_OK;
1022}
1023
Mayank Ranae3926882011-12-26 09:47:54 +05301024static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1025{
1026 int ret;
1027 static bool vbus_is_on;
1028
1029 if (vbus_is_on == on)
1030 return;
1031
1032 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301033 ret = motg->pdata->vbus_power(on);
1034 if (!ret)
1035 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301036 return;
1037 }
1038
1039 if (!vbus_otg) {
1040 pr_err("vbus_otg is NULL.");
1041 return;
1042 }
1043
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001044 /*
1045 * if entering host mode tell the charger to not draw any current
1046 * from usb - if exiting host mode let the charger draw current
1047 */
1048 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301049 if (on) {
1050 ret = regulator_enable(vbus_otg);
1051 if (ret) {
1052 pr_err("unable to enable vbus_otg\n");
1053 return;
1054 }
1055 vbus_is_on = true;
1056 } else {
1057 ret = regulator_disable(vbus_otg);
1058 if (ret) {
1059 pr_err("unable to disable vbus_otg\n");
1060 return;
1061 }
1062 vbus_is_on = false;
1063 }
1064}
1065
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301066static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1067{
1068 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1069 struct usb_hcd *hcd;
1070
1071 /*
1072 * Fail host registration if this board can support
1073 * only peripheral configuration.
1074 */
1075 if (motg->pdata->mode == USB_PERIPHERAL) {
1076 dev_info(otg->dev, "Host mode is not supported\n");
1077 return -ENODEV;
1078 }
1079
Mayank Ranae3926882011-12-26 09:47:54 +05301080 if (!motg->pdata->vbus_power && host) {
1081 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1082 if (IS_ERR(vbus_otg)) {
1083 pr_err("Unable to get vbus_otg\n");
1084 return -ENODEV;
1085 }
1086 }
1087
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301088 if (!host) {
1089 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301090 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301092 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301093 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301094 otg->host = NULL;
1095 otg->state = OTG_STATE_UNDEFINED;
1096 schedule_work(&motg->sm_work);
1097 } else {
1098 otg->host = NULL;
1099 }
1100
Mayank Ranae3926882011-12-26 09:47:54 +05301101 if (vbus_otg)
1102 regulator_put(vbus_otg);
1103
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301104 return 0;
1105 }
1106
1107 hcd = bus_to_hcd(host);
1108 hcd->power_budget = motg->pdata->power_budget;
1109
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001110 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1111 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301112 otg->host = host;
1113 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1114
1115 /*
1116 * Kick the state machine work, if peripheral is not supported
1117 * or peripheral is already registered with us.
1118 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301119 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1120 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301122 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123
1124 return 0;
1125}
1126
1127static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1128{
1129 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1130 struct msm_otg_platform_data *pdata = motg->pdata;
1131
1132 if (!otg->gadget)
1133 return;
1134
1135 if (on) {
1136 dev_dbg(otg->dev, "gadget on\n");
1137 /*
1138 * Some boards have a switch cotrolled by gpio
1139 * to enable/disable internal HUB. Disable internal
1140 * HUB before kicking the gadget.
1141 */
1142 if (pdata->setup_gpio)
1143 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301144 /*
1145 * vote for minimum dma_latency to prevent idle
1146 * power collapse(pc) while running in peripheral mode.
1147 */
1148 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301149 usb_gadget_vbus_connect(otg->gadget);
1150 } else {
1151 dev_dbg(otg->dev, "gadget off\n");
1152 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301153 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301154 if (pdata->setup_gpio)
1155 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1156 }
1157
1158}
1159
1160static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1161 struct usb_gadget *gadget)
1162{
1163 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1164
1165 /*
1166 * Fail peripheral registration if this board can support
1167 * only host configuration.
1168 */
1169 if (motg->pdata->mode == USB_HOST) {
1170 dev_info(otg->dev, "Peripheral mode is not supported\n");
1171 return -ENODEV;
1172 }
1173
1174 if (!gadget) {
1175 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301176 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301177 msm_otg_start_peripheral(otg, 0);
1178 otg->gadget = NULL;
1179 otg->state = OTG_STATE_UNDEFINED;
1180 schedule_work(&motg->sm_work);
1181 } else {
1182 otg->gadget = NULL;
1183 }
1184
1185 return 0;
1186 }
1187 otg->gadget = gadget;
1188 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1189
1190 /*
1191 * Kick the state machine work, if host is not supported
1192 * or host is already registered with us.
1193 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301194 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1195 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301196 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301197 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301198
1199 return 0;
1200}
1201
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202static bool msm_chg_aca_detect(struct msm_otg *motg)
1203{
1204 struct otg_transceiver *otg = &motg->otg;
1205 u32 int_sts;
1206 bool ret = false;
1207
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301208 if (!aca_enabled())
1209 goto out;
1210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001211 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1212 goto out;
1213
1214 int_sts = ulpi_read(otg, 0x87);
1215 switch (int_sts & 0x1C) {
1216 case 0x08:
1217 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1218 dev_dbg(otg->dev, "ID_A\n");
1219 motg->chg_type = USB_ACA_A_CHARGER;
1220 motg->chg_state = USB_CHG_STATE_DETECTED;
1221 clear_bit(ID_B, &motg->inputs);
1222 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301223 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001224 ret = true;
1225 }
1226 break;
1227 case 0x0C:
1228 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1229 dev_dbg(otg->dev, "ID_B\n");
1230 motg->chg_type = USB_ACA_B_CHARGER;
1231 motg->chg_state = USB_CHG_STATE_DETECTED;
1232 clear_bit(ID_A, &motg->inputs);
1233 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301234 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001235 ret = true;
1236 }
1237 break;
1238 case 0x10:
1239 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1240 dev_dbg(otg->dev, "ID_C\n");
1241 motg->chg_type = USB_ACA_C_CHARGER;
1242 motg->chg_state = USB_CHG_STATE_DETECTED;
1243 clear_bit(ID_A, &motg->inputs);
1244 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301245 set_bit(ID, &motg->inputs);
1246 ret = true;
1247 }
1248 break;
1249 case 0x04:
1250 if (test_and_clear_bit(ID, &motg->inputs)) {
1251 dev_dbg(otg->dev, "ID_GND\n");
1252 motg->chg_type = USB_INVALID_CHARGER;
1253 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1254 clear_bit(ID_A, &motg->inputs);
1255 clear_bit(ID_B, &motg->inputs);
1256 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 ret = true;
1258 }
1259 break;
1260 default:
1261 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1262 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301263 test_and_clear_bit(ID_C, &motg->inputs) |
1264 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301266 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001267 motg->chg_type = USB_INVALID_CHARGER;
1268 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1269 }
1270 }
1271out:
1272 return ret;
1273}
1274
1275static void msm_chg_enable_aca_det(struct msm_otg *motg)
1276{
1277 struct otg_transceiver *otg = &motg->otg;
1278
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301279 if (!aca_enabled())
1280 return;
1281
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 switch (motg->pdata->phy_type) {
1283 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301284 /* Disable ID_GND in link and PHY */
1285 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1286 OTGSC_IDIE), USB_OTGSC);
1287 ulpi_write(otg, 0x01, 0x0C);
1288 ulpi_write(otg, 0x10, 0x0F);
1289 ulpi_write(otg, 0x10, 0x12);
1290 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301292 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 break;
1294 default:
1295 break;
1296 }
1297}
1298
1299static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1300{
1301 struct otg_transceiver *otg = &motg->otg;
1302
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301303 if (!aca_enabled())
1304 return;
1305
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 switch (motg->pdata->phy_type) {
1307 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301308 /* Enable ACA Detection interrupt (on any RID change) */
1309 ulpi_write(otg, 0x01, 0x94);
1310 break;
1311 default:
1312 break;
1313 }
1314}
1315
1316static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1317{
1318 struct otg_transceiver *otg = &motg->otg;
1319
1320 if (!aca_enabled())
1321 return;
1322
1323 switch (motg->pdata->phy_type) {
1324 case SNPS_28NM_INTEGRATED_PHY:
1325 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001326 break;
1327 default:
1328 break;
1329 }
1330}
1331
1332static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1333{
1334 struct otg_transceiver *otg = &motg->otg;
1335 bool ret = false;
1336
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301337 if (!aca_enabled())
1338 return ret;
1339
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001340 switch (motg->pdata->phy_type) {
1341 case SNPS_28NM_INTEGRATED_PHY:
1342 if (ulpi_read(otg, 0x91) & 1) {
1343 dev_dbg(otg->dev, "RID change\n");
1344 ulpi_write(otg, 0x01, 0x92);
1345 ret = msm_chg_aca_detect(motg);
1346 }
1347 default:
1348 break;
1349 }
1350 return ret;
1351}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301352
1353static void msm_otg_id_timer_func(unsigned long data)
1354{
1355 struct msm_otg *motg = (struct msm_otg *) data;
1356
1357 if (!aca_enabled())
1358 return;
1359
1360 if (atomic_read(&motg->in_lpm)) {
1361 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1362 return;
1363 }
1364
1365 if (msm_chg_check_aca_intr(motg)) {
1366 dev_dbg(motg->otg.dev, "timer: aca work\n");
1367 schedule_work(&motg->sm_work);
1368 }
1369
1370 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1371 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1372}
1373
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301374static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1375{
1376 struct otg_transceiver *otg = &motg->otg;
1377 u32 chg_det;
1378 bool ret = false;
1379
1380 switch (motg->pdata->phy_type) {
1381 case CI_45NM_INTEGRATED_PHY:
1382 chg_det = ulpi_read(otg, 0x34);
1383 ret = chg_det & (1 << 4);
1384 break;
1385 case SNPS_28NM_INTEGRATED_PHY:
1386 chg_det = ulpi_read(otg, 0x87);
1387 ret = chg_det & 1;
1388 break;
1389 default:
1390 break;
1391 }
1392 return ret;
1393}
1394
1395static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1396{
1397 struct otg_transceiver *otg = &motg->otg;
1398 u32 chg_det;
1399
1400 switch (motg->pdata->phy_type) {
1401 case CI_45NM_INTEGRATED_PHY:
1402 chg_det = ulpi_read(otg, 0x34);
1403 /* Turn off charger block */
1404 chg_det |= ~(1 << 1);
1405 ulpi_write(otg, chg_det, 0x34);
1406 udelay(20);
1407 /* control chg block via ULPI */
1408 chg_det &= ~(1 << 3);
1409 ulpi_write(otg, chg_det, 0x34);
1410 /* put it in host mode for enabling D- source */
1411 chg_det &= ~(1 << 2);
1412 ulpi_write(otg, chg_det, 0x34);
1413 /* Turn on chg detect block */
1414 chg_det &= ~(1 << 1);
1415 ulpi_write(otg, chg_det, 0x34);
1416 udelay(20);
1417 /* enable chg detection */
1418 chg_det &= ~(1 << 0);
1419 ulpi_write(otg, chg_det, 0x34);
1420 break;
1421 case SNPS_28NM_INTEGRATED_PHY:
1422 /*
1423 * Configure DM as current source, DP as current sink
1424 * and enable battery charging comparators.
1425 */
1426 ulpi_write(otg, 0x8, 0x85);
1427 ulpi_write(otg, 0x2, 0x85);
1428 ulpi_write(otg, 0x1, 0x85);
1429 break;
1430 default:
1431 break;
1432 }
1433}
1434
1435static bool msm_chg_check_primary_det(struct msm_otg *motg)
1436{
1437 struct otg_transceiver *otg = &motg->otg;
1438 u32 chg_det;
1439 bool ret = false;
1440
1441 switch (motg->pdata->phy_type) {
1442 case CI_45NM_INTEGRATED_PHY:
1443 chg_det = ulpi_read(otg, 0x34);
1444 ret = chg_det & (1 << 4);
1445 break;
1446 case SNPS_28NM_INTEGRATED_PHY:
1447 chg_det = ulpi_read(otg, 0x87);
1448 ret = chg_det & 1;
1449 break;
1450 default:
1451 break;
1452 }
1453 return ret;
1454}
1455
1456static void msm_chg_enable_primary_det(struct msm_otg *motg)
1457{
1458 struct otg_transceiver *otg = &motg->otg;
1459 u32 chg_det;
1460
1461 switch (motg->pdata->phy_type) {
1462 case CI_45NM_INTEGRATED_PHY:
1463 chg_det = ulpi_read(otg, 0x34);
1464 /* enable chg detection */
1465 chg_det &= ~(1 << 0);
1466 ulpi_write(otg, chg_det, 0x34);
1467 break;
1468 case SNPS_28NM_INTEGRATED_PHY:
1469 /*
1470 * Configure DP as current source, DM as current sink
1471 * and enable battery charging comparators.
1472 */
1473 ulpi_write(otg, 0x2, 0x85);
1474 ulpi_write(otg, 0x1, 0x85);
1475 break;
1476 default:
1477 break;
1478 }
1479}
1480
1481static bool msm_chg_check_dcd(struct msm_otg *motg)
1482{
1483 struct otg_transceiver *otg = &motg->otg;
1484 u32 line_state;
1485 bool ret = false;
1486
1487 switch (motg->pdata->phy_type) {
1488 case CI_45NM_INTEGRATED_PHY:
1489 line_state = ulpi_read(otg, 0x15);
1490 ret = !(line_state & 1);
1491 break;
1492 case SNPS_28NM_INTEGRATED_PHY:
1493 line_state = ulpi_read(otg, 0x87);
1494 ret = line_state & 2;
1495 break;
1496 default:
1497 break;
1498 }
1499 return ret;
1500}
1501
1502static void msm_chg_disable_dcd(struct msm_otg *motg)
1503{
1504 struct otg_transceiver *otg = &motg->otg;
1505 u32 chg_det;
1506
1507 switch (motg->pdata->phy_type) {
1508 case CI_45NM_INTEGRATED_PHY:
1509 chg_det = ulpi_read(otg, 0x34);
1510 chg_det &= ~(1 << 5);
1511 ulpi_write(otg, chg_det, 0x34);
1512 break;
1513 case SNPS_28NM_INTEGRATED_PHY:
1514 ulpi_write(otg, 0x10, 0x86);
1515 break;
1516 default:
1517 break;
1518 }
1519}
1520
1521static void msm_chg_enable_dcd(struct msm_otg *motg)
1522{
1523 struct otg_transceiver *otg = &motg->otg;
1524 u32 chg_det;
1525
1526 switch (motg->pdata->phy_type) {
1527 case CI_45NM_INTEGRATED_PHY:
1528 chg_det = ulpi_read(otg, 0x34);
1529 /* Turn on D+ current source */
1530 chg_det |= (1 << 5);
1531 ulpi_write(otg, chg_det, 0x34);
1532 break;
1533 case SNPS_28NM_INTEGRATED_PHY:
1534 /* Data contact detection enable */
1535 ulpi_write(otg, 0x10, 0x85);
1536 break;
1537 default:
1538 break;
1539 }
1540}
1541
1542static void msm_chg_block_on(struct msm_otg *motg)
1543{
1544 struct otg_transceiver *otg = &motg->otg;
1545 u32 func_ctrl, chg_det;
1546
1547 /* put the controller in non-driving mode */
1548 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1549 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1550 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1551 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1552
1553 switch (motg->pdata->phy_type) {
1554 case CI_45NM_INTEGRATED_PHY:
1555 chg_det = ulpi_read(otg, 0x34);
1556 /* control chg block via ULPI */
1557 chg_det &= ~(1 << 3);
1558 ulpi_write(otg, chg_det, 0x34);
1559 /* Turn on chg detect block */
1560 chg_det &= ~(1 << 1);
1561 ulpi_write(otg, chg_det, 0x34);
1562 udelay(20);
1563 break;
1564 case SNPS_28NM_INTEGRATED_PHY:
1565 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301566 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301567 /* Clear alt interrupt latch and enable bits */
1568 ulpi_write(otg, 0x1F, 0x92);
1569 ulpi_write(otg, 0x1F, 0x95);
1570 udelay(100);
1571 break;
1572 default:
1573 break;
1574 }
1575}
1576
1577static void msm_chg_block_off(struct msm_otg *motg)
1578{
1579 struct otg_transceiver *otg = &motg->otg;
1580 u32 func_ctrl, chg_det;
1581
1582 switch (motg->pdata->phy_type) {
1583 case CI_45NM_INTEGRATED_PHY:
1584 chg_det = ulpi_read(otg, 0x34);
1585 /* Turn off charger block */
1586 chg_det |= ~(1 << 1);
1587 ulpi_write(otg, chg_det, 0x34);
1588 break;
1589 case SNPS_28NM_INTEGRATED_PHY:
1590 /* Clear charger detecting control bits */
1591 ulpi_write(otg, 0x3F, 0x86);
1592 /* Clear alt interrupt latch and enable bits */
1593 ulpi_write(otg, 0x1F, 0x92);
1594 ulpi_write(otg, 0x1F, 0x95);
1595 break;
1596 default:
1597 break;
1598 }
1599
1600 /* put the controller in normal mode */
1601 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1602 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1603 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1604 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1605}
1606
Anji jonnalad270e2d2011-08-09 11:28:32 +05301607static const char *chg_to_string(enum usb_chg_type chg_type)
1608{
1609 switch (chg_type) {
1610 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1611 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1612 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1613 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1614 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1615 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1616 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1617 default: return "INVALID_CHARGER";
1618 }
1619}
1620
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301621#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1622#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1623#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1624#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1625static void msm_chg_detect_work(struct work_struct *w)
1626{
1627 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1628 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301629 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301630 unsigned long delay;
1631
1632 dev_dbg(otg->dev, "chg detection work\n");
1633 switch (motg->chg_state) {
1634 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301635 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301636 if (motg->pdata->enable_dcd)
1637 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001638 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301639 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1640 motg->dcd_retries = 0;
1641 delay = MSM_CHG_DCD_POLL_TIME;
1642 break;
1643 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001644 is_aca = msm_chg_aca_detect(motg);
1645 if (is_aca) {
1646 /*
1647 * ID_A can be ACA dock too. continue
1648 * primary detection after DCD.
1649 */
1650 if (test_bit(ID_A, &motg->inputs)) {
1651 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1652 } else {
1653 delay = 0;
1654 break;
1655 }
1656 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301657 if (motg->pdata->enable_dcd)
1658 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301659 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1660 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301661 if (motg->pdata->enable_dcd)
1662 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301663 msm_chg_enable_primary_det(motg);
1664 delay = MSM_CHG_PRIMARY_DET_TIME;
1665 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1666 } else {
1667 delay = MSM_CHG_DCD_POLL_TIME;
1668 }
1669 break;
1670 case USB_CHG_STATE_DCD_DONE:
1671 vout = msm_chg_check_primary_det(motg);
1672 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301673 if (test_bit(ID_A, &motg->inputs)) {
1674 motg->chg_type = USB_ACA_DOCK_CHARGER;
1675 motg->chg_state = USB_CHG_STATE_DETECTED;
1676 delay = 0;
1677 break;
1678 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301679 msm_chg_enable_secondary_det(motg);
1680 delay = MSM_CHG_SECONDARY_DET_TIME;
1681 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1682 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301683 if (test_bit(ID_A, &motg->inputs)) {
1684 motg->chg_type = USB_ACA_A_CHARGER;
1685 motg->chg_state = USB_CHG_STATE_DETECTED;
1686 delay = 0;
1687 break;
1688 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301689 motg->chg_type = USB_SDP_CHARGER;
1690 motg->chg_state = USB_CHG_STATE_DETECTED;
1691 delay = 0;
1692 }
1693 break;
1694 case USB_CHG_STATE_PRIMARY_DONE:
1695 vout = msm_chg_check_secondary_det(motg);
1696 if (vout)
1697 motg->chg_type = USB_DCP_CHARGER;
1698 else
1699 motg->chg_type = USB_CDP_CHARGER;
1700 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1701 /* fall through */
1702 case USB_CHG_STATE_SECONDARY_DONE:
1703 motg->chg_state = USB_CHG_STATE_DETECTED;
1704 case USB_CHG_STATE_DETECTED:
1705 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001706 msm_chg_enable_aca_det(motg);
1707 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301708 dev_dbg(otg->dev, "chg_type = %s\n",
1709 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301710 schedule_work(&motg->sm_work);
1711 return;
1712 default:
1713 return;
1714 }
1715
1716 schedule_delayed_work(&motg->chg_work, delay);
1717}
1718
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301719/*
1720 * We support OTG, Peripheral only and Host only configurations. In case
1721 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1722 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1723 * enabled when switch is controlled by user and default mode is supplied
1724 * by board file, which can be changed by userspace later.
1725 */
1726static void msm_otg_init_sm(struct msm_otg *motg)
1727{
1728 struct msm_otg_platform_data *pdata = motg->pdata;
1729 u32 otgsc = readl(USB_OTGSC);
1730
1731 switch (pdata->mode) {
1732 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001733 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301734 if (pdata->default_mode == USB_HOST) {
1735 clear_bit(ID, &motg->inputs);
1736 } else if (pdata->default_mode == USB_PERIPHERAL) {
1737 set_bit(ID, &motg->inputs);
1738 set_bit(B_SESS_VLD, &motg->inputs);
1739 } else {
1740 set_bit(ID, &motg->inputs);
1741 clear_bit(B_SESS_VLD, &motg->inputs);
1742 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301743 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1744 if (otgsc & OTGSC_ID)
1745 set_bit(ID, &motg->inputs);
1746 else
1747 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001748 if (otgsc & OTGSC_BSV)
1749 set_bit(B_SESS_VLD, &motg->inputs);
1750 else
1751 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301752 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301753 if (pdata->pmic_id_irq) {
1754 if (irq_read_line(pdata->pmic_id_irq))
1755 set_bit(ID, &motg->inputs);
1756 else
1757 clear_bit(ID, &motg->inputs);
1758 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301759 /*
1760 * VBUS initial state is reported after PMIC
1761 * driver initialization. Wait for it.
1762 */
1763 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301764 }
1765 break;
1766 case USB_HOST:
1767 clear_bit(ID, &motg->inputs);
1768 break;
1769 case USB_PERIPHERAL:
1770 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301771 if (pdata->otg_control == OTG_PHY_CONTROL) {
1772 if (otgsc & OTGSC_BSV)
1773 set_bit(B_SESS_VLD, &motg->inputs);
1774 else
1775 clear_bit(B_SESS_VLD, &motg->inputs);
1776 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1777 /*
1778 * VBUS initial state is reported after PMIC
1779 * driver initialization. Wait for it.
1780 */
1781 wait_for_completion(&pmic_vbus_init);
1782 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301783 break;
1784 default:
1785 break;
1786 }
1787}
1788
1789static void msm_otg_sm_work(struct work_struct *w)
1790{
1791 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1792 struct otg_transceiver *otg = &motg->otg;
1793
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301794 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301795 switch (otg->state) {
1796 case OTG_STATE_UNDEFINED:
1797 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1798 msm_otg_reset(otg);
1799 msm_otg_init_sm(motg);
1800 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301801 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1802 test_bit(ID, &motg->inputs)) {
1803 pm_runtime_put_noidle(otg->dev);
1804 pm_runtime_suspend(otg->dev);
1805 break;
1806 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301807 /* FALL THROUGH */
1808 case OTG_STATE_B_IDLE:
1809 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 if ((!test_bit(ID, &motg->inputs) ||
1811 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001812 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1813 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301814 IDEV_ACA_CHG_MAX);
1815 else if (test_bit(ID_A, &motg->inputs))
1816 msm_otg_notify_charger(motg,
1817 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301818 else
1819 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301820 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301821 /*
1822 * Link can not generate PHY_ALT interrupt
1823 * in host mode when no device is attached
1824 * to the port. It is also observed PHY_ALT
1825 * interrupt missing upon Micro-A cable disconnect.
1826 * Hence disable PHY_ALT interrupt and perform
1827 * polling to detect RID change.
1828 */
1829 msm_chg_enable_aca_det(motg);
1830 msm_chg_disable_aca_intr(motg);
1831 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301832 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301833 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1834 switch (motg->chg_state) {
1835 case USB_CHG_STATE_UNDEFINED:
1836 msm_chg_detect_work(&motg->chg_work.work);
1837 break;
1838 case USB_CHG_STATE_DETECTED:
1839 switch (motg->chg_type) {
1840 case USB_DCP_CHARGER:
1841 msm_otg_notify_charger(motg,
1842 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301843 pm_runtime_put_noidle(otg->dev);
1844 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301845 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301846 case USB_ACA_B_CHARGER:
1847 msm_otg_notify_charger(motg,
1848 IDEV_ACA_CHG_MAX);
1849 /*
1850 * (ID_B --> ID_C) PHY_ALT interrupt can
1851 * not be detected in LPM.
1852 */
1853 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301854 case USB_CDP_CHARGER:
1855 msm_otg_notify_charger(motg,
1856 IDEV_CHG_MAX);
1857 msm_otg_start_peripheral(otg, 1);
1858 otg->state = OTG_STATE_B_PERIPHERAL;
1859 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301860 case USB_ACA_C_CHARGER:
1861 msm_otg_notify_charger(motg,
1862 IDEV_ACA_CHG_MAX);
1863 msm_otg_start_peripheral(otg, 1);
1864 otg->state = OTG_STATE_B_PERIPHERAL;
1865 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301866 case USB_SDP_CHARGER:
1867 msm_otg_notify_charger(motg, IUNIT);
1868 msm_otg_start_peripheral(otg, 1);
1869 otg->state = OTG_STATE_B_PERIPHERAL;
1870 break;
1871 default:
1872 break;
1873 }
1874 break;
1875 default:
1876 break;
1877 }
1878 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301879 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301880 msm_otg_notify_charger(motg, 0);
1881 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1882 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301883 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301884 pm_runtime_put_noidle(otg->dev);
1885 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301886 }
1887 break;
1888 case OTG_STATE_B_PERIPHERAL:
1889 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1890 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001891 !test_bit(ID, &motg->inputs) ||
1892 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301893 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001894 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 schedule_work(w);
1896 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301897 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898 }
1899 break;
1900 case OTG_STATE_A_HOST:
1901 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1902 if (test_bit(ID, &motg->inputs) &&
1903 !test_bit(ID_A, &motg->inputs)) {
1904 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301905 msm_hsusb_vbus_power(motg, 0);
1906 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301907 /*
1908 * Exit point of host mode.
1909 *
1910 * 1. Micro-A cable disconnect: Just schedule
1911 * the work. PHY is reset in B_IDLE and LPM
1912 * is allowed.
1913 * 2. ID_GND --> ID_B: No need to reset the PHY.
1914 * HCD core clears all PORTSC bits and initializes
1915 * the controller to host mode in remove_hcd.
1916 * Restore PORTSC transceiver select bits (ULPI)
1917 * and reset the controller to change MODE bits.
1918 * PHY_ALT interrupt can not occur in host mode.
1919 */
1920 del_timer_sync(&motg->id_timer);
1921 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1922 msm_otg_link_reset(motg);
1923 msm_chg_enable_aca_intr(motg);
1924 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301925 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301926 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001927 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301928 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301930 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001931 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001932 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301933 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301934 }
1935 break;
1936 default:
1937 break;
1938 }
1939}
1940
1941static irqreturn_t msm_otg_irq(int irq, void *data)
1942{
1943 struct msm_otg *motg = data;
1944 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301946
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301947 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301948 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301949 disable_irq_nosync(irq);
1950 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301951 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301952 return IRQ_HANDLED;
1953 }
1954
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 usbsts = readl(USB_USBSTS);
1956 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301957 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301959 if (msm_chg_check_aca_intr(motg)) {
1960 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301962 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001963 return IRQ_HANDLED;
1964 }
1965
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301966 otgsc = readl(USB_OTGSC);
1967 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1968 return IRQ_NONE;
1969
1970 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301971 if (otgsc & OTGSC_ID) {
1972 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301973 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301974 } else {
1975 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301976 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301977 msm_chg_enable_aca_det(motg);
1978 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001979 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301980 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301981 if (otgsc & OTGSC_BSV) {
1982 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301983 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301984 } else {
1985 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301986 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301987 msm_chg_check_aca_intr(motg);
1988 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301990 }
1991
1992 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001993 return IRQ_HANDLED;
1994}
1995
1996static void msm_otg_set_vbus_state(int online)
1997{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301998 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001999 struct msm_otg *motg = the_msm_otg;
2000
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302001 if (online) {
2002 pr_debug("PMIC: BSV set\n");
2003 set_bit(B_SESS_VLD, &motg->inputs);
2004 } else {
2005 pr_debug("PMIC: BSV clear\n");
2006 clear_bit(B_SESS_VLD, &motg->inputs);
2007 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002008
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302009 if (!init) {
2010 init = true;
2011 complete(&pmic_vbus_init);
2012 pr_debug("PMIC: BSV init complete\n");
2013 return;
2014 }
2015
2016 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002017}
2018
2019static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2020{
2021 struct msm_otg *motg = data;
2022
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302023 if (aca_id_turned_on)
2024 return IRQ_HANDLED;
2025
2026 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2027 pr_debug("PMIC: ID set\n");
2028 set_bit(ID, &motg->inputs);
2029 } else {
2030 pr_debug("PMIC: ID clear\n");
2031 clear_bit(ID, &motg->inputs);
2032 }
2033
2034 if (motg->otg.state != OTG_STATE_UNDEFINED)
2035 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002036
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302037 return IRQ_HANDLED;
2038}
2039
2040static int msm_otg_mode_show(struct seq_file *s, void *unused)
2041{
2042 struct msm_otg *motg = s->private;
2043 struct otg_transceiver *otg = &motg->otg;
2044
2045 switch (otg->state) {
2046 case OTG_STATE_A_HOST:
2047 seq_printf(s, "host\n");
2048 break;
2049 case OTG_STATE_B_PERIPHERAL:
2050 seq_printf(s, "peripheral\n");
2051 break;
2052 default:
2053 seq_printf(s, "none\n");
2054 break;
2055 }
2056
2057 return 0;
2058}
2059
2060static int msm_otg_mode_open(struct inode *inode, struct file *file)
2061{
2062 return single_open(file, msm_otg_mode_show, inode->i_private);
2063}
2064
2065static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2066 size_t count, loff_t *ppos)
2067{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302068 struct seq_file *s = file->private_data;
2069 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302070 char buf[16];
2071 struct otg_transceiver *otg = &motg->otg;
2072 int status = count;
2073 enum usb_mode_type req_mode;
2074
2075 memset(buf, 0x00, sizeof(buf));
2076
2077 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2078 status = -EFAULT;
2079 goto out;
2080 }
2081
2082 if (!strncmp(buf, "host", 4)) {
2083 req_mode = USB_HOST;
2084 } else if (!strncmp(buf, "peripheral", 10)) {
2085 req_mode = USB_PERIPHERAL;
2086 } else if (!strncmp(buf, "none", 4)) {
2087 req_mode = USB_NONE;
2088 } else {
2089 status = -EINVAL;
2090 goto out;
2091 }
2092
2093 switch (req_mode) {
2094 case USB_NONE:
2095 switch (otg->state) {
2096 case OTG_STATE_A_HOST:
2097 case OTG_STATE_B_PERIPHERAL:
2098 set_bit(ID, &motg->inputs);
2099 clear_bit(B_SESS_VLD, &motg->inputs);
2100 break;
2101 default:
2102 goto out;
2103 }
2104 break;
2105 case USB_PERIPHERAL:
2106 switch (otg->state) {
2107 case OTG_STATE_B_IDLE:
2108 case OTG_STATE_A_HOST:
2109 set_bit(ID, &motg->inputs);
2110 set_bit(B_SESS_VLD, &motg->inputs);
2111 break;
2112 default:
2113 goto out;
2114 }
2115 break;
2116 case USB_HOST:
2117 switch (otg->state) {
2118 case OTG_STATE_B_IDLE:
2119 case OTG_STATE_B_PERIPHERAL:
2120 clear_bit(ID, &motg->inputs);
2121 break;
2122 default:
2123 goto out;
2124 }
2125 break;
2126 default:
2127 goto out;
2128 }
2129
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302130 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302131 schedule_work(&motg->sm_work);
2132out:
2133 return status;
2134}
2135
2136const struct file_operations msm_otg_mode_fops = {
2137 .open = msm_otg_mode_open,
2138 .read = seq_read,
2139 .write = msm_otg_mode_write,
2140 .llseek = seq_lseek,
2141 .release = single_release,
2142};
2143
Anji jonnalad270e2d2011-08-09 11:28:32 +05302144static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2145{
2146 struct msm_otg *motg = s->private;
2147
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302148 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302149 return 0;
2150}
2151
2152static int msm_otg_chg_open(struct inode *inode, struct file *file)
2153{
2154 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2155}
2156
2157const struct file_operations msm_otg_chg_fops = {
2158 .open = msm_otg_chg_open,
2159 .read = seq_read,
2160 .llseek = seq_lseek,
2161 .release = single_release,
2162};
2163
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302164static int msm_otg_aca_show(struct seq_file *s, void *unused)
2165{
2166 if (debug_aca_enabled)
2167 seq_printf(s, "enabled\n");
2168 else
2169 seq_printf(s, "disabled\n");
2170
2171 return 0;
2172}
2173
2174static int msm_otg_aca_open(struct inode *inode, struct file *file)
2175{
2176 return single_open(file, msm_otg_aca_show, inode->i_private);
2177}
2178
2179static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2180 size_t count, loff_t *ppos)
2181{
2182 char buf[8];
2183
2184 memset(buf, 0x00, sizeof(buf));
2185
2186 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2187 return -EFAULT;
2188
2189 if (!strncmp(buf, "enable", 6))
2190 debug_aca_enabled = true;
2191 else
2192 debug_aca_enabled = false;
2193
2194 return count;
2195}
2196
2197const struct file_operations msm_otg_aca_fops = {
2198 .open = msm_otg_aca_open,
2199 .read = seq_read,
2200 .write = msm_otg_aca_write,
2201 .llseek = seq_lseek,
2202 .release = single_release,
2203};
2204
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302205static struct dentry *msm_otg_dbg_root;
2206static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302207static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302208static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302209
2210static int msm_otg_debugfs_init(struct msm_otg *motg)
2211{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302212
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302213 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2214
2215 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2216 return -ENODEV;
2217
Anji jonnalad270e2d2011-08-09 11:28:32 +05302218 if (motg->pdata->mode == USB_OTG &&
2219 motg->pdata->otg_control == OTG_USER_CONTROL) {
2220
2221 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2222 S_IWUSR, msm_otg_dbg_root, motg,
2223 &msm_otg_mode_fops);
2224
2225 if (!msm_otg_dbg_mode) {
2226 debugfs_remove(msm_otg_dbg_root);
2227 msm_otg_dbg_root = NULL;
2228 return -ENODEV;
2229 }
2230 }
2231
2232 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2233 msm_otg_dbg_root, motg,
2234 &msm_otg_chg_fops);
2235
2236 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302237 debugfs_remove_recursive(msm_otg_dbg_root);
2238 return -ENODEV;
2239 }
2240
2241 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2242 msm_otg_dbg_root, motg,
2243 &msm_otg_aca_fops);
2244
2245 if (!msm_otg_dbg_aca) {
2246 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302247 return -ENODEV;
2248 }
2249
2250 return 0;
2251}
2252
2253static void msm_otg_debugfs_cleanup(void)
2254{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302255 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302256}
2257
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302258static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2259static struct platform_device *msm_otg_add_pdev(
2260 struct platform_device *ofdev, const char *name)
2261{
2262 struct platform_device *pdev;
2263 const struct resource *res = ofdev->resource;
2264 unsigned int num = ofdev->num_resources;
2265 int retval;
2266
2267 pdev = platform_device_alloc(name, -1);
2268 if (!pdev) {
2269 retval = -ENOMEM;
2270 goto error;
2271 }
2272
2273 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2274 pdev->dev.dma_mask = &msm_otg_dma_mask;
2275
2276 if (num) {
2277 retval = platform_device_add_resources(pdev, res, num);
2278 if (retval)
2279 goto error;
2280 }
2281
2282 retval = platform_device_add(pdev);
2283 if (retval)
2284 goto error;
2285
2286 return pdev;
2287
2288error:
2289 platform_device_put(pdev);
2290 return ERR_PTR(retval);
2291}
2292
2293static int msm_otg_setup_devices(struct platform_device *ofdev,
2294 enum usb_mode_type mode, bool init)
2295{
2296 const char *gadget_name = "msm_hsusb";
2297 const char *host_name = "msm_hsusb_host";
2298 static struct platform_device *gadget_pdev;
2299 static struct platform_device *host_pdev;
2300 int retval = 0;
2301
2302 if (!init) {
2303 if (gadget_pdev)
2304 platform_device_unregister(gadget_pdev);
2305 if (host_pdev)
2306 platform_device_unregister(host_pdev);
2307 return 0;
2308 }
2309
2310 switch (mode) {
2311 case USB_OTG:
2312 /* fall through */
2313 case USB_PERIPHERAL:
2314 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2315 if (IS_ERR(gadget_pdev)) {
2316 retval = PTR_ERR(gadget_pdev);
2317 break;
2318 }
2319 if (mode == USB_PERIPHERAL)
2320 break;
2321 /* fall through */
2322 case USB_HOST:
2323 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2324 if (IS_ERR(host_pdev)) {
2325 retval = PTR_ERR(host_pdev);
2326 if (mode == USB_OTG)
2327 platform_device_unregister(gadget_pdev);
2328 }
2329 break;
2330 default:
2331 break;
2332 }
2333
2334 return retval;
2335}
2336
2337struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2338{
2339 struct device_node *node = pdev->dev.of_node;
2340 struct msm_otg_platform_data *pdata;
2341 int len = 0;
2342
2343 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2344 if (!pdata) {
2345 pr_err("unable to allocate platform data\n");
2346 return NULL;
2347 }
2348 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2349 if (len) {
2350 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2351 if (!pdata->phy_init_seq)
2352 return NULL;
2353 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2354 pdata->phy_init_seq,
2355 len/sizeof(*pdata->phy_init_seq));
2356 }
2357 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2358 &pdata->power_budget);
2359 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2360 &pdata->mode);
2361 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2362 &pdata->otg_control);
2363 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2364 &pdata->default_mode);
2365 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2366 &pdata->phy_type);
2367 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2368 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302369 return pdata;
2370}
2371
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302372static int __init msm_otg_probe(struct platform_device *pdev)
2373{
2374 int ret = 0;
2375 struct resource *res;
2376 struct msm_otg *motg;
2377 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302378 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302379
2380 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302381
2382 if (pdev->dev.of_node) {
2383 dev_dbg(&pdev->dev, "device tree enabled\n");
2384 pdata = msm_otg_dt_to_pdata(pdev);
2385 if (!pdata)
2386 return -ENOMEM;
2387 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2388 if (ret) {
2389 dev_err(&pdev->dev, "devices setup failed\n");
2390 return ret;
2391 }
2392 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302393 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2394 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302395 } else {
2396 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302397 }
2398
2399 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2400 if (!motg) {
2401 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2402 return -ENOMEM;
2403 }
2404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002405 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302406 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302407 otg = &motg->otg;
2408 otg->dev = &pdev->dev;
2409
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302410 /*
2411 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2412 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2413 * PMIC can detect ACA ID_GND and generate an interrupt.
2414 */
2415 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2416 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2417 ret = -EINVAL;
2418 goto free_motg;
2419 }
2420
Ofir Cohen4da266f2012-01-03 10:19:29 +02002421 /* initialize reset counter */
2422 motg->reset_counter = 0;
2423
Amit Blay02eff132011-09-21 16:46:24 +03002424 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002425 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002426 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002427 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302428
Manu Gautam5143b252012-01-05 19:25:23 -08002429 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302430 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002431 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302432 ret = PTR_ERR(motg->clk);
2433 goto put_phy_reset_clk;
2434 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302435 clk_set_rate(motg->clk, 60000000);
2436
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302437 /* pm qos request to prevent apps idle power collapse */
2438 if (motg->pdata->swfi_latency)
2439 pm_qos_add_request(&motg->pm_qos_req_dma,
2440 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002441
Anji jonnala0f73cac2011-05-04 10:19:46 +05302442 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002443 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302444 * CORE CLK must be running at >55Mhz for correct HSUSB
2445 * operation and USB core cannot tolerate frequency changes on
2446 * CORE CLK. For such USB cores, vote for maximum clk frequency
2447 * on pclk source
2448 */
Manu Gautam5143b252012-01-05 19:25:23 -08002449 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2450 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302451 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002452 dev_err(&pdev->dev, "failed to get core_clk\n");
2453 ret = PTR_ERR(motg->clk);
2454 goto put_clk;
2455 }
2456 clk_set_rate(motg->core_clk, INT_MAX);
2457
2458 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2459 if (IS_ERR(motg->pclk)) {
2460 dev_err(&pdev->dev, "failed to get iface_clk\n");
2461 ret = PTR_ERR(motg->pclk);
2462 goto put_core_clk;
2463 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302464
2465 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2466 if (!res) {
2467 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2468 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002469 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302470 }
2471
2472 motg->regs = ioremap(res->start, resource_size(res));
2473 if (!motg->regs) {
2474 dev_err(&pdev->dev, "ioremap failed\n");
2475 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002476 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302477 }
2478 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2479
2480 motg->irq = platform_get_irq(pdev, 0);
2481 if (!motg->irq) {
2482 dev_err(&pdev->dev, "platform_get_irq failed\n");
2483 ret = -ENODEV;
2484 goto free_regs;
2485 }
2486
Stephen Boyd48a32912012-01-09 19:47:17 -08002487 motg->xo_handle = msm_xo_get(MSM_XO_CXO, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08002488 if (IS_ERR(motg->xo_handle)) {
2489 dev_err(&pdev->dev, "%s not able to get the handle "
2490 "to vote for TCXO D0 buffer\n", __func__);
2491 ret = PTR_ERR(motg->xo_handle);
2492 goto free_regs;
2493 }
2494
2495 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2496 if (ret) {
2497 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2498 "D0 buffer%d\n", __func__, ret);
2499 goto free_xo_handle;
2500 }
2501
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302502 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302503
2504 ret = msm_hsusb_init_vddcx(motg, 1);
2505 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002506 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002507 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302508 }
2509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002510 ret = msm_hsusb_config_vddcx(1);
2511 if (ret) {
2512 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2513 goto free_init_vddcx;
2514 }
2515
Anji jonnala11aa5c42011-05-04 10:19:48 +05302516 ret = msm_hsusb_ldo_init(motg, 1);
2517 if (ret) {
2518 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302520 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002521
2522 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302523 if (ret) {
2524 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002525 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302526 }
Manu Gautam5143b252012-01-05 19:25:23 -08002527 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302528
2529 writel(0, USB_USBINTR);
2530 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002531 /* Ensure that above STOREs are completed before enabling interrupts */
2532 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002534 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302535 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302536 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302537 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2538 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302539 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2540 "msm_otg", motg);
2541 if (ret) {
2542 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002543 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302544 }
2545
2546 otg->init = msm_otg_reset;
2547 otg->set_host = msm_otg_set_host;
2548 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302549 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302550 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302551
2552 otg->io_ops = &msm_otg_io_ops;
2553
2554 ret = otg_set_transceiver(&motg->otg);
2555 if (ret) {
2556 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2557 goto free_irq;
2558 }
2559
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302560 if (motg->pdata->mode == USB_OTG &&
2561 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002562 if (motg->pdata->pmic_id_irq) {
2563 ret = request_irq(motg->pdata->pmic_id_irq,
2564 msm_pmic_id_irq,
2565 IRQF_TRIGGER_RISING |
2566 IRQF_TRIGGER_FALLING,
2567 "msm_otg", motg);
2568 if (ret) {
2569 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2570 goto remove_otg;
2571 }
2572 } else {
2573 ret = -ENODEV;
2574 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2575 goto remove_otg;
2576 }
2577 }
2578
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302579 msm_hsusb_mhl_switch_enable(motg, 1);
2580
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302581 platform_set_drvdata(pdev, motg);
2582 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002583 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302584
Anji jonnalad270e2d2011-08-09 11:28:32 +05302585 ret = msm_otg_debugfs_init(motg);
2586 if (ret)
2587 dev_dbg(&pdev->dev, "mode debugfs file is"
2588 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302589
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002590 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2591 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2592
Amit Blay58b31472011-11-18 09:39:39 +02002593 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2594 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302595 (!(motg->pdata->mode == USB_OTG) ||
2596 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002597 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002598 ALLOW_PHY_RETENTION |
2599 ALLOW_PHY_COMP_DISABLE;
2600
Amit Blay58b31472011-11-18 09:39:39 +02002601 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2602 motg->caps = ALLOW_PHY_RETENTION;
2603 }
2604
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002605 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302606 pm_runtime_set_active(&pdev->dev);
2607 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302608
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302609 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002610
2611remove_otg:
2612 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302613free_irq:
2614 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002615destroy_wlock:
2616 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002617 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002618 msm_hsusb_ldo_enable(motg, 0);
2619free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302620 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302622 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002623devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002624 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002625 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2626free_xo_handle:
2627 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302628free_regs:
2629 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002630put_pclk:
2631 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302632put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002633 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302634put_clk:
2635 clk_put(motg->clk);
2636put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002637 if (!IS_ERR(motg->phy_reset_clk))
2638 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302639free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302640 if (motg->pdata->swfi_latency)
2641 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302642 kfree(motg);
2643 return ret;
2644}
2645
2646static int __devexit msm_otg_remove(struct platform_device *pdev)
2647{
2648 struct msm_otg *motg = platform_get_drvdata(pdev);
2649 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302650 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302651
2652 if (otg->host || otg->gadget)
2653 return -EBUSY;
2654
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302655 if (pdev->dev.of_node)
2656 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002657 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2658 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302659 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302660 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302661 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302662
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302663 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302664
2665 device_init_wakeup(&pdev->dev, 0);
2666 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002667 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302668
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302669 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002670 if (motg->pdata->pmic_id_irq)
2671 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302672 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302673 free_irq(motg->irq, motg);
2674
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302675 /*
2676 * Put PHY in low power mode.
2677 */
2678 ulpi_read(otg, 0x14);
2679 ulpi_write(otg, 0x08, 0x09);
2680
2681 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2682 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2683 if (readl(USB_PORTSC) & PORTSC_PHCD)
2684 break;
2685 udelay(1);
2686 cnt++;
2687 }
2688 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2689 dev_err(otg->dev, "Unable to suspend PHY\n");
2690
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302691 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002692 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002693 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002694 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302695 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002696 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302697
2698 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302699 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302700
Amit Blay02eff132011-09-21 16:46:24 +03002701 if (!IS_ERR(motg->phy_reset_clk))
2702 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302703 clk_put(motg->pclk);
2704 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002705 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302706
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302707 if (motg->pdata->swfi_latency)
2708 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302709
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302710 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302711 return 0;
2712}
2713
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302714#ifdef CONFIG_PM_RUNTIME
2715static int msm_otg_runtime_idle(struct device *dev)
2716{
2717 struct msm_otg *motg = dev_get_drvdata(dev);
2718 struct otg_transceiver *otg = &motg->otg;
2719
2720 dev_dbg(dev, "OTG runtime idle\n");
2721
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302722 if (otg->state == OTG_STATE_UNDEFINED)
2723 return -EAGAIN;
2724 else
2725 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302726}
2727
2728static int msm_otg_runtime_suspend(struct device *dev)
2729{
2730 struct msm_otg *motg = dev_get_drvdata(dev);
2731
2732 dev_dbg(dev, "OTG runtime suspend\n");
2733 return msm_otg_suspend(motg);
2734}
2735
2736static int msm_otg_runtime_resume(struct device *dev)
2737{
2738 struct msm_otg *motg = dev_get_drvdata(dev);
2739
2740 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302741 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742 return msm_otg_resume(motg);
2743}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302744#endif
2745
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302746#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302747static int msm_otg_pm_suspend(struct device *dev)
2748{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302749 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302750
2751 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302752
2753#ifdef CONFIG_PM_RUNTIME
2754 ret = pm_runtime_suspend(dev);
2755 if (ret > 0)
2756 ret = 0;
2757#else
2758 ret = msm_otg_suspend(dev_get_drvdata(dev));
2759#endif
2760 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302761}
2762
2763static int msm_otg_pm_resume(struct device *dev)
2764{
2765 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302766
2767 dev_dbg(dev, "OTG PM resume\n");
2768
Manu Gautamf284c052011-09-08 16:52:48 +05302769#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302770 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302771 * Do not resume hardware as part of system resume,
2772 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302773 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002774 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302775#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302776
Manu Gautamf284c052011-09-08 16:52:48 +05302777 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302778}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302779#endif
2780
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302781#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302782static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302783 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2784 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2785 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302786};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302787#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302788
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302789static struct of_device_id msm_otg_dt_match[] = {
2790 { .compatible = "qcom,hsusb-otg",
2791 },
2792 {}
2793};
2794
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302795static struct platform_driver msm_otg_driver = {
2796 .remove = __devexit_p(msm_otg_remove),
2797 .driver = {
2798 .name = DRIVER_NAME,
2799 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302800#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302801 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302802#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302803 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302804 },
2805};
2806
2807static int __init msm_otg_init(void)
2808{
2809 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2810}
2811
2812static void __exit msm_otg_exit(void)
2813{
2814 platform_driver_unregister(&msm_otg_driver);
2815}
2816
2817module_init(msm_otg_init);
2818module_exit(msm_otg_exit);
2819
2820MODULE_LICENSE("GPL v2");
2821MODULE_DESCRIPTION("MSM USB transceiver driver");