blob: 6b3400d44b72907a61c78230eb070306d59f436c [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>
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +053036#include <linux/usb/quirks.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053037#include <linux/usb/msm_hsusb.h>
38#include <linux/usb/msm_hsusb_hw.h>
Anji jonnala11aa5c42011-05-04 10:19:48 +053039#include <linux/regulator/consumer.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070040#include <linux/mfd/pm8xxx/pm8921-charger.h>
Pavankumar Kondeti446f4542012-02-01 13:57:13 +053041#include <linux/mfd/pm8xxx/misc.h>
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +053042#include <linux/pm_qos_params.h>
Amit Blay0f7edf72012-01-15 10:11:27 +020043#include <linux/power_supply.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080046#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053047#include <mach/msm_bus.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048
49#define MSM_USB_BASE (motg->regs)
50#define DRIVER_NAME "msm_otg"
51
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053052#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053054#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;
Mayank Rana9e9a2ac2012-03-24 04:05:28 +053095static struct regulator *mhl_analog_switch;
Anji jonnala11aa5c42011-05-04 10:19:48 +053096
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053097static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053098static inline bool aca_enabled(void)
99{
100#ifdef CONFIG_USB_MSM_ACA
101 return true;
102#else
103 return debug_aca_enabled;
104#endif
105}
106
Anji jonnala11aa5c42011-05-04 10:19:48 +0530107static int msm_hsusb_init_vddcx(struct msm_otg *motg, int init)
108{
109 int ret = 0;
110
111 if (init) {
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530112 hsusb_vddcx = devm_regulator_get(motg->otg.dev, "HSUSB_VDDCX");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530113 if (IS_ERR(hsusb_vddcx)) {
114 dev_err(motg->otg.dev, "unable to get hsusb vddcx\n");
115 return PTR_ERR(hsusb_vddcx);
116 }
117
118 ret = regulator_set_voltage(hsusb_vddcx,
119 USB_PHY_VDD_DIG_VOL_MIN,
120 USB_PHY_VDD_DIG_VOL_MAX);
121 if (ret) {
122 dev_err(motg->otg.dev, "unable to set the voltage "
123 "for hsusb vddcx\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 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);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 dev_err(motg->otg.dev, "unable to enable the hsusb vddcx\n");
132 return ret;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530133 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700136
Anji jonnala11aa5c42011-05-04 10:19:48 +0530137 ret = regulator_disable(hsusb_vddcx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700138 if (ret) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530139 dev_err(motg->otg.dev, "unable to disable hsusb vddcx\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140 return ret;
141 }
142
143 ret = regulator_set_voltage(hsusb_vddcx, 0,
144 USB_PHY_VDD_DIG_VOL_MIN);
145 if (ret) {
146 dev_err(motg->otg.dev, "unable to set the voltage"
147 "for hsusb vddcx\n");
148 return ret;
149 }
Anji jonnala11aa5c42011-05-04 10:19:48 +0530150 }
151
152 return ret;
153}
154
155static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
156{
157 int rc = 0;
158
159 if (init) {
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530160 hsusb_3p3 = devm_regulator_get(motg->otg.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530161 if (IS_ERR(hsusb_3p3)) {
162 dev_err(motg->otg.dev, "unable to get hsusb 3p3\n");
163 return PTR_ERR(hsusb_3p3);
164 }
165
166 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
167 USB_PHY_3P3_VOL_MAX);
168 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169 dev_err(motg->otg.dev, "unable to set voltage level for"
170 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530171 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530172 }
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530173 hsusb_1p8 = devm_regulator_get(motg->otg.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530174 if (IS_ERR(hsusb_1p8)) {
175 dev_err(motg->otg.dev, "unable to get hsusb 1p8\n");
176 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700177 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530178 }
179 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
180 USB_PHY_1P8_VOL_MAX);
181 if (rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 dev_err(motg->otg.dev, "unable to set voltage level for"
183 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530184 goto put_1p8;
185 }
186
187 return 0;
188 }
189
Anji jonnala11aa5c42011-05-04 10:19:48 +0530190put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192put_3p3_lpm:
193 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530194 return rc;
195}
196
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530197#ifdef CONFIG_PM_SLEEP
198#define USB_PHY_SUSP_DIG_VOL 500000
199static int msm_hsusb_config_vddcx(int high)
200{
201 int max_vol = USB_PHY_VDD_DIG_VOL_MAX;
202 int min_vol;
203 int ret;
204
205 if (high)
206 min_vol = USB_PHY_VDD_DIG_VOL_MIN;
207 else
208 min_vol = USB_PHY_SUSP_DIG_VOL;
209
210 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
211 if (ret) {
212 pr_err("%s: unable to set the voltage for regulator "
213 "HSUSB_VDDCX\n", __func__);
214 return ret;
215 }
216
217 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
218
219 return ret;
220}
Hemant Kumar8e7bd072011-08-01 14:14:24 -0700221#else
222static int msm_hsusb_config_vddcx(int high)
223{
224 return 0;
225}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530226#endif
227
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530229{
230 int ret = 0;
231
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530232 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530233 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
234 return -ENODEV;
235 }
236
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530237 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530238 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
239 return -ENODEV;
240 }
241
242 if (on) {
243 ret = regulator_set_optimum_mode(hsusb_1p8,
244 USB_PHY_1P8_HPM_LOAD);
245 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700246 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530247 "HSUSB_1p8\n", __func__);
248 return ret;
249 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700250
251 ret = regulator_enable(hsusb_1p8);
252 if (ret) {
253 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 1p8\n",
254 __func__);
255 regulator_set_optimum_mode(hsusb_1p8, 0);
256 return ret;
257 }
258
Anji jonnala11aa5c42011-05-04 10:19:48 +0530259 ret = regulator_set_optimum_mode(hsusb_3p3,
260 USB_PHY_3P3_HPM_LOAD);
261 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530263 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700264 regulator_set_optimum_mode(hsusb_1p8, 0);
265 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530266 return ret;
267 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700268
269 ret = regulator_enable(hsusb_3p3);
270 if (ret) {
271 dev_err(motg->otg.dev, "%s: unable to enable the hsusb 3p3\n",
272 __func__);
273 regulator_set_optimum_mode(hsusb_3p3, 0);
274 regulator_set_optimum_mode(hsusb_1p8, 0);
275 regulator_disable(hsusb_1p8);
276 return ret;
277 }
278
Anji jonnala11aa5c42011-05-04 10:19:48 +0530279 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 ret = regulator_disable(hsusb_1p8);
281 if (ret) {
282 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 1p8\n",
283 __func__);
284 return ret;
285 }
286
287 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530288 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700289 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530290 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291
292 ret = regulator_disable(hsusb_3p3);
293 if (ret) {
294 dev_err(motg->otg.dev, "%s: unable to disable the hsusb 3p3\n",
295 __func__);
296 return ret;
297 }
298 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530299 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530301 "HSUSB_3p3\n", __func__);
302 }
303
304 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
305 return ret < 0 ? ret : 0;
306}
307
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530308static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
309{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530310 struct msm_otg_platform_data *pdata = motg->pdata;
311
312 if (!pdata->mhl_enable)
313 return;
314
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530315 if (!mhl_analog_switch) {
316 pr_err("%s: mhl_analog_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530317 return;
318 }
319
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530320 if (on) {
321 if (regulator_enable(mhl_analog_switch))
322 pr_err("unable to enable mhl_analog_switch\n");
323 } else {
324 regulator_disable(mhl_analog_switch);
325 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530326}
327
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530328static int ulpi_read(struct otg_transceiver *otg, u32 reg)
329{
330 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
331 int cnt = 0;
332
333 /* initiate read operation */
334 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
335 USB_ULPI_VIEWPORT);
336
337 /* wait for completion */
338 while (cnt < ULPI_IO_TIMEOUT_USEC) {
339 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
340 break;
341 udelay(1);
342 cnt++;
343 }
344
345 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
346 dev_err(otg->dev, "ulpi_read: timeout %08x\n",
347 readl(USB_ULPI_VIEWPORT));
348 return -ETIMEDOUT;
349 }
350 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
351}
352
353static int ulpi_write(struct otg_transceiver *otg, u32 val, u32 reg)
354{
355 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
356 int cnt = 0;
357
358 /* initiate write operation */
359 writel(ULPI_RUN | ULPI_WRITE |
360 ULPI_ADDR(reg) | ULPI_DATA(val),
361 USB_ULPI_VIEWPORT);
362
363 /* wait for completion */
364 while (cnt < ULPI_IO_TIMEOUT_USEC) {
365 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
366 break;
367 udelay(1);
368 cnt++;
369 }
370
371 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
372 dev_err(otg->dev, "ulpi_write: timeout\n");
373 return -ETIMEDOUT;
374 }
375 return 0;
376}
377
378static struct otg_io_access_ops msm_otg_io_ops = {
379 .read = ulpi_read,
380 .write = ulpi_write,
381};
382
383static void ulpi_init(struct msm_otg *motg)
384{
385 struct msm_otg_platform_data *pdata = motg->pdata;
386 int *seq = pdata->phy_init_seq;
387
388 if (!seq)
389 return;
390
391 while (seq[0] >= 0) {
392 dev_vdbg(motg->otg.dev, "ulpi: write 0x%02x to 0x%02x\n",
393 seq[0], seq[1]);
394 ulpi_write(&motg->otg, seq[0], seq[1]);
395 seq += 2;
396 }
397}
398
399static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
400{
401 int ret;
402
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530403 if (IS_ERR(motg->clk))
404 return 0;
405
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530406 if (assert) {
407 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
408 if (ret)
409 dev_err(motg->otg.dev, "usb hs_clk assert failed\n");
410 } else {
411 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
412 if (ret)
413 dev_err(motg->otg.dev, "usb hs_clk deassert failed\n");
414 }
415 return ret;
416}
417
418static int msm_otg_phy_clk_reset(struct msm_otg *motg)
419{
420 int ret;
421
Amit Blay02eff132011-09-21 16:46:24 +0300422 if (IS_ERR(motg->phy_reset_clk))
423 return 0;
424
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530425 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
426 if (ret) {
427 dev_err(motg->otg.dev, "usb phy clk assert failed\n");
428 return ret;
429 }
430 usleep_range(10000, 12000);
431 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
432 if (ret)
433 dev_err(motg->otg.dev, "usb phy clk deassert failed\n");
434 return ret;
435}
436
437static int msm_otg_phy_reset(struct msm_otg *motg)
438{
439 u32 val;
440 int ret;
441 int retries;
442
443 ret = msm_otg_link_clk_reset(motg, 1);
444 if (ret)
445 return ret;
446 ret = msm_otg_phy_clk_reset(motg);
447 if (ret)
448 return ret;
449 ret = msm_otg_link_clk_reset(motg, 0);
450 if (ret)
451 return ret;
452
453 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
454 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
455
456 for (retries = 3; retries > 0; retries--) {
457 ret = ulpi_write(&motg->otg, ULPI_FUNC_CTRL_SUSPENDM,
458 ULPI_CLR(ULPI_FUNC_CTRL));
459 if (!ret)
460 break;
461 ret = msm_otg_phy_clk_reset(motg);
462 if (ret)
463 return ret;
464 }
465 if (!retries)
466 return -ETIMEDOUT;
467
468 /* This reset calibrates the phy, if the above write succeeded */
469 ret = msm_otg_phy_clk_reset(motg);
470 if (ret)
471 return ret;
472
473 for (retries = 3; retries > 0; retries--) {
474 ret = ulpi_read(&motg->otg, ULPI_DEBUG);
475 if (ret != -ETIMEDOUT)
476 break;
477 ret = msm_otg_phy_clk_reset(motg);
478 if (ret)
479 return ret;
480 }
481 if (!retries)
482 return -ETIMEDOUT;
483
484 dev_info(motg->otg.dev, "phy_reset: success\n");
485 return 0;
486}
487
488#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530489static int msm_otg_link_reset(struct msm_otg *motg)
490{
491 int cnt = 0;
492
493 writel_relaxed(USBCMD_RESET, USB_USBCMD);
494 while (cnt < LINK_RESET_TIMEOUT_USEC) {
495 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
496 break;
497 udelay(1);
498 cnt++;
499 }
500 if (cnt >= LINK_RESET_TIMEOUT_USEC)
501 return -ETIMEDOUT;
502
503 /* select ULPI phy */
504 writel_relaxed(0x80000000, USB_PORTSC);
505 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530506 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530507
508 return 0;
509}
510
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511static int msm_otg_reset(struct otg_transceiver *otg)
512{
513 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
514 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530515 int ret;
516 u32 val = 0;
517 u32 ulpi_val = 0;
518
Ofir Cohen4da266f2012-01-03 10:19:29 +0200519 /*
520 * USB PHY and Link reset also reset the USB BAM.
521 * Thus perform reset operation only once to avoid
522 * USB BAM reset on other cases e.g. USB cable disconnections.
523 */
524 if (pdata->disable_reset_on_disconnect) {
525 if (motg->reset_counter)
526 return 0;
527 else
528 motg->reset_counter++;
529 }
530
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530531 if (!IS_ERR(motg->clk))
532 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530533 ret = msm_otg_phy_reset(motg);
534 if (ret) {
535 dev_err(otg->dev, "phy_reset failed\n");
536 return ret;
537 }
538
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530539 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530540 ret = msm_otg_link_reset(motg);
541 if (ret) {
542 dev_err(otg->dev, "link reset failed\n");
543 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530544 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530545 msleep(100);
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530546
547 ulpi_init(motg);
548
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 /* Ensure that RESET operation is completed before turning off clock */
550 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530551
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530552 if (!IS_ERR(motg->clk))
553 clk_disable_unprepare(motg->clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530555 if (pdata->otg_control == OTG_PHY_CONTROL) {
556 val = readl_relaxed(USB_OTGSC);
557 if (pdata->mode == USB_OTG) {
558 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
559 val |= OTGSC_IDIE | OTGSC_BSVIE;
560 } else if (pdata->mode == USB_PERIPHERAL) {
561 ulpi_val = ULPI_INT_SESS_VALID;
562 val |= OTGSC_BSVIE;
563 }
564 writel_relaxed(val, USB_OTGSC);
565 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_RISE);
566 ulpi_write(otg, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530567 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
568 /* Enable PMIC pull-up */
569 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530570 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530572 return 0;
573}
574
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530575static const char *timer_string(int bit)
576{
577 switch (bit) {
578 case A_WAIT_VRISE: return "a_wait_vrise";
579 case A_WAIT_VFALL: return "a_wait_vfall";
580 case B_SRP_FAIL: return "b_srp_fail";
581 case A_WAIT_BCON: return "a_wait_bcon";
582 case A_AIDL_BDIS: return "a_aidl_bdis";
583 case A_BIDL_ADIS: return "a_bidl_adis";
584 case B_ASE0_BRST: return "b_ase0_brst";
585 case A_TST_MAINT: return "a_tst_maint";
586 case B_TST_SRP: return "b_tst_srp";
587 case B_TST_CONFIG: return "b_tst_config";
588 default: return "UNDEFINED";
589 }
590}
591
592static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
593{
594 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
595
596 switch (motg->active_tmout) {
597 case A_WAIT_VRISE:
598 /* TODO: use vbus_vld interrupt */
599 set_bit(A_VBUS_VLD, &motg->inputs);
600 break;
601 case A_TST_MAINT:
602 /* OTG PET: End session after TA_TST_MAINT */
603 set_bit(A_BUS_DROP, &motg->inputs);
604 break;
605 case B_TST_SRP:
606 /*
607 * OTG PET: Initiate SRP after TB_TST_SRP of
608 * previous session end.
609 */
610 set_bit(B_BUS_REQ, &motg->inputs);
611 break;
612 case B_TST_CONFIG:
613 clear_bit(A_CONN, &motg->inputs);
614 break;
615 default:
616 set_bit(motg->active_tmout, &motg->tmouts);
617 }
618
619 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
620 queue_work(system_nrt_wq, &motg->sm_work);
621 return HRTIMER_NORESTART;
622}
623
624static void msm_otg_del_timer(struct msm_otg *motg)
625{
626 int bit = motg->active_tmout;
627
628 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
629 div_s64(ktime_to_us(hrtimer_get_remaining(
630 &motg->timer)), 1000));
631 hrtimer_cancel(&motg->timer);
632 clear_bit(bit, &motg->tmouts);
633}
634
635static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
636{
637 clear_bit(bit, &motg->tmouts);
638 motg->active_tmout = bit;
639 pr_debug("starting %s timer\n", timer_string(bit));
640 hrtimer_start(&motg->timer,
641 ktime_set(time / 1000, (time % 1000) * 1000000),
642 HRTIMER_MODE_REL);
643}
644
645static void msm_otg_init_timer(struct msm_otg *motg)
646{
647 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
648 motg->timer.function = msm_otg_timer_func;
649}
650
651static int msm_otg_start_hnp(struct otg_transceiver *otg)
652{
653 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
654
655 if (otg->state != OTG_STATE_A_HOST) {
656 pr_err("HNP can not be initiated in %s state\n",
657 otg_state_string(otg->state));
658 return -EINVAL;
659 }
660
661 pr_debug("A-Host: HNP initiated\n");
662 clear_bit(A_BUS_REQ, &motg->inputs);
663 queue_work(system_nrt_wq, &motg->sm_work);
664 return 0;
665}
666
667static int msm_otg_start_srp(struct otg_transceiver *otg)
668{
669 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
670 u32 val;
671 int ret = 0;
672
673 if (otg->state != OTG_STATE_B_IDLE) {
674 pr_err("SRP can not be initiated in %s state\n",
675 otg_state_string(otg->state));
676 ret = -EINVAL;
677 goto out;
678 }
679
680 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
681 pr_debug("initial conditions of SRP are not met. Try again"
682 "after some time\n");
683 ret = -EAGAIN;
684 goto out;
685 }
686
687 pr_debug("B-Device SRP started\n");
688
689 /*
690 * PHY won't pull D+ high unless it detects Vbus valid.
691 * Since by definition, SRP is only done when Vbus is not valid,
692 * software work-around needs to be used to spoof the PHY into
693 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
694 * VBUSVLDEXT register bits.
695 */
696 ulpi_write(otg, 0x03, 0x97);
697 /*
698 * Harware auto assist data pulsing: Data pulse is given
699 * for 7msec; wait for vbus
700 */
701 val = readl_relaxed(USB_OTGSC);
702 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
703
704 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
705out:
706 return ret;
707}
708
709static void msm_otg_host_hnp_enable(struct otg_transceiver *otg, bool enable)
710{
711 struct usb_hcd *hcd = bus_to_hcd(otg->host);
712 struct usb_device *rhub = otg->host->root_hub;
713
714 if (enable) {
715 pm_runtime_disable(&rhub->dev);
716 rhub->state = USB_STATE_NOTATTACHED;
717 hcd->driver->bus_suspend(hcd);
718 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
719 } else {
720 usb_remove_hcd(hcd);
721 msm_otg_reset(otg);
722 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
723 }
724}
725
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530726static int msm_otg_set_suspend(struct otg_transceiver *otg, int suspend)
727{
728 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
729
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530730 if (aca_enabled() || (test_bit(ID, &motg->inputs) &&
731 !test_bit(ID_A, &motg->inputs)))
732 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530733
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530734 if (suspend) {
735 switch (otg->state) {
736 case OTG_STATE_A_WAIT_BCON:
737 if (TA_WAIT_BCON > 0)
738 break;
739 /* fall through */
740 case OTG_STATE_A_HOST:
741 pr_debug("host bus suspend\n");
742 clear_bit(A_BUS_REQ, &motg->inputs);
743 queue_work(system_nrt_wq, &motg->sm_work);
744 break;
745 default:
746 break;
747 }
748 } else {
749 switch (otg->state) {
750 case OTG_STATE_A_SUSPEND:
751 /* Remote wakeup or resume */
752 set_bit(A_BUS_REQ, &motg->inputs);
753 otg->state = OTG_STATE_A_HOST;
754 break;
755 default:
756 break;
757 }
758 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530759 return 0;
760}
761
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530762#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530763#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
764
765#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530766static int msm_otg_suspend(struct msm_otg *motg)
767{
768 struct otg_transceiver *otg = &motg->otg;
769 struct usb_bus *bus = otg->host;
770 struct msm_otg_platform_data *pdata = motg->pdata;
771 int cnt = 0;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530772 bool host_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530773 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800774 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530775 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530776
777 if (atomic_read(&motg->in_lpm))
778 return 0;
779
780 disable_irq(motg->irq);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530781 host_bus_suspend = otg->host && !test_bit(ID, &motg->inputs);
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530782 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530783 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530784 * Chipidea 45-nm PHY suspend sequence:
785 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530786 * Interrupt Latch Register auto-clear feature is not present
787 * in all PHY versions. Latch register is clear on read type.
788 * Clear latch register to avoid spurious wakeup from
789 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530790 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530791 * PHY comparators are disabled when PHY enters into low power
792 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
793 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
794 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530795 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530796 * PLL is not turned off when PHY enters into low power mode (LPM).
797 * Disable PLL for maximum power savings.
798 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530799
800 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
801 ulpi_read(otg, 0x14);
802 if (pdata->otg_control == OTG_PHY_CONTROL)
803 ulpi_write(otg, 0x01, 0x30);
804 ulpi_write(otg, 0x08, 0x09);
805 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530806
807 /*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700808 * Turn off the OTG comparators, if depends on PMIC for
809 * VBUS and ID notifications.
810 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530811 if ((motg->caps & ALLOW_PHY_COMP_DISABLE) && !host_bus_suspend) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700812 ulpi_write(otg, OTG_COMP_DISABLE,
813 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
814 motg->lpm_flags |= PHY_OTG_COMP_DISABLED;
815 }
816
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530817 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530818 * PHY may take some time or even fail to enter into low power
819 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
820 * in failure case.
821 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530822 portsc = readl_relaxed(USB_PORTSC);
823 if (!(portsc & PORTSC_PHCD)) {
824 writel_relaxed(portsc | PORTSC_PHCD,
825 USB_PORTSC);
826 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
827 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
828 break;
829 udelay(1);
830 cnt++;
831 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530832 }
833
834 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
835 dev_err(otg->dev, "Unable to suspend PHY\n");
836 msm_otg_reset(otg);
837 enable_irq(motg->irq);
838 return -ETIMEDOUT;
839 }
840
841 /*
842 * PHY has capability to generate interrupt asynchronously in low
843 * power mode (LPM). This interrupt is level triggered. So USB IRQ
844 * line must be disabled till async interrupt enable bit is cleared
845 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
846 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530847 *
848 * PHY retention mode is disallowed while entering to LPM with wall
849 * charger connected. But PHY is put into suspend mode. Hence
850 * enable asynchronous interrupt to detect charger disconnection when
851 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530852 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530853 cmd_val = readl_relaxed(USB_USBCMD);
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530854 if (host_bus_suspend || (motg->pdata->otg_control == OTG_PHY_CONTROL &&
855 dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530856 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
857 else
858 cmd_val |= ULPI_STP_CTRL;
859 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530860
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530861 /*
862 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
863 * PHY retention and collapse can not happen with VDP_SRC enabled.
864 */
865 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200866 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
867 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
868 /* Enable PHY HV interrupts to wake MPM/Link */
869 phy_ctrl_val |=
870 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
871
872 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700873 motg->lpm_flags |= PHY_RETENTIONED;
874 }
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530875
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 /* Ensure that above operation is completed before turning off clocks */
877 mb();
Manu Gautam28b1bac2012-01-30 16:43:06 +0530878 clk_disable_unprepare(motg->pclk);
879 clk_disable_unprepare(motg->core_clk);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530880
Anji jonnala7da3f262011-12-02 17:22:14 -0800881 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800882 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
883 if (ret)
884 dev_err(otg->dev, "%s failed to devote for "
885 "TCXO D0 buffer%d\n", __func__, ret);
Anji jonnala7da3f262011-12-02 17:22:14 -0800886
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530887 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
888 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 msm_hsusb_ldo_enable(motg, 0);
890 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530891 }
892
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530893 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700894 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530895 msm_hsusb_mhl_switch_enable(motg, 0);
896 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897
898 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530899 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700900 if (motg->pdata->pmic_id_irq)
901 enable_irq_wake(motg->pdata->pmic_id_irq);
902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903 if (bus)
904 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
905
906 atomic_set(&motg->in_lpm, 1);
907 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909
910 dev_info(otg->dev, "USB in low power mode\n");
911
912 return 0;
913}
914
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530915static int msm_otg_resume(struct msm_otg *motg)
916{
917 struct otg_transceiver *otg = &motg->otg;
918 struct usb_bus *bus = otg->host;
919 int cnt = 0;
920 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200921 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800922 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530923
924 if (!atomic_read(&motg->in_lpm))
925 return 0;
926
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 wake_lock(&motg->wlock);
Anji jonnala7da3f262011-12-02 17:22:14 -0800928
929 /* Vote for TCXO when waking up the phy */
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800930 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -0800931 if (ret)
932 dev_err(otg->dev, "%s failed to vote for "
933 "TCXO D0 buffer%d\n", __func__, ret);
934
Manu Gautam28b1bac2012-01-30 16:43:06 +0530935 clk_prepare_enable(motg->core_clk);
Amit Blay137575f2011-11-06 15:20:54 +0200936
Manu Gautam28b1bac2012-01-30 16:43:06 +0530937 clk_prepare_enable(motg->pclk);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530938
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
940 msm_hsusb_ldo_enable(motg, 1);
941 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
942 }
943
944 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530945 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530946 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200947 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
948 phy_ctrl_val |= PHY_RETEN;
949 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
950 /* Disable PHY HV interrupts */
951 phy_ctrl_val &=
952 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
953 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700954 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530955 }
956
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530957 temp = readl(USB_USBCMD);
958 temp &= ~ASYNC_INTR_CTRL;
959 temp &= ~ULPI_STP_CTRL;
960 writel(temp, USB_USBCMD);
961
962 /*
963 * PHY comes out of low power mode (LPM) in case of wakeup
964 * from asynchronous interrupt.
965 */
966 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
967 goto skip_phy_resume;
968
969 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
970 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
971 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
972 break;
973 udelay(1);
974 cnt++;
975 }
976
977 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
978 /*
979 * This is a fatal error. Reset the link and
980 * PHY. USB state can not be restored. Re-insertion
981 * of USB cable is the only way to get USB working.
982 */
983 dev_err(otg->dev, "Unable to resume USB."
984 "Re-plugin the cable\n");
985 msm_otg_reset(otg);
986 }
987
988skip_phy_resume:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700989 /* Turn on the OTG comparators on resume */
990 if (motg->lpm_flags & PHY_OTG_COMP_DISABLED) {
991 ulpi_write(otg, OTG_COMP_DISABLE,
992 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
993 motg->lpm_flags &= ~PHY_OTG_COMP_DISABLED;
994 }
995 if (device_may_wakeup(otg->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530996 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 if (motg->pdata->pmic_id_irq)
998 disable_irq_wake(motg->pdata->pmic_id_irq);
999 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301000 if (bus)
1001 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1002
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301003 atomic_set(&motg->in_lpm, 0);
1004
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301005 if (motg->async_int) {
1006 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301007 enable_irq(motg->irq);
1008 }
1009
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301010 dev_info(otg->dev, "USB exited from low power mode\n");
1011
1012 return 0;
1013}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301014#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301015
David Keitel081a3e22012-04-18 12:37:07 -07001016static int msm_otg_notify_chg_type(struct msm_otg *motg)
1017{
1018 static int charger_type;
1019 /*
1020 * TODO
1021 * Unify OTG driver charger types and power supply charger types
1022 */
1023 if (charger_type == motg->chg_type)
1024 return 0;
1025
1026 if (motg->chg_type == USB_SDP_CHARGER)
1027 charger_type = POWER_SUPPLY_TYPE_USB;
1028 else if (motg->chg_type == USB_CDP_CHARGER)
1029 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
1030 else if (motg->chg_type == USB_DCP_CHARGER)
1031 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1032 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1033 motg->chg_type == USB_ACA_A_CHARGER ||
1034 motg->chg_type == USB_ACA_B_CHARGER ||
1035 motg->chg_type == USB_ACA_C_CHARGER))
1036 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1037 else
1038 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1039
1040 return pm8921_set_usb_power_supply_type(charger_type);
1041}
1042
Amit Blay0f7edf72012-01-15 10:11:27 +02001043static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1044{
1045 struct power_supply *psy;
1046
1047 psy = power_supply_get_by_name("usb");
1048 if (!psy)
1049 goto psy_not_supported;
1050
1051 if (motg->cur_power == 0 && mA > 0) {
1052 /* Enable charging */
1053 if (power_supply_set_online(psy, true))
1054 goto psy_not_supported;
1055 } else if (motg->cur_power > 0 && mA == 0) {
1056 /* Disable charging */
1057 if (power_supply_set_online(psy, false))
1058 goto psy_not_supported;
1059 return 0;
1060 }
1061 /* Set max current limit */
1062 if (power_supply_set_current_limit(psy, 1000*mA))
1063 goto psy_not_supported;
1064
1065 return 0;
1066
1067psy_not_supported:
1068 dev_dbg(motg->otg.dev, "Power Supply doesn't support USB charger\n");
1069 return -ENXIO;
1070}
1071
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301072static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1073{
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301074 struct usb_gadget *g = motg->otg.gadget;
1075
1076 if (g && g->is_a_peripheral)
1077 return;
1078
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301079 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1080 motg->chg_type == USB_ACA_A_CHARGER ||
1081 motg->chg_type == USB_ACA_B_CHARGER ||
1082 motg->chg_type == USB_ACA_C_CHARGER) &&
1083 mA > IDEV_ACA_CHG_LIMIT)
1084 mA = IDEV_ACA_CHG_LIMIT;
1085
David Keitel081a3e22012-04-18 12:37:07 -07001086 if (msm_otg_notify_chg_type(motg))
1087 dev_err(motg->otg.dev,
1088 "Failed notifying %d charger type to PMIC\n",
1089 motg->chg_type);
1090
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301091 if (motg->cur_power == mA)
1092 return;
1093
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301094 dev_info(motg->otg.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001095
1096 /*
1097 * Use Power Supply API if supported, otherwise fallback
1098 * to legacy pm8921 API.
1099 */
1100 if (msm_otg_notify_power_supply(motg, mA))
1101 pm8921_charger_vbus_draw(mA);
1102
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301103 motg->cur_power = mA;
1104}
1105
1106static int msm_otg_set_power(struct otg_transceiver *otg, unsigned mA)
1107{
1108 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1109
1110 /*
1111 * Gadget driver uses set_power method to notify about the
1112 * available current based on suspend/configured states.
1113 *
1114 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1115 * states when CDP/ACA is connected.
1116 */
1117 if (motg->chg_type == USB_SDP_CHARGER)
1118 msm_otg_notify_charger(motg, mA);
1119
1120 return 0;
1121}
1122
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123static void msm_otg_start_host(struct otg_transceiver *otg, int on)
1124{
1125 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1126 struct msm_otg_platform_data *pdata = motg->pdata;
1127 struct usb_hcd *hcd;
1128
1129 if (!otg->host)
1130 return;
1131
1132 hcd = bus_to_hcd(otg->host);
1133
1134 if (on) {
1135 dev_dbg(otg->dev, "host on\n");
1136
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301137 /*
1138 * Some boards have a switch cotrolled by gpio
1139 * to enable/disable internal HUB. Enable internal
1140 * HUB before kicking the host.
1141 */
1142 if (pdata->setup_gpio)
1143 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301144 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145 } else {
1146 dev_dbg(otg->dev, "host off\n");
1147
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301148 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301149 /* HCD core reset all bits of PORTSC. select ULPI phy */
1150 writel_relaxed(0x80000000, USB_PORTSC);
1151
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301152 if (pdata->setup_gpio)
1153 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301154 }
1155}
1156
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157static int msm_otg_usbdev_notify(struct notifier_block *self,
1158 unsigned long action, void *priv)
1159{
1160 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301161 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301162 struct usb_device *udev = priv;
1163
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301164 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1165 goto out;
1166
1167 if (udev->bus != motg->otg.host)
1168 goto out;
1169 /*
1170 * Interested in devices connected directly to the root hub.
1171 * ACA dock can supply IDEV_CHG irrespective devices connected
1172 * on the accessory port.
1173 */
1174 if (!udev->parent || udev->parent->parent ||
1175 motg->chg_type == USB_ACA_DOCK_CHARGER)
1176 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177
1178 switch (action) {
1179 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301180 if (aca_enabled())
1181 usb_disable_autosuspend(udev);
1182 if (otg->state == OTG_STATE_A_WAIT_BCON) {
1183 pr_debug("B_CONN set\n");
1184 set_bit(B_CONN, &motg->inputs);
1185 msm_otg_del_timer(motg);
1186 otg->state = OTG_STATE_A_HOST;
1187 /*
1188 * OTG PET: A-device must end session within
1189 * 10 sec after PET enumeration.
1190 */
1191 if (udev->quirks & USB_QUIRK_OTG_PET)
1192 msm_otg_start_timer(motg, TA_TST_MAINT,
1193 A_TST_MAINT);
1194 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301195 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001196 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001197 if (udev->actconfig)
1198 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1199 else
1200 motg->mA_port = IUNIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301201 if (otg->state == OTG_STATE_B_HOST)
1202 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301203 break;
1204 case USB_DEVICE_REMOVE:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301205 if ((otg->state == OTG_STATE_A_HOST) ||
1206 (otg->state == OTG_STATE_A_SUSPEND)) {
1207 pr_debug("B_CONN clear\n");
1208 clear_bit(B_CONN, &motg->inputs);
1209 /*
1210 * OTG PET: A-device must end session after
1211 * PET disconnection if it is enumerated
1212 * with bcdDevice[0] = 1. USB core sets
1213 * bus->otg_vbus_off for us. clear it here.
1214 */
1215 if (udev->bus->otg_vbus_off) {
1216 udev->bus->otg_vbus_off = 0;
1217 set_bit(A_BUS_DROP, &motg->inputs);
1218 }
1219 queue_work(system_nrt_wq, &motg->sm_work);
1220 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001221 default:
1222 break;
1223 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301224 if (test_bit(ID_A, &motg->inputs))
1225 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1226 motg->mA_port);
1227out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001228 return NOTIFY_OK;
1229}
1230
Mayank Ranae3926882011-12-26 09:47:54 +05301231static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1232{
1233 int ret;
1234 static bool vbus_is_on;
1235
1236 if (vbus_is_on == on)
1237 return;
1238
1239 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301240 ret = motg->pdata->vbus_power(on);
1241 if (!ret)
1242 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301243 return;
1244 }
1245
1246 if (!vbus_otg) {
1247 pr_err("vbus_otg is NULL.");
1248 return;
1249 }
1250
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001251 /*
1252 * if entering host mode tell the charger to not draw any current
1253 * from usb - if exiting host mode let the charger draw current
1254 */
1255 pm8921_disable_source_current(on);
Mayank Ranae3926882011-12-26 09:47:54 +05301256 if (on) {
1257 ret = regulator_enable(vbus_otg);
1258 if (ret) {
1259 pr_err("unable to enable vbus_otg\n");
1260 return;
1261 }
1262 vbus_is_on = true;
1263 } else {
1264 ret = regulator_disable(vbus_otg);
1265 if (ret) {
1266 pr_err("unable to disable vbus_otg\n");
1267 return;
1268 }
1269 vbus_is_on = false;
1270 }
1271}
1272
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301273static int msm_otg_set_host(struct otg_transceiver *otg, struct usb_bus *host)
1274{
1275 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1276 struct usb_hcd *hcd;
1277
1278 /*
1279 * Fail host registration if this board can support
1280 * only peripheral configuration.
1281 */
1282 if (motg->pdata->mode == USB_PERIPHERAL) {
1283 dev_info(otg->dev, "Host mode is not supported\n");
1284 return -ENODEV;
1285 }
1286
Mayank Ranae3926882011-12-26 09:47:54 +05301287 if (!motg->pdata->vbus_power && host) {
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05301288 vbus_otg = devm_regulator_get(motg->otg.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301289 if (IS_ERR(vbus_otg)) {
1290 pr_err("Unable to get vbus_otg\n");
1291 return -ENODEV;
1292 }
1293 }
1294
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301295 if (!host) {
1296 if (otg->state == OTG_STATE_A_HOST) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301297 pm_runtime_get_sync(otg->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001298 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301299 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301300 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301301 otg->host = NULL;
1302 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301303 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301304 } else {
1305 otg->host = NULL;
1306 }
1307
1308 return 0;
1309 }
1310
1311 hcd = bus_to_hcd(host);
1312 hcd->power_budget = motg->pdata->power_budget;
1313
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301314#ifdef CONFIG_USB_OTG
1315 host->otg_port = 1;
1316#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001317 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1318 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301319 otg->host = host;
1320 dev_dbg(otg->dev, "host driver registered w/ tranceiver\n");
1321
1322 /*
1323 * Kick the state machine work, if peripheral is not supported
1324 * or peripheral is already registered with us.
1325 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301326 if (motg->pdata->mode == USB_HOST || otg->gadget) {
1327 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301328 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301329 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301330
1331 return 0;
1332}
1333
1334static void msm_otg_start_peripheral(struct otg_transceiver *otg, int on)
1335{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301336 int ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301337 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1338 struct msm_otg_platform_data *pdata = motg->pdata;
1339
1340 if (!otg->gadget)
1341 return;
1342
1343 if (on) {
1344 dev_dbg(otg->dev, "gadget on\n");
1345 /*
1346 * Some boards have a switch cotrolled by gpio
1347 * to enable/disable internal HUB. Disable internal
1348 * HUB before kicking the gadget.
1349 */
1350 if (pdata->setup_gpio)
1351 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301352 /*
1353 * vote for minimum dma_latency to prevent idle
1354 * power collapse(pc) while running in peripheral mode.
1355 */
1356 otg_pm_qos_update_latency(motg, 1);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301357 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301358 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301359 ret = msm_bus_scale_client_update_request(
1360 motg->bus_perf_client, 1);
1361 if (ret)
1362 dev_err(motg->otg.dev, "%s: Failed to vote for "
1363 "bus bandwidth %d\n", __func__, ret);
1364 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301365 usb_gadget_vbus_connect(otg->gadget);
1366 } else {
1367 dev_dbg(otg->dev, "gadget off\n");
1368 usb_gadget_vbus_disconnect(otg->gadget);
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05301369 otg_pm_qos_update_latency(motg, 0);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301370 /* Configure BUS performance parameters to default */
1371 if (motg->bus_perf_client) {
1372 ret = msm_bus_scale_client_update_request(
1373 motg->bus_perf_client, 0);
1374 if (ret)
1375 dev_err(motg->otg.dev, "%s: Failed to devote "
1376 "for bus bw %d\n", __func__, ret);
1377 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301378 if (pdata->setup_gpio)
1379 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1380 }
1381
1382}
1383
1384static int msm_otg_set_peripheral(struct otg_transceiver *otg,
1385 struct usb_gadget *gadget)
1386{
1387 struct msm_otg *motg = container_of(otg, struct msm_otg, otg);
1388
1389 /*
1390 * Fail peripheral registration if this board can support
1391 * only host configuration.
1392 */
1393 if (motg->pdata->mode == USB_HOST) {
1394 dev_info(otg->dev, "Peripheral mode is not supported\n");
1395 return -ENODEV;
1396 }
1397
1398 if (!gadget) {
1399 if (otg->state == OTG_STATE_B_PERIPHERAL) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301400 pm_runtime_get_sync(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301401 msm_otg_start_peripheral(otg, 0);
1402 otg->gadget = NULL;
1403 otg->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301404 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301405 } else {
1406 otg->gadget = NULL;
1407 }
1408
1409 return 0;
1410 }
1411 otg->gadget = gadget;
1412 dev_dbg(otg->dev, "peripheral driver registered w/ tranceiver\n");
1413
1414 /*
1415 * Kick the state machine work, if host is not supported
1416 * or host is already registered with us.
1417 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301418 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
1419 pm_runtime_get_sync(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301420 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301421 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301422
1423 return 0;
1424}
1425
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426static bool msm_chg_aca_detect(struct msm_otg *motg)
1427{
1428 struct otg_transceiver *otg = &motg->otg;
1429 u32 int_sts;
1430 bool ret = false;
1431
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301432 if (!aca_enabled())
1433 goto out;
1434
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001435 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1436 goto out;
1437
1438 int_sts = ulpi_read(otg, 0x87);
1439 switch (int_sts & 0x1C) {
1440 case 0x08:
1441 if (!test_and_set_bit(ID_A, &motg->inputs)) {
1442 dev_dbg(otg->dev, "ID_A\n");
1443 motg->chg_type = USB_ACA_A_CHARGER;
1444 motg->chg_state = USB_CHG_STATE_DETECTED;
1445 clear_bit(ID_B, &motg->inputs);
1446 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301447 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001448 ret = true;
1449 }
1450 break;
1451 case 0x0C:
1452 if (!test_and_set_bit(ID_B, &motg->inputs)) {
1453 dev_dbg(otg->dev, "ID_B\n");
1454 motg->chg_type = USB_ACA_B_CHARGER;
1455 motg->chg_state = USB_CHG_STATE_DETECTED;
1456 clear_bit(ID_A, &motg->inputs);
1457 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301458 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459 ret = true;
1460 }
1461 break;
1462 case 0x10:
1463 if (!test_and_set_bit(ID_C, &motg->inputs)) {
1464 dev_dbg(otg->dev, "ID_C\n");
1465 motg->chg_type = USB_ACA_C_CHARGER;
1466 motg->chg_state = USB_CHG_STATE_DETECTED;
1467 clear_bit(ID_A, &motg->inputs);
1468 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301469 set_bit(ID, &motg->inputs);
1470 ret = true;
1471 }
1472 break;
1473 case 0x04:
1474 if (test_and_clear_bit(ID, &motg->inputs)) {
1475 dev_dbg(otg->dev, "ID_GND\n");
1476 motg->chg_type = USB_INVALID_CHARGER;
1477 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1478 clear_bit(ID_A, &motg->inputs);
1479 clear_bit(ID_B, &motg->inputs);
1480 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481 ret = true;
1482 }
1483 break;
1484 default:
1485 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1486 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301487 test_and_clear_bit(ID_C, &motg->inputs) |
1488 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489 if (ret) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301490 dev_dbg(otg->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 motg->chg_type = USB_INVALID_CHARGER;
1492 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1493 }
1494 }
1495out:
1496 return ret;
1497}
1498
1499static void msm_chg_enable_aca_det(struct msm_otg *motg)
1500{
1501 struct otg_transceiver *otg = &motg->otg;
1502
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301503 if (!aca_enabled())
1504 return;
1505
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506 switch (motg->pdata->phy_type) {
1507 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301508 /* Disable ID_GND in link and PHY */
1509 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1510 OTGSC_IDIE), USB_OTGSC);
1511 ulpi_write(otg, 0x01, 0x0C);
1512 ulpi_write(otg, 0x10, 0x0F);
1513 ulpi_write(otg, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301514 /* Disable PMIC ID pull-up */
1515 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301516 /* Enable ACA ID detection */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001517 ulpi_write(otg, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301518 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001519 break;
1520 default:
1521 break;
1522 }
1523}
1524
1525static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1526{
1527 struct otg_transceiver *otg = &motg->otg;
1528
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301529 if (!aca_enabled())
1530 return;
1531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532 switch (motg->pdata->phy_type) {
1533 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301534 /* Enable ACA Detection interrupt (on any RID change) */
1535 ulpi_write(otg, 0x01, 0x94);
1536 break;
1537 default:
1538 break;
1539 }
1540}
1541
1542static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1543{
1544 struct otg_transceiver *otg = &motg->otg;
1545
1546 if (!aca_enabled())
1547 return;
1548
1549 switch (motg->pdata->phy_type) {
1550 case SNPS_28NM_INTEGRATED_PHY:
1551 ulpi_write(otg, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 break;
1553 default:
1554 break;
1555 }
1556}
1557
1558static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1559{
1560 struct otg_transceiver *otg = &motg->otg;
1561 bool ret = false;
1562
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301563 if (!aca_enabled())
1564 return ret;
1565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001566 switch (motg->pdata->phy_type) {
1567 case SNPS_28NM_INTEGRATED_PHY:
1568 if (ulpi_read(otg, 0x91) & 1) {
1569 dev_dbg(otg->dev, "RID change\n");
1570 ulpi_write(otg, 0x01, 0x92);
1571 ret = msm_chg_aca_detect(motg);
1572 }
1573 default:
1574 break;
1575 }
1576 return ret;
1577}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301578
1579static void msm_otg_id_timer_func(unsigned long data)
1580{
1581 struct msm_otg *motg = (struct msm_otg *) data;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301582 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301583
1584 if (!aca_enabled())
1585 return;
1586
1587 if (atomic_read(&motg->in_lpm)) {
1588 dev_dbg(motg->otg.dev, "timer: in lpm\n");
1589 return;
1590 }
1591
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301592 if (otg->state == OTG_STATE_A_SUSPEND)
1593 goto out;
1594
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301595 if (msm_chg_check_aca_intr(motg)) {
1596 dev_dbg(motg->otg.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301597 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301598 }
1599
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301600out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301601 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1602 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1603}
1604
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301605static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1606{
1607 struct otg_transceiver *otg = &motg->otg;
1608 u32 chg_det;
1609 bool ret = false;
1610
1611 switch (motg->pdata->phy_type) {
1612 case CI_45NM_INTEGRATED_PHY:
1613 chg_det = ulpi_read(otg, 0x34);
1614 ret = chg_det & (1 << 4);
1615 break;
1616 case SNPS_28NM_INTEGRATED_PHY:
1617 chg_det = ulpi_read(otg, 0x87);
1618 ret = chg_det & 1;
1619 break;
1620 default:
1621 break;
1622 }
1623 return ret;
1624}
1625
1626static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1627{
1628 struct otg_transceiver *otg = &motg->otg;
1629 u32 chg_det;
1630
1631 switch (motg->pdata->phy_type) {
1632 case CI_45NM_INTEGRATED_PHY:
1633 chg_det = ulpi_read(otg, 0x34);
1634 /* Turn off charger block */
1635 chg_det |= ~(1 << 1);
1636 ulpi_write(otg, chg_det, 0x34);
1637 udelay(20);
1638 /* control chg block via ULPI */
1639 chg_det &= ~(1 << 3);
1640 ulpi_write(otg, chg_det, 0x34);
1641 /* put it in host mode for enabling D- source */
1642 chg_det &= ~(1 << 2);
1643 ulpi_write(otg, chg_det, 0x34);
1644 /* Turn on chg detect block */
1645 chg_det &= ~(1 << 1);
1646 ulpi_write(otg, chg_det, 0x34);
1647 udelay(20);
1648 /* enable chg detection */
1649 chg_det &= ~(1 << 0);
1650 ulpi_write(otg, chg_det, 0x34);
1651 break;
1652 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301653 /* Turn off VDP_SRC */
1654 ulpi_write(otg, 0x3, 0x86);
1655 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301656 /*
1657 * Configure DM as current source, DP as current sink
1658 * and enable battery charging comparators.
1659 */
1660 ulpi_write(otg, 0x8, 0x85);
1661 ulpi_write(otg, 0x2, 0x85);
1662 ulpi_write(otg, 0x1, 0x85);
1663 break;
1664 default:
1665 break;
1666 }
1667}
1668
1669static bool msm_chg_check_primary_det(struct msm_otg *motg)
1670{
1671 struct otg_transceiver *otg = &motg->otg;
1672 u32 chg_det;
1673 bool ret = false;
1674
1675 switch (motg->pdata->phy_type) {
1676 case CI_45NM_INTEGRATED_PHY:
1677 chg_det = ulpi_read(otg, 0x34);
1678 ret = chg_det & (1 << 4);
1679 break;
1680 case SNPS_28NM_INTEGRATED_PHY:
1681 chg_det = ulpi_read(otg, 0x87);
1682 ret = chg_det & 1;
1683 break;
1684 default:
1685 break;
1686 }
1687 return ret;
1688}
1689
1690static void msm_chg_enable_primary_det(struct msm_otg *motg)
1691{
1692 struct otg_transceiver *otg = &motg->otg;
1693 u32 chg_det;
1694
1695 switch (motg->pdata->phy_type) {
1696 case CI_45NM_INTEGRATED_PHY:
1697 chg_det = ulpi_read(otg, 0x34);
1698 /* enable chg detection */
1699 chg_det &= ~(1 << 0);
1700 ulpi_write(otg, chg_det, 0x34);
1701 break;
1702 case SNPS_28NM_INTEGRATED_PHY:
1703 /*
1704 * Configure DP as current source, DM as current sink
1705 * and enable battery charging comparators.
1706 */
1707 ulpi_write(otg, 0x2, 0x85);
1708 ulpi_write(otg, 0x1, 0x85);
1709 break;
1710 default:
1711 break;
1712 }
1713}
1714
1715static bool msm_chg_check_dcd(struct msm_otg *motg)
1716{
1717 struct otg_transceiver *otg = &motg->otg;
1718 u32 line_state;
1719 bool ret = false;
1720
1721 switch (motg->pdata->phy_type) {
1722 case CI_45NM_INTEGRATED_PHY:
1723 line_state = ulpi_read(otg, 0x15);
1724 ret = !(line_state & 1);
1725 break;
1726 case SNPS_28NM_INTEGRATED_PHY:
1727 line_state = ulpi_read(otg, 0x87);
1728 ret = line_state & 2;
1729 break;
1730 default:
1731 break;
1732 }
1733 return ret;
1734}
1735
1736static void msm_chg_disable_dcd(struct msm_otg *motg)
1737{
1738 struct otg_transceiver *otg = &motg->otg;
1739 u32 chg_det;
1740
1741 switch (motg->pdata->phy_type) {
1742 case CI_45NM_INTEGRATED_PHY:
1743 chg_det = ulpi_read(otg, 0x34);
1744 chg_det &= ~(1 << 5);
1745 ulpi_write(otg, chg_det, 0x34);
1746 break;
1747 case SNPS_28NM_INTEGRATED_PHY:
1748 ulpi_write(otg, 0x10, 0x86);
1749 break;
1750 default:
1751 break;
1752 }
1753}
1754
1755static void msm_chg_enable_dcd(struct msm_otg *motg)
1756{
1757 struct otg_transceiver *otg = &motg->otg;
1758 u32 chg_det;
1759
1760 switch (motg->pdata->phy_type) {
1761 case CI_45NM_INTEGRATED_PHY:
1762 chg_det = ulpi_read(otg, 0x34);
1763 /* Turn on D+ current source */
1764 chg_det |= (1 << 5);
1765 ulpi_write(otg, chg_det, 0x34);
1766 break;
1767 case SNPS_28NM_INTEGRATED_PHY:
1768 /* Data contact detection enable */
1769 ulpi_write(otg, 0x10, 0x85);
1770 break;
1771 default:
1772 break;
1773 }
1774}
1775
1776static void msm_chg_block_on(struct msm_otg *motg)
1777{
1778 struct otg_transceiver *otg = &motg->otg;
1779 u32 func_ctrl, chg_det;
1780
1781 /* put the controller in non-driving mode */
1782 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1783 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1784 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
1785 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1786
1787 switch (motg->pdata->phy_type) {
1788 case CI_45NM_INTEGRATED_PHY:
1789 chg_det = ulpi_read(otg, 0x34);
1790 /* control chg block via ULPI */
1791 chg_det &= ~(1 << 3);
1792 ulpi_write(otg, chg_det, 0x34);
1793 /* Turn on chg detect block */
1794 chg_det &= ~(1 << 1);
1795 ulpi_write(otg, chg_det, 0x34);
1796 udelay(20);
1797 break;
1798 case SNPS_28NM_INTEGRATED_PHY:
1799 /* Clear charger detecting control bits */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301800 ulpi_write(otg, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801 /* Clear alt interrupt latch and enable bits */
1802 ulpi_write(otg, 0x1F, 0x92);
1803 ulpi_write(otg, 0x1F, 0x95);
1804 udelay(100);
1805 break;
1806 default:
1807 break;
1808 }
1809}
1810
1811static void msm_chg_block_off(struct msm_otg *motg)
1812{
1813 struct otg_transceiver *otg = &motg->otg;
1814 u32 func_ctrl, chg_det;
1815
1816 switch (motg->pdata->phy_type) {
1817 case CI_45NM_INTEGRATED_PHY:
1818 chg_det = ulpi_read(otg, 0x34);
1819 /* Turn off charger block */
1820 chg_det |= ~(1 << 1);
1821 ulpi_write(otg, chg_det, 0x34);
1822 break;
1823 case SNPS_28NM_INTEGRATED_PHY:
1824 /* Clear charger detecting control bits */
1825 ulpi_write(otg, 0x3F, 0x86);
1826 /* Clear alt interrupt latch and enable bits */
1827 ulpi_write(otg, 0x1F, 0x92);
1828 ulpi_write(otg, 0x1F, 0x95);
1829 break;
1830 default:
1831 break;
1832 }
1833
1834 /* put the controller in normal mode */
1835 func_ctrl = ulpi_read(otg, ULPI_FUNC_CTRL);
1836 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1837 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
1838 ulpi_write(otg, func_ctrl, ULPI_FUNC_CTRL);
1839}
1840
Anji jonnalad270e2d2011-08-09 11:28:32 +05301841static const char *chg_to_string(enum usb_chg_type chg_type)
1842{
1843 switch (chg_type) {
1844 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1845 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1846 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1847 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1848 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1849 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1850 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1851 default: return "INVALID_CHARGER";
1852 }
1853}
1854
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301855#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1856#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301857#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1858#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301859static void msm_chg_detect_work(struct work_struct *w)
1860{
1861 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
1862 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301863 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 unsigned long delay;
1865
1866 dev_dbg(otg->dev, "chg detection work\n");
1867 switch (motg->chg_state) {
1868 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301869 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301870 if (motg->pdata->enable_dcd)
1871 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001872 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1874 motg->dcd_retries = 0;
1875 delay = MSM_CHG_DCD_POLL_TIME;
1876 break;
1877 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001878 is_aca = msm_chg_aca_detect(motg);
1879 if (is_aca) {
1880 /*
1881 * ID_A can be ACA dock too. continue
1882 * primary detection after DCD.
1883 */
1884 if (test_bit(ID_A, &motg->inputs)) {
1885 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1886 } else {
1887 delay = 0;
1888 break;
1889 }
1890 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301891 if (motg->pdata->enable_dcd)
1892 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301893 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1894 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301895 if (motg->pdata->enable_dcd)
1896 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301897 msm_chg_enable_primary_det(motg);
1898 delay = MSM_CHG_PRIMARY_DET_TIME;
1899 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1900 } else {
1901 delay = MSM_CHG_DCD_POLL_TIME;
1902 }
1903 break;
1904 case USB_CHG_STATE_DCD_DONE:
1905 vout = msm_chg_check_primary_det(motg);
1906 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301907 if (test_bit(ID_A, &motg->inputs)) {
1908 motg->chg_type = USB_ACA_DOCK_CHARGER;
1909 motg->chg_state = USB_CHG_STATE_DETECTED;
1910 delay = 0;
1911 break;
1912 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301913 msm_chg_enable_secondary_det(motg);
1914 delay = MSM_CHG_SECONDARY_DET_TIME;
1915 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1916 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301917 if (test_bit(ID_A, &motg->inputs)) {
1918 motg->chg_type = USB_ACA_A_CHARGER;
1919 motg->chg_state = USB_CHG_STATE_DETECTED;
1920 delay = 0;
1921 break;
1922 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301923 motg->chg_type = USB_SDP_CHARGER;
1924 motg->chg_state = USB_CHG_STATE_DETECTED;
1925 delay = 0;
1926 }
1927 break;
1928 case USB_CHG_STATE_PRIMARY_DONE:
1929 vout = msm_chg_check_secondary_det(motg);
1930 if (vout)
1931 motg->chg_type = USB_DCP_CHARGER;
1932 else
1933 motg->chg_type = USB_CDP_CHARGER;
1934 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1935 /* fall through */
1936 case USB_CHG_STATE_SECONDARY_DONE:
1937 motg->chg_state = USB_CHG_STATE_DETECTED;
1938 case USB_CHG_STATE_DETECTED:
1939 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001940 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301941 /*
1942 * Spurious interrupt is seen after enabling ACA detection
1943 * due to which charger detection fails in case of PET.
1944 * Add delay of 100 microsec to avoid that.
1945 */
1946 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001947 msm_chg_enable_aca_intr(motg);
Anji jonnalad270e2d2011-08-09 11:28:32 +05301948 dev_dbg(otg->dev, "chg_type = %s\n",
1949 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301950 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301951 return;
1952 default:
1953 return;
1954 }
1955
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301956 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301957}
1958
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301959/*
1960 * We support OTG, Peripheral only and Host only configurations. In case
1961 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1962 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1963 * enabled when switch is controlled by user and default mode is supplied
1964 * by board file, which can be changed by userspace later.
1965 */
1966static void msm_otg_init_sm(struct msm_otg *motg)
1967{
1968 struct msm_otg_platform_data *pdata = motg->pdata;
1969 u32 otgsc = readl(USB_OTGSC);
1970
1971 switch (pdata->mode) {
1972 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001973 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301974 if (pdata->default_mode == USB_HOST) {
1975 clear_bit(ID, &motg->inputs);
1976 } else if (pdata->default_mode == USB_PERIPHERAL) {
1977 set_bit(ID, &motg->inputs);
1978 set_bit(B_SESS_VLD, &motg->inputs);
1979 } else {
1980 set_bit(ID, &motg->inputs);
1981 clear_bit(B_SESS_VLD, &motg->inputs);
1982 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301983 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301984 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301985 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301986 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301987 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301988 set_bit(A_BUS_REQ, &motg->inputs);
1989 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001990 if (otgsc & OTGSC_BSV)
1991 set_bit(B_SESS_VLD, &motg->inputs);
1992 else
1993 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301994 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301995 if (pdata->pmic_id_irq) {
1996 if (irq_read_line(pdata->pmic_id_irq))
1997 set_bit(ID, &motg->inputs);
1998 else
1999 clear_bit(ID, &motg->inputs);
2000 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302001 /*
2002 * VBUS initial state is reported after PMIC
2003 * driver initialization. Wait for it.
2004 */
2005 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302006 }
2007 break;
2008 case USB_HOST:
2009 clear_bit(ID, &motg->inputs);
2010 break;
2011 case USB_PERIPHERAL:
2012 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302013 if (pdata->otg_control == OTG_PHY_CONTROL) {
2014 if (otgsc & OTGSC_BSV)
2015 set_bit(B_SESS_VLD, &motg->inputs);
2016 else
2017 clear_bit(B_SESS_VLD, &motg->inputs);
2018 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2019 /*
2020 * VBUS initial state is reported after PMIC
2021 * driver initialization. Wait for it.
2022 */
2023 wait_for_completion(&pmic_vbus_init);
2024 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302025 break;
2026 default:
2027 break;
2028 }
2029}
2030
2031static void msm_otg_sm_work(struct work_struct *w)
2032{
2033 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
2034 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302035 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302036
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302037 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302038 pr_debug("%s work\n", otg_state_string(otg->state));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302039 switch (otg->state) {
2040 case OTG_STATE_UNDEFINED:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302041 msm_otg_reset(otg);
2042 msm_otg_init_sm(motg);
2043 otg->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302044 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2045 test_bit(ID, &motg->inputs)) {
2046 pm_runtime_put_noidle(otg->dev);
2047 pm_runtime_suspend(otg->dev);
2048 break;
2049 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302050 /* FALL THROUGH */
2051 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052 if ((!test_bit(ID, &motg->inputs) ||
2053 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302054 pr_debug("!id || id_A\n");
2055 clear_bit(B_BUS_REQ, &motg->inputs);
2056 set_bit(A_BUS_REQ, &motg->inputs);
2057 otg->state = OTG_STATE_A_IDLE;
2058 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302059 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302060 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302061 switch (motg->chg_state) {
2062 case USB_CHG_STATE_UNDEFINED:
2063 msm_chg_detect_work(&motg->chg_work.work);
2064 break;
2065 case USB_CHG_STATE_DETECTED:
2066 switch (motg->chg_type) {
2067 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302068 /* Enable VDP_SRC */
2069 ulpi_write(otg, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302070 msm_otg_notify_charger(motg,
2071 IDEV_CHG_MAX);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302072 pm_runtime_put_noidle(otg->dev);
2073 pm_runtime_suspend(otg->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302074 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302075 case USB_ACA_B_CHARGER:
2076 msm_otg_notify_charger(motg,
2077 IDEV_ACA_CHG_MAX);
2078 /*
2079 * (ID_B --> ID_C) PHY_ALT interrupt can
2080 * not be detected in LPM.
2081 */
2082 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 case USB_CDP_CHARGER:
2084 msm_otg_notify_charger(motg,
2085 IDEV_CHG_MAX);
2086 msm_otg_start_peripheral(otg, 1);
2087 otg->state = OTG_STATE_B_PERIPHERAL;
2088 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302089 case USB_ACA_C_CHARGER:
2090 msm_otg_notify_charger(motg,
2091 IDEV_ACA_CHG_MAX);
2092 msm_otg_start_peripheral(otg, 1);
2093 otg->state = OTG_STATE_B_PERIPHERAL;
2094 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302095 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302096 msm_otg_start_peripheral(otg, 1);
2097 otg->state = OTG_STATE_B_PERIPHERAL;
2098 break;
2099 default:
2100 break;
2101 }
2102 break;
2103 default:
2104 break;
2105 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302106 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2107 pr_debug("b_sess_end && b_bus_req\n");
2108 if (msm_otg_start_srp(otg) < 0) {
2109 clear_bit(B_BUS_REQ, &motg->inputs);
2110 work = 1;
2111 break;
2112 }
2113 otg->state = OTG_STATE_B_SRP_INIT;
2114 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2115 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302116 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302117 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302118 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302119 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2120 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302121 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302122 msm_otg_reset(otg);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302123 pm_runtime_put_noidle(otg->dev);
2124 pm_runtime_suspend(otg->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302125 }
2126 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302127 case OTG_STATE_B_SRP_INIT:
2128 if (!test_bit(ID, &motg->inputs) ||
2129 test_bit(ID_A, &motg->inputs) ||
2130 test_bit(ID_C, &motg->inputs) ||
2131 (test_bit(B_SESS_VLD, &motg->inputs) &&
2132 !test_bit(ID_B, &motg->inputs))) {
2133 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2134 msm_otg_del_timer(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002135 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302136 /*
2137 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2138 * bits after SRP initiation.
2139 */
2140 ulpi_write(otg, 0x0, 0x98);
2141 work = 1;
2142 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2143 pr_debug("b_srp_fail\n");
2144 pr_info("A-device did not respond to SRP\n");
2145 clear_bit(B_BUS_REQ, &motg->inputs);
2146 clear_bit(B_SRP_FAIL, &motg->tmouts);
2147 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
2148 ulpi_write(otg, 0x0, 0x98);
2149 otg->state = OTG_STATE_B_IDLE;
2150 motg->b_last_se0_sess = jiffies;
2151 work = 1;
2152 }
2153 break;
2154 case OTG_STATE_B_PERIPHERAL:
2155 if (!test_bit(ID, &motg->inputs) ||
2156 test_bit(ID_A, &motg->inputs) ||
2157 test_bit(ID_B, &motg->inputs) ||
2158 !test_bit(B_SESS_VLD, &motg->inputs)) {
2159 pr_debug("!id || id_a/b || !b_sess_vld\n");
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302160 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2161 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302162 msm_otg_notify_charger(motg, 0);
2163 srp_reqd = otg->gadget->otg_srp_reqd;
2164 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302165 if (test_bit(ID_B, &motg->inputs))
2166 clear_bit(ID_B, &motg->inputs);
2167 clear_bit(B_BUS_REQ, &motg->inputs);
2168 otg->state = OTG_STATE_B_IDLE;
2169 motg->b_last_se0_sess = jiffies;
2170 if (srp_reqd)
2171 msm_otg_start_timer(motg,
2172 TB_TST_SRP, B_TST_SRP);
2173 else
2174 work = 1;
2175 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2176 otg->gadget->b_hnp_enable &&
2177 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2178 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2179 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2180 /* D+ pullup should not be disconnected within 4msec
2181 * after A device suspends the bus. Otherwise PET will
2182 * fail the compliance test.
2183 */
2184 udelay(1000);
2185 msm_otg_start_peripheral(otg, 0);
2186 otg->state = OTG_STATE_B_WAIT_ACON;
2187 /*
2188 * start HCD even before A-device enable
2189 * pull-up to meet HNP timings.
2190 */
2191 otg->host->is_b_host = 1;
2192 msm_otg_start_host(otg, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302194 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002195 }
2196 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302197 case OTG_STATE_B_WAIT_ACON:
2198 if (!test_bit(ID, &motg->inputs) ||
2199 test_bit(ID_A, &motg->inputs) ||
2200 test_bit(ID_B, &motg->inputs) ||
2201 !test_bit(B_SESS_VLD, &motg->inputs)) {
2202 pr_debug("!id || id_a/b || !b_sess_vld\n");
2203 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302204 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302205 * A-device is physically disconnected during
2206 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302207 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302208 msm_otg_start_host(otg, 0);
2209 otg->host->is_b_host = 0;
2210
2211 clear_bit(B_BUS_REQ, &motg->inputs);
2212 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2213 motg->b_last_se0_sess = jiffies;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302214 otg->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302215 msm_otg_reset(otg);
2216 work = 1;
2217 } else if (test_bit(A_CONN, &motg->inputs)) {
2218 pr_debug("a_conn\n");
2219 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2220 otg->state = OTG_STATE_B_HOST;
2221 /*
2222 * PET disconnects D+ pullup after reset is generated
2223 * by B device in B_HOST role which is not detected by
2224 * B device. As workaorund , start timer of 300msec
2225 * and stop timer if A device is enumerated else clear
2226 * A_CONN.
2227 */
2228 msm_otg_start_timer(motg, TB_TST_CONFIG,
2229 B_TST_CONFIG);
2230 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2231 pr_debug("b_ase0_brst_tmout\n");
2232 pr_info("B HNP fail:No response from A device\n");
2233 msm_otg_start_host(otg, 0);
2234 msm_otg_reset(otg);
2235 otg->host->is_b_host = 0;
2236 clear_bit(B_ASE0_BRST, &motg->tmouts);
2237 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2238 clear_bit(B_BUS_REQ, &motg->inputs);
2239 otg_send_event(OTG_EVENT_HNP_FAILED);
2240 otg->state = OTG_STATE_B_IDLE;
2241 work = 1;
2242 } else if (test_bit(ID_C, &motg->inputs)) {
2243 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2244 }
2245 break;
2246 case OTG_STATE_B_HOST:
2247 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2248 !test_bit(A_CONN, &motg->inputs) ||
2249 !test_bit(B_SESS_VLD, &motg->inputs)) {
2250 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2251 clear_bit(A_CONN, &motg->inputs);
2252 clear_bit(B_BUS_REQ, &motg->inputs);
2253 msm_otg_start_host(otg, 0);
2254 otg->host->is_b_host = 0;
2255 otg->state = OTG_STATE_B_IDLE;
2256 msm_otg_reset(otg);
2257 work = 1;
2258 } else if (test_bit(ID_C, &motg->inputs)) {
2259 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2260 }
2261 break;
2262 case OTG_STATE_A_IDLE:
2263 otg->default_a = 1;
2264 if (test_bit(ID, &motg->inputs) &&
2265 !test_bit(ID_A, &motg->inputs)) {
2266 pr_debug("id && !id_a\n");
2267 otg->default_a = 0;
2268 clear_bit(A_BUS_DROP, &motg->inputs);
2269 otg->state = OTG_STATE_B_IDLE;
2270 del_timer_sync(&motg->id_timer);
2271 msm_otg_link_reset(motg);
2272 msm_chg_enable_aca_intr(motg);
2273 msm_otg_notify_charger(motg, 0);
2274 work = 1;
2275 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2276 (test_bit(A_SRP_DET, &motg->inputs) ||
2277 test_bit(A_BUS_REQ, &motg->inputs))) {
2278 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2279
2280 clear_bit(A_SRP_DET, &motg->inputs);
2281 /* Disable SRP detection */
2282 writel_relaxed((readl_relaxed(USB_OTGSC) &
2283 ~OTGSC_INTSTS_MASK) &
2284 ~OTGSC_DPIE, USB_OTGSC);
2285
2286 otg->state = OTG_STATE_A_WAIT_VRISE;
2287 /* VBUS should not be supplied before end of SRP pulse
2288 * generated by PET, if not complaince test fail.
2289 */
2290 usleep_range(10000, 12000);
2291 /* ACA: ID_A: Stop charging untill enumeration */
2292 if (test_bit(ID_A, &motg->inputs))
2293 msm_otg_notify_charger(motg, 0);
2294 else
2295 msm_hsusb_vbus_power(motg, 1);
2296 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2297 } else {
2298 pr_debug("No session requested\n");
2299 clear_bit(A_BUS_DROP, &motg->inputs);
2300 if (test_bit(ID_A, &motg->inputs)) {
2301 msm_otg_notify_charger(motg,
2302 IDEV_ACA_CHG_MAX);
2303 } else if (!test_bit(ID, &motg->inputs)) {
2304 msm_otg_notify_charger(motg, 0);
2305 /*
2306 * A-device is not providing power on VBUS.
2307 * Enable SRP detection.
2308 */
2309 writel_relaxed(0x13, USB_USBMODE);
2310 writel_relaxed((readl_relaxed(USB_OTGSC) &
2311 ~OTGSC_INTSTS_MASK) |
2312 OTGSC_DPIE, USB_OTGSC);
2313 mb();
2314 }
2315 }
2316 break;
2317 case OTG_STATE_A_WAIT_VRISE:
2318 if ((test_bit(ID, &motg->inputs) &&
2319 !test_bit(ID_A, &motg->inputs)) ||
2320 test_bit(A_BUS_DROP, &motg->inputs) ||
2321 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2322 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2323 clear_bit(A_BUS_REQ, &motg->inputs);
2324 msm_otg_del_timer(motg);
2325 msm_hsusb_vbus_power(motg, 0);
2326 otg->state = OTG_STATE_A_WAIT_VFALL;
2327 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2328 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2329 pr_debug("a_vbus_vld\n");
2330 otg->state = OTG_STATE_A_WAIT_BCON;
2331 if (TA_WAIT_BCON > 0)
2332 msm_otg_start_timer(motg, TA_WAIT_BCON,
2333 A_WAIT_BCON);
2334 msm_otg_start_host(otg, 1);
2335 msm_chg_enable_aca_det(motg);
2336 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302337 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302338 if (msm_chg_check_aca_intr(motg))
2339 work = 1;
2340 }
2341 break;
2342 case OTG_STATE_A_WAIT_BCON:
2343 if ((test_bit(ID, &motg->inputs) &&
2344 !test_bit(ID_A, &motg->inputs)) ||
2345 test_bit(A_BUS_DROP, &motg->inputs) ||
2346 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2347 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2348 "a_wait_bcon_tmout\n");
2349 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2350 pr_info("Device No Response\n");
2351 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2352 }
2353 msm_otg_del_timer(motg);
2354 clear_bit(A_BUS_REQ, &motg->inputs);
2355 clear_bit(B_CONN, &motg->inputs);
2356 msm_otg_start_host(otg, 0);
2357 /*
2358 * ACA: ID_A with NO accessory, just the A plug is
2359 * attached to ACA: Use IDCHG_MAX for charging
2360 */
2361 if (test_bit(ID_A, &motg->inputs))
2362 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2363 else
2364 msm_hsusb_vbus_power(motg, 0);
2365 otg->state = OTG_STATE_A_WAIT_VFALL;
2366 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2367 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2368 pr_debug("!a_vbus_vld\n");
2369 clear_bit(B_CONN, &motg->inputs);
2370 msm_otg_del_timer(motg);
2371 msm_otg_start_host(otg, 0);
2372 otg->state = OTG_STATE_A_VBUS_ERR;
2373 msm_otg_reset(otg);
2374 } else if (test_bit(ID_A, &motg->inputs)) {
2375 msm_hsusb_vbus_power(motg, 0);
2376 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2377 /*
2378 * If TA_WAIT_BCON is infinite, we don;t
2379 * turn off VBUS. Enter low power mode.
2380 */
2381 if (TA_WAIT_BCON < 0)
2382 pm_runtime_put_sync(otg->dev);
2383 } else if (!test_bit(ID, &motg->inputs)) {
2384 msm_hsusb_vbus_power(motg, 1);
2385 }
2386 break;
2387 case OTG_STATE_A_HOST:
2388 if ((test_bit(ID, &motg->inputs) &&
2389 !test_bit(ID_A, &motg->inputs)) ||
2390 test_bit(A_BUS_DROP, &motg->inputs)) {
2391 pr_debug("id_a/b/c || a_bus_drop\n");
2392 clear_bit(B_CONN, &motg->inputs);
2393 clear_bit(A_BUS_REQ, &motg->inputs);
2394 msm_otg_del_timer(motg);
2395 otg->state = OTG_STATE_A_WAIT_VFALL;
2396 msm_otg_start_host(otg, 0);
2397 if (!test_bit(ID_A, &motg->inputs))
2398 msm_hsusb_vbus_power(motg, 0);
2399 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2400 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2401 pr_debug("!a_vbus_vld\n");
2402 clear_bit(B_CONN, &motg->inputs);
2403 msm_otg_del_timer(motg);
2404 otg->state = OTG_STATE_A_VBUS_ERR;
2405 msm_otg_start_host(otg, 0);
2406 msm_otg_reset(otg);
2407 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2408 /*
2409 * a_bus_req is de-asserted when root hub is
2410 * suspended or HNP is in progress.
2411 */
2412 pr_debug("!a_bus_req\n");
2413 msm_otg_del_timer(motg);
2414 otg->state = OTG_STATE_A_SUSPEND;
2415 if (otg->host->b_hnp_enable)
2416 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2417 A_AIDL_BDIS);
2418 else
2419 pm_runtime_put_sync(otg->dev);
2420 } else if (!test_bit(B_CONN, &motg->inputs)) {
2421 pr_debug("!b_conn\n");
2422 msm_otg_del_timer(motg);
2423 otg->state = OTG_STATE_A_WAIT_BCON;
2424 if (TA_WAIT_BCON > 0)
2425 msm_otg_start_timer(motg, TA_WAIT_BCON,
2426 A_WAIT_BCON);
2427 if (msm_chg_check_aca_intr(motg))
2428 work = 1;
2429 } else if (test_bit(ID_A, &motg->inputs)) {
2430 msm_otg_del_timer(motg);
2431 msm_hsusb_vbus_power(motg, 0);
2432 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2433 msm_otg_notify_charger(motg,
2434 IDEV_ACA_CHG_MAX);
2435 else
2436 msm_otg_notify_charger(motg,
2437 IDEV_CHG_MIN - motg->mA_port);
2438 } else if (!test_bit(ID, &motg->inputs)) {
2439 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2440 motg->chg_type = USB_INVALID_CHARGER;
2441 msm_otg_notify_charger(motg, 0);
2442 msm_hsusb_vbus_power(motg, 1);
2443 }
2444 break;
2445 case OTG_STATE_A_SUSPEND:
2446 if ((test_bit(ID, &motg->inputs) &&
2447 !test_bit(ID_A, &motg->inputs)) ||
2448 test_bit(A_BUS_DROP, &motg->inputs) ||
2449 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2450 pr_debug("id_a/b/c || a_bus_drop ||"
2451 "a_aidl_bdis_tmout\n");
2452 msm_otg_del_timer(motg);
2453 clear_bit(B_CONN, &motg->inputs);
2454 otg->state = OTG_STATE_A_WAIT_VFALL;
2455 msm_otg_start_host(otg, 0);
2456 msm_otg_reset(otg);
2457 if (!test_bit(ID_A, &motg->inputs))
2458 msm_hsusb_vbus_power(motg, 0);
2459 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2460 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2461 pr_debug("!a_vbus_vld\n");
2462 msm_otg_del_timer(motg);
2463 clear_bit(B_CONN, &motg->inputs);
2464 otg->state = OTG_STATE_A_VBUS_ERR;
2465 msm_otg_start_host(otg, 0);
2466 msm_otg_reset(otg);
2467 } else if (!test_bit(B_CONN, &motg->inputs) &&
2468 otg->host->b_hnp_enable) {
2469 pr_debug("!b_conn && b_hnp_enable");
2470 otg->state = OTG_STATE_A_PERIPHERAL;
2471 msm_otg_host_hnp_enable(otg, 1);
2472 otg->gadget->is_a_peripheral = 1;
2473 msm_otg_start_peripheral(otg, 1);
2474 } else if (!test_bit(B_CONN, &motg->inputs) &&
2475 !otg->host->b_hnp_enable) {
2476 pr_debug("!b_conn && !b_hnp_enable");
2477 /*
2478 * bus request is dropped during suspend.
2479 * acquire again for next device.
2480 */
2481 set_bit(A_BUS_REQ, &motg->inputs);
2482 otg->state = OTG_STATE_A_WAIT_BCON;
2483 if (TA_WAIT_BCON > 0)
2484 msm_otg_start_timer(motg, TA_WAIT_BCON,
2485 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302487 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002488 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302489 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002490 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002491 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302492 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302493 }
2494 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302495 case OTG_STATE_A_PERIPHERAL:
2496 if ((test_bit(ID, &motg->inputs) &&
2497 !test_bit(ID_A, &motg->inputs)) ||
2498 test_bit(A_BUS_DROP, &motg->inputs)) {
2499 pr_debug("id _f/b/c || a_bus_drop\n");
2500 /* Clear BIDL_ADIS timer */
2501 msm_otg_del_timer(motg);
2502 otg->state = OTG_STATE_A_WAIT_VFALL;
2503 msm_otg_start_peripheral(otg, 0);
2504 otg->gadget->is_a_peripheral = 0;
2505 msm_otg_start_host(otg, 0);
2506 msm_otg_reset(otg);
2507 if (!test_bit(ID_A, &motg->inputs))
2508 msm_hsusb_vbus_power(motg, 0);
2509 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2510 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2511 pr_debug("!a_vbus_vld\n");
2512 /* Clear BIDL_ADIS timer */
2513 msm_otg_del_timer(motg);
2514 otg->state = OTG_STATE_A_VBUS_ERR;
2515 msm_otg_start_peripheral(otg, 0);
2516 otg->gadget->is_a_peripheral = 0;
2517 msm_otg_start_host(otg, 0);
2518 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2519 pr_debug("a_bidl_adis_tmout\n");
2520 msm_otg_start_peripheral(otg, 0);
2521 otg->gadget->is_a_peripheral = 0;
2522 otg->state = OTG_STATE_A_WAIT_BCON;
2523 set_bit(A_BUS_REQ, &motg->inputs);
2524 msm_otg_host_hnp_enable(otg, 0);
2525 if (TA_WAIT_BCON > 0)
2526 msm_otg_start_timer(motg, TA_WAIT_BCON,
2527 A_WAIT_BCON);
2528 } else if (test_bit(ID_A, &motg->inputs)) {
2529 msm_hsusb_vbus_power(motg, 0);
2530 msm_otg_notify_charger(motg,
2531 IDEV_CHG_MIN - motg->mA_port);
2532 } else if (!test_bit(ID, &motg->inputs)) {
2533 msm_otg_notify_charger(motg, 0);
2534 msm_hsusb_vbus_power(motg, 1);
2535 }
2536 break;
2537 case OTG_STATE_A_WAIT_VFALL:
2538 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2539 clear_bit(A_VBUS_VLD, &motg->inputs);
2540 otg->state = OTG_STATE_A_IDLE;
2541 work = 1;
2542 }
2543 break;
2544 case OTG_STATE_A_VBUS_ERR:
2545 if ((test_bit(ID, &motg->inputs) &&
2546 !test_bit(ID_A, &motg->inputs)) ||
2547 test_bit(A_BUS_DROP, &motg->inputs) ||
2548 test_bit(A_CLR_ERR, &motg->inputs)) {
2549 otg->state = OTG_STATE_A_WAIT_VFALL;
2550 if (!test_bit(ID_A, &motg->inputs))
2551 msm_hsusb_vbus_power(motg, 0);
2552 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2553 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2554 motg->chg_type = USB_INVALID_CHARGER;
2555 msm_otg_notify_charger(motg, 0);
2556 }
2557 break;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302558 default:
2559 break;
2560 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302561 if (work)
2562 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302563}
2564
2565static irqreturn_t msm_otg_irq(int irq, void *data)
2566{
2567 struct msm_otg *motg = data;
2568 struct otg_transceiver *otg = &motg->otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302569 u32 otgsc = 0, usbsts, pc;
2570 bool work = 0;
2571 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302572
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302573 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302574 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302575 disable_irq_nosync(irq);
2576 motg->async_int = 1;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302577 pm_request_resume(otg->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302578 return IRQ_HANDLED;
2579 }
2580
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002581 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302582 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302583
2584 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302585 return IRQ_NONE;
2586
2587 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302588 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302589 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302590 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302591 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302592 pr_debug("Id clear\n");
2593 /*
2594 * Assert a_bus_req to supply power on
2595 * VBUS when Micro/Mini-A cable is connected
2596 * with out user intervention.
2597 */
2598 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302599 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302600 msm_chg_enable_aca_det(motg);
2601 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302602 writel_relaxed(otgsc, USB_OTGSC);
2603 work = 1;
2604 } else if (otgsc & OTGSC_DPIS) {
2605 pr_debug("DPIS detected\n");
2606 writel_relaxed(otgsc, USB_OTGSC);
2607 set_bit(A_SRP_DET, &motg->inputs);
2608 set_bit(A_BUS_REQ, &motg->inputs);
2609 work = 1;
2610 } else if (otgsc & OTGSC_BSVIS) {
2611 writel_relaxed(otgsc, USB_OTGSC);
2612 /*
2613 * BSV interrupt comes when operating as an A-device
2614 * (VBUS on/off).
2615 * But, handle BSV when charger is removed from ACA in ID_A
2616 */
2617 if ((otg->state >= OTG_STATE_A_IDLE) &&
2618 !test_bit(ID_A, &motg->inputs))
2619 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302620 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302621 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302622 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302623 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302624 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302625 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302626 msm_chg_check_aca_intr(motg);
2627 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302628 work = 1;
2629 } else if (usbsts & STS_PCI) {
2630 pc = readl_relaxed(USB_PORTSC);
2631 pr_debug("portsc = %x\n", pc);
2632 ret = IRQ_NONE;
2633 /*
2634 * HCD Acks PCI interrupt. We use this to switch
2635 * between different OTG states.
2636 */
2637 work = 1;
2638 switch (otg->state) {
2639 case OTG_STATE_A_SUSPEND:
2640 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2641 !(pc & PORTSC_CCS)) {
2642 pr_debug("B_CONN clear\n");
2643 clear_bit(B_CONN, &motg->inputs);
2644 msm_otg_del_timer(motg);
2645 }
2646 break;
2647 case OTG_STATE_A_PERIPHERAL:
2648 /*
2649 * A-peripheral observed activity on bus.
2650 * clear A_BIDL_ADIS timer.
2651 */
2652 msm_otg_del_timer(motg);
2653 work = 0;
2654 break;
2655 case OTG_STATE_B_WAIT_ACON:
2656 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2657 pr_debug("A_CONN set\n");
2658 set_bit(A_CONN, &motg->inputs);
2659 /* Clear ASE0_BRST timer */
2660 msm_otg_del_timer(motg);
2661 }
2662 break;
2663 case OTG_STATE_B_HOST:
2664 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2665 pr_debug("A_CONN clear\n");
2666 clear_bit(A_CONN, &motg->inputs);
2667 msm_otg_del_timer(motg);
2668 }
2669 break;
2670 case OTG_STATE_A_WAIT_BCON:
2671 if (TA_WAIT_BCON < 0)
2672 set_bit(A_BUS_REQ, &motg->inputs);
2673 default:
2674 work = 0;
2675 break;
2676 }
2677 } else if (usbsts & STS_URI) {
2678 ret = IRQ_NONE;
2679 switch (otg->state) {
2680 case OTG_STATE_A_PERIPHERAL:
2681 /*
2682 * A-peripheral observed activity on bus.
2683 * clear A_BIDL_ADIS timer.
2684 */
2685 msm_otg_del_timer(motg);
2686 work = 0;
2687 break;
2688 default:
2689 work = 0;
2690 break;
2691 }
2692 } else if (usbsts & STS_SLI) {
2693 ret = IRQ_NONE;
2694 work = 0;
2695 switch (otg->state) {
2696 case OTG_STATE_B_PERIPHERAL:
2697 if (otg->gadget->b_hnp_enable) {
2698 set_bit(A_BUS_SUSPEND, &motg->inputs);
2699 set_bit(B_BUS_REQ, &motg->inputs);
2700 work = 1;
2701 }
2702 break;
2703 case OTG_STATE_A_PERIPHERAL:
2704 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2705 A_BIDL_ADIS);
2706 break;
2707 default:
2708 break;
2709 }
2710 } else if ((usbsts & PHY_ALT_INT)) {
2711 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2712 if (msm_chg_check_aca_intr(motg))
2713 work = 1;
2714 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302715 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302716 if (work)
2717 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302718
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302719 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002720}
2721
2722static void msm_otg_set_vbus_state(int online)
2723{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302724 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002725 struct msm_otg *motg = the_msm_otg;
2726
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302727 if (online) {
2728 pr_debug("PMIC: BSV set\n");
2729 set_bit(B_SESS_VLD, &motg->inputs);
2730 } else {
2731 pr_debug("PMIC: BSV clear\n");
2732 clear_bit(B_SESS_VLD, &motg->inputs);
2733 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002734
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302735 if (!init) {
2736 init = true;
2737 complete(&pmic_vbus_init);
2738 pr_debug("PMIC: BSV init complete\n");
2739 return;
2740 }
2741
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302742 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002743}
2744
2745static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2746{
2747 struct msm_otg *motg = data;
2748
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302749 if (aca_id_turned_on)
2750 return IRQ_HANDLED;
2751
2752 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2753 pr_debug("PMIC: ID set\n");
2754 set_bit(ID, &motg->inputs);
2755 } else {
2756 pr_debug("PMIC: ID clear\n");
2757 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302758 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302759 }
2760
2761 if (motg->otg.state != OTG_STATE_UNDEFINED)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302762 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002763
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302764 return IRQ_HANDLED;
2765}
2766
2767static int msm_otg_mode_show(struct seq_file *s, void *unused)
2768{
2769 struct msm_otg *motg = s->private;
2770 struct otg_transceiver *otg = &motg->otg;
2771
2772 switch (otg->state) {
2773 case OTG_STATE_A_HOST:
2774 seq_printf(s, "host\n");
2775 break;
2776 case OTG_STATE_B_PERIPHERAL:
2777 seq_printf(s, "peripheral\n");
2778 break;
2779 default:
2780 seq_printf(s, "none\n");
2781 break;
2782 }
2783
2784 return 0;
2785}
2786
2787static int msm_otg_mode_open(struct inode *inode, struct file *file)
2788{
2789 return single_open(file, msm_otg_mode_show, inode->i_private);
2790}
2791
2792static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2793 size_t count, loff_t *ppos)
2794{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302795 struct seq_file *s = file->private_data;
2796 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302797 char buf[16];
2798 struct otg_transceiver *otg = &motg->otg;
2799 int status = count;
2800 enum usb_mode_type req_mode;
2801
2802 memset(buf, 0x00, sizeof(buf));
2803
2804 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2805 status = -EFAULT;
2806 goto out;
2807 }
2808
2809 if (!strncmp(buf, "host", 4)) {
2810 req_mode = USB_HOST;
2811 } else if (!strncmp(buf, "peripheral", 10)) {
2812 req_mode = USB_PERIPHERAL;
2813 } else if (!strncmp(buf, "none", 4)) {
2814 req_mode = USB_NONE;
2815 } else {
2816 status = -EINVAL;
2817 goto out;
2818 }
2819
2820 switch (req_mode) {
2821 case USB_NONE:
2822 switch (otg->state) {
2823 case OTG_STATE_A_HOST:
2824 case OTG_STATE_B_PERIPHERAL:
2825 set_bit(ID, &motg->inputs);
2826 clear_bit(B_SESS_VLD, &motg->inputs);
2827 break;
2828 default:
2829 goto out;
2830 }
2831 break;
2832 case USB_PERIPHERAL:
2833 switch (otg->state) {
2834 case OTG_STATE_B_IDLE:
2835 case OTG_STATE_A_HOST:
2836 set_bit(ID, &motg->inputs);
2837 set_bit(B_SESS_VLD, &motg->inputs);
2838 break;
2839 default:
2840 goto out;
2841 }
2842 break;
2843 case USB_HOST:
2844 switch (otg->state) {
2845 case OTG_STATE_B_IDLE:
2846 case OTG_STATE_B_PERIPHERAL:
2847 clear_bit(ID, &motg->inputs);
2848 break;
2849 default:
2850 goto out;
2851 }
2852 break;
2853 default:
2854 goto out;
2855 }
2856
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05302857 pm_runtime_resume(otg->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302858 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302859out:
2860 return status;
2861}
2862
2863const struct file_operations msm_otg_mode_fops = {
2864 .open = msm_otg_mode_open,
2865 .read = seq_read,
2866 .write = msm_otg_mode_write,
2867 .llseek = seq_lseek,
2868 .release = single_release,
2869};
2870
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302871static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2872{
2873 struct msm_otg *motg = s->private;
2874 struct otg_transceiver *otg = &motg->otg;
2875
2876 seq_printf(s, "%s\n", otg_state_string(otg->state));
2877 return 0;
2878}
2879
2880static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2881{
2882 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2883}
2884
2885const struct file_operations msm_otg_state_fops = {
2886 .open = msm_otg_otg_state_open,
2887 .read = seq_read,
2888 .llseek = seq_lseek,
2889 .release = single_release,
2890};
2891
Anji jonnalad270e2d2011-08-09 11:28:32 +05302892static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2893{
2894 struct msm_otg *motg = s->private;
2895
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302896 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302897 return 0;
2898}
2899
2900static int msm_otg_chg_open(struct inode *inode, struct file *file)
2901{
2902 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2903}
2904
2905const struct file_operations msm_otg_chg_fops = {
2906 .open = msm_otg_chg_open,
2907 .read = seq_read,
2908 .llseek = seq_lseek,
2909 .release = single_release,
2910};
2911
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302912static int msm_otg_aca_show(struct seq_file *s, void *unused)
2913{
2914 if (debug_aca_enabled)
2915 seq_printf(s, "enabled\n");
2916 else
2917 seq_printf(s, "disabled\n");
2918
2919 return 0;
2920}
2921
2922static int msm_otg_aca_open(struct inode *inode, struct file *file)
2923{
2924 return single_open(file, msm_otg_aca_show, inode->i_private);
2925}
2926
2927static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2928 size_t count, loff_t *ppos)
2929{
2930 char buf[8];
2931
2932 memset(buf, 0x00, sizeof(buf));
2933
2934 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2935 return -EFAULT;
2936
2937 if (!strncmp(buf, "enable", 6))
2938 debug_aca_enabled = true;
2939 else
2940 debug_aca_enabled = false;
2941
2942 return count;
2943}
2944
2945const struct file_operations msm_otg_aca_fops = {
2946 .open = msm_otg_aca_open,
2947 .read = seq_read,
2948 .write = msm_otg_aca_write,
2949 .llseek = seq_lseek,
2950 .release = single_release,
2951};
2952
Manu Gautam8bdcc592012-03-06 11:26:06 +05302953static int msm_otg_bus_show(struct seq_file *s, void *unused)
2954{
2955 if (debug_bus_voting_enabled)
2956 seq_printf(s, "enabled\n");
2957 else
2958 seq_printf(s, "disabled\n");
2959
2960 return 0;
2961}
2962
2963static int msm_otg_bus_open(struct inode *inode, struct file *file)
2964{
2965 return single_open(file, msm_otg_bus_show, inode->i_private);
2966}
2967
2968static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2969 size_t count, loff_t *ppos)
2970{
2971 char buf[8];
2972 int ret;
2973 struct seq_file *s = file->private_data;
2974 struct msm_otg *motg = s->private;
2975
2976 memset(buf, 0x00, sizeof(buf));
2977
2978 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2979 return -EFAULT;
2980
2981 if (!strncmp(buf, "enable", 6)) {
2982 /* Do not vote here. Let OTG statemachine decide when to vote */
2983 debug_bus_voting_enabled = true;
2984 } else {
2985 debug_bus_voting_enabled = false;
2986 if (motg->bus_perf_client) {
2987 ret = msm_bus_scale_client_update_request(
2988 motg->bus_perf_client, 0);
2989 if (ret)
2990 dev_err(motg->otg.dev, "%s: Failed to devote "
2991 "for bus bw %d\n", __func__, ret);
2992 }
2993 }
2994
2995 return count;
2996}
2997
2998const struct file_operations msm_otg_bus_fops = {
2999 .open = msm_otg_bus_open,
3000 .read = seq_read,
3001 .write = msm_otg_bus_write,
3002 .llseek = seq_lseek,
3003 .release = single_release,
3004};
3005
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303006static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303007
3008static int msm_otg_debugfs_init(struct msm_otg *motg)
3009{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303010 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303011
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303012 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3013
3014 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3015 return -ENODEV;
3016
Anji jonnalad270e2d2011-08-09 11:28:32 +05303017 if (motg->pdata->mode == USB_OTG &&
3018 motg->pdata->otg_control == OTG_USER_CONTROL) {
3019
Manu Gautam8bdcc592012-03-06 11:26:06 +05303020 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303021 S_IWUSR, msm_otg_dbg_root, motg,
3022 &msm_otg_mode_fops);
3023
Manu Gautam8bdcc592012-03-06 11:26:06 +05303024 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303025 debugfs_remove(msm_otg_dbg_root);
3026 msm_otg_dbg_root = NULL;
3027 return -ENODEV;
3028 }
3029 }
3030
Manu Gautam8bdcc592012-03-06 11:26:06 +05303031 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303032 msm_otg_dbg_root, motg,
3033 &msm_otg_chg_fops);
3034
Manu Gautam8bdcc592012-03-06 11:26:06 +05303035 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303036 debugfs_remove_recursive(msm_otg_dbg_root);
3037 return -ENODEV;
3038 }
3039
Manu Gautam8bdcc592012-03-06 11:26:06 +05303040 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303041 msm_otg_dbg_root, motg,
3042 &msm_otg_aca_fops);
3043
Manu Gautam8bdcc592012-03-06 11:26:06 +05303044 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303045 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303046 return -ENODEV;
3047 }
3048
Manu Gautam8bdcc592012-03-06 11:26:06 +05303049 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3050 msm_otg_dbg_root, motg,
3051 &msm_otg_bus_fops);
3052
3053 if (!msm_otg_dentry) {
3054 debugfs_remove_recursive(msm_otg_dbg_root);
3055 return -ENODEV;
3056 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303057
3058 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3059 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3060
3061 if (!msm_otg_dentry) {
3062 debugfs_remove_recursive(msm_otg_dbg_root);
3063 return -ENODEV;
3064 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303065 return 0;
3066}
3067
3068static void msm_otg_debugfs_cleanup(void)
3069{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303070 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303071}
3072
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303073static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3074static struct platform_device *msm_otg_add_pdev(
3075 struct platform_device *ofdev, const char *name)
3076{
3077 struct platform_device *pdev;
3078 const struct resource *res = ofdev->resource;
3079 unsigned int num = ofdev->num_resources;
3080 int retval;
3081
3082 pdev = platform_device_alloc(name, -1);
3083 if (!pdev) {
3084 retval = -ENOMEM;
3085 goto error;
3086 }
3087
3088 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3089 pdev->dev.dma_mask = &msm_otg_dma_mask;
3090
3091 if (num) {
3092 retval = platform_device_add_resources(pdev, res, num);
3093 if (retval)
3094 goto error;
3095 }
3096
3097 retval = platform_device_add(pdev);
3098 if (retval)
3099 goto error;
3100
3101 return pdev;
3102
3103error:
3104 platform_device_put(pdev);
3105 return ERR_PTR(retval);
3106}
3107
3108static int msm_otg_setup_devices(struct platform_device *ofdev,
3109 enum usb_mode_type mode, bool init)
3110{
3111 const char *gadget_name = "msm_hsusb";
3112 const char *host_name = "msm_hsusb_host";
3113 static struct platform_device *gadget_pdev;
3114 static struct platform_device *host_pdev;
3115 int retval = 0;
3116
3117 if (!init) {
3118 if (gadget_pdev)
3119 platform_device_unregister(gadget_pdev);
3120 if (host_pdev)
3121 platform_device_unregister(host_pdev);
3122 return 0;
3123 }
3124
3125 switch (mode) {
3126 case USB_OTG:
3127 /* fall through */
3128 case USB_PERIPHERAL:
3129 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3130 if (IS_ERR(gadget_pdev)) {
3131 retval = PTR_ERR(gadget_pdev);
3132 break;
3133 }
3134 if (mode == USB_PERIPHERAL)
3135 break;
3136 /* fall through */
3137 case USB_HOST:
3138 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3139 if (IS_ERR(host_pdev)) {
3140 retval = PTR_ERR(host_pdev);
3141 if (mode == USB_OTG)
3142 platform_device_unregister(gadget_pdev);
3143 }
3144 break;
3145 default:
3146 break;
3147 }
3148
3149 return retval;
3150}
3151
3152struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3153{
3154 struct device_node *node = pdev->dev.of_node;
3155 struct msm_otg_platform_data *pdata;
3156 int len = 0;
3157
3158 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3159 if (!pdata) {
3160 pr_err("unable to allocate platform data\n");
3161 return NULL;
3162 }
3163 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3164 if (len) {
3165 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3166 if (!pdata->phy_init_seq)
3167 return NULL;
3168 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3169 pdata->phy_init_seq,
3170 len/sizeof(*pdata->phy_init_seq));
3171 }
3172 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3173 &pdata->power_budget);
3174 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3175 &pdata->mode);
3176 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3177 &pdata->otg_control);
3178 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3179 &pdata->default_mode);
3180 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3181 &pdata->phy_type);
3182 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3183 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303184 return pdata;
3185}
3186
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303187static int __init msm_otg_probe(struct platform_device *pdev)
3188{
3189 int ret = 0;
3190 struct resource *res;
3191 struct msm_otg *motg;
3192 struct otg_transceiver *otg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303193 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303194
3195 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303196
3197 if (pdev->dev.of_node) {
3198 dev_dbg(&pdev->dev, "device tree enabled\n");
3199 pdata = msm_otg_dt_to_pdata(pdev);
3200 if (!pdata)
3201 return -ENOMEM;
3202 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3203 if (ret) {
3204 dev_err(&pdev->dev, "devices setup failed\n");
3205 return ret;
3206 }
3207 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303208 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3209 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303210 } else {
3211 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303212 }
3213
3214 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3215 if (!motg) {
3216 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3217 return -ENOMEM;
3218 }
3219
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003220 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303221 motg->pdata = pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303222 otg = &motg->otg;
3223 otg->dev = &pdev->dev;
3224
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303225 /*
3226 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3227 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3228 * PMIC can detect ACA ID_GND and generate an interrupt.
3229 */
3230 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3231 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3232 ret = -EINVAL;
3233 goto free_motg;
3234 }
3235
Ofir Cohen4da266f2012-01-03 10:19:29 +02003236 /* initialize reset counter */
3237 motg->reset_counter = 0;
3238
Amit Blay02eff132011-09-21 16:46:24 +03003239 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003240 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003241 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003242 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303243
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303244 /*
3245 * Targets on which link uses asynchronous reset methodology,
3246 * free running clock is not required during the reset.
3247 */
Manu Gautam5143b252012-01-05 19:25:23 -08003248 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303249 if (IS_ERR(motg->clk))
3250 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3251 else
3252 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303253
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303254 /* pm qos request to prevent apps idle power collapse */
3255 if (motg->pdata->swfi_latency)
3256 pm_qos_add_request(&motg->pm_qos_req_dma,
3257 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
Manu Gautam5143b252012-01-05 19:25:23 -08003258
Anji jonnala0f73cac2011-05-04 10:19:46 +05303259 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003260 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303261 * CORE CLK must be running at >55Mhz for correct HSUSB
3262 * operation and USB core cannot tolerate frequency changes on
3263 * CORE CLK. For such USB cores, vote for maximum clk frequency
3264 * on pclk source
3265 */
Manu Gautam5143b252012-01-05 19:25:23 -08003266 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3267 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303268 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003269 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303270 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003271 goto put_clk;
3272 }
3273 clk_set_rate(motg->core_clk, INT_MAX);
3274
3275 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3276 if (IS_ERR(motg->pclk)) {
3277 dev_err(&pdev->dev, "failed to get iface_clk\n");
3278 ret = PTR_ERR(motg->pclk);
3279 goto put_core_clk;
3280 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303281
3282 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3283 if (!res) {
3284 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3285 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003286 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303287 }
3288
3289 motg->regs = ioremap(res->start, resource_size(res));
3290 if (!motg->regs) {
3291 dev_err(&pdev->dev, "ioremap failed\n");
3292 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003293 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303294 }
3295 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3296
3297 motg->irq = platform_get_irq(pdev, 0);
3298 if (!motg->irq) {
3299 dev_err(&pdev->dev, "platform_get_irq failed\n");
3300 ret = -ENODEV;
3301 goto free_regs;
3302 }
3303
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003304 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003305 if (IS_ERR(motg->xo_handle)) {
3306 dev_err(&pdev->dev, "%s not able to get the handle "
3307 "to vote for TCXO D0 buffer\n", __func__);
3308 ret = PTR_ERR(motg->xo_handle);
3309 goto free_regs;
3310 }
3311
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003312 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003313 if (ret) {
3314 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3315 "D0 buffer%d\n", __func__, ret);
3316 goto free_xo_handle;
3317 }
3318
Manu Gautam28b1bac2012-01-30 16:43:06 +05303319 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303320
3321 ret = msm_hsusb_init_vddcx(motg, 1);
3322 if (ret) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003323 dev_err(&pdev->dev, "hsusb vddcx init failed\n");
Anji jonnala7da3f262011-12-02 17:22:14 -08003324 goto devote_xo_handle;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303325 }
3326
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003327 ret = msm_hsusb_config_vddcx(1);
3328 if (ret) {
3329 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
3330 goto free_init_vddcx;
3331 }
3332
Anji jonnala11aa5c42011-05-04 10:19:48 +05303333 ret = msm_hsusb_ldo_init(motg, 1);
3334 if (ret) {
3335 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003336 goto free_init_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303337 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003338
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303339 if (pdata->mhl_enable) {
3340 mhl_analog_switch = devm_regulator_get(motg->otg.dev,
3341 "mhl_ext_3p3v");
3342 if (IS_ERR(mhl_analog_switch)) {
3343 dev_err(&pdev->dev, "Unable to get mhl_analog_switch\n");
3344 goto free_ldo_init;
3345 }
3346 }
3347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003348 ret = msm_hsusb_ldo_enable(motg, 1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303349 if (ret) {
3350 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003351 goto free_ldo_init;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303352 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303353 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303354
3355 writel(0, USB_USBINTR);
3356 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003357 /* Ensure that above STOREs are completed before enabling interrupts */
3358 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303359
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003360 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303361 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303362 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303363 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303364 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3365 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303366 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3367 "msm_otg", motg);
3368 if (ret) {
3369 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003370 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303371 }
3372
3373 otg->init = msm_otg_reset;
3374 otg->set_host = msm_otg_set_host;
3375 otg->set_peripheral = msm_otg_set_peripheral;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303376 otg->set_power = msm_otg_set_power;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303377 otg->start_hnp = msm_otg_start_hnp;
3378 otg->start_srp = msm_otg_start_srp;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303379 otg->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303380
3381 otg->io_ops = &msm_otg_io_ops;
3382
3383 ret = otg_set_transceiver(&motg->otg);
3384 if (ret) {
3385 dev_err(&pdev->dev, "otg_set_transceiver failed\n");
3386 goto free_irq;
3387 }
3388
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303389 if (motg->pdata->mode == USB_OTG &&
3390 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003391 if (motg->pdata->pmic_id_irq) {
3392 ret = request_irq(motg->pdata->pmic_id_irq,
3393 msm_pmic_id_irq,
3394 IRQF_TRIGGER_RISING |
3395 IRQF_TRIGGER_FALLING,
3396 "msm_otg", motg);
3397 if (ret) {
3398 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
3399 goto remove_otg;
3400 }
3401 } else {
3402 ret = -ENODEV;
3403 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
3404 goto remove_otg;
3405 }
3406 }
3407
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303408 msm_hsusb_mhl_switch_enable(motg, 1);
3409
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303410 platform_set_drvdata(pdev, motg);
3411 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003412 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303413
Anji jonnalad270e2d2011-08-09 11:28:32 +05303414 ret = msm_otg_debugfs_init(motg);
3415 if (ret)
3416 dev_dbg(&pdev->dev, "mode debugfs file is"
3417 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303418
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003419 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3420 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3421
Amit Blay58b31472011-11-18 09:39:39 +02003422 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3423 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303424 (!(motg->pdata->mode == USB_OTG) ||
3425 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003426 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003427 ALLOW_PHY_RETENTION |
3428 ALLOW_PHY_COMP_DISABLE;
3429
Amit Blay58b31472011-11-18 09:39:39 +02003430 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3431 motg->caps = ALLOW_PHY_RETENTION;
3432 }
3433
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003434 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303435 pm_runtime_set_active(&pdev->dev);
3436 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303437
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303438 if (motg->pdata->bus_scale_table) {
3439 motg->bus_perf_client =
3440 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3441 if (!motg->bus_perf_client)
3442 dev_err(motg->otg.dev, "%s: Failed to register BUS "
3443 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303444 else
3445 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303446 }
3447
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303448 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003449
3450remove_otg:
3451 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303452free_irq:
3453 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003454destroy_wlock:
3455 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303456 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003457 msm_hsusb_ldo_enable(motg, 0);
3458free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303459 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003460free_init_vddcx:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303461 msm_hsusb_init_vddcx(motg, 0);
Anji jonnala7da3f262011-12-02 17:22:14 -08003462devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303463 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003464 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003465free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003466 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303467free_regs:
3468 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003469put_pclk:
3470 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303471put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003472 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303473put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303474 if (!IS_ERR(motg->clk))
3475 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003476 if (!IS_ERR(motg->phy_reset_clk))
3477 clk_put(motg->phy_reset_clk);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303478free_motg:
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303479 if (motg->pdata->swfi_latency)
3480 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303481 kfree(motg);
3482 return ret;
3483}
3484
3485static int __devexit msm_otg_remove(struct platform_device *pdev)
3486{
3487 struct msm_otg *motg = platform_get_drvdata(pdev);
3488 struct otg_transceiver *otg = &motg->otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303489 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303490
3491 if (otg->host || otg->gadget)
3492 return -EBUSY;
3493
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303494 if (pdev->dev.of_node)
3495 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003496 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3497 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303498 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303499 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303500 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303501
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303502 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303503
3504 device_init_wakeup(&pdev->dev, 0);
3505 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003506 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303507
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303508 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003509 if (motg->pdata->pmic_id_irq)
3510 free_irq(motg->pdata->pmic_id_irq, motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303511 otg_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303512 free_irq(motg->irq, motg);
3513
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303514 /*
3515 * Put PHY in low power mode.
3516 */
3517 ulpi_read(otg, 0x14);
3518 ulpi_write(otg, 0x08, 0x09);
3519
3520 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3521 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3522 if (readl(USB_PORTSC) & PORTSC_PHCD)
3523 break;
3524 udelay(1);
3525 cnt++;
3526 }
3527 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
3528 dev_err(otg->dev, "Unable to suspend PHY\n");
3529
Manu Gautam28b1bac2012-01-30 16:43:06 +05303530 clk_disable_unprepare(motg->pclk);
3531 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003532 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003533 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303534 msm_hsusb_ldo_init(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003535 msm_hsusb_init_vddcx(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303536
3537 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303538 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303539
Amit Blay02eff132011-09-21 16:46:24 +03003540 if (!IS_ERR(motg->phy_reset_clk))
3541 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303542 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303543 if (!IS_ERR(motg->clk))
3544 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003545 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303546
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303547 if (motg->pdata->swfi_latency)
3548 pm_qos_remove_request(&motg->pm_qos_req_dma);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303549
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303550 if (motg->bus_perf_client)
3551 msm_bus_scale_unregister_client(motg->bus_perf_client);
3552
Anji jonnalaa7c1c5c2011-12-12 12:20:36 +05303553 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303554 return 0;
3555}
3556
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303557#ifdef CONFIG_PM_RUNTIME
3558static int msm_otg_runtime_idle(struct device *dev)
3559{
3560 struct msm_otg *motg = dev_get_drvdata(dev);
3561 struct otg_transceiver *otg = &motg->otg;
3562
3563 dev_dbg(dev, "OTG runtime idle\n");
3564
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303565 if (otg->state == OTG_STATE_UNDEFINED)
3566 return -EAGAIN;
3567 else
3568 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303569}
3570
3571static int msm_otg_runtime_suspend(struct device *dev)
3572{
3573 struct msm_otg *motg = dev_get_drvdata(dev);
3574
3575 dev_dbg(dev, "OTG runtime suspend\n");
3576 return msm_otg_suspend(motg);
3577}
3578
3579static int msm_otg_runtime_resume(struct device *dev)
3580{
3581 struct msm_otg *motg = dev_get_drvdata(dev);
3582
3583 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303584 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303585 return msm_otg_resume(motg);
3586}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303587#endif
3588
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303589#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303590static int msm_otg_pm_suspend(struct device *dev)
3591{
Jack Phamd44455a2012-04-10 16:23:49 -07003592 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303593
3594 dev_dbg(dev, "OTG PM suspend\n");
Jack Phamd44455a2012-04-10 16:23:49 -07003595 return msm_otg_suspend(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303596}
3597
3598static int msm_otg_pm_resume(struct device *dev)
3599{
3600 struct msm_otg *motg = dev_get_drvdata(dev);
Jack Phamd44455a2012-04-10 16:23:49 -07003601 int ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303602
3603 dev_dbg(dev, "OTG PM resume\n");
Jack Phamd44455a2012-04-10 16:23:49 -07003604 ret = msm_otg_resume(motg);
3605 if (ret)
3606 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303607
Jack Phamd44455a2012-04-10 16:23:49 -07003608 /* Update runtime PM status */
3609 pm_runtime_disable(dev);
3610 pm_runtime_set_active(dev);
3611 pm_runtime_enable(dev);
3612
Gregory Beanebd8ca22011-10-11 12:02:35 -07003613 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303614}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303615#endif
3616
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303617#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303618static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303619 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3620 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3621 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303622};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303623#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303624
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303625static struct of_device_id msm_otg_dt_match[] = {
3626 { .compatible = "qcom,hsusb-otg",
3627 },
3628 {}
3629};
3630
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303631static struct platform_driver msm_otg_driver = {
3632 .remove = __devexit_p(msm_otg_remove),
3633 .driver = {
3634 .name = DRIVER_NAME,
3635 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303636#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303637 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303638#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303639 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303640 },
3641};
3642
3643static int __init msm_otg_init(void)
3644{
3645 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3646}
3647
3648static void __exit msm_otg_exit(void)
3649{
3650 platform_driver_unregister(&msm_otg_driver);
3651}
3652
3653module_init(msm_otg_init);
3654module_exit(msm_otg_exit);
3655
3656MODULE_LICENSE("GPL v2");
3657MODULE_DESCRIPTION("MSM USB transceiver driver");