blob: dd07d4ec10c4a5a0ff1a43ba1e95aa677296497e [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>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053040#include <linux/mfd/pm8xxx/misc.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053041#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053043
44#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080045#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053046#include <mach/msm_bus.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053047
48#define MSM_USB_BASE (motg->regs)
49#define DRIVER_NAME "msm_otg"
50
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053051#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(2000))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053052#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053053
54#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
55#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
56#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
57#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
58
59#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
60#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
61#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
62#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
63
Vamsi Krishna132b2762011-11-11 16:09:20 -080064#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053065#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
66
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053067static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053069static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053070static bool debug_bus_voting_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053072/* Prevent idle power collapse(pc) while operating in peripheral mode */
73static void otg_pm_qos_update_latency(struct msm_otg *dev, int vote)
74{
75 struct msm_otg_platform_data *pdata = dev->pdata;
76 u32 swfi_latency = 0;
77
78 if (!pdata || !pdata->swfi_latency)
79 return;
80
81 swfi_latency = pdata->swfi_latency + 1;
82
83 if (vote)
84 pm_qos_update_request(&dev->pm_qos_req_dma,
85 swfi_latency);
86 else
87 pm_qos_update_request(&dev->pm_qos_req_dma,
88 PM_QOS_DEFAULT_VALUE);
89}
90
Anji jonnala11aa5c42011-05-04 10:19:48 +053091static struct regulator *hsusb_3p3;
92static struct regulator *hsusb_1p8;
93static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053094static struct regulator *vbus_otg;
Anji jonnala11aa5c42011-05-04 10:19:48 +053095
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053096static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053097static inline bool aca_enabled(void)
98{
99#ifdef CONFIG_USB_MSM_ACA
100 return true;
101#else
102 return debug_aca_enabled;
103#endif
104}
105
Anji jonnala11aa5c42011-05-04 10:19:48 +0530106static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
107{
108 int ret = 0;
109
110 if (init) {
111 hsusb_vddcx = regulator_get(motg->otg.dev, "HSUSB_VDDCX");
112 if (IS_ERR(hsusb_vddcx)) {
113 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
114 return PTR_ERR(hsusb_vddcx);
115 }
116
117 ret = regulator_set_voltage(hsusb_vddcx,
118 USB_PHY_VDD_DIG_VOL_MIN,
119 USB_PHY_VDD_DIG_VOL_MAX);
120 if (ret) {
121 dev_err(motg->otg.dev, "unable to set the voltage "
122 "for hsusb vddcx\n");
123 regulator_put(hsusb_vddcx);
124 return ret;
125 }
126
127 ret = regulator_enable(hsusb_vddcx);
128 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700129 regulator_set_voltage(hsusb_vddcx, 0,
130 USB_PHY_VDD_DIG_VOL_MIN);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530131 regulator_put(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700132 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
133 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530134 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135
Anji jonnala11aa5c42011-05-04 10:19:48 +0530136 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700137
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530140 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141 return ret;
142 }
143
144 ret = regulator_set_voltage(hsusb_vddcx, 0,
145 USB_PHY_VDD_DIG_VOL_MIN);
146 if (ret) {
147 dev_err(motg->otg.dev, "unable to set the voltage"
148 "for hsusb vddcx\n");
149 return ret;
150 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530151
152 regulator_put(hsusb_vddcx);
153 }
154
155 return ret;
156}
157
158static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
159{
160 int rc = 0;
161
162 if (init) {
163 hsusb_3p3 = regulator_get(motg->otg.dev, "HSUSB_3p3");
164 if (IS_ERR(hsusb_3p3)) {
165 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
166 return PTR_ERR(hsusb_3p3);
167 }
168
169 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
170 USB_PHY_3P3_VOL_MAX);
171 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172 dev_err(motg->otg.dev, "unable to set voltage level for"
173 "hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530174 goto put_3p3;
175 }
176 hsusb_1p8 = regulator_get(motg->otg.dev, "HSUSB_1p8");
177 if (IS_ERR(hsusb_1p8)) {
178 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
179 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530181 }
182 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
183 USB_PHY_1P8_VOL_MAX);
184 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700185 dev_err(motg->otg.dev, "unable to set voltage level for"
186 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530187 goto put_1p8;
188 }
189
190 return 0;
191 }
192
Anji jonnala11aa5c42011-05-04 10:19:48 +0530193put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530195 regulator_put(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196put_3p3_lpm:
197 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530198put_3p3:
199 regulator_put(hsusb_3p3);
200 return rc;
201}
202
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530203#ifdef CONFIG_PM_SLEEP
204#define USB_PHY_SUSP_DIG_VOL 500000
205static int msm_hsusb_config_vddcx(int high)
206{
207 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
208 int min_vol;
209 int ret;
210
211 if (high)
212 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
213 else
214 min_vol = USB_PHY_SUSP_DIG_VOL;
215
216 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
217 if (ret) {
218 pr_err("%s: unable to set the voltage for regulator "
219 "HSUSB_VDDCX\n", __func__);
220 return ret;
221 }
222
223 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
224
225 return ret;
226}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700227#else
228static int msm_hsusb_config_vddcx(int high)
229{
230 return 0;
231}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530232#endif
233
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700234static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530235{
236 int ret = 0;
237
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530238 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530239 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
240 return -ENODEV;
241 }
242
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530243 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530244 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
245 return -ENODEV;
246 }
247
248 if (on) {
249 ret = regulator_set_optimum_mode(hsusb_1p8,
250 USB_PHY_1P8_HPM_LOAD);
251 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530253 "HSUSB_1p8\n", __func__);
254 return ret;
255 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256
257 ret = regulator_enable(hsusb_1p8);
258 if (ret) {
259 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
260 __func__);
261 regulator_set_optimum_mode(hsusb_1p8, 0);
262 return ret;
263 }
264
Anji jonnala11aa5c42011-05-04 10:19:48 +0530265 ret = regulator_set_optimum_mode(hsusb_3p3,
266 USB_PHY_3P3_HPM_LOAD);
267 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530269 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 regulator_set_optimum_mode(hsusb_1p8, 0);
271 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530272 return ret;
273 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 ret = regulator_enable(hsusb_3p3);
276 if (ret) {
277 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
278 __func__);
279 regulator_set_optimum_mode(hsusb_3p3, 0);
280 regulator_set_optimum_mode(hsusb_1p8, 0);
281 regulator_disable(hsusb_1p8);
282 return ret;
283 }
284
Anji jonnala11aa5c42011-05-04 10:19:48 +0530285 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 ret = regulator_disable(hsusb_1p8);
287 if (ret) {
288 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
289 __func__);
290 return ret;
291 }
292
293 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530294 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530296 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297
298 ret = regulator_disable(hsusb_3p3);
299 if (ret) {
300 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
301 __func__);
302 return ret;
303 }
304 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530305 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700306 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530307 "HSUSB_3p3\n", __func__);
308 }
309
310 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
311 return ret < 0 ? ret : 0;
312}
313
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530314static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
315{
316 static struct regulator *mhl_analog_switch;
317 struct msm_otg_platform_data *pdata = motg->pdata;
318
319 if (!pdata->mhl_enable)
320 return;
321
322 if (on) {
323 mhl_analog_switch = regulator_get(motg->otg.dev,
324 "mhl_ext_3p3v");
325 if (IS_ERR(mhl_analog_switch)) {
326 pr_err("Unable to get mhl_analog_switch\n");
327 return;
328 }
329
330 if (regulator_enable(mhl_analog_switch)) {
331 pr_err("unable to enable mhl_analog_switch\n");
332 goto put_analog_switch;
333 }
334 return;
335 }
336
337 regulator_disable(mhl_analog_switch);
338put_analog_switch:
339 regulator_put(mhl_analog_switch);
340}
341
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530342static int ulpi_read(struct otg_transceiver *otg, u32 reg)
343{
344 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
345 int cnt = 0;
346
347 /* initiate read operation */
348 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
349 USB_ULPI_VIEWPORT);
350
351 /* wait for completion */
352 while (cnt < ULPI_IO_TIMEOUT_USEC) {
353 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
354 break;
355 udelay(1);
356 cnt++;
357 }
358
359 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
360 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
361 readl(USB_ULPI_VIEWPORT));
362 return -ETIMEDOUT;
363 }
364 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
365}
366
367static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
368{
369 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
370 int cnt = 0;
371
372 /* initiate write operation */
373 writel(ULPI_RUN | ULPI_WRITE |
374 ULPI_ADDR(reg) | ULPI_DATA(val),
375 USB_ULPI_VIEWPORT);
376
377 /* wait for completion */
378 while (cnt < ULPI_IO_TIMEOUT_USEC) {
379 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
380 break;
381 udelay(1);
382 cnt++;
383 }
384
385 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
386 dev_err(otg->dev, "ulpi_write: timeout\n");
387 return -ETIMEDOUT;
388 }
389 return 0;
390}
391
392static struct otg_io_access_ops msm_otg_io_ops = {
393 .read = ulpi_read,
394 .write = ulpi_write,
395};
396
397static void ulpi_init(struct msm_otg *motg)
398{
399 struct msm_otg_platform_data *pdata = motg->pdata;
400 int *seq = pdata->phy_init_seq;
401
402 if (!seq)
403 return;
404
405 while (seq[0] >= 0) {
406 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
407 seq[0], seq[1]);
408 ulpi_write(&motg->otg, seq[0], seq[1]);
409 seq += 2;
410 }
411}
412
413static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
414{
415 int ret;
416
417 if (assert) {
418 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
419 if (ret)
420 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
421 } else {
422 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
423 if (ret)
424 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
425 }
426 return ret;
427}
428
429static int msm_otg_phy_clk_reset(struct msm_otg *motg)
430{
431 int ret;
432
Amit Blay02eff132011-09-21 16:46:24 +0300433 if (IS_ERR(motg->phy_reset_clk))
434 return 0;
435
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530436 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
437 if (ret) {
438 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
439 return ret;
440 }
441 usleep_range(10000, 12000);
442 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
443 if (ret)
444 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
445 return ret;
446}
447
448static int msm_otg_phy_reset(struct msm_otg *motg)
449{
450 u32 val;
451 int ret;
452 int retries;
453
454 ret = msm_otg_link_clk_reset(motg, 1);
455 if (ret)
456 return ret;
457 ret = msm_otg_phy_clk_reset(motg);
458 if (ret)
459 return ret;
460 ret = msm_otg_link_clk_reset(motg, 0);
461 if (ret)
462 return ret;
463
464 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
465 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
466
467 for (retries = 3; retries > 0; retries--) {
468 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
469 ULPI_CLR(ULPI_FUNC_CTRL));
470 if (!ret)
471 break;
472 ret = msm_otg_phy_clk_reset(motg);
473 if (ret)
474 return ret;
475 }
476 if (!retries)
477 return -ETIMEDOUT;
478
479 /* This reset calibrates the phy, if the above write succeeded */
480 ret = msm_otg_phy_clk_reset(motg);
481 if (ret)
482 return ret;
483
484 for (retries = 3; retries > 0; retries--) {
485 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
486 if (ret != -ETIMEDOUT)
487 break;
488 ret = msm_otg_phy_clk_reset(motg);
489 if (ret)
490 return ret;
491 }
492 if (!retries)
493 return -ETIMEDOUT;
494
495 dev_info(motg->otg.dev, "phy_reset: success\n");
496 return 0;
497}
498
499#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530500static int msm_otg_link_reset(struct msm_otg *motg)
501{
502 int cnt = 0;
503
504 writel_relaxed(USBCMD_RESET, USB_USBCMD);
505 while (cnt < LINK_RESET_TIMEOUT_USEC) {
506 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
507 break;
508 udelay(1);
509 cnt++;
510 }
511 if (cnt >= LINK_RESET_TIMEOUT_USEC)
512 return -ETIMEDOUT;
513
514 /* select ULPI phy */
515 writel_relaxed(0x80000000, USB_PORTSC);
516 writel_relaxed(0x0, USB_AHBBURST);
517 writel_relaxed(0x00, USB_AHBMODE);
518
519 return 0;
520}
521
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530522static int msm_otg_reset(struct otg_transceiver *otg)
523{
524 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
525 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530526 int ret;
527 u32 val = 0;
528 u32 ulpi_val = 0;
529
Ofir Cohen4da266f2012-01-03 10:19:29 +0200530 /*
531 * USB PHY and Link reset also reset the USB BAM.
532 * Thus perform reset operation only once to avoid
533 * USB BAM reset on other cases e.g. USB cable disconnections.
534 */
535 if (pdata->disable_reset_on_disconnect) {
536 if (motg->reset_counter)
537 return 0;
538 else
539 motg->reset_counter++;
540 }
541
Manu Gautam28b1bac2012-01-30 16:43:06 +0530542 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530543 ret = msm_otg_phy_reset(motg);
544 if (ret) {
545 dev_err(otg->dev, "phy_reset failed\n");
546 return ret;
547 }
548
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530549 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530550 ret = msm_otg_link_reset(motg);
551 if (ret) {
552 dev_err(otg->dev, "link reset failed\n");
553 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530554 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530555 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530556
557 ulpi_init(motg);
558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700559 /* Ensure that RESET operation is completed before turning off clock */
560 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530561
Manu Gautam28b1bac2012-01-30 16:43:06 +0530562 clk_disable_unprepare(motg->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700563
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530564 if (pdata->otg_control == OTG_PHY_CONTROL) {
565 val = readl_relaxed(USB_OTGSC);
566 if (pdata->mode == USB_OTG) {
567 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
568 val |= OTGSC_IDIE | OTGSC_BSVIE;
569 } else if (pdata->mode == USB_PERIPHERAL) {
570 ulpi_val = ULPI_INT_SESS_VALID;
571 val |= OTGSC_BSVIE;
572 }
573 writel_relaxed(val, USB_OTGSC);
574 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
575 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530576 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
577 /* Enable PMIC pull-up */
578 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530579 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530581 return 0;
582}
583
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530584static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
585{
586 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
587
588 /*
589 * Allow bus suspend only for host mode. Device mode bus suspend
590 * is not implemented yet.
591 */
592 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530593 /*
594 * ID_GND --> ID_A transition can not be detected in LPM.
595 * Disallow host bus suspend when ACA is enabled.
596 */
597 if (suspend && !aca_enabled())
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530598 pm_runtime_put(otg->dev);
599 else
600 pm_runtime_resume(otg->dev);
601 }
602
603 return 0;
604}
605
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530606#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530607#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
608
609#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530610static int msm_otg_suspend(struct msm_otg *motg)
611{
612 struct otg_transceiver *otg = &motg->otg;
613 struct usb_bus *bus = otg->host;
614 struct msm_otg_platform_data *pdata = motg->pdata;
615 int cnt = 0;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530616 bool host_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530617 u32 phy_ctrl_val = 0, cmd_val;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530618 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530619
620 if (atomic_read(&motg->in_lpm))
621 return 0;
622
623 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530624 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530625 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530626 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530627 * Chipidea 45-nm PHY suspend sequence:
628 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530629 * Interrupt Latch Register auto-clear feature is not present
630 * in all PHY versions. Latch register is clear on read type.
631 * Clear latch register to avoid spurious wakeup from
632 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530633 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530634 * PHY comparators are disabled when PHY enters into low power
635 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
636 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
637 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530638 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530639 * PLL is not turned off when PHY enters into low power mode (LPM).
640 * Disable PLL for maximum power savings.
641 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530642
643 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
644 ulpi_read(otg, 0x14);
645 if (pdata->otg_control == OTG_PHY_CONTROL)
646 ulpi_write(otg, 0x01, 0x30);
647 ulpi_write(otg, 0x08, 0x09);
648 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530649
650 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 * Turn off the OTG comparators, if depends on PMIC for
652 * VBUS and ID notifications.
653 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530654 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700655 ulpi_write(otg, OTG_COMP_DISABLE,
656 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
657 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
658 }
659
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530660 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530661 * PHY may take some time or even fail to enter into low power
662 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
663 * in failure case.
664 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530665 portsc = readl_relaxed(USB_PORTSC);
666 if (!(portsc & PORTSC_PHCD)) {
667 writel_relaxed(portsc | PORTSC_PHCD,
668 USB_PORTSC);
669 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
670 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
671 break;
672 udelay(1);
673 cnt++;
674 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530675 }
676
677 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
678 dev_err(otg->dev, "Unable to suspend PHY\n");
679 msm_otg_reset(otg);
680 enable_irq(motg->irq);
681 return -ETIMEDOUT;
682 }
683
684 /*
685 * PHY has capability to generate interrupt asynchronously in low
686 * power mode (LPM). This interrupt is level triggered. So USB IRQ
687 * line must be disabled till async interrupt enable bit is cleared
688 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
689 * block data communication from PHY.
690 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530691 cmd_val = readl_relaxed(USB_USBCMD);
692 if (host_bus_suspend)
693 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
694 else
695 cmd_val |= ULPI_STP_CTRL;
696 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530697
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530698 /*
699 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
700 * PHY retention and collapse can not happen with VDP_SRC enabled.
701 */
702 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200703 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
704 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
705 /* Enable PHY HV interrupts to wake MPM/Link */
706 phy_ctrl_val |=
707 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
708
709 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700710 motg->lpm_flags |= PHY_RETENTIONED;
711 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530712
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700713 /* Ensure that above operation is completed before turning off clocks */
714 mb();
Manu Gautam28b1bac2012-01-30 16:43:06 +0530715 clk_disable_unprepare(motg->pclk);
716 clk_disable_unprepare(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530717
Anji jonnala7da3f262011-12-02 17:22:14 -0800718 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800719 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800720
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530721 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
722 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 msm_hsusb_ldo_enable(motg, 0);
724 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530725 }
726
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530727 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700728 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530729 msm_hsusb_mhl_switch_enable(motg, 0);
730 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700731
732 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530733 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700734 if (motg->pdata->pmic_id_irq)
735 enable_irq_wake(motg->pdata->pmic_id_irq);
736 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530737 if (bus)
738 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
739
740 atomic_set(&motg->in_lpm, 1);
741 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700742 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530743
744 dev_info(otg->dev, "USB in low power mode\n");
745
746 return 0;
747}
748
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749static int msm_otg_resume(struct msm_otg *motg)
750{
751 struct otg_transceiver *otg = &motg->otg;
752 struct usb_bus *bus = otg->host;
753 int cnt = 0;
754 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200755 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800756 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530757
758 if (!atomic_read(&motg->in_lpm))
759 return 0;
760
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800762
763 /* Vote for TCXO when waking up the phy */
Stephen Boyd7dd22662012-01-26 16:09:31 -0800764 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -0800765 if (ret)
766 dev_err(otg->dev, "%s failed to vote for "
767 "TCXO D0 buffer%d\n", __func__, ret);
768
Manu Gautam28b1bac2012-01-30 16:43:06 +0530769 clk_prepare_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200770
Manu Gautam28b1bac2012-01-30 16:43:06 +0530771 clk_prepare_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530772
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700773 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
774 msm_hsusb_ldo_enable(motg, 1);
775 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
776 }
777
778 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530779 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530780 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200781 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
782 phy_ctrl_val |= PHY_RETEN;
783 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
784 /* Disable PHY HV interrupts */
785 phy_ctrl_val &=
786 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
787 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700788 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530789 }
790
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530791 temp = readl(USB_USBCMD);
792 temp &= ~ASYNC_INTR_CTRL;
793 temp &= ~ULPI_STP_CTRL;
794 writel(temp, USB_USBCMD);
795
796 /*
797 * PHY comes out of low power mode (LPM) in case of wakeup
798 * from asynchronous interrupt.
799 */
800 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
801 goto skip_phy_resume;
802
803 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
804 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
805 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
806 break;
807 udelay(1);
808 cnt++;
809 }
810
811 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
812 /*
813 * This is a fatal error. Reset the link and
814 * PHY. USB state can not be restored. Re-insertion
815 * of USB cable is the only way to get USB working.
816 */
817 dev_err(otg->dev, "Unable to resume USB."
818 "Re-plugin the cable\n");
819 msm_otg_reset(otg);
820 }
821
822skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700823 /* Turn on the OTG comparators on resume */
824 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
825 ulpi_write(otg, OTG_COMP_DISABLE,
826 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
827 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
828 }
829 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530830 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 if (motg->pdata->pmic_id_irq)
832 disable_irq_wake(motg->pdata->pmic_id_irq);
833 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530834 if (bus)
835 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
836
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530837 atomic_set(&motg->in_lpm, 0);
838
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839 if (motg->async_int) {
840 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530841 enable_irq(motg->irq);
842 }
843
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530844 dev_info(otg->dev, "USB exited from low power mode\n");
845
846 return 0;
847}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530848#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530849
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530850static int msm_otg_notify_chg_type(struct msm_otg *motg)
851{
852 static int charger_type;
853 /*
854 * TODO
855 * Unify OTG driver charger types and power supply charger types
856 */
857 if (charger_type == motg->chg_type)
858 return 0;
859
860 if (motg->chg_type == USB_SDP_CHARGER)
861 charger_type = POWER_SUPPLY_TYPE_USB;
862 else if (motg->chg_type == USB_CDP_CHARGER)
863 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
864 else if (motg->chg_type == USB_DCP_CHARGER)
865 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
866 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
867 motg->chg_type == USB_ACA_A_CHARGER ||
868 motg->chg_type == USB_ACA_B_CHARGER ||
869 motg->chg_type == USB_ACA_C_CHARGER))
870 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
871 else
872 charger_type = POWER_SUPPLY_TYPE_BATTERY;
873
874 return pm8921_set_usb_power_supply_type(charger_type);
875}
876
Amit Blay0f7edf72012-01-15 10:11:27 +0200877static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
878{
879 struct power_supply *psy;
880
881 psy = power_supply_get_by_name("usb");
882 if (!psy)
883 goto psy_not_supported;
884
885 if (motg->cur_power == 0 && mA > 0) {
886 /* Enable charging */
887 if (power_supply_set_online(psy, true))
888 goto psy_not_supported;
889 } else if (motg->cur_power > 0 && mA == 0) {
890 /* Disable charging */
891 if (power_supply_set_online(psy, false))
892 goto psy_not_supported;
893 return 0;
894 }
895 /* Set max current limit */
896 if (power_supply_set_current_limit(psy, 1000*mA))
897 goto psy_not_supported;
898
899 return 0;
900
901psy_not_supported:
902 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
903 return -ENXIO;
904}
905
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530906static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
907{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530908 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
909 motg->chg_type == USB_ACA_A_CHARGER ||
910 motg->chg_type == USB_ACA_B_CHARGER ||
911 motg->chg_type == USB_ACA_C_CHARGER) &&
912 mA > IDEV_ACA_CHG_LIMIT)
913 mA = IDEV_ACA_CHG_LIMIT;
914
Chiranjeevi Velempati39f9b952012-01-24 10:46:12 +0530915 if (msm_otg_notify_chg_type(motg))
916 dev_err(motg->otg.dev,
917 "Failed notifying %d charger type to PMIC\n",
918 motg->chg_type);
919
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530920 if (motg->cur_power == mA)
921 return;
922
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530923 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +0200924
925 /*
926 * Use Power Supply API if supported, otherwise fallback
927 * to legacy pm8921 API.
928 */
929 if (msm_otg_notify_power_supply(motg, mA))
930 pm8921_charger_vbus_draw(mA);
931
Pavankumar Kondetid8608522011-05-04 10:19:47 +0530932 motg->cur_power = mA;
933}
934
935static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
936{
937 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
938
939 /*
940 * Gadget driver uses set_power method to notify about the
941 * available current based on suspend/configured states.
942 *
943 * IDEV_CHG can be drawn irrespective of suspend/un-configured
944 * states when CDP/ACA is connected.
945 */
946 if (motg->chg_type == USB_SDP_CHARGER)
947 msm_otg_notify_charger(motg, mA);
948
949 return 0;
950}
951
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530952static void msm_otg_start_host(struct otg_transceiver *otg, int on)
953{
954 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
955 struct msm_otg_platform_data *pdata = motg->pdata;
956 struct usb_hcd *hcd;
957
958 if (!otg->host)
959 return;
960
961 hcd = bus_to_hcd(otg->host);
962
963 if (on) {
964 dev_dbg(otg->dev, "host on\n");
965
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530966 /*
967 * Some boards have a switch cotrolled by gpio
968 * to enable/disable internal HUB. Enable internal
969 * HUB before kicking the host.
970 */
971 if (pdata->setup_gpio)
972 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530973 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530974 } else {
975 dev_dbg(otg->dev, "host off\n");
976
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530977 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530978 /* HCD core reset all bits of PORTSC. select ULPI phy */
979 writel_relaxed(0x80000000, USB_PORTSC);
980
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530981 if (pdata->setup_gpio)
982 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530983 }
984}
985
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700986static int msm_otg_usbdev_notify(struct notifier_block *self,
987 unsigned long action, void *priv)
988{
989 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530990 struct usb_device *udev = priv;
991
992 if (!aca_enabled())
993 goto out;
994
995 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
996 goto out;
997
998 if (udev->bus != motg->otg.host)
999 goto out;
1000 /*
1001 * Interested in devices connected directly to the root hub.
1002 * ACA dock can supply IDEV_CHG irrespective devices connected
1003 * on the accessory port.
1004 */
1005 if (!udev->parent || udev->parent->parent ||
1006 motg->chg_type == USB_ACA_DOCK_CHARGER)
1007 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001008
1009 switch (action) {
1010 case USB_DEVICE_ADD:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301011 usb_disable_autosuspend(udev);
1012 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001013 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001014 if (udev->actconfig)
1015 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1016 else
1017 motg->mA_port = IUNIT;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301018 break;
1019 case USB_DEVICE_REMOVE:
1020 motg->mA_port = IUNIT;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001021 break;
1022 default:
1023 break;
1024 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301025 if (test_bit(ID_A, &motg->inputs))
1026 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1027 motg->mA_port);
1028out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001029 return NOTIFY_OK;
1030}
1031
Mayank Ranae3926882011-12-26 09:47:54 +05301032static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1033{
1034 int ret;
1035 static bool vbus_is_on;
1036
1037 if (vbus_is_on == on)
1038 return;
1039
1040 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301041 ret = motg->pdata->vbus_power(on);
1042 if (!ret)
1043 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301044 return;
1045 }
1046
1047 if (!vbus_otg) {
1048 pr_err("vbus_otg is NULL.");
1049 return;
1050 }
1051
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001052 /*
1053 * if entering host mode tell the charger to not draw any current
1054 * from usb - if exiting host mode let the charger draw current
1055 */
1056 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301057 if (on) {
1058 ret = regulator_enable(vbus_otg);
1059 if (ret) {
1060 pr_err("unable to enable vbus_otg\n");
1061 return;
1062 }
1063 vbus_is_on = true;
1064 } else {
1065 ret = regulator_disable(vbus_otg);
1066 if (ret) {
1067 pr_err("unable to disable vbus_otg\n");
1068 return;
1069 }
1070 vbus_is_on = false;
1071 }
1072}
1073
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301074static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1075{
1076 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1077 struct usb_hcd *hcd;
1078
1079 /*
1080 * Fail host registration if this board can support
1081 * only peripheral configuration.
1082 */
1083 if (motg->pdata->mode == USB_PERIPHERAL) {
1084 dev_info(otg->dev, "Host mode is not supported\n");
1085 return -ENODEV;
1086 }
1087
Mayank Ranae3926882011-12-26 09:47:54 +05301088 if (!motg->pdata->vbus_power && host) {
1089 vbus_otg = regulator_get(motg->otg.dev, "vbus_otg");
1090 if (IS_ERR(vbus_otg)) {
1091 pr_err("Unable to get vbus_otg\n");
1092 return -ENODEV;
1093 }
1094 }
1095
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301096 if (!host) {
1097 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301098 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001099 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301100 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301101 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301102 otg->host = NULL;
1103 otg->state = OTG_STATE_UNDEFINED;
1104 schedule_work(&motg->sm_work);
1105 } else {
1106 otg->host = NULL;
1107 }
1108
Mayank Ranae3926882011-12-26 09:47:54 +05301109 if (vbus_otg)
1110 regulator_put(vbus_otg);
1111
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301112 return 0;
1113 }
1114
1115 hcd = bus_to_hcd(host);
1116 hcd->power_budget = motg->pdata->power_budget;
1117
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001118 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1119 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 otg->host = host;
1121 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1122
1123 /*
1124 * Kick the state machine work, if peripheral is not supported
1125 * or peripheral is already registered with us.
1126 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301127 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1128 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301129 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301130 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301131
1132 return 0;
1133}
1134
1135static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1136{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301137 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301138 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1139 struct msm_otg_platform_data *pdata = motg->pdata;
1140
1141 if (!otg->gadget)
1142 return;
1143
1144 if (on) {
1145 dev_dbg(otg->dev, "gadget on\n");
1146 /*
1147 * Some boards have a switch cotrolled by gpio
1148 * to enable/disable internal HUB. Disable internal
1149 * HUB before kicking the gadget.
1150 */
1151 if (pdata->setup_gpio)
1152 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301153 /*
1154 * vote for minimum dma_latency to prevent idle
1155 * power collapse(pc) while running in peripheral mode.
1156 */
1157 otg_pm_qos_update_latency(motg, 1);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301158 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301159 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301160 ret = msm_bus_scale_client_update_request(
1161 motg->bus_perf_client, 1);
1162 if (ret)
1163 dev_err(motg->otg.dev, "%s: Failed to vote for "
1164 "bus bandwidth %d\n", __func__, ret);
1165 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301166 usb_gadget_vbus_connect(otg->gadget);
1167 } else {
1168 dev_dbg(otg->dev, "gadget off\n");
1169 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301170 otg_pm_qos_update_latency(motg, 0);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301171 /* Configure BUS performance parameters to default */
1172 if (motg->bus_perf_client) {
1173 ret = msm_bus_scale_client_update_request(
1174 motg->bus_perf_client, 0);
1175 if (ret)
1176 dev_err(motg->otg.dev, "%s: Failed to devote "
1177 "for bus bw %d\n", __func__, ret);
1178 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301179 if (pdata->setup_gpio)
1180 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1181 }
1182
1183}
1184
1185static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1186 struct usb_gadget *gadget)
1187{
1188 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1189
1190 /*
1191 * Fail peripheral registration if this board can support
1192 * only host configuration.
1193 */
1194 if (motg->pdata->mode == USB_HOST) {
1195 dev_info(otg->dev, "Peripheral mode is not supported\n");
1196 return -ENODEV;
1197 }
1198
1199 if (!gadget) {
1200 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301201 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301202 msm_otg_start_peripheral(otg, 0);
1203 otg->gadget = NULL;
1204 otg->state = OTG_STATE_UNDEFINED;
1205 schedule_work(&motg->sm_work);
1206 } else {
1207 otg->gadget = NULL;
1208 }
1209
1210 return 0;
1211 }
1212 otg->gadget = gadget;
1213 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1214
1215 /*
1216 * Kick the state machine work, if host is not supported
1217 * or host is already registered with us.
1218 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301219 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1220 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301221 schedule_work(&motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301222 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301223
1224 return 0;
1225}
1226
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227static bool msm_chg_aca_detect(struct msm_otg *motg)
1228{
1229 struct otg_transceiver *otg = &motg->otg;
1230 u32 int_sts;
1231 bool ret = false;
1232
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301233 if (!aca_enabled())
1234 goto out;
1235
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001236 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1237 goto out;
1238
1239 int_sts = ulpi_read(otg, 0x87);
1240 switch (int_sts & 0x1C) {
1241 case 0x08:
1242 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1243 dev_dbg(otg->dev, "ID_A\n");
1244 motg->chg_type = USB_ACA_A_CHARGER;
1245 motg->chg_state = USB_CHG_STATE_DETECTED;
1246 clear_bit(ID_B, &motg->inputs);
1247 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301248 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001249 ret = true;
1250 }
1251 break;
1252 case 0x0C:
1253 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1254 dev_dbg(otg->dev, "ID_B\n");
1255 motg->chg_type = USB_ACA_B_CHARGER;
1256 motg->chg_state = USB_CHG_STATE_DETECTED;
1257 clear_bit(ID_A, &motg->inputs);
1258 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301259 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001260 ret = true;
1261 }
1262 break;
1263 case 0x10:
1264 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1265 dev_dbg(otg->dev, "ID_C\n");
1266 motg->chg_type = USB_ACA_C_CHARGER;
1267 motg->chg_state = USB_CHG_STATE_DETECTED;
1268 clear_bit(ID_A, &motg->inputs);
1269 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301270 set_bit(ID, &motg->inputs);
1271 ret = true;
1272 }
1273 break;
1274 case 0x04:
1275 if (test_and_clear_bit(ID, &motg->inputs)) {
1276 dev_dbg(otg->dev, "ID_GND\n");
1277 motg->chg_type = USB_INVALID_CHARGER;
1278 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1279 clear_bit(ID_A, &motg->inputs);
1280 clear_bit(ID_B, &motg->inputs);
1281 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 ret = true;
1283 }
1284 break;
1285 default:
1286 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1287 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301288 test_and_clear_bit(ID_C, &motg->inputs) |
1289 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301291 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001292 motg->chg_type = USB_INVALID_CHARGER;
1293 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1294 }
1295 }
1296out:
1297 return ret;
1298}
1299
1300static void msm_chg_enable_aca_det(struct msm_otg *motg)
1301{
1302 struct otg_transceiver *otg = &motg->otg;
1303
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301304 if (!aca_enabled())
1305 return;
1306
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001307 switch (motg->pdata->phy_type) {
1308 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301309 /* Disable ID_GND in link and PHY */
1310 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1311 OTGSC_IDIE), USB_OTGSC);
1312 ulpi_write(otg, 0x01, 0x0C);
1313 ulpi_write(otg, 0x10, 0x0F);
1314 ulpi_write(otg, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301315 /* Disable PMIC ID pull-up */
1316 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301317 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001318 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301319 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320 break;
1321 default:
1322 break;
1323 }
1324}
1325
1326static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1327{
1328 struct otg_transceiver *otg = &motg->otg;
1329
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301330 if (!aca_enabled())
1331 return;
1332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001333 switch (motg->pdata->phy_type) {
1334 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301335 /* Enable ACA Detection interrupt (on any RID change) */
1336 ulpi_write(otg, 0x01, 0x94);
1337 break;
1338 default:
1339 break;
1340 }
1341}
1342
1343static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1344{
1345 struct otg_transceiver *otg = &motg->otg;
1346
1347 if (!aca_enabled())
1348 return;
1349
1350 switch (motg->pdata->phy_type) {
1351 case SNPS_28NM_INTEGRATED_PHY:
1352 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001353 break;
1354 default:
1355 break;
1356 }
1357}
1358
1359static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1360{
1361 struct otg_transceiver *otg = &motg->otg;
1362 bool ret = false;
1363
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301364 if (!aca_enabled())
1365 return ret;
1366
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001367 switch (motg->pdata->phy_type) {
1368 case SNPS_28NM_INTEGRATED_PHY:
1369 if (ulpi_read(otg, 0x91) & 1) {
1370 dev_dbg(otg->dev, "RID change\n");
1371 ulpi_write(otg, 0x01, 0x92);
1372 ret = msm_chg_aca_detect(motg);
1373 }
1374 default:
1375 break;
1376 }
1377 return ret;
1378}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301379
1380static void msm_otg_id_timer_func(unsigned long data)
1381{
1382 struct msm_otg *motg = (struct msm_otg *) data;
1383
1384 if (!aca_enabled())
1385 return;
1386
1387 if (atomic_read(&motg->in_lpm)) {
1388 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1389 return;
1390 }
1391
1392 if (msm_chg_check_aca_intr(motg)) {
1393 dev_dbg(motg->otg.dev, "timer: aca work\n");
1394 schedule_work(&motg->sm_work);
1395 }
1396
1397 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1398 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1399}
1400
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301401static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1402{
1403 struct otg_transceiver *otg = &motg->otg;
1404 u32 chg_det;
1405 bool ret = false;
1406
1407 switch (motg->pdata->phy_type) {
1408 case CI_45NM_INTEGRATED_PHY:
1409 chg_det = ulpi_read(otg, 0x34);
1410 ret = chg_det & (1 << 4);
1411 break;
1412 case SNPS_28NM_INTEGRATED_PHY:
1413 chg_det = ulpi_read(otg, 0x87);
1414 ret = chg_det & 1;
1415 break;
1416 default:
1417 break;
1418 }
1419 return ret;
1420}
1421
1422static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1423{
1424 struct otg_transceiver *otg = &motg->otg;
1425 u32 chg_det;
1426
1427 switch (motg->pdata->phy_type) {
1428 case CI_45NM_INTEGRATED_PHY:
1429 chg_det = ulpi_read(otg, 0x34);
1430 /* Turn off charger block */
1431 chg_det |= ~(1 << 1);
1432 ulpi_write(otg, chg_det, 0x34);
1433 udelay(20);
1434 /* control chg block via ULPI */
1435 chg_det &= ~(1 << 3);
1436 ulpi_write(otg, chg_det, 0x34);
1437 /* put it in host mode for enabling D- source */
1438 chg_det &= ~(1 << 2);
1439 ulpi_write(otg, chg_det, 0x34);
1440 /* Turn on chg detect block */
1441 chg_det &= ~(1 << 1);
1442 ulpi_write(otg, chg_det, 0x34);
1443 udelay(20);
1444 /* enable chg detection */
1445 chg_det &= ~(1 << 0);
1446 ulpi_write(otg, chg_det, 0x34);
1447 break;
1448 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301449 /* Turn off VDP_SRC */
1450 ulpi_write(otg, 0x3, 0x86);
1451 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301452 /*
1453 * Configure DM as current source, DP as current sink
1454 * and enable battery charging comparators.
1455 */
1456 ulpi_write(otg, 0x8, 0x85);
1457 ulpi_write(otg, 0x2, 0x85);
1458 ulpi_write(otg, 0x1, 0x85);
1459 break;
1460 default:
1461 break;
1462 }
1463}
1464
1465static bool msm_chg_check_primary_det(struct msm_otg *motg)
1466{
1467 struct otg_transceiver *otg = &motg->otg;
1468 u32 chg_det;
1469 bool ret = false;
1470
1471 switch (motg->pdata->phy_type) {
1472 case CI_45NM_INTEGRATED_PHY:
1473 chg_det = ulpi_read(otg, 0x34);
1474 ret = chg_det & (1 << 4);
1475 break;
1476 case SNPS_28NM_INTEGRATED_PHY:
1477 chg_det = ulpi_read(otg, 0x87);
1478 ret = chg_det & 1;
1479 break;
1480 default:
1481 break;
1482 }
1483 return ret;
1484}
1485
1486static void msm_chg_enable_primary_det(struct msm_otg *motg)
1487{
1488 struct otg_transceiver *otg = &motg->otg;
1489 u32 chg_det;
1490
1491 switch (motg->pdata->phy_type) {
1492 case CI_45NM_INTEGRATED_PHY:
1493 chg_det = ulpi_read(otg, 0x34);
1494 /* enable chg detection */
1495 chg_det &= ~(1 << 0);
1496 ulpi_write(otg, chg_det, 0x34);
1497 break;
1498 case SNPS_28NM_INTEGRATED_PHY:
1499 /*
1500 * Configure DP as current source, DM as current sink
1501 * and enable battery charging comparators.
1502 */
1503 ulpi_write(otg, 0x2, 0x85);
1504 ulpi_write(otg, 0x1, 0x85);
1505 break;
1506 default:
1507 break;
1508 }
1509}
1510
1511static bool msm_chg_check_dcd(struct msm_otg *motg)
1512{
1513 struct otg_transceiver *otg = &motg->otg;
1514 u32 line_state;
1515 bool ret = false;
1516
1517 switch (motg->pdata->phy_type) {
1518 case CI_45NM_INTEGRATED_PHY:
1519 line_state = ulpi_read(otg, 0x15);
1520 ret = !(line_state & 1);
1521 break;
1522 case SNPS_28NM_INTEGRATED_PHY:
1523 line_state = ulpi_read(otg, 0x87);
1524 ret = line_state & 2;
1525 break;
1526 default:
1527 break;
1528 }
1529 return ret;
1530}
1531
1532static void msm_chg_disable_dcd(struct msm_otg *motg)
1533{
1534 struct otg_transceiver *otg = &motg->otg;
1535 u32 chg_det;
1536
1537 switch (motg->pdata->phy_type) {
1538 case CI_45NM_INTEGRATED_PHY:
1539 chg_det = ulpi_read(otg, 0x34);
1540 chg_det &= ~(1 << 5);
1541 ulpi_write(otg, chg_det, 0x34);
1542 break;
1543 case SNPS_28NM_INTEGRATED_PHY:
1544 ulpi_write(otg, 0x10, 0x86);
1545 break;
1546 default:
1547 break;
1548 }
1549}
1550
1551static void msm_chg_enable_dcd(struct msm_otg *motg)
1552{
1553 struct otg_transceiver *otg = &motg->otg;
1554 u32 chg_det;
1555
1556 switch (motg->pdata->phy_type) {
1557 case CI_45NM_INTEGRATED_PHY:
1558 chg_det = ulpi_read(otg, 0x34);
1559 /* Turn on D+ current source */
1560 chg_det |= (1 << 5);
1561 ulpi_write(otg, chg_det, 0x34);
1562 break;
1563 case SNPS_28NM_INTEGRATED_PHY:
1564 /* Data contact detection enable */
1565 ulpi_write(otg, 0x10, 0x85);
1566 break;
1567 default:
1568 break;
1569 }
1570}
1571
1572static void msm_chg_block_on(struct msm_otg *motg)
1573{
1574 struct otg_transceiver *otg = &motg->otg;
1575 u32 func_ctrl, chg_det;
1576
1577 /* put the controller in non-driving mode */
1578 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1579 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1580 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1581 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1582
1583 switch (motg->pdata->phy_type) {
1584 case CI_45NM_INTEGRATED_PHY:
1585 chg_det = ulpi_read(otg, 0x34);
1586 /* control chg block via ULPI */
1587 chg_det &= ~(1 << 3);
1588 ulpi_write(otg, chg_det, 0x34);
1589 /* Turn on chg detect block */
1590 chg_det &= ~(1 << 1);
1591 ulpi_write(otg, chg_det, 0x34);
1592 udelay(20);
1593 break;
1594 case SNPS_28NM_INTEGRATED_PHY:
1595 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301596 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301597 /* Clear alt interrupt latch and enable bits */
1598 ulpi_write(otg, 0x1F, 0x92);
1599 ulpi_write(otg, 0x1F, 0x95);
1600 udelay(100);
1601 break;
1602 default:
1603 break;
1604 }
1605}
1606
1607static void msm_chg_block_off(struct msm_otg *motg)
1608{
1609 struct otg_transceiver *otg = &motg->otg;
1610 u32 func_ctrl, chg_det;
1611
1612 switch (motg->pdata->phy_type) {
1613 case CI_45NM_INTEGRATED_PHY:
1614 chg_det = ulpi_read(otg, 0x34);
1615 /* Turn off charger block */
1616 chg_det |= ~(1 << 1);
1617 ulpi_write(otg, chg_det, 0x34);
1618 break;
1619 case SNPS_28NM_INTEGRATED_PHY:
1620 /* Clear charger detecting control bits */
1621 ulpi_write(otg, 0x3F, 0x86);
1622 /* Clear alt interrupt latch and enable bits */
1623 ulpi_write(otg, 0x1F, 0x92);
1624 ulpi_write(otg, 0x1F, 0x95);
1625 break;
1626 default:
1627 break;
1628 }
1629
1630 /* put the controller in normal mode */
1631 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1632 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1633 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1634 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1635}
1636
Anji jonnalad270e2d2011-08-09 11:28:32 +05301637static const char *chg_to_string(enum usb_chg_type chg_type)
1638{
1639 switch (chg_type) {
1640 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1641 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1642 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1643 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1644 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1645 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1646 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1647 default: return "INVALID_CHARGER";
1648 }
1649}
1650
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301651#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1652#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301653#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1654#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301655static void msm_chg_detect_work(struct work_struct *w)
1656{
1657 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1658 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301659 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301660 unsigned long delay;
1661
1662 dev_dbg(otg->dev, "chg detection work\n");
1663 switch (motg->chg_state) {
1664 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301665 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301666 if (motg->pdata->enable_dcd)
1667 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001668 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301669 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1670 motg->dcd_retries = 0;
1671 delay = MSM_CHG_DCD_POLL_TIME;
1672 break;
1673 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 is_aca = msm_chg_aca_detect(motg);
1675 if (is_aca) {
1676 /*
1677 * ID_A can be ACA dock too. continue
1678 * primary detection after DCD.
1679 */
1680 if (test_bit(ID_A, &motg->inputs)) {
1681 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1682 } else {
1683 delay = 0;
1684 break;
1685 }
1686 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301687 if (motg->pdata->enable_dcd)
1688 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301689 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1690 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301691 if (motg->pdata->enable_dcd)
1692 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301693 msm_chg_enable_primary_det(motg);
1694 delay = MSM_CHG_PRIMARY_DET_TIME;
1695 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1696 } else {
1697 delay = MSM_CHG_DCD_POLL_TIME;
1698 }
1699 break;
1700 case USB_CHG_STATE_DCD_DONE:
1701 vout = msm_chg_check_primary_det(motg);
1702 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301703 if (test_bit(ID_A, &motg->inputs)) {
1704 motg->chg_type = USB_ACA_DOCK_CHARGER;
1705 motg->chg_state = USB_CHG_STATE_DETECTED;
1706 delay = 0;
1707 break;
1708 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301709 msm_chg_enable_secondary_det(motg);
1710 delay = MSM_CHG_SECONDARY_DET_TIME;
1711 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1712 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301713 if (test_bit(ID_A, &motg->inputs)) {
1714 motg->chg_type = USB_ACA_A_CHARGER;
1715 motg->chg_state = USB_CHG_STATE_DETECTED;
1716 delay = 0;
1717 break;
1718 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301719 motg->chg_type = USB_SDP_CHARGER;
1720 motg->chg_state = USB_CHG_STATE_DETECTED;
1721 delay = 0;
1722 }
1723 break;
1724 case USB_CHG_STATE_PRIMARY_DONE:
1725 vout = msm_chg_check_secondary_det(motg);
1726 if (vout)
1727 motg->chg_type = USB_DCP_CHARGER;
1728 else
1729 motg->chg_type = USB_CDP_CHARGER;
1730 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1731 /* fall through */
1732 case USB_CHG_STATE_SECONDARY_DONE:
1733 motg->chg_state = USB_CHG_STATE_DETECTED;
1734 case USB_CHG_STATE_DETECTED:
1735 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736 msm_chg_enable_aca_det(motg);
1737 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301738 dev_dbg(otg->dev, "chg_type = %s\n",
1739 chg_to_string(motg->chg_type));
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301740 schedule_work(&motg->sm_work);
1741 return;
1742 default:
1743 return;
1744 }
1745
1746 schedule_delayed_work(&motg->chg_work, delay);
1747}
1748
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301749/*
1750 * We support OTG, Peripheral only and Host only configurations. In case
1751 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1752 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1753 * enabled when switch is controlled by user and default mode is supplied
1754 * by board file, which can be changed by userspace later.
1755 */
1756static void msm_otg_init_sm(struct msm_otg *motg)
1757{
1758 struct msm_otg_platform_data *pdata = motg->pdata;
1759 u32 otgsc = readl(USB_OTGSC);
1760
1761 switch (pdata->mode) {
1762 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001763 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301764 if (pdata->default_mode == USB_HOST) {
1765 clear_bit(ID, &motg->inputs);
1766 } else if (pdata->default_mode == USB_PERIPHERAL) {
1767 set_bit(ID, &motg->inputs);
1768 set_bit(B_SESS_VLD, &motg->inputs);
1769 } else {
1770 set_bit(ID, &motg->inputs);
1771 clear_bit(B_SESS_VLD, &motg->inputs);
1772 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301773 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
1774 if (otgsc & OTGSC_ID)
1775 set_bit(ID, &motg->inputs);
1776 else
1777 clear_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001778 if (otgsc & OTGSC_BSV)
1779 set_bit(B_SESS_VLD, &motg->inputs);
1780 else
1781 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301782 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301783 if (pdata->pmic_id_irq) {
1784 if (irq_read_line(pdata->pmic_id_irq))
1785 set_bit(ID, &motg->inputs);
1786 else
1787 clear_bit(ID, &motg->inputs);
1788 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301789 /*
1790 * VBUS initial state is reported after PMIC
1791 * driver initialization. Wait for it.
1792 */
1793 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301794 }
1795 break;
1796 case USB_HOST:
1797 clear_bit(ID, &motg->inputs);
1798 break;
1799 case USB_PERIPHERAL:
1800 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301801 if (pdata->otg_control == OTG_PHY_CONTROL) {
1802 if (otgsc & OTGSC_BSV)
1803 set_bit(B_SESS_VLD, &motg->inputs);
1804 else
1805 clear_bit(B_SESS_VLD, &motg->inputs);
1806 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1807 /*
1808 * VBUS initial state is reported after PMIC
1809 * driver initialization. Wait for it.
1810 */
1811 wait_for_completion(&pmic_vbus_init);
1812 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301813 break;
1814 default:
1815 break;
1816 }
1817}
1818
1819static void msm_otg_sm_work(struct work_struct *w)
1820{
1821 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
1822 struct otg_transceiver *otg = &motg->otg;
1823
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301824 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301825 switch (otg->state) {
1826 case OTG_STATE_UNDEFINED:
1827 dev_dbg(otg->dev, "OTG_STATE_UNDEFINED state\n");
1828 msm_otg_reset(otg);
1829 msm_otg_init_sm(motg);
1830 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05301831 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
1832 test_bit(ID, &motg->inputs)) {
1833 pm_runtime_put_noidle(otg->dev);
1834 pm_runtime_suspend(otg->dev);
1835 break;
1836 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301837 /* FALL THROUGH */
1838 case OTG_STATE_B_IDLE:
1839 dev_dbg(otg->dev, "OTG_STATE_B_IDLE state\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001840 if ((!test_bit(ID, &motg->inputs) ||
1841 test_bit(ID_A, &motg->inputs)) && otg->host) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001842 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
1843 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301844 IDEV_ACA_CHG_MAX);
1845 else if (test_bit(ID_A, &motg->inputs))
1846 msm_otg_notify_charger(motg,
1847 IDEV_ACA_CHG_MAX - IUNIT);
Mayank Ranae3926882011-12-26 09:47:54 +05301848 else
1849 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301850 msm_otg_start_host(otg, 1);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301851 /*
1852 * Link can not generate PHY_ALT interrupt
1853 * in host mode when no device is attached
1854 * to the port. It is also observed PHY_ALT
1855 * interrupt missing upon Micro-A cable disconnect.
1856 * Hence disable PHY_ALT interrupt and perform
1857 * polling to detect RID change.
1858 */
1859 msm_chg_enable_aca_det(motg);
1860 msm_chg_disable_aca_intr(motg);
1861 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301862 otg->state = OTG_STATE_A_HOST;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301863 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
1864 switch (motg->chg_state) {
1865 case USB_CHG_STATE_UNDEFINED:
1866 msm_chg_detect_work(&motg->chg_work.work);
1867 break;
1868 case USB_CHG_STATE_DETECTED:
1869 switch (motg->chg_type) {
1870 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301871 /* Enable VDP_SRC */
1872 ulpi_write(otg, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 msm_otg_notify_charger(motg,
1874 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301875 pm_runtime_put_noidle(otg->dev);
1876 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301878 case USB_ACA_B_CHARGER:
1879 msm_otg_notify_charger(motg,
1880 IDEV_ACA_CHG_MAX);
1881 /*
1882 * (ID_B --> ID_C) PHY_ALT interrupt can
1883 * not be detected in LPM.
1884 */
1885 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 case USB_CDP_CHARGER:
1887 msm_otg_notify_charger(motg,
1888 IDEV_CHG_MAX);
1889 msm_otg_start_peripheral(otg, 1);
1890 otg->state = OTG_STATE_B_PERIPHERAL;
1891 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301892 case USB_ACA_C_CHARGER:
1893 msm_otg_notify_charger(motg,
1894 IDEV_ACA_CHG_MAX);
1895 msm_otg_start_peripheral(otg, 1);
1896 otg->state = OTG_STATE_B_PERIPHERAL;
1897 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301898 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 msm_otg_start_peripheral(otg, 1);
1900 otg->state = OTG_STATE_B_PERIPHERAL;
1901 break;
1902 default:
1903 break;
1904 }
1905 break;
1906 default:
1907 break;
1908 }
1909 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301910 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301911 msm_otg_notify_charger(motg, 0);
1912 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1913 motg->chg_type = USB_INVALID_CHARGER;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301914 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301915 pm_runtime_put_noidle(otg->dev);
1916 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301917 }
1918 break;
1919 case OTG_STATE_B_PERIPHERAL:
1920 dev_dbg(otg->dev, "OTG_STATE_B_PERIPHERAL state\n");
1921 if (!test_bit(B_SESS_VLD, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001922 !test_bit(ID, &motg->inputs) ||
1923 !test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301924 msm_otg_start_peripheral(otg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001925 otg->state = OTG_STATE_B_IDLE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001926 schedule_work(w);
1927 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301928 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001929 }
1930 break;
1931 case OTG_STATE_A_HOST:
1932 dev_dbg(otg->dev, "OTG_STATE_A_HOST state\n");
1933 if (test_bit(ID, &motg->inputs) &&
1934 !test_bit(ID_A, &motg->inputs)) {
1935 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301936 msm_hsusb_vbus_power(motg, 0);
1937 msleep(100); /* TA_WAIT_VFALL */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301938 /*
1939 * Exit point of host mode.
1940 *
1941 * 1. Micro-A cable disconnect: Just schedule
1942 * the work. PHY is reset in B_IDLE and LPM
1943 * is allowed.
1944 * 2. ID_GND --> ID_B: No need to reset the PHY.
1945 * HCD core clears all PORTSC bits and initializes
1946 * the controller to host mode in remove_hcd.
1947 * Restore PORTSC transceiver select bits (ULPI)
1948 * and reset the controller to change MODE bits.
1949 * PHY_ALT interrupt can not occur in host mode.
1950 */
1951 del_timer_sync(&motg->id_timer);
1952 if (motg->chg_state != USB_CHG_STATE_UNDEFINED) {
1953 msm_otg_link_reset(motg);
1954 msm_chg_enable_aca_intr(motg);
1955 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301956 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301957 schedule_work(w);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001958 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05301959 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960 msm_otg_notify_charger(motg,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301961 IDEV_ACA_CHG_MAX - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001962 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001963 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301964 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301965 }
1966 break;
1967 default:
1968 break;
1969 }
1970}
1971
1972static irqreturn_t msm_otg_irq(int irq, void *data)
1973{
1974 struct msm_otg *motg = data;
1975 struct otg_transceiver *otg = &motg->otg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001976 u32 otgsc = 0, usbsts;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301977
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301978 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301979 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301980 disable_irq_nosync(irq);
1981 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05301982 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301983 return IRQ_HANDLED;
1984 }
1985
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001986 usbsts = readl(USB_USBSTS);
1987 if ((usbsts & PHY_ALT_INT)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301988 dev_dbg(otg->dev, "PHY_ALT interrupt\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001989 writel(PHY_ALT_INT, USB_USBSTS);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301990 if (msm_chg_check_aca_intr(motg)) {
1991 dev_dbg(otg->dev, "ACA work from IRQ\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001992 schedule_work(&motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301993 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001994 return IRQ_HANDLED;
1995 }
1996
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301997 otgsc = readl(USB_OTGSC);
1998 if (!(otgsc & (OTGSC_IDIS | OTGSC_BSVIS)))
1999 return IRQ_NONE;
2000
2001 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302002 if (otgsc & OTGSC_ID) {
2003 dev_dbg(otg->dev, "ID set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302004 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302005 } else {
2006 dev_dbg(otg->dev, "ID clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302007 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302008 msm_chg_enable_aca_det(motg);
2009 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002010 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302011 } else if ((otgsc & OTGSC_BSVIS) && (otgsc & OTGSC_BSVIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302012 if (otgsc & OTGSC_BSV) {
2013 dev_dbg(otg->dev, "BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302014 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302015 } else {
2016 dev_dbg(otg->dev, "BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302017 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302018 msm_chg_check_aca_intr(motg);
2019 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002020 schedule_work(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302021 }
2022
2023 writel(otgsc, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002024 return IRQ_HANDLED;
2025}
2026
2027static void msm_otg_set_vbus_state(int online)
2028{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302029 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002030 struct msm_otg *motg = the_msm_otg;
2031
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302032 if (online) {
2033 pr_debug("PMIC: BSV set\n");
2034 set_bit(B_SESS_VLD, &motg->inputs);
2035 } else {
2036 pr_debug("PMIC: BSV clear\n");
2037 clear_bit(B_SESS_VLD, &motg->inputs);
2038 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002039
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302040 if (!init) {
2041 init = true;
2042 complete(&pmic_vbus_init);
2043 pr_debug("PMIC: BSV init complete\n");
2044 return;
2045 }
2046
2047 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048}
2049
2050static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2051{
2052 struct msm_otg *motg = data;
2053
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302054 if (aca_id_turned_on)
2055 return IRQ_HANDLED;
2056
2057 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2058 pr_debug("PMIC: ID set\n");
2059 set_bit(ID, &motg->inputs);
2060 } else {
2061 pr_debug("PMIC: ID clear\n");
2062 clear_bit(ID, &motg->inputs);
2063 }
2064
2065 if (motg->otg.state != OTG_STATE_UNDEFINED)
2066 schedule_work(&motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002067
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302068 return IRQ_HANDLED;
2069}
2070
2071static int msm_otg_mode_show(struct seq_file *s, void *unused)
2072{
2073 struct msm_otg *motg = s->private;
2074 struct otg_transceiver *otg = &motg->otg;
2075
2076 switch (otg->state) {
2077 case OTG_STATE_A_HOST:
2078 seq_printf(s, "host\n");
2079 break;
2080 case OTG_STATE_B_PERIPHERAL:
2081 seq_printf(s, "peripheral\n");
2082 break;
2083 default:
2084 seq_printf(s, "none\n");
2085 break;
2086 }
2087
2088 return 0;
2089}
2090
2091static int msm_otg_mode_open(struct inode *inode, struct file *file)
2092{
2093 return single_open(file, msm_otg_mode_show, inode->i_private);
2094}
2095
2096static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2097 size_t count, loff_t *ppos)
2098{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302099 struct seq_file *s = file->private_data;
2100 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302101 char buf[16];
2102 struct otg_transceiver *otg = &motg->otg;
2103 int status = count;
2104 enum usb_mode_type req_mode;
2105
2106 memset(buf, 0x00, sizeof(buf));
2107
2108 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2109 status = -EFAULT;
2110 goto out;
2111 }
2112
2113 if (!strncmp(buf, "host", 4)) {
2114 req_mode = USB_HOST;
2115 } else if (!strncmp(buf, "peripheral", 10)) {
2116 req_mode = USB_PERIPHERAL;
2117 } else if (!strncmp(buf, "none", 4)) {
2118 req_mode = USB_NONE;
2119 } else {
2120 status = -EINVAL;
2121 goto out;
2122 }
2123
2124 switch (req_mode) {
2125 case USB_NONE:
2126 switch (otg->state) {
2127 case OTG_STATE_A_HOST:
2128 case OTG_STATE_B_PERIPHERAL:
2129 set_bit(ID, &motg->inputs);
2130 clear_bit(B_SESS_VLD, &motg->inputs);
2131 break;
2132 default:
2133 goto out;
2134 }
2135 break;
2136 case USB_PERIPHERAL:
2137 switch (otg->state) {
2138 case OTG_STATE_B_IDLE:
2139 case OTG_STATE_A_HOST:
2140 set_bit(ID, &motg->inputs);
2141 set_bit(B_SESS_VLD, &motg->inputs);
2142 break;
2143 default:
2144 goto out;
2145 }
2146 break;
2147 case USB_HOST:
2148 switch (otg->state) {
2149 case OTG_STATE_B_IDLE:
2150 case OTG_STATE_B_PERIPHERAL:
2151 clear_bit(ID, &motg->inputs);
2152 break;
2153 default:
2154 goto out;
2155 }
2156 break;
2157 default:
2158 goto out;
2159 }
2160
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302161 pm_runtime_resume(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302162 schedule_work(&motg->sm_work);
2163out:
2164 return status;
2165}
2166
2167const struct file_operations msm_otg_mode_fops = {
2168 .open = msm_otg_mode_open,
2169 .read = seq_read,
2170 .write = msm_otg_mode_write,
2171 .llseek = seq_lseek,
2172 .release = single_release,
2173};
2174
Anji jonnalad270e2d2011-08-09 11:28:32 +05302175static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2176{
2177 struct msm_otg *motg = s->private;
2178
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302179 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302180 return 0;
2181}
2182
2183static int msm_otg_chg_open(struct inode *inode, struct file *file)
2184{
2185 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2186}
2187
2188const struct file_operations msm_otg_chg_fops = {
2189 .open = msm_otg_chg_open,
2190 .read = seq_read,
2191 .llseek = seq_lseek,
2192 .release = single_release,
2193};
2194
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302195static int msm_otg_aca_show(struct seq_file *s, void *unused)
2196{
2197 if (debug_aca_enabled)
2198 seq_printf(s, "enabled\n");
2199 else
2200 seq_printf(s, "disabled\n");
2201
2202 return 0;
2203}
2204
2205static int msm_otg_aca_open(struct inode *inode, struct file *file)
2206{
2207 return single_open(file, msm_otg_aca_show, inode->i_private);
2208}
2209
2210static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2211 size_t count, loff_t *ppos)
2212{
2213 char buf[8];
2214
2215 memset(buf, 0x00, sizeof(buf));
2216
2217 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2218 return -EFAULT;
2219
2220 if (!strncmp(buf, "enable", 6))
2221 debug_aca_enabled = true;
2222 else
2223 debug_aca_enabled = false;
2224
2225 return count;
2226}
2227
2228const struct file_operations msm_otg_aca_fops = {
2229 .open = msm_otg_aca_open,
2230 .read = seq_read,
2231 .write = msm_otg_aca_write,
2232 .llseek = seq_lseek,
2233 .release = single_release,
2234};
2235
Manu Gautam8bdcc592012-03-06 11:26:06 +05302236static int msm_otg_bus_show(struct seq_file *s, void *unused)
2237{
2238 if (debug_bus_voting_enabled)
2239 seq_printf(s, "enabled\n");
2240 else
2241 seq_printf(s, "disabled\n");
2242
2243 return 0;
2244}
2245
2246static int msm_otg_bus_open(struct inode *inode, struct file *file)
2247{
2248 return single_open(file, msm_otg_bus_show, inode->i_private);
2249}
2250
2251static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2252 size_t count, loff_t *ppos)
2253{
2254 char buf[8];
2255 int ret;
2256 struct seq_file *s = file->private_data;
2257 struct msm_otg *motg = s->private;
2258
2259 memset(buf, 0x00, sizeof(buf));
2260
2261 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2262 return -EFAULT;
2263
2264 if (!strncmp(buf, "enable", 6)) {
2265 /* Do not vote here. Let OTG statemachine decide when to vote */
2266 debug_bus_voting_enabled = true;
2267 } else {
2268 debug_bus_voting_enabled = false;
2269 if (motg->bus_perf_client) {
2270 ret = msm_bus_scale_client_update_request(
2271 motg->bus_perf_client, 0);
2272 if (ret)
2273 dev_err(motg->otg.dev, "%s: Failed to devote "
2274 "for bus bw %d\n", __func__, ret);
2275 }
2276 }
2277
2278 return count;
2279}
2280
2281const struct file_operations msm_otg_bus_fops = {
2282 .open = msm_otg_bus_open,
2283 .read = seq_read,
2284 .write = msm_otg_bus_write,
2285 .llseek = seq_lseek,
2286 .release = single_release,
2287};
2288
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302289static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302290
2291static int msm_otg_debugfs_init(struct msm_otg *motg)
2292{
Manu Gautam8bdcc592012-03-06 11:26:06 +05302293 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05302294
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302295 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
2296
2297 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
2298 return -ENODEV;
2299
Anji jonnalad270e2d2011-08-09 11:28:32 +05302300 if (motg->pdata->mode == USB_OTG &&
2301 motg->pdata->otg_control == OTG_USER_CONTROL) {
2302
Manu Gautam8bdcc592012-03-06 11:26:06 +05302303 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05302304 S_IWUSR, msm_otg_dbg_root, motg,
2305 &msm_otg_mode_fops);
2306
Manu Gautam8bdcc592012-03-06 11:26:06 +05302307 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05302308 debugfs_remove(msm_otg_dbg_root);
2309 msm_otg_dbg_root = NULL;
2310 return -ENODEV;
2311 }
2312 }
2313
Manu Gautam8bdcc592012-03-06 11:26:06 +05302314 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05302315 msm_otg_dbg_root, motg,
2316 &msm_otg_chg_fops);
2317
Manu Gautam8bdcc592012-03-06 11:26:06 +05302318 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302319 debugfs_remove_recursive(msm_otg_dbg_root);
2320 return -ENODEV;
2321 }
2322
Manu Gautam8bdcc592012-03-06 11:26:06 +05302323 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302324 msm_otg_dbg_root, motg,
2325 &msm_otg_aca_fops);
2326
Manu Gautam8bdcc592012-03-06 11:26:06 +05302327 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302328 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302329 return -ENODEV;
2330 }
2331
Manu Gautam8bdcc592012-03-06 11:26:06 +05302332 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
2333 msm_otg_dbg_root, motg,
2334 &msm_otg_bus_fops);
2335
2336 if (!msm_otg_dentry) {
2337 debugfs_remove_recursive(msm_otg_dbg_root);
2338 return -ENODEV;
2339 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302340 return 0;
2341}
2342
2343static void msm_otg_debugfs_cleanup(void)
2344{
Anji jonnalad270e2d2011-08-09 11:28:32 +05302345 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302346}
2347
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302348static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
2349static struct platform_device *msm_otg_add_pdev(
2350 struct platform_device *ofdev, const char *name)
2351{
2352 struct platform_device *pdev;
2353 const struct resource *res = ofdev->resource;
2354 unsigned int num = ofdev->num_resources;
2355 int retval;
2356
2357 pdev = platform_device_alloc(name, -1);
2358 if (!pdev) {
2359 retval = -ENOMEM;
2360 goto error;
2361 }
2362
2363 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2364 pdev->dev.dma_mask = &msm_otg_dma_mask;
2365
2366 if (num) {
2367 retval = platform_device_add_resources(pdev, res, num);
2368 if (retval)
2369 goto error;
2370 }
2371
2372 retval = platform_device_add(pdev);
2373 if (retval)
2374 goto error;
2375
2376 return pdev;
2377
2378error:
2379 platform_device_put(pdev);
2380 return ERR_PTR(retval);
2381}
2382
2383static int msm_otg_setup_devices(struct platform_device *ofdev,
2384 enum usb_mode_type mode, bool init)
2385{
2386 const char *gadget_name = "msm_hsusb";
2387 const char *host_name = "msm_hsusb_host";
2388 static struct platform_device *gadget_pdev;
2389 static struct platform_device *host_pdev;
2390 int retval = 0;
2391
2392 if (!init) {
2393 if (gadget_pdev)
2394 platform_device_unregister(gadget_pdev);
2395 if (host_pdev)
2396 platform_device_unregister(host_pdev);
2397 return 0;
2398 }
2399
2400 switch (mode) {
2401 case USB_OTG:
2402 /* fall through */
2403 case USB_PERIPHERAL:
2404 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
2405 if (IS_ERR(gadget_pdev)) {
2406 retval = PTR_ERR(gadget_pdev);
2407 break;
2408 }
2409 if (mode == USB_PERIPHERAL)
2410 break;
2411 /* fall through */
2412 case USB_HOST:
2413 host_pdev = msm_otg_add_pdev(ofdev, host_name);
2414 if (IS_ERR(host_pdev)) {
2415 retval = PTR_ERR(host_pdev);
2416 if (mode == USB_OTG)
2417 platform_device_unregister(gadget_pdev);
2418 }
2419 break;
2420 default:
2421 break;
2422 }
2423
2424 return retval;
2425}
2426
2427struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
2428{
2429 struct device_node *node = pdev->dev.of_node;
2430 struct msm_otg_platform_data *pdata;
2431 int len = 0;
2432
2433 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
2434 if (!pdata) {
2435 pr_err("unable to allocate platform data\n");
2436 return NULL;
2437 }
2438 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
2439 if (len) {
2440 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
2441 if (!pdata->phy_init_seq)
2442 return NULL;
2443 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
2444 pdata->phy_init_seq,
2445 len/sizeof(*pdata->phy_init_seq));
2446 }
2447 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
2448 &pdata->power_budget);
2449 of_property_read_u32(node, "qcom,hsusb-otg-mode",
2450 &pdata->mode);
2451 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
2452 &pdata->otg_control);
2453 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
2454 &pdata->default_mode);
2455 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
2456 &pdata->phy_type);
2457 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
2458 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302459 return pdata;
2460}
2461
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302462static int __init msm_otg_probe(struct platform_device *pdev)
2463{
2464 int ret = 0;
2465 struct resource *res;
2466 struct msm_otg *motg;
2467 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302468 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302469
2470 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302471
2472 if (pdev->dev.of_node) {
2473 dev_dbg(&pdev->dev, "device tree enabled\n");
2474 pdata = msm_otg_dt_to_pdata(pdev);
2475 if (!pdata)
2476 return -ENOMEM;
2477 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
2478 if (ret) {
2479 dev_err(&pdev->dev, "devices setup failed\n");
2480 return ret;
2481 }
2482 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302483 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
2484 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302485 } else {
2486 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302487 }
2488
2489 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
2490 if (!motg) {
2491 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
2492 return -ENOMEM;
2493 }
2494
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002495 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302496 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302497 otg = &motg->otg;
2498 otg->dev = &pdev->dev;
2499
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302500 /*
2501 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
2502 * PHY treat ACA ID_GND as float and no interrupt is generated. But
2503 * PMIC can detect ACA ID_GND and generate an interrupt.
2504 */
2505 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
2506 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
2507 ret = -EINVAL;
2508 goto free_motg;
2509 }
2510
Ofir Cohen4da266f2012-01-03 10:19:29 +02002511 /* initialize reset counter */
2512 motg->reset_counter = 0;
2513
Amit Blay02eff132011-09-21 16:46:24 +03002514 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08002515 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03002516 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08002517 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302518
Manu Gautam5143b252012-01-05 19:25:23 -08002519 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302520 if (IS_ERR(motg->clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08002521 dev_err(&pdev->dev, "failed to get alt_core_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302522 ret = PTR_ERR(motg->clk);
2523 goto put_phy_reset_clk;
2524 }
Anji jonnala0f73cac2011-05-04 10:19:46 +05302525 clk_set_rate(motg->clk, 60000000);
2526
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302527 /* pm qos request to prevent apps idle power collapse */
2528 if (motg->pdata->swfi_latency)
2529 pm_qos_add_request(&motg->pm_qos_req_dma,
2530 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08002531
Anji jonnala0f73cac2011-05-04 10:19:46 +05302532 /*
Manu Gautam5143b252012-01-05 19:25:23 -08002533 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05302534 * CORE CLK must be running at >55Mhz for correct HSUSB
2535 * operation and USB core cannot tolerate frequency changes on
2536 * CORE CLK. For such USB cores, vote for maximum clk frequency
2537 * on pclk source
2538 */
Manu Gautam5143b252012-01-05 19:25:23 -08002539 motg->core_clk = clk_get(&pdev->dev, "core_clk");
2540 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302541 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08002542 dev_err(&pdev->dev, "failed to get core_clk\n");
2543 ret = PTR_ERR(motg->clk);
2544 goto put_clk;
2545 }
2546 clk_set_rate(motg->core_clk, INT_MAX);
2547
2548 motg->pclk = clk_get(&pdev->dev, "iface_clk");
2549 if (IS_ERR(motg->pclk)) {
2550 dev_err(&pdev->dev, "failed to get iface_clk\n");
2551 ret = PTR_ERR(motg->pclk);
2552 goto put_core_clk;
2553 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302554
2555 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2556 if (!res) {
2557 dev_err(&pdev->dev, "failed to get platform resource mem\n");
2558 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08002559 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302560 }
2561
2562 motg->regs = ioremap(res->start, resource_size(res));
2563 if (!motg->regs) {
2564 dev_err(&pdev->dev, "ioremap failed\n");
2565 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08002566 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302567 }
2568 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
2569
2570 motg->irq = platform_get_irq(pdev, 0);
2571 if (!motg->irq) {
2572 dev_err(&pdev->dev, "platform_get_irq failed\n");
2573 ret = -ENODEV;
2574 goto free_regs;
2575 }
2576
Stephen Boyd7dd22662012-01-26 16:09:31 -08002577 motg->xo_handle = clk_get(&pdev->dev, "xo");
Anji jonnala7da3f262011-12-02 17:22:14 -08002578 if (IS_ERR(motg->xo_handle)) {
2579 dev_err(&pdev->dev, "%s not able to get the handle "
2580 "to vote for TCXO D0 buffer\n", __func__);
2581 ret = PTR_ERR(motg->xo_handle);
2582 goto free_regs;
2583 }
2584
Stephen Boyd7dd22662012-01-26 16:09:31 -08002585 ret = clk_prepare_enable(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08002586 if (ret) {
2587 dev_err(&pdev->dev, "%s failed to vote for TCXO "
2588 "D0 buffer%d\n", __func__, ret);
2589 goto free_xo_handle;
2590 }
2591
Manu Gautam28b1bac2012-01-30 16:43:06 +05302592 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302593
2594 ret = msm_hsusb_init_vddcx(motg, 1);
2595 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002596 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08002597 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302598 }
2599
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002600 ret = msm_hsusb_config_vddcx(1);
2601 if (ret) {
2602 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
2603 goto free_init_vddcx;
2604 }
2605
Anji jonnala11aa5c42011-05-04 10:19:48 +05302606 ret = msm_hsusb_ldo_init(motg, 1);
2607 if (ret) {
2608 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002609 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302610 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002611
2612 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302613 if (ret) {
2614 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002615 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05302616 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05302617 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302618
2619 writel(0, USB_USBINTR);
2620 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002621 /* Ensure that above STOREs are completed before enabling interrupts */
2622 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302623
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002624 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302625 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302626 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302627 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
2628 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302629 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
2630 "msm_otg", motg);
2631 if (ret) {
2632 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002633 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302634 }
2635
2636 otg->init = msm_otg_reset;
2637 otg->set_host = msm_otg_set_host;
2638 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302639 otg->set_power = msm_otg_set_power;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302640 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302641
2642 otg->io_ops = &msm_otg_io_ops;
2643
2644 ret = otg_set_transceiver(&motg->otg);
2645 if (ret) {
2646 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
2647 goto free_irq;
2648 }
2649
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302650 if (motg->pdata->mode == USB_OTG &&
2651 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002652 if (motg->pdata->pmic_id_irq) {
2653 ret = request_irq(motg->pdata->pmic_id_irq,
2654 msm_pmic_id_irq,
2655 IRQF_TRIGGER_RISING |
2656 IRQF_TRIGGER_FALLING,
2657 "msm_otg", motg);
2658 if (ret) {
2659 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
2660 goto remove_otg;
2661 }
2662 } else {
2663 ret = -ENODEV;
2664 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
2665 goto remove_otg;
2666 }
2667 }
2668
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302669 msm_hsusb_mhl_switch_enable(motg, 1);
2670
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302671 platform_set_drvdata(pdev, motg);
2672 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002673 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302674
Anji jonnalad270e2d2011-08-09 11:28:32 +05302675 ret = msm_otg_debugfs_init(motg);
2676 if (ret)
2677 dev_dbg(&pdev->dev, "mode debugfs file is"
2678 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302679
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002680 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2681 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
2682
Amit Blay58b31472011-11-18 09:39:39 +02002683 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
2684 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302685 (!(motg->pdata->mode == USB_OTG) ||
2686 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02002687 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002688 ALLOW_PHY_RETENTION |
2689 ALLOW_PHY_COMP_DISABLE;
2690
Amit Blay58b31472011-11-18 09:39:39 +02002691 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
2692 motg->caps = ALLOW_PHY_RETENTION;
2693 }
2694
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002695 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302696 pm_runtime_set_active(&pdev->dev);
2697 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302698
Manu Gautamcd82e9d2011-12-20 14:17:28 +05302699 if (motg->pdata->bus_scale_table) {
2700 motg->bus_perf_client =
2701 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
2702 if (!motg->bus_perf_client)
2703 dev_err(motg->otg.dev, "%s: Failed to register BUS "
2704 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05302705 else
2706 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05302707 }
2708
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302709 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002710
2711remove_otg:
2712 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302713free_irq:
2714 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002715destroy_wlock:
2716 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05302717 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002718 msm_hsusb_ldo_enable(motg, 0);
2719free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302720 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002721free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05302722 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08002723devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05302724 clk_disable_unprepare(motg->pclk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08002725 clk_disable_unprepare(motg->xo_handle);
Anji jonnala7da3f262011-12-02 17:22:14 -08002726free_xo_handle:
Stephen Boyd7dd22662012-01-26 16:09:31 -08002727 clk_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302728free_regs:
2729 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08002730put_pclk:
2731 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302732put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08002733 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302734put_clk:
2735 clk_put(motg->clk);
2736put_phy_reset_clk:
Amit Blay02eff132011-09-21 16:46:24 +03002737 if (!IS_ERR(motg->phy_reset_clk))
2738 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302739free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302740 if (motg->pdata->swfi_latency)
2741 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302742 kfree(motg);
2743 return ret;
2744}
2745
2746static int __devexit msm_otg_remove(struct platform_device *pdev)
2747{
2748 struct msm_otg *motg = platform_get_drvdata(pdev);
2749 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302750 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302751
2752 if (otg->host || otg->gadget)
2753 return -EBUSY;
2754
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302755 if (pdev->dev.of_node)
2756 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002757 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
2758 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302759 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302760 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302761 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302762
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302763 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302764
2765 device_init_wakeup(&pdev->dev, 0);
2766 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302768
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05302769 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002770 if (motg->pdata->pmic_id_irq)
2771 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302772 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302773 free_irq(motg->irq, motg);
2774
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302775 /*
2776 * Put PHY in low power mode.
2777 */
2778 ulpi_read(otg, 0x14);
2779 ulpi_write(otg, 0x08, 0x09);
2780
2781 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
2782 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
2783 if (readl(USB_PORTSC) & PORTSC_PHCD)
2784 break;
2785 udelay(1);
2786 cnt++;
2787 }
2788 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
2789 dev_err(otg->dev, "Unable to suspend PHY\n");
2790
Manu Gautam28b1bac2012-01-30 16:43:06 +05302791 clk_disable_unprepare(motg->pclk);
2792 clk_disable_unprepare(motg->core_clk);
Stephen Boyd7dd22662012-01-26 16:09:31 -08002793 clk_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002794 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05302795 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002796 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302797
2798 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302799 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302800
Amit Blay02eff132011-09-21 16:46:24 +03002801 if (!IS_ERR(motg->phy_reset_clk))
2802 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302803 clk_put(motg->pclk);
2804 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08002805 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302806
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302807 if (motg->pdata->swfi_latency)
2808 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302809
Manu Gautamcd82e9d2011-12-20 14:17:28 +05302810 if (motg->bus_perf_client)
2811 msm_bus_scale_unregister_client(motg->bus_perf_client);
2812
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05302813 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302814 return 0;
2815}
2816
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302817#ifdef CONFIG_PM_RUNTIME
2818static int msm_otg_runtime_idle(struct device *dev)
2819{
2820 struct msm_otg *motg = dev_get_drvdata(dev);
2821 struct otg_transceiver *otg = &motg->otg;
2822
2823 dev_dbg(dev, "OTG runtime idle\n");
2824
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302825 if (otg->state == OTG_STATE_UNDEFINED)
2826 return -EAGAIN;
2827 else
2828 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302829}
2830
2831static int msm_otg_runtime_suspend(struct device *dev)
2832{
2833 struct msm_otg *motg = dev_get_drvdata(dev);
2834
2835 dev_dbg(dev, "OTG runtime suspend\n");
2836 return msm_otg_suspend(motg);
2837}
2838
2839static int msm_otg_runtime_resume(struct device *dev)
2840{
2841 struct msm_otg *motg = dev_get_drvdata(dev);
2842
2843 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302844 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302845 return msm_otg_resume(motg);
2846}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302847#endif
2848
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302849#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302850static int msm_otg_pm_suspend(struct device *dev)
2851{
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302852 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302853
2854 dev_dbg(dev, "OTG PM suspend\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302855
2856#ifdef CONFIG_PM_RUNTIME
2857 ret = pm_runtime_suspend(dev);
2858 if (ret > 0)
2859 ret = 0;
2860#else
2861 ret = msm_otg_suspend(dev_get_drvdata(dev));
2862#endif
2863 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302864}
2865
2866static int msm_otg_pm_resume(struct device *dev)
2867{
2868 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302869
2870 dev_dbg(dev, "OTG PM resume\n");
2871
Manu Gautamf284c052011-09-08 16:52:48 +05302872#ifdef CONFIG_PM_RUNTIME
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302873 /*
Manu Gautamf284c052011-09-08 16:52:48 +05302874 * Do not resume hardware as part of system resume,
2875 * rather, wait for the ASYNC INT from the h/w
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302876 */
Gregory Beanebd8ca22011-10-11 12:02:35 -07002877 return 0;
Manu Gautamf284c052011-09-08 16:52:48 +05302878#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302879
Manu Gautamf284c052011-09-08 16:52:48 +05302880 return msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302881}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302882#endif
2883
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302884#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302885static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302886 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
2887 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
2888 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302889};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302890#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302891
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302892static struct of_device_id msm_otg_dt_match[] = {
2893 { .compatible = "qcom,hsusb-otg",
2894 },
2895 {}
2896};
2897
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302898static struct platform_driver msm_otg_driver = {
2899 .remove = __devexit_p(msm_otg_remove),
2900 .driver = {
2901 .name = DRIVER_NAME,
2902 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302903#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302904 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05302905#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05302906 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302907 },
2908};
2909
2910static int __init msm_otg_init(void)
2911{
2912 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
2913}
2914
2915static void __exit msm_otg_exit(void)
2916{
2917 platform_driver_unregister(&msm_otg_driver);
2918}
2919
2920module_init(msm_otg_init);
2921module_exit(msm_otg_exit);
2922
2923MODULE_LICENSE("GPL v2");
2924MODULE_DESCRIPTION("MSM USB transceiver driver");