blob: eb76b302fedf933745f0842152daba08e1e07f1e [file] [log] [blame]
Manu Gautam5143b252012-01-05 19:25:23 -08001/* Copyright (c) 2009-2012, Code Aurora Forum. All rights reserved.
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053012 */
13
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/platform_device.h>
17#include <linux/clk.h>
18#include <linux/slab.h>
19#include <linux/interrupt.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/io.h>
23#include <linux/ioport.h>
24#include <linux/uaccess.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
Pavankumar Kondeti87c01042010-12-07 17:53:58 +053027#include <linux/pm_runtime.h>
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +053028#include <linux/of.h>
29#include <linux/dma-mapping.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053030
31#include <linux/usb.h>
32#include <linux/usb/otg.h>
33#include <linux/usb/ulpi.h>
34#include <linux/usb/gadget.h>
35#include <linux/usb/hcd.h>
36#include <linux/usb/msm_hsusb.h>
37#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053038#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#include <linux/mfd/pm8xxx/pm8921-charger.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053040#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020041#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053042
43#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080044#include <mach/msm_xo.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053045
46#define MSM_USB_BASE (motg->regs)
47#define DRIVER_NAME "msm_otg"
48
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053049#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053051
52#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
53#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
54#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
55#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
56
57#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
58#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
59#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
60#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
61
Vamsi Krishna132b2762011-11-11 16:09:20 -080062#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053063#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
64
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053065static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070066static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053067static bool debug_aca_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053069/* Prevent idle power collapse(pc) while operating in peripheral mode */
70static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
71{
72 struct msm_otg_platform_data *pdata = dev->pdata;
73 u32 swfi_latency = 0;
74
75 if (!pdata || !pdata->swfi_latency)
76 return;
77
78 swfi_latency = pdata->swfi_latency + 1;
79
80 if (vote)
81 pm_qos_update_request(&dev->pm_qos_req_dma,
82 swfi_latency);
83 else
84 pm_qos_update_request(&dev->pm_qos_req_dma,
85 PM_QOS_DEFAULT_VALUE);
86}
87
Anji jonnala11aa5c42011-05-04 10:19:48 +053088static struct regulator *hsusb_3p3;
89static struct regulator *hsusb_1p8;
90static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053091static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053092
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053093static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053094static inline bool aca_enabled(void)
95{
96#ifdef CONFIG_USB_MSM_ACA
97 return true;
98#else
99 return debug_aca_enabled;
100#endif
101}
102
Anji jonnala11aa5c42011-05-04 10:19:48 +0530103static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
104{
105 int ret = 0;
106
107 if (init) {
108 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
109 if (IS_ERR(hsusb_vddcx)) {
110 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
111 return PTR_ERR(hsusb_vddcx);
112 }
113
114 ret = regulator_set_voltage(hsusb_vddcx,
115 USB_PHY_VDD_DIG_VOL_MIN,
116 USB_PHY_VDD_DIG_VOL_MAX);
117 if (ret) {
118 dev_err(motg->otg.dev, "unable to set the voltage "
119 "for hsusb vddcx\n");
120 regulator_put(hsusb_vddcx);
121 return ret;
122 }
123
124 ret = regulator_enable(hsusb_vddcx);
125 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700126 regulator_set_voltage(hsusb_vddcx, 0,
127 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530128 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
130 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 return ret;
139 }
140
141 ret = regulator_set_voltage(hsusb_vddcx, 0,
142 USB_PHY_VDD_DIG_VOL_MIN);
143 if (ret) {
144 dev_err(motg->otg.dev, "unable to set the voltage"
145 "for hsusb vddcx\n");
146 return ret;
147 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530148
149 regulator_put(hsusb_vddcx);
150 }
151
152 return ret;
153}
154
155static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
156{
157 int rc = 0;
158
159 if (init) {
160 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
161 if (IS_ERR(hsusb_3p3)) {
162 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
163 return PTR_ERR(hsusb_3p3);
164 }
165
166 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
167 USB_PHY_3P3_VOL_MAX);
168 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 dev_err(motg->otg.dev, "unable to set voltage level for"
170 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 goto put_3p3;
172 }
173 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
174 if (IS_ERR(hsusb_1p8)) {
175 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
176 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 }
179 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
180 USB_PHY_1P8_VOL_MAX);
181 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 dev_err(motg->otg.dev, "unable to set voltage level for"
183 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530184 goto put_1p8;
185 }
186
187 return 0;
188 }
189
Anji jonnala11aa5c42011-05-04 10:19:48 +0530190put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530192 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193put_3p3_lpm:
194 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530195put_3p3:
196 regulator_put(hsusb_3p3);
197 return rc;
198}
199
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530200#ifdef CONFIG_PM_SLEEP
201#define USB_PHY_SUSP_DIG_VOL 500000
202static int msm_hsusb_config_vddcx(int high)
203{
204 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
205 int min_vol;
206 int ret;
207
208 if (high)
209 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
210 else
211 min_vol = USB_PHY_SUSP_DIG_VOL;
212
213 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
214 if (ret) {
215 pr_err("%s: unable to set the voltage for regulator "
216 "HSUSB_VDDCX\n", __func__);
217 return ret;
218 }
219
220 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
221
222 return ret;
223}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700224#else
225static int msm_hsusb_config_vddcx(int high)
226{
227 return 0;
228}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530229#endif
230
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700231static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530232{
233 int ret = 0;
234
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530235 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530236 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
237 return -ENODEV;
238 }
239
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530240 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530241 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
242 return -ENODEV;
243 }
244
245 if (on) {
246 ret = regulator_set_optimum_mode(hsusb_1p8,
247 USB_PHY_1P8_HPM_LOAD);
248 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530250 "HSUSB_1p8\n", __func__);
251 return ret;
252 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253
254 ret = regulator_enable(hsusb_1p8);
255 if (ret) {
256 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
257 __func__);
258 regulator_set_optimum_mode(hsusb_1p8, 0);
259 return ret;
260 }
261
Anji jonnala11aa5c42011-05-04 10:19:48 +0530262 ret = regulator_set_optimum_mode(hsusb_3p3,
263 USB_PHY_3P3_HPM_LOAD);
264 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700265 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 regulator_set_optimum_mode(hsusb_1p8, 0);
268 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530269 return ret;
270 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700271
272 ret = regulator_enable(hsusb_3p3);
273 if (ret) {
274 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
275 __func__);
276 regulator_set_optimum_mode(hsusb_3p3, 0);
277 regulator_set_optimum_mode(hsusb_1p8, 0);
278 regulator_disable(hsusb_1p8);
279 return ret;
280 }
281
Anji jonnala11aa5c42011-05-04 10:19:48 +0530282 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 ret = regulator_disable(hsusb_1p8);
284 if (ret) {
285 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
286 __func__);
287 return ret;
288 }
289
290 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530291 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700292 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530293 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294
295 ret = regulator_disable(hsusb_3p3);
296 if (ret) {
297 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
298 __func__);
299 return ret;
300 }
301 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530302 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700303 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530304 "HSUSB_3p3\n", __func__);
305 }
306
307 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
308 return ret < 0 ? ret : 0;
309}
310
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530311static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
312{
313 static struct regulator *mhl_analog_switch;
314 struct msm_otg_platform_data *pdata = motg->pdata;
315
316 if (!pdata->mhl_enable)
317 return;
318
319 if (on) {
320 mhl_analog_switch = regulator_get(motg->otg.dev,
321 "mhl_ext_3p3v");
322 if (IS_ERR(mhl_analog_switch)) {
323 pr_err("Unable to get mhl_analog_switch\n");
324 return;
325 }
326
327 if (regulator_enable(mhl_analog_switch)) {
328 pr_err("unable to enable mhl_analog_switch\n");
329 goto put_analog_switch;
330 }
331 return;
332 }
333
334 regulator_disable(mhl_analog_switch);
335put_analog_switch:
336 regulator_put(mhl_analog_switch);
337}
338
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530339static int ulpi_read(struct otg_transceiver *otg, u32 reg)
340{
341 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
342 int cnt = 0;
343
344 /* initiate read operation */
345 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
346 USB_ULPI_VIEWPORT);
347
348 /* wait for completion */
349 while (cnt < ULPI_IO_TIMEOUT_USEC) {
350 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
351 break;
352 udelay(1);
353 cnt++;
354 }
355
356 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
357 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
358 readl(USB_ULPI_VIEWPORT));
359 return -ETIMEDOUT;
360 }
361 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
362}
363
364static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
365{
366 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
367 int cnt = 0;
368
369 /* initiate write operation */
370 writel(ULPI_RUN | ULPI_WRITE |
371 ULPI_ADDR(reg) | ULPI_DATA(val),
372 USB_ULPI_VIEWPORT);
373
374 /* wait for completion */
375 while (cnt < ULPI_IO_TIMEOUT_USEC) {
376 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
377 break;
378 udelay(1);
379 cnt++;
380 }
381
382 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
383 dev_err(otg->dev, "ulpi_write: timeout\n");
384 return -ETIMEDOUT;
385 }
386 return 0;
387}
388
389static struct otg_io_access_ops msm_otg_io_ops = {
390 .read = ulpi_read,
391 .write = ulpi_write,
392};
393
394static void ulpi_init(struct msm_otg *motg)
395{
396 struct msm_otg_platform_data *pdata = motg->pdata;
397 int *seq = pdata->phy_init_seq;
398
399 if (!seq)
400 return;
401
402 while (seq[0] >= 0) {
403 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
404 seq[0], seq[1]);
405 ulpi_write(&motg->otg, seq[0], seq[1]);
406 seq += 2;
407 }
408}
409
410static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
411{
412 int ret;
413
414 if (assert) {
415 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
416 if (ret)
417 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
418 } else {
419 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
420 if (ret)
421 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
422 }
423 return ret;
424}
425
426static int msm_otg_phy_clk_reset(struct msm_otg *motg)
427{
428 int ret;
429
Amit Blay02eff132011-09-21 16:46:24 +0300430 if (IS_ERR(motg->phy_reset_clk))
431 return 0;
432
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530433 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
434 if (ret) {
435 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
436 return ret;
437 }
438 usleep_range(10000, 12000);
439 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
440 if (ret)
441 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
442 return ret;
443}
444
445static int msm_otg_phy_reset(struct msm_otg *motg)
446{
447 u32 val;
448 int ret;
449 int retries;
450
451 ret = msm_otg_link_clk_reset(motg, 1);
452 if (ret)
453 return ret;
454 ret = msm_otg_phy_clk_reset(motg);
455 if (ret)
456 return ret;
457 ret = msm_otg_link_clk_reset(motg, 0);
458 if (ret)
459 return ret;
460
461 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
462 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
463
464 for (retries = 3; retries > 0; retries--) {
465 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
466 ULPI_CLR(ULPI_FUNC_CTRL));
467 if (!ret)
468 break;
469 ret = msm_otg_phy_clk_reset(motg);
470 if (ret)
471 return ret;
472 }
473 if (!retries)
474 return -ETIMEDOUT;
475
476 /* This reset calibrates the phy, if the above write succeeded */
477 ret = msm_otg_phy_clk_reset(motg);
478 if (ret)
479 return ret;
480
481 for (retries = 3; retries > 0; retries--) {
482 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
483 if (ret != -ETIMEDOUT)
484 break;
485 ret = msm_otg_phy_clk_reset(motg);
486 if (ret)
487 return ret;
488 }
489 if (!retries)
490 return -ETIMEDOUT;
491
492 dev_info(motg->otg.dev, "phy_reset: success\n");
493 return 0;
494}
495
496#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497static int msm_otg_link_reset(struct msm_otg *motg)
498{
499 int cnt = 0;
500
501 writel_relaxed(USBCMD_RESET, USB_USBCMD);
502 while (cnt < LINK_RESET_TIMEOUT_USEC) {
503 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
504 break;
505 udelay(1);
506 cnt++;
507 }
508 if (cnt >= LINK_RESET_TIMEOUT_USEC)
509 return -ETIMEDOUT;
510
511 /* select ULPI phy */
512 writel_relaxed(0x80000000, USB_PORTSC);
513 writel_relaxed(0x0, USB_AHBBURST);
514 writel_relaxed(0x00, USB_AHBMODE);
515
516 return 0;
517}
518
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530519static int msm_otg_reset(struct otg_transceiver *otg)
520{
521 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
522 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530523 int ret;
524 u32 val = 0;
525 u32 ulpi_val = 0;
526
Ofir Cohen4da266f2012-01-03 10:19:29 +0200527 /*
528 * USB PHY and Link reset also reset the USB BAM.
529 * Thus perform reset operation only once to avoid
530 * USB BAM reset on other cases e.g. USB cable disconnections.
531 */
532 if (pdata->disable_reset_on_disconnect) {
533 if (motg->reset_counter)
534 return 0;
535 else
536 motg->reset_counter++;
537 }
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539 clk_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530540 ret = msm_otg_phy_reset(motg);
541 if (ret) {
542 dev_err(otg->dev, "phy_reset failed\n");
543 return ret;
544 }
545
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530546 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530547 ret = msm_otg_link_reset(motg);
548 if (ret) {
549 dev_err(otg->dev, "link reset failed\n");
550 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530551 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530552 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530553
554 ulpi_init(motg);
555
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556 /* Ensure that RESET operation is completed before turning off clock */
557 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 clk_disable(motg->clk);
560
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530561 if (pdata->otg_control == OTG_PHY_CONTROL) {
562 val = readl_relaxed(USB_OTGSC);
563 if (pdata->mode == USB_OTG) {
564 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
565 val |= OTGSC_IDIE | OTGSC_BSVIE;
566 } else if (pdata->mode == USB_PERIPHERAL) {
567 ulpi_val = ULPI_INT_SESS_VALID;
568 val |= OTGSC_BSVIE;
569 }
570 writel_relaxed(val, USB_OTGSC);
571 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
572 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530573 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700574
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530575 return 0;
576}
577
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530578static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
579{
580 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
581
582 /*
583 * Allow bus suspend only for host mode. Device mode bus suspend
584 * is not implemented yet.
585 */
586 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530587 /*
588 * ID_GND --> ID_A transition can not be detected in LPM.
589 * Disallow host bus suspend when ACA is enabled.
590 */
591 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530592 pm_runtime_put(otg->dev);
593 else
594 pm_runtime_resume(otg->dev);
595 }
596
597 return 0;
598}
599
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530600#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530601#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
602
603#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530604static int msm_otg_suspend(struct msm_otg *motg)
605{
606 struct otg_transceiver *otg = &motg->otg;
607 struct usb_bus *bus = otg->host;
608 struct msm_otg_platform_data *pdata = motg->pdata;
609 int cnt = 0;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530610 bool host_bus_suspend;
611 u32 phy_ctrl_val = 0, cmd_val;
Anji jonnala7da3f262011-12-02 17:22:14 -0800612 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530613 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530614
615 if (atomic_read(&motg->in_lpm))
616 return 0;
617
618 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530619 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530620 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530621 * Chipidea 45-nm PHY suspend sequence:
622 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530623 * Interrupt Latch Register auto-clear feature is not present
624 * in all PHY versions. Latch register is clear on read type.
625 * Clear latch register to avoid spurious wakeup from
626 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530627 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530628 * PHY comparators are disabled when PHY enters into low power
629 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
630 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
631 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530632 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530633 * PLL is not turned off when PHY enters into low power mode (LPM).
634 * Disable PLL for maximum power savings.
635 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530636
637 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
638 ulpi_read(otg, 0x14);
639 if (pdata->otg_control == OTG_PHY_CONTROL)
640 ulpi_write(otg, 0x01, 0x30);
641 ulpi_write(otg, 0x08, 0x09);
642 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530643
644 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700645 * Turn off the OTG comparators, if depends on PMIC for
646 * VBUS and ID notifications.
647 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530648 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 ulpi_write(otg, OTG_COMP_DISABLE,
650 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
651 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
652 }
653
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530654 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530655 * PHY may take some time or even fail to enter into low power
656 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
657 * in failure case.
658 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530659 portsc = readl_relaxed(USB_PORTSC);
660 if (!(portsc & PORTSC_PHCD)) {
661 writel_relaxed(portsc | PORTSC_PHCD,
662 USB_PORTSC);
663 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
664 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
665 break;
666 udelay(1);
667 cnt++;
668 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530669 }
670
671 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
672 dev_err(otg->dev, "Unable to suspend PHY\n");
673 msm_otg_reset(otg);
674 enable_irq(motg->irq);
675 return -ETIMEDOUT;
676 }
677
678 /*
679 * PHY has capability to generate interrupt asynchronously in low
680 * power mode (LPM). This interrupt is level triggered. So USB IRQ
681 * line must be disabled till async interrupt enable bit is cleared
682 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
683 * block data communication from PHY.
684 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530685 cmd_val = readl_relaxed(USB_USBCMD);
686 if (host_bus_suspend)
687 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
688 else
689 cmd_val |= ULPI_STP_CTRL;
690 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530691
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530692 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend) {
Amit Blay58b31472011-11-18 09:39:39 +0200693 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
694 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
695 /* Enable PHY HV interrupts to wake MPM/Link */
696 phy_ctrl_val |=
697 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
698
699 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700700 motg->lpm_flags |= PHY_RETENTIONED;
701 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530702
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700703 /* Ensure that above operation is completed before turning off clocks */
704 mb();
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530705 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -0800706 clk_disable(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530707
Anji jonnala7da3f262011-12-02 17:22:14 -0800708 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
709 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
710 if (ret)
711 dev_err(otg->dev, "%s failed to devote for "
712 "TCXO D0 buffer%d\n", __func__, ret);
713
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530714 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 msm_hsusb_ldo_enable(motg, 0);
716 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530717 }
718
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530719 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530721 msm_hsusb_mhl_switch_enable(motg, 0);
722 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723
724 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530725 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700726 if (motg->pdata->pmic_id_irq)
727 enable_irq_wake(motg->pdata->pmic_id_irq);
728 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530729 if (bus)
730 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
731
732 atomic_set(&motg->in_lpm, 1);
733 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530735
736 dev_info(otg->dev, "USB in low power mode\n");
737
738 return 0;
739}
740
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530741static int msm_otg_resume(struct msm_otg *motg)
742{
743 struct otg_transceiver *otg = &motg->otg;
744 struct usb_bus *bus = otg->host;
745 int cnt = 0;
746 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200747 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800748 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749
750 if (!atomic_read(&motg->in_lpm))
751 return 0;
752
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700753 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800754
755 /* Vote for TCXO when waking up the phy */
756 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
757 if (ret)
758 dev_err(otg->dev, "%s failed to vote for "
759 "TCXO D0 buffer%d\n", __func__, ret);
760
Manu Gautam5143b252012-01-05 19:25:23 -0800761 clk_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200762
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 clk_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530764
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700765 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
766 msm_hsusb_ldo_enable(motg, 1);
767 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
768 }
769
770 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530771 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530772 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200773 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
774 phy_ctrl_val |= PHY_RETEN;
775 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
776 /* Disable PHY HV interrupts */
777 phy_ctrl_val &=
778 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
779 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530781 }
782
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530783 temp = readl(USB_USBCMD);
784 temp &= ~ASYNC_INTR_CTRL;
785 temp &= ~ULPI_STP_CTRL;
786 writel(temp, USB_USBCMD);
787
788 /*
789 * PHY comes out of low power mode (LPM) in case of wakeup
790 * from asynchronous interrupt.
791 */
792 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
793 goto skip_phy_resume;
794
795 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
796 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
797 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
798 break;
799 udelay(1);
800 cnt++;
801 }
802
803 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
804 /*
805 * This is a fatal error. Reset the link and
806 * PHY. USB state can not be restored. Re-insertion
807 * of USB cable is the only way to get USB working.
808 */
809 dev_err(otg->dev, "Unable to resume USB."
810 "Re-plugin the cable\n");
811 msm_otg_reset(otg);
812 }
813
814skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 /* Turn on the OTG comparators on resume */
816 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
817 ulpi_write(otg, OTG_COMP_DISABLE,
818 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
819 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
820 }
821 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530822 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 if (motg->pdata->pmic_id_irq)
824 disable_irq_wake(motg->pdata->pmic_id_irq);
825 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530826 if (bus)
827 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
828
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530829 atomic_set(&motg->in_lpm, 0);
830
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 if (motg->async_int) {
832 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530833 enable_irq(motg->irq);
834 }
835
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530836 dev_info(otg->dev, "USB exited from low power mode\n");
837
838 return 0;
839}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530840#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530842static int msm_otg_notify_chg_type(struct msm_otg *motg)
843{
844 static int charger_type;
845 /*
846 * TODO
847 * Unify OTG driver charger types and power supply charger types
848 */
849 if (charger_type == motg->chg_type)
850 return 0;
851
852 if (motg->chg_type == USB_SDP_CHARGER)
853 charger_type = POWER_SUPPLY_TYPE_USB;
854 else if (motg->chg_type == USB_CDP_CHARGER)
855 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
856 else if (motg->chg_type == USB_DCP_CHARGER)
857 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
858 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
859 motg->chg_type == USB_ACA_A_CHARGER ||
860 motg->chg_type == USB_ACA_B_CHARGER ||
861 motg->chg_type == USB_ACA_C_CHARGER))
862 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
863 else
864 charger_type = POWER_SUPPLY_TYPE_BATTERY;
865
866 return pm8921_set_usb_power_supply_type(charger_type);
867}
868
Amit Blay0f7edf72012-01-15 10:11:27 +0200869static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
870{
871 struct power_supply *psy;
872
873 psy = power_supply_get_by_name("usb");
874 if (!psy)
875 goto psy_not_supported;
876
877 if (motg->cur_power == 0 && mA > 0) {
878 /* Enable charging */
879 if (power_supply_set_online(psy, true))
880 goto psy_not_supported;
881 } else if (motg->cur_power > 0 && mA == 0) {
882 /* Disable charging */
883 if (power_supply_set_online(psy, false))
884 goto psy_not_supported;
885 return 0;
886 }
887 /* Set max current limit */
888 if (power_supply_set_current_limit(psy, 1000*mA))
889 goto psy_not_supported;
890
891 return 0;
892
893psy_not_supported:
894 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
895 return -ENXIO;
896}
897
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530898static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
899{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530900 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
901 motg->chg_type == USB_ACA_A_CHARGER ||
902 motg->chg_type == USB_ACA_B_CHARGER ||
903 motg->chg_type == USB_ACA_C_CHARGER) &&
904 mA > IDEV_ACA_CHG_LIMIT)
905 mA = IDEV_ACA_CHG_LIMIT;
906
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530907 if (msm_otg_notify_chg_type(motg))
908 dev_err(motg->otg.dev,
909 "Failed notifying %d charger type to PMIC\n",
910 motg->chg_type);
911
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530912 if (motg->cur_power == mA)
913 return;
914
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530915 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +0200916
917 /*
918 * Use Power Supply API if supported, otherwise fallback
919 * to legacy pm8921 API.
920 */
921 if (msm_otg_notify_power_supply(motg, mA))
922 pm8921_charger_vbus_draw(mA);
923
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530924 motg->cur_power = mA;
925}
926
927static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
928{
929 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
930
931 /*
932 * Gadget driver uses set_power method to notify about the
933 * available current based on suspend/configured states.
934 *
935 * IDEV_CHG can be drawn irrespective of suspend/un-configured
936 * states when CDP/ACA is connected.
937 */
938 if (motg->chg_type == USB_SDP_CHARGER)
939 msm_otg_notify_charger(motg, mA);
940
941 return 0;
942}
943
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530944static void msm_otg_start_host(struct otg_transceiver *otg, int on)
945{
946 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
947 struct msm_otg_platform_data *pdata = motg->pdata;
948 struct usb_hcd *hcd;
949
950 if (!otg->host)
951 return;
952
953 hcd = bus_to_hcd(otg->host);
954
955 if (on) {
956 dev_dbg(otg->dev, "host on\n");
957
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530958 /*
959 * Some boards have a switch cotrolled by gpio
960 * to enable/disable internal HUB. Enable internal
961 * HUB before kicking the host.
962 */
963 if (pdata->setup_gpio)
964 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530965 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530966 } else {
967 dev_dbg(otg->dev, "host off\n");
968
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530969 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530970 /* HCD core reset all bits of PORTSC. select ULPI phy */
971 writel_relaxed(0x80000000, USB_PORTSC);
972
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530973 if (pdata->setup_gpio)
974 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530975 }
976}
977
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978static int msm_otg_usbdev_notify(struct notifier_block *self,
979 unsigned long action, void *priv)
980{
981 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530982 struct usb_device *udev = priv;
983
984 if (!aca_enabled())
985 goto out;
986
987 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
988 goto out;
989
990 if (udev->bus != motg->otg.host)
991 goto out;
992 /*
993 * Interested in devices connected directly to the root hub.
994 * ACA dock can supply IDEV_CHG irrespective devices connected
995 * on the accessory port.
996 */
997 if (!udev->parent || udev->parent->parent ||
998 motg->chg_type == USB_ACA_DOCK_CHARGER)
999 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001000
1001 switch (action) {
1002 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301003 usb_disable_autosuspend(udev);
1004 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001005 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 if (udev->actconfig)
1007 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1008 else
1009 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301010 break;
1011 case USB_DEVICE_REMOVE:
1012 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 break;
1014 default:
1015 break;
1016 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301017 if (test_bit(ID_A, &motg->inputs))
1018 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1019 motg->mA_port);
1020out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 return NOTIFY_OK;
1022}
1023
Mayank Ranae3926882011-12-26 09:47:54 +05301024static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1025{
1026 int ret;
1027 static bool vbus_is_on;
1028
1029 if (vbus_is_on == on)
1030 return;
1031
1032 if (motg->pdata->vbus_power) {
1033 motg->pdata->vbus_power(on);
1034 return;
1035 }
1036
1037 if (!vbus_otg) {
1038 pr_err("vbus_otg is NULL.");
1039 return;
1040 }
1041
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001042 /*
1043 * if entering host mode tell the charger to not draw any current
1044 * from usb - if exiting host mode let the charger draw current
1045 */
1046 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301047 if (on) {
1048 ret = regulator_enable(vbus_otg);
1049 if (ret) {
1050 pr_err("unable to enable vbus_otg\n");
1051 return;
1052 }
1053 vbus_is_on = true;
1054 } else {
1055 ret = regulator_disable(vbus_otg);
1056 if (ret) {
1057 pr_err("unable to disable vbus_otg\n");
1058 return;
1059 }
1060 vbus_is_on = false;
1061 }
1062}
1063
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301064static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1065{
1066 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1067 struct usb_hcd *hcd;
1068
1069 /*
1070 * Fail host registration if this board can support
1071 * only peripheral configuration.
1072 */
1073 if (motg->pdata->mode == USB_PERIPHERAL) {
1074 dev_info(otg->dev, "Host mode is not supported\n");
1075 return -ENODEV;
1076 }
1077
Mayank Ranae3926882011-12-26 09:47:54 +05301078 if (!motg->pdata->vbus_power && host) {
1079 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1080 if (IS_ERR(vbus_otg)) {
1081 pr_err("Unable to get vbus_otg\n");
1082 return -ENODEV;
1083 }
1084 }
1085
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301086 if (!host) {
1087 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301088 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001089 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301090 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301091 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301092 otg->host = NULL;
1093 otg->state = OTG_STATE_UNDEFINED;
1094 schedule_work(&motg->sm_work);
1095 } else {
1096 otg->host = NULL;
1097 }
1098
Mayank Ranae3926882011-12-26 09:47:54 +05301099 if (vbus_otg)
1100 regulator_put(vbus_otg);
1101
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301102 return 0;
1103 }
1104
1105 hcd = bus_to_hcd(host);
1106 hcd->power_budget = motg->pdata->power_budget;
1107
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001108 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1109 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301110 otg->host = host;
1111 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1112
1113 /*
1114 * Kick the state machine work, if peripheral is not supported
1115 * or peripheral is already registered with us.
1116 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301117 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1118 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301119 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301120 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121
1122 return 0;
1123}
1124
1125static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1126{
1127 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1128 struct msm_otg_platform_data *pdata = motg->pdata;
1129
1130 if (!otg->gadget)
1131 return;
1132
1133 if (on) {
1134 dev_dbg(otg->dev, "gadget on\n");
1135 /*
1136 * Some boards have a switch cotrolled by gpio
1137 * to enable/disable internal HUB. Disable internal
1138 * HUB before kicking the gadget.
1139 */
1140 if (pdata->setup_gpio)
1141 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301142 /*
1143 * vote for minimum dma_latency to prevent idle
1144 * power collapse(pc) while running in peripheral mode.
1145 */
1146 otg_pm_qos_update_latency(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301147 usb_gadget_vbus_connect(otg->gadget);
1148 } else {
1149 dev_dbg(otg->dev, "gadget off\n");
1150 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301151 otg_pm_qos_update_latency(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301152 if (pdata->setup_gpio)
1153 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1154 }
1155
1156}
1157
1158static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1159 struct usb_gadget *gadget)
1160{
1161 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1162
1163 /*
1164 * Fail peripheral registration if this board can support
1165 * only host configuration.
1166 */
1167 if (motg->pdata->mode == USB_HOST) {
1168 dev_info(otg->dev, "Peripheral mode is not supported\n");
1169 return -ENODEV;
1170 }
1171
1172 if (!gadget) {
1173 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301174 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301175 msm_otg_start_peripheral(otg, 0);
1176 otg->gadget = NULL;
1177 otg->state = OTG_STATE_UNDEFINED;
1178 schedule_work(&motg->sm_work);
1179 } else {
1180 otg->gadget = NULL;
1181 }
1182
1183 return 0;
1184 }
1185 otg->gadget = gadget;
1186 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1187
1188 /*
1189 * Kick the state machine work, if host is not supported
1190 * or host is already registered with us.
1191 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301192 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1193 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301194 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301195 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301196
1197 return 0;
1198}
1199
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001200static bool msm_chg_aca_detect(struct msm_otg *motg)
1201{
1202 struct otg_transceiver *otg = &motg->otg;
1203 u32 int_sts;
1204 bool ret = false;
1205
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301206 if (!aca_enabled())
1207 goto out;
1208
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1210 goto out;
1211
1212 int_sts = ulpi_read(otg, 0x87);
1213 switch (int_sts & 0x1C) {
1214 case 0x08:
1215 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1216 dev_dbg(otg->dev, "ID_A\n");
1217 motg->chg_type = USB_ACA_A_CHARGER;
1218 motg->chg_state = USB_CHG_STATE_DETECTED;
1219 clear_bit(ID_B, &motg->inputs);
1220 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301221 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222 ret = true;
1223 }
1224 break;
1225 case 0x0C:
1226 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1227 dev_dbg(otg->dev, "ID_B\n");
1228 motg->chg_type = USB_ACA_B_CHARGER;
1229 motg->chg_state = USB_CHG_STATE_DETECTED;
1230 clear_bit(ID_A, &motg->inputs);
1231 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301232 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 ret = true;
1234 }
1235 break;
1236 case 0x10:
1237 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1238 dev_dbg(otg->dev, "ID_C\n");
1239 motg->chg_type = USB_ACA_C_CHARGER;
1240 motg->chg_state = USB_CHG_STATE_DETECTED;
1241 clear_bit(ID_A, &motg->inputs);
1242 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301243 set_bit(ID, &motg->inputs);
1244 ret = true;
1245 }
1246 break;
1247 case 0x04:
1248 if (test_and_clear_bit(ID, &motg->inputs)) {
1249 dev_dbg(otg->dev, "ID_GND\n");
1250 motg->chg_type = USB_INVALID_CHARGER;
1251 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1252 clear_bit(ID_A, &motg->inputs);
1253 clear_bit(ID_B, &motg->inputs);
1254 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 ret = true;
1256 }
1257 break;
1258 default:
1259 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1260 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301261 test_and_clear_bit(ID_C, &motg->inputs) |
1262 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301264 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 motg->chg_type = USB_INVALID_CHARGER;
1266 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1267 }
1268 }
1269out:
1270 return ret;
1271}
1272
1273static void msm_chg_enable_aca_det(struct msm_otg *motg)
1274{
1275 struct otg_transceiver *otg = &motg->otg;
1276
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301277 if (!aca_enabled())
1278 return;
1279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001280 switch (motg->pdata->phy_type) {
1281 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301282 /* Disable ID_GND in link and PHY */
1283 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1284 OTGSC_IDIE), USB_OTGSC);
1285 ulpi_write(otg, 0x01, 0x0C);
1286 ulpi_write(otg, 0x10, 0x0F);
1287 ulpi_write(otg, 0x10, 0x12);
1288 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001289 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301290 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 break;
1292 default:
1293 break;
1294 }
1295}
1296
1297static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1298{
1299 struct otg_transceiver *otg = &motg->otg;
1300
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301301 if (!aca_enabled())
1302 return;
1303
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001304 switch (motg->pdata->phy_type) {
1305 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301306 /* Enable ACA Detection interrupt (on any RID change) */
1307 ulpi_write(otg, 0x01, 0x94);
1308 break;
1309 default:
1310 break;
1311 }
1312}
1313
1314static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1315{
1316 struct otg_transceiver *otg = &motg->otg;
1317
1318 if (!aca_enabled())
1319 return;
1320
1321 switch (motg->pdata->phy_type) {
1322 case SNPS_28NM_INTEGRATED_PHY:
1323 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324 break;
1325 default:
1326 break;
1327 }
1328}
1329
1330static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1331{
1332 struct otg_transceiver *otg = &motg->otg;
1333 bool ret = false;
1334
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301335 if (!aca_enabled())
1336 return ret;
1337
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001338 switch (motg->pdata->phy_type) {
1339 case SNPS_28NM_INTEGRATED_PHY:
1340 if (ulpi_read(otg, 0x91) & 1) {
1341 dev_dbg(otg->dev, "RID change\n");
1342 ulpi_write(otg, 0x01, 0x92);
1343 ret = msm_chg_aca_detect(motg);
1344 }
1345 default:
1346 break;
1347 }
1348 return ret;
1349}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301350
1351static void msm_otg_id_timer_func(unsigned long data)
1352{
1353 struct msm_otg *motg = (struct msm_otg *) data;
1354
1355 if (!aca_enabled())
1356 return;
1357
1358 if (atomic_read(&motg->in_lpm)) {
1359 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1360 return;
1361 }
1362
1363 if (msm_chg_check_aca_intr(motg)) {
1364 dev_dbg(motg->otg.dev, "timer: aca work\n");
1365 schedule_work(&motg->sm_work);
1366 }
1367
1368 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1369 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1370}
1371
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301372static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1373{
1374 struct otg_transceiver *otg = &motg->otg;
1375 u32 chg_det;
1376 bool ret = false;
1377
1378 switch (motg->pdata->phy_type) {
1379 case CI_45NM_INTEGRATED_PHY:
1380 chg_det = ulpi_read(otg, 0x34);
1381 ret = chg_det & (1 << 4);
1382 break;
1383 case SNPS_28NM_INTEGRATED_PHY:
1384 chg_det = ulpi_read(otg, 0x87);
1385 ret = chg_det & 1;
1386 break;
1387 default:
1388 break;
1389 }
1390 return ret;
1391}
1392
1393static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1394{
1395 struct otg_transceiver *otg = &motg->otg;
1396 u32 chg_det;
1397
1398 switch (motg->pdata->phy_type) {
1399 case CI_45NM_INTEGRATED_PHY:
1400 chg_det = ulpi_read(otg, 0x34);
1401 /* Turn off charger block */
1402 chg_det |= ~(1 << 1);
1403 ulpi_write(otg, chg_det, 0x34);
1404 udelay(20);
1405 /* control chg block via ULPI */
1406 chg_det &= ~(1 << 3);
1407 ulpi_write(otg, chg_det, 0x34);
1408 /* put it in host mode for enabling D- source */
1409 chg_det &= ~(1 << 2);
1410 ulpi_write(otg, chg_det, 0x34);
1411 /* Turn on chg detect block */
1412 chg_det &= ~(1 << 1);
1413 ulpi_write(otg, chg_det, 0x34);
1414 udelay(20);
1415 /* enable chg detection */
1416 chg_det &= ~(1 << 0);
1417 ulpi_write(otg, chg_det, 0x34);
1418 break;
1419 case SNPS_28NM_INTEGRATED_PHY:
1420 /*
1421 * Configure DM as current source, DP as current sink
1422 * and enable battery charging comparators.
1423 */
1424 ulpi_write(otg, 0x8, 0x85);
1425 ulpi_write(otg, 0x2, 0x85);
1426 ulpi_write(otg, 0x1, 0x85);
1427 break;
1428 default:
1429 break;
1430 }
1431}
1432
1433static bool msm_chg_check_primary_det(struct msm_otg *motg)
1434{
1435 struct otg_transceiver *otg = &motg->otg;
1436 u32 chg_det;
1437 bool ret = false;
1438
1439 switch (motg->pdata->phy_type) {
1440 case CI_45NM_INTEGRATED_PHY:
1441 chg_det = ulpi_read(otg, 0x34);
1442 ret = chg_det & (1 << 4);
1443 break;
1444 case SNPS_28NM_INTEGRATED_PHY:
1445 chg_det = ulpi_read(otg, 0x87);
1446 ret = chg_det & 1;
1447 break;
1448 default:
1449 break;
1450 }
1451 return ret;
1452}
1453
1454static void msm_chg_enable_primary_det(struct msm_otg *motg)
1455{
1456 struct otg_transceiver *otg = &motg->otg;
1457 u32 chg_det;
1458
1459 switch (motg->pdata->phy_type) {
1460 case CI_45NM_INTEGRATED_PHY:
1461 chg_det = ulpi_read(otg, 0x34);
1462 /* enable chg detection */
1463 chg_det &= ~(1 << 0);
1464 ulpi_write(otg, chg_det, 0x34);
1465 break;
1466 case SNPS_28NM_INTEGRATED_PHY:
1467 /*
1468 * Configure DP as current source, DM as current sink
1469 * and enable battery charging comparators.
1470 */
1471 ulpi_write(otg, 0x2, 0x85);
1472 ulpi_write(otg, 0x1, 0x85);
1473 break;
1474 default:
1475 break;
1476 }
1477}
1478
1479static bool msm_chg_check_dcd(struct msm_otg *motg)
1480{
1481 struct otg_transceiver *otg = &motg->otg;
1482 u32 line_state;
1483 bool ret = false;
1484
1485 switch (motg->pdata->phy_type) {
1486 case CI_45NM_INTEGRATED_PHY:
1487 line_state = ulpi_read(otg, 0x15);
1488 ret = !(line_state & 1);
1489 break;
1490 case SNPS_28NM_INTEGRATED_PHY:
1491 line_state = ulpi_read(otg, 0x87);
1492 ret = line_state & 2;
1493 break;
1494 default:
1495 break;
1496 }
1497 return ret;
1498}
1499
1500static void msm_chg_disable_dcd(struct msm_otg *motg)
1501{
1502 struct otg_transceiver *otg = &motg->otg;
1503 u32 chg_det;
1504
1505 switch (motg->pdata->phy_type) {
1506 case CI_45NM_INTEGRATED_PHY:
1507 chg_det = ulpi_read(otg, 0x34);
1508 chg_det &= ~(1 << 5);
1509 ulpi_write(otg, chg_det, 0x34);
1510 break;
1511 case SNPS_28NM_INTEGRATED_PHY:
1512 ulpi_write(otg, 0x10, 0x86);
1513 break;
1514 default:
1515 break;
1516 }
1517}
1518
1519static void msm_chg_enable_dcd(struct msm_otg *motg)
1520{
1521 struct otg_transceiver *otg = &motg->otg;
1522 u32 chg_det;
1523
1524 switch (motg->pdata->phy_type) {
1525 case CI_45NM_INTEGRATED_PHY:
1526 chg_det = ulpi_read(otg, 0x34);
1527 /* Turn on D+ current source */
1528 chg_det |= (1 << 5);
1529 ulpi_write(otg, chg_det, 0x34);
1530 break;
1531 case SNPS_28NM_INTEGRATED_PHY:
1532 /* Data contact detection enable */
1533 ulpi_write(otg, 0x10, 0x85);
1534 break;
1535 default:
1536 break;
1537 }
1538}
1539
1540static void msm_chg_block_on(struct msm_otg *motg)
1541{
1542 struct otg_transceiver *otg = &motg->otg;
1543 u32 func_ctrl, chg_det;
1544
1545 /* put the controller in non-driving mode */
1546 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1547 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1548 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1549 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1550
1551 switch (motg->pdata->phy_type) {
1552 case CI_45NM_INTEGRATED_PHY:
1553 chg_det = ulpi_read(otg, 0x34);
1554 /* control chg block via ULPI */
1555 chg_det &= ~(1 << 3);
1556 ulpi_write(otg, chg_det, 0x34);
1557 /* Turn on chg detect block */
1558 chg_det &= ~(1 << 1);
1559 ulpi_write(otg, chg_det, 0x34);
1560 udelay(20);
1561 break;
1562 case SNPS_28NM_INTEGRATED_PHY:
1563 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301564 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301565 /* Clear alt interrupt latch and enable bits */
1566 ulpi_write(otg, 0x1F, 0x92);
1567 ulpi_write(otg, 0x1F, 0x95);
1568 udelay(100);
1569 break;
1570 default:
1571 break;
1572 }
1573}
1574
1575static void msm_chg_block_off(struct msm_otg *motg)
1576{
1577 struct otg_transceiver *otg = &motg->otg;
1578 u32 func_ctrl, chg_det;
1579
1580 switch (motg->pdata->phy_type) {
1581 case CI_45NM_INTEGRATED_PHY:
1582 chg_det = ulpi_read(otg, 0x34);
1583 /* Turn off charger block */
1584 chg_det |= ~(1 << 1);
1585 ulpi_write(otg, chg_det, 0x34);
1586 break;
1587 case SNPS_28NM_INTEGRATED_PHY:
1588 /* Clear charger detecting control bits */
1589 ulpi_write(otg, 0x3F, 0x86);
1590 /* Clear alt interrupt latch and enable bits */
1591 ulpi_write(otg, 0x1F, 0x92);
1592 ulpi_write(otg, 0x1F, 0x95);
1593 break;
1594 default:
1595 break;
1596 }
1597
1598 /* put the controller in normal mode */
1599 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1600 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1601 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1602 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1603}
1604
Anji jonnalad270e2d2011-08-09 11:28:32 +05301605static const char *chg_to_string(enum usb_chg_type chg_type)
1606{
1607 switch (chg_type) {
1608 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1609 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1610 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1611 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1612 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1613 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1614 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1615 default: return "INVALID_CHARGER";
1616 }
1617}
1618
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301619#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1620#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
1621#define MSM_CHG_PRIMARY_DET_TIME (40 * HZ/1000) /* TVDPSRC_ON */
1622#define MSM_CHG_SECONDARY_DET_TIME (40 * HZ/1000) /* TVDMSRC_ON */
1623static void msm_chg_detect_work(struct work_struct *w)
1624{
1625 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1626 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301627 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301628 unsigned long delay;
1629
1630 dev_dbg(otg->dev, "chg detection work\n");
1631 switch (motg->chg_state) {
1632 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301633 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301634 if (motg->pdata->enable_dcd)
1635 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301637 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1638 motg->dcd_retries = 0;
1639 delay = MSM_CHG_DCD_POLL_TIME;
1640 break;
1641 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001642 is_aca = msm_chg_aca_detect(motg);
1643 if (is_aca) {
1644 /*
1645 * ID_A can be ACA dock too. continue
1646 * primary detection after DCD.
1647 */
1648 if (test_bit(ID_A, &motg->inputs)) {
1649 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1650 } else {
1651 delay = 0;
1652 break;
1653 }
1654 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301655 if (motg->pdata->enable_dcd)
1656 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301657 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1658 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301659 if (motg->pdata->enable_dcd)
1660 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301661 msm_chg_enable_primary_det(motg);
1662 delay = MSM_CHG_PRIMARY_DET_TIME;
1663 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1664 } else {
1665 delay = MSM_CHG_DCD_POLL_TIME;
1666 }
1667 break;
1668 case USB_CHG_STATE_DCD_DONE:
1669 vout = msm_chg_check_primary_det(motg);
1670 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301671 if (test_bit(ID_A, &motg->inputs)) {
1672 motg->chg_type = USB_ACA_DOCK_CHARGER;
1673 motg->chg_state = USB_CHG_STATE_DETECTED;
1674 delay = 0;
1675 break;
1676 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301677 msm_chg_enable_secondary_det(motg);
1678 delay = MSM_CHG_SECONDARY_DET_TIME;
1679 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1680 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301681 if (test_bit(ID_A, &motg->inputs)) {
1682 motg->chg_type = USB_ACA_A_CHARGER;
1683 motg->chg_state = USB_CHG_STATE_DETECTED;
1684 delay = 0;
1685 break;
1686 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301687 motg->chg_type = USB_SDP_CHARGER;
1688 motg->chg_state = USB_CHG_STATE_DETECTED;
1689 delay = 0;
1690 }
1691 break;
1692 case USB_CHG_STATE_PRIMARY_DONE:
1693 vout = msm_chg_check_secondary_det(motg);
1694 if (vout)
1695 motg->chg_type = USB_DCP_CHARGER;
1696 else
1697 motg->chg_type = USB_CDP_CHARGER;
1698 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1699 /* fall through */
1700 case USB_CHG_STATE_SECONDARY_DONE:
1701 motg->chg_state = USB_CHG_STATE_DETECTED;
1702 case USB_CHG_STATE_DETECTED:
1703 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001704 msm_chg_enable_aca_det(motg);
1705 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301706 dev_dbg(otg->dev, "chg_type = %s\n",
1707 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301708 schedule_work(&motg->sm_work);
1709 return;
1710 default:
1711 return;
1712 }
1713
1714 schedule_delayed_work(&motg->chg_work, delay);
1715}
1716
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301717/*
1718 * We support OTG, Peripheral only and Host only configurations. In case
1719 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1720 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1721 * enabled when switch is controlled by user and default mode is supplied
1722 * by board file, which can be changed by userspace later.
1723 */
1724static void msm_otg_init_sm(struct msm_otg *motg)
1725{
1726 struct msm_otg_platform_data *pdata = motg->pdata;
1727 u32 otgsc = readl(USB_OTGSC);
1728
1729 switch (pdata->mode) {
1730 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001731 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301732 if (pdata->default_mode == USB_HOST) {
1733 clear_bit(ID, &motg->inputs);
1734 } else if (pdata->default_mode == USB_PERIPHERAL) {
1735 set_bit(ID, &motg->inputs);
1736 set_bit(B_SESS_VLD, &motg->inputs);
1737 } else {
1738 set_bit(ID, &motg->inputs);
1739 clear_bit(B_SESS_VLD, &motg->inputs);
1740 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301741 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1742 if (otgsc & OTGSC_ID)
1743 set_bit(ID, &motg->inputs);
1744 else
1745 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001746 if (otgsc & OTGSC_BSV)
1747 set_bit(B_SESS_VLD, &motg->inputs);
1748 else
1749 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301750 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301751 if (pdata->pmic_id_irq) {
1752 if (irq_read_line(pdata->pmic_id_irq))
1753 set_bit(ID, &motg->inputs);
1754 else
1755 clear_bit(ID, &motg->inputs);
1756 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301757 /*
1758 * VBUS initial state is reported after PMIC
1759 * driver initialization. Wait for it.
1760 */
1761 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301762 }
1763 break;
1764 case USB_HOST:
1765 clear_bit(ID, &motg->inputs);
1766 break;
1767 case USB_PERIPHERAL:
1768 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301769 if (pdata->otg_control == OTG_PHY_CONTROL) {
1770 if (otgsc & OTGSC_BSV)
1771 set_bit(B_SESS_VLD, &motg->inputs);
1772 else
1773 clear_bit(B_SESS_VLD, &motg->inputs);
1774 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1775 /*
1776 * VBUS initial state is reported after PMIC
1777 * driver initialization. Wait for it.
1778 */
1779 wait_for_completion(&pmic_vbus_init);
1780 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301781 break;
1782 default:
1783 break;
1784 }
1785}
1786
1787static void msm_otg_sm_work(struct work_struct *w)
1788{
1789 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1790 struct otg_transceiver *otg = &motg->otg;
1791
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301792 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301793 switch (otg->state) {
1794 case OTG_STATE_UNDEFINED:
1795 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1796 msm_otg_reset(otg);
1797 msm_otg_init_sm(motg);
1798 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301799 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1800 test_bit(ID, &motg->inputs)) {
1801 pm_runtime_put_noidle(otg->dev);
1802 pm_runtime_suspend(otg->dev);
1803 break;
1804 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301805 /* FALL THROUGH */
1806 case OTG_STATE_B_IDLE:
1807 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001808 if ((!test_bit(ID, &motg->inputs) ||
1809 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001810 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1811 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301812 IDEV_ACA_CHG_MAX);
1813 else if (test_bit(ID_A, &motg->inputs))
1814 msm_otg_notify_charger(motg,
1815 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301816 else
1817 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301818 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301819 /*
1820 * Link can not generate PHY_ALT interrupt
1821 * in host mode when no device is attached
1822 * to the port. It is also observed PHY_ALT
1823 * interrupt missing upon Micro-A cable disconnect.
1824 * Hence disable PHY_ALT interrupt and perform
1825 * polling to detect RID change.
1826 */
1827 msm_chg_enable_aca_det(motg);
1828 msm_chg_disable_aca_intr(motg);
1829 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301830 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301831 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1832 switch (motg->chg_state) {
1833 case USB_CHG_STATE_UNDEFINED:
1834 msm_chg_detect_work(&motg->chg_work.work);
1835 break;
1836 case USB_CHG_STATE_DETECTED:
1837 switch (motg->chg_type) {
1838 case USB_DCP_CHARGER:
1839 msm_otg_notify_charger(motg,
1840 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301841 pm_runtime_put_noidle(otg->dev);
1842 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301843 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301844 case USB_ACA_B_CHARGER:
1845 msm_otg_notify_charger(motg,
1846 IDEV_ACA_CHG_MAX);
1847 /*
1848 * (ID_B --> ID_C) PHY_ALT interrupt can
1849 * not be detected in LPM.
1850 */
1851 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301852 case USB_CDP_CHARGER:
1853 msm_otg_notify_charger(motg,
1854 IDEV_CHG_MAX);
1855 msm_otg_start_peripheral(otg, 1);
1856 otg->state = OTG_STATE_B_PERIPHERAL;
1857 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301858 case USB_ACA_C_CHARGER:
1859 msm_otg_notify_charger(motg,
1860 IDEV_ACA_CHG_MAX);
1861 msm_otg_start_peripheral(otg, 1);
1862 otg->state = OTG_STATE_B_PERIPHERAL;
1863 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 case USB_SDP_CHARGER:
1865 msm_otg_notify_charger(motg, IUNIT);
1866 msm_otg_start_peripheral(otg, 1);
1867 otg->state = OTG_STATE_B_PERIPHERAL;
1868 break;
1869 default:
1870 break;
1871 }
1872 break;
1873 default:
1874 break;
1875 }
1876 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301877 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301878 msm_otg_notify_charger(motg, 0);
1879 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1880 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301881 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301882 pm_runtime_put_noidle(otg->dev);
1883 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301884 }
1885 break;
1886 case OTG_STATE_B_PERIPHERAL:
1887 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1888 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001889 !test_bit(ID, &motg->inputs) ||
1890 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301891 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001892 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001893 schedule_work(w);
1894 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301895 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001896 }
1897 break;
1898 case OTG_STATE_A_HOST:
1899 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1900 if (test_bit(ID, &motg->inputs) &&
1901 !test_bit(ID_A, &motg->inputs)) {
1902 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301903 msm_hsusb_vbus_power(motg, 0);
1904 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301905 /*
1906 * Exit point of host mode.
1907 *
1908 * 1. Micro-A cable disconnect: Just schedule
1909 * the work. PHY is reset in B_IDLE and LPM
1910 * is allowed.
1911 * 2. ID_GND --> ID_B: No need to reset the PHY.
1912 * HCD core clears all PORTSC bits and initializes
1913 * the controller to host mode in remove_hcd.
1914 * Restore PORTSC transceiver select bits (ULPI)
1915 * and reset the controller to change MODE bits.
1916 * PHY_ALT interrupt can not occur in host mode.
1917 */
1918 del_timer_sync(&motg->id_timer);
1919 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1920 msm_otg_link_reset(motg);
1921 msm_chg_enable_aca_intr(motg);
1922 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301923 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301924 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301926 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001927 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301928 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001930 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301931 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301932 }
1933 break;
1934 default:
1935 break;
1936 }
1937}
1938
1939static irqreturn_t msm_otg_irq(int irq, void *data)
1940{
1941 struct msm_otg *motg = data;
1942 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301944
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301945 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301946 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301947 disable_irq_nosync(irq);
1948 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301949 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301950 return IRQ_HANDLED;
1951 }
1952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 usbsts = readl(USB_USBSTS);
1954 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301955 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001956 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301957 if (msm_chg_check_aca_intr(motg)) {
1958 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001959 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301960 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001961 return IRQ_HANDLED;
1962 }
1963
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301964 otgsc = readl(USB_OTGSC);
1965 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1966 return IRQ_NONE;
1967
1968 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301969 if (otgsc & OTGSC_ID) {
1970 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301971 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301972 } else {
1973 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301974 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301975 msm_chg_enable_aca_det(motg);
1976 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001977 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301978 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301979 if (otgsc & OTGSC_BSV) {
1980 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301981 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301982 } else {
1983 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301984 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301985 msm_chg_check_aca_intr(motg);
1986 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001987 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301988 }
1989
1990 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991 return IRQ_HANDLED;
1992}
1993
1994static void msm_otg_set_vbus_state(int online)
1995{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301996 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001997 struct msm_otg *motg = the_msm_otg;
1998
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301999 if (online) {
2000 pr_debug("PMIC: BSV set\n");
2001 set_bit(B_SESS_VLD, &motg->inputs);
2002 } else {
2003 pr_debug("PMIC: BSV clear\n");
2004 clear_bit(B_SESS_VLD, &motg->inputs);
2005 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002006
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302007 if (!init) {
2008 init = true;
2009 complete(&pmic_vbus_init);
2010 pr_debug("PMIC: BSV init complete\n");
2011 return;
2012 }
2013
2014 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015}
2016
2017static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2018{
2019 struct msm_otg *motg = data;
2020
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302021 if (aca_id_turned_on)
2022 return IRQ_HANDLED;
2023
2024 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2025 pr_debug("PMIC: ID set\n");
2026 set_bit(ID, &motg->inputs);
2027 } else {
2028 pr_debug("PMIC: ID clear\n");
2029 clear_bit(ID, &motg->inputs);
2030 }
2031
2032 if (motg->otg.state != OTG_STATE_UNDEFINED)
2033 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002034
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302035 return IRQ_HANDLED;
2036}
2037
2038static int msm_otg_mode_show(struct seq_file *s, void *unused)
2039{
2040 struct msm_otg *motg = s->private;
2041 struct otg_transceiver *otg = &motg->otg;
2042
2043 switch (otg->state) {
2044 case OTG_STATE_A_HOST:
2045 seq_printf(s, "host\n");
2046 break;
2047 case OTG_STATE_B_PERIPHERAL:
2048 seq_printf(s, "peripheral\n");
2049 break;
2050 default:
2051 seq_printf(s, "none\n");
2052 break;
2053 }
2054
2055 return 0;
2056}
2057
2058static int msm_otg_mode_open(struct inode *inode, struct file *file)
2059{
2060 return single_open(file, msm_otg_mode_show, inode->i_private);
2061}
2062
2063static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2064 size_t count, loff_t *ppos)
2065{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302066 struct seq_file *s = file->private_data;
2067 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302068 char buf[16];
2069 struct otg_transceiver *otg = &motg->otg;
2070 int status = count;
2071 enum usb_mode_type req_mode;
2072
2073 memset(buf, 0x00, sizeof(buf));
2074
2075 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2076 status = -EFAULT;
2077 goto out;
2078 }
2079
2080 if (!strncmp(buf, "host", 4)) {
2081 req_mode = USB_HOST;
2082 } else if (!strncmp(buf, "peripheral", 10)) {
2083 req_mode = USB_PERIPHERAL;
2084 } else if (!strncmp(buf, "none", 4)) {
2085 req_mode = USB_NONE;
2086 } else {
2087 status = -EINVAL;
2088 goto out;
2089 }
2090
2091 switch (req_mode) {
2092 case USB_NONE:
2093 switch (otg->state) {
2094 case OTG_STATE_A_HOST:
2095 case OTG_STATE_B_PERIPHERAL:
2096 set_bit(ID, &motg->inputs);
2097 clear_bit(B_SESS_VLD, &motg->inputs);
2098 break;
2099 default:
2100 goto out;
2101 }
2102 break;
2103 case USB_PERIPHERAL:
2104 switch (otg->state) {
2105 case OTG_STATE_B_IDLE:
2106 case OTG_STATE_A_HOST:
2107 set_bit(ID, &motg->inputs);
2108 set_bit(B_SESS_VLD, &motg->inputs);
2109 break;
2110 default:
2111 goto out;
2112 }
2113 break;
2114 case USB_HOST:
2115 switch (otg->state) {
2116 case OTG_STATE_B_IDLE:
2117 case OTG_STATE_B_PERIPHERAL:
2118 clear_bit(ID, &motg->inputs);
2119 break;
2120 default:
2121 goto out;
2122 }
2123 break;
2124 default:
2125 goto out;
2126 }
2127
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302128 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302129 schedule_work(&motg->sm_work);
2130out:
2131 return status;
2132}
2133
2134const struct file_operations msm_otg_mode_fops = {
2135 .open = msm_otg_mode_open,
2136 .read = seq_read,
2137 .write = msm_otg_mode_write,
2138 .llseek = seq_lseek,
2139 .release = single_release,
2140};
2141
Anji jonnalad270e2d2011-08-09 11:28:32 +05302142static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2143{
2144 struct msm_otg *motg = s->private;
2145
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302146 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302147 return 0;
2148}
2149
2150static int msm_otg_chg_open(struct inode *inode, struct file *file)
2151{
2152 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2153}
2154
2155const struct file_operations msm_otg_chg_fops = {
2156 .open = msm_otg_chg_open,
2157 .read = seq_read,
2158 .llseek = seq_lseek,
2159 .release = single_release,
2160};
2161
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302162static int msm_otg_aca_show(struct seq_file *s, void *unused)
2163{
2164 if (debug_aca_enabled)
2165 seq_printf(s, "enabled\n");
2166 else
2167 seq_printf(s, "disabled\n");
2168
2169 return 0;
2170}
2171
2172static int msm_otg_aca_open(struct inode *inode, struct file *file)
2173{
2174 return single_open(file, msm_otg_aca_show, inode->i_private);
2175}
2176
2177static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2178 size_t count, loff_t *ppos)
2179{
2180 char buf[8];
2181
2182 memset(buf, 0x00, sizeof(buf));
2183
2184 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2185 return -EFAULT;
2186
2187 if (!strncmp(buf, "enable", 6))
2188 debug_aca_enabled = true;
2189 else
2190 debug_aca_enabled = false;
2191
2192 return count;
2193}
2194
2195const struct file_operations msm_otg_aca_fops = {
2196 .open = msm_otg_aca_open,
2197 .read = seq_read,
2198 .write = msm_otg_aca_write,
2199 .llseek = seq_lseek,
2200 .release = single_release,
2201};
2202
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302203static struct dentry *msm_otg_dbg_root;
2204static struct dentry *msm_otg_dbg_mode;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302205static struct dentry *msm_otg_chg_type;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302206static struct dentry *msm_otg_dbg_aca;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302207
2208static int msm_otg_debugfs_init(struct msm_otg *motg)
2209{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302210
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302211 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2212
2213 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2214 return -ENODEV;
2215
Anji jonnalad270e2d2011-08-09 11:28:32 +05302216 if (motg->pdata->mode == USB_OTG &&
2217 motg->pdata->otg_control == OTG_USER_CONTROL) {
2218
2219 msm_otg_dbg_mode = debugfs_create_file("mode", S_IRUGO |
2220 S_IWUSR, msm_otg_dbg_root, motg,
2221 &msm_otg_mode_fops);
2222
2223 if (!msm_otg_dbg_mode) {
2224 debugfs_remove(msm_otg_dbg_root);
2225 msm_otg_dbg_root = NULL;
2226 return -ENODEV;
2227 }
2228 }
2229
2230 msm_otg_chg_type = debugfs_create_file("chg_type", S_IRUGO,
2231 msm_otg_dbg_root, motg,
2232 &msm_otg_chg_fops);
2233
2234 if (!msm_otg_chg_type) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302235 debugfs_remove_recursive(msm_otg_dbg_root);
2236 return -ENODEV;
2237 }
2238
2239 msm_otg_dbg_aca = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
2240 msm_otg_dbg_root, motg,
2241 &msm_otg_aca_fops);
2242
2243 if (!msm_otg_dbg_aca) {
2244 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302245 return -ENODEV;
2246 }
2247
2248 return 0;
2249}
2250
2251static void msm_otg_debugfs_cleanup(void)
2252{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302253 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302254}
2255
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302256static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2257static struct platform_device *msm_otg_add_pdev(
2258 struct platform_device *ofdev, const char *name)
2259{
2260 struct platform_device *pdev;
2261 const struct resource *res = ofdev->resource;
2262 unsigned int num = ofdev->num_resources;
2263 int retval;
2264
2265 pdev = platform_device_alloc(name, -1);
2266 if (!pdev) {
2267 retval = -ENOMEM;
2268 goto error;
2269 }
2270
2271 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2272 pdev->dev.dma_mask = &msm_otg_dma_mask;
2273
2274 if (num) {
2275 retval = platform_device_add_resources(pdev, res, num);
2276 if (retval)
2277 goto error;
2278 }
2279
2280 retval = platform_device_add(pdev);
2281 if (retval)
2282 goto error;
2283
2284 return pdev;
2285
2286error:
2287 platform_device_put(pdev);
2288 return ERR_PTR(retval);
2289}
2290
2291static int msm_otg_setup_devices(struct platform_device *ofdev,
2292 enum usb_mode_type mode, bool init)
2293{
2294 const char *gadget_name = "msm_hsusb";
2295 const char *host_name = "msm_hsusb_host";
2296 static struct platform_device *gadget_pdev;
2297 static struct platform_device *host_pdev;
2298 int retval = 0;
2299
2300 if (!init) {
2301 if (gadget_pdev)
2302 platform_device_unregister(gadget_pdev);
2303 if (host_pdev)
2304 platform_device_unregister(host_pdev);
2305 return 0;
2306 }
2307
2308 switch (mode) {
2309 case USB_OTG:
2310 /* fall through */
2311 case USB_PERIPHERAL:
2312 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2313 if (IS_ERR(gadget_pdev)) {
2314 retval = PTR_ERR(gadget_pdev);
2315 break;
2316 }
2317 if (mode == USB_PERIPHERAL)
2318 break;
2319 /* fall through */
2320 case USB_HOST:
2321 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2322 if (IS_ERR(host_pdev)) {
2323 retval = PTR_ERR(host_pdev);
2324 if (mode == USB_OTG)
2325 platform_device_unregister(gadget_pdev);
2326 }
2327 break;
2328 default:
2329 break;
2330 }
2331
2332 return retval;
2333}
2334
2335struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2336{
2337 struct device_node *node = pdev->dev.of_node;
2338 struct msm_otg_platform_data *pdata;
2339 int len = 0;
2340
2341 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2342 if (!pdata) {
2343 pr_err("unable to allocate platform data\n");
2344 return NULL;
2345 }
2346 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2347 if (len) {
2348 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2349 if (!pdata->phy_init_seq)
2350 return NULL;
2351 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2352 pdata->phy_init_seq,
2353 len/sizeof(*pdata->phy_init_seq));
2354 }
2355 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2356 &pdata->power_budget);
2357 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2358 &pdata->mode);
2359 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2360 &pdata->otg_control);
2361 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2362 &pdata->default_mode);
2363 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2364 &pdata->phy_type);
2365 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2366 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302367 return pdata;
2368}
2369
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302370static int __init msm_otg_probe(struct platform_device *pdev)
2371{
2372 int ret = 0;
2373 struct resource *res;
2374 struct msm_otg *motg;
2375 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302376 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302377
2378 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302379
2380 if (pdev->dev.of_node) {
2381 dev_dbg(&pdev->dev, "device tree enabled\n");
2382 pdata = msm_otg_dt_to_pdata(pdev);
2383 if (!pdata)
2384 return -ENOMEM;
2385 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2386 if (ret) {
2387 dev_err(&pdev->dev, "devices setup failed\n");
2388 return ret;
2389 }
2390 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302391 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2392 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302393 } else {
2394 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302395 }
2396
2397 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2398 if (!motg) {
2399 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2400 return -ENOMEM;
2401 }
2402
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002403 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302404 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302405 otg = &motg->otg;
2406 otg->dev = &pdev->dev;
2407
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302408 /*
2409 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2410 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2411 * PMIC can detect ACA ID_GND and generate an interrupt.
2412 */
2413 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2414 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2415 ret = -EINVAL;
2416 goto free_motg;
2417 }
2418
Ofir Cohen4da266f2012-01-03 10:19:29 +02002419 /* initialize reset counter */
2420 motg->reset_counter = 0;
2421
Amit Blay02eff132011-09-21 16:46:24 +03002422 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002423 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002424 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002425 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302426
Manu Gautam5143b252012-01-05 19:25:23 -08002427 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302428 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002429 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302430 ret = PTR_ERR(motg->clk);
2431 goto put_phy_reset_clk;
2432 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302433 clk_set_rate(motg->clk, 60000000);
2434
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302435 /* pm qos request to prevent apps idle power collapse */
2436 if (motg->pdata->swfi_latency)
2437 pm_qos_add_request(&motg->pm_qos_req_dma,
2438 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002439
Anji jonnala0f73cac2011-05-04 10:19:46 +05302440 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002441 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302442 * CORE CLK must be running at >55Mhz for correct HSUSB
2443 * operation and USB core cannot tolerate frequency changes on
2444 * CORE CLK. For such USB cores, vote for maximum clk frequency
2445 * on pclk source
2446 */
Manu Gautam5143b252012-01-05 19:25:23 -08002447 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2448 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302449 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002450 dev_err(&pdev->dev, "failed to get core_clk\n");
2451 ret = PTR_ERR(motg->clk);
2452 goto put_clk;
2453 }
2454 clk_set_rate(motg->core_clk, INT_MAX);
2455
2456 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2457 if (IS_ERR(motg->pclk)) {
2458 dev_err(&pdev->dev, "failed to get iface_clk\n");
2459 ret = PTR_ERR(motg->pclk);
2460 goto put_core_clk;
2461 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302462
2463 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2464 if (!res) {
2465 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2466 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002467 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302468 }
2469
2470 motg->regs = ioremap(res->start, resource_size(res));
2471 if (!motg->regs) {
2472 dev_err(&pdev->dev, "ioremap failed\n");
2473 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002474 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302475 }
2476 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2477
2478 motg->irq = platform_get_irq(pdev, 0);
2479 if (!motg->irq) {
2480 dev_err(&pdev->dev, "platform_get_irq failed\n");
2481 ret = -ENODEV;
2482 goto free_regs;
2483 }
2484
Stephen Boyd48a32912012-01-09 19:47:17 -08002485 motg->xo_handle = msm_xo_get(MSM_XO_CXO, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08002486 if (IS_ERR(motg->xo_handle)) {
2487 dev_err(&pdev->dev, "%s not able to get the handle "
2488 "to vote for TCXO D0 buffer\n", __func__);
2489 ret = PTR_ERR(motg->xo_handle);
2490 goto free_regs;
2491 }
2492
2493 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
2494 if (ret) {
2495 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2496 "D0 buffer%d\n", __func__, ret);
2497 goto free_xo_handle;
2498 }
2499
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302500 clk_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302501
2502 ret = msm_hsusb_init_vddcx(motg, 1);
2503 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002504 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002505 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302506 }
2507
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002508 ret = msm_hsusb_config_vddcx(1);
2509 if (ret) {
2510 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2511 goto free_init_vddcx;
2512 }
2513
Anji jonnala11aa5c42011-05-04 10:19:48 +05302514 ret = msm_hsusb_ldo_init(motg, 1);
2515 if (ret) {
2516 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002517 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302518 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002519
2520 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302521 if (ret) {
2522 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002523 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302524 }
Manu Gautam5143b252012-01-05 19:25:23 -08002525 clk_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302526
2527 writel(0, USB_USBINTR);
2528 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002529 /* Ensure that above STOREs are completed before enabling interrupts */
2530 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302533 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302534 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302535 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2536 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302537 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2538 "msm_otg", motg);
2539 if (ret) {
2540 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002541 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302542 }
2543
2544 otg->init = msm_otg_reset;
2545 otg->set_host = msm_otg_set_host;
2546 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302547 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302548 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302549
2550 otg->io_ops = &msm_otg_io_ops;
2551
2552 ret = otg_set_transceiver(&motg->otg);
2553 if (ret) {
2554 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2555 goto free_irq;
2556 }
2557
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302558 if (motg->pdata->mode == USB_OTG &&
2559 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002560 if (motg->pdata->pmic_id_irq) {
2561 ret = request_irq(motg->pdata->pmic_id_irq,
2562 msm_pmic_id_irq,
2563 IRQF_TRIGGER_RISING |
2564 IRQF_TRIGGER_FALLING,
2565 "msm_otg", motg);
2566 if (ret) {
2567 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2568 goto remove_otg;
2569 }
2570 } else {
2571 ret = -ENODEV;
2572 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2573 goto remove_otg;
2574 }
2575 }
2576
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302577 msm_hsusb_mhl_switch_enable(motg, 1);
2578
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302579 platform_set_drvdata(pdev, motg);
2580 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302582
Anji jonnalad270e2d2011-08-09 11:28:32 +05302583 ret = msm_otg_debugfs_init(motg);
2584 if (ret)
2585 dev_dbg(&pdev->dev, "mode debugfs file is"
2586 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302587
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002588 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2589 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2590
Amit Blay58b31472011-11-18 09:39:39 +02002591 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2592 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302593 (!(motg->pdata->mode == USB_OTG) ||
2594 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002595 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002596 ALLOW_PHY_RETENTION |
2597 ALLOW_PHY_COMP_DISABLE;
2598
Amit Blay58b31472011-11-18 09:39:39 +02002599 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2600 motg->caps = ALLOW_PHY_RETENTION;
2601 }
2602
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002603 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302604 pm_runtime_set_active(&pdev->dev);
2605 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302606
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302607 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002608
2609remove_otg:
2610 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302611free_irq:
2612 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002613destroy_wlock:
2614 wake_lock_destroy(&motg->wlock);
Manu Gautam5143b252012-01-05 19:25:23 -08002615 clk_disable(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002616 msm_hsusb_ldo_enable(motg, 0);
2617free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302618 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002619free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302620 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002621devote_xo_handle:
Manu Gautam5143b252012-01-05 19:25:23 -08002622 clk_disable(motg->pclk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002623 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
2624free_xo_handle:
2625 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302626free_regs:
2627 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002628put_pclk:
2629 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302630put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002631 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302632put_clk:
2633 clk_put(motg->clk);
2634put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002635 if (!IS_ERR(motg->phy_reset_clk))
2636 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302637free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302638 if (motg->pdata->swfi_latency)
2639 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302640 kfree(motg);
2641 return ret;
2642}
2643
2644static int __devexit msm_otg_remove(struct platform_device *pdev)
2645{
2646 struct msm_otg *motg = platform_get_drvdata(pdev);
2647 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302648 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302649
2650 if (otg->host || otg->gadget)
2651 return -EBUSY;
2652
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302653 if (pdev->dev.of_node)
2654 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002655 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2656 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302657 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302658 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302659 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302660
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302661 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302662
2663 device_init_wakeup(&pdev->dev, 0);
2664 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302666
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302667 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668 if (motg->pdata->pmic_id_irq)
2669 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302670 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302671 free_irq(motg->irq, motg);
2672
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302673 /*
2674 * Put PHY in low power mode.
2675 */
2676 ulpi_read(otg, 0x14);
2677 ulpi_write(otg, 0x08, 0x09);
2678
2679 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2680 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2681 if (readl(USB_PORTSC) & PORTSC_PHCD)
2682 break;
2683 udelay(1);
2684 cnt++;
2685 }
2686 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2687 dev_err(otg->dev, "Unable to suspend PHY\n");
2688
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302689 clk_disable(motg->pclk);
Manu Gautam5143b252012-01-05 19:25:23 -08002690 clk_disable(motg->core_clk);
Anji jonnala7da3f262011-12-02 17:22:14 -08002691 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002692 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302693 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002694 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302695
2696 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302697 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302698
Amit Blay02eff132011-09-21 16:46:24 +03002699 if (!IS_ERR(motg->phy_reset_clk))
2700 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302701 clk_put(motg->pclk);
2702 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002703 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302704
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302705 if (motg->pdata->swfi_latency)
2706 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302707
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302708 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302709 return 0;
2710}
2711
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302712#ifdef CONFIG_PM_RUNTIME
2713static int msm_otg_runtime_idle(struct device *dev)
2714{
2715 struct msm_otg *motg = dev_get_drvdata(dev);
2716 struct otg_transceiver *otg = &motg->otg;
2717
2718 dev_dbg(dev, "OTG runtime idle\n");
2719
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302720 if (otg->state == OTG_STATE_UNDEFINED)
2721 return -EAGAIN;
2722 else
2723 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302724}
2725
2726static int msm_otg_runtime_suspend(struct device *dev)
2727{
2728 struct msm_otg *motg = dev_get_drvdata(dev);
2729
2730 dev_dbg(dev, "OTG runtime suspend\n");
2731 return msm_otg_suspend(motg);
2732}
2733
2734static int msm_otg_runtime_resume(struct device *dev)
2735{
2736 struct msm_otg *motg = dev_get_drvdata(dev);
2737
2738 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302739 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302740 return msm_otg_resume(motg);
2741}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742#endif
2743
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302744#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302745static int msm_otg_pm_suspend(struct device *dev)
2746{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302747 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302748
2749 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302750
2751#ifdef CONFIG_PM_RUNTIME
2752 ret = pm_runtime_suspend(dev);
2753 if (ret > 0)
2754 ret = 0;
2755#else
2756 ret = msm_otg_suspend(dev_get_drvdata(dev));
2757#endif
2758 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302759}
2760
2761static int msm_otg_pm_resume(struct device *dev)
2762{
2763 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302764
2765 dev_dbg(dev, "OTG PM resume\n");
2766
Manu Gautamf284c052011-09-08 16:52:48 +05302767#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302768 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302769 * Do not resume hardware as part of system resume,
2770 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302771 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002772 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302773#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302774
Manu Gautamf284c052011-09-08 16:52:48 +05302775 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302776}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302777#endif
2778
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302779#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302780static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302781 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2782 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2783 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302784};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302785#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302786
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302787static struct of_device_id msm_otg_dt_match[] = {
2788 { .compatible = "qcom,hsusb-otg",
2789 },
2790 {}
2791};
2792
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302793static struct platform_driver msm_otg_driver = {
2794 .remove = __devexit_p(msm_otg_remove),
2795 .driver = {
2796 .name = DRIVER_NAME,
2797 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302798#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302799 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302800#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302801 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302802 },
2803};
2804
2805static int __init msm_otg_init(void)
2806{
2807 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2808}
2809
2810static void __exit msm_otg_exit(void)
2811{
2812 platform_driver_unregister(&msm_otg_driver);
2813}
2814
2815module_init(msm_otg_init);
2816module_exit(msm_otg_exit);
2817
2818MODULE_LICENSE("GPL v2");
2819MODULE_DESCRIPTION("MSM USB transceiver driver");