blob: f5bad1947778450e4977a05c1373984d17246fde [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>
Amit Blay0f7edf72012-01-15 10:11:27 +020042#include <linux/power_supply.h>
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053043#include <linux/mhl_8334.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053044
45#include <mach/clk.h>
Jack Pham87f202f2012-08-06 00:24:22 -070046#include <mach/mpm.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080047#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053048#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053049#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053050
51#define MSM_USB_BASE (motg->regs)
52#define DRIVER_NAME "msm_otg"
53
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053054#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053055#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053056#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
57#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
58#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
59#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
60
61#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
62#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
63#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
64#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
65
Mayank Rana248698c2012-04-19 00:03:16 +053066#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080067#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053068#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
69
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053070static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070071static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053072static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053073static bool debug_bus_voting_enabled;
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +053074static bool mhl_det_in_progress;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075
Anji jonnala11aa5c42011-05-04 10:19:48 +053076static struct regulator *hsusb_3p3;
77static struct regulator *hsusb_1p8;
78static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053079static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053080static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053081static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053082
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053083static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053084static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053085{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053086#ifdef CONFIG_USB_MSM_ACA
87 return true;
88#else
89 return debug_aca_enabled;
90#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053091}
92
Mayank Rana248698c2012-04-19 00:03:16 +053093static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
94 { /* VDD_CX CORNER Voting */
95 [VDD_NONE] = RPM_VREG_CORNER_NONE,
96 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
97 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
98 },
99 { /* VDD_CX Voltage Voting */
100 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
101 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
102 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
103 },
104};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530105
106static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
107{
108 int rc = 0;
109
110 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700111 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530112 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200113 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530114 return PTR_ERR(hsusb_3p3);
115 }
116
117 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
118 USB_PHY_3P3_VOL_MAX);
119 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700120 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530122 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530123 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700124 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530125 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200126 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530127 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530129 }
130 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
131 USB_PHY_1P8_VOL_MAX);
132 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700133 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700134 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530135 goto put_1p8;
136 }
137
138 return 0;
139 }
140
Anji jonnala11aa5c42011-05-04 10:19:48 +0530141put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700142 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143put_3p3_lpm:
144 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530145 return rc;
146}
147
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530148static int msm_hsusb_config_vddcx(int high)
149{
Mayank Rana248698c2012-04-19 00:03:16 +0530150 struct msm_otg *motg = the_msm_otg;
151 enum usb_vdd_type vdd_type = motg->vdd_type;
152 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530153 int min_vol;
154 int ret;
155
Mayank Rana248698c2012-04-19 00:03:16 +0530156 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530157 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
158 if (ret) {
159 pr_err("%s: unable to set the voltage for regulator "
160 "HSUSB_VDDCX\n", __func__);
161 return ret;
162 }
163
164 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
165
166 return ret;
167}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530168
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530170{
171 int ret = 0;
172
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530173 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530174 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
175 return -ENODEV;
176 }
177
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530178 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530179 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
180 return -ENODEV;
181 }
182
183 if (on) {
184 ret = regulator_set_optimum_mode(hsusb_1p8,
185 USB_PHY_1P8_HPM_LOAD);
186 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530188 "HSUSB_1p8\n", __func__);
189 return ret;
190 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191
192 ret = regulator_enable(hsusb_1p8);
193 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700194 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700195 __func__);
196 regulator_set_optimum_mode(hsusb_1p8, 0);
197 return ret;
198 }
199
Anji jonnala11aa5c42011-05-04 10:19:48 +0530200 ret = regulator_set_optimum_mode(hsusb_3p3,
201 USB_PHY_3P3_HPM_LOAD);
202 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700203 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530204 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 regulator_set_optimum_mode(hsusb_1p8, 0);
206 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530207 return ret;
208 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209
210 ret = regulator_enable(hsusb_3p3);
211 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700212 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 __func__);
214 regulator_set_optimum_mode(hsusb_3p3, 0);
215 regulator_set_optimum_mode(hsusb_1p8, 0);
216 regulator_disable(hsusb_1p8);
217 return ret;
218 }
219
Anji jonnala11aa5c42011-05-04 10:19:48 +0530220 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 ret = regulator_disable(hsusb_1p8);
222 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700223 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224 __func__);
225 return ret;
226 }
227
228 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530229 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530231 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700232
233 ret = regulator_disable(hsusb_3p3);
234 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700235 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700236 __func__);
237 return ret;
238 }
239 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530240 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700241 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530242 "HSUSB_3p3\n", __func__);
243 }
244
245 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
246 return ret < 0 ? ret : 0;
247}
248
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530249static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
250{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530251 struct msm_otg_platform_data *pdata = motg->pdata;
252
253 if (!pdata->mhl_enable)
254 return;
255
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530256 if (!mhl_usb_hs_switch) {
257 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530258 return;
259 }
260
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530261 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530262 if (regulator_enable(mhl_usb_hs_switch))
263 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530264 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530265 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530266 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530267}
268
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200269static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530270{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200271 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530272 int cnt = 0;
273
274 /* initiate read operation */
275 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
276 USB_ULPI_VIEWPORT);
277
278 /* wait for completion */
279 while (cnt < ULPI_IO_TIMEOUT_USEC) {
280 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
281 break;
282 udelay(1);
283 cnt++;
284 }
285
286 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200287 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530288 readl(USB_ULPI_VIEWPORT));
289 return -ETIMEDOUT;
290 }
291 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
292}
293
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200294static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530295{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200296 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530297 int cnt = 0;
298
299 /* initiate write operation */
300 writel(ULPI_RUN | ULPI_WRITE |
301 ULPI_ADDR(reg) | ULPI_DATA(val),
302 USB_ULPI_VIEWPORT);
303
304 /* wait for completion */
305 while (cnt < ULPI_IO_TIMEOUT_USEC) {
306 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
307 break;
308 udelay(1);
309 cnt++;
310 }
311
312 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200313 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530314 return -ETIMEDOUT;
315 }
316 return 0;
317}
318
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200319static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530320 .read = ulpi_read,
321 .write = ulpi_write,
322};
323
324static void ulpi_init(struct msm_otg *motg)
325{
326 struct msm_otg_platform_data *pdata = motg->pdata;
327 int *seq = pdata->phy_init_seq;
328
329 if (!seq)
330 return;
331
332 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200333 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530334 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200335 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530336 seq += 2;
337 }
338}
339
340static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
341{
342 int ret;
343
344 if (assert) {
Manu Gautam5025ff12012-07-20 10:56:50 +0530345 if (!IS_ERR(motg->clk)) {
346 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
347 } else {
348 /* Using asynchronous block reset to the hardware */
349 dev_dbg(motg->phy.dev, "block_reset ASSERT\n");
350 clk_disable_unprepare(motg->pclk);
351 clk_disable_unprepare(motg->core_clk);
352 ret = clk_reset(motg->core_clk, CLK_RESET_ASSERT);
353 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530354 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200355 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530356 } else {
Manu Gautam5025ff12012-07-20 10:56:50 +0530357 if (!IS_ERR(motg->clk)) {
358 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
359 } else {
360 dev_dbg(motg->phy.dev, "block_reset DEASSERT\n");
361 ret = clk_reset(motg->core_clk, CLK_RESET_DEASSERT);
362 ndelay(200);
363 clk_prepare_enable(motg->core_clk);
364 clk_prepare_enable(motg->pclk);
365 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200367 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530368 }
369 return ret;
370}
371
372static int msm_otg_phy_clk_reset(struct msm_otg *motg)
373{
374 int ret;
375
Amit Blay02eff132011-09-21 16:46:24 +0300376 if (IS_ERR(motg->phy_reset_clk))
377 return 0;
378
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530379 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
380 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200381 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530382 return ret;
383 }
384 usleep_range(10000, 12000);
385 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
386 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200387 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530388 return ret;
389}
390
391static int msm_otg_phy_reset(struct msm_otg *motg)
392{
393 u32 val;
394 int ret;
395 int retries;
396
397 ret = msm_otg_link_clk_reset(motg, 1);
398 if (ret)
399 return ret;
400 ret = msm_otg_phy_clk_reset(motg);
401 if (ret)
402 return ret;
403 ret = msm_otg_link_clk_reset(motg, 0);
404 if (ret)
405 return ret;
406
407 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
408 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
409
410 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200411 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530412 ULPI_CLR(ULPI_FUNC_CTRL));
413 if (!ret)
414 break;
415 ret = msm_otg_phy_clk_reset(motg);
416 if (ret)
417 return ret;
418 }
419 if (!retries)
420 return -ETIMEDOUT;
421
422 /* This reset calibrates the phy, if the above write succeeded */
423 ret = msm_otg_phy_clk_reset(motg);
424 if (ret)
425 return ret;
426
427 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200428 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530429 if (ret != -ETIMEDOUT)
430 break;
431 ret = msm_otg_phy_clk_reset(motg);
432 if (ret)
433 return ret;
434 }
435 if (!retries)
436 return -ETIMEDOUT;
437
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200438 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530439 return 0;
440}
441
442#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530443static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530444{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530446
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530447 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530448 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530449 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530450 break;
451 udelay(1);
452 cnt++;
453 }
454 if (cnt >= LINK_RESET_TIMEOUT_USEC)
455 return -ETIMEDOUT;
456
457 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530458 writel_relaxed(0x80000000, USB_PORTSC);
459 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530460 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530461
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530462 return 0;
463}
464
Steve Mucklef132c6c2012-06-06 18:30:57 -0700465static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530466{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700467 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530468 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530469 int ret;
470 u32 val = 0;
471 u32 ulpi_val = 0;
472
Ofir Cohen4da266f2012-01-03 10:19:29 +0200473 /*
474 * USB PHY and Link reset also reset the USB BAM.
475 * Thus perform reset operation only once to avoid
476 * USB BAM reset on other cases e.g. USB cable disconnections.
477 */
478 if (pdata->disable_reset_on_disconnect) {
479 if (motg->reset_counter)
480 return 0;
481 else
482 motg->reset_counter++;
483 }
484
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530485 if (!IS_ERR(motg->clk))
486 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530487 ret = msm_otg_phy_reset(motg);
488 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700489 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530490 return ret;
491 }
492
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530493 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530494 ret = msm_otg_link_reset(motg);
495 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700496 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530497 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530498 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530499 msleep(100);
500
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530501 ulpi_init(motg);
502
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700503 /* Ensure that RESET operation is completed before turning off clock */
504 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530505
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530506 if (!IS_ERR(motg->clk))
507 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530508
509 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530510 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530511 if (pdata->mode == USB_OTG) {
512 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
513 val |= OTGSC_IDIE | OTGSC_BSVIE;
514 } else if (pdata->mode == USB_PERIPHERAL) {
515 ulpi_val = ULPI_INT_SESS_VALID;
516 val |= OTGSC_BSVIE;
517 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530518 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200519 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
520 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530521 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700522 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530523 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530524 /* Enable PMIC pull-up */
525 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530526 }
527
528 return 0;
529}
530
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530531static const char *timer_string(int bit)
532{
533 switch (bit) {
534 case A_WAIT_VRISE: return "a_wait_vrise";
535 case A_WAIT_VFALL: return "a_wait_vfall";
536 case B_SRP_FAIL: return "b_srp_fail";
537 case A_WAIT_BCON: return "a_wait_bcon";
538 case A_AIDL_BDIS: return "a_aidl_bdis";
539 case A_BIDL_ADIS: return "a_bidl_adis";
540 case B_ASE0_BRST: return "b_ase0_brst";
541 case A_TST_MAINT: return "a_tst_maint";
542 case B_TST_SRP: return "b_tst_srp";
543 case B_TST_CONFIG: return "b_tst_config";
544 default: return "UNDEFINED";
545 }
546}
547
548static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
549{
550 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
551
552 switch (motg->active_tmout) {
553 case A_WAIT_VRISE:
554 /* TODO: use vbus_vld interrupt */
555 set_bit(A_VBUS_VLD, &motg->inputs);
556 break;
557 case A_TST_MAINT:
558 /* OTG PET: End session after TA_TST_MAINT */
559 set_bit(A_BUS_DROP, &motg->inputs);
560 break;
561 case B_TST_SRP:
562 /*
563 * OTG PET: Initiate SRP after TB_TST_SRP of
564 * previous session end.
565 */
566 set_bit(B_BUS_REQ, &motg->inputs);
567 break;
568 case B_TST_CONFIG:
569 clear_bit(A_CONN, &motg->inputs);
570 break;
571 default:
572 set_bit(motg->active_tmout, &motg->tmouts);
573 }
574
575 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
576 queue_work(system_nrt_wq, &motg->sm_work);
577 return HRTIMER_NORESTART;
578}
579
580static void msm_otg_del_timer(struct msm_otg *motg)
581{
582 int bit = motg->active_tmout;
583
584 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
585 div_s64(ktime_to_us(hrtimer_get_remaining(
586 &motg->timer)), 1000));
587 hrtimer_cancel(&motg->timer);
588 clear_bit(bit, &motg->tmouts);
589}
590
591static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
592{
593 clear_bit(bit, &motg->tmouts);
594 motg->active_tmout = bit;
595 pr_debug("starting %s timer\n", timer_string(bit));
596 hrtimer_start(&motg->timer,
597 ktime_set(time / 1000, (time % 1000) * 1000000),
598 HRTIMER_MODE_REL);
599}
600
601static void msm_otg_init_timer(struct msm_otg *motg)
602{
603 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
604 motg->timer.function = msm_otg_timer_func;
605}
606
Steve Mucklef132c6c2012-06-06 18:30:57 -0700607static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530608{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700609 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530610
Steve Mucklef132c6c2012-06-06 18:30:57 -0700611 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530612 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700613 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530614 return -EINVAL;
615 }
616
617 pr_debug("A-Host: HNP initiated\n");
618 clear_bit(A_BUS_REQ, &motg->inputs);
619 queue_work(system_nrt_wq, &motg->sm_work);
620 return 0;
621}
622
Steve Mucklef132c6c2012-06-06 18:30:57 -0700623static int msm_otg_start_srp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530624{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700625 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530626 u32 val;
627 int ret = 0;
628
Steve Mucklef132c6c2012-06-06 18:30:57 -0700629 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530630 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700631 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530632 ret = -EINVAL;
633 goto out;
634 }
635
636 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
637 pr_debug("initial conditions of SRP are not met. Try again"
638 "after some time\n");
639 ret = -EAGAIN;
640 goto out;
641 }
642
643 pr_debug("B-Device SRP started\n");
644
645 /*
646 * PHY won't pull D+ high unless it detects Vbus valid.
647 * Since by definition, SRP is only done when Vbus is not valid,
648 * software work-around needs to be used to spoof the PHY into
649 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
650 * VBUSVLDEXT register bits.
651 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700652 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530653 /*
654 * Harware auto assist data pulsing: Data pulse is given
655 * for 7msec; wait for vbus
656 */
657 val = readl_relaxed(USB_OTGSC);
658 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
659
660 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
661out:
662 return ret;
663}
664
Steve Mucklef132c6c2012-06-06 18:30:57 -0700665static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530666{
667 struct usb_hcd *hcd = bus_to_hcd(otg->host);
668 struct usb_device *rhub = otg->host->root_hub;
669
670 if (enable) {
671 pm_runtime_disable(&rhub->dev);
672 rhub->state = USB_STATE_NOTATTACHED;
673 hcd->driver->bus_suspend(hcd);
674 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
675 } else {
676 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700677 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530678 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
679 }
680}
681
Steve Mucklef132c6c2012-06-06 18:30:57 -0700682static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530683{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700684 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530685
Amit Blay6fa647a2012-05-24 14:12:08 +0300686 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530687 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530688
Jack Pham69e621d2012-06-25 18:48:07 -0700689 if (atomic_read(&motg->in_lpm) == suspend)
690 return 0;
691
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530692 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700693 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530694 case OTG_STATE_A_WAIT_BCON:
695 if (TA_WAIT_BCON > 0)
696 break;
697 /* fall through */
698 case OTG_STATE_A_HOST:
699 pr_debug("host bus suspend\n");
700 clear_bit(A_BUS_REQ, &motg->inputs);
701 queue_work(system_nrt_wq, &motg->sm_work);
702 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300703 case OTG_STATE_B_PERIPHERAL:
704 pr_debug("peripheral bus suspend\n");
705 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
706 break;
707 set_bit(A_BUS_SUSPEND, &motg->inputs);
708 queue_work(system_nrt_wq, &motg->sm_work);
709 break;
710
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530711 default:
712 break;
713 }
714 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700715 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530716 case OTG_STATE_A_SUSPEND:
717 /* Remote wakeup or resume */
718 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700719 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700720
721 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700722 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530723 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300724 case OTG_STATE_B_PERIPHERAL:
725 pr_debug("peripheral bus resume\n");
726 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
727 break;
728 clear_bit(A_BUS_SUSPEND, &motg->inputs);
729 queue_work(system_nrt_wq, &motg->sm_work);
730 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530731 default:
732 break;
733 }
734 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530735 return 0;
736}
737
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530738#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530739#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
740
741#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530742static int msm_otg_suspend(struct msm_otg *motg)
743{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200744 struct usb_phy *phy = &motg->phy;
745 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530746 struct msm_otg_platform_data *pdata = motg->pdata;
747 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300748 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530749 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800750 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530751 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530752
753 if (atomic_read(&motg->in_lpm))
754 return 0;
755
756 disable_irq(motg->irq);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +0530757 host_bus_suspend = !test_bit(MHL, &motg->inputs) && phy->otg->host &&
758 !test_bit(ID, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700759 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300760 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
761 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530762 dcp = motg->chg_type == USB_DCP_CHARGER;
Jack Pham502bea32012-08-13 15:34:20 -0700763
764 /* charging detection in progress due to cable plug-in */
765 if (test_bit(B_SESS_VLD, &motg->inputs) && !device_bus_suspend &&
766 !dcp) {
767 enable_irq(motg->irq);
768 return -EBUSY;
769 }
770
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530771 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530772 * Chipidea 45-nm PHY suspend sequence:
773 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530774 * Interrupt Latch Register auto-clear feature is not present
775 * in all PHY versions. Latch register is clear on read type.
776 * Clear latch register to avoid spurious wakeup from
777 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530778 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530779 * PHY comparators are disabled when PHY enters into low power
780 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
781 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
782 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530783 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530784 * PLL is not turned off when PHY enters into low power mode (LPM).
785 * Disable PLL for maximum power savings.
786 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530787
788 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200789 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530790 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200791 ulpi_write(phy, 0x01, 0x30);
792 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530793 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530794
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700795
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530796 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530797 * PHY may take some time or even fail to enter into low power
798 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
799 * in failure case.
800 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530801 portsc = readl_relaxed(USB_PORTSC);
802 if (!(portsc & PORTSC_PHCD)) {
803 writel_relaxed(portsc | PORTSC_PHCD,
804 USB_PORTSC);
805 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
806 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
807 break;
808 udelay(1);
809 cnt++;
810 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530811 }
812
813 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200814 dev_err(phy->dev, "Unable to suspend PHY\n");
815 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530816 enable_irq(motg->irq);
817 return -ETIMEDOUT;
818 }
819
820 /*
821 * PHY has capability to generate interrupt asynchronously in low
822 * power mode (LPM). This interrupt is level triggered. So USB IRQ
823 * line must be disabled till async interrupt enable bit is cleared
824 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
825 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530826 *
827 * PHY retention mode is disallowed while entering to LPM with wall
828 * charger connected. But PHY is put into suspend mode. Hence
829 * enable asynchronous interrupt to detect charger disconnection when
830 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530832 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300833 if (host_bus_suspend || device_bus_suspend ||
834 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530835 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
836 else
837 cmd_val |= ULPI_STP_CTRL;
838 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530839
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530840 /*
841 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
842 * PHY retention and collapse can not happen with VDP_SRC enabled.
843 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300844 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
845 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200846 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
847 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
848 /* Enable PHY HV interrupts to wake MPM/Link */
849 phy_ctrl_val |=
850 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530851
Amit Blay58b31472011-11-18 09:39:39 +0200852 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700853 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530854 }
855
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856 /* Ensure that above operation is completed before turning off clocks */
857 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300858 /* Consider clocks on workaround flag only in case of bus suspend */
859 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
860 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
861 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300862 clk_disable_unprepare(motg->pclk);
863 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300864 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300865 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530866
Anji jonnala7da3f262011-12-02 17:22:14 -0800867 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530868 if (!host_bus_suspend) {
869 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
870 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700871 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530872 "TCXO D0 buffer%d\n", __func__, ret);
873 else
874 motg->lpm_flags |= XO_SHUTDOWN;
875 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800876
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530877 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
878 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879 msm_hsusb_ldo_enable(motg, 0);
880 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530881 }
882
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530883 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530885 msm_hsusb_mhl_switch_enable(motg, 0);
886 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
Steve Mucklef132c6c2012-06-06 18:30:57 -0700888 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530889 enable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700890 if (motg->async_irq)
891 enable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892 if (motg->pdata->pmic_id_irq)
893 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700894 if (pdata->otg_control == OTG_PHY_CONTROL &&
895 pdata->mpm_otgsessvld_int)
896 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530898 if (bus)
899 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
900
901 atomic_set(&motg->in_lpm, 1);
Manu Gautamf8c45642012-08-10 10:20:56 -0700902 /* Enable ASYNC IRQ (if present) during LPM */
903 if (motg->async_irq)
904 enable_irq(motg->async_irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530905 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530907
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200908 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909
910 return 0;
911}
912
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530913static int msm_otg_resume(struct msm_otg *motg)
914{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200915 struct usb_phy *phy = &motg->phy;
916 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700917 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530918 int cnt = 0;
919 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200920 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800921 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922
923 if (!atomic_read(&motg->in_lpm))
924 return 0;
925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530927
Anji jonnala7da3f262011-12-02 17:22:14 -0800928 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530929 if (motg->lpm_flags & XO_SHUTDOWN) {
930 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
931 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700932 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530933 "TCXO D0 buffer%d\n", __func__, ret);
934 motg->lpm_flags &= ~XO_SHUTDOWN;
935 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530936
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300937 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300938 clk_prepare_enable(motg->core_clk);
939 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300940 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300941 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530942
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
944 msm_hsusb_ldo_enable(motg, 1);
945 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
946 }
947
948 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530949 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530950 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200951 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
952 phy_ctrl_val |= PHY_RETEN;
953 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
954 /* Disable PHY HV interrupts */
955 phy_ctrl_val &=
956 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
957 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530959 }
960
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530961 temp = readl(USB_USBCMD);
962 temp &= ~ASYNC_INTR_CTRL;
963 temp &= ~ULPI_STP_CTRL;
964 writel(temp, USB_USBCMD);
965
966 /*
967 * PHY comes out of low power mode (LPM) in case of wakeup
968 * from asynchronous interrupt.
969 */
970 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
971 goto skip_phy_resume;
972
973 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
974 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
975 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
976 break;
977 udelay(1);
978 cnt++;
979 }
980
981 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
982 /*
983 * This is a fatal error. Reset the link and
984 * PHY. USB state can not be restored. Re-insertion
985 * of USB cable is the only way to get USB working.
986 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200987 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530988 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200989 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530990 }
991
992skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700993 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530994 disable_irq_wake(motg->irq);
Manu Gautamf8c45642012-08-10 10:20:56 -0700995 if (motg->async_irq)
996 disable_irq_wake(motg->async_irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700997 if (motg->pdata->pmic_id_irq)
998 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700999 if (pdata->otg_control == OTG_PHY_CONTROL &&
1000 pdata->mpm_otgsessvld_int)
1001 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001002 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301003 if (bus)
1004 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
1005
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +05301006 atomic_set(&motg->in_lpm, 0);
1007
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301008 if (motg->async_int) {
Manu Gautamf8c45642012-08-10 10:20:56 -07001009 /* Match the disable_irq call from ISR */
1010 enable_irq(motg->async_int);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301011 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301012 }
1013
Manu Gautamf8c45642012-08-10 10:20:56 -07001014 /* If ASYNC IRQ is present then keep it enabled only during LPM */
1015 if (motg->async_irq)
1016 disable_irq(motg->async_irq);
1017
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001018 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301019
1020 return 0;
1021}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301022#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301023
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001024static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1025{
1026 if (!psy)
1027 goto psy_not_supported;
1028
1029 if (host_mode)
1030 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1031 else
1032 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1033
1034psy_not_supported:
1035 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1036 return -ENXIO;
1037}
1038
David Keitel081a3e22012-04-18 12:37:07 -07001039static int msm_otg_notify_chg_type(struct msm_otg *motg)
1040{
1041 static int charger_type;
1042 /*
1043 * TODO
1044 * Unify OTG driver charger types and power supply charger types
1045 */
1046 if (charger_type == motg->chg_type)
1047 return 0;
1048
1049 if (motg->chg_type == USB_SDP_CHARGER)
1050 charger_type = POWER_SUPPLY_TYPE_USB;
1051 else if (motg->chg_type == USB_CDP_CHARGER)
1052 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301053 else if (motg->chg_type == USB_DCP_CHARGER ||
1054 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001055 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1056 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1057 motg->chg_type == USB_ACA_A_CHARGER ||
1058 motg->chg_type == USB_ACA_B_CHARGER ||
1059 motg->chg_type == USB_ACA_C_CHARGER))
1060 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1061 else
1062 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1063
1064 return pm8921_set_usb_power_supply_type(charger_type);
1065}
1066
Amit Blay0f7edf72012-01-15 10:11:27 +02001067static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1068{
Amit Blay0f7edf72012-01-15 10:11:27 +02001069
Amit Blay0f7edf72012-01-15 10:11:27 +02001070 if (!psy)
1071 goto psy_not_supported;
1072
1073 if (motg->cur_power == 0 && mA > 0) {
1074 /* Enable charging */
1075 if (power_supply_set_online(psy, true))
1076 goto psy_not_supported;
1077 } else if (motg->cur_power > 0 && mA == 0) {
1078 /* Disable charging */
1079 if (power_supply_set_online(psy, false))
1080 goto psy_not_supported;
1081 return 0;
1082 }
1083 /* Set max current limit */
1084 if (power_supply_set_current_limit(psy, 1000*mA))
1085 goto psy_not_supported;
1086
1087 return 0;
1088
1089psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001090 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001091 return -ENXIO;
1092}
1093
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301094static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1095{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001096 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301097
1098 if (g && g->is_a_peripheral)
1099 return;
1100
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301101 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1102 motg->chg_type == USB_ACA_A_CHARGER ||
1103 motg->chg_type == USB_ACA_B_CHARGER ||
1104 motg->chg_type == USB_ACA_C_CHARGER) &&
1105 mA > IDEV_ACA_CHG_LIMIT)
1106 mA = IDEV_ACA_CHG_LIMIT;
1107
David Keitel081a3e22012-04-18 12:37:07 -07001108 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001109 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001110 "Failed notifying %d charger type to PMIC\n",
1111 motg->chg_type);
1112
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301113 if (motg->cur_power == mA)
1114 return;
1115
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001116 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001117
1118 /*
1119 * Use Power Supply API if supported, otherwise fallback
1120 * to legacy pm8921 API.
1121 */
1122 if (msm_otg_notify_power_supply(motg, mA))
1123 pm8921_charger_vbus_draw(mA);
1124
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301125 motg->cur_power = mA;
1126}
1127
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001128static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301129{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001130 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301131
1132 /*
1133 * Gadget driver uses set_power method to notify about the
1134 * available current based on suspend/configured states.
1135 *
1136 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1137 * states when CDP/ACA is connected.
1138 */
1139 if (motg->chg_type == USB_SDP_CHARGER)
1140 msm_otg_notify_charger(motg, mA);
1141
1142 return 0;
1143}
1144
Steve Mucklef132c6c2012-06-06 18:30:57 -07001145static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301146{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001147 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301148 struct msm_otg_platform_data *pdata = motg->pdata;
1149 struct usb_hcd *hcd;
1150
1151 if (!otg->host)
1152 return;
1153
1154 hcd = bus_to_hcd(otg->host);
1155
1156 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001157 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301158
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301159 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001160 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301161 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1162
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301163 /*
1164 * Some boards have a switch cotrolled by gpio
1165 * to enable/disable internal HUB. Enable internal
1166 * HUB before kicking the host.
1167 */
1168 if (pdata->setup_gpio)
1169 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301170 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301171 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001172 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301173
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301174 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301175 /* HCD core reset all bits of PORTSC. select ULPI phy */
1176 writel_relaxed(0x80000000, USB_PORTSC);
1177
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301178 if (pdata->setup_gpio)
1179 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301180
1181 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001182 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301183 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301184 }
1185}
1186
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187static int msm_otg_usbdev_notify(struct notifier_block *self,
1188 unsigned long action, void *priv)
1189{
1190 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001191 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301192 struct usb_device *udev = priv;
1193
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301194 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1195 goto out;
1196
Steve Mucklef132c6c2012-06-06 18:30:57 -07001197 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301198 goto out;
1199 /*
1200 * Interested in devices connected directly to the root hub.
1201 * ACA dock can supply IDEV_CHG irrespective devices connected
1202 * on the accessory port.
1203 */
1204 if (!udev->parent || udev->parent->parent ||
1205 motg->chg_type == USB_ACA_DOCK_CHARGER)
1206 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207
1208 switch (action) {
1209 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301210 if (aca_enabled())
1211 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001212 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301213 pr_debug("B_CONN set\n");
1214 set_bit(B_CONN, &motg->inputs);
1215 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001216 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301217 /*
1218 * OTG PET: A-device must end session within
1219 * 10 sec after PET enumeration.
1220 */
1221 if (udev->quirks & USB_QUIRK_OTG_PET)
1222 msm_otg_start_timer(motg, TA_TST_MAINT,
1223 A_TST_MAINT);
1224 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301225 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 if (udev->actconfig)
1228 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1229 else
1230 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001231 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301232 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301233 break;
1234 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001235 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1236 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301237 pr_debug("B_CONN clear\n");
1238 clear_bit(B_CONN, &motg->inputs);
1239 /*
1240 * OTG PET: A-device must end session after
1241 * PET disconnection if it is enumerated
1242 * with bcdDevice[0] = 1. USB core sets
1243 * bus->otg_vbus_off for us. clear it here.
1244 */
1245 if (udev->bus->otg_vbus_off) {
1246 udev->bus->otg_vbus_off = 0;
1247 set_bit(A_BUS_DROP, &motg->inputs);
1248 }
1249 queue_work(system_nrt_wq, &motg->sm_work);
1250 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001251 default:
1252 break;
1253 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301254 if (test_bit(ID_A, &motg->inputs))
1255 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1256 motg->mA_port);
1257out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258 return NOTIFY_OK;
1259}
1260
Mayank Ranae3926882011-12-26 09:47:54 +05301261static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1262{
1263 int ret;
1264 static bool vbus_is_on;
1265
1266 if (vbus_is_on == on)
1267 return;
1268
1269 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301270 ret = motg->pdata->vbus_power(on);
1271 if (!ret)
1272 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301273 return;
1274 }
1275
1276 if (!vbus_otg) {
1277 pr_err("vbus_otg is NULL.");
1278 return;
1279 }
1280
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001281 /*
1282 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001283 * from usb before turning on the boost.
1284 * if exiting host mode disable the boost before enabling to draw
1285 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001286 */
Mayank Ranae3926882011-12-26 09:47:54 +05301287 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001288 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301289 ret = regulator_enable(vbus_otg);
1290 if (ret) {
1291 pr_err("unable to enable vbus_otg\n");
1292 return;
1293 }
1294 vbus_is_on = true;
1295 } else {
1296 ret = regulator_disable(vbus_otg);
1297 if (ret) {
1298 pr_err("unable to disable vbus_otg\n");
1299 return;
1300 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001301 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301302 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303 }
1304}
1305
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001306static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001308 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301309 struct usb_hcd *hcd;
1310
1311 /*
1312 * Fail host registration if this board can support
1313 * only peripheral configuration.
1314 */
1315 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001316 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301317 return -ENODEV;
1318 }
1319
Mayank Ranae3926882011-12-26 09:47:54 +05301320 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001321 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301322 if (IS_ERR(vbus_otg)) {
1323 pr_err("Unable to get vbus_otg\n");
1324 return -ENODEV;
1325 }
1326 }
1327
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301328 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001329 if (otg->phy->state == OTG_STATE_A_HOST) {
1330 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001331 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301332 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301333 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001335 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301336 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301337 } else {
1338 otg->host = NULL;
1339 }
1340
1341 return 0;
1342 }
1343
1344 hcd = bus_to_hcd(host);
1345 hcd->power_budget = motg->pdata->power_budget;
1346
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301347#ifdef CONFIG_USB_OTG
1348 host->otg_port = 1;
1349#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1351 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301352 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001353 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301354
1355 /*
1356 * Kick the state machine work, if peripheral is not supported
1357 * or peripheral is already registered with us.
1358 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301359 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001360 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301361 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301362 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301363
1364 return 0;
1365}
1366
Steve Mucklef132c6c2012-06-06 18:30:57 -07001367static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301368{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301369 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001370 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301371 struct msm_otg_platform_data *pdata = motg->pdata;
1372
1373 if (!otg->gadget)
1374 return;
1375
1376 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001377 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301378 /*
1379 * Some boards have a switch cotrolled by gpio
1380 * to enable/disable internal HUB. Disable internal
1381 * HUB before kicking the gadget.
1382 */
1383 if (pdata->setup_gpio)
1384 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001385
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301386 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301387 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301388 ret = msm_bus_scale_client_update_request(
1389 motg->bus_perf_client, 1);
1390 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001391 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301392 "bus bandwidth %d\n", __func__, ret);
1393 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301394 usb_gadget_vbus_connect(otg->gadget);
1395 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001396 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301397 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301398 /* Configure BUS performance parameters to default */
1399 if (motg->bus_perf_client) {
1400 ret = msm_bus_scale_client_update_request(
1401 motg->bus_perf_client, 0);
1402 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001403 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301404 "for bus bw %d\n", __func__, ret);
1405 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301406 if (pdata->setup_gpio)
1407 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1408 }
1409
1410}
1411
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001412static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301413 struct usb_gadget *gadget)
1414{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001415 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301416
1417 /*
1418 * Fail peripheral registration if this board can support
1419 * only host configuration.
1420 */
1421 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001422 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301423 return -ENODEV;
1424 }
1425
1426 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001427 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1428 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301429 msm_otg_start_peripheral(otg, 0);
1430 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001431 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301432 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301433 } else {
1434 otg->gadget = NULL;
1435 }
1436
1437 return 0;
1438 }
1439 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001440 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301441
1442 /*
1443 * Kick the state machine work, if host is not supported
1444 * or host is already registered with us.
1445 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301446 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001447 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301448 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301449 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301450
1451 return 0;
1452}
1453
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301454static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1455 void (*callback)(int on))
1456{
1457 struct usb_phy *phy = &motg->phy;
1458 int ret;
1459
1460 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1461 !motg->pdata->pmic_id_irq) {
1462 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1463 return -ENODEV;
1464 }
1465
1466 if (!motg->pdata->mhl_dev_name) {
1467 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1468 return -ENODEV;
1469 }
1470
1471 if (callback)
1472 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1473 callback);
1474 else
1475 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1476
1477 if (ret)
1478 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1479 motg->pdata->mhl_dev_name, ret);
1480 else
1481 motg->mhl_enabled = true;
1482
1483 return ret;
1484}
1485
1486static void msm_otg_mhl_notify_online(int on)
1487{
1488 struct msm_otg *motg = the_msm_otg;
1489 struct usb_phy *phy = &motg->phy;
1490 bool queue = false;
1491
1492 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1493
1494 if (on) {
1495 set_bit(MHL, &motg->inputs);
1496 } else {
1497 clear_bit(MHL, &motg->inputs);
1498 queue = true;
1499 }
1500
1501 if (queue && phy->state != OTG_STATE_UNDEFINED)
1502 schedule_work(&motg->sm_work);
1503}
1504
1505static bool msm_otg_is_mhl(struct msm_otg *motg)
1506{
1507 struct usb_phy *phy = &motg->phy;
1508 int is_mhl, ret;
1509
1510 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1511 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1512 /*
1513 * MHL driver calls our callback saying that MHL connected
1514 * if RID_GND is detected. But at later part of discovery
1515 * it may figure out MHL is not connected and returns
1516 * false. Hence clear MHL input here.
1517 */
1518 clear_bit(MHL, &motg->inputs);
1519 dev_dbg(phy->dev, "MHL device not found\n");
1520 return false;
1521 }
1522
1523 set_bit(MHL, &motg->inputs);
1524 dev_dbg(phy->dev, "MHL device found\n");
1525 return true;
1526}
1527
1528static bool msm_chg_mhl_detect(struct msm_otg *motg)
1529{
1530 bool ret, id;
1531 unsigned long flags;
1532
1533 if (!motg->mhl_enabled)
1534 return false;
1535
1536 local_irq_save(flags);
1537 id = irq_read_line(motg->pdata->pmic_id_irq);
1538 local_irq_restore(flags);
1539
1540 if (id)
1541 return false;
1542
1543 mhl_det_in_progress = true;
1544 ret = msm_otg_is_mhl(motg);
1545 mhl_det_in_progress = false;
1546
1547 return ret;
1548}
1549
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001550static bool msm_chg_aca_detect(struct msm_otg *motg)
1551{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001552 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 u32 int_sts;
1554 bool ret = false;
1555
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301556 if (!aca_enabled())
1557 goto out;
1558
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001559 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1560 goto out;
1561
Steve Mucklef132c6c2012-06-06 18:30:57 -07001562 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 switch (int_sts & 0x1C) {
1564 case 0x08:
1565 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001566 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001567 motg->chg_type = USB_ACA_A_CHARGER;
1568 motg->chg_state = USB_CHG_STATE_DETECTED;
1569 clear_bit(ID_B, &motg->inputs);
1570 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301571 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572 ret = true;
1573 }
1574 break;
1575 case 0x0C:
1576 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001577 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001578 motg->chg_type = USB_ACA_B_CHARGER;
1579 motg->chg_state = USB_CHG_STATE_DETECTED;
1580 clear_bit(ID_A, &motg->inputs);
1581 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301582 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001583 ret = true;
1584 }
1585 break;
1586 case 0x10:
1587 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001588 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001589 motg->chg_type = USB_ACA_C_CHARGER;
1590 motg->chg_state = USB_CHG_STATE_DETECTED;
1591 clear_bit(ID_A, &motg->inputs);
1592 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301593 set_bit(ID, &motg->inputs);
1594 ret = true;
1595 }
1596 break;
1597 case 0x04:
1598 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001599 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301600 motg->chg_type = USB_INVALID_CHARGER;
1601 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1602 clear_bit(ID_A, &motg->inputs);
1603 clear_bit(ID_B, &motg->inputs);
1604 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 ret = true;
1606 }
1607 break;
1608 default:
1609 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1610 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301611 test_and_clear_bit(ID_C, &motg->inputs) |
1612 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001613 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001614 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001615 motg->chg_type = USB_INVALID_CHARGER;
1616 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1617 }
1618 }
1619out:
1620 return ret;
1621}
1622
1623static void msm_chg_enable_aca_det(struct msm_otg *motg)
1624{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001625 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001626
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301627 if (!aca_enabled())
1628 return;
1629
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630 switch (motg->pdata->phy_type) {
1631 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301632 /* Disable ID_GND in link and PHY */
1633 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1634 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001635 ulpi_write(phy, 0x01, 0x0C);
1636 ulpi_write(phy, 0x10, 0x0F);
1637 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301638 /* Disable PMIC ID pull-up */
1639 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301640 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001641 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301642 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001643 break;
1644 default:
1645 break;
1646 }
1647}
1648
1649static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1650{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001651 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001652
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301653 if (!aca_enabled())
1654 return;
1655
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001656 switch (motg->pdata->phy_type) {
1657 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301658 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001659 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301660 break;
1661 default:
1662 break;
1663 }
1664}
1665
1666static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1667{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001668 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301669
1670 if (!aca_enabled())
1671 return;
1672
1673 switch (motg->pdata->phy_type) {
1674 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001675 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001676 break;
1677 default:
1678 break;
1679 }
1680}
1681
1682static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1683{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001684 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 bool ret = false;
1686
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301687 if (!aca_enabled())
1688 return ret;
1689
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001690 switch (motg->pdata->phy_type) {
1691 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001692 if (ulpi_read(phy, 0x91) & 1) {
1693 dev_dbg(phy->dev, "RID change\n");
1694 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001695 ret = msm_chg_aca_detect(motg);
1696 }
1697 default:
1698 break;
1699 }
1700 return ret;
1701}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301702
1703static void msm_otg_id_timer_func(unsigned long data)
1704{
1705 struct msm_otg *motg = (struct msm_otg *) data;
1706
1707 if (!aca_enabled())
1708 return;
1709
1710 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001711 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301712 return;
1713 }
1714
Steve Mucklef132c6c2012-06-06 18:30:57 -07001715 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301716 goto out;
1717
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301718 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001719 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301720 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301721 }
1722
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301723out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301724 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1725 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1726}
1727
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301728static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1729{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001730 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301731 u32 chg_det;
1732 bool ret = false;
1733
1734 switch (motg->pdata->phy_type) {
1735 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001736 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301737 ret = chg_det & (1 << 4);
1738 break;
1739 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001740 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301741 ret = chg_det & 1;
1742 break;
1743 default:
1744 break;
1745 }
1746 return ret;
1747}
1748
1749static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1750{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001751 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301752 u32 chg_det;
1753
1754 switch (motg->pdata->phy_type) {
1755 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001756 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301757 /* Turn off charger block */
1758 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001759 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301760 udelay(20);
1761 /* control chg block via ULPI */
1762 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001763 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301764 /* put it in host mode for enabling D- source */
1765 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001766 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301767 /* Turn on chg detect block */
1768 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001769 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301770 udelay(20);
1771 /* enable chg detection */
1772 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001773 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301774 break;
1775 case SNPS_28NM_INTEGRATED_PHY:
1776 /*
1777 * Configure DM as current source, DP as current sink
1778 * and enable battery charging comparators.
1779 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001780 ulpi_write(phy, 0x8, 0x85);
1781 ulpi_write(phy, 0x2, 0x85);
1782 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301783 break;
1784 default:
1785 break;
1786 }
1787}
1788
1789static bool msm_chg_check_primary_det(struct msm_otg *motg)
1790{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001791 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301792 u32 chg_det;
1793 bool ret = false;
1794
1795 switch (motg->pdata->phy_type) {
1796 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001797 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301798 ret = chg_det & (1 << 4);
1799 break;
1800 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001801 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301802 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301803 /* Turn off VDP_SRC */
1804 ulpi_write(phy, 0x3, 0x86);
1805 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301806 break;
1807 default:
1808 break;
1809 }
1810 return ret;
1811}
1812
1813static void msm_chg_enable_primary_det(struct msm_otg *motg)
1814{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001815 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301816 u32 chg_det;
1817
1818 switch (motg->pdata->phy_type) {
1819 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001820 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301821 /* enable chg detection */
1822 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001823 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301824 break;
1825 case SNPS_28NM_INTEGRATED_PHY:
1826 /*
1827 * Configure DP as current source, DM as current sink
1828 * and enable battery charging comparators.
1829 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001830 ulpi_write(phy, 0x2, 0x85);
1831 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301832 break;
1833 default:
1834 break;
1835 }
1836}
1837
1838static bool msm_chg_check_dcd(struct msm_otg *motg)
1839{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001840 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301841 u32 line_state;
1842 bool ret = false;
1843
1844 switch (motg->pdata->phy_type) {
1845 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 ret = !(line_state & 1);
1848 break;
1849 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001850 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301851 ret = line_state & 2;
1852 break;
1853 default:
1854 break;
1855 }
1856 return ret;
1857}
1858
1859static void msm_chg_disable_dcd(struct msm_otg *motg)
1860{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001861 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301862 u32 chg_det;
1863
1864 switch (motg->pdata->phy_type) {
1865 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001866 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301867 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001868 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301869 break;
1870 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001871 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301872 break;
1873 default:
1874 break;
1875 }
1876}
1877
1878static void msm_chg_enable_dcd(struct msm_otg *motg)
1879{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001880 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301881 u32 chg_det;
1882
1883 switch (motg->pdata->phy_type) {
1884 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 /* Turn on D+ current source */
1887 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001888 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301889 break;
1890 case SNPS_28NM_INTEGRATED_PHY:
1891 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001892 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301893 break;
1894 default:
1895 break;
1896 }
1897}
1898
1899static void msm_chg_block_on(struct msm_otg *motg)
1900{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001901 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301902 u32 func_ctrl, chg_det;
1903
1904 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001905 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301906 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1907 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001908 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301909
1910 switch (motg->pdata->phy_type) {
1911 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001912 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301913 /* control chg block via ULPI */
1914 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001915 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301916 /* Turn on chg detect block */
1917 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001918 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301919 udelay(20);
1920 break;
1921 case SNPS_28NM_INTEGRATED_PHY:
1922 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001923 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301924 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001925 ulpi_write(phy, 0x1F, 0x92);
1926 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301927 udelay(100);
1928 break;
1929 default:
1930 break;
1931 }
1932}
1933
1934static void msm_chg_block_off(struct msm_otg *motg)
1935{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001936 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301937 u32 func_ctrl, chg_det;
1938
1939 switch (motg->pdata->phy_type) {
1940 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942 /* Turn off charger block */
1943 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001944 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301945 break;
1946 case SNPS_28NM_INTEGRATED_PHY:
1947 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001948 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301949 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001950 ulpi_write(phy, 0x1F, 0x92);
1951 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301952 break;
1953 default:
1954 break;
1955 }
1956
1957 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001958 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1960 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301962}
1963
Anji jonnalad270e2d2011-08-09 11:28:32 +05301964static const char *chg_to_string(enum usb_chg_type chg_type)
1965{
1966 switch (chg_type) {
1967 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1968 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1969 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1970 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1971 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1972 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1973 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301974 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301975 default: return "INVALID_CHARGER";
1976 }
1977}
1978
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1980#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301981#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1982#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301983static void msm_chg_detect_work(struct work_struct *w)
1984{
1985 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001986 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301987 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301988 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301989 unsigned long delay;
1990
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001991 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301992
1993 if (test_bit(MHL, &motg->inputs)) {
1994 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
1995 return;
1996 }
1997
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301998 switch (motg->chg_state) {
1999 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302000 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302001 if (motg->pdata->enable_dcd)
2002 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302004 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2005 motg->dcd_retries = 0;
2006 delay = MSM_CHG_DCD_POLL_TIME;
2007 break;
2008 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302009 if (msm_chg_mhl_detect(motg)) {
2010 msm_chg_block_off(motg);
2011 motg->chg_state = USB_CHG_STATE_DETECTED;
2012 motg->chg_type = USB_INVALID_CHARGER;
2013 queue_work(system_nrt_wq, &motg->sm_work);
2014 return;
2015 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002016 is_aca = msm_chg_aca_detect(motg);
2017 if (is_aca) {
2018 /*
2019 * ID_A can be ACA dock too. continue
2020 * primary detection after DCD.
2021 */
2022 if (test_bit(ID_A, &motg->inputs)) {
2023 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2024 } else {
2025 delay = 0;
2026 break;
2027 }
2028 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302029 if (motg->pdata->enable_dcd)
2030 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302031 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2032 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302033 if (motg->pdata->enable_dcd)
2034 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302035 msm_chg_enable_primary_det(motg);
2036 delay = MSM_CHG_PRIMARY_DET_TIME;
2037 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2038 } else {
2039 delay = MSM_CHG_DCD_POLL_TIME;
2040 }
2041 break;
2042 case USB_CHG_STATE_DCD_DONE:
2043 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302044 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2045 dm_vlgc = line_state & PORTSC_LS_DM;
2046 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302047 if (test_bit(ID_A, &motg->inputs)) {
2048 motg->chg_type = USB_ACA_DOCK_CHARGER;
2049 motg->chg_state = USB_CHG_STATE_DETECTED;
2050 delay = 0;
2051 break;
2052 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302053 if (line_state) { /* DP > VLGC */
2054 motg->chg_type = USB_PROPRIETARY_CHARGER;
2055 motg->chg_state = USB_CHG_STATE_DETECTED;
2056 delay = 0;
2057 } else {
2058 msm_chg_enable_secondary_det(motg);
2059 delay = MSM_CHG_SECONDARY_DET_TIME;
2060 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2061 }
2062 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302063 if (test_bit(ID_A, &motg->inputs)) {
2064 motg->chg_type = USB_ACA_A_CHARGER;
2065 motg->chg_state = USB_CHG_STATE_DETECTED;
2066 delay = 0;
2067 break;
2068 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302069
2070 if (line_state) /* DP > VLGC or/and DM > VLGC */
2071 motg->chg_type = USB_PROPRIETARY_CHARGER;
2072 else
2073 motg->chg_type = USB_SDP_CHARGER;
2074
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 motg->chg_state = USB_CHG_STATE_DETECTED;
2076 delay = 0;
2077 }
2078 break;
2079 case USB_CHG_STATE_PRIMARY_DONE:
2080 vout = msm_chg_check_secondary_det(motg);
2081 if (vout)
2082 motg->chg_type = USB_DCP_CHARGER;
2083 else
2084 motg->chg_type = USB_CDP_CHARGER;
2085 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2086 /* fall through */
2087 case USB_CHG_STATE_SECONDARY_DONE:
2088 motg->chg_state = USB_CHG_STATE_DETECTED;
2089 case USB_CHG_STATE_DETECTED:
2090 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002091 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302092 /*
2093 * Spurious interrupt is seen after enabling ACA detection
2094 * due to which charger detection fails in case of PET.
2095 * Add delay of 100 microsec to avoid that.
2096 */
2097 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002099 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302100 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302101 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 return;
2103 default:
2104 return;
2105 }
2106
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302107 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302108}
2109
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302110/*
2111 * We support OTG, Peripheral only and Host only configurations. In case
2112 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2113 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2114 * enabled when switch is controlled by user and default mode is supplied
2115 * by board file, which can be changed by userspace later.
2116 */
2117static void msm_otg_init_sm(struct msm_otg *motg)
2118{
2119 struct msm_otg_platform_data *pdata = motg->pdata;
2120 u32 otgsc = readl(USB_OTGSC);
2121
2122 switch (pdata->mode) {
2123 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002124 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302125 if (pdata->default_mode == USB_HOST) {
2126 clear_bit(ID, &motg->inputs);
2127 } else if (pdata->default_mode == USB_PERIPHERAL) {
2128 set_bit(ID, &motg->inputs);
2129 set_bit(B_SESS_VLD, &motg->inputs);
2130 } else {
2131 set_bit(ID, &motg->inputs);
2132 clear_bit(B_SESS_VLD, &motg->inputs);
2133 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302134 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302135 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302136 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302137 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302138 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302139 set_bit(A_BUS_REQ, &motg->inputs);
2140 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002141 if (otgsc & OTGSC_BSV)
2142 set_bit(B_SESS_VLD, &motg->inputs);
2143 else
2144 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302145 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302146 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002147 unsigned long flags;
2148 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302149 if (irq_read_line(pdata->pmic_id_irq))
2150 set_bit(ID, &motg->inputs);
2151 else
2152 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002153 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302154 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302155 /*
2156 * VBUS initial state is reported after PMIC
2157 * driver initialization. Wait for it.
2158 */
2159 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302160 }
2161 break;
2162 case USB_HOST:
2163 clear_bit(ID, &motg->inputs);
2164 break;
2165 case USB_PERIPHERAL:
2166 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302167 if (pdata->otg_control == OTG_PHY_CONTROL) {
2168 if (otgsc & OTGSC_BSV)
2169 set_bit(B_SESS_VLD, &motg->inputs);
2170 else
2171 clear_bit(B_SESS_VLD, &motg->inputs);
2172 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2173 /*
2174 * VBUS initial state is reported after PMIC
2175 * driver initialization. Wait for it.
2176 */
2177 wait_for_completion(&pmic_vbus_init);
2178 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302179 break;
2180 default:
2181 break;
2182 }
2183}
2184
2185static void msm_otg_sm_work(struct work_struct *w)
2186{
2187 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002188 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302189 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302190
Steve Mucklef132c6c2012-06-06 18:30:57 -07002191 pm_runtime_resume(otg->phy->dev);
2192 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002193 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302194 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002195 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302196 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302197 psy = power_supply_get_by_name("usb");
2198 if (!psy)
2199 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002200 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302201 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2202 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002203 pm_runtime_put_noidle(otg->phy->dev);
2204 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302205 break;
2206 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302207 /* FALL THROUGH */
2208 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302209 if (test_bit(MHL, &motg->inputs)) {
2210 /* allow LPM */
2211 pm_runtime_put_noidle(otg->phy->dev);
2212 pm_runtime_suspend(otg->phy->dev);
2213 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002214 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302215 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302216 if (msm_chg_mhl_detect(motg)) {
2217 work = 1;
2218 break;
2219 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302220 clear_bit(B_BUS_REQ, &motg->inputs);
2221 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002222 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302223 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302224 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302225 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302226 switch (motg->chg_state) {
2227 case USB_CHG_STATE_UNDEFINED:
2228 msm_chg_detect_work(&motg->chg_work.work);
2229 break;
2230 case USB_CHG_STATE_DETECTED:
2231 switch (motg->chg_type) {
2232 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302233 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002234 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302235 /* fall through */
2236 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302237 msm_otg_notify_charger(motg,
2238 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002239 pm_runtime_put_noidle(otg->phy->dev);
2240 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302241 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302242 case USB_ACA_B_CHARGER:
2243 msm_otg_notify_charger(motg,
2244 IDEV_ACA_CHG_MAX);
2245 /*
2246 * (ID_B --> ID_C) PHY_ALT interrupt can
2247 * not be detected in LPM.
2248 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302249 break;
2250 case USB_CDP_CHARGER:
2251 msm_otg_notify_charger(motg,
2252 IDEV_CHG_MAX);
2253 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002254 otg->phy->state =
2255 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302256 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302257 case USB_ACA_C_CHARGER:
2258 msm_otg_notify_charger(motg,
2259 IDEV_ACA_CHG_MAX);
2260 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002261 otg->phy->state =
2262 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302263 break;
2264 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302265 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002266 otg->phy->state =
2267 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302268 break;
2269 default:
2270 break;
2271 }
2272 break;
2273 default:
2274 break;
2275 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302276 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2277 pr_debug("b_sess_end && b_bus_req\n");
2278 if (msm_otg_start_srp(otg) < 0) {
2279 clear_bit(B_BUS_REQ, &motg->inputs);
2280 work = 1;
2281 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302282 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002283 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302284 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2285 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302286 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302287 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302288 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302289 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2290 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302291 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002292 msm_otg_reset(otg->phy);
2293 pm_runtime_put_noidle(otg->phy->dev);
2294 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302295 }
2296 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302297 case OTG_STATE_B_SRP_INIT:
2298 if (!test_bit(ID, &motg->inputs) ||
2299 test_bit(ID_A, &motg->inputs) ||
2300 test_bit(ID_C, &motg->inputs) ||
2301 (test_bit(B_SESS_VLD, &motg->inputs) &&
2302 !test_bit(ID_B, &motg->inputs))) {
2303 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2304 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002305 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302306 /*
2307 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2308 * bits after SRP initiation.
2309 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002310 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302311 work = 1;
2312 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2313 pr_debug("b_srp_fail\n");
2314 pr_info("A-device did not respond to SRP\n");
2315 clear_bit(B_BUS_REQ, &motg->inputs);
2316 clear_bit(B_SRP_FAIL, &motg->tmouts);
2317 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002318 ulpi_write(otg->phy, 0x0, 0x98);
2319 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302320 motg->b_last_se0_sess = jiffies;
2321 work = 1;
2322 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302323 break;
2324 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302325 if (!test_bit(ID, &motg->inputs) ||
2326 test_bit(ID_A, &motg->inputs) ||
2327 test_bit(ID_B, &motg->inputs) ||
2328 !test_bit(B_SESS_VLD, &motg->inputs)) {
2329 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302330 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2331 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302332 msm_otg_notify_charger(motg, 0);
2333 srp_reqd = otg->gadget->otg_srp_reqd;
2334 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302335 if (test_bit(ID_B, &motg->inputs))
2336 clear_bit(ID_B, &motg->inputs);
2337 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002338 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302339 motg->b_last_se0_sess = jiffies;
2340 if (srp_reqd)
2341 msm_otg_start_timer(motg,
2342 TB_TST_SRP, B_TST_SRP);
2343 else
2344 work = 1;
2345 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2346 otg->gadget->b_hnp_enable &&
2347 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2348 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2349 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2350 /* D+ pullup should not be disconnected within 4msec
2351 * after A device suspends the bus. Otherwise PET will
2352 * fail the compliance test.
2353 */
2354 udelay(1000);
2355 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002356 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302357 /*
2358 * start HCD even before A-device enable
2359 * pull-up to meet HNP timings.
2360 */
2361 otg->host->is_b_host = 1;
2362 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002363 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2364 test_bit(B_SESS_VLD, &motg->inputs)) {
2365 pr_debug("a_bus_suspend && b_sess_vld\n");
2366 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002367 pm_runtime_put_noidle(otg->phy->dev);
2368 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002369 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302371 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002372 }
2373 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302374 case OTG_STATE_B_WAIT_ACON:
2375 if (!test_bit(ID, &motg->inputs) ||
2376 test_bit(ID_A, &motg->inputs) ||
2377 test_bit(ID_B, &motg->inputs) ||
2378 !test_bit(B_SESS_VLD, &motg->inputs)) {
2379 pr_debug("!id || id_a/b || !b_sess_vld\n");
2380 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302381 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302382 * A-device is physically disconnected during
2383 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302384 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 msm_otg_start_host(otg, 0);
2386 otg->host->is_b_host = 0;
2387
2388 clear_bit(B_BUS_REQ, &motg->inputs);
2389 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2390 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002391 otg->phy->state = OTG_STATE_B_IDLE;
2392 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302393 work = 1;
2394 } else if (test_bit(A_CONN, &motg->inputs)) {
2395 pr_debug("a_conn\n");
2396 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002397 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302398 /*
2399 * PET disconnects D+ pullup after reset is generated
2400 * by B device in B_HOST role which is not detected by
2401 * B device. As workaorund , start timer of 300msec
2402 * and stop timer if A device is enumerated else clear
2403 * A_CONN.
2404 */
2405 msm_otg_start_timer(motg, TB_TST_CONFIG,
2406 B_TST_CONFIG);
2407 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2408 pr_debug("b_ase0_brst_tmout\n");
2409 pr_info("B HNP fail:No response from A device\n");
2410 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002411 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302412 otg->host->is_b_host = 0;
2413 clear_bit(B_ASE0_BRST, &motg->tmouts);
2414 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2415 clear_bit(B_BUS_REQ, &motg->inputs);
2416 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002417 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302418 work = 1;
2419 } else if (test_bit(ID_C, &motg->inputs)) {
2420 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2421 }
2422 break;
2423 case OTG_STATE_B_HOST:
2424 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2425 !test_bit(A_CONN, &motg->inputs) ||
2426 !test_bit(B_SESS_VLD, &motg->inputs)) {
2427 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2428 clear_bit(A_CONN, &motg->inputs);
2429 clear_bit(B_BUS_REQ, &motg->inputs);
2430 msm_otg_start_host(otg, 0);
2431 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002432 otg->phy->state = OTG_STATE_B_IDLE;
2433 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302434 work = 1;
2435 } else if (test_bit(ID_C, &motg->inputs)) {
2436 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2437 }
2438 break;
2439 case OTG_STATE_A_IDLE:
2440 otg->default_a = 1;
2441 if (test_bit(ID, &motg->inputs) &&
2442 !test_bit(ID_A, &motg->inputs)) {
2443 pr_debug("id && !id_a\n");
2444 otg->default_a = 0;
2445 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002446 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302447 del_timer_sync(&motg->id_timer);
2448 msm_otg_link_reset(motg);
2449 msm_chg_enable_aca_intr(motg);
2450 msm_otg_notify_charger(motg, 0);
2451 work = 1;
2452 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2453 (test_bit(A_SRP_DET, &motg->inputs) ||
2454 test_bit(A_BUS_REQ, &motg->inputs))) {
2455 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2456
2457 clear_bit(A_SRP_DET, &motg->inputs);
2458 /* Disable SRP detection */
2459 writel_relaxed((readl_relaxed(USB_OTGSC) &
2460 ~OTGSC_INTSTS_MASK) &
2461 ~OTGSC_DPIE, USB_OTGSC);
2462
Steve Mucklef132c6c2012-06-06 18:30:57 -07002463 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 /* VBUS should not be supplied before end of SRP pulse
2465 * generated by PET, if not complaince test fail.
2466 */
2467 usleep_range(10000, 12000);
2468 /* ACA: ID_A: Stop charging untill enumeration */
2469 if (test_bit(ID_A, &motg->inputs))
2470 msm_otg_notify_charger(motg, 0);
2471 else
2472 msm_hsusb_vbus_power(motg, 1);
2473 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2474 } else {
2475 pr_debug("No session requested\n");
2476 clear_bit(A_BUS_DROP, &motg->inputs);
2477 if (test_bit(ID_A, &motg->inputs)) {
2478 msm_otg_notify_charger(motg,
2479 IDEV_ACA_CHG_MAX);
2480 } else if (!test_bit(ID, &motg->inputs)) {
2481 msm_otg_notify_charger(motg, 0);
2482 /*
2483 * A-device is not providing power on VBUS.
2484 * Enable SRP detection.
2485 */
2486 writel_relaxed(0x13, USB_USBMODE);
2487 writel_relaxed((readl_relaxed(USB_OTGSC) &
2488 ~OTGSC_INTSTS_MASK) |
2489 OTGSC_DPIE, USB_OTGSC);
2490 mb();
2491 }
2492 }
2493 break;
2494 case OTG_STATE_A_WAIT_VRISE:
2495 if ((test_bit(ID, &motg->inputs) &&
2496 !test_bit(ID_A, &motg->inputs)) ||
2497 test_bit(A_BUS_DROP, &motg->inputs) ||
2498 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2499 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2500 clear_bit(A_BUS_REQ, &motg->inputs);
2501 msm_otg_del_timer(motg);
2502 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002503 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302504 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2505 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2506 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002507 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302508 if (TA_WAIT_BCON > 0)
2509 msm_otg_start_timer(motg, TA_WAIT_BCON,
2510 A_WAIT_BCON);
2511 msm_otg_start_host(otg, 1);
2512 msm_chg_enable_aca_det(motg);
2513 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302514 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302515 if (msm_chg_check_aca_intr(motg))
2516 work = 1;
2517 }
2518 break;
2519 case OTG_STATE_A_WAIT_BCON:
2520 if ((test_bit(ID, &motg->inputs) &&
2521 !test_bit(ID_A, &motg->inputs)) ||
2522 test_bit(A_BUS_DROP, &motg->inputs) ||
2523 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2524 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2525 "a_wait_bcon_tmout\n");
2526 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2527 pr_info("Device No Response\n");
2528 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2529 }
2530 msm_otg_del_timer(motg);
2531 clear_bit(A_BUS_REQ, &motg->inputs);
2532 clear_bit(B_CONN, &motg->inputs);
2533 msm_otg_start_host(otg, 0);
2534 /*
2535 * ACA: ID_A with NO accessory, just the A plug is
2536 * attached to ACA: Use IDCHG_MAX for charging
2537 */
2538 if (test_bit(ID_A, &motg->inputs))
2539 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2540 else
2541 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002542 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2544 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2545 pr_debug("!a_vbus_vld\n");
2546 clear_bit(B_CONN, &motg->inputs);
2547 msm_otg_del_timer(motg);
2548 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002549 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2550 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302551 } else if (test_bit(ID_A, &motg->inputs)) {
2552 msm_hsusb_vbus_power(motg, 0);
2553 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2554 /*
2555 * If TA_WAIT_BCON is infinite, we don;t
2556 * turn off VBUS. Enter low power mode.
2557 */
2558 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002559 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302560 } else if (!test_bit(ID, &motg->inputs)) {
2561 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302562 }
2563 break;
2564 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302565 if ((test_bit(ID, &motg->inputs) &&
2566 !test_bit(ID_A, &motg->inputs)) ||
2567 test_bit(A_BUS_DROP, &motg->inputs)) {
2568 pr_debug("id_a/b/c || a_bus_drop\n");
2569 clear_bit(B_CONN, &motg->inputs);
2570 clear_bit(A_BUS_REQ, &motg->inputs);
2571 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002572 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302573 msm_otg_start_host(otg, 0);
2574 if (!test_bit(ID_A, &motg->inputs))
2575 msm_hsusb_vbus_power(motg, 0);
2576 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2577 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2578 pr_debug("!a_vbus_vld\n");
2579 clear_bit(B_CONN, &motg->inputs);
2580 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002581 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302582 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002583 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302584 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2585 /*
2586 * a_bus_req is de-asserted when root hub is
2587 * suspended or HNP is in progress.
2588 */
2589 pr_debug("!a_bus_req\n");
2590 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002591 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302592 if (otg->host->b_hnp_enable)
2593 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2594 A_AIDL_BDIS);
2595 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002596 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302597 } else if (!test_bit(B_CONN, &motg->inputs)) {
2598 pr_debug("!b_conn\n");
2599 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002600 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302601 if (TA_WAIT_BCON > 0)
2602 msm_otg_start_timer(motg, TA_WAIT_BCON,
2603 A_WAIT_BCON);
2604 if (msm_chg_check_aca_intr(motg))
2605 work = 1;
2606 } else if (test_bit(ID_A, &motg->inputs)) {
2607 msm_otg_del_timer(motg);
2608 msm_hsusb_vbus_power(motg, 0);
2609 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2610 msm_otg_notify_charger(motg,
2611 IDEV_ACA_CHG_MAX);
2612 else
2613 msm_otg_notify_charger(motg,
2614 IDEV_CHG_MIN - motg->mA_port);
2615 } else if (!test_bit(ID, &motg->inputs)) {
2616 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2617 motg->chg_type = USB_INVALID_CHARGER;
2618 msm_otg_notify_charger(motg, 0);
2619 msm_hsusb_vbus_power(motg, 1);
2620 }
2621 break;
2622 case OTG_STATE_A_SUSPEND:
2623 if ((test_bit(ID, &motg->inputs) &&
2624 !test_bit(ID_A, &motg->inputs)) ||
2625 test_bit(A_BUS_DROP, &motg->inputs) ||
2626 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2627 pr_debug("id_a/b/c || a_bus_drop ||"
2628 "a_aidl_bdis_tmout\n");
2629 msm_otg_del_timer(motg);
2630 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002631 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302632 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002633 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302634 if (!test_bit(ID_A, &motg->inputs))
2635 msm_hsusb_vbus_power(motg, 0);
2636 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2637 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2638 pr_debug("!a_vbus_vld\n");
2639 msm_otg_del_timer(motg);
2640 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002641 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302642 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002643 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 } else if (!test_bit(B_CONN, &motg->inputs) &&
2645 otg->host->b_hnp_enable) {
2646 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002647 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302648 msm_otg_host_hnp_enable(otg, 1);
2649 otg->gadget->is_a_peripheral = 1;
2650 msm_otg_start_peripheral(otg, 1);
2651 } else if (!test_bit(B_CONN, &motg->inputs) &&
2652 !otg->host->b_hnp_enable) {
2653 pr_debug("!b_conn && !b_hnp_enable");
2654 /*
2655 * bus request is dropped during suspend.
2656 * acquire again for next device.
2657 */
2658 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002659 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 if (TA_WAIT_BCON > 0)
2661 msm_otg_start_timer(motg, TA_WAIT_BCON,
2662 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002663 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302664 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002665 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302666 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002667 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002668 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302669 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302670 }
2671 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302672 case OTG_STATE_A_PERIPHERAL:
2673 if ((test_bit(ID, &motg->inputs) &&
2674 !test_bit(ID_A, &motg->inputs)) ||
2675 test_bit(A_BUS_DROP, &motg->inputs)) {
2676 pr_debug("id _f/b/c || a_bus_drop\n");
2677 /* Clear BIDL_ADIS timer */
2678 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 msm_otg_start_peripheral(otg, 0);
2681 otg->gadget->is_a_peripheral = 0;
2682 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002683 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302684 if (!test_bit(ID_A, &motg->inputs))
2685 msm_hsusb_vbus_power(motg, 0);
2686 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2687 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2688 pr_debug("!a_vbus_vld\n");
2689 /* Clear BIDL_ADIS timer */
2690 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002691 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302692 msm_otg_start_peripheral(otg, 0);
2693 otg->gadget->is_a_peripheral = 0;
2694 msm_otg_start_host(otg, 0);
2695 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2696 pr_debug("a_bidl_adis_tmout\n");
2697 msm_otg_start_peripheral(otg, 0);
2698 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002699 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302700 set_bit(A_BUS_REQ, &motg->inputs);
2701 msm_otg_host_hnp_enable(otg, 0);
2702 if (TA_WAIT_BCON > 0)
2703 msm_otg_start_timer(motg, TA_WAIT_BCON,
2704 A_WAIT_BCON);
2705 } else if (test_bit(ID_A, &motg->inputs)) {
2706 msm_hsusb_vbus_power(motg, 0);
2707 msm_otg_notify_charger(motg,
2708 IDEV_CHG_MIN - motg->mA_port);
2709 } else if (!test_bit(ID, &motg->inputs)) {
2710 msm_otg_notify_charger(motg, 0);
2711 msm_hsusb_vbus_power(motg, 1);
2712 }
2713 break;
2714 case OTG_STATE_A_WAIT_VFALL:
2715 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2716 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002717 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302718 work = 1;
2719 }
2720 break;
2721 case OTG_STATE_A_VBUS_ERR:
2722 if ((test_bit(ID, &motg->inputs) &&
2723 !test_bit(ID_A, &motg->inputs)) ||
2724 test_bit(A_BUS_DROP, &motg->inputs) ||
2725 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002726 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302727 if (!test_bit(ID_A, &motg->inputs))
2728 msm_hsusb_vbus_power(motg, 0);
2729 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2730 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2731 motg->chg_type = USB_INVALID_CHARGER;
2732 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302733 }
2734 break;
2735 default:
2736 break;
2737 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302738 if (work)
2739 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302740}
2741
2742static irqreturn_t msm_otg_irq(int irq, void *data)
2743{
2744 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002745 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302746 u32 otgsc = 0, usbsts, pc;
2747 bool work = 0;
2748 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302749
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302750 if (atomic_read(&motg->in_lpm)) {
Manu Gautamf8c45642012-08-10 10:20:56 -07002751 pr_debug("OTG IRQ: %d in LPM\n", irq);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302752 disable_irq_nosync(irq);
Manu Gautamf8c45642012-08-10 10:20:56 -07002753 motg->async_int = irq;
Jack Phamc7edb172012-08-13 15:32:39 -07002754 if (!atomic_read(&motg->pm_suspended))
Steve Mucklef132c6c2012-06-06 18:30:57 -07002755 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302756 return IRQ_HANDLED;
2757 }
2758
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002759 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302760 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302761
2762 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302763 return IRQ_NONE;
2764
2765 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302766 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302767 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302768 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302769 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302770 pr_debug("Id clear\n");
2771 /*
2772 * Assert a_bus_req to supply power on
2773 * VBUS when Micro/Mini-A cable is connected
2774 * with out user intervention.
2775 */
2776 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302777 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302778 msm_chg_enable_aca_det(motg);
2779 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302780 writel_relaxed(otgsc, USB_OTGSC);
2781 work = 1;
2782 } else if (otgsc & OTGSC_DPIS) {
2783 pr_debug("DPIS detected\n");
2784 writel_relaxed(otgsc, USB_OTGSC);
2785 set_bit(A_SRP_DET, &motg->inputs);
2786 set_bit(A_BUS_REQ, &motg->inputs);
2787 work = 1;
2788 } else if (otgsc & OTGSC_BSVIS) {
2789 writel_relaxed(otgsc, USB_OTGSC);
2790 /*
2791 * BSV interrupt comes when operating as an A-device
2792 * (VBUS on/off).
2793 * But, handle BSV when charger is removed from ACA in ID_A
2794 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002795 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302796 !test_bit(ID_A, &motg->inputs))
2797 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302798 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302799 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302800 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302801 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302802 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302803 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002804 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2805
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302806 msm_chg_check_aca_intr(motg);
2807 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302808 work = 1;
2809 } else if (usbsts & STS_PCI) {
2810 pc = readl_relaxed(USB_PORTSC);
2811 pr_debug("portsc = %x\n", pc);
2812 ret = IRQ_NONE;
2813 /*
2814 * HCD Acks PCI interrupt. We use this to switch
2815 * between different OTG states.
2816 */
2817 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002818 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302819 case OTG_STATE_A_SUSPEND:
2820 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2821 !(pc & PORTSC_CCS)) {
2822 pr_debug("B_CONN clear\n");
2823 clear_bit(B_CONN, &motg->inputs);
2824 msm_otg_del_timer(motg);
2825 }
2826 break;
2827 case OTG_STATE_A_PERIPHERAL:
2828 /*
2829 * A-peripheral observed activity on bus.
2830 * clear A_BIDL_ADIS timer.
2831 */
2832 msm_otg_del_timer(motg);
2833 work = 0;
2834 break;
2835 case OTG_STATE_B_WAIT_ACON:
2836 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2837 pr_debug("A_CONN set\n");
2838 set_bit(A_CONN, &motg->inputs);
2839 /* Clear ASE0_BRST timer */
2840 msm_otg_del_timer(motg);
2841 }
2842 break;
2843 case OTG_STATE_B_HOST:
2844 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2845 pr_debug("A_CONN clear\n");
2846 clear_bit(A_CONN, &motg->inputs);
2847 msm_otg_del_timer(motg);
2848 }
2849 break;
2850 case OTG_STATE_A_WAIT_BCON:
2851 if (TA_WAIT_BCON < 0)
2852 set_bit(A_BUS_REQ, &motg->inputs);
2853 default:
2854 work = 0;
2855 break;
2856 }
2857 } else if (usbsts & STS_URI) {
2858 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002859 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302860 case OTG_STATE_A_PERIPHERAL:
2861 /*
2862 * A-peripheral observed activity on bus.
2863 * clear A_BIDL_ADIS timer.
2864 */
2865 msm_otg_del_timer(motg);
2866 work = 0;
2867 break;
2868 default:
2869 work = 0;
2870 break;
2871 }
2872 } else if (usbsts & STS_SLI) {
2873 ret = IRQ_NONE;
2874 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002875 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 case OTG_STATE_B_PERIPHERAL:
2877 if (otg->gadget->b_hnp_enable) {
2878 set_bit(A_BUS_SUSPEND, &motg->inputs);
2879 set_bit(B_BUS_REQ, &motg->inputs);
2880 work = 1;
2881 }
2882 break;
2883 case OTG_STATE_A_PERIPHERAL:
2884 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2885 A_BIDL_ADIS);
2886 break;
2887 default:
2888 break;
2889 }
2890 } else if ((usbsts & PHY_ALT_INT)) {
2891 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2892 if (msm_chg_check_aca_intr(motg))
2893 work = 1;
2894 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302895 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302896 if (work)
2897 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302898
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302899 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002900}
2901
2902static void msm_otg_set_vbus_state(int online)
2903{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302904 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002905 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302906 struct usb_otg *otg = motg->phy.otg;
2907
2908 /* In A Host Mode, ignore received BSV interrupts */
2909 if (otg->phy->state >= OTG_STATE_A_IDLE)
2910 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002911
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302912 if (online) {
2913 pr_debug("PMIC: BSV set\n");
2914 set_bit(B_SESS_VLD, &motg->inputs);
2915 } else {
2916 pr_debug("PMIC: BSV clear\n");
2917 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302918 }
2919
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302920 if (!init) {
2921 init = true;
2922 complete(&pmic_vbus_init);
2923 pr_debug("PMIC: BSV init complete\n");
2924 return;
2925 }
2926
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302927 if (test_bit(MHL, &motg->inputs) ||
2928 mhl_det_in_progress) {
2929 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2930 return;
2931 }
2932
Jack Pham5ca279b2012-05-14 18:42:54 -07002933 if (atomic_read(&motg->pm_suspended))
2934 motg->sm_work_pending = true;
2935 else
2936 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002937}
2938
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302939static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002940{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302941 struct msm_otg *motg = container_of(w, struct msm_otg,
2942 pmic_id_status_work.work);
2943 int work = 0;
2944 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002945
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302946 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302947 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302948 if (!test_and_set_bit(ID, &motg->inputs)) {
2949 pr_debug("PMIC: ID set\n");
2950 work = 1;
2951 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302952 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302953 if (test_and_clear_bit(ID, &motg->inputs)) {
2954 pr_debug("PMIC: ID clear\n");
2955 set_bit(A_BUS_REQ, &motg->inputs);
2956 work = 1;
2957 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302958 }
2959
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302960 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002961 if (atomic_read(&motg->pm_suspended))
2962 motg->sm_work_pending = true;
2963 else
2964 queue_work(system_nrt_wq, &motg->sm_work);
2965 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302966 local_irq_restore(flags);
2967
2968}
2969
2970#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2971static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2972{
2973 struct msm_otg *motg = data;
2974
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302975 if (test_bit(MHL, &motg->inputs) ||
2976 mhl_det_in_progress) {
2977 pr_debug("PMIC: Id interrupt ignored in MHL\n");
2978 return IRQ_HANDLED;
2979 }
2980
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302981 if (!aca_id_turned_on)
2982 /*schedule delayed work for 5msec for ID line state to settle*/
2983 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2984 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002985
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302986 return IRQ_HANDLED;
2987}
2988
2989static int msm_otg_mode_show(struct seq_file *s, void *unused)
2990{
2991 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002992 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302993
Steve Mucklef132c6c2012-06-06 18:30:57 -07002994 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302995 case OTG_STATE_A_HOST:
2996 seq_printf(s, "host\n");
2997 break;
2998 case OTG_STATE_B_PERIPHERAL:
2999 seq_printf(s, "peripheral\n");
3000 break;
3001 default:
3002 seq_printf(s, "none\n");
3003 break;
3004 }
3005
3006 return 0;
3007}
3008
3009static int msm_otg_mode_open(struct inode *inode, struct file *file)
3010{
3011 return single_open(file, msm_otg_mode_show, inode->i_private);
3012}
3013
3014static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3015 size_t count, loff_t *ppos)
3016{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303017 struct seq_file *s = file->private_data;
3018 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303019 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003020 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303021 int status = count;
3022 enum usb_mode_type req_mode;
3023
3024 memset(buf, 0x00, sizeof(buf));
3025
3026 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3027 status = -EFAULT;
3028 goto out;
3029 }
3030
3031 if (!strncmp(buf, "host", 4)) {
3032 req_mode = USB_HOST;
3033 } else if (!strncmp(buf, "peripheral", 10)) {
3034 req_mode = USB_PERIPHERAL;
3035 } else if (!strncmp(buf, "none", 4)) {
3036 req_mode = USB_NONE;
3037 } else {
3038 status = -EINVAL;
3039 goto out;
3040 }
3041
3042 switch (req_mode) {
3043 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003044 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303045 case OTG_STATE_A_HOST:
3046 case OTG_STATE_B_PERIPHERAL:
3047 set_bit(ID, &motg->inputs);
3048 clear_bit(B_SESS_VLD, &motg->inputs);
3049 break;
3050 default:
3051 goto out;
3052 }
3053 break;
3054 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003055 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303056 case OTG_STATE_B_IDLE:
3057 case OTG_STATE_A_HOST:
3058 set_bit(ID, &motg->inputs);
3059 set_bit(B_SESS_VLD, &motg->inputs);
3060 break;
3061 default:
3062 goto out;
3063 }
3064 break;
3065 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003066 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303067 case OTG_STATE_B_IDLE:
3068 case OTG_STATE_B_PERIPHERAL:
3069 clear_bit(ID, &motg->inputs);
3070 break;
3071 default:
3072 goto out;
3073 }
3074 break;
3075 default:
3076 goto out;
3077 }
3078
Steve Mucklef132c6c2012-06-06 18:30:57 -07003079 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303080 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303081out:
3082 return status;
3083}
3084
3085const struct file_operations msm_otg_mode_fops = {
3086 .open = msm_otg_mode_open,
3087 .read = seq_read,
3088 .write = msm_otg_mode_write,
3089 .llseek = seq_lseek,
3090 .release = single_release,
3091};
3092
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303093static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3094{
3095 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003096 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303097
Steve Mucklef132c6c2012-06-06 18:30:57 -07003098 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303099 return 0;
3100}
3101
3102static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3103{
3104 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3105}
3106
3107const struct file_operations msm_otg_state_fops = {
3108 .open = msm_otg_otg_state_open,
3109 .read = seq_read,
3110 .llseek = seq_lseek,
3111 .release = single_release,
3112};
3113
Anji jonnalad270e2d2011-08-09 11:28:32 +05303114static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3115{
3116 struct msm_otg *motg = s->private;
3117
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303118 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303119 return 0;
3120}
3121
3122static int msm_otg_chg_open(struct inode *inode, struct file *file)
3123{
3124 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3125}
3126
3127const struct file_operations msm_otg_chg_fops = {
3128 .open = msm_otg_chg_open,
3129 .read = seq_read,
3130 .llseek = seq_lseek,
3131 .release = single_release,
3132};
3133
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303134static int msm_otg_aca_show(struct seq_file *s, void *unused)
3135{
3136 if (debug_aca_enabled)
3137 seq_printf(s, "enabled\n");
3138 else
3139 seq_printf(s, "disabled\n");
3140
3141 return 0;
3142}
3143
3144static int msm_otg_aca_open(struct inode *inode, struct file *file)
3145{
3146 return single_open(file, msm_otg_aca_show, inode->i_private);
3147}
3148
3149static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3150 size_t count, loff_t *ppos)
3151{
3152 char buf[8];
3153
3154 memset(buf, 0x00, sizeof(buf));
3155
3156 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3157 return -EFAULT;
3158
3159 if (!strncmp(buf, "enable", 6))
3160 debug_aca_enabled = true;
3161 else
3162 debug_aca_enabled = false;
3163
3164 return count;
3165}
3166
3167const struct file_operations msm_otg_aca_fops = {
3168 .open = msm_otg_aca_open,
3169 .read = seq_read,
3170 .write = msm_otg_aca_write,
3171 .llseek = seq_lseek,
3172 .release = single_release,
3173};
3174
Manu Gautam8bdcc592012-03-06 11:26:06 +05303175static int msm_otg_bus_show(struct seq_file *s, void *unused)
3176{
3177 if (debug_bus_voting_enabled)
3178 seq_printf(s, "enabled\n");
3179 else
3180 seq_printf(s, "disabled\n");
3181
3182 return 0;
3183}
3184
3185static int msm_otg_bus_open(struct inode *inode, struct file *file)
3186{
3187 return single_open(file, msm_otg_bus_show, inode->i_private);
3188}
3189
3190static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3191 size_t count, loff_t *ppos)
3192{
3193 char buf[8];
3194 int ret;
3195 struct seq_file *s = file->private_data;
3196 struct msm_otg *motg = s->private;
3197
3198 memset(buf, 0x00, sizeof(buf));
3199
3200 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3201 return -EFAULT;
3202
3203 if (!strncmp(buf, "enable", 6)) {
3204 /* Do not vote here. Let OTG statemachine decide when to vote */
3205 debug_bus_voting_enabled = true;
3206 } else {
3207 debug_bus_voting_enabled = false;
3208 if (motg->bus_perf_client) {
3209 ret = msm_bus_scale_client_update_request(
3210 motg->bus_perf_client, 0);
3211 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003212 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303213 "for bus bw %d\n", __func__, ret);
3214 }
3215 }
3216
3217 return count;
3218}
3219
3220const struct file_operations msm_otg_bus_fops = {
3221 .open = msm_otg_bus_open,
3222 .read = seq_read,
3223 .write = msm_otg_bus_write,
3224 .llseek = seq_lseek,
3225 .release = single_release,
3226};
3227
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303228static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303229
3230static int msm_otg_debugfs_init(struct msm_otg *motg)
3231{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303232 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303233
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303234 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3235
3236 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3237 return -ENODEV;
3238
Anji jonnalad270e2d2011-08-09 11:28:32 +05303239 if (motg->pdata->mode == USB_OTG &&
3240 motg->pdata->otg_control == OTG_USER_CONTROL) {
3241
Manu Gautam8bdcc592012-03-06 11:26:06 +05303242 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303243 S_IWUSR, msm_otg_dbg_root, motg,
3244 &msm_otg_mode_fops);
3245
Manu Gautam8bdcc592012-03-06 11:26:06 +05303246 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303247 debugfs_remove(msm_otg_dbg_root);
3248 msm_otg_dbg_root = NULL;
3249 return -ENODEV;
3250 }
3251 }
3252
Manu Gautam8bdcc592012-03-06 11:26:06 +05303253 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303254 msm_otg_dbg_root, motg,
3255 &msm_otg_chg_fops);
3256
Manu Gautam8bdcc592012-03-06 11:26:06 +05303257 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303258 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303259 return -ENODEV;
3260 }
3261
Manu Gautam8bdcc592012-03-06 11:26:06 +05303262 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303263 msm_otg_dbg_root, motg,
3264 &msm_otg_aca_fops);
3265
Manu Gautam8bdcc592012-03-06 11:26:06 +05303266 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303267 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303268 return -ENODEV;
3269 }
3270
Manu Gautam8bdcc592012-03-06 11:26:06 +05303271 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3272 msm_otg_dbg_root, motg,
3273 &msm_otg_bus_fops);
3274
3275 if (!msm_otg_dentry) {
3276 debugfs_remove_recursive(msm_otg_dbg_root);
3277 return -ENODEV;
3278 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303279
3280 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3281 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3282
3283 if (!msm_otg_dentry) {
3284 debugfs_remove_recursive(msm_otg_dbg_root);
3285 return -ENODEV;
3286 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303287 return 0;
3288}
3289
3290static void msm_otg_debugfs_cleanup(void)
3291{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303292 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303293}
3294
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303295static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3296static struct platform_device *msm_otg_add_pdev(
3297 struct platform_device *ofdev, const char *name)
3298{
3299 struct platform_device *pdev;
3300 const struct resource *res = ofdev->resource;
3301 unsigned int num = ofdev->num_resources;
3302 int retval;
3303
3304 pdev = platform_device_alloc(name, -1);
3305 if (!pdev) {
3306 retval = -ENOMEM;
3307 goto error;
3308 }
3309
3310 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3311 pdev->dev.dma_mask = &msm_otg_dma_mask;
3312
3313 if (num) {
3314 retval = platform_device_add_resources(pdev, res, num);
3315 if (retval)
3316 goto error;
3317 }
3318
3319 retval = platform_device_add(pdev);
3320 if (retval)
3321 goto error;
3322
3323 return pdev;
3324
3325error:
3326 platform_device_put(pdev);
3327 return ERR_PTR(retval);
3328}
3329
3330static int msm_otg_setup_devices(struct platform_device *ofdev,
3331 enum usb_mode_type mode, bool init)
3332{
3333 const char *gadget_name = "msm_hsusb";
3334 const char *host_name = "msm_hsusb_host";
3335 static struct platform_device *gadget_pdev;
3336 static struct platform_device *host_pdev;
3337 int retval = 0;
3338
3339 if (!init) {
3340 if (gadget_pdev)
3341 platform_device_unregister(gadget_pdev);
3342 if (host_pdev)
3343 platform_device_unregister(host_pdev);
3344 return 0;
3345 }
3346
3347 switch (mode) {
3348 case USB_OTG:
3349 /* fall through */
3350 case USB_PERIPHERAL:
3351 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3352 if (IS_ERR(gadget_pdev)) {
3353 retval = PTR_ERR(gadget_pdev);
3354 break;
3355 }
3356 if (mode == USB_PERIPHERAL)
3357 break;
3358 /* fall through */
3359 case USB_HOST:
3360 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3361 if (IS_ERR(host_pdev)) {
3362 retval = PTR_ERR(host_pdev);
3363 if (mode == USB_OTG)
3364 platform_device_unregister(gadget_pdev);
3365 }
3366 break;
3367 default:
3368 break;
3369 }
3370
3371 return retval;
3372}
3373
3374struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3375{
3376 struct device_node *node = pdev->dev.of_node;
3377 struct msm_otg_platform_data *pdata;
3378 int len = 0;
3379
3380 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3381 if (!pdata) {
3382 pr_err("unable to allocate platform data\n");
3383 return NULL;
3384 }
3385 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3386 if (len) {
3387 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3388 if (!pdata->phy_init_seq)
3389 return NULL;
3390 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3391 pdata->phy_init_seq,
3392 len/sizeof(*pdata->phy_init_seq));
3393 }
3394 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3395 &pdata->power_budget);
3396 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3397 &pdata->mode);
3398 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3399 &pdata->otg_control);
3400 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3401 &pdata->default_mode);
3402 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3403 &pdata->phy_type);
3404 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3405 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303406 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3407 "qcom,hsusb-otg-disable-reset");
3408
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303409 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303410}
3411
3412static int __init msm_otg_probe(struct platform_device *pdev)
3413{
Manu Gautamf8c45642012-08-10 10:20:56 -07003414 int ret = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303415 struct resource *res;
3416 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003417 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303418 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303419
3420 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303421
3422 if (pdev->dev.of_node) {
3423 dev_dbg(&pdev->dev, "device tree enabled\n");
3424 pdata = msm_otg_dt_to_pdata(pdev);
3425 if (!pdata)
3426 return -ENOMEM;
3427 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3428 if (ret) {
3429 dev_err(&pdev->dev, "devices setup failed\n");
3430 return ret;
3431 }
3432 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303433 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3434 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303435 } else {
3436 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303437 }
3438
3439 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3440 if (!motg) {
3441 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3442 return -ENOMEM;
3443 }
3444
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003445 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3446 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003447 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3448 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303449 goto free_motg;
3450 }
3451
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003452 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303453 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003454 phy = &motg->phy;
3455 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303456
3457 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303458 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3459 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3460 * PMIC can detect ACA ID_GND and generate an interrupt.
3461 */
3462 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3463 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3464 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003465 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303466 }
3467
Ofir Cohen4da266f2012-01-03 10:19:29 +02003468 /* initialize reset counter */
3469 motg->reset_counter = 0;
3470
Amit Blay02eff132011-09-21 16:46:24 +03003471 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003472 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003473 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003474 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303475
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303476 /*
3477 * Targets on which link uses asynchronous reset methodology,
3478 * free running clock is not required during the reset.
3479 */
Manu Gautam5143b252012-01-05 19:25:23 -08003480 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303481 if (IS_ERR(motg->clk))
3482 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3483 else
3484 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303485
3486 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003487 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303488 * CORE CLK must be running at >55Mhz for correct HSUSB
3489 * operation and USB core cannot tolerate frequency changes on
3490 * CORE CLK. For such USB cores, vote for maximum clk frequency
3491 * on pclk source
3492 */
Manu Gautam5143b252012-01-05 19:25:23 -08003493 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3494 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303495 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003496 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303497 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003498 goto put_clk;
3499 }
3500 clk_set_rate(motg->core_clk, INT_MAX);
3501
3502 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3503 if (IS_ERR(motg->pclk)) {
3504 dev_err(&pdev->dev, "failed to get iface_clk\n");
3505 ret = PTR_ERR(motg->pclk);
3506 goto put_core_clk;
3507 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303508
3509 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3510 if (!res) {
3511 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3512 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003513 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303514 }
3515
3516 motg->regs = ioremap(res->start, resource_size(res));
3517 if (!motg->regs) {
3518 dev_err(&pdev->dev, "ioremap failed\n");
3519 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003520 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303521 }
3522 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3523
3524 motg->irq = platform_get_irq(pdev, 0);
3525 if (!motg->irq) {
3526 dev_err(&pdev->dev, "platform_get_irq failed\n");
3527 ret = -ENODEV;
3528 goto free_regs;
3529 }
3530
Manu Gautamf8c45642012-08-10 10:20:56 -07003531 motg->async_irq = platform_get_irq_byname(pdev, "async_irq");
3532 if (motg->async_irq < 0) {
3533 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
3534 motg->async_irq = 0;
3535 }
3536
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003537 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003538 if (IS_ERR(motg->xo_handle)) {
3539 dev_err(&pdev->dev, "%s not able to get the handle "
3540 "to vote for TCXO D0 buffer\n", __func__);
3541 ret = PTR_ERR(motg->xo_handle);
3542 goto free_regs;
3543 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303544
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003545 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003546 if (ret) {
3547 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3548 "D0 buffer%d\n", __func__, ret);
3549 goto free_xo_handle;
3550 }
3551
Manu Gautam28b1bac2012-01-30 16:43:06 +05303552 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303553
Mayank Rana248698c2012-04-19 00:03:16 +05303554 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003555 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303556 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003557 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303558 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003559 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003560 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303561 goto devote_xo_handle;
3562 }
3563 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303564 }
3565
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003566 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303567 if (ret) {
3568 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303569 goto devote_xo_handle;
3570 }
3571
3572 ret = regulator_enable(hsusb_vddcx);
3573 if (ret) {
3574 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3575 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303576 }
3577
3578 ret = msm_hsusb_ldo_init(motg, 1);
3579 if (ret) {
3580 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303581 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303582 }
3583
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303584 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303585 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3586 "mhl_usb_hs_switch");
3587 if (IS_ERR(mhl_usb_hs_switch)) {
3588 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003589 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303590 goto free_ldo_init;
3591 }
3592 }
3593
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003594 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303595 if (ret) {
3596 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003597 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303598 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303599 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303600
3601 writel(0, USB_USBINTR);
3602 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003603 /* Ensure that above STOREs are completed before enabling interrupts */
3604 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303605
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303606 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3607 if (ret)
3608 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003609 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303610 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303611 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303612 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303613 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303614 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3615 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303616 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3617 "msm_otg", motg);
3618 if (ret) {
3619 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003620 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303621 }
3622
Manu Gautamf8c45642012-08-10 10:20:56 -07003623 if (motg->async_irq) {
3624 ret = request_irq(motg->async_irq, msm_otg_irq, IRQF_SHARED,
3625 "msm_otg", motg);
3626 if (ret) {
3627 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
3628 goto free_irq;
3629 }
3630 disable_irq(motg->async_irq);
3631 }
3632
Jack Pham87f202f2012-08-06 00:24:22 -07003633 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3634 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3635
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003636 phy->init = msm_otg_reset;
3637 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003638 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303639
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003640 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303641
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003642 phy->otg->phy = &motg->phy;
3643 phy->otg->set_host = msm_otg_set_host;
3644 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003645 phy->otg->start_hnp = msm_otg_start_hnp;
3646 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003647
3648 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303649 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003650 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Manu Gautamf8c45642012-08-10 10:20:56 -07003651 goto free_async_irq;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303652 }
3653
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303654 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303655 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003656 if (motg->pdata->pmic_id_irq) {
3657 ret = request_irq(motg->pdata->pmic_id_irq,
3658 msm_pmic_id_irq,
3659 IRQF_TRIGGER_RISING |
3660 IRQF_TRIGGER_FALLING,
3661 "msm_otg", motg);
3662 if (ret) {
3663 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003664 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003665 }
3666 } else {
3667 ret = -ENODEV;
3668 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003669 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003670 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303671 }
3672
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303673 msm_hsusb_mhl_switch_enable(motg, 1);
3674
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303675 platform_set_drvdata(pdev, motg);
3676 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003677 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303678
Anji jonnalad270e2d2011-08-09 11:28:32 +05303679 ret = msm_otg_debugfs_init(motg);
3680 if (ret)
3681 dev_dbg(&pdev->dev, "mode debugfs file is"
3682 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303683
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003684 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3685 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3686
Amit Blay58b31472011-11-18 09:39:39 +02003687 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3688 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303689 (!(motg->pdata->mode == USB_OTG) ||
3690 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003691 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303692 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003693
Amit Blay58b31472011-11-18 09:39:39 +02003694 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3695 motg->caps = ALLOW_PHY_RETENTION;
3696 }
3697
Amit Blay6fa647a2012-05-24 14:12:08 +03003698 if (motg->pdata->enable_lpm_on_dev_suspend)
3699 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3700
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003701 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303702 pm_runtime_set_active(&pdev->dev);
Manu Gautamf8c45642012-08-10 10:20:56 -07003703 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303704
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303705 if (motg->pdata->bus_scale_table) {
3706 motg->bus_perf_client =
3707 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3708 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003709 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303710 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303711 else
3712 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303713 }
3714
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303715 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003716
Steve Mucklef132c6c2012-06-06 18:30:57 -07003717remove_phy:
3718 usb_set_transceiver(NULL);
Manu Gautamf8c45642012-08-10 10:20:56 -07003719free_async_irq:
3720 if (motg->async_irq)
3721 free_irq(motg->async_irq, motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303722free_irq:
3723 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003724destroy_wlock:
3725 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303726 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003727 msm_hsusb_ldo_enable(motg, 0);
3728free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303729 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303730free_hsusb_vddcx:
3731 regulator_disable(hsusb_vddcx);
3732free_config_vddcx:
3733 regulator_set_voltage(hsusb_vddcx,
3734 vdd_val[motg->vdd_type][VDD_NONE],
3735 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003736devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303737 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003738 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003739free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003740 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303741free_regs:
3742 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003743put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303744 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303745put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003746 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303747put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303748 if (!IS_ERR(motg->clk))
3749 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003750 if (!IS_ERR(motg->phy_reset_clk))
3751 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003752free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003753 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303754free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303755 kfree(motg);
3756 return ret;
3757}
3758
3759static int __devexit msm_otg_remove(struct platform_device *pdev)
3760{
3761 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003762 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303763 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303764
3765 if (otg->host || otg->gadget)
3766 return -EBUSY;
3767
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303768 if (pdev->dev.of_node)
3769 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003770 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3771 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303772 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303773 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303774 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303775 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303776 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303777
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303778 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303779
3780 device_init_wakeup(&pdev->dev, 0);
3781 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003782 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303783
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303784 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003785 if (motg->pdata->pmic_id_irq)
3786 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003787 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303788 free_irq(motg->irq, motg);
3789
Jack Pham87f202f2012-08-06 00:24:22 -07003790 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3791 motg->pdata->mpm_otgsessvld_int)
3792 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3793
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303794 /*
3795 * Put PHY in low power mode.
3796 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003797 ulpi_read(otg->phy, 0x14);
3798 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303799
3800 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3801 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3802 if (readl(USB_PORTSC) & PORTSC_PHCD)
3803 break;
3804 udelay(1);
3805 cnt++;
3806 }
3807 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003808 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303809
Manu Gautam28b1bac2012-01-30 16:43:06 +05303810 clk_disable_unprepare(motg->pclk);
3811 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003812 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003813 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303814 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303815 regulator_disable(hsusb_vddcx);
3816 regulator_set_voltage(hsusb_vddcx,
3817 vdd_val[motg->vdd_type][VDD_NONE],
3818 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303819
3820 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303821 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303822
Amit Blay02eff132011-09-21 16:46:24 +03003823 if (!IS_ERR(motg->phy_reset_clk))
3824 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303825 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303826 if (!IS_ERR(motg->clk))
3827 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003828 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303829
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303830 if (motg->bus_perf_client)
3831 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303832
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003833 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303834 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303835 return 0;
3836}
3837
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303838#ifdef CONFIG_PM_RUNTIME
3839static int msm_otg_runtime_idle(struct device *dev)
3840{
3841 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003842 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303843
3844 dev_dbg(dev, "OTG runtime idle\n");
3845
Steve Mucklef132c6c2012-06-06 18:30:57 -07003846 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303847 return -EAGAIN;
3848 else
3849 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303850}
3851
3852static int msm_otg_runtime_suspend(struct device *dev)
3853{
3854 struct msm_otg *motg = dev_get_drvdata(dev);
3855
3856 dev_dbg(dev, "OTG runtime suspend\n");
3857 return msm_otg_suspend(motg);
3858}
3859
3860static int msm_otg_runtime_resume(struct device *dev)
3861{
3862 struct msm_otg *motg = dev_get_drvdata(dev);
3863
3864 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303865 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303866 return msm_otg_resume(motg);
3867}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303868#endif
3869
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303870#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303871static int msm_otg_pm_suspend(struct device *dev)
3872{
Jack Pham5ca279b2012-05-14 18:42:54 -07003873 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303874 struct msm_otg *motg = dev_get_drvdata(dev);
3875
3876 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003877
3878 atomic_set(&motg->pm_suspended, 1);
3879 ret = msm_otg_suspend(motg);
3880 if (ret)
3881 atomic_set(&motg->pm_suspended, 0);
3882
3883 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303884}
3885
3886static int msm_otg_pm_resume(struct device *dev)
3887{
Jack Pham5ca279b2012-05-14 18:42:54 -07003888 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303889 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303890
3891 dev_dbg(dev, "OTG PM resume\n");
3892
Jack Pham5ca279b2012-05-14 18:42:54 -07003893 atomic_set(&motg->pm_suspended, 0);
Jack Phamc7edb172012-08-13 15:32:39 -07003894 if (motg->async_int || motg->sm_work_pending) {
Jack Pham5ca279b2012-05-14 18:42:54 -07003895 pm_runtime_get_noresume(dev);
3896 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303897
Jack Pham5ca279b2012-05-14 18:42:54 -07003898 /* Update runtime PM status */
3899 pm_runtime_disable(dev);
3900 pm_runtime_set_active(dev);
3901 pm_runtime_enable(dev);
3902
Jack Phamc7edb172012-08-13 15:32:39 -07003903 if (motg->sm_work_pending) {
3904 motg->sm_work_pending = false;
3905 queue_work(system_nrt_wq, &motg->sm_work);
3906 }
Jack Pham5ca279b2012-05-14 18:42:54 -07003907 }
3908
3909 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303910}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303911#endif
3912
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303913#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303914static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303915 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3916 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3917 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303918};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303919#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303920
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303921static struct of_device_id msm_otg_dt_match[] = {
3922 { .compatible = "qcom,hsusb-otg",
3923 },
3924 {}
3925};
3926
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303927static struct platform_driver msm_otg_driver = {
3928 .remove = __devexit_p(msm_otg_remove),
3929 .driver = {
3930 .name = DRIVER_NAME,
3931 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303932#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303933 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303934#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303935 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303936 },
3937};
3938
3939static int __init msm_otg_init(void)
3940{
3941 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3942}
3943
3944static void __exit msm_otg_exit(void)
3945{
3946 platform_driver_unregister(&msm_otg_driver);
3947}
3948
3949module_init(msm_otg_init);
3950module_exit(msm_otg_exit);
3951
3952MODULE_LICENSE("GPL v2");
3953MODULE_DESCRIPTION("MSM USB transceiver driver");