blob: 0e1267b3e9ffa6112d37772a186794531612281a [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();
Amit Blay9b6e58b2012-06-18 13:12:49 +0300850 if (!motg->pdata->core_clk_always_on_workaround) {
851 clk_disable_unprepare(motg->pclk);
852 clk_disable_unprepare(motg->core_clk);
853 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530854
Anji jonnala7da3f262011-12-02 17:22:14 -0800855 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530856 if (!host_bus_suspend) {
857 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
858 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700859 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530860 "TCXO D0 buffer%d\n", __func__, ret);
861 else
862 motg->lpm_flags |= XO_SHUTDOWN;
863 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800864
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530865 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
866 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700867 msm_hsusb_ldo_enable(motg, 0);
868 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530869 }
870
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530871 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700872 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530873 msm_hsusb_mhl_switch_enable(motg, 0);
874 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700875
Steve Mucklef132c6c2012-06-06 18:30:57 -0700876 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530877 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 if (motg->pdata->pmic_id_irq)
879 enable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700880 if (pdata->otg_control == OTG_PHY_CONTROL &&
881 pdata->mpm_otgsessvld_int)
882 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700883 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530884 if (bus)
885 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
886
887 atomic_set(&motg->in_lpm, 1);
888 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700889 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530890
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200891 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530892
893 return 0;
894}
895
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530896static int msm_otg_resume(struct msm_otg *motg)
897{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200898 struct usb_phy *phy = &motg->phy;
899 struct usb_bus *bus = phy->otg->host;
Jack Pham87f202f2012-08-06 00:24:22 -0700900 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530901 int cnt = 0;
902 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200903 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800904 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530905
906 if (!atomic_read(&motg->in_lpm))
907 return 0;
908
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530910
Anji jonnala7da3f262011-12-02 17:22:14 -0800911 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530912 if (motg->lpm_flags & XO_SHUTDOWN) {
913 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
914 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700915 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530916 "TCXO D0 buffer%d\n", __func__, ret);
917 motg->lpm_flags &= ~XO_SHUTDOWN;
918 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530919
Amit Blay9b6e58b2012-06-18 13:12:49 +0300920 if (!motg->pdata->core_clk_always_on_workaround) {
921 clk_prepare_enable(motg->core_clk);
922 clk_prepare_enable(motg->pclk);
923 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530924
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700925 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
926 msm_hsusb_ldo_enable(motg, 1);
927 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
928 }
929
930 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530931 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530932 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200933 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
934 phy_ctrl_val |= PHY_RETEN;
935 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
936 /* Disable PHY HV interrupts */
937 phy_ctrl_val &=
938 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
939 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700940 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530941 }
942
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530943 temp = readl(USB_USBCMD);
944 temp &= ~ASYNC_INTR_CTRL;
945 temp &= ~ULPI_STP_CTRL;
946 writel(temp, USB_USBCMD);
947
948 /*
949 * PHY comes out of low power mode (LPM) in case of wakeup
950 * from asynchronous interrupt.
951 */
952 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
953 goto skip_phy_resume;
954
955 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
956 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
957 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
958 break;
959 udelay(1);
960 cnt++;
961 }
962
963 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
964 /*
965 * This is a fatal error. Reset the link and
966 * PHY. USB state can not be restored. Re-insertion
967 * of USB cable is the only way to get USB working.
968 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200969 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530970 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200971 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530972 }
973
974skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700975 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530976 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 if (motg->pdata->pmic_id_irq)
978 disable_irq_wake(motg->pdata->pmic_id_irq);
Jack Pham87f202f2012-08-06 00:24:22 -0700979 if (pdata->otg_control == OTG_PHY_CONTROL &&
980 pdata->mpm_otgsessvld_int)
981 msm_mpm_set_pin_wake(pdata->mpm_otgsessvld_int, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530983 if (bus)
984 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
985
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530986 atomic_set(&motg->in_lpm, 0);
987
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530988 if (motg->async_int) {
989 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530990 enable_irq(motg->irq);
991 }
992
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200993 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530994
995 return 0;
996}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530997#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530998
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -0700999static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
1000{
1001 if (!psy)
1002 goto psy_not_supported;
1003
1004 if (host_mode)
1005 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
1006 else
1007 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
1008
1009psy_not_supported:
1010 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
1011 return -ENXIO;
1012}
1013
David Keitel081a3e22012-04-18 12:37:07 -07001014static int msm_otg_notify_chg_type(struct msm_otg *motg)
1015{
1016 static int charger_type;
1017 /*
1018 * TODO
1019 * Unify OTG driver charger types and power supply charger types
1020 */
1021 if (charger_type == motg->chg_type)
1022 return 0;
1023
1024 if (motg->chg_type == USB_SDP_CHARGER)
1025 charger_type = POWER_SUPPLY_TYPE_USB;
1026 else if (motg->chg_type == USB_CDP_CHARGER)
1027 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301028 else if (motg->chg_type == USB_DCP_CHARGER ||
1029 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001030 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1031 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1032 motg->chg_type == USB_ACA_A_CHARGER ||
1033 motg->chg_type == USB_ACA_B_CHARGER ||
1034 motg->chg_type == USB_ACA_C_CHARGER))
1035 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1036 else
1037 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1038
1039 return pm8921_set_usb_power_supply_type(charger_type);
1040}
1041
Amit Blay0f7edf72012-01-15 10:11:27 +02001042static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1043{
Amit Blay0f7edf72012-01-15 10:11:27 +02001044
Amit Blay0f7edf72012-01-15 10:11:27 +02001045 if (!psy)
1046 goto psy_not_supported;
1047
1048 if (motg->cur_power == 0 && mA > 0) {
1049 /* Enable charging */
1050 if (power_supply_set_online(psy, true))
1051 goto psy_not_supported;
1052 } else if (motg->cur_power > 0 && mA == 0) {
1053 /* Disable charging */
1054 if (power_supply_set_online(psy, false))
1055 goto psy_not_supported;
1056 return 0;
1057 }
1058 /* Set max current limit */
1059 if (power_supply_set_current_limit(psy, 1000*mA))
1060 goto psy_not_supported;
1061
1062 return 0;
1063
1064psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001065 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001066 return -ENXIO;
1067}
1068
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301069static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1070{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001071 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301072
1073 if (g && g->is_a_peripheral)
1074 return;
1075
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301076 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1077 motg->chg_type == USB_ACA_A_CHARGER ||
1078 motg->chg_type == USB_ACA_B_CHARGER ||
1079 motg->chg_type == USB_ACA_C_CHARGER) &&
1080 mA > IDEV_ACA_CHG_LIMIT)
1081 mA = IDEV_ACA_CHG_LIMIT;
1082
David Keitel081a3e22012-04-18 12:37:07 -07001083 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001084 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001085 "Failed notifying %d charger type to PMIC\n",
1086 motg->chg_type);
1087
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301088 if (motg->cur_power == mA)
1089 return;
1090
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001091 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001092
1093 /*
1094 * Use Power Supply API if supported, otherwise fallback
1095 * to legacy pm8921 API.
1096 */
1097 if (msm_otg_notify_power_supply(motg, mA))
1098 pm8921_charger_vbus_draw(mA);
1099
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301100 motg->cur_power = mA;
1101}
1102
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001103static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301104{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001105 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301106
1107 /*
1108 * Gadget driver uses set_power method to notify about the
1109 * available current based on suspend/configured states.
1110 *
1111 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1112 * states when CDP/ACA is connected.
1113 */
1114 if (motg->chg_type == USB_SDP_CHARGER)
1115 msm_otg_notify_charger(motg, mA);
1116
1117 return 0;
1118}
1119
Steve Mucklef132c6c2012-06-06 18:30:57 -07001120static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001122 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123 struct msm_otg_platform_data *pdata = motg->pdata;
1124 struct usb_hcd *hcd;
1125
1126 if (!otg->host)
1127 return;
1128
1129 hcd = bus_to_hcd(otg->host);
1130
1131 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001132 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301133
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301134 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001135 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301136 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1137
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301138 /*
1139 * Some boards have a switch cotrolled by gpio
1140 * to enable/disable internal HUB. Enable internal
1141 * HUB before kicking the host.
1142 */
1143 if (pdata->setup_gpio)
1144 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301145 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301146 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001147 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301148
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301149 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301150 /* HCD core reset all bits of PORTSC. select ULPI phy */
1151 writel_relaxed(0x80000000, USB_PORTSC);
1152
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301153 if (pdata->setup_gpio)
1154 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301155
1156 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001157 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301158 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301159 }
1160}
1161
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001162static int msm_otg_usbdev_notify(struct notifier_block *self,
1163 unsigned long action, void *priv)
1164{
1165 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001166 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301167 struct usb_device *udev = priv;
1168
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301169 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1170 goto out;
1171
Steve Mucklef132c6c2012-06-06 18:30:57 -07001172 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301173 goto out;
1174 /*
1175 * Interested in devices connected directly to the root hub.
1176 * ACA dock can supply IDEV_CHG irrespective devices connected
1177 * on the accessory port.
1178 */
1179 if (!udev->parent || udev->parent->parent ||
1180 motg->chg_type == USB_ACA_DOCK_CHARGER)
1181 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182
1183 switch (action) {
1184 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301185 if (aca_enabled())
1186 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001187 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301188 pr_debug("B_CONN set\n");
1189 set_bit(B_CONN, &motg->inputs);
1190 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001191 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301192 /*
1193 * OTG PET: A-device must end session within
1194 * 10 sec after PET enumeration.
1195 */
1196 if (udev->quirks & USB_QUIRK_OTG_PET)
1197 msm_otg_start_timer(motg, TA_TST_MAINT,
1198 A_TST_MAINT);
1199 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301200 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 if (udev->actconfig)
1203 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1204 else
1205 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001206 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301207 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301208 break;
1209 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001210 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1211 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301212 pr_debug("B_CONN clear\n");
1213 clear_bit(B_CONN, &motg->inputs);
1214 /*
1215 * OTG PET: A-device must end session after
1216 * PET disconnection if it is enumerated
1217 * with bcdDevice[0] = 1. USB core sets
1218 * bus->otg_vbus_off for us. clear it here.
1219 */
1220 if (udev->bus->otg_vbus_off) {
1221 udev->bus->otg_vbus_off = 0;
1222 set_bit(A_BUS_DROP, &motg->inputs);
1223 }
1224 queue_work(system_nrt_wq, &motg->sm_work);
1225 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001226 default:
1227 break;
1228 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301229 if (test_bit(ID_A, &motg->inputs))
1230 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1231 motg->mA_port);
1232out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 return NOTIFY_OK;
1234}
1235
Mayank Ranae3926882011-12-26 09:47:54 +05301236static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1237{
1238 int ret;
1239 static bool vbus_is_on;
1240
1241 if (vbus_is_on == on)
1242 return;
1243
1244 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301245 ret = motg->pdata->vbus_power(on);
1246 if (!ret)
1247 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301248 return;
1249 }
1250
1251 if (!vbus_otg) {
1252 pr_err("vbus_otg is NULL.");
1253 return;
1254 }
1255
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001256 /*
1257 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001258 * from usb before turning on the boost.
1259 * if exiting host mode disable the boost before enabling to draw
1260 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001261 */
Mayank Ranae3926882011-12-26 09:47:54 +05301262 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001263 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301264 ret = regulator_enable(vbus_otg);
1265 if (ret) {
1266 pr_err("unable to enable vbus_otg\n");
1267 return;
1268 }
1269 vbus_is_on = true;
1270 } else {
1271 ret = regulator_disable(vbus_otg);
1272 if (ret) {
1273 pr_err("unable to disable vbus_otg\n");
1274 return;
1275 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001276 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301277 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301278 }
1279}
1280
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001281static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301282{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001283 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301284 struct usb_hcd *hcd;
1285
1286 /*
1287 * Fail host registration if this board can support
1288 * only peripheral configuration.
1289 */
1290 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001291 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301292 return -ENODEV;
1293 }
1294
Mayank Ranae3926882011-12-26 09:47:54 +05301295 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001296 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301297 if (IS_ERR(vbus_otg)) {
1298 pr_err("Unable to get vbus_otg\n");
1299 return -ENODEV;
1300 }
1301 }
1302
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001304 if (otg->phy->state == OTG_STATE_A_HOST) {
1305 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001306 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301307 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301308 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301309 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001310 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301311 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301312 } else {
1313 otg->host = NULL;
1314 }
1315
1316 return 0;
1317 }
1318
1319 hcd = bus_to_hcd(host);
1320 hcd->power_budget = motg->pdata->power_budget;
1321
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301322#ifdef CONFIG_USB_OTG
1323 host->otg_port = 1;
1324#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1326 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301327 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001328 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329
1330 /*
1331 * Kick the state machine work, if peripheral is not supported
1332 * or peripheral is already registered with us.
1333 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301334 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001335 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301336 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301337 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301338
1339 return 0;
1340}
1341
Steve Mucklef132c6c2012-06-06 18:30:57 -07001342static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301343{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301344 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001345 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301346 struct msm_otg_platform_data *pdata = motg->pdata;
1347
1348 if (!otg->gadget)
1349 return;
1350
1351 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001352 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301353 /*
1354 * Some boards have a switch cotrolled by gpio
1355 * to enable/disable internal HUB. Disable internal
1356 * HUB before kicking the gadget.
1357 */
1358 if (pdata->setup_gpio)
1359 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001360
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301361 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301362 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301363 ret = msm_bus_scale_client_update_request(
1364 motg->bus_perf_client, 1);
1365 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001366 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301367 "bus bandwidth %d\n", __func__, ret);
1368 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301369 usb_gadget_vbus_connect(otg->gadget);
1370 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001371 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301372 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301373 /* Configure BUS performance parameters to default */
1374 if (motg->bus_perf_client) {
1375 ret = msm_bus_scale_client_update_request(
1376 motg->bus_perf_client, 0);
1377 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001378 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301379 "for bus bw %d\n", __func__, ret);
1380 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301381 if (pdata->setup_gpio)
1382 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1383 }
1384
1385}
1386
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001387static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301388 struct usb_gadget *gadget)
1389{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001390 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391
1392 /*
1393 * Fail peripheral registration if this board can support
1394 * only host configuration.
1395 */
1396 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001397 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301398 return -ENODEV;
1399 }
1400
1401 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001402 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1403 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301404 msm_otg_start_peripheral(otg, 0);
1405 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001406 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301407 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301408 } else {
1409 otg->gadget = NULL;
1410 }
1411
1412 return 0;
1413 }
1414 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001415 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301416
1417 /*
1418 * Kick the state machine work, if host is not supported
1419 * or host is already registered with us.
1420 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301421 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001422 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301423 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301424 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301425
1426 return 0;
1427}
1428
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301429static int msm_otg_mhl_register_callback(struct msm_otg *motg,
1430 void (*callback)(int on))
1431{
1432 struct usb_phy *phy = &motg->phy;
1433 int ret;
1434
1435 if (motg->pdata->otg_control != OTG_PMIC_CONTROL ||
1436 !motg->pdata->pmic_id_irq) {
1437 dev_dbg(phy->dev, "MHL can not be supported without PMIC Id\n");
1438 return -ENODEV;
1439 }
1440
1441 if (!motg->pdata->mhl_dev_name) {
1442 dev_dbg(phy->dev, "MHL device name does not exist.\n");
1443 return -ENODEV;
1444 }
1445
1446 if (callback)
1447 ret = mhl_register_callback(motg->pdata->mhl_dev_name,
1448 callback);
1449 else
1450 ret = mhl_unregister_callback(motg->pdata->mhl_dev_name);
1451
1452 if (ret)
1453 dev_dbg(phy->dev, "mhl_register_callback(%s) return error=%d\n",
1454 motg->pdata->mhl_dev_name, ret);
1455 else
1456 motg->mhl_enabled = true;
1457
1458 return ret;
1459}
1460
1461static void msm_otg_mhl_notify_online(int on)
1462{
1463 struct msm_otg *motg = the_msm_otg;
1464 struct usb_phy *phy = &motg->phy;
1465 bool queue = false;
1466
1467 dev_dbg(phy->dev, "notify MHL %s%s\n", on ? "" : "dis", "connected");
1468
1469 if (on) {
1470 set_bit(MHL, &motg->inputs);
1471 } else {
1472 clear_bit(MHL, &motg->inputs);
1473 queue = true;
1474 }
1475
1476 if (queue && phy->state != OTG_STATE_UNDEFINED)
1477 schedule_work(&motg->sm_work);
1478}
1479
1480static bool msm_otg_is_mhl(struct msm_otg *motg)
1481{
1482 struct usb_phy *phy = &motg->phy;
1483 int is_mhl, ret;
1484
1485 ret = mhl_device_discovery(motg->pdata->mhl_dev_name, &is_mhl);
1486 if (ret || is_mhl != MHL_DISCOVERY_RESULT_MHL) {
1487 /*
1488 * MHL driver calls our callback saying that MHL connected
1489 * if RID_GND is detected. But at later part of discovery
1490 * it may figure out MHL is not connected and returns
1491 * false. Hence clear MHL input here.
1492 */
1493 clear_bit(MHL, &motg->inputs);
1494 dev_dbg(phy->dev, "MHL device not found\n");
1495 return false;
1496 }
1497
1498 set_bit(MHL, &motg->inputs);
1499 dev_dbg(phy->dev, "MHL device found\n");
1500 return true;
1501}
1502
1503static bool msm_chg_mhl_detect(struct msm_otg *motg)
1504{
1505 bool ret, id;
1506 unsigned long flags;
1507
1508 if (!motg->mhl_enabled)
1509 return false;
1510
1511 local_irq_save(flags);
1512 id = irq_read_line(motg->pdata->pmic_id_irq);
1513 local_irq_restore(flags);
1514
1515 if (id)
1516 return false;
1517
1518 mhl_det_in_progress = true;
1519 ret = msm_otg_is_mhl(motg);
1520 mhl_det_in_progress = false;
1521
1522 return ret;
1523}
1524
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525static bool msm_chg_aca_detect(struct msm_otg *motg)
1526{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001527 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 u32 int_sts;
1529 bool ret = false;
1530
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301531 if (!aca_enabled())
1532 goto out;
1533
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1535 goto out;
1536
Steve Mucklef132c6c2012-06-06 18:30:57 -07001537 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001538 switch (int_sts & 0x1C) {
1539 case 0x08:
1540 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001541 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542 motg->chg_type = USB_ACA_A_CHARGER;
1543 motg->chg_state = USB_CHG_STATE_DETECTED;
1544 clear_bit(ID_B, &motg->inputs);
1545 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301546 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001547 ret = true;
1548 }
1549 break;
1550 case 0x0C:
1551 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001552 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001553 motg->chg_type = USB_ACA_B_CHARGER;
1554 motg->chg_state = USB_CHG_STATE_DETECTED;
1555 clear_bit(ID_A, &motg->inputs);
1556 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301557 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001558 ret = true;
1559 }
1560 break;
1561 case 0x10:
1562 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001563 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001564 motg->chg_type = USB_ACA_C_CHARGER;
1565 motg->chg_state = USB_CHG_STATE_DETECTED;
1566 clear_bit(ID_A, &motg->inputs);
1567 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301568 set_bit(ID, &motg->inputs);
1569 ret = true;
1570 }
1571 break;
1572 case 0x04:
1573 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001574 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301575 motg->chg_type = USB_INVALID_CHARGER;
1576 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1577 clear_bit(ID_A, &motg->inputs);
1578 clear_bit(ID_B, &motg->inputs);
1579 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580 ret = true;
1581 }
1582 break;
1583 default:
1584 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1585 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301586 test_and_clear_bit(ID_C, &motg->inputs) |
1587 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001588 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001589 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001590 motg->chg_type = USB_INVALID_CHARGER;
1591 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1592 }
1593 }
1594out:
1595 return ret;
1596}
1597
1598static void msm_chg_enable_aca_det(struct msm_otg *motg)
1599{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001600 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001601
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301602 if (!aca_enabled())
1603 return;
1604
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001605 switch (motg->pdata->phy_type) {
1606 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301607 /* Disable ID_GND in link and PHY */
1608 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1609 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001610 ulpi_write(phy, 0x01, 0x0C);
1611 ulpi_write(phy, 0x10, 0x0F);
1612 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301613 /* Disable PMIC ID pull-up */
1614 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301615 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001616 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301617 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001618 break;
1619 default:
1620 break;
1621 }
1622}
1623
1624static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1625{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001626 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001627
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301628 if (!aca_enabled())
1629 return;
1630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 switch (motg->pdata->phy_type) {
1632 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301633 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001634 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301635 break;
1636 default:
1637 break;
1638 }
1639}
1640
1641static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1642{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001643 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301644
1645 if (!aca_enabled())
1646 return;
1647
1648 switch (motg->pdata->phy_type) {
1649 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001650 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001651 break;
1652 default:
1653 break;
1654 }
1655}
1656
1657static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1658{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001659 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001660 bool ret = false;
1661
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301662 if (!aca_enabled())
1663 return ret;
1664
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001665 switch (motg->pdata->phy_type) {
1666 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001667 if (ulpi_read(phy, 0x91) & 1) {
1668 dev_dbg(phy->dev, "RID change\n");
1669 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 ret = msm_chg_aca_detect(motg);
1671 }
1672 default:
1673 break;
1674 }
1675 return ret;
1676}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301677
1678static void msm_otg_id_timer_func(unsigned long data)
1679{
1680 struct msm_otg *motg = (struct msm_otg *) data;
1681
1682 if (!aca_enabled())
1683 return;
1684
1685 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001686 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301687 return;
1688 }
1689
Steve Mucklef132c6c2012-06-06 18:30:57 -07001690 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301691 goto out;
1692
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301693 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001694 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301695 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301696 }
1697
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301698out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301699 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1700 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1701}
1702
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301703static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1704{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001705 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301706 u32 chg_det;
1707 bool ret = false;
1708
1709 switch (motg->pdata->phy_type) {
1710 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001711 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301712 ret = chg_det & (1 << 4);
1713 break;
1714 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001715 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301716 ret = chg_det & 1;
1717 break;
1718 default:
1719 break;
1720 }
1721 return ret;
1722}
1723
1724static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1725{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001726 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301727 u32 chg_det;
1728
1729 switch (motg->pdata->phy_type) {
1730 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001731 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301732 /* Turn off charger block */
1733 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001734 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301735 udelay(20);
1736 /* control chg block via ULPI */
1737 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001738 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301739 /* put it in host mode for enabling D- source */
1740 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001741 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301742 /* Turn on chg detect block */
1743 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001744 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301745 udelay(20);
1746 /* enable chg detection */
1747 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001748 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301749 break;
1750 case SNPS_28NM_INTEGRATED_PHY:
1751 /*
1752 * Configure DM as current source, DP as current sink
1753 * and enable battery charging comparators.
1754 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001755 ulpi_write(phy, 0x8, 0x85);
1756 ulpi_write(phy, 0x2, 0x85);
1757 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301758 break;
1759 default:
1760 break;
1761 }
1762}
1763
1764static bool msm_chg_check_primary_det(struct msm_otg *motg)
1765{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001766 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301767 u32 chg_det;
1768 bool ret = false;
1769
1770 switch (motg->pdata->phy_type) {
1771 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001772 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301773 ret = chg_det & (1 << 4);
1774 break;
1775 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001776 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301777 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301778 /* Turn off VDP_SRC */
1779 ulpi_write(phy, 0x3, 0x86);
1780 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301781 break;
1782 default:
1783 break;
1784 }
1785 return ret;
1786}
1787
1788static void msm_chg_enable_primary_det(struct msm_otg *motg)
1789{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001790 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 u32 chg_det;
1792
1793 switch (motg->pdata->phy_type) {
1794 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001795 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301796 /* enable chg detection */
1797 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001798 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 break;
1800 case SNPS_28NM_INTEGRATED_PHY:
1801 /*
1802 * Configure DP as current source, DM as current sink
1803 * and enable battery charging comparators.
1804 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001805 ulpi_write(phy, 0x2, 0x85);
1806 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 break;
1808 default:
1809 break;
1810 }
1811}
1812
1813static bool msm_chg_check_dcd(struct msm_otg *motg)
1814{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001815 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301816 u32 line_state;
1817 bool ret = false;
1818
1819 switch (motg->pdata->phy_type) {
1820 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001821 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301822 ret = !(line_state & 1);
1823 break;
1824 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001825 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301826 ret = line_state & 2;
1827 break;
1828 default:
1829 break;
1830 }
1831 return ret;
1832}
1833
1834static void msm_chg_disable_dcd(struct msm_otg *motg)
1835{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001836 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301837 u32 chg_det;
1838
1839 switch (motg->pdata->phy_type) {
1840 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001841 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301842 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 break;
1845 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 break;
1848 default:
1849 break;
1850 }
1851}
1852
1853static void msm_chg_enable_dcd(struct msm_otg *motg)
1854{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001855 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301856 u32 chg_det;
1857
1858 switch (motg->pdata->phy_type) {
1859 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001860 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301861 /* Turn on D+ current source */
1862 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001863 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301864 break;
1865 case SNPS_28NM_INTEGRATED_PHY:
1866 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001867 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301868 break;
1869 default:
1870 break;
1871 }
1872}
1873
1874static void msm_chg_block_on(struct msm_otg *motg)
1875{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001876 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 u32 func_ctrl, chg_det;
1878
1879 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001880 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301881 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1882 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001883 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301884
1885 switch (motg->pdata->phy_type) {
1886 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001887 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301888 /* control chg block via ULPI */
1889 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001890 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301891 /* Turn on chg detect block */
1892 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001893 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301894 udelay(20);
1895 break;
1896 case SNPS_28NM_INTEGRATED_PHY:
1897 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001898 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301899 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001900 ulpi_write(phy, 0x1F, 0x92);
1901 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301902 udelay(100);
1903 break;
1904 default:
1905 break;
1906 }
1907}
1908
1909static void msm_chg_block_off(struct msm_otg *motg)
1910{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001911 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301912 u32 func_ctrl, chg_det;
1913
1914 switch (motg->pdata->phy_type) {
1915 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001916 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 /* Turn off charger block */
1918 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001919 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301920 break;
1921 case SNPS_28NM_INTEGRATED_PHY:
1922 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001923 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301924 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001925 ulpi_write(phy, 0x1F, 0x92);
1926 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301927 break;
1928 default:
1929 break;
1930 }
1931
1932 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001933 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1935 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001936 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301937}
1938
Anji jonnalad270e2d2011-08-09 11:28:32 +05301939static const char *chg_to_string(enum usb_chg_type chg_type)
1940{
1941 switch (chg_type) {
1942 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1943 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1944 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1945 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1946 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1947 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1948 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301949 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301950 default: return "INVALID_CHARGER";
1951 }
1952}
1953
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301954#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1955#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301956#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1957#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301958static void msm_chg_detect_work(struct work_struct *w)
1959{
1960 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001961 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301962 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301963 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301964 unsigned long delay;
1965
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001966 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301967
1968 if (test_bit(MHL, &motg->inputs)) {
1969 dev_dbg(phy->dev, "detected MHL, escape chg detection work\n");
1970 return;
1971 }
1972
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301973 switch (motg->chg_state) {
1974 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301975 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301976 if (motg->pdata->enable_dcd)
1977 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001978 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301979 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1980 motg->dcd_retries = 0;
1981 delay = MSM_CHG_DCD_POLL_TIME;
1982 break;
1983 case USB_CHG_STATE_WAIT_FOR_DCD:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05301984 if (msm_chg_mhl_detect(motg)) {
1985 msm_chg_block_off(motg);
1986 motg->chg_state = USB_CHG_STATE_DETECTED;
1987 motg->chg_type = USB_INVALID_CHARGER;
1988 queue_work(system_nrt_wq, &motg->sm_work);
1989 return;
1990 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001991 is_aca = msm_chg_aca_detect(motg);
1992 if (is_aca) {
1993 /*
1994 * ID_A can be ACA dock too. continue
1995 * primary detection after DCD.
1996 */
1997 if (test_bit(ID_A, &motg->inputs)) {
1998 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1999 } else {
2000 delay = 0;
2001 break;
2002 }
2003 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302004 if (motg->pdata->enable_dcd)
2005 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302006 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
2007 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05302008 if (motg->pdata->enable_dcd)
2009 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302010 msm_chg_enable_primary_det(motg);
2011 delay = MSM_CHG_PRIMARY_DET_TIME;
2012 motg->chg_state = USB_CHG_STATE_DCD_DONE;
2013 } else {
2014 delay = MSM_CHG_DCD_POLL_TIME;
2015 }
2016 break;
2017 case USB_CHG_STATE_DCD_DONE:
2018 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302019 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
2020 dm_vlgc = line_state & PORTSC_LS_DM;
2021 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302022 if (test_bit(ID_A, &motg->inputs)) {
2023 motg->chg_type = USB_ACA_DOCK_CHARGER;
2024 motg->chg_state = USB_CHG_STATE_DETECTED;
2025 delay = 0;
2026 break;
2027 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302028 if (line_state) { /* DP > VLGC */
2029 motg->chg_type = USB_PROPRIETARY_CHARGER;
2030 motg->chg_state = USB_CHG_STATE_DETECTED;
2031 delay = 0;
2032 } else {
2033 msm_chg_enable_secondary_det(motg);
2034 delay = MSM_CHG_SECONDARY_DET_TIME;
2035 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
2036 }
2037 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302038 if (test_bit(ID_A, &motg->inputs)) {
2039 motg->chg_type = USB_ACA_A_CHARGER;
2040 motg->chg_state = USB_CHG_STATE_DETECTED;
2041 delay = 0;
2042 break;
2043 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302044
2045 if (line_state) /* DP > VLGC or/and DM > VLGC */
2046 motg->chg_type = USB_PROPRIETARY_CHARGER;
2047 else
2048 motg->chg_type = USB_SDP_CHARGER;
2049
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302050 motg->chg_state = USB_CHG_STATE_DETECTED;
2051 delay = 0;
2052 }
2053 break;
2054 case USB_CHG_STATE_PRIMARY_DONE:
2055 vout = msm_chg_check_secondary_det(motg);
2056 if (vout)
2057 motg->chg_type = USB_DCP_CHARGER;
2058 else
2059 motg->chg_type = USB_CDP_CHARGER;
2060 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
2061 /* fall through */
2062 case USB_CHG_STATE_SECONDARY_DONE:
2063 motg->chg_state = USB_CHG_STATE_DETECTED;
2064 case USB_CHG_STATE_DETECTED:
2065 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002066 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302067 /*
2068 * Spurious interrupt is seen after enabling ACA detection
2069 * due to which charger detection fails in case of PET.
2070 * Add delay of 100 microsec to avoid that.
2071 */
2072 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002074 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05302075 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302076 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302077 return;
2078 default:
2079 return;
2080 }
2081
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302082 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083}
2084
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302085/*
2086 * We support OTG, Peripheral only and Host only configurations. In case
2087 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
2088 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
2089 * enabled when switch is controlled by user and default mode is supplied
2090 * by board file, which can be changed by userspace later.
2091 */
2092static void msm_otg_init_sm(struct msm_otg *motg)
2093{
2094 struct msm_otg_platform_data *pdata = motg->pdata;
2095 u32 otgsc = readl(USB_OTGSC);
2096
2097 switch (pdata->mode) {
2098 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002099 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302100 if (pdata->default_mode == USB_HOST) {
2101 clear_bit(ID, &motg->inputs);
2102 } else if (pdata->default_mode == USB_PERIPHERAL) {
2103 set_bit(ID, &motg->inputs);
2104 set_bit(B_SESS_VLD, &motg->inputs);
2105 } else {
2106 set_bit(ID, &motg->inputs);
2107 clear_bit(B_SESS_VLD, &motg->inputs);
2108 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302109 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302110 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302111 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302112 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302113 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302114 set_bit(A_BUS_REQ, &motg->inputs);
2115 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002116 if (otgsc & OTGSC_BSV)
2117 set_bit(B_SESS_VLD, &motg->inputs);
2118 else
2119 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302120 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302121 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07002122 unsigned long flags;
2123 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302124 if (irq_read_line(pdata->pmic_id_irq))
2125 set_bit(ID, &motg->inputs);
2126 else
2127 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07002128 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302129 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302130 /*
2131 * VBUS initial state is reported after PMIC
2132 * driver initialization. Wait for it.
2133 */
2134 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302135 }
2136 break;
2137 case USB_HOST:
2138 clear_bit(ID, &motg->inputs);
2139 break;
2140 case USB_PERIPHERAL:
2141 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302142 if (pdata->otg_control == OTG_PHY_CONTROL) {
2143 if (otgsc & OTGSC_BSV)
2144 set_bit(B_SESS_VLD, &motg->inputs);
2145 else
2146 clear_bit(B_SESS_VLD, &motg->inputs);
2147 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2148 /*
2149 * VBUS initial state is reported after PMIC
2150 * driver initialization. Wait for it.
2151 */
2152 wait_for_completion(&pmic_vbus_init);
2153 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302154 break;
2155 default:
2156 break;
2157 }
2158}
2159
2160static void msm_otg_sm_work(struct work_struct *w)
2161{
2162 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002163 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302164 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302165
Steve Mucklef132c6c2012-06-06 18:30:57 -07002166 pm_runtime_resume(otg->phy->dev);
2167 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002168 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302169 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002170 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302171 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302172 psy = power_supply_get_by_name("usb");
2173 if (!psy)
2174 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002175 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302176 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2177 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002178 pm_runtime_put_noidle(otg->phy->dev);
2179 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302180 break;
2181 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302182 /* FALL THROUGH */
2183 case OTG_STATE_B_IDLE:
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302184 if (test_bit(MHL, &motg->inputs)) {
2185 /* allow LPM */
2186 pm_runtime_put_noidle(otg->phy->dev);
2187 pm_runtime_suspend(otg->phy->dev);
2188 } else if ((!test_bit(ID, &motg->inputs) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002189 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302190 pr_debug("!id || id_A\n");
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302191 if (msm_chg_mhl_detect(motg)) {
2192 work = 1;
2193 break;
2194 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302195 clear_bit(B_BUS_REQ, &motg->inputs);
2196 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002197 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302198 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302199 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302200 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302201 switch (motg->chg_state) {
2202 case USB_CHG_STATE_UNDEFINED:
2203 msm_chg_detect_work(&motg->chg_work.work);
2204 break;
2205 case USB_CHG_STATE_DETECTED:
2206 switch (motg->chg_type) {
2207 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302208 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002209 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302210 /* fall through */
2211 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302212 msm_otg_notify_charger(motg,
2213 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002214 pm_runtime_put_noidle(otg->phy->dev);
2215 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302216 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302217 case USB_ACA_B_CHARGER:
2218 msm_otg_notify_charger(motg,
2219 IDEV_ACA_CHG_MAX);
2220 /*
2221 * (ID_B --> ID_C) PHY_ALT interrupt can
2222 * not be detected in LPM.
2223 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302224 break;
2225 case USB_CDP_CHARGER:
2226 msm_otg_notify_charger(motg,
2227 IDEV_CHG_MAX);
2228 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002229 otg->phy->state =
2230 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302231 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302232 case USB_ACA_C_CHARGER:
2233 msm_otg_notify_charger(motg,
2234 IDEV_ACA_CHG_MAX);
2235 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002236 otg->phy->state =
2237 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302238 break;
2239 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302240 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 default:
2245 break;
2246 }
2247 break;
2248 default:
2249 break;
2250 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302251 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2252 pr_debug("b_sess_end && b_bus_req\n");
2253 if (msm_otg_start_srp(otg) < 0) {
2254 clear_bit(B_BUS_REQ, &motg->inputs);
2255 work = 1;
2256 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302257 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002258 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302259 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2260 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302261 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302262 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302263 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302264 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2265 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302266 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002267 msm_otg_reset(otg->phy);
2268 pm_runtime_put_noidle(otg->phy->dev);
2269 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302270 }
2271 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302272 case OTG_STATE_B_SRP_INIT:
2273 if (!test_bit(ID, &motg->inputs) ||
2274 test_bit(ID_A, &motg->inputs) ||
2275 test_bit(ID_C, &motg->inputs) ||
2276 (test_bit(B_SESS_VLD, &motg->inputs) &&
2277 !test_bit(ID_B, &motg->inputs))) {
2278 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2279 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002280 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302281 /*
2282 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2283 * bits after SRP initiation.
2284 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002285 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302286 work = 1;
2287 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2288 pr_debug("b_srp_fail\n");
2289 pr_info("A-device did not respond to SRP\n");
2290 clear_bit(B_BUS_REQ, &motg->inputs);
2291 clear_bit(B_SRP_FAIL, &motg->tmouts);
2292 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002293 ulpi_write(otg->phy, 0x0, 0x98);
2294 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302295 motg->b_last_se0_sess = jiffies;
2296 work = 1;
2297 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302298 break;
2299 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302300 if (!test_bit(ID, &motg->inputs) ||
2301 test_bit(ID_A, &motg->inputs) ||
2302 test_bit(ID_B, &motg->inputs) ||
2303 !test_bit(B_SESS_VLD, &motg->inputs)) {
2304 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302305 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2306 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302307 msm_otg_notify_charger(motg, 0);
2308 srp_reqd = otg->gadget->otg_srp_reqd;
2309 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302310 if (test_bit(ID_B, &motg->inputs))
2311 clear_bit(ID_B, &motg->inputs);
2312 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002313 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302314 motg->b_last_se0_sess = jiffies;
2315 if (srp_reqd)
2316 msm_otg_start_timer(motg,
2317 TB_TST_SRP, B_TST_SRP);
2318 else
2319 work = 1;
2320 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2321 otg->gadget->b_hnp_enable &&
2322 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2323 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2324 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2325 /* D+ pullup should not be disconnected within 4msec
2326 * after A device suspends the bus. Otherwise PET will
2327 * fail the compliance test.
2328 */
2329 udelay(1000);
2330 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002331 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302332 /*
2333 * start HCD even before A-device enable
2334 * pull-up to meet HNP timings.
2335 */
2336 otg->host->is_b_host = 1;
2337 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002338 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2339 test_bit(B_SESS_VLD, &motg->inputs)) {
2340 pr_debug("a_bus_suspend && b_sess_vld\n");
2341 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002342 pm_runtime_put_noidle(otg->phy->dev);
2343 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002344 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002345 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302346 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002347 }
2348 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302349 case OTG_STATE_B_WAIT_ACON:
2350 if (!test_bit(ID, &motg->inputs) ||
2351 test_bit(ID_A, &motg->inputs) ||
2352 test_bit(ID_B, &motg->inputs) ||
2353 !test_bit(B_SESS_VLD, &motg->inputs)) {
2354 pr_debug("!id || id_a/b || !b_sess_vld\n");
2355 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302356 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302357 * A-device is physically disconnected during
2358 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302359 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302360 msm_otg_start_host(otg, 0);
2361 otg->host->is_b_host = 0;
2362
2363 clear_bit(B_BUS_REQ, &motg->inputs);
2364 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2365 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002366 otg->phy->state = OTG_STATE_B_IDLE;
2367 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302368 work = 1;
2369 } else if (test_bit(A_CONN, &motg->inputs)) {
2370 pr_debug("a_conn\n");
2371 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002372 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302373 /*
2374 * PET disconnects D+ pullup after reset is generated
2375 * by B device in B_HOST role which is not detected by
2376 * B device. As workaorund , start timer of 300msec
2377 * and stop timer if A device is enumerated else clear
2378 * A_CONN.
2379 */
2380 msm_otg_start_timer(motg, TB_TST_CONFIG,
2381 B_TST_CONFIG);
2382 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2383 pr_debug("b_ase0_brst_tmout\n");
2384 pr_info("B HNP fail:No response from A device\n");
2385 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002386 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302387 otg->host->is_b_host = 0;
2388 clear_bit(B_ASE0_BRST, &motg->tmouts);
2389 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2390 clear_bit(B_BUS_REQ, &motg->inputs);
2391 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002392 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302393 work = 1;
2394 } else if (test_bit(ID_C, &motg->inputs)) {
2395 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2396 }
2397 break;
2398 case OTG_STATE_B_HOST:
2399 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2400 !test_bit(A_CONN, &motg->inputs) ||
2401 !test_bit(B_SESS_VLD, &motg->inputs)) {
2402 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2403 clear_bit(A_CONN, &motg->inputs);
2404 clear_bit(B_BUS_REQ, &motg->inputs);
2405 msm_otg_start_host(otg, 0);
2406 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002407 otg->phy->state = OTG_STATE_B_IDLE;
2408 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302409 work = 1;
2410 } else if (test_bit(ID_C, &motg->inputs)) {
2411 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2412 }
2413 break;
2414 case OTG_STATE_A_IDLE:
2415 otg->default_a = 1;
2416 if (test_bit(ID, &motg->inputs) &&
2417 !test_bit(ID_A, &motg->inputs)) {
2418 pr_debug("id && !id_a\n");
2419 otg->default_a = 0;
2420 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002421 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302422 del_timer_sync(&motg->id_timer);
2423 msm_otg_link_reset(motg);
2424 msm_chg_enable_aca_intr(motg);
2425 msm_otg_notify_charger(motg, 0);
2426 work = 1;
2427 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2428 (test_bit(A_SRP_DET, &motg->inputs) ||
2429 test_bit(A_BUS_REQ, &motg->inputs))) {
2430 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2431
2432 clear_bit(A_SRP_DET, &motg->inputs);
2433 /* Disable SRP detection */
2434 writel_relaxed((readl_relaxed(USB_OTGSC) &
2435 ~OTGSC_INTSTS_MASK) &
2436 ~OTGSC_DPIE, USB_OTGSC);
2437
Steve Mucklef132c6c2012-06-06 18:30:57 -07002438 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302439 /* VBUS should not be supplied before end of SRP pulse
2440 * generated by PET, if not complaince test fail.
2441 */
2442 usleep_range(10000, 12000);
2443 /* ACA: ID_A: Stop charging untill enumeration */
2444 if (test_bit(ID_A, &motg->inputs))
2445 msm_otg_notify_charger(motg, 0);
2446 else
2447 msm_hsusb_vbus_power(motg, 1);
2448 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2449 } else {
2450 pr_debug("No session requested\n");
2451 clear_bit(A_BUS_DROP, &motg->inputs);
2452 if (test_bit(ID_A, &motg->inputs)) {
2453 msm_otg_notify_charger(motg,
2454 IDEV_ACA_CHG_MAX);
2455 } else if (!test_bit(ID, &motg->inputs)) {
2456 msm_otg_notify_charger(motg, 0);
2457 /*
2458 * A-device is not providing power on VBUS.
2459 * Enable SRP detection.
2460 */
2461 writel_relaxed(0x13, USB_USBMODE);
2462 writel_relaxed((readl_relaxed(USB_OTGSC) &
2463 ~OTGSC_INTSTS_MASK) |
2464 OTGSC_DPIE, USB_OTGSC);
2465 mb();
2466 }
2467 }
2468 break;
2469 case OTG_STATE_A_WAIT_VRISE:
2470 if ((test_bit(ID, &motg->inputs) &&
2471 !test_bit(ID_A, &motg->inputs)) ||
2472 test_bit(A_BUS_DROP, &motg->inputs) ||
2473 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2474 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2475 clear_bit(A_BUS_REQ, &motg->inputs);
2476 msm_otg_del_timer(motg);
2477 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002478 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302479 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2480 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2481 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002482 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302483 if (TA_WAIT_BCON > 0)
2484 msm_otg_start_timer(motg, TA_WAIT_BCON,
2485 A_WAIT_BCON);
2486 msm_otg_start_host(otg, 1);
2487 msm_chg_enable_aca_det(motg);
2488 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302489 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302490 if (msm_chg_check_aca_intr(motg))
2491 work = 1;
2492 }
2493 break;
2494 case OTG_STATE_A_WAIT_BCON:
2495 if ((test_bit(ID, &motg->inputs) &&
2496 !test_bit(ID_A, &motg->inputs)) ||
2497 test_bit(A_BUS_DROP, &motg->inputs) ||
2498 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2499 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2500 "a_wait_bcon_tmout\n");
2501 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2502 pr_info("Device No Response\n");
2503 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2504 }
2505 msm_otg_del_timer(motg);
2506 clear_bit(A_BUS_REQ, &motg->inputs);
2507 clear_bit(B_CONN, &motg->inputs);
2508 msm_otg_start_host(otg, 0);
2509 /*
2510 * ACA: ID_A with NO accessory, just the A plug is
2511 * attached to ACA: Use IDCHG_MAX for charging
2512 */
2513 if (test_bit(ID_A, &motg->inputs))
2514 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2515 else
2516 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002517 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302518 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2519 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2520 pr_debug("!a_vbus_vld\n");
2521 clear_bit(B_CONN, &motg->inputs);
2522 msm_otg_del_timer(motg);
2523 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002524 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2525 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302526 } else if (test_bit(ID_A, &motg->inputs)) {
2527 msm_hsusb_vbus_power(motg, 0);
2528 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2529 /*
2530 * If TA_WAIT_BCON is infinite, we don;t
2531 * turn off VBUS. Enter low power mode.
2532 */
2533 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002534 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302535 } else if (!test_bit(ID, &motg->inputs)) {
2536 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302537 }
2538 break;
2539 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302540 if ((test_bit(ID, &motg->inputs) &&
2541 !test_bit(ID_A, &motg->inputs)) ||
2542 test_bit(A_BUS_DROP, &motg->inputs)) {
2543 pr_debug("id_a/b/c || a_bus_drop\n");
2544 clear_bit(B_CONN, &motg->inputs);
2545 clear_bit(A_BUS_REQ, &motg->inputs);
2546 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002547 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302548 msm_otg_start_host(otg, 0);
2549 if (!test_bit(ID_A, &motg->inputs))
2550 msm_hsusb_vbus_power(motg, 0);
2551 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2552 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2553 pr_debug("!a_vbus_vld\n");
2554 clear_bit(B_CONN, &motg->inputs);
2555 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002556 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302557 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002558 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302559 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2560 /*
2561 * a_bus_req is de-asserted when root hub is
2562 * suspended or HNP is in progress.
2563 */
2564 pr_debug("!a_bus_req\n");
2565 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002566 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302567 if (otg->host->b_hnp_enable)
2568 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2569 A_AIDL_BDIS);
2570 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002571 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 } else if (!test_bit(B_CONN, &motg->inputs)) {
2573 pr_debug("!b_conn\n");
2574 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002575 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302576 if (TA_WAIT_BCON > 0)
2577 msm_otg_start_timer(motg, TA_WAIT_BCON,
2578 A_WAIT_BCON);
2579 if (msm_chg_check_aca_intr(motg))
2580 work = 1;
2581 } else if (test_bit(ID_A, &motg->inputs)) {
2582 msm_otg_del_timer(motg);
2583 msm_hsusb_vbus_power(motg, 0);
2584 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2585 msm_otg_notify_charger(motg,
2586 IDEV_ACA_CHG_MAX);
2587 else
2588 msm_otg_notify_charger(motg,
2589 IDEV_CHG_MIN - motg->mA_port);
2590 } else if (!test_bit(ID, &motg->inputs)) {
2591 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2592 motg->chg_type = USB_INVALID_CHARGER;
2593 msm_otg_notify_charger(motg, 0);
2594 msm_hsusb_vbus_power(motg, 1);
2595 }
2596 break;
2597 case OTG_STATE_A_SUSPEND:
2598 if ((test_bit(ID, &motg->inputs) &&
2599 !test_bit(ID_A, &motg->inputs)) ||
2600 test_bit(A_BUS_DROP, &motg->inputs) ||
2601 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2602 pr_debug("id_a/b/c || a_bus_drop ||"
2603 "a_aidl_bdis_tmout\n");
2604 msm_otg_del_timer(motg);
2605 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002606 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302607 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002608 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302609 if (!test_bit(ID_A, &motg->inputs))
2610 msm_hsusb_vbus_power(motg, 0);
2611 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2612 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2613 pr_debug("!a_vbus_vld\n");
2614 msm_otg_del_timer(motg);
2615 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002616 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302617 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002618 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302619 } else if (!test_bit(B_CONN, &motg->inputs) &&
2620 otg->host->b_hnp_enable) {
2621 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002622 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302623 msm_otg_host_hnp_enable(otg, 1);
2624 otg->gadget->is_a_peripheral = 1;
2625 msm_otg_start_peripheral(otg, 1);
2626 } else if (!test_bit(B_CONN, &motg->inputs) &&
2627 !otg->host->b_hnp_enable) {
2628 pr_debug("!b_conn && !b_hnp_enable");
2629 /*
2630 * bus request is dropped during suspend.
2631 * acquire again for next device.
2632 */
2633 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002634 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302635 if (TA_WAIT_BCON > 0)
2636 msm_otg_start_timer(motg, TA_WAIT_BCON,
2637 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002638 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302639 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002640 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302641 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002642 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002643 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302644 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302645 }
2646 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302647 case OTG_STATE_A_PERIPHERAL:
2648 if ((test_bit(ID, &motg->inputs) &&
2649 !test_bit(ID_A, &motg->inputs)) ||
2650 test_bit(A_BUS_DROP, &motg->inputs)) {
2651 pr_debug("id _f/b/c || a_bus_drop\n");
2652 /* Clear BIDL_ADIS timer */
2653 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002654 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302655 msm_otg_start_peripheral(otg, 0);
2656 otg->gadget->is_a_peripheral = 0;
2657 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002658 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302659 if (!test_bit(ID_A, &motg->inputs))
2660 msm_hsusb_vbus_power(motg, 0);
2661 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2662 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2663 pr_debug("!a_vbus_vld\n");
2664 /* Clear BIDL_ADIS timer */
2665 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002666 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302667 msm_otg_start_peripheral(otg, 0);
2668 otg->gadget->is_a_peripheral = 0;
2669 msm_otg_start_host(otg, 0);
2670 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2671 pr_debug("a_bidl_adis_tmout\n");
2672 msm_otg_start_peripheral(otg, 0);
2673 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002674 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302675 set_bit(A_BUS_REQ, &motg->inputs);
2676 msm_otg_host_hnp_enable(otg, 0);
2677 if (TA_WAIT_BCON > 0)
2678 msm_otg_start_timer(motg, TA_WAIT_BCON,
2679 A_WAIT_BCON);
2680 } else if (test_bit(ID_A, &motg->inputs)) {
2681 msm_hsusb_vbus_power(motg, 0);
2682 msm_otg_notify_charger(motg,
2683 IDEV_CHG_MIN - motg->mA_port);
2684 } else if (!test_bit(ID, &motg->inputs)) {
2685 msm_otg_notify_charger(motg, 0);
2686 msm_hsusb_vbus_power(motg, 1);
2687 }
2688 break;
2689 case OTG_STATE_A_WAIT_VFALL:
2690 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2691 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002692 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302693 work = 1;
2694 }
2695 break;
2696 case OTG_STATE_A_VBUS_ERR:
2697 if ((test_bit(ID, &motg->inputs) &&
2698 !test_bit(ID_A, &motg->inputs)) ||
2699 test_bit(A_BUS_DROP, &motg->inputs) ||
2700 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002701 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302702 if (!test_bit(ID_A, &motg->inputs))
2703 msm_hsusb_vbus_power(motg, 0);
2704 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2705 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2706 motg->chg_type = USB_INVALID_CHARGER;
2707 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302708 }
2709 break;
2710 default:
2711 break;
2712 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302713 if (work)
2714 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302715}
2716
2717static irqreturn_t msm_otg_irq(int irq, void *data)
2718{
2719 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002720 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302721 u32 otgsc = 0, usbsts, pc;
2722 bool work = 0;
2723 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302724
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302725 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302726 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302727 disable_irq_nosync(irq);
2728 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002729 if (atomic_read(&motg->pm_suspended))
2730 motg->sm_work_pending = true;
2731 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002732 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302733 return IRQ_HANDLED;
2734 }
2735
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002736 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302737 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302738
2739 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302740 return IRQ_NONE;
2741
2742 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302743 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302744 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302745 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302746 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302747 pr_debug("Id clear\n");
2748 /*
2749 * Assert a_bus_req to supply power on
2750 * VBUS when Micro/Mini-A cable is connected
2751 * with out user intervention.
2752 */
2753 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302754 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302755 msm_chg_enable_aca_det(motg);
2756 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302757 writel_relaxed(otgsc, USB_OTGSC);
2758 work = 1;
2759 } else if (otgsc & OTGSC_DPIS) {
2760 pr_debug("DPIS detected\n");
2761 writel_relaxed(otgsc, USB_OTGSC);
2762 set_bit(A_SRP_DET, &motg->inputs);
2763 set_bit(A_BUS_REQ, &motg->inputs);
2764 work = 1;
2765 } else if (otgsc & OTGSC_BSVIS) {
2766 writel_relaxed(otgsc, USB_OTGSC);
2767 /*
2768 * BSV interrupt comes when operating as an A-device
2769 * (VBUS on/off).
2770 * But, handle BSV when charger is removed from ACA in ID_A
2771 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002772 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302773 !test_bit(ID_A, &motg->inputs))
2774 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302775 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302776 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302777 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302778 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302779 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302780 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002781 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2782
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302783 msm_chg_check_aca_intr(motg);
2784 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302785 work = 1;
2786 } else if (usbsts & STS_PCI) {
2787 pc = readl_relaxed(USB_PORTSC);
2788 pr_debug("portsc = %x\n", pc);
2789 ret = IRQ_NONE;
2790 /*
2791 * HCD Acks PCI interrupt. We use this to switch
2792 * between different OTG states.
2793 */
2794 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002795 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302796 case OTG_STATE_A_SUSPEND:
2797 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2798 !(pc & PORTSC_CCS)) {
2799 pr_debug("B_CONN clear\n");
2800 clear_bit(B_CONN, &motg->inputs);
2801 msm_otg_del_timer(motg);
2802 }
2803 break;
2804 case OTG_STATE_A_PERIPHERAL:
2805 /*
2806 * A-peripheral observed activity on bus.
2807 * clear A_BIDL_ADIS timer.
2808 */
2809 msm_otg_del_timer(motg);
2810 work = 0;
2811 break;
2812 case OTG_STATE_B_WAIT_ACON:
2813 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2814 pr_debug("A_CONN set\n");
2815 set_bit(A_CONN, &motg->inputs);
2816 /* Clear ASE0_BRST timer */
2817 msm_otg_del_timer(motg);
2818 }
2819 break;
2820 case OTG_STATE_B_HOST:
2821 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2822 pr_debug("A_CONN clear\n");
2823 clear_bit(A_CONN, &motg->inputs);
2824 msm_otg_del_timer(motg);
2825 }
2826 break;
2827 case OTG_STATE_A_WAIT_BCON:
2828 if (TA_WAIT_BCON < 0)
2829 set_bit(A_BUS_REQ, &motg->inputs);
2830 default:
2831 work = 0;
2832 break;
2833 }
2834 } else if (usbsts & STS_URI) {
2835 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002836 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302837 case OTG_STATE_A_PERIPHERAL:
2838 /*
2839 * A-peripheral observed activity on bus.
2840 * clear A_BIDL_ADIS timer.
2841 */
2842 msm_otg_del_timer(motg);
2843 work = 0;
2844 break;
2845 default:
2846 work = 0;
2847 break;
2848 }
2849 } else if (usbsts & STS_SLI) {
2850 ret = IRQ_NONE;
2851 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002852 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302853 case OTG_STATE_B_PERIPHERAL:
2854 if (otg->gadget->b_hnp_enable) {
2855 set_bit(A_BUS_SUSPEND, &motg->inputs);
2856 set_bit(B_BUS_REQ, &motg->inputs);
2857 work = 1;
2858 }
2859 break;
2860 case OTG_STATE_A_PERIPHERAL:
2861 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2862 A_BIDL_ADIS);
2863 break;
2864 default:
2865 break;
2866 }
2867 } else if ((usbsts & PHY_ALT_INT)) {
2868 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2869 if (msm_chg_check_aca_intr(motg))
2870 work = 1;
2871 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302872 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302873 if (work)
2874 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302875
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302876 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002877}
2878
2879static void msm_otg_set_vbus_state(int online)
2880{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302881 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002882 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302883 struct usb_otg *otg = motg->phy.otg;
2884
2885 /* In A Host Mode, ignore received BSV interrupts */
2886 if (otg->phy->state >= OTG_STATE_A_IDLE)
2887 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002888
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302889 if (online) {
2890 pr_debug("PMIC: BSV set\n");
2891 set_bit(B_SESS_VLD, &motg->inputs);
2892 } else {
2893 pr_debug("PMIC: BSV clear\n");
2894 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302895 }
2896
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302897 if (!init) {
2898 init = true;
2899 complete(&pmic_vbus_init);
2900 pr_debug("PMIC: BSV init complete\n");
2901 return;
2902 }
2903
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302904 if (test_bit(MHL, &motg->inputs) ||
2905 mhl_det_in_progress) {
2906 pr_debug("PMIC: BSV interrupt ignored in MHL\n");
2907 return;
2908 }
2909
Jack Pham5ca279b2012-05-14 18:42:54 -07002910 if (atomic_read(&motg->pm_suspended))
2911 motg->sm_work_pending = true;
2912 else
2913 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002914}
2915
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302916static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002917{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302918 struct msm_otg *motg = container_of(w, struct msm_otg,
2919 pmic_id_status_work.work);
2920 int work = 0;
2921 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002922
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302923 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302924 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302925 if (!test_and_set_bit(ID, &motg->inputs)) {
2926 pr_debug("PMIC: ID set\n");
2927 work = 1;
2928 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302929 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302930 if (test_and_clear_bit(ID, &motg->inputs)) {
2931 pr_debug("PMIC: ID clear\n");
2932 set_bit(A_BUS_REQ, &motg->inputs);
2933 work = 1;
2934 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302935 }
2936
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302937 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002938 if (atomic_read(&motg->pm_suspended))
2939 motg->sm_work_pending = true;
2940 else
2941 queue_work(system_nrt_wq, &motg->sm_work);
2942 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302943 local_irq_restore(flags);
2944
2945}
2946
2947#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2948static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2949{
2950 struct msm_otg *motg = data;
2951
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05302952 if (test_bit(MHL, &motg->inputs) ||
2953 mhl_det_in_progress) {
2954 pr_debug("PMIC: Id interrupt ignored in MHL\n");
2955 return IRQ_HANDLED;
2956 }
2957
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302958 if (!aca_id_turned_on)
2959 /*schedule delayed work for 5msec for ID line state to settle*/
2960 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2961 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002962
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302963 return IRQ_HANDLED;
2964}
2965
2966static int msm_otg_mode_show(struct seq_file *s, void *unused)
2967{
2968 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002969 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302970
Steve Mucklef132c6c2012-06-06 18:30:57 -07002971 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302972 case OTG_STATE_A_HOST:
2973 seq_printf(s, "host\n");
2974 break;
2975 case OTG_STATE_B_PERIPHERAL:
2976 seq_printf(s, "peripheral\n");
2977 break;
2978 default:
2979 seq_printf(s, "none\n");
2980 break;
2981 }
2982
2983 return 0;
2984}
2985
2986static int msm_otg_mode_open(struct inode *inode, struct file *file)
2987{
2988 return single_open(file, msm_otg_mode_show, inode->i_private);
2989}
2990
2991static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2992 size_t count, loff_t *ppos)
2993{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302994 struct seq_file *s = file->private_data;
2995 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302996 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002997 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302998 int status = count;
2999 enum usb_mode_type req_mode;
3000
3001 memset(buf, 0x00, sizeof(buf));
3002
3003 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
3004 status = -EFAULT;
3005 goto out;
3006 }
3007
3008 if (!strncmp(buf, "host", 4)) {
3009 req_mode = USB_HOST;
3010 } else if (!strncmp(buf, "peripheral", 10)) {
3011 req_mode = USB_PERIPHERAL;
3012 } else if (!strncmp(buf, "none", 4)) {
3013 req_mode = USB_NONE;
3014 } else {
3015 status = -EINVAL;
3016 goto out;
3017 }
3018
3019 switch (req_mode) {
3020 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003021 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303022 case OTG_STATE_A_HOST:
3023 case OTG_STATE_B_PERIPHERAL:
3024 set_bit(ID, &motg->inputs);
3025 clear_bit(B_SESS_VLD, &motg->inputs);
3026 break;
3027 default:
3028 goto out;
3029 }
3030 break;
3031 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003032 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303033 case OTG_STATE_B_IDLE:
3034 case OTG_STATE_A_HOST:
3035 set_bit(ID, &motg->inputs);
3036 set_bit(B_SESS_VLD, &motg->inputs);
3037 break;
3038 default:
3039 goto out;
3040 }
3041 break;
3042 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07003043 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303044 case OTG_STATE_B_IDLE:
3045 case OTG_STATE_B_PERIPHERAL:
3046 clear_bit(ID, &motg->inputs);
3047 break;
3048 default:
3049 goto out;
3050 }
3051 break;
3052 default:
3053 goto out;
3054 }
3055
Steve Mucklef132c6c2012-06-06 18:30:57 -07003056 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303057 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303058out:
3059 return status;
3060}
3061
3062const struct file_operations msm_otg_mode_fops = {
3063 .open = msm_otg_mode_open,
3064 .read = seq_read,
3065 .write = msm_otg_mode_write,
3066 .llseek = seq_lseek,
3067 .release = single_release,
3068};
3069
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303070static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
3071{
3072 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003073 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303074
Steve Mucklef132c6c2012-06-06 18:30:57 -07003075 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303076 return 0;
3077}
3078
3079static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
3080{
3081 return single_open(file, msm_otg_show_otg_state, inode->i_private);
3082}
3083
3084const struct file_operations msm_otg_state_fops = {
3085 .open = msm_otg_otg_state_open,
3086 .read = seq_read,
3087 .llseek = seq_lseek,
3088 .release = single_release,
3089};
3090
Anji jonnalad270e2d2011-08-09 11:28:32 +05303091static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
3092{
3093 struct msm_otg *motg = s->private;
3094
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05303095 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05303096 return 0;
3097}
3098
3099static int msm_otg_chg_open(struct inode *inode, struct file *file)
3100{
3101 return single_open(file, msm_otg_show_chg_type, inode->i_private);
3102}
3103
3104const struct file_operations msm_otg_chg_fops = {
3105 .open = msm_otg_chg_open,
3106 .read = seq_read,
3107 .llseek = seq_lseek,
3108 .release = single_release,
3109};
3110
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303111static int msm_otg_aca_show(struct seq_file *s, void *unused)
3112{
3113 if (debug_aca_enabled)
3114 seq_printf(s, "enabled\n");
3115 else
3116 seq_printf(s, "disabled\n");
3117
3118 return 0;
3119}
3120
3121static int msm_otg_aca_open(struct inode *inode, struct file *file)
3122{
3123 return single_open(file, msm_otg_aca_show, inode->i_private);
3124}
3125
3126static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
3127 size_t count, loff_t *ppos)
3128{
3129 char buf[8];
3130
3131 memset(buf, 0x00, sizeof(buf));
3132
3133 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3134 return -EFAULT;
3135
3136 if (!strncmp(buf, "enable", 6))
3137 debug_aca_enabled = true;
3138 else
3139 debug_aca_enabled = false;
3140
3141 return count;
3142}
3143
3144const struct file_operations msm_otg_aca_fops = {
3145 .open = msm_otg_aca_open,
3146 .read = seq_read,
3147 .write = msm_otg_aca_write,
3148 .llseek = seq_lseek,
3149 .release = single_release,
3150};
3151
Manu Gautam8bdcc592012-03-06 11:26:06 +05303152static int msm_otg_bus_show(struct seq_file *s, void *unused)
3153{
3154 if (debug_bus_voting_enabled)
3155 seq_printf(s, "enabled\n");
3156 else
3157 seq_printf(s, "disabled\n");
3158
3159 return 0;
3160}
3161
3162static int msm_otg_bus_open(struct inode *inode, struct file *file)
3163{
3164 return single_open(file, msm_otg_bus_show, inode->i_private);
3165}
3166
3167static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3168 size_t count, loff_t *ppos)
3169{
3170 char buf[8];
3171 int ret;
3172 struct seq_file *s = file->private_data;
3173 struct msm_otg *motg = s->private;
3174
3175 memset(buf, 0x00, sizeof(buf));
3176
3177 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3178 return -EFAULT;
3179
3180 if (!strncmp(buf, "enable", 6)) {
3181 /* Do not vote here. Let OTG statemachine decide when to vote */
3182 debug_bus_voting_enabled = true;
3183 } else {
3184 debug_bus_voting_enabled = false;
3185 if (motg->bus_perf_client) {
3186 ret = msm_bus_scale_client_update_request(
3187 motg->bus_perf_client, 0);
3188 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003189 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303190 "for bus bw %d\n", __func__, ret);
3191 }
3192 }
3193
3194 return count;
3195}
3196
3197const struct file_operations msm_otg_bus_fops = {
3198 .open = msm_otg_bus_open,
3199 .read = seq_read,
3200 .write = msm_otg_bus_write,
3201 .llseek = seq_lseek,
3202 .release = single_release,
3203};
3204
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303205static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303206
3207static int msm_otg_debugfs_init(struct msm_otg *motg)
3208{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303209 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303210
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303211 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3212
3213 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3214 return -ENODEV;
3215
Anji jonnalad270e2d2011-08-09 11:28:32 +05303216 if (motg->pdata->mode == USB_OTG &&
3217 motg->pdata->otg_control == OTG_USER_CONTROL) {
3218
Manu Gautam8bdcc592012-03-06 11:26:06 +05303219 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303220 S_IWUSR, msm_otg_dbg_root, motg,
3221 &msm_otg_mode_fops);
3222
Manu Gautam8bdcc592012-03-06 11:26:06 +05303223 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303224 debugfs_remove(msm_otg_dbg_root);
3225 msm_otg_dbg_root = NULL;
3226 return -ENODEV;
3227 }
3228 }
3229
Manu Gautam8bdcc592012-03-06 11:26:06 +05303230 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303231 msm_otg_dbg_root, motg,
3232 &msm_otg_chg_fops);
3233
Manu Gautam8bdcc592012-03-06 11:26:06 +05303234 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303235 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303236 return -ENODEV;
3237 }
3238
Manu Gautam8bdcc592012-03-06 11:26:06 +05303239 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303240 msm_otg_dbg_root, motg,
3241 &msm_otg_aca_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("bus_voting", S_IRUGO | S_IWUSR,
3249 msm_otg_dbg_root, motg,
3250 &msm_otg_bus_fops);
3251
3252 if (!msm_otg_dentry) {
3253 debugfs_remove_recursive(msm_otg_dbg_root);
3254 return -ENODEV;
3255 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303256
3257 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3258 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3259
3260 if (!msm_otg_dentry) {
3261 debugfs_remove_recursive(msm_otg_dbg_root);
3262 return -ENODEV;
3263 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303264 return 0;
3265}
3266
3267static void msm_otg_debugfs_cleanup(void)
3268{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303269 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303270}
3271
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303272static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3273static struct platform_device *msm_otg_add_pdev(
3274 struct platform_device *ofdev, const char *name)
3275{
3276 struct platform_device *pdev;
3277 const struct resource *res = ofdev->resource;
3278 unsigned int num = ofdev->num_resources;
3279 int retval;
3280
3281 pdev = platform_device_alloc(name, -1);
3282 if (!pdev) {
3283 retval = -ENOMEM;
3284 goto error;
3285 }
3286
3287 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3288 pdev->dev.dma_mask = &msm_otg_dma_mask;
3289
3290 if (num) {
3291 retval = platform_device_add_resources(pdev, res, num);
3292 if (retval)
3293 goto error;
3294 }
3295
3296 retval = platform_device_add(pdev);
3297 if (retval)
3298 goto error;
3299
3300 return pdev;
3301
3302error:
3303 platform_device_put(pdev);
3304 return ERR_PTR(retval);
3305}
3306
3307static int msm_otg_setup_devices(struct platform_device *ofdev,
3308 enum usb_mode_type mode, bool init)
3309{
3310 const char *gadget_name = "msm_hsusb";
3311 const char *host_name = "msm_hsusb_host";
3312 static struct platform_device *gadget_pdev;
3313 static struct platform_device *host_pdev;
3314 int retval = 0;
3315
3316 if (!init) {
3317 if (gadget_pdev)
3318 platform_device_unregister(gadget_pdev);
3319 if (host_pdev)
3320 platform_device_unregister(host_pdev);
3321 return 0;
3322 }
3323
3324 switch (mode) {
3325 case USB_OTG:
3326 /* fall through */
3327 case USB_PERIPHERAL:
3328 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3329 if (IS_ERR(gadget_pdev)) {
3330 retval = PTR_ERR(gadget_pdev);
3331 break;
3332 }
3333 if (mode == USB_PERIPHERAL)
3334 break;
3335 /* fall through */
3336 case USB_HOST:
3337 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3338 if (IS_ERR(host_pdev)) {
3339 retval = PTR_ERR(host_pdev);
3340 if (mode == USB_OTG)
3341 platform_device_unregister(gadget_pdev);
3342 }
3343 break;
3344 default:
3345 break;
3346 }
3347
3348 return retval;
3349}
3350
3351struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3352{
3353 struct device_node *node = pdev->dev.of_node;
3354 struct msm_otg_platform_data *pdata;
3355 int len = 0;
3356
3357 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3358 if (!pdata) {
3359 pr_err("unable to allocate platform data\n");
3360 return NULL;
3361 }
3362 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3363 if (len) {
3364 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3365 if (!pdata->phy_init_seq)
3366 return NULL;
3367 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3368 pdata->phy_init_seq,
3369 len/sizeof(*pdata->phy_init_seq));
3370 }
3371 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3372 &pdata->power_budget);
3373 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3374 &pdata->mode);
3375 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3376 &pdata->otg_control);
3377 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3378 &pdata->default_mode);
3379 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3380 &pdata->phy_type);
3381 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3382 &pdata->pmic_id_irq);
Manu Gautambd53fba2012-07-31 16:13:06 +05303383 pdata->disable_reset_on_disconnect = of_property_read_bool(node,
3384 "qcom,hsusb-otg-disable-reset");
3385
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303386 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303387}
3388
3389static int __init msm_otg_probe(struct platform_device *pdev)
3390{
3391 int ret = 0;
3392 struct resource *res;
3393 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003394 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303395 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303396
3397 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303398
3399 if (pdev->dev.of_node) {
3400 dev_dbg(&pdev->dev, "device tree enabled\n");
3401 pdata = msm_otg_dt_to_pdata(pdev);
3402 if (!pdata)
3403 return -ENOMEM;
3404 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3405 if (ret) {
3406 dev_err(&pdev->dev, "devices setup failed\n");
3407 return ret;
3408 }
3409 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303410 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3411 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303412 } else {
3413 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303414 }
3415
3416 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3417 if (!motg) {
3418 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3419 return -ENOMEM;
3420 }
3421
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003422 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3423 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003424 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3425 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303426 goto free_motg;
3427 }
3428
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003429 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303430 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003431 phy = &motg->phy;
3432 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303433
3434 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303435 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3436 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3437 * PMIC can detect ACA ID_GND and generate an interrupt.
3438 */
3439 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3440 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3441 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003442 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303443 }
3444
Ofir Cohen4da266f2012-01-03 10:19:29 +02003445 /* initialize reset counter */
3446 motg->reset_counter = 0;
3447
Amit Blay02eff132011-09-21 16:46:24 +03003448 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003449 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003450 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003451 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303452
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303453 /*
3454 * Targets on which link uses asynchronous reset methodology,
3455 * free running clock is not required during the reset.
3456 */
Manu Gautam5143b252012-01-05 19:25:23 -08003457 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303458 if (IS_ERR(motg->clk))
3459 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3460 else
3461 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303462
3463 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003464 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303465 * CORE CLK must be running at >55Mhz for correct HSUSB
3466 * operation and USB core cannot tolerate frequency changes on
3467 * CORE CLK. For such USB cores, vote for maximum clk frequency
3468 * on pclk source
3469 */
Manu Gautam5143b252012-01-05 19:25:23 -08003470 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3471 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303472 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003473 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303474 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003475 goto put_clk;
3476 }
3477 clk_set_rate(motg->core_clk, INT_MAX);
3478
3479 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3480 if (IS_ERR(motg->pclk)) {
3481 dev_err(&pdev->dev, "failed to get iface_clk\n");
3482 ret = PTR_ERR(motg->pclk);
3483 goto put_core_clk;
3484 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303485
3486 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3487 if (!res) {
3488 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3489 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003490 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303491 }
3492
3493 motg->regs = ioremap(res->start, resource_size(res));
3494 if (!motg->regs) {
3495 dev_err(&pdev->dev, "ioremap failed\n");
3496 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003497 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303498 }
3499 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3500
3501 motg->irq = platform_get_irq(pdev, 0);
3502 if (!motg->irq) {
3503 dev_err(&pdev->dev, "platform_get_irq failed\n");
3504 ret = -ENODEV;
3505 goto free_regs;
3506 }
3507
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003508 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003509 if (IS_ERR(motg->xo_handle)) {
3510 dev_err(&pdev->dev, "%s not able to get the handle "
3511 "to vote for TCXO D0 buffer\n", __func__);
3512 ret = PTR_ERR(motg->xo_handle);
3513 goto free_regs;
3514 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303515
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003516 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003517 if (ret) {
3518 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3519 "D0 buffer%d\n", __func__, ret);
3520 goto free_xo_handle;
3521 }
3522
Manu Gautam28b1bac2012-01-30 16:43:06 +05303523 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303524
Mayank Rana248698c2012-04-19 00:03:16 +05303525 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003526 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303527 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003528 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303529 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003530 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003531 ret = PTR_ERR(hsusb_vddcx);
Mayank Rana248698c2012-04-19 00:03:16 +05303532 goto devote_xo_handle;
3533 }
3534 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303535 }
3536
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003537 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303538 if (ret) {
3539 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303540 goto devote_xo_handle;
3541 }
3542
3543 ret = regulator_enable(hsusb_vddcx);
3544 if (ret) {
3545 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3546 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303547 }
3548
3549 ret = msm_hsusb_ldo_init(motg, 1);
3550 if (ret) {
3551 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303552 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303553 }
3554
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303555 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303556 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3557 "mhl_usb_hs_switch");
3558 if (IS_ERR(mhl_usb_hs_switch)) {
3559 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Hemant Kumar41812392012-07-13 15:26:20 -07003560 ret = PTR_ERR(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303561 goto free_ldo_init;
3562 }
3563 }
3564
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003565 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303566 if (ret) {
3567 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003568 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303569 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303570 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303571
3572 writel(0, USB_USBINTR);
3573 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003574 /* Ensure that above STOREs are completed before enabling interrupts */
3575 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303576
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303577 ret = msm_otg_mhl_register_callback(motg, msm_otg_mhl_notify_online);
3578 if (ret)
3579 dev_dbg(&pdev->dev, "MHL can not be supported\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003580 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303581 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303582 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303583 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303584 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303585 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3586 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303587 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3588 "msm_otg", motg);
3589 if (ret) {
3590 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003591 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303592 }
3593
Jack Pham87f202f2012-08-06 00:24:22 -07003594 if (pdata->otg_control == OTG_PHY_CONTROL && pdata->mpm_otgsessvld_int)
3595 msm_mpm_enable_pin(pdata->mpm_otgsessvld_int, 1);
3596
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003597 phy->init = msm_otg_reset;
3598 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003599 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303600
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003601 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303602
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003603 phy->otg->phy = &motg->phy;
3604 phy->otg->set_host = msm_otg_set_host;
3605 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003606 phy->otg->start_hnp = msm_otg_start_hnp;
3607 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003608
3609 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303610 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003611 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303612 goto free_irq;
3613 }
3614
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303615 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303616 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003617 if (motg->pdata->pmic_id_irq) {
3618 ret = request_irq(motg->pdata->pmic_id_irq,
3619 msm_pmic_id_irq,
3620 IRQF_TRIGGER_RISING |
3621 IRQF_TRIGGER_FALLING,
3622 "msm_otg", motg);
3623 if (ret) {
3624 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003625 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003626 }
3627 } else {
3628 ret = -ENODEV;
3629 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003630 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003631 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303632 }
3633
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303634 msm_hsusb_mhl_switch_enable(motg, 1);
3635
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303636 platform_set_drvdata(pdev, motg);
3637 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003638 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303639
Anji jonnalad270e2d2011-08-09 11:28:32 +05303640 ret = msm_otg_debugfs_init(motg);
3641 if (ret)
3642 dev_dbg(&pdev->dev, "mode debugfs file is"
3643 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303644
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003645 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3646 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3647
Amit Blay58b31472011-11-18 09:39:39 +02003648 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3649 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303650 (!(motg->pdata->mode == USB_OTG) ||
3651 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003652 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303653 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003654
Amit Blay58b31472011-11-18 09:39:39 +02003655 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3656 motg->caps = ALLOW_PHY_RETENTION;
3657 }
3658
Amit Blay6fa647a2012-05-24 14:12:08 +03003659 if (motg->pdata->enable_lpm_on_dev_suspend)
3660 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3661
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003662 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303663 pm_runtime_set_active(&pdev->dev);
3664 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303665
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303666 if (motg->pdata->bus_scale_table) {
3667 motg->bus_perf_client =
3668 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3669 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003670 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303671 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303672 else
3673 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303674 }
3675
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303676 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003677
Steve Mucklef132c6c2012-06-06 18:30:57 -07003678remove_phy:
3679 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303680free_irq:
3681 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003682destroy_wlock:
3683 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303684 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003685 msm_hsusb_ldo_enable(motg, 0);
3686free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303687 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303688free_hsusb_vddcx:
3689 regulator_disable(hsusb_vddcx);
3690free_config_vddcx:
3691 regulator_set_voltage(hsusb_vddcx,
3692 vdd_val[motg->vdd_type][VDD_NONE],
3693 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003694devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303695 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003696 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003697free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003698 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303699free_regs:
3700 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003701put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303702 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303703put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003704 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303705put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303706 if (!IS_ERR(motg->clk))
3707 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003708 if (!IS_ERR(motg->phy_reset_clk))
3709 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003710free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003711 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303712free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303713 kfree(motg);
3714 return ret;
3715}
3716
3717static int __devexit msm_otg_remove(struct platform_device *pdev)
3718{
3719 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003720 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303721 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303722
3723 if (otg->host || otg->gadget)
3724 return -EBUSY;
3725
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303726 if (pdev->dev.of_node)
3727 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003728 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3729 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondeti2aec9f32012-06-13 09:06:03 +05303730 msm_otg_mhl_register_callback(motg, NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303731 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303732 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303733 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303734 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303735
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303736 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303737
3738 device_init_wakeup(&pdev->dev, 0);
3739 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003740 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303741
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303742 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003743 if (motg->pdata->pmic_id_irq)
3744 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003745 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303746 free_irq(motg->irq, motg);
3747
Jack Pham87f202f2012-08-06 00:24:22 -07003748 if (motg->pdata->otg_control == OTG_PHY_CONTROL &&
3749 motg->pdata->mpm_otgsessvld_int)
3750 msm_mpm_enable_pin(motg->pdata->mpm_otgsessvld_int, 0);
3751
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303752 /*
3753 * Put PHY in low power mode.
3754 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003755 ulpi_read(otg->phy, 0x14);
3756 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303757
3758 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3759 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3760 if (readl(USB_PORTSC) & PORTSC_PHCD)
3761 break;
3762 udelay(1);
3763 cnt++;
3764 }
3765 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003766 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303767
Manu Gautam28b1bac2012-01-30 16:43:06 +05303768 clk_disable_unprepare(motg->pclk);
3769 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003770 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003771 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303772 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303773 regulator_disable(hsusb_vddcx);
3774 regulator_set_voltage(hsusb_vddcx,
3775 vdd_val[motg->vdd_type][VDD_NONE],
3776 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303777
3778 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303779 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303780
Amit Blay02eff132011-09-21 16:46:24 +03003781 if (!IS_ERR(motg->phy_reset_clk))
3782 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303783 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303784 if (!IS_ERR(motg->clk))
3785 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003786 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303787
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303788 if (motg->bus_perf_client)
3789 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303790
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003791 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303792 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303793 return 0;
3794}
3795
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303796#ifdef CONFIG_PM_RUNTIME
3797static int msm_otg_runtime_idle(struct device *dev)
3798{
3799 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003800 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303801
3802 dev_dbg(dev, "OTG runtime idle\n");
3803
Steve Mucklef132c6c2012-06-06 18:30:57 -07003804 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303805 return -EAGAIN;
3806 else
3807 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303808}
3809
3810static int msm_otg_runtime_suspend(struct device *dev)
3811{
3812 struct msm_otg *motg = dev_get_drvdata(dev);
3813
3814 dev_dbg(dev, "OTG runtime suspend\n");
3815 return msm_otg_suspend(motg);
3816}
3817
3818static int msm_otg_runtime_resume(struct device *dev)
3819{
3820 struct msm_otg *motg = dev_get_drvdata(dev);
3821
3822 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303823 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303824 return msm_otg_resume(motg);
3825}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303826#endif
3827
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303828#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303829static int msm_otg_pm_suspend(struct device *dev)
3830{
Jack Pham5ca279b2012-05-14 18:42:54 -07003831 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303832 struct msm_otg *motg = dev_get_drvdata(dev);
3833
3834 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003835
3836 atomic_set(&motg->pm_suspended, 1);
3837 ret = msm_otg_suspend(motg);
3838 if (ret)
3839 atomic_set(&motg->pm_suspended, 0);
3840
3841 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303842}
3843
3844static int msm_otg_pm_resume(struct device *dev)
3845{
Jack Pham5ca279b2012-05-14 18:42:54 -07003846 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303847 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303848
3849 dev_dbg(dev, "OTG PM resume\n");
3850
Jack Pham5ca279b2012-05-14 18:42:54 -07003851 atomic_set(&motg->pm_suspended, 0);
3852 if (motg->sm_work_pending) {
3853 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303854
Jack Pham5ca279b2012-05-14 18:42:54 -07003855 pm_runtime_get_noresume(dev);
3856 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303857
Jack Pham5ca279b2012-05-14 18:42:54 -07003858 /* Update runtime PM status */
3859 pm_runtime_disable(dev);
3860 pm_runtime_set_active(dev);
3861 pm_runtime_enable(dev);
3862
3863 queue_work(system_nrt_wq, &motg->sm_work);
3864 }
3865
3866 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303867}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303868#endif
3869
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303870#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303871static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303872 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3873 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3874 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303875};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303876#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303877
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303878static struct of_device_id msm_otg_dt_match[] = {
3879 { .compatible = "qcom,hsusb-otg",
3880 },
3881 {}
3882};
3883
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303884static struct platform_driver msm_otg_driver = {
3885 .remove = __devexit_p(msm_otg_remove),
3886 .driver = {
3887 .name = DRIVER_NAME,
3888 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303889#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303890 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303891#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303892 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303893 },
3894};
3895
3896static int __init msm_otg_init(void)
3897{
3898 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3899}
3900
3901static void __exit msm_otg_exit(void)
3902{
3903 platform_driver_unregister(&msm_otg_driver);
3904}
3905
3906module_init(msm_otg_init);
3907module_exit(msm_otg_exit);
3908
3909MODULE_LICENSE("GPL v2");
3910MODULE_DESCRIPTION("MSM USB transceiver driver");