blob: c1e1e13a0971222010a531eddb8bea6d558e7f85 [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 Kondetie0c201f2010-12-07 17:53:55 +053043
44#include <mach/clk.h>
Anji jonnala7da3f262011-12-02 17:22:14 -080045#include <mach/msm_xo.h>
Manu Gautamcd82e9d2011-12-20 14:17:28 +053046#include <mach/msm_bus.h>
Mayank Rana248698c2012-04-19 00:03:16 +053047#include <mach/rpm-regulator.h>
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053048
49#define MSM_USB_BASE (motg->regs)
50#define DRIVER_NAME "msm_otg"
51
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +053052#define ID_TIMER_FREQ (jiffies + msecs_to_jiffies(500))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +053053#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
Anji jonnala11aa5c42011-05-04 10:19:48 +053054#define USB_PHY_3P3_VOL_MIN 3050000 /* uV */
55#define USB_PHY_3P3_VOL_MAX 3300000 /* uV */
56#define USB_PHY_3P3_HPM_LOAD 50000 /* uA */
57#define USB_PHY_3P3_LPM_LOAD 4000 /* uA */
58
59#define USB_PHY_1P8_VOL_MIN 1800000 /* uV */
60#define USB_PHY_1P8_VOL_MAX 1800000 /* uV */
61#define USB_PHY_1P8_HPM_LOAD 50000 /* uA */
62#define USB_PHY_1P8_LPM_LOAD 4000 /* uA */
63
Mayank Rana248698c2012-04-19 00:03:16 +053064#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Vamsi Krishna132b2762011-11-11 16:09:20 -080065#define USB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
Anji jonnala11aa5c42011-05-04 10:19:48 +053066#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
67
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053068static DECLARE_COMPLETION(pmic_vbus_init);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069static struct msm_otg *the_msm_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053070static bool debug_aca_enabled;
Manu Gautam8bdcc592012-03-06 11:26:06 +053071static bool debug_bus_voting_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070072
Anji jonnala11aa5c42011-05-04 10:19:48 +053073static struct regulator *hsusb_3p3;
74static struct regulator *hsusb_1p8;
75static struct regulator *hsusb_vddcx;
Mayank Ranae3926882011-12-26 09:47:54 +053076static struct regulator *vbus_otg;
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +053077static struct regulator *mhl_usb_hs_switch;
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +053078static struct power_supply *psy;
Anji jonnala11aa5c42011-05-04 10:19:48 +053079
Pavankumar Kondeti4960f312011-12-06 15:46:14 +053080static bool aca_id_turned_on;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053081static inline bool aca_enabled(void)
Anji jonnala11aa5c42011-05-04 10:19:48 +053082{
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +053083#ifdef CONFIG_USB_MSM_ACA
84 return true;
85#else
86 return debug_aca_enabled;
87#endif
Anji jonnala11aa5c42011-05-04 10:19:48 +053088}
89
Mayank Rana248698c2012-04-19 00:03:16 +053090static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
91 { /* VDD_CX CORNER Voting */
92 [VDD_NONE] = RPM_VREG_CORNER_NONE,
93 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
94 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
95 },
96 { /* VDD_CX Voltage Voting */
97 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
98 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
99 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
100 },
101};
Anji jonnala11aa5c42011-05-04 10:19:48 +0530102
103static int msm_hsusb_ldo_init(struct msm_otg *motg, int init)
104{
105 int rc = 0;
106
107 if (init) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700108 hsusb_3p3 = devm_regulator_get(motg->phy.dev, "HSUSB_3p3");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530109 if (IS_ERR(hsusb_3p3)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200110 dev_err(motg->phy.dev, "unable to get hsusb 3p3\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530111 return PTR_ERR(hsusb_3p3);
112 }
113
114 rc = regulator_set_voltage(hsusb_3p3, USB_PHY_3P3_VOL_MIN,
115 USB_PHY_3P3_VOL_MAX);
116 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700117 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700118 "hsusb 3p3\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530119 return rc;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530120 }
Steve Mucklef132c6c2012-06-06 18:30:57 -0700121 hsusb_1p8 = devm_regulator_get(motg->phy.dev, "HSUSB_1p8");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530122 if (IS_ERR(hsusb_1p8)) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200123 dev_err(motg->phy.dev, "unable to get hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530124 rc = PTR_ERR(hsusb_1p8);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125 goto put_3p3_lpm;
Anji jonnala11aa5c42011-05-04 10:19:48 +0530126 }
127 rc = regulator_set_voltage(hsusb_1p8, USB_PHY_1P8_VOL_MIN,
128 USB_PHY_1P8_VOL_MAX);
129 if (rc) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700130 dev_err(motg->phy.dev, "unable to set voltage level for"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700131 "hsusb 1p8\n");
Anji jonnala11aa5c42011-05-04 10:19:48 +0530132 goto put_1p8;
133 }
134
135 return 0;
136 }
137
Anji jonnala11aa5c42011-05-04 10:19:48 +0530138put_1p8:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 regulator_set_voltage(hsusb_1p8, 0, USB_PHY_1P8_VOL_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700140put_3p3_lpm:
141 regulator_set_voltage(hsusb_3p3, 0, USB_PHY_3P3_VOL_MAX);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530142 return rc;
143}
144
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530145static int msm_hsusb_config_vddcx(int high)
146{
Mayank Rana248698c2012-04-19 00:03:16 +0530147 struct msm_otg *motg = the_msm_otg;
148 enum usb_vdd_type vdd_type = motg->vdd_type;
149 int max_vol = vdd_val[vdd_type][VDD_MAX];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530150 int min_vol;
151 int ret;
152
Mayank Rana248698c2012-04-19 00:03:16 +0530153 min_vol = vdd_val[vdd_type][!!high];
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530154 ret = regulator_set_voltage(hsusb_vddcx, min_vol, max_vol);
155 if (ret) {
156 pr_err("%s: unable to set the voltage for regulator "
157 "HSUSB_VDDCX\n", __func__);
158 return ret;
159 }
160
161 pr_debug("%s: min_vol:%d max_vol:%d\n", __func__, min_vol, max_vol);
162
163 return ret;
164}
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166static int msm_hsusb_ldo_enable(struct msm_otg *motg, int on)
Anji jonnala11aa5c42011-05-04 10:19:48 +0530167{
168 int ret = 0;
169
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530170 if (IS_ERR(hsusb_1p8)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530171 pr_err("%s: HSUSB_1p8 is not initialized\n", __func__);
172 return -ENODEV;
173 }
174
Pavankumar Kondeti68964c92011-10-27 14:58:56 +0530175 if (IS_ERR(hsusb_3p3)) {
Anji jonnala11aa5c42011-05-04 10:19:48 +0530176 pr_err("%s: HSUSB_3p3 is not initialized\n", __func__);
177 return -ENODEV;
178 }
179
180 if (on) {
181 ret = regulator_set_optimum_mode(hsusb_1p8,
182 USB_PHY_1P8_HPM_LOAD);
183 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530185 "HSUSB_1p8\n", __func__);
186 return ret;
187 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188
189 ret = regulator_enable(hsusb_1p8);
190 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700191 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700192 __func__);
193 regulator_set_optimum_mode(hsusb_1p8, 0);
194 return ret;
195 }
196
Anji jonnala11aa5c42011-05-04 10:19:48 +0530197 ret = regulator_set_optimum_mode(hsusb_3p3,
198 USB_PHY_3P3_HPM_LOAD);
199 if (ret < 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200 pr_err("%s: Unable to set HPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530201 "HSUSB_3p3\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 regulator_set_optimum_mode(hsusb_1p8, 0);
203 regulator_disable(hsusb_1p8);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530204 return ret;
205 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700206
207 ret = regulator_enable(hsusb_3p3);
208 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700209 dev_err(motg->phy.dev, "%s: unable to enable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 __func__);
211 regulator_set_optimum_mode(hsusb_3p3, 0);
212 regulator_set_optimum_mode(hsusb_1p8, 0);
213 regulator_disable(hsusb_1p8);
214 return ret;
215 }
216
Anji jonnala11aa5c42011-05-04 10:19:48 +0530217 } else {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 ret = regulator_disable(hsusb_1p8);
219 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700220 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 1p8\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221 __func__);
222 return ret;
223 }
224
225 ret = regulator_set_optimum_mode(hsusb_1p8, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530226 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530228 "HSUSB_1p8\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229
230 ret = regulator_disable(hsusb_3p3);
231 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700232 dev_err(motg->phy.dev, "%s: unable to disable the hsusb 3p3\n",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 __func__);
234 return ret;
235 }
236 ret = regulator_set_optimum_mode(hsusb_3p3, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +0530237 if (ret < 0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 pr_err("%s: Unable to set LPM of the regulator:"
Anji jonnala11aa5c42011-05-04 10:19:48 +0530239 "HSUSB_3p3\n", __func__);
240 }
241
242 pr_debug("reg (%s)\n", on ? "HPM" : "LPM");
243 return ret < 0 ? ret : 0;
244}
245
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530246static void msm_hsusb_mhl_switch_enable(struct msm_otg *motg, bool on)
247{
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530248 struct msm_otg_platform_data *pdata = motg->pdata;
249
250 if (!pdata->mhl_enable)
251 return;
252
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530253 if (!mhl_usb_hs_switch) {
254 pr_err("%s: mhl_usb_hs_switch is NULL.\n", __func__);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530255 return;
256 }
257
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530258 if (on) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530259 if (regulator_enable(mhl_usb_hs_switch))
260 pr_err("unable to enable mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530261 } else {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +0530262 regulator_disable(mhl_usb_hs_switch);
Mayank Rana9e9a2ac2012-03-24 04:05:28 +0530263 }
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530264}
265
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200266static int ulpi_read(struct usb_phy *phy, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530267{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200268 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530269 int cnt = 0;
270
271 /* initiate read operation */
272 writel(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
273 USB_ULPI_VIEWPORT);
274
275 /* wait for completion */
276 while (cnt < ULPI_IO_TIMEOUT_USEC) {
277 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
278 break;
279 udelay(1);
280 cnt++;
281 }
282
283 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200284 dev_err(phy->dev, "ulpi_read: timeout %08x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530285 readl(USB_ULPI_VIEWPORT));
286 return -ETIMEDOUT;
287 }
288 return ULPI_DATA_READ(readl(USB_ULPI_VIEWPORT));
289}
290
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200291static int ulpi_write(struct usb_phy *phy, u32 val, u32 reg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530292{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200293 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530294 int cnt = 0;
295
296 /* initiate write operation */
297 writel(ULPI_RUN | ULPI_WRITE |
298 ULPI_ADDR(reg) | ULPI_DATA(val),
299 USB_ULPI_VIEWPORT);
300
301 /* wait for completion */
302 while (cnt < ULPI_IO_TIMEOUT_USEC) {
303 if (!(readl(USB_ULPI_VIEWPORT) & ULPI_RUN))
304 break;
305 udelay(1);
306 cnt++;
307 }
308
309 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200310 dev_err(phy->dev, "ulpi_write: timeout\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530311 return -ETIMEDOUT;
312 }
313 return 0;
314}
315
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200316static struct usb_phy_io_ops msm_otg_io_ops = {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530317 .read = ulpi_read,
318 .write = ulpi_write,
319};
320
321static void ulpi_init(struct msm_otg *motg)
322{
323 struct msm_otg_platform_data *pdata = motg->pdata;
324 int *seq = pdata->phy_init_seq;
325
326 if (!seq)
327 return;
328
329 while (seq[0] >= 0) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200330 dev_vdbg(motg->phy.dev, "ulpi: write 0x%02x to 0x%02x\n",
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530331 seq[0], seq[1]);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200332 ulpi_write(&motg->phy, seq[0], seq[1]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530333 seq += 2;
334 }
335}
336
337static int msm_otg_link_clk_reset(struct msm_otg *motg, bool assert)
338{
339 int ret;
340
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530341 if (IS_ERR(motg->clk))
342 return 0;
343
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530344 if (assert) {
345 ret = clk_reset(motg->clk, CLK_RESET_ASSERT);
346 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200347 dev_err(motg->phy.dev, "usb hs_clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530348 } else {
349 ret = clk_reset(motg->clk, CLK_RESET_DEASSERT);
350 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200351 dev_err(motg->phy.dev, "usb hs_clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530352 }
353 return ret;
354}
355
356static int msm_otg_phy_clk_reset(struct msm_otg *motg)
357{
358 int ret;
359
Amit Blay02eff132011-09-21 16:46:24 +0300360 if (IS_ERR(motg->phy_reset_clk))
361 return 0;
362
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530363 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_ASSERT);
364 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200365 dev_err(motg->phy.dev, "usb phy clk assert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530366 return ret;
367 }
368 usleep_range(10000, 12000);
369 ret = clk_reset(motg->phy_reset_clk, CLK_RESET_DEASSERT);
370 if (ret)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200371 dev_err(motg->phy.dev, "usb phy clk deassert failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530372 return ret;
373}
374
375static int msm_otg_phy_reset(struct msm_otg *motg)
376{
377 u32 val;
378 int ret;
379 int retries;
380
381 ret = msm_otg_link_clk_reset(motg, 1);
382 if (ret)
383 return ret;
384 ret = msm_otg_phy_clk_reset(motg);
385 if (ret)
386 return ret;
387 ret = msm_otg_link_clk_reset(motg, 0);
388 if (ret)
389 return ret;
390
391 val = readl(USB_PORTSC) & ~PORTSC_PTS_MASK;
392 writel(val | PORTSC_PTS_ULPI, USB_PORTSC);
393
394 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200395 ret = ulpi_write(&motg->phy, ULPI_FUNC_CTRL_SUSPENDM,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530396 ULPI_CLR(ULPI_FUNC_CTRL));
397 if (!ret)
398 break;
399 ret = msm_otg_phy_clk_reset(motg);
400 if (ret)
401 return ret;
402 }
403 if (!retries)
404 return -ETIMEDOUT;
405
406 /* This reset calibrates the phy, if the above write succeeded */
407 ret = msm_otg_phy_clk_reset(motg);
408 if (ret)
409 return ret;
410
411 for (retries = 3; retries > 0; retries--) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200412 ret = ulpi_read(&motg->phy, ULPI_DEBUG);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530413 if (ret != -ETIMEDOUT)
414 break;
415 ret = msm_otg_phy_clk_reset(motg);
416 if (ret)
417 return ret;
418 }
419 if (!retries)
420 return -ETIMEDOUT;
421
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200422 dev_info(motg->phy.dev, "phy_reset: success\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530423 return 0;
424}
425
426#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530427static int msm_otg_link_reset(struct msm_otg *motg)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530428{
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530429 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530430
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530431 writel_relaxed(USBCMD_RESET, USB_USBCMD);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530432 while (cnt < LINK_RESET_TIMEOUT_USEC) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530433 if (!(readl_relaxed(USB_USBCMD) & USBCMD_RESET))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530434 break;
435 udelay(1);
436 cnt++;
437 }
438 if (cnt >= LINK_RESET_TIMEOUT_USEC)
439 return -ETIMEDOUT;
440
441 /* select ULPI phy */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530442 writel_relaxed(0x80000000, USB_PORTSC);
443 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530444 writel_relaxed(0x08, USB_AHBMODE);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530445
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530446 return 0;
447}
448
Steve Mucklef132c6c2012-06-06 18:30:57 -0700449static int msm_otg_reset(struct usb_phy *phy)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530450{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700451 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530452 struct msm_otg_platform_data *pdata = motg->pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530453 int ret;
454 u32 val = 0;
455 u32 ulpi_val = 0;
456
Ofir Cohen4da266f2012-01-03 10:19:29 +0200457 /*
458 * USB PHY and Link reset also reset the USB BAM.
459 * Thus perform reset operation only once to avoid
460 * USB BAM reset on other cases e.g. USB cable disconnections.
461 */
462 if (pdata->disable_reset_on_disconnect) {
463 if (motg->reset_counter)
464 return 0;
465 else
466 motg->reset_counter++;
467 }
468
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530469 if (!IS_ERR(motg->clk))
470 clk_prepare_enable(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530471 ret = msm_otg_phy_reset(motg);
472 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700473 dev_err(phy->dev, "phy_reset failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530474 return ret;
475 }
476
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530477 aca_id_turned_on = false;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530478 ret = msm_otg_link_reset(motg);
479 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700480 dev_err(phy->dev, "link reset failed\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530481 return ret;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530482 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530483 msleep(100);
484
Anji jonnalaa8b8d732011-12-06 10:03:24 +0530485 ulpi_init(motg);
486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 /* Ensure that RESET operation is completed before turning off clock */
488 mb();
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +0530489
Pavankumar Kondeti923262e2012-04-20 15:34:24 +0530490 if (!IS_ERR(motg->clk))
491 clk_disable_unprepare(motg->clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530492
493 if (pdata->otg_control == OTG_PHY_CONTROL) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530494 val = readl_relaxed(USB_OTGSC);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530495 if (pdata->mode == USB_OTG) {
496 ulpi_val = ULPI_INT_IDGRD | ULPI_INT_SESS_VALID;
497 val |= OTGSC_IDIE | OTGSC_BSVIE;
498 } else if (pdata->mode == USB_PERIPHERAL) {
499 ulpi_val = ULPI_INT_SESS_VALID;
500 val |= OTGSC_BSVIE;
501 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530502 writel_relaxed(val, USB_OTGSC);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200503 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_RISE);
504 ulpi_write(phy, ulpi_val, ULPI_USB_INT_EN_FALL);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530505 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700506 ulpi_write(phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +0530507 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondeti446f4542012-02-01 13:57:13 +0530508 /* Enable PMIC pull-up */
509 pm8xxx_usb_id_pullup(1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +0530510 }
511
512 return 0;
513}
514
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530515static const char *timer_string(int bit)
516{
517 switch (bit) {
518 case A_WAIT_VRISE: return "a_wait_vrise";
519 case A_WAIT_VFALL: return "a_wait_vfall";
520 case B_SRP_FAIL: return "b_srp_fail";
521 case A_WAIT_BCON: return "a_wait_bcon";
522 case A_AIDL_BDIS: return "a_aidl_bdis";
523 case A_BIDL_ADIS: return "a_bidl_adis";
524 case B_ASE0_BRST: return "b_ase0_brst";
525 case A_TST_MAINT: return "a_tst_maint";
526 case B_TST_SRP: return "b_tst_srp";
527 case B_TST_CONFIG: return "b_tst_config";
528 default: return "UNDEFINED";
529 }
530}
531
532static enum hrtimer_restart msm_otg_timer_func(struct hrtimer *hrtimer)
533{
534 struct msm_otg *motg = container_of(hrtimer, struct msm_otg, timer);
535
536 switch (motg->active_tmout) {
537 case A_WAIT_VRISE:
538 /* TODO: use vbus_vld interrupt */
539 set_bit(A_VBUS_VLD, &motg->inputs);
540 break;
541 case A_TST_MAINT:
542 /* OTG PET: End session after TA_TST_MAINT */
543 set_bit(A_BUS_DROP, &motg->inputs);
544 break;
545 case B_TST_SRP:
546 /*
547 * OTG PET: Initiate SRP after TB_TST_SRP of
548 * previous session end.
549 */
550 set_bit(B_BUS_REQ, &motg->inputs);
551 break;
552 case B_TST_CONFIG:
553 clear_bit(A_CONN, &motg->inputs);
554 break;
555 default:
556 set_bit(motg->active_tmout, &motg->tmouts);
557 }
558
559 pr_debug("expired %s timer\n", timer_string(motg->active_tmout));
560 queue_work(system_nrt_wq, &motg->sm_work);
561 return HRTIMER_NORESTART;
562}
563
564static void msm_otg_del_timer(struct msm_otg *motg)
565{
566 int bit = motg->active_tmout;
567
568 pr_debug("deleting %s timer. remaining %lld msec\n", timer_string(bit),
569 div_s64(ktime_to_us(hrtimer_get_remaining(
570 &motg->timer)), 1000));
571 hrtimer_cancel(&motg->timer);
572 clear_bit(bit, &motg->tmouts);
573}
574
575static void msm_otg_start_timer(struct msm_otg *motg, int time, int bit)
576{
577 clear_bit(bit, &motg->tmouts);
578 motg->active_tmout = bit;
579 pr_debug("starting %s timer\n", timer_string(bit));
580 hrtimer_start(&motg->timer,
581 ktime_set(time / 1000, (time % 1000) * 1000000),
582 HRTIMER_MODE_REL);
583}
584
585static void msm_otg_init_timer(struct msm_otg *motg)
586{
587 hrtimer_init(&motg->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
588 motg->timer.function = msm_otg_timer_func;
589}
590
Steve Mucklef132c6c2012-06-06 18:30:57 -0700591static int msm_otg_start_hnp(struct usb_otg *otg)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530592{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700593 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530594
Steve Mucklef132c6c2012-06-06 18:30:57 -0700595 if (otg->phy->state != OTG_STATE_A_HOST) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530596 pr_err("HNP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700597 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530598 return -EINVAL;
599 }
600
601 pr_debug("A-Host: HNP initiated\n");
602 clear_bit(A_BUS_REQ, &motg->inputs);
603 queue_work(system_nrt_wq, &motg->sm_work);
604 return 0;
605}
606
Steve Mucklef132c6c2012-06-06 18:30:57 -0700607static int msm_otg_start_srp(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 u32 val;
611 int ret = 0;
612
Steve Mucklef132c6c2012-06-06 18:30:57 -0700613 if (otg->phy->state != OTG_STATE_B_IDLE) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530614 pr_err("SRP can not be initiated in %s state\n",
Steve Mucklef132c6c2012-06-06 18:30:57 -0700615 otg_state_string(otg->phy->state));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530616 ret = -EINVAL;
617 goto out;
618 }
619
620 if ((jiffies - motg->b_last_se0_sess) < msecs_to_jiffies(TB_SRP_INIT)) {
621 pr_debug("initial conditions of SRP are not met. Try again"
622 "after some time\n");
623 ret = -EAGAIN;
624 goto out;
625 }
626
627 pr_debug("B-Device SRP started\n");
628
629 /*
630 * PHY won't pull D+ high unless it detects Vbus valid.
631 * Since by definition, SRP is only done when Vbus is not valid,
632 * software work-around needs to be used to spoof the PHY into
633 * thinking it is valid. This can be done using the VBUSVLDEXTSEL and
634 * VBUSVLDEXT register bits.
635 */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700636 ulpi_write(otg->phy, 0x03, 0x97);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530637 /*
638 * Harware auto assist data pulsing: Data pulse is given
639 * for 7msec; wait for vbus
640 */
641 val = readl_relaxed(USB_OTGSC);
642 writel_relaxed((val & ~OTGSC_INTSTS_MASK) | OTGSC_HADP, USB_OTGSC);
643
644 /* VBUS plusing is obsoleted in OTG 2.0 supplement */
645out:
646 return ret;
647}
648
Steve Mucklef132c6c2012-06-06 18:30:57 -0700649static void msm_otg_host_hnp_enable(struct usb_otg *otg, bool enable)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530650{
651 struct usb_hcd *hcd = bus_to_hcd(otg->host);
652 struct usb_device *rhub = otg->host->root_hub;
653
654 if (enable) {
655 pm_runtime_disable(&rhub->dev);
656 rhub->state = USB_STATE_NOTATTACHED;
657 hcd->driver->bus_suspend(hcd);
658 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
659 } else {
660 usb_remove_hcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700661 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530662 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
663 }
664}
665
Steve Mucklef132c6c2012-06-06 18:30:57 -0700666static int msm_otg_set_suspend(struct usb_phy *phy, int suspend)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530667{
Steve Mucklef132c6c2012-06-06 18:30:57 -0700668 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530669
Amit Blay6fa647a2012-05-24 14:12:08 +0300670 if (aca_enabled())
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530671 return 0;
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530672
Jack Pham69e621d2012-06-25 18:48:07 -0700673 if (atomic_read(&motg->in_lpm) == suspend)
674 return 0;
675
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530676 if (suspend) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700677 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530678 case OTG_STATE_A_WAIT_BCON:
679 if (TA_WAIT_BCON > 0)
680 break;
681 /* fall through */
682 case OTG_STATE_A_HOST:
683 pr_debug("host bus suspend\n");
684 clear_bit(A_BUS_REQ, &motg->inputs);
685 queue_work(system_nrt_wq, &motg->sm_work);
686 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300687 case OTG_STATE_B_PERIPHERAL:
688 pr_debug("peripheral bus suspend\n");
689 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
690 break;
691 set_bit(A_BUS_SUSPEND, &motg->inputs);
692 queue_work(system_nrt_wq, &motg->sm_work);
693 break;
694
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530695 default:
696 break;
697 }
698 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700699 switch (phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530700 case OTG_STATE_A_SUSPEND:
701 /* Remote wakeup or resume */
702 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700703 phy->state = OTG_STATE_A_HOST;
Jack Pham5ca279b2012-05-14 18:42:54 -0700704
705 /* ensure hardware is not in low power mode */
Steve Mucklef132c6c2012-06-06 18:30:57 -0700706 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530707 break;
Amit Blay6fa647a2012-05-24 14:12:08 +0300708 case OTG_STATE_B_PERIPHERAL:
709 pr_debug("peripheral bus resume\n");
710 if (!(motg->caps & ALLOW_LPM_ON_DEV_SUSPEND))
711 break;
712 clear_bit(A_BUS_SUSPEND, &motg->inputs);
713 queue_work(system_nrt_wq, &motg->sm_work);
714 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +0530715 default:
716 break;
717 }
718 }
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +0530719 return 0;
720}
721
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530722#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530723#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
724
725#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530726static int msm_otg_suspend(struct msm_otg *motg)
727{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200728 struct usb_phy *phy = &motg->phy;
729 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530730 struct msm_otg_platform_data *pdata = motg->pdata;
731 int cnt = 0;
Amit Blay6fa647a2012-05-24 14:12:08 +0300732 bool host_bus_suspend, device_bus_suspend, dcp;
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530733 u32 phy_ctrl_val = 0, cmd_val;
Stephen Boyd30ad10b2012-03-01 14:51:04 -0800734 unsigned ret;
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530735 u32 portsc;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530736
737 if (atomic_read(&motg->in_lpm))
738 return 0;
739
740 disable_irq(motg->irq);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700741 host_bus_suspend = phy->otg->host && !test_bit(ID, &motg->inputs);
742 device_bus_suspend = phy->otg->gadget && test_bit(ID, &motg->inputs) &&
Amit Blay6fa647a2012-05-24 14:12:08 +0300743 test_bit(A_BUS_SUSPEND, &motg->inputs) &&
744 motg->caps & ALLOW_LPM_ON_DEV_SUSPEND;
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530745 dcp = motg->chg_type == USB_DCP_CHARGER;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530746 /*
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530747 * Chipidea 45-nm PHY suspend sequence:
748 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530749 * Interrupt Latch Register auto-clear feature is not present
750 * in all PHY versions. Latch register is clear on read type.
751 * Clear latch register to avoid spurious wakeup from
752 * low power mode (LPM).
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530753 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530754 * PHY comparators are disabled when PHY enters into low power
755 * mode (LPM). Keep PHY comparators ON in LPM only when we expect
756 * VBUS/Id notifications from USB PHY. Otherwise turn off USB
757 * PHY comparators. This save significant amount of power.
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530758 *
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530759 * PLL is not turned off when PHY enters into low power mode (LPM).
760 * Disable PLL for maximum power savings.
761 */
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530762
763 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200764 ulpi_read(phy, 0x14);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530765 if (pdata->otg_control == OTG_PHY_CONTROL)
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200766 ulpi_write(phy, 0x01, 0x30);
767 ulpi_write(phy, 0x08, 0x09);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530768 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530769
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530771 /* Set the PHCD bit, only if it is not set by the controller.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530772 * PHY may take some time or even fail to enter into low power
773 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
774 * in failure case.
775 */
Rajkumar Raghupathy242565d2011-12-13 12:10:59 +0530776 portsc = readl_relaxed(USB_PORTSC);
777 if (!(portsc & PORTSC_PHCD)) {
778 writel_relaxed(portsc | PORTSC_PHCD,
779 USB_PORTSC);
780 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
781 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
782 break;
783 udelay(1);
784 cnt++;
785 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530786 }
787
788 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200789 dev_err(phy->dev, "Unable to suspend PHY\n");
790 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530791 enable_irq(motg->irq);
792 return -ETIMEDOUT;
793 }
794
795 /*
796 * PHY has capability to generate interrupt asynchronously in low
797 * power mode (LPM). This interrupt is level triggered. So USB IRQ
798 * line must be disabled till async interrupt enable bit is cleared
799 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
800 * block data communication from PHY.
Pavankumar Kondeti6be675f2012-04-16 13:29:24 +0530801 *
802 * PHY retention mode is disallowed while entering to LPM with wall
803 * charger connected. But PHY is put into suspend mode. Hence
804 * enable asynchronous interrupt to detect charger disconnection when
805 * PMIC notifications are unavailable.
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530806 */
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530807 cmd_val = readl_relaxed(USB_USBCMD);
Amit Blay6fa647a2012-05-24 14:12:08 +0300808 if (host_bus_suspend || device_bus_suspend ||
809 (motg->pdata->otg_control == OTG_PHY_CONTROL && dcp))
Pavankumar Kondeti4960f312011-12-06 15:46:14 +0530810 cmd_val |= ASYNC_INTR_CTRL | ULPI_STP_CTRL;
811 else
812 cmd_val |= ULPI_STP_CTRL;
813 writel_relaxed(cmd_val, USB_USBCMD);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530814
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530815 /*
816 * BC1.2 spec mandates PD to enable VDP_SRC when charging from DCP.
817 * PHY retention and collapse can not happen with VDP_SRC enabled.
818 */
Amit Blay6fa647a2012-05-24 14:12:08 +0300819 if (motg->caps & ALLOW_PHY_RETENTION && !host_bus_suspend &&
820 !device_bus_suspend && !dcp) {
Amit Blay58b31472011-11-18 09:39:39 +0200821 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
822 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
823 /* Enable PHY HV interrupts to wake MPM/Link */
824 phy_ctrl_val |=
825 (PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530826
Amit Blay58b31472011-11-18 09:39:39 +0200827 writel_relaxed(phy_ctrl_val & ~PHY_RETEN, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700828 motg->lpm_flags |= PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530829 }
830
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 /* Ensure that above operation is completed before turning off clocks */
832 mb();
Amit Blay9b6e58b2012-06-18 13:12:49 +0300833 if (!motg->pdata->core_clk_always_on_workaround) {
834 clk_disable_unprepare(motg->pclk);
835 clk_disable_unprepare(motg->core_clk);
836 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530837
Anji jonnala7da3f262011-12-02 17:22:14 -0800838 /* usb phy no more require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530839 if (!host_bus_suspend) {
840 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
841 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700842 dev_err(phy->dev, "%s failed to devote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530843 "TCXO D0 buffer%d\n", __func__, ret);
844 else
845 motg->lpm_flags |= XO_SHUTDOWN;
846 }
Anji jonnala7da3f262011-12-02 17:22:14 -0800847
Pavankumar Kondeti283146f2012-01-12 12:51:19 +0530848 if (motg->caps & ALLOW_PHY_POWER_COLLAPSE &&
849 !host_bus_suspend && !dcp) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 msm_hsusb_ldo_enable(motg, 0);
851 motg->lpm_flags |= PHY_PWR_COLLAPSED;
Anji jonnala0f73cac2011-05-04 10:19:46 +0530852 }
853
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530854 if (motg->lpm_flags & PHY_RETENTIONED) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 msm_hsusb_config_vddcx(0);
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530856 msm_hsusb_mhl_switch_enable(motg, 0);
857 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700858
Steve Mucklef132c6c2012-06-06 18:30:57 -0700859 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530860 enable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700861 if (motg->pdata->pmic_id_irq)
862 enable_irq_wake(motg->pdata->pmic_id_irq);
863 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530864 if (bus)
865 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
866
867 atomic_set(&motg->in_lpm, 1);
868 enable_irq(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869 wake_unlock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530870
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200871 dev_info(phy->dev, "USB in low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530872
873 return 0;
874}
875
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530876static int msm_otg_resume(struct msm_otg *motg)
877{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200878 struct usb_phy *phy = &motg->phy;
879 struct usb_bus *bus = phy->otg->host;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530880 int cnt = 0;
881 unsigned temp;
Amit Blay58b31472011-11-18 09:39:39 +0200882 u32 phy_ctrl_val = 0;
Anji jonnala7da3f262011-12-02 17:22:14 -0800883 unsigned ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530884
885 if (!atomic_read(&motg->in_lpm))
886 return 0;
887
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888 wake_lock(&motg->wlock);
Anji jonnala0f73cac2011-05-04 10:19:46 +0530889
Anji jonnala7da3f262011-12-02 17:22:14 -0800890 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530891 if (motg->lpm_flags & XO_SHUTDOWN) {
892 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
893 if (ret)
Steve Muckle75c34ca2012-06-12 14:27:40 -0700894 dev_err(phy->dev, "%s failed to vote for "
Vijayavardhan Vennapusabbdd6082012-06-06 14:14:25 +0530895 "TCXO D0 buffer%d\n", __func__, ret);
896 motg->lpm_flags &= ~XO_SHUTDOWN;
897 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530898
Amit Blay9b6e58b2012-06-18 13:12:49 +0300899 if (!motg->pdata->core_clk_always_on_workaround) {
900 clk_prepare_enable(motg->core_clk);
901 clk_prepare_enable(motg->pclk);
902 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530903
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 if (motg->lpm_flags & PHY_PWR_COLLAPSED) {
905 msm_hsusb_ldo_enable(motg, 1);
906 motg->lpm_flags &= ~PHY_PWR_COLLAPSED;
907 }
908
909 if (motg->lpm_flags & PHY_RETENTIONED) {
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +0530910 msm_hsusb_mhl_switch_enable(motg, 1);
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530911 msm_hsusb_config_vddcx(1);
Amit Blay58b31472011-11-18 09:39:39 +0200912 phy_ctrl_val = readl_relaxed(USB_PHY_CTRL);
913 phy_ctrl_val |= PHY_RETEN;
914 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
915 /* Disable PHY HV interrupts */
916 phy_ctrl_val &=
917 ~(PHY_IDHV_INTEN | PHY_OTGSESSVLDHV_INTEN);
918 writel_relaxed(phy_ctrl_val, USB_PHY_CTRL);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700919 motg->lpm_flags &= ~PHY_RETENTIONED;
Pavankumar Kondeti04aebcb2011-05-04 10:19:49 +0530920 }
921
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530922 temp = readl(USB_USBCMD);
923 temp &= ~ASYNC_INTR_CTRL;
924 temp &= ~ULPI_STP_CTRL;
925 writel(temp, USB_USBCMD);
926
927 /*
928 * PHY comes out of low power mode (LPM) in case of wakeup
929 * from asynchronous interrupt.
930 */
931 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
932 goto skip_phy_resume;
933
934 writel(readl(USB_PORTSC) & ~PORTSC_PHCD, USB_PORTSC);
935 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
936 if (!(readl(USB_PORTSC) & PORTSC_PHCD))
937 break;
938 udelay(1);
939 cnt++;
940 }
941
942 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
943 /*
944 * This is a fatal error. Reset the link and
945 * PHY. USB state can not be restored. Re-insertion
946 * of USB cable is the only way to get USB working.
947 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200948 dev_err(phy->dev, "Unable to resume USB."
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530949 "Re-plugin the cable\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200950 msm_otg_reset(phy);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530951 }
952
953skip_phy_resume:
Steve Mucklef132c6c2012-06-06 18:30:57 -0700954 if (device_may_wakeup(phy->dev)) {
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530955 disable_irq_wake(motg->irq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700956 if (motg->pdata->pmic_id_irq)
957 disable_irq_wake(motg->pdata->pmic_id_irq);
958 }
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530959 if (bus)
960 set_bit(HCD_FLAG_HW_ACCESSIBLE, &(bus_to_hcd(bus))->flags);
961
Pavankumar Kondeti2ce2c3a2011-05-02 11:56:33 +0530962 atomic_set(&motg->in_lpm, 0);
963
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530964 if (motg->async_int) {
965 motg->async_int = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530966 enable_irq(motg->irq);
967 }
968
Heikki Krogerus1d4c9292012-02-13 13:24:09 +0200969 dev_info(phy->dev, "USB exited from low power mode\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530970
971 return 0;
972}
Pavankumar Kondeti70187732011-02-15 09:42:34 +0530973#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +0530974
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -0700975static int msm_otg_notify_host_mode(struct msm_otg *motg, bool host_mode)
976{
977 if (!psy)
978 goto psy_not_supported;
979
980 if (host_mode)
981 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_SYSTEM);
982 else
983 power_supply_set_scope(psy, POWER_SUPPLY_SCOPE_DEVICE);
984
985psy_not_supported:
986 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
987 return -ENXIO;
988}
989
David Keitel081a3e22012-04-18 12:37:07 -0700990static int msm_otg_notify_chg_type(struct msm_otg *motg)
991{
992 static int charger_type;
993 /*
994 * TODO
995 * Unify OTG driver charger types and power supply charger types
996 */
997 if (charger_type == motg->chg_type)
998 return 0;
999
1000 if (motg->chg_type == USB_SDP_CHARGER)
1001 charger_type = POWER_SUPPLY_TYPE_USB;
1002 else if (motg->chg_type == USB_CDP_CHARGER)
1003 charger_type = POWER_SUPPLY_TYPE_USB_CDP;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301004 else if (motg->chg_type == USB_DCP_CHARGER ||
1005 motg->chg_type == USB_PROPRIETARY_CHARGER)
David Keitel081a3e22012-04-18 12:37:07 -07001006 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1007 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1008 motg->chg_type == USB_ACA_A_CHARGER ||
1009 motg->chg_type == USB_ACA_B_CHARGER ||
1010 motg->chg_type == USB_ACA_C_CHARGER))
1011 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1012 else
1013 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1014
1015 return pm8921_set_usb_power_supply_type(charger_type);
1016}
1017
Amit Blay0f7edf72012-01-15 10:11:27 +02001018static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1019{
Amit Blay0f7edf72012-01-15 10:11:27 +02001020
Amit Blay0f7edf72012-01-15 10:11:27 +02001021 if (!psy)
1022 goto psy_not_supported;
1023
1024 if (motg->cur_power == 0 && mA > 0) {
1025 /* Enable charging */
1026 if (power_supply_set_online(psy, true))
1027 goto psy_not_supported;
1028 } else if (motg->cur_power > 0 && mA == 0) {
1029 /* Disable charging */
1030 if (power_supply_set_online(psy, false))
1031 goto psy_not_supported;
1032 return 0;
1033 }
1034 /* Set max current limit */
1035 if (power_supply_set_current_limit(psy, 1000*mA))
1036 goto psy_not_supported;
1037
1038 return 0;
1039
1040psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001041 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001042 return -ENXIO;
1043}
1044
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301045static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1046{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001047 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301048
1049 if (g && g->is_a_peripheral)
1050 return;
1051
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301052 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1053 motg->chg_type == USB_ACA_A_CHARGER ||
1054 motg->chg_type == USB_ACA_B_CHARGER ||
1055 motg->chg_type == USB_ACA_C_CHARGER) &&
1056 mA > IDEV_ACA_CHG_LIMIT)
1057 mA = IDEV_ACA_CHG_LIMIT;
1058
David Keitel081a3e22012-04-18 12:37:07 -07001059 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001060 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001061 "Failed notifying %d charger type to PMIC\n",
1062 motg->chg_type);
1063
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301064 if (motg->cur_power == mA)
1065 return;
1066
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001067 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001068
1069 /*
1070 * Use Power Supply API if supported, otherwise fallback
1071 * to legacy pm8921 API.
1072 */
1073 if (msm_otg_notify_power_supply(motg, mA))
1074 pm8921_charger_vbus_draw(mA);
1075
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301076 motg->cur_power = mA;
1077}
1078
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001079static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301080{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001081 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301082
1083 /*
1084 * Gadget driver uses set_power method to notify about the
1085 * available current based on suspend/configured states.
1086 *
1087 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1088 * states when CDP/ACA is connected.
1089 */
1090 if (motg->chg_type == USB_SDP_CHARGER)
1091 msm_otg_notify_charger(motg, mA);
1092
1093 return 0;
1094}
1095
Steve Mucklef132c6c2012-06-06 18:30:57 -07001096static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301097{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001098 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301099 struct msm_otg_platform_data *pdata = motg->pdata;
1100 struct usb_hcd *hcd;
1101
1102 if (!otg->host)
1103 return;
1104
1105 hcd = bus_to_hcd(otg->host);
1106
1107 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001108 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301109
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301110 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001111 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301112 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1113
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301114 /*
1115 * Some boards have a switch cotrolled by gpio
1116 * to enable/disable internal HUB. Enable internal
1117 * HUB before kicking the host.
1118 */
1119 if (pdata->setup_gpio)
1120 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301122 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001123 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301124
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301125 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301126 /* HCD core reset all bits of PORTSC. select ULPI phy */
1127 writel_relaxed(0x80000000, USB_PORTSC);
1128
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301129 if (pdata->setup_gpio)
1130 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301131
1132 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001133 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301134 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301135 }
1136}
1137
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001138static int msm_otg_usbdev_notify(struct notifier_block *self,
1139 unsigned long action, void *priv)
1140{
1141 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001142 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301143 struct usb_device *udev = priv;
1144
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301145 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1146 goto out;
1147
Steve Mucklef132c6c2012-06-06 18:30:57 -07001148 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301149 goto out;
1150 /*
1151 * Interested in devices connected directly to the root hub.
1152 * ACA dock can supply IDEV_CHG irrespective devices connected
1153 * on the accessory port.
1154 */
1155 if (!udev->parent || udev->parent->parent ||
1156 motg->chg_type == USB_ACA_DOCK_CHARGER)
1157 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001158
1159 switch (action) {
1160 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301161 if (aca_enabled())
1162 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001163 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301164 pr_debug("B_CONN set\n");
1165 set_bit(B_CONN, &motg->inputs);
1166 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001167 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301168 /*
1169 * OTG PET: A-device must end session within
1170 * 10 sec after PET enumeration.
1171 */
1172 if (udev->quirks & USB_QUIRK_OTG_PET)
1173 msm_otg_start_timer(motg, TA_TST_MAINT,
1174 A_TST_MAINT);
1175 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301176 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178 if (udev->actconfig)
1179 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1180 else
1181 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001182 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301183 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301184 break;
1185 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001186 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1187 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301188 pr_debug("B_CONN clear\n");
1189 clear_bit(B_CONN, &motg->inputs);
1190 /*
1191 * OTG PET: A-device must end session after
1192 * PET disconnection if it is enumerated
1193 * with bcdDevice[0] = 1. USB core sets
1194 * bus->otg_vbus_off for us. clear it here.
1195 */
1196 if (udev->bus->otg_vbus_off) {
1197 udev->bus->otg_vbus_off = 0;
1198 set_bit(A_BUS_DROP, &motg->inputs);
1199 }
1200 queue_work(system_nrt_wq, &motg->sm_work);
1201 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202 default:
1203 break;
1204 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301205 if (test_bit(ID_A, &motg->inputs))
1206 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1207 motg->mA_port);
1208out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209 return NOTIFY_OK;
1210}
1211
Mayank Ranae3926882011-12-26 09:47:54 +05301212static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1213{
1214 int ret;
1215 static bool vbus_is_on;
1216
1217 if (vbus_is_on == on)
1218 return;
1219
1220 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301221 ret = motg->pdata->vbus_power(on);
1222 if (!ret)
1223 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301224 return;
1225 }
1226
1227 if (!vbus_otg) {
1228 pr_err("vbus_otg is NULL.");
1229 return;
1230 }
1231
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001232 /*
1233 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001234 * from usb before turning on the boost.
1235 * if exiting host mode disable the boost before enabling to draw
1236 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001237 */
Mayank Ranae3926882011-12-26 09:47:54 +05301238 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001239 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301240 ret = regulator_enable(vbus_otg);
1241 if (ret) {
1242 pr_err("unable to enable vbus_otg\n");
1243 return;
1244 }
1245 vbus_is_on = true;
1246 } else {
1247 ret = regulator_disable(vbus_otg);
1248 if (ret) {
1249 pr_err("unable to disable vbus_otg\n");
1250 return;
1251 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001252 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301253 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301254 }
1255}
1256
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001257static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301258{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001259 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301260 struct usb_hcd *hcd;
1261
1262 /*
1263 * Fail host registration if this board can support
1264 * only peripheral configuration.
1265 */
1266 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001267 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301268 return -ENODEV;
1269 }
1270
Mayank Ranae3926882011-12-26 09:47:54 +05301271 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001272 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301273 if (IS_ERR(vbus_otg)) {
1274 pr_err("Unable to get vbus_otg\n");
1275 return -ENODEV;
1276 }
1277 }
1278
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301279 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001280 if (otg->phy->state == OTG_STATE_A_HOST) {
1281 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001282 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301283 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301284 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301285 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001286 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301287 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301288 } else {
1289 otg->host = NULL;
1290 }
1291
1292 return 0;
1293 }
1294
1295 hcd = bus_to_hcd(host);
1296 hcd->power_budget = motg->pdata->power_budget;
1297
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301298#ifdef CONFIG_USB_OTG
1299 host->otg_port = 1;
1300#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001301 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1302 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301303 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001304 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301305
1306 /*
1307 * Kick the state machine work, if peripheral is not supported
1308 * or peripheral is already registered with us.
1309 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301310 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001311 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301312 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301313 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301314
1315 return 0;
1316}
1317
Steve Mucklef132c6c2012-06-06 18:30:57 -07001318static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301319{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301320 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001321 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301322 struct msm_otg_platform_data *pdata = motg->pdata;
1323
1324 if (!otg->gadget)
1325 return;
1326
1327 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001328 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301329 /*
1330 * Some boards have a switch cotrolled by gpio
1331 * to enable/disable internal HUB. Disable internal
1332 * HUB before kicking the gadget.
1333 */
1334 if (pdata->setup_gpio)
1335 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001336
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301337 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301338 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301339 ret = msm_bus_scale_client_update_request(
1340 motg->bus_perf_client, 1);
1341 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001342 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301343 "bus bandwidth %d\n", __func__, ret);
1344 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301345 usb_gadget_vbus_connect(otg->gadget);
1346 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001347 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301348 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301349 /* Configure BUS performance parameters to default */
1350 if (motg->bus_perf_client) {
1351 ret = msm_bus_scale_client_update_request(
1352 motg->bus_perf_client, 0);
1353 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001354 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301355 "for bus bw %d\n", __func__, ret);
1356 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301357 if (pdata->setup_gpio)
1358 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1359 }
1360
1361}
1362
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001363static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301364 struct usb_gadget *gadget)
1365{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001366 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301367
1368 /*
1369 * Fail peripheral registration if this board can support
1370 * only host configuration.
1371 */
1372 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001373 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301374 return -ENODEV;
1375 }
1376
1377 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001378 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1379 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301380 msm_otg_start_peripheral(otg, 0);
1381 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001382 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301383 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301384 } else {
1385 otg->gadget = NULL;
1386 }
1387
1388 return 0;
1389 }
1390 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001391 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301392
1393 /*
1394 * Kick the state machine work, if host is not supported
1395 * or host is already registered with us.
1396 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301397 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001398 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301399 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301400 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301401
1402 return 0;
1403}
1404
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001405static bool msm_chg_aca_detect(struct msm_otg *motg)
1406{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001407 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001408 u32 int_sts;
1409 bool ret = false;
1410
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301411 if (!aca_enabled())
1412 goto out;
1413
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001414 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1415 goto out;
1416
Steve Mucklef132c6c2012-06-06 18:30:57 -07001417 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001418 switch (int_sts & 0x1C) {
1419 case 0x08:
1420 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001421 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001422 motg->chg_type = USB_ACA_A_CHARGER;
1423 motg->chg_state = USB_CHG_STATE_DETECTED;
1424 clear_bit(ID_B, &motg->inputs);
1425 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301426 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001427 ret = true;
1428 }
1429 break;
1430 case 0x0C:
1431 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001432 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 motg->chg_type = USB_ACA_B_CHARGER;
1434 motg->chg_state = USB_CHG_STATE_DETECTED;
1435 clear_bit(ID_A, &motg->inputs);
1436 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301437 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001438 ret = true;
1439 }
1440 break;
1441 case 0x10:
1442 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001443 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444 motg->chg_type = USB_ACA_C_CHARGER;
1445 motg->chg_state = USB_CHG_STATE_DETECTED;
1446 clear_bit(ID_A, &motg->inputs);
1447 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301448 set_bit(ID, &motg->inputs);
1449 ret = true;
1450 }
1451 break;
1452 case 0x04:
1453 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001454 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301455 motg->chg_type = USB_INVALID_CHARGER;
1456 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1457 clear_bit(ID_A, &motg->inputs);
1458 clear_bit(ID_B, &motg->inputs);
1459 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001460 ret = true;
1461 }
1462 break;
1463 default:
1464 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1465 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301466 test_and_clear_bit(ID_C, &motg->inputs) |
1467 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001468 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001469 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001470 motg->chg_type = USB_INVALID_CHARGER;
1471 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1472 }
1473 }
1474out:
1475 return ret;
1476}
1477
1478static void msm_chg_enable_aca_det(struct msm_otg *motg)
1479{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001480 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301482 if (!aca_enabled())
1483 return;
1484
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001485 switch (motg->pdata->phy_type) {
1486 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301487 /* Disable ID_GND in link and PHY */
1488 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1489 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001490 ulpi_write(phy, 0x01, 0x0C);
1491 ulpi_write(phy, 0x10, 0x0F);
1492 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301493 /* Disable PMIC ID pull-up */
1494 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301495 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001496 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301497 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001498 break;
1499 default:
1500 break;
1501 }
1502}
1503
1504static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1505{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001506 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001507
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301508 if (!aca_enabled())
1509 return;
1510
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001511 switch (motg->pdata->phy_type) {
1512 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301513 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001514 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301515 break;
1516 default:
1517 break;
1518 }
1519}
1520
1521static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1522{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001523 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301524
1525 if (!aca_enabled())
1526 return;
1527
1528 switch (motg->pdata->phy_type) {
1529 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001530 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001531 break;
1532 default:
1533 break;
1534 }
1535}
1536
1537static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1538{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001539 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001540 bool ret = false;
1541
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301542 if (!aca_enabled())
1543 return ret;
1544
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001545 switch (motg->pdata->phy_type) {
1546 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001547 if (ulpi_read(phy, 0x91) & 1) {
1548 dev_dbg(phy->dev, "RID change\n");
1549 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001550 ret = msm_chg_aca_detect(motg);
1551 }
1552 default:
1553 break;
1554 }
1555 return ret;
1556}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301557
1558static void msm_otg_id_timer_func(unsigned long data)
1559{
1560 struct msm_otg *motg = (struct msm_otg *) data;
1561
1562 if (!aca_enabled())
1563 return;
1564
1565 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001566 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301567 return;
1568 }
1569
Steve Mucklef132c6c2012-06-06 18:30:57 -07001570 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301571 goto out;
1572
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301573 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001574 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301575 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301576 }
1577
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301578out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301579 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1580 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1581}
1582
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301583static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1584{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001585 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301586 u32 chg_det;
1587 bool ret = false;
1588
1589 switch (motg->pdata->phy_type) {
1590 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001591 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301592 ret = chg_det & (1 << 4);
1593 break;
1594 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001595 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301596 ret = chg_det & 1;
1597 break;
1598 default:
1599 break;
1600 }
1601 return ret;
1602}
1603
1604static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1605{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001606 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301607 u32 chg_det;
1608
1609 switch (motg->pdata->phy_type) {
1610 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001611 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301612 /* Turn off charger block */
1613 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001614 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301615 udelay(20);
1616 /* control chg block via ULPI */
1617 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001618 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301619 /* put it in host mode for enabling D- source */
1620 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001621 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301622 /* Turn on chg detect block */
1623 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001624 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301625 udelay(20);
1626 /* enable chg detection */
1627 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001628 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301629 break;
1630 case SNPS_28NM_INTEGRATED_PHY:
1631 /*
1632 * Configure DM as current source, DP as current sink
1633 * and enable battery charging comparators.
1634 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001635 ulpi_write(phy, 0x8, 0x85);
1636 ulpi_write(phy, 0x2, 0x85);
1637 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301638 break;
1639 default:
1640 break;
1641 }
1642}
1643
1644static bool msm_chg_check_primary_det(struct msm_otg *motg)
1645{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001646 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301647 u32 chg_det;
1648 bool ret = false;
1649
1650 switch (motg->pdata->phy_type) {
1651 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001652 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301653 ret = chg_det & (1 << 4);
1654 break;
1655 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001656 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301657 ret = chg_det & 1;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301658 /* Turn off VDP_SRC */
1659 ulpi_write(phy, 0x3, 0x86);
1660 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301661 break;
1662 default:
1663 break;
1664 }
1665 return ret;
1666}
1667
1668static void msm_chg_enable_primary_det(struct msm_otg *motg)
1669{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001670 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301671 u32 chg_det;
1672
1673 switch (motg->pdata->phy_type) {
1674 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001675 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301676 /* enable chg detection */
1677 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001678 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301679 break;
1680 case SNPS_28NM_INTEGRATED_PHY:
1681 /*
1682 * Configure DP as current source, DM as current sink
1683 * and enable battery charging comparators.
1684 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001685 ulpi_write(phy, 0x2, 0x85);
1686 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301687 break;
1688 default:
1689 break;
1690 }
1691}
1692
1693static bool msm_chg_check_dcd(struct msm_otg *motg)
1694{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001695 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301696 u32 line_state;
1697 bool ret = false;
1698
1699 switch (motg->pdata->phy_type) {
1700 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001701 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301702 ret = !(line_state & 1);
1703 break;
1704 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001705 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301706 ret = line_state & 2;
1707 break;
1708 default:
1709 break;
1710 }
1711 return ret;
1712}
1713
1714static void msm_chg_disable_dcd(struct msm_otg *motg)
1715{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001716 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301717 u32 chg_det;
1718
1719 switch (motg->pdata->phy_type) {
1720 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001721 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301722 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001723 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301724 break;
1725 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001726 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301727 break;
1728 default:
1729 break;
1730 }
1731}
1732
1733static void msm_chg_enable_dcd(struct msm_otg *motg)
1734{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001735 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301736 u32 chg_det;
1737
1738 switch (motg->pdata->phy_type) {
1739 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001740 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301741 /* Turn on D+ current source */
1742 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001743 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301744 break;
1745 case SNPS_28NM_INTEGRATED_PHY:
1746 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001747 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301748 break;
1749 default:
1750 break;
1751 }
1752}
1753
1754static void msm_chg_block_on(struct msm_otg *motg)
1755{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001756 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301757 u32 func_ctrl, chg_det;
1758
1759 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001760 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301761 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1762 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001763 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301764
1765 switch (motg->pdata->phy_type) {
1766 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001767 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301768 /* control chg block via ULPI */
1769 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001770 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301771 /* Turn on chg detect block */
1772 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001773 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301774 udelay(20);
1775 break;
1776 case SNPS_28NM_INTEGRATED_PHY:
1777 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001778 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301779 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001780 ulpi_write(phy, 0x1F, 0x92);
1781 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301782 udelay(100);
1783 break;
1784 default:
1785 break;
1786 }
1787}
1788
1789static void msm_chg_block_off(struct msm_otg *motg)
1790{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001791 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301792 u32 func_ctrl, chg_det;
1793
1794 switch (motg->pdata->phy_type) {
1795 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001796 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301797 /* Turn off charger block */
1798 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001799 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301800 break;
1801 case SNPS_28NM_INTEGRATED_PHY:
1802 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001803 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301804 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001805 ulpi_write(phy, 0x1F, 0x92);
1806 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301807 break;
1808 default:
1809 break;
1810 }
1811
1812 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001813 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301814 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1815 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001816 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301817}
1818
Anji jonnalad270e2d2011-08-09 11:28:32 +05301819static const char *chg_to_string(enum usb_chg_type chg_type)
1820{
1821 switch (chg_type) {
1822 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1823 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1824 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1825 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1826 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1827 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1828 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301829 case USB_PROPRIETARY_CHARGER: return "USB_PROPRIETARY_CHARGER";
Anji jonnalad270e2d2011-08-09 11:28:32 +05301830 default: return "INVALID_CHARGER";
1831 }
1832}
1833
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301834#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1835#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301836#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1837#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301838static void msm_chg_detect_work(struct work_struct *w)
1839{
1840 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001841 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301842 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301843 u32 line_state, dm_vlgc;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 unsigned long delay;
1845
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001846 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301847 switch (motg->chg_state) {
1848 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301849 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301850 if (motg->pdata->enable_dcd)
1851 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001852 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301853 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1854 motg->dcd_retries = 0;
1855 delay = MSM_CHG_DCD_POLL_TIME;
1856 break;
1857 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001858 is_aca = msm_chg_aca_detect(motg);
1859 if (is_aca) {
1860 /*
1861 * ID_A can be ACA dock too. continue
1862 * primary detection after DCD.
1863 */
1864 if (test_bit(ID_A, &motg->inputs)) {
1865 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1866 } else {
1867 delay = 0;
1868 break;
1869 }
1870 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301871 if (motg->pdata->enable_dcd)
1872 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301873 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1874 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301875 if (motg->pdata->enable_dcd)
1876 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301877 msm_chg_enable_primary_det(motg);
1878 delay = MSM_CHG_PRIMARY_DET_TIME;
1879 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1880 } else {
1881 delay = MSM_CHG_DCD_POLL_TIME;
1882 }
1883 break;
1884 case USB_CHG_STATE_DCD_DONE:
1885 vout = msm_chg_check_primary_det(motg);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301886 line_state = readl_relaxed(USB_PORTSC) & PORTSC_LS;
1887 dm_vlgc = line_state & PORTSC_LS_DM;
1888 if (vout && !dm_vlgc) { /* VDAT_REF < DM < VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301889 if (test_bit(ID_A, &motg->inputs)) {
1890 motg->chg_type = USB_ACA_DOCK_CHARGER;
1891 motg->chg_state = USB_CHG_STATE_DETECTED;
1892 delay = 0;
1893 break;
1894 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301895 if (line_state) { /* DP > VLGC */
1896 motg->chg_type = USB_PROPRIETARY_CHARGER;
1897 motg->chg_state = USB_CHG_STATE_DETECTED;
1898 delay = 0;
1899 } else {
1900 msm_chg_enable_secondary_det(motg);
1901 delay = MSM_CHG_SECONDARY_DET_TIME;
1902 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1903 }
1904 } else { /* DM < VDAT_REF || DM > VLGC */
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301905 if (test_bit(ID_A, &motg->inputs)) {
1906 motg->chg_type = USB_ACA_A_CHARGER;
1907 motg->chg_state = USB_CHG_STATE_DETECTED;
1908 delay = 0;
1909 break;
1910 }
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05301911
1912 if (line_state) /* DP > VLGC or/and DM > VLGC */
1913 motg->chg_type = USB_PROPRIETARY_CHARGER;
1914 else
1915 motg->chg_type = USB_SDP_CHARGER;
1916
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301917 motg->chg_state = USB_CHG_STATE_DETECTED;
1918 delay = 0;
1919 }
1920 break;
1921 case USB_CHG_STATE_PRIMARY_DONE:
1922 vout = msm_chg_check_secondary_det(motg);
1923 if (vout)
1924 motg->chg_type = USB_DCP_CHARGER;
1925 else
1926 motg->chg_type = USB_CDP_CHARGER;
1927 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1928 /* fall through */
1929 case USB_CHG_STATE_SECONDARY_DONE:
1930 motg->chg_state = USB_CHG_STATE_DETECTED;
1931 case USB_CHG_STATE_DETECTED:
1932 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001933 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301934 /*
1935 * Spurious interrupt is seen after enabling ACA detection
1936 * due to which charger detection fails in case of PET.
1937 * Add delay of 100 microsec to avoid that.
1938 */
1939 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001940 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001941 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05301942 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301943 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301944 return;
1945 default:
1946 return;
1947 }
1948
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301949 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301950}
1951
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301952/*
1953 * We support OTG, Peripheral only and Host only configurations. In case
1954 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1955 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1956 * enabled when switch is controlled by user and default mode is supplied
1957 * by board file, which can be changed by userspace later.
1958 */
1959static void msm_otg_init_sm(struct msm_otg *motg)
1960{
1961 struct msm_otg_platform_data *pdata = motg->pdata;
1962 u32 otgsc = readl(USB_OTGSC);
1963
1964 switch (pdata->mode) {
1965 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301967 if (pdata->default_mode == USB_HOST) {
1968 clear_bit(ID, &motg->inputs);
1969 } else if (pdata->default_mode == USB_PERIPHERAL) {
1970 set_bit(ID, &motg->inputs);
1971 set_bit(B_SESS_VLD, &motg->inputs);
1972 } else {
1973 set_bit(ID, &motg->inputs);
1974 clear_bit(B_SESS_VLD, &motg->inputs);
1975 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301976 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301977 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301978 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301979 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301980 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301981 set_bit(A_BUS_REQ, &motg->inputs);
1982 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001983 if (otgsc & OTGSC_BSV)
1984 set_bit(B_SESS_VLD, &motg->inputs);
1985 else
1986 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301987 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301988 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07001989 unsigned long flags;
1990 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301991 if (irq_read_line(pdata->pmic_id_irq))
1992 set_bit(ID, &motg->inputs);
1993 else
1994 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07001995 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301996 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301997 /*
1998 * VBUS initial state is reported after PMIC
1999 * driver initialization. Wait for it.
2000 */
2001 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302002 }
2003 break;
2004 case USB_HOST:
2005 clear_bit(ID, &motg->inputs);
2006 break;
2007 case USB_PERIPHERAL:
2008 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05302009 if (pdata->otg_control == OTG_PHY_CONTROL) {
2010 if (otgsc & OTGSC_BSV)
2011 set_bit(B_SESS_VLD, &motg->inputs);
2012 else
2013 clear_bit(B_SESS_VLD, &motg->inputs);
2014 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
2015 /*
2016 * VBUS initial state is reported after PMIC
2017 * driver initialization. Wait for it.
2018 */
2019 wait_for_completion(&pmic_vbus_init);
2020 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302021 break;
2022 default:
2023 break;
2024 }
2025}
2026
2027static void msm_otg_sm_work(struct work_struct *w)
2028{
2029 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002030 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302031 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302032
Steve Mucklef132c6c2012-06-06 18:30:57 -07002033 pm_runtime_resume(otg->phy->dev);
2034 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002035 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302036 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002037 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302038 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302039 psy = power_supply_get_by_name("usb");
2040 if (!psy)
2041 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002042 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302043 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2044 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002045 pm_runtime_put_noidle(otg->phy->dev);
2046 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302047 break;
2048 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302049 /* FALL THROUGH */
2050 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002051 if ((!test_bit(ID, &motg->inputs) ||
2052 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302053 pr_debug("!id || id_A\n");
2054 clear_bit(B_BUS_REQ, &motg->inputs);
2055 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002056 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302057 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302058 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302059 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302060 switch (motg->chg_state) {
2061 case USB_CHG_STATE_UNDEFINED:
2062 msm_chg_detect_work(&motg->chg_work.work);
2063 break;
2064 case USB_CHG_STATE_DETECTED:
2065 switch (motg->chg_type) {
2066 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302067 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002068 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondeti9ec21d32012-05-07 15:50:23 +05302069 /* fall through */
2070 case USB_PROPRIETARY_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302071 msm_otg_notify_charger(motg,
2072 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002073 pm_runtime_put_noidle(otg->phy->dev);
2074 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302075 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302076 case USB_ACA_B_CHARGER:
2077 msm_otg_notify_charger(motg,
2078 IDEV_ACA_CHG_MAX);
2079 /*
2080 * (ID_B --> ID_C) PHY_ALT interrupt can
2081 * not be detected in LPM.
2082 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302083 break;
2084 case USB_CDP_CHARGER:
2085 msm_otg_notify_charger(motg,
2086 IDEV_CHG_MAX);
2087 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002088 otg->phy->state =
2089 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302090 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302091 case USB_ACA_C_CHARGER:
2092 msm_otg_notify_charger(motg,
2093 IDEV_ACA_CHG_MAX);
2094 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002095 otg->phy->state =
2096 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302097 break;
2098 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302099 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002100 otg->phy->state =
2101 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 break;
2103 default:
2104 break;
2105 }
2106 break;
2107 default:
2108 break;
2109 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302110 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2111 pr_debug("b_sess_end && b_bus_req\n");
2112 if (msm_otg_start_srp(otg) < 0) {
2113 clear_bit(B_BUS_REQ, &motg->inputs);
2114 work = 1;
2115 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302116 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002117 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302118 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2119 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302120 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302121 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302122 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302123 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2124 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302125 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002126 msm_otg_reset(otg->phy);
2127 pm_runtime_put_noidle(otg->phy->dev);
2128 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302129 }
2130 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302131 case OTG_STATE_B_SRP_INIT:
2132 if (!test_bit(ID, &motg->inputs) ||
2133 test_bit(ID_A, &motg->inputs) ||
2134 test_bit(ID_C, &motg->inputs) ||
2135 (test_bit(B_SESS_VLD, &motg->inputs) &&
2136 !test_bit(ID_B, &motg->inputs))) {
2137 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2138 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002139 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302140 /*
2141 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2142 * bits after SRP initiation.
2143 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002144 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302145 work = 1;
2146 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2147 pr_debug("b_srp_fail\n");
2148 pr_info("A-device did not respond to SRP\n");
2149 clear_bit(B_BUS_REQ, &motg->inputs);
2150 clear_bit(B_SRP_FAIL, &motg->tmouts);
2151 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002152 ulpi_write(otg->phy, 0x0, 0x98);
2153 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302154 motg->b_last_se0_sess = jiffies;
2155 work = 1;
2156 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302157 break;
2158 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302159 if (!test_bit(ID, &motg->inputs) ||
2160 test_bit(ID_A, &motg->inputs) ||
2161 test_bit(ID_B, &motg->inputs) ||
2162 !test_bit(B_SESS_VLD, &motg->inputs)) {
2163 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302164 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2165 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302166 msm_otg_notify_charger(motg, 0);
2167 srp_reqd = otg->gadget->otg_srp_reqd;
2168 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302169 if (test_bit(ID_B, &motg->inputs))
2170 clear_bit(ID_B, &motg->inputs);
2171 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002172 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302173 motg->b_last_se0_sess = jiffies;
2174 if (srp_reqd)
2175 msm_otg_start_timer(motg,
2176 TB_TST_SRP, B_TST_SRP);
2177 else
2178 work = 1;
2179 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2180 otg->gadget->b_hnp_enable &&
2181 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2182 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2183 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2184 /* D+ pullup should not be disconnected within 4msec
2185 * after A device suspends the bus. Otherwise PET will
2186 * fail the compliance test.
2187 */
2188 udelay(1000);
2189 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002190 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302191 /*
2192 * start HCD even before A-device enable
2193 * pull-up to meet HNP timings.
2194 */
2195 otg->host->is_b_host = 1;
2196 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002197 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2198 test_bit(B_SESS_VLD, &motg->inputs)) {
2199 pr_debug("a_bus_suspend && b_sess_vld\n");
2200 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002201 pm_runtime_put_noidle(otg->phy->dev);
2202 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002203 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302205 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002206 }
2207 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302208 case OTG_STATE_B_WAIT_ACON:
2209 if (!test_bit(ID, &motg->inputs) ||
2210 test_bit(ID_A, &motg->inputs) ||
2211 test_bit(ID_B, &motg->inputs) ||
2212 !test_bit(B_SESS_VLD, &motg->inputs)) {
2213 pr_debug("!id || id_a/b || !b_sess_vld\n");
2214 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302215 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302216 * A-device is physically disconnected during
2217 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302218 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302219 msm_otg_start_host(otg, 0);
2220 otg->host->is_b_host = 0;
2221
2222 clear_bit(B_BUS_REQ, &motg->inputs);
2223 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2224 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002225 otg->phy->state = OTG_STATE_B_IDLE;
2226 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302227 work = 1;
2228 } else if (test_bit(A_CONN, &motg->inputs)) {
2229 pr_debug("a_conn\n");
2230 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002231 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302232 /*
2233 * PET disconnects D+ pullup after reset is generated
2234 * by B device in B_HOST role which is not detected by
2235 * B device. As workaorund , start timer of 300msec
2236 * and stop timer if A device is enumerated else clear
2237 * A_CONN.
2238 */
2239 msm_otg_start_timer(motg, TB_TST_CONFIG,
2240 B_TST_CONFIG);
2241 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2242 pr_debug("b_ase0_brst_tmout\n");
2243 pr_info("B HNP fail:No response from A device\n");
2244 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002245 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302246 otg->host->is_b_host = 0;
2247 clear_bit(B_ASE0_BRST, &motg->tmouts);
2248 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2249 clear_bit(B_BUS_REQ, &motg->inputs);
2250 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002251 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302252 work = 1;
2253 } else if (test_bit(ID_C, &motg->inputs)) {
2254 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2255 }
2256 break;
2257 case OTG_STATE_B_HOST:
2258 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2259 !test_bit(A_CONN, &motg->inputs) ||
2260 !test_bit(B_SESS_VLD, &motg->inputs)) {
2261 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2262 clear_bit(A_CONN, &motg->inputs);
2263 clear_bit(B_BUS_REQ, &motg->inputs);
2264 msm_otg_start_host(otg, 0);
2265 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002266 otg->phy->state = OTG_STATE_B_IDLE;
2267 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302268 work = 1;
2269 } else if (test_bit(ID_C, &motg->inputs)) {
2270 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2271 }
2272 break;
2273 case OTG_STATE_A_IDLE:
2274 otg->default_a = 1;
2275 if (test_bit(ID, &motg->inputs) &&
2276 !test_bit(ID_A, &motg->inputs)) {
2277 pr_debug("id && !id_a\n");
2278 otg->default_a = 0;
2279 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002280 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302281 del_timer_sync(&motg->id_timer);
2282 msm_otg_link_reset(motg);
2283 msm_chg_enable_aca_intr(motg);
2284 msm_otg_notify_charger(motg, 0);
2285 work = 1;
2286 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2287 (test_bit(A_SRP_DET, &motg->inputs) ||
2288 test_bit(A_BUS_REQ, &motg->inputs))) {
2289 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2290
2291 clear_bit(A_SRP_DET, &motg->inputs);
2292 /* Disable SRP detection */
2293 writel_relaxed((readl_relaxed(USB_OTGSC) &
2294 ~OTGSC_INTSTS_MASK) &
2295 ~OTGSC_DPIE, USB_OTGSC);
2296
Steve Mucklef132c6c2012-06-06 18:30:57 -07002297 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302298 /* VBUS should not be supplied before end of SRP pulse
2299 * generated by PET, if not complaince test fail.
2300 */
2301 usleep_range(10000, 12000);
2302 /* ACA: ID_A: Stop charging untill enumeration */
2303 if (test_bit(ID_A, &motg->inputs))
2304 msm_otg_notify_charger(motg, 0);
2305 else
2306 msm_hsusb_vbus_power(motg, 1);
2307 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2308 } else {
2309 pr_debug("No session requested\n");
2310 clear_bit(A_BUS_DROP, &motg->inputs);
2311 if (test_bit(ID_A, &motg->inputs)) {
2312 msm_otg_notify_charger(motg,
2313 IDEV_ACA_CHG_MAX);
2314 } else if (!test_bit(ID, &motg->inputs)) {
2315 msm_otg_notify_charger(motg, 0);
2316 /*
2317 * A-device is not providing power on VBUS.
2318 * Enable SRP detection.
2319 */
2320 writel_relaxed(0x13, USB_USBMODE);
2321 writel_relaxed((readl_relaxed(USB_OTGSC) &
2322 ~OTGSC_INTSTS_MASK) |
2323 OTGSC_DPIE, USB_OTGSC);
2324 mb();
2325 }
2326 }
2327 break;
2328 case OTG_STATE_A_WAIT_VRISE:
2329 if ((test_bit(ID, &motg->inputs) &&
2330 !test_bit(ID_A, &motg->inputs)) ||
2331 test_bit(A_BUS_DROP, &motg->inputs) ||
2332 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2333 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2334 clear_bit(A_BUS_REQ, &motg->inputs);
2335 msm_otg_del_timer(motg);
2336 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002337 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302338 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2339 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2340 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002341 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302342 if (TA_WAIT_BCON > 0)
2343 msm_otg_start_timer(motg, TA_WAIT_BCON,
2344 A_WAIT_BCON);
2345 msm_otg_start_host(otg, 1);
2346 msm_chg_enable_aca_det(motg);
2347 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302348 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302349 if (msm_chg_check_aca_intr(motg))
2350 work = 1;
2351 }
2352 break;
2353 case OTG_STATE_A_WAIT_BCON:
2354 if ((test_bit(ID, &motg->inputs) &&
2355 !test_bit(ID_A, &motg->inputs)) ||
2356 test_bit(A_BUS_DROP, &motg->inputs) ||
2357 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2358 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2359 "a_wait_bcon_tmout\n");
2360 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2361 pr_info("Device No Response\n");
2362 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2363 }
2364 msm_otg_del_timer(motg);
2365 clear_bit(A_BUS_REQ, &motg->inputs);
2366 clear_bit(B_CONN, &motg->inputs);
2367 msm_otg_start_host(otg, 0);
2368 /*
2369 * ACA: ID_A with NO accessory, just the A plug is
2370 * attached to ACA: Use IDCHG_MAX for charging
2371 */
2372 if (test_bit(ID_A, &motg->inputs))
2373 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2374 else
2375 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002376 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302377 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2378 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2379 pr_debug("!a_vbus_vld\n");
2380 clear_bit(B_CONN, &motg->inputs);
2381 msm_otg_del_timer(motg);
2382 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002383 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2384 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302385 } else if (test_bit(ID_A, &motg->inputs)) {
2386 msm_hsusb_vbus_power(motg, 0);
2387 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2388 /*
2389 * If TA_WAIT_BCON is infinite, we don;t
2390 * turn off VBUS. Enter low power mode.
2391 */
2392 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002393 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302394 } else if (!test_bit(ID, &motg->inputs)) {
2395 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302396 }
2397 break;
2398 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302399 if ((test_bit(ID, &motg->inputs) &&
2400 !test_bit(ID_A, &motg->inputs)) ||
2401 test_bit(A_BUS_DROP, &motg->inputs)) {
2402 pr_debug("id_a/b/c || a_bus_drop\n");
2403 clear_bit(B_CONN, &motg->inputs);
2404 clear_bit(A_BUS_REQ, &motg->inputs);
2405 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002406 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302407 msm_otg_start_host(otg, 0);
2408 if (!test_bit(ID_A, &motg->inputs))
2409 msm_hsusb_vbus_power(motg, 0);
2410 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2411 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2412 pr_debug("!a_vbus_vld\n");
2413 clear_bit(B_CONN, &motg->inputs);
2414 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002415 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302416 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002417 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302418 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2419 /*
2420 * a_bus_req is de-asserted when root hub is
2421 * suspended or HNP is in progress.
2422 */
2423 pr_debug("!a_bus_req\n");
2424 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002425 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302426 if (otg->host->b_hnp_enable)
2427 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2428 A_AIDL_BDIS);
2429 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002430 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302431 } else if (!test_bit(B_CONN, &motg->inputs)) {
2432 pr_debug("!b_conn\n");
2433 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002434 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302435 if (TA_WAIT_BCON > 0)
2436 msm_otg_start_timer(motg, TA_WAIT_BCON,
2437 A_WAIT_BCON);
2438 if (msm_chg_check_aca_intr(motg))
2439 work = 1;
2440 } else if (test_bit(ID_A, &motg->inputs)) {
2441 msm_otg_del_timer(motg);
2442 msm_hsusb_vbus_power(motg, 0);
2443 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2444 msm_otg_notify_charger(motg,
2445 IDEV_ACA_CHG_MAX);
2446 else
2447 msm_otg_notify_charger(motg,
2448 IDEV_CHG_MIN - motg->mA_port);
2449 } else if (!test_bit(ID, &motg->inputs)) {
2450 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2451 motg->chg_type = USB_INVALID_CHARGER;
2452 msm_otg_notify_charger(motg, 0);
2453 msm_hsusb_vbus_power(motg, 1);
2454 }
2455 break;
2456 case OTG_STATE_A_SUSPEND:
2457 if ((test_bit(ID, &motg->inputs) &&
2458 !test_bit(ID_A, &motg->inputs)) ||
2459 test_bit(A_BUS_DROP, &motg->inputs) ||
2460 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2461 pr_debug("id_a/b/c || a_bus_drop ||"
2462 "a_aidl_bdis_tmout\n");
2463 msm_otg_del_timer(motg);
2464 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002465 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302466 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002467 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302468 if (!test_bit(ID_A, &motg->inputs))
2469 msm_hsusb_vbus_power(motg, 0);
2470 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2471 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2472 pr_debug("!a_vbus_vld\n");
2473 msm_otg_del_timer(motg);
2474 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002475 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302476 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002477 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302478 } else if (!test_bit(B_CONN, &motg->inputs) &&
2479 otg->host->b_hnp_enable) {
2480 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002481 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 msm_otg_host_hnp_enable(otg, 1);
2483 otg->gadget->is_a_peripheral = 1;
2484 msm_otg_start_peripheral(otg, 1);
2485 } else if (!test_bit(B_CONN, &motg->inputs) &&
2486 !otg->host->b_hnp_enable) {
2487 pr_debug("!b_conn && !b_hnp_enable");
2488 /*
2489 * bus request is dropped during suspend.
2490 * acquire again for next device.
2491 */
2492 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002493 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302494 if (TA_WAIT_BCON > 0)
2495 msm_otg_start_timer(motg, TA_WAIT_BCON,
2496 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002497 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302498 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002499 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302500 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002501 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002502 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302503 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302504 }
2505 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302506 case OTG_STATE_A_PERIPHERAL:
2507 if ((test_bit(ID, &motg->inputs) &&
2508 !test_bit(ID_A, &motg->inputs)) ||
2509 test_bit(A_BUS_DROP, &motg->inputs)) {
2510 pr_debug("id _f/b/c || a_bus_drop\n");
2511 /* Clear BIDL_ADIS timer */
2512 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002513 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302514 msm_otg_start_peripheral(otg, 0);
2515 otg->gadget->is_a_peripheral = 0;
2516 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002517 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302518 if (!test_bit(ID_A, &motg->inputs))
2519 msm_hsusb_vbus_power(motg, 0);
2520 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2521 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2522 pr_debug("!a_vbus_vld\n");
2523 /* Clear BIDL_ADIS timer */
2524 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002525 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302526 msm_otg_start_peripheral(otg, 0);
2527 otg->gadget->is_a_peripheral = 0;
2528 msm_otg_start_host(otg, 0);
2529 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2530 pr_debug("a_bidl_adis_tmout\n");
2531 msm_otg_start_peripheral(otg, 0);
2532 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002533 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302534 set_bit(A_BUS_REQ, &motg->inputs);
2535 msm_otg_host_hnp_enable(otg, 0);
2536 if (TA_WAIT_BCON > 0)
2537 msm_otg_start_timer(motg, TA_WAIT_BCON,
2538 A_WAIT_BCON);
2539 } else if (test_bit(ID_A, &motg->inputs)) {
2540 msm_hsusb_vbus_power(motg, 0);
2541 msm_otg_notify_charger(motg,
2542 IDEV_CHG_MIN - motg->mA_port);
2543 } else if (!test_bit(ID, &motg->inputs)) {
2544 msm_otg_notify_charger(motg, 0);
2545 msm_hsusb_vbus_power(motg, 1);
2546 }
2547 break;
2548 case OTG_STATE_A_WAIT_VFALL:
2549 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2550 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002551 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302552 work = 1;
2553 }
2554 break;
2555 case OTG_STATE_A_VBUS_ERR:
2556 if ((test_bit(ID, &motg->inputs) &&
2557 !test_bit(ID_A, &motg->inputs)) ||
2558 test_bit(A_BUS_DROP, &motg->inputs) ||
2559 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002560 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302561 if (!test_bit(ID_A, &motg->inputs))
2562 msm_hsusb_vbus_power(motg, 0);
2563 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2564 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2565 motg->chg_type = USB_INVALID_CHARGER;
2566 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302567 }
2568 break;
2569 default:
2570 break;
2571 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302572 if (work)
2573 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302574}
2575
2576static irqreturn_t msm_otg_irq(int irq, void *data)
2577{
2578 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002579 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302580 u32 otgsc = 0, usbsts, pc;
2581 bool work = 0;
2582 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302583
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302584 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302585 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302586 disable_irq_nosync(irq);
2587 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002588 if (atomic_read(&motg->pm_suspended))
2589 motg->sm_work_pending = true;
2590 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002591 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302592 return IRQ_HANDLED;
2593 }
2594
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002595 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302596 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302597
2598 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302599 return IRQ_NONE;
2600
2601 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302602 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302603 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302604 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302605 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302606 pr_debug("Id clear\n");
2607 /*
2608 * Assert a_bus_req to supply power on
2609 * VBUS when Micro/Mini-A cable is connected
2610 * with out user intervention.
2611 */
2612 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302613 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302614 msm_chg_enable_aca_det(motg);
2615 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302616 writel_relaxed(otgsc, USB_OTGSC);
2617 work = 1;
2618 } else if (otgsc & OTGSC_DPIS) {
2619 pr_debug("DPIS detected\n");
2620 writel_relaxed(otgsc, USB_OTGSC);
2621 set_bit(A_SRP_DET, &motg->inputs);
2622 set_bit(A_BUS_REQ, &motg->inputs);
2623 work = 1;
2624 } else if (otgsc & OTGSC_BSVIS) {
2625 writel_relaxed(otgsc, USB_OTGSC);
2626 /*
2627 * BSV interrupt comes when operating as an A-device
2628 * (VBUS on/off).
2629 * But, handle BSV when charger is removed from ACA in ID_A
2630 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002631 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302632 !test_bit(ID_A, &motg->inputs))
2633 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302634 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302635 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302636 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302637 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302638 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302639 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002640 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2641
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302642 msm_chg_check_aca_intr(motg);
2643 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302644 work = 1;
2645 } else if (usbsts & STS_PCI) {
2646 pc = readl_relaxed(USB_PORTSC);
2647 pr_debug("portsc = %x\n", pc);
2648 ret = IRQ_NONE;
2649 /*
2650 * HCD Acks PCI interrupt. We use this to switch
2651 * between different OTG states.
2652 */
2653 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002654 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302655 case OTG_STATE_A_SUSPEND:
2656 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2657 !(pc & PORTSC_CCS)) {
2658 pr_debug("B_CONN clear\n");
2659 clear_bit(B_CONN, &motg->inputs);
2660 msm_otg_del_timer(motg);
2661 }
2662 break;
2663 case OTG_STATE_A_PERIPHERAL:
2664 /*
2665 * A-peripheral observed activity on bus.
2666 * clear A_BIDL_ADIS timer.
2667 */
2668 msm_otg_del_timer(motg);
2669 work = 0;
2670 break;
2671 case OTG_STATE_B_WAIT_ACON:
2672 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2673 pr_debug("A_CONN set\n");
2674 set_bit(A_CONN, &motg->inputs);
2675 /* Clear ASE0_BRST timer */
2676 msm_otg_del_timer(motg);
2677 }
2678 break;
2679 case OTG_STATE_B_HOST:
2680 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2681 pr_debug("A_CONN clear\n");
2682 clear_bit(A_CONN, &motg->inputs);
2683 msm_otg_del_timer(motg);
2684 }
2685 break;
2686 case OTG_STATE_A_WAIT_BCON:
2687 if (TA_WAIT_BCON < 0)
2688 set_bit(A_BUS_REQ, &motg->inputs);
2689 default:
2690 work = 0;
2691 break;
2692 }
2693 } else if (usbsts & STS_URI) {
2694 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002695 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302696 case OTG_STATE_A_PERIPHERAL:
2697 /*
2698 * A-peripheral observed activity on bus.
2699 * clear A_BIDL_ADIS timer.
2700 */
2701 msm_otg_del_timer(motg);
2702 work = 0;
2703 break;
2704 default:
2705 work = 0;
2706 break;
2707 }
2708 } else if (usbsts & STS_SLI) {
2709 ret = IRQ_NONE;
2710 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002711 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302712 case OTG_STATE_B_PERIPHERAL:
2713 if (otg->gadget->b_hnp_enable) {
2714 set_bit(A_BUS_SUSPEND, &motg->inputs);
2715 set_bit(B_BUS_REQ, &motg->inputs);
2716 work = 1;
2717 }
2718 break;
2719 case OTG_STATE_A_PERIPHERAL:
2720 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2721 A_BIDL_ADIS);
2722 break;
2723 default:
2724 break;
2725 }
2726 } else if ((usbsts & PHY_ALT_INT)) {
2727 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2728 if (msm_chg_check_aca_intr(motg))
2729 work = 1;
2730 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302731 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302732 if (work)
2733 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302734
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302735 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002736}
2737
2738static void msm_otg_set_vbus_state(int online)
2739{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302740 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002741 struct msm_otg *motg = the_msm_otg;
Mayank Ranabaf31f42012-07-05 09:43:54 +05302742 struct usb_otg *otg = motg->phy.otg;
2743
2744 /* In A Host Mode, ignore received BSV interrupts */
2745 if (otg->phy->state >= OTG_STATE_A_IDLE)
2746 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002747
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302748 if (online) {
2749 pr_debug("PMIC: BSV set\n");
2750 set_bit(B_SESS_VLD, &motg->inputs);
2751 } else {
2752 pr_debug("PMIC: BSV clear\n");
2753 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302754 }
2755
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302756 if (!init) {
2757 init = true;
2758 complete(&pmic_vbus_init);
2759 pr_debug("PMIC: BSV init complete\n");
2760 return;
2761 }
2762
Jack Pham5ca279b2012-05-14 18:42:54 -07002763 if (atomic_read(&motg->pm_suspended))
2764 motg->sm_work_pending = true;
2765 else
2766 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002767}
2768
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302769static void msm_pmic_id_status_w(struct work_struct *w)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002770{
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302771 struct msm_otg *motg = container_of(w, struct msm_otg,
2772 pmic_id_status_work.work);
2773 int work = 0;
2774 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002775
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302776 local_irq_save(flags);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302777 if (irq_read_line(motg->pdata->pmic_id_irq)) {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302778 if (!test_and_set_bit(ID, &motg->inputs)) {
2779 pr_debug("PMIC: ID set\n");
2780 work = 1;
2781 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302782 } else {
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302783 if (test_and_clear_bit(ID, &motg->inputs)) {
2784 pr_debug("PMIC: ID clear\n");
2785 set_bit(A_BUS_REQ, &motg->inputs);
2786 work = 1;
2787 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302788 }
2789
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302790 if (work && (motg->phy.state != OTG_STATE_UNDEFINED)) {
Jack Pham5ca279b2012-05-14 18:42:54 -07002791 if (atomic_read(&motg->pm_suspended))
2792 motg->sm_work_pending = true;
2793 else
2794 queue_work(system_nrt_wq, &motg->sm_work);
2795 }
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05302796 local_irq_restore(flags);
2797
2798}
2799
2800#define MSM_PMIC_ID_STATUS_DELAY 5 /* 5msec */
2801static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2802{
2803 struct msm_otg *motg = data;
2804
2805 if (!aca_id_turned_on)
2806 /*schedule delayed work for 5msec for ID line state to settle*/
2807 queue_delayed_work(system_nrt_wq, &motg->pmic_id_status_work,
2808 msecs_to_jiffies(MSM_PMIC_ID_STATUS_DELAY));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002809
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302810 return IRQ_HANDLED;
2811}
2812
2813static int msm_otg_mode_show(struct seq_file *s, void *unused)
2814{
2815 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002816 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302817
Steve Mucklef132c6c2012-06-06 18:30:57 -07002818 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302819 case OTG_STATE_A_HOST:
2820 seq_printf(s, "host\n");
2821 break;
2822 case OTG_STATE_B_PERIPHERAL:
2823 seq_printf(s, "peripheral\n");
2824 break;
2825 default:
2826 seq_printf(s, "none\n");
2827 break;
2828 }
2829
2830 return 0;
2831}
2832
2833static int msm_otg_mode_open(struct inode *inode, struct file *file)
2834{
2835 return single_open(file, msm_otg_mode_show, inode->i_private);
2836}
2837
2838static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2839 size_t count, loff_t *ppos)
2840{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302841 struct seq_file *s = file->private_data;
2842 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302843 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002844 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302845 int status = count;
2846 enum usb_mode_type req_mode;
2847
2848 memset(buf, 0x00, sizeof(buf));
2849
2850 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2851 status = -EFAULT;
2852 goto out;
2853 }
2854
2855 if (!strncmp(buf, "host", 4)) {
2856 req_mode = USB_HOST;
2857 } else if (!strncmp(buf, "peripheral", 10)) {
2858 req_mode = USB_PERIPHERAL;
2859 } else if (!strncmp(buf, "none", 4)) {
2860 req_mode = USB_NONE;
2861 } else {
2862 status = -EINVAL;
2863 goto out;
2864 }
2865
2866 switch (req_mode) {
2867 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002868 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302869 case OTG_STATE_A_HOST:
2870 case OTG_STATE_B_PERIPHERAL:
2871 set_bit(ID, &motg->inputs);
2872 clear_bit(B_SESS_VLD, &motg->inputs);
2873 break;
2874 default:
2875 goto out;
2876 }
2877 break;
2878 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002879 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302880 case OTG_STATE_B_IDLE:
2881 case OTG_STATE_A_HOST:
2882 set_bit(ID, &motg->inputs);
2883 set_bit(B_SESS_VLD, &motg->inputs);
2884 break;
2885 default:
2886 goto out;
2887 }
2888 break;
2889 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002890 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302891 case OTG_STATE_B_IDLE:
2892 case OTG_STATE_B_PERIPHERAL:
2893 clear_bit(ID, &motg->inputs);
2894 break;
2895 default:
2896 goto out;
2897 }
2898 break;
2899 default:
2900 goto out;
2901 }
2902
Steve Mucklef132c6c2012-06-06 18:30:57 -07002903 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302904 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302905out:
2906 return status;
2907}
2908
2909const struct file_operations msm_otg_mode_fops = {
2910 .open = msm_otg_mode_open,
2911 .read = seq_read,
2912 .write = msm_otg_mode_write,
2913 .llseek = seq_lseek,
2914 .release = single_release,
2915};
2916
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302917static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2918{
2919 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002920 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302921
Steve Mucklef132c6c2012-06-06 18:30:57 -07002922 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302923 return 0;
2924}
2925
2926static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2927{
2928 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2929}
2930
2931const struct file_operations msm_otg_state_fops = {
2932 .open = msm_otg_otg_state_open,
2933 .read = seq_read,
2934 .llseek = seq_lseek,
2935 .release = single_release,
2936};
2937
Anji jonnalad270e2d2011-08-09 11:28:32 +05302938static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2939{
2940 struct msm_otg *motg = s->private;
2941
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302942 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302943 return 0;
2944}
2945
2946static int msm_otg_chg_open(struct inode *inode, struct file *file)
2947{
2948 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2949}
2950
2951const struct file_operations msm_otg_chg_fops = {
2952 .open = msm_otg_chg_open,
2953 .read = seq_read,
2954 .llseek = seq_lseek,
2955 .release = single_release,
2956};
2957
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302958static int msm_otg_aca_show(struct seq_file *s, void *unused)
2959{
2960 if (debug_aca_enabled)
2961 seq_printf(s, "enabled\n");
2962 else
2963 seq_printf(s, "disabled\n");
2964
2965 return 0;
2966}
2967
2968static int msm_otg_aca_open(struct inode *inode, struct file *file)
2969{
2970 return single_open(file, msm_otg_aca_show, inode->i_private);
2971}
2972
2973static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2974 size_t count, loff_t *ppos)
2975{
2976 char buf[8];
2977
2978 memset(buf, 0x00, sizeof(buf));
2979
2980 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2981 return -EFAULT;
2982
2983 if (!strncmp(buf, "enable", 6))
2984 debug_aca_enabled = true;
2985 else
2986 debug_aca_enabled = false;
2987
2988 return count;
2989}
2990
2991const struct file_operations msm_otg_aca_fops = {
2992 .open = msm_otg_aca_open,
2993 .read = seq_read,
2994 .write = msm_otg_aca_write,
2995 .llseek = seq_lseek,
2996 .release = single_release,
2997};
2998
Manu Gautam8bdcc592012-03-06 11:26:06 +05302999static int msm_otg_bus_show(struct seq_file *s, void *unused)
3000{
3001 if (debug_bus_voting_enabled)
3002 seq_printf(s, "enabled\n");
3003 else
3004 seq_printf(s, "disabled\n");
3005
3006 return 0;
3007}
3008
3009static int msm_otg_bus_open(struct inode *inode, struct file *file)
3010{
3011 return single_open(file, msm_otg_bus_show, inode->i_private);
3012}
3013
3014static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
3015 size_t count, loff_t *ppos)
3016{
3017 char buf[8];
3018 int ret;
3019 struct seq_file *s = file->private_data;
3020 struct msm_otg *motg = s->private;
3021
3022 memset(buf, 0x00, sizeof(buf));
3023
3024 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3025 return -EFAULT;
3026
3027 if (!strncmp(buf, "enable", 6)) {
3028 /* Do not vote here. Let OTG statemachine decide when to vote */
3029 debug_bus_voting_enabled = true;
3030 } else {
3031 debug_bus_voting_enabled = false;
3032 if (motg->bus_perf_client) {
3033 ret = msm_bus_scale_client_update_request(
3034 motg->bus_perf_client, 0);
3035 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003036 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05303037 "for bus bw %d\n", __func__, ret);
3038 }
3039 }
3040
3041 return count;
3042}
3043
3044const struct file_operations msm_otg_bus_fops = {
3045 .open = msm_otg_bus_open,
3046 .read = seq_read,
3047 .write = msm_otg_bus_write,
3048 .llseek = seq_lseek,
3049 .release = single_release,
3050};
3051
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303052static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303053
3054static int msm_otg_debugfs_init(struct msm_otg *motg)
3055{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303056 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303057
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303058 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3059
3060 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3061 return -ENODEV;
3062
Anji jonnalad270e2d2011-08-09 11:28:32 +05303063 if (motg->pdata->mode == USB_OTG &&
3064 motg->pdata->otg_control == OTG_USER_CONTROL) {
3065
Manu Gautam8bdcc592012-03-06 11:26:06 +05303066 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303067 S_IWUSR, msm_otg_dbg_root, motg,
3068 &msm_otg_mode_fops);
3069
Manu Gautam8bdcc592012-03-06 11:26:06 +05303070 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303071 debugfs_remove(msm_otg_dbg_root);
3072 msm_otg_dbg_root = NULL;
3073 return -ENODEV;
3074 }
3075 }
3076
Manu Gautam8bdcc592012-03-06 11:26:06 +05303077 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303078 msm_otg_dbg_root, motg,
3079 &msm_otg_chg_fops);
3080
Manu Gautam8bdcc592012-03-06 11:26:06 +05303081 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303082 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303083 return -ENODEV;
3084 }
3085
Manu Gautam8bdcc592012-03-06 11:26:06 +05303086 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303087 msm_otg_dbg_root, motg,
3088 &msm_otg_aca_fops);
3089
Manu Gautam8bdcc592012-03-06 11:26:06 +05303090 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303091 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303092 return -ENODEV;
3093 }
3094
Manu Gautam8bdcc592012-03-06 11:26:06 +05303095 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3096 msm_otg_dbg_root, motg,
3097 &msm_otg_bus_fops);
3098
3099 if (!msm_otg_dentry) {
3100 debugfs_remove_recursive(msm_otg_dbg_root);
3101 return -ENODEV;
3102 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303103
3104 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3105 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3106
3107 if (!msm_otg_dentry) {
3108 debugfs_remove_recursive(msm_otg_dbg_root);
3109 return -ENODEV;
3110 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303111 return 0;
3112}
3113
3114static void msm_otg_debugfs_cleanup(void)
3115{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303116 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303117}
3118
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303119static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3120static struct platform_device *msm_otg_add_pdev(
3121 struct platform_device *ofdev, const char *name)
3122{
3123 struct platform_device *pdev;
3124 const struct resource *res = ofdev->resource;
3125 unsigned int num = ofdev->num_resources;
3126 int retval;
3127
3128 pdev = platform_device_alloc(name, -1);
3129 if (!pdev) {
3130 retval = -ENOMEM;
3131 goto error;
3132 }
3133
3134 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3135 pdev->dev.dma_mask = &msm_otg_dma_mask;
3136
3137 if (num) {
3138 retval = platform_device_add_resources(pdev, res, num);
3139 if (retval)
3140 goto error;
3141 }
3142
3143 retval = platform_device_add(pdev);
3144 if (retval)
3145 goto error;
3146
3147 return pdev;
3148
3149error:
3150 platform_device_put(pdev);
3151 return ERR_PTR(retval);
3152}
3153
3154static int msm_otg_setup_devices(struct platform_device *ofdev,
3155 enum usb_mode_type mode, bool init)
3156{
3157 const char *gadget_name = "msm_hsusb";
3158 const char *host_name = "msm_hsusb_host";
3159 static struct platform_device *gadget_pdev;
3160 static struct platform_device *host_pdev;
3161 int retval = 0;
3162
3163 if (!init) {
3164 if (gadget_pdev)
3165 platform_device_unregister(gadget_pdev);
3166 if (host_pdev)
3167 platform_device_unregister(host_pdev);
3168 return 0;
3169 }
3170
3171 switch (mode) {
3172 case USB_OTG:
3173 /* fall through */
3174 case USB_PERIPHERAL:
3175 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3176 if (IS_ERR(gadget_pdev)) {
3177 retval = PTR_ERR(gadget_pdev);
3178 break;
3179 }
3180 if (mode == USB_PERIPHERAL)
3181 break;
3182 /* fall through */
3183 case USB_HOST:
3184 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3185 if (IS_ERR(host_pdev)) {
3186 retval = PTR_ERR(host_pdev);
3187 if (mode == USB_OTG)
3188 platform_device_unregister(gadget_pdev);
3189 }
3190 break;
3191 default:
3192 break;
3193 }
3194
3195 return retval;
3196}
3197
3198struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3199{
3200 struct device_node *node = pdev->dev.of_node;
3201 struct msm_otg_platform_data *pdata;
3202 int len = 0;
3203
3204 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3205 if (!pdata) {
3206 pr_err("unable to allocate platform data\n");
3207 return NULL;
3208 }
3209 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3210 if (len) {
3211 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3212 if (!pdata->phy_init_seq)
3213 return NULL;
3214 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3215 pdata->phy_init_seq,
3216 len/sizeof(*pdata->phy_init_seq));
3217 }
3218 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3219 &pdata->power_budget);
3220 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3221 &pdata->mode);
3222 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3223 &pdata->otg_control);
3224 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3225 &pdata->default_mode);
3226 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3227 &pdata->phy_type);
3228 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3229 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303230 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303231}
3232
3233static int __init msm_otg_probe(struct platform_device *pdev)
3234{
3235 int ret = 0;
3236 struct resource *res;
3237 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003238 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303239 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303240
3241 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303242
3243 if (pdev->dev.of_node) {
3244 dev_dbg(&pdev->dev, "device tree enabled\n");
3245 pdata = msm_otg_dt_to_pdata(pdev);
3246 if (!pdata)
3247 return -ENOMEM;
3248 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3249 if (ret) {
3250 dev_err(&pdev->dev, "devices setup failed\n");
3251 return ret;
3252 }
3253 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303254 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3255 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303256 } else {
3257 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303258 }
3259
3260 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3261 if (!motg) {
3262 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3263 return -ENOMEM;
3264 }
3265
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003266 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3267 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003268 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3269 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303270 goto free_motg;
3271 }
3272
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003273 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303274 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003275 phy = &motg->phy;
3276 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303277
3278 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303279 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3280 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3281 * PMIC can detect ACA ID_GND and generate an interrupt.
3282 */
3283 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3284 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3285 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003286 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303287 }
3288
Ofir Cohen4da266f2012-01-03 10:19:29 +02003289 /* initialize reset counter */
3290 motg->reset_counter = 0;
3291
Amit Blay02eff132011-09-21 16:46:24 +03003292 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003293 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003294 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003295 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303296
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303297 /*
3298 * Targets on which link uses asynchronous reset methodology,
3299 * free running clock is not required during the reset.
3300 */
Manu Gautam5143b252012-01-05 19:25:23 -08003301 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303302 if (IS_ERR(motg->clk))
3303 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3304 else
3305 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303306
3307 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003308 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303309 * CORE CLK must be running at >55Mhz for correct HSUSB
3310 * operation and USB core cannot tolerate frequency changes on
3311 * CORE CLK. For such USB cores, vote for maximum clk frequency
3312 * on pclk source
3313 */
Manu Gautam5143b252012-01-05 19:25:23 -08003314 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3315 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303316 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003317 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303318 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003319 goto put_clk;
3320 }
3321 clk_set_rate(motg->core_clk, INT_MAX);
3322
3323 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3324 if (IS_ERR(motg->pclk)) {
3325 dev_err(&pdev->dev, "failed to get iface_clk\n");
3326 ret = PTR_ERR(motg->pclk);
3327 goto put_core_clk;
3328 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303329
3330 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3331 if (!res) {
3332 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3333 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003334 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303335 }
3336
3337 motg->regs = ioremap(res->start, resource_size(res));
3338 if (!motg->regs) {
3339 dev_err(&pdev->dev, "ioremap failed\n");
3340 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003341 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303342 }
3343 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3344
3345 motg->irq = platform_get_irq(pdev, 0);
3346 if (!motg->irq) {
3347 dev_err(&pdev->dev, "platform_get_irq failed\n");
3348 ret = -ENODEV;
3349 goto free_regs;
3350 }
3351
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003352 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003353 if (IS_ERR(motg->xo_handle)) {
3354 dev_err(&pdev->dev, "%s not able to get the handle "
3355 "to vote for TCXO D0 buffer\n", __func__);
3356 ret = PTR_ERR(motg->xo_handle);
3357 goto free_regs;
3358 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303359
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003360 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003361 if (ret) {
3362 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3363 "D0 buffer%d\n", __func__, ret);
3364 goto free_xo_handle;
3365 }
3366
Manu Gautam28b1bac2012-01-30 16:43:06 +05303367 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303368
Mayank Rana248698c2012-04-19 00:03:16 +05303369 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003370 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303371 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003372 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303373 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003374 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303375 goto devote_xo_handle;
3376 }
3377 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303378 }
3379
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003380 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303381 if (ret) {
3382 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303383 goto devote_xo_handle;
3384 }
3385
3386 ret = regulator_enable(hsusb_vddcx);
3387 if (ret) {
3388 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3389 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303390 }
3391
3392 ret = msm_hsusb_ldo_init(motg, 1);
3393 if (ret) {
3394 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303395 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303396 }
3397
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303398 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303399 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3400 "mhl_usb_hs_switch");
3401 if (IS_ERR(mhl_usb_hs_switch)) {
3402 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303403 goto free_ldo_init;
3404 }
3405 }
3406
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003407 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303408 if (ret) {
3409 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003410 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303411 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303412 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303413
3414 writel(0, USB_USBINTR);
3415 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003416 /* Ensure that above STOREs are completed before enabling interrupts */
3417 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303418
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003419 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303420 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303421 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303422 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303423 INIT_DELAYED_WORK(&motg->pmic_id_status_work, msm_pmic_id_status_w);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303424 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3425 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303426 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3427 "msm_otg", motg);
3428 if (ret) {
3429 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003430 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303431 }
3432
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003433 phy->init = msm_otg_reset;
3434 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003435 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303436
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003437 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303438
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003439 phy->otg->phy = &motg->phy;
3440 phy->otg->set_host = msm_otg_set_host;
3441 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003442 phy->otg->start_hnp = msm_otg_start_hnp;
3443 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003444
3445 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303446 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003447 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303448 goto free_irq;
3449 }
3450
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303451 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303452 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003453 if (motg->pdata->pmic_id_irq) {
3454 ret = request_irq(motg->pdata->pmic_id_irq,
3455 msm_pmic_id_irq,
3456 IRQF_TRIGGER_RISING |
3457 IRQF_TRIGGER_FALLING,
3458 "msm_otg", motg);
3459 if (ret) {
3460 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003461 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003462 }
3463 } else {
3464 ret = -ENODEV;
3465 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003466 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003467 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303468 }
3469
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303470 msm_hsusb_mhl_switch_enable(motg, 1);
3471
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303472 platform_set_drvdata(pdev, motg);
3473 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003474 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303475
Anji jonnalad270e2d2011-08-09 11:28:32 +05303476 ret = msm_otg_debugfs_init(motg);
3477 if (ret)
3478 dev_dbg(&pdev->dev, "mode debugfs file is"
3479 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003481 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3482 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3483
Amit Blay58b31472011-11-18 09:39:39 +02003484 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3485 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303486 (!(motg->pdata->mode == USB_OTG) ||
3487 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003488 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303489 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003490
Amit Blay58b31472011-11-18 09:39:39 +02003491 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3492 motg->caps = ALLOW_PHY_RETENTION;
3493 }
3494
Amit Blay6fa647a2012-05-24 14:12:08 +03003495 if (motg->pdata->enable_lpm_on_dev_suspend)
3496 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3497
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003498 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303499 pm_runtime_set_active(&pdev->dev);
3500 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303501
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303502 if (motg->pdata->bus_scale_table) {
3503 motg->bus_perf_client =
3504 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3505 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003506 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303507 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303508 else
3509 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303510 }
3511
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303512 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003513
Steve Mucklef132c6c2012-06-06 18:30:57 -07003514remove_phy:
3515 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303516free_irq:
3517 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003518destroy_wlock:
3519 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303520 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003521 msm_hsusb_ldo_enable(motg, 0);
3522free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303523 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303524free_hsusb_vddcx:
3525 regulator_disable(hsusb_vddcx);
3526free_config_vddcx:
3527 regulator_set_voltage(hsusb_vddcx,
3528 vdd_val[motg->vdd_type][VDD_NONE],
3529 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003530devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303531 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003532 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003533free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003534 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303535free_regs:
3536 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003537put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303538 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303539put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003540 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303541put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303542 if (!IS_ERR(motg->clk))
3543 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003544 if (!IS_ERR(motg->phy_reset_clk))
3545 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003546free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003547 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303548free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303549 kfree(motg);
3550 return ret;
3551}
3552
3553static int __devexit msm_otg_remove(struct platform_device *pdev)
3554{
3555 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003556 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303557 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303558
3559 if (otg->host || otg->gadget)
3560 return -EBUSY;
3561
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303562 if (pdev->dev.of_node)
3563 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003564 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3565 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303566 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303567 cancel_delayed_work_sync(&motg->chg_work);
Vijayavardhan Vennapusa68d55cf2012-06-27 20:06:12 +05303568 cancel_delayed_work_sync(&motg->pmic_id_status_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303569 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303570
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303571 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303572
3573 device_init_wakeup(&pdev->dev, 0);
3574 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003575 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303576
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303577 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003578 if (motg->pdata->pmic_id_irq)
3579 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003580 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581 free_irq(motg->irq, motg);
3582
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303583 /*
3584 * Put PHY in low power mode.
3585 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003586 ulpi_read(otg->phy, 0x14);
3587 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303588
3589 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3590 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3591 if (readl(USB_PORTSC) & PORTSC_PHCD)
3592 break;
3593 udelay(1);
3594 cnt++;
3595 }
3596 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003597 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303598
Manu Gautam28b1bac2012-01-30 16:43:06 +05303599 clk_disable_unprepare(motg->pclk);
3600 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003601 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003602 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303603 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303604 regulator_disable(hsusb_vddcx);
3605 regulator_set_voltage(hsusb_vddcx,
3606 vdd_val[motg->vdd_type][VDD_NONE],
3607 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303608
3609 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303610 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303611
Amit Blay02eff132011-09-21 16:46:24 +03003612 if (!IS_ERR(motg->phy_reset_clk))
3613 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303614 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303615 if (!IS_ERR(motg->clk))
3616 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003617 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303618
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303619 if (motg->bus_perf_client)
3620 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303621
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003622 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303623 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303624 return 0;
3625}
3626
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303627#ifdef CONFIG_PM_RUNTIME
3628static int msm_otg_runtime_idle(struct device *dev)
3629{
3630 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003631 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303632
3633 dev_dbg(dev, "OTG runtime idle\n");
3634
Steve Mucklef132c6c2012-06-06 18:30:57 -07003635 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303636 return -EAGAIN;
3637 else
3638 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303639}
3640
3641static int msm_otg_runtime_suspend(struct device *dev)
3642{
3643 struct msm_otg *motg = dev_get_drvdata(dev);
3644
3645 dev_dbg(dev, "OTG runtime suspend\n");
3646 return msm_otg_suspend(motg);
3647}
3648
3649static int msm_otg_runtime_resume(struct device *dev)
3650{
3651 struct msm_otg *motg = dev_get_drvdata(dev);
3652
3653 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303654 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303655 return msm_otg_resume(motg);
3656}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303657#endif
3658
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303659#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303660static int msm_otg_pm_suspend(struct device *dev)
3661{
Jack Pham5ca279b2012-05-14 18:42:54 -07003662 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303663 struct msm_otg *motg = dev_get_drvdata(dev);
3664
3665 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003666
3667 atomic_set(&motg->pm_suspended, 1);
3668 ret = msm_otg_suspend(motg);
3669 if (ret)
3670 atomic_set(&motg->pm_suspended, 0);
3671
3672 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303673}
3674
3675static int msm_otg_pm_resume(struct device *dev)
3676{
Jack Pham5ca279b2012-05-14 18:42:54 -07003677 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303678 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303679
3680 dev_dbg(dev, "OTG PM resume\n");
3681
Jack Pham5ca279b2012-05-14 18:42:54 -07003682 atomic_set(&motg->pm_suspended, 0);
3683 if (motg->sm_work_pending) {
3684 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303685
Jack Pham5ca279b2012-05-14 18:42:54 -07003686 pm_runtime_get_noresume(dev);
3687 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303688
Jack Pham5ca279b2012-05-14 18:42:54 -07003689 /* Update runtime PM status */
3690 pm_runtime_disable(dev);
3691 pm_runtime_set_active(dev);
3692 pm_runtime_enable(dev);
3693
3694 queue_work(system_nrt_wq, &motg->sm_work);
3695 }
3696
3697 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303698}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303699#endif
3700
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303701#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303702static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303703 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3704 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3705 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303706};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303707#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303708
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303709static struct of_device_id msm_otg_dt_match[] = {
3710 { .compatible = "qcom,hsusb-otg",
3711 },
3712 {}
3713};
3714
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303715static struct platform_driver msm_otg_driver = {
3716 .remove = __devexit_p(msm_otg_remove),
3717 .driver = {
3718 .name = DRIVER_NAME,
3719 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303720#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303721 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303722#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303723 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303724 },
3725};
3726
3727static int __init msm_otg_init(void)
3728{
3729 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3730}
3731
3732static void __exit msm_otg_exit(void)
3733{
3734 platform_driver_unregister(&msm_otg_driver);
3735}
3736
3737module_init(msm_otg_init);
3738module_exit(msm_otg_exit);
3739
3740MODULE_LICENSE("GPL v2");
3741MODULE_DESCRIPTION("MSM USB transceiver driver");