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