blob: 14118e723c4cf1ba47ba695ebfc00c20d297dc21 [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;
1004 else if (motg->chg_type == USB_DCP_CHARGER)
1005 charger_type = POWER_SUPPLY_TYPE_USB_DCP;
1006 else if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1007 motg->chg_type == USB_ACA_A_CHARGER ||
1008 motg->chg_type == USB_ACA_B_CHARGER ||
1009 motg->chg_type == USB_ACA_C_CHARGER))
1010 charger_type = POWER_SUPPLY_TYPE_USB_ACA;
1011 else
1012 charger_type = POWER_SUPPLY_TYPE_BATTERY;
1013
1014 return pm8921_set_usb_power_supply_type(charger_type);
1015}
1016
Amit Blay0f7edf72012-01-15 10:11:27 +02001017static int msm_otg_notify_power_supply(struct msm_otg *motg, unsigned mA)
1018{
Amit Blay0f7edf72012-01-15 10:11:27 +02001019
Amit Blay0f7edf72012-01-15 10:11:27 +02001020 if (!psy)
1021 goto psy_not_supported;
1022
1023 if (motg->cur_power == 0 && mA > 0) {
1024 /* Enable charging */
1025 if (power_supply_set_online(psy, true))
1026 goto psy_not_supported;
1027 } else if (motg->cur_power > 0 && mA == 0) {
1028 /* Disable charging */
1029 if (power_supply_set_online(psy, false))
1030 goto psy_not_supported;
1031 return 0;
1032 }
1033 /* Set max current limit */
1034 if (power_supply_set_current_limit(psy, 1000*mA))
1035 goto psy_not_supported;
1036
1037 return 0;
1038
1039psy_not_supported:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001040 dev_dbg(motg->phy.dev, "Power Supply doesn't support USB charger\n");
Amit Blay0f7edf72012-01-15 10:11:27 +02001041 return -ENXIO;
1042}
1043
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301044static void msm_otg_notify_charger(struct msm_otg *motg, unsigned mA)
1045{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001046 struct usb_gadget *g = motg->phy.otg->gadget;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301047
1048 if (g && g->is_a_peripheral)
1049 return;
1050
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301051 if ((motg->chg_type == USB_ACA_DOCK_CHARGER ||
1052 motg->chg_type == USB_ACA_A_CHARGER ||
1053 motg->chg_type == USB_ACA_B_CHARGER ||
1054 motg->chg_type == USB_ACA_C_CHARGER) &&
1055 mA > IDEV_ACA_CHG_LIMIT)
1056 mA = IDEV_ACA_CHG_LIMIT;
1057
David Keitel081a3e22012-04-18 12:37:07 -07001058 if (msm_otg_notify_chg_type(motg))
Steve Mucklef132c6c2012-06-06 18:30:57 -07001059 dev_err(motg->phy.dev,
David Keitel081a3e22012-04-18 12:37:07 -07001060 "Failed notifying %d charger type to PMIC\n",
1061 motg->chg_type);
1062
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301063 if (motg->cur_power == mA)
1064 return;
1065
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001066 dev_info(motg->phy.dev, "Avail curr from USB = %u\n", mA);
Amit Blay0f7edf72012-01-15 10:11:27 +02001067
1068 /*
1069 * Use Power Supply API if supported, otherwise fallback
1070 * to legacy pm8921 API.
1071 */
1072 if (msm_otg_notify_power_supply(motg, mA))
1073 pm8921_charger_vbus_draw(mA);
1074
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301075 motg->cur_power = mA;
1076}
1077
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001078static int msm_otg_set_power(struct usb_phy *phy, unsigned mA)
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301079{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001080 struct msm_otg *motg = container_of(phy, struct msm_otg, phy);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301081
1082 /*
1083 * Gadget driver uses set_power method to notify about the
1084 * available current based on suspend/configured states.
1085 *
1086 * IDEV_CHG can be drawn irrespective of suspend/un-configured
1087 * states when CDP/ACA is connected.
1088 */
1089 if (motg->chg_type == USB_SDP_CHARGER)
1090 msm_otg_notify_charger(motg, mA);
1091
1092 return 0;
1093}
1094
Steve Mucklef132c6c2012-06-06 18:30:57 -07001095static void msm_otg_start_host(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301096{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001097 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301098 struct msm_otg_platform_data *pdata = motg->pdata;
1099 struct usb_hcd *hcd;
1100
1101 if (!otg->host)
1102 return;
1103
1104 hcd = bus_to_hcd(otg->host);
1105
1106 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001107 dev_dbg(otg->phy->dev, "host on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301108
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301109 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001110 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301111 ULPI_SET(ULPI_PWR_CLK_MNG_REG));
1112
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301113 /*
1114 * Some boards have a switch cotrolled by gpio
1115 * to enable/disable internal HUB. Enable internal
1116 * HUB before kicking the host.
1117 */
1118 if (pdata->setup_gpio)
1119 pdata->setup_gpio(OTG_STATE_A_HOST);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301120 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301121 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001122 dev_dbg(otg->phy->dev, "host off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301123
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301124 usb_remove_hcd(hcd);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301125 /* HCD core reset all bits of PORTSC. select ULPI phy */
1126 writel_relaxed(0x80000000, USB_PORTSC);
1127
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301128 if (pdata->setup_gpio)
1129 pdata->setup_gpio(OTG_STATE_UNDEFINED);
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301130
1131 if (pdata->otg_control == OTG_PHY_CONTROL)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001132 ulpi_write(otg->phy, OTG_COMP_DISABLE,
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05301133 ULPI_CLR(ULPI_PWR_CLK_MNG_REG));
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301134 }
1135}
1136
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001137static int msm_otg_usbdev_notify(struct notifier_block *self,
1138 unsigned long action, void *priv)
1139{
1140 struct msm_otg *motg = container_of(self, struct msm_otg, usbdev_nb);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001141 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301142 struct usb_device *udev = priv;
1143
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301144 if (action == USB_BUS_ADD || action == USB_BUS_REMOVE)
1145 goto out;
1146
Steve Mucklef132c6c2012-06-06 18:30:57 -07001147 if (udev->bus != otg->host)
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301148 goto out;
1149 /*
1150 * Interested in devices connected directly to the root hub.
1151 * ACA dock can supply IDEV_CHG irrespective devices connected
1152 * on the accessory port.
1153 */
1154 if (!udev->parent || udev->parent->parent ||
1155 motg->chg_type == USB_ACA_DOCK_CHARGER)
1156 goto out;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001157
1158 switch (action) {
1159 case USB_DEVICE_ADD:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301160 if (aca_enabled())
1161 usb_disable_autosuspend(udev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001162 if (otg->phy->state == OTG_STATE_A_WAIT_BCON) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301163 pr_debug("B_CONN set\n");
1164 set_bit(B_CONN, &motg->inputs);
1165 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001166 otg->phy->state = OTG_STATE_A_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301167 /*
1168 * OTG PET: A-device must end session within
1169 * 10 sec after PET enumeration.
1170 */
1171 if (udev->quirks & USB_QUIRK_OTG_PET)
1172 msm_otg_start_timer(motg, TA_TST_MAINT,
1173 A_TST_MAINT);
1174 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301175 /* fall through */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001176 case USB_DEVICE_CONFIG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001177 if (udev->actconfig)
1178 motg->mA_port = udev->actconfig->desc.bMaxPower * 2;
1179 else
1180 motg->mA_port = IUNIT;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001181 if (otg->phy->state == OTG_STATE_B_HOST)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301182 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301183 break;
1184 case USB_DEVICE_REMOVE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001185 if ((otg->phy->state == OTG_STATE_A_HOST) ||
1186 (otg->phy->state == OTG_STATE_A_SUSPEND)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301187 pr_debug("B_CONN clear\n");
1188 clear_bit(B_CONN, &motg->inputs);
1189 /*
1190 * OTG PET: A-device must end session after
1191 * PET disconnection if it is enumerated
1192 * with bcdDevice[0] = 1. USB core sets
1193 * bus->otg_vbus_off for us. clear it here.
1194 */
1195 if (udev->bus->otg_vbus_off) {
1196 udev->bus->otg_vbus_off = 0;
1197 set_bit(A_BUS_DROP, &motg->inputs);
1198 }
1199 queue_work(system_nrt_wq, &motg->sm_work);
1200 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001201 default:
1202 break;
1203 }
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301204 if (test_bit(ID_A, &motg->inputs))
1205 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX -
1206 motg->mA_port);
1207out:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001208 return NOTIFY_OK;
1209}
1210
Mayank Ranae3926882011-12-26 09:47:54 +05301211static void msm_hsusb_vbus_power(struct msm_otg *motg, bool on)
1212{
1213 int ret;
1214 static bool vbus_is_on;
1215
1216 if (vbus_is_on == on)
1217 return;
1218
1219 if (motg->pdata->vbus_power) {
Mayank Rana91f597e2012-01-20 10:12:06 +05301220 ret = motg->pdata->vbus_power(on);
1221 if (!ret)
1222 vbus_is_on = on;
Mayank Ranae3926882011-12-26 09:47:54 +05301223 return;
1224 }
1225
1226 if (!vbus_otg) {
1227 pr_err("vbus_otg is NULL.");
1228 return;
1229 }
1230
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001231 /*
1232 * if entering host mode tell the charger to not draw any current
Abhijeet Dharmapurikar6d941212012-03-05 10:30:56 -08001233 * from usb before turning on the boost.
1234 * if exiting host mode disable the boost before enabling to draw
1235 * current from the source.
Abhijeet Dharmapurikarbe054882012-01-03 20:27:07 -08001236 */
Mayank Ranae3926882011-12-26 09:47:54 +05301237 if (on) {
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001238 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301239 ret = regulator_enable(vbus_otg);
1240 if (ret) {
1241 pr_err("unable to enable vbus_otg\n");
1242 return;
1243 }
1244 vbus_is_on = true;
1245 } else {
1246 ret = regulator_disable(vbus_otg);
1247 if (ret) {
1248 pr_err("unable to disable vbus_otg\n");
1249 return;
1250 }
Abhijeet Dharmapurikar5e96aaa2012-06-26 11:21:03 -07001251 msm_otg_notify_host_mode(motg, on);
Mayank Ranae3926882011-12-26 09:47:54 +05301252 vbus_is_on = false;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301253 }
1254}
1255
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001256static int msm_otg_set_host(struct usb_otg *otg, struct usb_bus *host)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301257{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001258 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301259 struct usb_hcd *hcd;
1260
1261 /*
1262 * Fail host registration if this board can support
1263 * only peripheral configuration.
1264 */
1265 if (motg->pdata->mode == USB_PERIPHERAL) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001266 dev_info(otg->phy->dev, "Host mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301267 return -ENODEV;
1268 }
1269
Mayank Ranae3926882011-12-26 09:47:54 +05301270 if (!motg->pdata->vbus_power && host) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001271 vbus_otg = devm_regulator_get(motg->phy.dev, "vbus_otg");
Mayank Ranae3926882011-12-26 09:47:54 +05301272 if (IS_ERR(vbus_otg)) {
1273 pr_err("Unable to get vbus_otg\n");
1274 return -ENODEV;
1275 }
1276 }
1277
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301278 if (!host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001279 if (otg->phy->state == OTG_STATE_A_HOST) {
1280 pm_runtime_get_sync(otg->phy->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281 usb_unregister_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301282 msm_otg_start_host(otg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05301283 msm_hsusb_vbus_power(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301284 otg->host = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001285 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301286 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301287 } else {
1288 otg->host = NULL;
1289 }
1290
1291 return 0;
1292 }
1293
1294 hcd = bus_to_hcd(host);
1295 hcd->power_budget = motg->pdata->power_budget;
1296
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301297#ifdef CONFIG_USB_OTG
1298 host->otg_port = 1;
1299#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001300 motg->usbdev_nb.notifier_call = msm_otg_usbdev_notify;
1301 usb_register_notify(&motg->usbdev_nb);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301302 otg->host = host;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001303 dev_dbg(otg->phy->dev, "host driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301304
1305 /*
1306 * Kick the state machine work, if peripheral is not supported
1307 * or peripheral is already registered with us.
1308 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301309 if (motg->pdata->mode == USB_HOST || otg->gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001310 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301311 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301312 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301313
1314 return 0;
1315}
1316
Steve Mucklef132c6c2012-06-06 18:30:57 -07001317static void msm_otg_start_peripheral(struct usb_otg *otg, int on)
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301318{
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301319 int ret;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001320 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301321 struct msm_otg_platform_data *pdata = motg->pdata;
1322
1323 if (!otg->gadget)
1324 return;
1325
1326 if (on) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001327 dev_dbg(otg->phy->dev, "gadget on\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301328 /*
1329 * Some boards have a switch cotrolled by gpio
1330 * to enable/disable internal HUB. Disable internal
1331 * HUB before kicking the gadget.
1332 */
1333 if (pdata->setup_gpio)
1334 pdata->setup_gpio(OTG_STATE_B_PERIPHERAL);
Ofir Cohen94213a72012-05-03 14:26:32 +03001335
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301336 /* Configure BUS performance parameters for MAX bandwidth */
Manu Gautam8bdcc592012-03-06 11:26:06 +05301337 if (motg->bus_perf_client && debug_bus_voting_enabled) {
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301338 ret = msm_bus_scale_client_update_request(
1339 motg->bus_perf_client, 1);
1340 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001341 dev_err(motg->phy.dev, "%s: Failed to vote for "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301342 "bus bandwidth %d\n", __func__, ret);
1343 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301344 usb_gadget_vbus_connect(otg->gadget);
1345 } else {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001346 dev_dbg(otg->phy->dev, "gadget off\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301347 usb_gadget_vbus_disconnect(otg->gadget);
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301348 /* Configure BUS performance parameters to default */
1349 if (motg->bus_perf_client) {
1350 ret = msm_bus_scale_client_update_request(
1351 motg->bus_perf_client, 0);
1352 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001353 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05301354 "for bus bw %d\n", __func__, ret);
1355 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301356 if (pdata->setup_gpio)
1357 pdata->setup_gpio(OTG_STATE_UNDEFINED);
1358 }
1359
1360}
1361
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001362static int msm_otg_set_peripheral(struct usb_otg *otg,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301363 struct usb_gadget *gadget)
1364{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001365 struct msm_otg *motg = container_of(otg->phy, struct msm_otg, phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301366
1367 /*
1368 * Fail peripheral registration if this board can support
1369 * only host configuration.
1370 */
1371 if (motg->pdata->mode == USB_HOST) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001372 dev_info(otg->phy->dev, "Peripheral mode is not supported\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301373 return -ENODEV;
1374 }
1375
1376 if (!gadget) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001377 if (otg->phy->state == OTG_STATE_B_PERIPHERAL) {
1378 pm_runtime_get_sync(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301379 msm_otg_start_peripheral(otg, 0);
1380 otg->gadget = NULL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001381 otg->phy->state = OTG_STATE_UNDEFINED;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301382 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301383 } else {
1384 otg->gadget = NULL;
1385 }
1386
1387 return 0;
1388 }
1389 otg->gadget = gadget;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001390 dev_dbg(otg->phy->dev, "peripheral driver registered w/ tranceiver\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301391
1392 /*
1393 * Kick the state machine work, if host is not supported
1394 * or host is already registered with us.
1395 */
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301396 if (motg->pdata->mode == USB_PERIPHERAL || otg->host) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001397 pm_runtime_get_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301398 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05301399 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301400
1401 return 0;
1402}
1403
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404static bool msm_chg_aca_detect(struct msm_otg *motg)
1405{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001406 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001407 u32 int_sts;
1408 bool ret = false;
1409
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301410 if (!aca_enabled())
1411 goto out;
1412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001413 if (motg->pdata->phy_type == CI_45NM_INTEGRATED_PHY)
1414 goto out;
1415
Steve Mucklef132c6c2012-06-06 18:30:57 -07001416 int_sts = ulpi_read(phy, 0x87);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 switch (int_sts & 0x1C) {
1418 case 0x08:
1419 if (!test_and_set_bit(ID_A, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001420 dev_dbg(phy->dev, "ID_A\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001421 motg->chg_type = USB_ACA_A_CHARGER;
1422 motg->chg_state = USB_CHG_STATE_DETECTED;
1423 clear_bit(ID_B, &motg->inputs);
1424 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301425 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426 ret = true;
1427 }
1428 break;
1429 case 0x0C:
1430 if (!test_and_set_bit(ID_B, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001431 dev_dbg(phy->dev, "ID_B\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 motg->chg_type = USB_ACA_B_CHARGER;
1433 motg->chg_state = USB_CHG_STATE_DETECTED;
1434 clear_bit(ID_A, &motg->inputs);
1435 clear_bit(ID_C, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301436 set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437 ret = true;
1438 }
1439 break;
1440 case 0x10:
1441 if (!test_and_set_bit(ID_C, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001442 dev_dbg(phy->dev, "ID_C\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443 motg->chg_type = USB_ACA_C_CHARGER;
1444 motg->chg_state = USB_CHG_STATE_DETECTED;
1445 clear_bit(ID_A, &motg->inputs);
1446 clear_bit(ID_B, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301447 set_bit(ID, &motg->inputs);
1448 ret = true;
1449 }
1450 break;
1451 case 0x04:
1452 if (test_and_clear_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001453 dev_dbg(phy->dev, "ID_GND\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301454 motg->chg_type = USB_INVALID_CHARGER;
1455 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1456 clear_bit(ID_A, &motg->inputs);
1457 clear_bit(ID_B, &motg->inputs);
1458 clear_bit(ID_C, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001459 ret = true;
1460 }
1461 break;
1462 default:
1463 ret = test_and_clear_bit(ID_A, &motg->inputs) |
1464 test_and_clear_bit(ID_B, &motg->inputs) |
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301465 test_and_clear_bit(ID_C, &motg->inputs) |
1466 !test_and_set_bit(ID, &motg->inputs);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 if (ret) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001468 dev_dbg(phy->dev, "ID A/B/C/GND is no more\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001469 motg->chg_type = USB_INVALID_CHARGER;
1470 motg->chg_state = USB_CHG_STATE_UNDEFINED;
1471 }
1472 }
1473out:
1474 return ret;
1475}
1476
1477static void msm_chg_enable_aca_det(struct msm_otg *motg)
1478{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001479 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001480
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301481 if (!aca_enabled())
1482 return;
1483
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484 switch (motg->pdata->phy_type) {
1485 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301486 /* Disable ID_GND in link and PHY */
1487 writel_relaxed(readl_relaxed(USB_OTGSC) & ~(OTGSC_IDPU |
1488 OTGSC_IDIE), USB_OTGSC);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001489 ulpi_write(phy, 0x01, 0x0C);
1490 ulpi_write(phy, 0x10, 0x0F);
1491 ulpi_write(phy, 0x10, 0x12);
Pavankumar Kondeti446f4542012-02-01 13:57:13 +05301492 /* Disable PMIC ID pull-up */
1493 pm8xxx_usb_id_pullup(0);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301494 /* Enable ACA ID detection */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001495 ulpi_write(phy, 0x20, 0x85);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301496 aca_id_turned_on = true;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001497 break;
1498 default:
1499 break;
1500 }
1501}
1502
1503static void msm_chg_enable_aca_intr(struct msm_otg *motg)
1504{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001505 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001506
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301507 if (!aca_enabled())
1508 return;
1509
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001510 switch (motg->pdata->phy_type) {
1511 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301512 /* Enable ACA Detection interrupt (on any RID change) */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001513 ulpi_write(phy, 0x01, 0x94);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301514 break;
1515 default:
1516 break;
1517 }
1518}
1519
1520static void msm_chg_disable_aca_intr(struct msm_otg *motg)
1521{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001522 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301523
1524 if (!aca_enabled())
1525 return;
1526
1527 switch (motg->pdata->phy_type) {
1528 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001529 ulpi_write(phy, 0x01, 0x95);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001530 break;
1531 default:
1532 break;
1533 }
1534}
1535
1536static bool msm_chg_check_aca_intr(struct msm_otg *motg)
1537{
Steve Mucklef132c6c2012-06-06 18:30:57 -07001538 struct usb_phy *phy = &motg->phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 bool ret = false;
1540
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301541 if (!aca_enabled())
1542 return ret;
1543
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001544 switch (motg->pdata->phy_type) {
1545 case SNPS_28NM_INTEGRATED_PHY:
Steve Mucklef132c6c2012-06-06 18:30:57 -07001546 if (ulpi_read(phy, 0x91) & 1) {
1547 dev_dbg(phy->dev, "RID change\n");
1548 ulpi_write(phy, 0x01, 0x92);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001549 ret = msm_chg_aca_detect(motg);
1550 }
1551 default:
1552 break;
1553 }
1554 return ret;
1555}
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301556
1557static void msm_otg_id_timer_func(unsigned long data)
1558{
1559 struct msm_otg *motg = (struct msm_otg *) data;
1560
1561 if (!aca_enabled())
1562 return;
1563
1564 if (atomic_read(&motg->in_lpm)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001565 dev_dbg(motg->phy.dev, "timer: in lpm\n");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301566 return;
1567 }
1568
Steve Mucklef132c6c2012-06-06 18:30:57 -07001569 if (motg->phy.state == OTG_STATE_A_SUSPEND)
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301570 goto out;
1571
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301572 if (msm_chg_check_aca_intr(motg)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07001573 dev_dbg(motg->phy.dev, "timer: aca work\n");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301574 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301575 }
1576
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301577out:
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301578 if (!test_bit(ID, &motg->inputs) || test_bit(ID_A, &motg->inputs))
1579 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
1580}
1581
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301582static bool msm_chg_check_secondary_det(struct msm_otg *motg)
1583{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001584 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301585 u32 chg_det;
1586 bool ret = false;
1587
1588 switch (motg->pdata->phy_type) {
1589 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001590 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301591 ret = chg_det & (1 << 4);
1592 break;
1593 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001594 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301595 ret = chg_det & 1;
1596 break;
1597 default:
1598 break;
1599 }
1600 return ret;
1601}
1602
1603static void msm_chg_enable_secondary_det(struct msm_otg *motg)
1604{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001605 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301606 u32 chg_det;
1607
1608 switch (motg->pdata->phy_type) {
1609 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001610 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301611 /* Turn off charger block */
1612 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001613 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301614 udelay(20);
1615 /* control chg block via ULPI */
1616 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001617 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301618 /* put it in host mode for enabling D- source */
1619 chg_det &= ~(1 << 2);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001620 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301621 /* Turn on chg detect block */
1622 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001623 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301624 udelay(20);
1625 /* enable chg detection */
1626 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001627 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301628 break;
1629 case SNPS_28NM_INTEGRATED_PHY:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301630 /* Turn off VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001631 ulpi_write(phy, 0x3, 0x86);
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301632 msleep(20);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301633 /*
1634 * Configure DM as current source, DP as current sink
1635 * and enable battery charging comparators.
1636 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001637 ulpi_write(phy, 0x8, 0x85);
1638 ulpi_write(phy, 0x2, 0x85);
1639 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301640 break;
1641 default:
1642 break;
1643 }
1644}
1645
1646static bool msm_chg_check_primary_det(struct msm_otg *motg)
1647{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001648 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301649 u32 chg_det;
1650 bool ret = false;
1651
1652 switch (motg->pdata->phy_type) {
1653 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001654 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301655 ret = chg_det & (1 << 4);
1656 break;
1657 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001658 chg_det = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301659 ret = chg_det & 1;
1660 break;
1661 default:
1662 break;
1663 }
1664 return ret;
1665}
1666
1667static void msm_chg_enable_primary_det(struct msm_otg *motg)
1668{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001669 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301670 u32 chg_det;
1671
1672 switch (motg->pdata->phy_type) {
1673 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001674 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301675 /* enable chg detection */
1676 chg_det &= ~(1 << 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001677 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301678 break;
1679 case SNPS_28NM_INTEGRATED_PHY:
1680 /*
1681 * Configure DP as current source, DM as current sink
1682 * and enable battery charging comparators.
1683 */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001684 ulpi_write(phy, 0x2, 0x85);
1685 ulpi_write(phy, 0x1, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301686 break;
1687 default:
1688 break;
1689 }
1690}
1691
1692static bool msm_chg_check_dcd(struct msm_otg *motg)
1693{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001694 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301695 u32 line_state;
1696 bool ret = false;
1697
1698 switch (motg->pdata->phy_type) {
1699 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001700 line_state = ulpi_read(phy, 0x15);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301701 ret = !(line_state & 1);
1702 break;
1703 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001704 line_state = ulpi_read(phy, 0x87);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301705 ret = line_state & 2;
1706 break;
1707 default:
1708 break;
1709 }
1710 return ret;
1711}
1712
1713static void msm_chg_disable_dcd(struct msm_otg *motg)
1714{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001715 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301716 u32 chg_det;
1717
1718 switch (motg->pdata->phy_type) {
1719 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001720 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301721 chg_det &= ~(1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001722 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301723 break;
1724 case SNPS_28NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001725 ulpi_write(phy, 0x10, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301726 break;
1727 default:
1728 break;
1729 }
1730}
1731
1732static void msm_chg_enable_dcd(struct msm_otg *motg)
1733{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001734 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301735 u32 chg_det;
1736
1737 switch (motg->pdata->phy_type) {
1738 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001739 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301740 /* Turn on D+ current source */
1741 chg_det |= (1 << 5);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001742 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301743 break;
1744 case SNPS_28NM_INTEGRATED_PHY:
1745 /* Data contact detection enable */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001746 ulpi_write(phy, 0x10, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301747 break;
1748 default:
1749 break;
1750 }
1751}
1752
1753static void msm_chg_block_on(struct msm_otg *motg)
1754{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001755 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301756 u32 func_ctrl, chg_det;
1757
1758 /* put the controller in non-driving mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001759 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301760 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1761 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NONDRIVING;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001762 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301763
1764 switch (motg->pdata->phy_type) {
1765 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001766 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301767 /* control chg block via ULPI */
1768 chg_det &= ~(1 << 3);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001769 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301770 /* Turn on chg detect block */
1771 chg_det &= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001772 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301773 udelay(20);
1774 break;
1775 case SNPS_28NM_INTEGRATED_PHY:
1776 /* Clear charger detecting control bits */
Steve Mucklef132c6c2012-06-06 18:30:57 -07001777 ulpi_write(phy, 0x1F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301778 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001779 ulpi_write(phy, 0x1F, 0x92);
1780 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301781 udelay(100);
1782 break;
1783 default:
1784 break;
1785 }
1786}
1787
1788static void msm_chg_block_off(struct msm_otg *motg)
1789{
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001790 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301791 u32 func_ctrl, chg_det;
1792
1793 switch (motg->pdata->phy_type) {
1794 case CI_45NM_INTEGRATED_PHY:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001795 chg_det = ulpi_read(phy, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301796 /* Turn off charger block */
1797 chg_det |= ~(1 << 1);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001798 ulpi_write(phy, chg_det, 0x34);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301799 break;
1800 case SNPS_28NM_INTEGRATED_PHY:
1801 /* Clear charger detecting control bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001802 ulpi_write(phy, 0x3F, 0x86);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301803 /* Clear alt interrupt latch and enable bits */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001804 ulpi_write(phy, 0x1F, 0x92);
1805 ulpi_write(phy, 0x1F, 0x95);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301806 break;
1807 default:
1808 break;
1809 }
1810
1811 /* put the controller in normal mode */
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001812 func_ctrl = ulpi_read(phy, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301813 func_ctrl &= ~ULPI_FUNC_CTRL_OPMODE_MASK;
1814 func_ctrl |= ULPI_FUNC_CTRL_OPMODE_NORMAL;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001815 ulpi_write(phy, func_ctrl, ULPI_FUNC_CTRL);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301816}
1817
Anji jonnalad270e2d2011-08-09 11:28:32 +05301818static const char *chg_to_string(enum usb_chg_type chg_type)
1819{
1820 switch (chg_type) {
1821 case USB_SDP_CHARGER: return "USB_SDP_CHARGER";
1822 case USB_DCP_CHARGER: return "USB_DCP_CHARGER";
1823 case USB_CDP_CHARGER: return "USB_CDP_CHARGER";
1824 case USB_ACA_A_CHARGER: return "USB_ACA_A_CHARGER";
1825 case USB_ACA_B_CHARGER: return "USB_ACA_B_CHARGER";
1826 case USB_ACA_C_CHARGER: return "USB_ACA_C_CHARGER";
1827 case USB_ACA_DOCK_CHARGER: return "USB_ACA_DOCK_CHARGER";
1828 default: return "INVALID_CHARGER";
1829 }
1830}
1831
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301832#define MSM_CHG_DCD_POLL_TIME (100 * HZ/1000) /* 100 msec */
1833#define MSM_CHG_DCD_MAX_RETRIES 6 /* Tdcd_tmout = 6 * 100 msec */
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05301834#define MSM_CHG_PRIMARY_DET_TIME (50 * HZ/1000) /* TVDPSRC_ON */
1835#define MSM_CHG_SECONDARY_DET_TIME (50 * HZ/1000) /* TVDMSRC_ON */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301836static void msm_chg_detect_work(struct work_struct *w)
1837{
1838 struct msm_otg *motg = container_of(w, struct msm_otg, chg_work.work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001839 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301840 bool is_dcd = false, tmout, vout, is_aca;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301841 unsigned long delay;
1842
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02001843 dev_dbg(phy->dev, "chg detection work\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301844 switch (motg->chg_state) {
1845 case USB_CHG_STATE_UNDEFINED:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301846 msm_chg_block_on(motg);
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301847 if (motg->pdata->enable_dcd)
1848 msm_chg_enable_dcd(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001849 msm_chg_enable_aca_det(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301850 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1851 motg->dcd_retries = 0;
1852 delay = MSM_CHG_DCD_POLL_TIME;
1853 break;
1854 case USB_CHG_STATE_WAIT_FOR_DCD:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001855 is_aca = msm_chg_aca_detect(motg);
1856 if (is_aca) {
1857 /*
1858 * ID_A can be ACA dock too. continue
1859 * primary detection after DCD.
1860 */
1861 if (test_bit(ID_A, &motg->inputs)) {
1862 motg->chg_state = USB_CHG_STATE_WAIT_FOR_DCD;
1863 } else {
1864 delay = 0;
1865 break;
1866 }
1867 }
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301868 if (motg->pdata->enable_dcd)
1869 is_dcd = msm_chg_check_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301870 tmout = ++motg->dcd_retries == MSM_CHG_DCD_MAX_RETRIES;
1871 if (is_dcd || tmout) {
Pavankumar Kondeti2d09e5f2012-01-16 08:56:57 +05301872 if (motg->pdata->enable_dcd)
1873 msm_chg_disable_dcd(motg);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301874 msm_chg_enable_primary_det(motg);
1875 delay = MSM_CHG_PRIMARY_DET_TIME;
1876 motg->chg_state = USB_CHG_STATE_DCD_DONE;
1877 } else {
1878 delay = MSM_CHG_DCD_POLL_TIME;
1879 }
1880 break;
1881 case USB_CHG_STATE_DCD_DONE:
1882 vout = msm_chg_check_primary_det(motg);
1883 if (vout) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301884 if (test_bit(ID_A, &motg->inputs)) {
1885 motg->chg_type = USB_ACA_DOCK_CHARGER;
1886 motg->chg_state = USB_CHG_STATE_DETECTED;
1887 delay = 0;
1888 break;
1889 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301890 msm_chg_enable_secondary_det(motg);
1891 delay = MSM_CHG_SECONDARY_DET_TIME;
1892 motg->chg_state = USB_CHG_STATE_PRIMARY_DONE;
1893 } else {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05301894 if (test_bit(ID_A, &motg->inputs)) {
1895 motg->chg_type = USB_ACA_A_CHARGER;
1896 motg->chg_state = USB_CHG_STATE_DETECTED;
1897 delay = 0;
1898 break;
1899 }
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301900 motg->chg_type = USB_SDP_CHARGER;
1901 motg->chg_state = USB_CHG_STATE_DETECTED;
1902 delay = 0;
1903 }
1904 break;
1905 case USB_CHG_STATE_PRIMARY_DONE:
1906 vout = msm_chg_check_secondary_det(motg);
1907 if (vout)
1908 motg->chg_type = USB_DCP_CHARGER;
1909 else
1910 motg->chg_type = USB_CDP_CHARGER;
1911 motg->chg_state = USB_CHG_STATE_SECONDARY_DONE;
1912 /* fall through */
1913 case USB_CHG_STATE_SECONDARY_DONE:
1914 motg->chg_state = USB_CHG_STATE_DETECTED;
1915 case USB_CHG_STATE_DETECTED:
1916 msm_chg_block_off(motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001917 msm_chg_enable_aca_det(motg);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301918 /*
1919 * Spurious interrupt is seen after enabling ACA detection
1920 * due to which charger detection fails in case of PET.
1921 * Add delay of 100 microsec to avoid that.
1922 */
1923 udelay(100);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001924 msm_chg_enable_aca_intr(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07001925 dev_dbg(phy->dev, "chg_type = %s\n",
Anji jonnalad270e2d2011-08-09 11:28:32 +05301926 chg_to_string(motg->chg_type));
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301927 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301928 return;
1929 default:
1930 return;
1931 }
1932
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301933 queue_delayed_work(system_nrt_wq, &motg->chg_work, delay);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05301934}
1935
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301936/*
1937 * We support OTG, Peripheral only and Host only configurations. In case
1938 * of OTG, mode switch (host-->peripheral/peripheral-->host) can happen
1939 * via Id pin status or user request (debugfs). Id/BSV interrupts are not
1940 * enabled when switch is controlled by user and default mode is supplied
1941 * by board file, which can be changed by userspace later.
1942 */
1943static void msm_otg_init_sm(struct msm_otg *motg)
1944{
1945 struct msm_otg_platform_data *pdata = motg->pdata;
1946 u32 otgsc = readl(USB_OTGSC);
1947
1948 switch (pdata->mode) {
1949 case USB_OTG:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001950 if (pdata->otg_control == OTG_USER_CONTROL) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301951 if (pdata->default_mode == USB_HOST) {
1952 clear_bit(ID, &motg->inputs);
1953 } else if (pdata->default_mode == USB_PERIPHERAL) {
1954 set_bit(ID, &motg->inputs);
1955 set_bit(B_SESS_VLD, &motg->inputs);
1956 } else {
1957 set_bit(ID, &motg->inputs);
1958 clear_bit(B_SESS_VLD, &motg->inputs);
1959 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301960 } else if (pdata->otg_control == OTG_PHY_CONTROL) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301961 if (otgsc & OTGSC_ID) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301962 set_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301963 } else {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301964 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05301965 set_bit(A_BUS_REQ, &motg->inputs);
1966 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001967 if (otgsc & OTGSC_BSV)
1968 set_bit(B_SESS_VLD, &motg->inputs);
1969 else
1970 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301971 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301972 if (pdata->pmic_id_irq) {
Stephen Boyd431771e2012-04-18 20:00:23 -07001973 unsigned long flags;
1974 local_irq_save(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301975 if (irq_read_line(pdata->pmic_id_irq))
1976 set_bit(ID, &motg->inputs);
1977 else
1978 clear_bit(ID, &motg->inputs);
Stephen Boyd431771e2012-04-18 20:00:23 -07001979 local_irq_restore(flags);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301980 }
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05301981 /*
1982 * VBUS initial state is reported after PMIC
1983 * driver initialization. Wait for it.
1984 */
1985 wait_for_completion(&pmic_vbus_init);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05301986 }
1987 break;
1988 case USB_HOST:
1989 clear_bit(ID, &motg->inputs);
1990 break;
1991 case USB_PERIPHERAL:
1992 set_bit(ID, &motg->inputs);
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05301993 if (pdata->otg_control == OTG_PHY_CONTROL) {
1994 if (otgsc & OTGSC_BSV)
1995 set_bit(B_SESS_VLD, &motg->inputs);
1996 else
1997 clear_bit(B_SESS_VLD, &motg->inputs);
1998 } else if (pdata->otg_control == OTG_PMIC_CONTROL) {
1999 /*
2000 * VBUS initial state is reported after PMIC
2001 * driver initialization. Wait for it.
2002 */
2003 wait_for_completion(&pmic_vbus_init);
2004 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302005 break;
2006 default:
2007 break;
2008 }
2009}
2010
2011static void msm_otg_sm_work(struct work_struct *w)
2012{
2013 struct msm_otg *motg = container_of(w, struct msm_otg, sm_work);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002014 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302015 bool work = 0, srp_reqd;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302016
Steve Mucklef132c6c2012-06-06 18:30:57 -07002017 pm_runtime_resume(otg->phy->dev);
2018 pr_debug("%s work\n", otg_state_string(otg->phy->state));
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002019 switch (otg->phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302020 case OTG_STATE_UNDEFINED:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002021 msm_otg_reset(otg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302022 msm_otg_init_sm(motg);
Vijayavardhan Vennapusa05c437c2012-05-25 16:20:46 +05302023 psy = power_supply_get_by_name("usb");
2024 if (!psy)
2025 pr_err("couldn't get usb power supply\n");
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002026 otg->phy->state = OTG_STATE_B_IDLE;
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302027 if (!test_bit(B_SESS_VLD, &motg->inputs) &&
2028 test_bit(ID, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002029 pm_runtime_put_noidle(otg->phy->dev);
2030 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondeti8a379b42011-12-12 13:07:23 +05302031 break;
2032 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302033 /* FALL THROUGH */
2034 case OTG_STATE_B_IDLE:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 if ((!test_bit(ID, &motg->inputs) ||
2036 test_bit(ID_A, &motg->inputs)) && otg->host) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302037 pr_debug("!id || id_A\n");
2038 clear_bit(B_BUS_REQ, &motg->inputs);
2039 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002040 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302041 work = 1;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302042 } else if (test_bit(B_SESS_VLD, &motg->inputs)) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302043 pr_debug("b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302044 switch (motg->chg_state) {
2045 case USB_CHG_STATE_UNDEFINED:
2046 msm_chg_detect_work(&motg->chg_work.work);
2047 break;
2048 case USB_CHG_STATE_DETECTED:
2049 switch (motg->chg_type) {
2050 case USB_DCP_CHARGER:
Pavankumar Kondeti283146f2012-01-12 12:51:19 +05302051 /* Enable VDP_SRC */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002052 ulpi_write(otg->phy, 0x2, 0x85);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302053 msm_otg_notify_charger(motg,
2054 IDEV_CHG_MAX);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002055 pm_runtime_put_noidle(otg->phy->dev);
2056 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302057 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302058 case USB_ACA_B_CHARGER:
2059 msm_otg_notify_charger(motg,
2060 IDEV_ACA_CHG_MAX);
2061 /*
2062 * (ID_B --> ID_C) PHY_ALT interrupt can
2063 * not be detected in LPM.
2064 */
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302065 break;
2066 case USB_CDP_CHARGER:
2067 msm_otg_notify_charger(motg,
2068 IDEV_CHG_MAX);
2069 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002070 otg->phy->state =
2071 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302072 break;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302073 case USB_ACA_C_CHARGER:
2074 msm_otg_notify_charger(motg,
2075 IDEV_ACA_CHG_MAX);
2076 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002077 otg->phy->state =
2078 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302079 break;
2080 case USB_SDP_CHARGER:
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302081 msm_otg_start_peripheral(otg, 1);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002082 otg->phy->state =
2083 OTG_STATE_B_PERIPHERAL;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302084 break;
2085 default:
2086 break;
2087 }
2088 break;
2089 default:
2090 break;
2091 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302092 } else if (test_bit(B_BUS_REQ, &motg->inputs)) {
2093 pr_debug("b_sess_end && b_bus_req\n");
2094 if (msm_otg_start_srp(otg) < 0) {
2095 clear_bit(B_BUS_REQ, &motg->inputs);
2096 work = 1;
2097 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302098 }
Steve Mucklef132c6c2012-06-06 18:30:57 -07002099 otg->phy->state = OTG_STATE_B_SRP_INIT;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302100 msm_otg_start_timer(motg, TB_SRP_FAIL, B_SRP_FAIL);
2101 break;
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302102 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302103 pr_debug("chg_work cancel");
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302104 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302105 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2106 motg->chg_type = USB_INVALID_CHARGER;
Rajkumar Raghupathy18fd7132012-04-20 11:28:13 +05302107 msm_otg_notify_charger(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002108 msm_otg_reset(otg->phy);
2109 pm_runtime_put_noidle(otg->phy->dev);
2110 pm_runtime_suspend(otg->phy->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302111 }
2112 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302113 case OTG_STATE_B_SRP_INIT:
2114 if (!test_bit(ID, &motg->inputs) ||
2115 test_bit(ID_A, &motg->inputs) ||
2116 test_bit(ID_C, &motg->inputs) ||
2117 (test_bit(B_SESS_VLD, &motg->inputs) &&
2118 !test_bit(ID_B, &motg->inputs))) {
2119 pr_debug("!id || id_a/c || b_sess_vld+!id_b\n");
2120 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002121 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302122 /*
2123 * clear VBUSVLDEXTSEL and VBUSVLDEXT register
2124 * bits after SRP initiation.
2125 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002126 ulpi_write(otg->phy, 0x0, 0x98);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302127 work = 1;
2128 } else if (test_bit(B_SRP_FAIL, &motg->tmouts)) {
2129 pr_debug("b_srp_fail\n");
2130 pr_info("A-device did not respond to SRP\n");
2131 clear_bit(B_BUS_REQ, &motg->inputs);
2132 clear_bit(B_SRP_FAIL, &motg->tmouts);
2133 otg_send_event(OTG_EVENT_NO_RESP_FOR_SRP);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002134 ulpi_write(otg->phy, 0x0, 0x98);
2135 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302136 motg->b_last_se0_sess = jiffies;
2137 work = 1;
2138 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302139 break;
2140 case OTG_STATE_B_PERIPHERAL:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302141 if (!test_bit(ID, &motg->inputs) ||
2142 test_bit(ID_A, &motg->inputs) ||
2143 test_bit(ID_B, &motg->inputs) ||
2144 !test_bit(B_SESS_VLD, &motg->inputs)) {
2145 pr_debug("!id || id_a/b || !b_sess_vld\n");
Pavankumar Kondetid8608522011-05-04 10:19:47 +05302146 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2147 motg->chg_type = USB_INVALID_CHARGER;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302148 msm_otg_notify_charger(motg, 0);
2149 srp_reqd = otg->gadget->otg_srp_reqd;
2150 msm_otg_start_peripheral(otg, 0);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302151 if (test_bit(ID_B, &motg->inputs))
2152 clear_bit(ID_B, &motg->inputs);
2153 clear_bit(B_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002154 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302155 motg->b_last_se0_sess = jiffies;
2156 if (srp_reqd)
2157 msm_otg_start_timer(motg,
2158 TB_TST_SRP, B_TST_SRP);
2159 else
2160 work = 1;
2161 } else if (test_bit(B_BUS_REQ, &motg->inputs) &&
2162 otg->gadget->b_hnp_enable &&
2163 test_bit(A_BUS_SUSPEND, &motg->inputs)) {
2164 pr_debug("b_bus_req && b_hnp_en && a_bus_suspend\n");
2165 msm_otg_start_timer(motg, TB_ASE0_BRST, B_ASE0_BRST);
2166 /* D+ pullup should not be disconnected within 4msec
2167 * after A device suspends the bus. Otherwise PET will
2168 * fail the compliance test.
2169 */
2170 udelay(1000);
2171 msm_otg_start_peripheral(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002172 otg->phy->state = OTG_STATE_B_WAIT_ACON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302173 /*
2174 * start HCD even before A-device enable
2175 * pull-up to meet HNP timings.
2176 */
2177 otg->host->is_b_host = 1;
2178 msm_otg_start_host(otg, 1);
Amit Blay6fa647a2012-05-24 14:12:08 +03002179 } else if (test_bit(A_BUS_SUSPEND, &motg->inputs) &&
2180 test_bit(B_SESS_VLD, &motg->inputs)) {
2181 pr_debug("a_bus_suspend && b_sess_vld\n");
2182 if (motg->caps & ALLOW_LPM_ON_DEV_SUSPEND) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002183 pm_runtime_put_noidle(otg->phy->dev);
2184 pm_runtime_suspend(otg->phy->dev);
Amit Blay6fa647a2012-05-24 14:12:08 +03002185 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 } else if (test_bit(ID_C, &motg->inputs)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302187 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002188 }
2189 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302190 case OTG_STATE_B_WAIT_ACON:
2191 if (!test_bit(ID, &motg->inputs) ||
2192 test_bit(ID_A, &motg->inputs) ||
2193 test_bit(ID_B, &motg->inputs) ||
2194 !test_bit(B_SESS_VLD, &motg->inputs)) {
2195 pr_debug("!id || id_a/b || !b_sess_vld\n");
2196 msm_otg_del_timer(motg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302197 /*
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302198 * A-device is physically disconnected during
2199 * HNP. Remove HCD.
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302200 */
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302201 msm_otg_start_host(otg, 0);
2202 otg->host->is_b_host = 0;
2203
2204 clear_bit(B_BUS_REQ, &motg->inputs);
2205 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2206 motg->b_last_se0_sess = jiffies;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002207 otg->phy->state = OTG_STATE_B_IDLE;
2208 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302209 work = 1;
2210 } else if (test_bit(A_CONN, &motg->inputs)) {
2211 pr_debug("a_conn\n");
2212 clear_bit(A_BUS_SUSPEND, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002213 otg->phy->state = OTG_STATE_B_HOST;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302214 /*
2215 * PET disconnects D+ pullup after reset is generated
2216 * by B device in B_HOST role which is not detected by
2217 * B device. As workaorund , start timer of 300msec
2218 * and stop timer if A device is enumerated else clear
2219 * A_CONN.
2220 */
2221 msm_otg_start_timer(motg, TB_TST_CONFIG,
2222 B_TST_CONFIG);
2223 } else if (test_bit(B_ASE0_BRST, &motg->tmouts)) {
2224 pr_debug("b_ase0_brst_tmout\n");
2225 pr_info("B HNP fail:No response from A device\n");
2226 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002227 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302228 otg->host->is_b_host = 0;
2229 clear_bit(B_ASE0_BRST, &motg->tmouts);
2230 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2231 clear_bit(B_BUS_REQ, &motg->inputs);
2232 otg_send_event(OTG_EVENT_HNP_FAILED);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002233 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302234 work = 1;
2235 } else if (test_bit(ID_C, &motg->inputs)) {
2236 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2237 }
2238 break;
2239 case OTG_STATE_B_HOST:
2240 if (!test_bit(B_BUS_REQ, &motg->inputs) ||
2241 !test_bit(A_CONN, &motg->inputs) ||
2242 !test_bit(B_SESS_VLD, &motg->inputs)) {
2243 pr_debug("!b_bus_req || !a_conn || !b_sess_vld\n");
2244 clear_bit(A_CONN, &motg->inputs);
2245 clear_bit(B_BUS_REQ, &motg->inputs);
2246 msm_otg_start_host(otg, 0);
2247 otg->host->is_b_host = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002248 otg->phy->state = OTG_STATE_B_IDLE;
2249 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302250 work = 1;
2251 } else if (test_bit(ID_C, &motg->inputs)) {
2252 msm_otg_notify_charger(motg, IDEV_ACA_CHG_MAX);
2253 }
2254 break;
2255 case OTG_STATE_A_IDLE:
2256 otg->default_a = 1;
2257 if (test_bit(ID, &motg->inputs) &&
2258 !test_bit(ID_A, &motg->inputs)) {
2259 pr_debug("id && !id_a\n");
2260 otg->default_a = 0;
2261 clear_bit(A_BUS_DROP, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002262 otg->phy->state = OTG_STATE_B_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302263 del_timer_sync(&motg->id_timer);
2264 msm_otg_link_reset(motg);
2265 msm_chg_enable_aca_intr(motg);
2266 msm_otg_notify_charger(motg, 0);
2267 work = 1;
2268 } else if (!test_bit(A_BUS_DROP, &motg->inputs) &&
2269 (test_bit(A_SRP_DET, &motg->inputs) ||
2270 test_bit(A_BUS_REQ, &motg->inputs))) {
2271 pr_debug("!a_bus_drop && (a_srp_det || a_bus_req)\n");
2272
2273 clear_bit(A_SRP_DET, &motg->inputs);
2274 /* Disable SRP detection */
2275 writel_relaxed((readl_relaxed(USB_OTGSC) &
2276 ~OTGSC_INTSTS_MASK) &
2277 ~OTGSC_DPIE, USB_OTGSC);
2278
Steve Mucklef132c6c2012-06-06 18:30:57 -07002279 otg->phy->state = OTG_STATE_A_WAIT_VRISE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302280 /* VBUS should not be supplied before end of SRP pulse
2281 * generated by PET, if not complaince test fail.
2282 */
2283 usleep_range(10000, 12000);
2284 /* ACA: ID_A: Stop charging untill enumeration */
2285 if (test_bit(ID_A, &motg->inputs))
2286 msm_otg_notify_charger(motg, 0);
2287 else
2288 msm_hsusb_vbus_power(motg, 1);
2289 msm_otg_start_timer(motg, TA_WAIT_VRISE, A_WAIT_VRISE);
2290 } else {
2291 pr_debug("No session requested\n");
2292 clear_bit(A_BUS_DROP, &motg->inputs);
2293 if (test_bit(ID_A, &motg->inputs)) {
2294 msm_otg_notify_charger(motg,
2295 IDEV_ACA_CHG_MAX);
2296 } else if (!test_bit(ID, &motg->inputs)) {
2297 msm_otg_notify_charger(motg, 0);
2298 /*
2299 * A-device is not providing power on VBUS.
2300 * Enable SRP detection.
2301 */
2302 writel_relaxed(0x13, USB_USBMODE);
2303 writel_relaxed((readl_relaxed(USB_OTGSC) &
2304 ~OTGSC_INTSTS_MASK) |
2305 OTGSC_DPIE, USB_OTGSC);
2306 mb();
2307 }
2308 }
2309 break;
2310 case OTG_STATE_A_WAIT_VRISE:
2311 if ((test_bit(ID, &motg->inputs) &&
2312 !test_bit(ID_A, &motg->inputs)) ||
2313 test_bit(A_BUS_DROP, &motg->inputs) ||
2314 test_bit(A_WAIT_VRISE, &motg->tmouts)) {
2315 pr_debug("id || a_bus_drop || a_wait_vrise_tmout\n");
2316 clear_bit(A_BUS_REQ, &motg->inputs);
2317 msm_otg_del_timer(motg);
2318 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002319 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302320 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2321 } else if (test_bit(A_VBUS_VLD, &motg->inputs)) {
2322 pr_debug("a_vbus_vld\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002323 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302324 if (TA_WAIT_BCON > 0)
2325 msm_otg_start_timer(motg, TA_WAIT_BCON,
2326 A_WAIT_BCON);
2327 msm_otg_start_host(otg, 1);
2328 msm_chg_enable_aca_det(motg);
2329 msm_chg_disable_aca_intr(motg);
Chiranjeevi Velempati489a27c2012-03-29 09:47:17 +05302330 mod_timer(&motg->id_timer, ID_TIMER_FREQ);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302331 if (msm_chg_check_aca_intr(motg))
2332 work = 1;
2333 }
2334 break;
2335 case OTG_STATE_A_WAIT_BCON:
2336 if ((test_bit(ID, &motg->inputs) &&
2337 !test_bit(ID_A, &motg->inputs)) ||
2338 test_bit(A_BUS_DROP, &motg->inputs) ||
2339 test_bit(A_WAIT_BCON, &motg->tmouts)) {
2340 pr_debug("(id && id_a/b/c) || a_bus_drop ||"
2341 "a_wait_bcon_tmout\n");
2342 if (test_bit(A_WAIT_BCON, &motg->tmouts)) {
2343 pr_info("Device No Response\n");
2344 otg_send_event(OTG_EVENT_DEV_CONN_TMOUT);
2345 }
2346 msm_otg_del_timer(motg);
2347 clear_bit(A_BUS_REQ, &motg->inputs);
2348 clear_bit(B_CONN, &motg->inputs);
2349 msm_otg_start_host(otg, 0);
2350 /*
2351 * ACA: ID_A with NO accessory, just the A plug is
2352 * attached to ACA: Use IDCHG_MAX for charging
2353 */
2354 if (test_bit(ID_A, &motg->inputs))
2355 msm_otg_notify_charger(motg, IDEV_CHG_MIN);
2356 else
2357 msm_hsusb_vbus_power(motg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002358 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302359 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2360 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2361 pr_debug("!a_vbus_vld\n");
2362 clear_bit(B_CONN, &motg->inputs);
2363 msm_otg_del_timer(motg);
2364 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002365 otg->phy->state = OTG_STATE_A_VBUS_ERR;
2366 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302367 } else if (test_bit(ID_A, &motg->inputs)) {
2368 msm_hsusb_vbus_power(motg, 0);
2369 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2370 /*
2371 * If TA_WAIT_BCON is infinite, we don;t
2372 * turn off VBUS. Enter low power mode.
2373 */
2374 if (TA_WAIT_BCON < 0)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002375 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302376 } else if (!test_bit(ID, &motg->inputs)) {
2377 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302378 }
2379 break;
2380 case OTG_STATE_A_HOST:
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302381 if ((test_bit(ID, &motg->inputs) &&
2382 !test_bit(ID_A, &motg->inputs)) ||
2383 test_bit(A_BUS_DROP, &motg->inputs)) {
2384 pr_debug("id_a/b/c || a_bus_drop\n");
2385 clear_bit(B_CONN, &motg->inputs);
2386 clear_bit(A_BUS_REQ, &motg->inputs);
2387 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002388 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302389 msm_otg_start_host(otg, 0);
2390 if (!test_bit(ID_A, &motg->inputs))
2391 msm_hsusb_vbus_power(motg, 0);
2392 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2393 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2394 pr_debug("!a_vbus_vld\n");
2395 clear_bit(B_CONN, &motg->inputs);
2396 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002397 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302398 msm_otg_start_host(otg, 0);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02002399 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302400 } else if (!test_bit(A_BUS_REQ, &motg->inputs)) {
2401 /*
2402 * a_bus_req is de-asserted when root hub is
2403 * suspended or HNP is in progress.
2404 */
2405 pr_debug("!a_bus_req\n");
2406 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002407 otg->phy->state = OTG_STATE_A_SUSPEND;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302408 if (otg->host->b_hnp_enable)
2409 msm_otg_start_timer(motg, TA_AIDL_BDIS,
2410 A_AIDL_BDIS);
2411 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002412 pm_runtime_put_sync(otg->phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302413 } else if (!test_bit(B_CONN, &motg->inputs)) {
2414 pr_debug("!b_conn\n");
2415 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002416 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302417 if (TA_WAIT_BCON > 0)
2418 msm_otg_start_timer(motg, TA_WAIT_BCON,
2419 A_WAIT_BCON);
2420 if (msm_chg_check_aca_intr(motg))
2421 work = 1;
2422 } else if (test_bit(ID_A, &motg->inputs)) {
2423 msm_otg_del_timer(motg);
2424 msm_hsusb_vbus_power(motg, 0);
2425 if (motg->chg_type == USB_ACA_DOCK_CHARGER)
2426 msm_otg_notify_charger(motg,
2427 IDEV_ACA_CHG_MAX);
2428 else
2429 msm_otg_notify_charger(motg,
2430 IDEV_CHG_MIN - motg->mA_port);
2431 } else if (!test_bit(ID, &motg->inputs)) {
2432 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2433 motg->chg_type = USB_INVALID_CHARGER;
2434 msm_otg_notify_charger(motg, 0);
2435 msm_hsusb_vbus_power(motg, 1);
2436 }
2437 break;
2438 case OTG_STATE_A_SUSPEND:
2439 if ((test_bit(ID, &motg->inputs) &&
2440 !test_bit(ID_A, &motg->inputs)) ||
2441 test_bit(A_BUS_DROP, &motg->inputs) ||
2442 test_bit(A_AIDL_BDIS, &motg->tmouts)) {
2443 pr_debug("id_a/b/c || a_bus_drop ||"
2444 "a_aidl_bdis_tmout\n");
2445 msm_otg_del_timer(motg);
2446 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002447 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302448 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002449 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302450 if (!test_bit(ID_A, &motg->inputs))
2451 msm_hsusb_vbus_power(motg, 0);
2452 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2453 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2454 pr_debug("!a_vbus_vld\n");
2455 msm_otg_del_timer(motg);
2456 clear_bit(B_CONN, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002457 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302458 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002459 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302460 } else if (!test_bit(B_CONN, &motg->inputs) &&
2461 otg->host->b_hnp_enable) {
2462 pr_debug("!b_conn && b_hnp_enable");
Steve Mucklef132c6c2012-06-06 18:30:57 -07002463 otg->phy->state = OTG_STATE_A_PERIPHERAL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302464 msm_otg_host_hnp_enable(otg, 1);
2465 otg->gadget->is_a_peripheral = 1;
2466 msm_otg_start_peripheral(otg, 1);
2467 } else if (!test_bit(B_CONN, &motg->inputs) &&
2468 !otg->host->b_hnp_enable) {
2469 pr_debug("!b_conn && !b_hnp_enable");
2470 /*
2471 * bus request is dropped during suspend.
2472 * acquire again for next device.
2473 */
2474 set_bit(A_BUS_REQ, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002475 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302476 if (TA_WAIT_BCON > 0)
2477 msm_otg_start_timer(motg, TA_WAIT_BCON,
2478 A_WAIT_BCON);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002479 } else if (test_bit(ID_A, &motg->inputs)) {
Mayank Ranae3926882011-12-26 09:47:54 +05302480 msm_hsusb_vbus_power(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002481 msm_otg_notify_charger(motg,
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302482 IDEV_CHG_MIN - motg->mA_port);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002483 } else if (!test_bit(ID, &motg->inputs)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002484 msm_otg_notify_charger(motg, 0);
Mayank Ranae3926882011-12-26 09:47:54 +05302485 msm_hsusb_vbus_power(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302486 }
2487 break;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302488 case OTG_STATE_A_PERIPHERAL:
2489 if ((test_bit(ID, &motg->inputs) &&
2490 !test_bit(ID_A, &motg->inputs)) ||
2491 test_bit(A_BUS_DROP, &motg->inputs)) {
2492 pr_debug("id _f/b/c || a_bus_drop\n");
2493 /* Clear BIDL_ADIS timer */
2494 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002495 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302496 msm_otg_start_peripheral(otg, 0);
2497 otg->gadget->is_a_peripheral = 0;
2498 msm_otg_start_host(otg, 0);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002499 msm_otg_reset(otg->phy);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302500 if (!test_bit(ID_A, &motg->inputs))
2501 msm_hsusb_vbus_power(motg, 0);
2502 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2503 } else if (!test_bit(A_VBUS_VLD, &motg->inputs)) {
2504 pr_debug("!a_vbus_vld\n");
2505 /* Clear BIDL_ADIS timer */
2506 msm_otg_del_timer(motg);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002507 otg->phy->state = OTG_STATE_A_VBUS_ERR;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302508 msm_otg_start_peripheral(otg, 0);
2509 otg->gadget->is_a_peripheral = 0;
2510 msm_otg_start_host(otg, 0);
2511 } else if (test_bit(A_BIDL_ADIS, &motg->tmouts)) {
2512 pr_debug("a_bidl_adis_tmout\n");
2513 msm_otg_start_peripheral(otg, 0);
2514 otg->gadget->is_a_peripheral = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002515 otg->phy->state = OTG_STATE_A_WAIT_BCON;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302516 set_bit(A_BUS_REQ, &motg->inputs);
2517 msm_otg_host_hnp_enable(otg, 0);
2518 if (TA_WAIT_BCON > 0)
2519 msm_otg_start_timer(motg, TA_WAIT_BCON,
2520 A_WAIT_BCON);
2521 } else if (test_bit(ID_A, &motg->inputs)) {
2522 msm_hsusb_vbus_power(motg, 0);
2523 msm_otg_notify_charger(motg,
2524 IDEV_CHG_MIN - motg->mA_port);
2525 } else if (!test_bit(ID, &motg->inputs)) {
2526 msm_otg_notify_charger(motg, 0);
2527 msm_hsusb_vbus_power(motg, 1);
2528 }
2529 break;
2530 case OTG_STATE_A_WAIT_VFALL:
2531 if (test_bit(A_WAIT_VFALL, &motg->tmouts)) {
2532 clear_bit(A_VBUS_VLD, &motg->inputs);
Steve Mucklef132c6c2012-06-06 18:30:57 -07002533 otg->phy->state = OTG_STATE_A_IDLE;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302534 work = 1;
2535 }
2536 break;
2537 case OTG_STATE_A_VBUS_ERR:
2538 if ((test_bit(ID, &motg->inputs) &&
2539 !test_bit(ID_A, &motg->inputs)) ||
2540 test_bit(A_BUS_DROP, &motg->inputs) ||
2541 test_bit(A_CLR_ERR, &motg->inputs)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07002542 otg->phy->state = OTG_STATE_A_WAIT_VFALL;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302543 if (!test_bit(ID_A, &motg->inputs))
2544 msm_hsusb_vbus_power(motg, 0);
2545 msm_otg_start_timer(motg, TA_WAIT_VFALL, A_WAIT_VFALL);
2546 motg->chg_state = USB_CHG_STATE_UNDEFINED;
2547 motg->chg_type = USB_INVALID_CHARGER;
2548 msm_otg_notify_charger(motg, 0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302549 }
2550 break;
2551 default:
2552 break;
2553 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302554 if (work)
2555 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302556}
2557
2558static irqreturn_t msm_otg_irq(int irq, void *data)
2559{
2560 struct msm_otg *motg = data;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002561 struct usb_otg *otg = motg->phy.otg;
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302562 u32 otgsc = 0, usbsts, pc;
2563 bool work = 0;
2564 irqreturn_t ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302565
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302566 if (atomic_read(&motg->in_lpm)) {
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302567 pr_debug("OTG IRQ: in LPM\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302568 disable_irq_nosync(irq);
2569 motg->async_int = 1;
Jack Pham5ca279b2012-05-14 18:42:54 -07002570 if (atomic_read(&motg->pm_suspended))
2571 motg->sm_work_pending = true;
2572 else
Steve Mucklef132c6c2012-06-06 18:30:57 -07002573 pm_request_resume(otg->phy->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05302574 return IRQ_HANDLED;
2575 }
2576
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002577 usbsts = readl(USB_USBSTS);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302578 otgsc = readl(USB_OTGSC);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302579
2580 if (!(otgsc & OTG_OTGSTS_MASK) && !(usbsts & OTG_USBSTS_MASK))
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302581 return IRQ_NONE;
2582
2583 if ((otgsc & OTGSC_IDIS) && (otgsc & OTGSC_IDIE)) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302584 if (otgsc & OTGSC_ID) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302585 pr_debug("Id set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302586 set_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302587 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302588 pr_debug("Id clear\n");
2589 /*
2590 * Assert a_bus_req to supply power on
2591 * VBUS when Micro/Mini-A cable is connected
2592 * with out user intervention.
2593 */
2594 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302595 clear_bit(ID, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302596 msm_chg_enable_aca_det(motg);
2597 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302598 writel_relaxed(otgsc, USB_OTGSC);
2599 work = 1;
2600 } else if (otgsc & OTGSC_DPIS) {
2601 pr_debug("DPIS detected\n");
2602 writel_relaxed(otgsc, USB_OTGSC);
2603 set_bit(A_SRP_DET, &motg->inputs);
2604 set_bit(A_BUS_REQ, &motg->inputs);
2605 work = 1;
2606 } else if (otgsc & OTGSC_BSVIS) {
2607 writel_relaxed(otgsc, USB_OTGSC);
2608 /*
2609 * BSV interrupt comes when operating as an A-device
2610 * (VBUS on/off).
2611 * But, handle BSV when charger is removed from ACA in ID_A
2612 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07002613 if ((otg->phy->state >= OTG_STATE_A_IDLE) &&
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302614 !test_bit(ID_A, &motg->inputs))
2615 return IRQ_HANDLED;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302616 if (otgsc & OTGSC_BSV) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302617 pr_debug("BSV set\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302618 set_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302619 } else {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302620 pr_debug("BSV clear\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302621 clear_bit(B_SESS_VLD, &motg->inputs);
Amit Blay6fa647a2012-05-24 14:12:08 +03002622 clear_bit(A_BUS_SUSPEND, &motg->inputs);
2623
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302624 msm_chg_check_aca_intr(motg);
2625 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302626 work = 1;
2627 } else if (usbsts & STS_PCI) {
2628 pc = readl_relaxed(USB_PORTSC);
2629 pr_debug("portsc = %x\n", pc);
2630 ret = IRQ_NONE;
2631 /*
2632 * HCD Acks PCI interrupt. We use this to switch
2633 * between different OTG states.
2634 */
2635 work = 1;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002636 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302637 case OTG_STATE_A_SUSPEND:
2638 if (otg->host->b_hnp_enable && (pc & PORTSC_CSC) &&
2639 !(pc & PORTSC_CCS)) {
2640 pr_debug("B_CONN clear\n");
2641 clear_bit(B_CONN, &motg->inputs);
2642 msm_otg_del_timer(motg);
2643 }
2644 break;
2645 case OTG_STATE_A_PERIPHERAL:
2646 /*
2647 * A-peripheral observed activity on bus.
2648 * clear A_BIDL_ADIS timer.
2649 */
2650 msm_otg_del_timer(motg);
2651 work = 0;
2652 break;
2653 case OTG_STATE_B_WAIT_ACON:
2654 if ((pc & PORTSC_CSC) && (pc & PORTSC_CCS)) {
2655 pr_debug("A_CONN set\n");
2656 set_bit(A_CONN, &motg->inputs);
2657 /* Clear ASE0_BRST timer */
2658 msm_otg_del_timer(motg);
2659 }
2660 break;
2661 case OTG_STATE_B_HOST:
2662 if ((pc & PORTSC_CSC) && !(pc & PORTSC_CCS)) {
2663 pr_debug("A_CONN clear\n");
2664 clear_bit(A_CONN, &motg->inputs);
2665 msm_otg_del_timer(motg);
2666 }
2667 break;
2668 case OTG_STATE_A_WAIT_BCON:
2669 if (TA_WAIT_BCON < 0)
2670 set_bit(A_BUS_REQ, &motg->inputs);
2671 default:
2672 work = 0;
2673 break;
2674 }
2675 } else if (usbsts & STS_URI) {
2676 ret = IRQ_NONE;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002677 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302678 case OTG_STATE_A_PERIPHERAL:
2679 /*
2680 * A-peripheral observed activity on bus.
2681 * clear A_BIDL_ADIS timer.
2682 */
2683 msm_otg_del_timer(motg);
2684 work = 0;
2685 break;
2686 default:
2687 work = 0;
2688 break;
2689 }
2690 } else if (usbsts & STS_SLI) {
2691 ret = IRQ_NONE;
2692 work = 0;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002693 switch (otg->phy->state) {
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302694 case OTG_STATE_B_PERIPHERAL:
2695 if (otg->gadget->b_hnp_enable) {
2696 set_bit(A_BUS_SUSPEND, &motg->inputs);
2697 set_bit(B_BUS_REQ, &motg->inputs);
2698 work = 1;
2699 }
2700 break;
2701 case OTG_STATE_A_PERIPHERAL:
2702 msm_otg_start_timer(motg, TA_BIDL_ADIS,
2703 A_BIDL_ADIS);
2704 break;
2705 default:
2706 break;
2707 }
2708 } else if ((usbsts & PHY_ALT_INT)) {
2709 writel_relaxed(PHY_ALT_INT, USB_USBSTS);
2710 if (msm_chg_check_aca_intr(motg))
2711 work = 1;
2712 ret = IRQ_HANDLED;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302713 }
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302714 if (work)
2715 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302716
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302717 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002718}
2719
2720static void msm_otg_set_vbus_state(int online)
2721{
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302722 static bool init;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002723 struct msm_otg *motg = the_msm_otg;
2724
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302725 if (online) {
2726 pr_debug("PMIC: BSV set\n");
2727 set_bit(B_SESS_VLD, &motg->inputs);
2728 } else {
2729 pr_debug("PMIC: BSV clear\n");
2730 clear_bit(B_SESS_VLD, &motg->inputs);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302731 }
2732
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302733 if (!init) {
2734 init = true;
2735 complete(&pmic_vbus_init);
2736 pr_debug("PMIC: BSV init complete\n");
2737 return;
2738 }
2739
Jack Pham5ca279b2012-05-14 18:42:54 -07002740 if (atomic_read(&motg->pm_suspended))
2741 motg->sm_work_pending = true;
2742 else
2743 queue_work(system_nrt_wq, &motg->sm_work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002744}
2745
2746static irqreturn_t msm_pmic_id_irq(int irq, void *data)
2747{
2748 struct msm_otg *motg = data;
2749
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302750 if (aca_id_turned_on)
2751 return IRQ_HANDLED;
2752
2753 if (irq_read_line(motg->pdata->pmic_id_irq)) {
2754 pr_debug("PMIC: ID set\n");
2755 set_bit(ID, &motg->inputs);
2756 } else {
2757 pr_debug("PMIC: ID clear\n");
2758 clear_bit(ID, &motg->inputs);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302759 set_bit(A_BUS_REQ, &motg->inputs);
Pavankumar Kondeti4960f312011-12-06 15:46:14 +05302760 }
2761
Steve Mucklef132c6c2012-06-06 18:30:57 -07002762 if (motg->phy.state != OTG_STATE_UNDEFINED) {
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);
2767 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002768
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302769 return IRQ_HANDLED;
2770}
2771
2772static int msm_otg_mode_show(struct seq_file *s, void *unused)
2773{
2774 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002775 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302776
Steve Mucklef132c6c2012-06-06 18:30:57 -07002777 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302778 case OTG_STATE_A_HOST:
2779 seq_printf(s, "host\n");
2780 break;
2781 case OTG_STATE_B_PERIPHERAL:
2782 seq_printf(s, "peripheral\n");
2783 break;
2784 default:
2785 seq_printf(s, "none\n");
2786 break;
2787 }
2788
2789 return 0;
2790}
2791
2792static int msm_otg_mode_open(struct inode *inode, struct file *file)
2793{
2794 return single_open(file, msm_otg_mode_show, inode->i_private);
2795}
2796
2797static ssize_t msm_otg_mode_write(struct file *file, const char __user *ubuf,
2798 size_t count, loff_t *ppos)
2799{
Pavankumar Kondetie2904ee2011-02-15 09:42:35 +05302800 struct seq_file *s = file->private_data;
2801 struct msm_otg *motg = s->private;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302802 char buf[16];
Steve Mucklef132c6c2012-06-06 18:30:57 -07002803 struct usb_phy *phy = &motg->phy;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302804 int status = count;
2805 enum usb_mode_type req_mode;
2806
2807 memset(buf, 0x00, sizeof(buf));
2808
2809 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count))) {
2810 status = -EFAULT;
2811 goto out;
2812 }
2813
2814 if (!strncmp(buf, "host", 4)) {
2815 req_mode = USB_HOST;
2816 } else if (!strncmp(buf, "peripheral", 10)) {
2817 req_mode = USB_PERIPHERAL;
2818 } else if (!strncmp(buf, "none", 4)) {
2819 req_mode = USB_NONE;
2820 } else {
2821 status = -EINVAL;
2822 goto out;
2823 }
2824
2825 switch (req_mode) {
2826 case USB_NONE:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002827 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302828 case OTG_STATE_A_HOST:
2829 case OTG_STATE_B_PERIPHERAL:
2830 set_bit(ID, &motg->inputs);
2831 clear_bit(B_SESS_VLD, &motg->inputs);
2832 break;
2833 default:
2834 goto out;
2835 }
2836 break;
2837 case USB_PERIPHERAL:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002838 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302839 case OTG_STATE_B_IDLE:
2840 case OTG_STATE_A_HOST:
2841 set_bit(ID, &motg->inputs);
2842 set_bit(B_SESS_VLD, &motg->inputs);
2843 break;
2844 default:
2845 goto out;
2846 }
2847 break;
2848 case USB_HOST:
Steve Mucklef132c6c2012-06-06 18:30:57 -07002849 switch (phy->state) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302850 case OTG_STATE_B_IDLE:
2851 case OTG_STATE_B_PERIPHERAL:
2852 clear_bit(ID, &motg->inputs);
2853 break;
2854 default:
2855 goto out;
2856 }
2857 break;
2858 default:
2859 goto out;
2860 }
2861
Steve Mucklef132c6c2012-06-06 18:30:57 -07002862 pm_runtime_resume(phy->dev);
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05302863 queue_work(system_nrt_wq, &motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05302864out:
2865 return status;
2866}
2867
2868const struct file_operations msm_otg_mode_fops = {
2869 .open = msm_otg_mode_open,
2870 .read = seq_read,
2871 .write = msm_otg_mode_write,
2872 .llseek = seq_lseek,
2873 .release = single_release,
2874};
2875
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302876static int msm_otg_show_otg_state(struct seq_file *s, void *unused)
2877{
2878 struct msm_otg *motg = s->private;
Steve Mucklef132c6c2012-06-06 18:30:57 -07002879 struct usb_phy *phy = &motg->phy;
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302880
Steve Mucklef132c6c2012-06-06 18:30:57 -07002881 seq_printf(s, "%s\n", otg_state_string(phy->state));
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05302882 return 0;
2883}
2884
2885static int msm_otg_otg_state_open(struct inode *inode, struct file *file)
2886{
2887 return single_open(file, msm_otg_show_otg_state, inode->i_private);
2888}
2889
2890const struct file_operations msm_otg_state_fops = {
2891 .open = msm_otg_otg_state_open,
2892 .read = seq_read,
2893 .llseek = seq_lseek,
2894 .release = single_release,
2895};
2896
Anji jonnalad270e2d2011-08-09 11:28:32 +05302897static int msm_otg_show_chg_type(struct seq_file *s, void *unused)
2898{
2899 struct msm_otg *motg = s->private;
2900
Pavankumar Kondeti9ef69cb2011-12-12 14:18:22 +05302901 seq_printf(s, "%s\n", chg_to_string(motg->chg_type));
Anji jonnalad270e2d2011-08-09 11:28:32 +05302902 return 0;
2903}
2904
2905static int msm_otg_chg_open(struct inode *inode, struct file *file)
2906{
2907 return single_open(file, msm_otg_show_chg_type, inode->i_private);
2908}
2909
2910const struct file_operations msm_otg_chg_fops = {
2911 .open = msm_otg_chg_open,
2912 .read = seq_read,
2913 .llseek = seq_lseek,
2914 .release = single_release,
2915};
2916
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05302917static int msm_otg_aca_show(struct seq_file *s, void *unused)
2918{
2919 if (debug_aca_enabled)
2920 seq_printf(s, "enabled\n");
2921 else
2922 seq_printf(s, "disabled\n");
2923
2924 return 0;
2925}
2926
2927static int msm_otg_aca_open(struct inode *inode, struct file *file)
2928{
2929 return single_open(file, msm_otg_aca_show, inode->i_private);
2930}
2931
2932static ssize_t msm_otg_aca_write(struct file *file, const char __user *ubuf,
2933 size_t count, loff_t *ppos)
2934{
2935 char buf[8];
2936
2937 memset(buf, 0x00, sizeof(buf));
2938
2939 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2940 return -EFAULT;
2941
2942 if (!strncmp(buf, "enable", 6))
2943 debug_aca_enabled = true;
2944 else
2945 debug_aca_enabled = false;
2946
2947 return count;
2948}
2949
2950const struct file_operations msm_otg_aca_fops = {
2951 .open = msm_otg_aca_open,
2952 .read = seq_read,
2953 .write = msm_otg_aca_write,
2954 .llseek = seq_lseek,
2955 .release = single_release,
2956};
2957
Manu Gautam8bdcc592012-03-06 11:26:06 +05302958static int msm_otg_bus_show(struct seq_file *s, void *unused)
2959{
2960 if (debug_bus_voting_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_bus_open(struct inode *inode, struct file *file)
2969{
2970 return single_open(file, msm_otg_bus_show, inode->i_private);
2971}
2972
2973static ssize_t msm_otg_bus_write(struct file *file, const char __user *ubuf,
2974 size_t count, loff_t *ppos)
2975{
2976 char buf[8];
2977 int ret;
2978 struct seq_file *s = file->private_data;
2979 struct msm_otg *motg = s->private;
2980
2981 memset(buf, 0x00, sizeof(buf));
2982
2983 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
2984 return -EFAULT;
2985
2986 if (!strncmp(buf, "enable", 6)) {
2987 /* Do not vote here. Let OTG statemachine decide when to vote */
2988 debug_bus_voting_enabled = true;
2989 } else {
2990 debug_bus_voting_enabled = false;
2991 if (motg->bus_perf_client) {
2992 ret = msm_bus_scale_client_update_request(
2993 motg->bus_perf_client, 0);
2994 if (ret)
Steve Mucklef132c6c2012-06-06 18:30:57 -07002995 dev_err(motg->phy.dev, "%s: Failed to devote "
Manu Gautam8bdcc592012-03-06 11:26:06 +05302996 "for bus bw %d\n", __func__, ret);
2997 }
2998 }
2999
3000 return count;
3001}
3002
3003const struct file_operations msm_otg_bus_fops = {
3004 .open = msm_otg_bus_open,
3005 .read = seq_read,
3006 .write = msm_otg_bus_write,
3007 .llseek = seq_lseek,
3008 .release = single_release,
3009};
3010
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303011static struct dentry *msm_otg_dbg_root;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303012
3013static int msm_otg_debugfs_init(struct msm_otg *motg)
3014{
Manu Gautam8bdcc592012-03-06 11:26:06 +05303015 struct dentry *msm_otg_dentry;
Anji jonnalad270e2d2011-08-09 11:28:32 +05303016
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303017 msm_otg_dbg_root = debugfs_create_dir("msm_otg", NULL);
3018
3019 if (!msm_otg_dbg_root || IS_ERR(msm_otg_dbg_root))
3020 return -ENODEV;
3021
Anji jonnalad270e2d2011-08-09 11:28:32 +05303022 if (motg->pdata->mode == USB_OTG &&
3023 motg->pdata->otg_control == OTG_USER_CONTROL) {
3024
Manu Gautam8bdcc592012-03-06 11:26:06 +05303025 msm_otg_dentry = debugfs_create_file("mode", S_IRUGO |
Anji jonnalad270e2d2011-08-09 11:28:32 +05303026 S_IWUSR, msm_otg_dbg_root, motg,
3027 &msm_otg_mode_fops);
3028
Manu Gautam8bdcc592012-03-06 11:26:06 +05303029 if (!msm_otg_dentry) {
Anji jonnalad270e2d2011-08-09 11:28:32 +05303030 debugfs_remove(msm_otg_dbg_root);
3031 msm_otg_dbg_root = NULL;
3032 return -ENODEV;
3033 }
3034 }
3035
Manu Gautam8bdcc592012-03-06 11:26:06 +05303036 msm_otg_dentry = debugfs_create_file("chg_type", S_IRUGO,
Anji jonnalad270e2d2011-08-09 11:28:32 +05303037 msm_otg_dbg_root, motg,
3038 &msm_otg_chg_fops);
3039
Manu Gautam8bdcc592012-03-06 11:26:06 +05303040 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303041 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303042 return -ENODEV;
3043 }
3044
Manu Gautam8bdcc592012-03-06 11:26:06 +05303045 msm_otg_dentry = debugfs_create_file("aca", S_IRUGO | S_IWUSR,
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303046 msm_otg_dbg_root, motg,
3047 &msm_otg_aca_fops);
3048
Manu Gautam8bdcc592012-03-06 11:26:06 +05303049 if (!msm_otg_dentry) {
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303050 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303051 return -ENODEV;
3052 }
3053
Manu Gautam8bdcc592012-03-06 11:26:06 +05303054 msm_otg_dentry = debugfs_create_file("bus_voting", S_IRUGO | S_IWUSR,
3055 msm_otg_dbg_root, motg,
3056 &msm_otg_bus_fops);
3057
3058 if (!msm_otg_dentry) {
3059 debugfs_remove_recursive(msm_otg_dbg_root);
3060 return -ENODEV;
3061 }
Chiranjeevi Velempatif9a11542012-03-28 18:18:34 +05303062
3063 msm_otg_dentry = debugfs_create_file("otg_state", S_IRUGO,
3064 msm_otg_dbg_root, motg, &msm_otg_state_fops);
3065
3066 if (!msm_otg_dentry) {
3067 debugfs_remove_recursive(msm_otg_dbg_root);
3068 return -ENODEV;
3069 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303070 return 0;
3071}
3072
3073static void msm_otg_debugfs_cleanup(void)
3074{
Anji jonnalad270e2d2011-08-09 11:28:32 +05303075 debugfs_remove_recursive(msm_otg_dbg_root);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303076}
3077
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303078static u64 msm_otg_dma_mask = DMA_BIT_MASK(64);
3079static struct platform_device *msm_otg_add_pdev(
3080 struct platform_device *ofdev, const char *name)
3081{
3082 struct platform_device *pdev;
3083 const struct resource *res = ofdev->resource;
3084 unsigned int num = ofdev->num_resources;
3085 int retval;
3086
3087 pdev = platform_device_alloc(name, -1);
3088 if (!pdev) {
3089 retval = -ENOMEM;
3090 goto error;
3091 }
3092
3093 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
3094 pdev->dev.dma_mask = &msm_otg_dma_mask;
3095
3096 if (num) {
3097 retval = platform_device_add_resources(pdev, res, num);
3098 if (retval)
3099 goto error;
3100 }
3101
3102 retval = platform_device_add(pdev);
3103 if (retval)
3104 goto error;
3105
3106 return pdev;
3107
3108error:
3109 platform_device_put(pdev);
3110 return ERR_PTR(retval);
3111}
3112
3113static int msm_otg_setup_devices(struct platform_device *ofdev,
3114 enum usb_mode_type mode, bool init)
3115{
3116 const char *gadget_name = "msm_hsusb";
3117 const char *host_name = "msm_hsusb_host";
3118 static struct platform_device *gadget_pdev;
3119 static struct platform_device *host_pdev;
3120 int retval = 0;
3121
3122 if (!init) {
3123 if (gadget_pdev)
3124 platform_device_unregister(gadget_pdev);
3125 if (host_pdev)
3126 platform_device_unregister(host_pdev);
3127 return 0;
3128 }
3129
3130 switch (mode) {
3131 case USB_OTG:
3132 /* fall through */
3133 case USB_PERIPHERAL:
3134 gadget_pdev = msm_otg_add_pdev(ofdev, gadget_name);
3135 if (IS_ERR(gadget_pdev)) {
3136 retval = PTR_ERR(gadget_pdev);
3137 break;
3138 }
3139 if (mode == USB_PERIPHERAL)
3140 break;
3141 /* fall through */
3142 case USB_HOST:
3143 host_pdev = msm_otg_add_pdev(ofdev, host_name);
3144 if (IS_ERR(host_pdev)) {
3145 retval = PTR_ERR(host_pdev);
3146 if (mode == USB_OTG)
3147 platform_device_unregister(gadget_pdev);
3148 }
3149 break;
3150 default:
3151 break;
3152 }
3153
3154 return retval;
3155}
3156
3157struct msm_otg_platform_data *msm_otg_dt_to_pdata(struct platform_device *pdev)
3158{
3159 struct device_node *node = pdev->dev.of_node;
3160 struct msm_otg_platform_data *pdata;
3161 int len = 0;
3162
3163 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
3164 if (!pdata) {
3165 pr_err("unable to allocate platform data\n");
3166 return NULL;
3167 }
3168 of_get_property(node, "qcom,hsusb-otg-phy-init-seq", &len);
3169 if (len) {
3170 pdata->phy_init_seq = devm_kzalloc(&pdev->dev, len, GFP_KERNEL);
3171 if (!pdata->phy_init_seq)
3172 return NULL;
3173 of_property_read_u32_array(node, "qcom,hsusb-otg-phy-init-seq",
3174 pdata->phy_init_seq,
3175 len/sizeof(*pdata->phy_init_seq));
3176 }
3177 of_property_read_u32(node, "qcom,hsusb-otg-power-budget",
3178 &pdata->power_budget);
3179 of_property_read_u32(node, "qcom,hsusb-otg-mode",
3180 &pdata->mode);
3181 of_property_read_u32(node, "qcom,hsusb-otg-otg-control",
3182 &pdata->otg_control);
3183 of_property_read_u32(node, "qcom,hsusb-otg-default-mode",
3184 &pdata->default_mode);
3185 of_property_read_u32(node, "qcom,hsusb-otg-phy-type",
3186 &pdata->phy_type);
3187 of_property_read_u32(node, "qcom,hsusb-otg-pmic-id-irq",
3188 &pdata->pmic_id_irq);
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303189 return pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303190}
3191
3192static int __init msm_otg_probe(struct platform_device *pdev)
3193{
3194 int ret = 0;
3195 struct resource *res;
3196 struct msm_otg *motg;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003197 struct usb_phy *phy;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303198 struct msm_otg_platform_data *pdata;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303199
3200 dev_info(&pdev->dev, "msm_otg probe\n");
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303201
3202 if (pdev->dev.of_node) {
3203 dev_dbg(&pdev->dev, "device tree enabled\n");
3204 pdata = msm_otg_dt_to_pdata(pdev);
3205 if (!pdata)
3206 return -ENOMEM;
3207 ret = msm_otg_setup_devices(pdev, pdata->mode, true);
3208 if (ret) {
3209 dev_err(&pdev->dev, "devices setup failed\n");
3210 return ret;
3211 }
3212 } else if (!pdev->dev.platform_data) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303213 dev_err(&pdev->dev, "No platform data given. Bailing out\n");
3214 return -ENODEV;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303215 } else {
3216 pdata = pdev->dev.platform_data;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303217 }
3218
3219 motg = kzalloc(sizeof(struct msm_otg), GFP_KERNEL);
3220 if (!motg) {
3221 dev_err(&pdev->dev, "unable to allocate msm_otg\n");
3222 return -ENOMEM;
3223 }
3224
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003225 motg->phy.otg = kzalloc(sizeof(struct usb_otg), GFP_KERNEL);
3226 if (!motg->phy.otg) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003227 dev_err(&pdev->dev, "unable to allocate usb_otg\n");
3228 ret = -ENOMEM;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303229 goto free_motg;
3230 }
3231
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003232 the_msm_otg = motg;
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303233 motg->pdata = pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003234 phy = &motg->phy;
3235 phy->dev = &pdev->dev;
Anji jonnala0f73cac2011-05-04 10:19:46 +05303236
3237 /*
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303238 * ACA ID_GND threshold range is overlapped with OTG ID_FLOAT. Hence
3239 * PHY treat ACA ID_GND as float and no interrupt is generated. But
3240 * PMIC can detect ACA ID_GND and generate an interrupt.
3241 */
3242 if (aca_enabled() && motg->pdata->otg_control != OTG_PMIC_CONTROL) {
3243 dev_err(&pdev->dev, "ACA can not be enabled without PMIC\n");
3244 ret = -EINVAL;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003245 goto free_otg;
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303246 }
3247
Ofir Cohen4da266f2012-01-03 10:19:29 +02003248 /* initialize reset counter */
3249 motg->reset_counter = 0;
3250
Amit Blay02eff132011-09-21 16:46:24 +03003251 /* Some targets don't support PHY clock. */
Manu Gautam5143b252012-01-05 19:25:23 -08003252 motg->phy_reset_clk = clk_get(&pdev->dev, "phy_clk");
Amit Blay02eff132011-09-21 16:46:24 +03003253 if (IS_ERR(motg->phy_reset_clk))
Manu Gautam5143b252012-01-05 19:25:23 -08003254 dev_err(&pdev->dev, "failed to get phy_clk\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303255
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303256 /*
3257 * Targets on which link uses asynchronous reset methodology,
3258 * free running clock is not required during the reset.
3259 */
Manu Gautam5143b252012-01-05 19:25:23 -08003260 motg->clk = clk_get(&pdev->dev, "alt_core_clk");
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303261 if (IS_ERR(motg->clk))
3262 dev_dbg(&pdev->dev, "alt_core_clk is not present\n");
3263 else
3264 clk_set_rate(motg->clk, 60000000);
Anji jonnala0f73cac2011-05-04 10:19:46 +05303265
3266 /*
Manu Gautam5143b252012-01-05 19:25:23 -08003267 * USB Core is running its protocol engine based on CORE CLK,
Anji jonnala0f73cac2011-05-04 10:19:46 +05303268 * CORE CLK must be running at >55Mhz for correct HSUSB
3269 * operation and USB core cannot tolerate frequency changes on
3270 * CORE CLK. For such USB cores, vote for maximum clk frequency
3271 * on pclk source
3272 */
Manu Gautam5143b252012-01-05 19:25:23 -08003273 motg->core_clk = clk_get(&pdev->dev, "core_clk");
3274 if (IS_ERR(motg->core_clk)) {
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303275 motg->core_clk = NULL;
Manu Gautam5143b252012-01-05 19:25:23 -08003276 dev_err(&pdev->dev, "failed to get core_clk\n");
Pavankumar Kondetibc541332012-04-20 15:32:04 +05303277 ret = PTR_ERR(motg->core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003278 goto put_clk;
3279 }
3280 clk_set_rate(motg->core_clk, INT_MAX);
3281
3282 motg->pclk = clk_get(&pdev->dev, "iface_clk");
3283 if (IS_ERR(motg->pclk)) {
3284 dev_err(&pdev->dev, "failed to get iface_clk\n");
3285 ret = PTR_ERR(motg->pclk);
3286 goto put_core_clk;
3287 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303288
3289 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3290 if (!res) {
3291 dev_err(&pdev->dev, "failed to get platform resource mem\n");
3292 ret = -ENODEV;
Manu Gautam5143b252012-01-05 19:25:23 -08003293 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303294 }
3295
3296 motg->regs = ioremap(res->start, resource_size(res));
3297 if (!motg->regs) {
3298 dev_err(&pdev->dev, "ioremap failed\n");
3299 ret = -ENOMEM;
Manu Gautam5143b252012-01-05 19:25:23 -08003300 goto put_pclk;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303301 }
3302 dev_info(&pdev->dev, "OTG regs = %p\n", motg->regs);
3303
3304 motg->irq = platform_get_irq(pdev, 0);
3305 if (!motg->irq) {
3306 dev_err(&pdev->dev, "platform_get_irq failed\n");
3307 ret = -ENODEV;
3308 goto free_regs;
3309 }
3310
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003311 motg->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, "usb");
Anji jonnala7da3f262011-12-02 17:22:14 -08003312 if (IS_ERR(motg->xo_handle)) {
3313 dev_err(&pdev->dev, "%s not able to get the handle "
3314 "to vote for TCXO D0 buffer\n", __func__);
3315 ret = PTR_ERR(motg->xo_handle);
3316 goto free_regs;
3317 }
Anji jonnala11aa5c42011-05-04 10:19:48 +05303318
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003319 ret = msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_ON);
Anji jonnala7da3f262011-12-02 17:22:14 -08003320 if (ret) {
3321 dev_err(&pdev->dev, "%s failed to vote for TCXO "
3322 "D0 buffer%d\n", __func__, ret);
3323 goto free_xo_handle;
3324 }
3325
Manu Gautam28b1bac2012-01-30 16:43:06 +05303326 clk_prepare_enable(motg->pclk);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303327
Mayank Rana248698c2012-04-19 00:03:16 +05303328 motg->vdd_type = VDDCX_CORNER;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003329 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "hsusb_vdd_dig");
Mayank Rana248698c2012-04-19 00:03:16 +05303330 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003331 hsusb_vddcx = devm_regulator_get(motg->phy.dev, "HSUSB_VDDCX");
Mayank Rana248698c2012-04-19 00:03:16 +05303332 if (IS_ERR(hsusb_vddcx)) {
Steve Mucklef132c6c2012-06-06 18:30:57 -07003333 dev_err(motg->phy.dev, "unable to get hsusb vddcx\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303334 goto devote_xo_handle;
3335 }
3336 motg->vdd_type = VDDCX;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303337 }
3338
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003339 ret = msm_hsusb_config_vddcx(1);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303340 if (ret) {
3341 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303342 goto devote_xo_handle;
3343 }
3344
3345 ret = regulator_enable(hsusb_vddcx);
3346 if (ret) {
3347 dev_err(&pdev->dev, "unable to enable the hsusb vddcx\n");
3348 goto free_config_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303349 }
3350
3351 ret = msm_hsusb_ldo_init(motg, 1);
3352 if (ret) {
3353 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
Mayank Rana248698c2012-04-19 00:03:16 +05303354 goto free_hsusb_vddcx;
Anji jonnala11aa5c42011-05-04 10:19:48 +05303355 }
3356
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303357 if (pdata->mhl_enable) {
Pavankumar Kondeti56dc7422012-07-02 12:45:19 +05303358 mhl_usb_hs_switch = devm_regulator_get(motg->phy.dev,
3359 "mhl_usb_hs_switch");
3360 if (IS_ERR(mhl_usb_hs_switch)) {
3361 dev_err(&pdev->dev, "Unable to get mhl_usb_hs_switch\n");
Mayank Rana9e9a2ac2012-03-24 04:05:28 +05303362 goto free_ldo_init;
3363 }
3364 }
3365
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003366 ret = msm_hsusb_ldo_enable(motg, 1);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303367 if (ret) {
3368 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003369 goto free_ldo_init;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303370 }
Manu Gautam28b1bac2012-01-30 16:43:06 +05303371 clk_prepare_enable(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303372
3373 writel(0, USB_USBINTR);
3374 writel(0, USB_OTGSC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003375 /* Ensure that above STOREs are completed before enabling interrupts */
3376 mb();
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303377
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003378 wake_lock_init(&motg->wlock, WAKE_LOCK_SUSPEND, "msm_otg");
Vijayavardhan Vennapusaa3152032012-03-05 16:29:30 +05303379 msm_otg_init_timer(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303380 INIT_WORK(&motg->sm_work, msm_otg_sm_work);
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303381 INIT_DELAYED_WORK(&motg->chg_work, msm_chg_detect_work);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303382 setup_timer(&motg->id_timer, msm_otg_id_timer_func,
3383 (unsigned long) motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303384 ret = request_irq(motg->irq, msm_otg_irq, IRQF_SHARED,
3385 "msm_otg", motg);
3386 if (ret) {
3387 dev_err(&pdev->dev, "request irq failed\n");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003388 goto destroy_wlock;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303389 }
3390
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003391 phy->init = msm_otg_reset;
3392 phy->set_power = msm_otg_set_power;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003393 phy->set_suspend = msm_otg_set_suspend;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303394
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003395 phy->io_ops = &msm_otg_io_ops;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303396
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003397 phy->otg->phy = &motg->phy;
3398 phy->otg->set_host = msm_otg_set_host;
3399 phy->otg->set_peripheral = msm_otg_set_peripheral;
Steve Mucklef132c6c2012-06-06 18:30:57 -07003400 phy->otg->start_hnp = msm_otg_start_hnp;
3401 phy->otg->start_srp = msm_otg_start_srp;
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003402
3403 ret = usb_set_transceiver(&motg->phy);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303404 if (ret) {
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003405 dev_err(&pdev->dev, "usb_set_transceiver failed\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303406 goto free_irq;
3407 }
3408
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303409 if (motg->pdata->mode == USB_OTG &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303410 motg->pdata->otg_control == OTG_PMIC_CONTROL) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003411 if (motg->pdata->pmic_id_irq) {
3412 ret = request_irq(motg->pdata->pmic_id_irq,
3413 msm_pmic_id_irq,
3414 IRQF_TRIGGER_RISING |
3415 IRQF_TRIGGER_FALLING,
3416 "msm_otg", motg);
3417 if (ret) {
3418 dev_err(&pdev->dev, "request irq failed for PMIC ID\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003419 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003420 }
3421 } else {
3422 ret = -ENODEV;
3423 dev_err(&pdev->dev, "PMIC IRQ for ID notifications doesn't exist\n");
Steve Mucklef132c6c2012-06-06 18:30:57 -07003424 goto remove_phy;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003425 }
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303426 }
3427
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303428 msm_hsusb_mhl_switch_enable(motg, 1);
3429
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303430 platform_set_drvdata(pdev, motg);
3431 device_init_wakeup(&pdev->dev, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003432 motg->mA_port = IUNIT;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303433
Anji jonnalad270e2d2011-08-09 11:28:32 +05303434 ret = msm_otg_debugfs_init(motg);
3435 if (ret)
3436 dev_dbg(&pdev->dev, "mode debugfs file is"
3437 "not available\n");
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303438
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003439 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3440 pm8921_charger_register_vbus_sn(&msm_otg_set_vbus_state);
3441
Amit Blay58b31472011-11-18 09:39:39 +02003442 if (motg->pdata->phy_type == SNPS_28NM_INTEGRATED_PHY) {
3443 if (motg->pdata->otg_control == OTG_PMIC_CONTROL &&
Pavankumar Kondeti0d81f312012-01-13 11:34:10 +05303444 (!(motg->pdata->mode == USB_OTG) ||
3445 motg->pdata->pmic_id_irq))
Amit Blay58b31472011-11-18 09:39:39 +02003446 motg->caps = ALLOW_PHY_POWER_COLLAPSE |
Vijayavardhan Vennapusa03171c72012-04-26 14:44:48 +05303447 ALLOW_PHY_RETENTION;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003448
Amit Blay58b31472011-11-18 09:39:39 +02003449 if (motg->pdata->otg_control == OTG_PHY_CONTROL)
3450 motg->caps = ALLOW_PHY_RETENTION;
3451 }
3452
Amit Blay6fa647a2012-05-24 14:12:08 +03003453 if (motg->pdata->enable_lpm_on_dev_suspend)
3454 motg->caps |= ALLOW_LPM_ON_DEV_SUSPEND;
3455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003456 wake_lock(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303457 pm_runtime_set_active(&pdev->dev);
3458 pm_runtime_enable(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303459
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303460 if (motg->pdata->bus_scale_table) {
3461 motg->bus_perf_client =
3462 msm_bus_scale_register_client(motg->pdata->bus_scale_table);
3463 if (!motg->bus_perf_client)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003464 dev_err(motg->phy.dev, "%s: Failed to register BUS "
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303465 "scaling client!!\n", __func__);
Manu Gautam8bdcc592012-03-06 11:26:06 +05303466 else
3467 debug_bus_voting_enabled = true;
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303468 }
3469
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303470 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003471
Steve Mucklef132c6c2012-06-06 18:30:57 -07003472remove_phy:
3473 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303474free_irq:
3475 free_irq(motg->irq, motg);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003476destroy_wlock:
3477 wake_lock_destroy(&motg->wlock);
Manu Gautam28b1bac2012-01-30 16:43:06 +05303478 clk_disable_unprepare(motg->core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003479 msm_hsusb_ldo_enable(motg, 0);
3480free_ldo_init:
Anji jonnala11aa5c42011-05-04 10:19:48 +05303481 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303482free_hsusb_vddcx:
3483 regulator_disable(hsusb_vddcx);
3484free_config_vddcx:
3485 regulator_set_voltage(hsusb_vddcx,
3486 vdd_val[motg->vdd_type][VDD_NONE],
3487 vdd_val[motg->vdd_type][VDD_MAX]);
Anji jonnala7da3f262011-12-02 17:22:14 -08003488devote_xo_handle:
Manu Gautam28b1bac2012-01-30 16:43:06 +05303489 clk_disable_unprepare(motg->pclk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003490 msm_xo_mode_vote(motg->xo_handle, MSM_XO_MODE_OFF);
Anji jonnala7da3f262011-12-02 17:22:14 -08003491free_xo_handle:
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003492 msm_xo_put(motg->xo_handle);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303493free_regs:
3494 iounmap(motg->regs);
Manu Gautam5143b252012-01-05 19:25:23 -08003495put_pclk:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303496 clk_put(motg->pclk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303497put_core_clk:
Manu Gautam5143b252012-01-05 19:25:23 -08003498 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303499put_clk:
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303500 if (!IS_ERR(motg->clk))
3501 clk_put(motg->clk);
Amit Blay02eff132011-09-21 16:46:24 +03003502 if (!IS_ERR(motg->phy_reset_clk))
3503 clk_put(motg->phy_reset_clk);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003504free_otg:
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003505 kfree(motg->phy.otg);
Pavankumar Kondetiaa449e12011-11-04 11:09:26 +05303506free_motg:
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303507 kfree(motg);
3508 return ret;
3509}
3510
3511static int __devexit msm_otg_remove(struct platform_device *pdev)
3512{
3513 struct msm_otg *motg = platform_get_drvdata(pdev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003514 struct usb_otg *otg = motg->phy.otg;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303515 int cnt = 0;
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303516
3517 if (otg->host || otg->gadget)
3518 return -EBUSY;
3519
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303520 if (pdev->dev.of_node)
3521 msm_otg_setup_devices(pdev, motg->pdata->mode, false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003522 if (motg->pdata->otg_control == OTG_PMIC_CONTROL)
3523 pm8921_charger_unregister_vbus_sn(0);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303524 msm_otg_debugfs_cleanup();
Pavankumar Kondetid8608522011-05-04 10:19:47 +05303525 cancel_delayed_work_sync(&motg->chg_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303526 cancel_work_sync(&motg->sm_work);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303527
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303528 pm_runtime_resume(&pdev->dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303529
3530 device_init_wakeup(&pdev->dev, 0);
3531 pm_runtime_disable(&pdev->dev);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003532 wake_lock_destroy(&motg->wlock);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303533
Vijayavardhan Vennapusafc464f02011-11-04 21:54:00 +05303534 msm_hsusb_mhl_switch_enable(motg, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003535 if (motg->pdata->pmic_id_irq)
3536 free_irq(motg->pdata->pmic_id_irq, motg);
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003537 usb_set_transceiver(NULL);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303538 free_irq(motg->irq, motg);
3539
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303540 /*
3541 * Put PHY in low power mode.
3542 */
Steve Mucklef132c6c2012-06-06 18:30:57 -07003543 ulpi_read(otg->phy, 0x14);
3544 ulpi_write(otg->phy, 0x08, 0x09);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303545
3546 writel(readl(USB_PORTSC) | PORTSC_PHCD, USB_PORTSC);
3547 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
3548 if (readl(USB_PORTSC) & PORTSC_PHCD)
3549 break;
3550 udelay(1);
3551 cnt++;
3552 }
3553 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC)
Steve Mucklef132c6c2012-06-06 18:30:57 -07003554 dev_err(otg->phy->dev, "Unable to suspend PHY\n");
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303555
Manu Gautam28b1bac2012-01-30 16:43:06 +05303556 clk_disable_unprepare(motg->pclk);
3557 clk_disable_unprepare(motg->core_clk);
Stephen Boyd30ad10b2012-03-01 14:51:04 -08003558 msm_xo_put(motg->xo_handle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07003559 msm_hsusb_ldo_enable(motg, 0);
Anji jonnala11aa5c42011-05-04 10:19:48 +05303560 msm_hsusb_ldo_init(motg, 0);
Mayank Rana248698c2012-04-19 00:03:16 +05303561 regulator_disable(hsusb_vddcx);
3562 regulator_set_voltage(hsusb_vddcx,
3563 vdd_val[motg->vdd_type][VDD_NONE],
3564 vdd_val[motg->vdd_type][VDD_MAX]);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303565
3566 iounmap(motg->regs);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303567 pm_runtime_set_suspended(&pdev->dev);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303568
Amit Blay02eff132011-09-21 16:46:24 +03003569 if (!IS_ERR(motg->phy_reset_clk))
3570 clk_put(motg->phy_reset_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303571 clk_put(motg->pclk);
Pavankumar Kondeti923262e2012-04-20 15:34:24 +05303572 if (!IS_ERR(motg->clk))
3573 clk_put(motg->clk);
Manu Gautam5143b252012-01-05 19:25:23 -08003574 clk_put(motg->core_clk);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303575
Manu Gautamcd82e9d2011-12-20 14:17:28 +05303576 if (motg->bus_perf_client)
3577 msm_bus_scale_unregister_client(motg->bus_perf_client);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303578
Heikki Krogerus1d4c9292012-02-13 13:24:09 +02003579 kfree(motg->phy.otg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303580 kfree(motg);
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303581 return 0;
3582}
3583
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303584#ifdef CONFIG_PM_RUNTIME
3585static int msm_otg_runtime_idle(struct device *dev)
3586{
3587 struct msm_otg *motg = dev_get_drvdata(dev);
Steve Mucklef132c6c2012-06-06 18:30:57 -07003588 struct usb_phy *phy = &motg->phy;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303589
3590 dev_dbg(dev, "OTG runtime idle\n");
3591
Steve Mucklef132c6c2012-06-06 18:30:57 -07003592 if (phy->state == OTG_STATE_UNDEFINED)
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303593 return -EAGAIN;
3594 else
3595 return 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303596}
3597
3598static int msm_otg_runtime_suspend(struct device *dev)
3599{
3600 struct msm_otg *motg = dev_get_drvdata(dev);
3601
3602 dev_dbg(dev, "OTG runtime suspend\n");
3603 return msm_otg_suspend(motg);
3604}
3605
3606static int msm_otg_runtime_resume(struct device *dev)
3607{
3608 struct msm_otg *motg = dev_get_drvdata(dev);
3609
3610 dev_dbg(dev, "OTG runtime resume\n");
Pavankumar Kondeti8be99cf2011-08-04 10:48:08 +05303611 pm_runtime_get_noresume(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303612 return msm_otg_resume(motg);
3613}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303614#endif
3615
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303616#ifdef CONFIG_PM_SLEEP
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303617static int msm_otg_pm_suspend(struct device *dev)
3618{
Jack Pham5ca279b2012-05-14 18:42:54 -07003619 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303620 struct msm_otg *motg = dev_get_drvdata(dev);
3621
3622 dev_dbg(dev, "OTG PM suspend\n");
Jack Pham5ca279b2012-05-14 18:42:54 -07003623
3624 atomic_set(&motg->pm_suspended, 1);
3625 ret = msm_otg_suspend(motg);
3626 if (ret)
3627 atomic_set(&motg->pm_suspended, 0);
3628
3629 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303630}
3631
3632static int msm_otg_pm_resume(struct device *dev)
3633{
Jack Pham5ca279b2012-05-14 18:42:54 -07003634 int ret = 0;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303635 struct msm_otg *motg = dev_get_drvdata(dev);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303636
3637 dev_dbg(dev, "OTG PM resume\n");
3638
Jack Pham5ca279b2012-05-14 18:42:54 -07003639 atomic_set(&motg->pm_suspended, 0);
3640 if (motg->sm_work_pending) {
3641 motg->sm_work_pending = false;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303642
Jack Pham5ca279b2012-05-14 18:42:54 -07003643 pm_runtime_get_noresume(dev);
3644 ret = msm_otg_resume(motg);
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303645
Jack Pham5ca279b2012-05-14 18:42:54 -07003646 /* Update runtime PM status */
3647 pm_runtime_disable(dev);
3648 pm_runtime_set_active(dev);
3649 pm_runtime_enable(dev);
3650
3651 queue_work(system_nrt_wq, &motg->sm_work);
3652 }
3653
3654 return ret;
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303655}
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303656#endif
3657
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303658#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303659static const struct dev_pm_ops msm_otg_dev_pm_ops = {
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303660 SET_SYSTEM_SLEEP_PM_OPS(msm_otg_pm_suspend, msm_otg_pm_resume)
3661 SET_RUNTIME_PM_OPS(msm_otg_runtime_suspend, msm_otg_runtime_resume,
3662 msm_otg_runtime_idle)
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303663};
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303664#endif
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303665
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303666static struct of_device_id msm_otg_dt_match[] = {
3667 { .compatible = "qcom,hsusb-otg",
3668 },
3669 {}
3670};
3671
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303672static struct platform_driver msm_otg_driver = {
3673 .remove = __devexit_p(msm_otg_remove),
3674 .driver = {
3675 .name = DRIVER_NAME,
3676 .owner = THIS_MODULE,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303677#ifdef CONFIG_PM
Pavankumar Kondeti87c01042010-12-07 17:53:58 +05303678 .pm = &msm_otg_dev_pm_ops,
Pavankumar Kondeti70187732011-02-15 09:42:34 +05303679#endif
Pavankumar Kondetieaea7fe2011-10-27 14:46:45 +05303680 .of_match_table = msm_otg_dt_match,
Pavankumar Kondetie0c201f2010-12-07 17:53:55 +05303681 },
3682};
3683
3684static int __init msm_otg_init(void)
3685{
3686 return platform_driver_probe(&msm_otg_driver, msm_otg_probe);
3687}
3688
3689static void __exit msm_otg_exit(void)
3690{
3691 platform_driver_unregister(&msm_otg_driver);
3692}
3693
3694module_init(msm_otg_init);
3695module_exit(msm_otg_exit);
3696
3697MODULE_LICENSE("GPL v2");
3698MODULE_DESCRIPTION("MSM USB transceiver driver");