blob: 3d739aef4a5f0e438e3df8c1683482054d267e61 [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;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301595 bool is_dcd = false, 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);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301602 if (motg->pdata->enable_dcd)
1603 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001604 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301605 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1606 motg->dcd_retries = 0;
1607 delay = MSM_CHG_DCD_POLL_TIME;
1608 break;
1609 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 is_aca = msm_chg_aca_detect(motg);
1611 if (is_aca) {
1612 /*
1613 * ID_A can be ACA dock too. continue
1614 * primary detection after DCD.
1615 */
1616 if (test_bit(ID_A, &motg->inputs)) {
1617 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1618 } else {
1619 delay = 0;
1620 break;
1621 }
1622 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301623 if (motg->pdata->enable_dcd)
1624 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301625 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1626 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301627 if (motg->pdata->enable_dcd)
1628 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301629 msm_chg_enable_primary_det(motg);
1630 delay = MSM_CHG_PRIMARY_DET_TIME;
1631 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1632 } else {
1633 delay = MSM_CHG_DCD_POLL_TIME;
1634 }
1635 break;
1636 case USB_CHG_STATE_DCD_DONE:
1637 vout = msm_chg_check_primary_det(motg);
1638 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301639 if (test_bit(ID_A, &motg->inputs)) {
1640 motg->chg_type = USB_ACA_DOCK_CHARGER;
1641 motg->chg_state = USB_CHG_STATE_DETECTED;
1642 delay = 0;
1643 break;
1644 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301645 msm_chg_enable_secondary_det(motg);
1646 delay = MSM_CHG_SECONDARY_DET_TIME;
1647 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1648 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301649 if (test_bit(ID_A, &motg->inputs)) {
1650 motg->chg_type = USB_ACA_A_CHARGER;
1651 motg->chg_state = USB_CHG_STATE_DETECTED;
1652 delay = 0;
1653 break;
1654 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301655 motg->chg_type = USB_SDP_CHARGER;
1656 motg->chg_state = USB_CHG_STATE_DETECTED;
1657 delay = 0;
1658 }
1659 break;
1660 case USB_CHG_STATE_PRIMARY_DONE:
1661 vout = msm_chg_check_secondary_det(motg);
1662 if (vout)
1663 motg->chg_type = USB_DCP_CHARGER;
1664 else
1665 motg->chg_type = USB_CDP_CHARGER;
1666 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1667 /* fall through */
1668 case USB_CHG_STATE_SECONDARY_DONE:
1669 motg->chg_state = USB_CHG_STATE_DETECTED;
1670 case USB_CHG_STATE_DETECTED:
1671 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001672 msm_chg_enable_aca_det(motg);
1673 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301674 dev_dbg(otg->dev, "chg_type = %s\n",
1675 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301676 schedule_work(&motg->sm_work);
1677 return;
1678 default:
1679 return;
1680 }
1681
1682 schedule_delayed_work(&motg->chg_work, delay);
1683}
1684
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301685/*
1686 * We support OTG, Peripheral only and Host only configurations. In case
1687 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1688 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1689 * enabled when switch is controlled by user and default mode is supplied
1690 * by board file, which can be changed by userspace later.
1691 */
1692static void msm_otg_init_sm(struct msm_otg *motg)
1693{
1694 struct msm_otg_platform_data *pdata = motg->pdata;
1695 u32 otgsc = readl(USB_OTGSC);
1696
1697 switch (pdata->mode) {
1698 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001699 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301700 if (pdata->default_mode == USB_HOST) {
1701 clear_bit(ID, &motg->inputs);
1702 } else if (pdata->default_mode == USB_PERIPHERAL) {
1703 set_bit(ID, &motg->inputs);
1704 set_bit(B_SESS_VLD, &motg->inputs);
1705 } else {
1706 set_bit(ID, &motg->inputs);
1707 clear_bit(B_SESS_VLD, &motg->inputs);
1708 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301709 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1710 if (otgsc & OTGSC_ID)
1711 set_bit(ID, &motg->inputs);
1712 else
1713 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001714 if (otgsc & OTGSC_BSV)
1715 set_bit(B_SESS_VLD, &motg->inputs);
1716 else
1717 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301718 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301719 if (pdata->pmic_id_irq) {
1720 if (irq_read_line(pdata->pmic_id_irq))
1721 set_bit(ID, &motg->inputs);
1722 else
1723 clear_bit(ID, &motg->inputs);
1724 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301725 /*
1726 * VBUS initial state is reported after PMIC
1727 * driver initialization. Wait for it.
1728 */
1729 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301730 }
1731 break;
1732 case USB_HOST:
1733 clear_bit(ID, &motg->inputs);
1734 break;
1735 case USB_PERIPHERAL:
1736 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301737 if (pdata->otg_control == OTG_PHY_CONTROL) {
1738 if (otgsc & OTGSC_BSV)
1739 set_bit(B_SESS_VLD, &motg->inputs);
1740 else
1741 clear_bit(B_SESS_VLD, &motg->inputs);
1742 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1743 /*
1744 * VBUS initial state is reported after PMIC
1745 * driver initialization. Wait for it.
1746 */
1747 wait_for_completion(&pmic_vbus_init);
1748 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301749 break;
1750 default:
1751 break;
1752 }
1753}
1754
1755static void msm_otg_sm_work(struct work_struct *w)
1756{
1757 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1758 struct otg_transceiver *otg = &motg->otg;
1759
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301760 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301761 switch (otg->state) {
1762 case OTG_STATE_UNDEFINED:
1763 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1764 msm_otg_reset(otg);
1765 msm_otg_init_sm(motg);
1766 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301767 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1768 test_bit(ID, &motg->inputs)) {
1769 pm_runtime_put_noidle(otg->dev);
1770 pm_runtime_suspend(otg->dev);
1771 break;
1772 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301773 /* FALL THROUGH */
1774 case OTG_STATE_B_IDLE:
1775 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001776 if ((!test_bit(ID, &motg->inputs) ||
1777 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1779 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301780 IDEV_ACA_CHG_MAX);
1781 else if (test_bit(ID_A, &motg->inputs))
1782 msm_otg_notify_charger(motg,
1783 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301784 else
1785 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301786 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301787 /*
1788 * Link can not generate PHY_ALT interrupt
1789 * in host mode when no device is attached
1790 * to the port. It is also observed PHY_ALT
1791 * interrupt missing upon Micro-A cable disconnect.
1792 * Hence disable PHY_ALT interrupt and perform
1793 * polling to detect RID change.
1794 */
1795 msm_chg_enable_aca_det(motg);
1796 msm_chg_disable_aca_intr(motg);
1797 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301798 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1800 switch (motg->chg_state) {
1801 case USB_CHG_STATE_UNDEFINED:
1802 msm_chg_detect_work(&motg->chg_work.work);
1803 break;
1804 case USB_CHG_STATE_DETECTED:
1805 switch (motg->chg_type) {
1806 case USB_DCP_CHARGER:
1807 msm_otg_notify_charger(motg,
1808 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301809 pm_runtime_put_noidle(otg->dev);
1810 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301811 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301812 case USB_ACA_B_CHARGER:
1813 msm_otg_notify_charger(motg,
1814 IDEV_ACA_CHG_MAX);
1815 /*
1816 * (ID_B --> ID_C) PHY_ALT interrupt can
1817 * not be detected in LPM.
1818 */
1819 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301820 case USB_CDP_CHARGER:
1821 msm_otg_notify_charger(motg,
1822 IDEV_CHG_MAX);
1823 msm_otg_start_peripheral(otg, 1);
1824 otg->state = OTG_STATE_B_PERIPHERAL;
1825 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301826 case USB_ACA_C_CHARGER:
1827 msm_otg_notify_charger(motg,
1828 IDEV_ACA_CHG_MAX);
1829 msm_otg_start_peripheral(otg, 1);
1830 otg->state = OTG_STATE_B_PERIPHERAL;
1831 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301832 case USB_SDP_CHARGER:
1833 msm_otg_notify_charger(motg, IUNIT);
1834 msm_otg_start_peripheral(otg, 1);
1835 otg->state = OTG_STATE_B_PERIPHERAL;
1836 break;
1837 default:
1838 break;
1839 }
1840 break;
1841 default:
1842 break;
1843 }
1844 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301845 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301846 msm_otg_notify_charger(motg, 0);
1847 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1848 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301849 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301850 pm_runtime_put_noidle(otg->dev);
1851 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301852 }
1853 break;
1854 case OTG_STATE_B_PERIPHERAL:
1855 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1856 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001857 !test_bit(ID, &motg->inputs) ||
1858 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301859 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001860 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001861 schedule_work(w);
1862 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301863 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001864 }
1865 break;
1866 case OTG_STATE_A_HOST:
1867 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1868 if (test_bit(ID, &motg->inputs) &&
1869 !test_bit(ID_A, &motg->inputs)) {
1870 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301871 msm_hsusb_vbus_power(motg, 0);
1872 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301873 /*
1874 * Exit point of host mode.
1875 *
1876 * 1. Micro-A cable disconnect: Just schedule
1877 * the work. PHY is reset in B_IDLE and LPM
1878 * is allowed.
1879 * 2. ID_GND --> ID_B: No need to reset the PHY.
1880 * HCD core clears all PORTSC bits and initializes
1881 * the controller to host mode in remove_hcd.
1882 * Restore PORTSC transceiver select bits (ULPI)
1883 * and reset the controller to change MODE bits.
1884 * PHY_ALT interrupt can not occur in host mode.
1885 */
1886 del_timer_sync(&motg->id_timer);
1887 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1888 msm_otg_link_reset(motg);
1889 msm_chg_enable_aca_intr(motg);
1890 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301891 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301892 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301894 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001895 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301896 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001897 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001898 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301899 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301900 }
1901 break;
1902 default:
1903 break;
1904 }
1905}
1906
1907static irqreturn_t msm_otg_irq(int irq, void *data)
1908{
1909 struct msm_otg *motg = data;
1910 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001911 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301912
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301913 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301914 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301915 disable_irq_nosync(irq);
1916 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301917 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301918 return IRQ_HANDLED;
1919 }
1920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001921 usbsts = readl(USB_USBSTS);
1922 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301923 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301925 if (msm_chg_check_aca_intr(motg)) {
1926 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001927 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301928 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 return IRQ_HANDLED;
1930 }
1931
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301932 otgsc = readl(USB_OTGSC);
1933 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1934 return IRQ_NONE;
1935
1936 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301937 if (otgsc & OTGSC_ID) {
1938 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301939 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301940 } else {
1941 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301942 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301943 msm_chg_enable_aca_det(motg);
1944 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301946 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301947 if (otgsc & OTGSC_BSV) {
1948 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301949 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301950 } else {
1951 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301952 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301953 msm_chg_check_aca_intr(motg);
1954 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001955 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301956 }
1957
1958 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 return IRQ_HANDLED;
1960}
1961
1962static void msm_otg_set_vbus_state(int online)
1963{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301964 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001965 struct msm_otg *motg = the_msm_otg;
1966
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301967 if (online) {
1968 pr_debug("PMIC: BSV set\n");
1969 set_bit(B_SESS_VLD, &motg->inputs);
1970 } else {
1971 pr_debug("PMIC: BSV clear\n");
1972 clear_bit(B_SESS_VLD, &motg->inputs);
1973 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001974
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301975 if (!init) {
1976 init = true;
1977 complete(&pmic_vbus_init);
1978 pr_debug("PMIC: BSV init complete\n");
1979 return;
1980 }
1981
1982 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983}
1984
1985static irqreturn_t msm_pmic_id_irq(int irq, void *data)
1986{
1987 struct msm_otg *motg = data;
1988
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301989 if (aca_id_turned_on)
1990 return IRQ_HANDLED;
1991
1992 if (irq_read_line(motg->pdata->pmic_id_irq)) {
1993 pr_debug("PMIC: ID set\n");
1994 set_bit(ID, &motg->inputs);
1995 } else {
1996 pr_debug("PMIC: ID clear\n");
1997 clear_bit(ID, &motg->inputs);
1998 }
1999
2000 if (motg->otg.state != OTG_STATE_UNDEFINED)
2001 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302003 return IRQ_HANDLED;
2004}
2005
2006static int msm_otg_mode_show(struct seq_file *s, void *unused)
2007{
2008 struct msm_otg *motg = s->private;
2009 struct otg_transceiver *otg = &motg->otg;
2010
2011 switch (otg->state) {
2012 case OTG_STATE_A_HOST:
2013 seq_printf(s, "host\n");
2014 break;
2015 case OTG_STATE_B_PERIPHERAL:
2016 seq_printf(s, "peripheral\n");
2017 break;
2018 default:
2019 seq_printf(s, "none\n");
2020 break;
2021 }
2022
2023 return 0;
2024}
2025
2026static int msm_otg_mode_open(struct inode *inode, struct file *file)
2027{
2028 return single_open(file, msm_otg_mode_show, inode->i_private);
2029}
2030
2031static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2032 size_t count, loff_t *ppos)
2033{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302034 struct seq_file *s = file->private_data;
2035 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302036 char buf[16];
2037 struct otg_transceiver *otg = &motg->otg;
2038 int status = count;
2039 enum usb_mode_type req_mode;
2040
2041 memset(buf, 0x00, sizeof(buf));
2042
2043 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2044 status = -EFAULT;
2045 goto out;
2046 }
2047
2048 if (!strncmp(buf, "host", 4)) {
2049 req_mode = USB_HOST;
2050 } else if (!strncmp(buf, "peripheral", 10)) {
2051 req_mode = USB_PERIPHERAL;
2052 } else if (!strncmp(buf, "none", 4)) {
2053 req_mode = USB_NONE;
2054 } else {
2055 status = -EINVAL;
2056 goto out;
2057 }
2058
2059 switch (req_mode) {
2060 case USB_NONE:
2061 switch (otg->state) {
2062 case OTG_STATE_A_HOST:
2063 case OTG_STATE_B_PERIPHERAL:
2064 set_bit(ID, &motg->inputs);
2065 clear_bit(B_SESS_VLD, &motg->inputs);
2066 break;
2067 default:
2068 goto out;
2069 }
2070 break;
2071 case USB_PERIPHERAL:
2072 switch (otg->state) {
2073 case OTG_STATE_B_IDLE:
2074 case OTG_STATE_A_HOST:
2075 set_bit(ID, &motg->inputs);
2076 set_bit(B_SESS_VLD, &motg->inputs);
2077 break;
2078 default:
2079 goto out;
2080 }
2081 break;
2082 case USB_HOST:
2083 switch (otg->state) {
2084 case OTG_STATE_B_IDLE:
2085 case OTG_STATE_B_PERIPHERAL:
2086 clear_bit(ID, &motg->inputs);
2087 break;
2088 default:
2089 goto out;
2090 }
2091 break;
2092 default:
2093 goto out;
2094 }
2095
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302096 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302097 schedule_work(&motg->sm_work);
2098out:
2099 return status;
2100}
2101
2102const struct file_operations msm_otg_mode_fops = {
2103 .open = msm_otg_mode_open,
2104 .read = seq_read,
2105 .write = msm_otg_mode_write,
2106 .llseek = seq_lseek,
2107 .release = single_release,
2108};
2109
Anji jonnalad270e2d2011-08-09 11:28:32 +05302110static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2111{
2112 struct msm_otg *motg = s->private;
2113
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302114 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302115 return 0;
2116}
2117
2118static int msm_otg_chg_open(struct inode *inode, struct file *file)
2119{
2120 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2121}
2122
2123const struct file_operations msm_otg_chg_fops = {
2124 .open = msm_otg_chg_open,
2125 .read = seq_read,
2126 .llseek = seq_lseek,
2127 .release = single_release,
2128};
2129
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302130static int msm_otg_aca_show(struct seq_file *s, void *unused)
2131{
2132 if (debug_aca_enabled)
2133 seq_printf(s, "enabled\n");
2134 else
2135 seq_printf(s, "disabled\n");
2136
2137 return 0;
2138}
2139
2140static int msm_otg_aca_open(struct inode *inode, struct file *file)
2141{
2142 return single_open(file, msm_otg_aca_show, inode->i_private);
2143}
2144
2145static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2146 size_t count, loff_t *ppos)
2147{
2148 char buf[8];
2149
2150 memset(buf, 0x00, sizeof(buf));
2151
2152 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2153 return -EFAULT;
2154
2155 if (!strncmp(buf, "enable", 6))
2156 debug_aca_enabled = true;
2157 else
2158 debug_aca_enabled = false;
2159
2160 return count;
2161}
2162
2163const struct file_operations msm_otg_aca_fops = {
2164 .open = msm_otg_aca_open,
2165 .read = seq_read,
2166 .write = msm_otg_aca_write,
2167 .llseek = seq_lseek,
2168 .release = single_release,
2169};
2170
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302171static struct dentry *msm_otg_dbg_root;
2172static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302173static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302174static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302175
2176static int msm_otg_debugfs_init(struct msm_otg *motg)
2177{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302178
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302179 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2180
2181 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2182 return -ENODEV;
2183
Anji jonnalad270e2d2011-08-09 11:28:32 +05302184 if (motg->pdata->mode == USB_OTG &&
2185 motg->pdata->otg_control == OTG_USER_CONTROL) {
2186
2187 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2188 S_IWUSR, msm_otg_dbg_root, motg,
2189 &msm_otg_mode_fops);
2190
2191 if (!msm_otg_dbg_mode) {
2192 debugfs_remove(msm_otg_dbg_root);
2193 msm_otg_dbg_root = NULL;
2194 return -ENODEV;
2195 }
2196 }
2197
2198 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2199 msm_otg_dbg_root, motg,
2200 &msm_otg_chg_fops);
2201
2202 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302203 debugfs_remove_recursive(msm_otg_dbg_root);
2204 return -ENODEV;
2205 }
2206
2207 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2208 msm_otg_dbg_root, motg,
2209 &msm_otg_aca_fops);
2210
2211 if (!msm_otg_dbg_aca) {
2212 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302213 return -ENODEV;
2214 }
2215
2216 return 0;
2217}
2218
2219static void msm_otg_debugfs_cleanup(void)
2220{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302221 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302222}
2223
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302224static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2225static struct platform_device *msm_otg_add_pdev(
2226 struct platform_device *ofdev, const char *name)
2227{
2228 struct platform_device *pdev;
2229 const struct resource *res = ofdev->resource;
2230 unsigned int num = ofdev->num_resources;
2231 int retval;
2232
2233 pdev = platform_device_alloc(name, -1);
2234 if (!pdev) {
2235 retval = -ENOMEM;
2236 goto error;
2237 }
2238
2239 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2240 pdev->dev.dma_mask = &msm_otg_dma_mask;
2241
2242 if (num) {
2243 retval = platform_device_add_resources(pdev, res, num);
2244 if (retval)
2245 goto error;
2246 }
2247
2248 retval = platform_device_add(pdev);
2249 if (retval)
2250 goto error;
2251
2252 return pdev;
2253
2254error:
2255 platform_device_put(pdev);
2256 return ERR_PTR(retval);
2257}
2258
2259static int msm_otg_setup_devices(struct platform_device *ofdev,
2260 enum usb_mode_type mode, bool init)
2261{
2262 const char *gadget_name = "msm_hsusb";
2263 const char *host_name = "msm_hsusb_host";
2264 static struct platform_device *gadget_pdev;
2265 static struct platform_device *host_pdev;
2266 int retval = 0;
2267
2268 if (!init) {
2269 if (gadget_pdev)
2270 platform_device_unregister(gadget_pdev);
2271 if (host_pdev)
2272 platform_device_unregister(host_pdev);
2273 return 0;
2274 }
2275
2276 switch (mode) {
2277 case USB_OTG:
2278 /* fall through */
2279 case USB_PERIPHERAL:
2280 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2281 if (IS_ERR(gadget_pdev)) {
2282 retval = PTR_ERR(gadget_pdev);
2283 break;
2284 }
2285 if (mode == USB_PERIPHERAL)
2286 break;
2287 /* fall through */
2288 case USB_HOST:
2289 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2290 if (IS_ERR(host_pdev)) {
2291 retval = PTR_ERR(host_pdev);
2292 if (mode == USB_OTG)
2293 platform_device_unregister(gadget_pdev);
2294 }
2295 break;
2296 default:
2297 break;
2298 }
2299
2300 return retval;
2301}
2302
2303struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2304{
2305 struct device_node *node = pdev->dev.of_node;
2306 struct msm_otg_platform_data *pdata;
2307 int len = 0;
2308
2309 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2310 if (!pdata) {
2311 pr_err("unable to allocate platform data\n");
2312 return NULL;
2313 }
2314 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2315 if (len) {
2316 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2317 if (!pdata->phy_init_seq)
2318 return NULL;
2319 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2320 pdata->phy_init_seq,
2321 len/sizeof(*pdata->phy_init_seq));
2322 }
2323 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2324 &pdata->power_budget);
2325 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2326 &pdata->mode);
2327 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2328 &pdata->otg_control);
2329 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2330 &pdata->default_mode);
2331 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2332 &pdata->phy_type);
2333 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2334 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302335 return pdata;
2336}
2337
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302338static int __init msm_otg_probe(struct platform_device *pdev)
2339{
2340 int ret = 0;
2341 struct resource *res;
2342 struct msm_otg *motg;
2343 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302344 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302345
2346 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302347
2348 if (pdev->dev.of_node) {
2349 dev_dbg(&pdev->dev, "device tree enabled\n");
2350 pdata = msm_otg_dt_to_pdata(pdev);
2351 if (!pdata)
2352 return -ENOMEM;
2353 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2354 if (ret) {
2355 dev_err(&pdev->dev, "devices setup failed\n");
2356 return ret;
2357 }
2358 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302359 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2360 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302361 } else {
2362 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302363 }
2364
2365 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2366 if (!motg) {
2367 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2368 return -ENOMEM;
2369 }
2370
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002371 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302372 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302373 otg = &motg->otg;
2374 otg->dev = &pdev->dev;
2375
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302376 /*
2377 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2378 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2379 * PMIC can detect ACA ID_GND and generate an interrupt.
2380 */
2381 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2382 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2383 ret = -EINVAL;
2384 goto free_motg;
2385 }
2386
Ofir Cohen4da266f2012-01-03 10:19:29 +02002387 /* initialize reset counter */
2388 motg->reset_counter = 0;
2389
Amit Blay02eff132011-09-21 16:46:24 +03002390 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002391 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002392 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002393 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302394
Manu Gautam5143b252012-01-05 19:25:23 -08002395 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302396 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002397 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302398 ret = PTR_ERR(motg->clk);
2399 goto put_phy_reset_clk;
2400 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302401 clk_set_rate(motg->clk, 60000000);
2402
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302403 /* pm qos request to prevent apps idle power collapse */
2404 if (motg->pdata->swfi_latency)
2405 pm_qos_add_request(&motg->pm_qos_req_dma,
2406 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002407
Anji jonnala0f73cac2011-05-04 10:19:46 +05302408 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002409 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302410 * CORE CLK must be running at >55Mhz for correct HSUSB
2411 * operation and USB core cannot tolerate frequency changes on
2412 * CORE CLK. For such USB cores, vote for maximum clk frequency
2413 * on pclk source
2414 */
Manu Gautam5143b252012-01-05 19:25:23 -08002415 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2416 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302417 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002418 dev_err(&pdev->dev, "failed to get core_clk\n");
2419 ret = PTR_ERR(motg->clk);
2420 goto put_clk;
2421 }
2422 clk_set_rate(motg->core_clk, INT_MAX);
2423
2424 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2425 if (IS_ERR(motg->pclk)) {
2426 dev_err(&pdev->dev, "failed to get iface_clk\n");
2427 ret = PTR_ERR(motg->pclk);
2428 goto put_core_clk;
2429 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302430
2431 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2432 if (!res) {
2433 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2434 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002435 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302436 }
2437
2438 motg->regs = ioremap(res->start, resource_size(res));
2439 if (!motg->regs) {
2440 dev_err(&pdev->dev, "ioremap failed\n");
2441 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002442 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302443 }
2444 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2445
2446 motg->irq = platform_get_irq(pdev, 0);
2447 if (!motg->irq) {
2448 dev_err(&pdev->dev, "platform_get_irq failed\n");
2449 ret = -ENODEV;
2450 goto free_regs;
2451 }
2452
Stephen Boyd48a32912012-01-09 19:47:17 -08002453 motg->xo_handle = msm_xo_get(MSM_XO_CXO, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08002454 if (IS_ERR(motg->xo_handle)) {
2455 dev_err(&pdev->dev, "%s not able to get the handle "
2456 "to vote for TCXO D0 buffer\n", __func__);
2457 ret = PTR_ERR(motg->xo_handle);
2458 goto free_regs;
2459 }
2460
2461 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2462 if (ret) {
2463 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2464 "D0 buffer%d\n", __func__, ret);
2465 goto free_xo_handle;
2466 }
2467
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302468 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302469
2470 ret = msm_hsusb_init_vddcx(motg, 1);
2471 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002473 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302474 }
2475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476 ret = msm_hsusb_config_vddcx(1);
2477 if (ret) {
2478 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2479 goto free_init_vddcx;
2480 }
2481
Anji jonnala11aa5c42011-05-04 10:19:48 +05302482 ret = msm_hsusb_ldo_init(motg, 1);
2483 if (ret) {
2484 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002485 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302486 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002487
2488 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302489 if (ret) {
2490 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302492 }
Manu Gautam5143b252012-01-05 19:25:23 -08002493 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302494
2495 writel(0, USB_USBINTR);
2496 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 /* Ensure that above STOREs are completed before enabling interrupts */
2498 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302499
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002500 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302501 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302502 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302503 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2504 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302505 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2506 "msm_otg", motg);
2507 if (ret) {
2508 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302510 }
2511
2512 otg->init = msm_otg_reset;
2513 otg->set_host = msm_otg_set_host;
2514 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302515 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302516 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302517
2518 otg->io_ops = &msm_otg_io_ops;
2519
2520 ret = otg_set_transceiver(&motg->otg);
2521 if (ret) {
2522 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2523 goto free_irq;
2524 }
2525
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302526 if (motg->pdata->mode == USB_OTG &&
2527 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002528 if (motg->pdata->pmic_id_irq) {
2529 ret = request_irq(motg->pdata->pmic_id_irq,
2530 msm_pmic_id_irq,
2531 IRQF_TRIGGER_RISING |
2532 IRQF_TRIGGER_FALLING,
2533 "msm_otg", motg);
2534 if (ret) {
2535 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2536 goto remove_otg;
2537 }
2538 } else {
2539 ret = -ENODEV;
2540 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2541 goto remove_otg;
2542 }
2543 }
2544
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302545 msm_hsusb_mhl_switch_enable(motg, 1);
2546
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302547 platform_set_drvdata(pdev, motg);
2548 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002549 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302550
Anji jonnalad270e2d2011-08-09 11:28:32 +05302551 ret = msm_otg_debugfs_init(motg);
2552 if (ret)
2553 dev_dbg(&pdev->dev, "mode debugfs file is"
2554 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002556 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2557 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2558
Amit Blay58b31472011-11-18 09:39:39 +02002559 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2560 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302561 (!(motg->pdata->mode == USB_OTG) ||
2562 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002563 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002564 ALLOW_PHY_RETENTION |
2565 ALLOW_PHY_COMP_DISABLE;
2566
Amit Blay58b31472011-11-18 09:39:39 +02002567 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2568 motg->caps = ALLOW_PHY_RETENTION;
2569 }
2570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002571 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302572 pm_runtime_set_active(&pdev->dev);
2573 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302574
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302575 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002576
2577remove_otg:
2578 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302579free_irq:
2580 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581destroy_wlock:
2582 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002583 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002584 msm_hsusb_ldo_enable(motg, 0);
2585free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302586 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002587free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302588 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002589devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002590 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002591 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2592free_xo_handle:
2593 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302594free_regs:
2595 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002596put_pclk:
2597 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302598put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002599 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302600put_clk:
2601 clk_put(motg->clk);
2602put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002603 if (!IS_ERR(motg->phy_reset_clk))
2604 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302605free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302606 if (motg->pdata->swfi_latency)
2607 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302608 kfree(motg);
2609 return ret;
2610}
2611
2612static int __devexit msm_otg_remove(struct platform_device *pdev)
2613{
2614 struct msm_otg *motg = platform_get_drvdata(pdev);
2615 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302616 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302617
2618 if (otg->host || otg->gadget)
2619 return -EBUSY;
2620
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302621 if (pdev->dev.of_node)
2622 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002623 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2624 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302625 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302626 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302627 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302628
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302629 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302630
2631 device_init_wakeup(&pdev->dev, 0);
2632 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302634
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302635 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002636 if (motg->pdata->pmic_id_irq)
2637 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302638 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302639 free_irq(motg->irq, motg);
2640
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302641 /*
2642 * Put PHY in low power mode.
2643 */
2644 ulpi_read(otg, 0x14);
2645 ulpi_write(otg, 0x08, 0x09);
2646
2647 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2648 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2649 if (readl(USB_PORTSC) & PORTSC_PHCD)
2650 break;
2651 udelay(1);
2652 cnt++;
2653 }
2654 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2655 dev_err(otg->dev, "Unable to suspend PHY\n");
2656
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302657 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002658 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002659 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002660 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302661 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002662 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302663
2664 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302665 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302666
Amit Blay02eff132011-09-21 16:46:24 +03002667 if (!IS_ERR(motg->phy_reset_clk))
2668 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302669 clk_put(motg->pclk);
2670 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002671 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302672
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302673 if (motg->pdata->swfi_latency)
2674 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302675
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302676 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302677 return 0;
2678}
2679
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302680#ifdef CONFIG_PM_RUNTIME
2681static int msm_otg_runtime_idle(struct device *dev)
2682{
2683 struct msm_otg *motg = dev_get_drvdata(dev);
2684 struct otg_transceiver *otg = &motg->otg;
2685
2686 dev_dbg(dev, "OTG runtime idle\n");
2687
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302688 if (otg->state == OTG_STATE_UNDEFINED)
2689 return -EAGAIN;
2690 else
2691 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302692}
2693
2694static int msm_otg_runtime_suspend(struct device *dev)
2695{
2696 struct msm_otg *motg = dev_get_drvdata(dev);
2697
2698 dev_dbg(dev, "OTG runtime suspend\n");
2699 return msm_otg_suspend(motg);
2700}
2701
2702static int msm_otg_runtime_resume(struct device *dev)
2703{
2704 struct msm_otg *motg = dev_get_drvdata(dev);
2705
2706 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302707 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302708 return msm_otg_resume(motg);
2709}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302710#endif
2711
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302712#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302713static int msm_otg_pm_suspend(struct device *dev)
2714{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302715 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302716
2717 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302718
2719#ifdef CONFIG_PM_RUNTIME
2720 ret = pm_runtime_suspend(dev);
2721 if (ret > 0)
2722 ret = 0;
2723#else
2724 ret = msm_otg_suspend(dev_get_drvdata(dev));
2725#endif
2726 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302727}
2728
2729static int msm_otg_pm_resume(struct device *dev)
2730{
2731 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302732
2733 dev_dbg(dev, "OTG PM resume\n");
2734
Manu Gautamf284c052011-09-08 16:52:48 +05302735#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302736 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302737 * Do not resume hardware as part of system resume,
2738 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302739 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002740 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302741#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742
Manu Gautamf284c052011-09-08 16:52:48 +05302743 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302744}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302745#endif
2746
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302747#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302748static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302749 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2750 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2751 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302752};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302753#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302754
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302755static struct of_device_id msm_otg_dt_match[] = {
2756 { .compatible = "qcom,hsusb-otg",
2757 },
2758 {}
2759};
2760
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302761static struct platform_driver msm_otg_driver = {
2762 .remove = __devexit_p(msm_otg_remove),
2763 .driver = {
2764 .name = DRIVER_NAME,
2765 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302766#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302767 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302768#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302769 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302770 },
2771};
2772
2773static int __init msm_otg_init(void)
2774{
2775 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2776}
2777
2778static void __exit msm_otg_exit(void)
2779{
2780 platform_driver_unregister(&msm_otg_driver);
2781}
2782
2783module_init(msm_otg_init);
2784module_exit(msm_otg_exit);
2785
2786MODULE_LICENSE("GPL v2");
2787MODULE_DESCRIPTION("MSM USB transceiver driver");