blob: 7a1760f24563b18e4b89163da54e3660136996b5 [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;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530763 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530764 * Chipidea 45-nm PHY suspend sequence:
765 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530766 * Interrupt Latch Register auto-clear feature is not present
767 * in all PHY versions. Latch register is clear on read type.
768 * Clear latch register to avoid spurious wakeup from
769 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530770 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530771 * PHY comparators are disabled when PHY enters into low power
772 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
773 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
774 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530775 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530776 * PLL is not turned off when PHY enters into low power mode (LPM).
777 * Disable PLL for maximum power savings.
778 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530779
780 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200781 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530782 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200783 ulpi_write(phy, 0x01, 0x30);
784 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530785 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530786
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530788 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530789 * PHY may take some time or even fail to enter into low power
790 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
791 * in failure case.
792 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530793 portsc = readl_relaxed(USB_PORTSC);
794 if (!(portsc & PORTSC_PHCD)) {
795 writel_relaxed(portsc | PORTSC_PHCD,
796 USB_PORTSC);
797 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
798 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
799 break;
800 udelay(1);
801 cnt++;
802 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530803 }
804
805 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200806 dev_err(phy->dev, "Unable to suspend PHY\n");
807 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530808 enable_irq(motg->irq);
809 return -ETIMEDOUT;
810 }
811
812 /*
813 * PHY has capability to generate interrupt asynchronously in low
814 * power mode (LPM). This interrupt is level triggered. So USB IRQ
815 * line must be disabled till async interrupt enable bit is cleared
816 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
817 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530818 *
819 * PHY retention mode is disallowed while entering to LPM with wall
820 * charger connected. But PHY is put into suspend mode. Hence
821 * enable asynchronous interrupt to detect charger disconnection when
822 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530823 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530824 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300825 if (host_bus_suspend || device_bus_suspend ||
826 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530827 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
828 else
829 cmd_val |= ULPI_STP_CTRL;
830 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530831
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530832 /*
833 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
834 * PHY retention and collapse can not happen with VDP_SRC enabled.
835 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300836 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
837 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200838 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
839 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
840 /* Enable PHY HV interrupts to wake MPM/Link */
841 phy_ctrl_val |=
842 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530843
Amit Blay58b31472011-11-18 09:39:39 +0200844 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700845 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530846 }
847
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700848 /* Ensure that above operation is completed before turning off clocks */
849 mb();
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300850 /* Consider clocks on workaround flag only in case of bus suspend */
851 if (!(phy->state == OTG_STATE_B_PERIPHERAL &&
852 test_bit(A_BUS_SUSPEND, &motg->inputs)) ||
853 !motg->pdata->core_clk_always_on_workaround) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300854 clk_disable_unprepare(motg->pclk);
855 clk_disable_unprepare(motg->core_clk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300856 motg->lpm_flags |= CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300857 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530858
Anji jonnala7da3f262011-12-02 17:22:14 -0800859 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530860 if (!host_bus_suspend) {
861 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
862 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700863 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530864 "TCXO D0 buffer%d\n", __func__, ret);
865 else
866 motg->lpm_flags |= XO_SHUTDOWN;
867 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800868
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530869 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
870 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700871 msm_hsusb_ldo_enable(motg, 0);
872 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530873 }
874
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530875 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700876 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530877 msm_hsusb_mhl_switch_enable(motg, 0);
878 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700879
Steve Mucklef132c6c2012-06-06 18:30:57 -0700880 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530881 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 if (motg->pdata->pmic_id_irq)
883 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700884 if (pdata->otg_control == OTG_PHY_CONTROL &&
885 pdata->mpm_otgsessvld_int)
886 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530888 if (bus)
889 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
890
891 atomic_set(&motg->in_lpm, 1);
892 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700893 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530894
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200895 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530896
897 return 0;
898}
899
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530900static int msm_otg_resume(struct msm_otg *motg)
901{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200902 struct usb_phy *phy = &motg->phy;
903 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700904 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530905 int cnt = 0;
906 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200907 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800908 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530909
910 if (!atomic_read(&motg->in_lpm))
911 return 0;
912
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530914
Anji jonnala7da3f262011-12-02 17:22:14 -0800915 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530916 if (motg->lpm_flags & XO_SHUTDOWN) {
917 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
918 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700919 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530920 "TCXO D0 buffer%d\n", __func__, ret);
921 motg->lpm_flags &= ~XO_SHUTDOWN;
922 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530923
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300924 if (motg->lpm_flags & CLOCKS_DOWN) {
Amit Blay9b6e58b2012-06-18 13:12:49 +0300925 clk_prepare_enable(motg->core_clk);
926 clk_prepare_enable(motg->pclk);
Ido Shayevitzc5c0f572012-07-31 13:58:45 +0300927 motg->lpm_flags &= ~CLOCKS_DOWN;
Amit Blay9b6e58b2012-06-18 13:12:49 +0300928 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530929
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
931 msm_hsusb_ldo_enable(motg, 1);
932 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
933 }
934
935 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530936 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530937 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200938 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
939 phy_ctrl_val |= PHY_RETEN;
940 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
941 /* Disable PHY HV interrupts */
942 phy_ctrl_val &=
943 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
944 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700945 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530946 }
947
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530948 temp = readl(USB_USBCMD);
949 temp &= ~ASYNC_INTR_CTRL;
950 temp &= ~ULPI_STP_CTRL;
951 writel(temp, USB_USBCMD);
952
953 /*
954 * PHY comes out of low power mode (LPM) in case of wakeup
955 * from asynchronous interrupt.
956 */
957 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
958 goto skip_phy_resume;
959
960 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
961 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
962 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
963 break;
964 udelay(1);
965 cnt++;
966 }
967
968 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
969 /*
970 * This is a fatal error. Reset the link and
971 * PHY. USB state can not be restored. Re-insertion
972 * of USB cable is the only way to get USB working.
973 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200974 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530975 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200976 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530977 }
978
979skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700980 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530981 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 if (motg->pdata->pmic_id_irq)
983 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700984 if (pdata->otg_control == OTG_PHY_CONTROL &&
985 pdata->mpm_otgsessvld_int)
986 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700987 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530988 if (bus)
989 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
990
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530991 atomic_set(&motg->in_lpm, 0);
992
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530993 if (motg->async_int) {
994 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530995 enable_irq(motg->irq);
996 }
997
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200998 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530999
1000 return 0;
1001}
Pavankumar Kondeti70187732011-02-15 09:42:34 +05301002#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301003
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001004static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1005{
1006 if (!psy)
1007 goto psy_not_supported;
1008
1009 if (host_mode)
1010 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1011 else
1012 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1013
1014psy_not_supported:
1015 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1016 return -ENXIO;
1017}
1018
David Keitel081a3e22012-04-18 12:37:07 -07001019static int msm_otg_notify_chg_type(struct msm_otg *motg)
1020{
1021 static int charger_type;
1022 /*
1023 * TODO
1024 * Unify OTG driver charger types and power supply charger types
1025 */
1026 if (charger_type == motg->chg_type)
1027 return 0;
1028
1029 if (motg->chg_type == USB_SDP_CHARGER)
1030 charger_type = POWER_SUPPLY_TYPE_USB;
1031 else if (motg->chg_type == USB_CDP_CHARGER)
1032 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301033 else if (motg->chg_type == USB_DCP_CHARGER ||
1034 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001035 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1036 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1037 motg->chg_type == USB_ACA_A_CHARGER ||
1038 motg->chg_type == USB_ACA_B_CHARGER ||
1039 motg->chg_type == USB_ACA_C_CHARGER))
1040 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1041 else
1042 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1043
1044 return pm8921_set_usb_power_supply_type(charger_type);
1045}
1046
Amit Blay0f7edf72012-01-15 10:11:27 +02001047static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1048{
Amit Blay0f7edf72012-01-15 10:11:27 +02001049
Amit Blay0f7edf72012-01-15 10:11:27 +02001050 if (!psy)
1051 goto psy_not_supported;
1052
1053 if (motg->cur_power == 0 && mA > 0) {
1054 /* Enable charging */
1055 if (power_supply_set_online(psy, true))
1056 goto psy_not_supported;
1057 } else if (motg->cur_power > 0 && mA == 0) {
1058 /* Disable charging */
1059 if (power_supply_set_online(psy, false))
1060 goto psy_not_supported;
1061 return 0;
1062 }
1063 /* Set max current limit */
1064 if (power_supply_set_current_limit(psy, 1000*mA))
1065 goto psy_not_supported;
1066
1067 return 0;
1068
1069psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001070 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001071 return -ENXIO;
1072}
1073
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301074static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1075{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001076 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301077
1078 if (g && g->is_a_peripheral)
1079 return;
1080
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301081 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1082 motg->chg_type == USB_ACA_A_CHARGER ||
1083 motg->chg_type == USB_ACA_B_CHARGER ||
1084 motg->chg_type == USB_ACA_C_CHARGER) &&
1085 mA > IDEV_ACA_CHG_LIMIT)
1086 mA = IDEV_ACA_CHG_LIMIT;
1087
David Keitel081a3e22012-04-18 12:37:07 -07001088 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001089 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001090 "Failed notifying %d charger type to PMIC\n",
1091 motg->chg_type);
1092
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301093 if (motg->cur_power == mA)
1094 return;
1095
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001096 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001097
1098 /*
1099 * Use Power Supply API if supported, otherwise fallback
1100 * to legacy pm8921 API.
1101 */
1102 if (msm_otg_notify_power_supply(motg, mA))
1103 pm8921_charger_vbus_draw(mA);
1104
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301105 motg->cur_power = mA;
1106}
1107
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001108static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301109{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001110 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301111
1112 /*
1113 * Gadget driver uses set_power method to notify about the
1114 * available current based on suspend/configured states.
1115 *
1116 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1117 * states when CDP/ACA is connected.
1118 */
1119 if (motg->chg_type == USB_SDP_CHARGER)
1120 msm_otg_notify_charger(motg, mA);
1121
1122 return 0;
1123}
1124
Steve Mucklef132c6c2012-06-06 18:30:57 -07001125static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301126{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001127 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301128 struct msm_otg_platform_data *pdata = motg->pdata;
1129 struct usb_hcd *hcd;
1130
1131 if (!otg->host)
1132 return;
1133
1134 hcd = bus_to_hcd(otg->host);
1135
1136 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001137 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301138
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301139 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001140 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301141 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1142
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301143 /*
1144 * Some boards have a switch cotrolled by gpio
1145 * to enable/disable internal HUB. Enable internal
1146 * HUB before kicking the host.
1147 */
1148 if (pdata->setup_gpio)
1149 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301150 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301151 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001152 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301153
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301154 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301155 /* HCD core reset all bits of PORTSC. select ULPI phy */
1156 writel_relaxed(0x80000000, USB_PORTSC);
1157
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301158 if (pdata->setup_gpio)
1159 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301160
1161 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001162 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301163 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301164 }
1165}
1166
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001167static int msm_otg_usbdev_notify(struct notifier_block *self,
1168 unsigned long action, void *priv)
1169{
1170 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001171 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301172 struct usb_device *udev = priv;
1173
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301174 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1175 goto out;
1176
Steve Mucklef132c6c2012-06-06 18:30:57 -07001177 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301178 goto out;
1179 /*
1180 * Interested in devices connected directly to the root hub.
1181 * ACA dock can supply IDEV_CHG irrespective devices connected
1182 * on the accessory port.
1183 */
1184 if (!udev->parent || udev->parent->parent ||
1185 motg->chg_type == USB_ACA_DOCK_CHARGER)
1186 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001187
1188 switch (action) {
1189 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301190 if (aca_enabled())
1191 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001192 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301193 pr_debug("B_CONN set\n");
1194 set_bit(B_CONN, &motg->inputs);
1195 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001196 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301197 /*
1198 * OTG PET: A-device must end session within
1199 * 10 sec after PET enumeration.
1200 */
1201 if (udev->quirks & USB_QUIRK_OTG_PET)
1202 msm_otg_start_timer(motg, TA_TST_MAINT,
1203 A_TST_MAINT);
1204 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301205 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001206 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001207 if (udev->actconfig)
1208 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1209 else
1210 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001211 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301212 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301213 break;
1214 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001215 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1216 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301217 pr_debug("B_CONN clear\n");
1218 clear_bit(B_CONN, &motg->inputs);
1219 /*
1220 * OTG PET: A-device must end session after
1221 * PET disconnection if it is enumerated
1222 * with bcdDevice[0] = 1. USB core sets
1223 * bus->otg_vbus_off for us. clear it here.
1224 */
1225 if (udev->bus->otg_vbus_off) {
1226 udev->bus->otg_vbus_off = 0;
1227 set_bit(A_BUS_DROP, &motg->inputs);
1228 }
1229 queue_work(system_nrt_wq, &motg->sm_work);
1230 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 default:
1232 break;
1233 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301234 if (test_bit(ID_A, &motg->inputs))
1235 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1236 motg->mA_port);
1237out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001238 return NOTIFY_OK;
1239}
1240
Mayank Ranae3926882011-12-26 09:47:54 +05301241static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1242{
1243 int ret;
1244 static bool vbus_is_on;
1245
1246 if (vbus_is_on == on)
1247 return;
1248
1249 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301250 ret = motg->pdata->vbus_power(on);
1251 if (!ret)
1252 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301253 return;
1254 }
1255
1256 if (!vbus_otg) {
1257 pr_err("vbus_otg is NULL.");
1258 return;
1259 }
1260
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001261 /*
1262 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001263 * from usb before turning on the boost.
1264 * if exiting host mode disable the boost before enabling to draw
1265 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001266 */
Mayank Ranae3926882011-12-26 09:47:54 +05301267 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001268 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301269 ret = regulator_enable(vbus_otg);
1270 if (ret) {
1271 pr_err("unable to enable vbus_otg\n");
1272 return;
1273 }
1274 vbus_is_on = true;
1275 } else {
1276 ret = regulator_disable(vbus_otg);
1277 if (ret) {
1278 pr_err("unable to disable vbus_otg\n");
1279 return;
1280 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001281 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301282 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301283 }
1284}
1285
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001286static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301287{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001288 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301289 struct usb_hcd *hcd;
1290
1291 /*
1292 * Fail host registration if this board can support
1293 * only peripheral configuration.
1294 */
1295 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001296 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301297 return -ENODEV;
1298 }
1299
Mayank Ranae3926882011-12-26 09:47:54 +05301300 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001301 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301302 if (IS_ERR(vbus_otg)) {
1303 pr_err("Unable to get vbus_otg\n");
1304 return -ENODEV;
1305 }
1306 }
1307
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301308 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001309 if (otg->phy->state == OTG_STATE_A_HOST) {
1310 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001311 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301312 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301313 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301314 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001315 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301316 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301317 } else {
1318 otg->host = NULL;
1319 }
1320
1321 return 0;
1322 }
1323
1324 hcd = bus_to_hcd(host);
1325 hcd->power_budget = motg->pdata->power_budget;
1326
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301327#ifdef CONFIG_USB_OTG
1328 host->otg_port = 1;
1329#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001330 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1331 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301332 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001333 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301334
1335 /*
1336 * Kick the state machine work, if peripheral is not supported
1337 * or peripheral is already registered with us.
1338 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301339 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001340 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301341 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301342 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343
1344 return 0;
1345}
1346
Steve Mucklef132c6c2012-06-06 18:30:57 -07001347static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301349 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001350 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301351 struct msm_otg_platform_data *pdata = motg->pdata;
1352
1353 if (!otg->gadget)
1354 return;
1355
1356 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001357 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301358 /*
1359 * Some boards have a switch cotrolled by gpio
1360 * to enable/disable internal HUB. Disable internal
1361 * HUB before kicking the gadget.
1362 */
1363 if (pdata->setup_gpio)
1364 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001365
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301366 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301367 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301368 ret = msm_bus_scale_client_update_request(
1369 motg->bus_perf_client, 1);
1370 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001371 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301372 "bus bandwidth %d\n", __func__, ret);
1373 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374 usb_gadget_vbus_connect(otg->gadget);
1375 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001376 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301377 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301378 /* Configure BUS performance parameters to default */
1379 if (motg->bus_perf_client) {
1380 ret = msm_bus_scale_client_update_request(
1381 motg->bus_perf_client, 0);
1382 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001383 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301384 "for bus bw %d\n", __func__, ret);
1385 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301386 if (pdata->setup_gpio)
1387 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1388 }
1389
1390}
1391
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001392static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301393 struct usb_gadget *gadget)
1394{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001395 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301396
1397 /*
1398 * Fail peripheral registration if this board can support
1399 * only host configuration.
1400 */
1401 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001402 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301403 return -ENODEV;
1404 }
1405
1406 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001407 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1408 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301409 msm_otg_start_peripheral(otg, 0);
1410 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001411 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301412 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301413 } else {
1414 otg->gadget = NULL;
1415 }
1416
1417 return 0;
1418 }
1419 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001420 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301421
1422 /*
1423 * Kick the state machine work, if host is not supported
1424 * or host is already registered with us.
1425 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301426 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001427 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301428 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301429 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301430
1431 return 0;
1432}
1433
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301434static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1435 void (*callback)(int on))
1436{
1437 struct usb_phy *phy = &motg->phy;
1438 int ret;
1439
1440 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1441 !motg->pdata->pmic_id_irq) {
1442 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1443 return -ENODEV;
1444 }
1445
1446 if (!motg->pdata->mhl_dev_name) {
1447 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1448 return -ENODEV;
1449 }
1450
1451 if (callback)
1452 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1453 callback);
1454 else
1455 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1456
1457 if (ret)
1458 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1459 motg->pdata->mhl_dev_name, ret);
1460 else
1461 motg->mhl_enabled = true;
1462
1463 return ret;
1464}
1465
1466static void msm_otg_mhl_notify_online(int on)
1467{
1468 struct msm_otg *motg = the_msm_otg;
1469 struct usb_phy *phy = &motg->phy;
1470 bool queue = false;
1471
1472 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1473
1474 if (on) {
1475 set_bit(MHL, &motg->inputs);
1476 } else {
1477 clear_bit(MHL, &motg->inputs);
1478 queue = true;
1479 }
1480
1481 if (queue && phy->state != OTG_STATE_UNDEFINED)
1482 schedule_work(&motg->sm_work);
1483}
1484
1485static bool msm_otg_is_mhl(struct msm_otg *motg)
1486{
1487 struct usb_phy *phy = &motg->phy;
1488 int is_mhl, ret;
1489
1490 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1491 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1492 /*
1493 * MHL driver calls our callback saying that MHL connected
1494 * if RID_GND is detected. But at later part of discovery
1495 * it may figure out MHL is not connected and returns
1496 * false. Hence clear MHL input here.
1497 */
1498 clear_bit(MHL, &motg->inputs);
1499 dev_dbg(phy->dev, "MHL device not found\n");
1500 return false;
1501 }
1502
1503 set_bit(MHL, &motg->inputs);
1504 dev_dbg(phy->dev, "MHL device found\n");
1505 return true;
1506}
1507
1508static bool msm_chg_mhl_detect(struct msm_otg *motg)
1509{
1510 bool ret, id;
1511 unsigned long flags;
1512
1513 if (!motg->mhl_enabled)
1514 return false;
1515
1516 local_irq_save(flags);
1517 id = irq_read_line(motg->pdata->pmic_id_irq);
1518 local_irq_restore(flags);
1519
1520 if (id)
1521 return false;
1522
1523 mhl_det_in_progress = true;
1524 ret = msm_otg_is_mhl(motg);
1525 mhl_det_in_progress = false;
1526
1527 return ret;
1528}
1529
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530static bool msm_chg_aca_detect(struct msm_otg *motg)
1531{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001532 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001533 u32 int_sts;
1534 bool ret = false;
1535
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301536 if (!aca_enabled())
1537 goto out;
1538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1540 goto out;
1541
Steve Mucklef132c6c2012-06-06 18:30:57 -07001542 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001543 switch (int_sts & 0x1C) {
1544 case 0x08:
1545 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001546 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001547 motg->chg_type = USB_ACA_A_CHARGER;
1548 motg->chg_state = USB_CHG_STATE_DETECTED;
1549 clear_bit(ID_B, &motg->inputs);
1550 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301551 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001552 ret = true;
1553 }
1554 break;
1555 case 0x0C:
1556 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001557 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558 motg->chg_type = USB_ACA_B_CHARGER;
1559 motg->chg_state = USB_CHG_STATE_DETECTED;
1560 clear_bit(ID_A, &motg->inputs);
1561 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301562 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001563 ret = true;
1564 }
1565 break;
1566 case 0x10:
1567 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001568 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001569 motg->chg_type = USB_ACA_C_CHARGER;
1570 motg->chg_state = USB_CHG_STATE_DETECTED;
1571 clear_bit(ID_A, &motg->inputs);
1572 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301573 set_bit(ID, &motg->inputs);
1574 ret = true;
1575 }
1576 break;
1577 case 0x04:
1578 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001579 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301580 motg->chg_type = USB_INVALID_CHARGER;
1581 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1582 clear_bit(ID_A, &motg->inputs);
1583 clear_bit(ID_B, &motg->inputs);
1584 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001585 ret = true;
1586 }
1587 break;
1588 default:
1589 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1590 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301591 test_and_clear_bit(ID_C, &motg->inputs) |
1592 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001593 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001594 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001595 motg->chg_type = USB_INVALID_CHARGER;
1596 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1597 }
1598 }
1599out:
1600 return ret;
1601}
1602
1603static void msm_chg_enable_aca_det(struct msm_otg *motg)
1604{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001605 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001606
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301607 if (!aca_enabled())
1608 return;
1609
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001610 switch (motg->pdata->phy_type) {
1611 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301612 /* Disable ID_GND in link and PHY */
1613 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1614 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001615 ulpi_write(phy, 0x01, 0x0C);
1616 ulpi_write(phy, 0x10, 0x0F);
1617 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301618 /* Disable PMIC ID pull-up */
1619 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301620 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001621 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301622 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001623 break;
1624 default:
1625 break;
1626 }
1627}
1628
1629static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1630{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001631 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001632
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301633 if (!aca_enabled())
1634 return;
1635
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001636 switch (motg->pdata->phy_type) {
1637 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301638 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001639 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301640 break;
1641 default:
1642 break;
1643 }
1644}
1645
1646static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1647{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001648 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301649
1650 if (!aca_enabled())
1651 return;
1652
1653 switch (motg->pdata->phy_type) {
1654 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001655 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001656 break;
1657 default:
1658 break;
1659 }
1660}
1661
1662static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1663{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001664 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665 bool ret = false;
1666
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301667 if (!aca_enabled())
1668 return ret;
1669
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 switch (motg->pdata->phy_type) {
1671 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001672 if (ulpi_read(phy, 0x91) & 1) {
1673 dev_dbg(phy->dev, "RID change\n");
1674 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001675 ret = msm_chg_aca_detect(motg);
1676 }
1677 default:
1678 break;
1679 }
1680 return ret;
1681}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301682
1683static void msm_otg_id_timer_func(unsigned long data)
1684{
1685 struct msm_otg *motg = (struct msm_otg *) data;
1686
1687 if (!aca_enabled())
1688 return;
1689
1690 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001691 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301692 return;
1693 }
1694
Steve Mucklef132c6c2012-06-06 18:30:57 -07001695 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301696 goto out;
1697
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301698 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001699 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301700 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301701 }
1702
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301703out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301704 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1705 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1706}
1707
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301708static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1709{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001710 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301711 u32 chg_det;
1712 bool ret = false;
1713
1714 switch (motg->pdata->phy_type) {
1715 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001716 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301717 ret = chg_det & (1 << 4);
1718 break;
1719 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001720 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301721 ret = chg_det & 1;
1722 break;
1723 default:
1724 break;
1725 }
1726 return ret;
1727}
1728
1729static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1730{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001731 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301732 u32 chg_det;
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 /* Turn off charger block */
1738 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001739 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301740 udelay(20);
1741 /* control chg block via ULPI */
1742 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001743 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301744 /* put it in host mode for enabling D- source */
1745 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001746 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301747 /* Turn on chg detect block */
1748 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001749 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301750 udelay(20);
1751 /* enable chg detection */
1752 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001753 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301754 break;
1755 case SNPS_28NM_INTEGRATED_PHY:
1756 /*
1757 * Configure DM as current source, DP as current sink
1758 * and enable battery charging comparators.
1759 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001760 ulpi_write(phy, 0x8, 0x85);
1761 ulpi_write(phy, 0x2, 0x85);
1762 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301763 break;
1764 default:
1765 break;
1766 }
1767}
1768
1769static bool msm_chg_check_primary_det(struct msm_otg *motg)
1770{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001771 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301772 u32 chg_det;
1773 bool ret = false;
1774
1775 switch (motg->pdata->phy_type) {
1776 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001777 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301778 ret = chg_det & (1 << 4);
1779 break;
1780 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001781 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301782 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301783 /* Turn off VDP_SRC */
1784 ulpi_write(phy, 0x3, 0x86);
1785 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301786 break;
1787 default:
1788 break;
1789 }
1790 return ret;
1791}
1792
1793static void msm_chg_enable_primary_det(struct msm_otg *motg)
1794{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001795 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301796 u32 chg_det;
1797
1798 switch (motg->pdata->phy_type) {
1799 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001800 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301801 /* enable chg detection */
1802 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301804 break;
1805 case SNPS_28NM_INTEGRATED_PHY:
1806 /*
1807 * Configure DP as current source, DM as current sink
1808 * and enable battery charging comparators.
1809 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001810 ulpi_write(phy, 0x2, 0x85);
1811 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301812 break;
1813 default:
1814 break;
1815 }
1816}
1817
1818static bool msm_chg_check_dcd(struct msm_otg *motg)
1819{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001820 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301821 u32 line_state;
1822 bool ret = false;
1823
1824 switch (motg->pdata->phy_type) {
1825 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001826 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301827 ret = !(line_state & 1);
1828 break;
1829 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001830 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301831 ret = line_state & 2;
1832 break;
1833 default:
1834 break;
1835 }
1836 return ret;
1837}
1838
1839static void msm_chg_disable_dcd(struct msm_otg *motg)
1840{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001841 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301842 u32 chg_det;
1843
1844 switch (motg->pdata->phy_type) {
1845 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001848 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301849 break;
1850 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001851 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301852 break;
1853 default:
1854 break;
1855 }
1856}
1857
1858static void msm_chg_enable_dcd(struct msm_otg *motg)
1859{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001860 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301861 u32 chg_det;
1862
1863 switch (motg->pdata->phy_type) {
1864 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001865 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301866 /* Turn on D+ current source */
1867 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:
1871 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001872 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 break;
1874 default:
1875 break;
1876 }
1877}
1878
1879static void msm_chg_block_on(struct msm_otg *motg)
1880{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001881 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301882 u32 func_ctrl, chg_det;
1883
1884 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001885 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301886 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1887 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001888 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301889
1890 switch (motg->pdata->phy_type) {
1891 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001892 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301893 /* control chg block via ULPI */
1894 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001895 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301896 /* Turn on chg detect block */
1897 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001898 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 udelay(20);
1900 break;
1901 case SNPS_28NM_INTEGRATED_PHY:
1902 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001903 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301904 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001905 ulpi_write(phy, 0x1F, 0x92);
1906 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301907 udelay(100);
1908 break;
1909 default:
1910 break;
1911 }
1912}
1913
1914static void msm_chg_block_off(struct msm_otg *motg)
1915{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 u32 func_ctrl, chg_det;
1918
1919 switch (motg->pdata->phy_type) {
1920 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001921 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301922 /* Turn off charger block */
1923 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001924 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301925 break;
1926 case SNPS_28NM_INTEGRATED_PHY:
1927 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001928 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301929 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001930 ulpi_write(phy, 0x1F, 0x92);
1931 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301932 break;
1933 default:
1934 break;
1935 }
1936
1937 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001938 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301939 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1940 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001941 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301942}
1943
Anji jonnalad270e2d2011-08-09 11:28:32 +05301944static const char *chg_to_string(enum usb_chg_type chg_type)
1945{
1946 switch (chg_type) {
1947 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1948 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1949 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1950 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1951 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1952 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1953 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301954 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301955 default: return "INVALID_CHARGER";
1956 }
1957}
1958
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301959#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1960#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301961#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1962#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301963static void msm_chg_detect_work(struct work_struct *w)
1964{
1965 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301967 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301968 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301969 unsigned long delay;
1970
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001971 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301972
1973 if (test_bit(MHL, &motg->inputs)) {
1974 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
1975 return;
1976 }
1977
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301978 switch (motg->chg_state) {
1979 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301980 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301981 if (motg->pdata->enable_dcd)
1982 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301984 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1985 motg->dcd_retries = 0;
1986 delay = MSM_CHG_DCD_POLL_TIME;
1987 break;
1988 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301989 if (msm_chg_mhl_detect(motg)) {
1990 msm_chg_block_off(motg);
1991 motg->chg_state = USB_CHG_STATE_DETECTED;
1992 motg->chg_type = USB_INVALID_CHARGER;
1993 queue_work(system_nrt_wq, &motg->sm_work);
1994 return;
1995 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001996 is_aca = msm_chg_aca_detect(motg);
1997 if (is_aca) {
1998 /*
1999 * ID_A can be ACA dock too. continue
2000 * primary detection after DCD.
2001 */
2002 if (test_bit(ID_A, &motg->inputs)) {
2003 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
2004 } else {
2005 delay = 0;
2006 break;
2007 }
2008 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302009 if (motg->pdata->enable_dcd)
2010 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302011 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2012 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302013 if (motg->pdata->enable_dcd)
2014 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302015 msm_chg_enable_primary_det(motg);
2016 delay = MSM_CHG_PRIMARY_DET_TIME;
2017 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2018 } else {
2019 delay = MSM_CHG_DCD_POLL_TIME;
2020 }
2021 break;
2022 case USB_CHG_STATE_DCD_DONE:
2023 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302024 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2025 dm_vlgc = line_state & PORTSC_LS_DM;
2026 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302027 if (test_bit(ID_A, &motg->inputs)) {
2028 motg->chg_type = USB_ACA_DOCK_CHARGER;
2029 motg->chg_state = USB_CHG_STATE_DETECTED;
2030 delay = 0;
2031 break;
2032 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302033 if (line_state) { /* DP > VLGC */
2034 motg->chg_type = USB_PROPRIETARY_CHARGER;
2035 motg->chg_state = USB_CHG_STATE_DETECTED;
2036 delay = 0;
2037 } else {
2038 msm_chg_enable_secondary_det(motg);
2039 delay = MSM_CHG_SECONDARY_DET_TIME;
2040 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2041 }
2042 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302043 if (test_bit(ID_A, &motg->inputs)) {
2044 motg->chg_type = USB_ACA_A_CHARGER;
2045 motg->chg_state = USB_CHG_STATE_DETECTED;
2046 delay = 0;
2047 break;
2048 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302049
2050 if (line_state) /* DP > VLGC or/and DM > VLGC */
2051 motg->chg_type = USB_PROPRIETARY_CHARGER;
2052 else
2053 motg->chg_type = USB_SDP_CHARGER;
2054
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302055 motg->chg_state = USB_CHG_STATE_DETECTED;
2056 delay = 0;
2057 }
2058 break;
2059 case USB_CHG_STATE_PRIMARY_DONE:
2060 vout = msm_chg_check_secondary_det(motg);
2061 if (vout)
2062 motg->chg_type = USB_DCP_CHARGER;
2063 else
2064 motg->chg_type = USB_CDP_CHARGER;
2065 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2066 /* fall through */
2067 case USB_CHG_STATE_SECONDARY_DONE:
2068 motg->chg_state = USB_CHG_STATE_DETECTED;
2069 case USB_CHG_STATE_DETECTED:
2070 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002071 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302072 /*
2073 * Spurious interrupt is seen after enabling ACA detection
2074 * due to which charger detection fails in case of PET.
2075 * Add delay of 100 microsec to avoid that.
2076 */
2077 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002078 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002079 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302080 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302081 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302082 return;
2083 default:
2084 return;
2085 }
2086
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302087 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302088}
2089
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302090/*
2091 * We support OTG, Peripheral only and Host only configurations. In case
2092 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2093 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2094 * enabled when switch is controlled by user and default mode is supplied
2095 * by board file, which can be changed by userspace later.
2096 */
2097static void msm_otg_init_sm(struct msm_otg *motg)
2098{
2099 struct msm_otg_platform_data *pdata = motg->pdata;
2100 u32 otgsc = readl(USB_OTGSC);
2101
2102 switch (pdata->mode) {
2103 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002104 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302105 if (pdata->default_mode == USB_HOST) {
2106 clear_bit(ID, &motg->inputs);
2107 } else if (pdata->default_mode == USB_PERIPHERAL) {
2108 set_bit(ID, &motg->inputs);
2109 set_bit(B_SESS_VLD, &motg->inputs);
2110 } else {
2111 set_bit(ID, &motg->inputs);
2112 clear_bit(B_SESS_VLD, &motg->inputs);
2113 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302114 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302115 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302116 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302117 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302118 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302119 set_bit(A_BUS_REQ, &motg->inputs);
2120 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002121 if (otgsc & OTGSC_BSV)
2122 set_bit(B_SESS_VLD, &motg->inputs);
2123 else
2124 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302125 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302126 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002127 unsigned long flags;
2128 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302129 if (irq_read_line(pdata->pmic_id_irq))
2130 set_bit(ID, &motg->inputs);
2131 else
2132 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002133 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302134 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302135 /*
2136 * VBUS initial state is reported after PMIC
2137 * driver initialization. Wait for it.
2138 */
2139 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302140 }
2141 break;
2142 case USB_HOST:
2143 clear_bit(ID, &motg->inputs);
2144 break;
2145 case USB_PERIPHERAL:
2146 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302147 if (pdata->otg_control == OTG_PHY_CONTROL) {
2148 if (otgsc & OTGSC_BSV)
2149 set_bit(B_SESS_VLD, &motg->inputs);
2150 else
2151 clear_bit(B_SESS_VLD, &motg->inputs);
2152 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2153 /*
2154 * VBUS initial state is reported after PMIC
2155 * driver initialization. Wait for it.
2156 */
2157 wait_for_completion(&pmic_vbus_init);
2158 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302159 break;
2160 default:
2161 break;
2162 }
2163}
2164
2165static void msm_otg_sm_work(struct work_struct *w)
2166{
2167 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002168 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302169 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302170
Steve Mucklef132c6c2012-06-06 18:30:57 -07002171 pm_runtime_resume(otg->phy->dev);
2172 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002173 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302174 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002175 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302176 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302177 psy = power_supply_get_by_name("usb");
2178 if (!psy)
2179 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002180 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302181 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2182 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002183 pm_runtime_put_noidle(otg->phy->dev);
2184 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302185 break;
2186 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302187 /* FALL THROUGH */
2188 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302189 if (test_bit(MHL, &motg->inputs)) {
2190 /* allow LPM */
2191 pm_runtime_put_noidle(otg->phy->dev);
2192 pm_runtime_suspend(otg->phy->dev);
2193 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002194 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302195 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302196 if (msm_chg_mhl_detect(motg)) {
2197 work = 1;
2198 break;
2199 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302200 clear_bit(B_BUS_REQ, &motg->inputs);
2201 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002202 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302203 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302204 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302205 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302206 switch (motg->chg_state) {
2207 case USB_CHG_STATE_UNDEFINED:
2208 msm_chg_detect_work(&motg->chg_work.work);
2209 break;
2210 case USB_CHG_STATE_DETECTED:
2211 switch (motg->chg_type) {
2212 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302213 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002214 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302215 /* fall through */
2216 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302217 msm_otg_notify_charger(motg,
2218 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002219 pm_runtime_put_noidle(otg->phy->dev);
2220 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302221 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302222 case USB_ACA_B_CHARGER:
2223 msm_otg_notify_charger(motg,
2224 IDEV_ACA_CHG_MAX);
2225 /*
2226 * (ID_B --> ID_C) PHY_ALT interrupt can
2227 * not be detected in LPM.
2228 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302229 break;
2230 case USB_CDP_CHARGER:
2231 msm_otg_notify_charger(motg,
2232 IDEV_CHG_MAX);
2233 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002234 otg->phy->state =
2235 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302236 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302237 case USB_ACA_C_CHARGER:
2238 msm_otg_notify_charger(motg,
2239 IDEV_ACA_CHG_MAX);
2240 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002241 otg->phy->state =
2242 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302243 break;
2244 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302245 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002246 otg->phy->state =
2247 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302248 break;
2249 default:
2250 break;
2251 }
2252 break;
2253 default:
2254 break;
2255 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302256 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2257 pr_debug("b_sess_end && b_bus_req\n");
2258 if (msm_otg_start_srp(otg) < 0) {
2259 clear_bit(B_BUS_REQ, &motg->inputs);
2260 work = 1;
2261 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302262 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002263 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302264 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2265 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302266 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302267 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302268 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302269 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2270 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302271 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002272 msm_otg_reset(otg->phy);
2273 pm_runtime_put_noidle(otg->phy->dev);
2274 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302275 }
2276 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302277 case OTG_STATE_B_SRP_INIT:
2278 if (!test_bit(ID, &motg->inputs) ||
2279 test_bit(ID_A, &motg->inputs) ||
2280 test_bit(ID_C, &motg->inputs) ||
2281 (test_bit(B_SESS_VLD, &motg->inputs) &&
2282 !test_bit(ID_B, &motg->inputs))) {
2283 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2284 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002285 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302286 /*
2287 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2288 * bits after SRP initiation.
2289 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002290 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302291 work = 1;
2292 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2293 pr_debug("b_srp_fail\n");
2294 pr_info("A-device did not respond to SRP\n");
2295 clear_bit(B_BUS_REQ, &motg->inputs);
2296 clear_bit(B_SRP_FAIL, &motg->tmouts);
2297 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002298 ulpi_write(otg->phy, 0x0, 0x98);
2299 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302300 motg->b_last_se0_sess = jiffies;
2301 work = 1;
2302 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302303 break;
2304 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302305 if (!test_bit(ID, &motg->inputs) ||
2306 test_bit(ID_A, &motg->inputs) ||
2307 test_bit(ID_B, &motg->inputs) ||
2308 !test_bit(B_SESS_VLD, &motg->inputs)) {
2309 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302310 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2311 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302312 msm_otg_notify_charger(motg, 0);
2313 srp_reqd = otg->gadget->otg_srp_reqd;
2314 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302315 if (test_bit(ID_B, &motg->inputs))
2316 clear_bit(ID_B, &motg->inputs);
2317 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002318 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302319 motg->b_last_se0_sess = jiffies;
2320 if (srp_reqd)
2321 msm_otg_start_timer(motg,
2322 TB_TST_SRP, B_TST_SRP);
2323 else
2324 work = 1;
2325 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2326 otg->gadget->b_hnp_enable &&
2327 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2328 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2329 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2330 /* D+ pullup should not be disconnected within 4msec
2331 * after A device suspends the bus. Otherwise PET will
2332 * fail the compliance test.
2333 */
2334 udelay(1000);
2335 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002336 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302337 /*
2338 * start HCD even before A-device enable
2339 * pull-up to meet HNP timings.
2340 */
2341 otg->host->is_b_host = 1;
2342 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002343 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2344 test_bit(B_SESS_VLD, &motg->inputs)) {
2345 pr_debug("a_bus_suspend && b_sess_vld\n");
2346 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002347 pm_runtime_put_noidle(otg->phy->dev);
2348 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002349 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002350 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302351 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002352 }
2353 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302354 case OTG_STATE_B_WAIT_ACON:
2355 if (!test_bit(ID, &motg->inputs) ||
2356 test_bit(ID_A, &motg->inputs) ||
2357 test_bit(ID_B, &motg->inputs) ||
2358 !test_bit(B_SESS_VLD, &motg->inputs)) {
2359 pr_debug("!id || id_a/b || !b_sess_vld\n");
2360 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302361 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302362 * A-device is physically disconnected during
2363 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302364 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302365 msm_otg_start_host(otg, 0);
2366 otg->host->is_b_host = 0;
2367
2368 clear_bit(B_BUS_REQ, &motg->inputs);
2369 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2370 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002371 otg->phy->state = OTG_STATE_B_IDLE;
2372 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302373 work = 1;
2374 } else if (test_bit(A_CONN, &motg->inputs)) {
2375 pr_debug("a_conn\n");
2376 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002377 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302378 /*
2379 * PET disconnects D+ pullup after reset is generated
2380 * by B device in B_HOST role which is not detected by
2381 * B device. As workaorund , start timer of 300msec
2382 * and stop timer if A device is enumerated else clear
2383 * A_CONN.
2384 */
2385 msm_otg_start_timer(motg, TB_TST_CONFIG,
2386 B_TST_CONFIG);
2387 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2388 pr_debug("b_ase0_brst_tmout\n");
2389 pr_info("B HNP fail:No response from A device\n");
2390 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002391 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302392 otg->host->is_b_host = 0;
2393 clear_bit(B_ASE0_BRST, &motg->tmouts);
2394 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2395 clear_bit(B_BUS_REQ, &motg->inputs);
2396 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002397 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302398 work = 1;
2399 } else if (test_bit(ID_C, &motg->inputs)) {
2400 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2401 }
2402 break;
2403 case OTG_STATE_B_HOST:
2404 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2405 !test_bit(A_CONN, &motg->inputs) ||
2406 !test_bit(B_SESS_VLD, &motg->inputs)) {
2407 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2408 clear_bit(A_CONN, &motg->inputs);
2409 clear_bit(B_BUS_REQ, &motg->inputs);
2410 msm_otg_start_host(otg, 0);
2411 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002412 otg->phy->state = OTG_STATE_B_IDLE;
2413 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302414 work = 1;
2415 } else if (test_bit(ID_C, &motg->inputs)) {
2416 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2417 }
2418 break;
2419 case OTG_STATE_A_IDLE:
2420 otg->default_a = 1;
2421 if (test_bit(ID, &motg->inputs) &&
2422 !test_bit(ID_A, &motg->inputs)) {
2423 pr_debug("id && !id_a\n");
2424 otg->default_a = 0;
2425 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002426 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302427 del_timer_sync(&motg->id_timer);
2428 msm_otg_link_reset(motg);
2429 msm_chg_enable_aca_intr(motg);
2430 msm_otg_notify_charger(motg, 0);
2431 work = 1;
2432 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2433 (test_bit(A_SRP_DET, &motg->inputs) ||
2434 test_bit(A_BUS_REQ, &motg->inputs))) {
2435 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2436
2437 clear_bit(A_SRP_DET, &motg->inputs);
2438 /* Disable SRP detection */
2439 writel_relaxed((readl_relaxed(USB_OTGSC) &
2440 ~OTGSC_INTSTS_MASK) &
2441 ~OTGSC_DPIE, USB_OTGSC);
2442
Steve Mucklef132c6c2012-06-06 18:30:57 -07002443 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302444 /* VBUS should not be supplied before end of SRP pulse
2445 * generated by PET, if not complaince test fail.
2446 */
2447 usleep_range(10000, 12000);
2448 /* ACA: ID_A: Stop charging untill enumeration */
2449 if (test_bit(ID_A, &motg->inputs))
2450 msm_otg_notify_charger(motg, 0);
2451 else
2452 msm_hsusb_vbus_power(motg, 1);
2453 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2454 } else {
2455 pr_debug("No session requested\n");
2456 clear_bit(A_BUS_DROP, &motg->inputs);
2457 if (test_bit(ID_A, &motg->inputs)) {
2458 msm_otg_notify_charger(motg,
2459 IDEV_ACA_CHG_MAX);
2460 } else if (!test_bit(ID, &motg->inputs)) {
2461 msm_otg_notify_charger(motg, 0);
2462 /*
2463 * A-device is not providing power on VBUS.
2464 * Enable SRP detection.
2465 */
2466 writel_relaxed(0x13, USB_USBMODE);
2467 writel_relaxed((readl_relaxed(USB_OTGSC) &
2468 ~OTGSC_INTSTS_MASK) |
2469 OTGSC_DPIE, USB_OTGSC);
2470 mb();
2471 }
2472 }
2473 break;
2474 case OTG_STATE_A_WAIT_VRISE:
2475 if ((test_bit(ID, &motg->inputs) &&
2476 !test_bit(ID_A, &motg->inputs)) ||
2477 test_bit(A_BUS_DROP, &motg->inputs) ||
2478 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2479 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2480 clear_bit(A_BUS_REQ, &motg->inputs);
2481 msm_otg_del_timer(motg);
2482 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002483 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302484 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2485 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2486 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002487 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302488 if (TA_WAIT_BCON > 0)
2489 msm_otg_start_timer(motg, TA_WAIT_BCON,
2490 A_WAIT_BCON);
2491 msm_otg_start_host(otg, 1);
2492 msm_chg_enable_aca_det(motg);
2493 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302494 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302495 if (msm_chg_check_aca_intr(motg))
2496 work = 1;
2497 }
2498 break;
2499 case OTG_STATE_A_WAIT_BCON:
2500 if ((test_bit(ID, &motg->inputs) &&
2501 !test_bit(ID_A, &motg->inputs)) ||
2502 test_bit(A_BUS_DROP, &motg->inputs) ||
2503 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2504 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2505 "a_wait_bcon_tmout\n");
2506 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2507 pr_info("Device No Response\n");
2508 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2509 }
2510 msm_otg_del_timer(motg);
2511 clear_bit(A_BUS_REQ, &motg->inputs);
2512 clear_bit(B_CONN, &motg->inputs);
2513 msm_otg_start_host(otg, 0);
2514 /*
2515 * ACA: ID_A with NO accessory, just the A plug is
2516 * attached to ACA: Use IDCHG_MAX for charging
2517 */
2518 if (test_bit(ID_A, &motg->inputs))
2519 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2520 else
2521 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002522 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302523 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2524 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2525 pr_debug("!a_vbus_vld\n");
2526 clear_bit(B_CONN, &motg->inputs);
2527 msm_otg_del_timer(motg);
2528 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002529 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2530 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302531 } else if (test_bit(ID_A, &motg->inputs)) {
2532 msm_hsusb_vbus_power(motg, 0);
2533 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2534 /*
2535 * If TA_WAIT_BCON is infinite, we don;t
2536 * turn off VBUS. Enter low power mode.
2537 */
2538 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002539 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 } else if (!test_bit(ID, &motg->inputs)) {
2541 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302542 }
2543 break;
2544 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302545 if ((test_bit(ID, &motg->inputs) &&
2546 !test_bit(ID_A, &motg->inputs)) ||
2547 test_bit(A_BUS_DROP, &motg->inputs)) {
2548 pr_debug("id_a/b/c || a_bus_drop\n");
2549 clear_bit(B_CONN, &motg->inputs);
2550 clear_bit(A_BUS_REQ, &motg->inputs);
2551 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002552 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302553 msm_otg_start_host(otg, 0);
2554 if (!test_bit(ID_A, &motg->inputs))
2555 msm_hsusb_vbus_power(motg, 0);
2556 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2557 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2558 pr_debug("!a_vbus_vld\n");
2559 clear_bit(B_CONN, &motg->inputs);
2560 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002561 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302562 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002563 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302564 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2565 /*
2566 * a_bus_req is de-asserted when root hub is
2567 * suspended or HNP is in progress.
2568 */
2569 pr_debug("!a_bus_req\n");
2570 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002571 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 if (otg->host->b_hnp_enable)
2573 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2574 A_AIDL_BDIS);
2575 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002576 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302577 } else if (!test_bit(B_CONN, &motg->inputs)) {
2578 pr_debug("!b_conn\n");
2579 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002580 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302581 if (TA_WAIT_BCON > 0)
2582 msm_otg_start_timer(motg, TA_WAIT_BCON,
2583 A_WAIT_BCON);
2584 if (msm_chg_check_aca_intr(motg))
2585 work = 1;
2586 } else if (test_bit(ID_A, &motg->inputs)) {
2587 msm_otg_del_timer(motg);
2588 msm_hsusb_vbus_power(motg, 0);
2589 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2590 msm_otg_notify_charger(motg,
2591 IDEV_ACA_CHG_MAX);
2592 else
2593 msm_otg_notify_charger(motg,
2594 IDEV_CHG_MIN - motg->mA_port);
2595 } else if (!test_bit(ID, &motg->inputs)) {
2596 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2597 motg->chg_type = USB_INVALID_CHARGER;
2598 msm_otg_notify_charger(motg, 0);
2599 msm_hsusb_vbus_power(motg, 1);
2600 }
2601 break;
2602 case OTG_STATE_A_SUSPEND:
2603 if ((test_bit(ID, &motg->inputs) &&
2604 !test_bit(ID_A, &motg->inputs)) ||
2605 test_bit(A_BUS_DROP, &motg->inputs) ||
2606 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2607 pr_debug("id_a/b/c || a_bus_drop ||"
2608 "a_aidl_bdis_tmout\n");
2609 msm_otg_del_timer(motg);
2610 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002611 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302612 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002613 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302614 if (!test_bit(ID_A, &motg->inputs))
2615 msm_hsusb_vbus_power(motg, 0);
2616 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2617 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2618 pr_debug("!a_vbus_vld\n");
2619 msm_otg_del_timer(motg);
2620 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002621 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302622 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002623 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302624 } else if (!test_bit(B_CONN, &motg->inputs) &&
2625 otg->host->b_hnp_enable) {
2626 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002627 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302628 msm_otg_host_hnp_enable(otg, 1);
2629 otg->gadget->is_a_peripheral = 1;
2630 msm_otg_start_peripheral(otg, 1);
2631 } else if (!test_bit(B_CONN, &motg->inputs) &&
2632 !otg->host->b_hnp_enable) {
2633 pr_debug("!b_conn && !b_hnp_enable");
2634 /*
2635 * bus request is dropped during suspend.
2636 * acquire again for next device.
2637 */
2638 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002639 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302640 if (TA_WAIT_BCON > 0)
2641 msm_otg_start_timer(motg, TA_WAIT_BCON,
2642 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002643 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302644 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002645 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302646 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002647 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002648 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302649 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302650 }
2651 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302652 case OTG_STATE_A_PERIPHERAL:
2653 if ((test_bit(ID, &motg->inputs) &&
2654 !test_bit(ID_A, &motg->inputs)) ||
2655 test_bit(A_BUS_DROP, &motg->inputs)) {
2656 pr_debug("id _f/b/c || a_bus_drop\n");
2657 /* Clear BIDL_ADIS timer */
2658 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002659 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302660 msm_otg_start_peripheral(otg, 0);
2661 otg->gadget->is_a_peripheral = 0;
2662 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002663 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302664 if (!test_bit(ID_A, &motg->inputs))
2665 msm_hsusb_vbus_power(motg, 0);
2666 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2667 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2668 pr_debug("!a_vbus_vld\n");
2669 /* Clear BIDL_ADIS timer */
2670 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002671 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302672 msm_otg_start_peripheral(otg, 0);
2673 otg->gadget->is_a_peripheral = 0;
2674 msm_otg_start_host(otg, 0);
2675 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2676 pr_debug("a_bidl_adis_tmout\n");
2677 msm_otg_start_peripheral(otg, 0);
2678 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002679 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302680 set_bit(A_BUS_REQ, &motg->inputs);
2681 msm_otg_host_hnp_enable(otg, 0);
2682 if (TA_WAIT_BCON > 0)
2683 msm_otg_start_timer(motg, TA_WAIT_BCON,
2684 A_WAIT_BCON);
2685 } else if (test_bit(ID_A, &motg->inputs)) {
2686 msm_hsusb_vbus_power(motg, 0);
2687 msm_otg_notify_charger(motg,
2688 IDEV_CHG_MIN - motg->mA_port);
2689 } else if (!test_bit(ID, &motg->inputs)) {
2690 msm_otg_notify_charger(motg, 0);
2691 msm_hsusb_vbus_power(motg, 1);
2692 }
2693 break;
2694 case OTG_STATE_A_WAIT_VFALL:
2695 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2696 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002697 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302698 work = 1;
2699 }
2700 break;
2701 case OTG_STATE_A_VBUS_ERR:
2702 if ((test_bit(ID, &motg->inputs) &&
2703 !test_bit(ID_A, &motg->inputs)) ||
2704 test_bit(A_BUS_DROP, &motg->inputs) ||
2705 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002706 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302707 if (!test_bit(ID_A, &motg->inputs))
2708 msm_hsusb_vbus_power(motg, 0);
2709 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2710 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2711 motg->chg_type = USB_INVALID_CHARGER;
2712 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302713 }
2714 break;
2715 default:
2716 break;
2717 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302718 if (work)
2719 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302720}
2721
2722static irqreturn_t msm_otg_irq(int irq, void *data)
2723{
2724 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002725 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302726 u32 otgsc = 0, usbsts, pc;
2727 bool work = 0;
2728 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302729
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302730 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302731 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302732 disable_irq_nosync(irq);
2733 motg->async_int = 1;
Vijayavardhan Vennapusa6d783712012-08-06 17:18:56 +05302734 if (atomic_read(&motg->pm_suspended)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002735 motg->sm_work_pending = true;
Vijayavardhan Vennapusa6d783712012-08-06 17:18:56 +05302736 if ((otg->phy->state == OTG_STATE_A_SUSPEND) ||
2737 (otg->phy->state == OTG_STATE_A_WAIT_BCON))
2738 set_bit(A_BUS_REQ, &motg->inputs);
2739 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002740 pm_request_resume(otg->phy->dev);
Vijayavardhan Vennapusa6d783712012-08-06 17:18:56 +05302741 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302742 return IRQ_HANDLED;
2743 }
2744
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002745 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302746 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302747
2748 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302749 return IRQ_NONE;
2750
2751 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302752 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302753 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302754 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302755 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302756 pr_debug("Id clear\n");
2757 /*
2758 * Assert a_bus_req to supply power on
2759 * VBUS when Micro/Mini-A cable is connected
2760 * with out user intervention.
2761 */
2762 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302763 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302764 msm_chg_enable_aca_det(motg);
2765 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302766 writel_relaxed(otgsc, USB_OTGSC);
2767 work = 1;
2768 } else if (otgsc & OTGSC_DPIS) {
2769 pr_debug("DPIS detected\n");
2770 writel_relaxed(otgsc, USB_OTGSC);
2771 set_bit(A_SRP_DET, &motg->inputs);
2772 set_bit(A_BUS_REQ, &motg->inputs);
2773 work = 1;
2774 } else if (otgsc & OTGSC_BSVIS) {
2775 writel_relaxed(otgsc, USB_OTGSC);
2776 /*
2777 * BSV interrupt comes when operating as an A-device
2778 * (VBUS on/off).
2779 * But, handle BSV when charger is removed from ACA in ID_A
2780 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002781 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302782 !test_bit(ID_A, &motg->inputs))
2783 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302784 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302785 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302786 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302787 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302788 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302789 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002790 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2791
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302792 msm_chg_check_aca_intr(motg);
2793 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302794 work = 1;
2795 } else if (usbsts & STS_PCI) {
2796 pc = readl_relaxed(USB_PORTSC);
2797 pr_debug("portsc = %x\n", pc);
2798 ret = IRQ_NONE;
2799 /*
2800 * HCD Acks PCI interrupt. We use this to switch
2801 * between different OTG states.
2802 */
2803 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002804 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302805 case OTG_STATE_A_SUSPEND:
2806 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2807 !(pc & PORTSC_CCS)) {
2808 pr_debug("B_CONN clear\n");
2809 clear_bit(B_CONN, &motg->inputs);
2810 msm_otg_del_timer(motg);
2811 }
2812 break;
2813 case OTG_STATE_A_PERIPHERAL:
2814 /*
2815 * A-peripheral observed activity on bus.
2816 * clear A_BIDL_ADIS timer.
2817 */
2818 msm_otg_del_timer(motg);
2819 work = 0;
2820 break;
2821 case OTG_STATE_B_WAIT_ACON:
2822 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2823 pr_debug("A_CONN set\n");
2824 set_bit(A_CONN, &motg->inputs);
2825 /* Clear ASE0_BRST timer */
2826 msm_otg_del_timer(motg);
2827 }
2828 break;
2829 case OTG_STATE_B_HOST:
2830 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2831 pr_debug("A_CONN clear\n");
2832 clear_bit(A_CONN, &motg->inputs);
2833 msm_otg_del_timer(motg);
2834 }
2835 break;
2836 case OTG_STATE_A_WAIT_BCON:
2837 if (TA_WAIT_BCON < 0)
2838 set_bit(A_BUS_REQ, &motg->inputs);
2839 default:
2840 work = 0;
2841 break;
2842 }
2843 } else if (usbsts & STS_URI) {
2844 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002845 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302846 case OTG_STATE_A_PERIPHERAL:
2847 /*
2848 * A-peripheral observed activity on bus.
2849 * clear A_BIDL_ADIS timer.
2850 */
2851 msm_otg_del_timer(motg);
2852 work = 0;
2853 break;
2854 default:
2855 work = 0;
2856 break;
2857 }
2858 } else if (usbsts & STS_SLI) {
2859 ret = IRQ_NONE;
2860 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002861 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302862 case OTG_STATE_B_PERIPHERAL:
2863 if (otg->gadget->b_hnp_enable) {
2864 set_bit(A_BUS_SUSPEND, &motg->inputs);
2865 set_bit(B_BUS_REQ, &motg->inputs);
2866 work = 1;
2867 }
2868 break;
2869 case OTG_STATE_A_PERIPHERAL:
2870 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2871 A_BIDL_ADIS);
2872 break;
2873 default:
2874 break;
2875 }
2876 } else if ((usbsts & PHY_ALT_INT)) {
2877 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2878 if (msm_chg_check_aca_intr(motg))
2879 work = 1;
2880 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302881 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302882 if (work)
2883 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302884
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302885 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002886}
2887
2888static void msm_otg_set_vbus_state(int online)
2889{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302890 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002891 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302892 struct usb_otg *otg = motg->phy.otg;
2893
2894 /* In A Host Mode, ignore received BSV interrupts */
2895 if (otg->phy->state >= OTG_STATE_A_IDLE)
2896 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002897
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302898 if (online) {
2899 pr_debug("PMIC: BSV set\n");
2900 set_bit(B_SESS_VLD, &motg->inputs);
2901 } else {
2902 pr_debug("PMIC: BSV clear\n");
2903 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302904 }
2905
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302906 if (!init) {
2907 init = true;
2908 complete(&pmic_vbus_init);
2909 pr_debug("PMIC: BSV init complete\n");
2910 return;
2911 }
2912
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302913 if (test_bit(MHL, &motg->inputs) ||
2914 mhl_det_in_progress) {
2915 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2916 return;
2917 }
2918
Jack Pham5ca279b2012-05-14 18:42:54 -07002919 if (atomic_read(&motg->pm_suspended))
2920 motg->sm_work_pending = true;
2921 else
2922 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002923}
2924
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302925static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002926{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302927 struct msm_otg *motg = container_of(w, struct msm_otg,
2928 pmic_id_status_work.work);
2929 int work = 0;
2930 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002931
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302932 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302933 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302934 if (!test_and_set_bit(ID, &motg->inputs)) {
2935 pr_debug("PMIC: ID set\n");
2936 work = 1;
2937 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302938 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302939 if (test_and_clear_bit(ID, &motg->inputs)) {
2940 pr_debug("PMIC: ID clear\n");
2941 set_bit(A_BUS_REQ, &motg->inputs);
2942 work = 1;
2943 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302944 }
2945
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302946 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002947 if (atomic_read(&motg->pm_suspended))
2948 motg->sm_work_pending = true;
2949 else
2950 queue_work(system_nrt_wq, &motg->sm_work);
2951 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302952 local_irq_restore(flags);
2953
2954}
2955
2956#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2957static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2958{
2959 struct msm_otg *motg = data;
2960
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302961 if (test_bit(MHL, &motg->inputs) ||
2962 mhl_det_in_progress) {
2963 pr_debug("PMIC: Id interrupt ignored in MHL\n");
2964 return IRQ_HANDLED;
2965 }
2966
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302967 if (!aca_id_turned_on)
2968 /*schedule delayed work for 5msec for ID line state to settle*/
2969 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2970 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002971
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302972 return IRQ_HANDLED;
2973}
2974
2975static int msm_otg_mode_show(struct seq_file *s, void *unused)
2976{
2977 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002978 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302979
Steve Mucklef132c6c2012-06-06 18:30:57 -07002980 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302981 case OTG_STATE_A_HOST:
2982 seq_printf(s, "host\n");
2983 break;
2984 case OTG_STATE_B_PERIPHERAL:
2985 seq_printf(s, "peripheral\n");
2986 break;
2987 default:
2988 seq_printf(s, "none\n");
2989 break;
2990 }
2991
2992 return 0;
2993}
2994
2995static int msm_otg_mode_open(struct inode *inode, struct file *file)
2996{
2997 return single_open(file, msm_otg_mode_show, inode->i_private);
2998}
2999
3000static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
3001 size_t count, loff_t *ppos)
3002{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05303003 struct seq_file *s = file->private_data;
3004 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303005 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07003006 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303007 int status = count;
3008 enum usb_mode_type req_mode;
3009
3010 memset(buf, 0x00, sizeof(buf));
3011
3012 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3013 status = -EFAULT;
3014 goto out;
3015 }
3016
3017 if (!strncmp(buf, "host", 4)) {
3018 req_mode = USB_HOST;
3019 } else if (!strncmp(buf, "peripheral", 10)) {
3020 req_mode = USB_PERIPHERAL;
3021 } else if (!strncmp(buf, "none", 4)) {
3022 req_mode = USB_NONE;
3023 } else {
3024 status = -EINVAL;
3025 goto out;
3026 }
3027
3028 switch (req_mode) {
3029 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003030 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303031 case OTG_STATE_A_HOST:
3032 case OTG_STATE_B_PERIPHERAL:
3033 set_bit(ID, &motg->inputs);
3034 clear_bit(B_SESS_VLD, &motg->inputs);
3035 break;
3036 default:
3037 goto out;
3038 }
3039 break;
3040 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003041 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303042 case OTG_STATE_B_IDLE:
3043 case OTG_STATE_A_HOST:
3044 set_bit(ID, &motg->inputs);
3045 set_bit(B_SESS_VLD, &motg->inputs);
3046 break;
3047 default:
3048 goto out;
3049 }
3050 break;
3051 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003052 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303053 case OTG_STATE_B_IDLE:
3054 case OTG_STATE_B_PERIPHERAL:
3055 clear_bit(ID, &motg->inputs);
3056 break;
3057 default:
3058 goto out;
3059 }
3060 break;
3061 default:
3062 goto out;
3063 }
3064
Steve Mucklef132c6c2012-06-06 18:30:57 -07003065 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303066 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303067out:
3068 return status;
3069}
3070
3071const struct file_operations msm_otg_mode_fops = {
3072 .open = msm_otg_mode_open,
3073 .read = seq_read,
3074 .write = msm_otg_mode_write,
3075 .llseek = seq_lseek,
3076 .release = single_release,
3077};
3078
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303079static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3080{
3081 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003082 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303083
Steve Mucklef132c6c2012-06-06 18:30:57 -07003084 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303085 return 0;
3086}
3087
3088static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3089{
3090 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3091}
3092
3093const struct file_operations msm_otg_state_fops = {
3094 .open = msm_otg_otg_state_open,
3095 .read = seq_read,
3096 .llseek = seq_lseek,
3097 .release = single_release,
3098};
3099
Anji jonnalad270e2d2011-08-09 11:28:32 +05303100static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3101{
3102 struct msm_otg *motg = s->private;
3103
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303104 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303105 return 0;
3106}
3107
3108static int msm_otg_chg_open(struct inode *inode, struct file *file)
3109{
3110 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3111}
3112
3113const struct file_operations msm_otg_chg_fops = {
3114 .open = msm_otg_chg_open,
3115 .read = seq_read,
3116 .llseek = seq_lseek,
3117 .release = single_release,
3118};
3119
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303120static int msm_otg_aca_show(struct seq_file *s, void *unused)
3121{
3122 if (debug_aca_enabled)
3123 seq_printf(s, "enabled\n");
3124 else
3125 seq_printf(s, "disabled\n");
3126
3127 return 0;
3128}
3129
3130static int msm_otg_aca_open(struct inode *inode, struct file *file)
3131{
3132 return single_open(file, msm_otg_aca_show, inode->i_private);
3133}
3134
3135static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3136 size_t count, loff_t *ppos)
3137{
3138 char buf[8];
3139
3140 memset(buf, 0x00, sizeof(buf));
3141
3142 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3143 return -EFAULT;
3144
3145 if (!strncmp(buf, "enable", 6))
3146 debug_aca_enabled = true;
3147 else
3148 debug_aca_enabled = false;
3149
3150 return count;
3151}
3152
3153const struct file_operations msm_otg_aca_fops = {
3154 .open = msm_otg_aca_open,
3155 .read = seq_read,
3156 .write = msm_otg_aca_write,
3157 .llseek = seq_lseek,
3158 .release = single_release,
3159};
3160
Manu Gautam8bdcc592012-03-06 11:26:06 +05303161static int msm_otg_bus_show(struct seq_file *s, void *unused)
3162{
3163 if (debug_bus_voting_enabled)
3164 seq_printf(s, "enabled\n");
3165 else
3166 seq_printf(s, "disabled\n");
3167
3168 return 0;
3169}
3170
3171static int msm_otg_bus_open(struct inode *inode, struct file *file)
3172{
3173 return single_open(file, msm_otg_bus_show, inode->i_private);
3174}
3175
3176static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3177 size_t count, loff_t *ppos)
3178{
3179 char buf[8];
3180 int ret;
3181 struct seq_file *s = file->private_data;
3182 struct msm_otg *motg = s->private;
3183
3184 memset(buf, 0x00, sizeof(buf));
3185
3186 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3187 return -EFAULT;
3188
3189 if (!strncmp(buf, "enable", 6)) {
3190 /* Do not vote here. Let OTG statemachine decide when to vote */
3191 debug_bus_voting_enabled = true;
3192 } else {
3193 debug_bus_voting_enabled = false;
3194 if (motg->bus_perf_client) {
3195 ret = msm_bus_scale_client_update_request(
3196 motg->bus_perf_client, 0);
3197 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003198 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303199 "for bus bw %d\n", __func__, ret);
3200 }
3201 }
3202
3203 return count;
3204}
3205
3206const struct file_operations msm_otg_bus_fops = {
3207 .open = msm_otg_bus_open,
3208 .read = seq_read,
3209 .write = msm_otg_bus_write,
3210 .llseek = seq_lseek,
3211 .release = single_release,
3212};
3213
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303214static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303215
3216static int msm_otg_debugfs_init(struct msm_otg *motg)
3217{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303218 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303219
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303220 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3221
3222 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3223 return -ENODEV;
3224
Anji jonnalad270e2d2011-08-09 11:28:32 +05303225 if (motg->pdata->mode == USB_OTG &&
3226 motg->pdata->otg_control == OTG_USER_CONTROL) {
3227
Manu Gautam8bdcc592012-03-06 11:26:06 +05303228 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303229 S_IWUSR, msm_otg_dbg_root, motg,
3230 &msm_otg_mode_fops);
3231
Manu Gautam8bdcc592012-03-06 11:26:06 +05303232 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303233 debugfs_remove(msm_otg_dbg_root);
3234 msm_otg_dbg_root = NULL;
3235 return -ENODEV;
3236 }
3237 }
3238
Manu Gautam8bdcc592012-03-06 11:26:06 +05303239 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303240 msm_otg_dbg_root, motg,
3241 &msm_otg_chg_fops);
3242
Manu Gautam8bdcc592012-03-06 11:26:06 +05303243 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303244 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303245 return -ENODEV;
3246 }
3247
Manu Gautam8bdcc592012-03-06 11:26:06 +05303248 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303249 msm_otg_dbg_root, motg,
3250 &msm_otg_aca_fops);
3251
Manu Gautam8bdcc592012-03-06 11:26:06 +05303252 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303253 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303254 return -ENODEV;
3255 }
3256
Manu Gautam8bdcc592012-03-06 11:26:06 +05303257 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3258 msm_otg_dbg_root, motg,
3259 &msm_otg_bus_fops);
3260
3261 if (!msm_otg_dentry) {
3262 debugfs_remove_recursive(msm_otg_dbg_root);
3263 return -ENODEV;
3264 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303265
3266 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3267 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3268
3269 if (!msm_otg_dentry) {
3270 debugfs_remove_recursive(msm_otg_dbg_root);
3271 return -ENODEV;
3272 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303273 return 0;
3274}
3275
3276static void msm_otg_debugfs_cleanup(void)
3277{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303278 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303279}
3280
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303281static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3282static struct platform_device *msm_otg_add_pdev(
3283 struct platform_device *ofdev, const char *name)
3284{
3285 struct platform_device *pdev;
3286 const struct resource *res = ofdev->resource;
3287 unsigned int num = ofdev->num_resources;
3288 int retval;
3289
3290 pdev = platform_device_alloc(name, -1);
3291 if (!pdev) {
3292 retval = -ENOMEM;
3293 goto error;
3294 }
3295
3296 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3297 pdev->dev.dma_mask = &msm_otg_dma_mask;
3298
3299 if (num) {
3300 retval = platform_device_add_resources(pdev, res, num);
3301 if (retval)
3302 goto error;
3303 }
3304
3305 retval = platform_device_add(pdev);
3306 if (retval)
3307 goto error;
3308
3309 return pdev;
3310
3311error:
3312 platform_device_put(pdev);
3313 return ERR_PTR(retval);
3314}
3315
3316static int msm_otg_setup_devices(struct platform_device *ofdev,
3317 enum usb_mode_type mode, bool init)
3318{
3319 const char *gadget_name = "msm_hsusb";
3320 const char *host_name = "msm_hsusb_host";
3321 static struct platform_device *gadget_pdev;
3322 static struct platform_device *host_pdev;
3323 int retval = 0;
3324
3325 if (!init) {
3326 if (gadget_pdev)
3327 platform_device_unregister(gadget_pdev);
3328 if (host_pdev)
3329 platform_device_unregister(host_pdev);
3330 return 0;
3331 }
3332
3333 switch (mode) {
3334 case USB_OTG:
3335 /* fall through */
3336 case USB_PERIPHERAL:
3337 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3338 if (IS_ERR(gadget_pdev)) {
3339 retval = PTR_ERR(gadget_pdev);
3340 break;
3341 }
3342 if (mode == USB_PERIPHERAL)
3343 break;
3344 /* fall through */
3345 case USB_HOST:
3346 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3347 if (IS_ERR(host_pdev)) {
3348 retval = PTR_ERR(host_pdev);
3349 if (mode == USB_OTG)
3350 platform_device_unregister(gadget_pdev);
3351 }
3352 break;
3353 default:
3354 break;
3355 }
3356
3357 return retval;
3358}
3359
3360struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3361{
3362 struct device_node *node = pdev->dev.of_node;
3363 struct msm_otg_platform_data *pdata;
3364 int len = 0;
3365
3366 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3367 if (!pdata) {
3368 pr_err("unable to allocate platform data\n");
3369 return NULL;
3370 }
3371 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3372 if (len) {
3373 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3374 if (!pdata->phy_init_seq)
3375 return NULL;
3376 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3377 pdata->phy_init_seq,
3378 len/sizeof(*pdata->phy_init_seq));
3379 }
3380 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3381 &pdata->power_budget);
3382 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3383 &pdata->mode);
3384 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3385 &pdata->otg_control);
3386 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3387 &pdata->default_mode);
3388 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3389 &pdata->phy_type);
3390 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3391 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303392 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3393 "qcom,hsusb-otg-disable-reset");
3394
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303395 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303396}
3397
3398static int __init msm_otg_probe(struct platform_device *pdev)
3399{
3400 int ret = 0;
3401 struct resource *res;
3402 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003403 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303404 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303405
3406 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303407
3408 if (pdev->dev.of_node) {
3409 dev_dbg(&pdev->dev, "device tree enabled\n");
3410 pdata = msm_otg_dt_to_pdata(pdev);
3411 if (!pdata)
3412 return -ENOMEM;
3413 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3414 if (ret) {
3415 dev_err(&pdev->dev, "devices setup failed\n");
3416 return ret;
3417 }
3418 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303419 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3420 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303421 } else {
3422 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303423 }
3424
3425 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3426 if (!motg) {
3427 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3428 return -ENOMEM;
3429 }
3430
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003431 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3432 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003433 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3434 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303435 goto free_motg;
3436 }
3437
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003438 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303439 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003440 phy = &motg->phy;
3441 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303442
3443 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303444 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3445 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3446 * PMIC can detect ACA ID_GND and generate an interrupt.
3447 */
3448 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3449 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3450 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003451 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303452 }
3453
Ofir Cohen4da266f2012-01-03 10:19:29 +02003454 /* initialize reset counter */
3455 motg->reset_counter = 0;
3456
Amit Blay02eff132011-09-21 16:46:24 +03003457 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003458 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003459 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003460 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303461
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303462 /*
3463 * Targets on which link uses asynchronous reset methodology,
3464 * free running clock is not required during the reset.
3465 */
Manu Gautam5143b252012-01-05 19:25:23 -08003466 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303467 if (IS_ERR(motg->clk))
3468 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3469 else
3470 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303471
3472 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003473 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303474 * CORE CLK must be running at >55Mhz for correct HSUSB
3475 * operation and USB core cannot tolerate frequency changes on
3476 * CORE CLK. For such USB cores, vote for maximum clk frequency
3477 * on pclk source
3478 */
Manu Gautam5143b252012-01-05 19:25:23 -08003479 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3480 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303481 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003482 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303483 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003484 goto put_clk;
3485 }
3486 clk_set_rate(motg->core_clk, INT_MAX);
3487
3488 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3489 if (IS_ERR(motg->pclk)) {
3490 dev_err(&pdev->dev, "failed to get iface_clk\n");
3491 ret = PTR_ERR(motg->pclk);
3492 goto put_core_clk;
3493 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303494
3495 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3496 if (!res) {
3497 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3498 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003499 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303500 }
3501
3502 motg->regs = ioremap(res->start, resource_size(res));
3503 if (!motg->regs) {
3504 dev_err(&pdev->dev, "ioremap failed\n");
3505 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003506 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303507 }
3508 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3509
3510 motg->irq = platform_get_irq(pdev, 0);
3511 if (!motg->irq) {
3512 dev_err(&pdev->dev, "platform_get_irq failed\n");
3513 ret = -ENODEV;
3514 goto free_regs;
3515 }
3516
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003517 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003518 if (IS_ERR(motg->xo_handle)) {
3519 dev_err(&pdev->dev, "%s not able to get the handle "
3520 "to vote for TCXO D0 buffer\n", __func__);
3521 ret = PTR_ERR(motg->xo_handle);
3522 goto free_regs;
3523 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303524
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003525 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003526 if (ret) {
3527 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3528 "D0 buffer%d\n", __func__, ret);
3529 goto free_xo_handle;
3530 }
3531
Manu Gautam28b1bac2012-01-30 16:43:06 +05303532 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303533
Mayank Rana248698c2012-04-19 00:03:16 +05303534 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003535 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303536 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003537 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303538 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003539 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003540 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303541 goto devote_xo_handle;
3542 }
3543 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303544 }
3545
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003546 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303547 if (ret) {
3548 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303549 goto devote_xo_handle;
3550 }
3551
3552 ret = regulator_enable(hsusb_vddcx);
3553 if (ret) {
3554 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3555 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303556 }
3557
3558 ret = msm_hsusb_ldo_init(motg, 1);
3559 if (ret) {
3560 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303561 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303562 }
3563
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303564 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303565 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3566 "mhl_usb_hs_switch");
3567 if (IS_ERR(mhl_usb_hs_switch)) {
3568 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003569 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303570 goto free_ldo_init;
3571 }
3572 }
3573
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003574 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303575 if (ret) {
3576 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003577 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303578 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303579 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303580
3581 writel(0, USB_USBINTR);
3582 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003583 /* Ensure that above STOREs are completed before enabling interrupts */
3584 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303585
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303586 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3587 if (ret)
3588 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003589 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303590 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303591 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303592 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303593 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303594 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3595 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303596 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3597 "msm_otg", motg);
3598 if (ret) {
3599 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003600 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303601 }
3602
Jack Pham87f202f2012-08-06 00:24:22 -07003603 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3604 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3605
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003606 phy->init = msm_otg_reset;
3607 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003608 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303609
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003610 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303611
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003612 phy->otg->phy = &motg->phy;
3613 phy->otg->set_host = msm_otg_set_host;
3614 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003615 phy->otg->start_hnp = msm_otg_start_hnp;
3616 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003617
3618 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303619 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003620 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303621 goto free_irq;
3622 }
3623
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303624 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303625 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003626 if (motg->pdata->pmic_id_irq) {
3627 ret = request_irq(motg->pdata->pmic_id_irq,
3628 msm_pmic_id_irq,
3629 IRQF_TRIGGER_RISING |
3630 IRQF_TRIGGER_FALLING,
3631 "msm_otg", motg);
3632 if (ret) {
3633 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003634 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003635 }
3636 } else {
3637 ret = -ENODEV;
3638 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003639 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003640 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303641 }
3642
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303643 msm_hsusb_mhl_switch_enable(motg, 1);
3644
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303645 platform_set_drvdata(pdev, motg);
3646 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003647 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303648
Anji jonnalad270e2d2011-08-09 11:28:32 +05303649 ret = msm_otg_debugfs_init(motg);
3650 if (ret)
3651 dev_dbg(&pdev->dev, "mode debugfs file is"
3652 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303653
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003654 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3655 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3656
Amit Blay58b31472011-11-18 09:39:39 +02003657 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3658 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303659 (!(motg->pdata->mode == USB_OTG) ||
3660 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003661 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303662 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003663
Amit Blay58b31472011-11-18 09:39:39 +02003664 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3665 motg->caps = ALLOW_PHY_RETENTION;
3666 }
3667
Amit Blay6fa647a2012-05-24 14:12:08 +03003668 if (motg->pdata->enable_lpm_on_dev_suspend)
3669 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3670
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003671 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303672 pm_runtime_set_active(&pdev->dev);
3673 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303674
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303675 if (motg->pdata->bus_scale_table) {
3676 motg->bus_perf_client =
3677 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3678 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003679 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303680 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303681 else
3682 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303683 }
3684
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303685 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003686
Steve Mucklef132c6c2012-06-06 18:30:57 -07003687remove_phy:
3688 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303689free_irq:
3690 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003691destroy_wlock:
3692 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303693 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003694 msm_hsusb_ldo_enable(motg, 0);
3695free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303696 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303697free_hsusb_vddcx:
3698 regulator_disable(hsusb_vddcx);
3699free_config_vddcx:
3700 regulator_set_voltage(hsusb_vddcx,
3701 vdd_val[motg->vdd_type][VDD_NONE],
3702 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003703devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303704 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003705 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003706free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003707 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303708free_regs:
3709 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003710put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303711 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303712put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003713 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303714put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303715 if (!IS_ERR(motg->clk))
3716 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003717 if (!IS_ERR(motg->phy_reset_clk))
3718 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003719free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003720 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303721free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303722 kfree(motg);
3723 return ret;
3724}
3725
3726static int __devexit msm_otg_remove(struct platform_device *pdev)
3727{
3728 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003729 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303730 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303731
3732 if (otg->host || otg->gadget)
3733 return -EBUSY;
3734
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303735 if (pdev->dev.of_node)
3736 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003737 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3738 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303739 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303740 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303741 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303742 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303743 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303744
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303745 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303746
3747 device_init_wakeup(&pdev->dev, 0);
3748 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003749 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303750
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303751 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003752 if (motg->pdata->pmic_id_irq)
3753 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003754 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303755 free_irq(motg->irq, motg);
3756
Jack Pham87f202f2012-08-06 00:24:22 -07003757 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3758 motg->pdata->mpm_otgsessvld_int)
3759 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3760
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303761 /*
3762 * Put PHY in low power mode.
3763 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003764 ulpi_read(otg->phy, 0x14);
3765 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303766
3767 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3768 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3769 if (readl(USB_PORTSC) & PORTSC_PHCD)
3770 break;
3771 udelay(1);
3772 cnt++;
3773 }
3774 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003775 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303776
Manu Gautam28b1bac2012-01-30 16:43:06 +05303777 clk_disable_unprepare(motg->pclk);
3778 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003779 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003780 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303781 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303782 regulator_disable(hsusb_vddcx);
3783 regulator_set_voltage(hsusb_vddcx,
3784 vdd_val[motg->vdd_type][VDD_NONE],
3785 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303786
3787 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303788 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303789
Amit Blay02eff132011-09-21 16:46:24 +03003790 if (!IS_ERR(motg->phy_reset_clk))
3791 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303792 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303793 if (!IS_ERR(motg->clk))
3794 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003795 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303796
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303797 if (motg->bus_perf_client)
3798 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303799
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003800 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303801 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303802 return 0;
3803}
3804
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303805#ifdef CONFIG_PM_RUNTIME
3806static int msm_otg_runtime_idle(struct device *dev)
3807{
3808 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003809 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303810
3811 dev_dbg(dev, "OTG runtime idle\n");
3812
Steve Mucklef132c6c2012-06-06 18:30:57 -07003813 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303814 return -EAGAIN;
3815 else
3816 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303817}
3818
3819static int msm_otg_runtime_suspend(struct device *dev)
3820{
3821 struct msm_otg *motg = dev_get_drvdata(dev);
3822
3823 dev_dbg(dev, "OTG runtime suspend\n");
3824 return msm_otg_suspend(motg);
3825}
3826
3827static int msm_otg_runtime_resume(struct device *dev)
3828{
3829 struct msm_otg *motg = dev_get_drvdata(dev);
3830
3831 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303832 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303833 return msm_otg_resume(motg);
3834}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303835#endif
3836
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303837#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303838static int msm_otg_pm_suspend(struct device *dev)
3839{
Jack Pham5ca279b2012-05-14 18:42:54 -07003840 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303841 struct msm_otg *motg = dev_get_drvdata(dev);
3842
3843 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003844
3845 atomic_set(&motg->pm_suspended, 1);
3846 ret = msm_otg_suspend(motg);
3847 if (ret)
3848 atomic_set(&motg->pm_suspended, 0);
3849
3850 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303851}
3852
3853static int msm_otg_pm_resume(struct device *dev)
3854{
Jack Pham5ca279b2012-05-14 18:42:54 -07003855 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303856 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303857
3858 dev_dbg(dev, "OTG PM resume\n");
3859
Jack Pham5ca279b2012-05-14 18:42:54 -07003860 atomic_set(&motg->pm_suspended, 0);
3861 if (motg->sm_work_pending) {
3862 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303863
Jack Pham5ca279b2012-05-14 18:42:54 -07003864 pm_runtime_get_noresume(dev);
3865 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303866
Jack Pham5ca279b2012-05-14 18:42:54 -07003867 /* Update runtime PM status */
3868 pm_runtime_disable(dev);
3869 pm_runtime_set_active(dev);
3870 pm_runtime_enable(dev);
3871
3872 queue_work(system_nrt_wq, &motg->sm_work);
3873 }
3874
3875 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303876}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303877#endif
3878
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303879#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303880static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303881 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3882 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3883 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303884};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303885#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303886
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303887static struct of_device_id msm_otg_dt_match[] = {
3888 { .compatible = "qcom,hsusb-otg",
3889 },
3890 {}
3891};
3892
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303893static struct platform_driver msm_otg_driver = {
3894 .remove = __devexit_p(msm_otg_remove),
3895 .driver = {
3896 .name = DRIVER_NAME,
3897 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303898#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303899 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303900#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303901 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303902 },
3903};
3904
3905static int __init msm_otg_init(void)
3906{
3907 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3908}
3909
3910static void __exit msm_otg_exit(void)
3911{
3912 platform_driver_unregister(&msm_otg_driver);
3913}
3914
3915module_init(msm_otg_init);
3916module_exit(msm_otg_exit);
3917
3918MODULE_LICENSE("GPL v2");
3919MODULE_DESCRIPTION("MSM USB transceiver driver");